blob: 7e8100dcb1e46a475e9287e7a9a325cbbe0885e1 [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>
Harald Welte52b1f982008-12-23 20:25:15 +000030
Andreas Eversberg8226fa72009-06-29 15:19:38 +020031void *tall_bsc_ctx;
32
Harald Weltecd06bfb2009-02-10 17:33:56 +000033void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr,
34 u_int8_t e1_ts, u_int8_t e1_ts_ss)
35{
36 ts->e1_link.e1_nr = e1_nr;
37 ts->e1_link.e1_ts = e1_ts;
38 ts->e1_link.e1_ts_ss = e1_ts_ss;
39}
40
Harald Weltea72c98e2009-01-04 16:10:38 +000041static const char *pchan_names[] = {
42 [GSM_PCHAN_NONE] = "NONE",
43 [GSM_PCHAN_CCCH] = "CCCH",
44 [GSM_PCHAN_CCCH_SDCCH4] = "CCCH+SDCCH4",
45 [GSM_PCHAN_TCH_F] = "TCH/F",
46 [GSM_PCHAN_TCH_H] = "TCH/H",
47 [GSM_PCHAN_SDCCH8_SACCH8C] = "SDCCH8",
48 [GSM_PCHAN_UNKNOWN] = "UNKNOWN",
49};
50
51const char *gsm_pchan_name(enum gsm_phys_chan_config c)
52{
53 if (c >= ARRAY_SIZE(pchan_names))
54 return "INVALID";
55
56 return pchan_names[c];
57}
58
Harald Weltea171a1b2009-08-07 00:24:39 +020059enum gsm_phys_chan_config gsm_pchan_parse(const char *name)
60{
61 int i;
62
63 for (i = 0; i < ARRAY_SIZE(pchan_names); i++) {
64 if (!strcasecmp(name, pchan_names[i]))
65 return i;
66 }
67
68 return -1;
69}
70
Harald Weltea72c98e2009-01-04 16:10:38 +000071static const char *lchan_names[] = {
72 [GSM_LCHAN_NONE] = "NONE",
73 [GSM_LCHAN_SDCCH] = "SDCCH",
74 [GSM_LCHAN_TCH_F] = "TCH/F",
75 [GSM_LCHAN_TCH_H] = "TCH/H",
76 [GSM_LCHAN_UNKNOWN] = "UNKNOWN",
77};
78
79const char *gsm_lchan_name(enum gsm_chan_t c)
80{
81 if (c >= ARRAY_SIZE(lchan_names))
82 return "INVALID";
83
84 return lchan_names[c];
85}
86
87static const char *chreq_names[] = {
88 [GSM_CHREQ_REASON_EMERG] = "EMERGENCY",
89 [GSM_CHREQ_REASON_PAG] = "PAGING",
90 [GSM_CHREQ_REASON_CALL] = "CALL",
91 [GSM_CHREQ_REASON_LOCATION_UPD] = "LOCATION_UPDATE",
92 [GSM_CHREQ_REASON_OTHER] = "OTHER",
93};
94
95const char *gsm_chreq_name(enum gsm_chreq_reason_t c)
96{
97 if (c >= ARRAY_SIZE(chreq_names))
98 return "INVALID";
99
100 return chreq_names[c];
101}
102
Harald Weltee441d9c2009-06-21 16:17:15 +0200103struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000104{
Harald Weltee441d9c2009-06-21 16:17:15 +0200105 struct gsm_bts_trx *trx = talloc(bts, struct gsm_bts_trx);
106 int k;
Harald Welte52b1f982008-12-23 20:25:15 +0000107
Harald Weltee441d9c2009-06-21 16:17:15 +0200108 if (!trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000109 return NULL;
110
Harald Weltee441d9c2009-06-21 16:17:15 +0200111 memset(trx, 0, sizeof(*trx));
112 trx->bts = bts;
113 trx->nr = bts->num_trx++;
114
115 for (k = 0; k < TRX_NR_TS; k++) {
116 struct gsm_bts_trx_ts *ts = &trx->ts[k];
117 int l;
118
119 ts->trx = trx;
120 ts->nr = k;
121 ts->pchan = GSM_PCHAN_NONE;
122
123 for (l = 0; l < TS_MAX_LCHAN; l++) {
124 struct gsm_lchan *lchan;
125 lchan = &ts->lchan[l];
126
127 lchan->ts = ts;
128 lchan->nr = l;
129 lchan->type = GSM_LCHAN_NONE;
130 }
131 }
132
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200133 llist_add_tail(&trx->list, &bts->trx_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200134
135 return trx;
136}
137
138struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, enum gsm_bts_type type,
139 u_int8_t tsc, u_int8_t bsic)
140{
141 struct gsm_bts *bts = talloc(net, struct gsm_bts);
142
143 if (!bts)
144 return NULL;
145
146 memset(bts, 0, sizeof(*bts));
147 bts->network = net;
148 bts->nr = net->num_bts++;
149 bts->type = type;
150 bts->tsc = tsc;
151 bts->bsic = bsic;
152 bts->num_trx = 0;
153 INIT_LLIST_HEAD(&bts->trx_list);
Harald Welte (local)0e451d02009-08-13 10:14:26 +0200154 bts->ms_max_power = 15; /* dBm */
Harald Weltee441d9c2009-06-21 16:17:15 +0200155
156 /* create our primary TRX */
157 bts->c0 = gsm_bts_trx_alloc(bts);
158 if (!bts->c0) {
159 talloc_free(bts);
160 return NULL;
161 }
162 bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
163
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200164 llist_add_tail(&bts->list, &net->bts_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200165
166 return bts;
167}
168
169struct gsm_network *gsm_network_init(u_int16_t country_code, u_int16_t network_code,
170 int (*mncc_recv)(struct gsm_network *, int, void *))
171{
172 struct gsm_network *net;
173
174 net = talloc(tall_bsc_ctx, struct gsm_network);
Harald Welte52b1f982008-12-23 20:25:15 +0000175 if (!net)
176 return NULL;
177 memset(net, 0, sizeof(*net));
178
179 net->country_code = country_code;
180 net->network_code = network_code;
Harald Weltee441d9c2009-06-21 16:17:15 +0200181 net->num_bts = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000182
Harald Welte4bfdfe72009-06-10 23:11:52 +0800183 INIT_LLIST_HEAD(&net->trans_list);
184 INIT_LLIST_HEAD(&net->upqueue);
Harald Weltee441d9c2009-06-21 16:17:15 +0200185 INIT_LLIST_HEAD(&net->bts_list);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800186
187 net->mncc_recv = mncc_recv;
188
Harald Welte8470bf22008-12-25 23:28:35 +0000189 return net;
Harald Welte52b1f982008-12-23 20:25:15 +0000190}
Harald Welte23a68632009-02-19 17:06:42 +0000191
Harald Weltee441d9c2009-06-21 16:17:15 +0200192struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
193{
194 struct gsm_bts *bts;
195
196 if (num >= net->num_bts)
197 return NULL;
198
199 llist_for_each_entry(bts, &net->bts_list, list) {
200 if (bts->nr == num)
201 return bts;
202 }
203
204 return NULL;
205}
206
207struct gsm_bts_trx *gsm_bts_trx_num(struct gsm_bts *bts, int num)
208{
209 struct gsm_bts_trx *trx;
210
211 if (num >= bts->num_trx)
212 return NULL;
213
214 llist_for_each_entry(trx, &bts->trx_list, list) {
215 if (trx->nr == num)
216 return trx;
217 }
218
219 return NULL;
220}
221
Harald Welte23a68632009-02-19 17:06:42 +0000222static char ts2str[255];
223
224char *gsm_ts_name(struct gsm_bts_trx_ts *ts)
225{
226 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)",
Harald Welte90f64762009-05-01 17:22:09 +0000227 ts->trx->bts->bts_nr, ts->trx->nr, ts->nr);
Harald Welte23a68632009-02-19 17:06:42 +0000228
229 return ts2str;
230}
Harald Welte32201c12009-03-10 12:15:10 +0000231
232static const char *bts_types[] = {
233 [GSM_BTS_TYPE_UNKNOWN] = "unknown",
234 [GSM_BTS_TYPE_BS11] = "bs11",
235 [GSM_BTS_TYPE_NANOBTS_900] = "nanobts900",
236 [GSM_BTS_TYPE_NANOBTS_1800] = "nanobts1800",
237};
238
Harald Welte1d014a52009-08-08 15:38:29 +0200239enum gsm_bts_type parse_btstype(const char *arg)
Harald Welte32201c12009-03-10 12:15:10 +0000240{
241 int i;
242 for (i = 0; i < ARRAY_SIZE(bts_types); i++) {
243 if (!strcmp(arg, bts_types[i]))
244 return i;
245 }
246 return GSM_BTS_TYPE_BS11; /* Default: BS11 */
247}
248
Holger Hans Peter Freyther2dceae62009-06-12 17:39:38 +0200249const char *btstype2str(enum gsm_bts_type type)
Harald Welte32201c12009-03-10 12:15:10 +0000250{
251 if (type > ARRAY_SIZE(bts_types))
252 return "undefined";
253 return bts_types[type];
254}
Harald Weltebe991492009-05-23 13:56:40 +0000255
256/* Search for a BTS in the given Location Area; optionally start searching
257 * with start_bts (for continuing to search after the first result) */
258struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
259 struct gsm_bts *start_bts)
260{
261 int i;
262 struct gsm_bts *bts;
263 int skip = 0;
264
265 if (start_bts)
266 skip = 1;
267
268 for (i = 0; i < net->num_bts; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200269 bts = gsm_bts_num(net, i);
Harald Weltebe991492009-05-23 13:56:40 +0000270
271 if (skip) {
272 if (start_bts == bts)
273 skip = 0;
274 continue;
275 }
276
277 if (bts->location_area_code == lac)
278 return bts;
279 }
280 return NULL;
281}
Harald Weltefcd24452009-06-20 18:15:19 +0200282
283char *gsm_band_name(enum gsm_band band)
284{
285 switch (band) {
286 case GSM_BAND_400:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200287 return "GSM400";
Harald Weltefcd24452009-06-20 18:15:19 +0200288 case GSM_BAND_850:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200289 return "GSM850";
Harald Weltefcd24452009-06-20 18:15:19 +0200290 case GSM_BAND_900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200291 return "GSM900";
Harald Weltefcd24452009-06-20 18:15:19 +0200292 case GSM_BAND_1800:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200293 return "DCS1800";
Harald Weltefcd24452009-06-20 18:15:19 +0200294 case GSM_BAND_1900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200295 return "PCS1900";
Harald Weltefcd24452009-06-20 18:15:19 +0200296 }
297 return "invalid";
298}
299
Harald Welte42581822009-08-08 16:12:58 +0200300enum gsm_band gsm_band_parse(const char* mhz)
Harald Weltefcd24452009-06-20 18:15:19 +0200301{
Harald Welte42581822009-08-08 16:12:58 +0200302 while (*mhz && !isdigit(*mhz))
303 mhz++;
304
305 if (*mhz == '\0')
306 return -EINVAL;
307
308 switch (atoi(mhz)) {
Harald Weltefcd24452009-06-20 18:15:19 +0200309 case 400:
310 return GSM_BAND_400;
311 case 850:
312 return GSM_BAND_850;
313 case 900:
314 return GSM_BAND_900;
315 case 1800:
316 return GSM_BAND_1800;
317 case 1900:
318 return GSM_BAND_1900;
319 default:
320 return -EINVAL;
321 }
322}
323
Harald Welte (local)69de3972009-08-12 14:42:23 +0200324static const char *gsm_auth_policy_names[] = {
325 [GSM_AUTH_POLICY_CLOSED] = "closed",
326 [GSM_AUTH_POLICY_ACCEPT_ALL] = "accept-all",
327 [GSM_AUTH_POLICY_TOKEN] = "token",
328};
329
330enum gsm_auth_policy gsm_auth_policy_parse(const char *arg)
331{
332 int i;
333 for (i = 0; i < ARRAY_SIZE(gsm_auth_policy_names); i++) {
334 if (!strcmp(arg, gsm_auth_policy_names[i]))
335 return i;
336 }
337 return GSM_AUTH_POLICY_CLOSED;
338}
339
340const char *gsm_auth_policy_name(enum gsm_auth_policy policy)
341{
342 if (policy > ARRAY_SIZE(gsm_auth_policy_names))
343 return "undefined";
344 return gsm_auth_policy_names[policy];
345}
346