blob: 0ed08454ae609cd8f971bc0cab25894e26144661 [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 Welte52b1f982008-12-23 20:25:15 +000026
Harald Welte8470bf22008-12-25 23:28:35 +000027#include <openbsc/gsm_data.h>
Harald Weltee441d9c2009-06-21 16:17:15 +020028#include <openbsc/talloc.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Andreas Eversberg8226fa72009-06-29 15:19:38 +020030void *tall_bsc_ctx;
31
Harald Weltecd06bfb2009-02-10 17:33:56 +000032void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr,
33 u_int8_t e1_ts, u_int8_t e1_ts_ss)
34{
35 ts->e1_link.e1_nr = e1_nr;
36 ts->e1_link.e1_ts = e1_ts;
37 ts->e1_link.e1_ts_ss = e1_ts_ss;
38}
39
Harald Weltea72c98e2009-01-04 16:10:38 +000040static const char *pchan_names[] = {
41 [GSM_PCHAN_NONE] = "NONE",
42 [GSM_PCHAN_CCCH] = "CCCH",
43 [GSM_PCHAN_CCCH_SDCCH4] = "CCCH+SDCCH4",
44 [GSM_PCHAN_TCH_F] = "TCH/F",
45 [GSM_PCHAN_TCH_H] = "TCH/H",
46 [GSM_PCHAN_SDCCH8_SACCH8C] = "SDCCH8",
47 [GSM_PCHAN_UNKNOWN] = "UNKNOWN",
48};
49
50const char *gsm_pchan_name(enum gsm_phys_chan_config c)
51{
52 if (c >= ARRAY_SIZE(pchan_names))
53 return "INVALID";
54
55 return pchan_names[c];
56}
57
Harald Weltea171a1b2009-08-07 00:24:39 +020058enum gsm_phys_chan_config gsm_pchan_parse(const char *name)
59{
60 int i;
61
62 for (i = 0; i < ARRAY_SIZE(pchan_names); i++) {
63 if (!strcasecmp(name, pchan_names[i]))
64 return i;
65 }
66
67 return -1;
68}
69
Harald Weltea72c98e2009-01-04 16:10:38 +000070static const char *lchan_names[] = {
71 [GSM_LCHAN_NONE] = "NONE",
72 [GSM_LCHAN_SDCCH] = "SDCCH",
73 [GSM_LCHAN_TCH_F] = "TCH/F",
74 [GSM_LCHAN_TCH_H] = "TCH/H",
75 [GSM_LCHAN_UNKNOWN] = "UNKNOWN",
76};
77
78const char *gsm_lchan_name(enum gsm_chan_t c)
79{
80 if (c >= ARRAY_SIZE(lchan_names))
81 return "INVALID";
82
83 return lchan_names[c];
84}
85
86static const char *chreq_names[] = {
87 [GSM_CHREQ_REASON_EMERG] = "EMERGENCY",
88 [GSM_CHREQ_REASON_PAG] = "PAGING",
89 [GSM_CHREQ_REASON_CALL] = "CALL",
90 [GSM_CHREQ_REASON_LOCATION_UPD] = "LOCATION_UPDATE",
91 [GSM_CHREQ_REASON_OTHER] = "OTHER",
92};
93
94const char *gsm_chreq_name(enum gsm_chreq_reason_t c)
95{
96 if (c >= ARRAY_SIZE(chreq_names))
97 return "INVALID";
98
99 return chreq_names[c];
100}
101
Harald Weltee441d9c2009-06-21 16:17:15 +0200102struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000103{
Harald Weltee441d9c2009-06-21 16:17:15 +0200104 struct gsm_bts_trx *trx = talloc(bts, struct gsm_bts_trx);
105 int k;
Harald Welte52b1f982008-12-23 20:25:15 +0000106
Harald Weltee441d9c2009-06-21 16:17:15 +0200107 if (!trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000108 return NULL;
109
Harald Weltee441d9c2009-06-21 16:17:15 +0200110 memset(trx, 0, sizeof(*trx));
111 trx->bts = bts;
112 trx->nr = bts->num_trx++;
113
114 for (k = 0; k < TRX_NR_TS; k++) {
115 struct gsm_bts_trx_ts *ts = &trx->ts[k];
116 int l;
117
118 ts->trx = trx;
119 ts->nr = k;
120 ts->pchan = GSM_PCHAN_NONE;
121
122 for (l = 0; l < TS_MAX_LCHAN; l++) {
123 struct gsm_lchan *lchan;
124 lchan = &ts->lchan[l];
125
126 lchan->ts = ts;
127 lchan->nr = l;
128 lchan->type = GSM_LCHAN_NONE;
129 }
130 }
131
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200132 llist_add_tail(&trx->list, &bts->trx_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200133
134 return trx;
135}
136
137struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, enum gsm_bts_type type,
138 u_int8_t tsc, u_int8_t bsic)
139{
140 struct gsm_bts *bts = talloc(net, struct gsm_bts);
141
142 if (!bts)
143 return NULL;
144
145 memset(bts, 0, sizeof(*bts));
146 bts->network = net;
147 bts->nr = net->num_bts++;
148 bts->type = type;
149 bts->tsc = tsc;
150 bts->bsic = bsic;
151 bts->num_trx = 0;
152 INIT_LLIST_HEAD(&bts->trx_list);
153
154 /* create our primary TRX */
155 bts->c0 = gsm_bts_trx_alloc(bts);
156 if (!bts->c0) {
157 talloc_free(bts);
158 return NULL;
159 }
160 bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
161
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200162 llist_add_tail(&bts->list, &net->bts_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200163
164 return bts;
165}
166
167struct gsm_network *gsm_network_init(u_int16_t country_code, u_int16_t network_code,
168 int (*mncc_recv)(struct gsm_network *, int, void *))
169{
170 struct gsm_network *net;
171
172 net = talloc(tall_bsc_ctx, struct gsm_network);
Harald Welte52b1f982008-12-23 20:25:15 +0000173 if (!net)
174 return NULL;
175 memset(net, 0, sizeof(*net));
176
177 net->country_code = country_code;
178 net->network_code = network_code;
Harald Weltee441d9c2009-06-21 16:17:15 +0200179 net->num_bts = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000180
Harald Welte4bfdfe72009-06-10 23:11:52 +0800181 INIT_LLIST_HEAD(&net->trans_list);
182 INIT_LLIST_HEAD(&net->upqueue);
Harald Weltee441d9c2009-06-21 16:17:15 +0200183 INIT_LLIST_HEAD(&net->bts_list);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800184
185 net->mncc_recv = mncc_recv;
186
Harald Welte8470bf22008-12-25 23:28:35 +0000187 return net;
Harald Welte52b1f982008-12-23 20:25:15 +0000188}
Harald Welte23a68632009-02-19 17:06:42 +0000189
Harald Weltee441d9c2009-06-21 16:17:15 +0200190struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
191{
192 struct gsm_bts *bts;
193
194 if (num >= net->num_bts)
195 return NULL;
196
197 llist_for_each_entry(bts, &net->bts_list, list) {
198 if (bts->nr == num)
199 return bts;
200 }
201
202 return NULL;
203}
204
205struct gsm_bts_trx *gsm_bts_trx_num(struct gsm_bts *bts, int num)
206{
207 struct gsm_bts_trx *trx;
208
209 if (num >= bts->num_trx)
210 return NULL;
211
212 llist_for_each_entry(trx, &bts->trx_list, list) {
213 if (trx->nr == num)
214 return trx;
215 }
216
217 return NULL;
218}
219
Harald Welte23a68632009-02-19 17:06:42 +0000220static char ts2str[255];
221
222char *gsm_ts_name(struct gsm_bts_trx_ts *ts)
223{
224 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)",
Harald Welte90f64762009-05-01 17:22:09 +0000225 ts->trx->bts->bts_nr, ts->trx->nr, ts->nr);
Harald Welte23a68632009-02-19 17:06:42 +0000226
227 return ts2str;
228}
Harald Welte32201c12009-03-10 12:15:10 +0000229
230static const char *bts_types[] = {
231 [GSM_BTS_TYPE_UNKNOWN] = "unknown",
232 [GSM_BTS_TYPE_BS11] = "bs11",
233 [GSM_BTS_TYPE_NANOBTS_900] = "nanobts900",
234 [GSM_BTS_TYPE_NANOBTS_1800] = "nanobts1800",
235};
236
237enum gsm_bts_type parse_btstype(char *arg)
238{
239 int i;
240 for (i = 0; i < ARRAY_SIZE(bts_types); i++) {
241 if (!strcmp(arg, bts_types[i]))
242 return i;
243 }
244 return GSM_BTS_TYPE_BS11; /* Default: BS11 */
245}
246
Holger Hans Peter Freyther2dceae62009-06-12 17:39:38 +0200247const char *btstype2str(enum gsm_bts_type type)
Harald Welte32201c12009-03-10 12:15:10 +0000248{
249 if (type > ARRAY_SIZE(bts_types))
250 return "undefined";
251 return bts_types[type];
252}
Harald Weltebe991492009-05-23 13:56:40 +0000253
254/* Search for a BTS in the given Location Area; optionally start searching
255 * with start_bts (for continuing to search after the first result) */
256struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
257 struct gsm_bts *start_bts)
258{
259 int i;
260 struct gsm_bts *bts;
261 int skip = 0;
262
263 if (start_bts)
264 skip = 1;
265
266 for (i = 0; i < net->num_bts; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200267 bts = gsm_bts_num(net, i);
Harald Weltebe991492009-05-23 13:56:40 +0000268
269 if (skip) {
270 if (start_bts == bts)
271 skip = 0;
272 continue;
273 }
274
275 if (bts->location_area_code == lac)
276 return bts;
277 }
278 return NULL;
279}
Harald Weltefcd24452009-06-20 18:15:19 +0200280
281char *gsm_band_name(enum gsm_band band)
282{
283 switch (band) {
284 case GSM_BAND_400:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200285 return "GSM400";
Harald Weltefcd24452009-06-20 18:15:19 +0200286 case GSM_BAND_850:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200287 return "GSM850";
Harald Weltefcd24452009-06-20 18:15:19 +0200288 case GSM_BAND_900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200289 return "GSM900";
Harald Weltefcd24452009-06-20 18:15:19 +0200290 case GSM_BAND_1800:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200291 return "DCS1800";
Harald Weltefcd24452009-06-20 18:15:19 +0200292 case GSM_BAND_1900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200293 return "PCS1900";
Harald Weltefcd24452009-06-20 18:15:19 +0200294 }
295 return "invalid";
296}
297
298enum gsm_band gsm_band_parse(int mhz)
299{
300 switch (mhz) {
301 case 400:
302 return GSM_BAND_400;
303 case 850:
304 return GSM_BAND_850;
305 case 900:
306 return GSM_BAND_900;
307 case 1800:
308 return GSM_BAND_1800;
309 case 1900:
310 return GSM_BAND_1900;
311 default:
312 return -EINVAL;
313 }
314}
315