blob: 80020e50936bf787209f679c33f4914bdf716050 [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 Welte52b1f982008-12-23 20:25:15 +000028
Harald Weltecd06bfb2009-02-10 17:33:56 +000029void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr,
30 u_int8_t e1_ts, u_int8_t e1_ts_ss)
31{
32 ts->e1_link.e1_nr = e1_nr;
33 ts->e1_link.e1_ts = e1_ts;
34 ts->e1_link.e1_ts_ss = e1_ts_ss;
35}
36
Harald Weltea72c98e2009-01-04 16:10:38 +000037static const char *pchan_names[] = {
38 [GSM_PCHAN_NONE] = "NONE",
39 [GSM_PCHAN_CCCH] = "CCCH",
40 [GSM_PCHAN_CCCH_SDCCH4] = "CCCH+SDCCH4",
41 [GSM_PCHAN_TCH_F] = "TCH/F",
42 [GSM_PCHAN_TCH_H] = "TCH/H",
43 [GSM_PCHAN_SDCCH8_SACCH8C] = "SDCCH8",
44 [GSM_PCHAN_UNKNOWN] = "UNKNOWN",
45};
46
47const char *gsm_pchan_name(enum gsm_phys_chan_config c)
48{
49 if (c >= ARRAY_SIZE(pchan_names))
50 return "INVALID";
51
52 return pchan_names[c];
53}
54
55static const char *lchan_names[] = {
56 [GSM_LCHAN_NONE] = "NONE",
57 [GSM_LCHAN_SDCCH] = "SDCCH",
58 [GSM_LCHAN_TCH_F] = "TCH/F",
59 [GSM_LCHAN_TCH_H] = "TCH/H",
60 [GSM_LCHAN_UNKNOWN] = "UNKNOWN",
61};
62
63const char *gsm_lchan_name(enum gsm_chan_t c)
64{
65 if (c >= ARRAY_SIZE(lchan_names))
66 return "INVALID";
67
68 return lchan_names[c];
69}
70
71static const char *chreq_names[] = {
72 [GSM_CHREQ_REASON_EMERG] = "EMERGENCY",
73 [GSM_CHREQ_REASON_PAG] = "PAGING",
74 [GSM_CHREQ_REASON_CALL] = "CALL",
75 [GSM_CHREQ_REASON_LOCATION_UPD] = "LOCATION_UPDATE",
76 [GSM_CHREQ_REASON_OTHER] = "OTHER",
77};
78
79const char *gsm_chreq_name(enum gsm_chreq_reason_t c)
80{
81 if (c >= ARRAY_SIZE(chreq_names))
82 return "INVALID";
83
84 return chreq_names[c];
85}
86
Harald Welte8c1d0e42009-02-15 03:38:12 +000087struct gsm_network *gsm_network_init(unsigned int num_bts, enum gsm_bts_type bts_type,
Harald Welte4bfdfe72009-06-10 23:11:52 +080088 u_int16_t country_code, u_int16_t network_code,
89 int (*mncc_recv)(struct gsm_network *, int, void *))
Harald Welte52b1f982008-12-23 20:25:15 +000090{
91 int i;
92 struct gsm_network *net;
93
94 if (num_bts > GSM_MAX_BTS)
95 return NULL;
96
97 net = malloc(sizeof(*net));
98 if (!net)
99 return NULL;
100 memset(net, 0, sizeof(*net));
101
102 net->country_code = country_code;
103 net->network_code = network_code;
104 net->num_bts = num_bts;
105
Harald Welte4bfdfe72009-06-10 23:11:52 +0800106 INIT_LLIST_HEAD(&net->trans_list);
107 INIT_LLIST_HEAD(&net->upqueue);
108
109 net->mncc_recv = mncc_recv;
110
Harald Welte52b1f982008-12-23 20:25:15 +0000111 for (i = 0; i < num_bts; i++) {
112 struct gsm_bts *bts = &net->bts[i];
113 int j;
114
115 bts->network = net;
116 bts->nr = i;
Harald Welte8c1d0e42009-02-15 03:38:12 +0000117 bts->type = bts_type;
Harald Welte02b0e092009-02-28 13:11:07 +0000118 bts->tsc = HARDCODED_TSC;
Harald Welte78f2f502009-05-23 16:56:52 +0000119 bts->bsic = HARDCODED_BSIC;
Harald Welte52b1f982008-12-23 20:25:15 +0000120
121 for (j = 0; j < BTS_MAX_TRX; j++) {
122 struct gsm_bts_trx *trx = &bts->trx[j];
123 int k;
124
125 trx->bts = bts;
126 trx->nr = j;
127
Harald Welte4bfdfe72009-06-10 23:11:52 +0800128 for (k = 0; k < TRX_NR_TS; k++) {
Harald Welte52b1f982008-12-23 20:25:15 +0000129 struct gsm_bts_trx_ts *ts = &trx->ts[k];
Harald Welte8470bf22008-12-25 23:28:35 +0000130 int l;
Harald Welte52b1f982008-12-23 20:25:15 +0000131
132 ts->trx = trx;
133 ts->nr = k;
Harald Welte8470bf22008-12-25 23:28:35 +0000134 ts->pchan = GSM_PCHAN_NONE;
135
136 for (l = 0; l < TS_MAX_LCHAN; l++) {
137 struct gsm_lchan *lchan;
138 lchan = &ts->lchan[l];
139
140 lchan->ts = ts;
141 lchan->nr = l;
142 lchan->type = GSM_LCHAN_NONE;
143 }
Harald Welte52b1f982008-12-23 20:25:15 +0000144 }
145 }
146
147 bts->num_trx = 1; /* FIXME */
Harald Weltecd06bfb2009-02-10 17:33:56 +0000148#ifdef HAVE_TRX1
149 bts->num_trx++;
150#endif
Harald Welte702d8702008-12-26 20:25:35 +0000151 bts->c0 = &bts->trx[0];
152 bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4;
Harald Welte52b1f982008-12-23 20:25:15 +0000153 }
Harald Welte8470bf22008-12-25 23:28:35 +0000154 return net;
Harald Welte52b1f982008-12-23 20:25:15 +0000155}
Harald Welte23a68632009-02-19 17:06:42 +0000156
157static char ts2str[255];
158
159char *gsm_ts_name(struct gsm_bts_trx_ts *ts)
160{
161 snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)",
Harald Welte90f64762009-05-01 17:22:09 +0000162 ts->trx->bts->bts_nr, ts->trx->nr, ts->nr);
Harald Welte23a68632009-02-19 17:06:42 +0000163
164 return ts2str;
165}
Harald Welte32201c12009-03-10 12:15:10 +0000166
167static const char *bts_types[] = {
168 [GSM_BTS_TYPE_UNKNOWN] = "unknown",
169 [GSM_BTS_TYPE_BS11] = "bs11",
170 [GSM_BTS_TYPE_NANOBTS_900] = "nanobts900",
171 [GSM_BTS_TYPE_NANOBTS_1800] = "nanobts1800",
172};
173
174enum gsm_bts_type parse_btstype(char *arg)
175{
176 int i;
177 for (i = 0; i < ARRAY_SIZE(bts_types); i++) {
178 if (!strcmp(arg, bts_types[i]))
179 return i;
180 }
181 return GSM_BTS_TYPE_BS11; /* Default: BS11 */
182}
183
Holger Hans Peter Freyther2dceae62009-06-12 17:39:38 +0200184const char *btstype2str(enum gsm_bts_type type)
Harald Welte32201c12009-03-10 12:15:10 +0000185{
186 if (type > ARRAY_SIZE(bts_types))
187 return "undefined";
188 return bts_types[type];
189}
Harald Weltebe991492009-05-23 13:56:40 +0000190
191/* Search for a BTS in the given Location Area; optionally start searching
192 * with start_bts (for continuing to search after the first result) */
193struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
194 struct gsm_bts *start_bts)
195{
196 int i;
197 struct gsm_bts *bts;
198 int skip = 0;
199
200 if (start_bts)
201 skip = 1;
202
203 for (i = 0; i < net->num_bts; i++) {
204 bts = &net->bts[i];
205
206 if (skip) {
207 if (start_bts == bts)
208 skip = 0;
209 continue;
210 }
211
212 if (bts->location_area_code == lac)
213 return bts;
214 }
215 return NULL;
216}
Harald Weltefcd24452009-06-20 18:15:19 +0200217
218char *gsm_band_name(enum gsm_band band)
219{
220 switch (band) {
221 case GSM_BAND_400:
222 return "GSM 400";
223 case GSM_BAND_850:
224 return "GSM 850";
225 case GSM_BAND_900:
226 return "GSM 900";
227 case GSM_BAND_1800:
228 return "DCS 1800";
229 case GSM_BAND_1900:
230 return "PCS 1900";
231 }
232 return "invalid";
233}
234
235enum gsm_band gsm_band_parse(int mhz)
236{
237 switch (mhz) {
238 case 400:
239 return GSM_BAND_400;
240 case 850:
241 return GSM_BAND_850;
242 case 900:
243 return GSM_BAND_900;
244 case 1800:
245 return GSM_BAND_1800;
246 case 1900:
247 return GSM_BAND_1900;
248 default:
249 return -EINVAL;
250 }
251}
252