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