blob: 950e570066f068c719d7fb7ef61ab1e2169edd67 [file] [log] [blame]
Harald Weltea43f7892009-12-01 18:04:30 +05301/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface,
2 * rest octet handling according to
3 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
4
5/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Weltea43f7892009-12-01 18:04:30 +053012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Weltea43f7892009-12-01 18:04:30 +053018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Weltea43f7892009-12-01 18:04:30 +053021 *
22 */
23
24#include <string.h>
25#include <stdlib.h>
26#include <errno.h>
Max59a1bf32016-04-15 16:04:46 +020027#include <stdbool.h>
Harald Weltea43f7892009-12-01 18:04:30 +053028
Max59a1bf32016-04-15 16:04:46 +020029#include <openbsc/debug.h>
Harald Weltea43f7892009-12-01 18:04:30 +053030#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010031#include <osmocom/core/bitvec.h>
Maxe01f5052016-04-23 18:00:18 +020032#include <osmocom/gsm/bitvec_gsm.h>
Harald Weltea43f7892009-12-01 18:04:30 +053033#include <openbsc/rest_octets.h>
Max59a1bf32016-04-15 16:04:46 +020034#include <openbsc/arfcn_range_encode.h>
Max26679e02016-04-20 15:57:13 +020035#include <openbsc/system_information.h>
Harald Weltea43f7892009-12-01 18:04:30 +053036
37/* generate SI1 rest octets */
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +010038int rest_octets_si1(uint8_t *data, uint8_t *nch_pos, int is1800_net)
Harald Weltea43f7892009-12-01 18:04:30 +053039{
40 struct bitvec bv;
41
42 memset(&bv, 0, sizeof(bv));
43 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +010044 bv.data_len = 1;
Harald Weltea43f7892009-12-01 18:04:30 +053045
46 if (nch_pos) {
47 bitvec_set_bit(&bv, H);
48 bitvec_set_uint(&bv, *nch_pos, 5);
49 } else
50 bitvec_set_bit(&bv, L);
51
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +010052 if (is1800_net)
53 bitvec_set_bit(&bv, L);
54 else
55 bitvec_set_bit(&bv, H);
56
57 bitvec_spare_padding(&bv, 6);
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +010058 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +053059}
60
Maxf39d03a2017-05-12 17:00:30 +020061/* Append Repeated E-UTRAN Neighbour Cell to bitvec: see 3GPP TS 44.018 Table 10.5.2.33b.1 */
62static inline void append_eutran_neib_cell(struct bitvec *bv, struct gsm_bts *bts, uint8_t budget)
Max59a1bf32016-04-15 16:04:46 +020063{
Maxf39d03a2017-05-12 17:00:30 +020064 const struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
65 unsigned i, skip = 0;
66 size_t offset = bts->e_offset;
67 uint8_t rem = budget - 6, earfcn_budget; /* account for mandatory stop bit and THRESH_E-UTRAN_high */
68 /* first we have to properly adjust budget requirements */
69 if (e->prio_valid) /* E-UTRAN_PRIORITY: 3GPP TS 45.008*/
70 rem -= 4;
71 else
72 rem--;
73
74 if (e->thresh_lo_valid) /* THRESH_E-UTRAN_low: */
75 rem -= 6;
76 else
77 rem--;
78
79 if (e->qrxlm_valid) /* E-UTRAN_QRXLEVMIN: */
80 rem -= 6;
81 else
82 rem--;
83
84 /* now we can proceed with actually adding EARFCNs within adjusted budget limit */
Max59a1bf32016-04-15 16:04:46 +020085 for (i = 0; i < e->length; i++) {
86 if (e->arfcn[i] != OSMO_EARFCN_INVALID) {
Maxf39d03a2017-05-12 17:00:30 +020087 if (skip < offset) {
88 skip++; /* ignore EARFCNs added on previous calls */
89 } else {
90 earfcn_budget = 17; /* computer budget per-EARFCN */
91 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
92 earfcn_budget++;
93 else
94 earfcn_budget += 4;
Max59a1bf32016-04-15 16:04:46 +020095
Maxf39d03a2017-05-12 17:00:30 +020096 if (rem - earfcn_budget < 0) {
97 break;
98 } else {
99 bts->e_offset++;
100 bitvec_set_bit(bv, 1); /* EARFCN: */
101 bitvec_set_uint(bv, e->arfcn[i], 16);
102
103 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
104 bitvec_set_bit(bv, 0);
105 else { /* Measurement Bandwidth: 9.1.54 */
106 bitvec_set_bit(bv, 1);
107 bitvec_set_uint(bv, e->meas_bw[i], 3);
108 }
109 }
Max59a1bf32016-04-15 16:04:46 +0200110 }
111 }
112 }
113
114 /* stop bit - end of EARFCN + Measurement Bandwidth sequence */
115 bitvec_set_bit(bv, 0);
116
Maxf39d03a2017-05-12 17:00:30 +0200117 /* Note: we don't support different EARFCN arrays each with different priority, threshold etc. */
118
Max59a1bf32016-04-15 16:04:46 +0200119 if (e->prio_valid) {
120 /* E-UTRAN_PRIORITY: 3GPP TS 45.008*/
121 bitvec_set_bit(bv, 1);
122 bitvec_set_uint(bv, e->prio, 3);
123 } else
124 bitvec_set_bit(bv, 0);
125
126 /* THRESH_E-UTRAN_high */
127 bitvec_set_uint(bv, e->thresh_hi, 5);
128
129 if (e->thresh_lo_valid) {
130 /* THRESH_E-UTRAN_low: */
131 bitvec_set_bit(bv, 1);
132 bitvec_set_uint(bv, e->thresh_lo, 5);
133 } else
134 bitvec_set_bit(bv, 0);
135
136 if (e->qrxlm_valid) {
137 /* E-UTRAN_QRXLEVMIN: */
138 bitvec_set_bit(bv, 1);
139 bitvec_set_uint(bv, e->qrxlm, 5);
140 } else
141 bitvec_set_bit(bv, 0);
142}
143
Maxf39d03a2017-05-12 17:00:30 +0200144static inline void append_earfcn(struct bitvec *bv, struct gsm_bts *bts, uint8_t budget)
Max59a1bf32016-04-15 16:04:46 +0200145{
146 /* Additions in Rel-5: */
147 bitvec_set_bit(bv, H);
148 /* No 3G Additional Measurement Param. Descr. */
149 bitvec_set_bit(bv, 0);
150 /* No 3G ADDITIONAL MEASUREMENT Param. Descr. 2 */
151 bitvec_set_bit(bv, 0);
152 /* Additions in Rel-6: */
153 bitvec_set_bit(bv, H);
154 /* 3G_CCN_ACTIVE */
155 bitvec_set_bit(bv, 0);
156 /* Additions in Rel-7: */
157 bitvec_set_bit(bv, H);
158 /* No 700_REPORTING_OFFSET */
159 bitvec_set_bit(bv, 0);
160 /* No 810_REPORTING_OFFSET */
161 bitvec_set_bit(bv, 0);
162 /* Additions in Rel-8: */
163 bitvec_set_bit(bv, H);
164
165 /* Priority and E-UTRAN Parameters Description */
166 bitvec_set_bit(bv, 1);
167
168 /* No Serving Cell Priority Parameters Descr. */
169 bitvec_set_bit(bv, 0);
170 /* No 3G Priority Parameters Description */
171 bitvec_set_bit(bv, 0);
172 /* E-UTRAN Parameters Description */
173 bitvec_set_bit(bv, 1);
174
175 /* E-UTRAN_CCN_ACTIVE */
176 bitvec_set_bit(bv, 0);
177 /* E-UTRAN_Start: 9.1.54 */
178 bitvec_set_bit(bv, 1);
179 /* E-UTRAN_Stop: 9.1.54 */
180 bitvec_set_bit(bv, 1);
181
182 /* No E-UTRAN Measurement Parameters Descr. */
183 bitvec_set_bit(bv, 0);
184 /* No GPRS E-UTRAN Measurement Param. Descr. */
185 bitvec_set_bit(bv, 0);
186
187 /* Note: each of next 3 "repeated" structures might be repeated any
188 (0, 1, 2...) times - we only support 1 and 0 */
189
190 /* Repeated E-UTRAN Neighbour Cells */
191 bitvec_set_bit(bv, 1);
192
Maxf39d03a2017-05-12 17:00:30 +0200193 /* N. B: 25 bits are set in append_earfcn() - keep it in sync with budget adjustment below: */
194 append_eutran_neib_cell(bv, bts, budget - 25);
Max59a1bf32016-04-15 16:04:46 +0200195
196 /* stop bit - end of Repeated E-UTRAN Neighbour Cells sequence: */
197 bitvec_set_bit(bv, 0);
198
199 /* Note: following 2 repeated structs are not supported ATM */
200 /* stop bit - end of Repeated E-UTRAN Not Allowed Cells sequence: */
201 bitvec_set_bit(bv, 0);
202 /* stop bit - end of Repeated E-UTRAN PCID to TA mapping sequence: */
203 bitvec_set_bit(bv, 0);
204
205 /* Priority and E-UTRAN Parameters Description ends here */
206 /* No 3G CSG Description */
207 bitvec_set_bit(bv, 0);
208 /* No E-UTRAN CSG Description */
209 bitvec_set_bit(bv, 0);
210 /* No Additions in Rel-9: */
211 bitvec_set_bit(bv, L);
212}
213
Maxf39d03a2017-05-12 17:00:30 +0200214static inline int f0_helper(int *sc, size_t length, uint8_t *chan_list)
Maxe610e702016-12-19 13:41:48 +0100215{
Maxf39d03a2017-05-12 17:00:30 +0200216 int w[RANGE_ENC_MAX_ARFCNS] = { 0 };
217
218 return range_encode(ARFCN_RANGE_1024, sc, length, w, 0, chan_list);
219}
220
221/* Estimate how many bits it'll take to append single FDD UARFCN */
222static inline int append_utran_fdd_length(uint16_t u, int *sc, size_t sc_len, size_t length)
223{
224 uint8_t chan_list[16] = { 0 };
225 int tmp[sc_len], f0;
226
227 memcpy(tmp, sc, sizeof(tmp));
228
229 f0 = f0_helper(tmp, length, chan_list);
230 if (f0 < 0)
231 return f0;
232
233 return 21 + range1024_p(length);
234}
235
236/* Append single FDD UARFCN */
237static inline int append_utran_fdd(struct bitvec *bv, uint16_t u, int *sc, size_t length)
238{
239 uint8_t chan_list[16] = { 0 };
240 int f0 = f0_helper(sc, length, chan_list);
241
242 if (f0 < 0)
243 return f0;
244
Maxe610e702016-12-19 13:41:48 +0100245 /* Repeated UTRAN FDD Neighbour Cells */
246 bitvec_set_bit(bv, 1);
247
248 /* FDD-ARFCN */
249 bitvec_set_bit(bv, 0);
250 bitvec_set_uint(bv, u, 14);
251
Maxe610e702016-12-19 13:41:48 +0100252 /* FDD_Indic0: parameter value '0000000000' is a member of the set? */
253 bitvec_set_bit(bv, f0);
254 /* NR_OF_FDD_CELLS */
255 bitvec_set_uint(bv, length, 5);
256
257 f0 = bv->cur_bit;
258 bitvec_add_range1024(bv, (struct gsm48_range_1024 *)chan_list);
259 bv->cur_bit = f0 + range1024_p(length);
Maxf39d03a2017-05-12 17:00:30 +0200260
261 return 21 + range1024_p(length);
Maxe610e702016-12-19 13:41:48 +0100262}
263
264/* Append multiple FDD UARFCNs */
Maxf39d03a2017-05-12 17:00:30 +0200265static inline int append_uarfcns(struct bitvec *bv, struct gsm_bts *bts, uint8_t budget)
Max26679e02016-04-20 15:57:13 +0200266{
Maxf39d03a2017-05-12 17:00:30 +0200267 const uint16_t *u = bts->si_common.data.uarfcn_list, *sc = bts->si_common.data.scramble_list;
268 int i, j, k, rc, st = 0, a[bts->si_common.uarfcn_length];
269 uint16_t cu = u[bts->u_offset]; /* caller ensures that length is positive */
270 uint8_t rem = budget - 7; /* account for constant bits right away */
Max26679e02016-04-20 15:57:13 +0200271
272 /* 3G Neighbour Cell Description */
273 bitvec_set_bit(bv, 1);
274 /* No Index_Start_3G */
275 bitvec_set_bit(bv, 0);
276 /* No Absolute_Index_Start_EMR */
277 bitvec_set_bit(bv, 0);
278
279 /* UTRAN FDD Description */
280 bitvec_set_bit(bv, 1);
281 /* No Bandwidth_FDD */
282 bitvec_set_bit(bv, 0);
283
Maxf39d03a2017-05-12 17:00:30 +0200284 for (i = bts->u_offset; i < bts->si_common.uarfcn_length; i++) {
Maxe610e702016-12-19 13:41:48 +0100285 for (j = st, k = 0; j < i; j++)
286 a[k++] = sc[j]; /* copy corresponding SCs */
Maxf39d03a2017-05-12 17:00:30 +0200287
Maxe610e702016-12-19 13:41:48 +0100288 if (u[i] != cu) { /* we've reached new UARFCN */
Maxf39d03a2017-05-12 17:00:30 +0200289 rc = append_utran_fdd_length(cu, a, bts->si_common.uarfcn_length, k);
290 if (rc < 0) { /* estimate bit length requirements */
Maxe610e702016-12-19 13:41:48 +0100291 return rc;
Maxf39d03a2017-05-12 17:00:30 +0200292 }
293
294 if (rem - rc < 0) {
295 break; /* we have ran out of budget in current SI2q */
296 } else {
297 rem -= append_utran_fdd(bv, cu, a, k);
298 bts->u_offset++;
299 }
Maxe610e702016-12-19 13:41:48 +0100300 cu = u[i];
301 st = i; /* update start position */
302 }
303 }
Max26679e02016-04-20 15:57:13 +0200304
Maxf39d03a2017-05-12 17:00:30 +0200305 if (rem > 22) { /* add last UARFCN not covered by previous cycle if it could possibly fit into budget */
306 for (i = st, k = 0; i < bts->si_common.uarfcn_length; i++)
307 a[k++] = sc[i];
308
309 rc = append_utran_fdd_length(cu, a, bts->si_common.uarfcn_length, k);
310 if (rc < 0) {
311 return rc;
312 }
313
314 if (rem - rc >= 0) {
315 rem -= append_utran_fdd(bv, cu, a, k);
316 bts->u_offset++;
317 }
318 }
Max26679e02016-04-20 15:57:13 +0200319
320 /* stop bit - end of Repeated UTRAN FDD Neighbour Cells */
321 bitvec_set_bit(bv, 0);
322
323 /* UTRAN TDD Description */
324 bitvec_set_bit(bv, 0);
Max881064e2016-12-14 14:51:40 +0100325
326 return 0;
Max26679e02016-04-20 15:57:13 +0200327}
328
Max59a1bf32016-04-15 16:04:46 +0200329/* generate SI2quater rest octets: 3GPP TS 44.018 § 10.5.2.33b */
Maxf39d03a2017-05-12 17:00:30 +0200330int rest_octets_si2quater(uint8_t *data, struct gsm_bts *bts)
Max59a1bf32016-04-15 16:04:46 +0200331{
Max881064e2016-12-14 14:51:40 +0100332 int rc;
Max59a1bf32016-04-15 16:04:46 +0200333 struct bitvec bv;
334 bv.data = data;
335 bv.data_len = 20;
336 bitvec_zero(&bv);
337
338 /* BA_IND */
339 bitvec_set_bit(&bv, 1);
340 /* 3G_BA_IND */
341 bitvec_set_bit(&bv, 1);
342 /* MP_CHANGE_MARK */
343 bitvec_set_bit(&bv, 0);
344
Max59a1bf32016-04-15 16:04:46 +0200345 /* SI2quater_INDEX */
Maxf39d03a2017-05-12 17:00:30 +0200346 bitvec_set_uint(&bv, bts->si2q_index, 4);
Max59a1bf32016-04-15 16:04:46 +0200347 /* SI2quater_COUNT */
Maxf39d03a2017-05-12 17:00:30 +0200348 bitvec_set_uint(&bv, bts->si2q_count, 4);
Max59a1bf32016-04-15 16:04:46 +0200349
350 /* No Measurement_Parameters Description */
351 bitvec_set_bit(&bv, 0);
352 /* No GPRS_Real Time Difference Description */
353 bitvec_set_bit(&bv, 0);
354 /* No GPRS_BSIC Description */
355 bitvec_set_bit(&bv, 0);
356 /* No GPRS_REPORT PRIORITY Description */
357 bitvec_set_bit(&bv, 0);
358 /* No GPRS_MEASUREMENT_Parameters Description */
359 bitvec_set_bit(&bv, 0);
360 /* No NC Measurement Parameters */
361 bitvec_set_bit(&bv, 0);
362 /* No extension (length) */
363 bitvec_set_bit(&bv, 0);
364
Maxf39d03a2017-05-12 17:00:30 +0200365 if (bts->si_common.uarfcn_length) {
Max26679e02016-04-20 15:57:13 +0200366 /* Even if we do not append EARFCN we still need to set 3 bits */
Maxf39d03a2017-05-12 17:00:30 +0200367 rc = append_uarfcns(&bv, bts, SI2Q_MAX_LEN - (bv.cur_bit + 3));
Max881064e2016-12-14 14:51:40 +0100368 if (rc < 0) {
Maxf39d03a2017-05-12 17:00:30 +0200369 LOGP(DRR, LOGL_ERROR, "SI2quater: failed to append %zu UARFCNs due to range encoding failure: %s\n",
370 bts->si_common.uarfcn_length, strerror(-rc));
Max881064e2016-12-14 14:51:40 +0100371 return rc;
372 }
Max59a1bf32016-04-15 16:04:46 +0200373 } else { /* No 3G Neighbour Cell Description */
374 bitvec_set_bit(&bv, 0);
375 }
376
377 /* No 3G Measurement Parameters Description */
378 bitvec_set_bit(&bv, 0);
379 /* No GPRS_3G_MEASUREMENT Parameters Descr. */
380 bitvec_set_bit(&bv, 0);
381
Maxf39d03a2017-05-12 17:00:30 +0200382 if (&bts->si_common.si2quater_neigh_list) { /* FIXME: use si2q_earfcn_count() in if */
383 append_earfcn(&bv, bts, SI2Q_MAX_LEN - bv.cur_bit);
384
385 /* FIXME: remove following check once multiple SI2q are properly supported */
386 if ((bts->e_offset != si2q_earfcn_count(&bts->si_common.si2quater_neigh_list)) ||
387 si2q_earfcn_count(&bts->si_common.si2quater_neigh_list) > 5)
Max59a1bf32016-04-15 16:04:46 +0200388 return -ENOMEM;
Maxf39d03a2017-05-12 17:00:30 +0200389
Max59a1bf32016-04-15 16:04:46 +0200390 } else {
391 /* No Additions in Rel-5: */
392 bitvec_set_bit(&bv, L);
393 }
394
395 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
396 return bv.data_len;
397}
398
Harald Weltea43f7892009-12-01 18:04:30 +0530399/* Append selection parameters to bitvec */
400static void append_selection_params(struct bitvec *bv,
401 const struct gsm48_si_selection_params *sp)
402{
403 if (sp->present) {
404 bitvec_set_bit(bv, H);
405 bitvec_set_bit(bv, sp->cbq);
406 bitvec_set_uint(bv, sp->cell_resel_off, 6);
407 bitvec_set_uint(bv, sp->temp_offs, 3);
408 bitvec_set_uint(bv, sp->penalty_time, 5);
409 } else
410 bitvec_set_bit(bv, L);
411}
412
413/* Append power offset to bitvec */
414static void append_power_offset(struct bitvec *bv,
415 const struct gsm48_si_power_offset *po)
416{
417 if (po->present) {
418 bitvec_set_bit(bv, H);
419 bitvec_set_uint(bv, po->power_offset, 2);
420 } else
421 bitvec_set_bit(bv, L);
422}
423
424/* Append GPRS indicator to bitvec */
425static void append_gprs_ind(struct bitvec *bv,
426 const struct gsm48_si3_gprs_ind *gi)
427{
428 if (gi->present) {
429 bitvec_set_bit(bv, H);
430 bitvec_set_uint(bv, gi->ra_colour, 3);
431 /* 0 == SI13 in BCCH Norm, 1 == SI13 sent on BCCH Ext */
432 bitvec_set_bit(bv, gi->si13_position);
433 } else
434 bitvec_set_bit(bv, L);
435}
436
437
438/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200439int rest_octets_si3(uint8_t *data, const struct gsm48_si_ro_info *si3)
Harald Weltea43f7892009-12-01 18:04:30 +0530440{
441 struct bitvec bv;
442
443 memset(&bv, 0, sizeof(bv));
444 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100445 bv.data_len = 4;
Harald Weltea43f7892009-12-01 18:04:30 +0530446
447 /* Optional Selection Parameters */
448 append_selection_params(&bv, &si3->selection_params);
449
450 /* Optional Power Offset */
451 append_power_offset(&bv, &si3->power_offset);
452
453 /* Do we have a SI2ter on the BCCH? */
454 if (si3->si2ter_indicator)
455 bitvec_set_bit(&bv, H);
456 else
457 bitvec_set_bit(&bv, L);
458
459 /* Early Classmark Sending Control */
460 if (si3->early_cm_ctrl)
461 bitvec_set_bit(&bv, H);
462 else
463 bitvec_set_bit(&bv, L);
464
465 /* Do we have a SI Type 9 on the BCCH? */
466 if (si3->scheduling.present) {
467 bitvec_set_bit(&bv, H);
468 bitvec_set_uint(&bv, si3->scheduling.where, 3);
469 } else
470 bitvec_set_bit(&bv, L);
471
472 /* GPRS Indicator */
473 append_gprs_ind(&bv, &si3->gprs_ind);
474
Maxf3f35052016-04-15 16:04:44 +0200475 /* 3G Early Classmark Sending Restriction controlled by
476 * early_cm_ctrl above */
477 bitvec_set_bit(&bv, H);
478
479 if (si3->si2quater_indicator) {
480 bitvec_set_bit(&bv, H); /* indicator struct present */
481 bitvec_set_uint(&bv, 0, 1); /* message is sent on BCCH Norm */
482 }
483
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100484 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
485 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530486}
487
488static int append_lsa_params(struct bitvec *bv,
489 const struct gsm48_lsa_params *lsa_params)
490{
491 /* FIXME */
Holger Hans Peter Freytherae80f922010-04-10 00:05:16 +0200492 return -1;
Harald Weltea43f7892009-12-01 18:04:30 +0530493}
494
495/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100496int rest_octets_si4(uint8_t *data, const struct gsm48_si_ro_info *si4, int len)
Harald Weltea43f7892009-12-01 18:04:30 +0530497{
498 struct bitvec bv;
499
500 memset(&bv, 0, sizeof(bv));
501 bv.data = data;
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100502 bv.data_len = len;
Harald Weltea43f7892009-12-01 18:04:30 +0530503
504 /* SI4 Rest Octets O */
505 append_selection_params(&bv, &si4->selection_params);
506 append_power_offset(&bv, &si4->power_offset);
507 append_gprs_ind(&bv, &si4->gprs_ind);
508
509 if (0 /* FIXME */) {
510 /* H and SI4 Rest Octets S */
511 bitvec_set_bit(&bv, H);
512
513 /* LSA Parameters */
514 if (si4->lsa_params.present) {
515 bitvec_set_bit(&bv, H);
516 append_lsa_params(&bv, &si4->lsa_params);
517 } else
518 bitvec_set_bit(&bv, L);
519
520 /* Cell Identity */
521 if (1) {
522 bitvec_set_bit(&bv, H);
523 bitvec_set_uint(&bv, si4->cell_id, 16);
524 } else
525 bitvec_set_bit(&bv, L);
526
527 /* LSA ID Information */
528 if (0) {
529 bitvec_set_bit(&bv, H);
530 /* FIXME */
531 } else
532 bitvec_set_bit(&bv, L);
533 } else {
534 /* L and break indicator */
535 bitvec_set_bit(&bv, L);
536 bitvec_set_bit(&bv, si4->break_ind ? H : L);
537 }
538
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100539 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530540}
541
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +0200542
543/* GSM 04.18 ETSI TS 101 503 V8.27.0 (2006-05)
544
545<SI6 rest octets> ::=
546{L | H <PCH and NCH info>}
547{L | H <VBS/VGCS options : bit(2)>}
548{ < DTM_support : bit == L > I < DTM_support : bit == H >
549< RAC : bit (8) >
550< MAX_LAPDm : bit (3) > }
551< Band indicator >
552{ L | H < GPRS_MS_TXPWR_MAX_CCH : bit (5) > }
553<implicit spare >;
554*/
555int rest_octets_si6(uint8_t *data, bool is1800_net)
556{
557 struct bitvec bv;
558
559 memset(&bv, 0, sizeof(bv));
560 bv.data = data;
561 bv.data_len = 1;
562
563 /* no PCH/NCH info */
564 bitvec_set_bit(&bv, L);
565 /* no VBS/VGCS options */
566 bitvec_set_bit(&bv, L);
567 /* no DTM_support */
568 bitvec_set_bit(&bv, L);
569 /* band indicator */
570 if (is1800_net)
571 bitvec_set_bit(&bv, L);
572 else
573 bitvec_set_bit(&bv, H);
574 /* no GPRS_MS_TXPWR_MAX_CCH */
575 bitvec_set_bit(&bv, L);
576
577 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
578 return bv.data_len;
579}
580
Harald Weltea43f7892009-12-01 18:04:30 +0530581/* GPRS Mobile Allocation as per TS 04.60 Chapter 12.10a:
582 < GPRS Mobile Allocation IE > ::=
583 < HSN : bit (6) >
584 { 0 | 1 < RFL number list : < RFL number list struct > > }
585 { 0 < MA_LENGTH : bit (6) >
586 < MA_BITMAP: bit (val(MA_LENGTH) + 1) >
587 | 1 { 0 | 1 <ARFCN index list : < ARFCN index list struct > > } } ;
588
589 < RFL number list struct > :: =
590 < RFL_NUMBER : bit (4) >
591 { 0 | 1 < RFL number list struct > } ;
592 < ARFCN index list struct > ::=
593 < ARFCN_INDEX : bit(6) >
594 { 0 | 1 < ARFCN index list struct > } ;
595 */
596static int append_gprs_mobile_alloc(struct bitvec *bv)
597{
598 /* Hopping Sequence Number */
599 bitvec_set_uint(bv, 0, 6);
600
601 if (0) {
602 /* We want to use a RFL number list */
603 bitvec_set_bit(bv, 1);
604 /* FIXME: RFL number list */
605 } else
606 bitvec_set_bit(bv, 0);
607
608 if (0) {
609 /* We want to use a MA_BITMAP */
610 bitvec_set_bit(bv, 0);
611 /* FIXME: MA_LENGTH, MA_BITMAP, ... */
612 } else {
613 bitvec_set_bit(bv, 1);
614 if (0) {
615 /* We want to provide an ARFCN index list */
616 bitvec_set_bit(bv, 1);
617 /* FIXME */
618 } else
619 bitvec_set_bit(bv, 0);
620 }
621 return 0;
622}
623
624static int encode_t3192(unsigned int t3192)
625{
Philipp Maier3d6cb332016-12-20 14:23:45 +0100626 /* See also 3GPP TS 44.060
627 Table 12.24.2: GPRS Cell Options information element details */
Harald Weltea43f7892009-12-01 18:04:30 +0530628 if (t3192 == 0)
629 return 3;
630 else if (t3192 <= 80)
631 return 4;
632 else if (t3192 <= 120)
633 return 5;
634 else if (t3192 <= 160)
635 return 6;
636 else if (t3192 <= 200)
637 return 7;
638 else if (t3192 <= 500)
639 return 0;
640 else if (t3192 <= 1000)
641 return 1;
642 else if (t3192 <= 1500)
643 return 2;
644 else
645 return -EINVAL;
646}
647
648static int encode_drx_timer(unsigned int drx)
649{
650 if (drx == 0)
651 return 0;
652 else if (drx == 1)
653 return 1;
654 else if (drx == 2)
655 return 2;
656 else if (drx <= 4)
657 return 3;
658 else if (drx <= 8)
659 return 4;
660 else if (drx <= 16)
661 return 5;
662 else if (drx <= 32)
663 return 6;
664 else if (drx <= 64)
665 return 7;
666 else
667 return -EINVAL;
668}
669
670/* GPRS Cell Options as per TS 04.60 Chapter 12.24
671 < GPRS Cell Options IE > ::=
672 < NMO : bit(2) >
673 < T3168 : bit(3) >
674 < T3192 : bit(3) >
675 < DRX_TIMER_MAX: bit(3) >
676 < ACCESS_BURST_TYPE: bit >
677 < CONTROL_ACK_TYPE : bit >
678 < BS_CV_MAX: bit(4) >
679 { 0 | 1 < PAN_DEC : bit(3) >
680 < PAN_INC : bit(3) >
681 < PAN_MAX : bit(3) >
682 { 0 | 1 < Extension Length : bit(6) >
683 < bit (val(Extension Length) + 1
684 & { < Extension Information > ! { bit ** = <no string> } } ;
685 < Extension Information > ::=
686 { 0 | 1 < EGPRS_PACKET_CHANNEL_REQUEST : bit >
687 < BEP_PERIOD : bit(4) > }
688 < PFC_FEATURE_MODE : bit >
689 < DTM_SUPPORT : bit >
690 <BSS_PAGING_COORDINATION: bit >
691 <spare bit > ** ;
692 */
Harald Weltea4e2d042009-12-20 17:08:22 +0100693static int append_gprs_cell_opt(struct bitvec *bv,
694 const struct gprs_cell_options *gco)
Harald Weltea43f7892009-12-01 18:04:30 +0530695{
696 int t3192, drx_timer_max;
697
698 t3192 = encode_t3192(gco->t3192);
699 if (t3192 < 0)
700 return t3192;
701
702 drx_timer_max = encode_drx_timer(gco->drx_timer_max);
703 if (drx_timer_max < 0)
704 return drx_timer_max;
705
706 bitvec_set_uint(bv, gco->nmo, 2);
Philipp Maier3d6cb332016-12-20 14:23:45 +0100707
708 /* See also 3GPP TS 44.060
709 Table 12.24.2: GPRS Cell Options information element details */
710 bitvec_set_uint(bv, gco->t3168 / 500 - 1, 3);
711
Harald Weltea43f7892009-12-01 18:04:30 +0530712 bitvec_set_uint(bv, t3192, 3);
713 bitvec_set_uint(bv, drx_timer_max, 3);
714 /* ACCESS_BURST_TYPE: Hard-code 8bit */
715 bitvec_set_bit(bv, 0);
Max292ec582016-07-28 11:55:37 +0200716 /* CONTROL_ACK_TYPE: */
717 bitvec_set_bit(bv, gco->ctrl_ack_type_use_block);
Harald Weltea43f7892009-12-01 18:04:30 +0530718 bitvec_set_uint(bv, gco->bs_cv_max, 4);
719
Harald Weltea4b16652010-05-31 12:54:23 +0200720 if (0) {
721 /* hard-code no PAN_{DEC,INC,MAX} */
722 bitvec_set_bit(bv, 0);
723 } else {
724 /* copied from ip.access BSC protocol trace */
725 bitvec_set_bit(bv, 1);
726 bitvec_set_uint(bv, 1, 3); /* DEC */
727 bitvec_set_uint(bv, 1, 3); /* INC */
728 bitvec_set_uint(bv, 15, 3); /* MAX */
729 }
Harald Weltea43f7892009-12-01 18:04:30 +0530730
Harald Welteda0586a2010-04-18 14:49:05 +0200731 if (!gco->ext_info_present) {
732 /* no extension information */
733 bitvec_set_bit(bv, 0);
734 } else {
735 /* extension information */
736 bitvec_set_bit(bv, 1);
737 if (!gco->ext_info.egprs_supported) {
738 /* 6bit length of extension */
Harald Welte39608dc2010-04-18 22:47:22 +0200739 bitvec_set_uint(bv, (1 + 3)-1, 6);
Harald Welteda0586a2010-04-18 14:49:05 +0200740 /* EGPRS supported in the cell */
741 bitvec_set_bit(bv, 0);
742 } else {
743 /* 6bit length of extension */
Harald Welte39608dc2010-04-18 22:47:22 +0200744 bitvec_set_uint(bv, (1 + 5 + 3)-1, 6);
Harald Welteda0586a2010-04-18 14:49:05 +0200745 /* EGPRS supported in the cell */
746 bitvec_set_bit(bv, 1);
bhargava350533c2016-07-21 11:14:34 +0530747
Harald Welteda0586a2010-04-18 14:49:05 +0200748 /* 1bit EGPRS PACKET CHANNEL REQUEST */
bhargava350533c2016-07-21 11:14:34 +0530749 if (gco->supports_egprs_11bit_rach == 0) {
750 bitvec_set_bit(bv,
751 gco->ext_info.use_egprs_p_ch_req);
752 } else {
753 bitvec_set_bit(bv, 0);
754 }
755
Harald Welteda0586a2010-04-18 14:49:05 +0200756 /* 4bit BEP PERIOD */
757 bitvec_set_uint(bv, gco->ext_info.bep_period, 4);
758 }
759 bitvec_set_bit(bv, gco->ext_info.pfc_supported);
760 bitvec_set_bit(bv, gco->ext_info.dtm_supported);
761 bitvec_set_bit(bv, gco->ext_info.bss_paging_coordination);
762 }
Harald Weltea43f7892009-12-01 18:04:30 +0530763
764 return 0;
765}
766
767static void append_gprs_pwr_ctrl_pars(struct bitvec *bv,
Harald Weltea4e2d042009-12-20 17:08:22 +0100768 const struct gprs_power_ctrl_pars *pcp)
Harald Weltea43f7892009-12-01 18:04:30 +0530769{
770 bitvec_set_uint(bv, pcp->alpha, 4);
771 bitvec_set_uint(bv, pcp->t_avg_w, 5);
772 bitvec_set_uint(bv, pcp->t_avg_t, 5);
773 bitvec_set_uint(bv, pcp->pc_meas_chan, 1);
774 bitvec_set_uint(bv, pcp->n_avg_i, 4);
775}
776
Harald Welte5fda9082010-04-18 22:41:01 +0200777/* Generate SI13 Rest Octests (04.08 Chapter 10.5.2.37b) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200778int rest_octets_si13(uint8_t *data, const struct gsm48_si13_info *si13)
Harald Weltea43f7892009-12-01 18:04:30 +0530779{
780 struct bitvec bv;
781
782 memset(&bv, 0, sizeof(bv));
783 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100784 bv.data_len = 20;
Harald Weltea43f7892009-12-01 18:04:30 +0530785
786 if (0) {
787 /* No rest octets */
788 bitvec_set_bit(&bv, L);
789 } else {
790 bitvec_set_bit(&bv, H);
791 bitvec_set_uint(&bv, si13->bcch_change_mark, 3);
792 bitvec_set_uint(&bv, si13->si_change_field, 4);
Harald Welte97a282b2010-03-14 15:37:43 +0800793 if (1) {
Harald Weltea43f7892009-12-01 18:04:30 +0530794 bitvec_set_bit(&bv, 0);
795 } else {
796 bitvec_set_bit(&bv, 1);
797 bitvec_set_uint(&bv, si13->bcch_change_mark, 2);
798 append_gprs_mobile_alloc(&bv);
799 }
800 if (!si13->pbcch_present) {
801 /* PBCCH not present in cell */
802 bitvec_set_bit(&bv, 0);
803 bitvec_set_uint(&bv, si13->no_pbcch.rac, 8);
804 bitvec_set_bit(&bv, si13->no_pbcch.spgc_ccch_sup);
805 bitvec_set_uint(&bv, si13->no_pbcch.prio_acc_thr, 3);
806 bitvec_set_uint(&bv, si13->no_pbcch.net_ctrl_ord, 2);
807 append_gprs_cell_opt(&bv, &si13->cell_opts);
808 append_gprs_pwr_ctrl_pars(&bv, &si13->pwr_ctrl_pars);
809 } else {
810 /* PBCCH present in cell */
811 bitvec_set_bit(&bv, 1);
812 bitvec_set_uint(&bv, si13->pbcch.psi1_rep_per, 4);
813 /* PBCCH Descripiton */
814 bitvec_set_uint(&bv, si13->pbcch.pb, 4);
815 bitvec_set_uint(&bv, si13->pbcch.tsc, 3);
816 bitvec_set_uint(&bv, si13->pbcch.tn, 3);
817 switch (si13->pbcch.carrier_type) {
818 case PBCCH_BCCH:
819 bitvec_set_bit(&bv, 0);
820 bitvec_set_bit(&bv, 0);
821 break;
822 case PBCCH_ARFCN:
823 bitvec_set_bit(&bv, 0);
824 bitvec_set_bit(&bv, 1);
825 bitvec_set_uint(&bv, si13->pbcch.arfcn, 10);
826 break;
827 case PBCCH_MAIO:
828 bitvec_set_bit(&bv, 1);
829 bitvec_set_uint(&bv, si13->pbcch.maio, 6);
830 break;
831 }
832 }
Harald Welte5fda9082010-04-18 22:41:01 +0200833 /* 3GPP TS 44.018 Release 6 / 10.5.2.37b */
834 bitvec_set_bit(&bv, H); /* added Release 99 */
835 /* claim our SGSN is compatible with Release 99, as EDGE and EGPRS
836 * was only added in this Release */
837 bitvec_set_bit(&bv, 1);
Harald Weltea43f7892009-12-01 18:04:30 +0530838 }
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100839 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
840 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530841}