blob: b404e5151080de2ac3c1834641100b5ec284f36b [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{
Harald Welte6c40def2009-12-14 22:07:14 +0100101 int i, rc, min = 1024, max = 0;
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
Harald Welte152b6262009-12-16 11:57:48 +0100131 if ((max - min) > 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100132 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, "
133 "distance > 111\n", min, max);
Harald Weltea43f7892009-12-01 18:04:30 +0530134 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +0100135 }
Harald Weltea43f7892009-12-01 18:04:30 +0530136
137 chan_list[0] |= (min >> 9) & 1;
138 chan_list[1] = (min >> 1);
139 chan_list[2] = (min & 1) << 7;
140
Harald Welte6c40def2009-12-14 22:07:14 +0100141 for (i = 0; i < bv->data_len*8; i++) {
Harald Welte152b6262009-12-16 11:57:48 +0100142 if (bitvec_get_bit_pos(bv, i)) {
143 rc = freq_list_bmrel_set_arfcn(chan_list, i);
144 if (rc < 0)
145 return rc;
146 }
Harald Weltea43f7892009-12-01 18:04:30 +0530147 }
148
149 return 0;
150}
151
152/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Harald Welte6c40def2009-12-14 22:07:14 +0100153static int generate_cell_chan_list(u_int8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530154{
Harald Weltea43f7892009-12-01 18:04:30 +0530155 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100156 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530157
Harald Welte6c40def2009-12-14 22:07:14 +0100158 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
159 llist_for_each_entry(trx, &bts->trx_list, list)
160 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea43f7892009-12-01 18:04:30 +0530161
Harald Welte6c40def2009-12-14 22:07:14 +0100162 /* then we generate a GSM 04.08 frequency list from the bitvec */
163 return bitvec2freq_list(chan_list, bv, bts);
Harald Weltea43f7892009-12-01 18:04:30 +0530164}
165
Harald Welte6c40def2009-12-14 22:07:14 +0100166/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
167static int generate_bcch_chan_list(u_int8_t *chan_list, struct gsm_bts *bts)
168{
169 struct gsm_bts *cur_bts;
170 struct bitvec *bv = &bts->si_common.neigh_list;
171
172 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
173 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
174 if (cur_bts == bts)
175 continue;
176 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
177 }
178
179 /* then we generate a GSM 04.08 frequency list from the bitvec */
180 return bitvec2freq_list(chan_list, bv, bts);
181}
182
183static int generate_si1(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530184{
185 int rc;
186 struct gsm48_system_information_type_1 *si1 =
187 (struct gsm48_system_information_type_1 *) output;
188
189 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
190
191 si1->header.l2_plen = (21 << 2) | 1;
192 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
193 si1->header.skip_indicator = 0;
194 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
195
196 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
197 if (rc < 0)
198 return rc;
199
200 si1->rach_control = bts->si_common.rach_control;
201
202 /* SI1 Rest Octets (10.5.2.32), contains NCH position */
203 rest_octets_si1(si1->rest_octets, NULL);
204
205 return GSM_MACBLOCK_LEN;
206}
207
Harald Welte6c40def2009-12-14 22:07:14 +0100208static int generate_si2(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530209{
210 int rc;
211 struct gsm48_system_information_type_2 *si2 =
212 (struct gsm48_system_information_type_2 *) output;
213
214 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
215
216 si2->header.l2_plen = (22 << 2) | 1;
217 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
218 si2->header.skip_indicator = 0;
219 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
220
221 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts);
222 if (rc < 0)
223 return rc;
224
225 si2->ncc_permitted = bts->si_common.ncc_permitted;
226 si2->rach_control = bts->si_common.rach_control;
227
228 return GSM_MACBLOCK_LEN;
229}
230
231struct gsm48_si_ro_info si_info = {
232 .selection_params = {
233 .present = 0,
234 },
235 .power_offset = {
236 .present = 0,
237 },
238 .si2ter_indicator = 0,
239 .early_cm_ctrl = 1,
240 .scheduling = {
241 .present = 0,
242 },
243 .gprs_ind = {
244 .si13_position = 0,
245 .ra_colour = 0,
246 .present = 0,
247 },
248 .lsa_params = {
249 .present = 0,
250 },
251 .cell_id = 0, /* FIXME: doesn't the bts have this? */
252 .break_ind = 0,
253};
254
Harald Welte6c40def2009-12-14 22:07:14 +0100255static int generate_si3(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530256{
257 struct gsm48_system_information_type_3 *si3 =
258 (struct gsm48_system_information_type_3 *) output;
259
260 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
261
262 si3->header.l2_plen = (18 << 2) | 1;
263 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
264 si3->header.skip_indicator = 0;
265 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
266
267 si3->cell_identity = htons(bts->cell_identity);
268 gsm0408_generate_lai(&si3->lai, bts->network->country_code,
269 bts->network->network_code,
270 bts->location_area_code);
271 si3->control_channel_desc = bts->si_common.chan_desc;
272 si3->cell_options = bts->si_common.cell_options;
273 si3->cell_sel_par = bts->si_common.cell_sel_par;
274 si3->rach_control = bts->si_common.rach_control;
275
276 /* SI3 Rest Octets (10.5.2.34), containing
277 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
278 Power Offset, 2ter Indicator, Early Classmark Sending,
279 Scheduling if and WHERE, GPRS Indicator, SI13 position */
280 rest_octets_si3(si3->rest_octets, &si_info);
281
282 return GSM_MACBLOCK_LEN;
283}
284
Harald Welte6c40def2009-12-14 22:07:14 +0100285static int generate_si4(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530286{
287 struct gsm48_system_information_type_4 *si4 =
288 (struct gsm48_system_information_type_4 *) output;
289
290 /* length of all IEs present except SI4 rest octets and l2_plen */
291 int l2_plen = sizeof(*si4) - 1;
292
293 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
294
295 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
296 si4->header.skip_indicator = 0;
297 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
298
299 gsm0408_generate_lai(&si4->lai, bts->network->country_code,
300 bts->network->network_code,
301 bts->location_area_code);
302 si4->cell_sel_par = bts->si_common.cell_sel_par;
303 si4->rach_control = bts->si_common.rach_control;
304
305 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
306
307 si4->header.l2_plen = (l2_plen << 2) | 1;
308
309 /* SI4 Rest Octets (10.5.2.35), containing
310 Optional Power offset, GPRS Indicator,
311 Cell Identity, LSA ID, Selection Parameter */
312 rest_octets_si4(si4->data, &si_info);
313
314 return GSM_MACBLOCK_LEN;
315}
316
Harald Welte6c40def2009-12-14 22:07:14 +0100317static int generate_si5(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530318{
319 struct gsm48_system_information_type_5 *si5 =
320 (struct gsm48_system_information_type_5 *) output;
321 int rc;
322
323 memset(si5, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
324
325 /* l2 pseudo length, not part of msg: 18 */
326 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
327 si5->skip_indicator = 0;
328 si5->system_information = GSM48_MT_RR_SYSINFO_5;
329 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts);
330 if (rc < 0)
331 return rc;
332
333 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
334 return 18;
335}
336
Harald Welte6c40def2009-12-14 22:07:14 +0100337static int generate_si6(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530338{
339 struct gsm48_system_information_type_6 *si6 =
340 (struct gsm48_system_information_type_6 *) output;
341
342 memset(si6, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
343
344 /* l2 pseudo length, not part of msg: 11 */
345 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
346 si6->skip_indicator = 0;
347 si6->system_information = GSM48_MT_RR_SYSINFO_6;
348 si6->cell_identity = htons(bts->cell_identity);
349 gsm0408_generate_lai(&si6->lai, bts->network->country_code,
350 bts->network->network_code,
351 bts->location_area_code);
352 si6->cell_options = bts->si_common.cell_options;
353 si6->ncc_permitted = bts->si_common.ncc_permitted;
354
355 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
356
357 return 18;
358}
359
360static struct gsm48_si13_info si13_default = {
361 .cell_opts = {
362 .nmo = GPRS_NMO_III,
363 .t3168 = 1000,
364 .t3192 = 1000,
365 .drx_timer_max = 1,
366 .bs_cv_max = 15,
367 },
368 .pwr_ctrl_pars = {
369 .alpha = 10, /* a = 1.0 */
370 .t_avg_w = 25,
371 .t_avg_t = 25,
372 .pc_meas_chan = 0, /* downling measured on CCCH */
373 .n_avg_i = 15,
374 },
375 .bcch_change_mark = 0,
376 .si_change_field = 0,
377 .pbcch_present = 0,
378 {
379 .no_pbcch = {
380 .rac = 0,
381 .spgc_ccch_sup = 0,
382 .net_ctrl_ord = 0,
383 .prio_acc_thr = 0,
384 },
385 },
386};
387
Harald Welte6c40def2009-12-14 22:07:14 +0100388static int generate_si13(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530389{
390 struct gsm48_system_information_type_13 *si13 =
391 (struct gsm48_system_information_type_13 *) output;
392 int ret;
393
394 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
395
396 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
397 si13->header.skip_indicator = 0;
398 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
399
400 ret = rest_octets_si13(si13->rest_octets, &si13_default);
401 if (ret < 0)
402 return ret;
403
404 si13->header.l2_plen = ret & 0xff;
405
406 return GSM_MACBLOCK_LEN;
407}
408
409int gsm_generate_si(u_int8_t *output, struct gsm_bts *bts, int type)
410{
411 switch (type) {
412 case RSL_SYSTEM_INFO_1:
413 return generate_si1(output, bts);
414 case RSL_SYSTEM_INFO_2:
415 return generate_si2(output, bts);
416 case RSL_SYSTEM_INFO_3:
417 return generate_si3(output, bts);
418 case RSL_SYSTEM_INFO_4:
419 return generate_si4(output, bts);
420 case RSL_SYSTEM_INFO_5:
421 return generate_si5(output, bts);
422 case RSL_SYSTEM_INFO_6:
423 return generate_si6(output, bts);
424 case RSL_SYSTEM_INFO_13:
425 return generate_si13(output, bts);
426 default:
427 return -EINVAL;
428 }
429
430 return 0;
431}