Harald Welte | a082a69 | 2017-07-15 15:58:13 +0200 | [diff] [blame] | 1 | /* Encoding/Decoding routines for GSM System Information messages |
| 2 | * according to 3GPP TS 44.018 Version 12.3.0 Release 12 */ |
| 3 | |
| 4 | /* (C) 2017 by Harald Welte <laforge@gnumonks.org> */ |
| 5 | |
| 6 | module GSM_Types { |
| 7 | |
| 8 | import from General_Types all; |
| 9 | import from Osmocom_Types all; |
| 10 | |
Harald Welte | bdc5dbd | 2017-07-16 00:00:43 +0200 | [diff] [blame] | 11 | type integer GsmArfcn (0..1023); |
| 12 | type integer UmtsArfcn (0..16383); |
| 13 | type integer UmtsScramblingCode (0..511); |
Harald Welte | d2e342f | 2017-07-16 07:34:13 +0200 | [diff] [blame] | 14 | const integer GsmMaxFrameNumber := 26*51*2048; |
| 15 | type integer GsmFrameNumber (0..GsmMaxFrameNumber); |
| 16 | type integer GsmRxLev (0..63); |
| 17 | type integer GsmTsc (0..7) with { variant "FIELDLENGTH(8)" }; |
| 18 | type uint32_t GsmTmsi; |
Harald Welte | acc93ab | 2018-03-02 21:39:09 +0100 | [diff] [blame] | 19 | type OCT4 GprsTlli; |
Harald Welte | 72cecfa | 2017-12-11 19:50:14 +0100 | [diff] [blame] | 20 | type hexstring GsmMcc length(3); |
| 21 | type hexstring GsmMnc length(2 .. 3); |
| 22 | type uint16_t GsmLac; |
| 23 | type uint16_t GsmCellId; |
Harald Welte | bdc5dbd | 2017-07-16 00:00:43 +0200 | [diff] [blame] | 24 | |
Harald Welte | 1dcc371 | 2017-08-01 00:05:52 +0200 | [diff] [blame] | 25 | type enumerated GprsCodingScheme { |
| 26 | CS1, CS2, CS3, CS4 |
| 27 | }; |
| 28 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 29 | function f_gprs_blocksize(GprsCodingScheme cs) return integer { |
| 30 | select (cs) { |
| 31 | case (CS1) { return 22 } |
| 32 | case (CS2) { return 32 } |
| 33 | case (CS3) { return 38 } |
| 34 | case (CS3) { return 52 } |
| 35 | case else { |
| 36 | setverdict(fail, "Invalid GPRS CS ", cs); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame^] | 37 | mtc.stop; |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 38 | return -1; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
Harald Welte | d2e342f | 2017-07-16 07:34:13 +0200 | [diff] [blame] | 43 | /* 10.5.2.8 */ |
| 44 | type enumerated ChannelNeeded { |
| 45 | CHAN_NEED_ANY (0), |
| 46 | CHAN_NEED_SDCCH (1), |
| 47 | CHAN_NEED_TCH_F (2), |
| 48 | CHAN_NEED_TCH_H (3) |
| 49 | } with { variant "FIELDLENGTH(2)" }; |
| 50 | type record ChannelNeeded12 { |
| 51 | ChannelNeeded second, |
| 52 | ChannelNeeded first |
| 53 | } with { variant "" }; |
| 54 | |
Harald Welte | d2e342f | 2017-07-16 07:34:13 +0200 | [diff] [blame] | 55 | |
Harald Welte | 0c8d5c0 | 2017-07-16 18:53:58 +0200 | [diff] [blame] | 56 | /* TS 48.058 9.3.1 Channel Number IE */ |
| 57 | type enumerated RslChanNr0 { |
Harald Welte | 6f3c223 | 2017-07-30 17:17:12 +0200 | [diff] [blame] | 58 | RSL_CHAN_NR_INVALID ('00000'B), |
| 59 | RSL_CHAN_NR_Bm_ACCH ('00001'B), |
| 60 | RSL_CHAN_NR_BCCH ('10000'B), |
| 61 | RSL_CHAN_NR_RACH ('10001'B), |
| 62 | RSL_CHAN_NR_PCH_AGCH ('10010'B), |
| 63 | RSL_CHAN_NR_OSMO_PDCH ('11000'B) |
Harald Welte | c84d847 | 2017-07-30 00:49:56 +0200 | [diff] [blame] | 64 | } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" }; |
Harald Welte | 0c8d5c0 | 2017-07-16 18:53:58 +0200 | [diff] [blame] | 65 | |
| 66 | type record RslChanNr2 { |
| 67 | BIT4 tag ('0001'B), |
| 68 | uint1_t sub_chan |
Harald Welte | c84d847 | 2017-07-30 00:49:56 +0200 | [diff] [blame] | 69 | } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" }; |
Harald Welte | 0c8d5c0 | 2017-07-16 18:53:58 +0200 | [diff] [blame] | 70 | |
| 71 | type record RslChanNr4 { |
| 72 | BIT3 tag ('001'B), |
| 73 | uint2_t sub_chan |
Harald Welte | c84d847 | 2017-07-30 00:49:56 +0200 | [diff] [blame] | 74 | } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" }; |
Harald Welte | 0c8d5c0 | 2017-07-16 18:53:58 +0200 | [diff] [blame] | 75 | |
| 76 | type record RslChanNr8 { |
| 77 | BIT2 tag ('01'B), |
| 78 | uint3_t sub_chan |
Harald Welte | c84d847 | 2017-07-30 00:49:56 +0200 | [diff] [blame] | 79 | } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" }; |
Harald Welte | 0c8d5c0 | 2017-07-16 18:53:58 +0200 | [diff] [blame] | 80 | |
| 81 | type union RslChanNrU { |
| 82 | RslChanNr0 ch0, |
| 83 | RslChanNr2 lm, |
| 84 | RslChanNr4 sdcch4, |
| 85 | RslChanNr8 sdcch8 |
| 86 | } with { |
| 87 | variant "TAG(lm, tag = '0001'B; |
| 88 | sdcch4, tag = '001'B; |
| 89 | sdcch8, tag = '01'B; |
| 90 | ch0, OTHERWISE)" |
| 91 | variant "FIELDLENGTH(5)" |
Harald Welte | c84d847 | 2017-07-30 00:49:56 +0200 | [diff] [blame] | 92 | variant "FIELDORDER(msb)" |
Harald Welte | 0c8d5c0 | 2017-07-16 18:53:58 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | type record RslChannelNr { |
| 96 | RslChanNrU u, |
| 97 | uint3_t tn |
Harald Welte | c84d847 | 2017-07-30 00:49:56 +0200 | [diff] [blame] | 98 | } with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" }; |
Harald Welte | 0c8d5c0 | 2017-07-16 18:53:58 +0200 | [diff] [blame] | 99 | |
| 100 | template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := { |
| 101 | u := { ch0 := cht }, |
| 102 | tn := tn |
| 103 | } |
| 104 | |
| 105 | template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH); |
| 106 | template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH); |
| 107 | template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH); |
| 108 | template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH); |
Harald Welte | 1dcc371 | 2017-08-01 00:05:52 +0200 | [diff] [blame] | 109 | template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH); |
Harald Welte | 2c2e8c4 | 2018-01-29 21:59:39 +0100 | [diff] [blame] | 110 | template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := { |
Harald Welte | 0c8d5c0 | 2017-07-16 18:53:58 +0200 | [diff] [blame] | 111 | u := { lm := { tag := '0001'B, sub_chan := sub_slot } }, |
| 112 | tn := tn |
| 113 | } |
| 114 | template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := { |
| 115 | u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } }, |
| 116 | tn := tn |
| 117 | } |
| 118 | template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := { |
| 119 | u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } }, |
| 120 | tn := tn |
| 121 | } |
Harald Welte | d2e342f | 2017-07-16 07:34:13 +0200 | [diff] [blame] | 122 | |
Harald Welte | 629cc6b | 2018-03-11 17:19:05 +0100 | [diff] [blame] | 123 | template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := { |
| 124 | u := { ch0 := cht }, |
| 125 | tn := tn |
| 126 | } |
| 127 | template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH); |
| 128 | template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH); |
| 129 | template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH); |
| 130 | template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH); |
| 131 | template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH); |
| 132 | template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := { |
| 133 | u := { lm := { tag := '0001'B, sub_chan := sub_slot } }, |
| 134 | tn := tn |
| 135 | } |
| 136 | template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := { |
| 137 | u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } }, |
| 138 | tn := tn |
| 139 | } |
| 140 | template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := { |
| 141 | u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } }, |
| 142 | tn := tn |
| 143 | } |
| 144 | |
Harald Welte | b33e727 | 2017-07-16 21:04:12 +0200 | [diff] [blame] | 145 | /* TS 48.058 9.3.2 Link ID */ |
| 146 | type enumerated RslLinkIdC { |
| 147 | FACCH_SDCCH (0), |
| 148 | SACCH (1) |
| 149 | } with { variant "FIELDLENGTH(2)" }; |
| 150 | |
| 151 | type enumerated RslSapi0Prio { |
| 152 | SAPI0_PRIO_NORMAL (0), |
| 153 | SAPI0_PRIO_HIGH (1), |
| 154 | SAPI0_PRIO_LOW (2) |
| 155 | } with { variant "FIELDLENGTH(2)" }; |
| 156 | |
| 157 | type uint3_t GsmSapi; |
| 158 | |
| 159 | type record RslLinkId { |
| 160 | RslLinkIdC c, |
| 161 | boolean na, |
| 162 | RslSapi0Prio prio, |
| 163 | GsmSapi sapi |
| 164 | } with { variant "" }; |
| 165 | |
| 166 | template RslLinkId tr_RslLinkId := { |
| 167 | c := ?, |
| 168 | na := ?, |
| 169 | prio := ?, |
| 170 | sapi := ? |
| 171 | }; |
| 172 | |
| 173 | template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := { |
| 174 | c := FACCH_SDCCH, |
| 175 | na := false, |
| 176 | sapi := sapi |
| 177 | }; |
| 178 | |
| 179 | template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := { |
| 180 | c := SACCH, |
| 181 | na := false, |
| 182 | sapi := sapi |
| 183 | }; |
| 184 | |
Harald Welte | 3b2ce02 | 2017-12-07 17:47:00 +0100 | [diff] [blame] | 185 | template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := { |
Harald Welte | b33e727 | 2017-07-16 21:04:12 +0200 | [diff] [blame] | 186 | c := FACCH_SDCCH, |
| 187 | na := false, |
| 188 | prio := SAPI0_PRIO_NORMAL, |
| 189 | sapi := sapi |
| 190 | }; |
| 191 | |
Harald Welte | 3b2ce02 | 2017-12-07 17:47:00 +0100 | [diff] [blame] | 192 | template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := { |
Harald Welte | cb5d1fb | 2017-07-17 21:00:15 +0200 | [diff] [blame] | 193 | c := SACCH, |
| 194 | na := false, |
| 195 | prio := SAPI0_PRIO_NORMAL, |
| 196 | sapi := sapi |
Harald Welte | b33e727 | 2017-07-16 21:04:12 +0200 | [diff] [blame] | 197 | }; |
| 198 | |
Harald Welte | 23b774e | 2018-02-05 09:14:34 +0100 | [diff] [blame] | 199 | function f_hex_is_odd_length(hexstring digits) return bitstring { |
| 200 | if (lengthof(digits) rem 2 == 1) { |
| 201 | return '1'B; |
| 202 | } else { |
| 203 | return '0'B; |
| 204 | } |
| 205 | } |
| 206 | |
Harald Welte | 5377d2f | 2018-02-24 01:01:19 +0100 | [diff] [blame] | 207 | /* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */ |
| 208 | function dbm2rxlev(integer dbm) return uint6_t { |
| 209 | var integer rxlev := dbm + 110; |
| 210 | if (rxlev > 63) { |
| 211 | rxlev := 63; |
| 212 | } else if (rxlev < 0) { |
| 213 | rxlev := 0; |
| 214 | } |
| 215 | return rxlev; |
| 216 | } |
| 217 | |
| 218 | function rxlev2dbm(uint6_t rxlev) return integer { |
| 219 | return -110 + rxlev; |
| 220 | } |
| 221 | |
| 222 | /* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */ |
| 223 | function ber2rxqual(float ber) return uint3_t { |
| 224 | if (ber < 0.2) { |
| 225 | return 0; |
| 226 | } else if (ber < 0.4) { |
| 227 | return 1; |
| 228 | } else if (ber < 0.8) { |
| 229 | return 2; |
| 230 | } else if (ber < 1.6) { |
| 231 | return 3; |
| 232 | } else if (ber < 3.2) { |
| 233 | return 4; |
| 234 | } else if (ber < 6.4) { |
| 235 | return 5; |
| 236 | } else if (ber < 12.8) { |
| 237 | return 6; |
| 238 | } else { |
| 239 | return 7; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */ |
| 244 | function rxqual2ber(uint3_t rxqual) return float { |
| 245 | select (rxqual) { |
| 246 | case (0) { return 0.14 } |
| 247 | case (1) { return 0.28 } |
| 248 | case (2) { return 0.57 } |
| 249 | case (3) { return 1.13 } |
| 250 | case (4) { return 2.26 } |
| 251 | case (5) { return 4.53 } |
| 252 | case (6) { return 9.05 } |
| 253 | case (7) { return 18.10 } |
| 254 | case else { return 1000.0 } |
| 255 | } |
| 256 | } |
| 257 | |
Harald Welte | 68e495b | 2018-02-25 00:05:57 +0100 | [diff] [blame] | 258 | const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */ |
| 259 | const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */ |
| 260 | const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */ |
| 261 | |
| 262 | /* number of downlink CCCH blocks per second */ |
| 263 | function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer { |
| 264 | if (not combined_ccch) { |
| 265 | /* 9 blocks per 51 multiframe */ |
| 266 | return 9; |
| 267 | } else { |
| 268 | /* 3 blocks per 51 multiframe */ |
| 269 | return 3; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* this ignores any possible paging combining! */ |
| 274 | function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float { |
| 275 | var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch); |
| 276 | var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res; |
| 277 | return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe); |
| 278 | } |
| 279 | |
| 280 | /* this ignores any possible imm.ass combining! */ |
| 281 | function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float { |
| 282 | var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch); |
| 283 | return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res); |
| 284 | } |
| 285 | |
Harald Welte | 262f122 | 2018-02-25 16:33:38 +0100 | [diff] [blame] | 286 | /* compute TC as per 45.002 6.3.1.3 */ |
| 287 | function f_gsm_compute_tc(integer fn) return integer { |
| 288 | return (fn / 51) mod 8; |
| 289 | } |
| 290 | |
Harald Welte | 5377d2f | 2018-02-24 01:01:19 +0100 | [diff] [blame] | 291 | |
Harald Welte | a082a69 | 2017-07-15 15:58:13 +0200 | [diff] [blame] | 292 | } with { encode "RAW"; variant "FIELDORDER(msb)" } |