blob: 62be5ff9534cd55327fd3e94c7ac3f819b3f1823 [file] [log] [blame]
Harald Welte25de9912009-04-30 15:53:07 +00001/* ip.access nanoBTS configuration tool */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <unistd.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <getopt.h>
27#include <sys/types.h>
28
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32
33
34#include <openbsc/select.h>
35#include <openbsc/timer.h>
36#include <openbsc/ipaccess.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/e1_input.h>
39#include <openbsc/abis_nm.h>
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +020040#include <openbsc/signal.h>
Harald Weltec7310382009-08-08 00:02:36 +020041#include <openbsc/debug.h>
Harald Welte25de9912009-04-30 15:53:07 +000042
43static struct gsm_network *gsmnet;
44
Harald Weltef1676e52009-08-10 11:39:47 +020045static int net_listen_testnr;
Harald Welte25de9912009-04-30 15:53:07 +000046static int restart;
47static char *prim_oml_ip;
48static char *unit_id;
Harald Welte684b1a82009-07-03 11:26:45 +020049static u_int16_t nv_flags;
50static u_int16_t nv_mask;
Harald Welte25de9912009-04-30 15:53:07 +000051
52/*
53static u_int8_t prim_oml_attr[] = { 0x95, 0x00, 7, 0x88, 192, 168, 100, 11, 0x00, 0x00 };
54static u_int8_t unit_id_attr[] = { 0x91, 0x00, 9, '2', '3', '4', '2', '/' , '0', '/', '0', 0x00 };
55*/
56
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +020057/*
58 * Callback function for NACK on the OML NM
59 *
60 * Currently we send the config requests but don't check the
61 * result. The nanoBTS will send us a NACK when we did something the
62 * BTS didn't like.
63 */
64static int ipacc_msg_nack(int mt)
65{
66 fprintf(stderr, "Failure to set attribute. This seems fatal\n");
67 exit(-1);
68 return 0;
69}
70
Harald Weltec7310382009-08-08 00:02:36 +020071struct ipacc_ferr_elem {
72 int16_t freq_err;
73 u_int8_t freq_qual;
74 u_int8_t arfcn;
75} __attribute__((packed));
76
Harald Weltef1676e52009-08-10 11:39:47 +020077struct ipacc_cusage_elem {
78 u_int16_t arfcn:10,
79 rxlev:6;
80} __attribute__ ((packed));
81
Harald Weltec7310382009-08-08 00:02:36 +020082static int test_rep(void *_msg)
83{
84 struct msgb *msg = _msg;
85 struct abis_om_fom_hdr *foh = msgb_l3(msg);
86 u_int16_t test_rep_len, ferr_list_len;
87 struct ipacc_ferr_elem *ife;
88 int i;
89
90 DEBUGP(DNM, "TEST REPORT: ");
91
92 if (foh->data[0] != NM_ATT_TEST_NO ||
93 foh->data[2] != NM_ATT_TEST_REPORT)
94 return -EINVAL;
95
96 DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]);
97 /* data[2] == NM_ATT_TEST_REPORT */
98 /* data[3..4]: test_rep_len */
99 test_rep_len = ntohs(*(u_int16_t *) &foh->data[3]);
100 /* data[5]: ip.access test result */
Harald Weltef1676e52009-08-10 11:39:47 +0200101 DEBUGPC(DNM, "test_res=%s\n", ipacc_testres_name(foh->data[5]));
Harald Weltec7310382009-08-08 00:02:36 +0200102
103 /* data[6]: ip.access nested IE. 3 == freq_err_list */
104 switch (foh->data[6]) {
105 case 3:
106 /* data[7..8]: length of ferr_list */
107 ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]);
108
109 /* data[9...]: frequency error list elements */
110 for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) {
111 ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i);
112 DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n",
113 ife->arfcn, ntohs(ife->freq_err));
114 }
115 break;
Harald Weltef1676e52009-08-10 11:39:47 +0200116 case 4:
117 /* data[7..8]: length of ferr_list */
118 ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]);
119
120 /* data[9...]: channel usage list elements */
121 for (i = 0; i < ferr_list_len; i+= 2) {
122 u_int16_t *cu_ptr = (u_int16_t *)(foh->data + 9 + i);
123 u_int16_t cu = ntohs(*cu_ptr);
124 DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n",
125 cu & 0x3ff, cu >> 10);
126 }
127 break;
Harald Weltec7310382009-08-08 00:02:36 +0200128 default:
129 break;
130 }
131
132 return 0;
133}
134
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +0200135static int nm_sig_cb(unsigned int subsys, unsigned int signal,
136 void *handler_data, void *signal_data)
137{
138 switch (signal) {
139 case S_NM_IPACC_NACK:
140 return ipacc_msg_nack((int)signal_data);
Harald Weltec7310382009-08-08 00:02:36 +0200141 case S_NM_TEST_REP:
142 return test_rep(signal_data);
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +0200143 default:
144 break;
145 }
146
147 return 0;
148}
149
Harald Welte25de9912009-04-30 15:53:07 +0000150static void bootstrap_om(struct gsm_bts *bts)
151{
152 int len;
153 static u_int8_t buf[1024];
Harald Welte684b1a82009-07-03 11:26:45 +0200154 u_int8_t *cur = buf;
Harald Welte25de9912009-04-30 15:53:07 +0000155
156 printf("OML link established\n");
157
158 if (unit_id) {
159 len = strlen(unit_id);
160 if (len > sizeof(buf)-10)
161 return;
162 buf[0] = NM_ATT_IPACC_UNIT_ID;
163 buf[1] = (len+1) >> 8;
164 buf[2] = (len+1) & 0xff;
165 memcpy(buf+3, unit_id, len);
166 buf[3+len] = 0;
167 printf("setting Unit ID to '%s'\n", unit_id);
168 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len+1);
169 }
170 if (prim_oml_ip) {
171 struct in_addr ia;
Harald Welte25de9912009-04-30 15:53:07 +0000172
173 if (!inet_aton(prim_oml_ip, &ia)) {
174 fprintf(stderr, "invalid IP address: %s\n",
175 prim_oml_ip);
176 return;
177 }
178
179 /* 0x88 + IP + port */
180 len = 1 + sizeof(ia) + 2;
181
Harald Welte0efe9b72009-07-12 09:33:54 +0200182 *cur++ = NM_ATT_IPACC_PRIM_OML_CFG_LIST;
Harald Welte25de9912009-04-30 15:53:07 +0000183 *cur++ = (len) >> 8;
184 *cur++ = (len) & 0xff;
185 *cur++ = 0x88;
186 memcpy(cur, &ia, sizeof(ia));
187 cur += sizeof(ia);
188 *cur++ = 0;
189 *cur++ = 0;
Harald Welte4802b882009-04-30 16:23:45 +0000190 printf("setting primary OML link IP to '%s'\n", inet_ntoa(ia));
Harald Welte25de9912009-04-30 15:53:07 +0000191 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
192 }
Harald Welte684b1a82009-07-03 11:26:45 +0200193 if (nv_mask) {
194 len = 4;
195
196 *cur++ = NM_ATT_IPACC_NV_FLAGS;
197 *cur++ = (len) >> 8;
198 *cur++ = (len) & 0xff;
199 *cur++ = nv_flags & 0xff;
200 *cur++ = nv_mask & 0xff;
201 *cur++ = nv_flags >> 8;
202 *cur++ = nv_mask >> 8;
203 printf("setting NV Flags/Mask to 0x%04x/0x%04x\n",
204 nv_flags, nv_mask);
205 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
206 }
Harald Welte25de9912009-04-30 15:53:07 +0000207
208 if (restart) {
209 printf("restarting BTS\n");
210 abis_nm_ipaccess_restart(bts);
211 }
212}
213
214void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
215{
216 switch (event) {
217 case EVT_E1_TEI_UP:
218 switch (type) {
219 case E1INP_SIGN_OML:
220 bootstrap_om(trx->bts);
221 break;
222 case E1INP_SIGN_RSL:
223 /* FIXME */
224 break;
225 default:
226 break;
227 }
228 break;
229 case EVT_E1_TEI_DN:
230 fprintf(stderr, "Lost some E1 TEI link\n");
231 /* FIXME: deal with TEI or L1 link loss */
232 break;
233 default:
234 break;
235 }
236}
237
238int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
239 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
240{
Harald Weltec7310382009-08-08 00:02:36 +0200241 if (evt == EVT_STATECHG_OPER &&
242 obj_class == NM_OC_RADIO_CARRIER &&
243 new_state->availability == 3 &&
Harald Weltef1676e52009-08-10 11:39:47 +0200244 net_listen_testnr) {
Harald Weltec7310382009-08-08 00:02:36 +0200245 struct gsm_bts_trx *trx = obj;
246 u_int8_t phys_config[] = { 0x02, 0x0a, 0x00, 0x01, 0x02 };
247 abis_nm_perform_test(trx->bts, 2, 0, 0, 0xff,
Harald Weltef1676e52009-08-10 11:39:47 +0200248 net_listen_testnr, 1,
Harald Weltec7310382009-08-08 00:02:36 +0200249 phys_config, sizeof(phys_config));
250 }
Harald Welte25de9912009-04-30 15:53:07 +0000251 return 0;
252}
253
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200254static void print_usage(void)
255{
256 printf("Usage: ipaccess-config\n");
257}
258
259static void print_help(void)
260{
261 printf(" -u --unit-id UNIT_ID\n");
262 printf(" -o --oml-ip ip\n");
263 printf(" -r --restart\n");
Harald Weltec7310382009-08-08 00:02:36 +0200264 printf(" -n flags/mask\tSet NVRAM attributes.\n");
Harald Weltef1676e52009-08-10 11:39:47 +0200265 printf(" -l --listen testnr \tPerform speciified test number\n");
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200266 printf(" -h --help this text\n");
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100267 printf(" -s --stream-id ID\n");
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200268}
269
Harald Welte25de9912009-04-30 15:53:07 +0000270int main(int argc, char **argv)
271{
272 struct gsm_bts *bts;
273 struct sockaddr_in sin;
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100274 int rc, option_index = 0, stream_id = 0xff;
Harald Welte25de9912009-04-30 15:53:07 +0000275
276 printf("ipaccess-config (C) 2009 by Harald Welte\n");
277 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
278
279 while (1) {
280 int c;
Harald Welte684b1a82009-07-03 11:26:45 +0200281 unsigned long ul;
282 char *slash;
Harald Welte25de9912009-04-30 15:53:07 +0000283 static struct option long_options[] = {
284 { "unit-id", 1, 0, 'u' },
285 { "oml-ip", 1, 0, 'o' },
286 { "restart", 0, 0, 'r' },
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200287 { "help", 0, 0, 'h' },
Harald Weltef1676e52009-08-10 11:39:47 +0200288 { "listen", 1, 0, 'l' },
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100289 { "stream-id", 1, 0, 's' },
Harald Welte25de9912009-04-30 15:53:07 +0000290 };
291
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100292 c = getopt_long(argc, argv, "u:o:rn:l:hs:", long_options,
Harald Welte25de9912009-04-30 15:53:07 +0000293 &option_index);
294
295 if (c == -1)
296 break;
297
298 switch (c) {
299 case 'u':
300 unit_id = optarg;
301 break;
302 case 'o':
303 prim_oml_ip = optarg;
304 break;
305 case 'r':
306 restart = 1;
307 break;
Harald Welte684b1a82009-07-03 11:26:45 +0200308 case 'n':
309 slash = strchr(optarg, '/');
310 if (!slash)
311 exit(2);
312 ul = strtoul(optarg, NULL, 16);
313 nv_flags = ul & 0xffff;
314 ul = strtoul(slash+1, NULL, 16);
315 nv_mask = ul & 0xffff;
316 break;
Harald Weltec7310382009-08-08 00:02:36 +0200317 case 'l':
Harald Weltef1676e52009-08-10 11:39:47 +0200318 net_listen_testnr = atoi(optarg);
Harald Weltec7310382009-08-08 00:02:36 +0200319 break;
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100320 case 's':
321 stream_id = atoi(optarg);
322 printf("foo: %d\n", stream_id);
323 break;
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200324 case 'h':
325 print_usage();
326 print_help();
327 exit(0);
Harald Welte25de9912009-04-30 15:53:07 +0000328 }
329 };
330
331 if (optind >= argc) {
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200332 fprintf(stderr, "you have to specify the IP address of the BTS. Use --help for more information\n");
Harald Welte25de9912009-04-30 15:53:07 +0000333 exit(2);
334 }
335
Harald Weltee441d9c2009-06-21 16:17:15 +0200336 gsmnet = gsm_network_init(1, 1, NULL);
Harald Welte25de9912009-04-30 15:53:07 +0000337 if (!gsmnet)
338 exit(1);
339
Mike Habene2d82272009-10-02 12:19:34 +0100340 bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS, HARDCODED_TSC,
Harald Weltee441d9c2009-06-21 16:17:15 +0200341 HARDCODED_BSIC);
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100342 bts->oml_tei = stream_id;
Harald Welte25de9912009-04-30 15:53:07 +0000343
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +0200344 register_signal_handler(SS_NM, nm_sig_cb, NULL);
Harald Welte25de9912009-04-30 15:53:07 +0000345 printf("Trying to connect to ip.access BTS ...\n");
346
347 memset(&sin, 0, sizeof(sin));
348 sin.sin_family = AF_INET;
349 inet_aton(argv[optind], &sin.sin_addr);
350 rc = ia_config_connect(bts, &sin);
351 if (rc < 0) {
352 perror("Error connecting to the BTS");
353 exit(1);
354 }
355
356 while (1) {
Harald Welte04d3c922009-05-23 06:07:04 +0000357 rc = bsc_select_main(0);
Harald Welte25de9912009-04-30 15:53:07 +0000358 if (rc < 0)
359 exit(3);
360 }
361
362 exit(0);
363}
364