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