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 | 549faad | 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 | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 41 | #define WHITELIST_MAX_SIZE ((NUM_ARFCNS*2)+2+1) |
| 42 | |
| 43 | int ipac_rxlevstat2whitelist(uint16_t *buf, const struct rxlev_stats *st) |
| 44 | { |
| 45 | int i; |
| 46 | unsigned int num_arfcn = 0; |
| 47 | |
| 48 | for (i = NUM_RXLEVS-1; i >= 0; i--) { |
| 49 | int16_t arfcn = -1; |
| 50 | |
| 51 | while ((arfcn = rxlev_stat_get_next(st, i, arfcn)) >= 0) { |
| 52 | *buf++ = htons(arfcn); |
| 53 | num_arfcn++; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return num_arfcn; |
| 58 | } |
| 59 | |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 60 | enum ipac_test_state { |
| 61 | IPAC_TEST_S_IDLE, |
| 62 | IPAC_TEST_S_RQD, |
| 63 | IPAC_TEST_S_EXEC, |
| 64 | IPAC_TEST_S_PARTIAL, |
| 65 | }; |
| 66 | |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 67 | int ipac_nwl_test_start(struct gsm_bts_trx *trx, uint8_t testnr, |
| 68 | const uint8_t *phys_conf, unsigned int phys_conf_len) |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 69 | { |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 70 | struct msgb *msg; |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 71 | |
| 72 | if (trx->ipaccess.test_state != IPAC_TEST_S_IDLE) { |
| 73 | fprintf(stderr, "Cannot start test in state %u\n", trx->ipaccess.test_state); |
| 74 | return -EINVAL; |
| 75 | } |
| 76 | |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 77 | switch (testnr) { |
| 78 | case NM_IPACC_TESTNO_CHAN_USAGE: |
Harald Welte | a0b0f36 | 2010-03-09 22:35:37 +0100 | [diff] [blame] | 79 | case NM_IPACC_TESTNO_BCCH_CHAN_USAGE: |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 80 | rxlev_stat_reset(&trx->ipaccess.rxlev_stat); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | msg = msgb_alloc_headroom(phys_conf_len+256, 128, "OML"); |
| 85 | |
| 86 | if (phys_conf && phys_conf_len) { |
| 87 | uint8_t *payload; |
| 88 | /* first put the phys conf header */ |
| 89 | msgb_tv16_put(msg, NM_ATT_PHYS_CONF, phys_conf_len); |
| 90 | payload = msgb_put(msg, phys_conf_len); |
| 91 | memcpy(payload, phys_conf, phys_conf_len); |
| 92 | } |
| 93 | |
| 94 | abis_nm_perform_test(trx->bts, NM_OC_RADIO_CARRIER, 0, trx->nr, 0xff, |
| 95 | testnr, 1, msg); |
Harald Welte | a0b0f36 | 2010-03-09 22:35:37 +0100 | [diff] [blame] | 96 | trx->ipaccess.test_nr = testnr; |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 97 | |
| 98 | /* FIXME: start safety timer until when test is supposed to complete */ |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | struct ipacc_ferr_elem { |
| 104 | int16_t freq_err; |
| 105 | uint8_t freq_qual; |
| 106 | uint8_t arfcn; |
| 107 | } __attribute__((packed)); |
| 108 | |
| 109 | struct ipacc_cusage_elem { |
| 110 | uint16_t arfcn:10, |
| 111 | rxlev:6; |
| 112 | } __attribute__ ((packed)); |
| 113 | |
| 114 | static int test_rep(void *_msg) |
| 115 | { |
| 116 | struct msgb *msg = _msg; |
| 117 | struct abis_om_fom_hdr *foh = msgb_l3(msg); |
| 118 | uint16_t test_rep_len, ferr_list_len; |
| 119 | struct ipacc_ferr_elem *ife; |
| 120 | struct ipac_bcch_info binfo; |
| 121 | int i, rc; |
| 122 | |
| 123 | DEBUGP(DNM, "TEST REPORT: "); |
| 124 | |
| 125 | if (foh->data[0] != NM_ATT_TEST_NO || |
| 126 | foh->data[2] != NM_ATT_TEST_REPORT) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]); |
| 130 | /* data[2] == NM_ATT_TEST_REPORT */ |
| 131 | /* data[3..4]: test_rep_len */ |
| 132 | test_rep_len = ntohs(*(uint16_t *) &foh->data[3]); |
| 133 | /* data[5]: ip.access test result */ |
| 134 | DEBUGPC(DNM, "tst_res=%s\n", ipacc_testres_name(foh->data[5])); |
| 135 | |
| 136 | /* data[6]: ip.access nested IE. 3 == freq_err_list */ |
| 137 | switch (foh->data[6]) { |
| 138 | case NM_IPAC_EIE_FREQ_ERR_LIST: |
| 139 | /* data[7..8]: length of ferr_list */ |
| 140 | ferr_list_len = ntohs(*(uint16_t *) &foh->data[7]); |
| 141 | |
| 142 | /* data[9...]: frequency error list elements */ |
| 143 | for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) { |
| 144 | ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i); |
| 145 | DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n", |
| 146 | ife->arfcn, ntohs(ife->freq_err)); |
| 147 | } |
| 148 | break; |
| 149 | case NM_IPAC_EIE_CHAN_USE_LIST: |
| 150 | /* data[7..8]: length of ferr_list */ |
| 151 | ferr_list_len = ntohs(*(uint16_t *) &foh->data[7]); |
| 152 | |
| 153 | /* data[9...]: channel usage list elements */ |
| 154 | for (i = 0; i < ferr_list_len; i+= 2) { |
| 155 | uint16_t *cu_ptr = (uint16_t *)(foh->data + 9 + i); |
| 156 | uint16_t cu = ntohs(*cu_ptr); |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 157 | uint16_t arfcn = cu & 0x3ff; |
| 158 | uint8_t rxlev = cu >> 10; |
| 159 | DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n", arfcn, rxlev); |
| 160 | rxlev_stat_input(&msg->trx->ipaccess.rxlev_stat, |
| 161 | arfcn, rxlev); |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 162 | } |
| 163 | break; |
| 164 | case NM_IPAC_EIE_BCCH_INFO_TYPE: |
| 165 | break; |
| 166 | case NM_IPAC_EIE_BCCH_INFO: |
| 167 | rc = ipac_parse_bcch_info(&binfo, foh->data+6); |
| 168 | if (rc < 0) { |
| 169 | DEBUGP(DNM, "BCCH Info parsing failed\n"); |
| 170 | break; |
| 171 | } |
| 172 | DEBUGP(DNM, "==> ARFCN %u, RxLev %2u, RxQual %2u: %3d-%d, LAC %d CI %d\n", |
| 173 | binfo.arfcn, binfo.rx_lev, binfo.rx_qual, |
| 174 | binfo.cgi.mcc, binfo.cgi.mnc, |
| 175 | binfo.cgi.lac, binfo.cgi.ci); |
| 176 | break; |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | switch (foh->data[5]) { |
| 182 | case NM_IPACC_TESTRES_SUCCESS: |
| 183 | case NM_IPACC_TESTRES_STOPPED: |
| 184 | case NM_IPACC_TESTRES_TIMEOUT: |
| 185 | case NM_IPACC_TESTRES_NO_CHANS: |
| 186 | msg->trx->ipaccess.test_state = IPAC_TEST_S_IDLE; |
| 187 | /* Send signal to notify higher layers of test completion */ |
Harald Welte | 887deab | 2010-03-06 11:38:05 +0100 | [diff] [blame] | 188 | DEBUGP(DNM, "dispatching S_IPAC_NWL_COMPLETE signal\n"); |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 189 | dispatch_signal(SS_IPAC_NWL, S_IPAC_NWL_COMPLETE, msg->trx); |
| 190 | break; |
| 191 | case NM_IPACC_TESTRES_PARTIAL: |
| 192 | msg->trx->ipaccess.test_state = IPAC_TEST_S_PARTIAL; |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int nwl_sig_cb(unsigned int subsys, unsigned int signal, |
| 200 | void *handler_data, void *signal_data) |
| 201 | { |
Harald Welte | 549faad | 2010-03-05 19:36:20 +0100 | [diff] [blame] | 202 | switch (signal) { |
| 203 | case S_NM_TEST_REP: |
| 204 | return test_rep(signal_data); |
| 205 | default: |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | void ipac_nwl_init(void) |
| 213 | { |
| 214 | register_signal_handler(SS_NM, nwl_sig_cb, NULL); |
| 215 | } |