blob: 4545794a91332b07e219c38260a123cd9cae185f [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>
27
28#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010029#include <osmocom/core/bitvec.h>
Harald Weltea43f7892009-12-01 18:04:30 +053030#include <openbsc/rest_octets.h>
31
32/* generate SI1 rest octets */
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +010033int rest_octets_si1(uint8_t *data, uint8_t *nch_pos, int is1800_net)
Harald Weltea43f7892009-12-01 18:04:30 +053034{
35 struct bitvec bv;
36
37 memset(&bv, 0, sizeof(bv));
38 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +010039 bv.data_len = 1;
Harald Weltea43f7892009-12-01 18:04:30 +053040
41 if (nch_pos) {
42 bitvec_set_bit(&bv, H);
43 bitvec_set_uint(&bv, *nch_pos, 5);
44 } else
45 bitvec_set_bit(&bv, L);
46
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +010047 if (is1800_net)
48 bitvec_set_bit(&bv, L);
49 else
50 bitvec_set_bit(&bv, H);
51
52 bitvec_spare_padding(&bv, 6);
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +010053 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +053054}
55
56/* Append selection parameters to bitvec */
57static void append_selection_params(struct bitvec *bv,
58 const struct gsm48_si_selection_params *sp)
59{
60 if (sp->present) {
61 bitvec_set_bit(bv, H);
62 bitvec_set_bit(bv, sp->cbq);
63 bitvec_set_uint(bv, sp->cell_resel_off, 6);
64 bitvec_set_uint(bv, sp->temp_offs, 3);
65 bitvec_set_uint(bv, sp->penalty_time, 5);
66 } else
67 bitvec_set_bit(bv, L);
68}
69
70/* Append power offset to bitvec */
71static void append_power_offset(struct bitvec *bv,
72 const struct gsm48_si_power_offset *po)
73{
74 if (po->present) {
75 bitvec_set_bit(bv, H);
76 bitvec_set_uint(bv, po->power_offset, 2);
77 } else
78 bitvec_set_bit(bv, L);
79}
80
81/* Append GPRS indicator to bitvec */
82static void append_gprs_ind(struct bitvec *bv,
83 const struct gsm48_si3_gprs_ind *gi)
84{
85 if (gi->present) {
86 bitvec_set_bit(bv, H);
87 bitvec_set_uint(bv, gi->ra_colour, 3);
88 /* 0 == SI13 in BCCH Norm, 1 == SI13 sent on BCCH Ext */
89 bitvec_set_bit(bv, gi->si13_position);
90 } else
91 bitvec_set_bit(bv, L);
92}
93
94
95/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020096int rest_octets_si3(uint8_t *data, const struct gsm48_si_ro_info *si3)
Harald Weltea43f7892009-12-01 18:04:30 +053097{
98 struct bitvec bv;
99
100 memset(&bv, 0, sizeof(bv));
101 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100102 bv.data_len = 4;
Harald Weltea43f7892009-12-01 18:04:30 +0530103
104 /* Optional Selection Parameters */
105 append_selection_params(&bv, &si3->selection_params);
106
107 /* Optional Power Offset */
108 append_power_offset(&bv, &si3->power_offset);
109
110 /* Do we have a SI2ter on the BCCH? */
111 if (si3->si2ter_indicator)
112 bitvec_set_bit(&bv, H);
113 else
114 bitvec_set_bit(&bv, L);
115
116 /* Early Classmark Sending Control */
117 if (si3->early_cm_ctrl)
118 bitvec_set_bit(&bv, H);
119 else
120 bitvec_set_bit(&bv, L);
121
122 /* Do we have a SI Type 9 on the BCCH? */
123 if (si3->scheduling.present) {
124 bitvec_set_bit(&bv, H);
125 bitvec_set_uint(&bv, si3->scheduling.where, 3);
126 } else
127 bitvec_set_bit(&bv, L);
128
129 /* GPRS Indicator */
130 append_gprs_ind(&bv, &si3->gprs_ind);
131
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100132 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
133 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530134}
135
136static int append_lsa_params(struct bitvec *bv,
137 const struct gsm48_lsa_params *lsa_params)
138{
139 /* FIXME */
Holger Hans Peter Freytherae80f922010-04-10 00:05:16 +0200140 return -1;
Harald Weltea43f7892009-12-01 18:04:30 +0530141}
142
143/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100144int rest_octets_si4(uint8_t *data, const struct gsm48_si_ro_info *si4, int len)
Harald Weltea43f7892009-12-01 18:04:30 +0530145{
146 struct bitvec bv;
147
148 memset(&bv, 0, sizeof(bv));
149 bv.data = data;
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100150 bv.data_len = len;
Harald Weltea43f7892009-12-01 18:04:30 +0530151
152 /* SI4 Rest Octets O */
153 append_selection_params(&bv, &si4->selection_params);
154 append_power_offset(&bv, &si4->power_offset);
155 append_gprs_ind(&bv, &si4->gprs_ind);
156
157 if (0 /* FIXME */) {
158 /* H and SI4 Rest Octets S */
159 bitvec_set_bit(&bv, H);
160
161 /* LSA Parameters */
162 if (si4->lsa_params.present) {
163 bitvec_set_bit(&bv, H);
164 append_lsa_params(&bv, &si4->lsa_params);
165 } else
166 bitvec_set_bit(&bv, L);
167
168 /* Cell Identity */
169 if (1) {
170 bitvec_set_bit(&bv, H);
171 bitvec_set_uint(&bv, si4->cell_id, 16);
172 } else
173 bitvec_set_bit(&bv, L);
174
175 /* LSA ID Information */
176 if (0) {
177 bitvec_set_bit(&bv, H);
178 /* FIXME */
179 } else
180 bitvec_set_bit(&bv, L);
181 } else {
182 /* L and break indicator */
183 bitvec_set_bit(&bv, L);
184 bitvec_set_bit(&bv, si4->break_ind ? H : L);
185 }
186
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100187 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530188}
189
190/* GPRS Mobile Allocation as per TS 04.60 Chapter 12.10a:
191 < GPRS Mobile Allocation IE > ::=
192 < HSN : bit (6) >
193 { 0 | 1 < RFL number list : < RFL number list struct > > }
194 { 0 < MA_LENGTH : bit (6) >
195 < MA_BITMAP: bit (val(MA_LENGTH) + 1) >
196 | 1 { 0 | 1 <ARFCN index list : < ARFCN index list struct > > } } ;
197
198 < RFL number list struct > :: =
199 < RFL_NUMBER : bit (4) >
200 { 0 | 1 < RFL number list struct > } ;
201 < ARFCN index list struct > ::=
202 < ARFCN_INDEX : bit(6) >
203 { 0 | 1 < ARFCN index list struct > } ;
204 */
205static int append_gprs_mobile_alloc(struct bitvec *bv)
206{
207 /* Hopping Sequence Number */
208 bitvec_set_uint(bv, 0, 6);
209
210 if (0) {
211 /* We want to use a RFL number list */
212 bitvec_set_bit(bv, 1);
213 /* FIXME: RFL number list */
214 } else
215 bitvec_set_bit(bv, 0);
216
217 if (0) {
218 /* We want to use a MA_BITMAP */
219 bitvec_set_bit(bv, 0);
220 /* FIXME: MA_LENGTH, MA_BITMAP, ... */
221 } else {
222 bitvec_set_bit(bv, 1);
223 if (0) {
224 /* We want to provide an ARFCN index list */
225 bitvec_set_bit(bv, 1);
226 /* FIXME */
227 } else
228 bitvec_set_bit(bv, 0);
229 }
230 return 0;
231}
232
233static int encode_t3192(unsigned int t3192)
234{
235 if (t3192 == 0)
236 return 3;
237 else if (t3192 <= 80)
238 return 4;
239 else if (t3192 <= 120)
240 return 5;
241 else if (t3192 <= 160)
242 return 6;
243 else if (t3192 <= 200)
244 return 7;
245 else if (t3192 <= 500)
246 return 0;
247 else if (t3192 <= 1000)
248 return 1;
249 else if (t3192 <= 1500)
250 return 2;
251 else
252 return -EINVAL;
253}
254
255static int encode_drx_timer(unsigned int drx)
256{
257 if (drx == 0)
258 return 0;
259 else if (drx == 1)
260 return 1;
261 else if (drx == 2)
262 return 2;
263 else if (drx <= 4)
264 return 3;
265 else if (drx <= 8)
266 return 4;
267 else if (drx <= 16)
268 return 5;
269 else if (drx <= 32)
270 return 6;
271 else if (drx <= 64)
272 return 7;
273 else
274 return -EINVAL;
275}
276
277/* GPRS Cell Options as per TS 04.60 Chapter 12.24
278 < GPRS Cell Options IE > ::=
279 < NMO : bit(2) >
280 < T3168 : bit(3) >
281 < T3192 : bit(3) >
282 < DRX_TIMER_MAX: bit(3) >
283 < ACCESS_BURST_TYPE: bit >
284 < CONTROL_ACK_TYPE : bit >
285 < BS_CV_MAX: bit(4) >
286 { 0 | 1 < PAN_DEC : bit(3) >
287 < PAN_INC : bit(3) >
288 < PAN_MAX : bit(3) >
289 { 0 | 1 < Extension Length : bit(6) >
290 < bit (val(Extension Length) + 1
291 & { < Extension Information > ! { bit ** = <no string> } } ;
292 < Extension Information > ::=
293 { 0 | 1 < EGPRS_PACKET_CHANNEL_REQUEST : bit >
294 < BEP_PERIOD : bit(4) > }
295 < PFC_FEATURE_MODE : bit >
296 < DTM_SUPPORT : bit >
297 <BSS_PAGING_COORDINATION: bit >
298 <spare bit > ** ;
299 */
Harald Weltea4e2d042009-12-20 17:08:22 +0100300static int append_gprs_cell_opt(struct bitvec *bv,
301 const struct gprs_cell_options *gco)
Harald Weltea43f7892009-12-01 18:04:30 +0530302{
303 int t3192, drx_timer_max;
304
305 t3192 = encode_t3192(gco->t3192);
306 if (t3192 < 0)
307 return t3192;
308
309 drx_timer_max = encode_drx_timer(gco->drx_timer_max);
310 if (drx_timer_max < 0)
311 return drx_timer_max;
312
313 bitvec_set_uint(bv, gco->nmo, 2);
314 bitvec_set_uint(bv, gco->t3168 / 500, 3);
315 bitvec_set_uint(bv, t3192, 3);
316 bitvec_set_uint(bv, drx_timer_max, 3);
317 /* ACCESS_BURST_TYPE: Hard-code 8bit */
318 bitvec_set_bit(bv, 0);
319 /* CONTROL_ACK_TYPE: Hard-code to RLC/MAC control block */
320 bitvec_set_bit(bv, 1);
321 bitvec_set_uint(bv, gco->bs_cv_max, 4);
322
Harald Weltea4b16652010-05-31 12:54:23 +0200323 if (0) {
324 /* hard-code no PAN_{DEC,INC,MAX} */
325 bitvec_set_bit(bv, 0);
326 } else {
327 /* copied from ip.access BSC protocol trace */
328 bitvec_set_bit(bv, 1);
329 bitvec_set_uint(bv, 1, 3); /* DEC */
330 bitvec_set_uint(bv, 1, 3); /* INC */
331 bitvec_set_uint(bv, 15, 3); /* MAX */
332 }
Harald Weltea43f7892009-12-01 18:04:30 +0530333
Harald Welteda0586a2010-04-18 14:49:05 +0200334 if (!gco->ext_info_present) {
335 /* no extension information */
336 bitvec_set_bit(bv, 0);
337 } else {
338 /* extension information */
339 bitvec_set_bit(bv, 1);
340 if (!gco->ext_info.egprs_supported) {
341 /* 6bit length of extension */
Harald Welte39608dc2010-04-18 22:47:22 +0200342 bitvec_set_uint(bv, (1 + 3)-1, 6);
Harald Welteda0586a2010-04-18 14:49:05 +0200343 /* EGPRS supported in the cell */
344 bitvec_set_bit(bv, 0);
345 } else {
346 /* 6bit length of extension */
Harald Welte39608dc2010-04-18 22:47:22 +0200347 bitvec_set_uint(bv, (1 + 5 + 3)-1, 6);
Harald Welteda0586a2010-04-18 14:49:05 +0200348 /* EGPRS supported in the cell */
349 bitvec_set_bit(bv, 1);
350 /* 1bit EGPRS PACKET CHANNEL REQUEST */
351 bitvec_set_bit(bv, gco->ext_info.use_egprs_p_ch_req);
352 /* 4bit BEP PERIOD */
353 bitvec_set_uint(bv, gco->ext_info.bep_period, 4);
354 }
355 bitvec_set_bit(bv, gco->ext_info.pfc_supported);
356 bitvec_set_bit(bv, gco->ext_info.dtm_supported);
357 bitvec_set_bit(bv, gco->ext_info.bss_paging_coordination);
358 }
Harald Weltea43f7892009-12-01 18:04:30 +0530359
360 return 0;
361}
362
363static void append_gprs_pwr_ctrl_pars(struct bitvec *bv,
Harald Weltea4e2d042009-12-20 17:08:22 +0100364 const struct gprs_power_ctrl_pars *pcp)
Harald Weltea43f7892009-12-01 18:04:30 +0530365{
366 bitvec_set_uint(bv, pcp->alpha, 4);
367 bitvec_set_uint(bv, pcp->t_avg_w, 5);
368 bitvec_set_uint(bv, pcp->t_avg_t, 5);
369 bitvec_set_uint(bv, pcp->pc_meas_chan, 1);
370 bitvec_set_uint(bv, pcp->n_avg_i, 4);
371}
372
Harald Welte5fda9082010-04-18 22:41:01 +0200373/* Generate SI13 Rest Octests (04.08 Chapter 10.5.2.37b) */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200374int rest_octets_si13(uint8_t *data, const struct gsm48_si13_info *si13)
Harald Weltea43f7892009-12-01 18:04:30 +0530375{
376 struct bitvec bv;
377
378 memset(&bv, 0, sizeof(bv));
379 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100380 bv.data_len = 20;
Harald Weltea43f7892009-12-01 18:04:30 +0530381
382 if (0) {
383 /* No rest octets */
384 bitvec_set_bit(&bv, L);
385 } else {
386 bitvec_set_bit(&bv, H);
387 bitvec_set_uint(&bv, si13->bcch_change_mark, 3);
388 bitvec_set_uint(&bv, si13->si_change_field, 4);
Harald Welte97a282b2010-03-14 15:37:43 +0800389 if (1) {
Harald Weltea43f7892009-12-01 18:04:30 +0530390 bitvec_set_bit(&bv, 0);
391 } else {
392 bitvec_set_bit(&bv, 1);
393 bitvec_set_uint(&bv, si13->bcch_change_mark, 2);
394 append_gprs_mobile_alloc(&bv);
395 }
396 if (!si13->pbcch_present) {
397 /* PBCCH not present in cell */
398 bitvec_set_bit(&bv, 0);
399 bitvec_set_uint(&bv, si13->no_pbcch.rac, 8);
400 bitvec_set_bit(&bv, si13->no_pbcch.spgc_ccch_sup);
401 bitvec_set_uint(&bv, si13->no_pbcch.prio_acc_thr, 3);
402 bitvec_set_uint(&bv, si13->no_pbcch.net_ctrl_ord, 2);
403 append_gprs_cell_opt(&bv, &si13->cell_opts);
404 append_gprs_pwr_ctrl_pars(&bv, &si13->pwr_ctrl_pars);
405 } else {
406 /* PBCCH present in cell */
407 bitvec_set_bit(&bv, 1);
408 bitvec_set_uint(&bv, si13->pbcch.psi1_rep_per, 4);
409 /* PBCCH Descripiton */
410 bitvec_set_uint(&bv, si13->pbcch.pb, 4);
411 bitvec_set_uint(&bv, si13->pbcch.tsc, 3);
412 bitvec_set_uint(&bv, si13->pbcch.tn, 3);
413 switch (si13->pbcch.carrier_type) {
414 case PBCCH_BCCH:
415 bitvec_set_bit(&bv, 0);
416 bitvec_set_bit(&bv, 0);
417 break;
418 case PBCCH_ARFCN:
419 bitvec_set_bit(&bv, 0);
420 bitvec_set_bit(&bv, 1);
421 bitvec_set_uint(&bv, si13->pbcch.arfcn, 10);
422 break;
423 case PBCCH_MAIO:
424 bitvec_set_bit(&bv, 1);
425 bitvec_set_uint(&bv, si13->pbcch.maio, 6);
426 break;
427 }
428 }
Harald Welte5fda9082010-04-18 22:41:01 +0200429 /* 3GPP TS 44.018 Release 6 / 10.5.2.37b */
430 bitvec_set_bit(&bv, H); /* added Release 99 */
431 /* claim our SGSN is compatible with Release 99, as EDGE and EGPRS
432 * was only added in this Release */
433 bitvec_set_bit(&bv, 1);
Harald Weltea43f7892009-12-01 18:04:30 +0530434 }
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100435 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
436 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530437}