Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 1 | /* ip.access nanoBTS network listen mode */ |
| 2 | |
| 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | #include <stdint.h> |
| 29 | |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 30 | #include <arpa/inet.h> |
| 31 | |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 32 | #include <osmocore/talloc.h> |
| 33 | #include <osmocore/timer.h> |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 34 | #include <osmocore/rxlev_stat.h> |
Harald Welte | 6526ca7 | 2010-08-04 13:13:07 +0200 | [diff] [blame] | 35 | #include <osmocore/gsm48_ie.h> |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 36 | |
| 37 | #include <openbsc/gsm_data.h> |
| 38 | #include <openbsc/abis_nm.h> |
| 39 | #include <openbsc/signal.h> |
| 40 | #include <openbsc/debug.h> |
| 41 | |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 42 | #define WHITELIST_MAX_SIZE ((NUM_ARFCNS*2)+2+1) |
| 43 | |
Harald Welte | c95cf10 | 2010-07-22 20:12:09 +0200 | [diff] [blame] | 44 | int ipac_rxlevstat2whitelist(uint16_t *buf, const struct rxlev_stats *st, uint8_t min_rxlev, |
| 45 | uint16_t max_num_arfcns) |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 46 | { |
| 47 | int i; |
| 48 | unsigned int num_arfcn = 0; |
| 49 | |
Harald Welte | c95cf10 | 2010-07-22 20:12:09 +0200 | [diff] [blame] | 50 | for (i = NUM_RXLEVS-1; i >= min_rxlev; i--) { |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 51 | int16_t arfcn = -1; |
| 52 | |
| 53 | while ((arfcn = rxlev_stat_get_next(st, i, arfcn)) >= 0) { |
| 54 | *buf++ = htons(arfcn); |
| 55 | num_arfcn++; |
Harald Welte | c95cf10 | 2010-07-22 20:12:09 +0200 | [diff] [blame] | 56 | |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 57 | } |
Harald Welte | c95cf10 | 2010-07-22 20:12:09 +0200 | [diff] [blame] | 58 | |
| 59 | if (num_arfcn > max_num_arfcns) |
| 60 | break; |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return num_arfcn; |
| 64 | } |
| 65 | |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 66 | enum ipac_test_state { |
| 67 | IPAC_TEST_S_IDLE, |
| 68 | IPAC_TEST_S_RQD, |
| 69 | IPAC_TEST_S_EXEC, |
| 70 | IPAC_TEST_S_PARTIAL, |
| 71 | }; |
| 72 | |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 73 | int ipac_nwl_test_start(struct gsm_bts_trx *trx, uint8_t testnr, |
| 74 | const uint8_t *phys_conf, unsigned int phys_conf_len) |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 75 | { |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 76 | struct msgb *msg; |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 77 | |
| 78 | if (trx->ipaccess.test_state != IPAC_TEST_S_IDLE) { |
| 79 | fprintf(stderr, "Cannot start test in state %u\n", trx->ipaccess.test_state); |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 83 | switch (testnr) { |
| 84 | case NM_IPACC_TESTNO_CHAN_USAGE: |
Harald Welte | a0b0f36 | 2010-03-09 22:35:37 +0100 | [diff] [blame] | 85 | case NM_IPACC_TESTNO_BCCH_CHAN_USAGE: |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 86 | rxlev_stat_reset(&trx->ipaccess.rxlev_stat); |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | msg = msgb_alloc_headroom(phys_conf_len+256, 128, "OML"); |
| 91 | |
| 92 | if (phys_conf && phys_conf_len) { |
| 93 | uint8_t *payload; |
| 94 | /* first put the phys conf header */ |
| 95 | msgb_tv16_put(msg, NM_ATT_PHYS_CONF, phys_conf_len); |
| 96 | payload = msgb_put(msg, phys_conf_len); |
| 97 | memcpy(payload, phys_conf, phys_conf_len); |
| 98 | } |
| 99 | |
| 100 | abis_nm_perform_test(trx->bts, NM_OC_RADIO_CARRIER, 0, trx->nr, 0xff, |
| 101 | testnr, 1, msg); |
Harald Welte | a0b0f36 | 2010-03-09 22:35:37 +0100 | [diff] [blame] | 102 | trx->ipaccess.test_nr = testnr; |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 103 | |
| 104 | /* FIXME: start safety timer until when test is supposed to complete */ |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Harald Welte | 6526ca7 | 2010-08-04 13:13:07 +0200 | [diff] [blame] | 109 | static uint16_t last_arfcn; |
| 110 | static struct gsm_sysinfo_freq nwl_si_freq[1024]; |
| 111 | #define FREQ_TYPE_NCELL_2 0x04 /* sub channel of SI 2 */ |
| 112 | #define FREQ_TYPE_NCELL_2bis 0x08 /* sub channel of SI 2bis */ |
| 113 | #define FREQ_TYPE_NCELL_2ter 0x10 /* sub channel of SI 2ter */ |
| 114 | |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 115 | struct ipacc_ferr_elem { |
| 116 | int16_t freq_err; |
| 117 | uint8_t freq_qual; |
| 118 | uint8_t arfcn; |
| 119 | } __attribute__((packed)); |
| 120 | |
| 121 | struct ipacc_cusage_elem { |
| 122 | uint16_t arfcn:10, |
| 123 | rxlev:6; |
| 124 | } __attribute__ ((packed)); |
| 125 | |
| 126 | static int test_rep(void *_msg) |
| 127 | { |
| 128 | struct msgb *msg = _msg; |
| 129 | struct abis_om_fom_hdr *foh = msgb_l3(msg); |
| 130 | uint16_t test_rep_len, ferr_list_len; |
| 131 | struct ipacc_ferr_elem *ife; |
| 132 | struct ipac_bcch_info binfo; |
| 133 | int i, rc; |
| 134 | |
| 135 | DEBUGP(DNM, "TEST REPORT: "); |
| 136 | |
| 137 | if (foh->data[0] != NM_ATT_TEST_NO || |
| 138 | foh->data[2] != NM_ATT_TEST_REPORT) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]); |
| 142 | /* data[2] == NM_ATT_TEST_REPORT */ |
| 143 | /* data[3..4]: test_rep_len */ |
| 144 | test_rep_len = ntohs(*(uint16_t *) &foh->data[3]); |
| 145 | /* data[5]: ip.access test result */ |
| 146 | DEBUGPC(DNM, "tst_res=%s\n", ipacc_testres_name(foh->data[5])); |
| 147 | |
| 148 | /* data[6]: ip.access nested IE. 3 == freq_err_list */ |
| 149 | switch (foh->data[6]) { |
| 150 | case NM_IPAC_EIE_FREQ_ERR_LIST: |
| 151 | /* data[7..8]: length of ferr_list */ |
| 152 | ferr_list_len = ntohs(*(uint16_t *) &foh->data[7]); |
| 153 | |
| 154 | /* data[9...]: frequency error list elements */ |
| 155 | for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) { |
| 156 | ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i); |
| 157 | DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n", |
| 158 | ife->arfcn, ntohs(ife->freq_err)); |
| 159 | } |
| 160 | break; |
| 161 | case NM_IPAC_EIE_CHAN_USE_LIST: |
| 162 | /* data[7..8]: length of ferr_list */ |
| 163 | ferr_list_len = ntohs(*(uint16_t *) &foh->data[7]); |
| 164 | |
| 165 | /* data[9...]: channel usage list elements */ |
| 166 | for (i = 0; i < ferr_list_len; i+= 2) { |
| 167 | uint16_t *cu_ptr = (uint16_t *)(foh->data + 9 + i); |
| 168 | uint16_t cu = ntohs(*cu_ptr); |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 169 | uint16_t arfcn = cu & 0x3ff; |
| 170 | uint8_t rxlev = cu >> 10; |
| 171 | DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n", arfcn, rxlev); |
| 172 | rxlev_stat_input(&msg->trx->ipaccess.rxlev_stat, |
| 173 | arfcn, rxlev); |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 174 | } |
| 175 | break; |
| 176 | case NM_IPAC_EIE_BCCH_INFO_TYPE: |
| 177 | break; |
| 178 | case NM_IPAC_EIE_BCCH_INFO: |
| 179 | rc = ipac_parse_bcch_info(&binfo, foh->data+6); |
| 180 | if (rc < 0) { |
| 181 | DEBUGP(DNM, "BCCH Info parsing failed\n"); |
| 182 | break; |
| 183 | } |
Harald Welte | 1e194a3 | 2010-07-30 22:30:35 +0200 | [diff] [blame] | 184 | DEBUGP(DNM, "==> ARFCN %u, RxLev %2u, RxQual %2u: %3d-%d, LAC %d CI %d BSIC %u\n", |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 185 | binfo.arfcn, binfo.rx_lev, binfo.rx_qual, |
| 186 | binfo.cgi.mcc, binfo.cgi.mnc, |
Harald Welte | 1e194a3 | 2010-07-30 22:30:35 +0200 | [diff] [blame] | 187 | binfo.cgi.lac, binfo.cgi.ci, binfo.bsic); |
Harald Welte | 6526ca7 | 2010-08-04 13:13:07 +0200 | [diff] [blame] | 188 | |
| 189 | if (binfo.arfcn != last_arfcn) { |
| 190 | /* report is on a new arfcn, need to clear channel list */ |
| 191 | memset(nwl_si_freq, 0, sizeof(nwl_si_freq)); |
| 192 | last_arfcn = binfo.arfcn; |
| 193 | } |
| 194 | if (binfo.info_type & IPAC_BINF_NEIGH_BA_SI2) { |
Harald Welte | 5eed0a2 | 2010-07-22 21:17:33 +0200 | [diff] [blame] | 195 | DEBUGP(DNM, "BA SI2: %s\n", hexdump(binfo.ba_list_si2, sizeof(binfo.ba_list_si2))); |
Harald Welte | 6526ca7 | 2010-08-04 13:13:07 +0200 | [diff] [blame] | 196 | gsm48_decode_freq_list(nwl_si_freq, binfo.ba_list_si2, sizeof(binfo.ba_list_si2), |
| 197 | 0x8c, FREQ_TYPE_NCELL_2); |
| 198 | } |
| 199 | if (binfo.info_type & IPAC_BINF_NEIGH_BA_SI2bis) { |
Harald Welte | 5eed0a2 | 2010-07-22 21:17:33 +0200 | [diff] [blame] | 200 | DEBUGP(DNM, "BA SI2bis: %s\n", hexdump(binfo.ba_list_si2bis, sizeof(binfo.ba_list_si2bis))); |
Harald Welte | 6526ca7 | 2010-08-04 13:13:07 +0200 | [diff] [blame] | 201 | gsm48_decode_freq_list(nwl_si_freq, binfo.ba_list_si2bis, sizeof(binfo.ba_list_si2bis), |
| 202 | 0x8e, FREQ_TYPE_NCELL_2bis); |
| 203 | } |
| 204 | if (binfo.info_type & IPAC_BINF_NEIGH_BA_SI2ter) { |
Harald Welte | 5eed0a2 | 2010-07-22 21:17:33 +0200 | [diff] [blame] | 205 | DEBUGP(DNM, "BA SI2ter: %s\n", hexdump(binfo.ba_list_si2ter, sizeof(binfo.ba_list_si2ter))); |
Harald Welte | 6526ca7 | 2010-08-04 13:13:07 +0200 | [diff] [blame] | 206 | gsm48_decode_freq_list(nwl_si_freq, binfo.ba_list_si2ter, sizeof(binfo.ba_list_si2ter), |
| 207 | 0x8e, FREQ_TYPE_NCELL_2ter); |
| 208 | } |
| 209 | for (i = 0; i < ARRAY_SIZE(nwl_si_freq); i++) { |
| 210 | if (nwl_si_freq[i].mask) |
| 211 | DEBUGP(DNM, "Neighbor Cell on ARFCN %u\n", i); |
| 212 | } |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 213 | break; |
| 214 | default: |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | switch (foh->data[5]) { |
| 219 | case NM_IPACC_TESTRES_SUCCESS: |
| 220 | case NM_IPACC_TESTRES_STOPPED: |
| 221 | case NM_IPACC_TESTRES_TIMEOUT: |
| 222 | case NM_IPACC_TESTRES_NO_CHANS: |
| 223 | msg->trx->ipaccess.test_state = IPAC_TEST_S_IDLE; |
| 224 | /* Send signal to notify higher layers of test completion */ |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 225 | DEBUGP(DNM, "dispatching S_IPAC_NWL_COMPLETE signal\n"); |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 226 | dispatch_signal(SS_IPAC_NWL, S_IPAC_NWL_COMPLETE, msg->trx); |
| 227 | break; |
| 228 | case NM_IPACC_TESTRES_PARTIAL: |
| 229 | msg->trx->ipaccess.test_state = IPAC_TEST_S_PARTIAL; |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int nwl_sig_cb(unsigned int subsys, unsigned int signal, |
| 237 | void *handler_data, void *signal_data) |
| 238 | { |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 239 | switch (signal) { |
| 240 | case S_NM_TEST_REP: |
| 241 | return test_rep(signal_data); |
| 242 | default: |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | void ipac_nwl_init(void) |
| 250 | { |
| 251 | register_signal_handler(SS_NM, nwl_sig_cb, NULL); |
| 252 | } |