blob: 0c7b5cecb58bd7e26c85bfadc9763b4eea933e54 [file] [log] [blame]
Holger Freytheraa0fb362008-12-28 21:55:40 +00001/* simple test for the gsm0408 formatting functions */
2/*
3 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Holger Freytheraa0fb362008-12-28 21:55:40 +00009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Freytheraa0fb362008-12-28 21:55:40 +000018 *
19 */
20
21#include <assert.h>
22#include <stdio.h>
23#include <stdlib.h>
Max69e9c0d2016-05-18 13:04:47 +020024#include <stdbool.h>
Harald Welteafedeab2010-03-04 10:55:40 +010025#include <arpa/inet.h>
26
Neels Hofmeyr2d521a02016-05-14 00:57:04 +020027#include <openbsc/common_bsc.h>
Holger Freytheraa0fb362008-12-28 21:55:40 +000028#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +010029#include <openbsc/gsm_04_11.h>
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020030#include <openbsc/gsm_subscriber.h>
31#include <openbsc/debug.h>
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010032#include <openbsc/arfcn_range_encode.h>
Max59a1bf32016-04-15 16:04:46 +020033#include <openbsc/system_information.h>
34#include <openbsc/abis_rsl.h>
35
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010036#include <osmocom/core/application.h>
Max59a1bf32016-04-15 16:04:46 +020037#include <osmocom/gsm/sysinfo.h>
Holger Freytheraa0fb362008-12-28 21:55:40 +000038
39#define COMPARE(result, op, value) \
40 if (!((result) op (value))) {\
41 fprintf(stderr, "Compare failed. Was %x should be %x in %s:%d\n",result, value, __FILE__, __LINE__); \
42 exit(-1); \
43 }
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +020044
45#define COMPARE_STR(result, value) \
46 if (strcmp(result, value) != 0) { \
47 fprintf(stderr, "Compare failed. Was %s should be %s in %s:%d\n",result, value, __FILE__, __LINE__); \
48 exit(-1); \
49 }
Holger Freytheraa0fb362008-12-28 21:55:40 +000050
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +010051#define DBG(...)
52
53#define VERIFY(res, cmp, wanted) \
54 if (!(res cmp wanted)) { \
55 printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \
Holger Hans Peter Freytherdaaea0c2015-08-03 09:28:41 +020056 __FILE__, __LINE__, (int) res, # cmp, (int) wanted); \
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +010057 }
58
59
60
Holger Freytheraa0fb362008-12-28 21:55:40 +000061/*
62 * Test Location Area Identifier formatting. Table 10.5.3 of 04.08
63 */
64static void test_location_area_identifier(void)
65{
66 struct gsm48_loc_area_id lai48;
67
68 printf("Testing test location area identifier\n");
69
70 /*
71 * Test the default/test setup. Coming from
72 * bsc_hack.c dumps
73 */
Harald Welteafedeab2010-03-04 10:55:40 +010074 gsm48_generate_lai(&lai48, 1, 1, 1);
Holger Freytheraa0fb362008-12-28 21:55:40 +000075 COMPARE(lai48.digits[0], ==, 0x00);
76 COMPARE(lai48.digits[1], ==, 0xF1);
77 COMPARE(lai48.digits[2], ==, 0x10);
78 COMPARE(lai48.lac, ==, htons(0x0001));
79
Harald Welteafedeab2010-03-04 10:55:40 +010080 gsm48_generate_lai(&lai48, 602, 1, 15);
Holger Freytheraa0fb362008-12-28 21:55:40 +000081 COMPARE(lai48.digits[0], ==, 0x06);
82 COMPARE(lai48.digits[1], ==, 0xF2);
83 COMPARE(lai48.digits[2], ==, 0x10);
84 COMPARE(lai48.lac, ==, htons(0x000f));
85}
86
Max59a1bf32016-04-15 16:04:46 +020087static inline void add_arfcn_b(struct osmo_earfcn_si2q *e, uint16_t earfcn,
88 uint8_t bw)
89{
90 int r = osmo_earfcn_add(e, earfcn, bw);
91 if (r)
92 printf("failed to add EARFCN %u: %s\n", earfcn, strerror(r));
93 else
94 printf("added EARFCN %u - ", earfcn);
95}
96
97static inline void gen(struct gsm_bts *bts)
98{
Max69e9c0d2016-05-18 13:04:47 +020099 bts->si_valid = 0;
100 bts->si_valid |= (1 << SYSINFO_TYPE_2quater);
101 /* should be no-op as entire buffer is filled with padding: */
102 memset(bts->si_buf[SYSINFO_TYPE_2quater], 0xAE, GSM_MACBLOCK_LEN);
Max59a1bf32016-04-15 16:04:46 +0200103 int r = gsm_generate_si(bts, SYSINFO_TYPE_2quater);
Max69e9c0d2016-05-18 13:04:47 +0200104 bool v = bts->si_valid & (1 << SYSINFO_TYPE_2quater);
Max59a1bf32016-04-15 16:04:46 +0200105 if (r > 0)
Max69e9c0d2016-05-18 13:04:47 +0200106 printf("generated %s SI2quater: [%d] %s\n",
107 v ? "valid" : "invalid", r,
Max59a1bf32016-04-15 16:04:46 +0200108 osmo_hexdump(bts->si_buf[SYSINFO_TYPE_2quater], r));
109 else
110 printf("failed to generate SI2quater: %s\n", strerror(-r));
111}
112
Maxaafff962016-04-20 15:57:14 +0200113static inline void _bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn,
114 uint16_t scramble, bool diversity)
115{
116 int r = bts_uarfcn_add(bts, arfcn, scramble, diversity);
117 if (r < 0)
118 printf("failed to add UARFCN to SI2quater: %s\n", strerror(-r));
119 else
120 gen(bts);
121}
122
Max26679e02016-04-20 15:57:13 +0200123static inline void test_si2q_u(void)
Max59a1bf32016-04-15 16:04:46 +0200124{
125 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200126 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200127 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
128
129 if (!network)
130 exit(1);
131 bts = gsm_bts_alloc(network);
132
Max69e9c0d2016-05-18 13:04:47 +0200133 /* first generate invalid SI as no UARFCN added */
134 gen(bts);
135 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200136 _bts_uarfcn_add(bts, 1982, 13, 1);
137 _bts_uarfcn_add(bts, 1982, 44, 0);
138 _bts_uarfcn_add(bts, 1982, 61, 1);
139 _bts_uarfcn_add(bts, 1982, 89, 1);
140 _bts_uarfcn_add(bts, 1982, 113, 0);
141 _bts_uarfcn_add(bts, 1982, 123, 0);
142 _bts_uarfcn_add(bts, 1982, 56, 1);
143 _bts_uarfcn_add(bts, 1982, 72, 1);
144 _bts_uarfcn_add(bts, 1982, 223, 1);
145 _bts_uarfcn_add(bts, 1982, 14, 0);
146 _bts_uarfcn_add(bts, 1982, 88, 0);
Max26679e02016-04-20 15:57:13 +0200147 gen(bts);
148}
149
150static inline void test_si2q_e(void)
151{
152 struct gsm_bts *bts;
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200153 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200154 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200155
156 if (!network)
157 exit(1);
158 bts = gsm_bts_alloc(network);
159
160 bts->si_common.si2quater_neigh_list.arfcn =
161 bts->si_common.data.earfcn_list;
162 bts->si_common.si2quater_neigh_list.meas_bw =
163 bts->si_common.data.meas_bw_list;
164 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
165 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
166
167 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max69e9c0d2016-05-18 13:04:47 +0200168 /* first generate invalid SI as no EARFCN added */
169 gen(bts);
170 /* subsequent calls should produce valid SI if there's enough memory */
Max59a1bf32016-04-15 16:04:46 +0200171 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1917, 1);
172 gen(bts);
173
174 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1932,
175 OSMO_EARFCN_MEAS_INVALID);
176 gen(bts);
177
178 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1937, 2);
179 gen(bts);
180
181 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1945,
182 OSMO_EARFCN_MEAS_INVALID);
183 gen(bts);
184
185 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1965,
186 OSMO_EARFCN_MEAS_INVALID);
187 gen(bts);
188
189 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1967, 4);
190 gen(bts);
191
192 add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1982, 3);
193 gen(bts);
194}
195
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200196static void test_mi_functionality(void)
197{
198 const char *imsi_odd = "987654321098763";
199 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200200 const uint32_t tmsi = 0xfabeacd0;
201 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200202 unsigned int mi_len;
203 char mi_parsed[GSM48_MI_SIZE];
204
205 printf("Testing parsing and generating TMSI/IMSI\n");
206
207 /* tmsi code */
208 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
209 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100210 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200211
212 /* imsi code */
213 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
214 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200215 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200216 COMPARE_STR(mi_parsed, imsi_odd);
217
218 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
219 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200220 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200221 COMPARE_STR(mi_parsed, imsi_even);
222}
223
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100224struct {
225 int range;
226 int arfcns_num;
227 int arfcns[RANGE_ENC_MAX_ARFCNS];
228} arfcn_test_ranges[] = {
229 {ARFCN_RANGE_512, 12,
230 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
231 {ARFCN_RANGE_512, 17,
232 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
233 {ARFCN_RANGE_512, 18,
234 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
235 {ARFCN_RANGE_512, 18,
236 { 1, 17, 31, 45, 58, 79, 81, 97,
237 113, 127, 213, 277, 287, 311, 331, 391,
238 417, 511 }},
239 {ARFCN_RANGE_512, 6,
240 { 1, 17, 31, 45, 58, 79 }},
241 {ARFCN_RANGE_512, 6,
242 { 10, 17, 31, 45, 58, 79 }},
243 {ARFCN_RANGE_1024, 17,
244 { 0, 17, 31, 45, 58, 79, 81, 97,
245 113, 127, 213, 277, 287, 311, 331, 391,
246 1023 }},
247 {ARFCN_RANGE_1024, 16,
248 { 17, 31, 45, 58, 79, 81, 97, 113,
249 127, 213, 277, 287, 311, 331, 391, 1023 }},
250 {-1}
251};
252
253static int test_single_range_encoding(int range, const int *orig_arfcns,
254 int arfcns_num, int silent)
255{
256 int arfcns[RANGE_ENC_MAX_ARFCNS];
257 int w[RANGE_ENC_MAX_ARFCNS];
258 int f0_included = 0;
259 int rc, f0;
260 uint8_t chan_list[16] = {0};
261 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
262 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
263 int dec_arfcns_count = 0;
264 int arfcns_used = 0;
265 int i;
266
267 arfcns_used = arfcns_num;
268 memmove(arfcns, orig_arfcns, sizeof(arfcns));
269
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100270 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100271 /*
272 * Manipulate the ARFCN list according to the rules in J4 depending
273 * on the selected range.
274 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100275 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100276 f0, &f0_included);
277
278 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200279 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100280
281 if (!silent)
282 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
283 range, arfcns_used, f0, f0_included);
284
285 /* Select the range and the amount of bits needed */
286 switch (range) {
287 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200288 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100289 break;
290 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200291 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100292 break;
293 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200294 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100295 break;
296 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200297 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100298 break;
299 default:
300 return 1;
301 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100302
303 if (!silent)
304 printf("chan_list = %s\n",
305 osmo_hexdump(chan_list, sizeof(chan_list)));
306
307 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
308 0xfe, 1);
309 if (rc != 0) {
310 printf("Cannot decode freq list, rc = %d\n", rc);
311 return 1;
312 }
313
314 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
315 if (dec_freq[i].mask &&
316 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
317 dec_arfcns[dec_arfcns_count++] = i;
318 }
319
320 if (!silent) {
321 printf("Decoded freqs %d (expected %d)\n",
322 dec_arfcns_count, arfcns_num);
323 printf("Decoded: ");
324 for (i = 0; i < dec_arfcns_count; i++) {
325 printf("%d ", dec_arfcns[i]);
326 if (dec_arfcns[i] != orig_arfcns[i])
327 printf("(!= %d) ", orig_arfcns[i]);
328 }
329 printf("\n");
330 }
331
332 if (dec_arfcns_count != arfcns_num) {
333 printf("Wrong number of arfcns\n");
334 return 1;
335 }
336
337 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
338 printf("Decoding error, got wrong freqs\n");
339 fprintf(stderr, " w = ");
340 for (i = 0; i < ARRAY_SIZE(w); i++)
341 fprintf(stderr, "%d ", w[i]);
342 fprintf(stderr, "\n");
343 return 1;
344 }
345
346 return 0;
347}
348
349static void test_random_range_encoding(int range, int max_arfcn_num)
350{
351 int arfcns_num = 0;
352 int test_idx;
353 int rc, max_count;
354 int num_tests = 1024;
355
356 printf("Random range test: range %d, max num ARFCNs %d\n",
357 range, max_arfcn_num);
358
359 srandom(1);
360
361 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
362 for (test_idx = 0; test_idx < num_tests; test_idx++) {
363 int count;
364 int i;
365 int min_freq = 0;
366
367 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
368 char rnd_arfcns_set[1024] = {0};
369
370 if (range < ARFCN_RANGE_1024)
371 min_freq = random() % (1023 - range);
372
373 for (count = max_count; count; ) {
374 int arfcn = min_freq + random() % (range + 1);
375 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
376
377 if (!rnd_arfcns_set[arfcn]) {
378 rnd_arfcns_set[arfcn] = 1;
379 count -= 1;
380 }
381 }
382
383 arfcns_num = 0;
384 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
385 if (rnd_arfcns_set[i])
386 rnd_arfcns[arfcns_num++] = i;
387
388 rc = test_single_range_encoding(range, rnd_arfcns,
389 arfcns_num, 1);
390 if (rc != 0) {
391 printf("Failed on test %d, range %d, num ARFCNs %d\n",
392 test_idx, range, max_count);
393 test_single_range_encoding(range, rnd_arfcns,
394 arfcns_num, 0);
395 return;
396 }
397 }
398 }
399}
400
401static void test_range_encoding()
402{
403 int *arfcns;
404 int arfcns_num = 0;
405 int test_idx;
406 int range;
407
408 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
409 {
410 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
411 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
412 range = arfcn_test_ranges[test_idx].range;
413
414 printf("Range test %d: range %d, num ARFCNs %d\n",
415 test_idx, range, arfcns_num);
416
417 test_single_range_encoding(range, arfcns, arfcns_num, 0);
418 }
419
420 test_random_range_encoding(ARFCN_RANGE_128, 29);
421 test_random_range_encoding(ARFCN_RANGE_256, 22);
422 test_random_range_encoding(ARFCN_RANGE_512, 18);
423 test_random_range_encoding(ARFCN_RANGE_1024, 16);
424}
425
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100426static int freqs1[] = {
427 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
428};
429
430static int freqs2[] = {
431 402, 460, 1, 67, 131, 197, 272, 347,
432};
433
434static int freqs3[] = {
435 68, 128, 198, 279, 353, 398, 452,
436
437};
438
439static int w_out[] = {
440 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
441};
442
443static int range128[] = {
444 1, 1 + 127,
445};
446
447static int range256[] = {
448 1, 1 + 128,
449};
450
451static int range512[] = {
452 1, 1+ 511,
453};
454
455
456static void test_arfcn_filter()
457{
458 int arfcns[50], i, res, f0_included;
459 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
460 arfcns[i] = (i + 1) * 2;
461
462 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
463 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100464 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100465 arfcns[0], &f0_included);
466 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
467 VERIFY(f0_included, ==, 1);
468 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100469 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100470
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100471 /* check with range1024, ARFCN 0 is included */
472 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
473 arfcns[i] = i * 2;
474 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
475 0, &f0_included);
476 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
477 VERIFY(f0_included, ==, 1);
478 for (i = 0; i < res; ++i)
479 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
480
481 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100482 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
483 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100484 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
485 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100486 VERIFY(res, ==, ARRAY_SIZE(arfcns));
487 VERIFY(f0_included, ==, 0);
488 for (i = 0; i < res; ++i)
489 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
490}
491
492static void test_print_encoding()
493{
494 int rc;
495 int w[17];
496 uint8_t chan_list[16];
497 memset(chan_list, 0x23, sizeof(chan_list));
498
499 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
500 switch (rc % 3) {
501 case 0:
502 w[rc] = 0xAAAA;
503 break;
504 case 1:
505 w[rc] = 0x5555;
506 break;
507 case 2:
508 w[rc] = 0x9696;
509 break;
510 }
511
Max26679e02016-04-20 15:57:13 +0200512 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100513
514 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
515}
516
517static void test_si_range_helpers()
518{
519 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
520 int i, f0 = 0xFFFFFF;
521
522 memset(&ws[0], 0x23, sizeof(ws));
523
524 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100525 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100526 VERIFY(i, ==, 2);
527
528 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100529 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100530 VERIFY(i, ==, 2);
531
532 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100533 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100534 VERIFY(i, ==, 0);
535
Max26679e02016-04-20 15:57:13 +0200536 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100537
538 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
539 printf("w[%d]=%d\n", i, ws[i]);
540 VERIFY(ws[i], ==, w_out[i]);
541 }
542
543 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
544 VERIFY(i, ==, ARFCN_RANGE_128);
545 VERIFY(f0, ==, 1);
546
547 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
548 VERIFY(i, ==, ARFCN_RANGE_256);
549 VERIFY(f0, ==, 1);
550
551 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
552 VERIFY(i, ==, ARFCN_RANGE_512);
553 VERIFY(f0, ==, 1);
554}
555
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100556static void test_gsm411_rp_ref_wrap(void)
557{
558 struct gsm_subscriber_connection conn;
559 int res;
560
561 printf("testing RP-Reference wrap\n");
562
563 memset(&conn, 0, sizeof(conn));
564 conn.next_rp_ref = 255;
565
566 res = sms_next_rp_msg_ref(&conn);
567 printf("Allocated reference: %d\n", res);
568 OSMO_ASSERT(res == 255);
569
570 res = sms_next_rp_msg_ref(&conn);
571 printf("Allocated reference: %d\n", res);
572 OSMO_ASSERT(res == 0);
573
574 res = sms_next_rp_msg_ref(&conn);
575 printf("Allocated reference: %d\n", res);
576 OSMO_ASSERT(res == 1);
577}
578
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800579int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000580{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100581 osmo_init_logging(&log_info);
582 log_set_log_level(osmo_stderr_target, LOGL_INFO);
583
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200584 test_location_area_identifier();
585 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100586
587 test_si_range_helpers();
588 test_arfcn_filter();
589 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100590 test_range_encoding();
Holger Hans Peter Freytherca114432014-02-08 15:20:48 +0100591 test_gsm411_rp_ref_wrap();
Harald Welteafedeab2010-03-04 10:55:40 +0100592
Max26679e02016-04-20 15:57:13 +0200593 test_si2q_e();
594 test_si2q_u();
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100595 printf("Done.\n");
596 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000597}