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> |
| 25 | |
| 26 | #include <openbsc/gsm_data.h> |
| 27 | |
| 28 | void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr, |
| 29 | u_int8_t e1_ts, u_int8_t e1_ts_ss) |
| 30 | { |
| 31 | ts->e1_link.e1_nr = e1_nr; |
| 32 | ts->e1_link.e1_ts = e1_ts; |
| 33 | ts->e1_link.e1_ts_ss = e1_ts_ss; |
| 34 | } |
| 35 | |
| 36 | static const char *pchan_names[] = { |
| 37 | [GSM_PCHAN_NONE] = "NONE", |
| 38 | [GSM_PCHAN_CCCH] = "CCCH", |
| 39 | [GSM_PCHAN_CCCH_SDCCH4] = "CCCH+SDCCH4", |
| 40 | [GSM_PCHAN_TCH_F] = "TCH/F", |
| 41 | [GSM_PCHAN_TCH_H] = "TCH/H", |
| 42 | [GSM_PCHAN_SDCCH8_SACCH8C] = "SDCCH8", |
| 43 | [GSM_PCHAN_UNKNOWN] = "UNKNOWN", |
| 44 | }; |
| 45 | |
| 46 | const char *gsm_pchan_name(enum gsm_phys_chan_config c) |
| 47 | { |
| 48 | if (c >= ARRAY_SIZE(pchan_names)) |
| 49 | return "INVALID"; |
| 50 | |
| 51 | return pchan_names[c]; |
| 52 | } |
| 53 | |
| 54 | static const char *lchan_names[] = { |
| 55 | [GSM_LCHAN_NONE] = "NONE", |
| 56 | [GSM_LCHAN_SDCCH] = "SDCCH", |
| 57 | [GSM_LCHAN_TCH_F] = "TCH/F", |
| 58 | [GSM_LCHAN_TCH_H] = "TCH/H", |
| 59 | [GSM_LCHAN_UNKNOWN] = "UNKNOWN", |
| 60 | }; |
| 61 | |
| 62 | const char *gsm_lchan_name(enum gsm_chan_t c) |
| 63 | { |
| 64 | if (c >= ARRAY_SIZE(lchan_names)) |
| 65 | return "INVALID"; |
| 66 | |
| 67 | return lchan_names[c]; |
| 68 | } |
| 69 | |
| 70 | static const char *chreq_names[] = { |
| 71 | [GSM_CHREQ_REASON_EMERG] = "EMERGENCY", |
| 72 | [GSM_CHREQ_REASON_PAG] = "PAGING", |
| 73 | [GSM_CHREQ_REASON_CALL] = "CALL", |
| 74 | [GSM_CHREQ_REASON_LOCATION_UPD] = "LOCATION_UPDATE", |
| 75 | [GSM_CHREQ_REASON_OTHER] = "OTHER", |
| 76 | }; |
| 77 | |
| 78 | const char *gsm_chreq_name(enum gsm_chreq_reason_t c) |
| 79 | { |
| 80 | if (c >= ARRAY_SIZE(chreq_names)) |
| 81 | return "INVALID"; |
| 82 | |
| 83 | return chreq_names[c]; |
| 84 | } |
| 85 | |
| 86 | struct gsm_network *gsm_network_init(unsigned int num_bts, enum gsm_bts_type bts_type, |
| 87 | u_int16_t country_code, u_int16_t network_code) |
| 88 | { |
| 89 | int i; |
| 90 | struct gsm_network *net; |
| 91 | |
| 92 | if (num_bts > GSM_MAX_BTS) |
| 93 | return NULL; |
| 94 | |
| 95 | net = malloc(sizeof(*net)); |
| 96 | if (!net) |
| 97 | return NULL; |
| 98 | memset(net, 0, sizeof(*net)); |
| 99 | |
| 100 | net->country_code = country_code; |
| 101 | net->network_code = network_code; |
| 102 | net->num_bts = num_bts; |
| 103 | |
| 104 | for (i = 0; i < num_bts; i++) { |
| 105 | struct gsm_bts *bts = &net->bts[i]; |
| 106 | int j; |
| 107 | |
| 108 | bts->network = net; |
| 109 | bts->nr = i; |
| 110 | bts->type = bts_type; |
| 111 | bts->tsc = HARDCODED_TSC; |
| 112 | bts->bsic = HARDCODED_BSIC; |
| 113 | |
| 114 | for (j = 0; j < BTS_MAX_TRX; j++) { |
| 115 | struct gsm_bts_trx *trx = &bts->trx[j]; |
| 116 | int k; |
| 117 | |
| 118 | trx->bts = bts; |
| 119 | trx->nr = j; |
| 120 | |
| 121 | for (k = 0; k < 8; k++) { |
| 122 | struct gsm_bts_trx_ts *ts = &trx->ts[k]; |
| 123 | int l; |
| 124 | |
| 125 | ts->trx = trx; |
| 126 | ts->nr = k; |
| 127 | ts->pchan = GSM_PCHAN_NONE; |
| 128 | |
| 129 | for (l = 0; l < TS_MAX_LCHAN; l++) { |
| 130 | struct gsm_lchan *lchan; |
| 131 | lchan = &ts->lchan[l]; |
| 132 | |
| 133 | lchan->ts = ts; |
| 134 | lchan->nr = l; |
| 135 | lchan->type = GSM_LCHAN_NONE; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | bts->num_trx = 1; /* FIXME */ |
| 141 | #ifdef HAVE_TRX1 |
| 142 | bts->num_trx++; |
| 143 | #endif |
| 144 | bts->c0 = &bts->trx[0]; |
| 145 | bts->c0->ts[0].pchan = GSM_PCHAN_CCCH_SDCCH4; |
| 146 | } |
| 147 | return net; |
| 148 | } |
| 149 | |
| 150 | static char ts2str[255]; |
| 151 | |
| 152 | char *gsm_ts_name(struct gsm_bts_trx_ts *ts) |
| 153 | { |
| 154 | snprintf(ts2str, sizeof(ts2str), "(bts=%d,trx=%d,ts=%d)", |
| 155 | ts->trx->bts->bts_nr, ts->trx->nr, ts->nr); |
| 156 | |
| 157 | return ts2str; |
| 158 | } |
| 159 | |
| 160 | static const char *bts_types[] = { |
| 161 | [GSM_BTS_TYPE_UNKNOWN] = "unknown", |
| 162 | [GSM_BTS_TYPE_BS11] = "bs11", |
| 163 | [GSM_BTS_TYPE_NANOBTS_900] = "nanobts900", |
| 164 | [GSM_BTS_TYPE_NANOBTS_1800] = "nanobts1800", |
| 165 | }; |
| 166 | |
| 167 | enum gsm_bts_type parse_btstype(char *arg) |
| 168 | { |
| 169 | int i; |
| 170 | for (i = 0; i < ARRAY_SIZE(bts_types); i++) { |
| 171 | if (!strcmp(arg, bts_types[i])) |
| 172 | return i; |
| 173 | } |
| 174 | return GSM_BTS_TYPE_BS11; /* Default: BS11 */ |
| 175 | } |
| 176 | |
| 177 | char *btstype2str(enum gsm_bts_type type) |
| 178 | { |
| 179 | if (type > ARRAY_SIZE(bts_types)) |
| 180 | return "undefined"; |
| 181 | return bts_types[type]; |
| 182 | } |
| 183 | |
| 184 | /* Search for a BTS in the given Location Area; optionally start searching |
| 185 | * with start_bts (for continuing to search after the first result) */ |
| 186 | struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac, |
| 187 | struct gsm_bts *start_bts) |
| 188 | { |
| 189 | int i; |
| 190 | struct gsm_bts *bts; |
| 191 | int skip = 0; |
| 192 | |
| 193 | if (start_bts) |
| 194 | skip = 1; |
| 195 | |
| 196 | for (i = 0; i < net->num_bts; i++) { |
| 197 | bts = &net->bts[i]; |
| 198 | |
| 199 | if (skip) { |
| 200 | if (start_bts == bts) |
| 201 | skip = 0; |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | if (bts->location_area_code == lac) |
| 206 | return bts; |
| 207 | } |
| 208 | return NULL; |
| 209 | } |