Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 1 | /* Frame number calculation test */ |
| 2 | |
| 3 | /* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Philipp Maier |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include "bts.h" |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | extern "C" { |
| 27 | #include <osmocom/core/application.h> |
| 28 | #include <osmocom/gsm/gsm_utils.h> |
| 29 | #include <osmocom/core/talloc.h> |
| 30 | #include <osmocom/core/utils.h> |
| 31 | } |
| 32 | |
| 33 | #define RFN_MODULUS 42432 |
| 34 | |
| 35 | /* globals used by the code */ void *tall_pcu_ctx; |
| 36 | int16_t spoof_mnc = 0, spoof_mcc = 0; |
Neels Hofmeyr | bdc55fa | 2018-02-21 00:39:07 +0100 | [diff] [blame] | 37 | bool spoof_mnc_3_digits = false; |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 38 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 39 | static uint32_t calc_fn(struct gprs_rlcmac_bts * bts, uint32_t rfn) |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 40 | { |
| 41 | uint32_t fn; |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 42 | fn = bts_rfn_to_fn(bts, rfn); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 43 | printf("rfn=%i ==> fn=%i\n", rfn, fn); |
| 44 | return fn; |
| 45 | } |
| 46 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 47 | static void set_fn(struct gprs_rlcmac_bts * bts, uint32_t fn) |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 48 | { |
| 49 | printf("\n"); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 50 | bts_set_current_frame_number(bts, fn); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 51 | printf("bts: fn=%i\n", fn); |
| 52 | } |
| 53 | |
| 54 | static void run_test() |
| 55 | { |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 56 | struct gprs_rlcmac_bts *bts = bts_alloc(the_pcu, 0); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 57 | uint32_t fn; |
| 58 | |
| 59 | printf("RFN_MODULUS=%i\n",RFN_MODULUS); |
| 60 | printf("GSM_MAX_FN=%i\n",GSM_MAX_FN); |
| 61 | |
| 62 | |
| 63 | /* Test with a collection of real world examples, |
| 64 | * all all of them are not critical and do not |
| 65 | * assume the occurence of any race contions */ |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 66 | set_fn(bts, 1320462); |
| 67 | fn = calc_fn(bts, 5066); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 68 | OSMO_ASSERT(fn == 1320458); |
| 69 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 70 | set_fn(bts, 8246); |
| 71 | fn = calc_fn(bts, 8244); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 72 | OSMO_ASSERT(fn == 8244); |
| 73 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 74 | set_fn(bts, 10270); |
| 75 | fn = calc_fn(bts, 10269); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 76 | OSMO_ASSERT(fn == 10269); |
| 77 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 78 | set_fn(bts, 311276); |
| 79 | fn = calc_fn(bts, 14250); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 80 | OSMO_ASSERT(fn == 311274); |
| 81 | |
| 82 | |
| 83 | /* Now lets assume a case where the frame number |
| 84 | * just wrapped over a little bit above the |
| 85 | * modulo 42432 raster, but the rach request |
| 86 | * occurred before the wrapping */ |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 87 | set_fn(bts, RFN_MODULUS + 30); |
| 88 | fn = calc_fn(bts, RFN_MODULUS - 10); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 89 | OSMO_ASSERT(fn == 42422); |
| 90 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 91 | set_fn(bts, RFN_MODULUS + 1); |
| 92 | fn = calc_fn(bts, RFN_MODULUS - 1); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 93 | OSMO_ASSERT(fn == 42431); |
| 94 | |
Philipp Maier | 39f5e27 | 2023-01-05 11:55:17 +0100 | [diff] [blame] | 95 | set_fn(bts, RFN_MODULUS * 12 + 16); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 96 | fn = calc_fn(bts, RFN_MODULUS - 4); |
Philipp Maier | 39f5e27 | 2023-01-05 11:55:17 +0100 | [diff] [blame] | 97 | OSMO_ASSERT(fn == 509180); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 98 | |
Philipp Maier | 39f5e27 | 2023-01-05 11:55:17 +0100 | [diff] [blame] | 99 | set_fn(bts, RFN_MODULUS * 12 + 451); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 100 | fn = calc_fn(bts, RFN_MODULUS - 175); |
Philipp Maier | 39f5e27 | 2023-01-05 11:55:17 +0100 | [diff] [blame] | 101 | OSMO_ASSERT(fn == 509009); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 102 | |
| 103 | |
| 104 | /* Lets check a special cornercase. We assume that |
| 105 | * the BTS just wrapped its internal frame number |
| 106 | * but we still get rach requests with high relative |
| 107 | * frame numbers. */ |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 108 | set_fn(bts, 0); |
| 109 | fn = calc_fn(bts, RFN_MODULUS - 13); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 110 | OSMO_ASSERT(fn == 2715635); |
| 111 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 112 | set_fn(bts, 453); |
| 113 | fn = calc_fn(bts, RFN_MODULUS - 102); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 114 | OSMO_ASSERT(fn == 2715546); |
| 115 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 116 | set_fn(bts, 10); |
| 117 | fn = calc_fn(bts, RFN_MODULUS - 10); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 118 | OSMO_ASSERT(fn == 2715638); |
| 119 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 120 | set_fn(bts, 23); |
| 121 | fn = calc_fn(bts, RFN_MODULUS - 42); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 122 | OSMO_ASSERT(fn == 2715606); |
| 123 | |
| 124 | |
| 125 | /* Also check with some corner case |
| 126 | * values where Fn and RFn reach its |
| 127 | * maximum/minimum valid range */ |
Philipp Maier | 39f5e27 | 2023-01-05 11:55:17 +0100 | [diff] [blame] | 128 | set_fn(bts, GSM_MAX_FN-1); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 129 | fn = calc_fn(bts, RFN_MODULUS-1); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 130 | OSMO_ASSERT(fn == GSM_MAX_FN-1); |
| 131 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 132 | set_fn(bts, 0); |
| 133 | fn = calc_fn(bts, RFN_MODULUS-1); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 134 | OSMO_ASSERT(fn == GSM_MAX_FN-1); |
| 135 | |
Philipp Maier | 39f5e27 | 2023-01-05 11:55:17 +0100 | [diff] [blame] | 136 | set_fn(bts, GSM_MAX_FN-1); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 137 | fn = calc_fn(bts, 0); |
Philipp Maier | 39f5e27 | 2023-01-05 11:55:17 +0100 | [diff] [blame] | 138 | OSMO_ASSERT(fn == GSM_MAX_FN-RFN_MODULUS*2); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 139 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 140 | set_fn(bts, 0); |
| 141 | fn = calc_fn(bts, 0); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 142 | OSMO_ASSERT(fn == 0); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 143 | talloc_free(bts); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int main(int argc, char **argv) |
| 147 | { |
| 148 | tall_pcu_ctx = talloc_named_const(NULL, 1, "fn test context"); |
| 149 | if (!tall_pcu_ctx) |
| 150 | abort(); |
| 151 | |
| 152 | msgb_talloc_ctx_init(tall_pcu_ctx, 0); |
Neels Hofmeyr | 42f2d61 | 2018-04-01 16:54:40 +0200 | [diff] [blame] | 153 | osmo_init_logging2(tall_pcu_ctx, &gprs_log_info); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 154 | log_set_use_color(osmo_stderr_target, 0); |
Pau Espin Pedrol | 00f52cc | 2021-02-19 14:01:52 +0100 | [diff] [blame] | 155 | log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 156 | log_set_log_level(osmo_stderr_target, LOGL_DEBUG); |
| 157 | |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 158 | the_pcu = gprs_pcu_alloc(tall_pcu_ctx); |
| 159 | |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 160 | run_test(); |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 161 | |
| 162 | talloc_free(the_pcu); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 163 | return EXIT_SUCCESS; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * stubs that should not be reached |
| 168 | */ |
| 169 | extern "C" { |
| 170 | void l1if_pdch_req() { |
| 171 | abort(); |
| 172 | } void l1if_connect_pdch() { |
| 173 | abort(); |
Philipp Maier | 72ed333 | 2023-02-27 15:32:00 +0100 | [diff] [blame^] | 174 | } void l1if_disconnect_pdch() { |
| 175 | abort(); |
Philipp Maier | 1275a3f | 2017-02-21 19:35:23 +0100 | [diff] [blame] | 176 | } |
| 177 | void l1if_close_pdch() { |
| 178 | abort(); |
| 179 | } |
| 180 | void l1if_open_pdch() { |
| 181 | abort(); |
| 182 | } |
| 183 | } |