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