blob: ee6635ba7a3dbf65158f98bc2c50bf7d924e9111 [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
Alexander Couzens5e6ae792020-09-11 19:49:22 +020049/* 10.5.2.8 */
50type enumerated ChannelNeeded {
51 CHAN_NEED_ANY (0),
52 CHAN_NEED_SDCCH (1),
53 CHAN_NEED_TCH_F (2),
54 CHAN_NEED_TCH_H (3)
55} with { variant "FIELDLENGTH(2)" };
56type record ChannelNeeded12 {
57 ChannelNeeded second,
58 ChannelNeeded first
59} with { variant "" };
Harald Welted2e342f2017-07-16 07:34:13 +020060
Harald Welted2e342f2017-07-16 07:34:13 +020061
Alexander Couzens5e6ae792020-09-11 19:49:22 +020062/* TS 48.058 9.3.1 Channel Number IE */
63type enumerated RslChanNr0 {
64 RSL_CHAN_NR_INVALID ('00000'B),
65 RSL_CHAN_NR_Bm_ACCH ('00001'B),
66 RSL_CHAN_NR_BCCH ('10000'B),
67 RSL_CHAN_NR_RACH ('10001'B),
68 RSL_CHAN_NR_PCH_AGCH ('10010'B),
69 RSL_CHAN_NR_OSMO_PDCH ('11000'B),
70 RSL_CHAN_NR_OSMO_CBCH4 ('11001'B),
71 RSL_CHAN_NR_OSMO_CBCH8 ('11010'B)
72} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020073
Alexander Couzens5e6ae792020-09-11 19:49:22 +020074type record RslChanNr2 {
75 BIT4 tag ('0001'B),
76 uint1_t sub_chan
77} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020078
Alexander Couzens5e6ae792020-09-11 19:49:22 +020079type record RslChanNr4 {
80 BIT3 tag ('001'B),
81 uint2_t sub_chan
82} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020083
Alexander Couzens5e6ae792020-09-11 19:49:22 +020084type record RslChanNr8 {
85 BIT2 tag ('01'B),
86 uint3_t sub_chan
87} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020088
Alexander Couzens5e6ae792020-09-11 19:49:22 +020089type 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)"
100 variant "FIELDORDER(msb)"
101};
Harald Welte0c8d5c02017-07-16 18:53:58 +0200102
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200103type record RslChannelNr {
104 RslChanNrU u,
105 uint3_t tn
106} with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200107
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200108template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := {
109 u := { ch0 := cht },
110 tn := tn
111}
112
113template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH);
114template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
115template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
116template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
117template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
118template RslChannelNr t_RslChanNr_CBCH4(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH4);
119template RslChannelNr t_RslChanNr_CBCH8(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH8);
120template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := {
121 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
122 tn := tn
123}
124template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := {
125 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
126 tn := tn
127}
128template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := {
129 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
130 tn := tn
131}
132
133template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := {
134 u := { ch0 := cht },
135 tn := tn
136}
137template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH);
138template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
139template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
140template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
141template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
142template (value) RslChannelNr ts_RslChanNr_CBCH4(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH4);
143template (value) RslChannelNr ts_RslChanNr_CBCH8(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH8);
144template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := {
145 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
146 tn := tn
147}
148template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := {
149 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
150 tn := tn
151}
152template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := {
153 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
154 tn := tn
155}
156
157/* TS 48.058 9.3.2 Link ID */
158type enumerated RslLinkIdC {
159 FACCH_SDCCH (0),
160 SACCH (1),
161 OSMO_PTCCH (2) /* Osmocom (trxcon) specific extension */
162} with { variant "FIELDLENGTH(2)" };
163
164type enumerated RslSapi0Prio {
165 SAPI0_PRIO_NORMAL (0),
166 SAPI0_PRIO_HIGH (1),
167 SAPI0_PRIO_LOW (2)
168} with { variant "FIELDLENGTH(2)" };
169
170type uint3_t GsmSapi;
171
172type record RslLinkId {
173 RslLinkIdC c,
174 boolean na,
175 RslSapi0Prio prio,
176 GsmSapi sapi
177} with { variant "" };
178
179template RslLinkId tr_RslLinkId := {
180 c := ?,
181 na := ?,
182 prio := ?,
183 sapi := ?
184};
185
186template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
187 c := FACCH_SDCCH,
188 na := false,
189 sapi := sapi
190};
191
192template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
193 c := SACCH,
194 na := false,
195 sapi := sapi
196};
197
198template RslLinkId tr_RslLinkID_OSMO_PTCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
199 c := OSMO_PTCCH,
200 na := false,
201 sapi := sapi
202};
203
204template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
205 c := FACCH_SDCCH,
206 na := false,
207 prio := SAPI0_PRIO_NORMAL,
208 sapi := sapi
209};
210
211template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
212 c := SACCH,
213 na := false,
214 prio := SAPI0_PRIO_NORMAL,
215 sapi := sapi
216};
217
218template (value) RslLinkId ts_RslLinkID_OSMO_PTCCH(GsmSapi sapi) := {
219 c := OSMO_PTCCH,
220 na := false,
221 prio := SAPI0_PRIO_NORMAL,
222 sapi := sapi
223};
224
225function f_hex_is_odd_length(hexstring digits) return bitstring {
226 if (lengthof(digits) rem 2 == 1) {
227 return '1'B;
228 } else {
229 return '0'B;
Harald Welte0c8d5c02017-07-16 18:53:58 +0200230 }
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200231}
Harald Welte23b774e2018-02-05 09:14:34 +0100232
Harald Weltef097a8b2018-09-16 18:11:26 +0200233
234/* TS 04.12 Section 3.3.1 Block type */
235type record CBCH_BlockType {
236 BIT1 spare,
237 BIT2 lpd,
238 boolean last_block,
239 uint4_t seq_nr
240};
241template (value) CBCH_BlockType ts_CBCH_BlockType(template (value) uint4_t seq_nr, template (value) boolean last_block) := {
242 spare := '0'B,
243 lpd := '01'B,
244 last_block := last_block,
245 seq_nr := seq_nr
246};
247template CBCH_BlockType tr_CBCH_BlockType(template uint4_t seq_nr := ?, template boolean last_block := ?) := {
248 spare := '0'B,
249 lpd := '01'B,
250 last_block := last_block,
251 seq_nr := seq_nr
252};
253
254/* TS 04.12 Section 3.3 */
255type record CBCH_Block {
256 CBCH_BlockType block_type,
257 OCT22 payload
258};
259template (value) CBCH_Block ts_CBCH_Block(template (value) uint4_t seq_nr, template (value) boolean last_block, template (value) OCT22 payload) := {
260 block_type := ts_CBCH_BlockType(seq_nr, last_block),
261 payload := payload
262};
263template CBCH_Block tr_CBCH_Block(template uint4_t seq_nr := ?, template boolean last_block := ?, template OCT22 payload := ?) := {
264 block_type := tr_CBCH_BlockType(seq_nr, last_block),
265 payload := payload
266};
267
268
269external function enc_CBCH_Block(in CBCH_Block msg) return octetstring
270 with { extension "prototype(convert) encode(RAW)" };
271external function dec_CBCH_Block(in octetstring stream) return CBCH_Block
272 with { extension "prototype(convert) decode(RAW)" };
273
274
Harald Welte5377d2f2018-02-24 01:01:19 +0100275/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
276function dbm2rxlev(integer dbm) return uint6_t {
277 var integer rxlev := dbm + 110;
278 if (rxlev > 63) {
279 rxlev := 63;
280 } else if (rxlev < 0) {
281 rxlev := 0;
282 }
283 return rxlev;
284}
285
286function rxlev2dbm(uint6_t rxlev) return integer {
287 return -110 + rxlev;
288}
289
290/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
291function ber2rxqual(float ber) return uint3_t {
292 if (ber < 0.2) {
293 return 0;
294 } else if (ber < 0.4) {
295 return 1;
296 } else if (ber < 0.8) {
297 return 2;
298 } else if (ber < 1.6) {
299 return 3;
300 } else if (ber < 3.2) {
301 return 4;
302 } else if (ber < 6.4) {
303 return 5;
304 } else if (ber < 12.8) {
305 return 6;
306 } else {
307 return 7;
308 }
309}
310
311/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
312function rxqual2ber(uint3_t rxqual) return float {
313 select (rxqual) {
314 case (0) { return 0.14 }
315 case (1) { return 0.28 }
316 case (2) { return 0.57 }
317 case (3) { return 1.13 }
318 case (4) { return 2.26 }
319 case (5) { return 4.53 }
320 case (6) { return 9.05 }
321 case (7) { return 18.10 }
322 case else { return 1000.0 }
323 }
324}
325
Harald Welte68e495b2018-02-25 00:05:57 +0100326const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
327const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
328const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
329
330/* number of downlink CCCH blocks per second */
331function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
332 if (not combined_ccch) {
333 /* 9 blocks per 51 multiframe */
334 return 9;
335 } else {
336 /* 3 blocks per 51 multiframe */
337 return 3;
338 }
339}
340
341/* this ignores any possible paging combining! */
342function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
343 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
344 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
345 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
346}
347
348/* this ignores any possible imm.ass combining! */
349function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
350 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
351 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
352}
353
Harald Welte262f1222018-02-25 16:33:38 +0100354/* compute TC as per 45.002 6.3.1.3 */
355function f_gsm_compute_tc(integer fn) return integer {
356 return (fn / 51) mod 8;
357}
358
Harald Welte557c9f82020-09-12 21:30:17 +0200359type hexstring GsmBcdString with { variant "HEXORDER(low)" };
360type GsmBcdString BcdMccMnc with { variant "FIELDLENGTH(6)" };
361
362/* 24.008 10.5.1.3 */
363type record LocationAreaIdentification {
364 BcdMccMnc mcc_mnc,
365 uint16_t lac
366} with { variant "" };
367
368/* 24.008 10.5.5.15 */
369type record RoutingAreaIdentification {
370 LocationAreaIdentification lai,
371 uint8_t rac
372} with { variant "" };
373
374external function enc_RoutingAreaIdentification(RoutingAreaIdentification rai) return octetstring
375with { extension "prototype(convert)" extension "encode(RAW)" }
376
377/* TS 24.008 10.5.1.1 */
378type uint16_t CellIdentity;
379
Harald Welte5377d2f2018-02-24 01:01:19 +0100380
Harald Weltea082a692017-07-15 15:58:13 +0200381} with { encode "RAW"; variant "FIELDORDER(msb)" }