blob: f944f38bfe010febb63e1a384c74d17228629b6d [file] [log] [blame]
Max92db1502016-05-25 18:13:51 +02001/*
2 * (C) 2016 by Sysmocom s.f.m.c. GmbH
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Maxec8f1922016-05-31 14:50:21 +020023#include <stdbool.h>
Max92db1502016-05-25 18:13:51 +020024
25#include <osmocom/core/utils.h>
26#include <osmocom/codec/codec.h>
27
28const uint8_t sid_update[] = {0x20, 0x44, 0x29, 0xc2, 0x92, 0x91, 0xf4};
29const uint8_t sid_first[] = {0x20, 0x44, 0x00, 0x00, 0x00, 0x00, 0x04};
30
31#define PAYLOAD_LEN 34
32#define SID_LEN 7
33
34static const char * cmpr(int a, int b)
35{
36 return (a == b) ? "OK" : "BAD";
37}
38
39static void test_sid_dec(const uint8_t *t, size_t len)
40{
Maxe9e5f8e2016-11-07 14:49:13 +010041 uint8_t cmr, tmp[SID_LEN], *t2 = NULL;
Max92db1502016-05-25 18:13:51 +020042 enum osmo_amr_type ft;
43 enum osmo_amr_quality bfi;
44 int8_t sti, cmi;
Maxe9e5f8e2016-11-07 14:49:13 +010045 if (t) {
46 memcpy(tmp, t, SID_LEN);
47 t2 = tmp;
48 }
49 int rc = osmo_amr_rtp_dec(t2, len, &cmr, &cmi, &ft, &bfi, &sti);
50 if (rc < 0)
51 return;
Max92db1502016-05-25 18:13:51 +020052 printf("[%d] decode RTP %s%s: FT %s, CMR %s, CMI is %d, SID type %s\t",
53 rc, osmo_hexdump(tmp, len), cmpr(bfi, AMR_GOOD),
54 get_value_string(osmo_amr_type_names, ft),
55 get_value_string(osmo_amr_type_names, cmr),
56 cmi, sti ? "UPDATE" : "FIRST");
57 if (sti == -1)
58 printf("FAIL: incompatible STI for SID\n");
59 rc = osmo_amr_rtp_enc(tmp, cmr, ft, bfi);
60 printf("[%d] encode [%d]\n", rc, memcmp(tmp, t, SID_LEN));
61}
62
63static void test_amr_rt(uint8_t _cmr, enum osmo_amr_type _ft,
64 enum osmo_amr_quality _bfi)
65{
66 uint8_t cmr, payload[PAYLOAD_LEN];
67 enum osmo_amr_type ft;
68 enum osmo_amr_quality bfi;
69 int8_t sti, cmi;
70 int rc, re = osmo_amr_rtp_enc(payload, _cmr, _ft, _bfi);
71 rc = osmo_amr_rtp_dec(payload, PAYLOAD_LEN, &cmr, &cmi, &ft, &bfi, &sti);
72 printf("[%d/%d] %s, CMR: %s, FT: %s, BFI: %s, CMI: %d, STI: %d\n", re,
73 rc, get_value_string(osmo_amr_type_names, ft),
74 cmpr(_cmr, cmr), cmpr(_ft, ft), cmpr(_bfi, bfi), cmi, sti);
75}
76
Maxec8f1922016-05-31 14:50:21 +020077uint8_t fr[] = {0xd8, 0xa9, 0xb5, 0x1d, 0xda, 0xa8, 0x82, 0xcc, 0xec, 0x52,
78 0x29, 0x05, 0xa8, 0xc3, 0xe3, 0x0e, 0xb0, 0x89, 0x7a, 0xee,
79 0x42, 0xca, 0xc4, 0x97, 0x22, 0xe6, 0x9e, 0xa8, 0xb8, 0xec,
80 0x52, 0x26, 0xbd};
81uint8_t sid_fr[] = {0xd7, 0x27, 0x93, 0xe5, 0xe3, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
85
86uint8_t hr[] = {0x06, 0x46, 0x76, 0xb1, 0x8e, 0x48, 0x9a, 0x2f, 0x5e, 0x4c,
87 0x22, 0x2b, 0x62, 0x25};
88uint8_t sid_hr[] = {0x03, 0x8e, 0xb6, 0xcb, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff, 0xff, 0xff, 0xff};
90
91static void test_sid_xr(uint8_t *t, size_t len, bool hr)
92{
93 printf("%s SID ? %s:: %d\n", hr ? "HR" : "FR", osmo_hexdump(t, len),
94 hr ? osmo_hr_check_sid(t, len) : osmo_fr_check_sid(t, len));
95}
96
Max92db1502016-05-25 18:13:51 +020097int main(int argc, char **argv)
98{
99 printf("AMR RTP payload decoder test:\n");
100 test_sid_dec(sid_first, 7);
101 test_sid_dec(sid_update, 7);
Maxe9e5f8e2016-11-07 14:49:13 +0100102 test_sid_dec(NULL, 7);
Max92db1502016-05-25 18:13:51 +0200103 test_amr_rt(0, AMR_NO_DATA, AMR_BAD);
104 test_amr_rt(0, AMR_NO_DATA, AMR_GOOD);
105 test_amr_rt(AMR_12_2, AMR_12_2, AMR_BAD);
106 test_amr_rt(AMR_12_2, AMR_12_2, AMR_GOOD);
107 test_amr_rt(AMR_7_40, AMR_7_40, AMR_BAD);
108 test_amr_rt(AMR_7_40, AMR_7_40, AMR_GOOD);
Maxec8f1922016-05-31 14:50:21 +0200109 printf("FR RTP payload SID test:\n");
110 test_sid_xr(sid_fr, 33, false);
111 test_sid_xr(fr, 33, false);
112
113 printf("HR RTP payload SID test:\n");
114 test_sid_xr(sid_hr, 14, true);
115 test_sid_xr(hr, 14, true);
Max92db1502016-05-25 18:13:51 +0200116
117 return 0;
118}
119
120