blob: bcf2794eb4e9427354270de984b504f56e7575d3 [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
Alexander Couzens5e6ae792020-09-11 19:49:22 +020015import from General_Types all;
16import from Osmocom_Types all;
Harald Weltea082a692017-07-15 15:58:13 +020017
Alexander Couzens5e6ae792020-09-11 19:49:22 +020018type integer GsmArfcn (0..1023);
19type integer UmtsArfcn (0..16383);
20type integer UmtsScramblingCode (0..511);
21const integer GsmMaxFrameNumber := 26*51*2048;
22type integer GsmFrameNumber (0..GsmMaxFrameNumber);
23type integer GsmRxLev (0..63);
24type integer GsmTsc (0..7) with { variant "FIELDLENGTH(8)" };
25type uint32_t GsmTmsi;
26type OCT4 GprsTlli;
27type hexstring GsmMcc length(3);
28type hexstring GsmMnc length(2 .. 3);
29type uint16_t GsmLac;
30type uint16_t GsmCellId;
Harald Weltebdc5dbd2017-07-16 00:00:43 +020031
Alexander Couzens5e6ae792020-09-11 19:49:22 +020032type enumerated GprsCodingScheme {
33 CS1, CS2, CS3, CS4
34};
Harald Welte1dcc3712017-08-01 00:05:52 +020035
Alexander Couzens5e6ae792020-09-11 19:49:22 +020036function 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);
44 mtc.stop;
Harald Welte060e27a2018-03-03 20:38:19 +010045 }
46 }
Alexander Couzens5e6ae792020-09-11 19:49:22 +020047}
Harald Welte060e27a2018-03-03 20:38:19 +010048
Pau Espin Pedrolbfd69f62020-10-22 18:06:07 +020049const GprsTlli TLLI_UNUSED := 'FFFFFFFF'O;
50function f_gen_tlli() return GprsTlli {
51 var GprsTlli tlli := f_rnd_octstring(4);
52 if (tlli == TLLI_UNUSED) {
53 tlli := 'EEEEEEEE'O;
54 }
55 return tlli;
56}
57
Alexander Couzens5e6ae792020-09-11 19:49:22 +020058/* 10.5.2.8 */
59type enumerated ChannelNeeded {
60 CHAN_NEED_ANY (0),
61 CHAN_NEED_SDCCH (1),
62 CHAN_NEED_TCH_F (2),
63 CHAN_NEED_TCH_H (3)
64} with { variant "FIELDLENGTH(2)" };
65type record ChannelNeeded12 {
66 ChannelNeeded second,
67 ChannelNeeded first
68} with { variant "" };
Harald Welted2e342f2017-07-16 07:34:13 +020069
Harald Welted2e342f2017-07-16 07:34:13 +020070
Alexander Couzens5e6ae792020-09-11 19:49:22 +020071/* TS 48.058 9.3.1 Channel Number IE */
72type enumerated RslChanNr0 {
73 RSL_CHAN_NR_INVALID ('00000'B),
74 RSL_CHAN_NR_Bm_ACCH ('00001'B),
75 RSL_CHAN_NR_BCCH ('10000'B),
76 RSL_CHAN_NR_RACH ('10001'B),
77 RSL_CHAN_NR_PCH_AGCH ('10010'B),
78 RSL_CHAN_NR_OSMO_PDCH ('11000'B),
79 RSL_CHAN_NR_OSMO_CBCH4 ('11001'B),
80 RSL_CHAN_NR_OSMO_CBCH8 ('11010'B)
81} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020082
Alexander Couzens5e6ae792020-09-11 19:49:22 +020083type record RslChanNr2 {
84 BIT4 tag ('0001'B),
85 uint1_t sub_chan
86} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020087
Alexander Couzens5e6ae792020-09-11 19:49:22 +020088type record RslChanNr4 {
89 BIT3 tag ('001'B),
90 uint2_t sub_chan
91} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020092
Alexander Couzens5e6ae792020-09-11 19:49:22 +020093type record RslChanNr8 {
94 BIT2 tag ('01'B),
95 uint3_t sub_chan
96} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020097
Alexander Couzens5e6ae792020-09-11 19:49:22 +020098type union RslChanNrU {
99 RslChanNr0 ch0,
100 RslChanNr2 lm,
101 RslChanNr4 sdcch4,
102 RslChanNr8 sdcch8
103} with {
104 variant "TAG(lm, tag = '0001'B;
105 sdcch4, tag = '001'B;
106 sdcch8, tag = '01'B;
107 ch0, OTHERWISE)"
108 variant "FIELDLENGTH(5)"
109 variant "FIELDORDER(msb)"
110};
Harald Welte0c8d5c02017-07-16 18:53:58 +0200111
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200112type record RslChannelNr {
113 RslChanNrU u,
114 uint3_t tn
115} with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200116
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200117template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := {
118 u := { ch0 := cht },
119 tn := tn
120}
121
122template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH);
123template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
124template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
125template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
126template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
127template RslChannelNr t_RslChanNr_CBCH4(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH4);
128template RslChannelNr t_RslChanNr_CBCH8(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH8);
129template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := {
130 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
131 tn := tn
132}
133template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := {
134 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
135 tn := tn
136}
137template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := {
138 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
139 tn := tn
140}
141
142template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := {
143 u := { ch0 := cht },
144 tn := tn
145}
146template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH);
147template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
148template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
149template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
150template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
151template (value) RslChannelNr ts_RslChanNr_CBCH4(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH4);
152template (value) RslChannelNr ts_RslChanNr_CBCH8(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH8);
153template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := {
154 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
155 tn := tn
156}
157template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := {
158 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
159 tn := tn
160}
161template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := {
162 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
163 tn := tn
164}
165
166/* TS 48.058 9.3.2 Link ID */
167type enumerated RslLinkIdC {
168 FACCH_SDCCH (0),
169 SACCH (1),
170 OSMO_PTCCH (2) /* Osmocom (trxcon) specific extension */
171} with { variant "FIELDLENGTH(2)" };
172
173type enumerated RslSapi0Prio {
174 SAPI0_PRIO_NORMAL (0),
175 SAPI0_PRIO_HIGH (1),
176 SAPI0_PRIO_LOW (2)
177} with { variant "FIELDLENGTH(2)" };
178
179type uint3_t GsmSapi;
180
181type record RslLinkId {
182 RslLinkIdC c,
183 boolean na,
184 RslSapi0Prio prio,
185 GsmSapi sapi
186} with { variant "" };
187
188template RslLinkId tr_RslLinkId := {
189 c := ?,
190 na := ?,
191 prio := ?,
192 sapi := ?
193};
194
195template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
196 c := FACCH_SDCCH,
197 na := false,
198 sapi := sapi
199};
200
201template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
202 c := SACCH,
203 na := false,
204 sapi := sapi
205};
206
207template RslLinkId tr_RslLinkID_OSMO_PTCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
208 c := OSMO_PTCCH,
209 na := false,
210 sapi := sapi
211};
212
213template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
214 c := FACCH_SDCCH,
215 na := false,
216 prio := SAPI0_PRIO_NORMAL,
217 sapi := sapi
218};
219
220template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
221 c := SACCH,
222 na := false,
223 prio := SAPI0_PRIO_NORMAL,
224 sapi := sapi
225};
226
227template (value) RslLinkId ts_RslLinkID_OSMO_PTCCH(GsmSapi sapi) := {
228 c := OSMO_PTCCH,
229 na := false,
230 prio := SAPI0_PRIO_NORMAL,
231 sapi := sapi
232};
233
234function f_hex_is_odd_length(hexstring digits) return bitstring {
235 if (lengthof(digits) rem 2 == 1) {
236 return '1'B;
237 } else {
238 return '0'B;
Harald Welte0c8d5c02017-07-16 18:53:58 +0200239 }
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200240}
Harald Welte23b774e2018-02-05 09:14:34 +0100241
Harald Weltef097a8b2018-09-16 18:11:26 +0200242
243/* TS 04.12 Section 3.3.1 Block type */
244type record CBCH_BlockType {
245 BIT1 spare,
246 BIT2 lpd,
247 boolean last_block,
248 uint4_t seq_nr
249};
250template (value) CBCH_BlockType ts_CBCH_BlockType(template (value) uint4_t seq_nr, template (value) boolean last_block) := {
251 spare := '0'B,
252 lpd := '01'B,
253 last_block := last_block,
254 seq_nr := seq_nr
255};
256template CBCH_BlockType tr_CBCH_BlockType(template uint4_t seq_nr := ?, template boolean last_block := ?) := {
257 spare := '0'B,
258 lpd := '01'B,
259 last_block := last_block,
260 seq_nr := seq_nr
261};
262
263/* TS 04.12 Section 3.3 */
264type record CBCH_Block {
265 CBCH_BlockType block_type,
266 OCT22 payload
267};
268template (value) CBCH_Block ts_CBCH_Block(template (value) uint4_t seq_nr, template (value) boolean last_block, template (value) OCT22 payload) := {
269 block_type := ts_CBCH_BlockType(seq_nr, last_block),
270 payload := payload
271};
272template CBCH_Block tr_CBCH_Block(template uint4_t seq_nr := ?, template boolean last_block := ?, template OCT22 payload := ?) := {
273 block_type := tr_CBCH_BlockType(seq_nr, last_block),
274 payload := payload
275};
276
277
278external function enc_CBCH_Block(in CBCH_Block msg) return octetstring
279 with { extension "prototype(convert) encode(RAW)" };
280external function dec_CBCH_Block(in octetstring stream) return CBCH_Block
281 with { extension "prototype(convert) decode(RAW)" };
282
283
Harald Welte5377d2f2018-02-24 01:01:19 +0100284/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
285function dbm2rxlev(integer dbm) return uint6_t {
286 var integer rxlev := dbm + 110;
287 if (rxlev > 63) {
288 rxlev := 63;
289 } else if (rxlev < 0) {
290 rxlev := 0;
291 }
292 return rxlev;
293}
294
295function rxlev2dbm(uint6_t rxlev) return integer {
296 return -110 + rxlev;
297}
298
299/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
300function ber2rxqual(float ber) return uint3_t {
301 if (ber < 0.2) {
302 return 0;
303 } else if (ber < 0.4) {
304 return 1;
305 } else if (ber < 0.8) {
306 return 2;
307 } else if (ber < 1.6) {
308 return 3;
309 } else if (ber < 3.2) {
310 return 4;
311 } else if (ber < 6.4) {
312 return 5;
313 } else if (ber < 12.8) {
314 return 6;
315 } else {
316 return 7;
317 }
318}
319
320/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
321function rxqual2ber(uint3_t rxqual) return float {
322 select (rxqual) {
323 case (0) { return 0.14 }
324 case (1) { return 0.28 }
325 case (2) { return 0.57 }
326 case (3) { return 1.13 }
327 case (4) { return 2.26 }
328 case (5) { return 4.53 }
329 case (6) { return 9.05 }
330 case (7) { return 18.10 }
331 case else { return 1000.0 }
332 }
333}
334
Harald Welte68e495b2018-02-25 00:05:57 +0100335const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
336const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
337const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
338
339/* number of downlink CCCH blocks per second */
340function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
341 if (not combined_ccch) {
342 /* 9 blocks per 51 multiframe */
343 return 9;
344 } else {
345 /* 3 blocks per 51 multiframe */
346 return 3;
347 }
348}
349
350/* this ignores any possible paging combining! */
351function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
352 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
353 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
354 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
355}
356
357/* this ignores any possible imm.ass combining! */
358function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
359 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
360 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
361}
362
Harald Welte262f1222018-02-25 16:33:38 +0100363/* compute TC as per 45.002 6.3.1.3 */
364function f_gsm_compute_tc(integer fn) return integer {
365 return (fn / 51) mod 8;
366}
367
Harald Welte557c9f82020-09-12 21:30:17 +0200368type hexstring GsmBcdString with { variant "HEXORDER(low)" };
369type GsmBcdString BcdMccMnc with { variant "FIELDLENGTH(6)" };
370
371/* 24.008 10.5.1.3 */
372type record LocationAreaIdentification {
373 BcdMccMnc mcc_mnc,
374 uint16_t lac
375} with { variant "" };
376
377/* 24.008 10.5.5.15 */
378type record RoutingAreaIdentification {
379 LocationAreaIdentification lai,
380 uint8_t rac
381} with { variant "" };
382
383external function enc_RoutingAreaIdentification(RoutingAreaIdentification rai) return octetstring
384with { extension "prototype(convert)" extension "encode(RAW)" }
385
386/* TS 24.008 10.5.1.1 */
387type uint16_t CellIdentity;
388
Harald Welte5377d2f2018-02-24 01:01:19 +0100389
Harald Weltea082a692017-07-15 15:58:13 +0200390} with { encode "RAW"; variant "FIELDORDER(msb)" }