blob: ee6d1326be54c548c2f7db8ae2027103496868f5 [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),
Harald Welte6b86b342019-05-27 17:14:36 +020071 RSL_CHAN_NR_OSMO_CBCH4 ('11001'B),
72 RSL_CHAN_NR_OSMO_CBCH8 ('11010'B)
Harald Weltec84d8472017-07-30 00:49:56 +020073 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020074
75 type record RslChanNr2 {
76 BIT4 tag ('0001'B),
77 uint1_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020078 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020079
80 type record RslChanNr4 {
81 BIT3 tag ('001'B),
82 uint2_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020083 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020084
85 type record RslChanNr8 {
86 BIT2 tag ('01'B),
87 uint3_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020088 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020089
90 type union RslChanNrU {
91 RslChanNr0 ch0,
92 RslChanNr2 lm,
93 RslChanNr4 sdcch4,
94 RslChanNr8 sdcch8
95 } with {
96 variant "TAG(lm, tag = '0001'B;
97 sdcch4, tag = '001'B;
98 sdcch8, tag = '01'B;
99 ch0, OTHERWISE)"
100 variant "FIELDLENGTH(5)"
Harald Weltec84d8472017-07-30 00:49:56 +0200101 variant "FIELDORDER(msb)"
Harald Welte0c8d5c02017-07-16 18:53:58 +0200102 };
103
104 type record RslChannelNr {
105 RslChanNrU u,
106 uint3_t tn
Harald Weltec84d8472017-07-30 00:49:56 +0200107 } with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200108
109 template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := {
110 u := { ch0 := cht },
111 tn := tn
112 }
113
114 template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH);
115 template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
116 template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
117 template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
Harald Welte1dcc3712017-08-01 00:05:52 +0200118 template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
Harald Welte6b86b342019-05-27 17:14:36 +0200119 template RslChannelNr t_RslChanNr_CBCH4(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH4);
120 template RslChannelNr t_RslChanNr_CBCH8(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH8);
Harald Welte2c2e8c42018-01-29 21:59:39 +0100121 template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := {
Harald Welte0c8d5c02017-07-16 18:53:58 +0200122 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
123 tn := tn
124 }
125 template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := {
126 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
127 tn := tn
128 }
129 template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := {
130 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
131 tn := tn
132 }
Harald Welted2e342f2017-07-16 07:34:13 +0200133
Harald Welte629cc6b2018-03-11 17:19:05 +0100134 template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := {
135 u := { ch0 := cht },
136 tn := tn
137 }
138 template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH);
139 template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
140 template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
141 template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
142 template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
Harald Welte6b86b342019-05-27 17:14:36 +0200143 template (value) RslChannelNr ts_RslChanNr_CBCH4(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH4);
144 template (value) RslChannelNr ts_RslChanNr_CBCH8(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH8);
Harald Welte629cc6b2018-03-11 17:19:05 +0100145 template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := {
146 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
147 tn := tn
148 }
149 template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := {
150 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
151 tn := tn
152 }
153 template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := {
154 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
155 tn := tn
156 }
157
Harald Welteb33e7272017-07-16 21:04:12 +0200158 /* TS 48.058 9.3.2 Link ID */
159 type enumerated RslLinkIdC {
160 FACCH_SDCCH (0),
Vadim Yanitskiy8c242f02019-10-13 15:25:54 +0700161 SACCH (1),
162 OSMO_PTCCH (2) /* Osmocom (trxcon) specific extension */
Harald Welteb33e7272017-07-16 21:04:12 +0200163 } with { variant "FIELDLENGTH(2)" };
164
165 type enumerated RslSapi0Prio {
166 SAPI0_PRIO_NORMAL (0),
167 SAPI0_PRIO_HIGH (1),
168 SAPI0_PRIO_LOW (2)
169 } with { variant "FIELDLENGTH(2)" };
170
171 type uint3_t GsmSapi;
172
173 type record RslLinkId {
174 RslLinkIdC c,
175 boolean na,
176 RslSapi0Prio prio,
177 GsmSapi sapi
178 } with { variant "" };
179
180 template RslLinkId tr_RslLinkId := {
181 c := ?,
182 na := ?,
183 prio := ?,
184 sapi := ?
185 };
186
187 template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
188 c := FACCH_SDCCH,
189 na := false,
190 sapi := sapi
191 };
192
193 template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
194 c := SACCH,
195 na := false,
196 sapi := sapi
197 };
198
Vadim Yanitskiy8c242f02019-10-13 15:25:54 +0700199 template RslLinkId tr_RslLinkID_OSMO_PTCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
200 c := OSMO_PTCCH,
201 na := false,
202 sapi := sapi
203 };
204
Harald Welte3b2ce022017-12-07 17:47:00 +0100205 template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
Harald Welteb33e7272017-07-16 21:04:12 +0200206 c := FACCH_SDCCH,
207 na := false,
208 prio := SAPI0_PRIO_NORMAL,
209 sapi := sapi
210 };
211
Harald Welte3b2ce022017-12-07 17:47:00 +0100212 template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
Harald Weltecb5d1fb2017-07-17 21:00:15 +0200213 c := SACCH,
214 na := false,
215 prio := SAPI0_PRIO_NORMAL,
216 sapi := sapi
Harald Welteb33e7272017-07-16 21:04:12 +0200217 };
218
Vadim Yanitskiy8c242f02019-10-13 15:25:54 +0700219 template (value) RslLinkId ts_RslLinkID_OSMO_PTCCH(GsmSapi sapi) := {
220 c := OSMO_PTCCH,
221 na := false,
222 prio := SAPI0_PRIO_NORMAL,
223 sapi := sapi
224 };
225
Harald Welte23b774e2018-02-05 09:14:34 +0100226 function f_hex_is_odd_length(hexstring digits) return bitstring {
227 if (lengthof(digits) rem 2 == 1) {
228 return '1'B;
229 } else {
230 return '0'B;
231 }
232 }
233
Harald Weltef097a8b2018-09-16 18:11:26 +0200234
235/* TS 04.12 Section 3.3.1 Block type */
236type record CBCH_BlockType {
237 BIT1 spare,
238 BIT2 lpd,
239 boolean last_block,
240 uint4_t seq_nr
241};
242template (value) CBCH_BlockType ts_CBCH_BlockType(template (value) uint4_t seq_nr, template (value) boolean last_block) := {
243 spare := '0'B,
244 lpd := '01'B,
245 last_block := last_block,
246 seq_nr := seq_nr
247};
248template CBCH_BlockType tr_CBCH_BlockType(template uint4_t seq_nr := ?, template boolean last_block := ?) := {
249 spare := '0'B,
250 lpd := '01'B,
251 last_block := last_block,
252 seq_nr := seq_nr
253};
254
255/* TS 04.12 Section 3.3 */
256type record CBCH_Block {
257 CBCH_BlockType block_type,
258 OCT22 payload
259};
260template (value) CBCH_Block ts_CBCH_Block(template (value) uint4_t seq_nr, template (value) boolean last_block, template (value) OCT22 payload) := {
261 block_type := ts_CBCH_BlockType(seq_nr, last_block),
262 payload := payload
263};
264template CBCH_Block tr_CBCH_Block(template uint4_t seq_nr := ?, template boolean last_block := ?, template OCT22 payload := ?) := {
265 block_type := tr_CBCH_BlockType(seq_nr, last_block),
266 payload := payload
267};
268
269
270external function enc_CBCH_Block(in CBCH_Block msg) return octetstring
271 with { extension "prototype(convert) encode(RAW)" };
272external function dec_CBCH_Block(in octetstring stream) return CBCH_Block
273 with { extension "prototype(convert) decode(RAW)" };
274
275
Harald Welte5377d2f2018-02-24 01:01:19 +0100276/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
277function dbm2rxlev(integer dbm) return uint6_t {
278 var integer rxlev := dbm + 110;
279 if (rxlev > 63) {
280 rxlev := 63;
281 } else if (rxlev < 0) {
282 rxlev := 0;
283 }
284 return rxlev;
285}
286
287function rxlev2dbm(uint6_t rxlev) return integer {
288 return -110 + rxlev;
289}
290
291/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
292function ber2rxqual(float ber) return uint3_t {
293 if (ber < 0.2) {
294 return 0;
295 } else if (ber < 0.4) {
296 return 1;
297 } else if (ber < 0.8) {
298 return 2;
299 } else if (ber < 1.6) {
300 return 3;
301 } else if (ber < 3.2) {
302 return 4;
303 } else if (ber < 6.4) {
304 return 5;
305 } else if (ber < 12.8) {
306 return 6;
307 } else {
308 return 7;
309 }
310}
311
312/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
313function rxqual2ber(uint3_t rxqual) return float {
314 select (rxqual) {
315 case (0) { return 0.14 }
316 case (1) { return 0.28 }
317 case (2) { return 0.57 }
318 case (3) { return 1.13 }
319 case (4) { return 2.26 }
320 case (5) { return 4.53 }
321 case (6) { return 9.05 }
322 case (7) { return 18.10 }
323 case else { return 1000.0 }
324 }
325}
326
Harald Welte68e495b2018-02-25 00:05:57 +0100327const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
328const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
329const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
330
331/* number of downlink CCCH blocks per second */
332function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
333 if (not combined_ccch) {
334 /* 9 blocks per 51 multiframe */
335 return 9;
336 } else {
337 /* 3 blocks per 51 multiframe */
338 return 3;
339 }
340}
341
342/* this ignores any possible paging combining! */
343function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
344 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
345 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
346 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
347}
348
349/* this ignores any possible imm.ass combining! */
350function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
351 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
352 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
353}
354
Harald Welte262f1222018-02-25 16:33:38 +0100355/* compute TC as per 45.002 6.3.1.3 */
356function f_gsm_compute_tc(integer fn) return integer {
357 return (fn / 51) mod 8;
358}
359
Harald Welte5377d2f2018-02-24 01:01:19 +0100360
Harald Weltea082a692017-07-15 15:58:13 +0200361} with { encode "RAW"; variant "FIELDORDER(msb)" }