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 | |
| 13 | import from General_Types all; |
| 14 | |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 15 | type integer uint8_t (0..255) with { variant "unsigned 8 bit" }; |
Harald Welte | 615bd54 | 2017-07-14 18:28:55 +0200 | [diff] [blame] | 16 | type integer uint16_t (0..65535) with { variant "unsigned 16 bit" }; |
Harald Welte | 484160b | 2017-07-28 13:30:24 +0200 | [diff] [blame] | 17 | type integer uint24_t (0..16777215) with { variant "unsigned 24 bit" }; |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 18 | type integer uint32_t (0..4294967295) with { variant "unsigned 32 bit" }; |
| 19 | |
| 20 | type integer int8_t (-128..127) with { variant "8 bit" }; |
| 21 | type integer int16_t (-32768..32767) with { variant "16 bit" }; |
| 22 | |
Harald Welte | f654332 | 2017-07-16 07:35:10 +0200 | [diff] [blame] | 23 | type integer uint1_t (0..1) with { variant "unsigned 1 bit" }; |
Harald Welte | 615bd54 | 2017-07-14 18:28:55 +0200 | [diff] [blame] | 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" }; |
Harald Welte | 615bd54 | 2017-07-14 18:28:55 +0200 | [diff] [blame] | 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" }; |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 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" }; |
Harald Welte | 88b3ccb | 2020-03-12 21:36:32 +0100 | [diff] [blame] | 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 | |
| 40 | const uint16_t c_UINT16_MAX := 65535; |
| 41 | const uint32_t c_UINT32_MAX := 4294967295; |
| 42 | |
Vadim Yanitskiy | 12cf3d9 | 2020-05-05 00:19:50 +0700 | [diff] [blame] | 43 | /* CSN.1 L/H placeholders */ |
| 44 | const BIT1 CSN1_L := '0'B; |
| 45 | const BIT1 CSN1_H := '1'B; |
| 46 | |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 47 | type record Arfcn { |
| 48 | boolean pcs, |
| 49 | uint15_t arfcn |
| 50 | } with { variant "" }; |
| 51 | |
Neels Hofmeyr | f793a7e | 2020-08-12 11:56:24 +0000 | [diff] [blame] | 52 | /* return random integer 0 <= ret < max. According to ETSI ES 201 873 C.6.1, rnd() returns *less* than 1, so |
| 53 | * the returned int will always be ret < max, or ret <= (max-1). */ |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 54 | function f_rnd_int(integer max) return integer { |
| 55 | return float2int(rnd()*int2float(max)); |
| 56 | } |
| 57 | |
| 58 | /* return hexstring composed of random digits */ |
| 59 | function f_rnd_hexstring(in integer len, in integer max := 15) return hexstring { |
| 60 | var integer i; |
| 61 | var hexstring ret := ''H; |
| 62 | for (i := 0; i < len; i := i + 1) { |
| 63 | ret := ret & int2hex(f_rnd_int(max), 1); |
| 64 | } |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | /* return octetstring composed of random bytes */ |
| 69 | function f_rnd_octstring(in integer len) return octetstring { |
| 70 | var integer i; |
| 71 | var octetstring ret := ''O; |
| 72 | for (i := 0; i < len; i := i + 1) { |
| 73 | ret := ret & int2oct(f_rnd_int(255), 1); |
| 74 | } |
| 75 | return ret; |
| 76 | } |
| 77 | |
Vadim Yanitskiy | a01a055 | 2020-07-20 17:47:18 +0700 | [diff] [blame] | 78 | /* return bitstring composed of random bits */ |
| 79 | function f_rnd_bitstring(in integer len) return bitstring { |
| 80 | var octetstring oct := f_rnd_octstring(len / 8 + 1); |
| 81 | return substr(oct2bit(oct), 0, len); |
| 82 | } |
| 83 | |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 84 | function f_rnd_imsi(in hexstring prefix) return hexstring { |
| 85 | return prefix & f_rnd_hexstring(15 - lengthof(prefix), 9); |
| 86 | } |
| 87 | |
| 88 | function f_rnd_msisdn(in octetstring prefix, integer len := 6) return octetstring { |
| 89 | return prefix & f_rnd_octstring(len - lengthof(prefix)); |
| 90 | } |
| 91 | |
Harald Welte | c2461d6 | 2018-01-26 18:55:00 +0100 | [diff] [blame] | 92 | function f_sleep(float seconds) { |
| 93 | timer T := seconds; |
| 94 | T.start; |
| 95 | T.timeout; |
| 96 | } |
| 97 | |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 98 | function bool2bit(boolean inp) return BIT1 { |
| 99 | if (inp) { |
| 100 | return '1'B; |
| 101 | } else { |
| 102 | return '0'B; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | function bool2bit_tmpl(template boolean inp) return template BIT1 { |
| 107 | if (istemplatekind(inp, "omit")) { |
| 108 | return omit; |
| 109 | } else if (istemplatekind(inp, "*")) { |
| 110 | return *; |
| 111 | } else if (istemplatekind(inp, "?")) { |
Harald Welte | b596c18 | 2018-02-16 18:12:59 +0100 | [diff] [blame] | 112 | return ?; |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 113 | } else { |
| 114 | if (valueof(inp)) { |
| 115 | return '1'B; |
| 116 | } else { |
| 117 | return '0'B; |
| 118 | } |
| 119 | } |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Harald Welte | 262f122 | 2018-02-25 16:33:38 +0100 | [diff] [blame] | 122 | type record of integer IntegerRecord; |
| 123 | |
| 124 | function int2bool(integer int) return boolean { |
| 125 | if (int != 0) { |
| 126 | return true; |
| 127 | } else { |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | function f_array_contains(IntegerRecord arr, integer key) return boolean { |
| 133 | for (var integer i:= 0; i< sizeof(arr); i := i + 1) { |
| 134 | if (arr[i] == key) { |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
Harald Welte | c3a3f45 | 2018-02-26 17:37:47 +0100 | [diff] [blame] | 141 | /* re-start given timer in a warning-safe way: Stop (only if running) + start */ |
| 142 | function f_timer_safe_restart(timer T) { |
| 143 | if (T.running) { |
| 144 | T.stop; |
| 145 | } |
| 146 | T.start; |
| 147 | } |
| 148 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 149 | /* divide two integers and return rounded-up result */ |
| 150 | function f_div_round_up(integer dividend, integer divisor) return integer { |
| 151 | var integer x := dividend / divisor; |
| 152 | if (dividend rem divisor != 0) { |
| 153 | x := x+1; |
| 154 | } |
| 155 | return x; |
| 156 | } |
| 157 | |
Harald Welte | 07d99c8 | 2019-05-02 18:33:16 +0200 | [diff] [blame] | 158 | function imsi_hex2oct(hexstring imsi) return octetstring { |
| 159 | var hexstring tmp := ''H; |
| 160 | var octetstring ret; |
| 161 | var integer i; |
| 162 | |
| 163 | /* swap nibbles and pad with F if insufficient input nibbles */ |
| 164 | for (i := 0; i < lengthof(imsi); i := i+1) { |
| 165 | if (i+1 < lengthof(imsi)) { |
| 166 | tmp := tmp & imsi[i+1]; |
| 167 | } else { |
| 168 | tmp := tmp & 'F'H; |
| 169 | } |
| 170 | tmp := tmp & imsi[i]; |
| 171 | i := i+1; |
| 172 | } |
| 173 | ret := hex2oct(tmp); |
| 174 | return ret; |
| 175 | } |
| 176 | |
Harald Welte | bb9512e | 2019-05-30 15:06:12 +0200 | [diff] [blame] | 177 | function f_pad_oct(octetstring str, integer len, OCT1 pad) return octetstring { |
| 178 | var integer strlen := lengthof(str); |
| 179 | for (var integer i := 0; i < len-strlen; i := i+1) { |
| 180 | str := str & pad; |
| 181 | } |
| 182 | return str; |
| 183 | } |
| 184 | |
Vadim Yanitskiy | a01a055 | 2020-07-20 17:47:18 +0700 | [diff] [blame] | 185 | function f_pad_bit(bitstring str, integer len, BIT1 pad) return bitstring { |
| 186 | var integer strlen := lengthof(str); |
| 187 | for (var integer i := 0; i < len-strlen; i := i+1) { |
| 188 | str := str & pad; |
| 189 | } |
| 190 | return str; |
| 191 | } |
| 192 | |
Vadim Yanitskiy | a52347c | 2019-12-12 17:32:33 +0900 | [diff] [blame] | 193 | function f_pad_bcd_number(hexstring number) return hexstring { |
| 194 | if (lengthof(number) mod 2 != 0) { |
| 195 | return number & 'F'H; |
| 196 | } else { |
| 197 | return number; |
| 198 | } |
| 199 | } |
| 200 | |
Vadim Yanitskiy | 6f9f7d2 | 2020-08-26 02:53:10 +0700 | [diff] [blame] | 201 | /* like L1SAP_IS_PACKET_RACH */ |
| 202 | function ra_is_ps(OCT1 ra) return boolean { |
| 203 | if ((ra and4b 'F0'O == '70'O) and (ra and4b '0F'O != '0F'O)) { |
| 204 | return true; |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | function ra_is_emerg(OCT1 ra) return boolean { |
| 210 | /* See also: 3GPP TS 04.08, Table 9.9, ra=101xxxxx */ |
| 211 | if ((ra and4b 'E0'O == 'A0'O) and (ra and4b '1F'O != '1F'O)) { |
| 212 | return true; |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | /* generate a random RACH for circuit-switched */ |
| 218 | function f_rnd_ra_cs() return OCT1 { |
| 219 | var OCT1 ra; |
| 220 | do { |
| 221 | ra := f_rnd_octstring(1); |
| 222 | } while (ra_is_ps(ra)); |
| 223 | return ra; |
| 224 | } |
| 225 | |
| 226 | /* generate a random RACH for emergency */ |
| 227 | function f_rnd_ra_emerg() return OCT1 { |
| 228 | var OCT1 ra; |
| 229 | do { |
| 230 | ra := f_rnd_octstring(1); |
| 231 | } while (not ra_is_emerg(ra)); |
| 232 | return ra; |
| 233 | } |
| 234 | |
| 235 | /* generate a random RACH for packet-switched */ |
| 236 | function f_rnd_ra_ps() return OCT1 { |
| 237 | var OCT1 ra; |
| 238 | do { |
| 239 | ra := f_rnd_octstring(1); |
| 240 | } while (not ra_is_ps(ra)); |
| 241 | return ra; |
| 242 | } |
| 243 | |
| 244 | /* generate a random 11-bit RA (packet-switched only) */ |
| 245 | function f_rnd_ra11_ps() return BIT11 { |
| 246 | var integer ra11 := f_rnd_int(bit2int('11111111111'B)); |
| 247 | return int2bit(ra11, 11); |
| 248 | } |
| 249 | |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 250 | |
Harald Welte | 615bd54 | 2017-07-14 18:28:55 +0200 | [diff] [blame] | 251 | } with { encode "RAW"; variant "FIELDORDER(msb)" } |