blob: cd7bfd552a6c8f459f838bb7757c22ea1a2dc826 [file] [log] [blame]
Harald Weltea43f7892009-12-01 18:04:30 +05301/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface,
2 * rest octet handling according to
3 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
4
5/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Weltea43f7892009-12-01 18:04:30 +053012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Weltea43f7892009-12-01 18:04:30 +053018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Weltea43f7892009-12-01 18:04:30 +053021 *
22 */
23
24#include <string.h>
25#include <stdlib.h>
26#include <errno.h>
Max59a1bf32016-04-15 16:04:46 +020027#include <stdbool.h>
Harald Weltea43f7892009-12-01 18:04:30 +053028
Max59a1bf32016-04-15 16:04:46 +020029#include <openbsc/debug.h>
Harald Weltea43f7892009-12-01 18:04:30 +053030#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010031#include <osmocom/core/bitvec.h>
Maxe01f5052016-04-23 18:00:18 +020032#include <osmocom/gsm/bitvec_gsm.h>
Harald Weltea43f7892009-12-01 18:04:30 +053033#include <openbsc/rest_octets.h>
Max59a1bf32016-04-15 16:04:46 +020034#include <openbsc/arfcn_range_encode.h>
Max26679e02016-04-20 15:57:13 +020035#include <openbsc/system_information.h>
Harald Weltea43f7892009-12-01 18:04:30 +053036
37/* generate SI1 rest octets */
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +010038int rest_octets_si1(uint8_t *data, uint8_t *nch_pos, int is1800_net)
Harald Weltea43f7892009-12-01 18:04:30 +053039{
40 struct bitvec bv;
41
42 memset(&bv, 0, sizeof(bv));
43 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +010044 bv.data_len = 1;
Harald Weltea43f7892009-12-01 18:04:30 +053045
46 if (nch_pos) {
47 bitvec_set_bit(&bv, H);
48 bitvec_set_uint(&bv, *nch_pos, 5);
49 } else
50 bitvec_set_bit(&bv, L);
51
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +010052 if (is1800_net)
53 bitvec_set_bit(&bv, L);
54 else
55 bitvec_set_bit(&bv, H);
56
57 bitvec_spare_padding(&bv, 6);
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +010058 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +053059}
60
Max59a1bf32016-04-15 16:04:46 +020061/* Append Repeated E-UTRAN Neighbour Cell to bitvec:
62 * see 3GPP TS 44.018 Table 10.5.2.33b.1
63 */
64static inline void append_eutran_neib_cell(struct bitvec *bv,
65 const struct osmo_earfcn_si2q *e)
66{
67 unsigned i;
68 for (i = 0; i < e->length; i++) {
69 if (e->arfcn[i] != OSMO_EARFCN_INVALID) {
70 bitvec_set_bit(bv, 1); /* EARFCN: */
71 bitvec_set_uint(bv, e->arfcn[i], 16);
72
73 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
74 bitvec_set_bit(bv, 0);
75 else {
76 /* Measurement Bandwidth: 9.1.54 */
77 bitvec_set_bit(bv, 1);
78 bitvec_set_uint(bv, e->meas_bw[i], 3);
79 }
80 }
81 }
82
83 /* stop bit - end of EARFCN + Measurement Bandwidth sequence */
84 bitvec_set_bit(bv, 0);
85
86 if (e->prio_valid) {
87 /* E-UTRAN_PRIORITY: 3GPP TS 45.008*/
88 bitvec_set_bit(bv, 1);
89 bitvec_set_uint(bv, e->prio, 3);
90 } else
91 bitvec_set_bit(bv, 0);
92
93 /* THRESH_E-UTRAN_high */
94 bitvec_set_uint(bv, e->thresh_hi, 5);
95
96 if (e->thresh_lo_valid) {
97 /* THRESH_E-UTRAN_low: */
98 bitvec_set_bit(bv, 1);
99 bitvec_set_uint(bv, e->thresh_lo, 5);
100 } else
101 bitvec_set_bit(bv, 0);
102
103 if (e->qrxlm_valid) {
104 /* E-UTRAN_QRXLEVMIN: */
105 bitvec_set_bit(bv, 1);
106 bitvec_set_uint(bv, e->qrxlm, 5);
107 } else
108 bitvec_set_bit(bv, 0);
109}
110
Max59a1bf32016-04-15 16:04:46 +0200111static inline void append_earfcn(struct bitvec *bv,
112 const struct osmo_earfcn_si2q *e)
113{
114 /* Additions in Rel-5: */
115 bitvec_set_bit(bv, H);
116 /* No 3G Additional Measurement Param. Descr. */
117 bitvec_set_bit(bv, 0);
118 /* No 3G ADDITIONAL MEASUREMENT Param. Descr. 2 */
119 bitvec_set_bit(bv, 0);
120 /* Additions in Rel-6: */
121 bitvec_set_bit(bv, H);
122 /* 3G_CCN_ACTIVE */
123 bitvec_set_bit(bv, 0);
124 /* Additions in Rel-7: */
125 bitvec_set_bit(bv, H);
126 /* No 700_REPORTING_OFFSET */
127 bitvec_set_bit(bv, 0);
128 /* No 810_REPORTING_OFFSET */
129 bitvec_set_bit(bv, 0);
130 /* Additions in Rel-8: */
131 bitvec_set_bit(bv, H);
132
133 /* Priority and E-UTRAN Parameters Description */
134 bitvec_set_bit(bv, 1);
135
136 /* No Serving Cell Priority Parameters Descr. */
137 bitvec_set_bit(bv, 0);
138 /* No 3G Priority Parameters Description */
139 bitvec_set_bit(bv, 0);
140 /* E-UTRAN Parameters Description */
141 bitvec_set_bit(bv, 1);
142
143 /* E-UTRAN_CCN_ACTIVE */
144 bitvec_set_bit(bv, 0);
145 /* E-UTRAN_Start: 9.1.54 */
146 bitvec_set_bit(bv, 1);
147 /* E-UTRAN_Stop: 9.1.54 */
148 bitvec_set_bit(bv, 1);
149
150 /* No E-UTRAN Measurement Parameters Descr. */
151 bitvec_set_bit(bv, 0);
152 /* No GPRS E-UTRAN Measurement Param. Descr. */
153 bitvec_set_bit(bv, 0);
154
155 /* Note: each of next 3 "repeated" structures might be repeated any
156 (0, 1, 2...) times - we only support 1 and 0 */
157
158 /* Repeated E-UTRAN Neighbour Cells */
159 bitvec_set_bit(bv, 1);
160
161 /* Note: we don't support different EARFCN arrays each with different
162 priority, threshold etc. */
163 append_eutran_neib_cell(bv, e);
164
165 /* stop bit - end of Repeated E-UTRAN Neighbour Cells sequence: */
166 bitvec_set_bit(bv, 0);
167
168 /* Note: following 2 repeated structs are not supported ATM */
169 /* stop bit - end of Repeated E-UTRAN Not Allowed Cells sequence: */
170 bitvec_set_bit(bv, 0);
171 /* stop bit - end of Repeated E-UTRAN PCID to TA mapping sequence: */
172 bitvec_set_bit(bv, 0);
173
174 /* Priority and E-UTRAN Parameters Description ends here */
175 /* No 3G CSG Description */
176 bitvec_set_bit(bv, 0);
177 /* No E-UTRAN CSG Description */
178 bitvec_set_bit(bv, 0);
179 /* No Additions in Rel-9: */
180 bitvec_set_bit(bv, L);
181}
182
Max26679e02016-04-20 15:57:13 +0200183static inline void append_uarfcn(struct bitvec *bv, const uint16_t *u,
184 const uint16_t *sc, size_t length)
185{
186 int f0_inc, i, arfcns_used, w[RANGE_ENC_MAX_ARFCNS], a[length];
187 uint8_t chan_list[16] = {0};
188
189 /* 3G Neighbour Cell Description */
190 bitvec_set_bit(bv, 1);
191 /* No Index_Start_3G */
192 bitvec_set_bit(bv, 0);
193 /* No Absolute_Index_Start_EMR */
194 bitvec_set_bit(bv, 0);
195
196 /* UTRAN FDD Description */
197 bitvec_set_bit(bv, 1);
198 /* No Bandwidth_FDD */
199 bitvec_set_bit(bv, 0);
200
201 memset(w, 0, sizeof(w));
202 for (i = 0; i < length; i++)
203 a[i] = sc[i];
204
205 /* Note: we do not support repeating Neighbour Cells ATM */
206 /* Repeated UTRAN FDD Neighbour Cells */
207 bitvec_set_bit(bv, 1);
208
209 /* FDD-ARFCN */
210 bitvec_set_bit(bv, 0);
211 /* Note: we do not support multiple UARFCN values ATM: */
212 bitvec_set_uint(bv, u[0], 14);
213
214 arfcns_used = range_enc_filter_arfcns(a, length, 0, &f0_inc);
215 range_enc_arfcns(ARFCN_RANGE_1024, a, arfcns_used, w, 0);
216 range_enc_range1024(chan_list, 0, f0_inc, w);
217
218 /* FDD_Indic0: parameter value '0000000000' is not a member of the set */
219 bitvec_set_bit(bv, f0_inc);
220 /* NR_OF_FDD_CELLS */
221 bitvec_set_uint(bv, length, 5);
222
223 i = bv->cur_bit;
224 bitvec_add_range1024(bv, (struct gsm48_range_1024 *)chan_list);
225 bv->cur_bit = i + range1024_p(length);
226
227 /* stop bit - end of Repeated UTRAN FDD Neighbour Cells */
228 bitvec_set_bit(bv, 0);
229
230 /* UTRAN TDD Description */
231 bitvec_set_bit(bv, 0);
232}
233
Max59a1bf32016-04-15 16:04:46 +0200234/* generate SI2quater rest octets: 3GPP TS 44.018 § 10.5.2.33b */
235int rest_octets_si2quater(uint8_t *data, const struct osmo_earfcn_si2q *e,
Max26679e02016-04-20 15:57:13 +0200236 const uint16_t *u, const uint16_t *sc, size_t u_len)
Max59a1bf32016-04-15 16:04:46 +0200237{
Max26679e02016-04-20 15:57:13 +0200238 unsigned sz;
Max59a1bf32016-04-15 16:04:46 +0200239 struct bitvec bv;
240 bv.data = data;
241 bv.data_len = 20;
242 bitvec_zero(&bv);
243
244 /* BA_IND */
245 bitvec_set_bit(&bv, 1);
246 /* 3G_BA_IND */
247 bitvec_set_bit(&bv, 1);
248 /* MP_CHANGE_MARK */
249 bitvec_set_bit(&bv, 0);
250
251 /* we do not support multiple si2quater messages at the moment: */
252 /* SI2quater_INDEX */
253 bitvec_set_uint(&bv, 0, 4);
254 /* SI2quater_COUNT */
255 bitvec_set_uint(&bv, 0, 4);
256
257 /* No Measurement_Parameters Description */
258 bitvec_set_bit(&bv, 0);
259 /* No GPRS_Real Time Difference Description */
260 bitvec_set_bit(&bv, 0);
261 /* No GPRS_BSIC Description */
262 bitvec_set_bit(&bv, 0);
263 /* No GPRS_REPORT PRIORITY Description */
264 bitvec_set_bit(&bv, 0);
265 /* No GPRS_MEASUREMENT_Parameters Description */
266 bitvec_set_bit(&bv, 0);
267 /* No NC Measurement Parameters */
268 bitvec_set_bit(&bv, 0);
269 /* No extension (length) */
270 bitvec_set_bit(&bv, 0);
271
Max26679e02016-04-20 15:57:13 +0200272 if (u_len) {
273 sz = uarfcn_size(u, sc, u_len);
274 /* Even if we do not append EARFCN we still need to set 3 bits */
275 if (sz + bv.cur_bit + 3 > SI2Q_MAX_LEN) {
276 LOGP(DRR, LOGL_ERROR, "SI2quater: not enough memory to "
277 "add UARFCNs bits, current %u + required %u + "
278 "reminder %u > max %u\n", bv.cur_bit, sz, 3,
279 SI2Q_MAX_LEN);
280 return -ENOMEM;
281 }
282 append_uarfcn(&bv, u, sc, u_len);
Max59a1bf32016-04-15 16:04:46 +0200283 } else { /* No 3G Neighbour Cell Description */
284 bitvec_set_bit(&bv, 0);
285 }
286
287 /* No 3G Measurement Parameters Description */
288 bitvec_set_bit(&bv, 0);
289 /* No GPRS_3G_MEASUREMENT Parameters Descr. */
290 bitvec_set_bit(&bv, 0);
291
Max26679e02016-04-20 15:57:13 +0200292 if (e) {
293 sz = earfcn_size(e);
294 if (sz + bv.cur_bit > SI2Q_MAX_LEN) {
295 LOGP(DRR, LOGL_ERROR, "SI2quater: not enough memory to "
296 "add EARFCNs bits, current %u + required %u > max "
297 "%u\n", bv.cur_bit, sz, SI2Q_MAX_LEN);
Max59a1bf32016-04-15 16:04:46 +0200298 return -ENOMEM;
Max26679e02016-04-20 15:57:13 +0200299 }
Max59a1bf32016-04-15 16:04:46 +0200300 append_earfcn(&bv, e);
301 } else {
302 /* No Additions in Rel-5: */
303 bitvec_set_bit(&bv, L);
304 }
305
306 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
307 return bv.data_len;
308}
309
Harald Weltea43f7892009-12-01 18:04:30 +0530310/* Append selection parameters to bitvec */
311static void append_selection_params(struct bitvec *bv,
312 const struct gsm48_si_selection_params *sp)
313{
314 if (sp->present) {
315 bitvec_set_bit(bv, H);
316 bitvec_set_bit(bv, sp->cbq);
317 bitvec_set_uint(bv, sp->cell_resel_off, 6);
318 bitvec_set_uint(bv, sp->temp_offs, 3);
319 bitvec_set_uint(bv, sp->penalty_time, 5);
320 } else
321 bitvec_set_bit(bv, L);
322}
323
324/* Append power offset to bitvec */
325static void append_power_offset(struct bitvec *bv,
326 const struct gsm48_si_power_offset *po)
327{
328 if (po->present) {
329 bitvec_set_bit(bv, H);
330 bitvec_set_uint(bv, po->power_offset, 2);
331 } else
332 bitvec_set_bit(bv, L);
333}
334
335/* Append GPRS indicator to bitvec */
336static void append_gprs_ind(struct bitvec *bv,
337 const struct gsm48_si3_gprs_ind *gi)
338{
339 if (gi->present) {
340 bitvec_set_bit(bv, H);
341 bitvec_set_uint(bv, gi->ra_colour, 3);
342 /* 0 == SI13 in BCCH Norm, 1 == SI13 sent on BCCH Ext */
343 bitvec_set_bit(bv, gi->si13_position);
344 } else
345 bitvec_set_bit(bv, L);
346}
347
348
349/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200350int rest_octets_si3(uint8_t *data, const struct gsm48_si_ro_info *si3)
Harald Weltea43f7892009-12-01 18:04:30 +0530351{
352 struct bitvec bv;
353
354 memset(&bv, 0, sizeof(bv));
355 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100356 bv.data_len = 4;
Harald Weltea43f7892009-12-01 18:04:30 +0530357
358 /* Optional Selection Parameters */
359 append_selection_params(&bv, &si3->selection_params);
360
361 /* Optional Power Offset */
362 append_power_offset(&bv, &si3->power_offset);
363
364 /* Do we have a SI2ter on the BCCH? */
365 if (si3->si2ter_indicator)
366 bitvec_set_bit(&bv, H);
367 else
368 bitvec_set_bit(&bv, L);
369
370 /* Early Classmark Sending Control */
371 if (si3->early_cm_ctrl)
372 bitvec_set_bit(&bv, H);
373 else
374 bitvec_set_bit(&bv, L);
375
376 /* Do we have a SI Type 9 on the BCCH? */
377 if (si3->scheduling.present) {
378 bitvec_set_bit(&bv, H);
379 bitvec_set_uint(&bv, si3->scheduling.where, 3);
380 } else
381 bitvec_set_bit(&bv, L);
382
383 /* GPRS Indicator */
384 append_gprs_ind(&bv, &si3->gprs_ind);
385
Maxf3f35052016-04-15 16:04:44 +0200386 /* 3G Early Classmark Sending Restriction controlled by
387 * early_cm_ctrl above */
388 bitvec_set_bit(&bv, H);
389
390 if (si3->si2quater_indicator) {
391 bitvec_set_bit(&bv, H); /* indicator struct present */
392 bitvec_set_uint(&bv, 0, 1); /* message is sent on BCCH Norm */
393 }
394
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100395 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
396 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530397}
398
399static int append_lsa_params(struct bitvec *bv,
400 const struct gsm48_lsa_params *lsa_params)
401{
402 /* FIXME */
Holger Hans Peter Freytherae80f922010-04-10 00:05:16 +0200403 return -1;
Harald Weltea43f7892009-12-01 18:04:30 +0530404}
405
406/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100407int rest_octets_si4(uint8_t *data, const struct gsm48_si_ro_info *si4, int len)
Harald Weltea43f7892009-12-01 18:04:30 +0530408{
409 struct bitvec bv;
410
411 memset(&bv, 0, sizeof(bv));
412 bv.data = data;
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100413 bv.data_len = len;
Harald Weltea43f7892009-12-01 18:04:30 +0530414
415 /* SI4 Rest Octets O */
416 append_selection_params(&bv, &si4->selection_params);
417 append_power_offset(&bv, &si4->power_offset);
418 append_gprs_ind(&bv, &si4->gprs_ind);
419
420 if (0 /* FIXME */) {
421 /* H and SI4 Rest Octets S */
422 bitvec_set_bit(&bv, H);
423
424 /* LSA Parameters */
425 if (si4->lsa_params.present) {
426 bitvec_set_bit(&bv, H);
427 append_lsa_params(&bv, &si4->lsa_params);
428 } else
429 bitvec_set_bit(&bv, L);
430
431 /* Cell Identity */
432 if (1) {
433 bitvec_set_bit(&bv, H);
434 bitvec_set_uint(&bv, si4->cell_id, 16);
435 } else
436 bitvec_set_bit(&bv, L);
437
438 /* LSA ID Information */
439 if (0) {
440 bitvec_set_bit(&bv, H);
441 /* FIXME */
442 } else
443 bitvec_set_bit(&bv, L);
444 } else {
445 /* L and break indicator */
446 bitvec_set_bit(&bv, L);
447 bitvec_set_bit(&bv, si4->break_ind ? H : L);
448 }
449
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100450 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530451}
452
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +0200453
454/* GSM 04.18 ETSI TS 101 503 V8.27.0 (2006-05)
455
456<SI6 rest octets> ::=
457{L | H <PCH and NCH info>}
458{L | H <VBS/VGCS options : bit(2)>}
459{ < DTM_support : bit == L > I < DTM_support : bit == H >
460< RAC : bit (8) >
461< MAX_LAPDm : bit (3) > }
462< Band indicator >
463{ L | H < GPRS_MS_TXPWR_MAX_CCH : bit (5) > }
464<implicit spare >;
465*/
466int rest_octets_si6(uint8_t *data, bool is1800_net)
467{
468 struct bitvec bv;
469
470 memset(&bv, 0, sizeof(bv));
471 bv.data = data;
472 bv.data_len = 1;
473
474 /* no PCH/NCH info */
475 bitvec_set_bit(&bv, L);
476 /* no VBS/VGCS options */
477 bitvec_set_bit(&bv, L);
478 /* no DTM_support */
479 bitvec_set_bit(&bv, L);
480 /* band indicator */
481 if (is1800_net)
482 bitvec_set_bit(&bv, L);
483 else
484 bitvec_set_bit(&bv, H);
485 /* no GPRS_MS_TXPWR_MAX_CCH */
486 bitvec_set_bit(&bv, L);
487
488 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
489 return bv.data_len;
490}
491
Harald Weltea43f7892009-12-01 18:04:30 +0530492/* GPRS Mobile Allocation as per TS 04.60 Chapter 12.10a:
493 < GPRS Mobile Allocation IE > ::=
494 < HSN : bit (6) >
495 { 0 | 1 < RFL number list : < RFL number list struct > > }
496 { 0 < MA_LENGTH : bit (6) >
497 < MA_BITMAP: bit (val(MA_LENGTH) + 1) >
498 | 1 { 0 | 1 <ARFCN index list : < ARFCN index list struct > > } } ;
499
500 < RFL number list struct > :: =
501 < RFL_NUMBER : bit (4) >
502 { 0 | 1 < RFL number list struct > } ;
503 < ARFCN index list struct > ::=
504 < ARFCN_INDEX : bit(6) >
505 { 0 | 1 < ARFCN index list struct > } ;
506 */
507static int append_gprs_mobile_alloc(struct bitvec *bv)
508{
509 /* Hopping Sequence Number */
510 bitvec_set_uint(bv, 0, 6);
511
512 if (0) {
513 /* We want to use a RFL number list */
514 bitvec_set_bit(bv, 1);
515 /* FIXME: RFL number list */
516 } else
517 bitvec_set_bit(bv, 0);
518
519 if (0) {
520 /* We want to use a MA_BITMAP */
521 bitvec_set_bit(bv, 0);
522 /* FIXME: MA_LENGTH, MA_BITMAP, ... */
523 } else {
524 bitvec_set_bit(bv, 1);
525 if (0) {
526 /* We want to provide an ARFCN index list */
527 bitvec_set_bit(bv, 1);
528 /* FIXME */
529 } else
530 bitvec_set_bit(bv, 0);
531 }
532 return 0;
533}
534
535static int encode_t3192(unsigned int t3192)
536{
537 if (t3192 == 0)
538 return 3;
539 else if (t3192 <= 80)
540 return 4;
541 else if (t3192 <= 120)
542 return 5;
543 else if (t3192 <= 160)
544 return 6;
545 else if (t3192 <= 200)
546 return 7;
547 else if (t3192 <= 500)
548 return 0;
549 else if (t3192 <= 1000)
550 return 1;
551 else if (t3192 <= 1500)
552 return 2;
553 else
554 return -EINVAL;
555}
556
557static int encode_drx_timer(unsigned int drx)
558{
559 if (drx == 0)
560 return 0;
561 else if (drx == 1)
562 return 1;
563 else if (drx == 2)
564 return 2;
565 else if (drx <= 4)
566 return 3;
567 else if (drx <= 8)
568 return 4;
569 else if (drx <= 16)
570 return 5;
571 else if (drx <= 32)
572 return 6;
573 else if (drx <= 64)
574 return 7;
575 else
576 return -EINVAL;
577}
578
579/* GPRS Cell Options as per TS 04.60 Chapter 12.24
580 < GPRS Cell Options IE > ::=
581 < NMO : bit(2) >
582 < T3168 : bit(3) >
583 < T3192 : bit(3) >
584 < DRX_TIMER_MAX: bit(3) >
585 < ACCESS_BURST_TYPE: bit >
586 < CONTROL_ACK_TYPE : bit >
587 < BS_CV_MAX: bit(4) >
588 { 0 | 1 < PAN_DEC : bit(3) >
589 < PAN_INC : bit(3) >
590 < PAN_MAX : bit(3) >
591 { 0 | 1 < Extension Length : bit(6) >
592 < bit (val(Extension Length) + 1
593 & { < Extension Information > ! { bit ** = <no string> } } ;
594 < Extension Information > ::=
595 { 0 | 1 < EGPRS_PACKET_CHANNEL_REQUEST : bit >
596 < BEP_PERIOD : bit(4) > }
597 < PFC_FEATURE_MODE : bit >
598 < DTM_SUPPORT : bit >
599 <BSS_PAGING_COORDINATION: bit >
600 <spare bit > ** ;
601 */
Harald Weltea4e2d042009-12-20 17:08:22 +0100602static int append_gprs_cell_opt(struct bitvec *bv,
603 const struct gprs_cell_options *gco)
Harald Weltea43f7892009-12-01 18:04:30 +0530604{
605 int t3192, drx_timer_max;
606
607 t3192 = encode_t3192(gco->t3192);
608 if (t3192 < 0)
609 return t3192;
610
611 drx_timer_max = encode_drx_timer(gco->drx_timer_max);
612 if (drx_timer_max < 0)
613 return drx_timer_max;
614
615 bitvec_set_uint(bv, gco->nmo, 2);
616 bitvec_set_uint(bv, gco->t3168 / 500, 3);
617 bitvec_set_uint(bv, t3192, 3);
618 bitvec_set_uint(bv, drx_timer_max, 3);
619 /* ACCESS_BURST_TYPE: Hard-code 8bit */
620 bitvec_set_bit(bv, 0);
621 /* CONTROL_ACK_TYPE: Hard-code to RLC/MAC control block */
622 bitvec_set_bit(bv, 1);
623 bitvec_set_uint(bv, gco->bs_cv_max, 4);
624
Harald Weltea4b16652010-05-31 12:54:23 +0200625 if (0) {
626 /* hard-code no PAN_{DEC,INC,MAX} */
627 bitvec_set_bit(bv, 0);
628 } else {
629 /* copied from ip.access BSC protocol trace */
630 bitvec_set_bit(bv, 1);
631 bitvec_set_uint(bv, 1, 3); /* DEC */
632 bitvec_set_uint(bv, 1, 3); /* INC */
633 bitvec_set_uint(bv, 15, 3); /* MAX */
634 }
Harald Weltea43f7892009-12-01 18:04:30 +0530635
Harald Welteda0586a2010-04-18 14:49:05 +0200636 if (!gco->ext_info_present) {
637 /* no extension information */
638 bitvec_set_bit(bv, 0);
639 } else {
640 /* extension information */
641 bitvec_set_bit(bv, 1);
642 if (!gco->ext_info.egprs_supported) {
643 /* 6bit length of extension */
Harald Welte39608dc2010-04-18 22:47:22 +0200644 bitvec_set_uint(bv, (1 + 3)-1, 6);
Harald Welteda0586a2010-04-18 14:49:05 +0200645 /* EGPRS supported in the cell */
646 bitvec_set_bit(bv, 0);
647 } else {
648 /* 6bit length of extension */
Harald Welte39608dc2010-04-18 22:47:22 +0200649 bitvec_set_uint(bv, (1 + 5 + 3)-1, 6);
Harald Welteda0586a2010-04-18 14:49:05 +0200650 /* EGPRS supported in the cell */
651 bitvec_set_bit(bv, 1);
bhargava350533c2016-07-21 11:14:34 +0530652
Harald Welteda0586a2010-04-18 14:49:05 +0200653 /* 1bit EGPRS PACKET CHANNEL REQUEST */
bhargava350533c2016-07-21 11:14:34 +0530654 if (gco->supports_egprs_11bit_rach == 0) {
655 bitvec_set_bit(bv,
656 gco->ext_info.use_egprs_p_ch_req);
657 } else {
658 bitvec_set_bit(bv, 0);
659 }
660
Harald Welteda0586a2010-04-18 14:49:05 +0200661 /* 4bit BEP PERIOD */
662 bitvec_set_uint(bv, gco->ext_info.bep_period, 4);
663 }
664 bitvec_set_bit(bv, gco->ext_info.pfc_supported);
665 bitvec_set_bit(bv, gco->ext_info.dtm_supported);
666 bitvec_set_bit(bv, gco->ext_info.bss_paging_coordination);
667 }
Harald Weltea43f7892009-12-01 18:04:30 +0530668
669 return 0;
670}
671
672static void append_gprs_pwr_ctrl_pars(struct bitvec *bv,
Harald Weltea4e2d042009-12-20 17:08:22 +0100673 const struct gprs_power_ctrl_pars *pcp)
Harald Weltea43f7892009-12-01 18:04:30 +0530674{
675 bitvec_set_uint(bv, pcp->alpha, 4);
676 bitvec_set_uint(bv, pcp->t_avg_w, 5);
677 bitvec_set_uint(bv, pcp->t_avg_t, 5);
678 bitvec_set_uint(bv, pcp->pc_meas_chan, 1);
679 bitvec_set_uint(bv, pcp->n_avg_i, 4);
680}
681
Harald Welte5fda9082010-04-18 22:41:01 +0200682/* Generate SI13 Rest Octests (04.08 Chapter 10.5.2.37b) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200683int rest_octets_si13(uint8_t *data, const struct gsm48_si13_info *si13)
Harald Weltea43f7892009-12-01 18:04:30 +0530684{
685 struct bitvec bv;
686
687 memset(&bv, 0, sizeof(bv));
688 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100689 bv.data_len = 20;
Harald Weltea43f7892009-12-01 18:04:30 +0530690
691 if (0) {
692 /* No rest octets */
693 bitvec_set_bit(&bv, L);
694 } else {
695 bitvec_set_bit(&bv, H);
696 bitvec_set_uint(&bv, si13->bcch_change_mark, 3);
697 bitvec_set_uint(&bv, si13->si_change_field, 4);
Harald Welte97a282b2010-03-14 15:37:43 +0800698 if (1) {
Harald Weltea43f7892009-12-01 18:04:30 +0530699 bitvec_set_bit(&bv, 0);
700 } else {
701 bitvec_set_bit(&bv, 1);
702 bitvec_set_uint(&bv, si13->bcch_change_mark, 2);
703 append_gprs_mobile_alloc(&bv);
704 }
705 if (!si13->pbcch_present) {
706 /* PBCCH not present in cell */
707 bitvec_set_bit(&bv, 0);
708 bitvec_set_uint(&bv, si13->no_pbcch.rac, 8);
709 bitvec_set_bit(&bv, si13->no_pbcch.spgc_ccch_sup);
710 bitvec_set_uint(&bv, si13->no_pbcch.prio_acc_thr, 3);
711 bitvec_set_uint(&bv, si13->no_pbcch.net_ctrl_ord, 2);
712 append_gprs_cell_opt(&bv, &si13->cell_opts);
713 append_gprs_pwr_ctrl_pars(&bv, &si13->pwr_ctrl_pars);
714 } else {
715 /* PBCCH present in cell */
716 bitvec_set_bit(&bv, 1);
717 bitvec_set_uint(&bv, si13->pbcch.psi1_rep_per, 4);
718 /* PBCCH Descripiton */
719 bitvec_set_uint(&bv, si13->pbcch.pb, 4);
720 bitvec_set_uint(&bv, si13->pbcch.tsc, 3);
721 bitvec_set_uint(&bv, si13->pbcch.tn, 3);
722 switch (si13->pbcch.carrier_type) {
723 case PBCCH_BCCH:
724 bitvec_set_bit(&bv, 0);
725 bitvec_set_bit(&bv, 0);
726 break;
727 case PBCCH_ARFCN:
728 bitvec_set_bit(&bv, 0);
729 bitvec_set_bit(&bv, 1);
730 bitvec_set_uint(&bv, si13->pbcch.arfcn, 10);
731 break;
732 case PBCCH_MAIO:
733 bitvec_set_bit(&bv, 1);
734 bitvec_set_uint(&bv, si13->pbcch.maio, 6);
735 break;
736 }
737 }
Harald Welte5fda9082010-04-18 22:41:01 +0200738 /* 3GPP TS 44.018 Release 6 / 10.5.2.37b */
739 bitvec_set_bit(&bv, H); /* added Release 99 */
740 /* claim our SGSN is compatible with Release 99, as EDGE and EGPRS
741 * was only added in this Release */
742 bitvec_set_bit(&bv, 1);
Harald Weltea43f7892009-12-01 18:04:30 +0530743 }
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100744 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
745 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530746}