blob: c0fc2c469abee562ef8244e7b64dc88aa6f69968 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* ip.access nanoBTS configuration tool */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther4b513382009-12-28 03:11:33 +01004 * (C) 2009 by Holger Hans Peter Freyther
5 * (C) 2009 by On Waves
Harald Welte59b04682009-06-10 05:40:52 +08006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <unistd.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <getopt.h>
29#include <sys/types.h>
30
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34
35
36#include <openbsc/select.h>
37#include <openbsc/timer.h>
38#include <openbsc/ipaccess.h>
39#include <openbsc/gsm_data.h>
40#include <openbsc/e1_input.h>
41#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +020042#include <openbsc/signal.h>
Harald Welte0bf8e302009-08-08 00:02:36 +020043#include <openbsc/debug.h>
Harald Welte59b04682009-06-10 05:40:52 +080044
45static struct gsm_network *gsmnet;
46
Harald Welte12a721e2009-08-10 11:39:47 +020047static int net_listen_testnr;
Harald Welte59b04682009-06-10 05:40:52 +080048static int restart;
49static char *prim_oml_ip;
50static char *unit_id;
Harald Welte21460f02009-07-03 11:26:45 +020051static u_int16_t nv_flags;
52static u_int16_t nv_mask;
Holger Hans Peter Freyther4b513382009-12-28 03:11:33 +010053static char *software = NULL;
Harald Welte59b04682009-06-10 05:40:52 +080054
55/*
56static u_int8_t prim_oml_attr[] = { 0x95, 0x00, 7, 0x88, 192, 168, 100, 11, 0x00, 0x00 };
57static u_int8_t unit_id_attr[] = { 0x91, 0x00, 9, '2', '3', '4', '2', '/' , '0', '/', '0', 0x00 };
58*/
59
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +020060/*
61 * Callback function for NACK on the OML NM
62 *
63 * Currently we send the config requests but don't check the
64 * result. The nanoBTS will send us a NACK when we did something the
65 * BTS didn't like.
66 */
Harald Welte6a21c732009-11-17 06:09:56 +010067static int ipacc_msg_nack(u_int8_t mt)
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +020068{
69 fprintf(stderr, "Failure to set attribute. This seems fatal\n");
70 exit(-1);
71 return 0;
72}
73
Harald Welte0bf8e302009-08-08 00:02:36 +020074struct ipacc_ferr_elem {
75 int16_t freq_err;
76 u_int8_t freq_qual;
77 u_int8_t arfcn;
78} __attribute__((packed));
79
Harald Welte12a721e2009-08-10 11:39:47 +020080struct ipacc_cusage_elem {
81 u_int16_t arfcn:10,
82 rxlev:6;
83} __attribute__ ((packed));
84
Harald Welte0bf8e302009-08-08 00:02:36 +020085static int test_rep(void *_msg)
86{
87 struct msgb *msg = _msg;
88 struct abis_om_fom_hdr *foh = msgb_l3(msg);
89 u_int16_t test_rep_len, ferr_list_len;
90 struct ipacc_ferr_elem *ife;
Harald Weltec73d9832009-11-13 14:43:15 +010091 struct ipac_bcch_info binfo;
92 int i, rc;
Harald Welte0bf8e302009-08-08 00:02:36 +020093
94 DEBUGP(DNM, "TEST REPORT: ");
95
96 if (foh->data[0] != NM_ATT_TEST_NO ||
97 foh->data[2] != NM_ATT_TEST_REPORT)
98 return -EINVAL;
99
100 DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]);
101 /* data[2] == NM_ATT_TEST_REPORT */
102 /* data[3..4]: test_rep_len */
103 test_rep_len = ntohs(*(u_int16_t *) &foh->data[3]);
104 /* data[5]: ip.access test result */
Harald Welte12a721e2009-08-10 11:39:47 +0200105 DEBUGPC(DNM, "test_res=%s\n", ipacc_testres_name(foh->data[5]));
Harald Welte0bf8e302009-08-08 00:02:36 +0200106
107 /* data[6]: ip.access nested IE. 3 == freq_err_list */
108 switch (foh->data[6]) {
Harald Weltec73d9832009-11-13 14:43:15 +0100109 case NM_IPAC_EIE_FREQ_ERR_LIST:
Harald Welte0bf8e302009-08-08 00:02:36 +0200110 /* data[7..8]: length of ferr_list */
111 ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]);
112
113 /* data[9...]: frequency error list elements */
114 for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) {
115 ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i);
116 DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n",
117 ife->arfcn, ntohs(ife->freq_err));
118 }
119 break;
Harald Weltec73d9832009-11-13 14:43:15 +0100120 case NM_IPAC_EIE_CHAN_USE_LIST:
Harald Welte12a721e2009-08-10 11:39:47 +0200121 /* data[7..8]: length of ferr_list */
122 ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]);
123
124 /* data[9...]: channel usage list elements */
125 for (i = 0; i < ferr_list_len; i+= 2) {
126 u_int16_t *cu_ptr = (u_int16_t *)(foh->data + 9 + i);
127 u_int16_t cu = ntohs(*cu_ptr);
128 DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n",
129 cu & 0x3ff, cu >> 10);
130 }
131 break;
Harald Weltec73d9832009-11-13 14:43:15 +0100132 case NM_IPAC_EIE_BCCH_INFO_TYPE:
133 break;
134 case NM_IPAC_EIE_BCCH_INFO:
135 rc = ipac_parse_bcch_info(&binfo, foh->data+6);
136 if (rc < 0) {
137 DEBUGP(DNM, "BCCH Info parsing failed\n");
138 break;
139 }
140 DEBUGP(DNM, "==> ARFCN %u, RxLev %2u, RxQual %2u: %3d-%d, LAC %d CI %d\n",
141 binfo.arfcn, binfo.rx_lev, binfo.rx_qual,
142 binfo.cgi.mcc, binfo.cgi.mnc,
143 binfo.cgi.lac, binfo.cgi.ci);
144 break;
Harald Welte0bf8e302009-08-08 00:02:36 +0200145 default:
146 break;
147 }
148
149 return 0;
150}
151
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +0200152static int nm_sig_cb(unsigned int subsys, unsigned int signal,
153 void *handler_data, void *signal_data)
154{
Harald Welte6a21c732009-11-17 06:09:56 +0100155 u_int8_t *msg_type;
156
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +0200157 switch (signal) {
158 case S_NM_IPACC_NACK:
Harald Welte6a21c732009-11-17 06:09:56 +0100159 msg_type = signal_data;
160 return ipacc_msg_nack(*msg_type);
Harald Welte0bf8e302009-08-08 00:02:36 +0200161 case S_NM_TEST_REP:
162 return test_rep(signal_data);
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +0200163 default:
164 break;
165 }
166
167 return 0;
168}
169
Holger Hans Peter Freyther4b513382009-12-28 03:11:33 +0100170/* callback function passed to the ABIS OML code */
171static int percent;
172static int percent_old;
173static int swload_cbfn(unsigned int hook, unsigned int event, struct msgb *msg,
174 void *data, void *param)
175{
176 struct gsm_bts *bts;
177
178 if (hook != GSM_HOOK_NM_SWLOAD)
179 return 0;
180
181 bts = (struct gsm_bts *) data;
182
183 switch (event) {
184 case NM_MT_LOAD_INIT_ACK:
185 fprintf(stdout, "Software Load Initiate ACK\n");
186 break;
187 case NM_MT_LOAD_INIT_NACK:
188 fprintf(stderr, "ERROR: Software Load Initiate NACK\n");
189 exit(5);
190 break;
191 case NM_MT_LOAD_END_ACK:
192 fprintf(stderr, "LOAD END ACK...");
193#if 0
194 if (data) {
195 /* we did a safety load and must activate it */
196 abis_nm_software_activate(g_bts, fname_safety,
197 swload_cbfn, bts);
198 sleep(5);
199 }
200#endif
201 break;
202 case NM_MT_LOAD_END_NACK:
203 fprintf(stderr, "ERROR: Software Load End NACK\n");
204 exit(3);
205 break;
206 case NM_MT_ACTIVATE_SW_NACK:
207 fprintf(stderr, "ERROR: Activate Software NACK\n");
208 exit(4);
209 break;
210 case NM_MT_ACTIVATE_SW_ACK:
211 break;
212 case NM_MT_LOAD_SEG_ACK:
213 percent = abis_nm_software_load_status(bts);
214 if (percent > percent_old)
215 printf("Software Download Progress: %d%%\n", percent);
216 percent_old = percent;
217 break;
218 }
219 return 0;
220}
221
Harald Welte59b04682009-06-10 05:40:52 +0800222static void bootstrap_om(struct gsm_bts *bts)
223{
224 int len;
225 static u_int8_t buf[1024];
Harald Welte21460f02009-07-03 11:26:45 +0200226 u_int8_t *cur = buf;
Harald Welte59b04682009-06-10 05:40:52 +0800227
228 printf("OML link established\n");
229
230 if (unit_id) {
231 len = strlen(unit_id);
232 if (len > sizeof(buf)-10)
233 return;
234 buf[0] = NM_ATT_IPACC_UNIT_ID;
235 buf[1] = (len+1) >> 8;
236 buf[2] = (len+1) & 0xff;
237 memcpy(buf+3, unit_id, len);
238 buf[3+len] = 0;
239 printf("setting Unit ID to '%s'\n", unit_id);
240 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len+1);
241 }
242 if (prim_oml_ip) {
243 struct in_addr ia;
Harald Welte59b04682009-06-10 05:40:52 +0800244
245 if (!inet_aton(prim_oml_ip, &ia)) {
246 fprintf(stderr, "invalid IP address: %s\n",
247 prim_oml_ip);
248 return;
249 }
250
251 /* 0x88 + IP + port */
252 len = 1 + sizeof(ia) + 2;
253
Harald Welte4206d982009-07-12 09:33:54 +0200254 *cur++ = NM_ATT_IPACC_PRIM_OML_CFG_LIST;
Harald Welte59b04682009-06-10 05:40:52 +0800255 *cur++ = (len) >> 8;
256 *cur++ = (len) & 0xff;
257 *cur++ = 0x88;
258 memcpy(cur, &ia, sizeof(ia));
259 cur += sizeof(ia);
260 *cur++ = 0;
261 *cur++ = 0;
262 printf("setting primary OML link IP to '%s'\n", inet_ntoa(ia));
263 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
264 }
Harald Welte21460f02009-07-03 11:26:45 +0200265 if (nv_mask) {
266 len = 4;
267
268 *cur++ = NM_ATT_IPACC_NV_FLAGS;
269 *cur++ = (len) >> 8;
270 *cur++ = (len) & 0xff;
271 *cur++ = nv_flags & 0xff;
272 *cur++ = nv_mask & 0xff;
273 *cur++ = nv_flags >> 8;
274 *cur++ = nv_mask >> 8;
275 printf("setting NV Flags/Mask to 0x%04x/0x%04x\n",
276 nv_flags, nv_mask);
277 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
278 }
Harald Welte59b04682009-06-10 05:40:52 +0800279
280 if (restart) {
281 printf("restarting BTS\n");
282 abis_nm_ipaccess_restart(bts);
283 }
Holger Hans Peter Freyther4b513382009-12-28 03:11:33 +0100284
285 if (software) {
286 int rc;
287 printf("Attempting software upload with '%s'\n", software);
288 rc = abis_nm_software_load(bts, software, 19, 0, swload_cbfn, bts);
289 if (rc < 0) {
290 fprintf(stderr, "Failed to start software load\n");
291 exit(-3);
292 }
293 }
Harald Welte59b04682009-06-10 05:40:52 +0800294}
295
296void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
297{
298 switch (event) {
299 case EVT_E1_TEI_UP:
300 switch (type) {
301 case E1INP_SIGN_OML:
302 bootstrap_om(trx->bts);
303 break;
304 case E1INP_SIGN_RSL:
305 /* FIXME */
306 break;
307 default:
308 break;
309 }
310 break;
311 case EVT_E1_TEI_DN:
312 fprintf(stderr, "Lost some E1 TEI link\n");
313 /* FIXME: deal with TEI or L1 link loss */
314 break;
315 default:
316 break;
317 }
318}
319
320int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
321 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
322{
Harald Welte0bf8e302009-08-08 00:02:36 +0200323 if (evt == EVT_STATECHG_OPER &&
324 obj_class == NM_OC_RADIO_CARRIER &&
325 new_state->availability == 3 &&
Harald Welte12a721e2009-08-10 11:39:47 +0200326 net_listen_testnr) {
Harald Welte0bf8e302009-08-08 00:02:36 +0200327 struct gsm_bts_trx *trx = obj;
328 u_int8_t phys_config[] = { 0x02, 0x0a, 0x00, 0x01, 0x02 };
329 abis_nm_perform_test(trx->bts, 2, 0, 0, 0xff,
Harald Welte12a721e2009-08-10 11:39:47 +0200330 net_listen_testnr, 1,
Harald Welte0bf8e302009-08-08 00:02:36 +0200331 phys_config, sizeof(phys_config));
332 }
Harald Welte59b04682009-06-10 05:40:52 +0800333 return 0;
334}
335
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200336static void print_usage(void)
337{
338 printf("Usage: ipaccess-config\n");
339}
340
341static void print_help(void)
342{
343 printf(" -u --unit-id UNIT_ID\n");
344 printf(" -o --oml-ip ip\n");
345 printf(" -r --restart\n");
Harald Welte0bf8e302009-08-08 00:02:36 +0200346 printf(" -n flags/mask\tSet NVRAM attributes.\n");
Holger Hans Peter Freythera4434c22009-11-16 19:53:08 +0100347 printf(" -l --listen testnr \tPerform specified test number\n");
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200348 printf(" -h --help this text\n");
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100349 printf(" -s --stream-id ID\n");
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200350}
351
Harald Welte59b04682009-06-10 05:40:52 +0800352int main(int argc, char **argv)
353{
354 struct gsm_bts *bts;
355 struct sockaddr_in sin;
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100356 int rc, option_index = 0, stream_id = 0xff;
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100357 struct debug_target *stderr_target;
358
359 debug_init();
360 stderr_target = debug_target_create_stderr();
361 debug_add_target(stderr_target);
362 debug_set_all_filter(stderr_target, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800363
364 printf("ipaccess-config (C) 2009 by Harald Welte\n");
365 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
366
367 while (1) {
368 int c;
Harald Welte21460f02009-07-03 11:26:45 +0200369 unsigned long ul;
370 char *slash;
Harald Welte59b04682009-06-10 05:40:52 +0800371 static struct option long_options[] = {
372 { "unit-id", 1, 0, 'u' },
373 { "oml-ip", 1, 0, 'o' },
374 { "restart", 0, 0, 'r' },
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200375 { "help", 0, 0, 'h' },
Harald Welte12a721e2009-08-10 11:39:47 +0200376 { "listen", 1, 0, 'l' },
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100377 { "stream-id", 1, 0, 's' },
Holger Hans Peter Freyther4b513382009-12-28 03:11:33 +0100378 { "software", 1, 0, 'd' },
Harald Welte59b04682009-06-10 05:40:52 +0800379 };
380
Holger Hans Peter Freyther4b513382009-12-28 03:11:33 +0100381 c = getopt_long(argc, argv, "u:o:rn:l:hs:d:", long_options,
Harald Welte59b04682009-06-10 05:40:52 +0800382 &option_index);
383
384 if (c == -1)
385 break;
386
387 switch (c) {
388 case 'u':
389 unit_id = optarg;
390 break;
391 case 'o':
392 prim_oml_ip = optarg;
393 break;
394 case 'r':
395 restart = 1;
396 break;
Harald Welte21460f02009-07-03 11:26:45 +0200397 case 'n':
398 slash = strchr(optarg, '/');
399 if (!slash)
400 exit(2);
401 ul = strtoul(optarg, NULL, 16);
402 nv_flags = ul & 0xffff;
403 ul = strtoul(slash+1, NULL, 16);
404 nv_mask = ul & 0xffff;
405 break;
Harald Welte0bf8e302009-08-08 00:02:36 +0200406 case 'l':
Harald Welte12a721e2009-08-10 11:39:47 +0200407 net_listen_testnr = atoi(optarg);
Harald Welte0bf8e302009-08-08 00:02:36 +0200408 break;
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100409 case 's':
410 stream_id = atoi(optarg);
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100411 break;
Holger Hans Peter Freyther4b513382009-12-28 03:11:33 +0100412 case 'd':
413 software = strdup(optarg);
414 break;
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200415 case 'h':
416 print_usage();
417 print_help();
418 exit(0);
Harald Welte59b04682009-06-10 05:40:52 +0800419 }
420 };
421
422 if (optind >= argc) {
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200423 fprintf(stderr, "you have to specify the IP address of the BTS. Use --help for more information\n");
Harald Welte59b04682009-06-10 05:40:52 +0800424 exit(2);
425 }
426
Harald Weltee712a5f2009-06-21 16:17:15 +0200427 gsmnet = gsm_network_init(1, 1, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800428 if (!gsmnet)
429 exit(1);
430
Mike Haben66e0ba02009-10-02 12:19:34 +0100431 bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS, HARDCODED_TSC,
Harald Weltee712a5f2009-06-21 16:17:15 +0200432 HARDCODED_BSIC);
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100433 bts->oml_tei = stream_id;
Harald Welte59b04682009-06-10 05:40:52 +0800434
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +0200435 register_signal_handler(SS_NM, nm_sig_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800436 printf("Trying to connect to ip.access BTS ...\n");
437
438 memset(&sin, 0, sizeof(sin));
439 sin.sin_family = AF_INET;
440 inet_aton(argv[optind], &sin.sin_addr);
441 rc = ia_config_connect(bts, &sin);
442 if (rc < 0) {
443 perror("Error connecting to the BTS");
444 exit(1);
445 }
446
447 while (1) {
448 rc = bsc_select_main(0);
449 if (rc < 0)
450 exit(3);
451 }
452
453 exit(0);
454}
455