blob: 3c1b22af6d884ccbc89f96de3c5648d817621cc9 [file] [log] [blame]
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +02001/* 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 Welte912d96d2019-05-27 23:17:46 +020010 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +020012 * (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 Welte912d96d2019-05-27 23:17:46 +020017 * GNU General Public License for more details.
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +020018 *
Harald Welte912d96d2019-05-27 23:17:46 +020019 * You should have received a copy of the GNU General Public License
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +020020 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
Harald Welte912d96d2019-05-27 23:17:46 +020022 * SPDX-License-Identifier: GPL-2.0+
23 *
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +020024 */
25
26#include <string.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <stdbool.h>
30
31#include <osmocom/core/bitvec.h>
32#include <osmocom/gsm/bitvec_gsm.h>
33#include <osmocom/gsm/sysinfo.h>
34#include <osmocom/gsm/gsm48_arfcn_range_encode.h>
35#include <osmocom/gsm/gsm48_rest_octets.h>
36
37/* generate SI1 rest octets */
Harald Welte428d03b2019-05-28 18:46:20 +020038int osmo_gsm48_rest_octets_si1_encode(uint8_t *data, uint8_t *nch_pos, int is1800_net)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +020039{
40 struct bitvec bv;
41
42 memset(&bv, 0, sizeof(bv));
43 bv.data = data;
44 bv.data_len = 1;
45
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
52 if (is1800_net)
53 bitvec_set_bit(&bv, L);
54 else
55 bitvec_set_bit(&bv, H);
56
57 bitvec_spare_padding(&bv, 6);
58 return bv.data_len;
59}
60
61/* Append Repeated E-UTRAN Neighbour Cell to bitvec: see 3GPP TS 44.018 Table 10.5.2.33b.1 */
62static inline bool append_eutran_neib_cell(struct bitvec *bv, const struct osmo_earfcn_si2q *e, size_t *e_offset,
63 uint8_t budget)
64{
65 unsigned i, skip = 0;
66 int16_t rem = budget - 6; /* account for mandatory stop bit and THRESH_E-UTRAN_high */
67 uint8_t earfcn_budget;
68
69 if (budget <= 6)
70 return false;
71
72 OSMO_ASSERT(budget <= SI2Q_MAX_LEN);
73
74 /* first we have to properly adjust budget requirements */
75 if (e->prio_valid) /* E-UTRAN_PRIORITY: 3GPP TS 45.008*/
76 rem -= 4;
77 else
78 rem--;
79
80 if (e->thresh_lo_valid) /* THRESH_E-UTRAN_low: */
81 rem -= 6;
82 else
83 rem--;
84
85 if (e->qrxlm_valid) /* E-UTRAN_QRXLEVMIN: */
86 rem -= 6;
87 else
88 rem--;
89
90 if (rem < 0)
91 return false;
92
93 /* now we can proceed with actually adding EARFCNs within adjusted budget limit */
94 for (i = 0; i < e->length; i++) {
95 if (e->arfcn[i] != OSMO_EARFCN_INVALID) {
96 if (skip < *e_offset) {
97 skip++; /* ignore EARFCNs added on previous calls */
98 } else {
99 earfcn_budget = 17; /* compute budget per-EARFCN */
100 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
101 earfcn_budget++;
102 else
103 earfcn_budget += 4;
104
105 if (rem - earfcn_budget < 0)
106 break;
107 else {
108 (*e_offset)++;
109 rem -= earfcn_budget;
110
111 if (rem < 0)
112 return false;
113
114 bitvec_set_bit(bv, 1); /* EARFCN: */
115 bitvec_set_uint(bv, e->arfcn[i], 16);
116
117 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
118 bitvec_set_bit(bv, 0);
119 else { /* Measurement Bandwidth: 9.1.54 */
120 bitvec_set_bit(bv, 1);
121 bitvec_set_uint(bv, e->meas_bw[i], 3);
122 }
123 }
124 }
125 }
126 }
127
128 /* stop bit - end of EARFCN + Measurement Bandwidth sequence */
129 bitvec_set_bit(bv, 0);
130
131 /* Note: we don't support different EARFCN arrays each with different priority, threshold etc. */
132
133 if (e->prio_valid) {
134 /* E-UTRAN_PRIORITY: 3GPP TS 45.008*/
135 bitvec_set_bit(bv, 1);
136 bitvec_set_uint(bv, e->prio, 3);
137 } else
138 bitvec_set_bit(bv, 0);
139
140 /* THRESH_E-UTRAN_high */
141 bitvec_set_uint(bv, e->thresh_hi, 5);
142
143 if (e->thresh_lo_valid) {
144 /* THRESH_E-UTRAN_low: */
145 bitvec_set_bit(bv, 1);
146 bitvec_set_uint(bv, e->thresh_lo, 5);
147 } else
148 bitvec_set_bit(bv, 0);
149
150 if (e->qrxlm_valid) {
151 /* E-UTRAN_QRXLEVMIN: */
152 bitvec_set_bit(bv, 1);
153 bitvec_set_uint(bv, e->qrxlm, 5);
154 } else
155 bitvec_set_bit(bv, 0);
156
157 return true;
158}
159
160static inline void append_earfcn(struct bitvec *bv, const struct osmo_earfcn_si2q *e, size_t *e_offset, uint8_t budget)
161{
162 bool appended;
163 unsigned int old = bv->cur_bit; /* save current position to make rollback possible */
164 int rem = budget - 25;
165 if (rem <= 0)
166 return;
167
168 OSMO_ASSERT(budget <= SI2Q_MAX_LEN);
169
170 /* Additions in Rel-5: */
171 bitvec_set_bit(bv, H);
172 /* No 3G Additional Measurement Param. Descr. */
173 bitvec_set_bit(bv, 0);
174 /* No 3G ADDITIONAL MEASUREMENT Param. Descr. 2 */
175 bitvec_set_bit(bv, 0);
176 /* Additions in Rel-6: */
177 bitvec_set_bit(bv, H);
178 /* 3G_CCN_ACTIVE */
179 bitvec_set_bit(bv, 0);
180 /* Additions in Rel-7: */
181 bitvec_set_bit(bv, H);
182 /* No 700_REPORTING_OFFSET */
183 bitvec_set_bit(bv, 0);
184 /* No 810_REPORTING_OFFSET */
185 bitvec_set_bit(bv, 0);
186 /* Additions in Rel-8: */
187 bitvec_set_bit(bv, H);
188
189 /* Priority and E-UTRAN Parameters Description */
190 bitvec_set_bit(bv, 1);
191
192 /* No Serving Cell Priority Parameters Descr. */
193 bitvec_set_bit(bv, 0);
194 /* No 3G Priority Parameters Description */
195 bitvec_set_bit(bv, 0);
196 /* E-UTRAN Parameters Description */
197 bitvec_set_bit(bv, 1);
198
199 /* E-UTRAN_CCN_ACTIVE */
200 bitvec_set_bit(bv, 0);
201 /* E-UTRAN_Start: 9.1.54 */
202 bitvec_set_bit(bv, 1);
203 /* E-UTRAN_Stop: 9.1.54 */
204 bitvec_set_bit(bv, 1);
205
206 /* No E-UTRAN Measurement Parameters Descr. */
207 bitvec_set_bit(bv, 0);
208 /* No GPRS E-UTRAN Measurement Param. Descr. */
209 bitvec_set_bit(bv, 0);
210
211 /* Note: each of next 3 "repeated" structures might be repeated any
212 (0, 1, 2...) times - we only support 1 and 0 */
213
214 /* Repeated E-UTRAN Neighbour Cells */
215 bitvec_set_bit(bv, 1);
216
217 appended = append_eutran_neib_cell(bv, e, e_offset, rem);
218 if (!appended) { /* appending is impossible within current budget: rollback */
219 bv->cur_bit = old;
220 return;
221 }
222
223 /* stop bit - end of Repeated E-UTRAN Neighbour Cells sequence: */
224 bitvec_set_bit(bv, 0);
225
226 /* Note: following 2 repeated structs are not supported ATM */
227 /* stop bit - end of Repeated E-UTRAN Not Allowed Cells sequence: */
228 bitvec_set_bit(bv, 0);
229 /* stop bit - end of Repeated E-UTRAN PCID to TA mapping sequence: */
230 bitvec_set_bit(bv, 0);
231
232 /* Priority and E-UTRAN Parameters Description ends here */
233 /* No 3G CSG Description */
234 bitvec_set_bit(bv, 0);
235 /* No E-UTRAN CSG Description */
236 bitvec_set_bit(bv, 0);
237 /* No Additions in Rel-9: */
238 bitvec_set_bit(bv, L);
239}
240
241static int range_encode(enum osmo_gsm48_range r, int *arfcns, int arfcns_used, int *w,
242 int f0, uint8_t *chan_list)
243{
244 /*
245 * Manipulate the ARFCN list according to the rules in J4 depending
246 * on the selected range.
247 */
248 int rc, f0_included;
249
250 osmo_gsm48_range_enc_filter_arfcns(arfcns, arfcns_used, f0, &f0_included);
251
252 rc = osmo_gsm48_range_enc_arfcns(r, arfcns, arfcns_used, w, 0);
253 if (rc < 0)
254 return rc;
255
256 /* Select the range and the amount of bits needed */
257 switch (r) {
258 case OSMO_GSM48_ARFCN_RANGE_128:
Harald Welte1276c172019-05-28 18:38:35 +0200259 return osmo_gsm48_range_enc_128(chan_list, f0, w);
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200260 case OSMO_GSM48_ARFCN_RANGE_256:
Harald Welte1276c172019-05-28 18:38:35 +0200261 return osmo_gsm48_range_enc_256(chan_list, f0, w);
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200262 case OSMO_GSM48_ARFCN_RANGE_512:
Harald Welte1276c172019-05-28 18:38:35 +0200263 return osmo_gsm48_range_enc_512(chan_list, f0, w);
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200264 case OSMO_GSM48_ARFCN_RANGE_1024:
Harald Welte1276c172019-05-28 18:38:35 +0200265 return osmo_gsm48_range_enc_1024(chan_list, f0, f0_included, w);
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200266 default:
267 return -ERANGE;
268 };
269
270 return f0_included;
271}
272
273static inline int f0_helper(int *sc, size_t length, uint8_t *chan_list)
274{
275 int w[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS] = { 0 };
276
277 return range_encode(OSMO_GSM48_ARFCN_RANGE_1024, sc, length, w, 0, chan_list);
278}
279
280/* Return p(n) for given NR_OF_TDD_CELLS - see Table 9.1.54.1a, 3GPP TS 44.018 */
281static unsigned range1024_p(unsigned n)
282{
283 switch (n) {
284 case 0: return 0;
285 case 1: return 10;
286 case 2: return 19;
287 case 3: return 28;
288 case 4: return 36;
289 case 5: return 44;
290 case 6: return 52;
291 case 7: return 60;
292 case 8: return 67;
293 case 9: return 74;
294 case 10: return 81;
295 case 11: return 88;
296 case 12: return 95;
297 case 13: return 102;
298 case 14: return 109;
299 case 15: return 116;
300 case 16: return 122;
301 default: return 0;
302 }
303}
304
305/* Estimate how many bits it'll take to append single FDD UARFCN */
306static inline int append_utran_fdd_length(uint16_t u, const int *sc, size_t sc_len, size_t length)
307{
308 uint8_t chan_list[16] = { 0 };
309 int tmp[sc_len], f0;
310
311 memcpy(tmp, sc, sizeof(tmp));
312
313 f0 = f0_helper(tmp, length, chan_list);
314 if (f0 < 0)
315 return f0;
316
317 return 21 + range1024_p(length);
318}
319
320/* Append single FDD UARFCN */
321static inline int append_utran_fdd(struct bitvec *bv, uint16_t u, int *sc, size_t length)
322{
323 uint8_t chan_list[16] = { 0 };
324 int f0 = f0_helper(sc, length, chan_list);
325
326 if (f0 < 0)
327 return f0;
328
329 /* Repeated UTRAN FDD Neighbour Cells */
330 bitvec_set_bit(bv, 1);
331
332 /* FDD-ARFCN */
333 bitvec_set_bit(bv, 0);
334 bitvec_set_uint(bv, u, 14);
335
336 /* FDD_Indic0: parameter value '0000000000' is a member of the set? */
337 bitvec_set_bit(bv, f0);
338 /* NR_OF_FDD_CELLS */
339 bitvec_set_uint(bv, length, 5);
340
341 f0 = bv->cur_bit;
342 bitvec_add_range1024(bv, (struct gsm48_range_1024 *)chan_list);
343 bv->cur_bit = f0 + range1024_p(length);
344
345 return 21 + range1024_p(length);
346}
347
348static inline int try_adding_uarfcn(struct bitvec *bv, uint16_t *scramble_list,
349 size_t uarfcn_length, size_t *u_offset,
350 uint16_t uarfcn, uint8_t num_sc, uint8_t start_pos, uint8_t budget)
351{
352 int i, k, rc, a[uarfcn_length];
353
354 if (budget < 23)
355 return -ENOMEM;
356
357 /* copy corresponding Scrambling Codes: range encoder make in-place modifications */
358 for (i = start_pos, k = 0; i < num_sc; a[k++] = scramble_list[i++]);
359
360 /* estimate bit length requirements */
361 rc = append_utran_fdd_length(uarfcn, a, uarfcn_length, k);
362 if (rc < 0)
363 return rc; /* range encoder failure */
364
365 if (budget - rc <= 0)
366 return -ENOMEM; /* we have ran out of budget in current SI2q */
367
368 /* compute next offset */
369 *u_offset += k;
370
371 return budget - append_utran_fdd(bv, uarfcn, a, k);
372}
373
374/* Append multiple FDD UARFCNs */
375static inline void append_uarfcns(struct bitvec *bv, const uint16_t *uarfcn_list, size_t *u_offset,
376 size_t uarfcn_length, uint16_t *scramble_list, uint8_t budget)
377{
378 int i, rem = budget - 7, st = *u_offset; /* account for constant bits right away */
379 uint16_t cu = uarfcn_list[*u_offset]; /* caller ensures that length is positive */
380
381 OSMO_ASSERT(budget <= SI2Q_MAX_LEN);
382
383 if (budget <= 7)
384 return;
385
386 /* 3G Neighbour Cell Description */
387 bitvec_set_bit(bv, 1);
388 /* No Index_Start_3G */
389 bitvec_set_bit(bv, 0);
390 /* No Absolute_Index_Start_EMR */
391 bitvec_set_bit(bv, 0);
392
393 /* UTRAN FDD Description */
394 bitvec_set_bit(bv, 1);
395 /* No Bandwidth_FDD */
396 bitvec_set_bit(bv, 0);
397
398 for (i = *u_offset; i <= uarfcn_length; i++)
399 if (uarfcn_list[i] != cu) { /* we've reached new UARFCN */
400 rem = try_adding_uarfcn(bv, scramble_list, uarfcn_length, u_offset, cu, i, st, rem);
401 if (rem < 0)
402 break;
403
404 if (i < uarfcn_length) {
405 cu = uarfcn_list[i];
406 st = i;
407 } else
408 break;
409 }
410
411 /* stop bit - end of Repeated UTRAN FDD Neighbour Cells */
412 bitvec_set_bit(bv, 0);
413
414 /* UTRAN TDD Description */
415 bitvec_set_bit(bv, 0);
416}
417
418static size_t si2q_earfcn_count(const struct osmo_earfcn_si2q *e)
419{
420 unsigned i, ret = 0;
421
422 if (!e)
423 return 0;
424
425 for (i = 0; i < e->length; i++)
426 if (e->arfcn[i] != OSMO_EARFCN_INVALID)
427 ret++;
428
429 return ret;
430}
431
432/* generate SI2quater rest octets: 3GPP TS 44.018 § 10.5.2.33b */
Harald Welte428d03b2019-05-28 18:46:20 +0200433int osmo_gsm48_rest_octets_si2quater_encode(uint8_t *data, uint8_t si2q_index, uint8_t si2q_count,
434 const uint16_t *uarfcn_list, size_t *u_offset,
435 size_t uarfcn_length, uint16_t *scramble_list,
436 struct osmo_earfcn_si2q *si2quater_neigh_list,
437 size_t *e_offset)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200438{
439 int rc;
440 struct bitvec bv;
441
442 if (si2q_count < si2q_index)
443 return -EINVAL;
444
445 bv.data = data;
446 bv.data_len = 20;
447 bitvec_zero(&bv);
448
449 /* BA_IND: Set to '0' as that's what we use for SI2xxx type,
450 * whereas '1' is used for SI5xxx type messages. The point here
451 * is to be able to correlate whether a given MS measurement
452 * report was using the neighbor cells advertised in SI2 or in
453 * SI5, as those two could very well be different */
454 bitvec_set_bit(&bv, 0);
455 /* 3G_BA_IND */
456 bitvec_set_bit(&bv, 1);
457 /* MP_CHANGE_MARK */
458 bitvec_set_bit(&bv, 0);
459
460 /* SI2quater_INDEX */
461 bitvec_set_uint(&bv, si2q_index, 4);
462 /* SI2quater_COUNT */
463 bitvec_set_uint(&bv, si2q_count, 4);
464
465 /* No Measurement_Parameters Description */
466 bitvec_set_bit(&bv, 0);
467 /* No GPRS_Real Time Difference Description */
468 bitvec_set_bit(&bv, 0);
469 /* No GPRS_BSIC Description */
470 bitvec_set_bit(&bv, 0);
471 /* No GPRS_REPORT PRIORITY Description */
472 bitvec_set_bit(&bv, 0);
473 /* No GPRS_MEASUREMENT_Parameters Description */
474 bitvec_set_bit(&bv, 0);
475 /* No NC Measurement Parameters */
476 bitvec_set_bit(&bv, 0);
477 /* No extension (length) */
478 bitvec_set_bit(&bv, 0);
479
480 rc = SI2Q_MAX_LEN - (bv.cur_bit + 3);
481 if (rc > 0 && uarfcn_length - *u_offset > 0)
482 append_uarfcns(&bv, uarfcn_list, u_offset, uarfcn_length, scramble_list, rc);
483 else /* No 3G Neighbour Cell Description */
484 bitvec_set_bit(&bv, 0);
485
486 /* No 3G Measurement Parameters Description */
487 bitvec_set_bit(&bv, 0);
488 /* No GPRS_3G_MEASUREMENT Parameters Descr. */
489 bitvec_set_bit(&bv, 0);
490
491 rc = SI2Q_MAX_LEN - bv.cur_bit;
492 if (rc > 0 && si2q_earfcn_count(si2quater_neigh_list) - *e_offset > 0)
493 append_earfcn(&bv, si2quater_neigh_list, e_offset, rc);
494 else /* No Additions in Rel-5: */
495 bitvec_set_bit(&bv, L);
496
497 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
498 return bv.data_len;
499}
500
501/* Append selection parameters to bitvec */
502static void append_selection_params(struct bitvec *bv,
503 const struct osmo_gsm48_si_selection_params *sp)
504{
505 if (sp->present) {
506 bitvec_set_bit(bv, H);
507 bitvec_set_bit(bv, sp->cbq);
508 bitvec_set_uint(bv, sp->cell_resel_off, 6);
509 bitvec_set_uint(bv, sp->temp_offs, 3);
510 bitvec_set_uint(bv, sp->penalty_time, 5);
511 } else
512 bitvec_set_bit(bv, L);
513}
514
515/* Append power offset to bitvec */
516static void append_power_offset(struct bitvec *bv,
517 const struct osmo_gsm48_si_power_offset *po)
518{
519 if (po->present) {
520 bitvec_set_bit(bv, H);
521 bitvec_set_uint(bv, po->power_offset, 2);
522 } else
523 bitvec_set_bit(bv, L);
524}
525
526/* Append GPRS indicator to bitvec */
527static void append_gprs_ind(struct bitvec *bv,
528 const struct osmo_gsm48_si3_gprs_ind *gi)
529{
530 if (gi->present) {
531 bitvec_set_bit(bv, H);
532 bitvec_set_uint(bv, gi->ra_colour, 3);
533 /* 0 == SI13 in BCCH Norm, 1 == SI13 sent on BCCH Ext */
534 bitvec_set_bit(bv, gi->si13_position);
535 } else
536 bitvec_set_bit(bv, L);
537}
538
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200539/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
Harald Welte428d03b2019-05-28 18:46:20 +0200540int osmo_gsm48_rest_octets_si3_encode(uint8_t *data, const struct osmo_gsm48_si_ro_info *si3)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200541{
542 struct bitvec bv;
543
544 memset(&bv, 0, sizeof(bv));
545 bv.data = data;
546 bv.data_len = 4;
547
548 /* Optional Selection Parameters */
549 append_selection_params(&bv, &si3->selection_params);
550
551 /* Optional Power Offset */
552 append_power_offset(&bv, &si3->power_offset);
553
554 /* Do we have a SI2ter on the BCCH? */
555 if (si3->si2ter_indicator)
556 bitvec_set_bit(&bv, H);
557 else
558 bitvec_set_bit(&bv, L);
559
560 /* Early Classmark Sending Control */
561 if (si3->early_cm_ctrl)
562 bitvec_set_bit(&bv, H);
563 else
564 bitvec_set_bit(&bv, L);
565
566 /* Do we have a SI Type 9 on the BCCH? */
567 if (si3->scheduling.present) {
568 bitvec_set_bit(&bv, H);
569 bitvec_set_uint(&bv, si3->scheduling.where, 3);
570 } else
571 bitvec_set_bit(&bv, L);
572
573 /* GPRS Indicator */
574 append_gprs_ind(&bv, &si3->gprs_ind);
575
576 /* 3G Early Classmark Sending Restriction. If H, then controlled by
577 * early_cm_ctrl above */
578 if (si3->early_cm_restrict_3g)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200579 bitvec_set_bit(&bv, H);
Vadim Yanitskiyc9eab822020-05-05 02:03:29 +0700580 else
581 bitvec_set_bit(&bv, L);
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200582
583 if (si3->si2quater_indicator) {
584 bitvec_set_bit(&bv, H); /* indicator struct present */
585 bitvec_set_uint(&bv, 0, 1); /* message is sent on BCCH Norm */
586 }
587
588 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
589 return bv.data_len;
590}
591
592static int append_lsa_params(struct bitvec *bv,
593 const struct osmo_gsm48_lsa_params *lsa_params)
594{
595 /* FIXME */
596 return -1;
597}
598
599/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
Harald Welte428d03b2019-05-28 18:46:20 +0200600int osmo_gsm48_rest_octets_si4_encode(uint8_t *data, const struct osmo_gsm48_si_ro_info *si4, int len)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200601{
602 struct bitvec bv;
603
604 memset(&bv, 0, sizeof(bv));
605 bv.data = data;
606 bv.data_len = len;
607
608 /* SI4 Rest Octets O */
609 append_selection_params(&bv, &si4->selection_params);
610 append_power_offset(&bv, &si4->power_offset);
611 append_gprs_ind(&bv, &si4->gprs_ind);
612
613 if (0 /* FIXME */) {
614 /* H and SI4 Rest Octets S */
615 bitvec_set_bit(&bv, H);
616
617 /* LSA Parameters */
618 if (si4->lsa_params.present) {
619 bitvec_set_bit(&bv, H);
620 append_lsa_params(&bv, &si4->lsa_params);
621 } else
622 bitvec_set_bit(&bv, L);
623
624 /* Cell Identity */
625 if (1) {
626 bitvec_set_bit(&bv, H);
627 bitvec_set_uint(&bv, si4->cell_id, 16);
628 } else
629 bitvec_set_bit(&bv, L);
630
631 /* LSA ID Information */
632 if (0) {
633 bitvec_set_bit(&bv, H);
634 /* FIXME */
635 } else
636 bitvec_set_bit(&bv, L);
637 } else {
638 /* L and break indicator */
639 bitvec_set_bit(&bv, L);
640 bitvec_set_bit(&bv, si4->break_ind ? H : L);
641 }
642
643 return bv.data_len;
644}
645
646
647/* GSM 04.18 ETSI TS 101 503 V8.27.0 (2006-05)
648
649<SI6 rest octets> ::=
650{L | H <PCH and NCH info>}
651{L | H <VBS/VGCS options : bit(2)>}
652{ < DTM_support : bit == L > I < DTM_support : bit == H >
653< RAC : bit (8) >
654< MAX_LAPDm : bit (3) > }
655< Band indicator >
656{ L | H < GPRS_MS_TXPWR_MAX_CCH : bit (5) > }
657<implicit spare >;
658*/
Harald Welte86156de2019-05-28 19:49:47 +0200659int osmo_gsm48_rest_octets_si6_encode(uint8_t *data, const struct osmo_gsm48_si6_ro_info *in)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200660{
661 struct bitvec bv;
662
663 memset(&bv, 0, sizeof(bv));
664 bv.data = data;
665 bv.data_len = 1;
666
Harald Welte86156de2019-05-28 19:49:47 +0200667 if (in->pch_nch_info.present) {
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200668 bitvec_set_bit(&bv, H);
Harald Welte86156de2019-05-28 19:49:47 +0200669 bitvec_set_bit(&bv, !!in->pch_nch_info.paging_channel_restructuring);
670 bitvec_set_uint(&bv, in->pch_nch_info.nln_sacch, 2);
671 if (in->pch_nch_info.call_priority_present) {
672 bitvec_set_bit(&bv, 1);
673 bitvec_set_uint(&bv, in->pch_nch_info.call_priority, 3);
674 } else
675 bitvec_set_bit(&bv, 0);
676 bitvec_set_bit(&bv, !!in->pch_nch_info.nln_status_sacch);
677 } else
678 bitvec_set_bit(&bv, L);
679
680 if (in->vbs_vgcs_options.present) {
681 bitvec_set_bit(&bv, H);
682 bitvec_set_bit(&bv, !!in->vbs_vgcs_options.inband_notifications);
683 bitvec_set_bit(&bv, !!in->vbs_vgcs_options.inband_pagings);
684 } else
685 bitvec_set_bit(&bv, L);
686
687 if (in->dtm_support.present) {
688 bitvec_set_bit(&bv, H);
689 bitvec_set_uint(&bv, in->dtm_support.rac, 8);
690 bitvec_set_uint(&bv, in->dtm_support.max_lapdm, 3);
691 } else
692 bitvec_set_bit(&bv, L);
693
694 if (in->band_indicator_1900)
695 bitvec_set_bit(&bv, H);
696 else
697 bitvec_set_bit(&bv, L);
698
699 if (in->gprs_ms_txpwr_max_ccch.present) {
700 bitvec_set_bit(&bv, H);
701 bitvec_set_uint(&bv, in->gprs_ms_txpwr_max_ccch.max_txpwr, 5);
702 } else
703 bitvec_set_bit(&bv, L);
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200704
705 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
706 return bv.data_len;
707}
708
709/* GPRS Mobile Allocation as per TS 04.60 Chapter 12.10a:
710 < GPRS Mobile Allocation IE > ::=
711 < HSN : bit (6) >
712 { 0 | 1 < RFL number list : < RFL number list struct > > }
713 { 0 < MA_LENGTH : bit (6) >
714 < MA_BITMAP: bit (val(MA_LENGTH) + 1) >
715 | 1 { 0 | 1 <ARFCN index list : < ARFCN index list struct > > } } ;
716
717 < RFL number list struct > :: =
718 < RFL_NUMBER : bit (4) >
719 { 0 | 1 < RFL number list struct > } ;
720 < ARFCN index list struct > ::=
721 < ARFCN_INDEX : bit(6) >
722 { 0 | 1 < ARFCN index list struct > } ;
723 */
724static int append_gprs_mobile_alloc(struct bitvec *bv)
725{
726 /* Hopping Sequence Number */
727 bitvec_set_uint(bv, 0, 6);
728
729 if (0) {
730 /* We want to use a RFL number list */
731 bitvec_set_bit(bv, 1);
732 /* FIXME: RFL number list */
733 } else
734 bitvec_set_bit(bv, 0);
735
736 if (0) {
737 /* We want to use a MA_BITMAP */
738 bitvec_set_bit(bv, 0);
739 /* FIXME: MA_LENGTH, MA_BITMAP, ... */
740 } else {
741 bitvec_set_bit(bv, 1);
742 if (0) {
743 /* We want to provide an ARFCN index list */
744 bitvec_set_bit(bv, 1);
745 /* FIXME */
746 } else
747 bitvec_set_bit(bv, 0);
748 }
749 return 0;
750}
751
752static int encode_t3192(unsigned int t3192)
753{
754 /* See also 3GPP TS 44.060
755 Table 12.24.2: GPRS Cell Options information element details */
756 if (t3192 == 0)
757 return 3;
758 else if (t3192 <= 80)
759 return 4;
760 else if (t3192 <= 120)
761 return 5;
762 else if (t3192 <= 160)
763 return 6;
764 else if (t3192 <= 200)
765 return 7;
766 else if (t3192 <= 500)
767 return 0;
768 else if (t3192 <= 1000)
769 return 1;
770 else if (t3192 <= 1500)
771 return 2;
772 else
773 return -EINVAL;
774}
775
776static int encode_drx_timer(unsigned int drx)
777{
778 if (drx == 0)
779 return 0;
780 else if (drx == 1)
781 return 1;
782 else if (drx == 2)
783 return 2;
784 else if (drx <= 4)
785 return 3;
786 else if (drx <= 8)
787 return 4;
788 else if (drx <= 16)
789 return 5;
790 else if (drx <= 32)
791 return 6;
792 else if (drx <= 64)
793 return 7;
794 else
795 return -EINVAL;
796}
797
798/* GPRS Cell Options as per TS 04.60 Chapter 12.24
799 < GPRS Cell Options IE > ::=
800 < NMO : bit(2) >
801 < T3168 : bit(3) >
802 < T3192 : bit(3) >
803 < DRX_TIMER_MAX: bit(3) >
804 < ACCESS_BURST_TYPE: bit >
805 < CONTROL_ACK_TYPE : bit >
806 < BS_CV_MAX: bit(4) >
807 { 0 | 1 < PAN_DEC : bit(3) >
808 < PAN_INC : bit(3) >
809 < PAN_MAX : bit(3) >
810 { 0 | 1 < Extension Length : bit(6) >
811 < bit (val(Extension Length) + 1
812 & { < Extension Information > ! { bit ** = <no string> } } ;
813 < Extension Information > ::=
814 { 0 | 1 < EGPRS_PACKET_CHANNEL_REQUEST : bit >
815 < BEP_PERIOD : bit(4) > }
816 < PFC_FEATURE_MODE : bit >
817 < DTM_SUPPORT : bit >
818 <BSS_PAGING_COORDINATION: bit >
819 <spare bit > ** ;
820 */
821static int append_gprs_cell_opt(struct bitvec *bv,
822 const struct osmo_gprs_cell_options *gco)
823{
824 int t3192, drx_timer_max;
825
826 t3192 = encode_t3192(gco->t3192);
827 if (t3192 < 0)
828 return t3192;
829
830 drx_timer_max = encode_drx_timer(gco->drx_timer_max);
831 if (drx_timer_max < 0)
832 return drx_timer_max;
833
834 bitvec_set_uint(bv, gco->nmo, 2);
835
836 /* See also 3GPP TS 44.060
837 Table 12.24.2: GPRS Cell Options information element details */
838 bitvec_set_uint(bv, gco->t3168 / 500 - 1, 3);
839
840 bitvec_set_uint(bv, t3192, 3);
841 bitvec_set_uint(bv, drx_timer_max, 3);
842 /* ACCESS_BURST_TYPE: Hard-code 8bit */
843 bitvec_set_bit(bv, 0);
844 /* CONTROL_ACK_TYPE: */
845 bitvec_set_bit(bv, gco->ctrl_ack_type_use_block);
846 bitvec_set_uint(bv, gco->bs_cv_max, 4);
847
848 if (0) {
849 /* hard-code no PAN_{DEC,INC,MAX} */
850 bitvec_set_bit(bv, 0);
851 } else {
852 /* copied from ip.access BSC protocol trace */
853 bitvec_set_bit(bv, 1);
854 bitvec_set_uint(bv, 1, 3); /* DEC */
855 bitvec_set_uint(bv, 1, 3); /* INC */
856 bitvec_set_uint(bv, 15, 3); /* MAX */
857 }
858
859 if (!gco->ext_info_present) {
860 /* no extension information */
861 bitvec_set_bit(bv, 0);
862 } else {
863 /* extension information */
864 bitvec_set_bit(bv, 1);
865 if (!gco->ext_info.egprs_supported) {
866 /* 6bit length of extension */
867 bitvec_set_uint(bv, (1 + 3)-1, 6);
868 /* EGPRS supported in the cell */
869 bitvec_set_bit(bv, 0);
870 } else {
871 /* 6bit length of extension */
872 bitvec_set_uint(bv, (1 + 5 + 3)-1, 6);
873 /* EGPRS supported in the cell */
874 bitvec_set_bit(bv, 1);
875
876 /* 1bit EGPRS PACKET CHANNEL REQUEST */
877 if (gco->supports_egprs_11bit_rach == 0) {
878 bitvec_set_bit(bv,
879 gco->ext_info.use_egprs_p_ch_req);
880 } else {
881 bitvec_set_bit(bv, 0);
882 }
883
884 /* 4bit BEP PERIOD */
885 bitvec_set_uint(bv, gco->ext_info.bep_period, 4);
886 }
887 bitvec_set_bit(bv, gco->ext_info.pfc_supported);
888 bitvec_set_bit(bv, gco->ext_info.dtm_supported);
889 bitvec_set_bit(bv, gco->ext_info.bss_paging_coordination);
890 }
891
892 return 0;
893}
894
895static void append_gprs_pwr_ctrl_pars(struct bitvec *bv,
896 const struct osmo_gprs_power_ctrl_pars *pcp)
897{
898 bitvec_set_uint(bv, pcp->alpha, 4);
899 bitvec_set_uint(bv, pcp->t_avg_w, 5);
900 bitvec_set_uint(bv, pcp->t_avg_t, 5);
901 bitvec_set_uint(bv, pcp->pc_meas_chan, 1);
902 bitvec_set_uint(bv, pcp->n_avg_i, 4);
903}
904
905/* Generate SI13 Rest Octests (04.08 Chapter 10.5.2.37b) */
Harald Welte428d03b2019-05-28 18:46:20 +0200906int osmo_gsm48_rest_octets_si13_encode(uint8_t *data, const struct osmo_gsm48_si13_info *si13)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200907{
908 struct bitvec bv;
909
910 memset(&bv, 0, sizeof(bv));
911 bv.data = data;
912 bv.data_len = 20;
913
914 if (0) {
915 /* No rest octets */
916 bitvec_set_bit(&bv, L);
917 } else {
918 bitvec_set_bit(&bv, H);
919 bitvec_set_uint(&bv, si13->bcch_change_mark, 3);
920 bitvec_set_uint(&bv, si13->si_change_field, 4);
921 if (1) {
922 bitvec_set_bit(&bv, 0);
923 } else {
924 bitvec_set_bit(&bv, 1);
925 bitvec_set_uint(&bv, si13->bcch_change_mark, 2);
926 append_gprs_mobile_alloc(&bv);
927 }
928 /* PBCCH not present in cell:
929 it shall never be indicated according to 3GPP TS 44.018 Table 10.5.2.37b.1 */
930 bitvec_set_bit(&bv, 0);
931 bitvec_set_uint(&bv, si13->rac, 8);
932 bitvec_set_bit(&bv, si13->spgc_ccch_sup);
933 bitvec_set_uint(&bv, si13->prio_acc_thr, 3);
934 bitvec_set_uint(&bv, si13->net_ctrl_ord, 2);
935 append_gprs_cell_opt(&bv, &si13->cell_opts);
936 append_gprs_pwr_ctrl_pars(&bv, &si13->pwr_ctrl_pars);
937
938 /* 3GPP TS 44.018 Release 6 / 10.5.2.37b */
939 bitvec_set_bit(&bv, H); /* added Release 99 */
940 /* claim our SGSN is compatible with Release 99, as EDGE and EGPRS
941 * was only added in this Release */
942 bitvec_set_bit(&bv, 1);
943 }
944 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
945 return bv.data_len;
946}
Harald Weltef85b33f2019-05-28 17:56:21 +0200947
948
949/***********************************************************************
950 * Decoder
951 ***********************************************************************/
952
953/*! Decode SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72).
954 * \param[out] si3 decoded SI3 rest octets
955 * \param[in] encoded SI3 rest octets, 4 octets long */
956void osmo_gsm48_rest_octets_si3_decode(struct osmo_gsm48_si_ro_info *si3, const uint8_t *data)
957{
958 struct osmo_gsm48_si_selection_params *sp = &si3->selection_params;
959 struct osmo_gsm48_si_power_offset *po = &si3->power_offset;
960 struct osmo_gsm48_si3_gprs_ind *gi = &si3->gprs_ind;
961 struct bitvec bv;
962
963 memset(&bv, 0, sizeof(bv));
964 bv.data = (uint8_t *) data;
965 bv.data_len = 4;
966
967 memset(si3, 0, sizeof(*si3));
968
Vadim Yanitskiy74474cf2019-07-17 16:40:11 +0700969 /* Optional Selection Parameters */
Harald Weltef85b33f2019-05-28 17:56:21 +0200970 if (bitvec_get_bit_high(&bv) == H) {
971 sp->present = 1;
972 sp->cbq = bitvec_get_uint(&bv, 1);
973 sp->cell_resel_off = bitvec_get_uint(&bv, 6);
974 sp->temp_offs = bitvec_get_uint(&bv, 3);
975 sp->penalty_time = bitvec_get_uint(&bv, 5);
976 } else
977 sp->present = 0;
978
979 /* Optional Power Offset */
980 if (bitvec_get_bit_high(&bv) == H) {
981 po->present = 1;
982 po->power_offset = bitvec_get_uint(&bv, 2);
983 } else
984 po->present = 0;
985
986 /* System Information 2ter Indicator */
987 if (bitvec_get_bit_high(&bv) == H)
988 si3->si2ter_indicator = 1;
989 else
990 si3->si2ter_indicator = 0;
991
992 /* Early Classmark Sending Control */
993 if (bitvec_get_bit_high(&bv) == H)
994 si3->early_cm_ctrl = 1;
995 else
996 si3->early_cm_ctrl = 0;
997
998 /* Scheduling if and where */
999 if (bitvec_get_bit_high(&bv) == H) {
1000 si3->scheduling.present = 1;
1001 si3->scheduling.where = bitvec_get_uint(&bv, 3);
1002 } else
1003 si3->scheduling.present = 0;
1004
1005 /* GPRS Indicator */
1006 if (bitvec_get_bit_high(&bv) == H) {
1007 gi->present = 1;
1008 gi->ra_colour = bitvec_get_uint(&bv, 3);
1009 gi->si13_position = bitvec_get_uint(&bv, 1);
1010 } else
1011 gi->present = 0;
1012
1013 /* 3G Early Classmark Sending Restriction. If H, then controlled by
1014 * early_cm_ctrl above */
1015 if (bitvec_get_bit_high(&bv) == H)
1016 si3->early_cm_restrict_3g = 1;
1017 else
1018 si3->early_cm_restrict_3g = 0;
1019
1020 if (bitvec_get_bit_high(&bv) == H)
1021 si3->si2quater_indicator = 1;
1022 else
1023 si3->si2quater_indicator = 0;
1024}