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 | |
Harald Welte | ddeaa57 | 2017-07-14 18:58:07 +0200 | [diff] [blame] | 43 | type record Arfcn { |
| 44 | boolean pcs, |
| 45 | uint15_t arfcn |
| 46 | } with { variant "" }; |
| 47 | |
Harald Welte | 9182704 | 2018-01-25 09:20:04 +0100 | [diff] [blame] | 48 | /* return random integer between 0 and max */ |
| 49 | function f_rnd_int(integer max) return integer { |
| 50 | return float2int(rnd()*int2float(max)); |
| 51 | } |
| 52 | |
| 53 | /* return hexstring composed of random digits */ |
| 54 | function f_rnd_hexstring(in integer len, in integer max := 15) return hexstring { |
| 55 | var integer i; |
| 56 | var hexstring ret := ''H; |
| 57 | for (i := 0; i < len; i := i + 1) { |
| 58 | ret := ret & int2hex(f_rnd_int(max), 1); |
| 59 | } |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | /* return octetstring composed of random bytes */ |
| 64 | function f_rnd_octstring(in integer len) return octetstring { |
| 65 | var integer i; |
| 66 | var octetstring ret := ''O; |
| 67 | for (i := 0; i < len; i := i + 1) { |
| 68 | ret := ret & int2oct(f_rnd_int(255), 1); |
| 69 | } |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | function f_rnd_imsi(in hexstring prefix) return hexstring { |
| 74 | return prefix & f_rnd_hexstring(15 - lengthof(prefix), 9); |
| 75 | } |
| 76 | |
| 77 | function f_rnd_msisdn(in octetstring prefix, integer len := 6) return octetstring { |
| 78 | return prefix & f_rnd_octstring(len - lengthof(prefix)); |
| 79 | } |
| 80 | |
Harald Welte | c2461d6 | 2018-01-26 18:55:00 +0100 | [diff] [blame] | 81 | function f_sleep(float seconds) { |
| 82 | timer T := seconds; |
| 83 | T.start; |
| 84 | T.timeout; |
| 85 | } |
| 86 | |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 87 | function bool2bit(boolean inp) return BIT1 { |
| 88 | if (inp) { |
| 89 | return '1'B; |
| 90 | } else { |
| 91 | return '0'B; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | function bool2bit_tmpl(template boolean inp) return template BIT1 { |
| 96 | if (istemplatekind(inp, "omit")) { |
| 97 | return omit; |
| 98 | } else if (istemplatekind(inp, "*")) { |
| 99 | return *; |
| 100 | } else if (istemplatekind(inp, "?")) { |
Harald Welte | b596c18 | 2018-02-16 18:12:59 +0100 | [diff] [blame] | 101 | return ?; |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 102 | } else { |
| 103 | if (valueof(inp)) { |
| 104 | return '1'B; |
| 105 | } else { |
| 106 | return '0'B; |
| 107 | } |
| 108 | } |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Harald Welte | 262f122 | 2018-02-25 16:33:38 +0100 | [diff] [blame] | 111 | type record of integer IntegerRecord; |
| 112 | |
| 113 | function int2bool(integer int) return boolean { |
| 114 | if (int != 0) { |
| 115 | return true; |
| 116 | } else { |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | function f_array_contains(IntegerRecord arr, integer key) return boolean { |
| 122 | for (var integer i:= 0; i< sizeof(arr); i := i + 1) { |
| 123 | if (arr[i] == key) { |
| 124 | return true; |
| 125 | } |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
Harald Welte | c3a3f45 | 2018-02-26 17:37:47 +0100 | [diff] [blame] | 130 | /* re-start given timer in a warning-safe way: Stop (only if running) + start */ |
| 131 | function f_timer_safe_restart(timer T) { |
| 132 | if (T.running) { |
| 133 | T.stop; |
| 134 | } |
| 135 | T.start; |
| 136 | } |
| 137 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 138 | /* divide two integers and return rounded-up result */ |
| 139 | function f_div_round_up(integer dividend, integer divisor) return integer { |
| 140 | var integer x := dividend / divisor; |
| 141 | if (dividend rem divisor != 0) { |
| 142 | x := x+1; |
| 143 | } |
| 144 | return x; |
| 145 | } |
| 146 | |
Harald Welte | 07d99c8 | 2019-05-02 18:33:16 +0200 | [diff] [blame] | 147 | function imsi_hex2oct(hexstring imsi) return octetstring { |
| 148 | var hexstring tmp := ''H; |
| 149 | var octetstring ret; |
| 150 | var integer i; |
| 151 | |
| 152 | /* swap nibbles and pad with F if insufficient input nibbles */ |
| 153 | for (i := 0; i < lengthof(imsi); i := i+1) { |
| 154 | if (i+1 < lengthof(imsi)) { |
| 155 | tmp := tmp & imsi[i+1]; |
| 156 | } else { |
| 157 | tmp := tmp & 'F'H; |
| 158 | } |
| 159 | tmp := tmp & imsi[i]; |
| 160 | i := i+1; |
| 161 | } |
| 162 | ret := hex2oct(tmp); |
| 163 | return ret; |
| 164 | } |
| 165 | |
Harald Welte | bb9512e | 2019-05-30 15:06:12 +0200 | [diff] [blame] | 166 | function f_pad_oct(octetstring str, integer len, OCT1 pad) return octetstring { |
| 167 | var integer strlen := lengthof(str); |
| 168 | for (var integer i := 0; i < len-strlen; i := i+1) { |
| 169 | str := str & pad; |
| 170 | } |
| 171 | return str; |
| 172 | } |
| 173 | |
Vadim Yanitskiy | a52347c | 2019-12-12 17:32:33 +0900 | [diff] [blame] | 174 | function f_pad_bcd_number(hexstring number) return hexstring { |
| 175 | if (lengthof(number) mod 2 != 0) { |
| 176 | return number & 'F'H; |
| 177 | } else { |
| 178 | return number; |
| 179 | } |
| 180 | } |
| 181 | |
Harald Welte | c7adaa7 | 2018-02-15 20:40:36 +0100 | [diff] [blame] | 182 | |
Harald Welte | 615bd54 | 2017-07-14 18:28:55 +0200 | [diff] [blame] | 183 | } with { encode "RAW"; variant "FIELDORDER(msb)" } |