blob: a6fdf46feb5b1f7aa7207dfd19188d5d963eea04 [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
Maxf39d03a2017-05-12 17:00:30 +020061/* Append Repeated E-UTRAN Neighbour Cell to bitvec: see 3GPP TS 44.018 Table 10.5.2.33b.1 */
62static inline void append_eutran_neib_cell(struct bitvec *bv, struct gsm_bts *bts, uint8_t budget)
Max59a1bf32016-04-15 16:04:46 +020063{
Maxf39d03a2017-05-12 17:00:30 +020064 const struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
65 unsigned i, skip = 0;
66 size_t offset = bts->e_offset;
67 uint8_t rem = budget - 6, earfcn_budget; /* account for mandatory stop bit and THRESH_E-UTRAN_high */
68 /* first we have to properly adjust budget requirements */
69 if (e->prio_valid) /* E-UTRAN_PRIORITY: 3GPP TS 45.008*/
70 rem -= 4;
71 else
72 rem--;
73
74 if (e->thresh_lo_valid) /* THRESH_E-UTRAN_low: */
75 rem -= 6;
76 else
77 rem--;
78
79 if (e->qrxlm_valid) /* E-UTRAN_QRXLEVMIN: */
80 rem -= 6;
81 else
82 rem--;
83
84 /* now we can proceed with actually adding EARFCNs within adjusted budget limit */
Max59a1bf32016-04-15 16:04:46 +020085 for (i = 0; i < e->length; i++) {
86 if (e->arfcn[i] != OSMO_EARFCN_INVALID) {
Maxf39d03a2017-05-12 17:00:30 +020087 if (skip < offset) {
88 skip++; /* ignore EARFCNs added on previous calls */
89 } else {
90 earfcn_budget = 17; /* computer budget per-EARFCN */
91 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
92 earfcn_budget++;
93 else
94 earfcn_budget += 4;
Max59a1bf32016-04-15 16:04:46 +020095
Maxf39d03a2017-05-12 17:00:30 +020096 if (rem - earfcn_budget < 0) {
97 break;
98 } else {
99 bts->e_offset++;
100 bitvec_set_bit(bv, 1); /* EARFCN: */
101 bitvec_set_uint(bv, e->arfcn[i], 16);
102
103 if (OSMO_EARFCN_MEAS_INVALID == e->meas_bw[i])
104 bitvec_set_bit(bv, 0);
105 else { /* Measurement Bandwidth: 9.1.54 */
106 bitvec_set_bit(bv, 1);
107 bitvec_set_uint(bv, e->meas_bw[i], 3);
108 }
109 }
Max59a1bf32016-04-15 16:04:46 +0200110 }
111 }
112 }
113
114 /* stop bit - end of EARFCN + Measurement Bandwidth sequence */
115 bitvec_set_bit(bv, 0);
116
Maxf39d03a2017-05-12 17:00:30 +0200117 /* Note: we don't support different EARFCN arrays each with different priority, threshold etc. */
118
Max59a1bf32016-04-15 16:04:46 +0200119 if (e->prio_valid) {
120 /* E-UTRAN_PRIORITY: 3GPP TS 45.008*/
121 bitvec_set_bit(bv, 1);
122 bitvec_set_uint(bv, e->prio, 3);
123 } else
124 bitvec_set_bit(bv, 0);
125
126 /* THRESH_E-UTRAN_high */
127 bitvec_set_uint(bv, e->thresh_hi, 5);
128
129 if (e->thresh_lo_valid) {
130 /* THRESH_E-UTRAN_low: */
131 bitvec_set_bit(bv, 1);
132 bitvec_set_uint(bv, e->thresh_lo, 5);
133 } else
134 bitvec_set_bit(bv, 0);
135
136 if (e->qrxlm_valid) {
137 /* E-UTRAN_QRXLEVMIN: */
138 bitvec_set_bit(bv, 1);
139 bitvec_set_uint(bv, e->qrxlm, 5);
140 } else
141 bitvec_set_bit(bv, 0);
142}
143
Maxf39d03a2017-05-12 17:00:30 +0200144static inline void append_earfcn(struct bitvec *bv, struct gsm_bts *bts, uint8_t budget)
Max59a1bf32016-04-15 16:04:46 +0200145{
146 /* Additions in Rel-5: */
147 bitvec_set_bit(bv, H);
148 /* No 3G Additional Measurement Param. Descr. */
149 bitvec_set_bit(bv, 0);
150 /* No 3G ADDITIONAL MEASUREMENT Param. Descr. 2 */
151 bitvec_set_bit(bv, 0);
152 /* Additions in Rel-6: */
153 bitvec_set_bit(bv, H);
154 /* 3G_CCN_ACTIVE */
155 bitvec_set_bit(bv, 0);
156 /* Additions in Rel-7: */
157 bitvec_set_bit(bv, H);
158 /* No 700_REPORTING_OFFSET */
159 bitvec_set_bit(bv, 0);
160 /* No 810_REPORTING_OFFSET */
161 bitvec_set_bit(bv, 0);
162 /* Additions in Rel-8: */
163 bitvec_set_bit(bv, H);
164
165 /* Priority and E-UTRAN Parameters Description */
166 bitvec_set_bit(bv, 1);
167
168 /* No Serving Cell Priority Parameters Descr. */
169 bitvec_set_bit(bv, 0);
170 /* No 3G Priority Parameters Description */
171 bitvec_set_bit(bv, 0);
172 /* E-UTRAN Parameters Description */
173 bitvec_set_bit(bv, 1);
174
175 /* E-UTRAN_CCN_ACTIVE */
176 bitvec_set_bit(bv, 0);
177 /* E-UTRAN_Start: 9.1.54 */
178 bitvec_set_bit(bv, 1);
179 /* E-UTRAN_Stop: 9.1.54 */
180 bitvec_set_bit(bv, 1);
181
182 /* No E-UTRAN Measurement Parameters Descr. */
183 bitvec_set_bit(bv, 0);
184 /* No GPRS E-UTRAN Measurement Param. Descr. */
185 bitvec_set_bit(bv, 0);
186
187 /* Note: each of next 3 "repeated" structures might be repeated any
188 (0, 1, 2...) times - we only support 1 and 0 */
189
190 /* Repeated E-UTRAN Neighbour Cells */
191 bitvec_set_bit(bv, 1);
192
Maxf39d03a2017-05-12 17:00:30 +0200193 /* N. B: 25 bits are set in append_earfcn() - keep it in sync with budget adjustment below: */
194 append_eutran_neib_cell(bv, bts, budget - 25);
Max59a1bf32016-04-15 16:04:46 +0200195
196 /* stop bit - end of Repeated E-UTRAN Neighbour Cells sequence: */
197 bitvec_set_bit(bv, 0);
198
199 /* Note: following 2 repeated structs are not supported ATM */
200 /* stop bit - end of Repeated E-UTRAN Not Allowed Cells sequence: */
201 bitvec_set_bit(bv, 0);
202 /* stop bit - end of Repeated E-UTRAN PCID to TA mapping sequence: */
203 bitvec_set_bit(bv, 0);
204
205 /* Priority and E-UTRAN Parameters Description ends here */
206 /* No 3G CSG Description */
207 bitvec_set_bit(bv, 0);
208 /* No E-UTRAN CSG Description */
209 bitvec_set_bit(bv, 0);
210 /* No Additions in Rel-9: */
211 bitvec_set_bit(bv, L);
212}
213
Maxf39d03a2017-05-12 17:00:30 +0200214static inline int f0_helper(int *sc, size_t length, uint8_t *chan_list)
Maxe610e702016-12-19 13:41:48 +0100215{
Maxf39d03a2017-05-12 17:00:30 +0200216 int w[RANGE_ENC_MAX_ARFCNS] = { 0 };
217
218 return range_encode(ARFCN_RANGE_1024, sc, length, w, 0, chan_list);
219}
220
221/* Estimate how many bits it'll take to append single FDD UARFCN */
222static inline int append_utran_fdd_length(uint16_t u, int *sc, size_t sc_len, size_t length)
223{
224 uint8_t chan_list[16] = { 0 };
225 int tmp[sc_len], f0;
226
227 memcpy(tmp, sc, sizeof(tmp));
228
229 f0 = f0_helper(tmp, length, chan_list);
230 if (f0 < 0)
231 return f0;
232
233 return 21 + range1024_p(length);
234}
235
236/* Append single FDD UARFCN */
237static inline int append_utran_fdd(struct bitvec *bv, uint16_t u, int *sc, size_t length)
238{
239 uint8_t chan_list[16] = { 0 };
240 int f0 = f0_helper(sc, length, chan_list);
241
242 if (f0 < 0)
243 return f0;
244
Maxe610e702016-12-19 13:41:48 +0100245 /* Repeated UTRAN FDD Neighbour Cells */
246 bitvec_set_bit(bv, 1);
247
248 /* FDD-ARFCN */
249 bitvec_set_bit(bv, 0);
250 bitvec_set_uint(bv, u, 14);
251
Maxe610e702016-12-19 13:41:48 +0100252 /* FDD_Indic0: parameter value '0000000000' is a member of the set? */
253 bitvec_set_bit(bv, f0);
254 /* NR_OF_FDD_CELLS */
255 bitvec_set_uint(bv, length, 5);
256
257 f0 = bv->cur_bit;
258 bitvec_add_range1024(bv, (struct gsm48_range_1024 *)chan_list);
259 bv->cur_bit = f0 + range1024_p(length);
Maxf39d03a2017-05-12 17:00:30 +0200260
261 return 21 + range1024_p(length);
Maxe610e702016-12-19 13:41:48 +0100262}
263
264/* Append multiple FDD UARFCNs */
Maxf39d03a2017-05-12 17:00:30 +0200265static inline int append_uarfcns(struct bitvec *bv, struct gsm_bts *bts, uint8_t budget)
Max26679e02016-04-20 15:57:13 +0200266{
Maxf39d03a2017-05-12 17:00:30 +0200267 const uint16_t *u = bts->si_common.data.uarfcn_list, *sc = bts->si_common.data.scramble_list;
268 int i, j, k, rc, st = 0, a[bts->si_common.uarfcn_length];
269 uint16_t cu = u[bts->u_offset]; /* caller ensures that length is positive */
270 uint8_t rem = budget - 7; /* account for constant bits right away */
Max26679e02016-04-20 15:57:13 +0200271
272 /* 3G Neighbour Cell Description */
273 bitvec_set_bit(bv, 1);
274 /* No Index_Start_3G */
275 bitvec_set_bit(bv, 0);
276 /* No Absolute_Index_Start_EMR */
277 bitvec_set_bit(bv, 0);
278
279 /* UTRAN FDD Description */
280 bitvec_set_bit(bv, 1);
281 /* No Bandwidth_FDD */
282 bitvec_set_bit(bv, 0);
283
Maxf39d03a2017-05-12 17:00:30 +0200284 for (i = bts->u_offset; i < bts->si_common.uarfcn_length; i++) {
Maxe610e702016-12-19 13:41:48 +0100285 for (j = st, k = 0; j < i; j++)
286 a[k++] = sc[j]; /* copy corresponding SCs */
Maxf39d03a2017-05-12 17:00:30 +0200287
Maxe610e702016-12-19 13:41:48 +0100288 if (u[i] != cu) { /* we've reached new UARFCN */
Maxf39d03a2017-05-12 17:00:30 +0200289 rc = append_utran_fdd_length(cu, a, bts->si_common.uarfcn_length, k);
290 if (rc < 0) { /* estimate bit length requirements */
Maxe610e702016-12-19 13:41:48 +0100291 return rc;
Maxf39d03a2017-05-12 17:00:30 +0200292 }
293
294 if (rem - rc < 0) {
295 break; /* we have ran out of budget in current SI2q */
296 } else {
297 rem -= append_utran_fdd(bv, cu, a, k);
298 bts->u_offset++;
299 }
Maxe610e702016-12-19 13:41:48 +0100300 cu = u[i];
301 st = i; /* update start position */
302 }
303 }
Max26679e02016-04-20 15:57:13 +0200304
Maxf39d03a2017-05-12 17:00:30 +0200305 if (rem > 22) { /* add last UARFCN not covered by previous cycle if it could possibly fit into budget */
306 for (i = st, k = 0; i < bts->si_common.uarfcn_length; i++)
307 a[k++] = sc[i];
308
309 rc = append_utran_fdd_length(cu, a, bts->si_common.uarfcn_length, k);
310 if (rc < 0) {
311 return rc;
312 }
313
314 if (rem - rc >= 0) {
315 rem -= append_utran_fdd(bv, cu, a, k);
316 bts->u_offset++;
317 }
318 }
Max26679e02016-04-20 15:57:13 +0200319
320 /* stop bit - end of Repeated UTRAN FDD Neighbour Cells */
321 bitvec_set_bit(bv, 0);
322
323 /* UTRAN TDD Description */
324 bitvec_set_bit(bv, 0);
Max881064e2016-12-14 14:51:40 +0100325
326 return 0;
Max26679e02016-04-20 15:57:13 +0200327}
328
Max59a1bf32016-04-15 16:04:46 +0200329/* generate SI2quater rest octets: 3GPP TS 44.018 § 10.5.2.33b */
Maxf39d03a2017-05-12 17:00:30 +0200330int rest_octets_si2quater(uint8_t *data, struct gsm_bts *bts)
Max59a1bf32016-04-15 16:04:46 +0200331{
Max881064e2016-12-14 14:51:40 +0100332 int rc;
Max59a1bf32016-04-15 16:04:46 +0200333 struct bitvec bv;
334 bv.data = data;
335 bv.data_len = 20;
336 bitvec_zero(&bv);
337
338 /* BA_IND */
339 bitvec_set_bit(&bv, 1);
340 /* 3G_BA_IND */
341 bitvec_set_bit(&bv, 1);
342 /* MP_CHANGE_MARK */
343 bitvec_set_bit(&bv, 0);
344
Max59a1bf32016-04-15 16:04:46 +0200345 /* SI2quater_INDEX */
Maxf39d03a2017-05-12 17:00:30 +0200346 bitvec_set_uint(&bv, bts->si2q_index, 4);
Max59a1bf32016-04-15 16:04:46 +0200347 /* SI2quater_COUNT */
Maxf39d03a2017-05-12 17:00:30 +0200348 bitvec_set_uint(&bv, bts->si2q_count, 4);
Max59a1bf32016-04-15 16:04:46 +0200349
350 /* No Measurement_Parameters Description */
351 bitvec_set_bit(&bv, 0);
352 /* No GPRS_Real Time Difference Description */
353 bitvec_set_bit(&bv, 0);
354 /* No GPRS_BSIC Description */
355 bitvec_set_bit(&bv, 0);
356 /* No GPRS_REPORT PRIORITY Description */
357 bitvec_set_bit(&bv, 0);
358 /* No GPRS_MEASUREMENT_Parameters Description */
359 bitvec_set_bit(&bv, 0);
360 /* No NC Measurement Parameters */
361 bitvec_set_bit(&bv, 0);
362 /* No extension (length) */
363 bitvec_set_bit(&bv, 0);
364
Maxf39d03a2017-05-12 17:00:30 +0200365 if (bts->si_common.uarfcn_length) {
Max26679e02016-04-20 15:57:13 +0200366 /* Even if we do not append EARFCN we still need to set 3 bits */
Maxf39d03a2017-05-12 17:00:30 +0200367 rc = append_uarfcns(&bv, bts, SI2Q_MAX_LEN - (bv.cur_bit + 3));
Max881064e2016-12-14 14:51:40 +0100368 if (rc < 0) {
Maxf39d03a2017-05-12 17:00:30 +0200369 LOGP(DRR, LOGL_ERROR, "SI2quater: failed to append %zu UARFCNs due to range encoding failure: %s\n",
370 bts->si_common.uarfcn_length, strerror(-rc));
Max881064e2016-12-14 14:51:40 +0100371 return rc;
372 }
Max59a1bf32016-04-15 16:04:46 +0200373 } else { /* No 3G Neighbour Cell Description */
374 bitvec_set_bit(&bv, 0);
375 }
376
377 /* No 3G Measurement Parameters Description */
378 bitvec_set_bit(&bv, 0);
379 /* No GPRS_3G_MEASUREMENT Parameters Descr. */
380 bitvec_set_bit(&bv, 0);
381
Max845a3a42017-05-15 12:02:29 +0200382 if (si2q_earfcn_count(&bts->si_common.si2quater_neigh_list)) {
Maxf39d03a2017-05-12 17:00:30 +0200383 append_earfcn(&bv, bts, SI2Q_MAX_LEN - bv.cur_bit);
384
385 /* FIXME: remove following check once multiple SI2q are properly supported */
386 if ((bts->e_offset != si2q_earfcn_count(&bts->si_common.si2quater_neigh_list)) ||
387 si2q_earfcn_count(&bts->si_common.si2quater_neigh_list) > 5)
Max59a1bf32016-04-15 16:04:46 +0200388 return -ENOMEM;
Max59a1bf32016-04-15 16:04:46 +0200389 } else {
390 /* No Additions in Rel-5: */
391 bitvec_set_bit(&bv, L);
392 }
393
394 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
395 return bv.data_len;
396}
397
Harald Weltea43f7892009-12-01 18:04:30 +0530398/* Append selection parameters to bitvec */
399static void append_selection_params(struct bitvec *bv,
400 const struct gsm48_si_selection_params *sp)
401{
402 if (sp->present) {
403 bitvec_set_bit(bv, H);
404 bitvec_set_bit(bv, sp->cbq);
405 bitvec_set_uint(bv, sp->cell_resel_off, 6);
406 bitvec_set_uint(bv, sp->temp_offs, 3);
407 bitvec_set_uint(bv, sp->penalty_time, 5);
408 } else
409 bitvec_set_bit(bv, L);
410}
411
412/* Append power offset to bitvec */
413static void append_power_offset(struct bitvec *bv,
414 const struct gsm48_si_power_offset *po)
415{
416 if (po->present) {
417 bitvec_set_bit(bv, H);
418 bitvec_set_uint(bv, po->power_offset, 2);
419 } else
420 bitvec_set_bit(bv, L);
421}
422
423/* Append GPRS indicator to bitvec */
424static void append_gprs_ind(struct bitvec *bv,
425 const struct gsm48_si3_gprs_ind *gi)
426{
427 if (gi->present) {
428 bitvec_set_bit(bv, H);
429 bitvec_set_uint(bv, gi->ra_colour, 3);
430 /* 0 == SI13 in BCCH Norm, 1 == SI13 sent on BCCH Ext */
431 bitvec_set_bit(bv, gi->si13_position);
432 } else
433 bitvec_set_bit(bv, L);
434}
435
436
437/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200438int rest_octets_si3(uint8_t *data, const struct gsm48_si_ro_info *si3)
Harald Weltea43f7892009-12-01 18:04:30 +0530439{
440 struct bitvec bv;
441
442 memset(&bv, 0, sizeof(bv));
443 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100444 bv.data_len = 4;
Harald Weltea43f7892009-12-01 18:04:30 +0530445
446 /* Optional Selection Parameters */
447 append_selection_params(&bv, &si3->selection_params);
448
449 /* Optional Power Offset */
450 append_power_offset(&bv, &si3->power_offset);
451
452 /* Do we have a SI2ter on the BCCH? */
453 if (si3->si2ter_indicator)
454 bitvec_set_bit(&bv, H);
455 else
456 bitvec_set_bit(&bv, L);
457
458 /* Early Classmark Sending Control */
459 if (si3->early_cm_ctrl)
460 bitvec_set_bit(&bv, H);
461 else
462 bitvec_set_bit(&bv, L);
463
464 /* Do we have a SI Type 9 on the BCCH? */
465 if (si3->scheduling.present) {
466 bitvec_set_bit(&bv, H);
467 bitvec_set_uint(&bv, si3->scheduling.where, 3);
468 } else
469 bitvec_set_bit(&bv, L);
470
471 /* GPRS Indicator */
472 append_gprs_ind(&bv, &si3->gprs_ind);
473
Maxf3f35052016-04-15 16:04:44 +0200474 /* 3G Early Classmark Sending Restriction controlled by
475 * early_cm_ctrl above */
476 bitvec_set_bit(&bv, H);
477
478 if (si3->si2quater_indicator) {
479 bitvec_set_bit(&bv, H); /* indicator struct present */
480 bitvec_set_uint(&bv, 0, 1); /* message is sent on BCCH Norm */
481 }
482
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100483 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
484 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530485}
486
487static int append_lsa_params(struct bitvec *bv,
488 const struct gsm48_lsa_params *lsa_params)
489{
490 /* FIXME */
Holger Hans Peter Freytherae80f922010-04-10 00:05:16 +0200491 return -1;
Harald Weltea43f7892009-12-01 18:04:30 +0530492}
493
494/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100495int rest_octets_si4(uint8_t *data, const struct gsm48_si_ro_info *si4, int len)
Harald Weltea43f7892009-12-01 18:04:30 +0530496{
497 struct bitvec bv;
498
499 memset(&bv, 0, sizeof(bv));
500 bv.data = data;
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100501 bv.data_len = len;
Harald Weltea43f7892009-12-01 18:04:30 +0530502
503 /* SI4 Rest Octets O */
504 append_selection_params(&bv, &si4->selection_params);
505 append_power_offset(&bv, &si4->power_offset);
506 append_gprs_ind(&bv, &si4->gprs_ind);
507
508 if (0 /* FIXME */) {
509 /* H and SI4 Rest Octets S */
510 bitvec_set_bit(&bv, H);
511
512 /* LSA Parameters */
513 if (si4->lsa_params.present) {
514 bitvec_set_bit(&bv, H);
515 append_lsa_params(&bv, &si4->lsa_params);
516 } else
517 bitvec_set_bit(&bv, L);
518
519 /* Cell Identity */
520 if (1) {
521 bitvec_set_bit(&bv, H);
522 bitvec_set_uint(&bv, si4->cell_id, 16);
523 } else
524 bitvec_set_bit(&bv, L);
525
526 /* LSA ID Information */
527 if (0) {
528 bitvec_set_bit(&bv, H);
529 /* FIXME */
530 } else
531 bitvec_set_bit(&bv, L);
532 } else {
533 /* L and break indicator */
534 bitvec_set_bit(&bv, L);
535 bitvec_set_bit(&bv, si4->break_ind ? H : L);
536 }
537
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100538 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530539}
540
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +0200541
542/* GSM 04.18 ETSI TS 101 503 V8.27.0 (2006-05)
543
544<SI6 rest octets> ::=
545{L | H <PCH and NCH info>}
546{L | H <VBS/VGCS options : bit(2)>}
547{ < DTM_support : bit == L > I < DTM_support : bit == H >
548< RAC : bit (8) >
549< MAX_LAPDm : bit (3) > }
550< Band indicator >
551{ L | H < GPRS_MS_TXPWR_MAX_CCH : bit (5) > }
552<implicit spare >;
553*/
554int rest_octets_si6(uint8_t *data, bool is1800_net)
555{
556 struct bitvec bv;
557
558 memset(&bv, 0, sizeof(bv));
559 bv.data = data;
560 bv.data_len = 1;
561
562 /* no PCH/NCH info */
563 bitvec_set_bit(&bv, L);
564 /* no VBS/VGCS options */
565 bitvec_set_bit(&bv, L);
566 /* no DTM_support */
567 bitvec_set_bit(&bv, L);
568 /* band indicator */
569 if (is1800_net)
570 bitvec_set_bit(&bv, L);
571 else
572 bitvec_set_bit(&bv, H);
573 /* no GPRS_MS_TXPWR_MAX_CCH */
574 bitvec_set_bit(&bv, L);
575
576 bitvec_spare_padding(&bv, (bv.data_len * 8) - 1);
577 return bv.data_len;
578}
579
Harald Weltea43f7892009-12-01 18:04:30 +0530580/* GPRS Mobile Allocation as per TS 04.60 Chapter 12.10a:
581 < GPRS Mobile Allocation IE > ::=
582 < HSN : bit (6) >
583 { 0 | 1 < RFL number list : < RFL number list struct > > }
584 { 0 < MA_LENGTH : bit (6) >
585 < MA_BITMAP: bit (val(MA_LENGTH) + 1) >
586 | 1 { 0 | 1 <ARFCN index list : < ARFCN index list struct > > } } ;
587
588 < RFL number list struct > :: =
589 < RFL_NUMBER : bit (4) >
590 { 0 | 1 < RFL number list struct > } ;
591 < ARFCN index list struct > ::=
592 < ARFCN_INDEX : bit(6) >
593 { 0 | 1 < ARFCN index list struct > } ;
594 */
595static int append_gprs_mobile_alloc(struct bitvec *bv)
596{
597 /* Hopping Sequence Number */
598 bitvec_set_uint(bv, 0, 6);
599
600 if (0) {
601 /* We want to use a RFL number list */
602 bitvec_set_bit(bv, 1);
603 /* FIXME: RFL number list */
604 } else
605 bitvec_set_bit(bv, 0);
606
607 if (0) {
608 /* We want to use a MA_BITMAP */
609 bitvec_set_bit(bv, 0);
610 /* FIXME: MA_LENGTH, MA_BITMAP, ... */
611 } else {
612 bitvec_set_bit(bv, 1);
613 if (0) {
614 /* We want to provide an ARFCN index list */
615 bitvec_set_bit(bv, 1);
616 /* FIXME */
617 } else
618 bitvec_set_bit(bv, 0);
619 }
620 return 0;
621}
622
623static int encode_t3192(unsigned int t3192)
624{
Philipp Maier3d6cb332016-12-20 14:23:45 +0100625 /* See also 3GPP TS 44.060
626 Table 12.24.2: GPRS Cell Options information element details */
Harald Weltea43f7892009-12-01 18:04:30 +0530627 if (t3192 == 0)
628 return 3;
629 else if (t3192 <= 80)
630 return 4;
631 else if (t3192 <= 120)
632 return 5;
633 else if (t3192 <= 160)
634 return 6;
635 else if (t3192 <= 200)
636 return 7;
637 else if (t3192 <= 500)
638 return 0;
639 else if (t3192 <= 1000)
640 return 1;
641 else if (t3192 <= 1500)
642 return 2;
643 else
644 return -EINVAL;
645}
646
647static int encode_drx_timer(unsigned int drx)
648{
649 if (drx == 0)
650 return 0;
651 else if (drx == 1)
652 return 1;
653 else if (drx == 2)
654 return 2;
655 else if (drx <= 4)
656 return 3;
657 else if (drx <= 8)
658 return 4;
659 else if (drx <= 16)
660 return 5;
661 else if (drx <= 32)
662 return 6;
663 else if (drx <= 64)
664 return 7;
665 else
666 return -EINVAL;
667}
668
669/* GPRS Cell Options as per TS 04.60 Chapter 12.24
670 < GPRS Cell Options IE > ::=
671 < NMO : bit(2) >
672 < T3168 : bit(3) >
673 < T3192 : bit(3) >
674 < DRX_TIMER_MAX: bit(3) >
675 < ACCESS_BURST_TYPE: bit >
676 < CONTROL_ACK_TYPE : bit >
677 < BS_CV_MAX: bit(4) >
678 { 0 | 1 < PAN_DEC : bit(3) >
679 < PAN_INC : bit(3) >
680 < PAN_MAX : bit(3) >
681 { 0 | 1 < Extension Length : bit(6) >
682 < bit (val(Extension Length) + 1
683 & { < Extension Information > ! { bit ** = <no string> } } ;
684 < Extension Information > ::=
685 { 0 | 1 < EGPRS_PACKET_CHANNEL_REQUEST : bit >
686 < BEP_PERIOD : bit(4) > }
687 < PFC_FEATURE_MODE : bit >
688 < DTM_SUPPORT : bit >
689 <BSS_PAGING_COORDINATION: bit >
690 <spare bit > ** ;
691 */
Harald Weltea4e2d042009-12-20 17:08:22 +0100692static int append_gprs_cell_opt(struct bitvec *bv,
693 const struct gprs_cell_options *gco)
Harald Weltea43f7892009-12-01 18:04:30 +0530694{
695 int t3192, drx_timer_max;
696
697 t3192 = encode_t3192(gco->t3192);
698 if (t3192 < 0)
699 return t3192;
700
701 drx_timer_max = encode_drx_timer(gco->drx_timer_max);
702 if (drx_timer_max < 0)
703 return drx_timer_max;
704
705 bitvec_set_uint(bv, gco->nmo, 2);
Philipp Maier3d6cb332016-12-20 14:23:45 +0100706
707 /* See also 3GPP TS 44.060
708 Table 12.24.2: GPRS Cell Options information element details */
709 bitvec_set_uint(bv, gco->t3168 / 500 - 1, 3);
710
Harald Weltea43f7892009-12-01 18:04:30 +0530711 bitvec_set_uint(bv, t3192, 3);
712 bitvec_set_uint(bv, drx_timer_max, 3);
713 /* ACCESS_BURST_TYPE: Hard-code 8bit */
714 bitvec_set_bit(bv, 0);
Max292ec582016-07-28 11:55:37 +0200715 /* CONTROL_ACK_TYPE: */
716 bitvec_set_bit(bv, gco->ctrl_ack_type_use_block);
Harald Weltea43f7892009-12-01 18:04:30 +0530717 bitvec_set_uint(bv, gco->bs_cv_max, 4);
718
Harald Weltea4b16652010-05-31 12:54:23 +0200719 if (0) {
720 /* hard-code no PAN_{DEC,INC,MAX} */
721 bitvec_set_bit(bv, 0);
722 } else {
723 /* copied from ip.access BSC protocol trace */
724 bitvec_set_bit(bv, 1);
725 bitvec_set_uint(bv, 1, 3); /* DEC */
726 bitvec_set_uint(bv, 1, 3); /* INC */
727 bitvec_set_uint(bv, 15, 3); /* MAX */
728 }
Harald Weltea43f7892009-12-01 18:04:30 +0530729
Harald Welteda0586a2010-04-18 14:49:05 +0200730 if (!gco->ext_info_present) {
731 /* no extension information */
732 bitvec_set_bit(bv, 0);
733 } else {
734 /* extension information */
735 bitvec_set_bit(bv, 1);
736 if (!gco->ext_info.egprs_supported) {
737 /* 6bit length of extension */
Harald Welte39608dc2010-04-18 22:47:22 +0200738 bitvec_set_uint(bv, (1 + 3)-1, 6);
Harald Welteda0586a2010-04-18 14:49:05 +0200739 /* EGPRS supported in the cell */
740 bitvec_set_bit(bv, 0);
741 } else {
742 /* 6bit length of extension */
Harald Welte39608dc2010-04-18 22:47:22 +0200743 bitvec_set_uint(bv, (1 + 5 + 3)-1, 6);
Harald Welteda0586a2010-04-18 14:49:05 +0200744 /* EGPRS supported in the cell */
745 bitvec_set_bit(bv, 1);
bhargava350533c2016-07-21 11:14:34 +0530746
Harald Welteda0586a2010-04-18 14:49:05 +0200747 /* 1bit EGPRS PACKET CHANNEL REQUEST */
bhargava350533c2016-07-21 11:14:34 +0530748 if (gco->supports_egprs_11bit_rach == 0) {
749 bitvec_set_bit(bv,
750 gco->ext_info.use_egprs_p_ch_req);
751 } else {
752 bitvec_set_bit(bv, 0);
753 }
754
Harald Welteda0586a2010-04-18 14:49:05 +0200755 /* 4bit BEP PERIOD */
756 bitvec_set_uint(bv, gco->ext_info.bep_period, 4);
757 }
758 bitvec_set_bit(bv, gco->ext_info.pfc_supported);
759 bitvec_set_bit(bv, gco->ext_info.dtm_supported);
760 bitvec_set_bit(bv, gco->ext_info.bss_paging_coordination);
761 }
Harald Weltea43f7892009-12-01 18:04:30 +0530762
763 return 0;
764}
765
766static void append_gprs_pwr_ctrl_pars(struct bitvec *bv,
Harald Weltea4e2d042009-12-20 17:08:22 +0100767 const struct gprs_power_ctrl_pars *pcp)
Harald Weltea43f7892009-12-01 18:04:30 +0530768{
769 bitvec_set_uint(bv, pcp->alpha, 4);
770 bitvec_set_uint(bv, pcp->t_avg_w, 5);
771 bitvec_set_uint(bv, pcp->t_avg_t, 5);
772 bitvec_set_uint(bv, pcp->pc_meas_chan, 1);
773 bitvec_set_uint(bv, pcp->n_avg_i, 4);
774}
775
Harald Welte5fda9082010-04-18 22:41:01 +0200776/* Generate SI13 Rest Octests (04.08 Chapter 10.5.2.37b) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200777int rest_octets_si13(uint8_t *data, const struct gsm48_si13_info *si13)
Harald Weltea43f7892009-12-01 18:04:30 +0530778{
779 struct bitvec bv;
780
781 memset(&bv, 0, sizeof(bv));
782 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100783 bv.data_len = 20;
Harald Weltea43f7892009-12-01 18:04:30 +0530784
785 if (0) {
786 /* No rest octets */
787 bitvec_set_bit(&bv, L);
788 } else {
789 bitvec_set_bit(&bv, H);
790 bitvec_set_uint(&bv, si13->bcch_change_mark, 3);
791 bitvec_set_uint(&bv, si13->si_change_field, 4);
Harald Welte97a282b2010-03-14 15:37:43 +0800792 if (1) {
Harald Weltea43f7892009-12-01 18:04:30 +0530793 bitvec_set_bit(&bv, 0);
794 } else {
795 bitvec_set_bit(&bv, 1);
796 bitvec_set_uint(&bv, si13->bcch_change_mark, 2);
797 append_gprs_mobile_alloc(&bv);
798 }
799 if (!si13->pbcch_present) {
800 /* PBCCH not present in cell */
801 bitvec_set_bit(&bv, 0);
802 bitvec_set_uint(&bv, si13->no_pbcch.rac, 8);
803 bitvec_set_bit(&bv, si13->no_pbcch.spgc_ccch_sup);
804 bitvec_set_uint(&bv, si13->no_pbcch.prio_acc_thr, 3);
805 bitvec_set_uint(&bv, si13->no_pbcch.net_ctrl_ord, 2);
806 append_gprs_cell_opt(&bv, &si13->cell_opts);
807 append_gprs_pwr_ctrl_pars(&bv, &si13->pwr_ctrl_pars);
808 } else {
809 /* PBCCH present in cell */
810 bitvec_set_bit(&bv, 1);
811 bitvec_set_uint(&bv, si13->pbcch.psi1_rep_per, 4);
812 /* PBCCH Descripiton */
813 bitvec_set_uint(&bv, si13->pbcch.pb, 4);
814 bitvec_set_uint(&bv, si13->pbcch.tsc, 3);
815 bitvec_set_uint(&bv, si13->pbcch.tn, 3);
816 switch (si13->pbcch.carrier_type) {
817 case PBCCH_BCCH:
818 bitvec_set_bit(&bv, 0);
819 bitvec_set_bit(&bv, 0);
820 break;
821 case PBCCH_ARFCN:
822 bitvec_set_bit(&bv, 0);
823 bitvec_set_bit(&bv, 1);
824 bitvec_set_uint(&bv, si13->pbcch.arfcn, 10);
825 break;
826 case PBCCH_MAIO:
827 bitvec_set_bit(&bv, 1);
828 bitvec_set_uint(&bv, si13->pbcch.maio, 6);
829 break;
830 }
831 }
Harald Welte5fda9082010-04-18 22:41:01 +0200832 /* 3GPP TS 44.018 Release 6 / 10.5.2.37b */
833 bitvec_set_bit(&bv, H); /* added Release 99 */
834 /* claim our SGSN is compatible with Release 99, as EDGE and EGPRS
835 * was only added in this Release */
836 bitvec_set_bit(&bv, 1);
Harald Weltea43f7892009-12-01 18:04:30 +0530837 }
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100838 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
839 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530840}