blob: 0289b0895b295360de33d0daa544c49d0ba6eb8b [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 Welteb33e7272017-07-16 21:04:12 +0200122 /* TS 48.058 9.3.2 Link ID */
123 type enumerated RslLinkIdC {
124 FACCH_SDCCH (0),
125 SACCH (1)
126 } with { variant "FIELDLENGTH(2)" };
127
128 type enumerated RslSapi0Prio {
129 SAPI0_PRIO_NORMAL (0),
130 SAPI0_PRIO_HIGH (1),
131 SAPI0_PRIO_LOW (2)
132 } with { variant "FIELDLENGTH(2)" };
133
134 type uint3_t GsmSapi;
135
136 type record RslLinkId {
137 RslLinkIdC c,
138 boolean na,
139 RslSapi0Prio prio,
140 GsmSapi sapi
141 } with { variant "" };
142
143 template RslLinkId tr_RslLinkId := {
144 c := ?,
145 na := ?,
146 prio := ?,
147 sapi := ?
148 };
149
150 template RslLinkId tr_RslLinkID_DCCH(template GsmSapi sapi) modifies tr_RslLinkId := {
151 c := FACCH_SDCCH,
152 na := false,
153 sapi := sapi
154 };
155
156 template RslLinkId tr_RslLinkID_SACCH(template GsmSapi sapi) modifies tr_RslLinkId := {
157 c := SACCH,
158 na := false,
159 sapi := sapi
160 };
161
Harald Welte3b2ce022017-12-07 17:47:00 +0100162 template (value) RslLinkId ts_RslLinkID_DCCH(GsmSapi sapi) := {
Harald Welteb33e7272017-07-16 21:04:12 +0200163 c := FACCH_SDCCH,
164 na := false,
165 prio := SAPI0_PRIO_NORMAL,
166 sapi := sapi
167 };
168
Harald Welte3b2ce022017-12-07 17:47:00 +0100169 template (value) RslLinkId ts_RslLinkID_SACCH(GsmSapi sapi) := {
Harald Weltecb5d1fb2017-07-17 21:00:15 +0200170 c := SACCH,
171 na := false,
172 prio := SAPI0_PRIO_NORMAL,
173 sapi := sapi
Harald Welteb33e7272017-07-16 21:04:12 +0200174 };
175
Harald Welte23b774e2018-02-05 09:14:34 +0100176 function f_hex_is_odd_length(hexstring digits) return bitstring {
177 if (lengthof(digits) rem 2 == 1) {
178 return '1'B;
179 } else {
180 return '0'B;
181 }
182 }
183
Harald Welte5377d2f2018-02-24 01:01:19 +0100184/* Convert RF signal level in dBm to RxLev (TS 45.008 Chapter 8.1.4) */
185function dbm2rxlev(integer dbm) return uint6_t {
186 var integer rxlev := dbm + 110;
187 if (rxlev > 63) {
188 rxlev := 63;
189 } else if (rxlev < 0) {
190 rxlev := 0;
191 }
192 return rxlev;
193}
194
195function rxlev2dbm(uint6_t rxlev) return integer {
196 return -110 + rxlev;
197}
198
199/* convert BER to RxQual value (TS 45.008 Chapter 8.2.4 */
200function ber2rxqual(float ber) return uint3_t {
201 if (ber < 0.2) {
202 return 0;
203 } else if (ber < 0.4) {
204 return 1;
205 } else if (ber < 0.8) {
206 return 2;
207 } else if (ber < 1.6) {
208 return 3;
209 } else if (ber < 3.2) {
210 return 4;
211 } else if (ber < 6.4) {
212 return 5;
213 } else if (ber < 12.8) {
214 return 6;
215 } else {
216 return 7;
217 }
218}
219
220/* convert RxQual to BER (TS 45.008 Chapter 8.2.4 */
221function rxqual2ber(uint3_t rxqual) return float {
222 select (rxqual) {
223 case (0) { return 0.14 }
224 case (1) { return 0.28 }
225 case (2) { return 0.57 }
226 case (3) { return 1.13 }
227 case (4) { return 2.26 }
228 case (5) { return 4.53 }
229 case (6) { return 9.05 }
230 case (7) { return 18.10 }
231 case else { return 1000.0 }
232 }
233}
234
Harald Welte68e495b2018-02-25 00:05:57 +0100235const float GSM_FRAME_DURATION := 0.12/26.0; /* 4.615 ms */
236const float GSM51_MFRAME_DURATION := 51.0 * GSM_FRAME_DURATION; /* 235.365 ms */
237const float GSM51_MFRAMES_PER_SEC := 1.0 / GSM51_MFRAME_DURATION; /* 4.248 */
238
239/* number of downlink CCCH blocks per second */
240function f_ccch_blocks_per_mframe(boolean combined_ccch) return integer {
241 if (not combined_ccch) {
242 /* 9 blocks per 51 multiframe */
243 return 9;
244 } else {
245 /* 3 blocks per 51 multiframe */
246 return 3;
247 }
248}
249
250/* this ignores any possible paging combining! */
251function f_pch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
252 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
253 var integer pch_per_mframe := ccch_per_mframe - bs_ag_blks_res;
254 return GSM51_MFRAMES_PER_SEC * int2float(pch_per_mframe);
255}
256
257/* this ignores any possible imm.ass combining! */
258function f_agch_block_rate_est(boolean combined_ccch, integer bs_ag_blks_res) return float {
259 var integer ccch_per_mframe := f_ccch_blocks_per_mframe(combined_ccch);
260 return GSM51_MFRAMES_PER_SEC * int2float(bs_ag_blks_res);
261}
262
Harald Welte262f1222018-02-25 16:33:38 +0100263/* compute TC as per 45.002 6.3.1.3 */
264function f_gsm_compute_tc(integer fn) return integer {
265 return (fn / 51) mod 8;
266}
267
Harald Welte5377d2f2018-02-24 01:01:19 +0100268
Harald Weltea082a692017-07-15 15:58:13 +0200269} with { encode "RAW"; variant "FIELDORDER(msb)" }