blob: 644abb4322b6a5bde28c96c37796c1273239a9c4 [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 Welteda760d32009-12-14 20:25:05 +010048 if (arfcn > 124 || arfcn < 1)
Harald Weltea43f7892009-12-01 18:04:30 +053049 return -EINVAL;
50
Harald Welteda760d32009-12-14 20:25:05 +010051 /* the bitmask is from 1..124, not from 0..123 */
52 arfcn--;
53
Harald Weltea43f7892009-12-01 18:04:30 +053054 byte = arfcn / 8;
55 bit = arfcn % 8;
56
Harald Welteda760d32009-12-14 20:25:05 +010057 chan_list[GSM48_CELL_CHAN_DESC_SIZE-1-byte] |= (1 << bit);
Harald Weltea43f7892009-12-01 18:04:30 +053058
59 return 0;
60}
61
Harald Welteda760d32009-12-14 20:25:05 +010062/* 10.5.2.13.7: Variable bit map format */
Harald Welte6c40def2009-12-14 22:07:14 +010063static int freq_list_bmrel_set_arfcn(u_int8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +053064{
65 unsigned int byte, bit;
66 unsigned int min_arfcn;
67 unsigned int bitno;
68
69 min_arfcn = (chan_list[0] & 1) << 9;
70 min_arfcn |= chan_list[1] << 1;
71 min_arfcn |= (chan_list[2] >> 7) & 1;
72
73 /* The lower end of our bitmaks is always implicitly included */
74 if (arfcn == min_arfcn)
75 return 0;
76
Harald Welte152b6262009-12-16 11:57:48 +010077 if (arfcn < min_arfcn) {
78 DEBUGP(DRR, "arfcn(%u) < min(%u)\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +053079 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +010080 }
81 if (arfcn > min_arfcn + 111) {
82 DEBUGP(DRR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +053083 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +010084 }
Harald Weltea43f7892009-12-01 18:04:30 +053085
86 bitno = (arfcn - min_arfcn);
87 byte = bitno / 8;
88 bit = bitno % 8;
89
90 chan_list[2 + byte] |= 1 << (7 - bit);
91
92 return 0;
93}
94
95/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Harald Welte6c40def2009-12-14 22:07:14 +010096static int bitvec2freq_list(u_int8_t *chan_list, struct bitvec *bv,
97 const struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +053098{
Harald Welte6c40def2009-12-14 22:07:14 +010099 int i, rc, min = 1024, max = 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530100
101 memset(chan_list, 0, 16);
102
103 /* GSM900-only handsets only support 'bit map 0 format' */
104 if (bts->band == GSM_BAND_900) {
105 chan_list[0] = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100106
107 for (i = 0; i < bv->data_len*8; i++) {
108 if (bitvec_get_bit_pos(bv, i)) {
109 rc = freq_list_bm0_set_arfcn(chan_list, i);
110 if (rc < 0)
111 return rc;
112 }
Harald Weltea43f7892009-12-01 18:04:30 +0530113 }
114 return 0;
115 }
116
117 /* We currently only support the 'Variable bitmap format' */
118 chan_list[0] = 0x8e;
119
Harald Welte6c40def2009-12-14 22:07:14 +0100120 for (i = 0; i < bv->data_len*8; i++) {
121 if (bitvec_get_bit_pos(bv, i)) {
122 if (i < min)
123 min = i;
124 if (i > max)
125 max = i;
126 }
Harald Weltea43f7892009-12-01 18:04:30 +0530127 }
128
Harald Welte152b6262009-12-16 11:57:48 +0100129 if ((max - min) > 111) {
130 DEBUGP(DRR, "min_arfcn=%u, max_arfcn=%u, distance > 111\n", min, max);
Harald Weltea43f7892009-12-01 18:04:30 +0530131 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +0100132 }
Harald Weltea43f7892009-12-01 18:04:30 +0530133
134 chan_list[0] |= (min >> 9) & 1;
135 chan_list[1] = (min >> 1);
136 chan_list[2] = (min & 1) << 7;
137
Harald Welte6c40def2009-12-14 22:07:14 +0100138 for (i = 0; i < bv->data_len*8; i++) {
Harald Welte152b6262009-12-16 11:57:48 +0100139 if (bitvec_get_bit_pos(bv, i)) {
140 rc = freq_list_bmrel_set_arfcn(chan_list, i);
141 if (rc < 0)
142 return rc;
143 }
Harald Weltea43f7892009-12-01 18:04:30 +0530144 }
145
146 return 0;
147}
148
149/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Harald Welte6c40def2009-12-14 22:07:14 +0100150static int generate_cell_chan_list(u_int8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530151{
Harald Weltea43f7892009-12-01 18:04:30 +0530152 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100153 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530154
Harald Welte6c40def2009-12-14 22:07:14 +0100155 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
156 llist_for_each_entry(trx, &bts->trx_list, list)
157 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea43f7892009-12-01 18:04:30 +0530158
Harald Welte6c40def2009-12-14 22:07:14 +0100159 /* then we generate a GSM 04.08 frequency list from the bitvec */
160 return bitvec2freq_list(chan_list, bv, bts);
Harald Weltea43f7892009-12-01 18:04:30 +0530161}
162
Harald Welte6c40def2009-12-14 22:07:14 +0100163/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
164static int generate_bcch_chan_list(u_int8_t *chan_list, struct gsm_bts *bts)
165{
166 struct gsm_bts *cur_bts;
167 struct bitvec *bv = &bts->si_common.neigh_list;
168
169 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
170 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
171 if (cur_bts == bts)
172 continue;
173 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
174 }
175
176 /* then we generate a GSM 04.08 frequency list from the bitvec */
177 return bitvec2freq_list(chan_list, bv, bts);
178}
179
180static int generate_si1(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530181{
182 int rc;
183 struct gsm48_system_information_type_1 *si1 =
184 (struct gsm48_system_information_type_1 *) output;
185
186 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
187
188 si1->header.l2_plen = (21 << 2) | 1;
189 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
190 si1->header.skip_indicator = 0;
191 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
192
193 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
194 if (rc < 0)
195 return rc;
196
197 si1->rach_control = bts->si_common.rach_control;
198
199 /* SI1 Rest Octets (10.5.2.32), contains NCH position */
200 rest_octets_si1(si1->rest_octets, NULL);
201
202 return GSM_MACBLOCK_LEN;
203}
204
Harald Welte6c40def2009-12-14 22:07:14 +0100205static int generate_si2(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530206{
207 int rc;
208 struct gsm48_system_information_type_2 *si2 =
209 (struct gsm48_system_information_type_2 *) output;
210
211 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
212
213 si2->header.l2_plen = (22 << 2) | 1;
214 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
215 si2->header.skip_indicator = 0;
216 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
217
218 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts);
219 if (rc < 0)
220 return rc;
221
222 si2->ncc_permitted = bts->si_common.ncc_permitted;
223 si2->rach_control = bts->si_common.rach_control;
224
225 return GSM_MACBLOCK_LEN;
226}
227
228struct gsm48_si_ro_info si_info = {
229 .selection_params = {
230 .present = 0,
231 },
232 .power_offset = {
233 .present = 0,
234 },
235 .si2ter_indicator = 0,
236 .early_cm_ctrl = 1,
237 .scheduling = {
238 .present = 0,
239 },
240 .gprs_ind = {
241 .si13_position = 0,
242 .ra_colour = 0,
243 .present = 0,
244 },
245 .lsa_params = {
246 .present = 0,
247 },
248 .cell_id = 0, /* FIXME: doesn't the bts have this? */
249 .break_ind = 0,
250};
251
Harald Welte6c40def2009-12-14 22:07:14 +0100252static int generate_si3(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530253{
254 struct gsm48_system_information_type_3 *si3 =
255 (struct gsm48_system_information_type_3 *) output;
256
257 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
258
259 si3->header.l2_plen = (18 << 2) | 1;
260 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
261 si3->header.skip_indicator = 0;
262 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
263
264 si3->cell_identity = htons(bts->cell_identity);
265 gsm0408_generate_lai(&si3->lai, bts->network->country_code,
266 bts->network->network_code,
267 bts->location_area_code);
268 si3->control_channel_desc = bts->si_common.chan_desc;
269 si3->cell_options = bts->si_common.cell_options;
270 si3->cell_sel_par = bts->si_common.cell_sel_par;
271 si3->rach_control = bts->si_common.rach_control;
272
273 /* SI3 Rest Octets (10.5.2.34), containing
274 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
275 Power Offset, 2ter Indicator, Early Classmark Sending,
276 Scheduling if and WHERE, GPRS Indicator, SI13 position */
277 rest_octets_si3(si3->rest_octets, &si_info);
278
279 return GSM_MACBLOCK_LEN;
280}
281
Harald Welte6c40def2009-12-14 22:07:14 +0100282static int generate_si4(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530283{
284 struct gsm48_system_information_type_4 *si4 =
285 (struct gsm48_system_information_type_4 *) output;
286
287 /* length of all IEs present except SI4 rest octets and l2_plen */
288 int l2_plen = sizeof(*si4) - 1;
289
290 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
291
292 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
293 si4->header.skip_indicator = 0;
294 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
295
296 gsm0408_generate_lai(&si4->lai, bts->network->country_code,
297 bts->network->network_code,
298 bts->location_area_code);
299 si4->cell_sel_par = bts->si_common.cell_sel_par;
300 si4->rach_control = bts->si_common.rach_control;
301
302 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
303
304 si4->header.l2_plen = (l2_plen << 2) | 1;
305
306 /* SI4 Rest Octets (10.5.2.35), containing
307 Optional Power offset, GPRS Indicator,
308 Cell Identity, LSA ID, Selection Parameter */
309 rest_octets_si4(si4->data, &si_info);
310
311 return GSM_MACBLOCK_LEN;
312}
313
Harald Welte6c40def2009-12-14 22:07:14 +0100314static int generate_si5(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530315{
316 struct gsm48_system_information_type_5 *si5 =
317 (struct gsm48_system_information_type_5 *) output;
318 int rc;
319
320 memset(si5, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
321
322 /* l2 pseudo length, not part of msg: 18 */
323 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
324 si5->skip_indicator = 0;
325 si5->system_information = GSM48_MT_RR_SYSINFO_5;
326 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts);
327 if (rc < 0)
328 return rc;
329
330 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
331 return 18;
332}
333
Harald Welte6c40def2009-12-14 22:07:14 +0100334static int generate_si6(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530335{
336 struct gsm48_system_information_type_6 *si6 =
337 (struct gsm48_system_information_type_6 *) output;
338
339 memset(si6, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
340
341 /* l2 pseudo length, not part of msg: 11 */
342 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
343 si6->skip_indicator = 0;
344 si6->system_information = GSM48_MT_RR_SYSINFO_6;
345 si6->cell_identity = htons(bts->cell_identity);
346 gsm0408_generate_lai(&si6->lai, bts->network->country_code,
347 bts->network->network_code,
348 bts->location_area_code);
349 si6->cell_options = bts->si_common.cell_options;
350 si6->ncc_permitted = bts->si_common.ncc_permitted;
351
352 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
353
354 return 18;
355}
356
357static struct gsm48_si13_info si13_default = {
358 .cell_opts = {
359 .nmo = GPRS_NMO_III,
360 .t3168 = 1000,
361 .t3192 = 1000,
362 .drx_timer_max = 1,
363 .bs_cv_max = 15,
364 },
365 .pwr_ctrl_pars = {
366 .alpha = 10, /* a = 1.0 */
367 .t_avg_w = 25,
368 .t_avg_t = 25,
369 .pc_meas_chan = 0, /* downling measured on CCCH */
370 .n_avg_i = 15,
371 },
372 .bcch_change_mark = 0,
373 .si_change_field = 0,
374 .pbcch_present = 0,
375 {
376 .no_pbcch = {
377 .rac = 0,
378 .spgc_ccch_sup = 0,
379 .net_ctrl_ord = 0,
380 .prio_acc_thr = 0,
381 },
382 },
383};
384
Harald Welte6c40def2009-12-14 22:07:14 +0100385static int generate_si13(u_int8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530386{
387 struct gsm48_system_information_type_13 *si13 =
388 (struct gsm48_system_information_type_13 *) output;
389 int ret;
390
391 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
392
393 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
394 si13->header.skip_indicator = 0;
395 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
396
397 ret = rest_octets_si13(si13->rest_octets, &si13_default);
398 if (ret < 0)
399 return ret;
400
401 si13->header.l2_plen = ret & 0xff;
402
403 return GSM_MACBLOCK_LEN;
404}
405
406int gsm_generate_si(u_int8_t *output, struct gsm_bts *bts, int type)
407{
408 switch (type) {
409 case RSL_SYSTEM_INFO_1:
410 return generate_si1(output, bts);
411 case RSL_SYSTEM_INFO_2:
412 return generate_si2(output, bts);
413 case RSL_SYSTEM_INFO_3:
414 return generate_si3(output, bts);
415 case RSL_SYSTEM_INFO_4:
416 return generate_si4(output, bts);
417 case RSL_SYSTEM_INFO_5:
418 return generate_si5(output, bts);
419 case RSL_SYSTEM_INFO_6:
420 return generate_si6(output, bts);
421 case RSL_SYSTEM_INFO_13:
422 return generate_si13(output, bts);
423 default:
424 return -EINVAL;
425 }
426
427 return 0;
428}