blob: 3e24f8160d7bb3987772f6bca2e78999bdf255ee [file] [log] [blame]
Harald Weltea082a692017-07-15 15:58:13 +02001/* Encoding/Decoding routines for GSM System Information messages
Harald Welte34b5a952019-05-27 11:54:11 +02002 * according to 3GPP TS 44.018 Version 12.3.0 Release 12
3 *
4 * (C) 2017-2018 Harald Welte <laforge@gnumonks.org>
5 * All rights reserved.
6 *
7 * Released under the terms of GNU General Public License, Version 2 or
8 * (at your option) any later version.
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
Harald Weltea082a692017-07-15 15:58:13 +020012
13module GSM_Types {
14
15 import from General_Types all;
16 import from Osmocom_Types all;
17
Harald Weltebdc5dbd2017-07-16 00:00:43 +020018 type integer GsmArfcn (0..1023);
19 type integer UmtsArfcn (0..16383);
20 type integer UmtsScramblingCode (0..511);
Harald Welted2e342f2017-07-16 07:34:13 +020021 const integer GsmMaxFrameNumber := 26*51*2048;
22 type integer GsmFrameNumber (0..GsmMaxFrameNumber);
23 type integer GsmRxLev (0..63);
24 type integer GsmTsc (0..7) with { variant "FIELDLENGTH(8)" };
25 type uint32_t GsmTmsi;
Harald Welteacc93ab2018-03-02 21:39:09 +010026 type OCT4 GprsTlli;
Harald Welte72cecfa2017-12-11 19:50:14 +010027 type hexstring GsmMcc length(3);
28 type hexstring GsmMnc length(2 .. 3);
29 type uint16_t GsmLac;
30 type uint16_t GsmCellId;
Harald Weltebdc5dbd2017-07-16 00:00:43 +020031
Harald Welte1dcc3712017-08-01 00:05:52 +020032 type enumerated GprsCodingScheme {
33 CS1, CS2, CS3, CS4
34 };
35
Harald Welte060e27a2018-03-03 20:38:19 +010036 function f_gprs_blocksize(GprsCodingScheme cs) return integer {
37 select (cs) {
38 case (CS1) { return 22 }
39 case (CS2) { return 32 }
40 case (CS3) { return 38 }
41 case (CS3) { return 52 }
42 case else {
43 setverdict(fail, "Invalid GPRS CS ", cs);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020044 mtc.stop;
Harald Welte060e27a2018-03-03 20:38:19 +010045 return -1;
46 }
47 }
48 }
49
Harald Welted2e342f2017-07-16 07:34:13 +020050 /* 10.5.2.8 */
51 type enumerated ChannelNeeded {
52 CHAN_NEED_ANY (0),
53 CHAN_NEED_SDCCH (1),
54 CHAN_NEED_TCH_F (2),
55 CHAN_NEED_TCH_H (3)
56 } with { variant "FIELDLENGTH(2)" };
57 type record ChannelNeeded12 {
58 ChannelNeeded second,
59 ChannelNeeded first
60 } with { variant "" };
61
Harald Welted2e342f2017-07-16 07:34:13 +020062
Harald Welte0c8d5c02017-07-16 18:53:58 +020063 /* TS 48.058 9.3.1 Channel Number IE */
64 type enumerated RslChanNr0 {
Harald Welte6f3c2232017-07-30 17:17:12 +020065 RSL_CHAN_NR_INVALID ('00000'B),
66 RSL_CHAN_NR_Bm_ACCH ('00001'B),
67 RSL_CHAN_NR_BCCH ('10000'B),
68 RSL_CHAN_NR_RACH ('10001'B),
69 RSL_CHAN_NR_PCH_AGCH ('10010'B),
Harald Welte42ca6b12018-09-15 17:44:06 +030070 RSL_CHAN_NR_OSMO_PDCH ('11000'B),
71 RSL_CHAN_NR_OSMO_CBCH ('11001'B)
Harald Weltec84d8472017-07-30 00:49:56 +020072 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020073
74 type record RslChanNr2 {
75 BIT4 tag ('0001'B),
76 uint1_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020077 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020078
79 type record RslChanNr4 {
80 BIT3 tag ('001'B),
81 uint2_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020082 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020083
84 type record RslChanNr8 {
85 BIT2 tag ('01'B),
86 uint3_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020087 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020088
89 type union RslChanNrU {
90 RslChanNr0 ch0,
91 RslChanNr2 lm,
92 RslChanNr4 sdcch4,
93 RslChanNr8 sdcch8
94 } with {
95 variant "TAG(lm, tag = '0001'B;
96 sdcch4, tag = '001'B;
97 sdcch8, tag = '01'B;
98 ch0, OTHERWISE)"
99 variant "FIELDLENGTH(5)"
Harald Weltec84d8472017-07-30 00:49:56 +0200100 variant "FIELDORDER(msb)"
Harald Welte0c8d5c02017-07-16 18:53:58 +0200101 };
102
103 type record RslChannelNr {
104 RslChanNrU u,
105 uint3_t tn
Harald Weltec84d8472017-07-30 00:49:56 +0200106 } with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200107
108 template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := {
109 u := { ch0 := cht },
110 tn := tn
111 }
112
113 template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH);
114 template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
115 template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
116 template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
Harald Welte1dcc3712017-08-01 00:05:52 +0200117 template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
Harald Welte42ca6b12018-09-15 17:44:06 +0300118 template RslChannelNr t_RslChanNr_CBCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH);
Harald Welte2c2e8c42018-01-29 21:59:39 +0100119 template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := {
Harald Welte0c8d5c02017-07-16 18:53:58 +0200120 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
121 tn := tn
122 }
123 template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := {
124 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
125 tn := tn
126 }
127 template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := {
128 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
129 tn := tn
130 }
Harald Welted2e342f2017-07-16 07:34:13 +0200131
Harald Welte629cc6b2018-03-11 17:19:05 +0100132 template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := {
133 u := { ch0 := cht },
134 tn := tn
135 }
136 template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH);
137 template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
138 template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
139 template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
140 template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
Harald Welte42ca6b12018-09-15 17:44:06 +0300141 template (value) RslChannelNr ts_RslChanNr_CBCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH);
Harald Welte629cc6b2018-03-11 17:19:05 +0100142 template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := {
143 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
144 tn := tn
145 }
146 template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := {
147 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
148 tn := tn
149 }
150 template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := {
151 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
152 tn := tn
153 }
154
Harald Welteb33e7272017-07-16 21:04:12 +0200155 /* TS 48.058 9.3.2 Link ID */
156 type enumerated RslLinkIdC {
157 FACCH_SDCCH (0),
158 SACCH (1)
159 } with { variant "FIELDLENGTH(2)" };
160
161 type enumerated RslSapi0Prio {
162 SAPI0_PRIO_NORMAL (0),
163 SAPI0_PRIO_HIGH (1),
164 SAPI0_PRIO_LOW (2)
165 } with { variant "FIELDLENGTH(2)" };
166
167 type uint3_t GsmSapi;
168
169 type record RslLinkId {
170 RslLinkIdC c,
171 boolean na,
172 RslSapi0Prio prio,
173 GsmSapi sapi
174 } with { variant "" };
175
176 template RslLinkId tr_RslLinkId := {
177 c := ?,
178 na := ?,
179 prio := ?,
180 sapi := ?
181 };
182
183 template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
184 c := FACCH_SDCCH,
185 na := false,
186 sapi := sapi
187 };
188
189 template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
190 c := SACCH,
191 na := false,
192 sapi := sapi
193 };
194
Harald Welte3b2ce022017-12-07 17:47:00 +0100195 template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
Harald Welteb33e7272017-07-16 21:04:12 +0200196 c := FACCH_SDCCH,
197 na := false,
198 prio := SAPI0_PRIO_NORMAL,
199 sapi := sapi
200 };
201
Harald Welte3b2ce022017-12-07 17:47:00 +0100202 template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
Harald Weltecb5d1fb2017-07-17 21:00:15 +0200203 c := SACCH,
204 na := false,
205 prio := SAPI0_PRIO_NORMAL,
206 sapi := sapi
Harald Welteb33e7272017-07-16 21:04:12 +0200207 };
208
Harald Welte23b774e2018-02-05 09:14:34 +0100209 function f_hex_is_odd_length(hexstring digits) return bitstring {
210 if (lengthof(digits) rem 2 == 1) {
211 return '1'B;
212 } else {
213 return '0'B;
214 }
215 }
216
Harald Weltef097a8b2018-09-16 18:11:26 +0200217
218/* TS 04.12 Section 3.3.1 Block type */
219type record CBCH_BlockType {
220 BIT1 spare,
221 BIT2 lpd,
222 boolean last_block,
223 uint4_t seq_nr
224};
225template (value) CBCH_BlockType ts_CBCH_BlockType(template (value) uint4_t seq_nr, template (value) boolean last_block) := {
226 spare := '0'B,
227 lpd := '01'B,
228 last_block := last_block,
229 seq_nr := seq_nr
230};
231template CBCH_BlockType tr_CBCH_BlockType(template uint4_t seq_nr := ?, template boolean last_block := ?) := {
232 spare := '0'B,
233 lpd := '01'B,
234 last_block := last_block,
235 seq_nr := seq_nr
236};
237
238/* TS 04.12 Section 3.3 */
239type record CBCH_Block {
240 CBCH_BlockType block_type,
241 OCT22 payload
242};
243template (value) CBCH_Block ts_CBCH_Block(template (value) uint4_t seq_nr, template (value) boolean last_block, template (value) OCT22 payload) := {
244 block_type := ts_CBCH_BlockType(seq_nr, last_block),
245 payload := payload
246};
247template CBCH_Block tr_CBCH_Block(template uint4_t seq_nr := ?, template boolean last_block := ?, template OCT22 payload := ?) := {
248 block_type := tr_CBCH_BlockType(seq_nr, last_block),
249 payload := payload
250};
251
252
253external function enc_CBCH_Block(in CBCH_Block msg) return octetstring
254 with { extension "prototype(convert) encode(RAW)" };
255external function dec_CBCH_Block(in octetstring stream) return CBCH_Block
256 with { extension "prototype(convert) decode(RAW)" };
257
258
Harald Welte5377d2f2018-02-24 01:01:19 +0100259/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
260function dbm2rxlev(integer dbm) return uint6_t {
261 var integer rxlev := dbm + 110;
262 if (rxlev > 63) {
263 rxlev := 63;
264 } else if (rxlev < 0) {
265 rxlev := 0;
266 }
267 return rxlev;
268}
269
270function rxlev2dbm(uint6_t rxlev) return integer {
271 return -110 + rxlev;
272}
273
274/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
275function ber2rxqual(float ber) return uint3_t {
276 if (ber < 0.2) {
277 return 0;
278 } else if (ber < 0.4) {
279 return 1;
280 } else if (ber < 0.8) {
281 return 2;
282 } else if (ber < 1.6) {
283 return 3;
284 } else if (ber < 3.2) {
285 return 4;
286 } else if (ber < 6.4) {
287 return 5;
288 } else if (ber < 12.8) {
289 return 6;
290 } else {
291 return 7;
292 }
293}
294
295/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
296function rxqual2ber(uint3_t rxqual) return float {
297 select (rxqual) {
298 case (0) { return 0.14 }
299 case (1) { return 0.28 }
300 case (2) { return 0.57 }
301 case (3) { return 1.13 }
302 case (4) { return 2.26 }
303 case (5) { return 4.53 }
304 case (6) { return 9.05 }
305 case (7) { return 18.10 }
306 case else { return 1000.0 }
307 }
308}
309
Harald Welte68e495b2018-02-25 00:05:57 +0100310const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
311const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
312const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
313
314/* number of downlink CCCH blocks per second */
315function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
316 if (not combined_ccch) {
317 /* 9 blocks per 51 multiframe */
318 return 9;
319 } else {
320 /* 3 blocks per 51 multiframe */
321 return 3;
322 }
323}
324
325/* this ignores any possible paging combining! */
326function f_pch_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 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
329 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
330}
331
332/* this ignores any possible imm.ass combining! */
333function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
334 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
335 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
336}
337
Harald Welte262f1222018-02-25 16:33:38 +0100338/* compute TC as per 45.002 6.3.1.3 */
339function f_gsm_compute_tc(integer fn) return integer {
340 return (fn / 51) mod 8;
341}
342
Harald Welte5377d2f2018-02-24 01:01:19 +0100343
Harald Weltea082a692017-07-15 15:58:13 +0200344} with { encode "RAW"; variant "FIELDORDER(msb)" }