blob: cdaba9e52ffbb3d9ac6974735af53f372308b362 [file] [log] [blame]
Harald Welte32201c12009-03-10 12:15:10 +00001/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte8470bf22008-12-25 23:28:35 +00002 *
Harald Welte52b1f982008-12-23 20:25:15 +00003 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21
22#include <stdlib.h>
Harald Welte51f38452009-02-24 22:36:40 +000023#include <stdio.h>
Harald Welte52b1f982008-12-23 20:25:15 +000024#include <string.h>
Harald Weltefcd24452009-06-20 18:15:19 +020025#include <errno.h>
Harald Welte42581822009-08-08 16:12:58 +020026#include <ctype.h>
Harald Welte52b1f982008-12-23 20:25:15 +000027
Harald Welte8470bf22008-12-25 23:28:35 +000028#include <openbsc/gsm_data.h>
Harald Weltee441d9c2009-06-21 16:17:15 +020029#include <openbsc/talloc.h>
Holger Hans Peter Freyther2d501ea2009-11-11 11:54:24 +010030#include <openbsc/abis_nm.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Andreas Eversberg8226fa72009-06-29 15:19:38 +020032void *tall_bsc_ctx;
33
Harald Welteccda9652009-11-12 22:28:18 +090034const char *get_value_string(const struct value_string *vs, u_int32_t val)
35{
36 int i;
37
38 for (i = 0;; i++) {
39 if (vs[i].value == 0 && vs[i].str == NULL)
40 break;
41 if (vs[i].value == val)
42 return vs[i].str;
43 }
44 return "unknown";
45}
46
Harald Weltecd06bfb2009-02-10 17:33:56 +000047void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr,
48 u_int8_t e1_ts, u_int8_t e1_ts_ss)
49{
50 ts->e1_link.e1_nr = e1_nr;
51 ts->e1_link.e1_ts = e1_ts;
52 ts->e1_link.e1_ts_ss = e1_ts_ss;
53}
54
Harald Weltea72c98e2009-01-04 16:10:38 +000055static const char *pchan_names[] = {
56 [GSM_PCHAN_NONE] = "NONE",
57 [GSM_PCHAN_CCCH] = "CCCH",
58 [GSM_PCHAN_CCCH_SDCCH4] = "CCCH+SDCCH4",
59 [GSM_PCHAN_TCH_F] = "TCH/F",
60 [GSM_PCHAN_TCH_H] = "TCH/H",
61 [GSM_PCHAN_SDCCH8_SACCH8C] = "SDCCH8",
Harald Weltea1499d02009-10-24 10:25:50 +020062 [GSM_PCHAN_PDCH] = "PDCH",
63 [GSM_PCHAN_TCH_F_PDCH] = "TCH/F_PDCH",
Harald Weltea72c98e2009-01-04 16:10:38 +000064 [GSM_PCHAN_UNKNOWN] = "UNKNOWN",
65};
66
67const char *gsm_pchan_name(enum gsm_phys_chan_config c)
68{
69 if (c >= ARRAY_SIZE(pchan_names))
70 return "INVALID";
71
72 return pchan_names[c];
73}
74
Harald Weltea171a1b2009-08-07 00:24:39 +020075enum gsm_phys_chan_config gsm_pchan_parse(const char *name)
76{
77 int i;
78
79 for (i = 0; i < ARRAY_SIZE(pchan_names); i++) {
80 if (!strcasecmp(name, pchan_names[i]))
81 return i;
82 }
83
84 return -1;
85}
86
Harald Weltea72c98e2009-01-04 16:10:38 +000087static const char *lchan_names[] = {
88 [GSM_LCHAN_NONE] = "NONE",
89 [GSM_LCHAN_SDCCH] = "SDCCH",
90 [GSM_LCHAN_TCH_F] = "TCH/F",
91 [GSM_LCHAN_TCH_H] = "TCH/H",
92 [GSM_LCHAN_UNKNOWN] = "UNKNOWN",
93};
94
95const char *gsm_lchan_name(enum gsm_chan_t c)
96{
97 if (c >= ARRAY_SIZE(lchan_names))
98 return "INVALID";
99
100 return lchan_names[c];
101}
102
103static const char *chreq_names[] = {
104 [GSM_CHREQ_REASON_EMERG] = "EMERGENCY",
105 [GSM_CHREQ_REASON_PAG] = "PAGING",
106 [GSM_CHREQ_REASON_CALL] = "CALL",
107 [GSM_CHREQ_REASON_LOCATION_UPD] = "LOCATION_UPDATE",
108 [GSM_CHREQ_REASON_OTHER] = "OTHER",
109};
110
111const char *gsm_chreq_name(enum gsm_chreq_reason_t c)
112{
113 if (c >= ARRAY_SIZE(chreq_names))
114 return "INVALID";
115
116 return chreq_names[c];
117}
118
Harald Weltee441d9c2009-06-21 16:17:15 +0200119struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000120{
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100121 struct gsm_bts_trx *trx = talloc_zero(bts, struct gsm_bts_trx);
Harald Weltee441d9c2009-06-21 16:17:15 +0200122 int k;
Harald Welte52b1f982008-12-23 20:25:15 +0000123
Harald Weltee441d9c2009-06-21 16:17:15 +0200124 if (!trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000125 return NULL;
126
Harald Weltee441d9c2009-06-21 16:17:15 +0200127 trx->bts = bts;
128 trx->nr = bts->num_trx++;
129
130 for (k = 0; k < TRX_NR_TS; k++) {
131 struct gsm_bts_trx_ts *ts = &trx->ts[k];
132 int l;
133
134 ts->trx = trx;
135 ts->nr = k;
136 ts->pchan = GSM_PCHAN_NONE;
137
138 for (l = 0; l < TS_MAX_LCHAN; l++) {
139 struct gsm_lchan *lchan;
140 lchan = &ts->lchan[l];
141
142 lchan->ts = ts;
143 lchan->nr = l;
144 lchan->type = GSM_LCHAN_NONE;
145 }
146 }
147
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200148 llist_add_tail(&trx->list, &bts->trx_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200149
150 return trx;
151}
152
153struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, enum gsm_bts_type type,
154 u_int8_t tsc, u_int8_t bsic)
155{
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100156 struct gsm_bts *bts = talloc_zero(net, struct gsm_bts);
Harald Welte55dd4432009-10-24 10:19:14 +0200157 int i;
Harald Weltee441d9c2009-06-21 16:17:15 +0200158
159 if (!bts)
160 return NULL;
161
Harald Weltee441d9c2009-06-21 16:17:15 +0200162 bts->network = net;
163 bts->nr = net->num_bts++;
164 bts->type = type;
165 bts->tsc = tsc;
166 bts->bsic = bsic;
167 bts->num_trx = 0;
168 INIT_LLIST_HEAD(&bts->trx_list);
Harald Welte (local)0e451d02009-08-13 10:14:26 +0200169 bts->ms_max_power = 15; /* dBm */
Harald Welte73225282009-12-12 18:17:25 +0100170 bts->si_common.cell_sel_par.cell_resel_hyst = 2; /* 4 dB */
171 bts->si_common.cell_sel_par.rxlev_acc_min = 0;
Harald Weltee441d9c2009-06-21 16:17:15 +0200172
Harald Welte55dd4432009-10-24 10:19:14 +0200173 for (i = 0; i < ARRAY_SIZE(bts->gprs.nsvc); i++) {
174 bts->gprs.nsvc[i].bts = bts;
175 bts->gprs.nsvc[i].id = i;
176 }
177
Harald Weltee441d9c2009-06-21 16:17:15 +0200178 /* create our primary TRX */
179 bts->c0 = gsm_bts_trx_alloc(bts);
180 if (!bts->c0) {
181 talloc_free(bts);
182 return NULL;
183 }
184 bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
185
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200186 llist_add_tail(&bts->list, &net->bts_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200187
188 return bts;
189}
190
191struct gsm_network *gsm_network_init(u_int16_t country_code, u_int16_t network_code,
192 int (*mncc_recv)(struct gsm_network *, int, void *))
193{
194 struct gsm_network *net;
195
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100196 net = talloc_zero(tall_bsc_ctx, struct gsm_network);
Harald Welte52b1f982008-12-23 20:25:15 +0000197 if (!net)
198 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000199
200 net->country_code = country_code;
201 net->network_code = network_code;
Harald Weltee441d9c2009-06-21 16:17:15 +0200202 net->num_bts = 0;
Harald Welteb84ddfc2009-12-01 17:36:54 +0530203 net->T3101 = GSM_T3101_DEFAULT;
204 net->T3113 = GSM_T3113_DEFAULT;
205 /* FIXME: initialize all other timers! */
Harald Welte52b1f982008-12-23 20:25:15 +0000206
Harald Welte4bfdfe72009-06-10 23:11:52 +0800207 INIT_LLIST_HEAD(&net->trans_list);
208 INIT_LLIST_HEAD(&net->upqueue);
Harald Weltee441d9c2009-06-21 16:17:15 +0200209 INIT_LLIST_HEAD(&net->bts_list);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800210
211 net->mncc_recv = mncc_recv;
212
Harald Welte8470bf22008-12-25 23:28:35 +0000213 return net;
Harald Welte52b1f982008-12-23 20:25:15 +0000214}
Harald Welte23a68632009-02-19 17:06:42 +0000215
Harald Weltee441d9c2009-06-21 16:17:15 +0200216struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
217{
218 struct gsm_bts *bts;
219
220 if (num >= net->num_bts)
221 return NULL;
222
223 llist_for_each_entry(bts, &net->bts_list, list) {
224 if (bts->nr == num)
225 return bts;
226 }
227
228 return NULL;
229}
230
231struct gsm_bts_trx *gsm_bts_trx_num(struct gsm_bts *bts, int num)
232{
233 struct gsm_bts_trx *trx;
234
235 if (num >= bts->num_trx)
236 return NULL;
237
238 llist_for_each_entry(trx, &bts->trx_list, list) {
239 if (trx->nr == num)
240 return trx;
241 }
242
243 return NULL;
244}
245
Harald Welte23a68632009-02-19 17:06:42 +0000246static char ts2str[255];
247
248char *gsm_ts_name(struct gsm_bts_trx_ts *ts)
249{
250 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)",
Mike Haben2449b372009-10-26 20:36:34 +0100251 ts->trx->bts->nr, ts->trx->nr, ts->nr);
Harald Welte23a68632009-02-19 17:06:42 +0000252
253 return ts2str;
254}
Harald Welte32201c12009-03-10 12:15:10 +0000255
256static const char *bts_types[] = {
257 [GSM_BTS_TYPE_UNKNOWN] = "unknown",
258 [GSM_BTS_TYPE_BS11] = "bs11",
Mike Habene2d82272009-10-02 12:19:34 +0100259 [GSM_BTS_TYPE_NANOBTS] = "nanobts",
Harald Welte32201c12009-03-10 12:15:10 +0000260};
261
Harald Welte1d014a52009-08-08 15:38:29 +0200262enum gsm_bts_type parse_btstype(const char *arg)
Harald Welte32201c12009-03-10 12:15:10 +0000263{
264 int i;
265 for (i = 0; i < ARRAY_SIZE(bts_types); i++) {
266 if (!strcmp(arg, bts_types[i]))
267 return i;
268 }
269 return GSM_BTS_TYPE_BS11; /* Default: BS11 */
270}
271
Holger Hans Peter Freyther2dceae62009-06-12 17:39:38 +0200272const char *btstype2str(enum gsm_bts_type type)
Harald Welte32201c12009-03-10 12:15:10 +0000273{
274 if (type > ARRAY_SIZE(bts_types))
275 return "undefined";
276 return bts_types[type];
277}
Harald Weltebe991492009-05-23 13:56:40 +0000278
279/* Search for a BTS in the given Location Area; optionally start searching
280 * with start_bts (for continuing to search after the first result) */
281struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
282 struct gsm_bts *start_bts)
283{
284 int i;
285 struct gsm_bts *bts;
286 int skip = 0;
287
288 if (start_bts)
289 skip = 1;
290
291 for (i = 0; i < net->num_bts; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200292 bts = gsm_bts_num(net, i);
Harald Weltebe991492009-05-23 13:56:40 +0000293
294 if (skip) {
295 if (start_bts == bts)
296 skip = 0;
297 continue;
298 }
299
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200300 if (lac == GSM_LAC_RESERVED_ALL_BTS || bts->location_area_code == lac)
Harald Weltebe991492009-05-23 13:56:40 +0000301 return bts;
302 }
303 return NULL;
304}
Harald Weltefcd24452009-06-20 18:15:19 +0200305
306char *gsm_band_name(enum gsm_band band)
307{
308 switch (band) {
309 case GSM_BAND_400:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200310 return "GSM400";
Harald Weltefcd24452009-06-20 18:15:19 +0200311 case GSM_BAND_850:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200312 return "GSM850";
Harald Weltefcd24452009-06-20 18:15:19 +0200313 case GSM_BAND_900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200314 return "GSM900";
Harald Weltefcd24452009-06-20 18:15:19 +0200315 case GSM_BAND_1800:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200316 return "DCS1800";
Harald Weltefcd24452009-06-20 18:15:19 +0200317 case GSM_BAND_1900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200318 return "PCS1900";
Harald Weltefcd24452009-06-20 18:15:19 +0200319 }
320 return "invalid";
321}
322
Harald Welte42581822009-08-08 16:12:58 +0200323enum gsm_band gsm_band_parse(const char* mhz)
Harald Weltefcd24452009-06-20 18:15:19 +0200324{
Harald Welte42581822009-08-08 16:12:58 +0200325 while (*mhz && !isdigit(*mhz))
326 mhz++;
327
328 if (*mhz == '\0')
329 return -EINVAL;
330
331 switch (atoi(mhz)) {
Harald Weltefcd24452009-06-20 18:15:19 +0200332 case 400:
333 return GSM_BAND_400;
334 case 850:
335 return GSM_BAND_850;
336 case 900:
337 return GSM_BAND_900;
338 case 1800:
339 return GSM_BAND_1800;
340 case 1900:
341 return GSM_BAND_1900;
342 default:
343 return -EINVAL;
344 }
345}
346
Harald Welte (local)69de3972009-08-12 14:42:23 +0200347static const char *gsm_auth_policy_names[] = {
348 [GSM_AUTH_POLICY_CLOSED] = "closed",
349 [GSM_AUTH_POLICY_ACCEPT_ALL] = "accept-all",
350 [GSM_AUTH_POLICY_TOKEN] = "token",
351};
352
353enum gsm_auth_policy gsm_auth_policy_parse(const char *arg)
354{
355 int i;
356 for (i = 0; i < ARRAY_SIZE(gsm_auth_policy_names); i++) {
357 if (!strcmp(arg, gsm_auth_policy_names[i]))
358 return i;
359 }
360 return GSM_AUTH_POLICY_CLOSED;
361}
362
363const char *gsm_auth_policy_name(enum gsm_auth_policy policy)
364{
365 if (policy > ARRAY_SIZE(gsm_auth_policy_names))
366 return "undefined";
367 return gsm_auth_policy_names[policy];
368}
369
Harald Welteeab84a12009-12-13 10:53:12 +0100370static const char *rrlp_mode_names[] = {
371 [RRLP_MODE_NONE] = "none",
372 [RRLP_MODE_MS_BASED] = "ms-based",
373 [RRLP_MODE_MS_PREF] = "ms-preferred",
374 [RRLP_MODE_ASS_PREF] = "ass-preferred",
375};
376
377enum rrlp_mode rrlp_mode_parse(const char *arg)
378{
379 int i;
380 for (i = 0; i < ARRAY_SIZE(rrlp_mode_names); i++) {
381 if (!strcmp(arg, rrlp_mode_names[i]))
382 return i;
383 }
384 return RRLP_MODE_NONE;
385}
386
387const char *rrlp_mode_name(enum rrlp_mode mode)
388{
389 if (mode > ARRAY_SIZE(rrlp_mode_names))
390 return "none";
391 return rrlp_mode_names[mode];
392}