Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 2 | * |
| 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> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
Harald Welte | 91afe4c | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 25 | #include <errno.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 26 | |
| 27 | #include <openbsc/gsm_data.h> |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 28 | #include <openbsc/talloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 29 | |
Andreas Eversberg | e4d5146 | 2009-06-29 15:19:38 +0200 | [diff] [blame] | 30 | void *tall_bsc_ctx; |
| 31 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [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 | |
| 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 | |
| 58 | static const char *lchan_names[] = { |
| 59 | [GSM_LCHAN_NONE] = "NONE", |
| 60 | [GSM_LCHAN_SDCCH] = "SDCCH", |
| 61 | [GSM_LCHAN_TCH_F] = "TCH/F", |
| 62 | [GSM_LCHAN_TCH_H] = "TCH/H", |
| 63 | [GSM_LCHAN_UNKNOWN] = "UNKNOWN", |
| 64 | }; |
| 65 | |
| 66 | const char *gsm_lchan_name(enum gsm_chan_t c) |
| 67 | { |
| 68 | if (c >= ARRAY_SIZE(lchan_names)) |
| 69 | return "INVALID"; |
| 70 | |
| 71 | return lchan_names[c]; |
| 72 | } |
| 73 | |
| 74 | static const char *chreq_names[] = { |
| 75 | [GSM_CHREQ_REASON_EMERG] = "EMERGENCY", |
| 76 | [GSM_CHREQ_REASON_PAG] = "PAGING", |
| 77 | [GSM_CHREQ_REASON_CALL] = "CALL", |
| 78 | [GSM_CHREQ_REASON_LOCATION_UPD] = "LOCATION_UPDATE", |
| 79 | [GSM_CHREQ_REASON_OTHER] = "OTHER", |
| 80 | }; |
| 81 | |
| 82 | const char *gsm_chreq_name(enum gsm_chreq_reason_t c) |
| 83 | { |
| 84 | if (c >= ARRAY_SIZE(chreq_names)) |
| 85 | return "INVALID"; |
| 86 | |
| 87 | return chreq_names[c]; |
| 88 | } |
| 89 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 90 | struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 91 | { |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 92 | struct gsm_bts_trx *trx = talloc(bts, struct gsm_bts_trx); |
| 93 | int k; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 94 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 95 | if (!trx) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 96 | return NULL; |
| 97 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 98 | memset(trx, 0, sizeof(*trx)); |
| 99 | trx->bts = bts; |
| 100 | trx->nr = bts->num_trx++; |
| 101 | |
| 102 | for (k = 0; k < TRX_NR_TS; k++) { |
| 103 | struct gsm_bts_trx_ts *ts = &trx->ts[k]; |
| 104 | int l; |
| 105 | |
| 106 | ts->trx = trx; |
| 107 | ts->nr = k; |
| 108 | ts->pchan = GSM_PCHAN_NONE; |
| 109 | |
| 110 | for (l = 0; l < TS_MAX_LCHAN; l++) { |
| 111 | struct gsm_lchan *lchan; |
| 112 | lchan = &ts->lchan[l]; |
| 113 | |
| 114 | lchan->ts = ts; |
| 115 | lchan->nr = l; |
| 116 | lchan->type = GSM_LCHAN_NONE; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | llist_add(&trx->list, &bts->trx_list); |
| 121 | |
| 122 | return trx; |
| 123 | } |
| 124 | |
| 125 | struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, enum gsm_bts_type type, |
| 126 | u_int8_t tsc, u_int8_t bsic) |
| 127 | { |
| 128 | struct gsm_bts *bts = talloc(net, struct gsm_bts); |
| 129 | |
| 130 | if (!bts) |
| 131 | return NULL; |
| 132 | |
| 133 | memset(bts, 0, sizeof(*bts)); |
| 134 | bts->network = net; |
| 135 | bts->nr = net->num_bts++; |
| 136 | bts->type = type; |
| 137 | bts->tsc = tsc; |
| 138 | bts->bsic = bsic; |
| 139 | bts->num_trx = 0; |
| 140 | INIT_LLIST_HEAD(&bts->trx_list); |
| 141 | |
| 142 | /* create our primary TRX */ |
| 143 | bts->c0 = gsm_bts_trx_alloc(bts); |
| 144 | if (!bts->c0) { |
| 145 | talloc_free(bts); |
| 146 | return NULL; |
| 147 | } |
| 148 | bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4; |
| 149 | |
| 150 | llist_add(&bts->list, &net->bts_list); |
| 151 | |
| 152 | return bts; |
| 153 | } |
| 154 | |
| 155 | struct gsm_network *gsm_network_init(u_int16_t country_code, u_int16_t network_code, |
| 156 | int (*mncc_recv)(struct gsm_network *, int, void *)) |
| 157 | { |
| 158 | struct gsm_network *net; |
| 159 | |
| 160 | net = talloc(tall_bsc_ctx, struct gsm_network); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 161 | if (!net) |
| 162 | return NULL; |
| 163 | memset(net, 0, sizeof(*net)); |
| 164 | |
| 165 | net->country_code = country_code; |
| 166 | net->network_code = network_code; |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 167 | net->num_bts = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 168 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 169 | INIT_LLIST_HEAD(&net->trans_list); |
| 170 | INIT_LLIST_HEAD(&net->upqueue); |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 171 | INIT_LLIST_HEAD(&net->bts_list); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 172 | |
| 173 | net->mncc_recv = mncc_recv; |
| 174 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 175 | return net; |
| 176 | } |
| 177 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 178 | struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num) |
| 179 | { |
| 180 | struct gsm_bts *bts; |
| 181 | |
| 182 | if (num >= net->num_bts) |
| 183 | return NULL; |
| 184 | |
| 185 | llist_for_each_entry(bts, &net->bts_list, list) { |
| 186 | if (bts->nr == num) |
| 187 | return bts; |
| 188 | } |
| 189 | |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | struct gsm_bts_trx *gsm_bts_trx_num(struct gsm_bts *bts, int num) |
| 194 | { |
| 195 | struct gsm_bts_trx *trx; |
| 196 | |
| 197 | if (num >= bts->num_trx) |
| 198 | return NULL; |
| 199 | |
| 200 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 201 | if (trx->nr == num) |
| 202 | return trx; |
| 203 | } |
| 204 | |
| 205 | return NULL; |
| 206 | } |
| 207 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 208 | static char ts2str[255]; |
| 209 | |
| 210 | char *gsm_ts_name(struct gsm_bts_trx_ts *ts) |
| 211 | { |
| 212 | snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)", |
| 213 | ts->trx->bts->bts_nr, ts->trx->nr, ts->nr); |
| 214 | |
| 215 | return ts2str; |
| 216 | } |
| 217 | |
| 218 | static const char *bts_types[] = { |
| 219 | [GSM_BTS_TYPE_UNKNOWN] = "unknown", |
| 220 | [GSM_BTS_TYPE_BS11] = "bs11", |
| 221 | [GSM_BTS_TYPE_NANOBTS_900] = "nanobts900", |
| 222 | [GSM_BTS_TYPE_NANOBTS_1800] = "nanobts1800", |
| 223 | }; |
| 224 | |
| 225 | enum gsm_bts_type parse_btstype(char *arg) |
| 226 | { |
| 227 | int i; |
| 228 | for (i = 0; i < ARRAY_SIZE(bts_types); i++) { |
| 229 | if (!strcmp(arg, bts_types[i])) |
| 230 | return i; |
| 231 | } |
| 232 | return GSM_BTS_TYPE_BS11; /* Default: BS11 */ |
| 233 | } |
| 234 | |
Holger Hans Peter Freyther | 56f59bb | 2009-06-12 17:39:38 +0200 | [diff] [blame] | 235 | const char *btstype2str(enum gsm_bts_type type) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 236 | { |
| 237 | if (type > ARRAY_SIZE(bts_types)) |
| 238 | return "undefined"; |
| 239 | return bts_types[type]; |
| 240 | } |
| 241 | |
| 242 | /* Search for a BTS in the given Location Area; optionally start searching |
| 243 | * with start_bts (for continuing to search after the first result) */ |
| 244 | struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac, |
| 245 | struct gsm_bts *start_bts) |
| 246 | { |
| 247 | int i; |
| 248 | struct gsm_bts *bts; |
| 249 | int skip = 0; |
| 250 | |
| 251 | if (start_bts) |
| 252 | skip = 1; |
| 253 | |
| 254 | for (i = 0; i < net->num_bts; i++) { |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 255 | bts = gsm_bts_num(net, i); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 256 | |
| 257 | if (skip) { |
| 258 | if (start_bts == bts) |
| 259 | skip = 0; |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | if (bts->location_area_code == lac) |
| 264 | return bts; |
| 265 | } |
| 266 | return NULL; |
| 267 | } |
Harald Welte | 91afe4c | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 268 | |
| 269 | char *gsm_band_name(enum gsm_band band) |
| 270 | { |
| 271 | switch (band) { |
| 272 | case GSM_BAND_400: |
| 273 | return "GSM 400"; |
| 274 | case GSM_BAND_850: |
| 275 | return "GSM 850"; |
| 276 | case GSM_BAND_900: |
| 277 | return "GSM 900"; |
| 278 | case GSM_BAND_1800: |
| 279 | return "DCS 1800"; |
| 280 | case GSM_BAND_1900: |
| 281 | return "PCS 1900"; |
| 282 | } |
| 283 | return "invalid"; |
| 284 | } |
| 285 | |
| 286 | enum gsm_band gsm_band_parse(int mhz) |
| 287 | { |
| 288 | switch (mhz) { |
| 289 | case 400: |
| 290 | return GSM_BAND_400; |
| 291 | case 850: |
| 292 | return GSM_BAND_850; |
| 293 | case 900: |
| 294 | return GSM_BAND_900; |
| 295 | case 1800: |
| 296 | return GSM_BAND_1800; |
| 297 | case 1900: |
| 298 | return GSM_BAND_1900; |
| 299 | default: |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | } |
| 303 | |