blob: 72a17727718ac1436035273bd86dfa54576baabf [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 Hofmeyrc0164792017-09-04 15:15:32 +020027#include <osmocom/bsc/common_bsc.h>
28#include <osmocom/bsc/gsm_subscriber.h>
29#include <osmocom/bsc/gsm_data_shared.h>
30#include <osmocom/bsc/debug.h>
31#include <osmocom/bsc/arfcn_range_encode.h>
32#include <osmocom/bsc/system_information.h>
33#include <osmocom/bsc/abis_rsl.h>
Max59a1bf32016-04-15 16:04:46 +020034
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010035#include <osmocom/core/application.h>
Max59a1bf32016-04-15 16:04:46 +020036#include <osmocom/gsm/sysinfo.h>
Neels Hofmeyr7b656882017-07-09 22:09:18 +020037#include <osmocom/gsm/gsm48.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
Maxf39d03a2017-05-12 17:00:30 +020087static inline void gen(struct gsm_bts *bts, const char *s)
Max59a1bf32016-04-15 16:04:46 +020088{
Max845a3a42017-05-15 12:02:29 +020089 int r;
90
Max69e9c0d2016-05-18 13:04:47 +020091 bts->si_valid = 0;
92 bts->si_valid |= (1 << SYSINFO_TYPE_2quater);
Max845a3a42017-05-15 12:02:29 +020093
Max845a3a42017-05-15 12:02:29 +020094 printf("generating SI2quater for %zu EARFCNs and %zu UARFCNs...\n",
95 si2q_earfcn_count(&bts->si_common.si2quater_neigh_list), bts->si_common.uarfcn_length);
96
97 r = gsm_generate_si(bts, SYSINFO_TYPE_2quater);
Max59a1bf32016-04-15 16:04:46 +020098 if (r > 0)
Max70fdd242017-06-15 15:10:53 +020099 for (bts->si2q_index = 0; bts->si2q_index < bts->si2q_count + 1; bts->si2q_index++)
100 printf("generated %s SI2quater [%02u/%02u]: [%d] %s\n",
101 GSM_BTS_HAS_SI(bts, SYSINFO_TYPE_2quater) ? "valid" : "invalid",
102 bts->si2q_index, bts->si2q_count, r,
103 osmo_hexdump((void *)GSM_BTS_SI2Q(bts, bts->si2q_index), GSM_MACBLOCK_LEN));
Max59a1bf32016-04-15 16:04:46 +0200104 else
Maxf39d03a2017-05-12 17:00:30 +0200105 printf("%s() failed to generate SI2quater: %s\n", s, strerror(-r));
Max59a1bf32016-04-15 16:04:46 +0200106}
107
Max845a3a42017-05-15 12:02:29 +0200108static inline void del_earfcn_b(struct gsm_bts *bts, uint16_t earfcn)
109{
110 struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
111 int r = osmo_earfcn_del(e, earfcn);
112 if (r)
113 printf("failed to remove EARFCN %u: %s\n", earfcn, strerror(-r));
114 else
115 printf("removed EARFCN %u - ", earfcn);
116
117 gen(bts, __func__);
118}
119
Maxf39d03a2017-05-12 17:00:30 +0200120static inline void add_earfcn_b(struct gsm_bts *bts, uint16_t earfcn, uint8_t bw)
Maxaafff962016-04-20 15:57:14 +0200121{
Maxf39d03a2017-05-12 17:00:30 +0200122 struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
123 int r = osmo_earfcn_add(e, earfcn, bw);
124 if (r)
125 printf("failed to add EARFCN %u: %s\n", earfcn, strerror(-r));
126 else
127 printf("added EARFCN %u - ", earfcn);
128
129 gen(bts, __func__);
130}
131
132static inline void _bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble, bool diversity)
133{
134 int r;
135
136 bts->u_offset = 0;
137
138 r = bts_uarfcn_add(bts, arfcn, scramble, diversity);
Maxaafff962016-04-20 15:57:14 +0200139 if (r < 0)
140 printf("failed to add UARFCN to SI2quater: %s\n", strerror(-r));
Max70fdd242017-06-15 15:10:53 +0200141 else {
142 bts->si2q_count = si2q_num(bts) - 1;
Maxf39d03a2017-05-12 17:00:30 +0200143 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200144 }
Maxaafff962016-04-20 15:57:14 +0200145}
146
Max881064e2016-12-14 14:51:40 +0100147static inline void test_si2q_segfault(void)
148{
149 struct gsm_bts *bts;
150 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
151 printf("Test SI2quater UARFCN (same scrambling code and diversity):\n");
152
153 if (!network)
154 exit(1);
Alexander Chemeris0796a6c2017-07-11 01:42:41 +0630155 bts = gsm_bts_alloc(network, 0);
Max881064e2016-12-14 14:51:40 +0100156
157 _bts_uarfcn_add(bts, 10564, 319, 0);
158 _bts_uarfcn_add(bts, 10612, 319, 0);
Maxf39d03a2017-05-12 17:00:30 +0200159 gen(bts, __func__);
Max881064e2016-12-14 14:51:40 +0100160}
161
Maxe610e702016-12-19 13:41:48 +0100162static inline void test_si2q_mu(void)
163{
164 struct gsm_bts *bts;
165 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
166 printf("Test SI2quater multiple UARFCNs:\n");
167
168 if (!network)
169 exit(1);
Alexander Chemeris0796a6c2017-07-11 01:42:41 +0630170 bts = gsm_bts_alloc(network, 0);
Maxe610e702016-12-19 13:41:48 +0100171
172 _bts_uarfcn_add(bts, 10564, 318, 0);
173 _bts_uarfcn_add(bts, 10612, 319, 0);
174 _bts_uarfcn_add(bts, 10612, 31, 0);
175 _bts_uarfcn_add(bts, 10612, 19, 0);
176 _bts_uarfcn_add(bts, 10613, 64, 0);
177 _bts_uarfcn_add(bts, 10613, 164, 0);
178 _bts_uarfcn_add(bts, 10613, 14, 0);
Maxe610e702016-12-19 13:41:48 +0100179}
180
Max26679e02016-04-20 15:57:13 +0200181static inline void test_si2q_u(void)
Max59a1bf32016-04-15 16:04:46 +0200182{
183 struct gsm_bts *bts;
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200184 struct gsm_network *network = bsc_network_init(NULL, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200185 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
186
187 if (!network)
188 exit(1);
Max70fdd242017-06-15 15:10:53 +0200189
Alexander Chemeris0796a6c2017-07-11 01:42:41 +0630190 bts = gsm_bts_alloc(network, 0);
Max26679e02016-04-20 15:57:13 +0200191
Max69e9c0d2016-05-18 13:04:47 +0200192 /* first generate invalid SI as no UARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200193 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200194
Max69e9c0d2016-05-18 13:04:47 +0200195 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200196 _bts_uarfcn_add(bts, 1982, 13, 1);
197 _bts_uarfcn_add(bts, 1982, 44, 0);
198 _bts_uarfcn_add(bts, 1982, 61, 1);
199 _bts_uarfcn_add(bts, 1982, 89, 1);
200 _bts_uarfcn_add(bts, 1982, 113, 0);
201 _bts_uarfcn_add(bts, 1982, 123, 0);
202 _bts_uarfcn_add(bts, 1982, 56, 1);
203 _bts_uarfcn_add(bts, 1982, 72, 1);
204 _bts_uarfcn_add(bts, 1982, 223, 1);
205 _bts_uarfcn_add(bts, 1982, 14, 0);
206 _bts_uarfcn_add(bts, 1982, 88, 0);
Max26679e02016-04-20 15:57:13 +0200207}
208
209static inline void test_si2q_e(void)
210{
211 struct gsm_bts *bts;
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200212 struct gsm_network *network = bsc_network_init(NULL, 1, 1, NULL);
Max26679e02016-04-20 15:57:13 +0200213 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200214
215 if (!network)
216 exit(1);
Max70fdd242017-06-15 15:10:53 +0200217
Alexander Chemeris0796a6c2017-07-11 01:42:41 +0630218 bts = gsm_bts_alloc(network, 0);
Max59a1bf32016-04-15 16:04:46 +0200219
Maxf39d03a2017-05-12 17:00:30 +0200220 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
221 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
Max59a1bf32016-04-15 16:04:46 +0200222 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
223 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
224
225 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max70fdd242017-06-15 15:10:53 +0200226
Max69e9c0d2016-05-18 13:04:47 +0200227 /* first generate invalid SI as no EARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200228 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200229
Max845a3a42017-05-15 12:02:29 +0200230 /* subsequent calls should produce valid SI if there's enough memory and EARFCNs */
231 add_earfcn_b(bts, 1917, 5);
232 del_earfcn_b(bts, 1917);
Maxf39d03a2017-05-12 17:00:30 +0200233 add_earfcn_b(bts, 1917, 1);
234 add_earfcn_b(bts, 1932, OSMO_EARFCN_MEAS_INVALID);
235 add_earfcn_b(bts, 1937, 2);
236 add_earfcn_b(bts, 1945, OSMO_EARFCN_MEAS_INVALID);
237 add_earfcn_b(bts, 1965, OSMO_EARFCN_MEAS_INVALID);
238 add_earfcn_b(bts, 1967, 4);
239 add_earfcn_b(bts, 1982, 3);
Max59a1bf32016-04-15 16:04:46 +0200240}
241
Max70fdd242017-06-15 15:10:53 +0200242static inline void test_si2q_long(void)
243{
244 struct gsm_bts *bts;
245 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
246 printf("Testing SYSINFO_TYPE_2quater combined EARFCN & UARFCN generation:\n");
247
248 if (!network)
249 exit(1);
250
Alexander Chemeris0796a6c2017-07-11 01:42:41 +0630251 bts = gsm_bts_alloc(network, 0);
Max70fdd242017-06-15 15:10:53 +0200252
253 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
254 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
255 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
256 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
257
258 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
259
260 bts_earfcn_add(bts, 1922, 11, 22, 8,32, 8);
261 bts_earfcn_add(bts, 1922, 11, 22, 8, 32, 8);
262 bts_earfcn_add(bts, 1924, 11, 12, 6, 11, 5);
263 bts_earfcn_add(bts, 1923, 11, 12, 6, 11, 5);
264 bts_earfcn_add(bts, 1925, 11, 12, 6, 11, 5);
265 bts_earfcn_add(bts, 2111, 11, 12, 6, 11, 5);
266 bts_earfcn_add(bts, 2112, 11, 12, 6, 11, 4);
267 bts_earfcn_add(bts, 2113, 11, 12, 6, 11, 3);
268 bts_earfcn_add(bts, 2114, 11, 12, 6, 11, 2);
269 bts_earfcn_add(bts, 2131, 11, 12, 6, 11, 5);
270 bts_earfcn_add(bts, 2132, 11, 12, 6, 11, 4);
271 bts_earfcn_add(bts, 2133, 11, 12, 6, 11, 3);
272 bts_earfcn_add(bts, 2134, 11, 12, 6, 11, 2);
273 bts_earfcn_add(bts, 2121, 11, 12, 6, 11, 5);
274 bts_earfcn_add(bts, 2122, 11, 12, 6, 11, 4);
275 bts_earfcn_add(bts, 2123, 11, 12, 6, 11, 3);
276 bts_earfcn_add(bts, 2124, 11, 12, 6, 11, 2);
277 _bts_uarfcn_add(bts, 1976, 13, 1);
278 _bts_uarfcn_add(bts, 1976, 38, 1);
279 _bts_uarfcn_add(bts, 1976, 44, 1);
280 _bts_uarfcn_add(bts, 1976, 120, 1);
281 _bts_uarfcn_add(bts, 1976, 140, 1);
282 _bts_uarfcn_add(bts, 1976, 163, 1);
283 _bts_uarfcn_add(bts, 1976, 166, 1);
284 _bts_uarfcn_add(bts, 1976, 217, 1);
285 _bts_uarfcn_add(bts, 1976, 224, 1);
286 _bts_uarfcn_add(bts, 1976, 225, 1);
287 _bts_uarfcn_add(bts, 1976, 226, 1);
288}
289
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200290static void test_mi_functionality(void)
291{
292 const char *imsi_odd = "987654321098763";
293 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200294 const uint32_t tmsi = 0xfabeacd0;
295 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200296 unsigned int mi_len;
297 char mi_parsed[GSM48_MI_SIZE];
298
299 printf("Testing parsing and generating TMSI/IMSI\n");
300
301 /* tmsi code */
302 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
303 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100304 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200305
306 /* imsi code */
307 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
308 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200309 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200310 COMPARE_STR(mi_parsed, imsi_odd);
311
312 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
313 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200314 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200315 COMPARE_STR(mi_parsed, imsi_even);
316}
317
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100318struct {
319 int range;
320 int arfcns_num;
321 int arfcns[RANGE_ENC_MAX_ARFCNS];
322} arfcn_test_ranges[] = {
323 {ARFCN_RANGE_512, 12,
324 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
325 {ARFCN_RANGE_512, 17,
326 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
327 {ARFCN_RANGE_512, 18,
328 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
329 {ARFCN_RANGE_512, 18,
330 { 1, 17, 31, 45, 58, 79, 81, 97,
331 113, 127, 213, 277, 287, 311, 331, 391,
332 417, 511 }},
333 {ARFCN_RANGE_512, 6,
334 { 1, 17, 31, 45, 58, 79 }},
335 {ARFCN_RANGE_512, 6,
336 { 10, 17, 31, 45, 58, 79 }},
337 {ARFCN_RANGE_1024, 17,
338 { 0, 17, 31, 45, 58, 79, 81, 97,
339 113, 127, 213, 277, 287, 311, 331, 391,
340 1023 }},
341 {ARFCN_RANGE_1024, 16,
342 { 17, 31, 45, 58, 79, 81, 97, 113,
343 127, 213, 277, 287, 311, 331, 391, 1023 }},
344 {-1}
345};
346
347static int test_single_range_encoding(int range, const int *orig_arfcns,
348 int arfcns_num, int silent)
349{
350 int arfcns[RANGE_ENC_MAX_ARFCNS];
351 int w[RANGE_ENC_MAX_ARFCNS];
352 int f0_included = 0;
353 int rc, f0;
354 uint8_t chan_list[16] = {0};
355 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
356 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
357 int dec_arfcns_count = 0;
358 int arfcns_used = 0;
359 int i;
360
361 arfcns_used = arfcns_num;
362 memmove(arfcns, orig_arfcns, sizeof(arfcns));
363
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100364 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100365 /*
366 * Manipulate the ARFCN list according to the rules in J4 depending
367 * on the selected range.
368 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100369 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100370 f0, &f0_included);
371
372 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200373 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100374
375 if (!silent)
376 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
377 range, arfcns_used, f0, f0_included);
378
379 /* Select the range and the amount of bits needed */
380 switch (range) {
381 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200382 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100383 break;
384 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200385 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100386 break;
387 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200388 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100389 break;
390 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200391 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100392 break;
393 default:
394 return 1;
395 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100396
397 if (!silent)
398 printf("chan_list = %s\n",
399 osmo_hexdump(chan_list, sizeof(chan_list)));
400
401 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
402 0xfe, 1);
403 if (rc != 0) {
404 printf("Cannot decode freq list, rc = %d\n", rc);
405 return 1;
406 }
407
408 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
409 if (dec_freq[i].mask &&
410 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
411 dec_arfcns[dec_arfcns_count++] = i;
412 }
413
414 if (!silent) {
415 printf("Decoded freqs %d (expected %d)\n",
416 dec_arfcns_count, arfcns_num);
417 printf("Decoded: ");
418 for (i = 0; i < dec_arfcns_count; i++) {
419 printf("%d ", dec_arfcns[i]);
420 if (dec_arfcns[i] != orig_arfcns[i])
421 printf("(!= %d) ", orig_arfcns[i]);
422 }
423 printf("\n");
424 }
425
426 if (dec_arfcns_count != arfcns_num) {
427 printf("Wrong number of arfcns\n");
428 return 1;
429 }
430
431 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
432 printf("Decoding error, got wrong freqs\n");
433 fprintf(stderr, " w = ");
434 for (i = 0; i < ARRAY_SIZE(w); i++)
435 fprintf(stderr, "%d ", w[i]);
436 fprintf(stderr, "\n");
437 return 1;
438 }
439
440 return 0;
441}
442
443static void test_random_range_encoding(int range, int max_arfcn_num)
444{
445 int arfcns_num = 0;
446 int test_idx;
447 int rc, max_count;
448 int num_tests = 1024;
449
450 printf("Random range test: range %d, max num ARFCNs %d\n",
451 range, max_arfcn_num);
452
453 srandom(1);
454
455 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
456 for (test_idx = 0; test_idx < num_tests; test_idx++) {
457 int count;
458 int i;
459 int min_freq = 0;
460
461 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
462 char rnd_arfcns_set[1024] = {0};
463
464 if (range < ARFCN_RANGE_1024)
465 min_freq = random() % (1023 - range);
466
467 for (count = max_count; count; ) {
468 int arfcn = min_freq + random() % (range + 1);
469 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
470
471 if (!rnd_arfcns_set[arfcn]) {
472 rnd_arfcns_set[arfcn] = 1;
473 count -= 1;
474 }
475 }
476
477 arfcns_num = 0;
478 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
479 if (rnd_arfcns_set[i])
480 rnd_arfcns[arfcns_num++] = i;
481
482 rc = test_single_range_encoding(range, rnd_arfcns,
483 arfcns_num, 1);
484 if (rc != 0) {
485 printf("Failed on test %d, range %d, num ARFCNs %d\n",
486 test_idx, range, max_count);
487 test_single_range_encoding(range, rnd_arfcns,
488 arfcns_num, 0);
489 return;
490 }
491 }
492 }
493}
494
495static void test_range_encoding()
496{
497 int *arfcns;
498 int arfcns_num = 0;
499 int test_idx;
500 int range;
501
502 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
503 {
504 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
505 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
506 range = arfcn_test_ranges[test_idx].range;
507
508 printf("Range test %d: range %d, num ARFCNs %d\n",
509 test_idx, range, arfcns_num);
510
511 test_single_range_encoding(range, arfcns, arfcns_num, 0);
512 }
513
514 test_random_range_encoding(ARFCN_RANGE_128, 29);
515 test_random_range_encoding(ARFCN_RANGE_256, 22);
516 test_random_range_encoding(ARFCN_RANGE_512, 18);
517 test_random_range_encoding(ARFCN_RANGE_1024, 16);
518}
519
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100520static int freqs1[] = {
521 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
522};
523
524static int freqs2[] = {
525 402, 460, 1, 67, 131, 197, 272, 347,
526};
527
528static int freqs3[] = {
529 68, 128, 198, 279, 353, 398, 452,
530
531};
532
533static int w_out[] = {
534 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
535};
536
537static int range128[] = {
538 1, 1 + 127,
539};
540
541static int range256[] = {
542 1, 1 + 128,
543};
544
545static int range512[] = {
546 1, 1+ 511,
547};
548
549
550static void test_arfcn_filter()
551{
552 int arfcns[50], i, res, f0_included;
553 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
554 arfcns[i] = (i + 1) * 2;
555
556 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
557 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100558 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100559 arfcns[0], &f0_included);
560 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
561 VERIFY(f0_included, ==, 1);
562 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100563 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100564
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100565 /* check with range1024, ARFCN 0 is included */
566 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
567 arfcns[i] = i * 2;
568 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
569 0, &f0_included);
570 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
571 VERIFY(f0_included, ==, 1);
572 for (i = 0; i < res; ++i)
573 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
574
575 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100576 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
577 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100578 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
579 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100580 VERIFY(res, ==, ARRAY_SIZE(arfcns));
581 VERIFY(f0_included, ==, 0);
582 for (i = 0; i < res; ++i)
583 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
584}
585
586static void test_print_encoding()
587{
588 int rc;
589 int w[17];
590 uint8_t chan_list[16];
591 memset(chan_list, 0x23, sizeof(chan_list));
592
593 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
594 switch (rc % 3) {
595 case 0:
596 w[rc] = 0xAAAA;
597 break;
598 case 1:
599 w[rc] = 0x5555;
600 break;
601 case 2:
602 w[rc] = 0x9696;
603 break;
604 }
605
Max26679e02016-04-20 15:57:13 +0200606 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100607
608 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
609}
610
611static void test_si_range_helpers()
612{
613 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
614 int i, f0 = 0xFFFFFF;
615
616 memset(&ws[0], 0x23, sizeof(ws));
617
618 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100619 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100620 VERIFY(i, ==, 2);
621
622 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100623 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100624 VERIFY(i, ==, 2);
625
626 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100627 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100628 VERIFY(i, ==, 0);
629
Max26679e02016-04-20 15:57:13 +0200630 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100631
632 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
633 printf("w[%d]=%d\n", i, ws[i]);
634 VERIFY(ws[i], ==, w_out[i]);
635 }
636
637 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
638 VERIFY(i, ==, ARFCN_RANGE_128);
639 VERIFY(f0, ==, 1);
640
641 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
642 VERIFY(i, ==, ARFCN_RANGE_256);
643 VERIFY(f0, ==, 1);
644
645 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
646 VERIFY(i, ==, ARFCN_RANGE_512);
647 VERIFY(f0, ==, 1);
648}
649
Harald Welte14f97722017-10-01 11:09:47 +0800650static void test_si_ba_ind(void)
651{
652 struct gsm_network *network = bsc_network_init(tall_bsc_ctx, 1, 1, NULL);
653 struct gsm_bts *bts = gsm_bts_alloc(network, 0);
654 const struct gsm48_system_information_type_2 *si2 =
655 (struct gsm48_system_information_type_2 *) GSM_BTS_SI(bts, SYSINFO_TYPE_2);
656 const struct gsm48_system_information_type_2bis *si2bis =
657 (struct gsm48_system_information_type_2bis *) GSM_BTS_SI(bts, SYSINFO_TYPE_2bis);
658 const struct gsm48_system_information_type_2ter *si2ter =
659 (struct gsm48_system_information_type_2ter *) GSM_BTS_SI(bts, SYSINFO_TYPE_2ter);
660 const struct gsm48_system_information_type_5 *si5 =
661 (struct gsm48_system_information_type_5 *) GSM_BTS_SI(bts, SYSINFO_TYPE_5);
662 const struct gsm48_system_information_type_5bis *si5bis =
663 (struct gsm48_system_information_type_5bis *) GSM_BTS_SI(bts, SYSINFO_TYPE_5bis);
664 const struct gsm48_system_information_type_5ter *si5ter =
665 (struct gsm48_system_information_type_5ter *) GSM_BTS_SI(bts, SYSINFO_TYPE_5ter);
666
667 int rc;
668
669 bts->network = network;
670 bts->c0->arfcn = 23;
671
672 printf("Testing if BA-IND is set as expected in SI2xxx and SI5xxx\n");
673
674 rc = gsm_generate_si(bts, SYSINFO_TYPE_2);
675 OSMO_ASSERT(rc > 0);
676 printf("SI2: %s\n", osmo_hexdump((uint8_t *)si2, rc));
677 /* Validate BA-IND == 0 */
678 OSMO_ASSERT(!(si2->bcch_frequency_list[0] & 0x10));
679
680 rc = gsm_generate_si(bts, SYSINFO_TYPE_2bis);
681 OSMO_ASSERT(rc > 0);
682 printf("SI2bis: %s\n", osmo_hexdump((uint8_t *)si2bis, rc));
683 /* Validate BA-IND == 0 */
684 OSMO_ASSERT(!(si2bis->bcch_frequency_list[0] & 0x10));
685
686 rc = gsm_generate_si(bts, SYSINFO_TYPE_2ter);
687 OSMO_ASSERT(rc > 0);
688 printf("SI2ter: %s\n", osmo_hexdump((uint8_t *)si2ter, rc));
689 /* Validate BA-IND == 0 */
690 OSMO_ASSERT(!(si2ter->ext_bcch_frequency_list[0] & 0x10));
691
692 rc = gsm_generate_si(bts, SYSINFO_TYPE_5);
693 OSMO_ASSERT(rc > 0);
694 printf("SI5: %s\n", osmo_hexdump((uint8_t *)si5, rc));
695 /* Validate BA-IND == 1 */
696 OSMO_ASSERT(si5->bcch_frequency_list[0] & 0x10);
697
698 rc = gsm_generate_si(bts, SYSINFO_TYPE_5bis);
699 OSMO_ASSERT(rc > 0);
700 printf("SI5bis: %s\n", osmo_hexdump((uint8_t *)si5bis, rc));
701 /* Validate BA-IND == 1 */
702 OSMO_ASSERT(si5bis->bcch_frequency_list[0] & 0x10);
703
704 rc = gsm_generate_si(bts, SYSINFO_TYPE_5ter);
705 OSMO_ASSERT(rc > 0);
706 printf("SI5ter: %s\n", osmo_hexdump((uint8_t *)si5ter, rc));
707 /* Validate BA-IND == 1 */
708 OSMO_ASSERT(si5ter->bcch_frequency_list[0] & 0x10);
709}
710
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800711int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000712{
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100713 osmo_init_logging(&log_info);
714 log_set_log_level(osmo_stderr_target, LOGL_INFO);
715
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200716 test_location_area_identifier();
717 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100718
719 test_si_range_helpers();
720 test_arfcn_filter();
721 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100722 test_range_encoding();
Harald Welteafedeab2010-03-04 10:55:40 +0100723
Max881064e2016-12-14 14:51:40 +0100724 test_si2q_segfault();
Max26679e02016-04-20 15:57:13 +0200725 test_si2q_e();
726 test_si2q_u();
Maxe610e702016-12-19 13:41:48 +0100727 test_si2q_mu();
Max70fdd242017-06-15 15:10:53 +0200728 test_si2q_long();
729
Harald Welte14f97722017-10-01 11:09:47 +0800730 test_si_ba_ind();
731
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100732 printf("Done.\n");
Max70fdd242017-06-15 15:10:53 +0200733
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100734 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000735}