Harald Welte | 32201c1 | 2009-03-10 12:15:10 +0000 | [diff] [blame] | 1 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 2 | * |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 3 | * 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 Welte | 51f3845 | 2009-02-24 22:36:40 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 24 | #include <string.h> |
Harald Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 25 | #include <errno.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 26 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 28 | #include <openbsc/talloc.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 29 | |
Andreas Eversberg | 8226fa7 | 2009-06-29 15:19:38 +0200 | [diff] [blame] | 30 | void *tall_bsc_ctx; |
| 31 | |
Harald Welte | cd06bfb | 2009-02-10 17:33:56 +0000 | [diff] [blame] | 32 | void 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 Welte | a72c98e | 2009-01-04 16:10:38 +0000 | [diff] [blame] | 40 | static 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 | |
| 50 | const 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 Welte | a171a1b | 2009-08-07 00:24:39 +0200 | [diff] [blame] | 58 | enum 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 Welte | a72c98e | 2009-01-04 16:10:38 +0000 | [diff] [blame] | 70 | static 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 | |
| 78 | const 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 | |
| 86 | static 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 | |
| 94 | const 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 Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 102 | struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 103 | { |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 104 | struct gsm_bts_trx *trx = talloc(bts, struct gsm_bts_trx); |
| 105 | int k; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 106 | |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 107 | if (!trx) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 108 | return NULL; |
| 109 | |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 110 | 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 Welte | 9fb3b8b | 2009-08-06 19:05:58 +0200 | [diff] [blame] | 132 | llist_add_tail(&trx->list, &bts->trx_list); |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 133 | |
| 134 | return trx; |
| 135 | } |
| 136 | |
| 137 | struct 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 Welte | 9fb3b8b | 2009-08-06 19:05:58 +0200 | [diff] [blame] | 162 | llist_add_tail(&bts->list, &net->bts_list); |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 163 | |
| 164 | return bts; |
| 165 | } |
| 166 | |
| 167 | struct 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 Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 173 | 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 Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 179 | net->num_bts = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 180 | |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 181 | INIT_LLIST_HEAD(&net->trans_list); |
| 182 | INIT_LLIST_HEAD(&net->upqueue); |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 183 | INIT_LLIST_HEAD(&net->bts_list); |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 184 | |
| 185 | net->mncc_recv = mncc_recv; |
| 186 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 187 | return net; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 188 | } |
Harald Welte | 23a6863 | 2009-02-19 17:06:42 +0000 | [diff] [blame] | 189 | |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 190 | struct 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 | |
| 205 | struct 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 Welte | 23a6863 | 2009-02-19 17:06:42 +0000 | [diff] [blame] | 220 | static char ts2str[255]; |
| 221 | |
| 222 | char *gsm_ts_name(struct gsm_bts_trx_ts *ts) |
| 223 | { |
| 224 | snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)", |
Harald Welte | 90f6476 | 2009-05-01 17:22:09 +0000 | [diff] [blame] | 225 | ts->trx->bts->bts_nr, ts->trx->nr, ts->nr); |
Harald Welte | 23a6863 | 2009-02-19 17:06:42 +0000 | [diff] [blame] | 226 | |
| 227 | return ts2str; |
| 228 | } |
Harald Welte | 32201c1 | 2009-03-10 12:15:10 +0000 | [diff] [blame] | 229 | |
| 230 | static 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 | |
Harald Welte | 1d014a5 | 2009-08-08 15:38:29 +0200 | [diff] [blame] | 237 | enum gsm_bts_type parse_btstype(const char *arg) |
Harald Welte | 32201c1 | 2009-03-10 12:15:10 +0000 | [diff] [blame] | 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 Freyther | 2dceae6 | 2009-06-12 17:39:38 +0200 | [diff] [blame] | 247 | const char *btstype2str(enum gsm_bts_type type) |
Harald Welte | 32201c1 | 2009-03-10 12:15:10 +0000 | [diff] [blame] | 248 | { |
| 249 | if (type > ARRAY_SIZE(bts_types)) |
| 250 | return "undefined"; |
| 251 | return bts_types[type]; |
| 252 | } |
Harald Welte | be99149 | 2009-05-23 13:56:40 +0000 | [diff] [blame] | 253 | |
| 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) */ |
| 256 | struct 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 Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 267 | bts = gsm_bts_num(net, i); |
Harald Welte | be99149 | 2009-05-23 13:56:40 +0000 | [diff] [blame] | 268 | |
| 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 Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 280 | |
| 281 | char *gsm_band_name(enum gsm_band band) |
| 282 | { |
| 283 | switch (band) { |
| 284 | case GSM_BAND_400: |
Harald Welte | 9fb3b8b | 2009-08-06 19:05:58 +0200 | [diff] [blame] | 285 | return "GSM400"; |
Harald Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 286 | case GSM_BAND_850: |
Harald Welte | 9fb3b8b | 2009-08-06 19:05:58 +0200 | [diff] [blame] | 287 | return "GSM850"; |
Harald Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 288 | case GSM_BAND_900: |
Harald Welte | 9fb3b8b | 2009-08-06 19:05:58 +0200 | [diff] [blame] | 289 | return "GSM900"; |
Harald Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 290 | case GSM_BAND_1800: |
Harald Welte | 9fb3b8b | 2009-08-06 19:05:58 +0200 | [diff] [blame] | 291 | return "DCS1800"; |
Harald Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 292 | case GSM_BAND_1900: |
Harald Welte | 9fb3b8b | 2009-08-06 19:05:58 +0200 | [diff] [blame] | 293 | return "PCS1900"; |
Harald Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 294 | } |
| 295 | return "invalid"; |
| 296 | } |
| 297 | |
| 298 | enum 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 | |