blob: 9ab5895ee19971d722210fcbbf6994270e870169 [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>
25
Harald Welte8470bf22008-12-25 23:28:35 +000026#include <openbsc/gsm_data.h>
Harald Welte52b1f982008-12-23 20:25:15 +000027
Harald Weltecd06bfb2009-02-10 17:33:56 +000028void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr,
29 u_int8_t e1_ts, u_int8_t e1_ts_ss)
30{
31 ts->e1_link.e1_nr = e1_nr;
32 ts->e1_link.e1_ts = e1_ts;
33 ts->e1_link.e1_ts_ss = e1_ts_ss;
34}
35
Harald Weltea72c98e2009-01-04 16:10:38 +000036static const char *pchan_names[] = {
37 [GSM_PCHAN_NONE] = "NONE",
38 [GSM_PCHAN_CCCH] = "CCCH",
39 [GSM_PCHAN_CCCH_SDCCH4] = "CCCH+SDCCH4",
40 [GSM_PCHAN_TCH_F] = "TCH/F",
41 [GSM_PCHAN_TCH_H] = "TCH/H",
42 [GSM_PCHAN_SDCCH8_SACCH8C] = "SDCCH8",
43 [GSM_PCHAN_UNKNOWN] = "UNKNOWN",
44};
45
46const char *gsm_pchan_name(enum gsm_phys_chan_config c)
47{
48 if (c >= ARRAY_SIZE(pchan_names))
49 return "INVALID";
50
51 return pchan_names[c];
52}
53
54static const char *lchan_names[] = {
55 [GSM_LCHAN_NONE] = "NONE",
56 [GSM_LCHAN_SDCCH] = "SDCCH",
57 [GSM_LCHAN_TCH_F] = "TCH/F",
58 [GSM_LCHAN_TCH_H] = "TCH/H",
59 [GSM_LCHAN_UNKNOWN] = "UNKNOWN",
60};
61
62const char *gsm_lchan_name(enum gsm_chan_t c)
63{
64 if (c >= ARRAY_SIZE(lchan_names))
65 return "INVALID";
66
67 return lchan_names[c];
68}
69
70static const char *chreq_names[] = {
71 [GSM_CHREQ_REASON_EMERG] = "EMERGENCY",
72 [GSM_CHREQ_REASON_PAG] = "PAGING",
73 [GSM_CHREQ_REASON_CALL] = "CALL",
74 [GSM_CHREQ_REASON_LOCATION_UPD] = "LOCATION_UPDATE",
75 [GSM_CHREQ_REASON_OTHER] = "OTHER",
76};
77
78const char *gsm_chreq_name(enum gsm_chreq_reason_t c)
79{
80 if (c >= ARRAY_SIZE(chreq_names))
81 return "INVALID";
82
83 return chreq_names[c];
84}
85
Harald Welte8c1d0e42009-02-15 03:38:12 +000086struct gsm_network *gsm_network_init(unsigned int num_bts, enum gsm_bts_type bts_type,
Harald Welte4bfdfe72009-06-10 23:11:52 +080087 u_int16_t country_code, u_int16_t network_code,
88 int (*mncc_recv)(struct gsm_network *, int, void *))
Harald Welte52b1f982008-12-23 20:25:15 +000089{
90 int i;
91 struct gsm_network *net;
92
93 if (num_bts > GSM_MAX_BTS)
94 return NULL;
95
96 net = malloc(sizeof(*net));
97 if (!net)
98 return NULL;
99 memset(net, 0, sizeof(*net));
100
101 net->country_code = country_code;
102 net->network_code = network_code;
103 net->num_bts = num_bts;
104
Harald Welte4bfdfe72009-06-10 23:11:52 +0800105 INIT_LLIST_HEAD(&net->trans_list);
106 INIT_LLIST_HEAD(&net->upqueue);
107
108 net->mncc_recv = mncc_recv;
109
Harald Welte52b1f982008-12-23 20:25:15 +0000110 for (i = 0; i < num_bts; i++) {
111 struct gsm_bts *bts = &net->bts[i];
112 int j;
113
114 bts->network = net;
115 bts->nr = i;
Harald Welte8c1d0e42009-02-15 03:38:12 +0000116 bts->type = bts_type;
Harald Welte02b0e092009-02-28 13:11:07 +0000117 bts->tsc = HARDCODED_TSC;
Harald Welte78f2f502009-05-23 16:56:52 +0000118 bts->bsic = HARDCODED_BSIC;
Harald Welte52b1f982008-12-23 20:25:15 +0000119
120 for (j = 0; j < BTS_MAX_TRX; j++) {
121 struct gsm_bts_trx *trx = &bts->trx[j];
122 int k;
123
124 trx->bts = bts;
125 trx->nr = j;
126
Harald Welte4bfdfe72009-06-10 23:11:52 +0800127 for (k = 0; k < TRX_NR_TS; k++) {
Harald Welte52b1f982008-12-23 20:25:15 +0000128 struct gsm_bts_trx_ts *ts = &trx->ts[k];
Harald Welte8470bf22008-12-25 23:28:35 +0000129 int l;
Harald Welte52b1f982008-12-23 20:25:15 +0000130
131 ts->trx = trx;
132 ts->nr = k;
Harald Welte8470bf22008-12-25 23:28:35 +0000133 ts->pchan = GSM_PCHAN_NONE;
134
135 for (l = 0; l < TS_MAX_LCHAN; l++) {
136 struct gsm_lchan *lchan;
137 lchan = &ts->lchan[l];
138
139 lchan->ts = ts;
140 lchan->nr = l;
141 lchan->type = GSM_LCHAN_NONE;
142 }
Harald Welte52b1f982008-12-23 20:25:15 +0000143 }
144 }
145
146 bts->num_trx = 1; /* FIXME */
Harald Weltecd06bfb2009-02-10 17:33:56 +0000147#ifdef HAVE_TRX1
148 bts->num_trx++;
149#endif
Harald Welte702d8702008-12-26 20:25:35 +0000150 bts->c0 = &bts->trx[0];
151 bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
Harald Welte52b1f982008-12-23 20:25:15 +0000152 }
Harald Welte8470bf22008-12-25 23:28:35 +0000153 return net;
Harald Welte52b1f982008-12-23 20:25:15 +0000154}
Harald Welte23a68632009-02-19 17:06:42 +0000155
156static char ts2str[255];
157
158char *gsm_ts_name(struct gsm_bts_trx_ts *ts)
159{
160 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)",
Harald Welte90f64762009-05-01 17:22:09 +0000161 ts->trx->bts->bts_nr, ts->trx->nr, ts->nr);
Harald Welte23a68632009-02-19 17:06:42 +0000162
163 return ts2str;
164}
Harald Welte32201c12009-03-10 12:15:10 +0000165
166static const char *bts_types[] = {
167 [GSM_BTS_TYPE_UNKNOWN] = "unknown",
168 [GSM_BTS_TYPE_BS11] = "bs11",
169 [GSM_BTS_TYPE_NANOBTS_900] = "nanobts900",
170 [GSM_BTS_TYPE_NANOBTS_1800] = "nanobts1800",
171};
172
173enum gsm_bts_type parse_btstype(char *arg)
174{
175 int i;
176 for (i = 0; i < ARRAY_SIZE(bts_types); i++) {
177 if (!strcmp(arg, bts_types[i]))
178 return i;
179 }
180 return GSM_BTS_TYPE_BS11; /* Default: BS11 */
181}
182
183char *btstype2str(enum gsm_bts_type type)
184{
185 if (type > ARRAY_SIZE(bts_types))
186 return "undefined";
187 return bts_types[type];
188}
Harald Weltebe991492009-05-23 13:56:40 +0000189
190/* Search for a BTS in the given Location Area; optionally start searching
191 * with start_bts (for continuing to search after the first result) */
192struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
193 struct gsm_bts *start_bts)
194{
195 int i;
196 struct gsm_bts *bts;
197 int skip = 0;
198
199 if (start_bts)
200 skip = 1;
201
202 for (i = 0; i < net->num_bts; i++) {
203 bts = &net->bts[i];
204
205 if (skip) {
206 if (start_bts == bts)
207 skip = 0;
208 continue;
209 }
210
211 if (bts->location_area_code == lac)
212 return bts;
213 }
214 return NULL;
215}