blob: 34b65176e3d54a1f793d634fed00ed9ed3e04009 [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
Vadim Yanitskiy1acc7bb2020-11-14 04:24:57 +070032/* ARFCN with explicit band discrimination */
33type record GsmBandArfcn {
34 boolean pcs,
Vadim Yanitskiy05a236a2020-11-14 04:58:30 +070035 boolean uplink,
36 BIT4 spare,
Vadim Yanitskiy1acc7bb2020-11-14 04:24:57 +070037 GsmArfcn arfcn
38} with {
39 variant (arfcn) "BYTEORDER(last)"
40 variant (arfcn) "FIELDLENGTH(10)"
41};
42
43template (value) GsmBandArfcn
44ts_GsmBandArfcn(template (value) GsmArfcn arfcn,
Vadim Yanitskiy05a236a2020-11-14 04:58:30 +070045 template (value) boolean pcs := false,
46 template (value) boolean uplink := false) := {
Vadim Yanitskiy1acc7bb2020-11-14 04:24:57 +070047 pcs := pcs,
Vadim Yanitskiy05a236a2020-11-14 04:58:30 +070048 uplink := uplink,
49 spare := '0000'B,
Vadim Yanitskiy1acc7bb2020-11-14 04:24:57 +070050 arfcn := arfcn
51};
52template GsmBandArfcn
53tr_GsmBandArfcn(template (present) GsmArfcn arfcn,
Vadim Yanitskiy05a236a2020-11-14 04:58:30 +070054 template (present) boolean pcs := ?,
55 template (present) boolean uplink := ?) := {
Vadim Yanitskiy1acc7bb2020-11-14 04:24:57 +070056 pcs := pcs,
Vadim Yanitskiy05a236a2020-11-14 04:58:30 +070057 uplink := uplink,
Vadim Yanitskiy1acc7bb2020-11-14 04:24:57 +070058 spare := ?,
59 arfcn := arfcn
60};
61
Vadim Yanitskiy42d8bd52020-11-15 20:41:02 +070062/* see enum 'gsm_phys_chan_config' in libosmocore */
63type enumerated PchanConfig {
64 GSM_PCHAN_NONE,
65 GSM_PCHAN_CCCH,
66 GSM_PCHAN_CCCH_SDCCH4,
67 GSM_PCHAN_CCCH_SDCCH4_CBCH,
68 GSM_PCHAN_SDCCH8,
69 GSM_PCHAN_SDCCH8_CBCH,
70 GSM_PCHAN_TCHF,
71 GSM_PCHAN_TCHH,
72 GSM_PCHAN_PDCH,
73 /* IPA style dynamic TCH/F+PDCH */
74 GSM_PCHAN_TCHF_PDCH,
75 /* Osmocom style dynamic TCH/H+TCH/F+PDCH */
76 GSM_PCHAN_TCHH_TCHF_PDCH
77};
78
Alexander Couzens5e6ae792020-09-11 19:49:22 +020079type enumerated GprsCodingScheme {
80 CS1, CS2, CS3, CS4
81};
Harald Welte1dcc3712017-08-01 00:05:52 +020082
Alexander Couzens5e6ae792020-09-11 19:49:22 +020083function f_gprs_blocksize(GprsCodingScheme cs) return integer {
84 select (cs) {
85 case (CS1) { return 22 }
86 case (CS2) { return 32 }
87 case (CS3) { return 38 }
88 case (CS3) { return 52 }
89 case else {
90 setverdict(fail, "Invalid GPRS CS ", cs);
91 mtc.stop;
Harald Welte060e27a2018-03-03 20:38:19 +010092 }
93 }
Alexander Couzens5e6ae792020-09-11 19:49:22 +020094}
Harald Welte060e27a2018-03-03 20:38:19 +010095
Pau Espin Pedrolbfd69f62020-10-22 18:06:07 +020096const GprsTlli TLLI_UNUSED := 'FFFFFFFF'O;
97function f_gen_tlli() return GprsTlli {
98 var GprsTlli tlli := f_rnd_octstring(4);
99 if (tlli == TLLI_UNUSED) {
100 tlli := 'EEEEEEEE'O;
101 }
102 return tlli;
103}
104
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200105/* 10.5.2.8 */
106type enumerated ChannelNeeded {
107 CHAN_NEED_ANY (0),
108 CHAN_NEED_SDCCH (1),
109 CHAN_NEED_TCH_F (2),
110 CHAN_NEED_TCH_H (3)
111} with { variant "FIELDLENGTH(2)" };
112type record ChannelNeeded12 {
113 ChannelNeeded second,
114 ChannelNeeded first
115} with { variant "" };
Harald Welted2e342f2017-07-16 07:34:13 +0200116
Harald Welted2e342f2017-07-16 07:34:13 +0200117
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200118/* TS 48.058 9.3.1 Channel Number IE */
119type enumerated RslChanNr0 {
120 RSL_CHAN_NR_INVALID ('00000'B),
121 RSL_CHAN_NR_Bm_ACCH ('00001'B),
122 RSL_CHAN_NR_BCCH ('10000'B),
123 RSL_CHAN_NR_RACH ('10001'B),
124 RSL_CHAN_NR_PCH_AGCH ('10010'B),
125 RSL_CHAN_NR_OSMO_PDCH ('11000'B),
126 RSL_CHAN_NR_OSMO_CBCH4 ('11001'B),
127 RSL_CHAN_NR_OSMO_CBCH8 ('11010'B)
128} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200129
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200130type record RslChanNr2 {
131 BIT4 tag ('0001'B),
132 uint1_t sub_chan
133} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200134
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200135type record RslChanNr4 {
136 BIT3 tag ('001'B),
137 uint2_t sub_chan
138} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200139
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200140type record RslChanNr8 {
141 BIT2 tag ('01'B),
142 uint3_t sub_chan
143} with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200144
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200145type union RslChanNrU {
146 RslChanNr0 ch0,
147 RslChanNr2 lm,
148 RslChanNr4 sdcch4,
149 RslChanNr8 sdcch8
150} with {
151 variant "TAG(lm, tag = '0001'B;
152 sdcch4, tag = '001'B;
153 sdcch8, tag = '01'B;
154 ch0, OTHERWISE)"
155 variant "FIELDLENGTH(5)"
156 variant "FIELDORDER(msb)"
157};
Harald Welte0c8d5c02017-07-16 18:53:58 +0200158
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200159type record RslChannelNr {
160 RslChanNrU u,
161 uint3_t tn
162} with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +0200163
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200164template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := {
165 u := { ch0 := cht },
166 tn := tn
167}
168
169template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH);
170template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
171template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
172template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
173template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
174template RslChannelNr t_RslChanNr_CBCH4(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH4);
175template RslChannelNr t_RslChanNr_CBCH8(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH8);
176template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := {
177 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
178 tn := tn
179}
180template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := {
181 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
182 tn := tn
183}
184template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := {
185 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
186 tn := tn
187}
188
189template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := {
190 u := { ch0 := cht },
191 tn := tn
192}
193template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH);
194template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
195template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
196template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
197template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
198template (value) RslChannelNr ts_RslChanNr_CBCH4(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH4);
199template (value) RslChannelNr ts_RslChanNr_CBCH8(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_CBCH8);
200template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := {
201 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
202 tn := tn
203}
204template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := {
205 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
206 tn := tn
207}
208template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := {
209 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
210 tn := tn
211}
212
213/* TS 48.058 9.3.2 Link ID */
214type enumerated RslLinkIdC {
215 FACCH_SDCCH (0),
216 SACCH (1),
217 OSMO_PTCCH (2) /* Osmocom (trxcon) specific extension */
218} with { variant "FIELDLENGTH(2)" };
219
220type enumerated RslSapi0Prio {
221 SAPI0_PRIO_NORMAL (0),
222 SAPI0_PRIO_HIGH (1),
223 SAPI0_PRIO_LOW (2)
224} with { variant "FIELDLENGTH(2)" };
225
226type uint3_t GsmSapi;
227
228type record RslLinkId {
229 RslLinkIdC c,
230 boolean na,
231 RslSapi0Prio prio,
232 GsmSapi sapi
233} with { variant "" };
234
235template RslLinkId tr_RslLinkId := {
236 c := ?,
237 na := ?,
238 prio := ?,
239 sapi := ?
240};
241
242template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
243 c := FACCH_SDCCH,
244 na := false,
245 sapi := sapi
246};
247
248template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
249 c := SACCH,
250 na := false,
251 sapi := sapi
252};
253
254template RslLinkId tr_RslLinkID_OSMO_PTCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
255 c := OSMO_PTCCH,
256 na := false,
257 sapi := sapi
258};
259
260template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
261 c := FACCH_SDCCH,
262 na := false,
263 prio := SAPI0_PRIO_NORMAL,
264 sapi := sapi
265};
266
267template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
268 c := SACCH,
269 na := false,
270 prio := SAPI0_PRIO_NORMAL,
271 sapi := sapi
272};
273
274template (value) RslLinkId ts_RslLinkID_OSMO_PTCCH(GsmSapi sapi) := {
275 c := OSMO_PTCCH,
276 na := false,
277 prio := SAPI0_PRIO_NORMAL,
278 sapi := sapi
279};
280
281function f_hex_is_odd_length(hexstring digits) return bitstring {
282 if (lengthof(digits) rem 2 == 1) {
283 return '1'B;
284 } else {
285 return '0'B;
Harald Welte0c8d5c02017-07-16 18:53:58 +0200286 }
Alexander Couzens5e6ae792020-09-11 19:49:22 +0200287}
Harald Welte23b774e2018-02-05 09:14:34 +0100288
Harald Weltef097a8b2018-09-16 18:11:26 +0200289
290/* TS 04.12 Section 3.3.1 Block type */
291type record CBCH_BlockType {
292 BIT1 spare,
293 BIT2 lpd,
294 boolean last_block,
295 uint4_t seq_nr
296};
297template (value) CBCH_BlockType ts_CBCH_BlockType(template (value) uint4_t seq_nr, template (value) boolean last_block) := {
298 spare := '0'B,
299 lpd := '01'B,
300 last_block := last_block,
301 seq_nr := seq_nr
302};
303template CBCH_BlockType tr_CBCH_BlockType(template uint4_t seq_nr := ?, template boolean last_block := ?) := {
304 spare := '0'B,
305 lpd := '01'B,
306 last_block := last_block,
307 seq_nr := seq_nr
308};
309
310/* TS 04.12 Section 3.3 */
311type record CBCH_Block {
312 CBCH_BlockType block_type,
313 OCT22 payload
314};
315template (value) CBCH_Block ts_CBCH_Block(template (value) uint4_t seq_nr, template (value) boolean last_block, template (value) OCT22 payload) := {
316 block_type := ts_CBCH_BlockType(seq_nr, last_block),
317 payload := payload
318};
319template CBCH_Block tr_CBCH_Block(template uint4_t seq_nr := ?, template boolean last_block := ?, template OCT22 payload := ?) := {
320 block_type := tr_CBCH_BlockType(seq_nr, last_block),
321 payload := payload
322};
323
324
325external function enc_CBCH_Block(in CBCH_Block msg) return octetstring
326 with { extension "prototype(convert) encode(RAW)" };
327external function dec_CBCH_Block(in octetstring stream) return CBCH_Block
328 with { extension "prototype(convert) decode(RAW)" };
329
330
Harald Welte5377d2f2018-02-24 01:01:19 +0100331/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
332function dbm2rxlev(integer dbm) return uint6_t {
333 var integer rxlev := dbm + 110;
334 if (rxlev > 63) {
335 rxlev := 63;
336 } else if (rxlev < 0) {
337 rxlev := 0;
338 }
339 return rxlev;
340}
341
342function rxlev2dbm(uint6_t rxlev) return integer {
343 return -110 + rxlev;
344}
345
346/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
347function ber2rxqual(float ber) return uint3_t {
348 if (ber < 0.2) {
349 return 0;
350 } else if (ber < 0.4) {
351 return 1;
352 } else if (ber < 0.8) {
353 return 2;
354 } else if (ber < 1.6) {
355 return 3;
356 } else if (ber < 3.2) {
357 return 4;
358 } else if (ber < 6.4) {
359 return 5;
360 } else if (ber < 12.8) {
361 return 6;
362 } else {
363 return 7;
364 }
365}
366
367/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
368function rxqual2ber(uint3_t rxqual) return float {
369 select (rxqual) {
370 case (0) { return 0.14 }
371 case (1) { return 0.28 }
372 case (2) { return 0.57 }
373 case (3) { return 1.13 }
374 case (4) { return 2.26 }
375 case (5) { return 4.53 }
376 case (6) { return 9.05 }
377 case (7) { return 18.10 }
378 case else { return 1000.0 }
379 }
380}
381
Harald Welte68e495b2018-02-25 00:05:57 +0100382const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
383const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
384const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
385
386/* number of downlink CCCH blocks per second */
387function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
388 if (not combined_ccch) {
389 /* 9 blocks per 51 multiframe */
390 return 9;
391 } else {
392 /* 3 blocks per 51 multiframe */
393 return 3;
394 }
395}
396
397/* this ignores any possible paging combining! */
398function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
399 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
400 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
401 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
402}
403
404/* this ignores any possible imm.ass combining! */
405function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
406 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
407 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
408}
409
Harald Welte262f1222018-02-25 16:33:38 +0100410/* compute TC as per 45.002 6.3.1.3 */
411function f_gsm_compute_tc(integer fn) return integer {
412 return (fn / 51) mod 8;
413}
414
Harald Welte557c9f82020-09-12 21:30:17 +0200415type hexstring GsmBcdString with { variant "HEXORDER(low)" };
416type GsmBcdString BcdMccMnc with { variant "FIELDLENGTH(6)" };
417
418/* 24.008 10.5.1.3 */
419type record LocationAreaIdentification {
420 BcdMccMnc mcc_mnc,
421 uint16_t lac
422} with { variant "" };
423
424/* 24.008 10.5.5.15 */
425type record RoutingAreaIdentification {
426 LocationAreaIdentification lai,
427 uint8_t rac
428} with { variant "" };
429
430external function enc_RoutingAreaIdentification(RoutingAreaIdentification rai) return octetstring
431with { extension "prototype(convert)" extension "encode(RAW)" }
432
433/* TS 24.008 10.5.1.1 */
434type uint16_t CellIdentity;
435
Harald Welte5377d2f2018-02-24 01:01:19 +0100436
Harald Weltea082a692017-07-15 15:58:13 +0200437} with { encode "RAW"; variant "FIELDORDER(msb)" }