blob: 3b44ceb744e8ace7831a0c36e5564eea6a08ee18 [file] [log] [blame]
Harald Welte549faad2010-03-05 19:36:20 +01001/* 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 Welte9af6ddf2011-01-01 15:25:50 +01008 * 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 Welte549faad2010-03-05 19:36:20 +010010 * (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 Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte549faad2010-03-05 19:36:20 +010016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * 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 Welte549faad2010-03-05 19:36:20 +010019 *
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 Welte887deab2010-03-06 11:38:05 +010029#include <arpa/inet.h>
30
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010031#include <osmocom/core/talloc.h>
32#include <osmocom/core/timer.h>
33#include <osmocom/gsm/rxlev_stat.h>
34#include <osmocom/gsm/gsm48_ie.h>
Harald Welte549faad2010-03-05 19:36:20 +010035
36#include <openbsc/gsm_data.h>
37#include <openbsc/abis_nm.h>
38#include <openbsc/signal.h>
39#include <openbsc/debug.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020040#include <osmocom/abis/e1_input.h>
Harald Welte549faad2010-03-05 19:36:20 +010041
Harald Welte887deab2010-03-06 11:38:05 +010042#define WHITELIST_MAX_SIZE ((NUM_ARFCNS*2)+2+1)
43
Harald Weltec95cf102010-07-22 20:12:09 +020044int ipac_rxlevstat2whitelist(uint16_t *buf, const struct rxlev_stats *st, uint8_t min_rxlev,
45 uint16_t max_num_arfcns)
Harald Welte887deab2010-03-06 11:38:05 +010046{
47 int i;
48 unsigned int num_arfcn = 0;
49
Harald Weltec95cf102010-07-22 20:12:09 +020050 for (i = NUM_RXLEVS-1; i >= min_rxlev; i--) {
Harald Welte887deab2010-03-06 11:38:05 +010051 int16_t arfcn = -1;
52
53 while ((arfcn = rxlev_stat_get_next(st, i, arfcn)) >= 0) {
54 *buf++ = htons(arfcn);
55 num_arfcn++;
Harald Weltec95cf102010-07-22 20:12:09 +020056
Harald Welte887deab2010-03-06 11:38:05 +010057 }
Harald Weltec95cf102010-07-22 20:12:09 +020058
59 if (num_arfcn > max_num_arfcns)
60 break;
Harald Welte887deab2010-03-06 11:38:05 +010061 }
62
63 return num_arfcn;
64}
65
Harald Welte549faad2010-03-05 19:36:20 +010066enum 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 Welte887deab2010-03-06 11:38:05 +010073int ipac_nwl_test_start(struct gsm_bts_trx *trx, uint8_t testnr,
74 const uint8_t *phys_conf, unsigned int phys_conf_len)
Harald Welte549faad2010-03-05 19:36:20 +010075{
Harald Welte887deab2010-03-06 11:38:05 +010076 struct msgb *msg;
Harald Welte549faad2010-03-05 19:36:20 +010077
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 Welte887deab2010-03-06 11:38:05 +010083 switch (testnr) {
84 case NM_IPACC_TESTNO_CHAN_USAGE:
Harald Weltea0b0f362010-03-09 22:35:37 +010085 case NM_IPACC_TESTNO_BCCH_CHAN_USAGE:
Harald Welte887deab2010-03-06 11:38:05 +010086 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 Weltea0b0f362010-03-09 22:35:37 +0100102 trx->ipaccess.test_nr = testnr;
Harald Welte549faad2010-03-05 19:36:20 +0100103
104 /* FIXME: start safety timer until when test is supposed to complete */
105
106 return 0;
107}
108
Harald Welte6526ca72010-08-04 13:13:07 +0200109static uint16_t last_arfcn;
110static 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 Welte549faad2010-03-05 19:36:20 +0100115struct ipacc_ferr_elem {
116 int16_t freq_err;
117 uint8_t freq_qual;
118 uint8_t arfcn;
119} __attribute__((packed));
120
121struct ipacc_cusage_elem {
122 uint16_t arfcn:10,
123 rxlev:6;
124} __attribute__ ((packed));
125
126static 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;
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200133 struct e1inp_sign_link *sign_link = (struct e1inp_sign_link *)msg->dst;
Harald Welte549faad2010-03-05 19:36:20 +0100134 int i, rc;
135
136 DEBUGP(DNM, "TEST REPORT: ");
137
138 if (foh->data[0] != NM_ATT_TEST_NO ||
139 foh->data[2] != NM_ATT_TEST_REPORT)
140 return -EINVAL;
141
142 DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]);
143 /* data[2] == NM_ATT_TEST_REPORT */
144 /* data[3..4]: test_rep_len */
Holger Hans Peter Freyther444d7d82014-04-04 13:01:28 +0200145 memcpy(&test_rep_len, &foh->data[3], sizeof(uint16_t));
146 test_rep_len = ntohs(test_rep_len);
Harald Welte549faad2010-03-05 19:36:20 +0100147 /* data[5]: ip.access test result */
148 DEBUGPC(DNM, "tst_res=%s\n", ipacc_testres_name(foh->data[5]));
149
150 /* data[6]: ip.access nested IE. 3 == freq_err_list */
151 switch (foh->data[6]) {
152 case NM_IPAC_EIE_FREQ_ERR_LIST:
153 /* data[7..8]: length of ferr_list */
Holger Hans Peter Freyther444d7d82014-04-04 13:01:28 +0200154 memcpy(&ferr_list_len, &foh->data[7], sizeof(uint16_t));
155 ferr_list_len = ntohs(ferr_list_len);
Harald Welte549faad2010-03-05 19:36:20 +0100156
157 /* data[9...]: frequency error list elements */
158 for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) {
159 ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i);
160 DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n",
161 ife->arfcn, ntohs(ife->freq_err));
162 }
163 break;
164 case NM_IPAC_EIE_CHAN_USE_LIST:
165 /* data[7..8]: length of ferr_list */
Holger Hans Peter Freyther444d7d82014-04-04 13:01:28 +0200166 memcpy(&ferr_list_len, &foh->data[7], sizeof(uint16_t));
167 ferr_list_len = ntohs(ferr_list_len);
Harald Welte549faad2010-03-05 19:36:20 +0100168
169 /* data[9...]: channel usage list elements */
170 for (i = 0; i < ferr_list_len; i+= 2) {
171 uint16_t *cu_ptr = (uint16_t *)(foh->data + 9 + i);
172 uint16_t cu = ntohs(*cu_ptr);
Harald Welte887deab2010-03-06 11:38:05 +0100173 uint16_t arfcn = cu & 0x3ff;
174 uint8_t rxlev = cu >> 10;
175 DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n", arfcn, rxlev);
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200176 rxlev_stat_input(&sign_link->trx->ipaccess.rxlev_stat,
Harald Welte887deab2010-03-06 11:38:05 +0100177 arfcn, rxlev);
Harald Welte549faad2010-03-05 19:36:20 +0100178 }
179 break;
180 case NM_IPAC_EIE_BCCH_INFO_TYPE:
181 break;
182 case NM_IPAC_EIE_BCCH_INFO:
183 rc = ipac_parse_bcch_info(&binfo, foh->data+6);
184 if (rc < 0) {
185 DEBUGP(DNM, "BCCH Info parsing failed\n");
186 break;
187 }
Harald Welte1e194a32010-07-30 22:30:35 +0200188 DEBUGP(DNM, "==> ARFCN %u, RxLev %2u, RxQual %2u: %3d-%d, LAC %d CI %d BSIC %u\n",
Harald Welte549faad2010-03-05 19:36:20 +0100189 binfo.arfcn, binfo.rx_lev, binfo.rx_qual,
190 binfo.cgi.mcc, binfo.cgi.mnc,
Harald Welte1e194a32010-07-30 22:30:35 +0200191 binfo.cgi.lac, binfo.cgi.ci, binfo.bsic);
Harald Welte6526ca72010-08-04 13:13:07 +0200192
193 if (binfo.arfcn != last_arfcn) {
194 /* report is on a new arfcn, need to clear channel list */
195 memset(nwl_si_freq, 0, sizeof(nwl_si_freq));
196 last_arfcn = binfo.arfcn;
197 }
198 if (binfo.info_type & IPAC_BINF_NEIGH_BA_SI2) {
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200199 DEBUGP(DNM, "BA SI2: %s\n", osmo_hexdump(binfo.ba_list_si2, sizeof(binfo.ba_list_si2)));
Harald Welte6526ca72010-08-04 13:13:07 +0200200 gsm48_decode_freq_list(nwl_si_freq, binfo.ba_list_si2, sizeof(binfo.ba_list_si2),
201 0x8c, FREQ_TYPE_NCELL_2);
202 }
203 if (binfo.info_type & IPAC_BINF_NEIGH_BA_SI2bis) {
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200204 DEBUGP(DNM, "BA SI2bis: %s\n", osmo_hexdump(binfo.ba_list_si2bis, sizeof(binfo.ba_list_si2bis)));
Harald Welte6526ca72010-08-04 13:13:07 +0200205 gsm48_decode_freq_list(nwl_si_freq, binfo.ba_list_si2bis, sizeof(binfo.ba_list_si2bis),
206 0x8e, FREQ_TYPE_NCELL_2bis);
207 }
208 if (binfo.info_type & IPAC_BINF_NEIGH_BA_SI2ter) {
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200209 DEBUGP(DNM, "BA SI2ter: %s\n", osmo_hexdump(binfo.ba_list_si2ter, sizeof(binfo.ba_list_si2ter)));
Harald Welte6526ca72010-08-04 13:13:07 +0200210 gsm48_decode_freq_list(nwl_si_freq, binfo.ba_list_si2ter, sizeof(binfo.ba_list_si2ter),
211 0x8e, FREQ_TYPE_NCELL_2ter);
212 }
213 for (i = 0; i < ARRAY_SIZE(nwl_si_freq); i++) {
214 if (nwl_si_freq[i].mask)
215 DEBUGP(DNM, "Neighbor Cell on ARFCN %u\n", i);
216 }
Harald Welte549faad2010-03-05 19:36:20 +0100217 break;
218 default:
219 break;
220 }
221
222 switch (foh->data[5]) {
223 case NM_IPACC_TESTRES_SUCCESS:
224 case NM_IPACC_TESTRES_STOPPED:
225 case NM_IPACC_TESTRES_TIMEOUT:
226 case NM_IPACC_TESTRES_NO_CHANS:
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200227 sign_link->trx->ipaccess.test_state = IPAC_TEST_S_IDLE;
Harald Welte549faad2010-03-05 19:36:20 +0100228 /* Send signal to notify higher layers of test completion */
Harald Welte887deab2010-03-06 11:38:05 +0100229 DEBUGP(DNM, "dispatching S_IPAC_NWL_COMPLETE signal\n");
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200230 osmo_signal_dispatch(SS_IPAC_NWL, S_IPAC_NWL_COMPLETE,
231 sign_link->trx);
Harald Welte549faad2010-03-05 19:36:20 +0100232 break;
233 case NM_IPACC_TESTRES_PARTIAL:
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200234 sign_link->trx->ipaccess.test_state = IPAC_TEST_S_PARTIAL;
Harald Welte549faad2010-03-05 19:36:20 +0100235 break;
236 }
237
238 return 0;
239}
240
241static int nwl_sig_cb(unsigned int subsys, unsigned int signal,
242 void *handler_data, void *signal_data)
243{
Harald Welte549faad2010-03-05 19:36:20 +0100244 switch (signal) {
245 case S_NM_TEST_REP:
246 return test_rep(signal_data);
247 default:
248 break;
249 }
250
251 return 0;
252}
253
254void ipac_nwl_init(void)
255{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200256 osmo_signal_register_handler(SS_NM, nwl_sig_cb, NULL);
Harald Welte549faad2010-03-05 19:36:20 +0100257}