blob: 74874bd9a026f16a408e50bd2a3996a6e7b2a0ee [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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
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 General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <string.h>
26#include <stdlib.h>
27#include <errno.h>
28
29#include <openbsc/gsm_data.h>
30#include <openbsc/bitvec.h>
31#include <openbsc/rest_octets.h>
32
33/* generate SI1 rest octets */
34int rest_octets_si1(u_int8_t *data, u_int8_t *nch_pos)
35{
36 struct bitvec bv;
37
38 memset(&bv, 0, sizeof(bv));
39 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +010040 bv.data_len = 1;
Harald Weltea43f7892009-12-01 18:04:30 +053041
42 if (nch_pos) {
43 bitvec_set_bit(&bv, H);
44 bitvec_set_uint(&bv, *nch_pos, 5);
45 } else
46 bitvec_set_bit(&bv, L);
47
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +010048 bitvec_spare_padding(&bv, 7);
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +010049 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +053050}
51
52/* Append selection parameters to bitvec */
53static void append_selection_params(struct bitvec *bv,
54 const struct gsm48_si_selection_params *sp)
55{
56 if (sp->present) {
57 bitvec_set_bit(bv, H);
58 bitvec_set_bit(bv, sp->cbq);
59 bitvec_set_uint(bv, sp->cell_resel_off, 6);
60 bitvec_set_uint(bv, sp->temp_offs, 3);
61 bitvec_set_uint(bv, sp->penalty_time, 5);
62 } else
63 bitvec_set_bit(bv, L);
64}
65
66/* Append power offset to bitvec */
67static void append_power_offset(struct bitvec *bv,
68 const struct gsm48_si_power_offset *po)
69{
70 if (po->present) {
71 bitvec_set_bit(bv, H);
72 bitvec_set_uint(bv, po->power_offset, 2);
73 } else
74 bitvec_set_bit(bv, L);
75}
76
77/* Append GPRS indicator to bitvec */
78static void append_gprs_ind(struct bitvec *bv,
79 const struct gsm48_si3_gprs_ind *gi)
80{
81 if (gi->present) {
82 bitvec_set_bit(bv, H);
83 bitvec_set_uint(bv, gi->ra_colour, 3);
84 /* 0 == SI13 in BCCH Norm, 1 == SI13 sent on BCCH Ext */
85 bitvec_set_bit(bv, gi->si13_position);
86 } else
87 bitvec_set_bit(bv, L);
88}
89
90
91/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
92int rest_octets_si3(u_int8_t *data, const struct gsm48_si_ro_info *si3)
93{
94 struct bitvec bv;
95
96 memset(&bv, 0, sizeof(bv));
97 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +010098 bv.data_len = 4;
Harald Weltea43f7892009-12-01 18:04:30 +053099
100 /* Optional Selection Parameters */
101 append_selection_params(&bv, &si3->selection_params);
102
103 /* Optional Power Offset */
104 append_power_offset(&bv, &si3->power_offset);
105
106 /* Do we have a SI2ter on the BCCH? */
107 if (si3->si2ter_indicator)
108 bitvec_set_bit(&bv, H);
109 else
110 bitvec_set_bit(&bv, L);
111
112 /* Early Classmark Sending Control */
113 if (si3->early_cm_ctrl)
114 bitvec_set_bit(&bv, H);
115 else
116 bitvec_set_bit(&bv, L);
117
118 /* Do we have a SI Type 9 on the BCCH? */
119 if (si3->scheduling.present) {
120 bitvec_set_bit(&bv, H);
121 bitvec_set_uint(&bv, si3->scheduling.where, 3);
122 } else
123 bitvec_set_bit(&bv, L);
124
125 /* GPRS Indicator */
126 append_gprs_ind(&bv, &si3->gprs_ind);
127
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100128 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
129 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530130}
131
132static int append_lsa_params(struct bitvec *bv,
133 const struct gsm48_lsa_params *lsa_params)
134{
135 /* FIXME */
136}
137
138/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
139int rest_octets_si4(u_int8_t *data, const struct gsm48_si_ro_info *si4)
140{
141 struct bitvec bv;
142
143 memset(&bv, 0, sizeof(bv));
144 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100145 bv.data_len = 10; /* FIXME: up to ? */
Harald Weltea43f7892009-12-01 18:04:30 +0530146
147 /* SI4 Rest Octets O */
148 append_selection_params(&bv, &si4->selection_params);
149 append_power_offset(&bv, &si4->power_offset);
150 append_gprs_ind(&bv, &si4->gprs_ind);
151
152 if (0 /* FIXME */) {
153 /* H and SI4 Rest Octets S */
154 bitvec_set_bit(&bv, H);
155
156 /* LSA Parameters */
157 if (si4->lsa_params.present) {
158 bitvec_set_bit(&bv, H);
159 append_lsa_params(&bv, &si4->lsa_params);
160 } else
161 bitvec_set_bit(&bv, L);
162
163 /* Cell Identity */
164 if (1) {
165 bitvec_set_bit(&bv, H);
166 bitvec_set_uint(&bv, si4->cell_id, 16);
167 } else
168 bitvec_set_bit(&bv, L);
169
170 /* LSA ID Information */
171 if (0) {
172 bitvec_set_bit(&bv, H);
173 /* FIXME */
174 } else
175 bitvec_set_bit(&bv, L);
176 } else {
177 /* L and break indicator */
178 bitvec_set_bit(&bv, L);
179 bitvec_set_bit(&bv, si4->break_ind ? H : L);
180 }
181
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100182 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530183}
184
185/* GPRS Mobile Allocation as per TS 04.60 Chapter 12.10a:
186 < GPRS Mobile Allocation IE > ::=
187 < HSN : bit (6) >
188 { 0 | 1 < RFL number list : < RFL number list struct > > }
189 { 0 < MA_LENGTH : bit (6) >
190 < MA_BITMAP: bit (val(MA_LENGTH) + 1) >
191 | 1 { 0 | 1 <ARFCN index list : < ARFCN index list struct > > } } ;
192
193 < RFL number list struct > :: =
194 < RFL_NUMBER : bit (4) >
195 { 0 | 1 < RFL number list struct > } ;
196 < ARFCN index list struct > ::=
197 < ARFCN_INDEX : bit(6) >
198 { 0 | 1 < ARFCN index list struct > } ;
199 */
200static int append_gprs_mobile_alloc(struct bitvec *bv)
201{
202 /* Hopping Sequence Number */
203 bitvec_set_uint(bv, 0, 6);
204
205 if (0) {
206 /* We want to use a RFL number list */
207 bitvec_set_bit(bv, 1);
208 /* FIXME: RFL number list */
209 } else
210 bitvec_set_bit(bv, 0);
211
212 if (0) {
213 /* We want to use a MA_BITMAP */
214 bitvec_set_bit(bv, 0);
215 /* FIXME: MA_LENGTH, MA_BITMAP, ... */
216 } else {
217 bitvec_set_bit(bv, 1);
218 if (0) {
219 /* We want to provide an ARFCN index list */
220 bitvec_set_bit(bv, 1);
221 /* FIXME */
222 } else
223 bitvec_set_bit(bv, 0);
224 }
225 return 0;
226}
227
228static int encode_t3192(unsigned int t3192)
229{
230 if (t3192 == 0)
231 return 3;
232 else if (t3192 <= 80)
233 return 4;
234 else if (t3192 <= 120)
235 return 5;
236 else if (t3192 <= 160)
237 return 6;
238 else if (t3192 <= 200)
239 return 7;
240 else if (t3192 <= 500)
241 return 0;
242 else if (t3192 <= 1000)
243 return 1;
244 else if (t3192 <= 1500)
245 return 2;
246 else
247 return -EINVAL;
248}
249
250static int encode_drx_timer(unsigned int drx)
251{
252 if (drx == 0)
253 return 0;
254 else if (drx == 1)
255 return 1;
256 else if (drx == 2)
257 return 2;
258 else if (drx <= 4)
259 return 3;
260 else if (drx <= 8)
261 return 4;
262 else if (drx <= 16)
263 return 5;
264 else if (drx <= 32)
265 return 6;
266 else if (drx <= 64)
267 return 7;
268 else
269 return -EINVAL;
270}
271
272/* GPRS Cell Options as per TS 04.60 Chapter 12.24
273 < GPRS Cell Options IE > ::=
274 < NMO : bit(2) >
275 < T3168 : bit(3) >
276 < T3192 : bit(3) >
277 < DRX_TIMER_MAX: bit(3) >
278 < ACCESS_BURST_TYPE: bit >
279 < CONTROL_ACK_TYPE : bit >
280 < BS_CV_MAX: bit(4) >
281 { 0 | 1 < PAN_DEC : bit(3) >
282 < PAN_INC : bit(3) >
283 < PAN_MAX : bit(3) >
284 { 0 | 1 < Extension Length : bit(6) >
285 < bit (val(Extension Length) + 1
286 & { < Extension Information > ! { bit ** = <no string> } } ;
287 < Extension Information > ::=
288 { 0 | 1 < EGPRS_PACKET_CHANNEL_REQUEST : bit >
289 < BEP_PERIOD : bit(4) > }
290 < PFC_FEATURE_MODE : bit >
291 < DTM_SUPPORT : bit >
292 <BSS_PAGING_COORDINATION: bit >
293 <spare bit > ** ;
294 */
Harald Weltea4e2d042009-12-20 17:08:22 +0100295static int append_gprs_cell_opt(struct bitvec *bv,
296 const struct gprs_cell_options *gco)
Harald Weltea43f7892009-12-01 18:04:30 +0530297{
298 int t3192, drx_timer_max;
299
300 t3192 = encode_t3192(gco->t3192);
301 if (t3192 < 0)
302 return t3192;
303
304 drx_timer_max = encode_drx_timer(gco->drx_timer_max);
305 if (drx_timer_max < 0)
306 return drx_timer_max;
307
308 bitvec_set_uint(bv, gco->nmo, 2);
309 bitvec_set_uint(bv, gco->t3168 / 500, 3);
310 bitvec_set_uint(bv, t3192, 3);
311 bitvec_set_uint(bv, drx_timer_max, 3);
312 /* ACCESS_BURST_TYPE: Hard-code 8bit */
313 bitvec_set_bit(bv, 0);
314 /* CONTROL_ACK_TYPE: Hard-code to RLC/MAC control block */
315 bitvec_set_bit(bv, 1);
316 bitvec_set_uint(bv, gco->bs_cv_max, 4);
317
318 /* hard-code no PAN_{DEC,INC,MAX} */
319 bitvec_set_bit(bv, 0);
320
321 /* no extension information (EDGE) */
322 bitvec_set_bit(bv, 0);
323
324 return 0;
325}
326
327static void append_gprs_pwr_ctrl_pars(struct bitvec *bv,
Harald Weltea4e2d042009-12-20 17:08:22 +0100328 const struct gprs_power_ctrl_pars *pcp)
Harald Weltea43f7892009-12-01 18:04:30 +0530329{
330 bitvec_set_uint(bv, pcp->alpha, 4);
331 bitvec_set_uint(bv, pcp->t_avg_w, 5);
332 bitvec_set_uint(bv, pcp->t_avg_t, 5);
333 bitvec_set_uint(bv, pcp->pc_meas_chan, 1);
334 bitvec_set_uint(bv, pcp->n_avg_i, 4);
335}
336
337/* Generate SI13 Rest Octests (Chapter 10.5.2.37b) */
338int rest_octets_si13(u_int8_t *data, const struct gsm48_si13_info *si13)
339{
340 struct bitvec bv;
341
342 memset(&bv, 0, sizeof(bv));
343 bv.data = data;
Holger Hans Peter Freyther4cffc452010-01-06 06:44:37 +0100344 bv.data_len = 20;
Harald Weltea43f7892009-12-01 18:04:30 +0530345
346 if (0) {
347 /* No rest octets */
348 bitvec_set_bit(&bv, L);
349 } else {
350 bitvec_set_bit(&bv, H);
351 bitvec_set_uint(&bv, si13->bcch_change_mark, 3);
352 bitvec_set_uint(&bv, si13->si_change_field, 4);
353 if (0) {
354 bitvec_set_bit(&bv, 0);
355 } else {
356 bitvec_set_bit(&bv, 1);
357 bitvec_set_uint(&bv, si13->bcch_change_mark, 2);
358 append_gprs_mobile_alloc(&bv);
359 }
360 if (!si13->pbcch_present) {
361 /* PBCCH not present in cell */
362 bitvec_set_bit(&bv, 0);
363 bitvec_set_uint(&bv, si13->no_pbcch.rac, 8);
364 bitvec_set_bit(&bv, si13->no_pbcch.spgc_ccch_sup);
365 bitvec_set_uint(&bv, si13->no_pbcch.prio_acc_thr, 3);
366 bitvec_set_uint(&bv, si13->no_pbcch.net_ctrl_ord, 2);
367 append_gprs_cell_opt(&bv, &si13->cell_opts);
368 append_gprs_pwr_ctrl_pars(&bv, &si13->pwr_ctrl_pars);
369 } else {
370 /* PBCCH present in cell */
371 bitvec_set_bit(&bv, 1);
372 bitvec_set_uint(&bv, si13->pbcch.psi1_rep_per, 4);
373 /* PBCCH Descripiton */
374 bitvec_set_uint(&bv, si13->pbcch.pb, 4);
375 bitvec_set_uint(&bv, si13->pbcch.tsc, 3);
376 bitvec_set_uint(&bv, si13->pbcch.tn, 3);
377 switch (si13->pbcch.carrier_type) {
378 case PBCCH_BCCH:
379 bitvec_set_bit(&bv, 0);
380 bitvec_set_bit(&bv, 0);
381 break;
382 case PBCCH_ARFCN:
383 bitvec_set_bit(&bv, 0);
384 bitvec_set_bit(&bv, 1);
385 bitvec_set_uint(&bv, si13->pbcch.arfcn, 10);
386 break;
387 case PBCCH_MAIO:
388 bitvec_set_bit(&bv, 1);
389 bitvec_set_uint(&bv, si13->pbcch.maio, 6);
390 break;
391 }
392 }
393 }
Holger Hans Peter Freythercaa14862010-01-06 07:49:58 +0100394 bitvec_spare_padding(&bv, (bv.data_len*8)-1);
395 return bv.data_len;
Harald Weltea43f7892009-12-01 18:04:30 +0530396}