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