blob: c1e4b308ad79ea0eaafa887424ec668bc434e695 [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
539/* Generate SI2ter Rest Octests 3GPP TS 44.018 Table 10.5.2.33a.1 */
Harald Welte428d03b2019-05-28 18:46:20 +0200540int osmo_gsm48_rest_octets_si2ter_encode(uint8_t *data)
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 /* No SI2ter_MP_CHANGE_MARK */
549 bitvec_set_bit(&bv, L);
550
551 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
552
553 return bv.data_len;
554}
555
556/* Generate SI2bis Rest Octests 3GPP TS 44.018 Table 10.5.2.33.1 */
Harald Welte428d03b2019-05-28 18:46:20 +0200557int osmo_gsm48_rest_octets_si2bis_encode(uint8_t *data)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200558{
559 struct bitvec bv;
560
561 memset(&bv, 0, sizeof(bv));
562 bv.data = data;
563 bv.data_len = 1;
564
565 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
566
567 return bv.data_len;
568}
569
570/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
Harald Welte428d03b2019-05-28 18:46:20 +0200571int osmo_gsm48_rest_octets_si3_encode(uint8_t *data, const struct osmo_gsm48_si_ro_info *si3)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200572{
573 struct bitvec bv;
574
575 memset(&bv, 0, sizeof(bv));
576 bv.data = data;
577 bv.data_len = 4;
578
579 /* Optional Selection Parameters */
580 append_selection_params(&bv, &si3->selection_params);
581
582 /* Optional Power Offset */
583 append_power_offset(&bv, &si3->power_offset);
584
585 /* Do we have a SI2ter on the BCCH? */
586 if (si3->si2ter_indicator)
587 bitvec_set_bit(&bv, H);
588 else
589 bitvec_set_bit(&bv, L);
590
591 /* Early Classmark Sending Control */
592 if (si3->early_cm_ctrl)
593 bitvec_set_bit(&bv, H);
594 else
595 bitvec_set_bit(&bv, L);
596
597 /* Do we have a SI Type 9 on the BCCH? */
598 if (si3->scheduling.present) {
599 bitvec_set_bit(&bv, H);
600 bitvec_set_uint(&bv, si3->scheduling.where, 3);
601 } else
602 bitvec_set_bit(&bv, L);
603
604 /* GPRS Indicator */
605 append_gprs_ind(&bv, &si3->gprs_ind);
606
607 /* 3G Early Classmark Sending Restriction. If H, then controlled by
608 * early_cm_ctrl above */
609 if (si3->early_cm_restrict_3g)
610 bitvec_set_bit(&bv, L);
611 else
612 bitvec_set_bit(&bv, H);
613
614 if (si3->si2quater_indicator) {
615 bitvec_set_bit(&bv, H); /* indicator struct present */
616 bitvec_set_uint(&bv, 0, 1); /* message is sent on BCCH Norm */
617 }
618
619 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
620 return bv.data_len;
621}
622
623static int append_lsa_params(struct bitvec *bv,
624 const struct osmo_gsm48_lsa_params *lsa_params)
625{
626 /* FIXME */
627 return -1;
628}
629
630/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
Harald Welte428d03b2019-05-28 18:46:20 +0200631int 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 +0200632{
633 struct bitvec bv;
634
635 memset(&bv, 0, sizeof(bv));
636 bv.data = data;
637 bv.data_len = len;
638
639 /* SI4 Rest Octets O */
640 append_selection_params(&bv, &si4->selection_params);
641 append_power_offset(&bv, &si4->power_offset);
642 append_gprs_ind(&bv, &si4->gprs_ind);
643
644 if (0 /* FIXME */) {
645 /* H and SI4 Rest Octets S */
646 bitvec_set_bit(&bv, H);
647
648 /* LSA Parameters */
649 if (si4->lsa_params.present) {
650 bitvec_set_bit(&bv, H);
651 append_lsa_params(&bv, &si4->lsa_params);
652 } else
653 bitvec_set_bit(&bv, L);
654
655 /* Cell Identity */
656 if (1) {
657 bitvec_set_bit(&bv, H);
658 bitvec_set_uint(&bv, si4->cell_id, 16);
659 } else
660 bitvec_set_bit(&bv, L);
661
662 /* LSA ID Information */
663 if (0) {
664 bitvec_set_bit(&bv, H);
665 /* FIXME */
666 } else
667 bitvec_set_bit(&bv, L);
668 } else {
669 /* L and break indicator */
670 bitvec_set_bit(&bv, L);
671 bitvec_set_bit(&bv, si4->break_ind ? H : L);
672 }
673
674 return bv.data_len;
675}
676
677
678/* GSM 04.18 ETSI TS 101 503 V8.27.0 (2006-05)
679
680<SI6 rest octets> ::=
681{L | H <PCH and NCH info>}
682{L | H <VBS/VGCS options : bit(2)>}
683{ < DTM_support : bit == L > I < DTM_support : bit == H >
684< RAC : bit (8) >
685< MAX_LAPDm : bit (3) > }
686< Band indicator >
687{ L | H < GPRS_MS_TXPWR_MAX_CCH : bit (5) > }
688<implicit spare >;
689*/
Harald Welte86156de2019-05-28 19:49:47 +0200690int osmo_gsm48_rest_octets_si6_encode(uint8_t *data, const struct osmo_gsm48_si6_ro_info *in)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200691{
692 struct bitvec bv;
693
694 memset(&bv, 0, sizeof(bv));
695 bv.data = data;
696 bv.data_len = 1;
697
Harald Welte86156de2019-05-28 19:49:47 +0200698 if (in->pch_nch_info.present) {
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200699 bitvec_set_bit(&bv, H);
Harald Welte86156de2019-05-28 19:49:47 +0200700 bitvec_set_bit(&bv, !!in->pch_nch_info.paging_channel_restructuring);
701 bitvec_set_uint(&bv, in->pch_nch_info.nln_sacch, 2);
702 if (in->pch_nch_info.call_priority_present) {
703 bitvec_set_bit(&bv, 1);
704 bitvec_set_uint(&bv, in->pch_nch_info.call_priority, 3);
705 } else
706 bitvec_set_bit(&bv, 0);
707 bitvec_set_bit(&bv, !!in->pch_nch_info.nln_status_sacch);
708 } else
709 bitvec_set_bit(&bv, L);
710
711 if (in->vbs_vgcs_options.present) {
712 bitvec_set_bit(&bv, H);
713 bitvec_set_bit(&bv, !!in->vbs_vgcs_options.inband_notifications);
714 bitvec_set_bit(&bv, !!in->vbs_vgcs_options.inband_pagings);
715 } else
716 bitvec_set_bit(&bv, L);
717
718 if (in->dtm_support.present) {
719 bitvec_set_bit(&bv, H);
720 bitvec_set_uint(&bv, in->dtm_support.rac, 8);
721 bitvec_set_uint(&bv, in->dtm_support.max_lapdm, 3);
722 } else
723 bitvec_set_bit(&bv, L);
724
725 if (in->band_indicator_1900)
726 bitvec_set_bit(&bv, H);
727 else
728 bitvec_set_bit(&bv, L);
729
730 if (in->gprs_ms_txpwr_max_ccch.present) {
731 bitvec_set_bit(&bv, H);
732 bitvec_set_uint(&bv, in->gprs_ms_txpwr_max_ccch.max_txpwr, 5);
733 } else
734 bitvec_set_bit(&bv, L);
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200735
736 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
737 return bv.data_len;
738}
739
740/* GPRS Mobile Allocation as per TS 04.60 Chapter 12.10a:
741 < GPRS Mobile Allocation IE > ::=
742 < HSN : bit (6) >
743 { 0 | 1 < RFL number list : < RFL number list struct > > }
744 { 0 < MA_LENGTH : bit (6) >
745 < MA_BITMAP: bit (val(MA_LENGTH) + 1) >
746 | 1 { 0 | 1 <ARFCN index list : < ARFCN index list struct > > } } ;
747
748 < RFL number list struct > :: =
749 < RFL_NUMBER : bit (4) >
750 { 0 | 1 < RFL number list struct > } ;
751 < ARFCN index list struct > ::=
752 < ARFCN_INDEX : bit(6) >
753 { 0 | 1 < ARFCN index list struct > } ;
754 */
755static int append_gprs_mobile_alloc(struct bitvec *bv)
756{
757 /* Hopping Sequence Number */
758 bitvec_set_uint(bv, 0, 6);
759
760 if (0) {
761 /* We want to use a RFL number list */
762 bitvec_set_bit(bv, 1);
763 /* FIXME: RFL number list */
764 } else
765 bitvec_set_bit(bv, 0);
766
767 if (0) {
768 /* We want to use a MA_BITMAP */
769 bitvec_set_bit(bv, 0);
770 /* FIXME: MA_LENGTH, MA_BITMAP, ... */
771 } else {
772 bitvec_set_bit(bv, 1);
773 if (0) {
774 /* We want to provide an ARFCN index list */
775 bitvec_set_bit(bv, 1);
776 /* FIXME */
777 } else
778 bitvec_set_bit(bv, 0);
779 }
780 return 0;
781}
782
783static int encode_t3192(unsigned int t3192)
784{
785 /* See also 3GPP TS 44.060
786 Table 12.24.2: GPRS Cell Options information element details */
787 if (t3192 == 0)
788 return 3;
789 else if (t3192 <= 80)
790 return 4;
791 else if (t3192 <= 120)
792 return 5;
793 else if (t3192 <= 160)
794 return 6;
795 else if (t3192 <= 200)
796 return 7;
797 else if (t3192 <= 500)
798 return 0;
799 else if (t3192 <= 1000)
800 return 1;
801 else if (t3192 <= 1500)
802 return 2;
803 else
804 return -EINVAL;
805}
806
807static int encode_drx_timer(unsigned int drx)
808{
809 if (drx == 0)
810 return 0;
811 else if (drx == 1)
812 return 1;
813 else if (drx == 2)
814 return 2;
815 else if (drx <= 4)
816 return 3;
817 else if (drx <= 8)
818 return 4;
819 else if (drx <= 16)
820 return 5;
821 else if (drx <= 32)
822 return 6;
823 else if (drx <= 64)
824 return 7;
825 else
826 return -EINVAL;
827}
828
829/* GPRS Cell Options as per TS 04.60 Chapter 12.24
830 < GPRS Cell Options IE > ::=
831 < NMO : bit(2) >
832 < T3168 : bit(3) >
833 < T3192 : bit(3) >
834 < DRX_TIMER_MAX: bit(3) >
835 < ACCESS_BURST_TYPE: bit >
836 < CONTROL_ACK_TYPE : bit >
837 < BS_CV_MAX: bit(4) >
838 { 0 | 1 < PAN_DEC : bit(3) >
839 < PAN_INC : bit(3) >
840 < PAN_MAX : bit(3) >
841 { 0 | 1 < Extension Length : bit(6) >
842 < bit (val(Extension Length) + 1
843 & { < Extension Information > ! { bit ** = <no string> } } ;
844 < Extension Information > ::=
845 { 0 | 1 < EGPRS_PACKET_CHANNEL_REQUEST : bit >
846 < BEP_PERIOD : bit(4) > }
847 < PFC_FEATURE_MODE : bit >
848 < DTM_SUPPORT : bit >
849 <BSS_PAGING_COORDINATION: bit >
850 <spare bit > ** ;
851 */
852static int append_gprs_cell_opt(struct bitvec *bv,
853 const struct osmo_gprs_cell_options *gco)
854{
855 int t3192, drx_timer_max;
856
857 t3192 = encode_t3192(gco->t3192);
858 if (t3192 < 0)
859 return t3192;
860
861 drx_timer_max = encode_drx_timer(gco->drx_timer_max);
862 if (drx_timer_max < 0)
863 return drx_timer_max;
864
865 bitvec_set_uint(bv, gco->nmo, 2);
866
867 /* See also 3GPP TS 44.060
868 Table 12.24.2: GPRS Cell Options information element details */
869 bitvec_set_uint(bv, gco->t3168 / 500 - 1, 3);
870
871 bitvec_set_uint(bv, t3192, 3);
872 bitvec_set_uint(bv, drx_timer_max, 3);
873 /* ACCESS_BURST_TYPE: Hard-code 8bit */
874 bitvec_set_bit(bv, 0);
875 /* CONTROL_ACK_TYPE: */
876 bitvec_set_bit(bv, gco->ctrl_ack_type_use_block);
877 bitvec_set_uint(bv, gco->bs_cv_max, 4);
878
879 if (0) {
880 /* hard-code no PAN_{DEC,INC,MAX} */
881 bitvec_set_bit(bv, 0);
882 } else {
883 /* copied from ip.access BSC protocol trace */
884 bitvec_set_bit(bv, 1);
885 bitvec_set_uint(bv, 1, 3); /* DEC */
886 bitvec_set_uint(bv, 1, 3); /* INC */
887 bitvec_set_uint(bv, 15, 3); /* MAX */
888 }
889
890 if (!gco->ext_info_present) {
891 /* no extension information */
892 bitvec_set_bit(bv, 0);
893 } else {
894 /* extension information */
895 bitvec_set_bit(bv, 1);
896 if (!gco->ext_info.egprs_supported) {
897 /* 6bit length of extension */
898 bitvec_set_uint(bv, (1 + 3)-1, 6);
899 /* EGPRS supported in the cell */
900 bitvec_set_bit(bv, 0);
901 } else {
902 /* 6bit length of extension */
903 bitvec_set_uint(bv, (1 + 5 + 3)-1, 6);
904 /* EGPRS supported in the cell */
905 bitvec_set_bit(bv, 1);
906
907 /* 1bit EGPRS PACKET CHANNEL REQUEST */
908 if (gco->supports_egprs_11bit_rach == 0) {
909 bitvec_set_bit(bv,
910 gco->ext_info.use_egprs_p_ch_req);
911 } else {
912 bitvec_set_bit(bv, 0);
913 }
914
915 /* 4bit BEP PERIOD */
916 bitvec_set_uint(bv, gco->ext_info.bep_period, 4);
917 }
918 bitvec_set_bit(bv, gco->ext_info.pfc_supported);
919 bitvec_set_bit(bv, gco->ext_info.dtm_supported);
920 bitvec_set_bit(bv, gco->ext_info.bss_paging_coordination);
921 }
922
923 return 0;
924}
925
926static void append_gprs_pwr_ctrl_pars(struct bitvec *bv,
927 const struct osmo_gprs_power_ctrl_pars *pcp)
928{
929 bitvec_set_uint(bv, pcp->alpha, 4);
930 bitvec_set_uint(bv, pcp->t_avg_w, 5);
931 bitvec_set_uint(bv, pcp->t_avg_t, 5);
932 bitvec_set_uint(bv, pcp->pc_meas_chan, 1);
933 bitvec_set_uint(bv, pcp->n_avg_i, 4);
934}
935
936/* Generate SI13 Rest Octests (04.08 Chapter 10.5.2.37b) */
Harald Welte428d03b2019-05-28 18:46:20 +0200937int osmo_gsm48_rest_octets_si13_encode(uint8_t *data, const struct osmo_gsm48_si13_info *si13)
Stefan Sperlingbd6e7a92018-07-27 15:17:09 +0200938{
939 struct bitvec bv;
940
941 memset(&bv, 0, sizeof(bv));
942 bv.data = data;
943 bv.data_len = 20;
944
945 if (0) {
946 /* No rest octets */
947 bitvec_set_bit(&bv, L);
948 } else {
949 bitvec_set_bit(&bv, H);
950 bitvec_set_uint(&bv, si13->bcch_change_mark, 3);
951 bitvec_set_uint(&bv, si13->si_change_field, 4);
952 if (1) {
953 bitvec_set_bit(&bv, 0);
954 } else {
955 bitvec_set_bit(&bv, 1);
956 bitvec_set_uint(&bv, si13->bcch_change_mark, 2);
957 append_gprs_mobile_alloc(&bv);
958 }
959 /* PBCCH not present in cell:
960 it shall never be indicated according to 3GPP TS 44.018 Table 10.5.2.37b.1 */
961 bitvec_set_bit(&bv, 0);
962 bitvec_set_uint(&bv, si13->rac, 8);
963 bitvec_set_bit(&bv, si13->spgc_ccch_sup);
964 bitvec_set_uint(&bv, si13->prio_acc_thr, 3);
965 bitvec_set_uint(&bv, si13->net_ctrl_ord, 2);
966 append_gprs_cell_opt(&bv, &si13->cell_opts);
967 append_gprs_pwr_ctrl_pars(&bv, &si13->pwr_ctrl_pars);
968
969 /* 3GPP TS 44.018 Release 6 / 10.5.2.37b */
970 bitvec_set_bit(&bv, H); /* added Release 99 */
971 /* claim our SGSN is compatible with Release 99, as EDGE and EGPRS
972 * was only added in this Release */
973 bitvec_set_bit(&bv, 1);
974 }
975 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
976 return bv.data_len;
977}
Harald Weltef85b33f2019-05-28 17:56:21 +0200978
979
980/***********************************************************************
981 * Decoder
982 ***********************************************************************/
983
984/*! Decode SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72).
985 * \param[out] si3 decoded SI3 rest octets
986 * \param[in] encoded SI3 rest octets, 4 octets long */
987void osmo_gsm48_rest_octets_si3_decode(struct osmo_gsm48_si_ro_info *si3, const uint8_t *data)
988{
989 struct osmo_gsm48_si_selection_params *sp = &si3->selection_params;
990 struct osmo_gsm48_si_power_offset *po = &si3->power_offset;
991 struct osmo_gsm48_si3_gprs_ind *gi = &si3->gprs_ind;
992 struct bitvec bv;
993
994 memset(&bv, 0, sizeof(bv));
995 bv.data = (uint8_t *) data;
996 bv.data_len = 4;
997
998 memset(si3, 0, sizeof(*si3));
999
1000 /* Optional Selection Paraosmo_gsm48_si_ro_infometers */
1001 if (bitvec_get_bit_high(&bv) == H) {
1002 sp->present = 1;
1003 sp->cbq = bitvec_get_uint(&bv, 1);
1004 sp->cell_resel_off = bitvec_get_uint(&bv, 6);
1005 sp->temp_offs = bitvec_get_uint(&bv, 3);
1006 sp->penalty_time = bitvec_get_uint(&bv, 5);
1007 } else
1008 sp->present = 0;
1009
1010 /* Optional Power Offset */
1011 if (bitvec_get_bit_high(&bv) == H) {
1012 po->present = 1;
1013 po->power_offset = bitvec_get_uint(&bv, 2);
1014 } else
1015 po->present = 0;
1016
1017 /* System Information 2ter Indicator */
1018 if (bitvec_get_bit_high(&bv) == H)
1019 si3->si2ter_indicator = 1;
1020 else
1021 si3->si2ter_indicator = 0;
1022
1023 /* Early Classmark Sending Control */
1024 if (bitvec_get_bit_high(&bv) == H)
1025 si3->early_cm_ctrl = 1;
1026 else
1027 si3->early_cm_ctrl = 0;
1028
1029 /* Scheduling if and where */
1030 if (bitvec_get_bit_high(&bv) == H) {
1031 si3->scheduling.present = 1;
1032 si3->scheduling.where = bitvec_get_uint(&bv, 3);
1033 } else
1034 si3->scheduling.present = 0;
1035
1036 /* GPRS Indicator */
1037 if (bitvec_get_bit_high(&bv) == H) {
1038 gi->present = 1;
1039 gi->ra_colour = bitvec_get_uint(&bv, 3);
1040 gi->si13_position = bitvec_get_uint(&bv, 1);
1041 } else
1042 gi->present = 0;
1043
1044 /* 3G Early Classmark Sending Restriction. If H, then controlled by
1045 * early_cm_ctrl above */
1046 if (bitvec_get_bit_high(&bv) == H)
1047 si3->early_cm_restrict_3g = 1;
1048 else
1049 si3->early_cm_restrict_3g = 0;
1050
1051 if (bitvec_get_bit_high(&bv) == H)
1052 si3->si2quater_indicator = 1;
1053 else
1054 si3->si2quater_indicator = 0;
1055}