blob: 5a4c168dcee50a5956c3436896ce6e1922acd8f5 [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>
Harald Welted9956d92017-12-17 21:48:47 +010028#include <osmocom/bsc/gsm_data.h>
Neels Hofmeyrc0164792017-09-04 15:15:32 +020029#include <osmocom/bsc/debug.h>
30#include <osmocom/bsc/arfcn_range_encode.h>
31#include <osmocom/bsc/system_information.h>
32#include <osmocom/bsc/abis_rsl.h>
Max59a1bf32016-04-15 16:04:46 +020033
Jacob Erlbeck4b903b42014-01-10 17:43:41 +010034#include <osmocom/core/application.h>
Neels Hofmeyra05360c2018-03-04 23:45:05 +010035#include <osmocom/core/byteswap.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
Max4ab72682017-12-18 18:19:49 +0100147static inline struct gsm_bts *bts_init(void *ctx, struct gsm_network *net, const char *msg)
Max881064e2016-12-14 14:51:40 +0100148{
Max4ab72682017-12-18 18:19:49 +0100149 struct gsm_bts *bts = gsm_bts_alloc(net, 0);
150 if (!bts) {
151 printf("BTS allocation failure in %s()\n", msg);
Max881064e2016-12-14 14:51:40 +0100152 exit(1);
Max4ab72682017-12-18 18:19:49 +0100153 }
154 printf("BTS allocation OK in %s()\n", msg);
155
156 bts->network = net;
157
158 return bts;
159}
160
161static inline void test_si2q_segfault(struct gsm_network *net)
162{
163 struct gsm_bts *bts = bts_init(tall_bsc_ctx, net, __func__);
164 printf("Test SI2quater UARFCN (same scrambling code and diversity):\n");
Max881064e2016-12-14 14:51:40 +0100165
166 _bts_uarfcn_add(bts, 10564, 319, 0);
167 _bts_uarfcn_add(bts, 10612, 319, 0);
Maxf39d03a2017-05-12 17:00:30 +0200168 gen(bts, __func__);
Max4ab72682017-12-18 18:19:49 +0100169
170 rate_ctr_group_free(bts->bts_ctrs);
171 talloc_free(bts);
Max881064e2016-12-14 14:51:40 +0100172}
173
Max4ab72682017-12-18 18:19:49 +0100174static inline void test_si2q_mu(struct gsm_network *net)
Maxe610e702016-12-19 13:41:48 +0100175{
Max4ab72682017-12-18 18:19:49 +0100176 struct gsm_bts *bts = bts_init(tall_bsc_ctx, net, __func__);
Maxe610e702016-12-19 13:41:48 +0100177 printf("Test SI2quater multiple UARFCNs:\n");
178
Maxe610e702016-12-19 13:41:48 +0100179 _bts_uarfcn_add(bts, 10564, 318, 0);
180 _bts_uarfcn_add(bts, 10612, 319, 0);
181 _bts_uarfcn_add(bts, 10612, 31, 0);
182 _bts_uarfcn_add(bts, 10612, 19, 0);
183 _bts_uarfcn_add(bts, 10613, 64, 0);
184 _bts_uarfcn_add(bts, 10613, 164, 0);
185 _bts_uarfcn_add(bts, 10613, 14, 0);
Max4ab72682017-12-18 18:19:49 +0100186
187 rate_ctr_group_free(bts->bts_ctrs);
188 talloc_free(bts);
Maxe610e702016-12-19 13:41:48 +0100189}
190
Max4ab72682017-12-18 18:19:49 +0100191static inline void test_si2q_u(struct gsm_network *net)
Max59a1bf32016-04-15 16:04:46 +0200192{
Max4ab72682017-12-18 18:19:49 +0100193 struct gsm_bts *bts = bts_init(tall_bsc_ctx, net, __func__);
Max26679e02016-04-20 15:57:13 +0200194 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
195
Max69e9c0d2016-05-18 13:04:47 +0200196 /* first generate invalid SI as no UARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200197 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200198
Max69e9c0d2016-05-18 13:04:47 +0200199 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200200 _bts_uarfcn_add(bts, 1982, 13, 1);
201 _bts_uarfcn_add(bts, 1982, 44, 0);
202 _bts_uarfcn_add(bts, 1982, 61, 1);
203 _bts_uarfcn_add(bts, 1982, 89, 1);
204 _bts_uarfcn_add(bts, 1982, 113, 0);
205 _bts_uarfcn_add(bts, 1982, 123, 0);
206 _bts_uarfcn_add(bts, 1982, 56, 1);
207 _bts_uarfcn_add(bts, 1982, 72, 1);
208 _bts_uarfcn_add(bts, 1982, 223, 1);
209 _bts_uarfcn_add(bts, 1982, 14, 0);
210 _bts_uarfcn_add(bts, 1982, 88, 0);
Max4ab72682017-12-18 18:19:49 +0100211
212 rate_ctr_group_free(bts->bts_ctrs);
213 talloc_free(bts);
Max26679e02016-04-20 15:57:13 +0200214}
215
Max4ab72682017-12-18 18:19:49 +0100216static inline void test_si2q_e(struct gsm_network *net)
Max26679e02016-04-20 15:57:13 +0200217{
Max4ab72682017-12-18 18:19:49 +0100218 struct gsm_bts *bts = bts_init(tall_bsc_ctx, net, __func__);
Max26679e02016-04-20 15:57:13 +0200219 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200220
Maxf39d03a2017-05-12 17:00:30 +0200221 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
222 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
Max59a1bf32016-04-15 16:04:46 +0200223 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
224 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
225
226 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max70fdd242017-06-15 15:10:53 +0200227
Max69e9c0d2016-05-18 13:04:47 +0200228 /* first generate invalid SI as no EARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200229 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200230
Max845a3a42017-05-15 12:02:29 +0200231 /* subsequent calls should produce valid SI if there's enough memory and EARFCNs */
232 add_earfcn_b(bts, 1917, 5);
233 del_earfcn_b(bts, 1917);
Maxf39d03a2017-05-12 17:00:30 +0200234 add_earfcn_b(bts, 1917, 1);
235 add_earfcn_b(bts, 1932, OSMO_EARFCN_MEAS_INVALID);
236 add_earfcn_b(bts, 1937, 2);
237 add_earfcn_b(bts, 1945, OSMO_EARFCN_MEAS_INVALID);
238 add_earfcn_b(bts, 1965, OSMO_EARFCN_MEAS_INVALID);
239 add_earfcn_b(bts, 1967, 4);
240 add_earfcn_b(bts, 1982, 3);
Max4ab72682017-12-18 18:19:49 +0100241
242 rate_ctr_group_free(bts->bts_ctrs);
243 talloc_free(bts);
Max59a1bf32016-04-15 16:04:46 +0200244}
245
Max4ab72682017-12-18 18:19:49 +0100246static inline void test_si2q_long(struct gsm_network *net)
Max70fdd242017-06-15 15:10:53 +0200247{
Max4ab72682017-12-18 18:19:49 +0100248 struct gsm_bts *bts = bts_init(tall_bsc_ctx, net, __func__);
Max70fdd242017-06-15 15:10:53 +0200249 printf("Testing SYSINFO_TYPE_2quater combined EARFCN & UARFCN generation:\n");
250
Max70fdd242017-06-15 15:10:53 +0200251 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
252 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
253 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
254 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
255
256 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
257
258 bts_earfcn_add(bts, 1922, 11, 22, 8,32, 8);
259 bts_earfcn_add(bts, 1922, 11, 22, 8, 32, 8);
260 bts_earfcn_add(bts, 1924, 11, 12, 6, 11, 5);
261 bts_earfcn_add(bts, 1923, 11, 12, 6, 11, 5);
262 bts_earfcn_add(bts, 1925, 11, 12, 6, 11, 5);
263 bts_earfcn_add(bts, 2111, 11, 12, 6, 11, 5);
264 bts_earfcn_add(bts, 2112, 11, 12, 6, 11, 4);
265 bts_earfcn_add(bts, 2113, 11, 12, 6, 11, 3);
266 bts_earfcn_add(bts, 2114, 11, 12, 6, 11, 2);
267 bts_earfcn_add(bts, 2131, 11, 12, 6, 11, 5);
268 bts_earfcn_add(bts, 2132, 11, 12, 6, 11, 4);
269 bts_earfcn_add(bts, 2133, 11, 12, 6, 11, 3);
270 bts_earfcn_add(bts, 2134, 11, 12, 6, 11, 2);
271 bts_earfcn_add(bts, 2121, 11, 12, 6, 11, 5);
272 bts_earfcn_add(bts, 2122, 11, 12, 6, 11, 4);
273 bts_earfcn_add(bts, 2123, 11, 12, 6, 11, 3);
274 bts_earfcn_add(bts, 2124, 11, 12, 6, 11, 2);
275 _bts_uarfcn_add(bts, 1976, 13, 1);
276 _bts_uarfcn_add(bts, 1976, 38, 1);
277 _bts_uarfcn_add(bts, 1976, 44, 1);
278 _bts_uarfcn_add(bts, 1976, 120, 1);
279 _bts_uarfcn_add(bts, 1976, 140, 1);
280 _bts_uarfcn_add(bts, 1976, 163, 1);
281 _bts_uarfcn_add(bts, 1976, 166, 1);
282 _bts_uarfcn_add(bts, 1976, 217, 1);
283 _bts_uarfcn_add(bts, 1976, 224, 1);
284 _bts_uarfcn_add(bts, 1976, 225, 1);
285 _bts_uarfcn_add(bts, 1976, 226, 1);
Max4ab72682017-12-18 18:19:49 +0100286
287 rate_ctr_group_free(bts->bts_ctrs);
288 talloc_free(bts);
Max70fdd242017-06-15 15:10:53 +0200289}
290
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200291static void test_mi_functionality(void)
292{
293 const char *imsi_odd = "987654321098763";
294 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200295 const uint32_t tmsi = 0xfabeacd0;
296 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200297 unsigned int mi_len;
298 char mi_parsed[GSM48_MI_SIZE];
299
300 printf("Testing parsing and generating TMSI/IMSI\n");
301
302 /* tmsi code */
303 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
304 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100305 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200306
307 /* imsi code */
308 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
309 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200310 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200311 COMPARE_STR(mi_parsed, imsi_odd);
312
313 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
314 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200315 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200316 COMPARE_STR(mi_parsed, imsi_even);
317}
318
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100319struct {
320 int range;
321 int arfcns_num;
322 int arfcns[RANGE_ENC_MAX_ARFCNS];
323} arfcn_test_ranges[] = {
324 {ARFCN_RANGE_512, 12,
325 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
326 {ARFCN_RANGE_512, 17,
327 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
328 {ARFCN_RANGE_512, 18,
329 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
330 {ARFCN_RANGE_512, 18,
331 { 1, 17, 31, 45, 58, 79, 81, 97,
332 113, 127, 213, 277, 287, 311, 331, 391,
333 417, 511 }},
334 {ARFCN_RANGE_512, 6,
335 { 1, 17, 31, 45, 58, 79 }},
336 {ARFCN_RANGE_512, 6,
337 { 10, 17, 31, 45, 58, 79 }},
338 {ARFCN_RANGE_1024, 17,
339 { 0, 17, 31, 45, 58, 79, 81, 97,
340 113, 127, 213, 277, 287, 311, 331, 391,
341 1023 }},
342 {ARFCN_RANGE_1024, 16,
343 { 17, 31, 45, 58, 79, 81, 97, 113,
344 127, 213, 277, 287, 311, 331, 391, 1023 }},
345 {-1}
346};
347
348static int test_single_range_encoding(int range, const int *orig_arfcns,
349 int arfcns_num, int silent)
350{
351 int arfcns[RANGE_ENC_MAX_ARFCNS];
352 int w[RANGE_ENC_MAX_ARFCNS];
353 int f0_included = 0;
354 int rc, f0;
355 uint8_t chan_list[16] = {0};
356 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
357 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
358 int dec_arfcns_count = 0;
359 int arfcns_used = 0;
360 int i;
361
362 arfcns_used = arfcns_num;
363 memmove(arfcns, orig_arfcns, sizeof(arfcns));
364
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100365 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100366 /*
367 * Manipulate the ARFCN list according to the rules in J4 depending
368 * on the selected range.
369 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100370 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100371 f0, &f0_included);
372
373 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200374 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100375
376 if (!silent)
377 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
378 range, arfcns_used, f0, f0_included);
379
380 /* Select the range and the amount of bits needed */
381 switch (range) {
382 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200383 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100384 break;
385 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200386 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100387 break;
388 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200389 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100390 break;
391 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200392 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100393 break;
394 default:
395 return 1;
396 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100397
398 if (!silent)
399 printf("chan_list = %s\n",
400 osmo_hexdump(chan_list, sizeof(chan_list)));
401
402 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
403 0xfe, 1);
404 if (rc != 0) {
405 printf("Cannot decode freq list, rc = %d\n", rc);
406 return 1;
407 }
408
409 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
410 if (dec_freq[i].mask &&
411 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
412 dec_arfcns[dec_arfcns_count++] = i;
413 }
414
415 if (!silent) {
416 printf("Decoded freqs %d (expected %d)\n",
417 dec_arfcns_count, arfcns_num);
418 printf("Decoded: ");
419 for (i = 0; i < dec_arfcns_count; i++) {
420 printf("%d ", dec_arfcns[i]);
421 if (dec_arfcns[i] != orig_arfcns[i])
422 printf("(!= %d) ", orig_arfcns[i]);
423 }
424 printf("\n");
425 }
426
427 if (dec_arfcns_count != arfcns_num) {
428 printf("Wrong number of arfcns\n");
429 return 1;
430 }
431
432 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
433 printf("Decoding error, got wrong freqs\n");
434 fprintf(stderr, " w = ");
435 for (i = 0; i < ARRAY_SIZE(w); i++)
436 fprintf(stderr, "%d ", w[i]);
437 fprintf(stderr, "\n");
438 return 1;
439 }
440
441 return 0;
442}
443
444static void test_random_range_encoding(int range, int max_arfcn_num)
445{
446 int arfcns_num = 0;
447 int test_idx;
448 int rc, max_count;
449 int num_tests = 1024;
450
451 printf("Random range test: range %d, max num ARFCNs %d\n",
452 range, max_arfcn_num);
453
454 srandom(1);
455
456 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
457 for (test_idx = 0; test_idx < num_tests; test_idx++) {
458 int count;
459 int i;
460 int min_freq = 0;
461
462 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
463 char rnd_arfcns_set[1024] = {0};
464
465 if (range < ARFCN_RANGE_1024)
466 min_freq = random() % (1023 - range);
467
468 for (count = max_count; count; ) {
469 int arfcn = min_freq + random() % (range + 1);
470 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
471
472 if (!rnd_arfcns_set[arfcn]) {
473 rnd_arfcns_set[arfcn] = 1;
474 count -= 1;
475 }
476 }
477
478 arfcns_num = 0;
479 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
480 if (rnd_arfcns_set[i])
481 rnd_arfcns[arfcns_num++] = i;
482
483 rc = test_single_range_encoding(range, rnd_arfcns,
484 arfcns_num, 1);
485 if (rc != 0) {
486 printf("Failed on test %d, range %d, num ARFCNs %d\n",
487 test_idx, range, max_count);
488 test_single_range_encoding(range, rnd_arfcns,
489 arfcns_num, 0);
490 return;
491 }
492 }
493 }
494}
495
496static void test_range_encoding()
497{
498 int *arfcns;
499 int arfcns_num = 0;
500 int test_idx;
501 int range;
502
503 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
504 {
505 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
506 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
507 range = arfcn_test_ranges[test_idx].range;
508
509 printf("Range test %d: range %d, num ARFCNs %d\n",
510 test_idx, range, arfcns_num);
511
512 test_single_range_encoding(range, arfcns, arfcns_num, 0);
513 }
514
515 test_random_range_encoding(ARFCN_RANGE_128, 29);
516 test_random_range_encoding(ARFCN_RANGE_256, 22);
517 test_random_range_encoding(ARFCN_RANGE_512, 18);
518 test_random_range_encoding(ARFCN_RANGE_1024, 16);
519}
520
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100521static int freqs1[] = {
522 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
523};
524
525static int freqs2[] = {
526 402, 460, 1, 67, 131, 197, 272, 347,
527};
528
529static int freqs3[] = {
530 68, 128, 198, 279, 353, 398, 452,
531
532};
533
534static int w_out[] = {
535 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
536};
537
538static int range128[] = {
539 1, 1 + 127,
540};
541
542static int range256[] = {
543 1, 1 + 128,
544};
545
546static int range512[] = {
547 1, 1+ 511,
548};
549
550
551static void test_arfcn_filter()
552{
553 int arfcns[50], i, res, f0_included;
554 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
555 arfcns[i] = (i + 1) * 2;
556
557 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
558 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100559 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100560 arfcns[0], &f0_included);
561 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
562 VERIFY(f0_included, ==, 1);
563 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100564 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100565
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100566 /* check with range1024, ARFCN 0 is included */
567 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
568 arfcns[i] = i * 2;
569 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
570 0, &f0_included);
571 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
572 VERIFY(f0_included, ==, 1);
573 for (i = 0; i < res; ++i)
574 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
575
576 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100577 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
578 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100579 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
580 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100581 VERIFY(res, ==, ARRAY_SIZE(arfcns));
582 VERIFY(f0_included, ==, 0);
583 for (i = 0; i < res; ++i)
584 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
585}
586
587static void test_print_encoding()
588{
589 int rc;
590 int w[17];
591 uint8_t chan_list[16];
592 memset(chan_list, 0x23, sizeof(chan_list));
593
594 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
595 switch (rc % 3) {
596 case 0:
597 w[rc] = 0xAAAA;
598 break;
599 case 1:
600 w[rc] = 0x5555;
601 break;
602 case 2:
603 w[rc] = 0x9696;
604 break;
605 }
606
Max26679e02016-04-20 15:57:13 +0200607 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100608
609 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
610}
611
612static void test_si_range_helpers()
613{
614 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
615 int i, f0 = 0xFFFFFF;
616
617 memset(&ws[0], 0x23, sizeof(ws));
618
619 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100620 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100621 VERIFY(i, ==, 2);
622
623 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100624 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100625 VERIFY(i, ==, 2);
626
627 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100628 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100629 VERIFY(i, ==, 0);
630
Max26679e02016-04-20 15:57:13 +0200631 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100632
633 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
634 printf("w[%d]=%d\n", i, ws[i]);
635 VERIFY(ws[i], ==, w_out[i]);
636 }
637
638 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
639 VERIFY(i, ==, ARFCN_RANGE_128);
640 VERIFY(f0, ==, 1);
641
642 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
643 VERIFY(i, ==, ARFCN_RANGE_256);
644 VERIFY(f0, ==, 1);
645
646 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
647 VERIFY(i, ==, ARFCN_RANGE_512);
648 VERIFY(f0, ==, 1);
649}
650
Max4ab72682017-12-18 18:19:49 +0100651static void test_si_ba_ind(struct gsm_network *net)
Harald Welte14f97722017-10-01 11:09:47 +0800652{
Max4ab72682017-12-18 18:19:49 +0100653 struct gsm_bts *bts = bts_init(tall_bsc_ctx, net, __func__);
654
Harald Welte14f97722017-10-01 11:09:47 +0800655 const struct gsm48_system_information_type_2 *si2 =
656 (struct gsm48_system_information_type_2 *) GSM_BTS_SI(bts, SYSINFO_TYPE_2);
657 const struct gsm48_system_information_type_2bis *si2bis =
658 (struct gsm48_system_information_type_2bis *) GSM_BTS_SI(bts, SYSINFO_TYPE_2bis);
659 const struct gsm48_system_information_type_2ter *si2ter =
660 (struct gsm48_system_information_type_2ter *) GSM_BTS_SI(bts, SYSINFO_TYPE_2ter);
661 const struct gsm48_system_information_type_5 *si5 =
662 (struct gsm48_system_information_type_5 *) GSM_BTS_SI(bts, SYSINFO_TYPE_5);
663 const struct gsm48_system_information_type_5bis *si5bis =
664 (struct gsm48_system_information_type_5bis *) GSM_BTS_SI(bts, SYSINFO_TYPE_5bis);
665 const struct gsm48_system_information_type_5ter *si5ter =
666 (struct gsm48_system_information_type_5ter *) GSM_BTS_SI(bts, SYSINFO_TYPE_5ter);
667
668 int rc;
669
Harald Welte14f97722017-10-01 11:09:47 +0800670 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
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100711struct test_gsm48_ra_id_by_bts {
712 struct osmo_plmn_id plmn;
713 uint16_t lac;
714 uint8_t rac;
715 struct gsm48_ra_id expect;
716};
717static const struct test_gsm48_ra_id_by_bts test_gsm48_ra_id_by_bts_data[] = {
718 {
719 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = false },
720 .lac = 3,
721 .rac = 4,
722 .expect = {
723 .digits = { 0x00, 0xf1, 0x20 },
724 .lac = 0x0300, /* network byte order of 3 */
725 .rac = 4,
726 },
727 },
728 {
729 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = true },
730 .lac = 3,
731 .rac = 4,
732 .expect = {
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100733 .digits = { 0x00, 0x21, 0x00 },
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100734 .lac = 0x0300, /* network byte order of 3 */
735 .rac = 4,
736 },
737 },
738 {
739 .plmn = { .mcc = 0, .mnc = 0, .mnc_3_digits = false },
740 .lac = 0,
741 .rac = 0,
742 .expect = {
743 .digits = { 0x00, 0xf0, 0x00 },
744 },
745 },
746 {
747 .plmn = { .mcc = 0, .mnc = 0, .mnc_3_digits = true },
748 .lac = 0,
749 .rac = 0,
750 .expect = {
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100751 .digits = {},
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100752 },
753 },
754 {
755 .plmn = { .mcc = 999, .mnc = 999, .mnc_3_digits = false },
756 .lac = 65535,
757 .rac = 255,
758 .expect = {
759 .digits = { 0x99, 0x99, 0x99 },
760 .lac = 0xffff,
761 .rac = 0xff,
762 },
763 },
764 {
765 .plmn = { .mcc = 909, .mnc = 90, .mnc_3_digits = false },
766 .lac = 0xabcd,
767 .rac = 0xab,
768 .expect = {
769 .digits = { 0x09, 0xf9, 0x09 },
770 .lac = 0xcdab,
771 .rac = 0xab,
772 },
773 },
774 {
775 .plmn = { .mcc = 909, .mnc = 90, .mnc_3_digits = true },
776 .lac = 0xabcd,
777 .rac = 0xab,
778 .expect = {
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100779 .digits = { 0x09, 0x09, 0x90 },
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100780 .lac = 0xcdab,
781 .rac = 0xab,
782 },
783 },
784};
785
786static void test_gsm48_ra_id_by_bts()
787{
788 int i;
789 bool pass = true;
790
791 for (i = 0; i < ARRAY_SIZE(test_gsm48_ra_id_by_bts_data); i++) {
792 struct gsm_network net;
793 struct gsm_bts bts;
794 const struct test_gsm48_ra_id_by_bts *t = &test_gsm48_ra_id_by_bts_data[i];
795 struct gsm48_ra_id result = {};
796 bool ok;
797
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100798 net.plmn = t->plmn;
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100799 bts.network = &net;
800 bts.location_area_code = t->lac;
801 bts.gprs.rac = t->rac;
802
Neels Hofmeyr4d358c02018-02-22 03:19:05 +0100803 gsm48_ra_id_by_bts(&result, &bts);
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100804
805 ok = (t->expect.digits[0] == result.digits[0])
806 && (t->expect.digits[1] == result.digits[1])
807 && (t->expect.digits[2] == result.digits[2])
808 && (t->expect.lac == result.lac)
809 && (t->expect.rac == result.rac);
810 printf("%s[%d]: digits='%02x%02x%02x' lac=0x%04x=htons(%u) rac=0x%02x=%u %s\n",
811 __func__, i,
812 result.digits[0], result.digits[1], result.digits[2],
813 result.lac, osmo_ntohs(result.lac), result.rac, result.rac,
814 ok ? "pass" : "FAIL");
815 pass = pass && ok;
816 }
817
818 OSMO_ASSERT(pass);
819}
820
Neels Hofmeyr7997bf42018-02-13 17:16:44 +0100821static const struct log_info_cat log_categories[] = {
822};
823
824static const struct log_info log_info = {
825 .cat = log_categories,
826 .num_cat = ARRAY_SIZE(log_categories),
827};
828
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800829int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000830{
Max4ab72682017-12-18 18:19:49 +0100831 struct gsm_network *net;
832
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100833 osmo_init_logging(&log_info);
834 log_set_log_level(osmo_stderr_target, LOGL_INFO);
835
Neels Hofmeyrf7a63292018-02-27 12:37:26 +0100836 net = bsc_network_init(tall_bsc_ctx);
Max4ab72682017-12-18 18:19:49 +0100837 if (!net) {
838 printf("Network init failure.\n");
839 return EXIT_FAILURE;
840 }
841
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200842 test_location_area_identifier();
843 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100844
845 test_si_range_helpers();
846 test_arfcn_filter();
847 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100848 test_range_encoding();
Harald Welteafedeab2010-03-04 10:55:40 +0100849
Max4ab72682017-12-18 18:19:49 +0100850 test_si2q_segfault(net);
851 test_si2q_e(net);
852 test_si2q_u(net);
853 test_si2q_mu(net);
854 test_si2q_long(net);
Max70fdd242017-06-15 15:10:53 +0200855
Max4ab72682017-12-18 18:19:49 +0100856 test_si_ba_ind(net);
Harald Welte14f97722017-10-01 11:09:47 +0800857
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100858 test_gsm48_ra_id_by_bts();
859
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100860 printf("Done.\n");
Max70fdd242017-06-15 15:10:53 +0200861
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100862 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000863}
Harald Welte3561bd42018-01-28 03:04:16 +0100864
865struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *net) {
866 OSMO_ASSERT(0);
867}