blob: 59ccb763d3309994215adf75488c169eb1002551 [file] [log] [blame]
Harald Weltea082a692017-07-15 15:58:13 +02001/* Encoding/Decoding routines for GSM System Information messages
2 * according to 3GPP TS 44.018 Version 12.3.0 Release 12 */
3
4/* (C) 2017 by Harald Welte <laforge@gnumonks.org> */
5
6module GSM_Types {
7
8 import from General_Types all;
9 import from Osmocom_Types all;
10
Harald Weltebdc5dbd2017-07-16 00:00:43 +020011 type integer GsmArfcn (0..1023);
12 type integer UmtsArfcn (0..16383);
13 type integer UmtsScramblingCode (0..511);
Harald Welted2e342f2017-07-16 07:34:13 +020014 const integer GsmMaxFrameNumber := 26*51*2048;
15 type integer GsmFrameNumber (0..GsmMaxFrameNumber);
16 type integer GsmRxLev (0..63);
17 type integer GsmTsc (0..7) with { variant "FIELDLENGTH(8)" };
18 type uint32_t GsmTmsi;
Harald Welteacc93ab2018-03-02 21:39:09 +010019 type OCT4 GprsTlli;
Harald Welte72cecfa2017-12-11 19:50:14 +010020 type hexstring GsmMcc length(3);
21 type hexstring GsmMnc length(2 .. 3);
22 type uint16_t GsmLac;
23 type uint16_t GsmCellId;
Harald Weltebdc5dbd2017-07-16 00:00:43 +020024
Harald Welte1dcc3712017-08-01 00:05:52 +020025 type enumerated GprsCodingScheme {
26 CS1, CS2, CS3, CS4
27 };
28
Harald Welte060e27a2018-03-03 20:38:19 +010029 function f_gprs_blocksize(GprsCodingScheme cs) return integer {
30 select (cs) {
31 case (CS1) { return 22 }
32 case (CS2) { return 32 }
33 case (CS3) { return 38 }
34 case (CS3) { return 52 }
35 case else {
36 setverdict(fail, "Invalid GPRS CS ", cs);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020037 mtc.stop;
Harald Welte060e27a2018-03-03 20:38:19 +010038 return -1;
39 }
40 }
41 }
42
Harald Welted2e342f2017-07-16 07:34:13 +020043 /* 10.5.2.8 */
44 type enumerated ChannelNeeded {
45 CHAN_NEED_ANY (0),
46 CHAN_NEED_SDCCH (1),
47 CHAN_NEED_TCH_F (2),
48 CHAN_NEED_TCH_H (3)
49 } with { variant "FIELDLENGTH(2)" };
50 type record ChannelNeeded12 {
51 ChannelNeeded second,
52 ChannelNeeded first
53 } with { variant "" };
54
Harald Welted2e342f2017-07-16 07:34:13 +020055
Harald Welte0c8d5c02017-07-16 18:53:58 +020056 /* TS 48.058 9.3.1 Channel Number IE */
57 type enumerated RslChanNr0 {
Harald Welte6f3c2232017-07-30 17:17:12 +020058 RSL_CHAN_NR_INVALID ('00000'B),
59 RSL_CHAN_NR_Bm_ACCH ('00001'B),
60 RSL_CHAN_NR_BCCH ('10000'B),
61 RSL_CHAN_NR_RACH ('10001'B),
62 RSL_CHAN_NR_PCH_AGCH ('10010'B),
63 RSL_CHAN_NR_OSMO_PDCH ('11000'B)
Harald Weltec84d8472017-07-30 00:49:56 +020064 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020065
66 type record RslChanNr2 {
67 BIT4 tag ('0001'B),
68 uint1_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020069 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020070
71 type record RslChanNr4 {
72 BIT3 tag ('001'B),
73 uint2_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020074 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020075
76 type record RslChanNr8 {
77 BIT2 tag ('01'B),
78 uint3_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020079 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020080
81 type union RslChanNrU {
82 RslChanNr0 ch0,
83 RslChanNr2 lm,
84 RslChanNr4 sdcch4,
85 RslChanNr8 sdcch8
86 } with {
87 variant "TAG(lm, tag = '0001'B;
88 sdcch4, tag = '001'B;
89 sdcch8, tag = '01'B;
90 ch0, OTHERWISE)"
91 variant "FIELDLENGTH(5)"
Harald Weltec84d8472017-07-30 00:49:56 +020092 variant "FIELDORDER(msb)"
Harald Welte0c8d5c02017-07-16 18:53:58 +020093 };
94
95 type record RslChannelNr {
96 RslChanNrU u,
97 uint3_t tn
Harald Weltec84d8472017-07-30 00:49:56 +020098 } with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020099
100 template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := {
101 u := { ch0 := cht },
102 tn := tn
103 }
104
105 template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH);
106 template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
107 template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
108 template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
Harald Welte1dcc3712017-08-01 00:05:52 +0200109 template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
Harald Welte2c2e8c42018-01-29 21:59:39 +0100110 template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := {
Harald Welte0c8d5c02017-07-16 18:53:58 +0200111 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
112 tn := tn
113 }
114 template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := {
115 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
116 tn := tn
117 }
118 template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := {
119 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
120 tn := tn
121 }
Harald Welted2e342f2017-07-16 07:34:13 +0200122
Harald Welte629cc6b2018-03-11 17:19:05 +0100123 template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := {
124 u := { ch0 := cht },
125 tn := tn
126 }
127 template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH);
128 template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
129 template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
130 template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
131 template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
132 template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := {
133 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
134 tn := tn
135 }
136 template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := {
137 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
138 tn := tn
139 }
140 template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := {
141 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
142 tn := tn
143 }
144
Harald Welteb33e7272017-07-16 21:04:12 +0200145 /* TS 48.058 9.3.2 Link ID */
146 type enumerated RslLinkIdC {
147 FACCH_SDCCH (0),
148 SACCH (1)
149 } with { variant "FIELDLENGTH(2)" };
150
151 type enumerated RslSapi0Prio {
152 SAPI0_PRIO_NORMAL (0),
153 SAPI0_PRIO_HIGH (1),
154 SAPI0_PRIO_LOW (2)
155 } with { variant "FIELDLENGTH(2)" };
156
157 type uint3_t GsmSapi;
158
159 type record RslLinkId {
160 RslLinkIdC c,
161 boolean na,
162 RslSapi0Prio prio,
163 GsmSapi sapi
164 } with { variant "" };
165
166 template RslLinkId tr_RslLinkId := {
167 c := ?,
168 na := ?,
169 prio := ?,
170 sapi := ?
171 };
172
173 template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
174 c := FACCH_SDCCH,
175 na := false,
176 sapi := sapi
177 };
178
179 template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
180 c := SACCH,
181 na := false,
182 sapi := sapi
183 };
184
Harald Welte3b2ce022017-12-07 17:47:00 +0100185 template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
Harald Welteb33e7272017-07-16 21:04:12 +0200186 c := FACCH_SDCCH,
187 na := false,
188 prio := SAPI0_PRIO_NORMAL,
189 sapi := sapi
190 };
191
Harald Welte3b2ce022017-12-07 17:47:00 +0100192 template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
Harald Weltecb5d1fb2017-07-17 21:00:15 +0200193 c := SACCH,
194 na := false,
195 prio := SAPI0_PRIO_NORMAL,
196 sapi := sapi
Harald Welteb33e7272017-07-16 21:04:12 +0200197 };
198
Harald Welte23b774e2018-02-05 09:14:34 +0100199 function f_hex_is_odd_length(hexstring digits) return bitstring {
200 if (lengthof(digits) rem 2 == 1) {
201 return '1'B;
202 } else {
203 return '0'B;
204 }
205 }
206
Harald Welte5377d2f2018-02-24 01:01:19 +0100207/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
208function dbm2rxlev(integer dbm) return uint6_t {
209 var integer rxlev := dbm + 110;
210 if (rxlev > 63) {
211 rxlev := 63;
212 } else if (rxlev < 0) {
213 rxlev := 0;
214 }
215 return rxlev;
216}
217
218function rxlev2dbm(uint6_t rxlev) return integer {
219 return -110 + rxlev;
220}
221
222/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
223function ber2rxqual(float ber) return uint3_t {
224 if (ber < 0.2) {
225 return 0;
226 } else if (ber < 0.4) {
227 return 1;
228 } else if (ber < 0.8) {
229 return 2;
230 } else if (ber < 1.6) {
231 return 3;
232 } else if (ber < 3.2) {
233 return 4;
234 } else if (ber < 6.4) {
235 return 5;
236 } else if (ber < 12.8) {
237 return 6;
238 } else {
239 return 7;
240 }
241}
242
243/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
244function rxqual2ber(uint3_t rxqual) return float {
245 select (rxqual) {
246 case (0) { return 0.14 }
247 case (1) { return 0.28 }
248 case (2) { return 0.57 }
249 case (3) { return 1.13 }
250 case (4) { return 2.26 }
251 case (5) { return 4.53 }
252 case (6) { return 9.05 }
253 case (7) { return 18.10 }
254 case else { return 1000.0 }
255 }
256}
257
Harald Welte68e495b2018-02-25 00:05:57 +0100258const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
259const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
260const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
261
262/* number of downlink CCCH blocks per second */
263function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
264 if (not combined_ccch) {
265 /* 9 blocks per 51 multiframe */
266 return 9;
267 } else {
268 /* 3 blocks per 51 multiframe */
269 return 3;
270 }
271}
272
273/* this ignores any possible paging combining! */
274function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
275 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
276 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
277 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
278}
279
280/* this ignores any possible imm.ass combining! */
281function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
282 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
283 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
284}
285
Harald Welte262f1222018-02-25 16:33:38 +0100286/* compute TC as per 45.002 6.3.1.3 */
287function f_gsm_compute_tc(integer fn) return integer {
288 return (fn / 51) mod 8;
289}
290
Harald Welte5377d2f2018-02-24 01:01:19 +0100291
Harald Weltea082a692017-07-15 15:58:13 +0200292} with { encode "RAW"; variant "FIELDORDER(msb)" }