blob: 2000b4a37323bb3e2f3d5cfce413efcbfbefa5d4 [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);
37 return -1;
38 }
39 }
40 }
41
Harald Welted2e342f2017-07-16 07:34:13 +020042 /* 10.5.2.8 */
43 type enumerated ChannelNeeded {
44 CHAN_NEED_ANY (0),
45 CHAN_NEED_SDCCH (1),
46 CHAN_NEED_TCH_F (2),
47 CHAN_NEED_TCH_H (3)
48 } with { variant "FIELDLENGTH(2)" };
49 type record ChannelNeeded12 {
50 ChannelNeeded second,
51 ChannelNeeded first
52 } with { variant "" };
53
Harald Welted2e342f2017-07-16 07:34:13 +020054
Harald Welte0c8d5c02017-07-16 18:53:58 +020055 /* TS 48.058 9.3.1 Channel Number IE */
56 type enumerated RslChanNr0 {
Harald Welte6f3c2232017-07-30 17:17:12 +020057 RSL_CHAN_NR_INVALID ('00000'B),
58 RSL_CHAN_NR_Bm_ACCH ('00001'B),
59 RSL_CHAN_NR_BCCH ('10000'B),
60 RSL_CHAN_NR_RACH ('10001'B),
61 RSL_CHAN_NR_PCH_AGCH ('10010'B),
62 RSL_CHAN_NR_OSMO_PDCH ('11000'B)
Harald Weltec84d8472017-07-30 00:49:56 +020063 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020064
65 type record RslChanNr2 {
66 BIT4 tag ('0001'B),
67 uint1_t sub_chan
Harald Weltec84d8472017-07-30 00:49:56 +020068 } with { variant "FIELDLENGTH(5)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020069
70 type record RslChanNr4 {
71 BIT3 tag ('001'B),
72 uint2_t sub_chan
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 RslChanNr8 {
76 BIT2 tag ('01'B),
77 uint3_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 union RslChanNrU {
81 RslChanNr0 ch0,
82 RslChanNr2 lm,
83 RslChanNr4 sdcch4,
84 RslChanNr8 sdcch8
85 } with {
86 variant "TAG(lm, tag = '0001'B;
87 sdcch4, tag = '001'B;
88 sdcch8, tag = '01'B;
89 ch0, OTHERWISE)"
90 variant "FIELDLENGTH(5)"
Harald Weltec84d8472017-07-30 00:49:56 +020091 variant "FIELDORDER(msb)"
Harald Welte0c8d5c02017-07-16 18:53:58 +020092 };
93
94 type record RslChannelNr {
95 RslChanNrU u,
96 uint3_t tn
Harald Weltec84d8472017-07-30 00:49:56 +020097 } with { variant "FIELDLENGTH(8)" variant "FIELDORDER(msb)" };
Harald Welte0c8d5c02017-07-16 18:53:58 +020098
99 template RslChannelNr t_RslChanNr0(template uint3_t tn, template RslChanNr0 cht) := {
100 u := { ch0 := cht },
101 tn := tn
102 }
103
104 template RslChannelNr t_RslChanNr_RACH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_RACH);
105 template RslChannelNr t_RslChanNr_BCCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
106 template RslChannelNr t_RslChanNr_PCH_AGCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
107 template RslChannelNr t_RslChanNr_Bm(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
Harald Welte1dcc3712017-08-01 00:05:52 +0200108 template RslChannelNr t_RslChanNr_PDCH(template uint3_t tn) := t_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
Harald Welte2c2e8c42018-01-29 21:59:39 +0100109 template RslChannelNr t_RslChanNr_Lm(template uint3_t tn, template uint1_t sub_slot) := {
Harald Welte0c8d5c02017-07-16 18:53:58 +0200110 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
111 tn := tn
112 }
113 template RslChannelNr t_RslChanNr_SDCCH4(template uint3_t tn, template uint2_t sub_slot) := {
114 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
115 tn := tn
116 }
117 template RslChannelNr t_RslChanNr_SDCCH8(template uint3_t tn, template uint3_t sub_slot) := {
118 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
119 tn := tn
120 }
Harald Welted2e342f2017-07-16 07:34:13 +0200121
Harald Welte629cc6b2018-03-11 17:19:05 +0100122 template (value) RslChannelNr ts_RslChanNr0(uint3_t tn, RslChanNr0 cht) := {
123 u := { ch0 := cht },
124 tn := tn
125 }
126 template (value) RslChannelNr ts_RslChanNr_RACH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_RACH);
127 template (value) RslChannelNr ts_RslChanNr_BCCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_BCCH);
128 template (value) RslChannelNr ts_RslChanNr_PCH_AGCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_PCH_AGCH);
129 template (value) RslChannelNr ts_RslChanNr_Bm(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_Bm_ACCH);
130 template (value) RslChannelNr ts_RslChanNr_PDCH(uint3_t tn) := ts_RslChanNr0(tn, RSL_CHAN_NR_OSMO_PDCH);
131 template (value) RslChannelNr ts_RslChanNr_Lm(uint3_t tn, uint1_t sub_slot) := {
132 u := { lm := { tag := '0001'B, sub_chan := sub_slot } },
133 tn := tn
134 }
135 template (value) RslChannelNr ts_RslChanNr_SDCCH4(uint3_t tn, uint2_t sub_slot) := {
136 u := { sdcch4 := { tag := '001'B, sub_chan := sub_slot } },
137 tn := tn
138 }
139 template (value) RslChannelNr ts_RslChanNr_SDCCH8(uint3_t tn, uint3_t sub_slot) := {
140 u := { sdcch8 := { tag := '01'B, sub_chan := sub_slot } },
141 tn := tn
142 }
143
Harald Welteb33e7272017-07-16 21:04:12 +0200144 /* TS 48.058 9.3.2 Link ID */
145 type enumerated RslLinkIdC {
146 FACCH_SDCCH (0),
147 SACCH (1)
148 } with { variant "FIELDLENGTH(2)" };
149
150 type enumerated RslSapi0Prio {
151 SAPI0_PRIO_NORMAL (0),
152 SAPI0_PRIO_HIGH (1),
153 SAPI0_PRIO_LOW (2)
154 } with { variant "FIELDLENGTH(2)" };
155
156 type uint3_t GsmSapi;
157
158 type record RslLinkId {
159 RslLinkIdC c,
160 boolean na,
161 RslSapi0Prio prio,
162 GsmSapi sapi
163 } with { variant "" };
164
165 template RslLinkId tr_RslLinkId := {
166 c := ?,
167 na := ?,
168 prio := ?,
169 sapi := ?
170 };
171
172 template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
173 c := FACCH_SDCCH,
174 na := false,
175 sapi := sapi
176 };
177
178 template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
179 c := SACCH,
180 na := false,
181 sapi := sapi
182 };
183
Harald Welte3b2ce022017-12-07 17:47:00 +0100184 template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
Harald Welteb33e7272017-07-16 21:04:12 +0200185 c := FACCH_SDCCH,
186 na := false,
187 prio := SAPI0_PRIO_NORMAL,
188 sapi := sapi
189 };
190
Harald Welte3b2ce022017-12-07 17:47:00 +0100191 template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
Harald Weltecb5d1fb2017-07-17 21:00:15 +0200192 c := SACCH,
193 na := false,
194 prio := SAPI0_PRIO_NORMAL,
195 sapi := sapi
Harald Welteb33e7272017-07-16 21:04:12 +0200196 };
197
Harald Welte23b774e2018-02-05 09:14:34 +0100198 function f_hex_is_odd_length(hexstring digits) return bitstring {
199 if (lengthof(digits) rem 2 == 1) {
200 return '1'B;
201 } else {
202 return '0'B;
203 }
204 }
205
Harald Welte5377d2f2018-02-24 01:01:19 +0100206/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
207function dbm2rxlev(integer dbm) return uint6_t {
208 var integer rxlev := dbm + 110;
209 if (rxlev > 63) {
210 rxlev := 63;
211 } else if (rxlev < 0) {
212 rxlev := 0;
213 }
214 return rxlev;
215}
216
217function rxlev2dbm(uint6_t rxlev) return integer {
218 return -110 + rxlev;
219}
220
221/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
222function ber2rxqual(float ber) return uint3_t {
223 if (ber < 0.2) {
224 return 0;
225 } else if (ber < 0.4) {
226 return 1;
227 } else if (ber < 0.8) {
228 return 2;
229 } else if (ber < 1.6) {
230 return 3;
231 } else if (ber < 3.2) {
232 return 4;
233 } else if (ber < 6.4) {
234 return 5;
235 } else if (ber < 12.8) {
236 return 6;
237 } else {
238 return 7;
239 }
240}
241
242/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
243function rxqual2ber(uint3_t rxqual) return float {
244 select (rxqual) {
245 case (0) { return 0.14 }
246 case (1) { return 0.28 }
247 case (2) { return 0.57 }
248 case (3) { return 1.13 }
249 case (4) { return 2.26 }
250 case (5) { return 4.53 }
251 case (6) { return 9.05 }
252 case (7) { return 18.10 }
253 case else { return 1000.0 }
254 }
255}
256
Harald Welte68e495b2018-02-25 00:05:57 +0100257const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
258const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
259const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
260
261/* number of downlink CCCH blocks per second */
262function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
263 if (not combined_ccch) {
264 /* 9 blocks per 51 multiframe */
265 return 9;
266 } else {
267 /* 3 blocks per 51 multiframe */
268 return 3;
269 }
270}
271
272/* this ignores any possible paging combining! */
273function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
274 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
275 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
276 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
277}
278
279/* this ignores any possible imm.ass combining! */
280function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
281 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
282 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
283}
284
Harald Welte262f1222018-02-25 16:33:38 +0100285/* compute TC as per 45.002 6.3.1.3 */
286function f_gsm_compute_tc(integer fn) return integer {
287 return (fn / 51) mod 8;
288}
289
Harald Welte5377d2f2018-02-24 01:01:19 +0100290
Harald Weltea082a692017-07-15 15:58:13 +0200291} with { encode "RAW"; variant "FIELDORDER(msb)" }