blob: 644bebdf45f057c5806d744221c3ceb70a041a2a [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
Maxf39d03a2017-05-12 17:00:30 +0200125static inline unsigned earfcn_size(const struct gsm_bts *bts)
Max26679e02016-04-20 15:57:13 +0200126{
Maxf39d03a2017-05-12 17:00:30 +0200127 const struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list; /* EARFCN */
128
Max26679e02016-04-20 15:57:13 +0200129 /* account for all the constant bits in append_earfcn() */
Maxf39d03a2017-05-12 17:00:30 +0200130 return 25 + osmo_earfcn_bit_size_ext(e, bts->e_offset);
Max26679e02016-04-20 15:57:13 +0200131}
132
Maxf39d03a2017-05-12 17:00:30 +0200133static inline unsigned uarfcn_size(const struct gsm_bts *bts)
Max26679e02016-04-20 15:57:13 +0200134{
Maxf39d03a2017-05-12 17:00:30 +0200135 const uint16_t *u = bts->si_common.data.uarfcn_list;
136 uint16_t cu = u[bts->u_offset]; /* UARFCN */
137 /* account for all the constant bits in append_uarfcns() */
Maxe610e702016-12-19 13:41:48 +0100138 unsigned s = 7, append = 22, r = 0, i, st = 0, j, k;
Maxe610e702016-12-19 13:41:48 +0100139
Maxf39d03a2017-05-12 17:00:30 +0200140 for (i = bts->u_offset; i < bts->si_common.uarfcn_length; i++) {
Maxe610e702016-12-19 13:41:48 +0100141 for (j = st, k = 0; j < i; j++, k++);
142 if (u[i] != cu) { /* we've reached new UARFCN */
143 r += (append + range1024_p(k));
144 cu = u[i];
145 st = i; /* update start position */
146 }
147 }
148
149 /* add last UARFCN not covered by previous cycle */
Maxf39d03a2017-05-12 17:00:30 +0200150 for (i = st, k = 0; i < bts->si_common.uarfcn_length; i++, k++);
Maxe610e702016-12-19 13:41:48 +0100151
152 return s + r + append + range1024_p(k);
Max26679e02016-04-20 15:57:13 +0200153}
154
Maxf39d03a2017-05-12 17:00:30 +0200155uint8_t si2q_num(struct gsm_bts *bts)
Maxaafff962016-04-20 15:57:14 +0200156{
Maxf39d03a2017-05-12 17:00:30 +0200157 size_t est, e_sz = 1, u_sz = 1;
158
159 if (&bts->si_common.si2quater_neigh_list) /* EARFCN */
160 e_sz = earfcn_size(bts);
161
162 if (bts->si_common.uarfcn_length) /* UARFCN */
163 u_sz = uarfcn_size(bts);
164
Maxaafff962016-04-20 15:57:14 +0200165 /* 2 bits are used in between UARFCN and EARFCN structs */
Maxf39d03a2017-05-12 17:00:30 +0200166 est = 1 + (e_sz + u_sz) / (SI2Q_MAX_LEN - (SI2Q_MIN_LEN + 2));
167
168 return est;
Maxaafff962016-04-20 15:57:14 +0200169}
170
Max26679e02016-04-20 15:57:13 +0200171/* 3GPP TS 44.018, Table 9.1.54.1 - prepend diversity bit to scrambling code */
Max34be86b2016-12-16 18:45:51 +0100172static inline uint16_t encode_fdd(uint16_t scramble, bool diversity)
Max26679e02016-04-20 15:57:13 +0200173{
174 if (diversity)
175 return scramble | (1 << 9);
176 return scramble;
177}
178
179int bts_uarfcn_del(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble)
180{
181 uint16_t sc0 = encode_fdd(scramble, false), sc1 = encode_fdd(scramble, true),
182 *ual = bts->si_common.data.uarfcn_list,
183 *scl = bts->si_common.data.scramble_list;
184 size_t len = bts->si_common.uarfcn_length, i;
185 for (i = 0; i < len; i++) {
186 if (arfcn == ual[i] && (sc0 == scl[i] || sc1 == scl[i])) {
187 /* we rely on the assumption that (uarfcn, scramble)
188 tuple is unique in the lists */
189 if (i != len - 1) { /* move the tail if necessary */
190 memmove(ual + i, ual + i + 1, 2 * (len - i + 1));
191 memmove(scl + i, scl + i + 1, 2 * (len - i + 1));
192 }
193 break;
194 }
195 }
196
197 if (i == len)
198 return -EINVAL;
199
200 bts->si_common.uarfcn_length--;
201 return 0;
202}
203
Maxf39d03a2017-05-12 17:00:30 +0200204int bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble, bool diversity)
Max26679e02016-04-20 15:57:13 +0200205{
Maxe610e702016-12-19 13:41:48 +0100206 size_t len = bts->si_common.uarfcn_length, i, k = 0;
Max26679e02016-04-20 15:57:13 +0200207 uint16_t scr, chk,
208 *ual = bts->si_common.data.uarfcn_list,
209 *scl = bts->si_common.data.scramble_list,
210 scramble1 = encode_fdd(scramble, true),
211 scramble0 = encode_fdd(scramble, false);
212
213 scr = diversity ? scramble1 : scramble0;
214 chk = diversity ? scramble0 : scramble1;
215
216 if (len == MAX_EARFCN_LIST)
217 return -ENOMEM;
218
Maxe610e702016-12-19 13:41:48 +0100219 for (i = 0; i < len; i++) /* find the position of arfcn if any */
220 if (arfcn == ual[i])
221 break;
222
223 for (k = 0; i < len; i++) {
Max26679e02016-04-20 15:57:13 +0200224 if (arfcn == ual[i] && (scr == scl[i] || chk == scl[i]))
225 return -EADDRINUSE;
226 if (scr > scl[i])
227 k = i + 1;
228 }
229 /* we keep lists sorted by scramble code:
230 insert into appropriate position and move the tail */
231 if (len - k) {
232 memmove(ual + k + 1, ual + k, (len - k) * 2);
233 memmove(scl + k + 1, scl + k, (len - k) * 2);
234 }
Maxe610e702016-12-19 13:41:48 +0100235
Max26679e02016-04-20 15:57:13 +0200236 ual[k] = arfcn;
237 scl[k] = scr;
238 bts->si_common.uarfcn_length++;
Maxaafff962016-04-20 15:57:14 +0200239
Maxf39d03a2017-05-12 17:00:30 +0200240 if (si2q_num(bts) < 2) /* FIXME: use SI2Q_MAX_NUM */
Maxaafff962016-04-20 15:57:14 +0200241 return 0;
242
243 bts_uarfcn_del(bts, arfcn, scramble);
244 return -ENOSPC;
Max26679e02016-04-20 15:57:13 +0200245}
246
Max5fa7e362016-04-15 16:04:45 +0200247static inline int use_arfcn(const struct gsm_bts *bts, const bool bis, const bool ter,
248 const bool pgsm, const int arfcn)
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100249{
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100250 if (bts->force_combined_si)
251 return !bis && !ter;
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100252 if (!bis && !ter && band_compatible(bts, arfcn))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100253 return 1;
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100254 /* Correct but somehow broken with either the nanoBTS or the iPhone5 */
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100255 if (bis && pgsm && band_compatible(bts, arfcn) && (arfcn < 1 || arfcn > 124))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100256 return 1;
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100257 if (ter && !band_compatible(bts, arfcn))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100258 return 1;
259 return 0;
260}
261
Harald Welteda760d32009-12-14 20:25:05 +0100262/* Frequency Lists as per TS 04.08 10.5.2.13 */
263
264/* 10.5.2.13.2: Bit map 0 format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200265static int freq_list_bm0_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +0530266{
267 unsigned int byte, bit;
268
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100269 if (arfcn > 124 || arfcn < 1) {
270 LOGP(DRR, LOGL_ERROR, "Bitmap 0 only supports ARFCN 1...124\n");
Harald Weltea43f7892009-12-01 18:04:30 +0530271 return -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100272 }
Harald Weltea43f7892009-12-01 18:04:30 +0530273
Harald Welteda760d32009-12-14 20:25:05 +0100274 /* the bitmask is from 1..124, not from 0..123 */
275 arfcn--;
276
Harald Weltea43f7892009-12-01 18:04:30 +0530277 byte = arfcn / 8;
278 bit = arfcn % 8;
279
Harald Welteda760d32009-12-14 20:25:05 +0100280 chan_list[GSM48_CELL_CHAN_DESC_SIZE-1-byte] |= (1 << bit);
Harald Weltea43f7892009-12-01 18:04:30 +0530281
282 return 0;
283}
284
Harald Welteda760d32009-12-14 20:25:05 +0100285/* 10.5.2.13.7: Variable bit map format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200286static int freq_list_bmrel_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +0530287{
288 unsigned int byte, bit;
289 unsigned int min_arfcn;
290 unsigned int bitno;
291
292 min_arfcn = (chan_list[0] & 1) << 9;
293 min_arfcn |= chan_list[1] << 1;
294 min_arfcn |= (chan_list[2] >> 7) & 1;
295
296 /* The lower end of our bitmaks is always implicitly included */
297 if (arfcn == min_arfcn)
298 return 0;
299
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100300 if (((arfcn - min_arfcn) & 1023) > 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100301 LOGP(DRR, LOGL_ERROR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +0530302 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +0100303 }
Harald Weltea43f7892009-12-01 18:04:30 +0530304
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100305 bitno = (arfcn - min_arfcn) & 1023;
Harald Weltea43f7892009-12-01 18:04:30 +0530306 byte = bitno / 8;
307 bit = bitno % 8;
308
309 chan_list[2 + byte] |= 1 << (7 - bit);
310
311 return 0;
312}
313
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200314/* generate a variable bitmap */
Max5fa7e362016-04-15 16:04:45 +0200315static inline int enc_freq_lst_var_bitmap(uint8_t *chan_list,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200316 struct bitvec *bv, const struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200317 bool bis, bool ter, int min, bool pgsm)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200318{
319 int i;
320
321 /* set it to 'Variable bitmap format' */
322 chan_list[0] = 0x8e;
323
324 chan_list[0] |= (min >> 9) & 1;
325 chan_list[1] = (min >> 1);
326 chan_list[2] = (min & 1) << 7;
327
328 for (i = 0; i < bv->data_len*8; i++) {
329 /* see notes in bitvec2freq_list */
330 if (bitvec_get_bit_pos(bv, i)
Harald Weltee18f78e2015-09-04 06:21:32 +0200331 && ((!bis && !ter && band_compatible(bts,i))
332 || (bis && pgsm && band_compatible(bts,i) && (i < 1 || i > 124))
333 || (ter && !band_compatible(bts, i)))) {
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200334 int rc = freq_list_bmrel_set_arfcn(chan_list, i);
335 if (rc < 0)
336 return rc;
337 }
338 }
339
340 return 0;
341}
342
Max881064e2016-12-14 14:51:40 +0100343int range_encode(enum gsm48_range r, int *arfcns, int arfcns_used, int *w,
344 int f0, uint8_t *chan_list)
345{
346 /*
347 * Manipulate the ARFCN list according to the rules in J4 depending
348 * on the selected range.
349 */
350 int rc, f0_included;
351
352 range_enc_filter_arfcns(arfcns, arfcns_used, f0, &f0_included);
353
354 rc = range_enc_arfcns(r, arfcns, arfcns_used, w, 0);
355 if (rc < 0)
356 return rc;
357
358 /* Select the range and the amount of bits needed */
359 switch (r) {
360 case ARFCN_RANGE_128:
361 return range_enc_range128(chan_list, f0, w);
362 case ARFCN_RANGE_256:
363 return range_enc_range256(chan_list, f0, w);
364 case ARFCN_RANGE_512:
365 return range_enc_range512(chan_list, f0, w);
366 case ARFCN_RANGE_1024:
367 return range_enc_range1024(chan_list, f0, f0_included, w);
368 default:
369 return -ERANGE;
370 };
371
372 return f0_included;
373}
374
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200375/* generate a frequency list with the range 512 format */
Max5fa7e362016-04-15 16:04:45 +0200376static inline int enc_freq_lst_range(uint8_t *chan_list,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200377 struct bitvec *bv, const struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200378 bool bis, bool ter, bool pgsm)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200379{
380 int arfcns[RANGE_ENC_MAX_ARFCNS];
381 int w[RANGE_ENC_MAX_ARFCNS];
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200382 int arfcns_used = 0;
Max881064e2016-12-14 14:51:40 +0100383 int i, range, f0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200384
385 /*
386 * Select ARFCNs according to the rules in bitvec2freq_list
387 */
388 for (i = 0; i < bv->data_len * 8; ++i) {
389 /* More ARFCNs than the maximum */
390 if (arfcns_used > ARRAY_SIZE(arfcns))
391 return -1;
392 /* Check if we can select it? */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100393 if (bitvec_get_bit_pos(bv, i) && use_arfcn(bts, bis, ter, pgsm, i))
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200394 arfcns[arfcns_used++] = i;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200395 }
396
397 /*
398 * Check if the given list of ARFCNs can be encoded.
399 */
400 range = range_enc_determine_range(arfcns, arfcns_used, &f0);
401 if (range == ARFCN_RANGE_INVALID)
402 return -2;
403
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200404 memset(w, 0, sizeof(w));
Max881064e2016-12-14 14:51:40 +0100405 return range_encode(range, arfcns, arfcns_used, w, f0, chan_list);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200406}
407
Harald Weltea43f7892009-12-01 18:04:30 +0530408/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200409static int bitvec2freq_list(uint8_t *chan_list, struct bitvec *bv,
Max5fa7e362016-04-15 16:04:45 +0200410 const struct gsm_bts *bts, bool bis, bool ter)
Harald Weltea43f7892009-12-01 18:04:30 +0530411{
Max5fa7e362016-04-15 16:04:45 +0200412 int i, rc, min = -1, max = -1, arfcns = 0;
413 bool pgsm = false;
Harald Weltea43f7892009-12-01 18:04:30 +0530414 memset(chan_list, 0, 16);
415
Harald Welte29350342012-02-17 15:58:23 +0100416 if (bts->band == GSM_BAND_900
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100417 && bts->c0->arfcn >= 1 && bts->c0->arfcn <= 124)
Max5fa7e362016-04-15 16:04:45 +0200418 pgsm = true;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100419 /* P-GSM-only handsets only support 'bit map 0 format' */
420 if (!bis && !ter && pgsm) {
Harald Weltea43f7892009-12-01 18:04:30 +0530421 chan_list[0] = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100422
423 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100424 if (i >= 1 && i <= 124
425 && bitvec_get_bit_pos(bv, i)) {
Harald Welte6c40def2009-12-14 22:07:14 +0100426 rc = freq_list_bm0_set_arfcn(chan_list, i);
427 if (rc < 0)
428 return rc;
429 }
Harald Weltea43f7892009-12-01 18:04:30 +0530430 }
431 return 0;
432 }
433
Harald Welte6c40def2009-12-14 22:07:14 +0100434 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100435 /* in case of SI2 or SI5 allow all neighbours in same band
436 * in case of SI*bis, allow neighbours in same band ouside pgsm
437 * in case of SI*ter, allow neighbours in different bands
438 */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100439 if (!bitvec_get_bit_pos(bv, i))
440 continue;
441 if (!use_arfcn(bts, bis, ter, pgsm, i))
442 continue;
443 /* count the arfcns we want to carry */
444 arfcns += 1;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200445
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100446 /* 955..1023 < 0..885 */
447 if (min < 0)
448 min = i;
449 if (i >= 955 && min < 955)
450 min = i;
451 if (i >= 955 && min >= 955 && i < min)
452 min = i;
453 if (i < 955 && min < 955 && i < min)
454 min = i;
455 if (max < 0)
456 max = i;
457 if (i < 955 && max >= 955)
458 max = i;
459 if (i >= 955 && max >= 955 && i > max)
460 max = i;
461 if (i < 955 && max < 955 && i > max)
462 max = i;
Harald Weltea43f7892009-12-01 18:04:30 +0530463 }
464
Sylvain Munaut42a56522009-12-24 14:20:19 +0100465 if (max == -1) {
466 /* Empty set, use 'bit map 0 format' */
467 chan_list[0] = 0;
468 return 0;
469 }
470
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200471 /* Now find the best encoding */
472 if (((max - min) & 1023) <= 111)
473 return enc_freq_lst_var_bitmap(chan_list, bv, bts, bis,
474 ter, min, pgsm);
Harald Weltea43f7892009-12-01 18:04:30 +0530475
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200476 /* Attempt to do the range encoding */
477 rc = enc_freq_lst_range(chan_list, bv, bts, bis, ter, pgsm);
Max881064e2016-12-14 14:51:40 +0100478 if (rc >= 0)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200479 return 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530480
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200481 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, arfcns=%d "
482 "can not generate ARFCN list", min, max, arfcns);
483 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530484}
485
486/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Dieter Spaar16646022011-07-28 00:01:50 +0200487/* static*/ int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530488{
Harald Weltea43f7892009-12-01 18:04:30 +0530489 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100490 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530491
Harald Weltea39b0f22010-06-14 22:26:10 +0200492 /* Zero-initialize the bit-vector */
Harald Weltec8794b52010-06-16 11:09:18 +0200493 memset(bv->data, 0, bv->data_len);
Harald Weltea39b0f22010-06-14 22:26:10 +0200494
Harald Welte6c40def2009-12-14 22:07:14 +0100495 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
Harald Weltea39b0f22010-06-14 22:26:10 +0200496 llist_for_each_entry(trx, &bts->trx_list, list) {
497 unsigned int i, j;
498 /* Always add the TRX's ARFCN */
Harald Welte6c40def2009-12-14 22:07:14 +0100499 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea39b0f22010-06-14 22:26:10 +0200500 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
501 struct gsm_bts_trx_ts *ts = &trx->ts[i];
502 /* Add any ARFCNs present in hopping channels */
503 for (j = 0; j < 1024; j++) {
504 if (bitvec_get_bit_pos(&ts->hopping.arfcns, j))
505 bitvec_set_bit_pos(bv, j, 1);
506 }
507 }
508 }
Harald Weltea43f7892009-12-01 18:04:30 +0530509
Harald Welte6c40def2009-12-14 22:07:14 +0100510 /* then we generate a GSM 04.08 frequency list from the bitvec */
Max5fa7e362016-04-15 16:04:45 +0200511 return bitvec2freq_list(chan_list, bv, bts, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530512}
513
Harald Welte6c40def2009-12-14 22:07:14 +0100514/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100515static int generate_bcch_chan_list(uint8_t *chan_list, struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200516 bool si5, bool bis, bool ter)
Harald Welte6c40def2009-12-14 22:07:14 +0100517{
518 struct gsm_bts *cur_bts;
Harald Welte64c07d22011-02-15 11:43:27 +0100519 struct bitvec *bv;
520
521 if (si5 && bts->neigh_list_manual_mode == NL_MODE_MANUAL_SI5SEP)
522 bv = &bts->si_common.si5_neigh_list;
523 else
524 bv = &bts->si_common.neigh_list;
Harald Welte6c40def2009-12-14 22:07:14 +0100525
Harald Welte32c09622011-01-11 23:44:56 +0100526 /* Generate list of neighbor cells if we are in automatic mode */
Harald Welte64c07d22011-02-15 11:43:27 +0100527 if (bts->neigh_list_manual_mode == NL_MODE_AUTOMATIC) {
Harald Welte21bbda22011-02-19 20:43:37 +0100528 /* Zero-initialize the bit-vector */
529 memset(bv->data, 0, bv->data_len);
530
Harald Welte32c09622011-01-11 23:44:56 +0100531 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
532 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
533 if (cur_bts == bts)
534 continue;
535 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
536 }
Harald Welte6c40def2009-12-14 22:07:14 +0100537 }
538
539 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100540 return bitvec2freq_list(chan_list, bv, bts, bis, ter);
541}
542
543static int list_arfcn(uint8_t *chan_list, uint8_t mask, char *text)
544{
545 int n = 0, i;
546 struct gsm_sysinfo_freq freq[1024];
547
548 memset(freq, 0, sizeof(freq));
549 gsm48_decode_freq_list(freq, chan_list, 16, 0xce, 1);
550 for (i = 0; i < 1024; i++) {
551 if (freq[i].mask) {
552 if (!n)
553 LOGP(DRR, LOGL_INFO, "%s", text);
554 LOGPC(DRR, LOGL_INFO, " %d", i);
555 n++;
556 }
557 }
558 if (n)
559 LOGPC(DRR, LOGL_INFO, "\n");
560
561 return n;
Harald Welte6c40def2009-12-14 22:07:14 +0100562}
563
Max6f0e50c2017-04-12 15:30:54 +0200564static int generate_si1(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530565{
566 int rc;
Max6f0e50c2017-04-12 15:30:54 +0200567 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 +0530568
569 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
570
Max2440f492016-12-07 18:03:03 +0100571 si1->header.l2_plen = GSM48_LEN2PLEN(21);
Harald Weltea43f7892009-12-01 18:04:30 +0530572 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
573 si1->header.skip_indicator = 0;
574 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
575
576 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
577 if (rc < 0)
578 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100579 list_arfcn(si1->cell_channel_description, 0xce, "Serving cell:");
Harald Weltea43f7892009-12-01 18:04:30 +0530580
581 si1->rach_control = bts->si_common.rach_control;
582
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +0100583 /*
584 * SI1 Rest Octets (10.5.2.32), contains NCH position and band
585 * indicator but that is not in the 04.08.
586 */
587 rc = rest_octets_si1(si1->rest_octets, NULL, is_dcs_net(bts));
Harald Welte7401ae62010-06-15 16:44:12 +0200588
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100589 return sizeof(*si1) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530590}
591
Max6f0e50c2017-04-12 15:30:54 +0200592static int generate_si2(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530593{
594 int rc;
Max6f0e50c2017-04-12 15:30:54 +0200595 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 +0530596
597 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
598
Max2440f492016-12-07 18:03:03 +0100599 si2->header.l2_plen = GSM48_LEN2PLEN(22);
Harald Weltea43f7892009-12-01 18:04:30 +0530600 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
601 si2->header.skip_indicator = 0;
602 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
603
Max5fa7e362016-04-15 16:04:45 +0200604 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts, false, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530605 if (rc < 0)
606 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100607 list_arfcn(si2->bcch_frequency_list, 0xce,
Harald Weltee5ba92e2015-09-04 06:22:46 +0200608 "SI2 Neighbour cells in same band:");
Harald Weltea43f7892009-12-01 18:04:30 +0530609
610 si2->ncc_permitted = bts->si_common.ncc_permitted;
611 si2->rach_control = bts->si_common.rach_control;
612
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100613 return sizeof(*si2);
Harald Weltea43f7892009-12-01 18:04:30 +0530614}
615
Max6f0e50c2017-04-12 15:30:54 +0200616static int generate_si2bis(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100617{
618 int rc;
619 struct gsm48_system_information_type_2bis *si2b =
Max6f0e50c2017-04-12 15:30:54 +0200620 (struct gsm48_system_information_type_2bis *) GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100621 int n;
622
623 memset(si2b, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
624
Max2440f492016-12-07 18:03:03 +0100625 si2b->header.l2_plen = GSM48_LEN2PLEN(22);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100626 si2b->header.rr_protocol_discriminator = GSM48_PDISC_RR;
627 si2b->header.skip_indicator = 0;
628 si2b->header.system_information = GSM48_MT_RR_SYSINFO_2bis;
629
Max5fa7e362016-04-15 16:04:45 +0200630 rc = generate_bcch_chan_list(si2b->bcch_frequency_list, bts, false, true, false);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100631 if (rc < 0)
632 return rc;
633 n = list_arfcn(si2b->bcch_frequency_list, 0xce,
634 "Neighbour cells in same band, but outside P-GSM:");
635 if (n) {
636 /* indicate in SI2 and SI2bis: there is an extension */
637 struct gsm48_system_information_type_2 *si2 =
Max6f0e50c2017-04-12 15:30:54 +0200638 (struct gsm48_system_information_type_2 *) GSM_BTS_SI(bts, SYSINFO_TYPE_2);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100639 si2->bcch_frequency_list[0] |= 0x20;
640 si2b->bcch_frequency_list[0] |= 0x20;
641 } else
642 bts->si_valid &= ~(1 << SYSINFO_TYPE_2bis);
643
644 si2b->rach_control = bts->si_common.rach_control;
645
646 return sizeof(*si2b);
647}
648
Max6f0e50c2017-04-12 15:30:54 +0200649static int generate_si2ter(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100650{
651 int rc;
652 struct gsm48_system_information_type_2ter *si2t =
Max6f0e50c2017-04-12 15:30:54 +0200653 (struct gsm48_system_information_type_2ter *) GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100654 int n;
655
656 memset(si2t, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
657
Max2440f492016-12-07 18:03:03 +0100658 si2t->header.l2_plen = GSM48_LEN2PLEN(22);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100659 si2t->header.rr_protocol_discriminator = GSM48_PDISC_RR;
660 si2t->header.skip_indicator = 0;
661 si2t->header.system_information = GSM48_MT_RR_SYSINFO_2ter;
662
Max5fa7e362016-04-15 16:04:45 +0200663 rc = generate_bcch_chan_list(si2t->ext_bcch_frequency_list, bts, false, false, true);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100664 if (rc < 0)
665 return rc;
666 n = list_arfcn(si2t->ext_bcch_frequency_list, 0x8e,
667 "Neighbour cells in different band:");
668 if (!n)
669 bts->si_valid &= ~(1 << SYSINFO_TYPE_2ter);
670
671 return sizeof(*si2t);
672}
673
Maxf39d03a2017-05-12 17:00:30 +0200674/* SI2quater messages are optional - we only generate them when neighbor UARFCNs or EARFCNs are configured */
675static inline bool si2quater_not_needed(struct gsm_bts *bts)
676{
677 unsigned i = MAX_EARFCN_LIST;
678
679 if (bts->si_common.si2quater_neigh_list.arfcn)
680 for (i = 0; i < MAX_EARFCN_LIST; i++)
681 if (bts->si_common.si2quater_neigh_list.arfcn[i] != OSMO_EARFCN_INVALID)
682 break;
683
684 if (!bts->si_common.uarfcn_length && i == MAX_EARFCN_LIST) {
685 bts->si_valid &= ~(1 << SYSINFO_TYPE_2quater); /* mark SI2q as invalid if no (E|U)ARFCNs are present */
686 return true;
687 }
688
689 return false;
690}
691
692size_t si2q_earfcn_count(const struct osmo_earfcn_si2q *e)
693{
694 unsigned i, ret = 0;
Max845a3a42017-05-15 12:02:29 +0200695
696 if (!e)
697 return 0;
698
Maxf39d03a2017-05-12 17:00:30 +0200699 for (i = 0; i < e->length; i++)
700 if (e->arfcn[i] != OSMO_EARFCN_INVALID)
701 ret++;
702
703 return ret;
704}
705
Max6f0e50c2017-04-12 15:30:54 +0200706static int generate_si2quater(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Max59a1bf32016-04-15 16:04:46 +0200707{
Maxf39d03a2017-05-12 17:00:30 +0200708 int rc;
709 struct gsm48_system_information_type_2quater *si2q = GSM_BTS_SI2Q(bts);
710
711 if (si2quater_not_needed(bts)) /* generate rest_octets for SI2q only when necessary */
712 return GSM_MACBLOCK_LEN;
Max59a1bf32016-04-15 16:04:46 +0200713
714 memset(si2q, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
715
716 si2q->header.l2_plen = GSM48_LEN2PLEN(22);
717 si2q->header.rr_protocol_discriminator = GSM48_PDISC_RR;
718 si2q->header.skip_indicator = 0;
719 si2q->header.system_information = GSM48_MT_RR_SYSINFO_2quater;
720
Maxf39d03a2017-05-12 17:00:30 +0200721 rc = rest_octets_si2quater(si2q->rest_octets, bts);
Max59a1bf32016-04-15 16:04:46 +0200722 if (rc < 0)
723 return rc;
724
725 return sizeof(*si2q) + rc;
726}
727
Harald Welte1f893292010-03-14 23:46:48 +0800728static struct gsm48_si_ro_info si_info = {
Harald Weltea43f7892009-12-01 18:04:30 +0530729 .selection_params = {
730 .present = 0,
731 },
732 .power_offset = {
733 .present = 0,
734 },
735 .si2ter_indicator = 0,
736 .early_cm_ctrl = 1,
737 .scheduling = {
738 .present = 0,
739 },
740 .gprs_ind = {
741 .si13_position = 0,
742 .ra_colour = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800743 .present = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530744 },
Maxf3f35052016-04-15 16:04:44 +0200745 .si2quater_indicator = 0,
Harald Weltea43f7892009-12-01 18:04:30 +0530746 .lsa_params = {
747 .present = 0,
748 },
749 .cell_id = 0, /* FIXME: doesn't the bts have this? */
750 .break_ind = 0,
751};
752
Max6f0e50c2017-04-12 15:30:54 +0200753static int generate_si3(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530754{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100755 int rc;
Max6f0e50c2017-04-12 15:30:54 +0200756 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 +0530757
758 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
759
Max2440f492016-12-07 18:03:03 +0100760 si3->header.l2_plen = GSM48_LEN2PLEN(18);
Harald Weltea43f7892009-12-01 18:04:30 +0530761 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
762 si3->header.skip_indicator = 0;
763 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
764
765 si3->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100766 gsm48_generate_lai(&si3->lai, bts->network->country_code,
767 bts->network->network_code,
768 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530769 si3->control_channel_desc = bts->si_common.chan_desc;
770 si3->cell_options = bts->si_common.cell_options;
771 si3->cell_sel_par = bts->si_common.cell_sel_par;
772 si3->rach_control = bts->si_common.rach_control;
773
Maxc08ee712016-05-11 12:45:13 +0200774 /* allow/disallow DTXu */
775 gsm48_set_dtx(&si3->cell_options, bts->dtxu, bts->dtxu, true);
776
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100777 if ((bts->si_valid & (1 << SYSINFO_TYPE_2ter))) {
778 LOGP(DRR, LOGL_INFO, "SI 2ter is included.\n");
779 si_info.si2ter_indicator = 1;
780 } else {
781 si_info.si2ter_indicator = 0;
782 }
Maxf3f35052016-04-15 16:04:44 +0200783 if ((bts->si_valid & (1 << SYSINFO_TYPE_2quater))) {
Max845a3a42017-05-15 12:02:29 +0200784 LOGP(DRR, LOGL_INFO, "SI 2quater is included, based on %zu EARFCNs and %zu UARFCNs.\n",
785 si2q_earfcn_count(&bts->si_common.si2quater_neigh_list), bts->si_common.uarfcn_length);
Maxf3f35052016-04-15 16:04:44 +0200786 si_info.si2quater_indicator = 1;
787 } else {
788 si_info.si2quater_indicator = 0;
789 }
Harald Welte42def722017-01-13 00:10:32 +0100790 si_info.early_cm_ctrl = bts->early_classmark_allowed;
791
Harald Weltea43f7892009-12-01 18:04:30 +0530792 /* SI3 Rest Octets (10.5.2.34), containing
793 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
794 Power Offset, 2ter Indicator, Early Classmark Sending,
795 Scheduling if and WHERE, GPRS Indicator, SI13 position */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100796 rc = rest_octets_si3(si3->rest_octets, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530797
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100798 return sizeof(*si3) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530799}
800
Max6f0e50c2017-04-12 15:30:54 +0200801static int generate_si4(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530802{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100803 int rc;
Max6f0e50c2017-04-12 15:30:54 +0200804 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 +0100805 struct gsm_lchan *cbch_lchan;
806 uint8_t *restoct = si4->data;
Harald Weltea43f7892009-12-01 18:04:30 +0530807
808 /* length of all IEs present except SI4 rest octets and l2_plen */
809 int l2_plen = sizeof(*si4) - 1;
810
811 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
812
813 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
814 si4->header.skip_indicator = 0;
815 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
816
Harald Welteafedeab2010-03-04 10:55:40 +0100817 gsm48_generate_lai(&si4->lai, bts->network->country_code,
818 bts->network->network_code,
819 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530820 si4->cell_sel_par = bts->si_common.cell_sel_par;
821 si4->rach_control = bts->si_common.rach_control;
822
823 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
Harald Welte142d12d2014-12-29 17:47:08 +0100824 cbch_lchan = gsm_bts_get_cbch(bts);
Harald Welte30f1f372014-12-28 15:00:45 +0100825 if (cbch_lchan) {
826 struct gsm48_chan_desc cd;
827 gsm48_lchan2chan_desc(&cd, cbch_lchan);
Daniel Willmann695675f2014-12-30 12:10:25 +0100828 tv_fixed_put(si4->data, GSM48_IE_CBCH_CHAN_DESC, 3,
Harald Welte30f1f372014-12-28 15:00:45 +0100829 (uint8_t *) &cd);
Daniel Willmann695675f2014-12-30 12:10:25 +0100830 l2_plen += 3 + 1;
831 restoct += 3 + 1;
Harald Welte30f1f372014-12-28 15:00:45 +0100832 /* we don't use hopping and thus don't need a CBCH MA */
833 }
Harald Weltea43f7892009-12-01 18:04:30 +0530834
Max2440f492016-12-07 18:03:03 +0100835 si4->header.l2_plen = GSM48_LEN2PLEN(l2_plen);
Harald Weltea43f7892009-12-01 18:04:30 +0530836
837 /* SI4 Rest Octets (10.5.2.35), containing
838 Optional Power offset, GPRS Indicator,
839 Cell Identity, LSA ID, Selection Parameter */
Max6f0e50c2017-04-12 15:30:54 +0200840 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 +0530841
Harald Welte30f1f372014-12-28 15:00:45 +0100842 return l2_plen + 1 + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530843}
844
Max6f0e50c2017-04-12 15:30:54 +0200845static int generate_si5(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530846{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100847 struct gsm48_system_information_type_5 *si5;
Max6f0e50c2017-04-12 15:30:54 +0200848 uint8_t *output = GSM_BTS_SI(bts, t);
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100849 int rc, l2_plen = 18;
Harald Weltea43f7892009-12-01 18:04:30 +0530850
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100851 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
852
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100853 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100854 switch (bts->type) {
855 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +0100856 case GSM_BTS_TYPE_OSMOBTS:
Max2440f492016-12-07 18:03:03 +0100857 *output++ = GSM48_LEN2PLEN(l2_plen);
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100858 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100859 break;
860 default:
861 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100862 }
863
Max6f0e50c2017-04-12 15:30:54 +0200864 si5 = (struct gsm48_system_information_type_5 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +0530865
866 /* l2 pseudo length, not part of msg: 18 */
867 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
868 si5->skip_indicator = 0;
869 si5->system_information = GSM48_MT_RR_SYSINFO_5;
Max5fa7e362016-04-15 16:04:45 +0200870 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts, true, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530871 if (rc < 0)
872 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100873 list_arfcn(si5->bcch_frequency_list, 0xce,
Harald Weltee5ba92e2015-09-04 06:22:46 +0200874 "SI5 Neighbour cells in same band:");
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100875
876 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
877 return l2_plen;
878}
879
Max6f0e50c2017-04-12 15:30:54 +0200880static int generate_si5bis(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100881{
882 struct gsm48_system_information_type_5bis *si5b;
Max6f0e50c2017-04-12 15:30:54 +0200883 uint8_t *output = GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100884 int rc, l2_plen = 18;
885 int n;
886
887 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
888
889 /* ip.access nanoBTS needs l2_plen!! */
890 switch (bts->type) {
891 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +0100892 case GSM_BTS_TYPE_OSMOBTS:
Max2440f492016-12-07 18:03:03 +0100893 *output++ = GSM48_LEN2PLEN(l2_plen);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100894 l2_plen++;
895 break;
896 default:
897 break;
898 }
899
Max6f0e50c2017-04-12 15:30:54 +0200900 si5b = (struct gsm48_system_information_type_5bis *) GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100901
902 /* l2 pseudo length, not part of msg: 18 */
903 si5b->rr_protocol_discriminator = GSM48_PDISC_RR;
904 si5b->skip_indicator = 0;
905 si5b->system_information = GSM48_MT_RR_SYSINFO_5bis;
Max5fa7e362016-04-15 16:04:45 +0200906 rc = generate_bcch_chan_list(si5b->bcch_frequency_list, bts, true, true, false);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100907 if (rc < 0)
908 return rc;
909 n = list_arfcn(si5b->bcch_frequency_list, 0xce,
910 "Neighbour cells in same band, but outside P-GSM:");
911 if (n) {
912 /* indicate in SI5 and SI5bis: there is an extension */
913 struct gsm48_system_information_type_5 *si5 =
Max6f0e50c2017-04-12 15:30:54 +0200914 (struct gsm48_system_information_type_5 *) GSM_BTS_SI(bts, SYSINFO_TYPE_5);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100915 si5->bcch_frequency_list[0] |= 0x20;
916 si5b->bcch_frequency_list[0] |= 0x20;
917 } else
918 bts->si_valid &= ~(1 << SYSINFO_TYPE_5bis);
919
920 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
921 return l2_plen;
922}
923
Max6f0e50c2017-04-12 15:30:54 +0200924static int generate_si5ter(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100925{
926 struct gsm48_system_information_type_5ter *si5t;
Max6f0e50c2017-04-12 15:30:54 +0200927 uint8_t *output = GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100928 int rc, l2_plen = 18;
929 int n;
930
931 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
932
933 /* ip.access nanoBTS needs l2_plen!! */
934 switch (bts->type) {
935 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +0100936 case GSM_BTS_TYPE_OSMOBTS:
Max2440f492016-12-07 18:03:03 +0100937 *output++ = GSM48_LEN2PLEN(l2_plen);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100938 l2_plen++;
939 break;
940 default:
941 break;
942 }
943
Max6f0e50c2017-04-12 15:30:54 +0200944 si5t = (struct gsm48_system_information_type_5ter *) GSM_BTS_SI(bts, t);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100945
946 /* l2 pseudo length, not part of msg: 18 */
947 si5t->rr_protocol_discriminator = GSM48_PDISC_RR;
948 si5t->skip_indicator = 0;
949 si5t->system_information = GSM48_MT_RR_SYSINFO_5ter;
Max5fa7e362016-04-15 16:04:45 +0200950 rc = generate_bcch_chan_list(si5t->bcch_frequency_list, bts, true, false, true);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100951 if (rc < 0)
952 return rc;
953 n = list_arfcn(si5t->bcch_frequency_list, 0x8e,
954 "Neighbour cells in different band:");
955 if (!n)
956 bts->si_valid &= ~(1 << SYSINFO_TYPE_5ter);
Harald Weltea43f7892009-12-01 18:04:30 +0530957
958 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100959 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530960}
961
Max6f0e50c2017-04-12 15:30:54 +0200962static int generate_si6(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530963{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100964 struct gsm48_system_information_type_6 *si6;
Max6f0e50c2017-04-12 15:30:54 +0200965 uint8_t *output = GSM_BTS_SI(bts, t);
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100966 int l2_plen = 11;
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +0200967 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530968
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100969 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
970
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100971 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100972 switch (bts->type) {
973 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +0100974 case GSM_BTS_TYPE_OSMOBTS:
Max2440f492016-12-07 18:03:03 +0100975 *output++ = GSM48_LEN2PLEN(l2_plen);
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100976 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100977 break;
978 default:
979 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100980 }
981
Max6f0e50c2017-04-12 15:30:54 +0200982 si6 = (struct gsm48_system_information_type_6 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +0530983
984 /* l2 pseudo length, not part of msg: 11 */
985 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
986 si6->skip_indicator = 0;
987 si6->system_information = GSM48_MT_RR_SYSINFO_6;
988 si6->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100989 gsm48_generate_lai(&si6->lai, bts->network->country_code,
990 bts->network->network_code,
991 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530992 si6->cell_options = bts->si_common.cell_options;
993 si6->ncc_permitted = bts->si_common.ncc_permitted;
Maxc08ee712016-05-11 12:45:13 +0200994 /* allow/disallow DTXu */
Maxea8e9832016-05-23 17:28:13 +0200995 gsm48_set_dtx(&si6->cell_options, bts->dtxu, bts->dtxu, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530996
997 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +0200998 rc = rest_octets_si6(si6->rest_octets, is_dcs_net(bts));
Harald Weltea43f7892009-12-01 18:04:30 +0530999
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +02001000 return l2_plen + rc;
Harald Weltea43f7892009-12-01 18:04:30 +05301001}
1002
1003static struct gsm48_si13_info si13_default = {
1004 .cell_opts = {
Harald Welte85849182010-06-02 12:19:21 +02001005 .nmo = GPRS_NMO_II,
Harald Weltea4b16652010-05-31 12:54:23 +02001006 .t3168 = 2000,
Andreas Eversbergbecc89a2012-09-23 08:14:51 +02001007 .t3192 = 1500,
Harald Welte97a282b2010-03-14 15:37:43 +08001008 .drx_timer_max = 3,
Harald Weltea43f7892009-12-01 18:04:30 +05301009 .bs_cv_max = 15,
Max292ec582016-07-28 11:55:37 +02001010 .ctrl_ack_type_use_block = true,
Sylvain Munaut851f1202011-10-17 13:56:02 +02001011 .ext_info_present = 0,
bhargava350533c2016-07-21 11:14:34 +05301012 .supports_egprs_11bit_rach = 0,
Harald Welteda0586a2010-04-18 14:49:05 +02001013 .ext_info = {
1014 /* The values below are just guesses ! */
Harald Weltea06fea02010-04-18 15:53:40 +02001015 .egprs_supported = 0,
Harald Welteda0586a2010-04-18 14:49:05 +02001016 .use_egprs_p_ch_req = 1,
Harald Weltea4b16652010-05-31 12:54:23 +02001017 .bep_period = 5,
Harald Welteda0586a2010-04-18 14:49:05 +02001018 .pfc_supported = 0,
1019 .dtm_supported = 0,
1020 .bss_paging_coordination = 0,
1021 },
Harald Weltea43f7892009-12-01 18:04:30 +05301022 },
1023 .pwr_ctrl_pars = {
Harald Weltec15d0ac2012-10-08 21:22:08 +02001024 .alpha = 0, /* a = 0.0 */
Harald Weltea4b16652010-05-31 12:54:23 +02001025 .t_avg_w = 16,
1026 .t_avg_t = 16,
Harald Weltea43f7892009-12-01 18:04:30 +05301027 .pc_meas_chan = 0, /* downling measured on CCCH */
Harald Weltea4b16652010-05-31 12:54:23 +02001028 .n_avg_i = 8,
Harald Weltea43f7892009-12-01 18:04:30 +05301029 },
Harald Welte97a282b2010-03-14 15:37:43 +08001030 .bcch_change_mark = 1,
Harald Weltea43f7892009-12-01 18:04:30 +05301031 .si_change_field = 0,
1032 .pbcch_present = 0,
1033 {
1034 .no_pbcch = {
Harald Welte97a282b2010-03-14 15:37:43 +08001035 .rac = 0, /* needs to be patched */
Harald Weltea43f7892009-12-01 18:04:30 +05301036 .spgc_ccch_sup = 0,
1037 .net_ctrl_ord = 0,
Harald Welte97a282b2010-03-14 15:37:43 +08001038 .prio_acc_thr = 6,
Harald Weltea43f7892009-12-01 18:04:30 +05301039 },
1040 },
1041};
1042
Max6f0e50c2017-04-12 15:30:54 +02001043static int generate_si13(enum osmo_sysinfo_type t, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +05301044{
1045 struct gsm48_system_information_type_13 *si13 =
Max6f0e50c2017-04-12 15:30:54 +02001046 (struct gsm48_system_information_type_13 *) GSM_BTS_SI(bts, t);
Harald Weltea43f7892009-12-01 18:04:30 +05301047 int ret;
1048
1049 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
1050
1051 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
1052 si13->header.skip_indicator = 0;
1053 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
1054
Harald Welte97a282b2010-03-14 15:37:43 +08001055 si13_default.no_pbcch.rac = bts->gprs.rac;
Andreas Eversberg0c8f9ca2013-03-16 16:31:26 +01001056 si13_default.no_pbcch.net_ctrl_ord = bts->gprs.net_ctrl_ord;
Harald Welte97a282b2010-03-14 15:37:43 +08001057
Max292ec582016-07-28 11:55:37 +02001058 si13_default.cell_opts.ctrl_ack_type_use_block =
1059 bts->gprs.ctrl_ack_type_use_block;
1060
Holger Hans Peter Freytherb92a5382014-11-21 10:00:39 +01001061 /* Information about the other SIs */
1062 si13_default.bcch_change_mark = bts->bcch_change_mark;
bhargava350533c2016-07-21 11:14:34 +05301063 si13_default.cell_opts.supports_egprs_11bit_rach =
1064 bts->gprs.supports_egprs_11bit_rach;
Holger Hans Peter Freytherb92a5382014-11-21 10:00:39 +01001065
Harald Weltea43f7892009-12-01 18:04:30 +05301066 ret = rest_octets_si13(si13->rest_octets, &si13_default);
1067 if (ret < 0)
1068 return ret;
1069
Holger Hans Peter Freyther1c6f3942010-06-16 12:05:56 +08001070 /* length is coded in bit 2 an up */
1071 si13->header.l2_plen = 0x01;
Harald Weltea43f7892009-12-01 18:04:30 +05301072
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +01001073 return sizeof (*si13) + ret;
Harald Weltea43f7892009-12-01 18:04:30 +05301074}
1075
Max6f0e50c2017-04-12 15:30:54 +02001076typedef int (*gen_si_fn_t)(enum osmo_sysinfo_type t, struct gsm_bts *bts);
Harald Welte7401ae62010-06-15 16:44:12 +02001077
1078static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
1079 [SYSINFO_TYPE_1] = &generate_si1,
1080 [SYSINFO_TYPE_2] = &generate_si2,
Andreas Eversbergc50543b2012-02-29 09:04:07 +01001081 [SYSINFO_TYPE_2bis] = &generate_si2bis,
1082 [SYSINFO_TYPE_2ter] = &generate_si2ter,
Max59a1bf32016-04-15 16:04:46 +02001083 [SYSINFO_TYPE_2quater] = &generate_si2quater,
Harald Welte7401ae62010-06-15 16:44:12 +02001084 [SYSINFO_TYPE_3] = &generate_si3,
1085 [SYSINFO_TYPE_4] = &generate_si4,
1086 [SYSINFO_TYPE_5] = &generate_si5,
Andreas Eversbergc50543b2012-02-29 09:04:07 +01001087 [SYSINFO_TYPE_5bis] = &generate_si5bis,
1088 [SYSINFO_TYPE_5ter] = &generate_si5ter,
Harald Welte7401ae62010-06-15 16:44:12 +02001089 [SYSINFO_TYPE_6] = &generate_si6,
1090 [SYSINFO_TYPE_13] = &generate_si13,
1091};
1092
Harald Welte7401ae62010-06-15 16:44:12 +02001093int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
1094{
1095 gen_si_fn_t gen_si;
1096
Harald Welte4511d892010-04-18 15:51:20 +02001097 switch (bts->gprs.mode) {
1098 case BTS_GPRS_EGPRS:
Harald Weltea06fea02010-04-18 15:53:40 +02001099 si13_default.cell_opts.ext_info_present = 1;
1100 si13_default.cell_opts.ext_info.egprs_supported = 1;
1101 /* fallthrough */
Harald Welte4511d892010-04-18 15:51:20 +02001102 case BTS_GPRS_GPRS:
1103 si_info.gprs_ind.present = 1;
1104 break;
1105 case BTS_GPRS_NONE:
1106 si_info.gprs_ind.present = 0;
1107 break;
1108 }
Harald Welte1f893292010-03-14 23:46:48 +08001109
Sylvain Munaute0b06b02010-11-28 18:17:28 +01001110 memcpy(&si_info.selection_params,
1111 &bts->si_common.cell_ro_sel_par,
1112 sizeof(struct gsm48_si_selection_params));
1113
Harald Welte7401ae62010-06-15 16:44:12 +02001114 gen_si = gen_si_fn[si_type];
1115 if (!gen_si)
Harald Weltea43f7892009-12-01 18:04:30 +05301116 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +05301117
Max6f0e50c2017-04-12 15:30:54 +02001118 return gen_si(si_type, bts);
Harald Weltea43f7892009-12-01 18:04:30 +05301119}