blob: 0171fd92ec29c7632ac50df298f2ed33aea4cca2 [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 */
38int osmo_gsm48_rest_octets_si1(uint8_t *data, uint8_t *nch_pos, int is1800_net)
39{
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 */
433int osmo_gsm48_rest_octets_si2quater(uint8_t *data, uint8_t si2q_index, uint8_t si2q_count, const uint16_t *uarfcn_list,
434 size_t *u_offset, size_t uarfcn_length, uint16_t *scramble_list,
435 struct osmo_earfcn_si2q *si2quater_neigh_list, size_t *e_offset)
436{
437 int rc;
438 struct bitvec bv;
439
440 if (si2q_count < si2q_index)
441 return -EINVAL;
442
443 bv.data = data;
444 bv.data_len = 20;
445 bitvec_zero(&bv);
446
447 /* BA_IND: Set to '0' as that's what we use for SI2xxx type,
448 * whereas '1' is used for SI5xxx type messages. The point here
449 * is to be able to correlate whether a given MS measurement
450 * report was using the neighbor cells advertised in SI2 or in
451 * SI5, as those two could very well be different */
452 bitvec_set_bit(&bv, 0);
453 /* 3G_BA_IND */
454 bitvec_set_bit(&bv, 1);
455 /* MP_CHANGE_MARK */
456 bitvec_set_bit(&bv, 0);
457
458 /* SI2quater_INDEX */
459 bitvec_set_uint(&bv, si2q_index, 4);
460 /* SI2quater_COUNT */
461 bitvec_set_uint(&bv, si2q_count, 4);
462
463 /* No Measurement_Parameters Description */
464 bitvec_set_bit(&bv, 0);
465 /* No GPRS_Real Time Difference Description */
466 bitvec_set_bit(&bv, 0);
467 /* No GPRS_BSIC Description */
468 bitvec_set_bit(&bv, 0);
469 /* No GPRS_REPORT PRIORITY Description */
470 bitvec_set_bit(&bv, 0);
471 /* No GPRS_MEASUREMENT_Parameters Description */
472 bitvec_set_bit(&bv, 0);
473 /* No NC Measurement Parameters */
474 bitvec_set_bit(&bv, 0);
475 /* No extension (length) */
476 bitvec_set_bit(&bv, 0);
477
478 rc = SI2Q_MAX_LEN - (bv.cur_bit + 3);
479 if (rc > 0 && uarfcn_length - *u_offset > 0)
480 append_uarfcns(&bv, uarfcn_list, u_offset, uarfcn_length, scramble_list, rc);
481 else /* No 3G Neighbour Cell Description */
482 bitvec_set_bit(&bv, 0);
483
484 /* No 3G Measurement Parameters Description */
485 bitvec_set_bit(&bv, 0);
486 /* No GPRS_3G_MEASUREMENT Parameters Descr. */
487 bitvec_set_bit(&bv, 0);
488
489 rc = SI2Q_MAX_LEN - bv.cur_bit;
490 if (rc > 0 && si2q_earfcn_count(si2quater_neigh_list) - *e_offset > 0)
491 append_earfcn(&bv, si2quater_neigh_list, e_offset, rc);
492 else /* No Additions in Rel-5: */
493 bitvec_set_bit(&bv, L);
494
495 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
496 return bv.data_len;
497}
498
499/* Append selection parameters to bitvec */
500static void append_selection_params(struct bitvec *bv,
501 const struct osmo_gsm48_si_selection_params *sp)
502{
503 if (sp->present) {
504 bitvec_set_bit(bv, H);
505 bitvec_set_bit(bv, sp->cbq);
506 bitvec_set_uint(bv, sp->cell_resel_off, 6);
507 bitvec_set_uint(bv, sp->temp_offs, 3);
508 bitvec_set_uint(bv, sp->penalty_time, 5);
509 } else
510 bitvec_set_bit(bv, L);
511}
512
513/* Append power offset to bitvec */
514static void append_power_offset(struct bitvec *bv,
515 const struct osmo_gsm48_si_power_offset *po)
516{
517 if (po->present) {
518 bitvec_set_bit(bv, H);
519 bitvec_set_uint(bv, po->power_offset, 2);
520 } else
521 bitvec_set_bit(bv, L);
522}
523
524/* Append GPRS indicator to bitvec */
525static void append_gprs_ind(struct bitvec *bv,
526 const struct osmo_gsm48_si3_gprs_ind *gi)
527{
528 if (gi->present) {
529 bitvec_set_bit(bv, H);
530 bitvec_set_uint(bv, gi->ra_colour, 3);
531 /* 0 == SI13 in BCCH Norm, 1 == SI13 sent on BCCH Ext */
532 bitvec_set_bit(bv, gi->si13_position);
533 } else
534 bitvec_set_bit(bv, L);
535}
536
537/* Generate SI2ter Rest Octests 3GPP TS 44.018 Table 10.5.2.33a.1 */
538int osmo_gsm48_rest_octets_si2ter(uint8_t *data)
539{
540 struct bitvec bv;
541
542 memset(&bv, 0, sizeof(bv));
543 bv.data = data;
544 bv.data_len = 4;
545
546 /* No SI2ter_MP_CHANGE_MARK */
547 bitvec_set_bit(&bv, L);
548
549 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
550
551 return bv.data_len;
552}
553
554/* Generate SI2bis Rest Octests 3GPP TS 44.018 Table 10.5.2.33.1 */
555int osmo_gsm48_rest_octets_si2bis(uint8_t *data)
556{
557 struct bitvec bv;
558
559 memset(&bv, 0, sizeof(bv));
560 bv.data = data;
561 bv.data_len = 1;
562
563 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
564
565 return bv.data_len;
566}
567
568/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
569int osmo_gsm48_rest_octets_si3(uint8_t *data, const struct osmo_gsm48_si_ro_info *si3)
570{
571 struct bitvec bv;
572
573 memset(&bv, 0, sizeof(bv));
574 bv.data = data;
575 bv.data_len = 4;
576
577 /* Optional Selection Parameters */
578 append_selection_params(&bv, &si3->selection_params);
579
580 /* Optional Power Offset */
581 append_power_offset(&bv, &si3->power_offset);
582
583 /* Do we have a SI2ter on the BCCH? */
584 if (si3->si2ter_indicator)
585 bitvec_set_bit(&bv, H);
586 else
587 bitvec_set_bit(&bv, L);
588
589 /* Early Classmark Sending Control */
590 if (si3->early_cm_ctrl)
591 bitvec_set_bit(&bv, H);
592 else
593 bitvec_set_bit(&bv, L);
594
595 /* Do we have a SI Type 9 on the BCCH? */
596 if (si3->scheduling.present) {
597 bitvec_set_bit(&bv, H);
598 bitvec_set_uint(&bv, si3->scheduling.where, 3);
599 } else
600 bitvec_set_bit(&bv, L);
601
602 /* GPRS Indicator */
603 append_gprs_ind(&bv, &si3->gprs_ind);
604
605 /* 3G Early Classmark Sending Restriction. If H, then controlled by
606 * early_cm_ctrl above */
607 if (si3->early_cm_restrict_3g)
608 bitvec_set_bit(&bv, L);
609 else
610 bitvec_set_bit(&bv, H);
611
612 if (si3->si2quater_indicator) {
613 bitvec_set_bit(&bv, H); /* indicator struct present */
614 bitvec_set_uint(&bv, 0, 1); /* message is sent on BCCH Norm */
615 }
616
617 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
618 return bv.data_len;
619}
620
621static int append_lsa_params(struct bitvec *bv,
622 const struct osmo_gsm48_lsa_params *lsa_params)
623{
624 /* FIXME */
625 return -1;
626}
627
628/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
629int osmo_gsm48_rest_octets_si4(uint8_t *data, const struct osmo_gsm48_si_ro_info *si4, int len)
630{
631 struct bitvec bv;
632
633 memset(&bv, 0, sizeof(bv));
634 bv.data = data;
635 bv.data_len = len;
636
637 /* SI4 Rest Octets O */
638 append_selection_params(&bv, &si4->selection_params);
639 append_power_offset(&bv, &si4->power_offset);
640 append_gprs_ind(&bv, &si4->gprs_ind);
641
642 if (0 /* FIXME */) {
643 /* H and SI4 Rest Octets S */
644 bitvec_set_bit(&bv, H);
645
646 /* LSA Parameters */
647 if (si4->lsa_params.present) {
648 bitvec_set_bit(&bv, H);
649 append_lsa_params(&bv, &si4->lsa_params);
650 } else
651 bitvec_set_bit(&bv, L);
652
653 /* Cell Identity */
654 if (1) {
655 bitvec_set_bit(&bv, H);
656 bitvec_set_uint(&bv, si4->cell_id, 16);
657 } else
658 bitvec_set_bit(&bv, L);
659
660 /* LSA ID Information */
661 if (0) {
662 bitvec_set_bit(&bv, H);
663 /* FIXME */
664 } else
665 bitvec_set_bit(&bv, L);
666 } else {
667 /* L and break indicator */
668 bitvec_set_bit(&bv, L);
669 bitvec_set_bit(&bv, si4->break_ind ? H : L);
670 }
671
672 return bv.data_len;
673}
674
675
676/* GSM 04.18 ETSI TS 101 503 V8.27.0 (2006-05)
677
678<SI6 rest octets> ::=
679{L | H <PCH and NCH info>}
680{L | H <VBS/VGCS options : bit(2)>}
681{ < DTM_support : bit == L > I < DTM_support : bit == H >
682< RAC : bit (8) >
683< MAX_LAPDm : bit (3) > }
684< Band indicator >
685{ L | H < GPRS_MS_TXPWR_MAX_CCH : bit (5) > }
686<implicit spare >;
687*/
688int osmo_gsm48_rest_octets_si6(uint8_t *data, bool is1800_net)
689{
690 struct bitvec bv;
691
692 memset(&bv, 0, sizeof(bv));
693 bv.data = data;
694 bv.data_len = 1;
695
696 /* no PCH/NCH info */
697 bitvec_set_bit(&bv, L);
698 /* no VBS/VGCS options */
699 bitvec_set_bit(&bv, L);
700 /* no DTM_support */
701 bitvec_set_bit(&bv, L);
702 /* band indicator */
703 if (is1800_net)
704 bitvec_set_bit(&bv, L);
705 else
706 bitvec_set_bit(&bv, H);
707 /* no GPRS_MS_TXPWR_MAX_CCH */
708 bitvec_set_bit(&bv, L);
709
710 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
711 return bv.data_len;
712}
713
714/* GPRS Mobile Allocation as per TS 04.60 Chapter 12.10a:
715 < GPRS Mobile Allocation IE > ::=
716 < HSN : bit (6) >
717 { 0 | 1 < RFL number list : < RFL number list struct > > }
718 { 0 < MA_LENGTH : bit (6) >
719 < MA_BITMAP: bit (val(MA_LENGTH) + 1) >
720 | 1 { 0 | 1 <ARFCN index list : < ARFCN index list struct > > } } ;
721
722 < RFL number list struct > :: =
723 < RFL_NUMBER : bit (4) >
724 { 0 | 1 < RFL number list struct > } ;
725 < ARFCN index list struct > ::=
726 < ARFCN_INDEX : bit(6) >
727 { 0 | 1 < ARFCN index list struct > } ;
728 */
729static int append_gprs_mobile_alloc(struct bitvec *bv)
730{
731 /* Hopping Sequence Number */
732 bitvec_set_uint(bv, 0, 6);
733
734 if (0) {
735 /* We want to use a RFL number list */
736 bitvec_set_bit(bv, 1);
737 /* FIXME: RFL number list */
738 } else
739 bitvec_set_bit(bv, 0);
740
741 if (0) {
742 /* We want to use a MA_BITMAP */
743 bitvec_set_bit(bv, 0);
744 /* FIXME: MA_LENGTH, MA_BITMAP, ... */
745 } else {
746 bitvec_set_bit(bv, 1);
747 if (0) {
748 /* We want to provide an ARFCN index list */
749 bitvec_set_bit(bv, 1);
750 /* FIXME */
751 } else
752 bitvec_set_bit(bv, 0);
753 }
754 return 0;
755}
756
757static int encode_t3192(unsigned int t3192)
758{
759 /* See also 3GPP TS 44.060
760 Table 12.24.2: GPRS Cell Options information element details */
761 if (t3192 == 0)
762 return 3;
763 else if (t3192 <= 80)
764 return 4;
765 else if (t3192 <= 120)
766 return 5;
767 else if (t3192 <= 160)
768 return 6;
769 else if (t3192 <= 200)
770 return 7;
771 else if (t3192 <= 500)
772 return 0;
773 else if (t3192 <= 1000)
774 return 1;
775 else if (t3192 <= 1500)
776 return 2;
777 else
778 return -EINVAL;
779}
780
781static int encode_drx_timer(unsigned int drx)
782{
783 if (drx == 0)
784 return 0;
785 else if (drx == 1)
786 return 1;
787 else if (drx == 2)
788 return 2;
789 else if (drx <= 4)
790 return 3;
791 else if (drx <= 8)
792 return 4;
793 else if (drx <= 16)
794 return 5;
795 else if (drx <= 32)
796 return 6;
797 else if (drx <= 64)
798 return 7;
799 else
800 return -EINVAL;
801}
802
803/* GPRS Cell Options as per TS 04.60 Chapter 12.24
804 < GPRS Cell Options IE > ::=
805 < NMO : bit(2) >
806 < T3168 : bit(3) >
807 < T3192 : bit(3) >
808 < DRX_TIMER_MAX: bit(3) >
809 < ACCESS_BURST_TYPE: bit >
810 < CONTROL_ACK_TYPE : bit >
811 < BS_CV_MAX: bit(4) >
812 { 0 | 1 < PAN_DEC : bit(3) >
813 < PAN_INC : bit(3) >
814 < PAN_MAX : bit(3) >
815 { 0 | 1 < Extension Length : bit(6) >
816 < bit (val(Extension Length) + 1
817 & { < Extension Information > ! { bit ** = <no string> } } ;
818 < Extension Information > ::=
819 { 0 | 1 < EGPRS_PACKET_CHANNEL_REQUEST : bit >
820 < BEP_PERIOD : bit(4) > }
821 < PFC_FEATURE_MODE : bit >
822 < DTM_SUPPORT : bit >
823 <BSS_PAGING_COORDINATION: bit >
824 <spare bit > ** ;
825 */
826static int append_gprs_cell_opt(struct bitvec *bv,
827 const struct osmo_gprs_cell_options *gco)
828{
829 int t3192, drx_timer_max;
830
831 t3192 = encode_t3192(gco->t3192);
832 if (t3192 < 0)
833 return t3192;
834
835 drx_timer_max = encode_drx_timer(gco->drx_timer_max);
836 if (drx_timer_max < 0)
837 return drx_timer_max;
838
839 bitvec_set_uint(bv, gco->nmo, 2);
840
841 /* See also 3GPP TS 44.060
842 Table 12.24.2: GPRS Cell Options information element details */
843 bitvec_set_uint(bv, gco->t3168 / 500 - 1, 3);
844
845 bitvec_set_uint(bv, t3192, 3);
846 bitvec_set_uint(bv, drx_timer_max, 3);
847 /* ACCESS_BURST_TYPE: Hard-code 8bit */
848 bitvec_set_bit(bv, 0);
849 /* CONTROL_ACK_TYPE: */
850 bitvec_set_bit(bv, gco->ctrl_ack_type_use_block);
851 bitvec_set_uint(bv, gco->bs_cv_max, 4);
852
853 if (0) {
854 /* hard-code no PAN_{DEC,INC,MAX} */
855 bitvec_set_bit(bv, 0);
856 } else {
857 /* copied from ip.access BSC protocol trace */
858 bitvec_set_bit(bv, 1);
859 bitvec_set_uint(bv, 1, 3); /* DEC */
860 bitvec_set_uint(bv, 1, 3); /* INC */
861 bitvec_set_uint(bv, 15, 3); /* MAX */
862 }
863
864 if (!gco->ext_info_present) {
865 /* no extension information */
866 bitvec_set_bit(bv, 0);
867 } else {
868 /* extension information */
869 bitvec_set_bit(bv, 1);
870 if (!gco->ext_info.egprs_supported) {
871 /* 6bit length of extension */
872 bitvec_set_uint(bv, (1 + 3)-1, 6);
873 /* EGPRS supported in the cell */
874 bitvec_set_bit(bv, 0);
875 } else {
876 /* 6bit length of extension */
877 bitvec_set_uint(bv, (1 + 5 + 3)-1, 6);
878 /* EGPRS supported in the cell */
879 bitvec_set_bit(bv, 1);
880
881 /* 1bit EGPRS PACKET CHANNEL REQUEST */
882 if (gco->supports_egprs_11bit_rach == 0) {
883 bitvec_set_bit(bv,
884 gco->ext_info.use_egprs_p_ch_req);
885 } else {
886 bitvec_set_bit(bv, 0);
887 }
888
889 /* 4bit BEP PERIOD */
890 bitvec_set_uint(bv, gco->ext_info.bep_period, 4);
891 }
892 bitvec_set_bit(bv, gco->ext_info.pfc_supported);
893 bitvec_set_bit(bv, gco->ext_info.dtm_supported);
894 bitvec_set_bit(bv, gco->ext_info.bss_paging_coordination);
895 }
896
897 return 0;
898}
899
900static void append_gprs_pwr_ctrl_pars(struct bitvec *bv,
901 const struct osmo_gprs_power_ctrl_pars *pcp)
902{
903 bitvec_set_uint(bv, pcp->alpha, 4);
904 bitvec_set_uint(bv, pcp->t_avg_w, 5);
905 bitvec_set_uint(bv, pcp->t_avg_t, 5);
906 bitvec_set_uint(bv, pcp->pc_meas_chan, 1);
907 bitvec_set_uint(bv, pcp->n_avg_i, 4);
908}
909
910/* Generate SI13 Rest Octests (04.08 Chapter 10.5.2.37b) */
911int osmo_gsm48_rest_octets_si13(uint8_t *data, const struct osmo_gsm48_si13_info *si13)
912{
913 struct bitvec bv;
914
915 memset(&bv, 0, sizeof(bv));
916 bv.data = data;
917 bv.data_len = 20;
918
919 if (0) {
920 /* No rest octets */
921 bitvec_set_bit(&bv, L);
922 } else {
923 bitvec_set_bit(&bv, H);
924 bitvec_set_uint(&bv, si13->bcch_change_mark, 3);
925 bitvec_set_uint(&bv, si13->si_change_field, 4);
926 if (1) {
927 bitvec_set_bit(&bv, 0);
928 } else {
929 bitvec_set_bit(&bv, 1);
930 bitvec_set_uint(&bv, si13->bcch_change_mark, 2);
931 append_gprs_mobile_alloc(&bv);
932 }
933 /* PBCCH not present in cell:
934 it shall never be indicated according to 3GPP TS 44.018 Table 10.5.2.37b.1 */
935 bitvec_set_bit(&bv, 0);
936 bitvec_set_uint(&bv, si13->rac, 8);
937 bitvec_set_bit(&bv, si13->spgc_ccch_sup);
938 bitvec_set_uint(&bv, si13->prio_acc_thr, 3);
939 bitvec_set_uint(&bv, si13->net_ctrl_ord, 2);
940 append_gprs_cell_opt(&bv, &si13->cell_opts);
941 append_gprs_pwr_ctrl_pars(&bv, &si13->pwr_ctrl_pars);
942
943 /* 3GPP TS 44.018 Release 6 / 10.5.2.37b */
944 bitvec_set_bit(&bv, H); /* added Release 99 */
945 /* claim our SGSN is compatible with Release 99, as EDGE and EGPRS
946 * was only added in this Release */
947 bitvec_set_bit(&bv, 1);
948 }
949 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
950 return bv.data_len;
951}