Debian Codegraph @

tests/amr/amr_test.c

raw symbols SCIP
1 /* (C) 2017 by sysmocom - s.f.m.c. GmbH <[email protected]>
2 *
3 * Author: Pau Espin Pedrol <[email protected]>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <unistd.h>
17 #include <stdint.h>
18 #include <inttypes.h>
19 #include <string.h>
20 #include <signal.h>
21 #include <arpa/inet.h>
22 #include <sys/time.h>
23 #include <osmocom/core/utils.h>
24
25 #include <osmocom/netif/amr.h>
26
27 /* Some octet aligned single-payload AMR frames on different rates */
28 char *oa_amr_samples[] = {
29 "703c22f979890338540179209572624a0f8535871c2f7039cbf926b7e4425b6ef0",
30 "703c2e671f3b1b0810412d5adae61e2b2a319885c6ced4e909b4eeaa2ea0f0cd80",
31 "703cf8fc77356c948141686cda34d35220db719e36a359d86b64420dc64b563850",
32 "703c3eec9c37dfb201c093c57a1235a02af55ccc22f1c9593a6e058c368b4d7f50",
33 "60344e300c0e6251342c2ae51fd8a698a945488d16c98922726f3e50",
34 "60341fc722c7880328a9c280030bc9755c3ef519f80000295323e000",
35 "60342c338655c00008efba03592419adf62478a79278b3e2d68ab0f0",
36 "502c98ab841e491ff7a1a555016a32a3c7f913210630",
37 "502cc5459a0d200e7097c4dfe86ec8d27f1756d776f0",
38 "502c42b332081813d7e916e7aa5e80d7fde812b8c080",
39 "40240343e959c79bacd20c77501054880a718db200",
40 "4024172c53401e39115ceecd12606df5689bdd0ca0",
41 "4024f871cf48801ec427f0fc3f7318898622062200",
42 "20141fd4c02667c742b164aef659ffe708",
43 "2014197e10ead7b250bccbbf3b81887c64",
44 "2014e959f35fdfe5e9667ffbc088818088",
45 "100c4e9ba850e30d5d53d04de41e7c",
46 "100c6c18e7b7fff53aeb055e7d1c54",
47 "100c1fb967f7f1fdf547bf2e61c060",
48 "0004f89d67f1160935bde1996840",
49 "0004633cc7f0630439ffe0000000",
50 "0004eb81fc0758973b9edc782550",
51 "a070ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00fc", /* sample with invalid FT=14, will be detected as bandwith-efficient */
52 "a078", /* sample with FT=15 NO_DATA */
53 "END",
54 };
55
56 /* Some bandwith efficient single-payload AMR frames */
57 char *bwe_amr_samples[] = {
58 "f4495c7cda8f80",
59 "f44aaa6c969780",
60 "7449c1e32c6780",
61 "74400000000380",
62 "703c493323b0bf68028086d15a00",
63 "703c59171697c53c01801fb6aa80",
64 "703c55029bb2e23a0020172e8b00",
65 "703c48f52442f08884a8050b8500",
66 "7038000000000000000000000000",
67 "7151434e01292c40c4735e4412204077",
68 "714eadbe13ac4694f4726f71985d64cd",
69 "f3d09c20e32da600c025a72e0a9b360386e40f87e19282094adc1a11e397d1d4",
70 "f3d39a49a09e7a802852e297e8c9246aadf5a45928bfc27177fed8404d97d3b8",
71 "f3c2155b65131c68682079fab4810911200003b360ae0446000025f11e539dd0",
72 "f3c381bc7061c9f8507f6029de6115c16e5fa470c243b21b6e35dbb48bd84c00",
73 "73c901b7a2004be7f85284b6ab7142acfe6872b1ae1c107d0588b551de7be650",
74 "a7bfc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03f", /* sample with invalid FT */
75 "4780", /* FT=15 NO_DATA */
76 "END",
77 };
78
79 /* Some IuUP payload containg AMR */
80 char *iuup_amr_samples[] = {
81 "7a687b8b29ba02013d3cb863a5af4206869618fb5336d504968d7541663210",
82 "4482803977dc1880b56aab097728a6bbbb48a57e3affe41125838a5ce65fc0",
83 "2683c1d26e",
84 "END",
85 };
86
87
88 void dump_bits(uint8_t *buf, int len)
89 {
90 unsigned int i;
91 if (len <= 0) {
92 printf("(no data)");
93 return;
94 }
95 for (i = 0; i < (len * 8); i++)
96 printf("%u", (buf[i / 8] >> (7 - (i % 8))) & 1);
97 return;
98 }
99
100 void osmo_amr_oa_to_bwe_test(void)
101 {
102 uint8_t buf[256];
103 unsigned int i = 0;
104 int len;
105 int rc;
106
107 printf("\n\n");
108 printf("Testing conversion from octet-aligned to bw-efficient:\n");
109
110 while (1) {
111 if (strcmp(oa_amr_samples[i], "END") == 0)
112 return;
113 printf("\n");
114 printf("Sample No.: %i\n", i);
115
116 len = osmo_hexparse(oa_amr_samples[i], buf, sizeof(buf));
117 OSMO_ASSERT(len > 0);
118
119 printf(" octet aligned: %s\n", osmo_hexdump_nospc(buf, len));
120 printf(" ");
121 dump_bits(buf, len);
122 printf("\n");
123 rc = osmo_amr_oa_to_bwe(buf, len);
124 printf(" bw-efficient: %s\n", osmo_hexdump_nospc(buf, rc));
125 printf(" ");
126 dump_bits(buf, rc);
127 printf("\n");
128 printf(" rc: %i\n", rc);
129 i++;
130 }
131 }
132
133 void osmo_amr_bwe_to_oa_test(void)
134 {
135 uint8_t buf[256];
136 unsigned int i = 0;
137 int len;
138 int rc;
139
140 printf("\n\n");
141 printf("Testing conversion from bw-efficient to octet-aligned:\n");
142
143 while (1) {
144 if (strcmp(bwe_amr_samples[i], "END") == 0)
145 return;
146 printf("\n");
147 printf("Sample No.: %i\n", i);
148
149 len = osmo_hexparse(bwe_amr_samples[i], buf, sizeof(buf));
150 OSMO_ASSERT(len > 0);
151
152 printf(" bw-efficient: %s\n", osmo_hexdump_nospc(buf, len));
153 printf(" ");
154 dump_bits(buf, len);
155 printf("\n");
156 rc = osmo_amr_bwe_to_oa(buf, len, sizeof(buf));
157 printf(" octet aligned: %s\n", osmo_hexdump_nospc(buf, rc));
158 printf(" ");
159 dump_bits(buf, rc);
160 printf("\n");
161 printf(" rc: %i\n", rc);
162
163 i++;
164 }
165 }
166
167 void osmo_amr_oa_to_bwe_and_inverse_test(void)
168 {
169 uint8_t buf[256];
170 uint8_t buf_chk[256];
171 struct amr_hdr *oa_hd = (struct amr_hdr *)buf;
172 unsigned int ft;
173
174 unsigned int i = 0;
175 int len;
176 int rc;
177
178 printf("\n\n");
179 printf
180 ("Testing conversion from octet-aligned to bw-efficient and inverse:\n");
181
182 while (1) {
183 if (strcmp(oa_amr_samples[i], "END") == 0)
184 return;
185 printf("Sample No.: %i...", i);
186
187 len = osmo_hexparse(oa_amr_samples[i], buf, sizeof(buf));
188 OSMO_ASSERT(len > 0);
189 i++;
190
191 ft = oa_hd->ft;
192 if (!osmo_amr_ft_valid(ft)) {
193 printf(" skipping a sample with a wrong FT\n");
194 continue;
195 }
196 OSMO_ASSERT(osmo_amr_bytes(ft) + 2 == len);
197 printf(" AMR mode: %d, OA: %d bytes,", ft, len);
198 memcpy(buf_chk, buf, sizeof(buf));
199
200 rc = osmo_amr_oa_to_bwe(buf, len);
201 OSMO_ASSERT(rc > 0);
202 printf(" BE: %d bytes,", rc);
203 rc = osmo_amr_bwe_to_oa(buf, rc, sizeof(buf));
204 printf(" OA: %d bytes\n", rc);
205 OSMO_ASSERT(len == rc);
206 OSMO_ASSERT(memcmp(buf, buf_chk, len) == 0);
207 }
208 }
209
210 void osmo_amr_iuup_to_bwe_and_inverse_test(void)
211 {
212 uint8_t buf[256];
213 uint8_t buf_chk[256];
214 int ft;
215
216 unsigned int i = 0;
217 int len;
218 int rc;
219
220 printf("\n\n");
221 printf
222 ("Testing conversion from IuUP to bw-efficient and inverse:\n");
223
224 while (1) {
225 if (strcmp(iuup_amr_samples[i], "END") == 0)
226 return;
227 printf("Sample No.: %d...", i);
228
229 len = osmo_hexparse(iuup_amr_samples[i], buf, sizeof(buf));
230 OSMO_ASSERT(len > 0);
231 i++;
232
233 ft = osmo_amr_bytes_to_ft(len);
234 if (ft < 0) {
235 printf(" skipping a sample with a wrong FT\n");
236 continue;
237 }
238 printf(" AMR mode: %d, IuUP: %d bytes,", ft, len);
239 memcpy(buf_chk, buf, sizeof(buf));
240
241 rc = osmo_amr_iuup_to_bwe(buf, len, sizeof(buf));
242 OSMO_ASSERT(rc > 0);
243 OSMO_ASSERT((rc == len + 1) || (rc == len + 2));
244 printf(" BE: %d bytes (%s),", rc, osmo_hexdump(buf, rc));
245
246 buf[0] = (ft >> 1) & 0x07;
247 buf[1] = ((ft & 0x01) << 7) | (buf[1] & 0x3f);
248 rc = osmo_amr_bwe_to_iuup(buf, rc);
249 printf(" IuUP: %d bytes\n", rc);
250 OSMO_ASSERT(rc > 0);
251 OSMO_ASSERT(len == rc);
252 if (memcmp(buf, buf_chk, len) != 0) {
253 printf("Got: %s\n", osmo_hexdump(buf, len));
254 printf("Expected:%s\n", osmo_hexdump(buf_chk, len));
255 }
256 OSMO_ASSERT(memcmp(buf, buf_chk, len) == 0);
257 }
258 }
259
260 void osmo_amr_is_oa_test(void)
261 {
262 uint8_t buf[256];
263 unsigned int i;
264 int len;
265 bool is_oc;
266
267 printf("\n\n");
268 printf("Testing detection of octet-aligned mode payloads:\n");
269
270 i = 0;
271 while (1) {
272 if (strcmp(oa_amr_samples[i], "END") == 0)
273 break;
274 printf("Sample No.: %i ==>", i);
275 len = strlen(oa_amr_samples[i]);
276
277 len = osmo_hexparse(oa_amr_samples[i], buf, sizeof(buf));
278 OSMO_ASSERT(len > 0);
279
280 is_oc = osmo_amr_is_oa(buf, len);
281 if (is_oc)
282 printf("octet aligned\n");
283 else
284 printf("bandwith efficient\n");
285
286 i++;
287 }
288
289 i = 0;
290 while (1) {
291 if (strcmp(bwe_amr_samples[i], "END") == 0)
292 break;
293 printf("Sample No.: %i ==>", i);
294 len = strlen(oa_amr_samples[i]);
295
296 len = osmo_hexparse(bwe_amr_samples[i], buf, sizeof(buf));
297 OSMO_ASSERT(len > 0);
298
299 is_oc = osmo_amr_is_oa(buf, len);
300 if (is_oc)
301 printf("octet aligned\n");
302 else
303 printf("bandwith efficient\n");
304
305 i++;
306 }
307 }
308
309 int main(int argc, char **argv)
310 {
311 osmo_amr_oa_to_bwe_test();
312 osmo_amr_bwe_to_oa_test();
313 osmo_amr_oa_to_bwe_and_inverse_test();
314 osmo_amr_iuup_to_bwe_and_inverse_test();
315 osmo_amr_is_oa_test();
316
317 fprintf(stdout, "OK: Test passed\n");
318 return EXIT_SUCCESS;
319 }