blob: 8212346ec86818be10cd35b3b3cc2d7ba53ba10b [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 Weltee441d9c2009-06-21 16:17:15 +0200170
Harald Welte55dd4432009-10-24 10:19:14 +0200171 for (i = 0; i < ARRAY_SIZE(bts->gprs.nsvc); i++) {
172 bts->gprs.nsvc[i].bts = bts;
173 bts->gprs.nsvc[i].id = i;
174 }
175
Harald Weltee441d9c2009-06-21 16:17:15 +0200176 /* create our primary TRX */
177 bts->c0 = gsm_bts_trx_alloc(bts);
178 if (!bts->c0) {
179 talloc_free(bts);
180 return NULL;
181 }
182 bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
183
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200184 llist_add_tail(&bts->list, &net->bts_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200185
186 return bts;
187}
188
189struct gsm_network *gsm_network_init(u_int16_t country_code, u_int16_t network_code,
190 int (*mncc_recv)(struct gsm_network *, int, void *))
191{
192 struct gsm_network *net;
193
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100194 net = talloc_zero(tall_bsc_ctx, struct gsm_network);
Harald Welte52b1f982008-12-23 20:25:15 +0000195 if (!net)
196 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000197
198 net->country_code = country_code;
199 net->network_code = network_code;
Harald Weltee441d9c2009-06-21 16:17:15 +0200200 net->num_bts = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000201
Harald Welte4bfdfe72009-06-10 23:11:52 +0800202 INIT_LLIST_HEAD(&net->trans_list);
203 INIT_LLIST_HEAD(&net->upqueue);
Harald Weltee441d9c2009-06-21 16:17:15 +0200204 INIT_LLIST_HEAD(&net->bts_list);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800205
206 net->mncc_recv = mncc_recv;
207
Harald Welte8470bf22008-12-25 23:28:35 +0000208 return net;
Harald Welte52b1f982008-12-23 20:25:15 +0000209}
Harald Welte23a68632009-02-19 17:06:42 +0000210
Harald Weltee441d9c2009-06-21 16:17:15 +0200211struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
212{
213 struct gsm_bts *bts;
214
215 if (num >= net->num_bts)
216 return NULL;
217
218 llist_for_each_entry(bts, &net->bts_list, list) {
219 if (bts->nr == num)
220 return bts;
221 }
222
223 return NULL;
224}
225
226struct gsm_bts_trx *gsm_bts_trx_num(struct gsm_bts *bts, int num)
227{
228 struct gsm_bts_trx *trx;
229
230 if (num >= bts->num_trx)
231 return NULL;
232
233 llist_for_each_entry(trx, &bts->trx_list, list) {
234 if (trx->nr == num)
235 return trx;
236 }
237
238 return NULL;
239}
240
Harald Welte23a68632009-02-19 17:06:42 +0000241static char ts2str[255];
242
243char *gsm_ts_name(struct gsm_bts_trx_ts *ts)
244{
245 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)",
Mike Haben2449b372009-10-26 20:36:34 +0100246 ts->trx->bts->nr, ts->trx->nr, ts->nr);
Harald Welte23a68632009-02-19 17:06:42 +0000247
248 return ts2str;
249}
Harald Welte32201c12009-03-10 12:15:10 +0000250
251static const char *bts_types[] = {
252 [GSM_BTS_TYPE_UNKNOWN] = "unknown",
253 [GSM_BTS_TYPE_BS11] = "bs11",
Mike Habene2d82272009-10-02 12:19:34 +0100254 [GSM_BTS_TYPE_NANOBTS] = "nanobts",
Harald Welte32201c12009-03-10 12:15:10 +0000255};
256
Harald Welte1d014a52009-08-08 15:38:29 +0200257enum gsm_bts_type parse_btstype(const char *arg)
Harald Welte32201c12009-03-10 12:15:10 +0000258{
259 int i;
260 for (i = 0; i < ARRAY_SIZE(bts_types); i++) {
261 if (!strcmp(arg, bts_types[i]))
262 return i;
263 }
264 return GSM_BTS_TYPE_BS11; /* Default: BS11 */
265}
266
Holger Hans Peter Freyther2dceae62009-06-12 17:39:38 +0200267const char *btstype2str(enum gsm_bts_type type)
Harald Welte32201c12009-03-10 12:15:10 +0000268{
269 if (type > ARRAY_SIZE(bts_types))
270 return "undefined";
271 return bts_types[type];
272}
Harald Weltebe991492009-05-23 13:56:40 +0000273
274/* Search for a BTS in the given Location Area; optionally start searching
275 * with start_bts (for continuing to search after the first result) */
276struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
277 struct gsm_bts *start_bts)
278{
279 int i;
280 struct gsm_bts *bts;
281 int skip = 0;
282
283 if (start_bts)
284 skip = 1;
285
286 for (i = 0; i < net->num_bts; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200287 bts = gsm_bts_num(net, i);
Harald Weltebe991492009-05-23 13:56:40 +0000288
289 if (skip) {
290 if (start_bts == bts)
291 skip = 0;
292 continue;
293 }
294
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200295 if (lac == GSM_LAC_RESERVED_ALL_BTS || bts->location_area_code == lac)
Harald Weltebe991492009-05-23 13:56:40 +0000296 return bts;
297 }
298 return NULL;
299}
Harald Weltefcd24452009-06-20 18:15:19 +0200300
301char *gsm_band_name(enum gsm_band band)
302{
303 switch (band) {
304 case GSM_BAND_400:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200305 return "GSM400";
Harald Weltefcd24452009-06-20 18:15:19 +0200306 case GSM_BAND_850:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200307 return "GSM850";
Harald Weltefcd24452009-06-20 18:15:19 +0200308 case GSM_BAND_900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200309 return "GSM900";
Harald Weltefcd24452009-06-20 18:15:19 +0200310 case GSM_BAND_1800:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200311 return "DCS1800";
Harald Weltefcd24452009-06-20 18:15:19 +0200312 case GSM_BAND_1900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200313 return "PCS1900";
Harald Weltefcd24452009-06-20 18:15:19 +0200314 }
315 return "invalid";
316}
317
Harald Welte42581822009-08-08 16:12:58 +0200318enum gsm_band gsm_band_parse(const char* mhz)
Harald Weltefcd24452009-06-20 18:15:19 +0200319{
Harald Welte42581822009-08-08 16:12:58 +0200320 while (*mhz && !isdigit(*mhz))
321 mhz++;
322
323 if (*mhz == '\0')
324 return -EINVAL;
325
326 switch (atoi(mhz)) {
Harald Weltefcd24452009-06-20 18:15:19 +0200327 case 400:
328 return GSM_BAND_400;
329 case 850:
330 return GSM_BAND_850;
331 case 900:
332 return GSM_BAND_900;
333 case 1800:
334 return GSM_BAND_1800;
335 case 1900:
336 return GSM_BAND_1900;
337 default:
338 return -EINVAL;
339 }
340}
341
Harald Welte (local)69de3972009-08-12 14:42:23 +0200342static const char *gsm_auth_policy_names[] = {
343 [GSM_AUTH_POLICY_CLOSED] = "closed",
344 [GSM_AUTH_POLICY_ACCEPT_ALL] = "accept-all",
345 [GSM_AUTH_POLICY_TOKEN] = "token",
346};
347
348enum gsm_auth_policy gsm_auth_policy_parse(const char *arg)
349{
350 int i;
351 for (i = 0; i < ARRAY_SIZE(gsm_auth_policy_names); i++) {
352 if (!strcmp(arg, gsm_auth_policy_names[i]))
353 return i;
354 }
355 return GSM_AUTH_POLICY_CLOSED;
356}
357
358const char *gsm_auth_policy_name(enum gsm_auth_policy policy)
359{
360 if (policy > ARRAY_SIZE(gsm_auth_policy_names))
361 return "undefined";
362 return gsm_auth_policy_names[policy];
363}
364