blob: 702924135d9660edb8554eeb4578e932c7be3fde [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
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 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020044static int freq_list_bm0_set_arfcn(uint8_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 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020065static int freq_list_bmrel_set_arfcn(uint8_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
Andreas Eversbergc50543b2012-02-29 09:04:07 +010079 if (((arfcn - min_arfcn) & 1023) > 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +010080 LOGP(DRR, LOGL_ERROR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +053081 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +010082 }
Harald Weltea43f7892009-12-01 18:04:30 +053083
Andreas Eversbergc50543b2012-02-29 09:04:07 +010084 bitno = (arfcn - min_arfcn) & 1023;
Harald Weltea43f7892009-12-01 18:04:30 +053085 byte = bitno / 8;
86 bit = bitno % 8;
87
88 chan_list[2 + byte] |= 1 << (7 - bit);
89
90 return 0;
91}
92
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +020093/* generate a variable bitmap */
94static int enc_freq_lst_var_bitmap(uint8_t *chan_list,
95 struct bitvec *bv, const struct gsm_bts *bts,
96 int bis, int ter, int min, int pgsm)
97{
98 int i;
99
100 /* set it to 'Variable bitmap format' */
101 chan_list[0] = 0x8e;
102
103 chan_list[0] |= (min >> 9) & 1;
104 chan_list[1] = (min >> 1);
105 chan_list[2] = (min & 1) << 7;
106
107 for (i = 0; i < bv->data_len*8; i++) {
108 /* see notes in bitvec2freq_list */
109 if (bitvec_get_bit_pos(bv, i)
110 && ((!bis && !ter && gsm_arfcn2band(i) == bts->band)
111 || (bis && pgsm && gsm_arfcn2band(i) == bts->band && (i < 1 || i > 124))
112 || (ter && gsm_arfcn2band(i) != bts->band))) {
113 int rc = freq_list_bmrel_set_arfcn(chan_list, i);
114 if (rc < 0)
115 return rc;
116 }
117 }
118
119 return 0;
120}
121
122/* generate a frequency list with the range 512 format */
123static int enc_freq_lst_range(uint8_t *chan_list,
124 struct bitvec *bv, const struct gsm_bts *bts,
125 int bis, int ter, int pgsm)
126{
127 int arfcns[RANGE_ENC_MAX_ARFCNS];
128 int w[RANGE_ENC_MAX_ARFCNS];
129 int f0_included = 0;
130 int arfcns_used = 0;
131 int i, rc, range, f0;
132
133 /*
134 * Select ARFCNs according to the rules in bitvec2freq_list
135 */
136 for (i = 0; i < bv->data_len * 8; ++i) {
137 /* More ARFCNs than the maximum */
138 if (arfcns_used > ARRAY_SIZE(arfcns))
139 return -1;
140 /* Check if we can select it? */
141 if (bitvec_get_bit_pos(bv, i)
142 && ((!bis && !ter && gsm_arfcn2band(i) == bts->band)
143 || (bis && pgsm && gsm_arfcn2band(i) == bts->band && (i < 1 || i > 124))
144 || (ter && gsm_arfcn2band(i) != bts->band))) {
145 arfcns[arfcns_used++] = i;
146 }
147 }
148
149 /*
150 * Check if the given list of ARFCNs can be encoded.
151 */
152 range = range_enc_determine_range(arfcns, arfcns_used, &f0);
153 if (range == ARFCN_RANGE_INVALID)
154 return -2;
155
156 /*
157 * Manipulate the ARFCN list according to the rules in J4 depending
158 * on the selected range.
159 */
160 arfcns_used = range_enc_filter_arfcns(range, arfcns, arfcns_used,
161 f0, &f0_included);
162
163 memset(w, 0, sizeof(w));
164 rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
165 if (rc != 0)
166 return -3;
167
168 /* Select the range and the amount of bits needed */
169 switch (range) {
170 case ARFCN_RANGE_128:
171 return range_enc_range128(chan_list, f0, w);
172 break;
173 case ARFCN_RANGE_256:
174 return range_enc_range256(chan_list, f0, w);
175 break;
176 case ARFCN_RANGE_512:
177 return range_enc_range512(chan_list, f0, w);
178 break;
179 case ARFCN_RANGE_1024:
180 return range_enc_range1024(chan_list, f0, f0_included, w);
181 break;
182 default:
183 return -4;
184 };
185}
186
Harald Weltea43f7892009-12-01 18:04:30 +0530187/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200188static int bitvec2freq_list(uint8_t *chan_list, struct bitvec *bv,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100189 const struct gsm_bts *bts, int bis, int ter)
Harald Weltea43f7892009-12-01 18:04:30 +0530190{
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200191 int i, rc, min = -1, max = -1, pgsm = 0, arfcns = 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530192
193 memset(chan_list, 0, 16);
194
Harald Welte29350342012-02-17 15:58:23 +0100195 if (bts->band == GSM_BAND_900
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100196 && bts->c0->arfcn >= 1 && bts->c0->arfcn <= 124)
197 pgsm = 1;
198 /* P-GSM-only handsets only support 'bit map 0 format' */
199 if (!bis && !ter && pgsm) {
Harald Weltea43f7892009-12-01 18:04:30 +0530200 chan_list[0] = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100201
202 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100203 if (i >= 1 && i <= 124
204 && bitvec_get_bit_pos(bv, i)) {
Harald Welte6c40def2009-12-14 22:07:14 +0100205 rc = freq_list_bm0_set_arfcn(chan_list, i);
206 if (rc < 0)
207 return rc;
208 }
Harald Weltea43f7892009-12-01 18:04:30 +0530209 }
210 return 0;
211 }
212
Harald Welte6c40def2009-12-14 22:07:14 +0100213 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100214 /* in case of SI2 or SI5 allow all neighbours in same band
215 * in case of SI*bis, allow neighbours in same band ouside pgsm
216 * in case of SI*ter, allow neighbours in different bands
217 */
218 if (bitvec_get_bit_pos(bv, i)
219 && ((!bis && !ter && gsm_arfcn2band(i) == bts->band)
220 || (bis && pgsm && gsm_arfcn2band(i) == bts->band && (i < 1 || i > 124))
221 || (ter && gsm_arfcn2band(i) != bts->band))) {
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200222 /* count the arfcns we want to carry */
223 arfcns += 1;
224
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100225 /* 955..1023 < 0..885 */
226 if (min < 0)
Harald Welte6c40def2009-12-14 22:07:14 +0100227 min = i;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100228 if (i >= 955 && min < 955)
229 min = i;
230 if (i >= 955 && min >= 955 && i < min)
231 min = i;
232 if (i < 955 && min < 955 && i < min)
233 min = i;
234 if (max < 0)
235 max = i;
236 if (i < 955 && max >= 955)
237 max = i;
238 if (i >= 955 && max >= 955 && i > max)
239 max = i;
240 if (i < 955 && max < 955 && i > max)
Harald Welte6c40def2009-12-14 22:07:14 +0100241 max = i;
242 }
Harald Weltea43f7892009-12-01 18:04:30 +0530243 }
244
Sylvain Munaut42a56522009-12-24 14:20:19 +0100245 if (max == -1) {
246 /* Empty set, use 'bit map 0 format' */
247 chan_list[0] = 0;
248 return 0;
249 }
250
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200251 /* Now find the best encoding */
252 if (((max - min) & 1023) <= 111)
253 return enc_freq_lst_var_bitmap(chan_list, bv, bts, bis,
254 ter, min, pgsm);
Harald Weltea43f7892009-12-01 18:04:30 +0530255
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200256 /* Attempt to do the range encoding */
257 rc = enc_freq_lst_range(chan_list, bv, bts, bis, ter, pgsm);
258 if (rc == 0)
259 return 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530260
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200261 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, arfcns=%d "
262 "can not generate ARFCN list", min, max, arfcns);
263 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530264}
265
266/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Dieter Spaar16646022011-07-28 00:01:50 +0200267/* static*/ int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530268{
Harald Weltea43f7892009-12-01 18:04:30 +0530269 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100270 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530271
Harald Weltea39b0f22010-06-14 22:26:10 +0200272 /* Zero-initialize the bit-vector */
Harald Weltec8794b52010-06-16 11:09:18 +0200273 memset(bv->data, 0, bv->data_len);
Harald Weltea39b0f22010-06-14 22:26:10 +0200274
Harald Welte6c40def2009-12-14 22:07:14 +0100275 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
Harald Weltea39b0f22010-06-14 22:26:10 +0200276 llist_for_each_entry(trx, &bts->trx_list, list) {
277 unsigned int i, j;
278 /* Always add the TRX's ARFCN */
Harald Welte6c40def2009-12-14 22:07:14 +0100279 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea39b0f22010-06-14 22:26:10 +0200280 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
281 struct gsm_bts_trx_ts *ts = &trx->ts[i];
282 /* Add any ARFCNs present in hopping channels */
283 for (j = 0; j < 1024; j++) {
284 if (bitvec_get_bit_pos(&ts->hopping.arfcns, j))
285 bitvec_set_bit_pos(bv, j, 1);
286 }
287 }
288 }
Harald Weltea43f7892009-12-01 18:04:30 +0530289
Harald Welte6c40def2009-12-14 22:07:14 +0100290 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100291 return bitvec2freq_list(chan_list, bv, bts, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530292}
293
Harald Welte6c40def2009-12-14 22:07:14 +0100294/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100295static int generate_bcch_chan_list(uint8_t *chan_list, struct gsm_bts *bts,
296 int si5, int bis, int ter)
Harald Welte6c40def2009-12-14 22:07:14 +0100297{
298 struct gsm_bts *cur_bts;
Harald Welte64c07d22011-02-15 11:43:27 +0100299 struct bitvec *bv;
300
301 if (si5 && bts->neigh_list_manual_mode == NL_MODE_MANUAL_SI5SEP)
302 bv = &bts->si_common.si5_neigh_list;
303 else
304 bv = &bts->si_common.neigh_list;
Harald Welte6c40def2009-12-14 22:07:14 +0100305
Harald Welte32c09622011-01-11 23:44:56 +0100306 /* Generate list of neighbor cells if we are in automatic mode */
Harald Welte64c07d22011-02-15 11:43:27 +0100307 if (bts->neigh_list_manual_mode == NL_MODE_AUTOMATIC) {
Harald Welte21bbda22011-02-19 20:43:37 +0100308 /* Zero-initialize the bit-vector */
309 memset(bv->data, 0, bv->data_len);
310
Harald Welte32c09622011-01-11 23:44:56 +0100311 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
312 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
313 if (cur_bts == bts)
314 continue;
315 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
316 }
Harald Welte6c40def2009-12-14 22:07:14 +0100317 }
318
319 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100320 return bitvec2freq_list(chan_list, bv, bts, bis, ter);
321}
322
323static int list_arfcn(uint8_t *chan_list, uint8_t mask, char *text)
324{
325 int n = 0, i;
326 struct gsm_sysinfo_freq freq[1024];
327
328 memset(freq, 0, sizeof(freq));
329 gsm48_decode_freq_list(freq, chan_list, 16, 0xce, 1);
330 for (i = 0; i < 1024; i++) {
331 if (freq[i].mask) {
332 if (!n)
333 LOGP(DRR, LOGL_INFO, "%s", text);
334 LOGPC(DRR, LOGL_INFO, " %d", i);
335 n++;
336 }
337 }
338 if (n)
339 LOGPC(DRR, LOGL_INFO, "\n");
340
341 return n;
Harald Welte6c40def2009-12-14 22:07:14 +0100342}
343
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200344static int generate_si1(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530345{
346 int rc;
347 struct gsm48_system_information_type_1 *si1 =
348 (struct gsm48_system_information_type_1 *) output;
349
350 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
351
352 si1->header.l2_plen = (21 << 2) | 1;
353 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
354 si1->header.skip_indicator = 0;
355 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
356
357 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
358 if (rc < 0)
359 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100360 list_arfcn(si1->cell_channel_description, 0xce, "Serving cell:");
Harald Weltea43f7892009-12-01 18:04:30 +0530361
362 si1->rach_control = bts->si_common.rach_control;
363
364 /* SI1 Rest Octets (10.5.2.32), contains NCH position */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100365 rc = rest_octets_si1(si1->rest_octets, NULL);
Harald Welte7401ae62010-06-15 16:44:12 +0200366
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100367 return sizeof(*si1) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530368}
369
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200370static int generate_si2(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530371{
372 int rc;
373 struct gsm48_system_information_type_2 *si2 =
374 (struct gsm48_system_information_type_2 *) output;
375
376 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
377
378 si2->header.l2_plen = (22 << 2) | 1;
379 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
380 si2->header.skip_indicator = 0;
381 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
382
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100383 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts, 0, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530384 if (rc < 0)
385 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100386 list_arfcn(si2->bcch_frequency_list, 0xce,
387 "Neighbour cells in same band:");
Harald Weltea43f7892009-12-01 18:04:30 +0530388
389 si2->ncc_permitted = bts->si_common.ncc_permitted;
390 si2->rach_control = bts->si_common.rach_control;
391
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100392 return sizeof(*si2);
Harald Weltea43f7892009-12-01 18:04:30 +0530393}
394
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100395static int generate_si2bis(uint8_t *output, struct gsm_bts *bts)
396{
397 int rc;
398 struct gsm48_system_information_type_2bis *si2b =
399 (struct gsm48_system_information_type_2bis *) output;
400 int n;
401
402 memset(si2b, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
403
404 si2b->header.l2_plen = (22 << 2) | 1;
405 si2b->header.rr_protocol_discriminator = GSM48_PDISC_RR;
406 si2b->header.skip_indicator = 0;
407 si2b->header.system_information = GSM48_MT_RR_SYSINFO_2bis;
408
409 rc = generate_bcch_chan_list(si2b->bcch_frequency_list, bts, 0, 1, 0);
410 if (rc < 0)
411 return rc;
412 n = list_arfcn(si2b->bcch_frequency_list, 0xce,
413 "Neighbour cells in same band, but outside P-GSM:");
414 if (n) {
415 /* indicate in SI2 and SI2bis: there is an extension */
416 struct gsm48_system_information_type_2 *si2 =
417 (struct gsm48_system_information_type_2 *)
418 bts->si_buf[SYSINFO_TYPE_2];
419 si2->bcch_frequency_list[0] |= 0x20;
420 si2b->bcch_frequency_list[0] |= 0x20;
421 } else
422 bts->si_valid &= ~(1 << SYSINFO_TYPE_2bis);
423
424 si2b->rach_control = bts->si_common.rach_control;
425
426 return sizeof(*si2b);
427}
428
429static int generate_si2ter(uint8_t *output, struct gsm_bts *bts)
430{
431 int rc;
432 struct gsm48_system_information_type_2ter *si2t =
433 (struct gsm48_system_information_type_2ter *) output;
434 int n;
435
436 memset(si2t, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
437
438 si2t->header.l2_plen = (22 << 2) | 1;
439 si2t->header.rr_protocol_discriminator = GSM48_PDISC_RR;
440 si2t->header.skip_indicator = 0;
441 si2t->header.system_information = GSM48_MT_RR_SYSINFO_2ter;
442
443 rc = generate_bcch_chan_list(si2t->ext_bcch_frequency_list, bts, 0, 0, 1);
444 if (rc < 0)
445 return rc;
446 n = list_arfcn(si2t->ext_bcch_frequency_list, 0x8e,
447 "Neighbour cells in different band:");
448 if (!n)
449 bts->si_valid &= ~(1 << SYSINFO_TYPE_2ter);
450
451 return sizeof(*si2t);
452}
453
Harald Welte1f893292010-03-14 23:46:48 +0800454static struct gsm48_si_ro_info si_info = {
Harald Weltea43f7892009-12-01 18:04:30 +0530455 .selection_params = {
456 .present = 0,
457 },
458 .power_offset = {
459 .present = 0,
460 },
461 .si2ter_indicator = 0,
462 .early_cm_ctrl = 1,
463 .scheduling = {
464 .present = 0,
465 },
466 .gprs_ind = {
467 .si13_position = 0,
468 .ra_colour = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800469 .present = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530470 },
471 .lsa_params = {
472 .present = 0,
473 },
474 .cell_id = 0, /* FIXME: doesn't the bts have this? */
475 .break_ind = 0,
476};
477
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200478static int generate_si3(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530479{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100480 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530481 struct gsm48_system_information_type_3 *si3 =
482 (struct gsm48_system_information_type_3 *) output;
483
484 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
485
486 si3->header.l2_plen = (18 << 2) | 1;
487 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
488 si3->header.skip_indicator = 0;
489 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
490
491 si3->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100492 gsm48_generate_lai(&si3->lai, bts->network->country_code,
493 bts->network->network_code,
494 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530495 si3->control_channel_desc = bts->si_common.chan_desc;
496 si3->cell_options = bts->si_common.cell_options;
497 si3->cell_sel_par = bts->si_common.cell_sel_par;
498 si3->rach_control = bts->si_common.rach_control;
499
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100500 if ((bts->si_valid & (1 << SYSINFO_TYPE_2ter))) {
501 LOGP(DRR, LOGL_INFO, "SI 2ter is included.\n");
502 si_info.si2ter_indicator = 1;
503 } else {
504 si_info.si2ter_indicator = 0;
505 }
506
Harald Weltea43f7892009-12-01 18:04:30 +0530507 /* SI3 Rest Octets (10.5.2.34), containing
508 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
509 Power Offset, 2ter Indicator, Early Classmark Sending,
510 Scheduling if and WHERE, GPRS Indicator, SI13 position */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100511 rc = rest_octets_si3(si3->rest_octets, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530512
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100513 return sizeof(*si3) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530514}
515
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200516static int generate_si4(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530517{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100518 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530519 struct gsm48_system_information_type_4 *si4 =
520 (struct gsm48_system_information_type_4 *) output;
521
522 /* length of all IEs present except SI4 rest octets and l2_plen */
523 int l2_plen = sizeof(*si4) - 1;
524
525 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
526
527 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
528 si4->header.skip_indicator = 0;
529 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
530
Harald Welteafedeab2010-03-04 10:55:40 +0100531 gsm48_generate_lai(&si4->lai, bts->network->country_code,
532 bts->network->network_code,
533 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530534 si4->cell_sel_par = bts->si_common.cell_sel_par;
535 si4->rach_control = bts->si_common.rach_control;
536
537 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
538
539 si4->header.l2_plen = (l2_plen << 2) | 1;
540
541 /* SI4 Rest Octets (10.5.2.35), containing
542 Optional Power offset, GPRS Indicator,
543 Cell Identity, LSA ID, Selection Parameter */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100544 rc = rest_octets_si4(si4->data, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530545
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100546 return sizeof(*si4) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530547}
548
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200549static int generate_si5(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530550{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100551 struct gsm48_system_information_type_5 *si5;
552 int rc, l2_plen = 18;
Harald Weltea43f7892009-12-01 18:04:30 +0530553
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100554 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
555
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100556 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100557 switch (bts->type) {
558 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200559 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Weltefd355a32011-03-04 13:41:31 +0100560 case GSM_BTS_TYPE_HSL_FEMTO:
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100561 *output++ = (l2_plen << 2) | 1;
562 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100563 break;
564 default:
565 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100566 }
567
568 si5 = (struct gsm48_system_information_type_5 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530569
570 /* l2 pseudo length, not part of msg: 18 */
571 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
572 si5->skip_indicator = 0;
573 si5->system_information = GSM48_MT_RR_SYSINFO_5;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100574 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts, 1, 0, 0);
Harald Weltea43f7892009-12-01 18:04:30 +0530575 if (rc < 0)
576 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100577 list_arfcn(si5->bcch_frequency_list, 0xce,
578 "Neighbour cells in same band:");
579
580 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
581 return l2_plen;
582}
583
584static int generate_si5bis(uint8_t *output, struct gsm_bts *bts)
585{
586 struct gsm48_system_information_type_5bis *si5b;
587 int rc, l2_plen = 18;
588 int n;
589
590 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
591
592 /* ip.access nanoBTS needs l2_plen!! */
593 switch (bts->type) {
594 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200595 case GSM_BTS_TYPE_OSMO_SYSMO:
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100596 case GSM_BTS_TYPE_HSL_FEMTO:
597 *output++ = (l2_plen << 2) | 1;
598 l2_plen++;
599 break;
600 default:
601 break;
602 }
603
604 si5b = (struct gsm48_system_information_type_5bis *) output;
605
606 /* l2 pseudo length, not part of msg: 18 */
607 si5b->rr_protocol_discriminator = GSM48_PDISC_RR;
608 si5b->skip_indicator = 0;
609 si5b->system_information = GSM48_MT_RR_SYSINFO_5bis;
610 rc = generate_bcch_chan_list(si5b->bcch_frequency_list, bts, 1, 1, 0);
611 if (rc < 0)
612 return rc;
613 n = list_arfcn(si5b->bcch_frequency_list, 0xce,
614 "Neighbour cells in same band, but outside P-GSM:");
615 if (n) {
616 /* indicate in SI5 and SI5bis: there is an extension */
617 struct gsm48_system_information_type_5 *si5 =
618 (struct gsm48_system_information_type_5 *)
619 bts->si_buf[SYSINFO_TYPE_5];
620 si5->bcch_frequency_list[0] |= 0x20;
621 si5b->bcch_frequency_list[0] |= 0x20;
622 } else
623 bts->si_valid &= ~(1 << SYSINFO_TYPE_5bis);
624
625 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
626 return l2_plen;
627}
628
629static int generate_si5ter(uint8_t *output, struct gsm_bts *bts)
630{
631 struct gsm48_system_information_type_5ter *si5t;
632 int rc, l2_plen = 18;
633 int n;
634
635 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
636
637 /* ip.access nanoBTS needs l2_plen!! */
638 switch (bts->type) {
639 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200640 case GSM_BTS_TYPE_OSMO_SYSMO:
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100641 case GSM_BTS_TYPE_HSL_FEMTO:
642 *output++ = (l2_plen << 2) | 1;
643 l2_plen++;
644 break;
645 default:
646 break;
647 }
648
649 si5t = (struct gsm48_system_information_type_5ter *) output;
650
651 /* l2 pseudo length, not part of msg: 18 */
652 si5t->rr_protocol_discriminator = GSM48_PDISC_RR;
653 si5t->skip_indicator = 0;
654 si5t->system_information = GSM48_MT_RR_SYSINFO_5ter;
655 rc = generate_bcch_chan_list(si5t->bcch_frequency_list, bts, 1, 0, 1);
656 if (rc < 0)
657 return rc;
658 n = list_arfcn(si5t->bcch_frequency_list, 0x8e,
659 "Neighbour cells in different band:");
660 if (!n)
661 bts->si_valid &= ~(1 << SYSINFO_TYPE_5ter);
Harald Weltea43f7892009-12-01 18:04:30 +0530662
663 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100664 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530665}
666
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200667static int generate_si6(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530668{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100669 struct gsm48_system_information_type_6 *si6;
670 int l2_plen = 11;
Harald Weltea43f7892009-12-01 18:04:30 +0530671
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100672 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
673
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100674 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100675 switch (bts->type) {
676 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200677 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Weltefd355a32011-03-04 13:41:31 +0100678 case GSM_BTS_TYPE_HSL_FEMTO:
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100679 *output++ = (l2_plen << 2) | 1;
680 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100681 break;
682 default:
683 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100684 }
685
686 si6 = (struct gsm48_system_information_type_6 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530687
688 /* l2 pseudo length, not part of msg: 11 */
689 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
690 si6->skip_indicator = 0;
691 si6->system_information = GSM48_MT_RR_SYSINFO_6;
692 si6->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100693 gsm48_generate_lai(&si6->lai, bts->network->country_code,
694 bts->network->network_code,
695 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530696 si6->cell_options = bts->si_common.cell_options;
697 si6->ncc_permitted = bts->si_common.ncc_permitted;
698
699 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
700
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100701 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530702}
703
704static struct gsm48_si13_info si13_default = {
705 .cell_opts = {
Harald Welte85849182010-06-02 12:19:21 +0200706 .nmo = GPRS_NMO_II,
Harald Weltea4b16652010-05-31 12:54:23 +0200707 .t3168 = 2000,
Andreas Eversbergbecc89a2012-09-23 08:14:51 +0200708 .t3192 = 1500,
Harald Welte97a282b2010-03-14 15:37:43 +0800709 .drx_timer_max = 3,
Harald Weltea43f7892009-12-01 18:04:30 +0530710 .bs_cv_max = 15,
Sylvain Munaut851f1202011-10-17 13:56:02 +0200711 .ext_info_present = 0,
Harald Welteda0586a2010-04-18 14:49:05 +0200712 .ext_info = {
713 /* The values below are just guesses ! */
Harald Weltea06fea02010-04-18 15:53:40 +0200714 .egprs_supported = 0,
Harald Welteda0586a2010-04-18 14:49:05 +0200715 .use_egprs_p_ch_req = 1,
Harald Weltea4b16652010-05-31 12:54:23 +0200716 .bep_period = 5,
Harald Welteda0586a2010-04-18 14:49:05 +0200717 .pfc_supported = 0,
718 .dtm_supported = 0,
719 .bss_paging_coordination = 0,
720 },
Harald Weltea43f7892009-12-01 18:04:30 +0530721 },
722 .pwr_ctrl_pars = {
Harald Weltec15d0ac2012-10-08 21:22:08 +0200723 .alpha = 0, /* a = 0.0 */
Harald Weltea4b16652010-05-31 12:54:23 +0200724 .t_avg_w = 16,
725 .t_avg_t = 16,
Harald Weltea43f7892009-12-01 18:04:30 +0530726 .pc_meas_chan = 0, /* downling measured on CCCH */
Harald Weltea4b16652010-05-31 12:54:23 +0200727 .n_avg_i = 8,
Harald Weltea43f7892009-12-01 18:04:30 +0530728 },
Harald Welte97a282b2010-03-14 15:37:43 +0800729 .bcch_change_mark = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530730 .si_change_field = 0,
731 .pbcch_present = 0,
732 {
733 .no_pbcch = {
Harald Welte97a282b2010-03-14 15:37:43 +0800734 .rac = 0, /* needs to be patched */
Harald Weltea43f7892009-12-01 18:04:30 +0530735 .spgc_ccch_sup = 0,
736 .net_ctrl_ord = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800737 .prio_acc_thr = 6,
Harald Weltea43f7892009-12-01 18:04:30 +0530738 },
739 },
740};
741
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200742static int generate_si13(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530743{
744 struct gsm48_system_information_type_13 *si13 =
745 (struct gsm48_system_information_type_13 *) output;
746 int ret;
747
748 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
749
750 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
751 si13->header.skip_indicator = 0;
752 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
753
Harald Welte97a282b2010-03-14 15:37:43 +0800754 si13_default.no_pbcch.rac = bts->gprs.rac;
755
Harald Weltea43f7892009-12-01 18:04:30 +0530756 ret = rest_octets_si13(si13->rest_octets, &si13_default);
757 if (ret < 0)
758 return ret;
759
Holger Hans Peter Freyther1c6f3942010-06-16 12:05:56 +0800760 /* length is coded in bit 2 an up */
761 si13->header.l2_plen = 0x01;
Harald Weltea43f7892009-12-01 18:04:30 +0530762
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100763 return sizeof (*si13) + ret;
Harald Weltea43f7892009-12-01 18:04:30 +0530764}
765
Harald Welte7401ae62010-06-15 16:44:12 +0200766typedef int (*gen_si_fn_t)(uint8_t *output, struct gsm_bts *bts);
767
768static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
769 [SYSINFO_TYPE_1] = &generate_si1,
770 [SYSINFO_TYPE_2] = &generate_si2,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100771 [SYSINFO_TYPE_2bis] = &generate_si2bis,
772 [SYSINFO_TYPE_2ter] = &generate_si2ter,
Harald Welte7401ae62010-06-15 16:44:12 +0200773 [SYSINFO_TYPE_3] = &generate_si3,
774 [SYSINFO_TYPE_4] = &generate_si4,
775 [SYSINFO_TYPE_5] = &generate_si5,
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100776 [SYSINFO_TYPE_5bis] = &generate_si5bis,
777 [SYSINFO_TYPE_5ter] = &generate_si5ter,
Harald Welte7401ae62010-06-15 16:44:12 +0200778 [SYSINFO_TYPE_6] = &generate_si6,
779 [SYSINFO_TYPE_13] = &generate_si13,
780};
781
Harald Welte7401ae62010-06-15 16:44:12 +0200782int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
783{
784 gen_si_fn_t gen_si;
785
Harald Welte4511d892010-04-18 15:51:20 +0200786 switch (bts->gprs.mode) {
787 case BTS_GPRS_EGPRS:
Harald Weltea06fea02010-04-18 15:53:40 +0200788 si13_default.cell_opts.ext_info_present = 1;
789 si13_default.cell_opts.ext_info.egprs_supported = 1;
790 /* fallthrough */
Harald Welte4511d892010-04-18 15:51:20 +0200791 case BTS_GPRS_GPRS:
792 si_info.gprs_ind.present = 1;
793 break;
794 case BTS_GPRS_NONE:
795 si_info.gprs_ind.present = 0;
796 break;
797 }
Harald Welte1f893292010-03-14 23:46:48 +0800798
Sylvain Munaute0b06b02010-11-28 18:17:28 +0100799 memcpy(&si_info.selection_params,
800 &bts->si_common.cell_ro_sel_par,
801 sizeof(struct gsm48_si_selection_params));
802
Harald Welte7401ae62010-06-15 16:44:12 +0200803 gen_si = gen_si_fn[si_type];
804 if (!gen_si)
Harald Weltea43f7892009-12-01 18:04:30 +0530805 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530806
Harald Welte7401ae62010-06-15 16:44:12 +0200807 return gen_si(bts->si_buf[si_type], bts);
Harald Weltea43f7892009-12-01 18:04:30 +0530808}