blob: a934806b50d57124ea2b708e51987c9e5d0a260d [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
Maxf39d03a2017-05-12 17:00:30 +020061static inline void gen(struct gsm_bts *bts, const char *s)
Max59a1bf32016-04-15 16:04:46 +020062{
Max845a3a42017-05-15 12:02:29 +020063 int r;
64
Max69e9c0d2016-05-18 13:04:47 +020065 bts->si_valid = 0;
66 bts->si_valid |= (1 << SYSINFO_TYPE_2quater);
Max845a3a42017-05-15 12:02:29 +020067
Max845a3a42017-05-15 12:02:29 +020068 printf("generating SI2quater for %zu EARFCNs and %zu UARFCNs...\n",
69 si2q_earfcn_count(&bts->si_common.si2quater_neigh_list), bts->si_common.uarfcn_length);
70
71 r = gsm_generate_si(bts, SYSINFO_TYPE_2quater);
Max59a1bf32016-04-15 16:04:46 +020072 if (r > 0)
Max70fdd242017-06-15 15:10:53 +020073 for (bts->si2q_index = 0; bts->si2q_index < bts->si2q_count + 1; bts->si2q_index++)
74 printf("generated %s SI2quater [%02u/%02u]: [%d] %s\n",
75 GSM_BTS_HAS_SI(bts, SYSINFO_TYPE_2quater) ? "valid" : "invalid",
76 bts->si2q_index, bts->si2q_count, r,
77 osmo_hexdump((void *)GSM_BTS_SI2Q(bts, bts->si2q_index), GSM_MACBLOCK_LEN));
Max59a1bf32016-04-15 16:04:46 +020078 else
Maxf39d03a2017-05-12 17:00:30 +020079 printf("%s() failed to generate SI2quater: %s\n", s, strerror(-r));
Max59a1bf32016-04-15 16:04:46 +020080}
81
Max845a3a42017-05-15 12:02:29 +020082static inline void del_earfcn_b(struct gsm_bts *bts, uint16_t earfcn)
83{
84 struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
85 int r = osmo_earfcn_del(e, earfcn);
86 if (r)
87 printf("failed to remove EARFCN %u: %s\n", earfcn, strerror(-r));
88 else
89 printf("removed EARFCN %u - ", earfcn);
90
91 gen(bts, __func__);
92}
93
Maxf39d03a2017-05-12 17:00:30 +020094static inline void add_earfcn_b(struct gsm_bts *bts, uint16_t earfcn, uint8_t bw)
Maxaafff962016-04-20 15:57:14 +020095{
Maxf39d03a2017-05-12 17:00:30 +020096 struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
97 int r = osmo_earfcn_add(e, earfcn, bw);
98 if (r)
99 printf("failed to add EARFCN %u: %s\n", earfcn, strerror(-r));
100 else
101 printf("added EARFCN %u - ", earfcn);
102
103 gen(bts, __func__);
104}
105
106static inline void _bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble, bool diversity)
107{
108 int r;
109
110 bts->u_offset = 0;
111
112 r = bts_uarfcn_add(bts, arfcn, scramble, diversity);
Maxaafff962016-04-20 15:57:14 +0200113 if (r < 0)
114 printf("failed to add UARFCN to SI2quater: %s\n", strerror(-r));
Max70fdd242017-06-15 15:10:53 +0200115 else {
116 bts->si2q_count = si2q_num(bts) - 1;
Maxf39d03a2017-05-12 17:00:30 +0200117 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200118 }
Maxaafff962016-04-20 15:57:14 +0200119}
120
Neels Hofmeyr68c76e92018-03-28 18:15:13 +0200121#define bts_init(net) _bts_init(net, __func__)
122static inline struct gsm_bts *_bts_init(struct gsm_network *net, const char *msg)
Max881064e2016-12-14 14:51:40 +0100123{
Max4ab72682017-12-18 18:19:49 +0100124 struct gsm_bts *bts = gsm_bts_alloc(net, 0);
125 if (!bts) {
126 printf("BTS allocation failure in %s()\n", msg);
Max881064e2016-12-14 14:51:40 +0100127 exit(1);
Max4ab72682017-12-18 18:19:49 +0100128 }
129 printf("BTS allocation OK in %s()\n", msg);
130
131 bts->network = net;
132
133 return bts;
134}
135
Neels Hofmeyrf2245092018-03-28 18:14:27 +0200136#define bts_del(bts) _bts_del(bts, __func__)
137static inline void _bts_del(struct gsm_bts *bts, const char *msg)
138{
139 osmo_stat_item_group_free(bts->bts_statg);
140 rate_ctr_group_free(bts->bts_ctrs);
141 /* no need to llist_del(&bts->list), we never registered the bts there. */
142 talloc_free(bts);
143 printf("BTS deallocated OK in %s()\n", msg);
144}
145
Max4ab72682017-12-18 18:19:49 +0100146static inline void test_si2q_segfault(struct gsm_network *net)
147{
Neels Hofmeyr68c76e92018-03-28 18:15:13 +0200148 struct gsm_bts *bts = bts_init(net);
Max4ab72682017-12-18 18:19:49 +0100149 printf("Test SI2quater UARFCN (same scrambling code and diversity):\n");
Max881064e2016-12-14 14:51:40 +0100150
151 _bts_uarfcn_add(bts, 10564, 319, 0);
152 _bts_uarfcn_add(bts, 10612, 319, 0);
Maxf39d03a2017-05-12 17:00:30 +0200153 gen(bts, __func__);
Max4ab72682017-12-18 18:19:49 +0100154
Neels Hofmeyrf2245092018-03-28 18:14:27 +0200155 bts_del(bts);
Max881064e2016-12-14 14:51:40 +0100156}
157
Max4ab72682017-12-18 18:19:49 +0100158static inline void test_si2q_mu(struct gsm_network *net)
Maxe610e702016-12-19 13:41:48 +0100159{
Neels Hofmeyr68c76e92018-03-28 18:15:13 +0200160 struct gsm_bts *bts = bts_init(net);
Maxe610e702016-12-19 13:41:48 +0100161 printf("Test SI2quater multiple UARFCNs:\n");
162
Maxe610e702016-12-19 13:41:48 +0100163 _bts_uarfcn_add(bts, 10564, 318, 0);
164 _bts_uarfcn_add(bts, 10612, 319, 0);
165 _bts_uarfcn_add(bts, 10612, 31, 0);
166 _bts_uarfcn_add(bts, 10612, 19, 0);
167 _bts_uarfcn_add(bts, 10613, 64, 0);
168 _bts_uarfcn_add(bts, 10613, 164, 0);
169 _bts_uarfcn_add(bts, 10613, 14, 0);
Max4ab72682017-12-18 18:19:49 +0100170
Neels Hofmeyrf2245092018-03-28 18:14:27 +0200171 bts_del(bts);
Maxe610e702016-12-19 13:41:48 +0100172}
173
Max4ab72682017-12-18 18:19:49 +0100174static inline void test_si2q_u(struct gsm_network *net)
Max59a1bf32016-04-15 16:04:46 +0200175{
Neels Hofmeyr68c76e92018-03-28 18:15:13 +0200176 struct gsm_bts *bts = bts_init(net);
Max26679e02016-04-20 15:57:13 +0200177 printf("Testing SYSINFO_TYPE_2quater UARFCN generation:\n");
178
Max69e9c0d2016-05-18 13:04:47 +0200179 /* first generate invalid SI as no UARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200180 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200181
Max69e9c0d2016-05-18 13:04:47 +0200182 /* subsequent calls should produce valid SI if there's enough memory */
Maxaafff962016-04-20 15:57:14 +0200183 _bts_uarfcn_add(bts, 1982, 13, 1);
184 _bts_uarfcn_add(bts, 1982, 44, 0);
185 _bts_uarfcn_add(bts, 1982, 61, 1);
186 _bts_uarfcn_add(bts, 1982, 89, 1);
187 _bts_uarfcn_add(bts, 1982, 113, 0);
188 _bts_uarfcn_add(bts, 1982, 123, 0);
189 _bts_uarfcn_add(bts, 1982, 56, 1);
190 _bts_uarfcn_add(bts, 1982, 72, 1);
191 _bts_uarfcn_add(bts, 1982, 223, 1);
192 _bts_uarfcn_add(bts, 1982, 14, 0);
193 _bts_uarfcn_add(bts, 1982, 88, 0);
Max4ab72682017-12-18 18:19:49 +0100194
Neels Hofmeyrf2245092018-03-28 18:14:27 +0200195 bts_del(bts);
Max26679e02016-04-20 15:57:13 +0200196}
197
Max4ab72682017-12-18 18:19:49 +0100198static inline void test_si2q_e(struct gsm_network *net)
Max26679e02016-04-20 15:57:13 +0200199{
Neels Hofmeyr68c76e92018-03-28 18:15:13 +0200200 struct gsm_bts *bts = bts_init(net);
Max26679e02016-04-20 15:57:13 +0200201 printf("Testing SYSINFO_TYPE_2quater EARFCN generation:\n");
Max59a1bf32016-04-15 16:04:46 +0200202
Maxf39d03a2017-05-12 17:00:30 +0200203 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
204 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
Max59a1bf32016-04-15 16:04:46 +0200205 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
206 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
207
208 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
Max70fdd242017-06-15 15:10:53 +0200209
Max69e9c0d2016-05-18 13:04:47 +0200210 /* first generate invalid SI as no EARFCN added */
Maxf39d03a2017-05-12 17:00:30 +0200211 gen(bts, __func__);
Max70fdd242017-06-15 15:10:53 +0200212
Max845a3a42017-05-15 12:02:29 +0200213 /* subsequent calls should produce valid SI if there's enough memory and EARFCNs */
214 add_earfcn_b(bts, 1917, 5);
215 del_earfcn_b(bts, 1917);
Maxf39d03a2017-05-12 17:00:30 +0200216 add_earfcn_b(bts, 1917, 1);
217 add_earfcn_b(bts, 1932, OSMO_EARFCN_MEAS_INVALID);
218 add_earfcn_b(bts, 1937, 2);
219 add_earfcn_b(bts, 1945, OSMO_EARFCN_MEAS_INVALID);
220 add_earfcn_b(bts, 1965, OSMO_EARFCN_MEAS_INVALID);
221 add_earfcn_b(bts, 1967, 4);
222 add_earfcn_b(bts, 1982, 3);
Max4ab72682017-12-18 18:19:49 +0100223
Neels Hofmeyrf2245092018-03-28 18:14:27 +0200224 bts_del(bts);
Max59a1bf32016-04-15 16:04:46 +0200225}
226
Max4ab72682017-12-18 18:19:49 +0100227static inline void test_si2q_long(struct gsm_network *net)
Max70fdd242017-06-15 15:10:53 +0200228{
Neels Hofmeyr68c76e92018-03-28 18:15:13 +0200229 struct gsm_bts *bts = bts_init(net);
Max70fdd242017-06-15 15:10:53 +0200230 printf("Testing SYSINFO_TYPE_2quater combined EARFCN & UARFCN generation:\n");
231
Max70fdd242017-06-15 15:10:53 +0200232 bts->si_common.si2quater_neigh_list.arfcn = bts->si_common.data.earfcn_list;
233 bts->si_common.si2quater_neigh_list.meas_bw = bts->si_common.data.meas_bw_list;
234 bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST;
235 bts->si_common.si2quater_neigh_list.thresh_hi = 5;
236
237 osmo_earfcn_init(&bts->si_common.si2quater_neigh_list);
238
239 bts_earfcn_add(bts, 1922, 11, 22, 8,32, 8);
240 bts_earfcn_add(bts, 1922, 11, 22, 8, 32, 8);
241 bts_earfcn_add(bts, 1924, 11, 12, 6, 11, 5);
242 bts_earfcn_add(bts, 1923, 11, 12, 6, 11, 5);
243 bts_earfcn_add(bts, 1925, 11, 12, 6, 11, 5);
244 bts_earfcn_add(bts, 2111, 11, 12, 6, 11, 5);
245 bts_earfcn_add(bts, 2112, 11, 12, 6, 11, 4);
246 bts_earfcn_add(bts, 2113, 11, 12, 6, 11, 3);
247 bts_earfcn_add(bts, 2114, 11, 12, 6, 11, 2);
248 bts_earfcn_add(bts, 2131, 11, 12, 6, 11, 5);
249 bts_earfcn_add(bts, 2132, 11, 12, 6, 11, 4);
250 bts_earfcn_add(bts, 2133, 11, 12, 6, 11, 3);
251 bts_earfcn_add(bts, 2134, 11, 12, 6, 11, 2);
252 bts_earfcn_add(bts, 2121, 11, 12, 6, 11, 5);
253 bts_earfcn_add(bts, 2122, 11, 12, 6, 11, 4);
254 bts_earfcn_add(bts, 2123, 11, 12, 6, 11, 3);
255 bts_earfcn_add(bts, 2124, 11, 12, 6, 11, 2);
256 _bts_uarfcn_add(bts, 1976, 13, 1);
257 _bts_uarfcn_add(bts, 1976, 38, 1);
258 _bts_uarfcn_add(bts, 1976, 44, 1);
259 _bts_uarfcn_add(bts, 1976, 120, 1);
260 _bts_uarfcn_add(bts, 1976, 140, 1);
261 _bts_uarfcn_add(bts, 1976, 163, 1);
262 _bts_uarfcn_add(bts, 1976, 166, 1);
263 _bts_uarfcn_add(bts, 1976, 217, 1);
264 _bts_uarfcn_add(bts, 1976, 224, 1);
265 _bts_uarfcn_add(bts, 1976, 225, 1);
266 _bts_uarfcn_add(bts, 1976, 226, 1);
Max4ab72682017-12-18 18:19:49 +0100267
Neels Hofmeyrf2245092018-03-28 18:14:27 +0200268 bts_del(bts);
Max70fdd242017-06-15 15:10:53 +0200269}
270
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200271static void test_mi_functionality(void)
272{
273 const char *imsi_odd = "987654321098763";
274 const char *imsi_even = "9876543210987654";
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200275 const uint32_t tmsi = 0xfabeacd0;
276 uint8_t mi[128];
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200277 unsigned int mi_len;
278 char mi_parsed[GSM48_MI_SIZE];
279
280 printf("Testing parsing and generating TMSI/IMSI\n");
281
282 /* tmsi code */
283 mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi);
284 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2);
Harald Welte3ad03462016-03-17 14:41:26 +0100285 COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200286
287 /* imsi code */
288 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd);
289 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200290 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200291 COMPARE_STR(mi_parsed, imsi_odd);
292
293 mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even);
294 gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200295 printf("hex: %s\n", osmo_hexdump(mi, mi_len));
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200296 COMPARE_STR(mi_parsed, imsi_even);
297}
298
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100299struct {
300 int range;
301 int arfcns_num;
302 int arfcns[RANGE_ENC_MAX_ARFCNS];
303} arfcn_test_ranges[] = {
304 {ARFCN_RANGE_512, 12,
305 { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
306 {ARFCN_RANGE_512, 17,
307 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
308 {ARFCN_RANGE_512, 18,
309 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
310 {ARFCN_RANGE_512, 18,
311 { 1, 17, 31, 45, 58, 79, 81, 97,
312 113, 127, 213, 277, 287, 311, 331, 391,
313 417, 511 }},
314 {ARFCN_RANGE_512, 6,
315 { 1, 17, 31, 45, 58, 79 }},
316 {ARFCN_RANGE_512, 6,
317 { 10, 17, 31, 45, 58, 79 }},
318 {ARFCN_RANGE_1024, 17,
319 { 0, 17, 31, 45, 58, 79, 81, 97,
320 113, 127, 213, 277, 287, 311, 331, 391,
321 1023 }},
322 {ARFCN_RANGE_1024, 16,
323 { 17, 31, 45, 58, 79, 81, 97, 113,
324 127, 213, 277, 287, 311, 331, 391, 1023 }},
325 {-1}
326};
327
328static int test_single_range_encoding(int range, const int *orig_arfcns,
329 int arfcns_num, int silent)
330{
331 int arfcns[RANGE_ENC_MAX_ARFCNS];
332 int w[RANGE_ENC_MAX_ARFCNS];
333 int f0_included = 0;
334 int rc, f0;
335 uint8_t chan_list[16] = {0};
336 struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
337 int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
338 int dec_arfcns_count = 0;
339 int arfcns_used = 0;
340 int i;
341
342 arfcns_used = arfcns_num;
343 memmove(arfcns, orig_arfcns, sizeof(arfcns));
344
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100345 f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0];
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100346 /*
347 * Manipulate the ARFCN list according to the rules in J4 depending
348 * on the selected range.
349 */
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100350 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100351 f0, &f0_included);
352
353 memset(w, 0, sizeof(w));
Max26679e02016-04-20 15:57:13 +0200354 range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100355
356 if (!silent)
357 fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n",
358 range, arfcns_used, f0, f0_included);
359
360 /* Select the range and the amount of bits needed */
361 switch (range) {
362 case ARFCN_RANGE_128:
Max26679e02016-04-20 15:57:13 +0200363 range_enc_range128(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100364 break;
365 case ARFCN_RANGE_256:
Max26679e02016-04-20 15:57:13 +0200366 range_enc_range256(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100367 break;
368 case ARFCN_RANGE_512:
Max26679e02016-04-20 15:57:13 +0200369 range_enc_range512(chan_list, f0, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100370 break;
371 case ARFCN_RANGE_1024:
Max26679e02016-04-20 15:57:13 +0200372 range_enc_range1024(chan_list, f0, f0_included, w);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100373 break;
374 default:
375 return 1;
376 };
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100377
378 if (!silent)
379 printf("chan_list = %s\n",
380 osmo_hexdump(chan_list, sizeof(chan_list)));
381
382 rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
383 0xfe, 1);
384 if (rc != 0) {
385 printf("Cannot decode freq list, rc = %d\n", rc);
386 return 1;
387 }
388
389 for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
390 if (dec_freq[i].mask &&
391 dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
392 dec_arfcns[dec_arfcns_count++] = i;
393 }
394
395 if (!silent) {
396 printf("Decoded freqs %d (expected %d)\n",
397 dec_arfcns_count, arfcns_num);
398 printf("Decoded: ");
399 for (i = 0; i < dec_arfcns_count; i++) {
400 printf("%d ", dec_arfcns[i]);
401 if (dec_arfcns[i] != orig_arfcns[i])
402 printf("(!= %d) ", orig_arfcns[i]);
403 }
404 printf("\n");
405 }
406
407 if (dec_arfcns_count != arfcns_num) {
408 printf("Wrong number of arfcns\n");
409 return 1;
410 }
411
412 if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
413 printf("Decoding error, got wrong freqs\n");
414 fprintf(stderr, " w = ");
415 for (i = 0; i < ARRAY_SIZE(w); i++)
416 fprintf(stderr, "%d ", w[i]);
417 fprintf(stderr, "\n");
418 return 1;
419 }
420
421 return 0;
422}
423
424static void test_random_range_encoding(int range, int max_arfcn_num)
425{
426 int arfcns_num = 0;
427 int test_idx;
428 int rc, max_count;
429 int num_tests = 1024;
430
431 printf("Random range test: range %d, max num ARFCNs %d\n",
432 range, max_arfcn_num);
433
434 srandom(1);
435
436 for (max_count = 1; max_count < max_arfcn_num; max_count++) {
437 for (test_idx = 0; test_idx < num_tests; test_idx++) {
438 int count;
439 int i;
440 int min_freq = 0;
441
442 int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
443 char rnd_arfcns_set[1024] = {0};
444
445 if (range < ARFCN_RANGE_1024)
446 min_freq = random() % (1023 - range);
447
448 for (count = max_count; count; ) {
449 int arfcn = min_freq + random() % (range + 1);
450 OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
451
452 if (!rnd_arfcns_set[arfcn]) {
453 rnd_arfcns_set[arfcn] = 1;
454 count -= 1;
455 }
456 }
457
458 arfcns_num = 0;
459 for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
460 if (rnd_arfcns_set[i])
461 rnd_arfcns[arfcns_num++] = i;
462
463 rc = test_single_range_encoding(range, rnd_arfcns,
464 arfcns_num, 1);
465 if (rc != 0) {
466 printf("Failed on test %d, range %d, num ARFCNs %d\n",
467 test_idx, range, max_count);
468 test_single_range_encoding(range, rnd_arfcns,
469 arfcns_num, 0);
470 return;
471 }
472 }
473 }
474}
475
476static void test_range_encoding()
477{
478 int *arfcns;
479 int arfcns_num = 0;
480 int test_idx;
481 int range;
482
483 for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++)
484 {
485 arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
486 arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
487 range = arfcn_test_ranges[test_idx].range;
488
489 printf("Range test %d: range %d, num ARFCNs %d\n",
490 test_idx, range, arfcns_num);
491
492 test_single_range_encoding(range, arfcns, arfcns_num, 0);
493 }
494
495 test_random_range_encoding(ARFCN_RANGE_128, 29);
496 test_random_range_encoding(ARFCN_RANGE_256, 22);
497 test_random_range_encoding(ARFCN_RANGE_512, 18);
498 test_random_range_encoding(ARFCN_RANGE_1024, 16);
499}
500
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100501static int freqs1[] = {
502 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980
503};
504
505static int freqs2[] = {
506 402, 460, 1, 67, 131, 197, 272, 347,
507};
508
509static int freqs3[] = {
510 68, 128, 198, 279, 353, 398, 452,
511
512};
513
514static int w_out[] = {
515 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9,
516};
517
518static int range128[] = {
519 1, 1 + 127,
520};
521
522static int range256[] = {
523 1, 1 + 128,
524};
525
526static int range512[] = {
527 1, 1+ 511,
528};
529
530
531static void test_arfcn_filter()
532{
533 int arfcns[50], i, res, f0_included;
534 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
535 arfcns[i] = (i + 1) * 2;
536
537 /* check that the arfcn is taken out. f0_included is only set for Range1024 */
538 f0_included = 24;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100539 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100540 arfcns[0], &f0_included);
541 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
542 VERIFY(f0_included, ==, 1);
543 for (i = 0; i < res; ++i)
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100544 VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1));
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100545
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100546 /* check with range1024, ARFCN 0 is included */
547 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
548 arfcns[i] = i * 2;
549 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
550 0, &f0_included);
551 VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1);
552 VERIFY(f0_included, ==, 1);
553 for (i = 0; i < res; ++i)
554 VERIFY(arfcns[i], ==, (i + 1) * 2 - 1);
555
556 /* check with range1024, ARFCN 0 not included */
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100557 for (i = 0; i < ARRAY_SIZE(arfcns); ++i)
558 arfcns[i] = (i + 1) * 2;
Jacob Erlbeck45014a02014-01-14 10:42:58 +0100559 res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns),
560 0, &f0_included);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100561 VERIFY(res, ==, ARRAY_SIZE(arfcns));
562 VERIFY(f0_included, ==, 0);
563 for (i = 0; i < res; ++i)
564 VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1);
565}
566
567static void test_print_encoding()
568{
569 int rc;
570 int w[17];
571 uint8_t chan_list[16];
572 memset(chan_list, 0x23, sizeof(chan_list));
573
574 for (rc = 0; rc < ARRAY_SIZE(w); ++rc)
575 switch (rc % 3) {
576 case 0:
577 w[rc] = 0xAAAA;
578 break;
579 case 1:
580 w[rc] = 0x5555;
581 break;
582 case 2:
583 w[rc] = 0x9696;
584 break;
585 }
586
Max26679e02016-04-20 15:57:13 +0200587 range_enc_range512(chan_list, (1 << 9) | 0x96, w);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100588
589 printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list)));
590}
591
592static void test_si_range_helpers()
593{
594 int ws[(sizeof(freqs1)/sizeof(freqs1[0]))];
595 int i, f0 = 0xFFFFFF;
596
597 memset(&ws[0], 0x23, sizeof(ws));
598
599 i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100600 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100601 VERIFY(i, ==, 2);
602
603 i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100604 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100605 VERIFY(i, ==, 2);
606
607 i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3));
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100608 printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100609 VERIFY(i, ==, 0);
610
Max26679e02016-04-20 15:57:13 +0200611 range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0);
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100612
613 for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) {
614 printf("w[%d]=%d\n", i, ws[i]);
615 VERIFY(ws[i], ==, w_out[i]);
616 }
617
618 i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0);
619 VERIFY(i, ==, ARFCN_RANGE_128);
620 VERIFY(f0, ==, 1);
621
622 i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0);
623 VERIFY(i, ==, ARFCN_RANGE_256);
624 VERIFY(f0, ==, 1);
625
626 i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0);
627 VERIFY(i, ==, ARFCN_RANGE_512);
628 VERIFY(f0, ==, 1);
629}
630
Max4ab72682017-12-18 18:19:49 +0100631static void test_si_ba_ind(struct gsm_network *net)
Harald Welte14f97722017-10-01 11:09:47 +0800632{
Neels Hofmeyr68c76e92018-03-28 18:15:13 +0200633 struct gsm_bts *bts = bts_init(net);
Max4ab72682017-12-18 18:19:49 +0100634
Harald Welte14f97722017-10-01 11:09:47 +0800635 const struct gsm48_system_information_type_2 *si2 =
636 (struct gsm48_system_information_type_2 *) GSM_BTS_SI(bts, SYSINFO_TYPE_2);
637 const struct gsm48_system_information_type_2bis *si2bis =
638 (struct gsm48_system_information_type_2bis *) GSM_BTS_SI(bts, SYSINFO_TYPE_2bis);
639 const struct gsm48_system_information_type_2ter *si2ter =
640 (struct gsm48_system_information_type_2ter *) GSM_BTS_SI(bts, SYSINFO_TYPE_2ter);
641 const struct gsm48_system_information_type_5 *si5 =
642 (struct gsm48_system_information_type_5 *) GSM_BTS_SI(bts, SYSINFO_TYPE_5);
643 const struct gsm48_system_information_type_5bis *si5bis =
644 (struct gsm48_system_information_type_5bis *) GSM_BTS_SI(bts, SYSINFO_TYPE_5bis);
645 const struct gsm48_system_information_type_5ter *si5ter =
646 (struct gsm48_system_information_type_5ter *) GSM_BTS_SI(bts, SYSINFO_TYPE_5ter);
647
648 int rc;
649
Harald Welte14f97722017-10-01 11:09:47 +0800650 bts->c0->arfcn = 23;
651
652 printf("Testing if BA-IND is set as expected in SI2xxx and SI5xxx\n");
653
654 rc = gsm_generate_si(bts, SYSINFO_TYPE_2);
655 OSMO_ASSERT(rc > 0);
656 printf("SI2: %s\n", osmo_hexdump((uint8_t *)si2, rc));
657 /* Validate BA-IND == 0 */
658 OSMO_ASSERT(!(si2->bcch_frequency_list[0] & 0x10));
659
660 rc = gsm_generate_si(bts, SYSINFO_TYPE_2bis);
661 OSMO_ASSERT(rc > 0);
662 printf("SI2bis: %s\n", osmo_hexdump((uint8_t *)si2bis, rc));
663 /* Validate BA-IND == 0 */
664 OSMO_ASSERT(!(si2bis->bcch_frequency_list[0] & 0x10));
665
666 rc = gsm_generate_si(bts, SYSINFO_TYPE_2ter);
667 OSMO_ASSERT(rc > 0);
668 printf("SI2ter: %s\n", osmo_hexdump((uint8_t *)si2ter, rc));
669 /* Validate BA-IND == 0 */
670 OSMO_ASSERT(!(si2ter->ext_bcch_frequency_list[0] & 0x10));
671
672 rc = gsm_generate_si(bts, SYSINFO_TYPE_5);
673 OSMO_ASSERT(rc > 0);
674 printf("SI5: %s\n", osmo_hexdump((uint8_t *)si5, rc));
675 /* Validate BA-IND == 1 */
676 OSMO_ASSERT(si5->bcch_frequency_list[0] & 0x10);
677
678 rc = gsm_generate_si(bts, SYSINFO_TYPE_5bis);
679 OSMO_ASSERT(rc > 0);
680 printf("SI5bis: %s\n", osmo_hexdump((uint8_t *)si5bis, rc));
681 /* Validate BA-IND == 1 */
682 OSMO_ASSERT(si5bis->bcch_frequency_list[0] & 0x10);
683
684 rc = gsm_generate_si(bts, SYSINFO_TYPE_5ter);
685 OSMO_ASSERT(rc > 0);
686 printf("SI5ter: %s\n", osmo_hexdump((uint8_t *)si5ter, rc));
687 /* Validate BA-IND == 1 */
688 OSMO_ASSERT(si5ter->bcch_frequency_list[0] & 0x10);
Neels Hofmeyrf2245092018-03-28 18:14:27 +0200689
690 bts_del(bts);
Harald Welte14f97722017-10-01 11:09:47 +0800691}
692
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100693struct test_gsm48_ra_id_by_bts {
694 struct osmo_plmn_id plmn;
695 uint16_t lac;
696 uint8_t rac;
697 struct gsm48_ra_id expect;
698};
699static const struct test_gsm48_ra_id_by_bts test_gsm48_ra_id_by_bts_data[] = {
700 {
701 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = false },
702 .lac = 3,
703 .rac = 4,
704 .expect = {
705 .digits = { 0x00, 0xf1, 0x20 },
706 .lac = 0x0300, /* network byte order of 3 */
707 .rac = 4,
708 },
709 },
710 {
711 .plmn = { .mcc = 1, .mnc = 2, .mnc_3_digits = true },
712 .lac = 3,
713 .rac = 4,
714 .expect = {
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100715 .digits = { 0x00, 0x21, 0x00 },
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100716 .lac = 0x0300, /* network byte order of 3 */
717 .rac = 4,
718 },
719 },
720 {
721 .plmn = { .mcc = 0, .mnc = 0, .mnc_3_digits = false },
722 .lac = 0,
723 .rac = 0,
724 .expect = {
725 .digits = { 0x00, 0xf0, 0x00 },
726 },
727 },
728 {
729 .plmn = { .mcc = 0, .mnc = 0, .mnc_3_digits = true },
730 .lac = 0,
731 .rac = 0,
732 .expect = {
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100733 .digits = {},
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100734 },
735 },
736 {
737 .plmn = { .mcc = 999, .mnc = 999, .mnc_3_digits = false },
738 .lac = 65535,
739 .rac = 255,
740 .expect = {
741 .digits = { 0x99, 0x99, 0x99 },
742 .lac = 0xffff,
743 .rac = 0xff,
744 },
745 },
746 {
747 .plmn = { .mcc = 909, .mnc = 90, .mnc_3_digits = false },
748 .lac = 0xabcd,
749 .rac = 0xab,
750 .expect = {
751 .digits = { 0x09, 0xf9, 0x09 },
752 .lac = 0xcdab,
753 .rac = 0xab,
754 },
755 },
756 {
757 .plmn = { .mcc = 909, .mnc = 90, .mnc_3_digits = true },
758 .lac = 0xabcd,
759 .rac = 0xab,
760 .expect = {
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100761 .digits = { 0x09, 0x09, 0x90 },
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100762 .lac = 0xcdab,
763 .rac = 0xab,
764 },
765 },
766};
767
768static void test_gsm48_ra_id_by_bts()
769{
770 int i;
771 bool pass = true;
772
773 for (i = 0; i < ARRAY_SIZE(test_gsm48_ra_id_by_bts_data); i++) {
774 struct gsm_network net;
775 struct gsm_bts bts;
776 const struct test_gsm48_ra_id_by_bts *t = &test_gsm48_ra_id_by_bts_data[i];
777 struct gsm48_ra_id result = {};
778 bool ok;
779
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100780 net.plmn = t->plmn;
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100781 bts.network = &net;
782 bts.location_area_code = t->lac;
783 bts.gprs.rac = t->rac;
784
Neels Hofmeyr4d358c02018-02-22 03:19:05 +0100785 gsm48_ra_id_by_bts(&result, &bts);
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100786
787 ok = (t->expect.digits[0] == result.digits[0])
788 && (t->expect.digits[1] == result.digits[1])
789 && (t->expect.digits[2] == result.digits[2])
790 && (t->expect.lac == result.lac)
791 && (t->expect.rac == result.rac);
792 printf("%s[%d]: digits='%02x%02x%02x' lac=0x%04x=htons(%u) rac=0x%02x=%u %s\n",
793 __func__, i,
794 result.digits[0], result.digits[1], result.digits[2],
795 result.lac, osmo_ntohs(result.lac), result.rac, result.rac,
796 ok ? "pass" : "FAIL");
797 pass = pass && ok;
798 }
799
800 OSMO_ASSERT(pass);
801}
802
Neels Hofmeyr7997bf42018-02-13 17:16:44 +0100803static const struct log_info_cat log_categories[] = {
804};
805
806static const struct log_info log_info = {
807 .cat = log_categories,
808 .num_cat = ARRAY_SIZE(log_categories),
809};
810
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800811int main(int argc, char **argv)
Holger Freytheraa0fb362008-12-28 21:55:40 +0000812{
Max4ab72682017-12-18 18:19:49 +0100813 struct gsm_network *net;
814
Neels Hofmeyre3416182018-03-05 05:31:14 +0100815 tall_bsc_ctx = talloc_named_const(NULL, 0, "gsm0408_test");
816
817 osmo_init_logging2(tall_bsc_ctx, &log_info);
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100818 log_set_log_level(osmo_stderr_target, LOGL_INFO);
819
Neels Hofmeyrf7a63292018-02-27 12:37:26 +0100820 net = bsc_network_init(tall_bsc_ctx);
Max4ab72682017-12-18 18:19:49 +0100821 if (!net) {
822 printf("Network init failure.\n");
823 return EXIT_FAILURE;
824 }
825
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200826 test_mi_functionality();
Jacob Erlbeck9444d4f2014-01-13 14:43:40 +0100827
828 test_si_range_helpers();
829 test_arfcn_filter();
830 test_print_encoding();
Jacob Erlbeck4b903b42014-01-10 17:43:41 +0100831 test_range_encoding();
Harald Welteafedeab2010-03-04 10:55:40 +0100832
Max4ab72682017-12-18 18:19:49 +0100833 test_si2q_segfault(net);
834 test_si2q_e(net);
835 test_si2q_u(net);
836 test_si2q_mu(net);
837 test_si2q_long(net);
Max70fdd242017-06-15 15:10:53 +0200838
Max4ab72682017-12-18 18:19:49 +0100839 test_si_ba_ind(net);
Harald Welte14f97722017-10-01 11:09:47 +0800840
Neels Hofmeyra05360c2018-03-04 23:45:05 +0100841 test_gsm48_ra_id_by_bts();
842
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100843 printf("Done.\n");
Max70fdd242017-06-15 15:10:53 +0200844
Holger Hans Peter Freyther300457b2012-01-06 15:03:28 +0100845 return EXIT_SUCCESS;
Holger Freytheraa0fb362008-12-28 21:55:40 +0000846}
Harald Welte3561bd42018-01-28 03:04:16 +0100847
848struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *net) {
849 OSMO_ASSERT(0);
850}