blob: dbee249e8aa132197844d31f62484f3331def23c [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 Freytherb91a1062010-01-06 06:38:14 +010040
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +010041static int use_arfcn(const struct gsm_bts *bts, const int bis, const int ter,
42 const int pgsm, const int arfcn)
43{
44 if (!bis && !ter && gsm_arfcn2band(arfcn) == bts->band)
45 return 1;
46 if (bis && pgsm && gsm_arfcn2band(arfcn) == bts->band && (arfcn < 1 || arfcn > 124))
47 return 1;
48 if (ter && gsm_arfcn2band(arfcn) != bts->band)
49 return 1;
50 return 0;
51}
52
Harald Welteda760d32009-12-14 20:25:05 +010053/* Frequency Lists as per TS 04.08 10.5.2.13 */
54
55/* 10.5.2.13.2: Bit map 0 format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020056static int freq_list_bm0_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +053057{
58 unsigned int byte, bit;
59
Harald Welteb1d4c8e2009-12-17 23:10:46 +010060 if (arfcn > 124 || arfcn < 1) {
61 LOGP(DRR, LOGL_ERROR, "Bitmap 0 only supports ARFCN 1...124\n");
Harald Weltea43f7892009-12-01 18:04:30 +053062 return -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +010063 }
Harald Weltea43f7892009-12-01 18:04:30 +053064
Harald Welteda760d32009-12-14 20:25:05 +010065 /* the bitmask is from 1..124, not from 0..123 */
66 arfcn--;
67
Harald Weltea43f7892009-12-01 18:04:30 +053068 byte = arfcn / 8;
69 bit = arfcn % 8;
70
Harald Welteda760d32009-12-14 20:25:05 +010071 chan_list[GSM48_CELL_CHAN_DESC_SIZE-1-byte] |= (1 << bit);
Harald Weltea43f7892009-12-01 18:04:30 +053072
73 return 0;
74}
75
Harald Welteda760d32009-12-14 20:25:05 +010076/* 10.5.2.13.7: Variable bit map format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020077static int freq_list_bmrel_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +053078{
79 unsigned int byte, bit;
80 unsigned int min_arfcn;
81 unsigned int bitno;
82
83 min_arfcn = (chan_list[0] & 1) << 9;
84 min_arfcn |= chan_list[1] << 1;
85 min_arfcn |= (chan_list[2] >> 7) & 1;
86
87 /* The lower end of our bitmaks is always implicitly included */
88 if (arfcn == min_arfcn)
89 return 0;
90
Andreas Eversbergc50543b2012-02-29 09:04:07 +010091 if (((arfcn - min_arfcn) & 1023) > 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +010092 LOGP(DRR, LOGL_ERROR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +053093 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +010094 }
Harald Weltea43f7892009-12-01 18:04:30 +053095
Andreas Eversbergc50543b2012-02-29 09:04:07 +010096 bitno = (arfcn - min_arfcn) & 1023;
Harald Weltea43f7892009-12-01 18:04:30 +053097 byte = bitno / 8;
98 bit = bitno % 8;
99
100 chan_list[2 + byte] |= 1 << (7 - bit);
101
102 return 0;
103}
104
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200105/* generate a variable bitmap */
106static int enc_freq_lst_var_bitmap(uint8_t *chan_list,
107 struct bitvec *bv, const struct gsm_bts *bts,
108 int bis, int ter, int min, int pgsm)
109{
110 int i;
111
112 /* set it to 'Variable bitmap format' */
113 chan_list[0] = 0x8e;
114
115 chan_list[0] |= (min >> 9) & 1;
116 chan_list[1] = (min >> 1);
117 chan_list[2] = (min & 1) << 7;
118
119 for (i = 0; i < bv->data_len*8; i++) {
120 /* see notes in bitvec2freq_list */
121 if (bitvec_get_bit_pos(bv, i)
122 && ((!bis && !ter && gsm_arfcn2band(i) == bts->band)
123 || (bis && pgsm && gsm_arfcn2band(i) == bts->band && (i < 1 || i > 124))
124 || (ter && gsm_arfcn2band(i) != bts->band))) {
125 int rc = freq_list_bmrel_set_arfcn(chan_list, i);
126 if (rc < 0)
127 return rc;
128 }
129 }
130
131 return 0;
132}
133
134/* generate a frequency list with the range 512 format */
135static int enc_freq_lst_range(uint8_t *chan_list,
136 struct bitvec *bv, const struct gsm_bts *bts,
137 int bis, int ter, int pgsm)
138{
139 int arfcns[RANGE_ENC_MAX_ARFCNS];
140 int w[RANGE_ENC_MAX_ARFCNS];
141 int f0_included = 0;
142 int arfcns_used = 0;
143 int i, rc, range, f0;
144
145 /*
146 * Select ARFCNs according to the rules in bitvec2freq_list
147 */
148 for (i = 0; i < bv->data_len * 8; ++i) {
149 /* More ARFCNs than the maximum */
150 if (arfcns_used > ARRAY_SIZE(arfcns))
151 return -1;
152 /* Check if we can select it? */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100153 if (bitvec_get_bit_pos(bv, i) && use_arfcn(bts, bis, ter, pgsm, i))
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200154 arfcns[arfcns_used++] = i;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200155 }
156
157 /*
158 * Check if the given list of ARFCNs can be encoded.
159 */
160 range = range_enc_determine_range(arfcns, arfcns_used, &f0);
161 if (range == ARFCN_RANGE_INVALID)
162 return -2;
163
164 /*
165 * Manipulate the ARFCN list according to the rules in J4 depending
166 * on the selected range.
167 */
168 arfcns_used = range_enc_filter_arfcns(range, arfcns, arfcns_used,
169 f0, &f0_included);
170
171 memset(w, 0, sizeof(w));
172 rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
173 if (rc != 0)
174 return -3;
175
176 /* Select the range and the amount of bits needed */
177 switch (range) {
178 case ARFCN_RANGE_128:
179 return range_enc_range128(chan_list, f0, w);
180 break;
181 case ARFCN_RANGE_256:
182 return range_enc_range256(chan_list, f0, w);
183 break;
184 case ARFCN_RANGE_512:
185 return range_enc_range512(chan_list, f0, w);
186 break;
187 case ARFCN_RANGE_1024:
188 return range_enc_range1024(chan_list, f0, f0_included, w);
189 break;
190 default:
191 return -4;
192 };
193}
194
Harald Weltea43f7892009-12-01 18:04:30 +0530195/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200196static int bitvec2freq_list(uint8_t *chan_list, struct bitvec *bv,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100197 const struct gsm_bts *bts, int bis, int ter)
Harald Weltea43f7892009-12-01 18:04:30 +0530198{
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200199 int i, rc, min = -1, max = -1, pgsm = 0, arfcns = 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530200
201 memset(chan_list, 0, 16);
202
Harald Welte29350342012-02-17 15:58:23 +0100203 if (bts->band == GSM_BAND_900
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100204 && bts->c0->arfcn >= 1 && bts->c0->arfcn <= 124)
205 pgsm = 1;
206 /* P-GSM-only handsets only support 'bit map 0 format' */
207 if (!bis && !ter && pgsm) {
Harald Weltea43f7892009-12-01 18:04:30 +0530208 chan_list[0] = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100209
210 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100211 if (i >= 1 && i <= 124
212 && bitvec_get_bit_pos(bv, i)) {
Harald Welte6c40def2009-12-14 22:07:14 +0100213 rc = freq_list_bm0_set_arfcn(chan_list, i);
214 if (rc < 0)
215 return rc;
216 }
Harald Weltea43f7892009-12-01 18:04:30 +0530217 }
218 return 0;
219 }
220
Harald Welte6c40def2009-12-14 22:07:14 +0100221 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100222 /* in case of SI2 or SI5 allow all neighbours in same band
223 * in case of SI*bis, allow neighbours in same band ouside pgsm
224 * in case of SI*ter, allow neighbours in different bands
225 */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100226 if (!bitvec_get_bit_pos(bv, i))
227 continue;
228 if (!use_arfcn(bts, bis, ter, pgsm, i))
229 continue;
230 /* count the arfcns we want to carry */
231 arfcns += 1;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200232
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100233 /* 955..1023 < 0..885 */
234 if (min < 0)
235 min = i;
236 if (i >= 955 && min < 955)
237 min = i;
238 if (i >= 955 && min >= 955 && i < min)
239 min = i;
240 if (i < 955 && min < 955 && i < min)
241 min = i;
242 if (max < 0)
243 max = i;
244 if (i < 955 && max >= 955)
245 max = i;
246 if (i >= 955 && max >= 955 && i > max)
247 max = i;
248 if (i < 955 && max < 955 && i > max)
249 max = i;
Harald Weltea43f7892009-12-01 18:04:30 +0530250 }
251
Sylvain Munaut42a56522009-12-24 14:20:19 +0100252 if (max == -1) {
253 /* Empty set, use 'bit map 0 format' */
254 chan_list[0] = 0;
255 return 0;
256 }
257
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200258 /* Now find the best encoding */
259 if (((max - min) & 1023) <= 111)
260 return enc_freq_lst_var_bitmap(chan_list, bv, bts, bis,
261 ter, min, pgsm);
Harald Weltea43f7892009-12-01 18:04:30 +0530262
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200263 /* Attempt to do the range encoding */
264 rc = enc_freq_lst_range(chan_list, bv, bts, bis, ter, pgsm);
265 if (rc == 0)
266 return 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530267
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200268 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, arfcns=%d "
269 "can not generate ARFCN list", min, max, arfcns);
270 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530271}
272
273/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Dieter Spaar16646022011-07-28 00:01:50 +0200274/* static*/ int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530275{
Harald Weltea43f7892009-12-01 18:04:30 +0530276 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100277 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530278
Harald Weltea39b0f22010-06-14 22:26:10 +0200279 /* Zero-initialize the bit-vector */
Harald Weltec8794b52010-06-16 11:09:18 +0200280 memset(bv->data, 0, bv->data_len);
Harald Weltea39b0f22010-06-14 22:26:10 +0200281
Harald Welte6c40def2009-12-14 22:07:14 +0100282 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
Harald Weltea39b0f22010-06-14 22:26:10 +0200283 llist_for_each_entry(trx, &bts->trx_list, list) {
284 unsigned int i, j;
285 /* Always add the TRX's ARFCN */
Harald Welte6c40def2009-12-14 22:07:14 +0100286 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea39b0f22010-06-14 22:26:10 +0200287 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
288 struct gsm_bts_trx_ts *ts = &trx->ts[i];
289 /* Add any ARFCNs present in hopping channels */
290 for (j = 0; j < 1024; j++) {
291 if (bitvec_get_bit_pos(&ts->hopping.arfcns, j))
292 bitvec_set_bit_pos(bv, j, 1);
293 }
294 }
295 }
Harald Weltea43f7892009-12-01 18:04:30 +0530296
Harald Welte6c40def2009-12-14 22:07:14 +0100297 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100298 return bitvec2freq_list(chan_list, bv, bts, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530299}
300
Harald Welte6c40def2009-12-14 22:07:14 +0100301/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100302static int generate_bcch_chan_list(uint8_t *chan_list, struct gsm_bts *bts,
303 int si5, int bis, int ter)
Harald Welte6c40def2009-12-14 22:07:14 +0100304{
305 struct gsm_bts *cur_bts;
Harald Welte64c07d22011-02-15 11:43:27 +0100306 struct bitvec *bv;
307
308 if (si5 && bts->neigh_list_manual_mode == NL_MODE_MANUAL_SI5SEP)
309 bv = &bts->si_common.si5_neigh_list;
310 else
311 bv = &bts->si_common.neigh_list;
Harald Welte6c40def2009-12-14 22:07:14 +0100312
Harald Welte32c09622011-01-11 23:44:56 +0100313 /* Generate list of neighbor cells if we are in automatic mode */
Harald Welte64c07d22011-02-15 11:43:27 +0100314 if (bts->neigh_list_manual_mode == NL_MODE_AUTOMATIC) {
Harald Welte21bbda22011-02-19 20:43:37 +0100315 /* Zero-initialize the bit-vector */
316 memset(bv->data, 0, bv->data_len);
317
Harald Welte32c09622011-01-11 23:44:56 +0100318 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
319 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
320 if (cur_bts == bts)
321 continue;
322 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
323 }
Harald Welte6c40def2009-12-14 22:07:14 +0100324 }
325
326 /* 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, bis, ter);
328}
329
330static int list_arfcn(uint8_t *chan_list, uint8_t mask, char *text)
331{
332 int n = 0, i;
333 struct gsm_sysinfo_freq freq[1024];
334
335 memset(freq, 0, sizeof(freq));
336 gsm48_decode_freq_list(freq, chan_list, 16, 0xce, 1);
337 for (i = 0; i < 1024; i++) {
338 if (freq[i].mask) {
339 if (!n)
340 LOGP(DRR, LOGL_INFO, "%s", text);
341 LOGPC(DRR, LOGL_INFO, " %d", i);
342 n++;
343 }
344 }
345 if (n)
346 LOGPC(DRR, LOGL_INFO, "\n");
347
348 return n;
Harald Welte6c40def2009-12-14 22:07:14 +0100349}
350
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200351static int generate_si1(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530352{
353 int rc;
354 struct gsm48_system_information_type_1 *si1 =
355 (struct gsm48_system_information_type_1 *) output;
356
357 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
358
359 si1->header.l2_plen = (21 << 2) | 1;
360 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
361 si1->header.skip_indicator = 0;
362 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
363
364 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
365 if (rc < 0)
366 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100367 list_arfcn(si1->cell_channel_description, 0xce, "Serving cell:");
Harald Weltea43f7892009-12-01 18:04:30 +0530368
369 si1->rach_control = bts->si_common.rach_control;
370
371 /* SI1 Rest Octets (10.5.2.32), contains NCH position */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100372 rc = rest_octets_si1(si1->rest_octets, NULL);
Harald Welte7401ae62010-06-15 16:44:12 +0200373
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100374 return sizeof(*si1) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530375}
376
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200377static int generate_si2(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530378{
379 int rc;
380 struct gsm48_system_information_type_2 *si2 =
381 (struct gsm48_system_information_type_2 *) output;
382
383 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
384
385 si2->header.l2_plen = (22 << 2) | 1;
386 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
387 si2->header.skip_indicator = 0;
388 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
389
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100390 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts, 0, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530391 if (rc < 0)
392 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100393 list_arfcn(si2->bcch_frequency_list, 0xce,
394 "Neighbour cells in same band:");
Harald Weltea43f7892009-12-01 18:04:30 +0530395
396 si2->ncc_permitted = bts->si_common.ncc_permitted;
397 si2->rach_control = bts->si_common.rach_control;
398
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100399 return sizeof(*si2);
Harald Weltea43f7892009-12-01 18:04:30 +0530400}
401
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100402static int generate_si2bis(uint8_t *output, struct gsm_bts *bts)
403{
404 int rc;
405 struct gsm48_system_information_type_2bis *si2b =
406 (struct gsm48_system_information_type_2bis *) output;
407 int n;
408
409 memset(si2b, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
410
411 si2b->header.l2_plen = (22 << 2) | 1;
412 si2b->header.rr_protocol_discriminator = GSM48_PDISC_RR;
413 si2b->header.skip_indicator = 0;
414 si2b->header.system_information = GSM48_MT_RR_SYSINFO_2bis;
415
416 rc = generate_bcch_chan_list(si2b->bcch_frequency_list, bts, 0, 1, 0);
417 if (rc < 0)
418 return rc;
419 n = list_arfcn(si2b->bcch_frequency_list, 0xce,
420 "Neighbour cells in same band, but outside P-GSM:");
421 if (n) {
422 /* indicate in SI2 and SI2bis: there is an extension */
423 struct gsm48_system_information_type_2 *si2 =
424 (struct gsm48_system_information_type_2 *)
425 bts->si_buf[SYSINFO_TYPE_2];
426 si2->bcch_frequency_list[0] |= 0x20;
427 si2b->bcch_frequency_list[0] |= 0x20;
428 } else
429 bts->si_valid &= ~(1 << SYSINFO_TYPE_2bis);
430
431 si2b->rach_control = bts->si_common.rach_control;
432
433 return sizeof(*si2b);
434}
435
436static int generate_si2ter(uint8_t *output, struct gsm_bts *bts)
437{
438 int rc;
439 struct gsm48_system_information_type_2ter *si2t =
440 (struct gsm48_system_information_type_2ter *) output;
441 int n;
442
443 memset(si2t, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
444
445 si2t->header.l2_plen = (22 << 2) | 1;
446 si2t->header.rr_protocol_discriminator = GSM48_PDISC_RR;
447 si2t->header.skip_indicator = 0;
448 si2t->header.system_information = GSM48_MT_RR_SYSINFO_2ter;
449
450 rc = generate_bcch_chan_list(si2t->ext_bcch_frequency_list, bts, 0, 0, 1);
451 if (rc < 0)
452 return rc;
453 n = list_arfcn(si2t->ext_bcch_frequency_list, 0x8e,
454 "Neighbour cells in different band:");
455 if (!n)
456 bts->si_valid &= ~(1 << SYSINFO_TYPE_2ter);
457
458 return sizeof(*si2t);
459}
460
Harald Welte1f893292010-03-14 23:46:48 +0800461static struct gsm48_si_ro_info si_info = {
Harald Weltea43f7892009-12-01 18:04:30 +0530462 .selection_params = {
463 .present = 0,
464 },
465 .power_offset = {
466 .present = 0,
467 },
468 .si2ter_indicator = 0,
469 .early_cm_ctrl = 1,
470 .scheduling = {
471 .present = 0,
472 },
473 .gprs_ind = {
474 .si13_position = 0,
475 .ra_colour = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800476 .present = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530477 },
478 .lsa_params = {
479 .present = 0,
480 },
481 .cell_id = 0, /* FIXME: doesn't the bts have this? */
482 .break_ind = 0,
483};
484
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200485static int generate_si3(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530486{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100487 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530488 struct gsm48_system_information_type_3 *si3 =
489 (struct gsm48_system_information_type_3 *) output;
490
491 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
492
493 si3->header.l2_plen = (18 << 2) | 1;
494 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
495 si3->header.skip_indicator = 0;
496 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
497
498 si3->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100499 gsm48_generate_lai(&si3->lai, bts->network->country_code,
500 bts->network->network_code,
501 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530502 si3->control_channel_desc = bts->si_common.chan_desc;
503 si3->cell_options = bts->si_common.cell_options;
504 si3->cell_sel_par = bts->si_common.cell_sel_par;
505 si3->rach_control = bts->si_common.rach_control;
506
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100507 if ((bts->si_valid & (1 << SYSINFO_TYPE_2ter))) {
508 LOGP(DRR, LOGL_INFO, "SI 2ter is included.\n");
509 si_info.si2ter_indicator = 1;
510 } else {
511 si_info.si2ter_indicator = 0;
512 }
513
Harald Weltea43f7892009-12-01 18:04:30 +0530514 /* SI3 Rest Octets (10.5.2.34), containing
515 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
516 Power Offset, 2ter Indicator, Early Classmark Sending,
517 Scheduling if and WHERE, GPRS Indicator, SI13 position */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100518 rc = rest_octets_si3(si3->rest_octets, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530519
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100520 return sizeof(*si3) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530521}
522
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200523static int generate_si4(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530524{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100525 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530526 struct gsm48_system_information_type_4 *si4 =
527 (struct gsm48_system_information_type_4 *) output;
528
529 /* length of all IEs present except SI4 rest octets and l2_plen */
530 int l2_plen = sizeof(*si4) - 1;
531
532 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
533
534 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
535 si4->header.skip_indicator = 0;
536 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
537
Harald Welteafedeab2010-03-04 10:55:40 +0100538 gsm48_generate_lai(&si4->lai, bts->network->country_code,
539 bts->network->network_code,
540 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530541 si4->cell_sel_par = bts->si_common.cell_sel_par;
542 si4->rach_control = bts->si_common.rach_control;
543
544 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
545
546 si4->header.l2_plen = (l2_plen << 2) | 1;
547
548 /* SI4 Rest Octets (10.5.2.35), containing
549 Optional Power offset, GPRS Indicator,
550 Cell Identity, LSA ID, Selection Parameter */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100551 rc = rest_octets_si4(si4->data, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530552
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100553 return sizeof(*si4) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530554}
555
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200556static int generate_si5(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530557{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100558 struct gsm48_system_information_type_5 *si5;
559 int rc, l2_plen = 18;
Harald Weltea43f7892009-12-01 18:04:30 +0530560
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100561 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
562
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100563 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100564 switch (bts->type) {
565 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200566 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Weltefd355a32011-03-04 13:41:31 +0100567 case GSM_BTS_TYPE_HSL_FEMTO:
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100568 *output++ = (l2_plen << 2) | 1;
569 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100570 break;
571 default:
572 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100573 }
574
575 si5 = (struct gsm48_system_information_type_5 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530576
577 /* l2 pseudo length, not part of msg: 18 */
578 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
579 si5->skip_indicator = 0;
580 si5->system_information = GSM48_MT_RR_SYSINFO_5;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100581 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts, 1, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530582 if (rc < 0)
583 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100584 list_arfcn(si5->bcch_frequency_list, 0xce,
585 "Neighbour cells in same band:");
586
587 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
588 return l2_plen;
589}
590
591static int generate_si5bis(uint8_t *output, struct gsm_bts *bts)
592{
593 struct gsm48_system_information_type_5bis *si5b;
594 int rc, l2_plen = 18;
595 int n;
596
597 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
598
599 /* ip.access nanoBTS needs l2_plen!! */
600 switch (bts->type) {
601 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200602 case GSM_BTS_TYPE_OSMO_SYSMO:
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100603 case GSM_BTS_TYPE_HSL_FEMTO:
604 *output++ = (l2_plen << 2) | 1;
605 l2_plen++;
606 break;
607 default:
608 break;
609 }
610
611 si5b = (struct gsm48_system_information_type_5bis *) output;
612
613 /* l2 pseudo length, not part of msg: 18 */
614 si5b->rr_protocol_discriminator = GSM48_PDISC_RR;
615 si5b->skip_indicator = 0;
616 si5b->system_information = GSM48_MT_RR_SYSINFO_5bis;
617 rc = generate_bcch_chan_list(si5b->bcch_frequency_list, bts, 1, 1, 0);
618 if (rc < 0)
619 return rc;
620 n = list_arfcn(si5b->bcch_frequency_list, 0xce,
621 "Neighbour cells in same band, but outside P-GSM:");
622 if (n) {
623 /* indicate in SI5 and SI5bis: there is an extension */
624 struct gsm48_system_information_type_5 *si5 =
625 (struct gsm48_system_information_type_5 *)
626 bts->si_buf[SYSINFO_TYPE_5];
627 si5->bcch_frequency_list[0] |= 0x20;
628 si5b->bcch_frequency_list[0] |= 0x20;
629 } else
630 bts->si_valid &= ~(1 << SYSINFO_TYPE_5bis);
631
632 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
633 return l2_plen;
634}
635
636static int generate_si5ter(uint8_t *output, struct gsm_bts *bts)
637{
638 struct gsm48_system_information_type_5ter *si5t;
639 int rc, l2_plen = 18;
640 int n;
641
642 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
643
644 /* ip.access nanoBTS needs l2_plen!! */
645 switch (bts->type) {
646 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200647 case GSM_BTS_TYPE_OSMO_SYSMO:
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100648 case GSM_BTS_TYPE_HSL_FEMTO:
649 *output++ = (l2_plen << 2) | 1;
650 l2_plen++;
651 break;
652 default:
653 break;
654 }
655
656 si5t = (struct gsm48_system_information_type_5ter *) output;
657
658 /* l2 pseudo length, not part of msg: 18 */
659 si5t->rr_protocol_discriminator = GSM48_PDISC_RR;
660 si5t->skip_indicator = 0;
661 si5t->system_information = GSM48_MT_RR_SYSINFO_5ter;
662 rc = generate_bcch_chan_list(si5t->bcch_frequency_list, bts, 1, 0, 1);
663 if (rc < 0)
664 return rc;
665 n = list_arfcn(si5t->bcch_frequency_list, 0x8e,
666 "Neighbour cells in different band:");
667 if (!n)
668 bts->si_valid &= ~(1 << SYSINFO_TYPE_5ter);
Harald Weltea43f7892009-12-01 18:04:30 +0530669
670 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100671 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530672}
673
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200674static int generate_si6(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530675{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100676 struct gsm48_system_information_type_6 *si6;
677 int l2_plen = 11;
Harald Weltea43f7892009-12-01 18:04:30 +0530678
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100679 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
680
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100681 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100682 switch (bts->type) {
683 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200684 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Weltefd355a32011-03-04 13:41:31 +0100685 case GSM_BTS_TYPE_HSL_FEMTO:
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100686 *output++ = (l2_plen << 2) | 1;
687 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100688 break;
689 default:
690 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100691 }
692
693 si6 = (struct gsm48_system_information_type_6 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530694
695 /* l2 pseudo length, not part of msg: 11 */
696 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
697 si6->skip_indicator = 0;
698 si6->system_information = GSM48_MT_RR_SYSINFO_6;
699 si6->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100700 gsm48_generate_lai(&si6->lai, bts->network->country_code,
701 bts->network->network_code,
702 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530703 si6->cell_options = bts->si_common.cell_options;
704 si6->ncc_permitted = bts->si_common.ncc_permitted;
705
706 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
707
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100708 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530709}
710
711static struct gsm48_si13_info si13_default = {
712 .cell_opts = {
Harald Welte85849182010-06-02 12:19:21 +0200713 .nmo = GPRS_NMO_II,
Harald Weltea4b16652010-05-31 12:54:23 +0200714 .t3168 = 2000,
Andreas Eversbergbecc89a2012-09-23 08:14:51 +0200715 .t3192 = 1500,
Harald Welte97a282b2010-03-14 15:37:43 +0800716 .drx_timer_max = 3,
Harald Weltea43f7892009-12-01 18:04:30 +0530717 .bs_cv_max = 15,
Sylvain Munaut851f1202011-10-17 13:56:02 +0200718 .ext_info_present = 0,
Harald Welteda0586a2010-04-18 14:49:05 +0200719 .ext_info = {
720 /* The values below are just guesses ! */
Harald Weltea06fea02010-04-18 15:53:40 +0200721 .egprs_supported = 0,
Harald Welteda0586a2010-04-18 14:49:05 +0200722 .use_egprs_p_ch_req = 1,
Harald Weltea4b16652010-05-31 12:54:23 +0200723 .bep_period = 5,
Harald Welteda0586a2010-04-18 14:49:05 +0200724 .pfc_supported = 0,
725 .dtm_supported = 0,
726 .bss_paging_coordination = 0,
727 },
Harald Weltea43f7892009-12-01 18:04:30 +0530728 },
729 .pwr_ctrl_pars = {
Harald Weltec15d0ac2012-10-08 21:22:08 +0200730 .alpha = 0, /* a = 0.0 */
Harald Weltea4b16652010-05-31 12:54:23 +0200731 .t_avg_w = 16,
732 .t_avg_t = 16,
Harald Weltea43f7892009-12-01 18:04:30 +0530733 .pc_meas_chan = 0, /* downling measured on CCCH */
Harald Weltea4b16652010-05-31 12:54:23 +0200734 .n_avg_i = 8,
Harald Weltea43f7892009-12-01 18:04:30 +0530735 },
Harald Welte97a282b2010-03-14 15:37:43 +0800736 .bcch_change_mark = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530737 .si_change_field = 0,
738 .pbcch_present = 0,
739 {
740 .no_pbcch = {
Harald Welte97a282b2010-03-14 15:37:43 +0800741 .rac = 0, /* needs to be patched */
Harald Weltea43f7892009-12-01 18:04:30 +0530742 .spgc_ccch_sup = 0,
743 .net_ctrl_ord = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800744 .prio_acc_thr = 6,
Harald Weltea43f7892009-12-01 18:04:30 +0530745 },
746 },
747};
748
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200749static int generate_si13(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530750{
751 struct gsm48_system_information_type_13 *si13 =
752 (struct gsm48_system_information_type_13 *) output;
753 int ret;
754
755 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
756
757 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
758 si13->header.skip_indicator = 0;
759 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
760
Harald Welte97a282b2010-03-14 15:37:43 +0800761 si13_default.no_pbcch.rac = bts->gprs.rac;
762
Harald Weltea43f7892009-12-01 18:04:30 +0530763 ret = rest_octets_si13(si13->rest_octets, &si13_default);
764 if (ret < 0)
765 return ret;
766
Holger Hans Peter Freyther1c6f3942010-06-16 12:05:56 +0800767 /* length is coded in bit 2 an up */
768 si13->header.l2_plen = 0x01;
Harald Weltea43f7892009-12-01 18:04:30 +0530769
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100770 return sizeof (*si13) + ret;
Harald Weltea43f7892009-12-01 18:04:30 +0530771}
772
Harald Welte7401ae62010-06-15 16:44:12 +0200773typedef int (*gen_si_fn_t)(uint8_t *output, struct gsm_bts *bts);
774
775static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
776 [SYSINFO_TYPE_1] = &generate_si1,
777 [SYSINFO_TYPE_2] = &generate_si2,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100778 [SYSINFO_TYPE_2bis] = &generate_si2bis,
779 [SYSINFO_TYPE_2ter] = &generate_si2ter,
Harald Welte7401ae62010-06-15 16:44:12 +0200780 [SYSINFO_TYPE_3] = &generate_si3,
781 [SYSINFO_TYPE_4] = &generate_si4,
782 [SYSINFO_TYPE_5] = &generate_si5,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100783 [SYSINFO_TYPE_5bis] = &generate_si5bis,
784 [SYSINFO_TYPE_5ter] = &generate_si5ter,
Harald Welte7401ae62010-06-15 16:44:12 +0200785 [SYSINFO_TYPE_6] = &generate_si6,
786 [SYSINFO_TYPE_13] = &generate_si13,
787};
788
Harald Welte7401ae62010-06-15 16:44:12 +0200789int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
790{
791 gen_si_fn_t gen_si;
792
Harald Welte4511d892010-04-18 15:51:20 +0200793 switch (bts->gprs.mode) {
794 case BTS_GPRS_EGPRS:
Harald Weltea06fea02010-04-18 15:53:40 +0200795 si13_default.cell_opts.ext_info_present = 1;
796 si13_default.cell_opts.ext_info.egprs_supported = 1;
797 /* fallthrough */
Harald Welte4511d892010-04-18 15:51:20 +0200798 case BTS_GPRS_GPRS:
799 si_info.gprs_ind.present = 1;
800 break;
801 case BTS_GPRS_NONE:
802 si_info.gprs_ind.present = 0;
803 break;
804 }
Harald Welte1f893292010-03-14 23:46:48 +0800805
Sylvain Munaute0b06b02010-11-28 18:17:28 +0100806 memcpy(&si_info.selection_params,
807 &bts->si_common.cell_ro_sel_par,
808 sizeof(struct gsm48_si_selection_params));
809
Harald Welte7401ae62010-06-15 16:44:12 +0200810 gen_si = gen_si_fn[si_type];
811 if (!gen_si)
Harald Weltea43f7892009-12-01 18:04:30 +0530812 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530813
Harald Welte7401ae62010-06-15 16:44:12 +0200814 return gen_si(bts->si_buf[si_type], bts);
Harald Weltea43f7892009-12-01 18:04:30 +0530815}