Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 1 | /* Osmocom utility type definitions in TTCN-3 |
| 2 | * (C) 2017-2019 Harald Welte <laforge@gnumonks.org> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Released under the terms of GNU General Public License, Version 2 or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 9 | */ |
| 10 | |
Harald Welte | 615bd54 | 2017-07-14 18:28:55 +0200 | [diff] [blame] | 11 | module Osmocom_Types { |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 12 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 13 | import from General_Types all; |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 14 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 15 | type integer uint8_t (0..255) with { variant "unsigned 8 bit" }; |
| 16 | type integer uint16_t (0..65535) with { variant "unsigned 16 bit" }; |
| 17 | type integer uint24_t (0..16777215) with { variant "unsigned 24 bit" }; |
| 18 | type integer uint32_t (0..4294967295) with { variant "unsigned 32 bit" }; |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 19 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 20 | type integer int8_t (-128..127) with { variant "8 bit" }; |
| 21 | type integer int16_t (-32768..32767) with { variant "16 bit" }; |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 22 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 23 | type integer uint1_t (0..1) with { variant "unsigned 1 bit" }; |
| 24 | type integer uint2_t (0..3) with { variant "unsigned 2 bit" }; |
| 25 | type integer uint3_t (0..7) with { variant "unsigned 3 bit" }; |
| 26 | type integer uint4_t (0..15) with { variant "unsigned 4 bit" }; |
| 27 | type integer uint5_t (0..31) with { variant "unsigned 5 bit" }; |
| 28 | type integer uint6_t (0..63) with { variant "unsigned 6 bit" }; |
| 29 | type integer uint7_t (0..127) with { variant "unsigned 7 bit" }; |
| 30 | type integer uint9_t (0..511) with { variant "unsigned 9 bit" }; |
| 31 | type integer uint10_t (0..1023) with { variant "unsigned 10 bit" }; |
| 32 | type integer uint11_t (0..2047) with { variant "unsigned 11 bit" }; |
| 33 | type integer uint12_t (0..4095) with { variant "unsigned 12 bit" }; |
| 34 | type integer uint13_t (0..8191) with { variant "unsigned 13 bit" }; |
| 35 | type integer uint14_t (0..16383) with { variant "unsigned 14 bit" }; |
| 36 | type integer uint15_t (0..32767) with { variant "unsigned 15 bit" }; |
| 37 | type integer uint40_t (0..1099511627776) with { variant "unsigned 40 bit" }; |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 38 | |
Harald Welte | 0236980 | 2018-02-25 12:39:05 +0100 | [diff] [blame] | 39 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 40 | const uint16_t c_UINT16_MAX := 65535; |
| 41 | const uint32_t c_UINT32_MAX := 4294967295; |
Harald Welte | 0236980 | 2018-02-25 12:39:05 +0100 | [diff] [blame] | 42 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 43 | /* CSN.1 L/H placeholders */ |
| 44 | const BIT1 CSN1_L := '0'B; |
| 45 | const BIT1 CSN1_H := '1'B; |
Vadim Yanitskiy | 12cf3d9 | 2020-05-05 00:19:50 +0700 | [diff] [blame] | 46 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 47 | type record Arfcn { |
| 48 | boolean pcs, |
| 49 | uint15_t arfcn |
| 50 | } with { variant "" }; |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 51 | |
Alexander Couzens | e0f7c54 | 2020-09-13 17:25:18 +0200 | [diff] [blame] | 52 | /* based on Linux */ |
| 53 | type enumerated AddressFamily { |
| 54 | AF_UNSPEC ('00'O), |
| 55 | AF_INET ('02'O), |
| 56 | AF_INET6 ('0a'O) |
| 57 | } |
| 58 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 59 | /* return random integer 0 <= ret < max. According to ETSI ES 201 873 C.6.1, rnd() returns *less* than 1, so |
| 60 | * the returned int will always be ret < max, or ret <= (max-1). */ |
| 61 | function f_rnd_int(integer max) return integer { |
| 62 | return float2int(rnd()*int2float(max)); |
| 63 | } |
| 64 | |
| 65 | /* return hexstring composed of random digits */ |
| 66 | function f_rnd_hexstring(in integer len, in integer max := 15) return hexstring { |
| 67 | var integer i; |
| 68 | var hexstring ret := ''H; |
| 69 | for (i := 0; i < len; i := i + 1) { |
| 70 | ret := ret & int2hex(f_rnd_int(max), 1); |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 71 | } |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 72 | return ret; |
| 73 | } |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 74 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 75 | /* return octetstring composed of random bytes */ |
| 76 | function f_rnd_octstring(in integer len) return octetstring { |
| 77 | var integer i; |
| 78 | var octetstring ret := ''O; |
| 79 | for (i := 0; i < len; i := i + 1) { |
| 80 | ret := ret & int2oct(f_rnd_int(255), 1); |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 81 | } |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 82 | return ret; |
| 83 | } |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 84 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 85 | /* return bitstring composed of random bits */ |
| 86 | function f_rnd_bitstring(in integer len) return bitstring { |
| 87 | var octetstring oct := f_rnd_octstring(len / 8 + 1); |
| 88 | return substr(oct2bit(oct), 0, len); |
| 89 | } |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 90 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 91 | function f_rnd_imsi(in hexstring prefix) return hexstring { |
| 92 | return prefix & f_rnd_hexstring(15 - lengthof(prefix), 9); |
| 93 | } |
Vadim Yanitskiy | a01a055 | 2020-07-20 17:47:18 +0700 | [diff] [blame] | 94 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 95 | function f_rnd_msisdn(in octetstring prefix, integer len := 6) return octetstring { |
| 96 | return prefix & f_rnd_octstring(len - lengthof(prefix)); |
| 97 | } |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 98 | |
Alexander Couzens | 4231de6 | 2020-09-11 19:52:15 +0200 | [diff] [blame] | 99 | function f_sleep(float seconds) { |
| 100 | timer T := seconds; |
| 101 | T.start; |
| 102 | T.timeout; |
| 103 | } |
Harald Welte | c2461d6 | 2018-01-26 18:55:00 +0100 | [diff] [blame] | 104 | |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 105 | function bool2bit(boolean inp) return BIT1 { |
| 106 | if (inp) { |
| 107 | return '1'B; |
| 108 | } else { |
| 109 | return '0'B; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | function bool2bit_tmpl(template boolean inp) return template BIT1 { |
| 114 | if (istemplatekind(inp, "omit")) { |
| 115 | return omit; |
| 116 | } else if (istemplatekind(inp, "*")) { |
| 117 | return *; |
| 118 | } else if (istemplatekind(inp, "?")) { |
Harald Welte | b596c18 | 2018-02-16 18:12:59 +0100 | [diff] [blame] | 119 | return ?; |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 120 | } else { |
| 121 | if (valueof(inp)) { |
| 122 | return '1'B; |
| 123 | } else { |
| 124 | return '0'B; |
| 125 | } |
| 126 | } |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Harald Welte | 262f122 | 2018-02-25 16:33:38 +0100 | [diff] [blame] | 129 | type record of integer IntegerRecord; |
| 130 | |
| 131 | function int2bool(integer int) return boolean { |
| 132 | if (int != 0) { |
| 133 | return true; |
| 134 | } else { |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | function f_array_contains(IntegerRecord arr, integer key) return boolean { |
| 140 | for (var integer i:= 0; i< sizeof(arr); i := i + 1) { |
| 141 | if (arr[i] == key) { |
| 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | |
Harald Welte | c3a3f45 | 2018-02-26 17:37:47 +0100 | [diff] [blame] | 148 | /* re-start given timer in a warning-safe way: Stop (only if running) + start */ |
| 149 | function f_timer_safe_restart(timer T) { |
| 150 | if (T.running) { |
| 151 | T.stop; |
| 152 | } |
| 153 | T.start; |
| 154 | } |
| 155 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 156 | /* divide two integers and return rounded-up result */ |
| 157 | function f_div_round_up(integer dividend, integer divisor) return integer { |
| 158 | var integer x := dividend / divisor; |
| 159 | if (dividend rem divisor != 0) { |
| 160 | x := x+1; |
| 161 | } |
| 162 | return x; |
| 163 | } |
| 164 | |
Harald Welte | 07d99c8 | 2019-05-02 18:33:16 +0200 | [diff] [blame] | 165 | function imsi_hex2oct(hexstring imsi) return octetstring { |
| 166 | var hexstring tmp := ''H; |
| 167 | var octetstring ret; |
| 168 | var integer i; |
| 169 | |
| 170 | /* swap nibbles and pad with F if insufficient input nibbles */ |
| 171 | for (i := 0; i < lengthof(imsi); i := i+1) { |
| 172 | if (i+1 < lengthof(imsi)) { |
| 173 | tmp := tmp & imsi[i+1]; |
| 174 | } else { |
| 175 | tmp := tmp & 'F'H; |
| 176 | } |
| 177 | tmp := tmp & imsi[i]; |
| 178 | i := i+1; |
| 179 | } |
| 180 | ret := hex2oct(tmp); |
| 181 | return ret; |
| 182 | } |
| 183 | |
Harald Welte | bb9512e | 2019-05-30 15:06:12 +0200 | [diff] [blame] | 184 | function f_pad_oct(octetstring str, integer len, OCT1 pad) return octetstring { |
| 185 | var integer strlen := lengthof(str); |
| 186 | for (var integer i := 0; i < len-strlen; i := i+1) { |
| 187 | str := str & pad; |
| 188 | } |
| 189 | return str; |
| 190 | } |
| 191 | |
Vadim Yanitskiy | a01a055 | 2020-07-20 17:47:18 +0700 | [diff] [blame] | 192 | function f_pad_bit(bitstring str, integer len, BIT1 pad) return bitstring { |
| 193 | var integer strlen := lengthof(str); |
| 194 | for (var integer i := 0; i < len-strlen; i := i+1) { |
| 195 | str := str & pad; |
| 196 | } |
| 197 | return str; |
| 198 | } |
| 199 | |
Vadim Yanitskiy | a52347c | 2019-12-12 17:32:33 +0900 | [diff] [blame] | 200 | function f_pad_bcd_number(hexstring number) return hexstring { |
| 201 | if (lengthof(number) mod 2 != 0) { |
| 202 | return number & 'F'H; |
| 203 | } else { |
| 204 | return number; |
| 205 | } |
| 206 | } |
| 207 | |
Vadim Yanitskiy | 6f9f7d2 | 2020-08-26 02:53:10 +0700 | [diff] [blame] | 208 | /* like L1SAP_IS_PACKET_RACH */ |
| 209 | function ra_is_ps(OCT1 ra) return boolean { |
| 210 | if ((ra and4b 'F0'O == '70'O) and (ra and4b '0F'O != '0F'O)) { |
| 211 | return true; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | function ra_is_emerg(OCT1 ra) return boolean { |
| 217 | /* See also: 3GPP TS 04.08, Table 9.9, ra=101xxxxx */ |
Vadim Yanitskiy | 146efe6 | 2020-08-31 02:13:56 +0700 | [diff] [blame] | 218 | return (ra and4b 'E0'O == 'A0'O); |
Vadim Yanitskiy | 6f9f7d2 | 2020-08-26 02:53:10 +0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* generate a random RACH for circuit-switched */ |
| 222 | function f_rnd_ra_cs() return OCT1 { |
| 223 | var OCT1 ra; |
| 224 | do { |
| 225 | ra := f_rnd_octstring(1); |
Vadim Yanitskiy | 27057e0 | 2020-08-29 16:16:52 +0700 | [diff] [blame] | 226 | } while (ra_is_ps(ra) or ra_is_emerg(ra)); |
Vadim Yanitskiy | 6f9f7d2 | 2020-08-26 02:53:10 +0700 | [diff] [blame] | 227 | return ra; |
| 228 | } |
| 229 | |
| 230 | /* generate a random RACH for emergency */ |
| 231 | function f_rnd_ra_emerg() return OCT1 { |
| 232 | var OCT1 ra; |
| 233 | do { |
| 234 | ra := f_rnd_octstring(1); |
| 235 | } while (not ra_is_emerg(ra)); |
| 236 | return ra; |
| 237 | } |
| 238 | |
| 239 | /* generate a random RACH for packet-switched */ |
| 240 | function f_rnd_ra_ps() return OCT1 { |
| 241 | var OCT1 ra; |
| 242 | do { |
| 243 | ra := f_rnd_octstring(1); |
| 244 | } while (not ra_is_ps(ra)); |
| 245 | return ra; |
| 246 | } |
| 247 | |
| 248 | /* generate a random 11-bit RA (packet-switched only) */ |
| 249 | function f_rnd_ra11_ps() return BIT11 { |
| 250 | var integer ra11 := f_rnd_int(bit2int('11111111111'B)); |
| 251 | return int2bit(ra11, 11); |
| 252 | } |
| 253 | |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 254 | |
Harald Welte | 69b3a48 | 2020-11-12 17:25:49 +0100 | [diff] [blame] | 255 | private function f_concat_pad(integer tot_len, hexstring prefix, integer suffix) return hexstring { |
| 256 | var integer suffix_len := tot_len - lengthof(prefix); |
| 257 | var charstring suffix_ch := int2str(suffix); |
| 258 | var integer pad_len := suffix_len - lengthof(suffix_ch); |
| 259 | |
| 260 | return prefix & int2hex(0, pad_len) & str2hex(suffix_ch); |
| 261 | } |
| 262 | |
| 263 | function f_gen_imei(integer suffix) return hexstring { |
| 264 | return f_concat_pad(14, '49999'H, suffix); |
| 265 | } |
| 266 | |
| 267 | function f_gen_imsi(integer suffix) return hexstring { |
| 268 | return f_concat_pad(15, '26242'H, suffix); |
| 269 | } |
| 270 | |
| 271 | function f_gen_msisdn(integer suffix) return hexstring { |
| 272 | return f_concat_pad(12, '49123'H, suffix); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | |
Harald Welte | 615bd54 | 2017-07-14 18:28:55 +0200 | [diff] [blame] | 277 | } with { encode "RAW"; variant "FIELDORDER(msb)" } |