blob: e71490eb7e7eee0e0372c40aff39a802d57db7b6 [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
125unsigned earfcn_size(const struct osmo_earfcn_si2q *e)
126{
127 /* account for all the constant bits in append_earfcn() */
128 return 25 + osmo_earfcn_bit_size(e);
129}
130
131unsigned uarfcn_size(const uint16_t *u, const uint16_t *sc, size_t u_len)
132{
Maxe610e702016-12-19 13:41:48 +0100133 /*account for all the constant bits in append_uarfcns() */
134 unsigned s = 7, append = 22, r = 0, i, st = 0, j, k;
135 uint16_t cu = u[0];
136
137 for (i = 0; i < u_len; i++) {
138 for (j = st, k = 0; j < i; j++, k++);
139 if (u[i] != cu) { /* we've reached new UARFCN */
140 r += (append + range1024_p(k));
141 cu = u[i];
142 st = i; /* update start position */
143 }
144 }
145
146 /* add last UARFCN not covered by previous cycle */
147 for (i = st, k = 0; i < u_len; i++, k++);
148
149 return s + r + append + range1024_p(k);
Max26679e02016-04-20 15:57:13 +0200150}
151
Maxaafff962016-04-20 15:57:14 +0200152bool si2q_size_check(const struct gsm_bts *bts)
153{
154 const struct osmo_earfcn_si2q *e = &bts->si_common.si2quater_neigh_list;
155 const uint16_t *u = bts->si_common.data.uarfcn_list,
156 *sc = bts->si_common.data.scramble_list;
157 size_t len = bts->si_common.uarfcn_length;
158 unsigned e_sz = e ? earfcn_size(e) : 1,
159 u_sz = len ? uarfcn_size(u, sc, len) : 1;
160 /* 2 bits are used in between UARFCN and EARFCN structs */
161 if (SI2Q_MIN_LEN + u_sz + 2 + e_sz > SI2Q_MAX_LEN)
162 return false;
163 return true;
164}
165
Max26679e02016-04-20 15:57:13 +0200166/* 3GPP TS 44.018, Table 9.1.54.1 - prepend diversity bit to scrambling code */
Max34be86b2016-12-16 18:45:51 +0100167static inline uint16_t encode_fdd(uint16_t scramble, bool diversity)
Max26679e02016-04-20 15:57:13 +0200168{
169 if (diversity)
170 return scramble | (1 << 9);
171 return scramble;
172}
173
174int bts_uarfcn_del(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble)
175{
176 uint16_t sc0 = encode_fdd(scramble, false), sc1 = encode_fdd(scramble, true),
177 *ual = bts->si_common.data.uarfcn_list,
178 *scl = bts->si_common.data.scramble_list;
179 size_t len = bts->si_common.uarfcn_length, i;
180 for (i = 0; i < len; i++) {
181 if (arfcn == ual[i] && (sc0 == scl[i] || sc1 == scl[i])) {
182 /* we rely on the assumption that (uarfcn, scramble)
183 tuple is unique in the lists */
184 if (i != len - 1) { /* move the tail if necessary */
185 memmove(ual + i, ual + i + 1, 2 * (len - i + 1));
186 memmove(scl + i, scl + i + 1, 2 * (len - i + 1));
187 }
188 break;
189 }
190 }
191
192 if (i == len)
193 return -EINVAL;
194
195 bts->si_common.uarfcn_length--;
196 return 0;
197}
198
199int bts_uarfcn_add(struct gsm_bts *bts, uint16_t arfcn, uint16_t scramble,
200 bool diversity)
201{
Maxe610e702016-12-19 13:41:48 +0100202 size_t len = bts->si_common.uarfcn_length, i, k = 0;
Max26679e02016-04-20 15:57:13 +0200203 uint16_t scr, chk,
204 *ual = bts->si_common.data.uarfcn_list,
205 *scl = bts->si_common.data.scramble_list,
206 scramble1 = encode_fdd(scramble, true),
207 scramble0 = encode_fdd(scramble, false);
208
209 scr = diversity ? scramble1 : scramble0;
210 chk = diversity ? scramble0 : scramble1;
211
212 if (len == MAX_EARFCN_LIST)
213 return -ENOMEM;
214
Maxe610e702016-12-19 13:41:48 +0100215 for (i = 0; i < len; i++) /* find the position of arfcn if any */
216 if (arfcn == ual[i])
217 break;
218
219 for (k = 0; i < len; i++) {
Max26679e02016-04-20 15:57:13 +0200220 if (arfcn == ual[i] && (scr == scl[i] || chk == scl[i]))
221 return -EADDRINUSE;
222 if (scr > scl[i])
223 k = i + 1;
224 }
225 /* we keep lists sorted by scramble code:
226 insert into appropriate position and move the tail */
227 if (len - k) {
228 memmove(ual + k + 1, ual + k, (len - k) * 2);
229 memmove(scl + k + 1, scl + k, (len - k) * 2);
230 }
Maxe610e702016-12-19 13:41:48 +0100231
Max26679e02016-04-20 15:57:13 +0200232 ual[k] = arfcn;
233 scl[k] = scr;
234 bts->si_common.uarfcn_length++;
Maxaafff962016-04-20 15:57:14 +0200235
236 if (si2q_size_check(bts))
237 return 0;
238
239 bts_uarfcn_del(bts, arfcn, scramble);
240 return -ENOSPC;
Max26679e02016-04-20 15:57:13 +0200241}
242
Max5fa7e362016-04-15 16:04:45 +0200243static inline int use_arfcn(const struct gsm_bts *bts, const bool bis, const bool ter,
244 const bool pgsm, const int arfcn)
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100245{
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100246 if (bts->force_combined_si)
247 return !bis && !ter;
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100248 if (!bis && !ter && band_compatible(bts, arfcn))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100249 return 1;
Jacob Erlbeck65d114f2014-01-16 11:02:14 +0100250 /* Correct but somehow broken with either the nanoBTS or the iPhone5 */
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100251 if (bis && pgsm && band_compatible(bts, arfcn) && (arfcn < 1 || arfcn > 124))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100252 return 1;
Holger Hans Peter Freytherd4d1d5e2013-01-17 13:49:39 +0100253 if (ter && !band_compatible(bts, arfcn))
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100254 return 1;
255 return 0;
256}
257
Harald Welteda760d32009-12-14 20:25:05 +0100258/* Frequency Lists as per TS 04.08 10.5.2.13 */
259
260/* 10.5.2.13.2: Bit map 0 format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200261static int freq_list_bm0_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +0530262{
263 unsigned int byte, bit;
264
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100265 if (arfcn > 124 || arfcn < 1) {
266 LOGP(DRR, LOGL_ERROR, "Bitmap 0 only supports ARFCN 1...124\n");
Harald Weltea43f7892009-12-01 18:04:30 +0530267 return -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100268 }
Harald Weltea43f7892009-12-01 18:04:30 +0530269
Harald Welteda760d32009-12-14 20:25:05 +0100270 /* the bitmask is from 1..124, not from 0..123 */
271 arfcn--;
272
Harald Weltea43f7892009-12-01 18:04:30 +0530273 byte = arfcn / 8;
274 bit = arfcn % 8;
275
Harald Welteda760d32009-12-14 20:25:05 +0100276 chan_list[GSM48_CELL_CHAN_DESC_SIZE-1-byte] |= (1 << bit);
Harald Weltea43f7892009-12-01 18:04:30 +0530277
278 return 0;
279}
280
Harald Welteda760d32009-12-14 20:25:05 +0100281/* 10.5.2.13.7: Variable bit map format */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200282static int freq_list_bmrel_set_arfcn(uint8_t *chan_list, unsigned int arfcn)
Harald Weltea43f7892009-12-01 18:04:30 +0530283{
284 unsigned int byte, bit;
285 unsigned int min_arfcn;
286 unsigned int bitno;
287
288 min_arfcn = (chan_list[0] & 1) << 9;
289 min_arfcn |= chan_list[1] << 1;
290 min_arfcn |= (chan_list[2] >> 7) & 1;
291
292 /* The lower end of our bitmaks is always implicitly included */
293 if (arfcn == min_arfcn)
294 return 0;
295
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100296 if (((arfcn - min_arfcn) & 1023) > 111) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100297 LOGP(DRR, LOGL_ERROR, "arfcn(%u) > min(%u) + 111\n", arfcn, min_arfcn);
Harald Weltea43f7892009-12-01 18:04:30 +0530298 return -EINVAL;
Harald Welte152b6262009-12-16 11:57:48 +0100299 }
Harald Weltea43f7892009-12-01 18:04:30 +0530300
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100301 bitno = (arfcn - min_arfcn) & 1023;
Harald Weltea43f7892009-12-01 18:04:30 +0530302 byte = bitno / 8;
303 bit = bitno % 8;
304
305 chan_list[2 + byte] |= 1 << (7 - bit);
306
307 return 0;
308}
309
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200310/* generate a variable bitmap */
Max5fa7e362016-04-15 16:04:45 +0200311static inline int enc_freq_lst_var_bitmap(uint8_t *chan_list,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200312 struct bitvec *bv, const struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200313 bool bis, bool ter, int min, bool pgsm)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200314{
315 int i;
316
317 /* set it to 'Variable bitmap format' */
318 chan_list[0] = 0x8e;
319
320 chan_list[0] |= (min >> 9) & 1;
321 chan_list[1] = (min >> 1);
322 chan_list[2] = (min & 1) << 7;
323
324 for (i = 0; i < bv->data_len*8; i++) {
325 /* see notes in bitvec2freq_list */
326 if (bitvec_get_bit_pos(bv, i)
Harald Weltee18f78e2015-09-04 06:21:32 +0200327 && ((!bis && !ter && band_compatible(bts,i))
328 || (bis && pgsm && band_compatible(bts,i) && (i < 1 || i > 124))
329 || (ter && !band_compatible(bts, i)))) {
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200330 int rc = freq_list_bmrel_set_arfcn(chan_list, i);
331 if (rc < 0)
332 return rc;
333 }
334 }
335
336 return 0;
337}
338
Max881064e2016-12-14 14:51:40 +0100339int range_encode(enum gsm48_range r, int *arfcns, int arfcns_used, int *w,
340 int f0, uint8_t *chan_list)
341{
342 /*
343 * Manipulate the ARFCN list according to the rules in J4 depending
344 * on the selected range.
345 */
346 int rc, f0_included;
347
348 range_enc_filter_arfcns(arfcns, arfcns_used, f0, &f0_included);
349
350 rc = range_enc_arfcns(r, arfcns, arfcns_used, w, 0);
351 if (rc < 0)
352 return rc;
353
354 /* Select the range and the amount of bits needed */
355 switch (r) {
356 case ARFCN_RANGE_128:
357 return range_enc_range128(chan_list, f0, w);
358 case ARFCN_RANGE_256:
359 return range_enc_range256(chan_list, f0, w);
360 case ARFCN_RANGE_512:
361 return range_enc_range512(chan_list, f0, w);
362 case ARFCN_RANGE_1024:
363 return range_enc_range1024(chan_list, f0, f0_included, w);
364 default:
365 return -ERANGE;
366 };
367
368 return f0_included;
369}
370
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200371/* generate a frequency list with the range 512 format */
Max5fa7e362016-04-15 16:04:45 +0200372static inline int enc_freq_lst_range(uint8_t *chan_list,
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200373 struct bitvec *bv, const struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200374 bool bis, bool ter, bool pgsm)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200375{
376 int arfcns[RANGE_ENC_MAX_ARFCNS];
377 int w[RANGE_ENC_MAX_ARFCNS];
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200378 int arfcns_used = 0;
Max881064e2016-12-14 14:51:40 +0100379 int i, range, f0;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200380
381 /*
382 * Select ARFCNs according to the rules in bitvec2freq_list
383 */
384 for (i = 0; i < bv->data_len * 8; ++i) {
385 /* More ARFCNs than the maximum */
386 if (arfcns_used > ARRAY_SIZE(arfcns))
387 return -1;
388 /* Check if we can select it? */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100389 if (bitvec_get_bit_pos(bv, i) && use_arfcn(bts, bis, ter, pgsm, i))
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200390 arfcns[arfcns_used++] = i;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200391 }
392
393 /*
394 * Check if the given list of ARFCNs can be encoded.
395 */
396 range = range_enc_determine_range(arfcns, arfcns_used, &f0);
397 if (range == ARFCN_RANGE_INVALID)
398 return -2;
399
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200400 memset(w, 0, sizeof(w));
Max881064e2016-12-14 14:51:40 +0100401 return range_encode(range, arfcns, arfcns_used, w, f0, chan_list);
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200402}
403
Harald Weltea43f7892009-12-01 18:04:30 +0530404/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200405static int bitvec2freq_list(uint8_t *chan_list, struct bitvec *bv,
Max5fa7e362016-04-15 16:04:45 +0200406 const struct gsm_bts *bts, bool bis, bool ter)
Harald Weltea43f7892009-12-01 18:04:30 +0530407{
Max5fa7e362016-04-15 16:04:45 +0200408 int i, rc, min = -1, max = -1, arfcns = 0;
409 bool pgsm = false;
Harald Weltea43f7892009-12-01 18:04:30 +0530410 memset(chan_list, 0, 16);
411
Harald Welte29350342012-02-17 15:58:23 +0100412 if (bts->band == GSM_BAND_900
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100413 && bts->c0->arfcn >= 1 && bts->c0->arfcn <= 124)
Max5fa7e362016-04-15 16:04:45 +0200414 pgsm = true;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100415 /* P-GSM-only handsets only support 'bit map 0 format' */
416 if (!bis && !ter && pgsm) {
Harald Weltea43f7892009-12-01 18:04:30 +0530417 chan_list[0] = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100418
419 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100420 if (i >= 1 && i <= 124
421 && bitvec_get_bit_pos(bv, i)) {
Harald Welte6c40def2009-12-14 22:07:14 +0100422 rc = freq_list_bm0_set_arfcn(chan_list, i);
423 if (rc < 0)
424 return rc;
425 }
Harald Weltea43f7892009-12-01 18:04:30 +0530426 }
427 return 0;
428 }
429
Harald Welte6c40def2009-12-14 22:07:14 +0100430 for (i = 0; i < bv->data_len*8; i++) {
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100431 /* in case of SI2 or SI5 allow all neighbours in same band
432 * in case of SI*bis, allow neighbours in same band ouside pgsm
433 * in case of SI*ter, allow neighbours in different bands
434 */
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100435 if (!bitvec_get_bit_pos(bv, i))
436 continue;
437 if (!use_arfcn(bts, bis, ter, pgsm, i))
438 continue;
439 /* count the arfcns we want to carry */
440 arfcns += 1;
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200441
Holger Hans Peter Freyther9e1952a2013-01-17 12:04:17 +0100442 /* 955..1023 < 0..885 */
443 if (min < 0)
444 min = i;
445 if (i >= 955 && min < 955)
446 min = i;
447 if (i >= 955 && min >= 955 && i < min)
448 min = i;
449 if (i < 955 && min < 955 && i < min)
450 min = i;
451 if (max < 0)
452 max = i;
453 if (i < 955 && max >= 955)
454 max = i;
455 if (i >= 955 && max >= 955 && i > max)
456 max = i;
457 if (i < 955 && max < 955 && i > max)
458 max = i;
Harald Weltea43f7892009-12-01 18:04:30 +0530459 }
460
Sylvain Munaut42a56522009-12-24 14:20:19 +0100461 if (max == -1) {
462 /* Empty set, use 'bit map 0 format' */
463 chan_list[0] = 0;
464 return 0;
465 }
466
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200467 /* Now find the best encoding */
468 if (((max - min) & 1023) <= 111)
469 return enc_freq_lst_var_bitmap(chan_list, bv, bts, bis,
470 ter, min, pgsm);
Harald Weltea43f7892009-12-01 18:04:30 +0530471
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200472 /* Attempt to do the range encoding */
473 rc = enc_freq_lst_range(chan_list, bv, bts, bis, ter, pgsm);
Max881064e2016-12-14 14:51:40 +0100474 if (rc >= 0)
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200475 return 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530476
Holger Hans Peter Freyther511f9c32012-10-13 12:38:54 +0200477 LOGP(DRR, LOGL_ERROR, "min_arfcn=%u, max_arfcn=%u, arfcns=%d "
478 "can not generate ARFCN list", min, max, arfcns);
479 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +0530480}
481
482/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Dieter Spaar16646022011-07-28 00:01:50 +0200483/* static*/ int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530484{
Harald Weltea43f7892009-12-01 18:04:30 +0530485 struct gsm_bts_trx *trx;
Harald Welte6c40def2009-12-14 22:07:14 +0100486 struct bitvec *bv = &bts->si_common.cell_alloc;
Harald Weltea43f7892009-12-01 18:04:30 +0530487
Harald Weltea39b0f22010-06-14 22:26:10 +0200488 /* Zero-initialize the bit-vector */
Harald Weltec8794b52010-06-16 11:09:18 +0200489 memset(bv->data, 0, bv->data_len);
Harald Weltea39b0f22010-06-14 22:26:10 +0200490
Harald Welte6c40def2009-12-14 22:07:14 +0100491 /* first we generate a bitvec of all TRX ARFCN's in our BTS */
Harald Weltea39b0f22010-06-14 22:26:10 +0200492 llist_for_each_entry(trx, &bts->trx_list, list) {
493 unsigned int i, j;
494 /* Always add the TRX's ARFCN */
Harald Welte6c40def2009-12-14 22:07:14 +0100495 bitvec_set_bit_pos(bv, trx->arfcn, 1);
Harald Weltea39b0f22010-06-14 22:26:10 +0200496 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
497 struct gsm_bts_trx_ts *ts = &trx->ts[i];
498 /* Add any ARFCNs present in hopping channels */
499 for (j = 0; j < 1024; j++) {
500 if (bitvec_get_bit_pos(&ts->hopping.arfcns, j))
501 bitvec_set_bit_pos(bv, j, 1);
502 }
503 }
504 }
Harald Weltea43f7892009-12-01 18:04:30 +0530505
Harald Welte6c40def2009-12-14 22:07:14 +0100506 /* then we generate a GSM 04.08 frequency list from the bitvec */
Max5fa7e362016-04-15 16:04:45 +0200507 return bitvec2freq_list(chan_list, bv, bts, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530508}
509
Harald Welte6c40def2009-12-14 22:07:14 +0100510/* generate a cell channel list as per Section 10.5.2.1b of 04.08 */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100511static int generate_bcch_chan_list(uint8_t *chan_list, struct gsm_bts *bts,
Max5fa7e362016-04-15 16:04:45 +0200512 bool si5, bool bis, bool ter)
Harald Welte6c40def2009-12-14 22:07:14 +0100513{
514 struct gsm_bts *cur_bts;
Harald Welte64c07d22011-02-15 11:43:27 +0100515 struct bitvec *bv;
516
517 if (si5 && bts->neigh_list_manual_mode == NL_MODE_MANUAL_SI5SEP)
518 bv = &bts->si_common.si5_neigh_list;
519 else
520 bv = &bts->si_common.neigh_list;
Harald Welte6c40def2009-12-14 22:07:14 +0100521
Harald Welte32c09622011-01-11 23:44:56 +0100522 /* Generate list of neighbor cells if we are in automatic mode */
Harald Welte64c07d22011-02-15 11:43:27 +0100523 if (bts->neigh_list_manual_mode == NL_MODE_AUTOMATIC) {
Harald Welte21bbda22011-02-19 20:43:37 +0100524 /* Zero-initialize the bit-vector */
525 memset(bv->data, 0, bv->data_len);
526
Harald Welte32c09622011-01-11 23:44:56 +0100527 /* first we generate a bitvec of the BCCH ARFCN's in our BSC */
528 llist_for_each_entry(cur_bts, &bts->network->bts_list, list) {
529 if (cur_bts == bts)
530 continue;
531 bitvec_set_bit_pos(bv, cur_bts->c0->arfcn, 1);
532 }
Harald Welte6c40def2009-12-14 22:07:14 +0100533 }
534
535 /* then we generate a GSM 04.08 frequency list from the bitvec */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100536 return bitvec2freq_list(chan_list, bv, bts, bis, ter);
537}
538
539static int list_arfcn(uint8_t *chan_list, uint8_t mask, char *text)
540{
541 int n = 0, i;
542 struct gsm_sysinfo_freq freq[1024];
543
544 memset(freq, 0, sizeof(freq));
545 gsm48_decode_freq_list(freq, chan_list, 16, 0xce, 1);
546 for (i = 0; i < 1024; i++) {
547 if (freq[i].mask) {
548 if (!n)
549 LOGP(DRR, LOGL_INFO, "%s", text);
550 LOGPC(DRR, LOGL_INFO, " %d", i);
551 n++;
552 }
553 }
554 if (n)
555 LOGPC(DRR, LOGL_INFO, "\n");
556
557 return n;
Harald Welte6c40def2009-12-14 22:07:14 +0100558}
559
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200560static int generate_si1(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530561{
562 int rc;
563 struct gsm48_system_information_type_1 *si1 =
564 (struct gsm48_system_information_type_1 *) output;
565
566 memset(si1, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
567
Max2440f492016-12-07 18:03:03 +0100568 si1->header.l2_plen = GSM48_LEN2PLEN(21);
Harald Weltea43f7892009-12-01 18:04:30 +0530569 si1->header.rr_protocol_discriminator = GSM48_PDISC_RR;
570 si1->header.skip_indicator = 0;
571 si1->header.system_information = GSM48_MT_RR_SYSINFO_1;
572
573 rc = generate_cell_chan_list(si1->cell_channel_description, bts);
574 if (rc < 0)
575 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100576 list_arfcn(si1->cell_channel_description, 0xce, "Serving cell:");
Harald Weltea43f7892009-12-01 18:04:30 +0530577
578 si1->rach_control = bts->si_common.rach_control;
579
Holger Hans Peter Freytherf876c392013-03-06 12:02:33 +0100580 /*
581 * SI1 Rest Octets (10.5.2.32), contains NCH position and band
582 * indicator but that is not in the 04.08.
583 */
584 rc = rest_octets_si1(si1->rest_octets, NULL, is_dcs_net(bts));
Harald Welte7401ae62010-06-15 16:44:12 +0200585
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100586 return sizeof(*si1) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530587}
588
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200589static int generate_si2(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530590{
591 int rc;
592 struct gsm48_system_information_type_2 *si2 =
593 (struct gsm48_system_information_type_2 *) output;
594
595 memset(si2, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
596
Max2440f492016-12-07 18:03:03 +0100597 si2->header.l2_plen = GSM48_LEN2PLEN(22);
Harald Weltea43f7892009-12-01 18:04:30 +0530598 si2->header.rr_protocol_discriminator = GSM48_PDISC_RR;
599 si2->header.skip_indicator = 0;
600 si2->header.system_information = GSM48_MT_RR_SYSINFO_2;
601
Max5fa7e362016-04-15 16:04:45 +0200602 rc = generate_bcch_chan_list(si2->bcch_frequency_list, bts, false, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530603 if (rc < 0)
604 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100605 list_arfcn(si2->bcch_frequency_list, 0xce,
Harald Weltee5ba92e2015-09-04 06:22:46 +0200606 "SI2 Neighbour cells in same band:");
Harald Weltea43f7892009-12-01 18:04:30 +0530607
608 si2->ncc_permitted = bts->si_common.ncc_permitted;
609 si2->rach_control = bts->si_common.rach_control;
610
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100611 return sizeof(*si2);
Harald Weltea43f7892009-12-01 18:04:30 +0530612}
613
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100614static int generate_si2bis(uint8_t *output, struct gsm_bts *bts)
615{
616 int rc;
617 struct gsm48_system_information_type_2bis *si2b =
618 (struct gsm48_system_information_type_2bis *) output;
619 int n;
620
621 memset(si2b, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
622
Max2440f492016-12-07 18:03:03 +0100623 si2b->header.l2_plen = GSM48_LEN2PLEN(22);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100624 si2b->header.rr_protocol_discriminator = GSM48_PDISC_RR;
625 si2b->header.skip_indicator = 0;
626 si2b->header.system_information = GSM48_MT_RR_SYSINFO_2bis;
627
Max5fa7e362016-04-15 16:04:45 +0200628 rc = generate_bcch_chan_list(si2b->bcch_frequency_list, bts, false, true, false);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100629 if (rc < 0)
630 return rc;
631 n = list_arfcn(si2b->bcch_frequency_list, 0xce,
632 "Neighbour cells in same band, but outside P-GSM:");
633 if (n) {
634 /* indicate in SI2 and SI2bis: there is an extension */
635 struct gsm48_system_information_type_2 *si2 =
636 (struct gsm48_system_information_type_2 *)
637 bts->si_buf[SYSINFO_TYPE_2];
638 si2->bcch_frequency_list[0] |= 0x20;
639 si2b->bcch_frequency_list[0] |= 0x20;
640 } else
641 bts->si_valid &= ~(1 << SYSINFO_TYPE_2bis);
642
643 si2b->rach_control = bts->si_common.rach_control;
644
645 return sizeof(*si2b);
646}
647
648static int generate_si2ter(uint8_t *output, struct gsm_bts *bts)
649{
650 int rc;
651 struct gsm48_system_information_type_2ter *si2t =
652 (struct gsm48_system_information_type_2ter *) output;
653 int n;
654
655 memset(si2t, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
656
Max2440f492016-12-07 18:03:03 +0100657 si2t->header.l2_plen = GSM48_LEN2PLEN(22);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100658 si2t->header.rr_protocol_discriminator = GSM48_PDISC_RR;
659 si2t->header.skip_indicator = 0;
660 si2t->header.system_information = GSM48_MT_RR_SYSINFO_2ter;
661
Max5fa7e362016-04-15 16:04:45 +0200662 rc = generate_bcch_chan_list(si2t->ext_bcch_frequency_list, bts, false, false, true);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100663 if (rc < 0)
664 return rc;
665 n = list_arfcn(si2t->ext_bcch_frequency_list, 0x8e,
666 "Neighbour cells in different band:");
667 if (!n)
668 bts->si_valid &= ~(1 << SYSINFO_TYPE_2ter);
669
670 return sizeof(*si2t);
671}
672
Max59a1bf32016-04-15 16:04:46 +0200673static int generate_si2quater(uint8_t *output, struct gsm_bts *bts)
674{
Max69e9c0d2016-05-18 13:04:47 +0200675 int rc, i = MAX_EARFCN_LIST;
Max59a1bf32016-04-15 16:04:46 +0200676 struct gsm48_system_information_type_2quater *si2q =
677 (struct gsm48_system_information_type_2quater *) output;
678
679 memset(si2q, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
680
681 si2q->header.l2_plen = GSM48_LEN2PLEN(22);
682 si2q->header.rr_protocol_discriminator = GSM48_PDISC_RR;
683 si2q->header.skip_indicator = 0;
684 si2q->header.system_information = GSM48_MT_RR_SYSINFO_2quater;
685
686 rc = rest_octets_si2quater(si2q->rest_octets,
Max26679e02016-04-20 15:57:13 +0200687 &bts->si_common.si2quater_neigh_list,
688 bts->si_common.data.uarfcn_list,
689 bts->si_common.data.scramble_list,
690 bts->si_common.uarfcn_length);
Max59a1bf32016-04-15 16:04:46 +0200691 if (rc < 0)
692 return rc;
693
Max69e9c0d2016-05-18 13:04:47 +0200694 if (bts->si_common.si2quater_neigh_list.arfcn)
695 for (i = 0; i < MAX_EARFCN_LIST; i++)
696 if (bts->si_common.si2quater_neigh_list.arfcn[i] !=
697 OSMO_EARFCN_INVALID)
698 break;
699 if (!bts->si_common.uarfcn_length && i == MAX_EARFCN_LIST)
700 bts->si_valid &= ~(1 << SYSINFO_TYPE_2quater);
701
Max59a1bf32016-04-15 16:04:46 +0200702 return sizeof(*si2q) + rc;
703}
704
Harald Welte1f893292010-03-14 23:46:48 +0800705static struct gsm48_si_ro_info si_info = {
Harald Weltea43f7892009-12-01 18:04:30 +0530706 .selection_params = {
707 .present = 0,
708 },
709 .power_offset = {
710 .present = 0,
711 },
712 .si2ter_indicator = 0,
713 .early_cm_ctrl = 1,
714 .scheduling = {
715 .present = 0,
716 },
717 .gprs_ind = {
718 .si13_position = 0,
719 .ra_colour = 0,
Harald Welte97a282b2010-03-14 15:37:43 +0800720 .present = 1,
Harald Weltea43f7892009-12-01 18:04:30 +0530721 },
Maxf3f35052016-04-15 16:04:44 +0200722 .si2quater_indicator = 0,
Harald Weltea43f7892009-12-01 18:04:30 +0530723 .lsa_params = {
724 .present = 0,
725 },
726 .cell_id = 0, /* FIXME: doesn't the bts have this? */
727 .break_ind = 0,
728};
729
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200730static int generate_si3(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530731{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100732 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530733 struct gsm48_system_information_type_3 *si3 =
734 (struct gsm48_system_information_type_3 *) output;
735
736 memset(si3, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
737
Max2440f492016-12-07 18:03:03 +0100738 si3->header.l2_plen = GSM48_LEN2PLEN(18);
Harald Weltea43f7892009-12-01 18:04:30 +0530739 si3->header.rr_protocol_discriminator = GSM48_PDISC_RR;
740 si3->header.skip_indicator = 0;
741 si3->header.system_information = GSM48_MT_RR_SYSINFO_3;
742
743 si3->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100744 gsm48_generate_lai(&si3->lai, bts->network->country_code,
745 bts->network->network_code,
746 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530747 si3->control_channel_desc = bts->si_common.chan_desc;
748 si3->cell_options = bts->si_common.cell_options;
749 si3->cell_sel_par = bts->si_common.cell_sel_par;
750 si3->rach_control = bts->si_common.rach_control;
751
Maxc08ee712016-05-11 12:45:13 +0200752 /* allow/disallow DTXu */
753 gsm48_set_dtx(&si3->cell_options, bts->dtxu, bts->dtxu, true);
754
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100755 if ((bts->si_valid & (1 << SYSINFO_TYPE_2ter))) {
756 LOGP(DRR, LOGL_INFO, "SI 2ter is included.\n");
757 si_info.si2ter_indicator = 1;
758 } else {
759 si_info.si2ter_indicator = 0;
760 }
Maxf3f35052016-04-15 16:04:44 +0200761 if ((bts->si_valid & (1 << SYSINFO_TYPE_2quater))) {
762 LOGP(DRR, LOGL_INFO, "SI 2quater is included.\n");
763 si_info.si2quater_indicator = 1;
764 } else {
765 si_info.si2quater_indicator = 0;
766 }
Harald Weltea43f7892009-12-01 18:04:30 +0530767 /* SI3 Rest Octets (10.5.2.34), containing
768 CBQ, CELL_RESELECT_OFFSET, TEMPORARY_OFFSET, PENALTY_TIME
769 Power Offset, 2ter Indicator, Early Classmark Sending,
770 Scheduling if and WHERE, GPRS Indicator, SI13 position */
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100771 rc = rest_octets_si3(si3->rest_octets, &si_info);
Harald Weltea43f7892009-12-01 18:04:30 +0530772
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100773 return sizeof(*si3) + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530774}
775
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200776static int generate_si4(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530777{
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +0100778 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530779 struct gsm48_system_information_type_4 *si4 =
780 (struct gsm48_system_information_type_4 *) output;
Harald Welte30f1f372014-12-28 15:00:45 +0100781 struct gsm_lchan *cbch_lchan;
782 uint8_t *restoct = si4->data;
Harald Weltea43f7892009-12-01 18:04:30 +0530783
784 /* length of all IEs present except SI4 rest octets and l2_plen */
785 int l2_plen = sizeof(*si4) - 1;
786
787 memset(si4, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
788
789 si4->header.rr_protocol_discriminator = GSM48_PDISC_RR;
790 si4->header.skip_indicator = 0;
791 si4->header.system_information = GSM48_MT_RR_SYSINFO_4;
792
Harald Welteafedeab2010-03-04 10:55:40 +0100793 gsm48_generate_lai(&si4->lai, bts->network->country_code,
794 bts->network->network_code,
795 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530796 si4->cell_sel_par = bts->si_common.cell_sel_par;
797 si4->rach_control = bts->si_common.rach_control;
798
799 /* Optional: CBCH Channel Description + CBCH Mobile Allocation */
Harald Welte142d12d2014-12-29 17:47:08 +0100800 cbch_lchan = gsm_bts_get_cbch(bts);
Harald Welte30f1f372014-12-28 15:00:45 +0100801 if (cbch_lchan) {
802 struct gsm48_chan_desc cd;
803 gsm48_lchan2chan_desc(&cd, cbch_lchan);
Daniel Willmann695675f2014-12-30 12:10:25 +0100804 tv_fixed_put(si4->data, GSM48_IE_CBCH_CHAN_DESC, 3,
Harald Welte30f1f372014-12-28 15:00:45 +0100805 (uint8_t *) &cd);
Daniel Willmann695675f2014-12-30 12:10:25 +0100806 l2_plen += 3 + 1;
807 restoct += 3 + 1;
Harald Welte30f1f372014-12-28 15:00:45 +0100808 /* we don't use hopping and thus don't need a CBCH MA */
809 }
Harald Weltea43f7892009-12-01 18:04:30 +0530810
Max2440f492016-12-07 18:03:03 +0100811 si4->header.l2_plen = GSM48_LEN2PLEN(l2_plen);
Harald Weltea43f7892009-12-01 18:04:30 +0530812
813 /* SI4 Rest Octets (10.5.2.35), containing
814 Optional Power offset, GPRS Indicator,
815 Cell Identity, LSA ID, Selection Parameter */
Holger Hans Peter Freyther7ff77ec2010-12-29 23:06:22 +0100816 rc = rest_octets_si4(restoct, &si_info, output + GSM_MACBLOCK_LEN - restoct);
Harald Weltea43f7892009-12-01 18:04:30 +0530817
Harald Welte30f1f372014-12-28 15:00:45 +0100818 return l2_plen + 1 + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530819}
820
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200821static int generate_si5(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530822{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100823 struct gsm48_system_information_type_5 *si5;
824 int rc, l2_plen = 18;
Harald Weltea43f7892009-12-01 18:04:30 +0530825
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100826 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
827
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100828 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100829 switch (bts->type) {
830 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200831 case GSM_BTS_TYPE_OSMO_SYSMO:
Max2440f492016-12-07 18:03:03 +0100832 *output++ = GSM48_LEN2PLEN(l2_plen);
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100833 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100834 break;
835 default:
836 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100837 }
838
839 si5 = (struct gsm48_system_information_type_5 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530840
841 /* l2 pseudo length, not part of msg: 18 */
842 si5->rr_protocol_discriminator = GSM48_PDISC_RR;
843 si5->skip_indicator = 0;
844 si5->system_information = GSM48_MT_RR_SYSINFO_5;
Max5fa7e362016-04-15 16:04:45 +0200845 rc = generate_bcch_chan_list(si5->bcch_frequency_list, bts, true, false, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530846 if (rc < 0)
847 return rc;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100848 list_arfcn(si5->bcch_frequency_list, 0xce,
Harald Weltee5ba92e2015-09-04 06:22:46 +0200849 "SI5 Neighbour cells in same band:");
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100850
851 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
852 return l2_plen;
853}
854
855static int generate_si5bis(uint8_t *output, struct gsm_bts *bts)
856{
857 struct gsm48_system_information_type_5bis *si5b;
858 int rc, l2_plen = 18;
859 int n;
860
861 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
862
863 /* ip.access nanoBTS needs l2_plen!! */
864 switch (bts->type) {
865 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200866 case GSM_BTS_TYPE_OSMO_SYSMO:
Max2440f492016-12-07 18:03:03 +0100867 *output++ = GSM48_LEN2PLEN(l2_plen);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100868 l2_plen++;
869 break;
870 default:
871 break;
872 }
873
874 si5b = (struct gsm48_system_information_type_5bis *) output;
875
876 /* l2 pseudo length, not part of msg: 18 */
877 si5b->rr_protocol_discriminator = GSM48_PDISC_RR;
878 si5b->skip_indicator = 0;
879 si5b->system_information = GSM48_MT_RR_SYSINFO_5bis;
Max5fa7e362016-04-15 16:04:45 +0200880 rc = generate_bcch_chan_list(si5b->bcch_frequency_list, bts, true, true, false);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100881 if (rc < 0)
882 return rc;
883 n = list_arfcn(si5b->bcch_frequency_list, 0xce,
884 "Neighbour cells in same band, but outside P-GSM:");
885 if (n) {
886 /* indicate in SI5 and SI5bis: there is an extension */
887 struct gsm48_system_information_type_5 *si5 =
888 (struct gsm48_system_information_type_5 *)
889 bts->si_buf[SYSINFO_TYPE_5];
890 si5->bcch_frequency_list[0] |= 0x20;
891 si5b->bcch_frequency_list[0] |= 0x20;
892 } else
893 bts->si_valid &= ~(1 << SYSINFO_TYPE_5bis);
894
895 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
896 return l2_plen;
897}
898
899static int generate_si5ter(uint8_t *output, struct gsm_bts *bts)
900{
901 struct gsm48_system_information_type_5ter *si5t;
902 int rc, l2_plen = 18;
903 int n;
904
905 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
906
907 /* ip.access nanoBTS needs l2_plen!! */
908 switch (bts->type) {
909 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200910 case GSM_BTS_TYPE_OSMO_SYSMO:
Max2440f492016-12-07 18:03:03 +0100911 *output++ = GSM48_LEN2PLEN(l2_plen);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100912 l2_plen++;
913 break;
914 default:
915 break;
916 }
917
918 si5t = (struct gsm48_system_information_type_5ter *) output;
919
920 /* l2 pseudo length, not part of msg: 18 */
921 si5t->rr_protocol_discriminator = GSM48_PDISC_RR;
922 si5t->skip_indicator = 0;
923 si5t->system_information = GSM48_MT_RR_SYSINFO_5ter;
Max5fa7e362016-04-15 16:04:45 +0200924 rc = generate_bcch_chan_list(si5t->bcch_frequency_list, bts, true, false, true);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100925 if (rc < 0)
926 return rc;
927 n = list_arfcn(si5t->bcch_frequency_list, 0x8e,
928 "Neighbour cells in different band:");
929 if (!n)
930 bts->si_valid &= ~(1 << SYSINFO_TYPE_5ter);
Harald Weltea43f7892009-12-01 18:04:30 +0530931
932 /* 04.08 9.1.37: L2 Pseudo Length of 18 */
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100933 return l2_plen;
Harald Weltea43f7892009-12-01 18:04:30 +0530934}
935
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200936static int generate_si6(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +0530937{
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100938 struct gsm48_system_information_type_6 *si6;
939 int l2_plen = 11;
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +0200940 int rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530941
Holger Hans Peter Freyther17d81e22010-01-06 07:52:55 +0100942 memset(output, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
943
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100944 /* ip.access nanoBTS needs l2_plen!! */
Harald Weltefd355a32011-03-04 13:41:31 +0100945 switch (bts->type) {
946 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +0200947 case GSM_BTS_TYPE_OSMO_SYSMO:
Max2440f492016-12-07 18:03:03 +0100948 *output++ = GSM48_LEN2PLEN(l2_plen);
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100949 l2_plen++;
Harald Weltefd355a32011-03-04 13:41:31 +0100950 break;
951 default:
952 break;
Harald Weltee2d0d5f2009-12-19 21:26:54 +0100953 }
954
955 si6 = (struct gsm48_system_information_type_6 *) output;
Harald Weltea43f7892009-12-01 18:04:30 +0530956
957 /* l2 pseudo length, not part of msg: 11 */
958 si6->rr_protocol_discriminator = GSM48_PDISC_RR;
959 si6->skip_indicator = 0;
960 si6->system_information = GSM48_MT_RR_SYSINFO_6;
961 si6->cell_identity = htons(bts->cell_identity);
Harald Welteafedeab2010-03-04 10:55:40 +0100962 gsm48_generate_lai(&si6->lai, bts->network->country_code,
963 bts->network->network_code,
964 bts->location_area_code);
Harald Weltea43f7892009-12-01 18:04:30 +0530965 si6->cell_options = bts->si_common.cell_options;
966 si6->ncc_permitted = bts->si_common.ncc_permitted;
Maxc08ee712016-05-11 12:45:13 +0200967 /* allow/disallow DTXu */
Maxea8e9832016-05-23 17:28:13 +0200968 gsm48_set_dtx(&si6->cell_options, bts->dtxu, bts->dtxu, false);
Harald Weltea43f7892009-12-01 18:04:30 +0530969
970 /* SI6 Rest Octets: 10.5.2.35a: PCH / NCH info, VBS/VGCS options */
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +0200971 rc = rest_octets_si6(si6->rest_octets, is_dcs_net(bts));
Harald Weltea43f7892009-12-01 18:04:30 +0530972
Holger Hans Peter Freyther82dd9832016-05-17 23:20:03 +0200973 return l2_plen + rc;
Harald Weltea43f7892009-12-01 18:04:30 +0530974}
975
976static struct gsm48_si13_info si13_default = {
977 .cell_opts = {
Harald Welte85849182010-06-02 12:19:21 +0200978 .nmo = GPRS_NMO_II,
Harald Weltea4b16652010-05-31 12:54:23 +0200979 .t3168 = 2000,
Andreas Eversbergbecc89a2012-09-23 08:14:51 +0200980 .t3192 = 1500,
Harald Welte97a282b2010-03-14 15:37:43 +0800981 .drx_timer_max = 3,
Harald Weltea43f7892009-12-01 18:04:30 +0530982 .bs_cv_max = 15,
Max292ec582016-07-28 11:55:37 +0200983 .ctrl_ack_type_use_block = true,
Sylvain Munaut851f1202011-10-17 13:56:02 +0200984 .ext_info_present = 0,
bhargava350533c2016-07-21 11:14:34 +0530985 .supports_egprs_11bit_rach = 0,
Harald Welteda0586a2010-04-18 14:49:05 +0200986 .ext_info = {
987 /* The values below are just guesses ! */
Harald Weltea06fea02010-04-18 15:53:40 +0200988 .egprs_supported = 0,
Harald Welteda0586a2010-04-18 14:49:05 +0200989 .use_egprs_p_ch_req = 1,
Harald Weltea4b16652010-05-31 12:54:23 +0200990 .bep_period = 5,
Harald Welteda0586a2010-04-18 14:49:05 +0200991 .pfc_supported = 0,
992 .dtm_supported = 0,
993 .bss_paging_coordination = 0,
994 },
Harald Weltea43f7892009-12-01 18:04:30 +0530995 },
996 .pwr_ctrl_pars = {
Harald Weltec15d0ac2012-10-08 21:22:08 +0200997 .alpha = 0, /* a = 0.0 */
Harald Weltea4b16652010-05-31 12:54:23 +0200998 .t_avg_w = 16,
999 .t_avg_t = 16,
Harald Weltea43f7892009-12-01 18:04:30 +05301000 .pc_meas_chan = 0, /* downling measured on CCCH */
Harald Weltea4b16652010-05-31 12:54:23 +02001001 .n_avg_i = 8,
Harald Weltea43f7892009-12-01 18:04:30 +05301002 },
Harald Welte97a282b2010-03-14 15:37:43 +08001003 .bcch_change_mark = 1,
Harald Weltea43f7892009-12-01 18:04:30 +05301004 .si_change_field = 0,
1005 .pbcch_present = 0,
1006 {
1007 .no_pbcch = {
Harald Welte97a282b2010-03-14 15:37:43 +08001008 .rac = 0, /* needs to be patched */
Harald Weltea43f7892009-12-01 18:04:30 +05301009 .spgc_ccch_sup = 0,
1010 .net_ctrl_ord = 0,
Harald Welte97a282b2010-03-14 15:37:43 +08001011 .prio_acc_thr = 6,
Harald Weltea43f7892009-12-01 18:04:30 +05301012 },
1013 },
1014};
1015
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001016static int generate_si13(uint8_t *output, struct gsm_bts *bts)
Harald Weltea43f7892009-12-01 18:04:30 +05301017{
1018 struct gsm48_system_information_type_13 *si13 =
1019 (struct gsm48_system_information_type_13 *) output;
1020 int ret;
1021
1022 memset(si13, GSM_MACBLOCK_PADDING, GSM_MACBLOCK_LEN);
1023
1024 si13->header.rr_protocol_discriminator = GSM48_PDISC_RR;
1025 si13->header.skip_indicator = 0;
1026 si13->header.system_information = GSM48_MT_RR_SYSINFO_13;
1027
Harald Welte97a282b2010-03-14 15:37:43 +08001028 si13_default.no_pbcch.rac = bts->gprs.rac;
Andreas Eversberg0c8f9ca2013-03-16 16:31:26 +01001029 si13_default.no_pbcch.net_ctrl_ord = bts->gprs.net_ctrl_ord;
Harald Welte97a282b2010-03-14 15:37:43 +08001030
Max292ec582016-07-28 11:55:37 +02001031 si13_default.cell_opts.ctrl_ack_type_use_block =
1032 bts->gprs.ctrl_ack_type_use_block;
1033
Holger Hans Peter Freytherb92a5382014-11-21 10:00:39 +01001034 /* Information about the other SIs */
1035 si13_default.bcch_change_mark = bts->bcch_change_mark;
bhargava350533c2016-07-21 11:14:34 +05301036 si13_default.cell_opts.supports_egprs_11bit_rach =
1037 bts->gprs.supports_egprs_11bit_rach;
Holger Hans Peter Freytherb92a5382014-11-21 10:00:39 +01001038
Harald Weltea43f7892009-12-01 18:04:30 +05301039 ret = rest_octets_si13(si13->rest_octets, &si13_default);
1040 if (ret < 0)
1041 return ret;
1042
Holger Hans Peter Freyther1c6f3942010-06-16 12:05:56 +08001043 /* length is coded in bit 2 an up */
1044 si13->header.l2_plen = 0x01;
Harald Weltea43f7892009-12-01 18:04:30 +05301045
Holger Hans Peter Freyther7ec448d2010-01-06 07:52:31 +01001046 return sizeof (*si13) + ret;
Harald Weltea43f7892009-12-01 18:04:30 +05301047}
1048
Harald Welte7401ae62010-06-15 16:44:12 +02001049typedef int (*gen_si_fn_t)(uint8_t *output, struct gsm_bts *bts);
1050
1051static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
1052 [SYSINFO_TYPE_1] = &generate_si1,
1053 [SYSINFO_TYPE_2] = &generate_si2,
Andreas Eversbergc50543b2012-02-29 09:04:07 +01001054 [SYSINFO_TYPE_2bis] = &generate_si2bis,
1055 [SYSINFO_TYPE_2ter] = &generate_si2ter,
Max59a1bf32016-04-15 16:04:46 +02001056 [SYSINFO_TYPE_2quater] = &generate_si2quater,
Harald Welte7401ae62010-06-15 16:44:12 +02001057 [SYSINFO_TYPE_3] = &generate_si3,
1058 [SYSINFO_TYPE_4] = &generate_si4,
1059 [SYSINFO_TYPE_5] = &generate_si5,
Andreas Eversbergc50543b2012-02-29 09:04:07 +01001060 [SYSINFO_TYPE_5bis] = &generate_si5bis,
1061 [SYSINFO_TYPE_5ter] = &generate_si5ter,
Harald Welte7401ae62010-06-15 16:44:12 +02001062 [SYSINFO_TYPE_6] = &generate_si6,
1063 [SYSINFO_TYPE_13] = &generate_si13,
1064};
1065
Harald Welte7401ae62010-06-15 16:44:12 +02001066int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
1067{
1068 gen_si_fn_t gen_si;
1069
Harald Welte4511d892010-04-18 15:51:20 +02001070 switch (bts->gprs.mode) {
1071 case BTS_GPRS_EGPRS:
Harald Weltea06fea02010-04-18 15:53:40 +02001072 si13_default.cell_opts.ext_info_present = 1;
1073 si13_default.cell_opts.ext_info.egprs_supported = 1;
1074 /* fallthrough */
Harald Welte4511d892010-04-18 15:51:20 +02001075 case BTS_GPRS_GPRS:
1076 si_info.gprs_ind.present = 1;
1077 break;
1078 case BTS_GPRS_NONE:
1079 si_info.gprs_ind.present = 0;
1080 break;
1081 }
Harald Welte1f893292010-03-14 23:46:48 +08001082
Sylvain Munaute0b06b02010-11-28 18:17:28 +01001083 memcpy(&si_info.selection_params,
1084 &bts->si_common.cell_ro_sel_par,
1085 sizeof(struct gsm48_si_selection_params));
1086
Harald Welte7401ae62010-06-15 16:44:12 +02001087 gen_si = gen_si_fn[si_type];
1088 if (!gen_si)
Harald Weltea43f7892009-12-01 18:04:30 +05301089 return -EINVAL;
Harald Weltea43f7892009-12-01 18:04:30 +05301090
Harald Welte7401ae62010-06-15 16:44:12 +02001091 return gen_si(bts->si_buf[si_type], bts);
Harald Weltea43f7892009-12-01 18:04:30 +05301092}