blob: de3a08d98eb73ced80af44b40a9d21926ed90253 [file] [log] [blame]
Harald Weltefbc5ca02010-03-04 10:39:50 +01001/* (C) 2008-2010 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 Welte97a282b2010-03-14 15:37:43 +080028#include <netinet/in.h>
29
Harald Welte8470bf22008-12-25 23:28:35 +000030#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010031#include <osmocore/talloc.h>
Harald Weltefbc5ca02010-03-04 10:39:50 +010032#include <osmocore/gsm_utils.h>
Holger Hans Peter Freyther2d501ea2009-11-11 11:54:24 +010033#include <openbsc/abis_nm.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010034#include <osmocore/statistics.h>
Harald Welte52b1f982008-12-23 20:25:15 +000035
Andreas Eversberg8226fa72009-06-29 15:19:38 +020036void *tall_bsc_ctx;
37
Harald Welte39315c42010-01-10 18:01:52 +010038static LLIST_HEAD(bts_models);
39
Harald Weltecd06bfb2009-02-10 17:33:56 +000040void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr,
41 u_int8_t e1_ts, u_int8_t e1_ts_ss)
42{
43 ts->e1_link.e1_nr = e1_nr;
44 ts->e1_link.e1_ts = e1_ts;
45 ts->e1_link.e1_ts_ss = e1_ts_ss;
46}
47
Harald Welte92b1fe42010-03-25 11:45:30 +080048static const struct value_string pchan_names[] = {
49 { GSM_PCHAN_NONE, "NONE" },
50 { GSM_PCHAN_CCCH, "CCCH" },
51 { GSM_PCHAN_CCCH_SDCCH4,"CCCH+SDCCH4" },
52 { GSM_PCHAN_TCH_F, "TCH/F" },
53 { GSM_PCHAN_TCH_H, "TCH/H" },
54 { GSM_PCHAN_SDCCH8_SACCH8C, "SDCCH8" },
55 { GSM_PCHAN_PDCH, "PDCH" },
56 { GSM_PCHAN_TCH_F_PDCH, "TCH/F_PDCH" },
57 { GSM_PCHAN_UNKNOWN, "UNKNOWN" },
58 { 0, NULL }
Harald Weltea72c98e2009-01-04 16:10:38 +000059};
60
61const char *gsm_pchan_name(enum gsm_phys_chan_config c)
62{
Harald Welte92b1fe42010-03-25 11:45:30 +080063 return get_value_string(pchan_names, c);
Harald Weltea72c98e2009-01-04 16:10:38 +000064}
65
Harald Weltea171a1b2009-08-07 00:24:39 +020066enum gsm_phys_chan_config gsm_pchan_parse(const char *name)
67{
Harald Welte92b1fe42010-03-25 11:45:30 +080068 return get_string_value(pchan_names, name);
Harald Weltea171a1b2009-08-07 00:24:39 +020069}
70
Harald Welte92b1fe42010-03-25 11:45:30 +080071static const struct value_string lchant_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 { 0, NULL }
Harald Weltea72c98e2009-01-04 16:10:38 +000078};
79
Harald Welte (local)ccd88452009-12-27 18:05:25 +010080const char *gsm_lchant_name(enum gsm_chan_t c)
Harald Weltea72c98e2009-01-04 16:10:38 +000081{
Harald Welte92b1fe42010-03-25 11:45:30 +080082 return get_value_string(lchant_names, c);
Harald Weltea72c98e2009-01-04 16:10:38 +000083}
84
Harald Welte1887f9d2009-12-29 10:52:38 +010085static const struct value_string lchan_s_names[] = {
86 { LCHAN_S_NONE, "NONE" },
87 { LCHAN_S_ACT_REQ, "ACTIVATION REQUESTED" },
88 { LCHAN_S_ACTIVE, "ACTIVE" },
89 { LCHAN_S_INACTIVE, "INACTIVE" },
90 { LCHAN_S_REL_REQ, "RELEASE REQUESTED" },
Holger Hans Peter Freyther4b4dd102010-05-31 21:38:24 +080091 { LCHAN_S_REL_ERR, "RELEASE DUE ERROR" },
Harald Welte92b1fe42010-03-25 11:45:30 +080092 { 0, NULL }
Harald Welte1887f9d2009-12-29 10:52:38 +010093};
94
95const char *gsm_lchans_name(enum gsm_lchan_state s)
96{
97 return get_value_string(lchan_s_names, s);
98}
99
Harald Welte92b1fe42010-03-25 11:45:30 +0800100static const struct value_string chreq_names[] = {
101 { GSM_CHREQ_REASON_EMERG, "EMERGENCY" },
102 { GSM_CHREQ_REASON_PAG, "PAGING" },
103 { GSM_CHREQ_REASON_CALL, "CALL" },
104 { GSM_CHREQ_REASON_LOCATION_UPD,"LOCATION_UPDATE" },
105 { GSM_CHREQ_REASON_OTHER, "OTHER" },
106 { 0, NULL }
Harald Weltea72c98e2009-01-04 16:10:38 +0000107};
108
109const char *gsm_chreq_name(enum gsm_chreq_reason_t c)
110{
Harald Welte92b1fe42010-03-25 11:45:30 +0800111 return get_value_string(chreq_names, c);
Harald Weltea72c98e2009-01-04 16:10:38 +0000112}
113
Harald Welte39315c42010-01-10 18:01:52 +0100114static struct gsm_bts_model *bts_model_find(enum gsm_bts_type type)
115{
116 struct gsm_bts_model *model;
117
118 llist_for_each_entry(model, &bts_models, list) {
119 if (model->type == type)
120 return model;
121 }
122
123 return NULL;
124}
125
126int gsm_bts_model_register(struct gsm_bts_model *model)
127{
128 if (bts_model_find(model->type))
129 return -EEXIST;
130
131 tlv_def_patch(&model->nm_att_tlvdef, &nm_att_tlvdef);
132 llist_add_tail(&model->list, &bts_models);
133 return 0;
134}
135
136
Harald Weltee441d9c2009-06-21 16:17:15 +0200137struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000138{
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100139 struct gsm_bts_trx *trx = talloc_zero(bts, struct gsm_bts_trx);
Harald Weltee441d9c2009-06-21 16:17:15 +0200140 int k;
Harald Welte52b1f982008-12-23 20:25:15 +0000141
Harald Weltee441d9c2009-06-21 16:17:15 +0200142 if (!trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000143 return NULL;
144
Harald Weltee441d9c2009-06-21 16:17:15 +0200145 trx->bts = bts;
146 trx->nr = bts->num_trx++;
Holger Hans Peter Freytherf31e4742009-12-31 03:05:52 +0100147 trx->nm_state.administrative = NM_STATE_UNLOCKED;
Harald Weltee441d9c2009-06-21 16:17:15 +0200148
149 for (k = 0; k < TRX_NR_TS; k++) {
150 struct gsm_bts_trx_ts *ts = &trx->ts[k];
151 int l;
152
153 ts->trx = trx;
154 ts->nr = k;
155 ts->pchan = GSM_PCHAN_NONE;
156
Harald Weltea39b0f22010-06-14 22:26:10 +0200157 ts->hopping.arfcns.data_len = sizeof(ts->hopping.arfcns_data);
158 ts->hopping.arfcns.data = ts->hopping.arfcns_data;
Harald Weltefd370892010-06-15 08:52:12 +0200159 ts->hopping.ma.data_len = sizeof(ts->hopping.ma_data);
160 ts->hopping.ma.data = ts->hopping.ma_data;
Harald Weltea39b0f22010-06-14 22:26:10 +0200161
Harald Weltee441d9c2009-06-21 16:17:15 +0200162 for (l = 0; l < TS_MAX_LCHAN; l++) {
163 struct gsm_lchan *lchan;
164 lchan = &ts->lchan[l];
165
166 lchan->ts = ts;
167 lchan->nr = l;
168 lchan->type = GSM_LCHAN_NONE;
169 }
170 }
171
Harald Welte (local)7b37d972009-12-27 20:56:38 +0100172 if (trx->nr != 0)
173 trx->nominal_power = bts->c0->nominal_power;
174
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200175 llist_add_tail(&trx->list, &bts->trx_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200176
177 return trx;
178}
179
Harald Welte615e9562010-05-11 23:50:21 +0200180static const uint8_t bts_nse_timer_default[] = { 3, 3, 3, 3, 30, 3, 10 };
181static const uint8_t bts_cell_timer_default[] =
182 { 3, 3, 3, 3, 3, 10, 3, 10, 3, 10, 3 };
183
Harald Weltee441d9c2009-06-21 16:17:15 +0200184struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, enum gsm_bts_type type,
185 u_int8_t tsc, u_int8_t bsic)
186{
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100187 struct gsm_bts *bts = talloc_zero(net, struct gsm_bts);
Harald Welte39315c42010-01-10 18:01:52 +0100188 struct gsm_bts_model *model = bts_model_find(type);
Harald Welte55dd4432009-10-24 10:19:14 +0200189 int i;
Harald Weltee441d9c2009-06-21 16:17:15 +0200190
191 if (!bts)
192 return NULL;
193
laforge6af56d12010-01-12 10:44:26 +0100194 if (!model && type != GSM_BTS_TYPE_UNKNOWN) {
Harald Welte39315c42010-01-10 18:01:52 +0100195 talloc_free(bts);
196 return NULL;
197 }
198
Harald Weltee441d9c2009-06-21 16:17:15 +0200199 bts->network = net;
200 bts->nr = net->num_bts++;
201 bts->type = type;
Harald Welte39315c42010-01-10 18:01:52 +0100202 bts->model = model;
Harald Weltee441d9c2009-06-21 16:17:15 +0200203 bts->tsc = tsc;
204 bts->bsic = bsic;
205 bts->num_trx = 0;
206 INIT_LLIST_HEAD(&bts->trx_list);
Harald Welte (local)0e451d02009-08-13 10:14:26 +0200207 bts->ms_max_power = 15; /* dBm */
Harald Welte73225282009-12-12 18:17:25 +0100208 bts->si_common.cell_sel_par.cell_resel_hyst = 2; /* 4 dB */
209 bts->si_common.cell_sel_par.rxlev_acc_min = 0;
Harald Welte6c40def2009-12-14 22:07:14 +0100210 bts->si_common.neigh_list.data = bts->si_common.data.neigh_list;
211 bts->si_common.neigh_list.data_len =
212 sizeof(bts->si_common.data.neigh_list);
213 bts->si_common.cell_alloc.data = bts->si_common.data.cell_alloc;
214 bts->si_common.cell_alloc.data_len =
215 sizeof(bts->si_common.data.cell_alloc);
Sylvain Munaut4010f1e2009-12-22 13:43:26 +0100216 bts->si_common.rach_control.re = 1; /* no re-establishment */
217 bts->si_common.rach_control.tx_integer = 9; /* 12 slots spread - 217/115 slots delay */
218 bts->si_common.rach_control.max_trans = 3; /* 7 retransmissions */
219 bts->si_common.rach_control.t2 = 4; /* no emergency calls */
Harald Weltee441d9c2009-06-21 16:17:15 +0200220
Harald Welte55dd4432009-10-24 10:19:14 +0200221 for (i = 0; i < ARRAY_SIZE(bts->gprs.nsvc); i++) {
222 bts->gprs.nsvc[i].bts = bts;
223 bts->gprs.nsvc[i].id = i;
224 }
Harald Welte615e9562010-05-11 23:50:21 +0200225 memcpy(&bts->gprs.nse.timer, bts_nse_timer_default,
226 sizeof(bts->gprs.nse.timer));
227 memcpy(&bts->gprs.cell.timer, bts_cell_timer_default,
228 sizeof(bts->gprs.cell.timer));
Harald Welte55dd4432009-10-24 10:19:14 +0200229
Harald Weltee441d9c2009-06-21 16:17:15 +0200230 /* create our primary TRX */
231 bts->c0 = gsm_bts_trx_alloc(bts);
232 if (!bts->c0) {
233 talloc_free(bts);
234 return NULL;
235 }
236 bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
237
Holger Hans Peter Freyther95c22902010-04-25 23:08:39 +0800238 bts->rach_b_thresh = -1;
239 bts->rach_ldavg_slots = -1;
Harald Welte9fb3b8b2009-08-06 19:05:58 +0200240 llist_add_tail(&bts->list, &net->bts_list);
Harald Weltee441d9c2009-06-21 16:17:15 +0200241
242 return bts;
243}
244
245struct gsm_network *gsm_network_init(u_int16_t country_code, u_int16_t network_code,
246 int (*mncc_recv)(struct gsm_network *, int, void *))
247{
248 struct gsm_network *net;
249
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100250 net = talloc_zero(tall_bsc_ctx, struct gsm_network);
Harald Welte52b1f982008-12-23 20:25:15 +0000251 if (!net)
252 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000253
254 net->country_code = country_code;
255 net->network_code = network_code;
Harald Weltee441d9c2009-06-21 16:17:15 +0200256 net->num_bts = 0;
Holger Hans Peter Freytherd0c54022010-01-07 16:08:42 +0100257 net->reject_cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
Harald Welteb84ddfc2009-12-01 17:36:54 +0530258 net->T3101 = GSM_T3101_DEFAULT;
259 net->T3113 = GSM_T3113_DEFAULT;
260 /* FIXME: initialize all other timers! */
Harald Welte52b1f982008-12-23 20:25:15 +0000261
Harald Weltef7c28b02009-12-21 13:30:17 +0100262 /* default set of handover parameters */
263 net->handover.win_rxlev_avg = 10;
264 net->handover.win_rxqual_avg = 1;
265 net->handover.win_rxlev_avg_neigh = 10;
266 net->handover.pwr_interval = 6;
267 net->handover.pwr_hysteresis = 3;
268 net->handover.max_distance = 9999;
269
Harald Welte4bfdfe72009-06-10 23:11:52 +0800270 INIT_LLIST_HEAD(&net->trans_list);
271 INIT_LLIST_HEAD(&net->upqueue);
Harald Weltee441d9c2009-06-21 16:17:15 +0200272 INIT_LLIST_HEAD(&net->bts_list);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800273
Harald Welteffa55a42009-12-22 19:07:32 +0100274 net->stats.chreq.total = counter_alloc("net.chreq.total");
275 net->stats.chreq.no_channel = counter_alloc("net.chreq.no_channel");
276 net->stats.handover.attempted = counter_alloc("net.handover.attempted");
277 net->stats.handover.no_channel = counter_alloc("net.handover.no_channel");
278 net->stats.handover.timeout = counter_alloc("net.handover.timeout");
279 net->stats.handover.completed = counter_alloc("net.handover.completed");
280 net->stats.handover.failed = counter_alloc("net.handover.failed");
281 net->stats.loc_upd_type.attach = counter_alloc("net.loc_upd_type.attach");
282 net->stats.loc_upd_type.normal = counter_alloc("net.loc_upd_type.normal");
283 net->stats.loc_upd_type.periodic = counter_alloc("net.loc_upd_type.periodic");
284 net->stats.loc_upd_type.detach = counter_alloc("net.imsi_detach.count");
285 net->stats.loc_upd_resp.reject = counter_alloc("net.loc_upd_resp.reject");
286 net->stats.loc_upd_resp.accept = counter_alloc("net.loc_upd_resp.accept");
287 net->stats.paging.attempted = counter_alloc("net.paging.attempted");
288 net->stats.paging.detached = counter_alloc("net.paging.detached");
289 net->stats.paging.completed = counter_alloc("net.paging.completed");
290 net->stats.paging.expired = counter_alloc("net.paging.expired");
291 net->stats.sms.submitted = counter_alloc("net.sms.submitted");
292 net->stats.sms.no_receiver = counter_alloc("net.sms.no_receiver");
293 net->stats.sms.delivered = counter_alloc("net.sms.delivered");
294 net->stats.sms.rp_err_mem = counter_alloc("net.sms.rp_err_mem");
295 net->stats.sms.rp_err_other = counter_alloc("net.sms.rp_err_other");
296 net->stats.call.dialled = counter_alloc("net.call.dialled");
297 net->stats.call.alerted = counter_alloc("net.call.alerted");
298 net->stats.call.connected = counter_alloc("net.call.connected");
Holger Hans Peter Freyther3ba36d52010-04-17 06:48:29 +0200299 net->stats.chan.rf_fail = counter_alloc("net.chan.rf_fail");
300 net->stats.chan.rll_err = counter_alloc("net.chan.rll_err");
Holger Hans Peter Freytherbb110f92010-04-12 10:45:52 +0200301 net->stats.bts.oml_fail = counter_alloc("net.bts.oml_fail");
302 net->stats.bts.rsl_fail = counter_alloc("net.bts.rsl_fail");
Harald Welteffa55a42009-12-22 19:07:32 +0100303
Harald Welte4bfdfe72009-06-10 23:11:52 +0800304 net->mncc_recv = mncc_recv;
305
Harald Welte8470bf22008-12-25 23:28:35 +0000306 return net;
Harald Welte52b1f982008-12-23 20:25:15 +0000307}
Harald Welte23a68632009-02-19 17:06:42 +0000308
Harald Weltee441d9c2009-06-21 16:17:15 +0200309struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
310{
311 struct gsm_bts *bts;
312
313 if (num >= net->num_bts)
314 return NULL;
315
316 llist_for_each_entry(bts, &net->bts_list, list) {
317 if (bts->nr == num)
318 return bts;
319 }
320
321 return NULL;
322}
323
Harald Welte84874c92009-12-14 22:33:02 +0100324/* Get reference to a neighbor cell on a given BCCH ARFCN */
Harald Welte0b121032009-12-15 00:21:31 +0100325struct gsm_bts *gsm_bts_neighbor(const struct gsm_bts *bts,
326 u_int16_t arfcn, u_int8_t bsic)
Harald Welte84874c92009-12-14 22:33:02 +0100327{
328 struct gsm_bts *neigh;
329 /* FIXME: use some better heuristics here to determine which cell
330 * using this ARFCN really is closest to the target cell. For
331 * now we simply assume that each ARFCN will only be used by one
332 * cell */
333
334 llist_for_each_entry(neigh, &bts->network->bts_list, list) {
Harald Welte0b121032009-12-15 00:21:31 +0100335 if (neigh->c0->arfcn == arfcn &&
336 neigh->bsic == bsic)
Harald Welte84874c92009-12-14 22:33:02 +0100337 return neigh;
338 }
339
340 return NULL;
341}
342
Harald Weltee441d9c2009-06-21 16:17:15 +0200343struct gsm_bts_trx *gsm_bts_trx_num(struct gsm_bts *bts, int num)
344{
345 struct gsm_bts_trx *trx;
346
347 if (num >= bts->num_trx)
348 return NULL;
349
350 llist_for_each_entry(trx, &bts->trx_list, list) {
351 if (trx->nr == num)
352 return trx;
353 }
354
355 return NULL;
356}
357
Harald Welte23a68632009-02-19 17:06:42 +0000358static char ts2str[255];
359
Harald Welte (local)d48f4eb2009-12-28 23:14:22 +0100360char *gsm_trx_name(struct gsm_bts_trx *trx)
361{
362 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d)",
363 trx->bts->nr, trx->nr);
364
365 return ts2str;
366}
367
368
Harald Welte23a68632009-02-19 17:06:42 +0000369char *gsm_ts_name(struct gsm_bts_trx_ts *ts)
370{
371 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)",
Mike Haben2449b372009-10-26 20:36:34 +0100372 ts->trx->bts->nr, ts->trx->nr, ts->nr);
Harald Welte23a68632009-02-19 17:06:42 +0000373
374 return ts2str;
375}
Harald Welte32201c12009-03-10 12:15:10 +0000376
Harald Welte (local)19ef62a2009-12-27 18:16:36 +0100377char *gsm_lchan_name(struct gsm_lchan *lchan)
378{
379 struct gsm_bts_trx_ts *ts = lchan->ts;
380
381 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d,ss=%d)",
382 ts->trx->bts->nr, ts->trx->nr, ts->nr, lchan->nr);
383
384 return ts2str;
385}
386
Harald Welte92b1fe42010-03-25 11:45:30 +0800387static const struct value_string bts_types[] = {
388 { GSM_BTS_TYPE_UNKNOWN, "unknown" },
389 { GSM_BTS_TYPE_BS11, "bs11" },
390 { GSM_BTS_TYPE_NANOBTS, "nanobts" },
391 { 0, NULL }
Harald Welte32201c12009-03-10 12:15:10 +0000392};
393
Harald Welte1d014a52009-08-08 15:38:29 +0200394enum gsm_bts_type parse_btstype(const char *arg)
Harald Welte32201c12009-03-10 12:15:10 +0000395{
Harald Welte92b1fe42010-03-25 11:45:30 +0800396 return get_string_value(bts_types, arg);
Harald Welte32201c12009-03-10 12:15:10 +0000397}
398
Holger Hans Peter Freyther2dceae62009-06-12 17:39:38 +0200399const char *btstype2str(enum gsm_bts_type type)
Harald Welte32201c12009-03-10 12:15:10 +0000400{
Harald Welte92b1fe42010-03-25 11:45:30 +0800401 return get_value_string(bts_types, type);
Harald Welte32201c12009-03-10 12:15:10 +0000402}
Harald Weltebe991492009-05-23 13:56:40 +0000403
Harald Welte6e670aa2010-01-07 20:44:32 +0100404struct gsm_bts_trx *gsm_bts_trx_by_nr(struct gsm_bts *bts, int nr)
405{
406 struct gsm_bts_trx *trx;
407
408 llist_for_each_entry(trx, &bts->trx_list, list) {
409 if (trx->nr == nr)
410 return trx;
411 }
412 return NULL;
413}
414
Harald Weltebe991492009-05-23 13:56:40 +0000415/* Search for a BTS in the given Location Area; optionally start searching
416 * with start_bts (for continuing to search after the first result) */
417struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
418 struct gsm_bts *start_bts)
419{
420 int i;
421 struct gsm_bts *bts;
422 int skip = 0;
423
424 if (start_bts)
425 skip = 1;
426
427 for (i = 0; i < net->num_bts; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200428 bts = gsm_bts_num(net, i);
Harald Weltebe991492009-05-23 13:56:40 +0000429
430 if (skip) {
431 if (start_bts == bts)
432 skip = 0;
433 continue;
434 }
435
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200436 if (lac == GSM_LAC_RESERVED_ALL_BTS || bts->location_area_code == lac)
Harald Weltebe991492009-05-23 13:56:40 +0000437 return bts;
438 }
439 return NULL;
440}
Harald Weltefcd24452009-06-20 18:15:19 +0200441
Harald Welte92b1fe42010-03-25 11:45:30 +0800442static const struct value_string auth_policy_names[] = {
443 { GSM_AUTH_POLICY_CLOSED, "closed" },
444 { GSM_AUTH_POLICY_ACCEPT_ALL, "accept-all" },
445 { GSM_AUTH_POLICY_TOKEN, "token" },
446 { 0, NULL }
Harald Welte (local)69de3972009-08-12 14:42:23 +0200447};
448
449enum gsm_auth_policy gsm_auth_policy_parse(const char *arg)
450{
Harald Welte92b1fe42010-03-25 11:45:30 +0800451 return get_string_value(auth_policy_names, arg);
Harald Welte (local)69de3972009-08-12 14:42:23 +0200452}
453
454const char *gsm_auth_policy_name(enum gsm_auth_policy policy)
455{
Harald Welte92b1fe42010-03-25 11:45:30 +0800456 return get_value_string(auth_policy_names, policy);
Harald Welte (local)69de3972009-08-12 14:42:23 +0200457}
458
Harald Welte97a282b2010-03-14 15:37:43 +0800459void gprs_ra_id_by_bts(struct gprs_ra_id *raid, struct gsm_bts *bts)
460{
461 raid->mcc = bts->network->country_code;
462 raid->mnc = bts->network->network_code;
463 raid->lac = bts->location_area_code;
464 raid->rac = bts->gprs.rac;
465}
466
467int gsm48_ra_id_by_bts(u_int8_t *buf, struct gsm_bts *bts)
468{
469 struct gprs_ra_id raid;
470
471 gprs_ra_id_by_bts(&raid, bts);
472
473 return gsm48_construct_ra(buf, &raid);
474}
475
Harald Welte92b1fe42010-03-25 11:45:30 +0800476static const struct value_string rrlp_mode_names[] = {
477 { RRLP_MODE_NONE, "none" },
478 { RRLP_MODE_MS_BASED, "ms-based" },
479 { RRLP_MODE_MS_PREF, "ms-preferred" },
480 { RRLP_MODE_ASS_PREF, "ass-preferred" },
481 { 0, NULL }
Harald Welteeab84a12009-12-13 10:53:12 +0100482};
483
484enum rrlp_mode rrlp_mode_parse(const char *arg)
485{
Harald Welte92b1fe42010-03-25 11:45:30 +0800486 return get_string_value(rrlp_mode_names, arg);
Harald Welteeab84a12009-12-13 10:53:12 +0100487}
488
489const char *rrlp_mode_name(enum rrlp_mode mode)
490{
Harald Welte92b1fe42010-03-25 11:45:30 +0800491 return get_value_string(rrlp_mode_names, mode);
Harald Welteeab84a12009-12-13 10:53:12 +0100492}
Harald Welted12b0fd2009-12-15 21:36:05 +0100493
Harald Welte4511d892010-04-18 15:51:20 +0200494static const struct value_string bts_gprs_mode_names[] = {
495 { BTS_GPRS_NONE, "none" },
496 { BTS_GPRS_GPRS, "gprs" },
497 { BTS_GPRS_EGPRS, "egprs" },
498 { 0, NULL }
499};
500
501enum bts_gprs_mode bts_gprs_mode_parse(const char *arg)
502{
503 return get_string_value(bts_gprs_mode_names, arg);
504}
505
506const char *bts_gprs_mode_name(enum bts_gprs_mode mode)
507{
508 return get_value_string(bts_gprs_mode_names, mode);
509}
510
Harald Welted12b0fd2009-12-15 21:36:05 +0100511struct gsm_meas_rep *lchan_next_meas_rep(struct gsm_lchan *lchan)
512{
513 struct gsm_meas_rep *meas_rep;
514
515 meas_rep = &lchan->meas_rep[lchan->meas_rep_idx];
516 memset(meas_rep, 0, sizeof(*meas_rep));
517 meas_rep->lchan = lchan;
518 lchan->meas_rep_idx = (lchan->meas_rep_idx + 1)
519 % ARRAY_SIZE(lchan->meas_rep);
520
521 return meas_rep;
522}
Harald Welte (local)7b37d972009-12-27 20:56:38 +0100523
Harald Weltef3d8e922010-06-14 22:44:42 +0200524int gsm_btsmodel_set_feature(struct gsm_bts_model *bts, enum gsm_bts_features feat)
525{
526 return bitvec_set_bit_pos(&bts->features, feat, 1);
527}
528
529int gsm_bts_has_feature(struct gsm_bts *bts, enum gsm_bts_features feat)
530{
531 return bitvec_get_bit_pos(&bts->model->features, feat);
532}
533
Harald Welte39315c42010-01-10 18:01:52 +0100534int gsm_set_bts_type(struct gsm_bts *bts, enum gsm_bts_type type)
Harald Welte (local)7b37d972009-12-27 20:56:38 +0100535{
Harald Welte39315c42010-01-10 18:01:52 +0100536 struct gsm_bts_model *model;
537
538 model = bts_model_find(type);
539 if (!model)
540 return -EINVAL;
541
Harald Welte (local)7b37d972009-12-27 20:56:38 +0100542 bts->type = type;
Harald Welte39315c42010-01-10 18:01:52 +0100543 bts->model = model;
Harald Welte (local)7b37d972009-12-27 20:56:38 +0100544
545 switch (bts->type) {
546 case GSM_BTS_TYPE_NANOBTS:
547 /* Set the default OML Stream ID to 0xff */
548 bts->oml_tei = 0xff;
549 bts->c0->nominal_power = 23;
550 break;
551 case GSM_BTS_TYPE_BS11:
552 break;
Holger Hans Peter Freyther6fc87912010-05-14 02:53:22 +0800553 case GSM_BTS_TYPE_UNKNOWN:
554 break;
Harald Welte (local)7b37d972009-12-27 20:56:38 +0100555 }
Harald Welte39315c42010-01-10 18:01:52 +0100556
557 return 0;
Harald Welte (local)7b37d972009-12-27 20:56:38 +0100558}