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 | 1d50e08 | 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> |
| 28 | |
| 29 | void 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 | |
| 37 | static 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 | |
| 47 | const 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 | |
| 55 | static 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 | |
| 63 | const 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 | |
| 71 | static 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 | |
| 79 | const 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 | |
| 87 | struct gsm_network *gsm_network_init(unsigned int num_bts, enum gsm_bts_type bts_type, |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 88 | u_int16_t country_code, u_int16_t network_code, |
| 89 | int (*mncc_recv)(struct gsm_network *, int, void *)) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 90 | { |
| 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 Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 106 | INIT_LLIST_HEAD(&net->trans_list); |
| 107 | INIT_LLIST_HEAD(&net->upqueue); |
| 108 | |
| 109 | net->mncc_recv = mncc_recv; |
| 110 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 111 | 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; |
| 117 | bts->type = bts_type; |
| 118 | bts->tsc = HARDCODED_TSC; |
| 119 | bts->bsic = HARDCODED_BSIC; |
| 120 | |
| 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 Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 128 | for (k = 0; k < TRX_NR_TS; k++) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 129 | struct gsm_bts_trx_ts *ts = &trx->ts[k]; |
| 130 | int l; |
| 131 | |
| 132 | ts->trx = trx; |
| 133 | ts->nr = k; |
| 134 | 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 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | bts->num_trx = 1; /* FIXME */ |
| 148 | #ifdef HAVE_TRX1 |
| 149 | bts->num_trx++; |
| 150 | #endif |
| 151 | bts->c0 = &bts->trx[0]; |
| 152 | bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4; |
| 153 | } |
| 154 | return net; |
| 155 | } |
| 156 | |
| 157 | static char ts2str[255]; |
| 158 | |
| 159 | char *gsm_ts_name(struct gsm_bts_trx_ts *ts) |
| 160 | { |
| 161 | snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)", |
| 162 | ts->trx->bts->bts_nr, ts->trx->nr, ts->nr); |
| 163 | |
| 164 | return ts2str; |
| 165 | } |
| 166 | |
| 167 | static 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 | |
| 174 | enum 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 Freyther | 56f59bb | 2009-06-12 17:39:38 +0200 | [diff] [blame] | 184 | const char *btstype2str(enum gsm_bts_type type) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 185 | { |
| 186 | if (type > ARRAY_SIZE(bts_types)) |
| 187 | return "undefined"; |
| 188 | return bts_types[type]; |
| 189 | } |
| 190 | |
| 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) */ |
| 193 | struct 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 Welte | 1d50e08 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 217 | |
| 218 | char *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 | |
| 235 | enum 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 | |