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