blob: 88be9757c23bb2d22fc2892f1d1d51aeadfb1077 [file] [log] [blame]
Harald Weltea082a692017-07-15 15:58:13 +02001/* 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
6module GSM_Types {
7
8 import from General_Types all;
9 import from Osmocom_Types all;
10
Harald Weltebdc5dbd2017-07-16 00:00:43 +020011 type integer GsmArfcn (0..1023);
12 type integer UmtsArfcn (0..16383);
13 type integer UmtsScramblingCode (0..511);
Harald Welted2e342f2017-07-16 07:34:13 +020014 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 Welteacc93ab2018-03-02 21:39:09 +010019 type OCT4 GprsTlli;
Harald Welte72cecfa2017-12-11 19:50:14 +010020 type hexstring GsmMcc length(3);
21 type hexstring GsmMnc length(2 .. 3);
22 type uint16_t GsmLac;
23 type uint16_t GsmCellId;
Harald Weltebdc5dbd2017-07-16 00:00:43 +020024
Harald Welte1dcc3712017-08-01 00:05:52 +020025 type enumerated GprsCodingScheme {
26 CS1, CS2, CS3, CS4
27 };
28
Harald Welte060e27a2018-03-03 20:38:19 +010029 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 Willmanne4ff5372018-07-05 17:35:03 +020037 mtc.stop;
Harald Welte060e27a2018-03-03 20:38:19 +010038 return -1;
39 }
40 }
41 }
42
Harald Welted2e342f2017-07-16 07:34:13 +020043 /* 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 Welted2e342f2017-07-16 07:34:13 +020055
Harald Welte0c8d5c02017-07-16 18:53:58 +020056 /* TS 48.058 9.3.1 Channel Number IE */
57 type enumerated RslChanNr0 {
Harald Welte6f3c2232017-07-30 17:17:12 +020058 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),
Harald Welte42ca6b12018-09-15 17:44:06 +030063 RSL_CHAN_NR_OSMO_PDCH ('11000'B),
64 RSL_CHAN_NR_OSMO_CBCH ('11001'B)
Harald Weltec84d8472017-07-30 00:49:56 +020065 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020066
67 type record RslChanNr2 {
68 BIT4 tag ('0001'B),
69 uint1_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020070 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020071
72 type record RslChanNr4 {
73 BIT3 tag ('001'B),
74 uint2_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020075 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020076
77 type record RslChanNr8 {
78 BIT2 tag ('01'B),
79 uint3_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020080 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020081
82 type union RslChanNrU {
83 RslChanNr0 ch0,
84 RslChanNr2 lm,
85 RslChanNr4 sdcch4,
86 RslChanNr8 sdcch8
87 } with {
88 variant "TAG(lm, tag = '0001'B;
89 sdcch4, tag = '001'B;
90 sdcch8, tag = '01'B;
91 ch0, OTHERWISE)"
92 variant "FIELDLENGTH(5)"
Harald Weltec84d8472017-07-30 00:49:56 +020093 variant "FIELDORDER(msb)"
Harald Welte0c8d5c02017-07-16 18:53:58 +020094 };
95
96 type record RslChannelNr {
97 RslChanNrU u,
98 uint3_t tn
Harald Weltec84d8472017-07-30 00:49:56 +020099 } with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200100
101 template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := {
102 u := { ch0 := cht },
103 tn := tn
104 }
105
106 template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH);
107 template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
108 template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
109 template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
Harald Welte1dcc3712017-08-01 00:05:52 +0200110 template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
Harald Welte42ca6b12018-09-15 17:44:06 +0300111 template RslChannelNr t_RslChanNr_CBCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH);
Harald Welte2c2e8c42018-01-29 21:59:39 +0100112 template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := {
Harald Welte0c8d5c02017-07-16 18:53:58 +0200113 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
114 tn := tn
115 }
116 template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := {
117 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
118 tn := tn
119 }
120 template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := {
121 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
122 tn := tn
123 }
Harald Welted2e342f2017-07-16 07:34:13 +0200124
Harald Welte629cc6b2018-03-11 17:19:05 +0100125 template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := {
126 u := { ch0 := cht },
127 tn := tn
128 }
129 template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH);
130 template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
131 template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
132 template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
133 template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
Harald Welte42ca6b12018-09-15 17:44:06 +0300134 template (value) RslChannelNr ts_RslChanNr_CBCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH);
Harald Welte629cc6b2018-03-11 17:19:05 +0100135 template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := {
136 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
137 tn := tn
138 }
139 template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := {
140 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
141 tn := tn
142 }
143 template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := {
144 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
145 tn := tn
146 }
147
Harald Welteb33e7272017-07-16 21:04:12 +0200148 /* TS 48.058 9.3.2 Link ID */
149 type enumerated RslLinkIdC {
150 FACCH_SDCCH (0),
151 SACCH (1)
152 } with { variant "FIELDLENGTH(2)" };
153
154 type enumerated RslSapi0Prio {
155 SAPI0_PRIO_NORMAL (0),
156 SAPI0_PRIO_HIGH (1),
157 SAPI0_PRIO_LOW (2)
158 } with { variant "FIELDLENGTH(2)" };
159
160 type uint3_t GsmSapi;
161
162 type record RslLinkId {
163 RslLinkIdC c,
164 boolean na,
165 RslSapi0Prio prio,
166 GsmSapi sapi
167 } with { variant "" };
168
169 template RslLinkId tr_RslLinkId := {
170 c := ?,
171 na := ?,
172 prio := ?,
173 sapi := ?
174 };
175
176 template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
177 c := FACCH_SDCCH,
178 na := false,
179 sapi := sapi
180 };
181
182 template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
183 c := SACCH,
184 na := false,
185 sapi := sapi
186 };
187
Harald Welte3b2ce022017-12-07 17:47:00 +0100188 template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
Harald Welteb33e7272017-07-16 21:04:12 +0200189 c := FACCH_SDCCH,
190 na := false,
191 prio := SAPI0_PRIO_NORMAL,
192 sapi := sapi
193 };
194
Harald Welte3b2ce022017-12-07 17:47:00 +0100195 template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
Harald Weltecb5d1fb2017-07-17 21:00:15 +0200196 c := SACCH,
197 na := false,
198 prio := SAPI0_PRIO_NORMAL,
199 sapi := sapi
Harald Welteb33e7272017-07-16 21:04:12 +0200200 };
201
Harald Welte23b774e2018-02-05 09:14:34 +0100202 function f_hex_is_odd_length(hexstring digits) return bitstring {
203 if (lengthof(digits) rem 2 == 1) {
204 return '1'B;
205 } else {
206 return '0'B;
207 }
208 }
209
Harald Weltef097a8b2018-09-16 18:11:26 +0200210
211/* TS 04.12 Section 3.3.1 Block type */
212type record CBCH_BlockType {
213 BIT1 spare,
214 BIT2 lpd,
215 boolean last_block,
216 uint4_t seq_nr
217};
218template (value) CBCH_BlockType ts_CBCH_BlockType(template (value) uint4_t seq_nr, template (value) boolean last_block) := {
219 spare := '0'B,
220 lpd := '01'B,
221 last_block := last_block,
222 seq_nr := seq_nr
223};
224template CBCH_BlockType tr_CBCH_BlockType(template uint4_t seq_nr := ?, template boolean last_block := ?) := {
225 spare := '0'B,
226 lpd := '01'B,
227 last_block := last_block,
228 seq_nr := seq_nr
229};
230
231/* TS 04.12 Section 3.3 */
232type record CBCH_Block {
233 CBCH_BlockType block_type,
234 OCT22 payload
235};
236template (value) CBCH_Block ts_CBCH_Block(template (value) uint4_t seq_nr, template (value) boolean last_block, template (value) OCT22 payload) := {
237 block_type := ts_CBCH_BlockType(seq_nr, last_block),
238 payload := payload
239};
240template CBCH_Block tr_CBCH_Block(template uint4_t seq_nr := ?, template boolean last_block := ?, template OCT22 payload := ?) := {
241 block_type := tr_CBCH_BlockType(seq_nr, last_block),
242 payload := payload
243};
244
245
246external function enc_CBCH_Block(in CBCH_Block msg) return octetstring
247 with { extension "prototype(convert) encode(RAW)" };
248external function dec_CBCH_Block(in octetstring stream) return CBCH_Block
249 with { extension "prototype(convert) decode(RAW)" };
250
251
Harald Welte5377d2f2018-02-24 01:01:19 +0100252/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
253function dbm2rxlev(integer dbm) return uint6_t {
254 var integer rxlev := dbm + 110;
255 if (rxlev > 63) {
256 rxlev := 63;
257 } else if (rxlev < 0) {
258 rxlev := 0;
259 }
260 return rxlev;
261}
262
263function rxlev2dbm(uint6_t rxlev) return integer {
264 return -110 + rxlev;
265}
266
267/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
268function ber2rxqual(float ber) return uint3_t {
269 if (ber < 0.2) {
270 return 0;
271 } else if (ber < 0.4) {
272 return 1;
273 } else if (ber < 0.8) {
274 return 2;
275 } else if (ber < 1.6) {
276 return 3;
277 } else if (ber < 3.2) {
278 return 4;
279 } else if (ber < 6.4) {
280 return 5;
281 } else if (ber < 12.8) {
282 return 6;
283 } else {
284 return 7;
285 }
286}
287
288/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
289function rxqual2ber(uint3_t rxqual) return float {
290 select (rxqual) {
291 case (0) { return 0.14 }
292 case (1) { return 0.28 }
293 case (2) { return 0.57 }
294 case (3) { return 1.13 }
295 case (4) { return 2.26 }
296 case (5) { return 4.53 }
297 case (6) { return 9.05 }
298 case (7) { return 18.10 }
299 case else { return 1000.0 }
300 }
301}
302
Harald Welte68e495b2018-02-25 00:05:57 +0100303const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
304const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
305const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
306
307/* number of downlink CCCH blocks per second */
308function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
309 if (not combined_ccch) {
310 /* 9 blocks per 51 multiframe */
311 return 9;
312 } else {
313 /* 3 blocks per 51 multiframe */
314 return 3;
315 }
316}
317
318/* this ignores any possible paging combining! */
319function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
320 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
321 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
322 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
323}
324
325/* this ignores any possible imm.ass combining! */
326function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
327 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
328 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
329}
330
Harald Welte262f1222018-02-25 16:33:38 +0100331/* compute TC as per 45.002 6.3.1.3 */
332function f_gsm_compute_tc(integer fn) return integer {
333 return (fn / 51) mod 8;
334}
335
Harald Welte5377d2f2018-02-24 01:01:19 +0100336
Harald Weltea082a692017-07-15 15:58:13 +0200337} with { encode "RAW"; variant "FIELDORDER(msb)" }