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