blob: 87bf588f82b428dad19ad96df4dc9b106973cf9d [file] [log] [blame]
Harald Welte0f317752010-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
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 Welteb31c9df2010-03-06 11:38:05 +010030#include <arpa/inet.h>
31
Harald Welte0f317752010-03-05 19:36:20 +010032#include <osmocore/talloc.h>
33#include <osmocore/timer.h>
Harald Welteb31c9df2010-03-06 11:38:05 +010034#include <osmocore/rxlev_stat.h>
Harald Welte0f317752010-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>
40
Harald Welteb31c9df2010-03-06 11:38:05 +010041#define WHITELIST_MAX_SIZE ((NUM_ARFCNS*2)+2+1)
42
43int 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 Welte0f317752010-03-05 19:36:20 +010060enum 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 Welteb31c9df2010-03-06 11:38:05 +010067int ipac_nwl_test_start(struct gsm_bts_trx *trx, uint8_t testnr,
68 const uint8_t *phys_conf, unsigned int phys_conf_len)
Harald Welte0f317752010-03-05 19:36:20 +010069{
Harald Welteb31c9df2010-03-06 11:38:05 +010070 struct msgb *msg;
Harald Welte0f317752010-03-05 19:36:20 +010071
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 Welteb31c9df2010-03-06 11:38:05 +010077 switch (testnr) {
78 case NM_IPACC_TESTNO_CHAN_USAGE:
79 rxlev_stat_reset(&trx->ipaccess.rxlev_stat);
80 break;
81 }
82
83 msg = msgb_alloc_headroom(phys_conf_len+256, 128, "OML");
84
85 if (phys_conf && phys_conf_len) {
86 uint8_t *payload;
87 /* first put the phys conf header */
88 msgb_tv16_put(msg, NM_ATT_PHYS_CONF, phys_conf_len);
89 payload = msgb_put(msg, phys_conf_len);
90 memcpy(payload, phys_conf, phys_conf_len);
91 }
92
93 abis_nm_perform_test(trx->bts, NM_OC_RADIO_CARRIER, 0, trx->nr, 0xff,
94 testnr, 1, msg);
Harald Welte0f317752010-03-05 19:36:20 +010095
96 /* FIXME: start safety timer until when test is supposed to complete */
97
98 return 0;
99}
100
101struct ipacc_ferr_elem {
102 int16_t freq_err;
103 uint8_t freq_qual;
104 uint8_t arfcn;
105} __attribute__((packed));
106
107struct ipacc_cusage_elem {
108 uint16_t arfcn:10,
109 rxlev:6;
110} __attribute__ ((packed));
111
112static int test_rep(void *_msg)
113{
114 struct msgb *msg = _msg;
115 struct abis_om_fom_hdr *foh = msgb_l3(msg);
116 uint16_t test_rep_len, ferr_list_len;
117 struct ipacc_ferr_elem *ife;
118 struct ipac_bcch_info binfo;
119 int i, rc;
120
121 DEBUGP(DNM, "TEST REPORT: ");
122
123 if (foh->data[0] != NM_ATT_TEST_NO ||
124 foh->data[2] != NM_ATT_TEST_REPORT)
125 return -EINVAL;
126
127 DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]);
128 /* data[2] == NM_ATT_TEST_REPORT */
129 /* data[3..4]: test_rep_len */
130 test_rep_len = ntohs(*(uint16_t *) &foh->data[3]);
131 /* data[5]: ip.access test result */
132 DEBUGPC(DNM, "tst_res=%s\n", ipacc_testres_name(foh->data[5]));
133
134 /* data[6]: ip.access nested IE. 3 == freq_err_list */
135 switch (foh->data[6]) {
136 case NM_IPAC_EIE_FREQ_ERR_LIST:
137 /* data[7..8]: length of ferr_list */
138 ferr_list_len = ntohs(*(uint16_t *) &foh->data[7]);
139
140 /* data[9...]: frequency error list elements */
141 for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) {
142 ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i);
143 DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n",
144 ife->arfcn, ntohs(ife->freq_err));
145 }
146 break;
147 case NM_IPAC_EIE_CHAN_USE_LIST:
148 /* data[7..8]: length of ferr_list */
149 ferr_list_len = ntohs(*(uint16_t *) &foh->data[7]);
150
151 /* data[9...]: channel usage list elements */
152 for (i = 0; i < ferr_list_len; i+= 2) {
153 uint16_t *cu_ptr = (uint16_t *)(foh->data + 9 + i);
154 uint16_t cu = ntohs(*cu_ptr);
Harald Welteb31c9df2010-03-06 11:38:05 +0100155 uint16_t arfcn = cu & 0x3ff;
156 uint8_t rxlev = cu >> 10;
157 DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n", arfcn, rxlev);
158 rxlev_stat_input(&msg->trx->ipaccess.rxlev_stat,
159 arfcn, rxlev);
Harald Welte0f317752010-03-05 19:36:20 +0100160 }
161 break;
162 case NM_IPAC_EIE_BCCH_INFO_TYPE:
163 break;
164 case NM_IPAC_EIE_BCCH_INFO:
165 rc = ipac_parse_bcch_info(&binfo, foh->data+6);
166 if (rc < 0) {
167 DEBUGP(DNM, "BCCH Info parsing failed\n");
168 break;
169 }
170 DEBUGP(DNM, "==> ARFCN %u, RxLev %2u, RxQual %2u: %3d-%d, LAC %d CI %d\n",
171 binfo.arfcn, binfo.rx_lev, binfo.rx_qual,
172 binfo.cgi.mcc, binfo.cgi.mnc,
173 binfo.cgi.lac, binfo.cgi.ci);
174 break;
175 default:
176 break;
177 }
178
179 switch (foh->data[5]) {
180 case NM_IPACC_TESTRES_SUCCESS:
181 case NM_IPACC_TESTRES_STOPPED:
182 case NM_IPACC_TESTRES_TIMEOUT:
183 case NM_IPACC_TESTRES_NO_CHANS:
184 msg->trx->ipaccess.test_state = IPAC_TEST_S_IDLE;
185 /* Send signal to notify higher layers of test completion */
Harald Welteb31c9df2010-03-06 11:38:05 +0100186 DEBUGP(DNM, "dispatching S_IPAC_NWL_COMPLETE signal\n");
Harald Welte0f317752010-03-05 19:36:20 +0100187 dispatch_signal(SS_IPAC_NWL, S_IPAC_NWL_COMPLETE, msg->trx);
188 break;
189 case NM_IPACC_TESTRES_PARTIAL:
190 msg->trx->ipaccess.test_state = IPAC_TEST_S_PARTIAL;
191 break;
192 }
193
194 return 0;
195}
196
197static int nwl_sig_cb(unsigned int subsys, unsigned int signal,
198 void *handler_data, void *signal_data)
199{
Harald Welte0f317752010-03-05 19:36:20 +0100200 switch (signal) {
201 case S_NM_TEST_REP:
202 return test_rep(signal_data);
203 default:
204 break;
205 }
206
207 return 0;
208}
209
210void ipac_nwl_init(void)
211{
212 register_signal_handler(SS_NM, nwl_sig_cb, NULL);
213}