blob: 8952534c1961acf1a452d57b3b78b9898447ad59 [file] [log] [blame]
Harald Weltea54a2bb2009-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 Welte99bed522010-06-15 16:44:12 +02004/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +02005 * (C) 2012 Holger Hans Peter Freyther
Harald Weltea54a2bb2009-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 Welte0e3e88e2011-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 Weltea54a2bb2009-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 Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Weltea54a2bb2009-12-01 18:04:30 +053018 *
Harald Welte0e3e88e2011-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 Weltea54a2bb2009-12-01 18:04:30 +053021 *
22 */
23
24#include <errno.h>
25#include <string.h>
Harald Welte6ae64672009-12-14 22:07:14 +010026#include <stdio.h>
Harald Weltea54a2bb2009-12-01 18:04:30 +053027#include <netinet/in.h>
Max754629f2016-04-15 16:04:45 +020028#include <stdbool.h>
Harald Weltea54a2bb2009-12-01 18:04:30 +053029
Harald Welte6b77f0f2011-05-24 15:02:20 +020030#include <osmocom/core/bitvec.h>
31#include <osmocom/core/utils.h>
32#include <osmocom/gsm/sysinfo.h>
33
34#include <openbsc/debug.h>
Harald Weltea54a2bb2009-12-01 18:04:30 +053035#include <openbsc/gsm_04_08.h>
36#include <openbsc/gsm_data.h>
37#include <openbsc/abis_rsl.h>
38#include <openbsc/rest_octets.h>
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +020039#include <openbsc/arfcn_range_encode.h>
Harald Weltea54a2bb2009-12-01 18:04:30 +053040
Holger Hans Peter Freyther59e7f2b2013-01-17 13:49:39 +010041/*
42 * DCS1800 and PCS1900 have overlapping ARFCNs. We would need to set the
43 * ARFCN_PCS flag on the 1900 ARFCNs but this would increase cell_alloc
44 * and other arrays to make sure (ARFCN_PCS + 1024)/8 ARFCNs fit into the
45 * array. DCS1800 and PCS1900 can not be used at the same time so conserve
46 * memory and do the below.
47 */
48static int band_compatible(const struct gsm_bts *bts, int arfcn)
49{
50 enum gsm_band band = gsm_arfcn2band(arfcn);
51
52 /* normal case */
53 if (band == bts->band)
54 return 1;
55 /* deal with ARFCN_PCS not set */
56 if (band == GSM_BAND_1800 && bts->band == GSM_BAND_1900)
57 return 1;
58
59 return 0;
60}
Holger Hans Peter Freyther8526b152010-01-06 06:38:14 +010061
Holger Hans Peter Freytherfb8f0b52013-03-06 12:02:33 +010062static int is_dcs_net(const struct gsm_bts *bts)
63{
64 if (bts->band == GSM_BAND_850)
65 return 0;
66 if (bts->band == GSM_BAND_1900)
67 return 0;
68 return 1;
69}
70
Maxeaf196c2016-04-20 15:57:13 +020071/* Return q(m) for given NR_OF_TDD_CELLS - see Table 9.1.54.1a, 3GPP TS 44.018 */
72unsigned range1024_p(unsigned n)
73{
74 switch (n) {
75 case 0: return 0;
76 case 1: return 10;
77 case 2: return 19;
78 case 3: return 28;
79 case 4: return 36;
80 case 5: return 44;
81 case 6: return 52;
82 case 7: return 60;
83 case 8: return 67;
84 case 9: return 74;
85 case 10: return 81;
86 case 11: return 88;
87 case 12: return 95;
88 case 13: return 102;
89 case 14: return 109;
90 case 15: return 116;
91 case 16: return 122;
92 default: return 0;
93 }
94}
95
96/* Return q(m) for given NR_OF_TDD_CELLS - see Table 9.1.54.1b, 3GPP TS 44.018 */
97unsigned range512_q(unsigned m)
98{
99 switch (m) {
100 case 0: return 0;
101 case 1: return 9;
102 case 2: return 17;
103 case 3: return 25;
104 case 4: return 32;
105 case 5: return 39;
106 case 6: return 46;
107 case 7: return 53;
108 case 8: return 59;
109 case 9: return 65;
110 case 10: return 71;
111 case 11: return 77;
112 case 12: return 83;
113 case 13: return 89;
114 case 14: return 95;
115 case 15: return 101;
116 case 16: return 106;
117 case 17: return 111;
118 case 18: return 116;
119 case 19: return 121;
120 case 20: return 126;
121 default: return 0;
122 }
123}
124
125unsigned earfcn_size(const struct osmo_earfcn_si2q *e)
126{
127 /* account for all the constant bits in append_earfcn() */
128 return 25 + osmo_earfcn_bit_size(e);
129}
130
131unsigned uarfcn_size(const uint16_t *u, const uint16_t *sc, size_t u_len)
132{
133 /*account for all the constant bits in append_uarfcn() */
134 return 29 + range1024_p(u_len);
135}
136
137/* 3GPP TS 44.018, Table 9.1.54.1 - prepend diversity bit to scrambling code */
138uint16_t encode_fdd(uint16_t scramble, bool diversity)
139{
140 if (diversity)
141 return scramble | (1 << 9);
142 return scramble;
143}
144
145int bts_uarfcn_del(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble)
146{
147 uint16_t sc0 = encode_fdd(scramble, false), sc1 = encode_fdd(scramble, true),
148 *ual = bts->si_common.data.uarfcn_list,
149 *scl = bts->si_common.data.scramble_list;
150 size_t len = bts->si_common.uarfcn_length, i;
151 for (i = 0; i < len; i++) {
152 if (arfcn == ual[i] && (sc0 == scl[i] || sc1 == scl[i])) {
153 /* we rely on the assumption that (uarfcn, scramble)
154 tuple is unique in the lists */
155 if (i != len - 1) { /* move the tail if necessary */
156 memmove(ual + i, ual + i + 1, 2 * (len - i + 1));
157 memmove(scl + i, scl + i + 1, 2 * (len - i + 1));
158 }
159 break;
160 }
161 }
162
163 if (i == len)
164 return -EINVAL;
165
166 bts->si_common.uarfcn_length--;
167 return 0;
168}
169
170int bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble,
171 bool diversity)
172{
173 size_t len = bts->si_common.uarfcn_length, i, k;
174 uint16_t scr, chk,
175 *ual = bts->si_common.data.uarfcn_list,
176 *scl = bts->si_common.data.scramble_list,
177 scramble1 = encode_fdd(scramble, true),
178 scramble0 = encode_fdd(scramble, false);
179
180 scr = diversity ? scramble1 : scramble0;
181 chk = diversity ? scramble0 : scramble1;
182
183 if (len == MAX_EARFCN_LIST)
184 return -ENOMEM;
185
186 for (i = 0, k = 0; i < len; i++) {
187 if (arfcn == ual[i] && (scr == scl[i] || chk == scl[i]))
188 return -EADDRINUSE;
189 if (scr > scl[i])
190 k = i + 1;
191 }
192 /* we keep lists sorted by scramble code:
193 insert into appropriate position and move the tail */
194 if (len - k) {
195 memmove(ual + k + 1, ual + k, (len - k) * 2);
196 memmove(scl + k + 1, scl + k, (len - k) * 2);
197 }
198 ual[k] = arfcn;
199 scl[k] = scr;
200 bts->si_common.uarfcn_length++;
201 return 0;
202}
203
Max754629f2016-04-15 16:04:45 +0200204static inline int use_arfcn(const struct gsm_bts *bts, const bool bis, const bool ter,
205 const bool pgsm, const int arfcn)
Holger Hans Peter Freyther2e778c72013-01-17 12:04:17 +0100206{
Jacob Erlbeck8f8e5bf2014-01-16 11:02:14 +0100207 if (bts->force_combined_si)
208 return !bis && !ter;
Holger Hans Peter Freyther59e7f2b2013-01-17 13:49:39 +0100209 if (!bis && !ter && band_compatible(bts, arfcn))
Holger Hans Peter Freyther2e778c72013-01-17 12:04:17 +0100210 return 1;
Jacob Erlbeck8f8e5bf2014-01-16 11:02:14 +0100211 /* Correct but somehow broken with either the nanoBTS or the iPhone5 */
Holger Hans Peter Freyther59e7f2b2013-01-17 13:49:39 +0100212 if (bis && pgsm && band_compatible(bts, arfcn) && (arfcn < 1 || arfcn > 124))
Holger Hans Peter Freyther2e778c72013-01-17 12:04:17 +0100213 return 1;
Holger Hans Peter Freyther59e7f2b2013-01-17 13:49:39 +0100214 if (ter && !band_compatible(bts, arfcn))
Holger Hans Peter Freyther2e778c72013-01-17 12:04:17 +0100215 return 1;
216 return 0;
217}
218
Harald Weltebc479662009-12-14 20:25:05 +0100219/* Frequency Lists as per TS 04.08 10.5.2.13 */
220
221/* 10.5.2.13.2: Bit map 0 format */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200222static int freq_list_bm0_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530223{
224 unsigned int byte, bit;
225
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100226 if (arfcn > 124 || arfcn < 1) {
227 LOGP(DRR, LOGL_ERROR, "Bitmap 0 only supports ARFCN 1...124\n");
Harald Weltea54a2bb2009-12-01 18:04:30 +0530228 return -EINVAL;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100229 }
Harald Weltea54a2bb2009-12-01 18:04:30 +0530230
Harald Weltebc479662009-12-14 20:25:05 +0100231 /* the bitmask is from 1..124, not from 0..123 */
232 arfcn--;
233
Harald Weltea54a2bb2009-12-01 18:04:30 +0530234 byte = arfcn / 8;
235 bit = arfcn % 8;
236
Harald Weltebc479662009-12-14 20:25:05 +0100237 chan_list[GSM48_CELL_CHAN_DESC_SIZE-1-byte] |= (1 << bit);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530238
239 return 0;
240}
241
Harald Weltebc479662009-12-14 20:25:05 +0100242/* 10.5.2.13.7: Variable bit map format */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200243static int freq_list_bmrel_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530244{
245 unsigned int byte, bit;
246 unsigned int min_arfcn;
247 unsigned int bitno;
248
249 min_arfcn = (chan_list[0] & 1) << 9;
250 min_arfcn |= chan_list[1] << 1;
251 min_arfcn |= (chan_list[2] >> 7) & 1;
252
253 /* The lower end of our bitmaks is always implicitly included */
254 if (arfcn == min_arfcn)
255 return 0;
256
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100257 if (((arfcn - min_arfcn) & 1023) > 111) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100258 LOGP(DRR, LOGL_ERROR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530259 return -EINVAL;
Harald Weltee285bad2009-12-16 11:57:48 +0100260 }
Harald Weltea54a2bb2009-12-01 18:04:30 +0530261
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100262 bitno = (arfcn - min_arfcn) & 1023;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530263 byte = bitno / 8;
264 bit = bitno % 8;
265
266 chan_list[2 + byte] |= 1 << (7 - bit);
267
268 return 0;
269}
270
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200271/* generate a variable bitmap */
Max754629f2016-04-15 16:04:45 +0200272static inline int enc_freq_lst_var_bitmap(uint8_t *chan_list,
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200273 struct bitvec *bv, const struct gsm_bts *bts,
Max754629f2016-04-15 16:04:45 +0200274 bool bis, bool ter, int min, bool pgsm)
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200275{
276 int i;
277
278 /* set it to 'Variable bitmap format' */
279 chan_list[0] = 0x8e;
280
281 chan_list[0] |= (min >> 9) & 1;
282 chan_list[1] = (min >> 1);
283 chan_list[2] = (min & 1) << 7;
284
285 for (i = 0; i < bv->data_len*8; i++) {
286 /* see notes in bitvec2freq_list */
287 if (bitvec_get_bit_pos(bv, i)
Harald Welte92e10262015-09-04 06:21:32 +0200288 && ((!bis && !ter && band_compatible(bts,i))
289 || (bis && pgsm && band_compatible(bts,i) && (i < 1 || i > 124))
290 || (ter && !band_compatible(bts, i)))) {
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200291 int rc = freq_list_bmrel_set_arfcn(chan_list, i);
292 if (rc < 0)
293 return rc;
294 }
295 }
296
297 return 0;
298}
299
300/* generate a frequency list with the range 512 format */
Max754629f2016-04-15 16:04:45 +0200301static inline int enc_freq_lst_range(uint8_t *chan_list,
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200302 struct bitvec *bv, const struct gsm_bts *bts,
Max754629f2016-04-15 16:04:45 +0200303 bool bis, bool ter, bool pgsm)
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200304{
305 int arfcns[RANGE_ENC_MAX_ARFCNS];
306 int w[RANGE_ENC_MAX_ARFCNS];
307 int f0_included = 0;
308 int arfcns_used = 0;
309 int i, rc, range, f0;
310
311 /*
312 * Select ARFCNs according to the rules in bitvec2freq_list
313 */
314 for (i = 0; i < bv->data_len * 8; ++i) {
315 /* More ARFCNs than the maximum */
316 if (arfcns_used > ARRAY_SIZE(arfcns))
317 return -1;
318 /* Check if we can select it? */
Holger Hans Peter Freyther2e778c72013-01-17 12:04:17 +0100319 if (bitvec_get_bit_pos(bv, i) && use_arfcn(bts, bis, ter, pgsm, i))
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200320 arfcns[arfcns_used++] = i;
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200321 }
322
323 /*
324 * Check if the given list of ARFCNs can be encoded.
325 */
326 range = range_enc_determine_range(arfcns, arfcns_used, &f0);
327 if (range == ARFCN_RANGE_INVALID)
328 return -2;
329
330 /*
331 * Manipulate the ARFCN list according to the rules in J4 depending
332 * on the selected range.
333 */
Jacob Erlbeck7be3fea2014-01-14 10:42:58 +0100334 arfcns_used = range_enc_filter_arfcns(arfcns, arfcns_used,
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200335 f0, &f0_included);
336
337 memset(w, 0, sizeof(w));
338 rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
339 if (rc != 0)
340 return -3;
341
342 /* Select the range and the amount of bits needed */
343 switch (range) {
344 case ARFCN_RANGE_128:
345 return range_enc_range128(chan_list, f0, w);
346 break;
347 case ARFCN_RANGE_256:
348 return range_enc_range256(chan_list, f0, w);
349 break;
350 case ARFCN_RANGE_512:
351 return range_enc_range512(chan_list, f0, w);
352 break;
353 case ARFCN_RANGE_1024:
354 return range_enc_range1024(chan_list, f0, f0_included, w);
355 break;
356 default:
357 return -4;
358 };
359}
360
Harald Weltea54a2bb2009-12-01 18:04:30 +0530361/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200362static int bitvec2freq_list(uint8_t *chan_list, struct bitvec *bv,
Max754629f2016-04-15 16:04:45 +0200363 const struct gsm_bts *bts, bool bis, bool ter)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530364{
Max754629f2016-04-15 16:04:45 +0200365 int i, rc, min = -1, max = -1, arfcns = 0;
366 bool pgsm = false;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530367 memset(chan_list, 0, 16);
368
Harald Weltea680b132012-02-17 15:58:23 +0100369 if (bts->band == GSM_BAND_900
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100370 && bts->c0->arfcn >= 1 && bts->c0->arfcn <= 124)
Max754629f2016-04-15 16:04:45 +0200371 pgsm = true;
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100372 /* P-GSM-only handsets only support 'bit map 0 format' */
373 if (!bis && !ter && pgsm) {
Harald Weltea54a2bb2009-12-01 18:04:30 +0530374 chan_list[0] = 0;
Harald Welte6ae64672009-12-14 22:07:14 +0100375
376 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100377 if (i >= 1 && i <= 124
378 && bitvec_get_bit_pos(bv, i)) {
Harald Welte6ae64672009-12-14 22:07:14 +0100379 rc = freq_list_bm0_set_arfcn(chan_list, i);
380 if (rc < 0)
381 return rc;
382 }
Harald Weltea54a2bb2009-12-01 18:04:30 +0530383 }
384 return 0;
385 }
386
Harald Welte6ae64672009-12-14 22:07:14 +0100387 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100388 /* in case of SI2 or SI5 allow all neighbours in same band
389 * in case of SI*bis, allow neighbours in same band ouside pgsm
390 * in case of SI*ter, allow neighbours in different bands
391 */
Holger Hans Peter Freyther2e778c72013-01-17 12:04:17 +0100392 if (!bitvec_get_bit_pos(bv, i))
393 continue;
394 if (!use_arfcn(bts, bis, ter, pgsm, i))
395 continue;
396 /* count the arfcns we want to carry */
397 arfcns += 1;
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200398
Holger Hans Peter Freyther2e778c72013-01-17 12:04:17 +0100399 /* 955..1023 < 0..885 */
400 if (min < 0)
401 min = i;
402 if (i >= 955 && min < 955)
403 min = i;
404 if (i >= 955 && min >= 955 && i < min)
405 min = i;
406 if (i < 955 && min < 955 && i < min)
407 min = i;
408 if (max < 0)
409 max = i;
410 if (i < 955 && max >= 955)
411 max = i;
412 if (i >= 955 && max >= 955 && i > max)
413 max = i;
414 if (i < 955 && max < 955 && i > max)
415 max = i;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530416 }
417
Sylvain Munaut40f84a92009-12-24 14:20:19 +0100418 if (max == -1) {
419 /* Empty set, use 'bit map 0 format' */
420 chan_list[0] = 0;
421 return 0;
422 }
423
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200424 /* Now find the best encoding */
425 if (((max - min) & 1023) <= 111)
426 return enc_freq_lst_var_bitmap(chan_list, bv, bts, bis,
427 ter, min, pgsm);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530428
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200429 /* Attempt to do the range encoding */
430 rc = enc_freq_lst_range(chan_list, bv, bts, bis, ter, pgsm);
431 if (rc == 0)
432 return 0;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530433
Holger Hans Peter Freythera9e7de62012-10-13 12:38:54 +0200434 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, arfcns=%d "
435 "can not generate ARFCN list", min, max, arfcns);
436 return -EINVAL;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530437}
438
439/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Dieter Spaar49c843e2011-07-28 00:01:50 +0200440/* static*/ int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530441{
Harald Weltea54a2bb2009-12-01 18:04:30 +0530442 struct gsm_bts_trx *trx;
Harald Welte6ae64672009-12-14 22:07:14 +0100443 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530444
Harald Weltea42a93f2010-06-14 22:26:10 +0200445 /* Zero-initialize the bit-vector */
Harald Welte439e4622010-06-16 11:09:18 +0200446 memset(bv->data, 0, bv->data_len);
Harald Weltea42a93f2010-06-14 22:26:10 +0200447
Harald Welte6ae64672009-12-14 22:07:14 +0100448 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
Harald Weltea42a93f2010-06-14 22:26:10 +0200449 llist_for_each_entry(trx, &bts->trx_list, list) {
450 unsigned int i, j;
451 /* Always add the TRX's ARFCN */
Harald Welte6ae64672009-12-14 22:07:14 +0100452 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea42a93f2010-06-14 22:26:10 +0200453 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
454 struct gsm_bts_trx_ts *ts = &trx->ts[i];
455 /* Add any ARFCNs present in hopping channels */
456 for (j = 0; j < 1024; j++) {
457 if (bitvec_get_bit_pos(&ts->hopping.arfcns, j))
458 bitvec_set_bit_pos(bv, j, 1);
459 }
460 }
461 }
Harald Weltea54a2bb2009-12-01 18:04:30 +0530462
Harald Welte6ae64672009-12-14 22:07:14 +0100463 /* then we generate a GSM 04.08 frequency list from the bitvec */
Max754629f2016-04-15 16:04:45 +0200464 return bitvec2freq_list(chan_list, bv, bts, false, false);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530465}
466
Harald Welte6ae64672009-12-14 22:07:14 +0100467/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100468static int generate_bcch_chan_list(uint8_t *chan_list, struct gsm_bts *bts,
Max754629f2016-04-15 16:04:45 +0200469 bool si5, bool bis, bool ter)
Harald Welte6ae64672009-12-14 22:07:14 +0100470{
471 struct gsm_bts *cur_bts;
Harald Weltef989b782011-02-15 11:43:27 +0100472 struct bitvec *bv;
473
474 if (si5 && bts->neigh_list_manual_mode == NL_MODE_MANUAL_SI5SEP)
475 bv = &bts->si_common.si5_neigh_list;
476 else
477 bv = &bts->si_common.neigh_list;
Harald Welte6ae64672009-12-14 22:07:14 +0100478
Harald Welteca46eff2011-01-11 23:44:56 +0100479 /* Generate list of neighbor cells if we are in automatic mode */
Harald Weltef989b782011-02-15 11:43:27 +0100480 if (bts->neigh_list_manual_mode == NL_MODE_AUTOMATIC) {
Harald Weltea72d91c2011-02-19 20:43:37 +0100481 /* Zero-initialize the bit-vector */
482 memset(bv->data, 0, bv->data_len);
483
Harald Welteca46eff2011-01-11 23:44:56 +0100484 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
485 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
486 if (cur_bts == bts)
487 continue;
488 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
489 }
Harald Welte6ae64672009-12-14 22:07:14 +0100490 }
491
492 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100493 return bitvec2freq_list(chan_list, bv, bts, bis, ter);
494}
495
496static int list_arfcn(uint8_t *chan_list, uint8_t mask, char *text)
497{
498 int n = 0, i;
499 struct gsm_sysinfo_freq freq[1024];
500
501 memset(freq, 0, sizeof(freq));
502 gsm48_decode_freq_list(freq, chan_list, 16, 0xce, 1);
503 for (i = 0; i < 1024; i++) {
504 if (freq[i].mask) {
505 if (!n)
506 LOGP(DRR, LOGL_INFO, "%s", text);
507 LOGPC(DRR, LOGL_INFO, " %d", i);
508 n++;
509 }
510 }
511 if (n)
512 LOGPC(DRR, LOGL_INFO, "\n");
513
514 return n;
Harald Welte6ae64672009-12-14 22:07:14 +0100515}
516
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200517static int generate_si1(uint8_t *output, struct gsm_bts *bts)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530518{
519 int rc;
520 struct gsm48_system_information_type_1 *si1 =
521 (struct gsm48_system_information_type_1 *) output;
522
523 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
524
525 si1->header.l2_plen = (21 << 2) | 1;
526 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
527 si1->header.skip_indicator = 0;
528 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
529
530 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
531 if (rc < 0)
532 return rc;
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100533 list_arfcn(si1->cell_channel_description, 0xce, "Serving cell:");
Harald Weltea54a2bb2009-12-01 18:04:30 +0530534
535 si1->rach_control = bts->si_common.rach_control;
536
Holger Hans Peter Freytherfb8f0b52013-03-06 12:02:33 +0100537 /*
538 * SI1 Rest Octets (10.5.2.32), contains NCH position and band
539 * indicator but that is not in the 04.08.
540 */
541 rc = rest_octets_si1(si1->rest_octets, NULL, is_dcs_net(bts));
Harald Welte99bed522010-06-15 16:44:12 +0200542
Holger Hans Peter Freyther02c608c2010-01-06 07:52:31 +0100543 return sizeof(*si1) + rc;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530544}
545
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200546static int generate_si2(uint8_t *output, struct gsm_bts *bts)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530547{
548 int rc;
549 struct gsm48_system_information_type_2 *si2 =
550 (struct gsm48_system_information_type_2 *) output;
551
552 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
553
554 si2->header.l2_plen = (22 << 2) | 1;
555 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
556 si2->header.skip_indicator = 0;
557 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
558
Max754629f2016-04-15 16:04:45 +0200559 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts, false, false, false);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530560 if (rc < 0)
561 return rc;
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100562 list_arfcn(si2->bcch_frequency_list, 0xce,
Harald Welte13428942015-09-04 06:22:46 +0200563 "SI2 Neighbour cells in same band:");
Harald Weltea54a2bb2009-12-01 18:04:30 +0530564
565 si2->ncc_permitted = bts->si_common.ncc_permitted;
566 si2->rach_control = bts->si_common.rach_control;
567
Holger Hans Peter Freyther02c608c2010-01-06 07:52:31 +0100568 return sizeof(*si2);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530569}
570
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100571static int generate_si2bis(uint8_t *output, struct gsm_bts *bts)
572{
573 int rc;
574 struct gsm48_system_information_type_2bis *si2b =
575 (struct gsm48_system_information_type_2bis *) output;
576 int n;
577
578 memset(si2b, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
579
580 si2b->header.l2_plen = (22 << 2) | 1;
581 si2b->header.rr_protocol_discriminator = GSM48_PDISC_RR;
582 si2b->header.skip_indicator = 0;
583 si2b->header.system_information = GSM48_MT_RR_SYSINFO_2bis;
584
Max754629f2016-04-15 16:04:45 +0200585 rc = generate_bcch_chan_list(si2b->bcch_frequency_list, bts, false, true, false);
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100586 if (rc < 0)
587 return rc;
588 n = list_arfcn(si2b->bcch_frequency_list, 0xce,
589 "Neighbour cells in same band, but outside P-GSM:");
590 if (n) {
591 /* indicate in SI2 and SI2bis: there is an extension */
592 struct gsm48_system_information_type_2 *si2 =
593 (struct gsm48_system_information_type_2 *)
594 bts->si_buf[SYSINFO_TYPE_2];
595 si2->bcch_frequency_list[0] |= 0x20;
596 si2b->bcch_frequency_list[0] |= 0x20;
597 } else
598 bts->si_valid &= ~(1 << SYSINFO_TYPE_2bis);
599
600 si2b->rach_control = bts->si_common.rach_control;
601
602 return sizeof(*si2b);
603}
604
605static int generate_si2ter(uint8_t *output, struct gsm_bts *bts)
606{
607 int rc;
608 struct gsm48_system_information_type_2ter *si2t =
609 (struct gsm48_system_information_type_2ter *) output;
610 int n;
611
612 memset(si2t, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
613
614 si2t->header.l2_plen = (22 << 2) | 1;
615 si2t->header.rr_protocol_discriminator = GSM48_PDISC_RR;
616 si2t->header.skip_indicator = 0;
617 si2t->header.system_information = GSM48_MT_RR_SYSINFO_2ter;
618
Max754629f2016-04-15 16:04:45 +0200619 rc = generate_bcch_chan_list(si2t->ext_bcch_frequency_list, bts, false, false, true);
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100620 if (rc < 0)
621 return rc;
622 n = list_arfcn(si2t->ext_bcch_frequency_list, 0x8e,
623 "Neighbour cells in different band:");
624 if (!n)
625 bts->si_valid &= ~(1 << SYSINFO_TYPE_2ter);
626
627 return sizeof(*si2t);
628}
629
Max88ddcaa2016-04-15 16:04:46 +0200630static int generate_si2quater(uint8_t *output, struct gsm_bts *bts)
631{
632 int rc;
633 struct gsm48_system_information_type_2quater *si2q =
634 (struct gsm48_system_information_type_2quater *) output;
635
636 memset(si2q, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
637
638 si2q->header.l2_plen = GSM48_LEN2PLEN(22);
639 si2q->header.rr_protocol_discriminator = GSM48_PDISC_RR;
640 si2q->header.skip_indicator = 0;
641 si2q->header.system_information = GSM48_MT_RR_SYSINFO_2quater;
642
643 rc = rest_octets_si2quater(si2q->rest_octets,
Maxeaf196c2016-04-20 15:57:13 +0200644 &bts->si_common.si2quater_neigh_list,
645 bts->si_common.data.uarfcn_list,
646 bts->si_common.data.scramble_list,
647 bts->si_common.uarfcn_length);
Max88ddcaa2016-04-15 16:04:46 +0200648 if (rc < 0)
649 return rc;
650
651 return sizeof(*si2q) + rc;
652}
653
Harald Weltee0500102010-03-14 23:46:48 +0800654static struct gsm48_si_ro_info si_info = {
Harald Weltea54a2bb2009-12-01 18:04:30 +0530655 .selection_params = {
656 .present = 0,
657 },
658 .power_offset = {
659 .present = 0,
660 },
661 .si2ter_indicator = 0,
662 .early_cm_ctrl = 1,
663 .scheduling = {
664 .present = 0,
665 },
666 .gprs_ind = {
667 .si13_position = 0,
668 .ra_colour = 0,
Harald Welte3055e332010-03-14 15:37:43 +0800669 .present = 1,
Harald Weltea54a2bb2009-12-01 18:04:30 +0530670 },
Maxcab5beb2016-04-15 16:04:44 +0200671 .si2quater_indicator = 0,
Harald Weltea54a2bb2009-12-01 18:04:30 +0530672 .lsa_params = {
673 .present = 0,
674 },
675 .cell_id = 0, /* FIXME: doesn't the bts have this? */
676 .break_ind = 0,
677};
678
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200679static int generate_si3(uint8_t *output, struct gsm_bts *bts)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530680{
Holger Hans Peter Freyther02c608c2010-01-06 07:52:31 +0100681 int rc;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530682 struct gsm48_system_information_type_3 *si3 =
683 (struct gsm48_system_information_type_3 *) output;
684
685 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
686
687 si3->header.l2_plen = (18 << 2) | 1;
688 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
689 si3->header.skip_indicator = 0;
690 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
691
692 si3->cell_identity = htons(bts->cell_identity);
Harald Welted8493ac2010-03-04 10:55:40 +0100693 gsm48_generate_lai(&si3->lai, bts->network->country_code,
694 bts->network->network_code,
695 bts->location_area_code);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530696 si3->control_channel_desc = bts->si_common.chan_desc;
697 si3->cell_options = bts->si_common.cell_options;
698 si3->cell_sel_par = bts->si_common.cell_sel_par;
699 si3->rach_control = bts->si_common.rach_control;
700
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100701 if ((bts->si_valid & (1 << SYSINFO_TYPE_2ter))) {
702 LOGP(DRR, LOGL_INFO, "SI 2ter is included.\n");
703 si_info.si2ter_indicator = 1;
704 } else {
705 si_info.si2ter_indicator = 0;
706 }
Maxcab5beb2016-04-15 16:04:44 +0200707 if ((bts->si_valid & (1 << SYSINFO_TYPE_2quater))) {
708 LOGP(DRR, LOGL_INFO, "SI 2quater is included.\n");
709 si_info.si2quater_indicator = 1;
710 } else {
711 si_info.si2quater_indicator = 0;
712 }
Harald Weltea54a2bb2009-12-01 18:04:30 +0530713 /* SI3 Rest Octets (10.5.2.34), containing
714 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
715 Power Offset, 2ter Indicator, Early Classmark Sending,
716 Scheduling if and WHERE, GPRS Indicator, SI13 position */
Holger Hans Peter Freyther02c608c2010-01-06 07:52:31 +0100717 rc = rest_octets_si3(si3->rest_octets, &si_info);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530718
Holger Hans Peter Freyther02c608c2010-01-06 07:52:31 +0100719 return sizeof(*si3) + rc;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530720}
721
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200722static int generate_si4(uint8_t *output, struct gsm_bts *bts)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530723{
Holger Hans Peter Freyther02c608c2010-01-06 07:52:31 +0100724 int rc;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530725 struct gsm48_system_information_type_4 *si4 =
726 (struct gsm48_system_information_type_4 *) output;
Harald Weltebf4ba722014-12-28 15:00:45 +0100727 struct gsm_lchan *cbch_lchan;
728 uint8_t *restoct = si4->data;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530729
730 /* length of all IEs present except SI4 rest octets and l2_plen */
731 int l2_plen = sizeof(*si4) - 1;
732
733 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
734
735 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
736 si4->header.skip_indicator = 0;
737 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
738
Harald Welted8493ac2010-03-04 10:55:40 +0100739 gsm48_generate_lai(&si4->lai, bts->network->country_code,
740 bts->network->network_code,
741 bts->location_area_code);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530742 si4->cell_sel_par = bts->si_common.cell_sel_par;
743 si4->rach_control = bts->si_common.rach_control;
744
745 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
Harald Welteadbd96f2014-12-29 17:47:08 +0100746 cbch_lchan = gsm_bts_get_cbch(bts);
Harald Weltebf4ba722014-12-28 15:00:45 +0100747 if (cbch_lchan) {
748 struct gsm48_chan_desc cd;
749 gsm48_lchan2chan_desc(&cd, cbch_lchan);
Daniel Willmann1415dd12014-12-30 12:10:25 +0100750 tv_fixed_put(si4->data, GSM48_IE_CBCH_CHAN_DESC, 3,
Harald Weltebf4ba722014-12-28 15:00:45 +0100751 (uint8_t *) &cd);
Daniel Willmann1415dd12014-12-30 12:10:25 +0100752 l2_plen += 3 + 1;
753 restoct += 3 + 1;
Harald Weltebf4ba722014-12-28 15:00:45 +0100754 /* we don't use hopping and thus don't need a CBCH MA */
755 }
Harald Weltea54a2bb2009-12-01 18:04:30 +0530756
757 si4->header.l2_plen = (l2_plen << 2) | 1;
758
759 /* SI4 Rest Octets (10.5.2.35), containing
760 Optional Power offset, GPRS Indicator,
761 Cell Identity, LSA ID, Selection Parameter */
Holger Hans Peter Freyther667308c2010-12-29 23:06:22 +0100762 rc = rest_octets_si4(restoct, &si_info, output + GSM_MACBLOCK_LEN - restoct);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530763
Harald Weltebf4ba722014-12-28 15:00:45 +0100764 return l2_plen + 1 + rc;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530765}
766
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200767static int generate_si5(uint8_t *output, struct gsm_bts *bts)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530768{
Harald Welte2a92a712009-12-19 21:26:54 +0100769 struct gsm48_system_information_type_5 *si5;
770 int rc, l2_plen = 18;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530771
Holger Hans Peter Freyther4f2ace42010-01-06 07:52:55 +0100772 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
773
Harald Welte2a92a712009-12-19 21:26:54 +0100774 /* ip.access nanoBTS needs l2_plen!! */
Harald Welte08011e22011-03-04 13:41:31 +0100775 switch (bts->type) {
776 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +0200777 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte2a92a712009-12-19 21:26:54 +0100778 *output++ = (l2_plen << 2) | 1;
779 l2_plen++;
Harald Welte08011e22011-03-04 13:41:31 +0100780 break;
781 default:
782 break;
Harald Welte2a92a712009-12-19 21:26:54 +0100783 }
784
785 si5 = (struct gsm48_system_information_type_5 *) output;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530786
787 /* l2 pseudo length, not part of msg: 18 */
788 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
789 si5->skip_indicator = 0;
790 si5->system_information = GSM48_MT_RR_SYSINFO_5;
Max754629f2016-04-15 16:04:45 +0200791 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts, true, false, false);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530792 if (rc < 0)
793 return rc;
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100794 list_arfcn(si5->bcch_frequency_list, 0xce,
Harald Welte13428942015-09-04 06:22:46 +0200795 "SI5 Neighbour cells in same band:");
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100796
797 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
798 return l2_plen;
799}
800
801static int generate_si5bis(uint8_t *output, struct gsm_bts *bts)
802{
803 struct gsm48_system_information_type_5bis *si5b;
804 int rc, l2_plen = 18;
805 int n;
806
807 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
808
809 /* ip.access nanoBTS needs l2_plen!! */
810 switch (bts->type) {
811 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +0200812 case GSM_BTS_TYPE_OSMO_SYSMO:
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100813 *output++ = (l2_plen << 2) | 1;
814 l2_plen++;
815 break;
816 default:
817 break;
818 }
819
820 si5b = (struct gsm48_system_information_type_5bis *) output;
821
822 /* l2 pseudo length, not part of msg: 18 */
823 si5b->rr_protocol_discriminator = GSM48_PDISC_RR;
824 si5b->skip_indicator = 0;
825 si5b->system_information = GSM48_MT_RR_SYSINFO_5bis;
Max754629f2016-04-15 16:04:45 +0200826 rc = generate_bcch_chan_list(si5b->bcch_frequency_list, bts, true, true, false);
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100827 if (rc < 0)
828 return rc;
829 n = list_arfcn(si5b->bcch_frequency_list, 0xce,
830 "Neighbour cells in same band, but outside P-GSM:");
831 if (n) {
832 /* indicate in SI5 and SI5bis: there is an extension */
833 struct gsm48_system_information_type_5 *si5 =
834 (struct gsm48_system_information_type_5 *)
835 bts->si_buf[SYSINFO_TYPE_5];
836 si5->bcch_frequency_list[0] |= 0x20;
837 si5b->bcch_frequency_list[0] |= 0x20;
838 } else
839 bts->si_valid &= ~(1 << SYSINFO_TYPE_5bis);
840
841 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
842 return l2_plen;
843}
844
845static int generate_si5ter(uint8_t *output, struct gsm_bts *bts)
846{
847 struct gsm48_system_information_type_5ter *si5t;
848 int rc, l2_plen = 18;
849 int n;
850
851 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
852
853 /* ip.access nanoBTS needs l2_plen!! */
854 switch (bts->type) {
855 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +0200856 case GSM_BTS_TYPE_OSMO_SYSMO:
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100857 *output++ = (l2_plen << 2) | 1;
858 l2_plen++;
859 break;
860 default:
861 break;
862 }
863
864 si5t = (struct gsm48_system_information_type_5ter *) output;
865
866 /* l2 pseudo length, not part of msg: 18 */
867 si5t->rr_protocol_discriminator = GSM48_PDISC_RR;
868 si5t->skip_indicator = 0;
869 si5t->system_information = GSM48_MT_RR_SYSINFO_5ter;
Max754629f2016-04-15 16:04:45 +0200870 rc = generate_bcch_chan_list(si5t->bcch_frequency_list, bts, true, false, true);
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100871 if (rc < 0)
872 return rc;
873 n = list_arfcn(si5t->bcch_frequency_list, 0x8e,
874 "Neighbour cells in different band:");
875 if (!n)
876 bts->si_valid &= ~(1 << SYSINFO_TYPE_5ter);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530877
878 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
Harald Welte2a92a712009-12-19 21:26:54 +0100879 return l2_plen;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530880}
881
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200882static int generate_si6(uint8_t *output, struct gsm_bts *bts)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530883{
Harald Welte2a92a712009-12-19 21:26:54 +0100884 struct gsm48_system_information_type_6 *si6;
885 int l2_plen = 11;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530886
Holger Hans Peter Freyther4f2ace42010-01-06 07:52:55 +0100887 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
888
Harald Welte2a92a712009-12-19 21:26:54 +0100889 /* ip.access nanoBTS needs l2_plen!! */
Harald Welte08011e22011-03-04 13:41:31 +0100890 switch (bts->type) {
891 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +0200892 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte2a92a712009-12-19 21:26:54 +0100893 *output++ = (l2_plen << 2) | 1;
894 l2_plen++;
Harald Welte08011e22011-03-04 13:41:31 +0100895 break;
896 default:
897 break;
Harald Welte2a92a712009-12-19 21:26:54 +0100898 }
899
900 si6 = (struct gsm48_system_information_type_6 *) output;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530901
902 /* l2 pseudo length, not part of msg: 11 */
903 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
904 si6->skip_indicator = 0;
905 si6->system_information = GSM48_MT_RR_SYSINFO_6;
906 si6->cell_identity = htons(bts->cell_identity);
Harald Welted8493ac2010-03-04 10:55:40 +0100907 gsm48_generate_lai(&si6->lai, bts->network->country_code,
908 bts->network->network_code,
909 bts->location_area_code);
Harald Weltea54a2bb2009-12-01 18:04:30 +0530910 si6->cell_options = bts->si_common.cell_options;
911 si6->ncc_permitted = bts->si_common.ncc_permitted;
912
913 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
914
Harald Welte2a92a712009-12-19 21:26:54 +0100915 return l2_plen;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530916}
917
918static struct gsm48_si13_info si13_default = {
919 .cell_opts = {
Harald Welteb0644712010-06-02 12:19:21 +0200920 .nmo = GPRS_NMO_II,
Harald Weltea9713bf2010-05-31 12:54:23 +0200921 .t3168 = 2000,
Andreas Eversbergf225f2d2012-09-23 08:14:51 +0200922 .t3192 = 1500,
Harald Welte3055e332010-03-14 15:37:43 +0800923 .drx_timer_max = 3,
Harald Weltea54a2bb2009-12-01 18:04:30 +0530924 .bs_cv_max = 15,
Sylvain Munaut1fa52392011-10-17 13:56:02 +0200925 .ext_info_present = 0,
Harald Welte3ce9a0d2010-04-18 14:49:05 +0200926 .ext_info = {
927 /* The values below are just guesses ! */
Harald Welte09faa062010-04-18 15:53:40 +0200928 .egprs_supported = 0,
Harald Welte3ce9a0d2010-04-18 14:49:05 +0200929 .use_egprs_p_ch_req = 1,
Harald Weltea9713bf2010-05-31 12:54:23 +0200930 .bep_period = 5,
Harald Welte3ce9a0d2010-04-18 14:49:05 +0200931 .pfc_supported = 0,
932 .dtm_supported = 0,
933 .bss_paging_coordination = 0,
934 },
Harald Weltea54a2bb2009-12-01 18:04:30 +0530935 },
936 .pwr_ctrl_pars = {
Harald Weltea4a7c432012-10-08 21:22:08 +0200937 .alpha = 0, /* a = 0.0 */
Harald Weltea9713bf2010-05-31 12:54:23 +0200938 .t_avg_w = 16,
939 .t_avg_t = 16,
Harald Weltea54a2bb2009-12-01 18:04:30 +0530940 .pc_meas_chan = 0, /* downling measured on CCCH */
Harald Weltea9713bf2010-05-31 12:54:23 +0200941 .n_avg_i = 8,
Harald Weltea54a2bb2009-12-01 18:04:30 +0530942 },
Harald Welte3055e332010-03-14 15:37:43 +0800943 .bcch_change_mark = 1,
Harald Weltea54a2bb2009-12-01 18:04:30 +0530944 .si_change_field = 0,
945 .pbcch_present = 0,
946 {
947 .no_pbcch = {
Harald Welte3055e332010-03-14 15:37:43 +0800948 .rac = 0, /* needs to be patched */
Harald Weltea54a2bb2009-12-01 18:04:30 +0530949 .spgc_ccch_sup = 0,
950 .net_ctrl_ord = 0,
Harald Welte3055e332010-03-14 15:37:43 +0800951 .prio_acc_thr = 6,
Harald Weltea54a2bb2009-12-01 18:04:30 +0530952 },
953 },
954};
955
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200956static int generate_si13(uint8_t *output, struct gsm_bts *bts)
Harald Weltea54a2bb2009-12-01 18:04:30 +0530957{
958 struct gsm48_system_information_type_13 *si13 =
959 (struct gsm48_system_information_type_13 *) output;
960 int ret;
961
962 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
963
964 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
965 si13->header.skip_indicator = 0;
966 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
967
Harald Welte3055e332010-03-14 15:37:43 +0800968 si13_default.no_pbcch.rac = bts->gprs.rac;
Andreas Eversberga4fa21c2013-03-16 16:31:26 +0100969 si13_default.no_pbcch.net_ctrl_ord = bts->gprs.net_ctrl_ord;
Harald Welte3055e332010-03-14 15:37:43 +0800970
Holger Hans Peter Freythereacb1782014-11-21 10:00:39 +0100971 /* Information about the other SIs */
972 si13_default.bcch_change_mark = bts->bcch_change_mark;
973
Harald Weltea54a2bb2009-12-01 18:04:30 +0530974 ret = rest_octets_si13(si13->rest_octets, &si13_default);
975 if (ret < 0)
976 return ret;
977
Holger Hans Peter Freyther6c84d702010-06-16 12:05:56 +0800978 /* length is coded in bit 2 an up */
979 si13->header.l2_plen = 0x01;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530980
Holger Hans Peter Freyther02c608c2010-01-06 07:52:31 +0100981 return sizeof (*si13) + ret;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530982}
983
Harald Welte99bed522010-06-15 16:44:12 +0200984typedef int (*gen_si_fn_t)(uint8_t *output, struct gsm_bts *bts);
985
986static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
987 [SYSINFO_TYPE_1] = &generate_si1,
988 [SYSINFO_TYPE_2] = &generate_si2,
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100989 [SYSINFO_TYPE_2bis] = &generate_si2bis,
990 [SYSINFO_TYPE_2ter] = &generate_si2ter,
Max88ddcaa2016-04-15 16:04:46 +0200991 [SYSINFO_TYPE_2quater] = &generate_si2quater,
Harald Welte99bed522010-06-15 16:44:12 +0200992 [SYSINFO_TYPE_3] = &generate_si3,
993 [SYSINFO_TYPE_4] = &generate_si4,
994 [SYSINFO_TYPE_5] = &generate_si5,
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100995 [SYSINFO_TYPE_5bis] = &generate_si5bis,
996 [SYSINFO_TYPE_5ter] = &generate_si5ter,
Harald Welte99bed522010-06-15 16:44:12 +0200997 [SYSINFO_TYPE_6] = &generate_si6,
998 [SYSINFO_TYPE_13] = &generate_si13,
999};
1000
Harald Welte99bed522010-06-15 16:44:12 +02001001int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
1002{
1003 gen_si_fn_t gen_si;
1004
Harald Weltecb20b7a2010-04-18 15:51:20 +02001005 switch (bts->gprs.mode) {
1006 case BTS_GPRS_EGPRS:
Harald Welte09faa062010-04-18 15:53:40 +02001007 si13_default.cell_opts.ext_info_present = 1;
1008 si13_default.cell_opts.ext_info.egprs_supported = 1;
1009 /* fallthrough */
Harald Weltecb20b7a2010-04-18 15:51:20 +02001010 case BTS_GPRS_GPRS:
1011 si_info.gprs_ind.present = 1;
1012 break;
1013 case BTS_GPRS_NONE:
1014 si_info.gprs_ind.present = 0;
1015 break;
1016 }
Harald Weltee0500102010-03-14 23:46:48 +08001017
Sylvain Munaut00d71462010-11-28 18:17:28 +01001018 memcpy(&si_info.selection_params,
1019 &bts->si_common.cell_ro_sel_par,
1020 sizeof(struct gsm48_si_selection_params));
1021
Harald Welte99bed522010-06-15 16:44:12 +02001022 gen_si = gen_si_fn[si_type];
1023 if (!gen_si)
Harald Weltea54a2bb2009-12-01 18:04:30 +05301024 return -EINVAL;
Harald Weltea54a2bb2009-12-01 18:04:30 +05301025
Harald Welte99bed522010-06-15 16:44:12 +02001026 return gen_si(bts->si_buf[si_type], bts);
Harald Weltea54a2bb2009-12-01 18:04:30 +05301027}