blob: 88b81dcb147df9454f934c334a18d4556934f229 [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
Harald Welte7401ae62010-06-15 16:44:12 +02004/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +02005 * (C) 2012 Holger Hans Peter Freyther
Harald Weltea43f7892009-12-01 18:04:30 +05306 *
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 <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 <netinet/in.h>
28
Harald Welte80cffaf2011-05-24 15:02:20 +020029#include <osmocom/core/bitvec.h>
30#include <osmocom/core/utils.h>
31#include <osmocom/gsm/sysinfo.h>
32
33#include <openbsc/debug.h>
Harald Weltea43f7892009-12-01 18:04:30 +053034#include <openbsc/gsm_04_08.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/abis_rsl.h>
37#include <openbsc/rest_octets.h>
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020038#include <openbsc/arfcn_range_encode.h>
Harald Weltea43f7892009-12-01 18:04:30 +053039
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +010040/*
41 * DCS1800 and PCS1900 have overlapping ARFCNs. We would need to set the
42 * ARFCN_PCS flag on the 1900 ARFCNs but this would increase cell_alloc
43 * and other arrays to make sure (ARFCN_PCS + 1024)/8 ARFCNs fit into the
44 * array. DCS1800 and PCS1900 can not be used at the same time so conserve
45 * memory and do the below.
46 */
47static int band_compatible(const struct gsm_bts *bts, int arfcn)
48{
49 enum gsm_band band = gsm_arfcn2band(arfcn);
50
51 /* normal case */
52 if (band == bts->band)
53 return 1;
54 /* deal with ARFCN_PCS not set */
55 if (band == GSM_BAND_1800 && bts->band == GSM_BAND_1900)
56 return 1;
57
58 return 0;
59}
Holger Hans Peter Freytherb91a1062010-01-06 06:38:14 +010060
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +010061static int is_dcs_net(const struct gsm_bts *bts)
62{
63 if (bts->band == GSM_BAND_850)
64 return 0;
65 if (bts->band == GSM_BAND_1900)
66 return 0;
67 return 1;
68}
69
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +010070static int use_arfcn(const struct gsm_bts *bts, const int bis, const int ter,
71 const int pgsm, const int arfcn)
72{
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +010073 if (!bis && !ter && band_compatible(bts, arfcn))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +010074 return 1;
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +010075 if (bis && pgsm && band_compatible(bts, arfcn) && (arfcn < 1 || arfcn > 124))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +010076 return 1;
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +010077 if (ter && !band_compatible(bts, arfcn))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +010078 return 1;
79 return 0;
80}
81
Harald Welteda760d32009-12-14 20:25:05 +010082/* Frequency Lists as per TS 04.08 10.5.2.13 */
83
84/* 10.5.2.13.2: Bit map 0 format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085static int freq_list_bm0_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +053086{
87 unsigned int byte, bit;
88
Harald Welteb1d4c8e2009-12-17 23:10:46 +010089 if (arfcn > 124 || arfcn < 1) {
90 LOGP(DRR, LOGL_ERROR, "Bitmap 0 only supports ARFCN 1...124\n");
Harald Weltea43f7892009-12-01 18:04:30 +053091 return -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +010092 }
Harald Weltea43f7892009-12-01 18:04:30 +053093
Harald Welteda760d32009-12-14 20:25:05 +010094 /* the bitmask is from 1..124, not from 0..123 */
95 arfcn--;
96
Harald Weltea43f7892009-12-01 18:04:30 +053097 byte = arfcn / 8;
98 bit = arfcn % 8;
99
Harald Welteda760d32009-12-14 20:25:05 +0100100 chan_list[GSM48_CELL_CHAN_DESC_SIZE-1-byte] |= (1 << bit);
Harald Weltea43f7892009-12-01 18:04:30 +0530101
102 return 0;
103}
104
Harald Welteda760d32009-12-14 20:25:05 +0100105/* 10.5.2.13.7: Variable bit map format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200106static int freq_list_bmrel_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +0530107{
108 unsigned int byte, bit;
109 unsigned int min_arfcn;
110 unsigned int bitno;
111
112 min_arfcn = (chan_list[0] & 1) << 9;
113 min_arfcn |= chan_list[1] << 1;
114 min_arfcn |= (chan_list[2] >> 7) & 1;
115
116 /* The lower end of our bitmaks is always implicitly included */
117 if (arfcn == min_arfcn)
118 return 0;
119
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100120 if (((arfcn - min_arfcn) & 1023) > 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100121 LOGP(DRR, LOGL_ERROR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +0530122 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +0100123 }
Harald Weltea43f7892009-12-01 18:04:30 +0530124
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100125 bitno = (arfcn - min_arfcn) & 1023;
Harald Weltea43f7892009-12-01 18:04:30 +0530126 byte = bitno / 8;
127 bit = bitno % 8;
128
129 chan_list[2 + byte] |= 1 << (7 - bit);
130
131 return 0;
132}
133
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200134/* generate a variable bitmap */
135static int enc_freq_lst_var_bitmap(uint8_t *chan_list,
136 struct bitvec *bv, const struct gsm_bts *bts,
137 int bis, int ter, int min, int pgsm)
138{
139 int i;
140
141 /* set it to 'Variable bitmap format' */
142 chan_list[0] = 0x8e;
143
144 chan_list[0] |= (min >> 9) & 1;
145 chan_list[1] = (min >> 1);
146 chan_list[2] = (min & 1) << 7;
147
148 for (i = 0; i < bv->data_len*8; i++) {
149 /* see notes in bitvec2freq_list */
150 if (bitvec_get_bit_pos(bv, i)
151 && ((!bis && !ter && gsm_arfcn2band(i) == bts->band)
152 || (bis && pgsm && gsm_arfcn2band(i) == bts->band && (i < 1 || i > 124))
153 || (ter && gsm_arfcn2band(i) != bts->band))) {
154 int rc = freq_list_bmrel_set_arfcn(chan_list, i);
155 if (rc < 0)
156 return rc;
157 }
158 }
159
160 return 0;
161}
162
163/* generate a frequency list with the range 512 format */
164static int enc_freq_lst_range(uint8_t *chan_list,
165 struct bitvec *bv, const struct gsm_bts *bts,
166 int bis, int ter, int pgsm)
167{
168 int arfcns[RANGE_ENC_MAX_ARFCNS];
169 int w[RANGE_ENC_MAX_ARFCNS];
170 int f0_included = 0;
171 int arfcns_used = 0;
172 int i, rc, range, f0;
173
174 /*
175 * Select ARFCNs according to the rules in bitvec2freq_list
176 */
177 for (i = 0; i < bv->data_len * 8; ++i) {
178 /* More ARFCNs than the maximum */
179 if (arfcns_used > ARRAY_SIZE(arfcns))
180 return -1;
181 /* Check if we can select it? */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100182 if (bitvec_get_bit_pos(bv, i) && use_arfcn(bts, bis, ter, pgsm, i))
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200183 arfcns[arfcns_used++] = i;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200184 }
185
186 /*
187 * Check if the given list of ARFCNs can be encoded.
188 */
189 range = range_enc_determine_range(arfcns, arfcns_used, &f0);
190 if (range == ARFCN_RANGE_INVALID)
191 return -2;
192
193 /*
194 * Manipulate the ARFCN list according to the rules in J4 depending
195 * on the selected range.
196 */
197 arfcns_used = range_enc_filter_arfcns(range, arfcns, arfcns_used,
198 f0, &f0_included);
199
200 memset(w, 0, sizeof(w));
201 rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
202 if (rc != 0)
203 return -3;
204
205 /* Select the range and the amount of bits needed */
206 switch (range) {
207 case ARFCN_RANGE_128:
208 return range_enc_range128(chan_list, f0, w);
209 break;
210 case ARFCN_RANGE_256:
211 return range_enc_range256(chan_list, f0, w);
212 break;
213 case ARFCN_RANGE_512:
214 return range_enc_range512(chan_list, f0, w);
215 break;
216 case ARFCN_RANGE_1024:
217 return range_enc_range1024(chan_list, f0, f0_included, w);
218 break;
219 default:
220 return -4;
221 };
222}
223
Harald Weltea43f7892009-12-01 18:04:30 +0530224/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200225static int bitvec2freq_list(uint8_t *chan_list, struct bitvec *bv,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100226 const struct gsm_bts *bts, int bis, int ter)
Harald Weltea43f7892009-12-01 18:04:30 +0530227{
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200228 int i, rc, min = -1, max = -1, pgsm = 0, arfcns = 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530229
230 memset(chan_list, 0, 16);
231
Harald Welte29350342012-02-17 15:58:23 +0100232 if (bts->band == GSM_BAND_900
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100233 && bts->c0->arfcn >= 1 && bts->c0->arfcn <= 124)
234 pgsm = 1;
235 /* P-GSM-only handsets only support 'bit map 0 format' */
236 if (!bis && !ter && pgsm) {
Harald Weltea43f7892009-12-01 18:04:30 +0530237 chan_list[0] = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100238
239 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100240 if (i >= 1 && i <= 124
241 && bitvec_get_bit_pos(bv, i)) {
Harald Welte6c40def2009-12-14 22:07:14 +0100242 rc = freq_list_bm0_set_arfcn(chan_list, i);
243 if (rc < 0)
244 return rc;
245 }
Harald Weltea43f7892009-12-01 18:04:30 +0530246 }
247 return 0;
248 }
249
Harald Welte6c40def2009-12-14 22:07:14 +0100250 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100251 /* in case of SI2 or SI5 allow all neighbours in same band
252 * in case of SI*bis, allow neighbours in same band ouside pgsm
253 * in case of SI*ter, allow neighbours in different bands
254 */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100255 if (!bitvec_get_bit_pos(bv, i))
256 continue;
257 if (!use_arfcn(bts, bis, ter, pgsm, i))
258 continue;
259 /* count the arfcns we want to carry */
260 arfcns += 1;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200261
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100262 /* 955..1023 < 0..885 */
263 if (min < 0)
264 min = i;
265 if (i >= 955 && min < 955)
266 min = i;
267 if (i >= 955 && min >= 955 && i < min)
268 min = i;
269 if (i < 955 && min < 955 && i < min)
270 min = i;
271 if (max < 0)
272 max = i;
273 if (i < 955 && max >= 955)
274 max = i;
275 if (i >= 955 && max >= 955 && i > max)
276 max = i;
277 if (i < 955 && max < 955 && i > max)
278 max = i;
Harald Weltea43f7892009-12-01 18:04:30 +0530279 }
280
Sylvain Munaut42a56522009-12-24 14:20:19 +0100281 if (max == -1) {
282 /* Empty set, use 'bit map 0 format' */
283 chan_list[0] = 0;
284 return 0;
285 }
286
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200287 /* Now find the best encoding */
288 if (((max - min) & 1023) <= 111)
289 return enc_freq_lst_var_bitmap(chan_list, bv, bts, bis,
290 ter, min, pgsm);
Harald Weltea43f7892009-12-01 18:04:30 +0530291
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200292 /* Attempt to do the range encoding */
293 rc = enc_freq_lst_range(chan_list, bv, bts, bis, ter, pgsm);
294 if (rc == 0)
295 return 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530296
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200297 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, arfcns=%d "
298 "can not generate ARFCN list", min, max, arfcns);
299 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530300}
301
302/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Dieter Spaar16646022011-07-28 00:01:50 +0200303/* static*/ int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530304{
Harald Weltea43f7892009-12-01 18:04:30 +0530305 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100306 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530307
Harald Weltea39b0f22010-06-14 22:26:10 +0200308 /* Zero-initialize the bit-vector */
Harald Weltec8794b52010-06-16 11:09:18 +0200309 memset(bv->data, 0, bv->data_len);
Harald Weltea39b0f22010-06-14 22:26:10 +0200310
Harald Welte6c40def2009-12-14 22:07:14 +0100311 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
Harald Weltea39b0f22010-06-14 22:26:10 +0200312 llist_for_each_entry(trx, &bts->trx_list, list) {
313 unsigned int i, j;
314 /* Always add the TRX's ARFCN */
Harald Welte6c40def2009-12-14 22:07:14 +0100315 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea39b0f22010-06-14 22:26:10 +0200316 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
317 struct gsm_bts_trx_ts *ts = &trx->ts[i];
318 /* Add any ARFCNs present in hopping channels */
319 for (j = 0; j < 1024; j++) {
320 if (bitvec_get_bit_pos(&ts->hopping.arfcns, j))
321 bitvec_set_bit_pos(bv, j, 1);
322 }
323 }
324 }
Harald Weltea43f7892009-12-01 18:04:30 +0530325
Harald Welte6c40def2009-12-14 22:07:14 +0100326 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100327 return bitvec2freq_list(chan_list, bv, bts, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530328}
329
Harald Welte6c40def2009-12-14 22:07:14 +0100330/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100331static int generate_bcch_chan_list(uint8_t *chan_list, struct gsm_bts *bts,
332 int si5, int bis, int ter)
Harald Welte6c40def2009-12-14 22:07:14 +0100333{
334 struct gsm_bts *cur_bts;
Harald Welte64c07d22011-02-15 11:43:27 +0100335 struct bitvec *bv;
336
337 if (si5 && bts->neigh_list_manual_mode == NL_MODE_MANUAL_SI5SEP)
338 bv = &bts->si_common.si5_neigh_list;
339 else
340 bv = &bts->si_common.neigh_list;
Harald Welte6c40def2009-12-14 22:07:14 +0100341
Harald Welte32c09622011-01-11 23:44:56 +0100342 /* Generate list of neighbor cells if we are in automatic mode */
Harald Welte64c07d22011-02-15 11:43:27 +0100343 if (bts->neigh_list_manual_mode == NL_MODE_AUTOMATIC) {
Harald Welte21bbda22011-02-19 20:43:37 +0100344 /* Zero-initialize the bit-vector */
345 memset(bv->data, 0, bv->data_len);
346
Harald Welte32c09622011-01-11 23:44:56 +0100347 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
348 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
349 if (cur_bts == bts)
350 continue;
351 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
352 }
Harald Welte6c40def2009-12-14 22:07:14 +0100353 }
354
355 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100356 return bitvec2freq_list(chan_list, bv, bts, bis, ter);
357}
358
359static int list_arfcn(uint8_t *chan_list, uint8_t mask, char *text)
360{
361 int n = 0, i;
362 struct gsm_sysinfo_freq freq[1024];
363
364 memset(freq, 0, sizeof(freq));
365 gsm48_decode_freq_list(freq, chan_list, 16, 0xce, 1);
366 for (i = 0; i < 1024; i++) {
367 if (freq[i].mask) {
368 if (!n)
369 LOGP(DRR, LOGL_INFO, "%s", text);
370 LOGPC(DRR, LOGL_INFO, " %d", i);
371 n++;
372 }
373 }
374 if (n)
375 LOGPC(DRR, LOGL_INFO, "\n");
376
377 return n;
Harald Welte6c40def2009-12-14 22:07:14 +0100378}
379
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200380static int generate_si1(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530381{
382 int rc;
383 struct gsm48_system_information_type_1 *si1 =
384 (struct gsm48_system_information_type_1 *) output;
385
386 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
387
388 si1->header.l2_plen = (21 << 2) | 1;
389 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
390 si1->header.skip_indicator = 0;
391 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
392
393 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
394 if (rc < 0)
395 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100396 list_arfcn(si1->cell_channel_description, 0xce, "Serving cell:");
Harald Weltea43f7892009-12-01 18:04:30 +0530397
398 si1->rach_control = bts->si_common.rach_control;
399
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +0100400 /*
401 * SI1 Rest Octets (10.5.2.32), contains NCH position and band
402 * indicator but that is not in the 04.08.
403 */
404 rc = rest_octets_si1(si1->rest_octets, NULL, is_dcs_net(bts));
Harald Welte7401ae62010-06-15 16:44:12 +0200405
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100406 return sizeof(*si1) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530407}
408
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200409static int generate_si2(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530410{
411 int rc;
412 struct gsm48_system_information_type_2 *si2 =
413 (struct gsm48_system_information_type_2 *) output;
414
415 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
416
417 si2->header.l2_plen = (22 << 2) | 1;
418 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
419 si2->header.skip_indicator = 0;
420 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
421
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100422 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts, 0, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530423 if (rc < 0)
424 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100425 list_arfcn(si2->bcch_frequency_list, 0xce,
426 "Neighbour cells in same band:");
Harald Weltea43f7892009-12-01 18:04:30 +0530427
428 si2->ncc_permitted = bts->si_common.ncc_permitted;
429 si2->rach_control = bts->si_common.rach_control;
430
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100431 return sizeof(*si2);
Harald Weltea43f7892009-12-01 18:04:30 +0530432}
433
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100434static int generate_si2bis(uint8_t *output, struct gsm_bts *bts)
435{
436 int rc;
437 struct gsm48_system_information_type_2bis *si2b =
438 (struct gsm48_system_information_type_2bis *) output;
439 int n;
440
441 memset(si2b, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
442
443 si2b->header.l2_plen = (22 << 2) | 1;
444 si2b->header.rr_protocol_discriminator = GSM48_PDISC_RR;
445 si2b->header.skip_indicator = 0;
446 si2b->header.system_information = GSM48_MT_RR_SYSINFO_2bis;
447
448 rc = generate_bcch_chan_list(si2b->bcch_frequency_list, bts, 0, 1, 0);
449 if (rc < 0)
450 return rc;
451 n = list_arfcn(si2b->bcch_frequency_list, 0xce,
452 "Neighbour cells in same band, but outside P-GSM:");
453 if (n) {
454 /* indicate in SI2 and SI2bis: there is an extension */
455 struct gsm48_system_information_type_2 *si2 =
456 (struct gsm48_system_information_type_2 *)
457 bts->si_buf[SYSINFO_TYPE_2];
458 si2->bcch_frequency_list[0] |= 0x20;
459 si2b->bcch_frequency_list[0] |= 0x20;
460 } else
461 bts->si_valid &= ~(1 << SYSINFO_TYPE_2bis);
462
463 si2b->rach_control = bts->si_common.rach_control;
464
465 return sizeof(*si2b);
466}
467
468static int generate_si2ter(uint8_t *output, struct gsm_bts *bts)
469{
470 int rc;
471 struct gsm48_system_information_type_2ter *si2t =
472 (struct gsm48_system_information_type_2ter *) output;
473 int n;
474
475 memset(si2t, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
476
477 si2t->header.l2_plen = (22 << 2) | 1;
478 si2t->header.rr_protocol_discriminator = GSM48_PDISC_RR;
479 si2t->header.skip_indicator = 0;
480 si2t->header.system_information = GSM48_MT_RR_SYSINFO_2ter;
481
482 rc = generate_bcch_chan_list(si2t->ext_bcch_frequency_list, bts, 0, 0, 1);
483 if (rc < 0)
484 return rc;
485 n = list_arfcn(si2t->ext_bcch_frequency_list, 0x8e,
486 "Neighbour cells in different band:");
487 if (!n)
488 bts->si_valid &= ~(1 << SYSINFO_TYPE_2ter);
489
490 return sizeof(*si2t);
491}
492
Harald Welte1f893292010-03-14 23:46:48 +0800493static struct gsm48_si_ro_info si_info = {
Harald Weltea43f7892009-12-01 18:04:30 +0530494 .selection_params = {
495 .present = 0,
496 },
497 .power_offset = {
498 .present = 0,
499 },
500 .si2ter_indicator = 0,
501 .early_cm_ctrl = 1,
502 .scheduling = {
503 .present = 0,
504 },
505 .gprs_ind = {
506 .si13_position = 0,
507 .ra_colour = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800508 .present = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530509 },
510 .lsa_params = {
511 .present = 0,
512 },
513 .cell_id = 0, /* FIXME: doesn't the bts have this? */
514 .break_ind = 0,
515};
516
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200517static int generate_si3(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530518{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100519 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530520 struct gsm48_system_information_type_3 *si3 =
521 (struct gsm48_system_information_type_3 *) output;
522
523 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
524
525 si3->header.l2_plen = (18 << 2) | 1;
526 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
527 si3->header.skip_indicator = 0;
528 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
529
530 si3->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100531 gsm48_generate_lai(&si3->lai, bts->network->country_code,
532 bts->network->network_code,
533 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530534 si3->control_channel_desc = bts->si_common.chan_desc;
535 si3->cell_options = bts->si_common.cell_options;
536 si3->cell_sel_par = bts->si_common.cell_sel_par;
537 si3->rach_control = bts->si_common.rach_control;
538
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100539 if ((bts->si_valid & (1 << SYSINFO_TYPE_2ter))) {
540 LOGP(DRR, LOGL_INFO, "SI 2ter is included.\n");
541 si_info.si2ter_indicator = 1;
542 } else {
543 si_info.si2ter_indicator = 0;
544 }
545
Harald Weltea43f7892009-12-01 18:04:30 +0530546 /* SI3 Rest Octets (10.5.2.34), containing
547 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
548 Power Offset, 2ter Indicator, Early Classmark Sending,
549 Scheduling if and WHERE, GPRS Indicator, SI13 position */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100550 rc = rest_octets_si3(si3->rest_octets, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530551
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100552 return sizeof(*si3) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530553}
554
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200555static int generate_si4(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530556{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100557 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530558 struct gsm48_system_information_type_4 *si4 =
559 (struct gsm48_system_information_type_4 *) output;
560
561 /* length of all IEs present except SI4 rest octets and l2_plen */
562 int l2_plen = sizeof(*si4) - 1;
563
564 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
565
566 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
567 si4->header.skip_indicator = 0;
568 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
569
Harald Welteafedeab2010-03-04 10:55:40 +0100570 gsm48_generate_lai(&si4->lai, bts->network->country_code,
571 bts->network->network_code,
572 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530573 si4->cell_sel_par = bts->si_common.cell_sel_par;
574 si4->rach_control = bts->si_common.rach_control;
575
576 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
577
578 si4->header.l2_plen = (l2_plen << 2) | 1;
579
580 /* SI4 Rest Octets (10.5.2.35), containing
581 Optional Power offset, GPRS Indicator,
582 Cell Identity, LSA ID, Selection Parameter */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100583 rc = rest_octets_si4(si4->data, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530584
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100585 return sizeof(*si4) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530586}
587
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200588static int generate_si5(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530589{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100590 struct gsm48_system_information_type_5 *si5;
591 int rc, l2_plen = 18;
Harald Weltea43f7892009-12-01 18:04:30 +0530592
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100593 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
594
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100595 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100596 switch (bts->type) {
597 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200598 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Weltefd355a32011-03-04 13:41:31 +0100599 case GSM_BTS_TYPE_HSL_FEMTO:
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100600 *output++ = (l2_plen << 2) | 1;
601 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100602 break;
603 default:
604 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100605 }
606
607 si5 = (struct gsm48_system_information_type_5 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530608
609 /* l2 pseudo length, not part of msg: 18 */
610 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
611 si5->skip_indicator = 0;
612 si5->system_information = GSM48_MT_RR_SYSINFO_5;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100613 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts, 1, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530614 if (rc < 0)
615 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100616 list_arfcn(si5->bcch_frequency_list, 0xce,
617 "Neighbour cells in same band:");
618
619 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
620 return l2_plen;
621}
622
623static int generate_si5bis(uint8_t *output, struct gsm_bts *bts)
624{
625 struct gsm48_system_information_type_5bis *si5b;
626 int rc, l2_plen = 18;
627 int n;
628
629 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
630
631 /* ip.access nanoBTS needs l2_plen!! */
632 switch (bts->type) {
633 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200634 case GSM_BTS_TYPE_OSMO_SYSMO:
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100635 case GSM_BTS_TYPE_HSL_FEMTO:
636 *output++ = (l2_plen << 2) | 1;
637 l2_plen++;
638 break;
639 default:
640 break;
641 }
642
643 si5b = (struct gsm48_system_information_type_5bis *) output;
644
645 /* l2 pseudo length, not part of msg: 18 */
646 si5b->rr_protocol_discriminator = GSM48_PDISC_RR;
647 si5b->skip_indicator = 0;
648 si5b->system_information = GSM48_MT_RR_SYSINFO_5bis;
649 rc = generate_bcch_chan_list(si5b->bcch_frequency_list, bts, 1, 1, 0);
650 if (rc < 0)
651 return rc;
652 n = list_arfcn(si5b->bcch_frequency_list, 0xce,
653 "Neighbour cells in same band, but outside P-GSM:");
654 if (n) {
655 /* indicate in SI5 and SI5bis: there is an extension */
656 struct gsm48_system_information_type_5 *si5 =
657 (struct gsm48_system_information_type_5 *)
658 bts->si_buf[SYSINFO_TYPE_5];
659 si5->bcch_frequency_list[0] |= 0x20;
660 si5b->bcch_frequency_list[0] |= 0x20;
661 } else
662 bts->si_valid &= ~(1 << SYSINFO_TYPE_5bis);
663
664 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
665 return l2_plen;
666}
667
668static int generate_si5ter(uint8_t *output, struct gsm_bts *bts)
669{
670 struct gsm48_system_information_type_5ter *si5t;
671 int rc, l2_plen = 18;
672 int n;
673
674 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
675
676 /* ip.access nanoBTS needs l2_plen!! */
677 switch (bts->type) {
678 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200679 case GSM_BTS_TYPE_OSMO_SYSMO:
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100680 case GSM_BTS_TYPE_HSL_FEMTO:
681 *output++ = (l2_plen << 2) | 1;
682 l2_plen++;
683 break;
684 default:
685 break;
686 }
687
688 si5t = (struct gsm48_system_information_type_5ter *) output;
689
690 /* l2 pseudo length, not part of msg: 18 */
691 si5t->rr_protocol_discriminator = GSM48_PDISC_RR;
692 si5t->skip_indicator = 0;
693 si5t->system_information = GSM48_MT_RR_SYSINFO_5ter;
694 rc = generate_bcch_chan_list(si5t->bcch_frequency_list, bts, 1, 0, 1);
695 if (rc < 0)
696 return rc;
697 n = list_arfcn(si5t->bcch_frequency_list, 0x8e,
698 "Neighbour cells in different band:");
699 if (!n)
700 bts->si_valid &= ~(1 << SYSINFO_TYPE_5ter);
Harald Weltea43f7892009-12-01 18:04:30 +0530701
702 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100703 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530704}
705
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200706static int generate_si6(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530707{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100708 struct gsm48_system_information_type_6 *si6;
709 int l2_plen = 11;
Harald Weltea43f7892009-12-01 18:04:30 +0530710
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100711 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
712
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100713 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100714 switch (bts->type) {
715 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200716 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Weltefd355a32011-03-04 13:41:31 +0100717 case GSM_BTS_TYPE_HSL_FEMTO:
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100718 *output++ = (l2_plen << 2) | 1;
719 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100720 break;
721 default:
722 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100723 }
724
725 si6 = (struct gsm48_system_information_type_6 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530726
727 /* l2 pseudo length, not part of msg: 11 */
728 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
729 si6->skip_indicator = 0;
730 si6->system_information = GSM48_MT_RR_SYSINFO_6;
731 si6->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100732 gsm48_generate_lai(&si6->lai, bts->network->country_code,
733 bts->network->network_code,
734 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530735 si6->cell_options = bts->si_common.cell_options;
736 si6->ncc_permitted = bts->si_common.ncc_permitted;
737
738 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
739
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100740 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530741}
742
743static struct gsm48_si13_info si13_default = {
744 .cell_opts = {
Harald Welte85849182010-06-02 12:19:21 +0200745 .nmo = GPRS_NMO_II,
Harald Weltea4b16652010-05-31 12:54:23 +0200746 .t3168 = 2000,
Andreas Eversbergbecc89a2012-09-23 08:14:51 +0200747 .t3192 = 1500,
Harald Welte97a282b2010-03-14 15:37:43 +0800748 .drx_timer_max = 3,
Harald Weltea43f7892009-12-01 18:04:30 +0530749 .bs_cv_max = 15,
Sylvain Munaut851f1202011-10-17 13:56:02 +0200750 .ext_info_present = 0,
Harald Welteda0586a2010-04-18 14:49:05 +0200751 .ext_info = {
752 /* The values below are just guesses ! */
Harald Weltea06fea02010-04-18 15:53:40 +0200753 .egprs_supported = 0,
Harald Welteda0586a2010-04-18 14:49:05 +0200754 .use_egprs_p_ch_req = 1,
Harald Weltea4b16652010-05-31 12:54:23 +0200755 .bep_period = 5,
Harald Welteda0586a2010-04-18 14:49:05 +0200756 .pfc_supported = 0,
757 .dtm_supported = 0,
758 .bss_paging_coordination = 0,
759 },
Harald Weltea43f7892009-12-01 18:04:30 +0530760 },
761 .pwr_ctrl_pars = {
Harald Weltec15d0ac2012-10-08 21:22:08 +0200762 .alpha = 0, /* a = 0.0 */
Harald Weltea4b16652010-05-31 12:54:23 +0200763 .t_avg_w = 16,
764 .t_avg_t = 16,
Harald Weltea43f7892009-12-01 18:04:30 +0530765 .pc_meas_chan = 0, /* downling measured on CCCH */
Harald Weltea4b16652010-05-31 12:54:23 +0200766 .n_avg_i = 8,
Harald Weltea43f7892009-12-01 18:04:30 +0530767 },
Harald Welte97a282b2010-03-14 15:37:43 +0800768 .bcch_change_mark = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530769 .si_change_field = 0,
770 .pbcch_present = 0,
771 {
772 .no_pbcch = {
Harald Welte97a282b2010-03-14 15:37:43 +0800773 .rac = 0, /* needs to be patched */
Harald Weltea43f7892009-12-01 18:04:30 +0530774 .spgc_ccch_sup = 0,
775 .net_ctrl_ord = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800776 .prio_acc_thr = 6,
Harald Weltea43f7892009-12-01 18:04:30 +0530777 },
778 },
779};
780
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200781static int generate_si13(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530782{
783 struct gsm48_system_information_type_13 *si13 =
784 (struct gsm48_system_information_type_13 *) output;
785 int ret;
786
787 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
788
789 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
790 si13->header.skip_indicator = 0;
791 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
792
Harald Welte97a282b2010-03-14 15:37:43 +0800793 si13_default.no_pbcch.rac = bts->gprs.rac;
Andreas Eversberg0c8f9ca2013-03-16 16:31:26 +0100794 si13_default.no_pbcch.net_ctrl_ord = bts->gprs.net_ctrl_ord;
Harald Welte97a282b2010-03-14 15:37:43 +0800795
Harald Weltea43f7892009-12-01 18:04:30 +0530796 ret = rest_octets_si13(si13->rest_octets, &si13_default);
797 if (ret < 0)
798 return ret;
799
Holger Hans Peter Freyther1c6f3942010-06-16 12:05:56 +0800800 /* length is coded in bit 2 an up */
801 si13->header.l2_plen = 0x01;
Harald Weltea43f7892009-12-01 18:04:30 +0530802
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100803 return sizeof (*si13) + ret;
Harald Weltea43f7892009-12-01 18:04:30 +0530804}
805
Harald Welte7401ae62010-06-15 16:44:12 +0200806typedef int (*gen_si_fn_t)(uint8_t *output, struct gsm_bts *bts);
807
808static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
809 [SYSINFO_TYPE_1] = &generate_si1,
810 [SYSINFO_TYPE_2] = &generate_si2,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100811 [SYSINFO_TYPE_2bis] = &generate_si2bis,
812 [SYSINFO_TYPE_2ter] = &generate_si2ter,
Harald Welte7401ae62010-06-15 16:44:12 +0200813 [SYSINFO_TYPE_3] = &generate_si3,
814 [SYSINFO_TYPE_4] = &generate_si4,
815 [SYSINFO_TYPE_5] = &generate_si5,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100816 [SYSINFO_TYPE_5bis] = &generate_si5bis,
817 [SYSINFO_TYPE_5ter] = &generate_si5ter,
Harald Welte7401ae62010-06-15 16:44:12 +0200818 [SYSINFO_TYPE_6] = &generate_si6,
819 [SYSINFO_TYPE_13] = &generate_si13,
820};
821
Harald Welte7401ae62010-06-15 16:44:12 +0200822int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
823{
824 gen_si_fn_t gen_si;
825
Harald Welte4511d892010-04-18 15:51:20 +0200826 switch (bts->gprs.mode) {
827 case BTS_GPRS_EGPRS:
Harald Weltea06fea02010-04-18 15:53:40 +0200828 si13_default.cell_opts.ext_info_present = 1;
829 si13_default.cell_opts.ext_info.egprs_supported = 1;
830 /* fallthrough */
Harald Welte4511d892010-04-18 15:51:20 +0200831 case BTS_GPRS_GPRS:
832 si_info.gprs_ind.present = 1;
833 break;
834 case BTS_GPRS_NONE:
835 si_info.gprs_ind.present = 0;
836 break;
837 }
Harald Welte1f893292010-03-14 23:46:48 +0800838
Sylvain Munaute0b06b02010-11-28 18:17:28 +0100839 memcpy(&si_info.selection_params,
840 &bts->si_common.cell_ro_sel_par,
841 sizeof(struct gsm48_si_selection_params));
842
Harald Welte7401ae62010-06-15 16:44:12 +0200843 gen_si = gen_si_fn[si_type];
844 if (!gen_si)
Harald Weltea43f7892009-12-01 18:04:30 +0530845 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530846
Harald Welte7401ae62010-06-15 16:44:12 +0200847 return gen_si(bts->si_buf[si_type], bts);
Harald Weltea43f7892009-12-01 18:04:30 +0530848}