blob: dcabbbdd14ac86fafce05effe3af39cc01442555 [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>
Max5fa7e362016-04-15 16:04:45 +020028#include <stdbool.h>
Harald Weltea43f7892009-12-01 18:04:30 +053029
Harald Welte80cffaf2011-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 Weltea43f7892009-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 Freyther511f9c32012-10-13 12:38:54 +020039#include <openbsc/arfcn_range_encode.h>
Harald Weltea43f7892009-12-01 18:04:30 +053040
Holger Hans Peter Freytherd4d1d5e2013-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 Freytherb91a1062010-01-06 06:38:14 +010061
Holger Hans Peter Freytherf876c392013-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
Max299a9992016-04-28 17:51:15 +020071/* Return p(n) for given NR_OF_TDD_CELLS - see Table 9.1.54.1a, 3GPP TS 44.018 */
Max26679e02016-04-20 15:57:13 +020072unsigned 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
Max70fdd242017-06-15 15:10:53 +0200125size_t si2q_earfcn_count(const struct osmo_earfcn_si2q *e)
Max26679e02016-04-20 15:57:13 +0200126{
Max70fdd242017-06-15 15:10:53 +0200127 unsigned i, ret = 0;
Maxf39d03a2017-05-12 17:00:30 +0200128
Max70fdd242017-06-15 15:10:53 +0200129 if (!e)
130 return 0;
131
132 for (i = 0; i < e->length; i++)
133 if (e->arfcn[i] != OSMO_EARFCN_INVALID)
134 ret++;
135
136 return ret;
Max26679e02016-04-20 15:57:13 +0200137}
138
Max70fdd242017-06-15 15:10:53 +0200139/* generate SI2quater messages, return rest octets length of last generated message or negative error code */
140static int make_si2quaters(struct gsm_bts *bts, bool counting)
Max26679e02016-04-20 15:57:13 +0200141{
Max70fdd242017-06-15 15:10:53 +0200142 int rc;
143 bool memory_exceeded = true;
144 struct gsm48_system_information_type_2quater *si2q;
Maxe610e702016-12-19 13:41:48 +0100145
Max70fdd242017-06-15 15:10:53 +0200146 for (bts->si2q_index = 0; bts->si2q_index < SI2Q_MAX_NUM; bts->si2q_index++) {
147 si2q = GSM_BTS_SI2Q(bts, bts->si2q_index);
148 if (counting) { /* that's legitimate if we're called for counting purpose: */
149 if (bts->si2q_count < bts->si2q_index)
150 bts->si2q_count = bts->si2q_index;
151 } else {
152 memset(si2q, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
153
154 si2q->header.l2_plen = GSM48_LEN2PLEN(22);
155 si2q->header.rr_protocol_discriminator = GSM48_PDISC_RR;
156 si2q->header.skip_indicator = 0;
157 si2q->header.system_information = GSM48_MT_RR_SYSINFO_2quater;
158 }
159
160 rc = rest_octets_si2quater(si2q->rest_octets, bts);
161 if (rc < 0)
162 return rc;
163
164 if (bts->u_offset >= bts->si_common.uarfcn_length &&
165 bts->e_offset >= si2q_earfcn_count(&bts->si_common.si2quater_neigh_list)) {
166 memory_exceeded = false;
167 break;
Maxe610e702016-12-19 13:41:48 +0100168 }
169 }
170
Max70fdd242017-06-15 15:10:53 +0200171 if (memory_exceeded)
172 return -ENOMEM;
Maxe610e702016-12-19 13:41:48 +0100173
Max70fdd242017-06-15 15:10:53 +0200174 return rc;
Max26679e02016-04-20 15:57:13 +0200175}
176
Max70fdd242017-06-15 15:10:53 +0200177/* we generate SI2q rest octets twice to get proper estimation but it's one time cost anyway */
Maxf39d03a2017-05-12 17:00:30 +0200178uint8_t si2q_num(struct gsm_bts *bts)
Maxaafff962016-04-20 15:57:14 +0200179{
Max70fdd242017-06-15 15:10:53 +0200180 int rc = make_si2quaters(bts, true);
181 uint8_t num = bts->si2q_index + 1; /* number of SI2quater messages */
Maxf39d03a2017-05-12 17:00:30 +0200182
Max70fdd242017-06-15 15:10:53 +0200183 /* N. B: si2q_num() should NEVER be called during actualSI2q rest octets generation
184 we're not re-entrant because of the following code: */
185 bts->u_offset = 0;
186 bts->e_offset = 0;
Maxf39d03a2017-05-12 17:00:30 +0200187
Max70fdd242017-06-15 15:10:53 +0200188 if (rc < 0)
189 return 0xFF; /* return impossible index as an indicator of error in generating SI2quater */
Maxf39d03a2017-05-12 17:00:30 +0200190
Max70fdd242017-06-15 15:10:53 +0200191 return num;
Maxaafff962016-04-20 15:57:14 +0200192}
193
Max26679e02016-04-20 15:57:13 +0200194/* 3GPP TS 44.018, Table 9.1.54.1 - prepend diversity bit to scrambling code */
Max34be86b2016-12-16 18:45:51 +0100195static inline uint16_t encode_fdd(uint16_t scramble, bool diversity)
Max26679e02016-04-20 15:57:13 +0200196{
197 if (diversity)
198 return scramble | (1 << 9);
199 return scramble;
200}
201
Max70fdd242017-06-15 15:10:53 +0200202int bts_earfcn_add(struct gsm_bts *bts, uint16_t earfcn, uint8_t thresh_hi, uint8_t thresh_lo, uint8_t prio,
203 uint8_t qrx, uint8_t meas_bw)
204{
205 struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
206 int r = osmo_earfcn_add(e, earfcn, (meas_bw < EARFCN_MEAS_BW_INVALID) ? meas_bw : OSMO_EARFCN_MEAS_INVALID);
207
208 if (r < 0)
209 return r;
210
211 if (e->thresh_hi && thresh_hi != e->thresh_hi)
212 r = 1;
213
214 e->thresh_hi = thresh_hi;
215
216 if (thresh_lo != EARFCN_THRESH_LOW_INVALID) {
217 if (e->thresh_lo_valid && e->thresh_lo != thresh_lo)
218 r = EARFCN_THRESH_LOW_INVALID;
219 e->thresh_lo = thresh_lo;
220 e->thresh_lo_valid = true;
221 }
222
223 if (qrx != EARFCN_QRXLV_INVALID) {
224 if (e->qrxlm_valid && e->qrxlm != qrx)
225 r = EARFCN_QRXLV_INVALID + 1;
226 e->qrxlm = qrx;
227 e->qrxlm_valid = true;
228 }
229
230 if (prio != EARFCN_PRIO_INVALID) {
231 if (e->prio_valid && e->prio != prio)
232 r = EARFCN_PRIO_INVALID;
233 e->prio = prio;
234 e->prio_valid = true;
235 }
236
237 return r;
238}
239
Max26679e02016-04-20 15:57:13 +0200240int bts_uarfcn_del(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble)
241{
242 uint16_t sc0 = encode_fdd(scramble, false), sc1 = encode_fdd(scramble, true),
243 *ual = bts->si_common.data.uarfcn_list,
244 *scl = bts->si_common.data.scramble_list;
245 size_t len = bts->si_common.uarfcn_length, i;
246 for (i = 0; i < len; i++) {
247 if (arfcn == ual[i] && (sc0 == scl[i] || sc1 == scl[i])) {
248 /* we rely on the assumption that (uarfcn, scramble)
249 tuple is unique in the lists */
250 if (i != len - 1) { /* move the tail if necessary */
251 memmove(ual + i, ual + i + 1, 2 * (len - i + 1));
252 memmove(scl + i, scl + i + 1, 2 * (len - i + 1));
253 }
254 break;
255 }
256 }
257
258 if (i == len)
259 return -EINVAL;
260
261 bts->si_common.uarfcn_length--;
262 return 0;
263}
264
Maxf39d03a2017-05-12 17:00:30 +0200265int bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble, bool diversity)
Max26679e02016-04-20 15:57:13 +0200266{
Maxe610e702016-12-19 13:41:48 +0100267 size_t len = bts->si_common.uarfcn_length, i, k = 0;
Max26679e02016-04-20 15:57:13 +0200268 uint16_t scr, chk,
269 *ual = bts->si_common.data.uarfcn_list,
270 *scl = bts->si_common.data.scramble_list,
271 scramble1 = encode_fdd(scramble, true),
272 scramble0 = encode_fdd(scramble, false);
273
274 scr = diversity ? scramble1 : scramble0;
275 chk = diversity ? scramble0 : scramble1;
276
277 if (len == MAX_EARFCN_LIST)
278 return -ENOMEM;
279
Maxe610e702016-12-19 13:41:48 +0100280 for (i = 0; i < len; i++) /* find the position of arfcn if any */
281 if (arfcn == ual[i])
282 break;
283
284 for (k = 0; i < len; i++) {
Max26679e02016-04-20 15:57:13 +0200285 if (arfcn == ual[i] && (scr == scl[i] || chk == scl[i]))
286 return -EADDRINUSE;
287 if (scr > scl[i])
288 k = i + 1;
289 }
290 /* we keep lists sorted by scramble code:
291 insert into appropriate position and move the tail */
292 if (len - k) {
293 memmove(ual + k + 1, ual + k, (len - k) * 2);
294 memmove(scl + k + 1, scl + k, (len - k) * 2);
295 }
Maxe610e702016-12-19 13:41:48 +0100296
Max26679e02016-04-20 15:57:13 +0200297 ual[k] = arfcn;
298 scl[k] = scr;
299 bts->si_common.uarfcn_length++;
Maxaafff962016-04-20 15:57:14 +0200300
Max70fdd242017-06-15 15:10:53 +0200301 if (si2q_num(bts) <= SI2Q_MAX_NUM) {
302 bts->si2q_count = si2q_num(bts) - 1;
Maxaafff962016-04-20 15:57:14 +0200303 return 0;
Max70fdd242017-06-15 15:10:53 +0200304 }
Maxaafff962016-04-20 15:57:14 +0200305
306 bts_uarfcn_del(bts, arfcn, scramble);
307 return -ENOSPC;
Max26679e02016-04-20 15:57:13 +0200308}
309
Max5fa7e362016-04-15 16:04:45 +0200310static inline int use_arfcn(const struct gsm_bts *bts, const bool bis, const bool ter,
311 const bool pgsm, const int arfcn)
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100312{
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100313 if (bts->force_combined_si)
314 return !bis && !ter;
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100315 if (!bis && !ter && band_compatible(bts, arfcn))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100316 return 1;
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100317 /* Correct but somehow broken with either the nanoBTS or the iPhone5 */
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100318 if (bis && pgsm && band_compatible(bts, arfcn) && (arfcn < 1 || arfcn > 124))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100319 return 1;
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100320 if (ter && !band_compatible(bts, arfcn))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100321 return 1;
322 return 0;
323}
324
Harald Welteda760d32009-12-14 20:25:05 +0100325/* Frequency Lists as per TS 04.08 10.5.2.13 */
326
327/* 10.5.2.13.2: Bit map 0 format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200328static int freq_list_bm0_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +0530329{
330 unsigned int byte, bit;
331
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100332 if (arfcn > 124 || arfcn < 1) {
333 LOGP(DRR, LOGL_ERROR, "Bitmap 0 only supports ARFCN 1...124\n");
Harald Weltea43f7892009-12-01 18:04:30 +0530334 return -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100335 }
Harald Weltea43f7892009-12-01 18:04:30 +0530336
Harald Welteda760d32009-12-14 20:25:05 +0100337 /* the bitmask is from 1..124, not from 0..123 */
338 arfcn--;
339
Harald Weltea43f7892009-12-01 18:04:30 +0530340 byte = arfcn / 8;
341 bit = arfcn % 8;
342
Harald Welteda760d32009-12-14 20:25:05 +0100343 chan_list[GSM48_CELL_CHAN_DESC_SIZE-1-byte] |= (1 << bit);
Harald Weltea43f7892009-12-01 18:04:30 +0530344
345 return 0;
346}
347
Harald Welteda760d32009-12-14 20:25:05 +0100348/* 10.5.2.13.7: Variable bit map format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200349static int freq_list_bmrel_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +0530350{
351 unsigned int byte, bit;
352 unsigned int min_arfcn;
353 unsigned int bitno;
354
355 min_arfcn = (chan_list[0] & 1) << 9;
356 min_arfcn |= chan_list[1] << 1;
357 min_arfcn |= (chan_list[2] >> 7) & 1;
358
359 /* The lower end of our bitmaks is always implicitly included */
360 if (arfcn == min_arfcn)
361 return 0;
362
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100363 if (((arfcn - min_arfcn) & 1023) > 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100364 LOGP(DRR, LOGL_ERROR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +0530365 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +0100366 }
Harald Weltea43f7892009-12-01 18:04:30 +0530367
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100368 bitno = (arfcn - min_arfcn) & 1023;
Harald Weltea43f7892009-12-01 18:04:30 +0530369 byte = bitno / 8;
370 bit = bitno % 8;
371
372 chan_list[2 + byte] |= 1 << (7 - bit);
373
374 return 0;
375}
376
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200377/* generate a variable bitmap */
Max5fa7e362016-04-15 16:04:45 +0200378static inline int enc_freq_lst_var_bitmap(uint8_t *chan_list,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200379 struct bitvec *bv, const struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200380 bool bis, bool ter, int min, bool pgsm)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200381{
382 int i;
383
384 /* set it to 'Variable bitmap format' */
385 chan_list[0] = 0x8e;
386
387 chan_list[0] |= (min >> 9) & 1;
388 chan_list[1] = (min >> 1);
389 chan_list[2] = (min & 1) << 7;
390
391 for (i = 0; i < bv->data_len*8; i++) {
392 /* see notes in bitvec2freq_list */
393 if (bitvec_get_bit_pos(bv, i)
Harald Weltee18f78e2015-09-04 06:21:32 +0200394 && ((!bis && !ter && band_compatible(bts,i))
395 || (bis && pgsm && band_compatible(bts,i) && (i < 1 || i > 124))
396 || (ter && !band_compatible(bts, i)))) {
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200397 int rc = freq_list_bmrel_set_arfcn(chan_list, i);
398 if (rc < 0)
399 return rc;
400 }
401 }
402
403 return 0;
404}
405
Max881064e2016-12-14 14:51:40 +0100406int range_encode(enum gsm48_range r, int *arfcns, int arfcns_used, int *w,
407 int f0, uint8_t *chan_list)
408{
409 /*
410 * Manipulate the ARFCN list according to the rules in J4 depending
411 * on the selected range.
412 */
413 int rc, f0_included;
414
415 range_enc_filter_arfcns(arfcns, arfcns_used, f0, &f0_included);
416
417 rc = range_enc_arfcns(r, arfcns, arfcns_used, w, 0);
418 if (rc < 0)
419 return rc;
420
421 /* Select the range and the amount of bits needed */
422 switch (r) {
423 case ARFCN_RANGE_128:
424 return range_enc_range128(chan_list, f0, w);
425 case ARFCN_RANGE_256:
426 return range_enc_range256(chan_list, f0, w);
427 case ARFCN_RANGE_512:
428 return range_enc_range512(chan_list, f0, w);
429 case ARFCN_RANGE_1024:
430 return range_enc_range1024(chan_list, f0, f0_included, w);
431 default:
432 return -ERANGE;
433 };
434
435 return f0_included;
436}
437
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200438/* generate a frequency list with the range 512 format */
Max5fa7e362016-04-15 16:04:45 +0200439static inline int enc_freq_lst_range(uint8_t *chan_list,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200440 struct bitvec *bv, const struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200441 bool bis, bool ter, bool pgsm)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200442{
443 int arfcns[RANGE_ENC_MAX_ARFCNS];
444 int w[RANGE_ENC_MAX_ARFCNS];
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200445 int arfcns_used = 0;
Max881064e2016-12-14 14:51:40 +0100446 int i, range, f0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200447
448 /*
449 * Select ARFCNs according to the rules in bitvec2freq_list
450 */
451 for (i = 0; i < bv->data_len * 8; ++i) {
452 /* More ARFCNs than the maximum */
453 if (arfcns_used > ARRAY_SIZE(arfcns))
454 return -1;
455 /* Check if we can select it? */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100456 if (bitvec_get_bit_pos(bv, i) && use_arfcn(bts, bis, ter, pgsm, i))
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200457 arfcns[arfcns_used++] = i;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200458 }
459
460 /*
461 * Check if the given list of ARFCNs can be encoded.
462 */
463 range = range_enc_determine_range(arfcns, arfcns_used, &f0);
464 if (range == ARFCN_RANGE_INVALID)
465 return -2;
466
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200467 memset(w, 0, sizeof(w));
Max881064e2016-12-14 14:51:40 +0100468 return range_encode(range, arfcns, arfcns_used, w, f0, chan_list);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200469}
470
Harald Weltea43f7892009-12-01 18:04:30 +0530471/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200472static int bitvec2freq_list(uint8_t *chan_list, struct bitvec *bv,
Max5fa7e362016-04-15 16:04:45 +0200473 const struct gsm_bts *bts, bool bis, bool ter)
Harald Weltea43f7892009-12-01 18:04:30 +0530474{
Max5fa7e362016-04-15 16:04:45 +0200475 int i, rc, min = -1, max = -1, arfcns = 0;
476 bool pgsm = false;
Harald Weltea43f7892009-12-01 18:04:30 +0530477 memset(chan_list, 0, 16);
478
Harald Welte29350342012-02-17 15:58:23 +0100479 if (bts->band == GSM_BAND_900
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100480 && bts->c0->arfcn >= 1 && bts->c0->arfcn <= 124)
Max5fa7e362016-04-15 16:04:45 +0200481 pgsm = true;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100482 /* P-GSM-only handsets only support 'bit map 0 format' */
483 if (!bis && !ter && pgsm) {
Harald Weltea43f7892009-12-01 18:04:30 +0530484 chan_list[0] = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100485
486 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100487 if (i >= 1 && i <= 124
488 && bitvec_get_bit_pos(bv, i)) {
Harald Welte6c40def2009-12-14 22:07:14 +0100489 rc = freq_list_bm0_set_arfcn(chan_list, i);
490 if (rc < 0)
491 return rc;
492 }
Harald Weltea43f7892009-12-01 18:04:30 +0530493 }
494 return 0;
495 }
496
Harald Welte6c40def2009-12-14 22:07:14 +0100497 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100498 /* in case of SI2 or SI5 allow all neighbours in same band
499 * in case of SI*bis, allow neighbours in same band ouside pgsm
500 * in case of SI*ter, allow neighbours in different bands
501 */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100502 if (!bitvec_get_bit_pos(bv, i))
503 continue;
504 if (!use_arfcn(bts, bis, ter, pgsm, i))
505 continue;
506 /* count the arfcns we want to carry */
507 arfcns += 1;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200508
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100509 /* 955..1023 < 0..885 */
510 if (min < 0)
511 min = i;
512 if (i >= 955 && min < 955)
513 min = i;
514 if (i >= 955 && min >= 955 && i < min)
515 min = i;
516 if (i < 955 && min < 955 && i < min)
517 min = i;
518 if (max < 0)
519 max = i;
520 if (i < 955 && max >= 955)
521 max = i;
522 if (i >= 955 && max >= 955 && i > max)
523 max = i;
524 if (i < 955 && max < 955 && i > max)
525 max = i;
Harald Weltea43f7892009-12-01 18:04:30 +0530526 }
527
Sylvain Munaut42a56522009-12-24 14:20:19 +0100528 if (max == -1) {
529 /* Empty set, use 'bit map 0 format' */
530 chan_list[0] = 0;
531 return 0;
532 }
533
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200534 /* Now find the best encoding */
535 if (((max - min) & 1023) <= 111)
536 return enc_freq_lst_var_bitmap(chan_list, bv, bts, bis,
537 ter, min, pgsm);
Harald Weltea43f7892009-12-01 18:04:30 +0530538
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200539 /* Attempt to do the range encoding */
540 rc = enc_freq_lst_range(chan_list, bv, bts, bis, ter, pgsm);
Max881064e2016-12-14 14:51:40 +0100541 if (rc >= 0)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200542 return 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530543
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200544 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, arfcns=%d "
545 "can not generate ARFCN list", min, max, arfcns);
546 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530547}
548
549/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Dieter Spaar16646022011-07-28 00:01:50 +0200550/* static*/ int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530551{
Harald Weltea43f7892009-12-01 18:04:30 +0530552 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100553 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530554
Harald Weltea39b0f22010-06-14 22:26:10 +0200555 /* Zero-initialize the bit-vector */
Harald Weltec8794b52010-06-16 11:09:18 +0200556 memset(bv->data, 0, bv->data_len);
Harald Weltea39b0f22010-06-14 22:26:10 +0200557
Harald Welte6c40def2009-12-14 22:07:14 +0100558 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
Harald Weltea39b0f22010-06-14 22:26:10 +0200559 llist_for_each_entry(trx, &bts->trx_list, list) {
560 unsigned int i, j;
561 /* Always add the TRX's ARFCN */
Harald Welte6c40def2009-12-14 22:07:14 +0100562 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea39b0f22010-06-14 22:26:10 +0200563 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
564 struct gsm_bts_trx_ts *ts = &trx->ts[i];
565 /* Add any ARFCNs present in hopping channels */
566 for (j = 0; j < 1024; j++) {
567 if (bitvec_get_bit_pos(&ts->hopping.arfcns, j))
568 bitvec_set_bit_pos(bv, j, 1);
569 }
570 }
571 }
Harald Weltea43f7892009-12-01 18:04:30 +0530572
Harald Welte6c40def2009-12-14 22:07:14 +0100573 /* then we generate a GSM 04.08 frequency list from the bitvec */
Max5fa7e362016-04-15 16:04:45 +0200574 return bitvec2freq_list(chan_list, bv, bts, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530575}
576
Harald Welte6c40def2009-12-14 22:07:14 +0100577/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100578static int generate_bcch_chan_list(uint8_t *chan_list, struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200579 bool si5, bool bis, bool ter)
Harald Welte6c40def2009-12-14 22:07:14 +0100580{
581 struct gsm_bts *cur_bts;
Harald Welte64c07d22011-02-15 11:43:27 +0100582 struct bitvec *bv;
583
584 if (si5 && bts->neigh_list_manual_mode == NL_MODE_MANUAL_SI5SEP)
585 bv = &bts->si_common.si5_neigh_list;
586 else
587 bv = &bts->si_common.neigh_list;
Harald Welte6c40def2009-12-14 22:07:14 +0100588
Harald Welte32c09622011-01-11 23:44:56 +0100589 /* Generate list of neighbor cells if we are in automatic mode */
Harald Welte64c07d22011-02-15 11:43:27 +0100590 if (bts->neigh_list_manual_mode == NL_MODE_AUTOMATIC) {
Harald Welte21bbda22011-02-19 20:43:37 +0100591 /* Zero-initialize the bit-vector */
592 memset(bv->data, 0, bv->data_len);
593
Harald Welte32c09622011-01-11 23:44:56 +0100594 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
595 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
596 if (cur_bts == bts)
597 continue;
598 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
599 }
Harald Welte6c40def2009-12-14 22:07:14 +0100600 }
601
602 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100603 return bitvec2freq_list(chan_list, bv, bts, bis, ter);
604}
605
606static int list_arfcn(uint8_t *chan_list, uint8_t mask, char *text)
607{
608 int n = 0, i;
609 struct gsm_sysinfo_freq freq[1024];
610
611 memset(freq, 0, sizeof(freq));
612 gsm48_decode_freq_list(freq, chan_list, 16, 0xce, 1);
613 for (i = 0; i < 1024; i++) {
614 if (freq[i].mask) {
615 if (!n)
616 LOGP(DRR, LOGL_INFO, "%s", text);
617 LOGPC(DRR, LOGL_INFO, " %d", i);
618 n++;
619 }
620 }
621 if (n)
622 LOGPC(DRR, LOGL_INFO, "\n");
623
624 return n;
Harald Welte6c40def2009-12-14 22:07:14 +0100625}
626
Max6f0e50c2017-04-12 15:30:54 +0200627static int generate_si1(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530628{
629 int rc;
Max6f0e50c2017-04-12 15:30:54 +0200630 struct gsm48_system_information_type_1 *si1 = (struct gsm48_system_information_type_1 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +0530631
632 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
633
Max2440f492016-12-07 18:03:03 +0100634 si1->header.l2_plen = GSM48_LEN2PLEN(21);
Harald Weltea43f7892009-12-01 18:04:30 +0530635 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
636 si1->header.skip_indicator = 0;
637 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
638
639 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
640 if (rc < 0)
641 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100642 list_arfcn(si1->cell_channel_description, 0xce, "Serving cell:");
Harald Weltea43f7892009-12-01 18:04:30 +0530643
644 si1->rach_control = bts->si_common.rach_control;
645
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +0100646 /*
647 * SI1 Rest Octets (10.5.2.32), contains NCH position and band
648 * indicator but that is not in the 04.08.
649 */
650 rc = rest_octets_si1(si1->rest_octets, NULL, is_dcs_net(bts));
Harald Welte7401ae62010-06-15 16:44:12 +0200651
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100652 return sizeof(*si1) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530653}
654
Max6f0e50c2017-04-12 15:30:54 +0200655static int generate_si2(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530656{
657 int rc;
Max6f0e50c2017-04-12 15:30:54 +0200658 struct gsm48_system_information_type_2 *si2 = (struct gsm48_system_information_type_2 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +0530659
660 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
661
Max2440f492016-12-07 18:03:03 +0100662 si2->header.l2_plen = GSM48_LEN2PLEN(22);
Harald Weltea43f7892009-12-01 18:04:30 +0530663 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
664 si2->header.skip_indicator = 0;
665 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
666
Max5fa7e362016-04-15 16:04:45 +0200667 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts, false, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530668 if (rc < 0)
669 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100670 list_arfcn(si2->bcch_frequency_list, 0xce,
Harald Weltee5ba92e2015-09-04 06:22:46 +0200671 "SI2 Neighbour cells in same band:");
Harald Weltea43f7892009-12-01 18:04:30 +0530672
673 si2->ncc_permitted = bts->si_common.ncc_permitted;
674 si2->rach_control = bts->si_common.rach_control;
675
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100676 return sizeof(*si2);
Harald Weltea43f7892009-12-01 18:04:30 +0530677}
678
Max6f0e50c2017-04-12 15:30:54 +0200679static int generate_si2bis(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100680{
681 int rc;
682 struct gsm48_system_information_type_2bis *si2b =
Max6f0e50c2017-04-12 15:30:54 +0200683 (struct gsm48_system_information_type_2bis *) GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100684 int n;
685
686 memset(si2b, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
687
Max2440f492016-12-07 18:03:03 +0100688 si2b->header.l2_plen = GSM48_LEN2PLEN(22);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100689 si2b->header.rr_protocol_discriminator = GSM48_PDISC_RR;
690 si2b->header.skip_indicator = 0;
691 si2b->header.system_information = GSM48_MT_RR_SYSINFO_2bis;
692
Max5fa7e362016-04-15 16:04:45 +0200693 rc = generate_bcch_chan_list(si2b->bcch_frequency_list, bts, false, true, false);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100694 if (rc < 0)
695 return rc;
696 n = list_arfcn(si2b->bcch_frequency_list, 0xce,
697 "Neighbour cells in same band, but outside P-GSM:");
698 if (n) {
699 /* indicate in SI2 and SI2bis: there is an extension */
700 struct gsm48_system_information_type_2 *si2 =
Max6f0e50c2017-04-12 15:30:54 +0200701 (struct gsm48_system_information_type_2 *) GSM_BTS_SI(bts, SYSINFO_TYPE_2);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100702 si2->bcch_frequency_list[0] |= 0x20;
703 si2b->bcch_frequency_list[0] |= 0x20;
704 } else
705 bts->si_valid &= ~(1 << SYSINFO_TYPE_2bis);
706
707 si2b->rach_control = bts->si_common.rach_control;
708
709 return sizeof(*si2b);
710}
711
Max6f0e50c2017-04-12 15:30:54 +0200712static int generate_si2ter(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100713{
714 int rc;
715 struct gsm48_system_information_type_2ter *si2t =
Max6f0e50c2017-04-12 15:30:54 +0200716 (struct gsm48_system_information_type_2ter *) GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100717 int n;
718
719 memset(si2t, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
720
Max2440f492016-12-07 18:03:03 +0100721 si2t->header.l2_plen = GSM48_LEN2PLEN(22);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100722 si2t->header.rr_protocol_discriminator = GSM48_PDISC_RR;
723 si2t->header.skip_indicator = 0;
724 si2t->header.system_information = GSM48_MT_RR_SYSINFO_2ter;
725
Max5fa7e362016-04-15 16:04:45 +0200726 rc = generate_bcch_chan_list(si2t->ext_bcch_frequency_list, bts, false, false, true);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100727 if (rc < 0)
728 return rc;
729 n = list_arfcn(si2t->ext_bcch_frequency_list, 0x8e,
730 "Neighbour cells in different band:");
731 if (!n)
732 bts->si_valid &= ~(1 << SYSINFO_TYPE_2ter);
733
734 return sizeof(*si2t);
735}
736
Maxf39d03a2017-05-12 17:00:30 +0200737/* SI2quater messages are optional - we only generate them when neighbor UARFCNs or EARFCNs are configured */
738static inline bool si2quater_not_needed(struct gsm_bts *bts)
739{
740 unsigned i = MAX_EARFCN_LIST;
741
742 if (bts->si_common.si2quater_neigh_list.arfcn)
743 for (i = 0; i < MAX_EARFCN_LIST; i++)
744 if (bts->si_common.si2quater_neigh_list.arfcn[i] != OSMO_EARFCN_INVALID)
745 break;
746
747 if (!bts->si_common.uarfcn_length && i == MAX_EARFCN_LIST) {
748 bts->si_valid &= ~(1 << SYSINFO_TYPE_2quater); /* mark SI2q as invalid if no (E|U)ARFCNs are present */
749 return true;
750 }
751
752 return false;
753}
754
Max6f0e50c2017-04-12 15:30:54 +0200755static int generate_si2quater(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Max59a1bf32016-04-15 16:04:46 +0200756{
Maxf39d03a2017-05-12 17:00:30 +0200757 int rc;
Max70fdd242017-06-15 15:10:53 +0200758 struct gsm48_system_information_type_2quater *si2q;
Maxf39d03a2017-05-12 17:00:30 +0200759
760 if (si2quater_not_needed(bts)) /* generate rest_octets for SI2q only when necessary */
761 return GSM_MACBLOCK_LEN;
Max59a1bf32016-04-15 16:04:46 +0200762
Max70fdd242017-06-15 15:10:53 +0200763 bts->u_offset = 0;
764 bts->e_offset = 0;
765 bts->si2q_index = 0;
766 bts->si2q_count = si2q_num(bts) - 1;
Max59a1bf32016-04-15 16:04:46 +0200767
Max70fdd242017-06-15 15:10:53 +0200768 rc = make_si2quaters(bts, false);
Max59a1bf32016-04-15 16:04:46 +0200769 if (rc < 0)
770 return rc;
771
Max70fdd242017-06-15 15:10:53 +0200772 OSMO_ASSERT(bts->si2q_count == bts->si2q_index);
773 OSMO_ASSERT(bts->si2q_count <= SI2Q_MAX_NUM);
774
Max59a1bf32016-04-15 16:04:46 +0200775 return sizeof(*si2q) + rc;
776}
777
Harald Welte1f893292010-03-14 23:46:48 +0800778static struct gsm48_si_ro_info si_info = {
Harald Weltea43f7892009-12-01 18:04:30 +0530779 .selection_params = {
780 .present = 0,
781 },
782 .power_offset = {
783 .present = 0,
784 },
785 .si2ter_indicator = 0,
786 .early_cm_ctrl = 1,
787 .scheduling = {
788 .present = 0,
789 },
790 .gprs_ind = {
791 .si13_position = 0,
792 .ra_colour = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800793 .present = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530794 },
Maxf3f35052016-04-15 16:04:44 +0200795 .si2quater_indicator = 0,
Harald Weltea43f7892009-12-01 18:04:30 +0530796 .lsa_params = {
797 .present = 0,
798 },
799 .cell_id = 0, /* FIXME: doesn't the bts have this? */
800 .break_ind = 0,
801};
802
Max6f0e50c2017-04-12 15:30:54 +0200803static int generate_si3(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530804{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100805 int rc;
Max6f0e50c2017-04-12 15:30:54 +0200806 struct gsm48_system_information_type_3 *si3 = (struct gsm48_system_information_type_3 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +0530807
808 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
809
Max2440f492016-12-07 18:03:03 +0100810 si3->header.l2_plen = GSM48_LEN2PLEN(18);
Harald Weltea43f7892009-12-01 18:04:30 +0530811 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
812 si3->header.skip_indicator = 0;
813 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
814
815 si3->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100816 gsm48_generate_lai(&si3->lai, bts->network->country_code,
817 bts->network->network_code,
818 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530819 si3->control_channel_desc = bts->si_common.chan_desc;
820 si3->cell_options = bts->si_common.cell_options;
821 si3->cell_sel_par = bts->si_common.cell_sel_par;
822 si3->rach_control = bts->si_common.rach_control;
823
Maxc08ee712016-05-11 12:45:13 +0200824 /* allow/disallow DTXu */
825 gsm48_set_dtx(&si3->cell_options, bts->dtxu, bts->dtxu, true);
826
Max9b97b002017-06-02 10:58:26 +0200827 if (GSM_BTS_HAS_SI(bts, SYSINFO_TYPE_2ter)) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100828 LOGP(DRR, LOGL_INFO, "SI 2ter is included.\n");
829 si_info.si2ter_indicator = 1;
830 } else {
831 si_info.si2ter_indicator = 0;
832 }
Max9b97b002017-06-02 10:58:26 +0200833 if (GSM_BTS_HAS_SI(bts, SYSINFO_TYPE_2quater)) {
Max845a3a42017-05-15 12:02:29 +0200834 LOGP(DRR, LOGL_INFO, "SI 2quater is included, based on %zu EARFCNs and %zu UARFCNs.\n",
835 si2q_earfcn_count(&bts->si_common.si2quater_neigh_list), bts->si_common.uarfcn_length);
Maxf3f35052016-04-15 16:04:44 +0200836 si_info.si2quater_indicator = 1;
837 } else {
838 si_info.si2quater_indicator = 0;
839 }
Harald Welte42def722017-01-13 00:10:32 +0100840 si_info.early_cm_ctrl = bts->early_classmark_allowed;
841
Harald Weltea43f7892009-12-01 18:04:30 +0530842 /* SI3 Rest Octets (10.5.2.34), containing
843 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
844 Power Offset, 2ter Indicator, Early Classmark Sending,
845 Scheduling if and WHERE, GPRS Indicator, SI13 position */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100846 rc = rest_octets_si3(si3->rest_octets, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530847
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100848 return sizeof(*si3) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530849}
850
Max6f0e50c2017-04-12 15:30:54 +0200851static int generate_si4(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530852{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100853 int rc;
Max6f0e50c2017-04-12 15:30:54 +0200854 struct gsm48_system_information_type_4 *si4 = (struct gsm48_system_information_type_4 *) GSM_BTS_SI(bts, t);
Harald Welte30f1f372014-12-28 15:00:45 +0100855 struct gsm_lchan *cbch_lchan;
856 uint8_t *restoct = si4->data;
Harald Weltea43f7892009-12-01 18:04:30 +0530857
858 /* length of all IEs present except SI4 rest octets and l2_plen */
859 int l2_plen = sizeof(*si4) - 1;
860
861 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
862
863 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
864 si4->header.skip_indicator = 0;
865 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
866
Harald Welteafedeab2010-03-04 10:55:40 +0100867 gsm48_generate_lai(&si4->lai, bts->network->country_code,
868 bts->network->network_code,
869 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530870 si4->cell_sel_par = bts->si_common.cell_sel_par;
871 si4->rach_control = bts->si_common.rach_control;
872
873 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
Harald Welte142d12d2014-12-29 17:47:08 +0100874 cbch_lchan = gsm_bts_get_cbch(bts);
Harald Welte30f1f372014-12-28 15:00:45 +0100875 if (cbch_lchan) {
876 struct gsm48_chan_desc cd;
877 gsm48_lchan2chan_desc(&cd, cbch_lchan);
Daniel Willmann695675f2014-12-30 12:10:25 +0100878 tv_fixed_put(si4->data, GSM48_IE_CBCH_CHAN_DESC, 3,
Harald Welte30f1f372014-12-28 15:00:45 +0100879 (uint8_t *) &cd);
Daniel Willmann695675f2014-12-30 12:10:25 +0100880 l2_plen += 3 + 1;
881 restoct += 3 + 1;
Harald Welte30f1f372014-12-28 15:00:45 +0100882 /* we don't use hopping and thus don't need a CBCH MA */
883 }
Harald Weltea43f7892009-12-01 18:04:30 +0530884
Max2440f492016-12-07 18:03:03 +0100885 si4->header.l2_plen = GSM48_LEN2PLEN(l2_plen);
Harald Weltea43f7892009-12-01 18:04:30 +0530886
887 /* SI4 Rest Octets (10.5.2.35), containing
888 Optional Power offset, GPRS Indicator,
889 Cell Identity, LSA ID, Selection Parameter */
Max6f0e50c2017-04-12 15:30:54 +0200890 rc = rest_octets_si4(restoct, &si_info, (uint8_t *)GSM_BTS_SI(bts, t) + GSM_MACBLOCK_LEN - restoct);
Harald Weltea43f7892009-12-01 18:04:30 +0530891
Harald Welte30f1f372014-12-28 15:00:45 +0100892 return l2_plen + 1 + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530893}
894
Max6f0e50c2017-04-12 15:30:54 +0200895static int generate_si5(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530896{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100897 struct gsm48_system_information_type_5 *si5;
Max6f0e50c2017-04-12 15:30:54 +0200898 uint8_t *output = GSM_BTS_SI(bts, t);
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100899 int rc, l2_plen = 18;
Harald Weltea43f7892009-12-01 18:04:30 +0530900
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100901 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
902
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100903 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100904 switch (bts->type) {
905 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +0100906 case GSM_BTS_TYPE_OSMOBTS:
Max2440f492016-12-07 18:03:03 +0100907 *output++ = GSM48_LEN2PLEN(l2_plen);
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100908 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100909 break;
910 default:
911 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100912 }
913
Max6f0e50c2017-04-12 15:30:54 +0200914 si5 = (struct gsm48_system_information_type_5 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +0530915
916 /* l2 pseudo length, not part of msg: 18 */
917 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
918 si5->skip_indicator = 0;
919 si5->system_information = GSM48_MT_RR_SYSINFO_5;
Max5fa7e362016-04-15 16:04:45 +0200920 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts, true, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530921 if (rc < 0)
922 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100923 list_arfcn(si5->bcch_frequency_list, 0xce,
Harald Weltee5ba92e2015-09-04 06:22:46 +0200924 "SI5 Neighbour cells in same band:");
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100925
926 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
927 return l2_plen;
928}
929
Max6f0e50c2017-04-12 15:30:54 +0200930static int generate_si5bis(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100931{
932 struct gsm48_system_information_type_5bis *si5b;
Max6f0e50c2017-04-12 15:30:54 +0200933 uint8_t *output = GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100934 int rc, l2_plen = 18;
935 int n;
936
937 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
938
939 /* ip.access nanoBTS needs l2_plen!! */
940 switch (bts->type) {
941 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +0100942 case GSM_BTS_TYPE_OSMOBTS:
Max2440f492016-12-07 18:03:03 +0100943 *output++ = GSM48_LEN2PLEN(l2_plen);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100944 l2_plen++;
945 break;
946 default:
947 break;
948 }
949
Max6f0e50c2017-04-12 15:30:54 +0200950 si5b = (struct gsm48_system_information_type_5bis *) GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100951
952 /* l2 pseudo length, not part of msg: 18 */
953 si5b->rr_protocol_discriminator = GSM48_PDISC_RR;
954 si5b->skip_indicator = 0;
955 si5b->system_information = GSM48_MT_RR_SYSINFO_5bis;
Max5fa7e362016-04-15 16:04:45 +0200956 rc = generate_bcch_chan_list(si5b->bcch_frequency_list, bts, true, true, false);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100957 if (rc < 0)
958 return rc;
959 n = list_arfcn(si5b->bcch_frequency_list, 0xce,
960 "Neighbour cells in same band, but outside P-GSM:");
961 if (n) {
962 /* indicate in SI5 and SI5bis: there is an extension */
963 struct gsm48_system_information_type_5 *si5 =
Max6f0e50c2017-04-12 15:30:54 +0200964 (struct gsm48_system_information_type_5 *) GSM_BTS_SI(bts, SYSINFO_TYPE_5);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100965 si5->bcch_frequency_list[0] |= 0x20;
966 si5b->bcch_frequency_list[0] |= 0x20;
967 } else
968 bts->si_valid &= ~(1 << SYSINFO_TYPE_5bis);
969
970 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
971 return l2_plen;
972}
973
Max6f0e50c2017-04-12 15:30:54 +0200974static int generate_si5ter(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100975{
976 struct gsm48_system_information_type_5ter *si5t;
Max6f0e50c2017-04-12 15:30:54 +0200977 uint8_t *output = GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100978 int rc, l2_plen = 18;
979 int n;
980
981 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
982
983 /* ip.access nanoBTS needs l2_plen!! */
984 switch (bts->type) {
985 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +0100986 case GSM_BTS_TYPE_OSMOBTS:
Max2440f492016-12-07 18:03:03 +0100987 *output++ = GSM48_LEN2PLEN(l2_plen);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100988 l2_plen++;
989 break;
990 default:
991 break;
992 }
993
Max6f0e50c2017-04-12 15:30:54 +0200994 si5t = (struct gsm48_system_information_type_5ter *) GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100995
996 /* l2 pseudo length, not part of msg: 18 */
997 si5t->rr_protocol_discriminator = GSM48_PDISC_RR;
998 si5t->skip_indicator = 0;
999 si5t->system_information = GSM48_MT_RR_SYSINFO_5ter;
Max5fa7e362016-04-15 16:04:45 +02001000 rc = generate_bcch_chan_list(si5t->bcch_frequency_list, bts, true, false, true);
Andreas Eversbergc50543b2012-02-29 09:04:07 +01001001 if (rc < 0)
1002 return rc;
1003 n = list_arfcn(si5t->bcch_frequency_list, 0x8e,
1004 "Neighbour cells in different band:");
1005 if (!n)
1006 bts->si_valid &= ~(1 << SYSINFO_TYPE_5ter);
Harald Weltea43f7892009-12-01 18:04:30 +05301007
1008 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
Harald Weltee2d0d5f2009-12-19 21:26:54 +01001009 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +05301010}
1011
Max6f0e50c2017-04-12 15:30:54 +02001012static int generate_si6(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +05301013{
Harald Weltee2d0d5f2009-12-19 21:26:54 +01001014 struct gsm48_system_information_type_6 *si6;
Max6f0e50c2017-04-12 15:30:54 +02001015 uint8_t *output = GSM_BTS_SI(bts, t);
Harald Weltee2d0d5f2009-12-19 21:26:54 +01001016 int l2_plen = 11;
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +02001017 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +05301018
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +01001019 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
1020
Harald Weltee2d0d5f2009-12-19 21:26:54 +01001021 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +01001022 switch (bts->type) {
1023 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +01001024 case GSM_BTS_TYPE_OSMOBTS:
Max2440f492016-12-07 18:03:03 +01001025 *output++ = GSM48_LEN2PLEN(l2_plen);
Harald Weltee2d0d5f2009-12-19 21:26:54 +01001026 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +01001027 break;
1028 default:
1029 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +01001030 }
1031
Max6f0e50c2017-04-12 15:30:54 +02001032 si6 = (struct gsm48_system_information_type_6 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +05301033
1034 /* l2 pseudo length, not part of msg: 11 */
1035 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
1036 si6->skip_indicator = 0;
1037 si6->system_information = GSM48_MT_RR_SYSINFO_6;
1038 si6->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +01001039 gsm48_generate_lai(&si6->lai, bts->network->country_code,
1040 bts->network->network_code,
1041 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +05301042 si6->cell_options = bts->si_common.cell_options;
1043 si6->ncc_permitted = bts->si_common.ncc_permitted;
Maxc08ee712016-05-11 12:45:13 +02001044 /* allow/disallow DTXu */
Maxea8e9832016-05-23 17:28:13 +02001045 gsm48_set_dtx(&si6->cell_options, bts->dtxu, bts->dtxu, false);
Harald Weltea43f7892009-12-01 18:04:30 +05301046
1047 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +02001048 rc = rest_octets_si6(si6->rest_octets, is_dcs_net(bts));
Harald Weltea43f7892009-12-01 18:04:30 +05301049
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +02001050 return l2_plen + rc;
Harald Weltea43f7892009-12-01 18:04:30 +05301051}
1052
1053static struct gsm48_si13_info si13_default = {
1054 .cell_opts = {
Harald Welte85849182010-06-02 12:19:21 +02001055 .nmo = GPRS_NMO_II,
Harald Weltea4b16652010-05-31 12:54:23 +02001056 .t3168 = 2000,
Andreas Eversbergbecc89a2012-09-23 08:14:51 +02001057 .t3192 = 1500,
Harald Welte97a282b2010-03-14 15:37:43 +08001058 .drx_timer_max = 3,
Harald Weltea43f7892009-12-01 18:04:30 +05301059 .bs_cv_max = 15,
Max292ec582016-07-28 11:55:37 +02001060 .ctrl_ack_type_use_block = true,
Sylvain Munaut851f1202011-10-17 13:56:02 +02001061 .ext_info_present = 0,
bhargava350533c2016-07-21 11:14:34 +05301062 .supports_egprs_11bit_rach = 0,
Harald Welteda0586a2010-04-18 14:49:05 +02001063 .ext_info = {
1064 /* The values below are just guesses ! */
Harald Weltea06fea02010-04-18 15:53:40 +02001065 .egprs_supported = 0,
Harald Welteda0586a2010-04-18 14:49:05 +02001066 .use_egprs_p_ch_req = 1,
Harald Weltea4b16652010-05-31 12:54:23 +02001067 .bep_period = 5,
Harald Welteda0586a2010-04-18 14:49:05 +02001068 .pfc_supported = 0,
1069 .dtm_supported = 0,
1070 .bss_paging_coordination = 0,
1071 },
Harald Weltea43f7892009-12-01 18:04:30 +05301072 },
1073 .pwr_ctrl_pars = {
Harald Weltec15d0ac2012-10-08 21:22:08 +02001074 .alpha = 0, /* a = 0.0 */
Harald Weltea4b16652010-05-31 12:54:23 +02001075 .t_avg_w = 16,
1076 .t_avg_t = 16,
Harald Weltea43f7892009-12-01 18:04:30 +05301077 .pc_meas_chan = 0, /* downling measured on CCCH */
Harald Weltea4b16652010-05-31 12:54:23 +02001078 .n_avg_i = 8,
Harald Weltea43f7892009-12-01 18:04:30 +05301079 },
Harald Welte97a282b2010-03-14 15:37:43 +08001080 .bcch_change_mark = 1,
Harald Weltea43f7892009-12-01 18:04:30 +05301081 .si_change_field = 0,
1082 .pbcch_present = 0,
1083 {
1084 .no_pbcch = {
Harald Welte97a282b2010-03-14 15:37:43 +08001085 .rac = 0, /* needs to be patched */
Harald Weltea43f7892009-12-01 18:04:30 +05301086 .spgc_ccch_sup = 0,
1087 .net_ctrl_ord = 0,
Harald Welte97a282b2010-03-14 15:37:43 +08001088 .prio_acc_thr = 6,
Harald Weltea43f7892009-12-01 18:04:30 +05301089 },
1090 },
1091};
1092
Max6f0e50c2017-04-12 15:30:54 +02001093static int generate_si13(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +05301094{
1095 struct gsm48_system_information_type_13 *si13 =
Max6f0e50c2017-04-12 15:30:54 +02001096 (struct gsm48_system_information_type_13 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +05301097 int ret;
1098
1099 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
1100
1101 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
1102 si13->header.skip_indicator = 0;
1103 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
1104
Harald Welte97a282b2010-03-14 15:37:43 +08001105 si13_default.no_pbcch.rac = bts->gprs.rac;
Andreas Eversberg0c8f9ca2013-03-16 16:31:26 +01001106 si13_default.no_pbcch.net_ctrl_ord = bts->gprs.net_ctrl_ord;
Harald Welte97a282b2010-03-14 15:37:43 +08001107
Max292ec582016-07-28 11:55:37 +02001108 si13_default.cell_opts.ctrl_ack_type_use_block =
1109 bts->gprs.ctrl_ack_type_use_block;
1110
Holger Hans Peter Freytherb92a5382014-11-21 10:00:39 +01001111 /* Information about the other SIs */
1112 si13_default.bcch_change_mark = bts->bcch_change_mark;
bhargava350533c2016-07-21 11:14:34 +05301113 si13_default.cell_opts.supports_egprs_11bit_rach =
1114 bts->gprs.supports_egprs_11bit_rach;
Holger Hans Peter Freytherb92a5382014-11-21 10:00:39 +01001115
Harald Weltea43f7892009-12-01 18:04:30 +05301116 ret = rest_octets_si13(si13->rest_octets, &si13_default);
1117 if (ret < 0)
1118 return ret;
1119
Holger Hans Peter Freyther1c6f3942010-06-16 12:05:56 +08001120 /* length is coded in bit 2 an up */
1121 si13->header.l2_plen = 0x01;
Harald Weltea43f7892009-12-01 18:04:30 +05301122
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +01001123 return sizeof (*si13) + ret;
Harald Weltea43f7892009-12-01 18:04:30 +05301124}
1125
Max6f0e50c2017-04-12 15:30:54 +02001126typedef int (*gen_si_fn_t)(enum osmo_sysinfo_type t, struct gsm_bts *bts);
Harald Welte7401ae62010-06-15 16:44:12 +02001127
1128static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
1129 [SYSINFO_TYPE_1] = &generate_si1,
1130 [SYSINFO_TYPE_2] = &generate_si2,
Andreas Eversbergc50543b2012-02-29 09:04:07 +01001131 [SYSINFO_TYPE_2bis] = &generate_si2bis,
1132 [SYSINFO_TYPE_2ter] = &generate_si2ter,
Max59a1bf32016-04-15 16:04:46 +02001133 [SYSINFO_TYPE_2quater] = &generate_si2quater,
Harald Welte7401ae62010-06-15 16:44:12 +02001134 [SYSINFO_TYPE_3] = &generate_si3,
1135 [SYSINFO_TYPE_4] = &generate_si4,
1136 [SYSINFO_TYPE_5] = &generate_si5,
Andreas Eversbergc50543b2012-02-29 09:04:07 +01001137 [SYSINFO_TYPE_5bis] = &generate_si5bis,
1138 [SYSINFO_TYPE_5ter] = &generate_si5ter,
Harald Welte7401ae62010-06-15 16:44:12 +02001139 [SYSINFO_TYPE_6] = &generate_si6,
1140 [SYSINFO_TYPE_13] = &generate_si13,
1141};
1142
Harald Welte7401ae62010-06-15 16:44:12 +02001143int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
1144{
1145 gen_si_fn_t gen_si;
1146
Harald Welte4511d892010-04-18 15:51:20 +02001147 switch (bts->gprs.mode) {
1148 case BTS_GPRS_EGPRS:
Harald Weltea06fea02010-04-18 15:53:40 +02001149 si13_default.cell_opts.ext_info_present = 1;
1150 si13_default.cell_opts.ext_info.egprs_supported = 1;
1151 /* fallthrough */
Harald Welte4511d892010-04-18 15:51:20 +02001152 case BTS_GPRS_GPRS:
1153 si_info.gprs_ind.present = 1;
1154 break;
1155 case BTS_GPRS_NONE:
1156 si_info.gprs_ind.present = 0;
1157 break;
1158 }
Harald Welte1f893292010-03-14 23:46:48 +08001159
Sylvain Munaute0b06b02010-11-28 18:17:28 +01001160 memcpy(&si_info.selection_params,
1161 &bts->si_common.cell_ro_sel_par,
1162 sizeof(struct gsm48_si_selection_params));
1163
Harald Welte7401ae62010-06-15 16:44:12 +02001164 gen_si = gen_si_fn[si_type];
1165 if (!gen_si)
Harald Weltea43f7892009-12-01 18:04:30 +05301166 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +05301167
Max6f0e50c2017-04-12 15:30:54 +02001168 return gen_si(si_type, bts);
Harald Weltea43f7892009-12-01 18:04:30 +05301169}