Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 1 | /* 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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * 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 Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 9 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * 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 Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 25 | #include <arpa/inet.h> |
| 26 | |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 27 | #include <openbsc/gsm_04_08.h> |
Holger Hans Peter Freyther | ca11443 | 2014-02-08 15:20:48 +0100 | [diff] [blame] | 28 | #include <openbsc/gsm_04_11.h> |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
| 30 | #include <openbsc/debug.h> |
Jacob Erlbeck | 4b903b4 | 2014-01-10 17:43:41 +0100 | [diff] [blame] | 31 | #include <openbsc/arfcn_range_encode.h> |
Max | 59a1bf3 | 2016-04-15 16:04:46 +0200 | [diff] [blame] | 32 | #include <openbsc/system_information.h> |
| 33 | #include <openbsc/abis_rsl.h> |
| 34 | |
Jacob Erlbeck | 4b903b4 | 2014-01-10 17:43:41 +0100 | [diff] [blame] | 35 | #include <osmocom/core/application.h> |
Max | 59a1bf3 | 2016-04-15 16:04:46 +0200 | [diff] [blame] | 36 | #include <osmocom/gsm/sysinfo.h> |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 37 | |
| 38 | #define COMPARE(result, op, value) \ |
| 39 | if (!((result) op (value))) {\ |
| 40 | fprintf(stderr, "Compare failed. Was %x should be %x in %s:%d\n",result, value, __FILE__, __LINE__); \ |
| 41 | exit(-1); \ |
| 42 | } |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 43 | |
| 44 | #define COMPARE_STR(result, value) \ |
| 45 | if (strcmp(result, value) != 0) { \ |
| 46 | fprintf(stderr, "Compare failed. Was %s should be %s in %s:%d\n",result, value, __FILE__, __LINE__); \ |
| 47 | exit(-1); \ |
| 48 | } |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 49 | |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 50 | #define DBG(...) |
| 51 | |
| 52 | #define VERIFY(res, cmp, wanted) \ |
| 53 | if (!(res cmp wanted)) { \ |
| 54 | printf("ASSERT failed: %s:%d Wanted: %d %s %d\n", \ |
Holger Hans Peter Freyther | daaea0c | 2015-08-03 09:28:41 +0200 | [diff] [blame] | 55 | __FILE__, __LINE__, (int) res, # cmp, (int) wanted); \ |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
| 59 | |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 60 | /* |
| 61 | * Test Location Area Identifier formatting. Table 10.5.3 of 04.08 |
| 62 | */ |
| 63 | static void test_location_area_identifier(void) |
| 64 | { |
| 65 | struct gsm48_loc_area_id lai48; |
| 66 | |
| 67 | printf("Testing test location area identifier\n"); |
| 68 | |
| 69 | /* |
| 70 | * Test the default/test setup. Coming from |
| 71 | * bsc_hack.c dumps |
| 72 | */ |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 73 | gsm48_generate_lai(&lai48, 1, 1, 1); |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 74 | COMPARE(lai48.digits[0], ==, 0x00); |
| 75 | COMPARE(lai48.digits[1], ==, 0xF1); |
| 76 | COMPARE(lai48.digits[2], ==, 0x10); |
| 77 | COMPARE(lai48.lac, ==, htons(0x0001)); |
| 78 | |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 79 | gsm48_generate_lai(&lai48, 602, 1, 15); |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 80 | COMPARE(lai48.digits[0], ==, 0x06); |
| 81 | COMPARE(lai48.digits[1], ==, 0xF2); |
| 82 | COMPARE(lai48.digits[2], ==, 0x10); |
| 83 | COMPARE(lai48.lac, ==, htons(0x000f)); |
| 84 | } |
| 85 | |
Max | 59a1bf3 | 2016-04-15 16:04:46 +0200 | [diff] [blame] | 86 | static inline void add_arfcn_b(struct osmo_earfcn_si2q *e, uint16_t earfcn, |
| 87 | uint8_t bw) |
| 88 | { |
| 89 | int r = osmo_earfcn_add(e, earfcn, bw); |
| 90 | if (r) |
| 91 | printf("failed to add EARFCN %u: %s\n", earfcn, strerror(r)); |
| 92 | else |
| 93 | printf("added EARFCN %u - ", earfcn); |
| 94 | } |
| 95 | |
| 96 | static inline void gen(struct gsm_bts *bts) |
| 97 | { |
| 98 | int r = gsm_generate_si(bts, SYSINFO_TYPE_2quater); |
| 99 | if (r > 0) |
| 100 | printf("generated SI2quater: [%d] %s\n", r, |
| 101 | osmo_hexdump(bts->si_buf[SYSINFO_TYPE_2quater], r)); |
| 102 | else |
| 103 | printf("failed to generate SI2quater: %s\n", strerror(-r)); |
| 104 | } |
| 105 | |
| 106 | static inline void test_si2q(void) |
| 107 | { |
| 108 | struct gsm_bts *bts; |
| 109 | struct gsm_network *network = gsm_network_init(1, 1, NULL); |
| 110 | printf("Testing SYSINFO_TYPE_2quater generation:\n"); |
| 111 | |
| 112 | if (!network) |
| 113 | exit(1); |
| 114 | bts = gsm_bts_alloc(network); |
| 115 | |
| 116 | bts->si_common.si2quater_neigh_list.arfcn = |
| 117 | bts->si_common.data.earfcn_list; |
| 118 | bts->si_common.si2quater_neigh_list.meas_bw = |
| 119 | bts->si_common.data.meas_bw_list; |
| 120 | bts->si_common.si2quater_neigh_list.length = MAX_EARFCN_LIST; |
| 121 | bts->si_common.si2quater_neigh_list.thresh_hi = 5; |
| 122 | |
| 123 | osmo_earfcn_init(&bts->si_common.si2quater_neigh_list); |
| 124 | |
| 125 | add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1917, 1); |
| 126 | gen(bts); |
| 127 | |
| 128 | add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1932, |
| 129 | OSMO_EARFCN_MEAS_INVALID); |
| 130 | gen(bts); |
| 131 | |
| 132 | add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1937, 2); |
| 133 | gen(bts); |
| 134 | |
| 135 | add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1945, |
| 136 | OSMO_EARFCN_MEAS_INVALID); |
| 137 | gen(bts); |
| 138 | |
| 139 | add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1965, |
| 140 | OSMO_EARFCN_MEAS_INVALID); |
| 141 | gen(bts); |
| 142 | |
| 143 | add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1967, 4); |
| 144 | gen(bts); |
| 145 | |
| 146 | add_arfcn_b(&bts->si_common.si2quater_neigh_list, 1982, 3); |
| 147 | gen(bts); |
| 148 | } |
| 149 | |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 150 | static void test_mi_functionality(void) |
| 151 | { |
| 152 | const char *imsi_odd = "987654321098763"; |
| 153 | const char *imsi_even = "9876543210987654"; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 154 | const uint32_t tmsi = 0xfabeacd0; |
| 155 | uint8_t mi[128]; |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 156 | unsigned int mi_len; |
| 157 | char mi_parsed[GSM48_MI_SIZE]; |
| 158 | |
| 159 | printf("Testing parsing and generating TMSI/IMSI\n"); |
| 160 | |
| 161 | /* tmsi code */ |
| 162 | mi_len = gsm48_generate_mid_from_tmsi(mi, tmsi); |
| 163 | gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len - 2); |
Harald Welte | 3ad0346 | 2016-03-17 14:41:26 +0100 | [diff] [blame] | 164 | COMPARE((uint32_t)strtoul(mi_parsed, NULL, 10), ==, tmsi); |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 165 | |
| 166 | /* imsi code */ |
| 167 | mi_len = gsm48_generate_mid_from_imsi(mi, imsi_odd); |
| 168 | gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2); |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 169 | printf("hex: %s\n", osmo_hexdump(mi, mi_len)); |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 170 | COMPARE_STR(mi_parsed, imsi_odd); |
| 171 | |
| 172 | mi_len = gsm48_generate_mid_from_imsi(mi, imsi_even); |
| 173 | gsm48_mi_to_string(mi_parsed, sizeof(mi_parsed), mi + 2, mi_len -2); |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 174 | printf("hex: %s\n", osmo_hexdump(mi, mi_len)); |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 175 | COMPARE_STR(mi_parsed, imsi_even); |
| 176 | } |
| 177 | |
Jacob Erlbeck | 4b903b4 | 2014-01-10 17:43:41 +0100 | [diff] [blame] | 178 | struct { |
| 179 | int range; |
| 180 | int arfcns_num; |
| 181 | int arfcns[RANGE_ENC_MAX_ARFCNS]; |
| 182 | } arfcn_test_ranges[] = { |
| 183 | {ARFCN_RANGE_512, 12, |
| 184 | { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }}, |
| 185 | {ARFCN_RANGE_512, 17, |
| 186 | { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }}, |
| 187 | {ARFCN_RANGE_512, 18, |
| 188 | { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }}, |
| 189 | {ARFCN_RANGE_512, 18, |
| 190 | { 1, 17, 31, 45, 58, 79, 81, 97, |
| 191 | 113, 127, 213, 277, 287, 311, 331, 391, |
| 192 | 417, 511 }}, |
| 193 | {ARFCN_RANGE_512, 6, |
| 194 | { 1, 17, 31, 45, 58, 79 }}, |
| 195 | {ARFCN_RANGE_512, 6, |
| 196 | { 10, 17, 31, 45, 58, 79 }}, |
| 197 | {ARFCN_RANGE_1024, 17, |
| 198 | { 0, 17, 31, 45, 58, 79, 81, 97, |
| 199 | 113, 127, 213, 277, 287, 311, 331, 391, |
| 200 | 1023 }}, |
| 201 | {ARFCN_RANGE_1024, 16, |
| 202 | { 17, 31, 45, 58, 79, 81, 97, 113, |
| 203 | 127, 213, 277, 287, 311, 331, 391, 1023 }}, |
| 204 | {-1} |
| 205 | }; |
| 206 | |
| 207 | static int test_single_range_encoding(int range, const int *orig_arfcns, |
| 208 | int arfcns_num, int silent) |
| 209 | { |
| 210 | int arfcns[RANGE_ENC_MAX_ARFCNS]; |
| 211 | int w[RANGE_ENC_MAX_ARFCNS]; |
| 212 | int f0_included = 0; |
| 213 | int rc, f0; |
| 214 | uint8_t chan_list[16] = {0}; |
| 215 | struct gsm_sysinfo_freq dec_freq[1024] = {{0}}; |
| 216 | int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0}; |
| 217 | int dec_arfcns_count = 0; |
| 218 | int arfcns_used = 0; |
| 219 | int i; |
| 220 | |
| 221 | arfcns_used = arfcns_num; |
| 222 | memmove(arfcns, orig_arfcns, sizeof(arfcns)); |
| 223 | |
Jacob Erlbeck | 45014a0 | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 224 | f0 = range == ARFCN_RANGE_1024 ? 0 : arfcns[0]; |
Jacob Erlbeck | 4b903b4 | 2014-01-10 17:43:41 +0100 | [diff] [blame] | 225 | /* |
| 226 | * Manipulate the ARFCN list according to the rules in J4 depending |
| 227 | * on the selected range. |
| 228 | */ |
Jacob Erlbeck | 45014a0 | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 229 | arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used, |
Jacob Erlbeck | 4b903b4 | 2014-01-10 17:43:41 +0100 | [diff] [blame] | 230 | f0, &f0_included); |
| 231 | |
| 232 | memset(w, 0, sizeof(w)); |
| 233 | rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0); |
| 234 | if (rc != 0) { |
| 235 | printf("Cannot compute range W(k), rc = %d\n", rc); |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | if (!silent) |
| 240 | fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, f0_included=%d\n", |
| 241 | range, arfcns_used, f0, f0_included); |
| 242 | |
| 243 | /* Select the range and the amount of bits needed */ |
| 244 | switch (range) { |
| 245 | case ARFCN_RANGE_128: |
| 246 | rc = range_enc_range128(chan_list, f0, w); |
| 247 | break; |
| 248 | case ARFCN_RANGE_256: |
| 249 | rc = range_enc_range256(chan_list, f0, w); |
| 250 | break; |
| 251 | case ARFCN_RANGE_512: |
| 252 | rc = range_enc_range512(chan_list, f0, w); |
| 253 | break; |
| 254 | case ARFCN_RANGE_1024: |
| 255 | rc = range_enc_range1024(chan_list, f0, f0_included, w); |
| 256 | break; |
| 257 | default: |
| 258 | return 1; |
| 259 | }; |
| 260 | if (rc != 0) { |
| 261 | printf("Cannot encode range, rc = %d\n", rc); |
| 262 | return 1; |
| 263 | } |
| 264 | |
| 265 | if (!silent) |
| 266 | printf("chan_list = %s\n", |
| 267 | osmo_hexdump(chan_list, sizeof(chan_list))); |
| 268 | |
| 269 | rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list), |
| 270 | 0xfe, 1); |
| 271 | if (rc != 0) { |
| 272 | printf("Cannot decode freq list, rc = %d\n", rc); |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | for (i = 0; i < ARRAY_SIZE(dec_freq); i++) { |
| 277 | if (dec_freq[i].mask && |
| 278 | dec_arfcns_count < ARRAY_SIZE(dec_arfcns)) |
| 279 | dec_arfcns[dec_arfcns_count++] = i; |
| 280 | } |
| 281 | |
| 282 | if (!silent) { |
| 283 | printf("Decoded freqs %d (expected %d)\n", |
| 284 | dec_arfcns_count, arfcns_num); |
| 285 | printf("Decoded: "); |
| 286 | for (i = 0; i < dec_arfcns_count; i++) { |
| 287 | printf("%d ", dec_arfcns[i]); |
| 288 | if (dec_arfcns[i] != orig_arfcns[i]) |
| 289 | printf("(!= %d) ", orig_arfcns[i]); |
| 290 | } |
| 291 | printf("\n"); |
| 292 | } |
| 293 | |
| 294 | if (dec_arfcns_count != arfcns_num) { |
| 295 | printf("Wrong number of arfcns\n"); |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) { |
| 300 | printf("Decoding error, got wrong freqs\n"); |
| 301 | fprintf(stderr, " w = "); |
| 302 | for (i = 0; i < ARRAY_SIZE(w); i++) |
| 303 | fprintf(stderr, "%d ", w[i]); |
| 304 | fprintf(stderr, "\n"); |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static void test_random_range_encoding(int range, int max_arfcn_num) |
| 312 | { |
| 313 | int arfcns_num = 0; |
| 314 | int test_idx; |
| 315 | int rc, max_count; |
| 316 | int num_tests = 1024; |
| 317 | |
| 318 | printf("Random range test: range %d, max num ARFCNs %d\n", |
| 319 | range, max_arfcn_num); |
| 320 | |
| 321 | srandom(1); |
| 322 | |
| 323 | for (max_count = 1; max_count < max_arfcn_num; max_count++) { |
| 324 | for (test_idx = 0; test_idx < num_tests; test_idx++) { |
| 325 | int count; |
| 326 | int i; |
| 327 | int min_freq = 0; |
| 328 | |
| 329 | int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0}; |
| 330 | char rnd_arfcns_set[1024] = {0}; |
| 331 | |
| 332 | if (range < ARFCN_RANGE_1024) |
| 333 | min_freq = random() % (1023 - range); |
| 334 | |
| 335 | for (count = max_count; count; ) { |
| 336 | int arfcn = min_freq + random() % (range + 1); |
| 337 | OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set)); |
| 338 | |
| 339 | if (!rnd_arfcns_set[arfcn]) { |
| 340 | rnd_arfcns_set[arfcn] = 1; |
| 341 | count -= 1; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | arfcns_num = 0; |
| 346 | for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++) |
| 347 | if (rnd_arfcns_set[i]) |
| 348 | rnd_arfcns[arfcns_num++] = i; |
| 349 | |
| 350 | rc = test_single_range_encoding(range, rnd_arfcns, |
| 351 | arfcns_num, 1); |
| 352 | if (rc != 0) { |
| 353 | printf("Failed on test %d, range %d, num ARFCNs %d\n", |
| 354 | test_idx, range, max_count); |
| 355 | test_single_range_encoding(range, rnd_arfcns, |
| 356 | arfcns_num, 0); |
| 357 | return; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | static void test_range_encoding() |
| 364 | { |
| 365 | int *arfcns; |
| 366 | int arfcns_num = 0; |
| 367 | int test_idx; |
| 368 | int range; |
| 369 | |
| 370 | for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; test_idx++) |
| 371 | { |
| 372 | arfcns_num = arfcn_test_ranges[test_idx].arfcns_num; |
| 373 | arfcns = &arfcn_test_ranges[test_idx].arfcns[0]; |
| 374 | range = arfcn_test_ranges[test_idx].range; |
| 375 | |
| 376 | printf("Range test %d: range %d, num ARFCNs %d\n", |
| 377 | test_idx, range, arfcns_num); |
| 378 | |
| 379 | test_single_range_encoding(range, arfcns, arfcns_num, 0); |
| 380 | } |
| 381 | |
| 382 | test_random_range_encoding(ARFCN_RANGE_128, 29); |
| 383 | test_random_range_encoding(ARFCN_RANGE_256, 22); |
| 384 | test_random_range_encoding(ARFCN_RANGE_512, 18); |
| 385 | test_random_range_encoding(ARFCN_RANGE_1024, 16); |
| 386 | } |
| 387 | |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 388 | static int freqs1[] = { |
| 389 | 12, 70, 121, 190, 250, 320, 401, 475, 520, 574, 634, 700, 764, 830, 905, 980 |
| 390 | }; |
| 391 | |
| 392 | static int freqs2[] = { |
| 393 | 402, 460, 1, 67, 131, 197, 272, 347, |
| 394 | }; |
| 395 | |
| 396 | static int freqs3[] = { |
| 397 | 68, 128, 198, 279, 353, 398, 452, |
| 398 | |
| 399 | }; |
| 400 | |
| 401 | static int w_out[] = { |
| 402 | 122, 2, 69, 204, 75, 66, 60, 70, 83, 3, 24, 67, 54, 64, 70, 9, |
| 403 | }; |
| 404 | |
| 405 | static int range128[] = { |
| 406 | 1, 1 + 127, |
| 407 | }; |
| 408 | |
| 409 | static int range256[] = { |
| 410 | 1, 1 + 128, |
| 411 | }; |
| 412 | |
| 413 | static int range512[] = { |
| 414 | 1, 1+ 511, |
| 415 | }; |
| 416 | |
| 417 | |
| 418 | static void test_arfcn_filter() |
| 419 | { |
| 420 | int arfcns[50], i, res, f0_included; |
| 421 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 422 | arfcns[i] = (i + 1) * 2; |
| 423 | |
| 424 | /* check that the arfcn is taken out. f0_included is only set for Range1024 */ |
| 425 | f0_included = 24; |
Jacob Erlbeck | 45014a0 | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 426 | res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 427 | arfcns[0], &f0_included); |
| 428 | VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1); |
| 429 | VERIFY(f0_included, ==, 1); |
| 430 | for (i = 0; i < res; ++i) |
Jacob Erlbeck | 45014a0 | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 431 | VERIFY(arfcns[i], ==, ((i+2) * 2) - (2+1)); |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 432 | |
Jacob Erlbeck | 45014a0 | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 433 | /* check with range1024, ARFCN 0 is included */ |
| 434 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 435 | arfcns[i] = i * 2; |
| 436 | res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), |
| 437 | 0, &f0_included); |
| 438 | VERIFY(res, ==, ARRAY_SIZE(arfcns) - 1); |
| 439 | VERIFY(f0_included, ==, 1); |
| 440 | for (i = 0; i < res; ++i) |
| 441 | VERIFY(arfcns[i], ==, (i + 1) * 2 - 1); |
| 442 | |
| 443 | /* check with range1024, ARFCN 0 not included */ |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 444 | for (i = 0; i < ARRAY_SIZE(arfcns); ++i) |
| 445 | arfcns[i] = (i + 1) * 2; |
Jacob Erlbeck | 45014a0 | 2014-01-14 10:42:58 +0100 | [diff] [blame] | 446 | res = range_enc_filter_arfcns(arfcns, ARRAY_SIZE(arfcns), |
| 447 | 0, &f0_included); |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 448 | VERIFY(res, ==, ARRAY_SIZE(arfcns)); |
| 449 | VERIFY(f0_included, ==, 0); |
| 450 | for (i = 0; i < res; ++i) |
| 451 | VERIFY(arfcns[i], ==, ((i + 1) * 2) - 1); |
| 452 | } |
| 453 | |
| 454 | static void test_print_encoding() |
| 455 | { |
| 456 | int rc; |
| 457 | int w[17]; |
| 458 | uint8_t chan_list[16]; |
| 459 | memset(chan_list, 0x23, sizeof(chan_list)); |
| 460 | |
| 461 | for (rc = 0; rc < ARRAY_SIZE(w); ++rc) |
| 462 | switch (rc % 3) { |
| 463 | case 0: |
| 464 | w[rc] = 0xAAAA; |
| 465 | break; |
| 466 | case 1: |
| 467 | w[rc] = 0x5555; |
| 468 | break; |
| 469 | case 2: |
| 470 | w[rc] = 0x9696; |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | rc = range_enc_range512(chan_list, (1 << 9) | 0x96, w); |
| 475 | VERIFY(rc, ==, 0); |
| 476 | |
| 477 | printf("Range512: %s\n", osmo_hexdump(chan_list, ARRAY_SIZE(chan_list))); |
| 478 | } |
| 479 | |
| 480 | static void test_si_range_helpers() |
| 481 | { |
| 482 | int ws[(sizeof(freqs1)/sizeof(freqs1[0]))]; |
| 483 | int i, f0 = 0xFFFFFF; |
| 484 | |
| 485 | memset(&ws[0], 0x23, sizeof(ws)); |
| 486 | |
| 487 | i = range_enc_find_index(1023, freqs1, ARRAY_SIZE(freqs1)); |
Jacob Erlbeck | 1dc022c | 2014-01-17 09:22:57 +0100 | [diff] [blame] | 488 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs1[i] : -1); |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 489 | VERIFY(i, ==, 2); |
| 490 | |
| 491 | i = range_enc_find_index(511, freqs2, ARRAY_SIZE(freqs2)); |
Jacob Erlbeck | 1dc022c | 2014-01-17 09:22:57 +0100 | [diff] [blame] | 492 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs2[i] : -1); |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 493 | VERIFY(i, ==, 2); |
| 494 | |
| 495 | i = range_enc_find_index(511, freqs3, ARRAY_SIZE(freqs3)); |
Jacob Erlbeck | 1dc022c | 2014-01-17 09:22:57 +0100 | [diff] [blame] | 496 | printf("Element is: %d => freqs[i] = %d\n", i, i >= 0 ? freqs3[i] : -1); |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 497 | VERIFY(i, ==, 0); |
| 498 | |
| 499 | i = range_enc_arfcns(1023, freqs1, ARRAY_SIZE(freqs1), ws, 0); |
| 500 | VERIFY(i, ==, 0); |
| 501 | |
| 502 | for (i = 0; i < sizeof(freqs1)/sizeof(freqs1[0]); ++i) { |
| 503 | printf("w[%d]=%d\n", i, ws[i]); |
| 504 | VERIFY(ws[i], ==, w_out[i]); |
| 505 | } |
| 506 | |
| 507 | i = range_enc_determine_range(range128, ARRAY_SIZE(range128), &f0); |
| 508 | VERIFY(i, ==, ARFCN_RANGE_128); |
| 509 | VERIFY(f0, ==, 1); |
| 510 | |
| 511 | i = range_enc_determine_range(range256, ARRAY_SIZE(range256), &f0); |
| 512 | VERIFY(i, ==, ARFCN_RANGE_256); |
| 513 | VERIFY(f0, ==, 1); |
| 514 | |
| 515 | i = range_enc_determine_range(range512, ARRAY_SIZE(range512), &f0); |
| 516 | VERIFY(i, ==, ARFCN_RANGE_512); |
| 517 | VERIFY(f0, ==, 1); |
| 518 | } |
| 519 | |
Holger Hans Peter Freyther | ca11443 | 2014-02-08 15:20:48 +0100 | [diff] [blame] | 520 | static void test_gsm411_rp_ref_wrap(void) |
| 521 | { |
| 522 | struct gsm_subscriber_connection conn; |
| 523 | int res; |
| 524 | |
| 525 | printf("testing RP-Reference wrap\n"); |
| 526 | |
| 527 | memset(&conn, 0, sizeof(conn)); |
| 528 | conn.next_rp_ref = 255; |
| 529 | |
| 530 | res = sms_next_rp_msg_ref(&conn); |
| 531 | printf("Allocated reference: %d\n", res); |
| 532 | OSMO_ASSERT(res == 255); |
| 533 | |
| 534 | res = sms_next_rp_msg_ref(&conn); |
| 535 | printf("Allocated reference: %d\n", res); |
| 536 | OSMO_ASSERT(res == 0); |
| 537 | |
| 538 | res = sms_next_rp_msg_ref(&conn); |
| 539 | printf("Allocated reference: %d\n", res); |
| 540 | OSMO_ASSERT(res == 1); |
| 541 | } |
| 542 | |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 543 | int main(int argc, char **argv) |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 544 | { |
Jacob Erlbeck | 4b903b4 | 2014-01-10 17:43:41 +0100 | [diff] [blame] | 545 | osmo_init_logging(&log_info); |
| 546 | log_set_log_level(osmo_stderr_target, LOGL_INFO); |
| 547 | |
Holger Hans Peter Freyther | 5d0e56f | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 548 | test_location_area_identifier(); |
| 549 | test_mi_functionality(); |
Jacob Erlbeck | 9444d4f | 2014-01-13 14:43:40 +0100 | [diff] [blame] | 550 | |
| 551 | test_si_range_helpers(); |
| 552 | test_arfcn_filter(); |
| 553 | test_print_encoding(); |
Jacob Erlbeck | 4b903b4 | 2014-01-10 17:43:41 +0100 | [diff] [blame] | 554 | test_range_encoding(); |
Holger Hans Peter Freyther | ca11443 | 2014-02-08 15:20:48 +0100 | [diff] [blame] | 555 | test_gsm411_rp_ref_wrap(); |
Harald Welte | afedeab | 2010-03-04 10:55:40 +0100 | [diff] [blame] | 556 | |
Max | 59a1bf3 | 2016-04-15 16:04:46 +0200 | [diff] [blame] | 557 | test_si2q(); |
Holger Hans Peter Freyther | 300457b | 2012-01-06 15:03:28 +0100 | [diff] [blame] | 558 | printf("Done.\n"); |
| 559 | return EXIT_SUCCESS; |
Holger Freyther | aa0fb36 | 2008-12-28 21:55:40 +0000 | [diff] [blame] | 560 | } |