blob: 6493794ef27cc24355c1681bfedba3d6567083e3 [file] [log] [blame]
Harald Weltea43f7892009-12-01 18:04:30 +05301/* GSM 04.08 System Information (SI) encoding and decoding
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <errno.h>
25#include <string.h>
Harald Welte6c40def2009-12-14 22:07:14 +010026#include <stdio.h>
Harald Weltea43f7892009-12-01 18:04:30 +053027#include <sys/types.h>
28#include <netinet/in.h>
29
30#include <openbsc/gsm_04_08.h>
31#include <openbsc/gsm_data.h>
32#include <openbsc/abis_rsl.h>
33#include <openbsc/rest_octets.h>
Harald Welte6c40def2009-12-14 22:07:14 +010034#include <openbsc/bitvec.h>
Harald Welte152b6262009-12-16 11:57:48 +010035#include <openbsc/debug.h>
Harald Weltea43f7892009-12-01 18:04:30 +053036
37#define GSM48_CELL_CHAN_DESC_SIZE 16
38#define GSM_MACBLOCK_LEN 23
39#define GSM_MACBLOCK_PADDING 0x2b
40
Harald Welteda760d32009-12-14 20:25:05 +010041/* Frequency Lists as per TS 04.08 10.5.2.13 */
42
43/* 10.5.2.13.2: Bit map 0 format */
Harald Welte6c40def2009-12-14 22:07:14 +010044static int freq_list_bm0_set_arfcn(u_int8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +053045{
46 unsigned int byte, bit;
47
Harald Welteb1d4c8e2009-12-17 23:10:46 +010048 if (arfcn > 124 || arfcn < 1) {
49 LOGP(DRR, LOGL_ERROR, "Bitmap 0 only supports ARFCN 1...124\n");
Harald Weltea43f7892009-12-01 18:04:30 +053050 return -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +010051 }
Harald Weltea43f7892009-12-01 18:04:30 +053052
Harald Welteda760d32009-12-14 20:25:05 +010053 /* the bitmask is from 1..124, not from 0..123 */
54 arfcn--;
55
Harald Weltea43f7892009-12-01 18:04:30 +053056 byte = arfcn / 8;
57 bit = arfcn % 8;
58
Harald Welteda760d32009-12-14 20:25:05 +010059 chan_list[GSM48_CELL_CHAN_DESC_SIZE-1-byte] |= (1 << bit);
Harald Weltea43f7892009-12-01 18:04:30 +053060
61 return 0;
62}
63
Harald Welteda760d32009-12-14 20:25:05 +010064/* 10.5.2.13.7: Variable bit map format */
Harald Welte6c40def2009-12-14 22:07:14 +010065static int freq_list_bmrel_set_arfcn(u_int8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +053066{
67 unsigned int byte, bit;
68 unsigned int min_arfcn;
69 unsigned int bitno;
70
71 min_arfcn = (chan_list[0] & 1) << 9;
72 min_arfcn |= chan_list[1] << 1;
73 min_arfcn |= (chan_list[2] >> 7) & 1;
74
75 /* The lower end of our bitmaks is always implicitly included */
76 if (arfcn == min_arfcn)
77 return 0;
78
Harald Welte152b6262009-12-16 11:57:48 +010079 if (arfcn < min_arfcn) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +010080 LOGP(DRR, LOGL_ERROR, "arfcn(%u) < min(%u)\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +053081 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +010082 }
83 if (arfcn > min_arfcn + 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +010084 LOGP(DRR, LOGL_ERROR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +053085 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +010086 }
Harald Weltea43f7892009-12-01 18:04:30 +053087
88 bitno = (arfcn - min_arfcn);
89 byte = bitno / 8;
90 bit = bitno % 8;
91
92 chan_list[2 + byte] |= 1 << (7 - bit);
93
94 return 0;
95}
96
97/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Harald Welte6c40def2009-12-14 22:07:14 +010098static int bitvec2freq_list(u_int8_t *chan_list, struct bitvec *bv,
99 const struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530100{
Sylvain Munaut42a56522009-12-24 14:20:19 +0100101 int i, rc, min = 1024, max = -1;
Harald Weltea43f7892009-12-01 18:04:30 +0530102
103 memset(chan_list, 0, 16);
104
105 /* GSM900-only handsets only support 'bit map 0 format' */
106 if (bts->band == GSM_BAND_900) {
107 chan_list[0] = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100108
109 for (i = 0; i < bv->data_len*8; i++) {
110 if (bitvec_get_bit_pos(bv, i)) {
111 rc = freq_list_bm0_set_arfcn(chan_list, i);
112 if (rc < 0)
113 return rc;
114 }
Harald Weltea43f7892009-12-01 18:04:30 +0530115 }
116 return 0;
117 }
118
119 /* We currently only support the 'Variable bitmap format' */
120 chan_list[0] = 0x8e;
121
Harald Welte6c40def2009-12-14 22:07:14 +0100122 for (i = 0; i < bv->data_len*8; i++) {
123 if (bitvec_get_bit_pos(bv, i)) {
124 if (i < min)
125 min = i;
126 if (i > max)
127 max = i;
128 }
Harald Weltea43f7892009-12-01 18:04:30 +0530129 }
130
Sylvain Munaut42a56522009-12-24 14:20:19 +0100131 if (max == -1) {
132 /* Empty set, use 'bit map 0 format' */
133 chan_list[0] = 0;
134 return 0;
135 }
136
Harald Welte152b6262009-12-16 11:57:48 +0100137 if ((max - min) > 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100138 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, "
139 "distance > 111\n", min, max);
Harald Weltea43f7892009-12-01 18:04:30 +0530140 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +0100141 }
Harald Weltea43f7892009-12-01 18:04:30 +0530142
143 chan_list[0] |= (min >> 9) & 1;
144 chan_list[1] = (min >> 1);
145 chan_list[2] = (min & 1) << 7;
146
Harald Welte6c40def2009-12-14 22:07:14 +0100147 for (i = 0; i < bv->data_len*8; i++) {
Harald Welte152b6262009-12-16 11:57:48 +0100148 if (bitvec_get_bit_pos(bv, i)) {
149 rc = freq_list_bmrel_set_arfcn(chan_list, i);
150 if (rc < 0)
151 return rc;
152 }
Harald Weltea43f7892009-12-01 18:04:30 +0530153 }
154
155 return 0;
156}
157
158/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Harald Welte6c40def2009-12-14 22:07:14 +0100159static int generate_cell_chan_list(u_int8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530160{
Harald Weltea43f7892009-12-01 18:04:30 +0530161 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100162 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530163
Harald Welte6c40def2009-12-14 22:07:14 +0100164 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
165 llist_for_each_entry(trx, &bts->trx_list, list)
166 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea43f7892009-12-01 18:04:30 +0530167
Harald Welte6c40def2009-12-14 22:07:14 +0100168 /* then we generate a GSM 04.08 frequency list from the bitvec */
169 return bitvec2freq_list(chan_list, bv, bts);
Harald Weltea43f7892009-12-01 18:04:30 +0530170}
171
Harald Welte6c40def2009-12-14 22:07:14 +0100172/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
173static int generate_bcch_chan_list(u_int8_t *chan_list, struct gsm_bts *bts)
174{
175 struct gsm_bts *cur_bts;
176 struct bitvec *bv = &bts->si_common.neigh_list;
177
178 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
179 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
180 if (cur_bts == bts)
181 continue;
182 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
183 }
184
185 /* then we generate a GSM 04.08 frequency list from the bitvec */
186 return bitvec2freq_list(chan_list, bv, bts);
187}
188
189static int generate_si1(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530190{
191 int rc;
192 struct gsm48_system_information_type_1 *si1 =
193 (struct gsm48_system_information_type_1 *) output;
194
195 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
196
197 si1->header.l2_plen = (21 << 2) | 1;
198 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
199 si1->header.skip_indicator = 0;
200 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
201
202 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
203 if (rc < 0)
204 return rc;
205
206 si1->rach_control = bts->si_common.rach_control;
207
208 /* SI1 Rest Octets (10.5.2.32), contains NCH position */
209 rest_octets_si1(si1->rest_octets, NULL);
210
211 return GSM_MACBLOCK_LEN;
212}
213
Harald Welte6c40def2009-12-14 22:07:14 +0100214static int generate_si2(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530215{
216 int rc;
217 struct gsm48_system_information_type_2 *si2 =
218 (struct gsm48_system_information_type_2 *) output;
219
220 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
221
222 si2->header.l2_plen = (22 << 2) | 1;
223 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
224 si2->header.skip_indicator = 0;
225 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
226
227 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts);
228 if (rc < 0)
229 return rc;
230
231 si2->ncc_permitted = bts->si_common.ncc_permitted;
232 si2->rach_control = bts->si_common.rach_control;
233
234 return GSM_MACBLOCK_LEN;
235}
236
237struct gsm48_si_ro_info si_info = {
238 .selection_params = {
239 .present = 0,
240 },
241 .power_offset = {
242 .present = 0,
243 },
244 .si2ter_indicator = 0,
245 .early_cm_ctrl = 1,
246 .scheduling = {
247 .present = 0,
248 },
249 .gprs_ind = {
250 .si13_position = 0,
251 .ra_colour = 0,
252 .present = 0,
253 },
254 .lsa_params = {
255 .present = 0,
256 },
257 .cell_id = 0, /* FIXME: doesn't the bts have this? */
258 .break_ind = 0,
259};
260
Harald Welte6c40def2009-12-14 22:07:14 +0100261static int generate_si3(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530262{
263 struct gsm48_system_information_type_3 *si3 =
264 (struct gsm48_system_information_type_3 *) output;
265
266 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
267
268 si3->header.l2_plen = (18 << 2) | 1;
269 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
270 si3->header.skip_indicator = 0;
271 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
272
273 si3->cell_identity = htons(bts->cell_identity);
274 gsm0408_generate_lai(&si3->lai, bts->network->country_code,
275 bts->network->network_code,
276 bts->location_area_code);
277 si3->control_channel_desc = bts->si_common.chan_desc;
278 si3->cell_options = bts->si_common.cell_options;
279 si3->cell_sel_par = bts->si_common.cell_sel_par;
280 si3->rach_control = bts->si_common.rach_control;
281
282 /* SI3 Rest Octets (10.5.2.34), containing
283 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
284 Power Offset, 2ter Indicator, Early Classmark Sending,
285 Scheduling if and WHERE, GPRS Indicator, SI13 position */
286 rest_octets_si3(si3->rest_octets, &si_info);
287
288 return GSM_MACBLOCK_LEN;
289}
290
Harald Welte6c40def2009-12-14 22:07:14 +0100291static int generate_si4(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530292{
293 struct gsm48_system_information_type_4 *si4 =
294 (struct gsm48_system_information_type_4 *) output;
295
296 /* length of all IEs present except SI4 rest octets and l2_plen */
297 int l2_plen = sizeof(*si4) - 1;
298
299 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
300
301 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
302 si4->header.skip_indicator = 0;
303 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
304
305 gsm0408_generate_lai(&si4->lai, bts->network->country_code,
306 bts->network->network_code,
307 bts->location_area_code);
308 si4->cell_sel_par = bts->si_common.cell_sel_par;
309 si4->rach_control = bts->si_common.rach_control;
310
311 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
312
313 si4->header.l2_plen = (l2_plen << 2) | 1;
314
315 /* SI4 Rest Octets (10.5.2.35), containing
316 Optional Power offset, GPRS Indicator,
317 Cell Identity, LSA ID, Selection Parameter */
318 rest_octets_si4(si4->data, &si_info);
319
320 return GSM_MACBLOCK_LEN;
321}
322
Harald Welte6c40def2009-12-14 22:07:14 +0100323static int generate_si5(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530324{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100325 struct gsm48_system_information_type_5 *si5;
326 int rc, l2_plen = 18;
Harald Weltea43f7892009-12-01 18:04:30 +0530327
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100328 /* ip.access nanoBTS needs l2_plen!! */
329 if (is_ipaccess_bts(bts)) {
330 *output++ = (l2_plen << 2) | 1;
331 l2_plen++;
332 }
333
334 si5 = (struct gsm48_system_information_type_5 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530335 memset(si5, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
336
337 /* l2 pseudo length, not part of msg: 18 */
338 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
339 si5->skip_indicator = 0;
340 si5->system_information = GSM48_MT_RR_SYSINFO_5;
341 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts);
342 if (rc < 0)
343 return rc;
344
345 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100346 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530347}
348
Harald Welte6c40def2009-12-14 22:07:14 +0100349static int generate_si6(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530350{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100351 struct gsm48_system_information_type_6 *si6;
352 int l2_plen = 11;
Harald Weltea43f7892009-12-01 18:04:30 +0530353
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100354 /* ip.access nanoBTS needs l2_plen!! */
355 if (is_ipaccess_bts(bts)) {
356 *output++ = (l2_plen << 2) | 1;
357 l2_plen++;
358 }
359
360 si6 = (struct gsm48_system_information_type_6 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530361 memset(si6, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
362
363 /* l2 pseudo length, not part of msg: 11 */
364 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
365 si6->skip_indicator = 0;
366 si6->system_information = GSM48_MT_RR_SYSINFO_6;
367 si6->cell_identity = htons(bts->cell_identity);
368 gsm0408_generate_lai(&si6->lai, bts->network->country_code,
369 bts->network->network_code,
370 bts->location_area_code);
371 si6->cell_options = bts->si_common.cell_options;
372 si6->ncc_permitted = bts->si_common.ncc_permitted;
373
374 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
375
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100376 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530377}
378
379static struct gsm48_si13_info si13_default = {
380 .cell_opts = {
381 .nmo = GPRS_NMO_III,
382 .t3168 = 1000,
383 .t3192 = 1000,
384 .drx_timer_max = 1,
385 .bs_cv_max = 15,
386 },
387 .pwr_ctrl_pars = {
388 .alpha = 10, /* a = 1.0 */
389 .t_avg_w = 25,
390 .t_avg_t = 25,
391 .pc_meas_chan = 0, /* downling measured on CCCH */
392 .n_avg_i = 15,
393 },
394 .bcch_change_mark = 0,
395 .si_change_field = 0,
396 .pbcch_present = 0,
397 {
398 .no_pbcch = {
399 .rac = 0,
400 .spgc_ccch_sup = 0,
401 .net_ctrl_ord = 0,
402 .prio_acc_thr = 0,
403 },
404 },
405};
406
Harald Welte6c40def2009-12-14 22:07:14 +0100407static int generate_si13(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530408{
409 struct gsm48_system_information_type_13 *si13 =
410 (struct gsm48_system_information_type_13 *) output;
411 int ret;
412
413 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
414
415 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
416 si13->header.skip_indicator = 0;
417 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
418
419 ret = rest_octets_si13(si13->rest_octets, &si13_default);
420 if (ret < 0)
421 return ret;
422
423 si13->header.l2_plen = ret & 0xff;
424
425 return GSM_MACBLOCK_LEN;
426}
427
428int gsm_generate_si(u_int8_t *output, struct gsm_bts *bts, int type)
429{
430 switch (type) {
431 case RSL_SYSTEM_INFO_1:
432 return generate_si1(output, bts);
433 case RSL_SYSTEM_INFO_2:
434 return generate_si2(output, bts);
435 case RSL_SYSTEM_INFO_3:
436 return generate_si3(output, bts);
437 case RSL_SYSTEM_INFO_4:
438 return generate_si4(output, bts);
439 case RSL_SYSTEM_INFO_5:
440 return generate_si5(output, bts);
441 case RSL_SYSTEM_INFO_6:
442 return generate_si6(output, bts);
443 case RSL_SYSTEM_INFO_13:
444 return generate_si13(output, bts);
445 default:
446 return -EINVAL;
447 }
448
449 return 0;
450}