blob: e2f56d539946fec22664054e2eabec0512855566 [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 Welteffa55a42009-12-22 19:07:32 +010031#include <openbsc/statistics.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Andreas Eversberg8226fa72009-06-29 15:19:38 +020033void *tall_bsc_ctx;
34
Harald Welteccda9652009-11-12 22:28:18 +090035const char *get_value_string(const struct value_string *vs, u_int32_t val)
36{
37 int i;
38
39 for (i = 0;; i++) {
40 if (vs[i].value == 0 && vs[i].str == NULL)
41 break;
42 if (vs[i].value == val)
43 return vs[i].str;
44 }
45 return "unknown";
46}
47
Harald Weltecd06bfb2009-02-10 17:33:56 +000048void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr,
49 u_int8_t e1_ts, u_int8_t e1_ts_ss)
50{
51 ts->e1_link.e1_nr = e1_nr;
52 ts->e1_link.e1_ts = e1_ts;
53 ts->e1_link.e1_ts_ss = e1_ts_ss;
54}
55
Harald Weltea72c98e2009-01-04 16:10:38 +000056static const char *pchan_names[] = {
57 [GSM_PCHAN_NONE] = "NONE",
58 [GSM_PCHAN_CCCH] = "CCCH",
59 [GSM_PCHAN_CCCH_SDCCH4] = "CCCH+SDCCH4",
60 [GSM_PCHAN_TCH_F] = "TCH/F",
61 [GSM_PCHAN_TCH_H] = "TCH/H",
62 [GSM_PCHAN_SDCCH8_SACCH8C] = "SDCCH8",
Harald Weltea1499d02009-10-24 10:25:50 +020063 [GSM_PCHAN_PDCH] = "PDCH",
64 [GSM_PCHAN_TCH_F_PDCH] = "TCH/F_PDCH",
Harald Weltea72c98e2009-01-04 16:10:38 +000065 [GSM_PCHAN_UNKNOWN] = "UNKNOWN",
66};
67
68const char *gsm_pchan_name(enum gsm_phys_chan_config c)
69{
70 if (c >= ARRAY_SIZE(pchan_names))
71 return "INVALID";
72
73 return pchan_names[c];
74}
75
Harald Weltea171a1b2009-08-07 00:24:39 +020076enum gsm_phys_chan_config gsm_pchan_parse(const char *name)
77{
78 int i;
79
80 for (i = 0; i < ARRAY_SIZE(pchan_names); i++) {
81 if (!strcasecmp(name, pchan_names[i]))
82 return i;
83 }
84
85 return -1;
86}
87
Harald Weltea72c98e2009-01-04 16:10:38 +000088static const char *lchan_names[] = {
89 [GSM_LCHAN_NONE] = "NONE",
90 [GSM_LCHAN_SDCCH] = "SDCCH",
91 [GSM_LCHAN_TCH_F] = "TCH/F",
92 [GSM_LCHAN_TCH_H] = "TCH/H",
93 [GSM_LCHAN_UNKNOWN] = "UNKNOWN",
94};
95
96const char *gsm_lchan_name(enum gsm_chan_t c)
97{
98 if (c >= ARRAY_SIZE(lchan_names))
99 return "INVALID";
100
101 return lchan_names[c];
102}
103
104static const char *chreq_names[] = {
105 [GSM_CHREQ_REASON_EMERG] = "EMERGENCY",
106 [GSM_CHREQ_REASON_PAG] = "PAGING",
107 [GSM_CHREQ_REASON_CALL] = "CALL",
108 [GSM_CHREQ_REASON_LOCATION_UPD] = "LOCATION_UPDATE",
109 [GSM_CHREQ_REASON_OTHER] = "OTHER",
110};
111
112const char *gsm_chreq_name(enum gsm_chreq_reason_t c)
113{
114 if (c >= ARRAY_SIZE(chreq_names))
115 return "INVALID";
116
117 return chreq_names[c];
118}
119
Harald Weltee441d9c2009-06-21 16:17:15 +0200120struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000121{
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100122 struct gsm_bts_trx *trx = talloc_zero(bts, struct gsm_bts_trx);
Harald Weltee441d9c2009-06-21 16:17:15 +0200123 int k;
Harald Welte52b1f982008-12-23 20:25:15 +0000124
Harald Weltee441d9c2009-06-21 16:17:15 +0200125 if (!trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000126 return NULL;
127
Harald Weltee441d9c2009-06-21 16:17:15 +0200128 trx->bts = bts;
129 trx->nr = bts->num_trx++;
130
131 for (k = 0; k < TRX_NR_TS; k++) {
132 struct gsm_bts_trx_ts *ts = &trx->ts[k];
133 int l;
134
135 ts->trx = trx;
136 ts->nr = k;
137 ts->pchan = GSM_PCHAN_NONE;
138
139 for (l = 0; l < TS_MAX_LCHAN; l++) {
140 struct gsm_lchan *lchan;
141 lchan = &ts->lchan[l];
142
143 lchan->ts = ts;
144 lchan->nr = l;
145 lchan->type = GSM_LCHAN_NONE;
146 }
147 }
148
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200149 llist_add_tail(&trx->list, &bts->trx_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200150
151 return trx;
152}
153
154struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, enum gsm_bts_type type,
155 u_int8_t tsc, u_int8_t bsic)
156{
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100157 struct gsm_bts *bts = talloc_zero(net, struct gsm_bts);
Harald Welte55dd4432009-10-24 10:19:14 +0200158 int i;
Harald Weltee441d9c2009-06-21 16:17:15 +0200159
160 if (!bts)
161 return NULL;
162
Harald Weltee441d9c2009-06-21 16:17:15 +0200163 bts->network = net;
164 bts->nr = net->num_bts++;
165 bts->type = type;
166 bts->tsc = tsc;
167 bts->bsic = bsic;
168 bts->num_trx = 0;
169 INIT_LLIST_HEAD(&bts->trx_list);
Harald Welte (local)0e451d02009-08-13 10:14:26 +0200170 bts->ms_max_power = 15; /* dBm */
Harald Welte73225282009-12-12 18:17:25 +0100171 bts->si_common.cell_sel_par.cell_resel_hyst = 2; /* 4 dB */
172 bts->si_common.cell_sel_par.rxlev_acc_min = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100173 bts->si_common.neigh_list.data = bts->si_common.data.neigh_list;
174 bts->si_common.neigh_list.data_len =
175 sizeof(bts->si_common.data.neigh_list);
176 bts->si_common.cell_alloc.data = bts->si_common.data.cell_alloc;
177 bts->si_common.cell_alloc.data_len =
178 sizeof(bts->si_common.data.cell_alloc);
Sylvain Munaut4010f1e2009-12-22 13:43:26 +0100179 bts->si_common.rach_control.re = 1; /* no re-establishment */
180 bts->si_common.rach_control.tx_integer = 9; /* 12 slots spread - 217/115 slots delay */
181 bts->si_common.rach_control.max_trans = 3; /* 7 retransmissions */
182 bts->si_common.rach_control.t2 = 4; /* no emergency calls */
Harald Weltee441d9c2009-06-21 16:17:15 +0200183
Harald Welte55dd4432009-10-24 10:19:14 +0200184 for (i = 0; i < ARRAY_SIZE(bts->gprs.nsvc); i++) {
185 bts->gprs.nsvc[i].bts = bts;
186 bts->gprs.nsvc[i].id = i;
187 }
188
Harald Weltee441d9c2009-06-21 16:17:15 +0200189 /* create our primary TRX */
190 bts->c0 = gsm_bts_trx_alloc(bts);
191 if (!bts->c0) {
192 talloc_free(bts);
193 return NULL;
194 }
195 bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
196
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200197 llist_add_tail(&bts->list, &net->bts_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200198
199 return bts;
200}
201
202struct gsm_network *gsm_network_init(u_int16_t country_code, u_int16_t network_code,
203 int (*mncc_recv)(struct gsm_network *, int, void *))
204{
205 struct gsm_network *net;
206
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100207 net = talloc_zero(tall_bsc_ctx, struct gsm_network);
Harald Welte52b1f982008-12-23 20:25:15 +0000208 if (!net)
209 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000210
211 net->country_code = country_code;
212 net->network_code = network_code;
Harald Weltee441d9c2009-06-21 16:17:15 +0200213 net->num_bts = 0;
Harald Welteb84ddfc2009-12-01 17:36:54 +0530214 net->T3101 = GSM_T3101_DEFAULT;
215 net->T3113 = GSM_T3113_DEFAULT;
216 /* FIXME: initialize all other timers! */
Harald Welte52b1f982008-12-23 20:25:15 +0000217
Harald Weltef7c28b02009-12-21 13:30:17 +0100218 /* default set of handover parameters */
219 net->handover.win_rxlev_avg = 10;
220 net->handover.win_rxqual_avg = 1;
221 net->handover.win_rxlev_avg_neigh = 10;
222 net->handover.pwr_interval = 6;
223 net->handover.pwr_hysteresis = 3;
224 net->handover.max_distance = 9999;
225
Harald Welte4bfdfe72009-06-10 23:11:52 +0800226 INIT_LLIST_HEAD(&net->trans_list);
227 INIT_LLIST_HEAD(&net->upqueue);
Harald Weltee441d9c2009-06-21 16:17:15 +0200228 INIT_LLIST_HEAD(&net->bts_list);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800229
Harald Welteffa55a42009-12-22 19:07:32 +0100230 net->stats.chreq.total = counter_alloc("net.chreq.total");
231 net->stats.chreq.no_channel = counter_alloc("net.chreq.no_channel");
232 net->stats.handover.attempted = counter_alloc("net.handover.attempted");
233 net->stats.handover.no_channel = counter_alloc("net.handover.no_channel");
234 net->stats.handover.timeout = counter_alloc("net.handover.timeout");
235 net->stats.handover.completed = counter_alloc("net.handover.completed");
236 net->stats.handover.failed = counter_alloc("net.handover.failed");
237 net->stats.loc_upd_type.attach = counter_alloc("net.loc_upd_type.attach");
238 net->stats.loc_upd_type.normal = counter_alloc("net.loc_upd_type.normal");
239 net->stats.loc_upd_type.periodic = counter_alloc("net.loc_upd_type.periodic");
240 net->stats.loc_upd_type.detach = counter_alloc("net.imsi_detach.count");
241 net->stats.loc_upd_resp.reject = counter_alloc("net.loc_upd_resp.reject");
242 net->stats.loc_upd_resp.accept = counter_alloc("net.loc_upd_resp.accept");
243 net->stats.paging.attempted = counter_alloc("net.paging.attempted");
244 net->stats.paging.detached = counter_alloc("net.paging.detached");
245 net->stats.paging.completed = counter_alloc("net.paging.completed");
246 net->stats.paging.expired = counter_alloc("net.paging.expired");
247 net->stats.sms.submitted = counter_alloc("net.sms.submitted");
248 net->stats.sms.no_receiver = counter_alloc("net.sms.no_receiver");
249 net->stats.sms.delivered = counter_alloc("net.sms.delivered");
250 net->stats.sms.rp_err_mem = counter_alloc("net.sms.rp_err_mem");
251 net->stats.sms.rp_err_other = counter_alloc("net.sms.rp_err_other");
252 net->stats.call.dialled = counter_alloc("net.call.dialled");
253 net->stats.call.alerted = counter_alloc("net.call.alerted");
254 net->stats.call.connected = counter_alloc("net.call.connected");
255
Harald Welte4bfdfe72009-06-10 23:11:52 +0800256 net->mncc_recv = mncc_recv;
257
Harald Welte8470bf22008-12-25 23:28:35 +0000258 return net;
Harald Welte52b1f982008-12-23 20:25:15 +0000259}
Harald Welte23a68632009-02-19 17:06:42 +0000260
Harald Weltee441d9c2009-06-21 16:17:15 +0200261struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
262{
263 struct gsm_bts *bts;
264
265 if (num >= net->num_bts)
266 return NULL;
267
268 llist_for_each_entry(bts, &net->bts_list, list) {
269 if (bts->nr == num)
270 return bts;
271 }
272
273 return NULL;
274}
275
Harald Welte84874c92009-12-14 22:33:02 +0100276/* Get reference to a neighbor cell on a given BCCH ARFCN */
Harald Welte0b121032009-12-15 00:21:31 +0100277struct gsm_bts *gsm_bts_neighbor(const struct gsm_bts *bts,
278 u_int16_t arfcn, u_int8_t bsic)
Harald Welte84874c92009-12-14 22:33:02 +0100279{
280 struct gsm_bts *neigh;
281 /* FIXME: use some better heuristics here to determine which cell
282 * using this ARFCN really is closest to the target cell. For
283 * now we simply assume that each ARFCN will only be used by one
284 * cell */
285
286 llist_for_each_entry(neigh, &bts->network->bts_list, list) {
Harald Welte0b121032009-12-15 00:21:31 +0100287 if (neigh->c0->arfcn == arfcn &&
288 neigh->bsic == bsic)
Harald Welte84874c92009-12-14 22:33:02 +0100289 return neigh;
290 }
291
292 return NULL;
293}
294
Harald Weltee441d9c2009-06-21 16:17:15 +0200295struct gsm_bts_trx *gsm_bts_trx_num(struct gsm_bts *bts, int num)
296{
297 struct gsm_bts_trx *trx;
298
299 if (num >= bts->num_trx)
300 return NULL;
301
302 llist_for_each_entry(trx, &bts->trx_list, list) {
303 if (trx->nr == num)
304 return trx;
305 }
306
307 return NULL;
308}
309
Harald Welte23a68632009-02-19 17:06:42 +0000310static char ts2str[255];
311
312char *gsm_ts_name(struct gsm_bts_trx_ts *ts)
313{
314 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)",
Mike Haben2449b372009-10-26 20:36:34 +0100315 ts->trx->bts->nr, ts->trx->nr, ts->nr);
Harald Welte23a68632009-02-19 17:06:42 +0000316
317 return ts2str;
318}
Harald Welte32201c12009-03-10 12:15:10 +0000319
320static const char *bts_types[] = {
321 [GSM_BTS_TYPE_UNKNOWN] = "unknown",
322 [GSM_BTS_TYPE_BS11] = "bs11",
Mike Habene2d82272009-10-02 12:19:34 +0100323 [GSM_BTS_TYPE_NANOBTS] = "nanobts",
Harald Welte32201c12009-03-10 12:15:10 +0000324};
325
Harald Welte1d014a52009-08-08 15:38:29 +0200326enum gsm_bts_type parse_btstype(const char *arg)
Harald Welte32201c12009-03-10 12:15:10 +0000327{
328 int i;
329 for (i = 0; i < ARRAY_SIZE(bts_types); i++) {
330 if (!strcmp(arg, bts_types[i]))
331 return i;
332 }
333 return GSM_BTS_TYPE_BS11; /* Default: BS11 */
334}
335
Holger Hans Peter Freyther2dceae62009-06-12 17:39:38 +0200336const char *btstype2str(enum gsm_bts_type type)
Harald Welte32201c12009-03-10 12:15:10 +0000337{
338 if (type > ARRAY_SIZE(bts_types))
339 return "undefined";
340 return bts_types[type];
341}
Harald Weltebe991492009-05-23 13:56:40 +0000342
343/* Search for a BTS in the given Location Area; optionally start searching
344 * with start_bts (for continuing to search after the first result) */
345struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
346 struct gsm_bts *start_bts)
347{
348 int i;
349 struct gsm_bts *bts;
350 int skip = 0;
351
352 if (start_bts)
353 skip = 1;
354
355 for (i = 0; i < net->num_bts; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200356 bts = gsm_bts_num(net, i);
Harald Weltebe991492009-05-23 13:56:40 +0000357
358 if (skip) {
359 if (start_bts == bts)
360 skip = 0;
361 continue;
362 }
363
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200364 if (lac == GSM_LAC_RESERVED_ALL_BTS || bts->location_area_code == lac)
Harald Weltebe991492009-05-23 13:56:40 +0000365 return bts;
366 }
367 return NULL;
368}
Harald Weltefcd24452009-06-20 18:15:19 +0200369
370char *gsm_band_name(enum gsm_band band)
371{
372 switch (band) {
373 case GSM_BAND_400:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200374 return "GSM400";
Harald Weltefcd24452009-06-20 18:15:19 +0200375 case GSM_BAND_850:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200376 return "GSM850";
Harald Weltefcd24452009-06-20 18:15:19 +0200377 case GSM_BAND_900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200378 return "GSM900";
Harald Weltefcd24452009-06-20 18:15:19 +0200379 case GSM_BAND_1800:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200380 return "DCS1800";
Harald Weltefcd24452009-06-20 18:15:19 +0200381 case GSM_BAND_1900:
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200382 return "PCS1900";
Harald Weltefcd24452009-06-20 18:15:19 +0200383 }
384 return "invalid";
385}
386
Harald Welte42581822009-08-08 16:12:58 +0200387enum gsm_band gsm_band_parse(const char* mhz)
Harald Weltefcd24452009-06-20 18:15:19 +0200388{
Harald Welte42581822009-08-08 16:12:58 +0200389 while (*mhz && !isdigit(*mhz))
390 mhz++;
391
392 if (*mhz == '\0')
393 return -EINVAL;
394
395 switch (atoi(mhz)) {
Harald Weltefcd24452009-06-20 18:15:19 +0200396 case 400:
397 return GSM_BAND_400;
398 case 850:
399 return GSM_BAND_850;
400 case 900:
401 return GSM_BAND_900;
402 case 1800:
403 return GSM_BAND_1800;
404 case 1900:
405 return GSM_BAND_1900;
406 default:
407 return -EINVAL;
408 }
409}
410
Harald Welte (local)69de3972009-08-12 14:42:23 +0200411static const char *gsm_auth_policy_names[] = {
412 [GSM_AUTH_POLICY_CLOSED] = "closed",
413 [GSM_AUTH_POLICY_ACCEPT_ALL] = "accept-all",
414 [GSM_AUTH_POLICY_TOKEN] = "token",
415};
416
417enum gsm_auth_policy gsm_auth_policy_parse(const char *arg)
418{
419 int i;
420 for (i = 0; i < ARRAY_SIZE(gsm_auth_policy_names); i++) {
421 if (!strcmp(arg, gsm_auth_policy_names[i]))
422 return i;
423 }
424 return GSM_AUTH_POLICY_CLOSED;
425}
426
427const char *gsm_auth_policy_name(enum gsm_auth_policy policy)
428{
429 if (policy > ARRAY_SIZE(gsm_auth_policy_names))
430 return "undefined";
431 return gsm_auth_policy_names[policy];
432}
433
Harald Welteeab84a12009-12-13 10:53:12 +0100434static const char *rrlp_mode_names[] = {
435 [RRLP_MODE_NONE] = "none",
436 [RRLP_MODE_MS_BASED] = "ms-based",
437 [RRLP_MODE_MS_PREF] = "ms-preferred",
438 [RRLP_MODE_ASS_PREF] = "ass-preferred",
439};
440
441enum rrlp_mode rrlp_mode_parse(const char *arg)
442{
443 int i;
444 for (i = 0; i < ARRAY_SIZE(rrlp_mode_names); i++) {
445 if (!strcmp(arg, rrlp_mode_names[i]))
446 return i;
447 }
448 return RRLP_MODE_NONE;
449}
450
451const char *rrlp_mode_name(enum rrlp_mode mode)
452{
453 if (mode > ARRAY_SIZE(rrlp_mode_names))
454 return "none";
455 return rrlp_mode_names[mode];
456}
Harald Welted12b0fd2009-12-15 21:36:05 +0100457
458struct gsm_meas_rep *lchan_next_meas_rep(struct gsm_lchan *lchan)
459{
460 struct gsm_meas_rep *meas_rep;
461
462 meas_rep = &lchan->meas_rep[lchan->meas_rep_idx];
463 memset(meas_rep, 0, sizeof(*meas_rep));
464 meas_rep->lchan = lchan;
465 lchan->meas_rep_idx = (lchan->meas_rep_idx + 1)
466 % ARRAY_SIZE(lchan->meas_rep);
467
468 return meas_rep;
469}