blob: bb1539173504b2c40cde751491d6912856f5a0ce [file] [log] [blame]
Harald Welte61e42ad2009-01-18 19:10:46 +00001/* Siemens BS-11 microBTS configuration tool */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This software is based on ideas (but not code) of BS11Config
7 * (C) 2009 by Dieter Spaar <spaar@mirider.augusta.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <unistd.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <errno.h>
29#include <string.h>
30#include <getopt.h>
31#include <fcntl.h>
Harald Welte14f09342009-01-29 19:28:38 +000032#include <signal.h>
Harald Welte61e42ad2009-01-18 19:10:46 +000033
34#include <sys/types.h>
35#include <sys/stat.h>
36
37#include <openbsc/gsm_data.h>
38#include <openbsc/abis_nm.h>
39#include <openbsc/msgb.h>
40#include <openbsc/tlv.h>
41#include <openbsc/debug.h>
Harald Welte14f09342009-01-29 19:28:38 +000042#include <openbsc/select.h>
Harald Welte61e42ad2009-01-18 19:10:46 +000043
Harald Welte3b8ba212009-01-29 12:27:58 +000044/* state of our bs11_config application */
45enum bs11cfg_state {
46 STATE_NONE,
47 STATE_LOGON_WAIT,
48 STATE_LOGON_ACK,
49 STATE_SWLOAD,
Harald Weltefb81ae92009-02-19 02:59:17 +000050 STATE_QUERY,
Harald Welte3b8ba212009-01-29 12:27:58 +000051};
52static enum bs11cfg_state bs11cfg_state = STATE_NONE;
Harald Weltefb81ae92009-02-19 02:59:17 +000053static char *command;
54struct timer_list status_timer;
Harald Welte3b8ba212009-01-29 12:27:58 +000055
Harald Welte61e42ad2009-01-18 19:10:46 +000056static const u_int8_t obj_li_attr[] = {
Harald Welte5e4d1b32009-02-01 13:36:56 +000057 NM_ATT_BS11_BIT_ERR_THESH, 0x09, 0x00,
58 NM_ATT_BS11_L1_PROT_TYPE, 0x00,
59 NM_ATT_BS11_LINE_CFG, 0x00,
Harald Welte61e42ad2009-01-18 19:10:46 +000060};
61static const u_int8_t obj_bbsig0_attr[] = {
Harald Welte5e4d1b32009-02-01 13:36:56 +000062 NM_ATT_BS11_RSSI_OFFS, 0x02, 0x00, 0x00,
63 NM_ATT_BS11_DIVERSITY, 0x01, 0x00,
Harald Welte61e42ad2009-01-18 19:10:46 +000064};
65static const u_int8_t obj_pa0_attr[] = {
Harald Welte5e4d1b32009-02-01 13:36:56 +000066 NM_ATT_BS11_TXPWR, 0x01, BS11_TRX_POWER_GSM_30mW,
Harald Welte61e42ad2009-01-18 19:10:46 +000067};
68static const char *trx1_password = "1111111111";
69#define TEI_OML 25
70
Harald Welte3b8ba212009-01-29 12:27:58 +000071static const u_int8_t too_fast[] = { 0x12, 0x80, 0x00, 0x00, 0x02, 0x02 };
72
Harald Welte14f09342009-01-29 19:28:38 +000073
Harald Weltec12d52b2009-02-01 21:39:06 +000074int handle_serial_msg(struct msgb *rx_msg);
Harald Welte14f09342009-01-29 19:28:38 +000075
Harald Welte1cbfaf52009-02-05 19:30:22 +000076/* create all objects for an initial configuration */
77static int create_objects(struct gsm_bts *bts)
Harald Welte268bb402009-02-01 19:11:56 +000078{
79 u_int8_t bbsig1_attr[sizeof(obj_bbsig0_attr)+12];
80 u_int8_t *cur = bbsig1_attr;
81
Harald Welte1cbfaf52009-02-05 19:30:22 +000082 fprintf(stdout, "Crating Objects for minimal config\n");
Harald Welte61e42ad2009-01-18 19:10:46 +000083 abis_nm_bs11_create_object(bts, BS11_OBJ_LI, 0, sizeof(obj_li_attr),
84 obj_li_attr);
85 abis_nm_bs11_create_object(bts, BS11_OBJ_GPSU, 0, 0, NULL);
86 abis_nm_bs11_create_object(bts, BS11_OBJ_ALCO, 0, 0, NULL);
Harald Welte623d5312009-01-29 21:14:05 +000087 abis_nm_bs11_create_object(bts, BS11_OBJ_CCLK, 0, 0, NULL);
Harald Welte61e42ad2009-01-18 19:10:46 +000088 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 0,
89 sizeof(obj_bbsig0_attr), obj_bbsig0_attr);
90 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 0,
91 sizeof(obj_pa0_attr), obj_pa0_attr);
Harald Welte61e42ad2009-01-18 19:10:46 +000092 abis_nm_bs11_create_envaBTSE(bts, 0);
93 abis_nm_bs11_create_envaBTSE(bts, 1);
94 abis_nm_bs11_create_envaBTSE(bts, 2);
95 abis_nm_bs11_create_envaBTSE(bts, 3);
96
97 abis_nm_bs11_conn_oml(bts, 0, 1, 0xff);
98 abis_nm_bs11_set_oml_tei(bts, TEI_OML);
99
Harald Welte5e4d1b32009-02-01 13:36:56 +0000100 abis_nm_bs11_set_trx_power(&bts->trx[0], BS11_TRX_POWER_GSM_30mW);
Harald Welte61e42ad2009-01-18 19:10:46 +0000101
Harald Welte1cbfaf52009-02-05 19:30:22 +0000102 sleep(1);
103
Harald Weltefb81ae92009-02-19 02:59:17 +0000104 abis_nm_bs11_set_trx1_pw(bts, trx1_password);
105
106 sleep(1);
107
108 return 0;
109}
110
111static int create_trx1(struct gsm_bts *bts)
112{
113 u_int8_t bbsig1_attr[sizeof(obj_bbsig0_attr)+12];
114 u_int8_t *cur = bbsig1_attr;
115
Harald Welte1cbfaf52009-02-05 19:30:22 +0000116 fprintf(stdout, "Crating Objects for TRX1\n");
117
118 abis_nm_bs11_set_trx1_pw(bts, trx1_password);
119
120 sleep(1);
Harald Weltefb81ae92009-02-19 02:59:17 +0000121
Harald Welte1cbfaf52009-02-05 19:30:22 +0000122 cur = tlv_put(cur, NM_ATT_BS11_PASSWORD, 10,
123 (u_int8_t *)trx1_password);
124 memcpy(cur, obj_bbsig0_attr, sizeof(obj_bbsig0_attr));
125 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 1,
126 sizeof(bbsig1_attr), bbsig1_attr);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000127 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 1,
128 sizeof(obj_pa0_attr), obj_pa0_attr);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000129 abis_nm_bs11_set_trx_power(&bts->trx[1], BS11_TRX_POWER_GSM_30mW);
Harald Weltefb81ae92009-02-19 02:59:17 +0000130
Harald Welte61e42ad2009-01-18 19:10:46 +0000131 return 0;
132}
133
134static char *serial_port = "/dev/ttyUSB0";
135static char *fname_safety = "BTSBMC76.SWI";
136static char *fname_software = "HS011106.SWL";
Harald Welte623d5312009-01-29 21:14:05 +0000137static int delay_ms = 0;
Harald Welte623d5312009-01-29 21:14:05 +0000138static int win_size = 8;
Harald Welte3ffd1372009-02-01 22:15:49 +0000139static int param_disconnect = 0;
Harald Welte1cbfaf52009-02-05 19:30:22 +0000140static int param_restart = 0;
Harald Welte3ffd1372009-02-01 22:15:49 +0000141static int param_forced = 0;
Harald Welte61e42ad2009-01-18 19:10:46 +0000142static struct gsm_bts *g_bts;
143
Harald Welte61e42ad2009-01-18 19:10:46 +0000144static int file_is_readable(const char *fname)
145{
146 int rc;
147 struct stat st;
148
149 rc = stat(fname, &st);
150 if (rc < 0)
151 return 0;
152
153 if (S_ISREG(st.st_mode) && (st.st_mode & S_IRUSR))
154 return 1;
155
156 return 0;
157}
158
Harald Welte3ffd1372009-02-01 22:15:49 +0000159static int percent;
160static int percent_old;
161
Harald Welte5e4d1b32009-02-01 13:36:56 +0000162/* callback function passed to the ABIS OML code */
163static int swload_cbfn(unsigned int hook, unsigned int event, struct msgb *msg,
164 void *data, void *param)
165{
166 if (hook != GSM_HOOK_NM_SWLOAD)
167 return 0;
Harald Welte61e42ad2009-01-18 19:10:46 +0000168
Harald Welte5e4d1b32009-02-01 13:36:56 +0000169 switch (event) {
170 case NM_MT_LOAD_INIT_ACK:
171 fprintf(stdout, "Software Load Initiate ACK\n");
172 break;
173 case NM_MT_LOAD_INIT_NACK:
174 fprintf(stderr, "ERROR: Software Load Initiate NACK\n");
175 exit(5);
176 break;
177 case NM_MT_LOAD_END_ACK:
Harald Welte1cbfaf52009-02-05 19:30:22 +0000178 if (data) {
Harald Welte03133942009-02-18 19:51:53 +0000179 /* we did a safety load and must activate it */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000180 abis_nm_software_activate(g_bts, fname_safety,
181 swload_cbfn, g_bts);
Harald Welte03133942009-02-18 19:51:53 +0000182 sleep(5);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000183 }
Harald Welte5e4d1b32009-02-01 13:36:56 +0000184 break;
185 case NM_MT_LOAD_END_NACK:
186 fprintf(stderr, "ERROR: Software Load End NACK\n");
187 exit(3);
188 break;
189 case NM_MT_ACTIVATE_SW_NACK:
190 fprintf(stderr, "ERROR: Activate Software NACK\n");
191 exit(4);
192 break;
193 case NM_MT_ACTIVATE_SW_ACK:
194 bs11cfg_state = STATE_NONE;
195
196 break;
Harald Welte3ffd1372009-02-01 22:15:49 +0000197 case NM_MT_LOAD_SEG_ACK:
198 percent = abis_nm_software_load_status(g_bts);
199 if (percent > percent_old)
200 printf("Software Download Progress: %d%%\n", percent);
201 percent_old = percent;
202 break;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000203 }
204 return 0;
205}
206
Harald Welte268bb402009-02-01 19:11:56 +0000207static const char *bs11_link_state[] = {
208 [0x00] = "Down",
209 [0x01] = "Up",
210 [0x02] = "Restoring",
211};
212
213static const char *linkstate_name(u_int8_t linkstate)
Harald Welte61e42ad2009-01-18 19:10:46 +0000214{
Harald Welte268bb402009-02-01 19:11:56 +0000215 if (linkstate > ARRAY_SIZE(bs11_link_state))
216 return "Unknown";
Harald Welte3b8ba212009-01-29 12:27:58 +0000217
Harald Welte268bb402009-02-01 19:11:56 +0000218 return bs11_link_state[linkstate];
219}
Harald Welte3b8ba212009-01-29 12:27:58 +0000220
Harald Welte268bb402009-02-01 19:11:56 +0000221static const char *mbccu_load[] = {
222 [0] = "No Load",
223 [1] = "Load BTSCAC",
224 [2] = "Load BTSDRX",
225 [3] = "Load BTSBBX",
226 [4] = "Load BTSARC",
227 [5] = "Load",
228};
229
230static const char *mbccu_load_name(u_int8_t linkstate)
231{
232 if (linkstate > ARRAY_SIZE(mbccu_load))
233 return "Unknown";
234
235 return mbccu_load[linkstate];
236}
237
Harald Welte03133942009-02-18 19:51:53 +0000238static const char *bts_phase_name(u_int8_t phase)
Harald Welte268bb402009-02-01 19:11:56 +0000239{
Harald Welte268bb402009-02-01 19:11:56 +0000240 switch (phase) {
Harald Welte61e42ad2009-01-18 19:10:46 +0000241 case BS11_STATE_WARM_UP:
Harald Weltec12d52b2009-02-01 21:39:06 +0000242 case BS11_STATE_WARM_UP_2:
Harald Welte03133942009-02-18 19:51:53 +0000243 return "Warm Up";
Harald Welte61e42ad2009-01-18 19:10:46 +0000244 break;
245 case BS11_STATE_LOAD_SMU_SAFETY:
Harald Welte03133942009-02-18 19:51:53 +0000246 return "Load SMU Safety";
Harald Welte268bb402009-02-01 19:11:56 +0000247 break;
248 case BS11_STATE_LOAD_SMU_INTENDED:
Harald Welte03133942009-02-18 19:51:53 +0000249 return "Load SMU Intended";
Harald Welte268bb402009-02-01 19:11:56 +0000250 break;
251 case BS11_STATE_LOAD_MBCCU:
Harald Welte03133942009-02-18 19:51:53 +0000252 return "Load MBCCU";
Harald Welte61e42ad2009-01-18 19:10:46 +0000253 break;
254 case BS11_STATE_SOFTWARE_RQD:
Harald Welte03133942009-02-18 19:51:53 +0000255 return "Software required";
Harald Welte268bb402009-02-01 19:11:56 +0000256 break;
257 case BS11_STATE_WAIT_MIN_CFG:
258 case BS11_STATE_WAIT_MIN_CFG_2:
Harald Welte03133942009-02-18 19:51:53 +0000259 return "Wait minimal config";
Harald Welte268bb402009-02-01 19:11:56 +0000260 break;
261 case BS11_STATE_MAINTENANCE:
Harald Welte03133942009-02-18 19:51:53 +0000262 return "Maintenance";
Harald Welte268bb402009-02-01 19:11:56 +0000263 break;
264 case BS11_STATE_NORMAL:
Harald Welte03133942009-02-18 19:51:53 +0000265 return "Normal";
266 break;
267 case BS11_STATE_ABIS_LOAD:
268 return "Abis load";
Harald Welte268bb402009-02-01 19:11:56 +0000269 break;
270 default:
Harald Welte03133942009-02-18 19:51:53 +0000271 return "Unknown";
Harald Welte268bb402009-02-01 19:11:56 +0000272 break;
273 }
274}
275
Harald Weltefb81ae92009-02-19 02:59:17 +0000276static const char *trx_power_name(u_int8_t pwr)
277{
278 switch (pwr) {
279 case BS11_TRX_POWER_GSM_2W:
280 return "2W (GSM)";
281 case BS11_TRX_POWER_GSM_250mW:
282 return "250mW (GSM)";
283 case BS11_TRX_POWER_GSM_80mW:
284 return "80mW (GSM)";
285 case BS11_TRX_POWER_GSM_30mW:
286 return "30mW (GSM)";
287 case BS11_TRX_POWER_DCS_3W:
288 return "3W (DCS)";
289 case BS11_TRX_POWER_DCS_1W6:
290 return "1.6W (DCS)";
291 case BS11_TRX_POWER_DCS_500mW:
292 return "500mW (DCS)";
293 case BS11_TRX_POWER_DCS_160mW:
294 return "160mW (DCS)";
295 default:
296 return "unknown value";
297 }
298}
299
300static const char *obj_name(struct abis_om_fom_hdr *foh)
301{
302 static char retbuf[256];
303
304 retbuf[0] = 0;
305
306 switch (foh->obj_class) {
307 case NM_OC_BS11:
308 strcat(retbuf, "BS11 ");
309 switch (foh->obj_inst.bts_nr) {
310 case BS11_OBJ_PA:
311 sprintf(retbuf+strlen(retbuf), "Power Amplifier %d ",
312 foh->obj_inst.ts_nr);
313 break;
314 }
315 break;
316 case NM_OC_SITE_MANAGER:
317 strcat(retbuf, "SITE MANAGER ");
318 break;
319 }
320 return retbuf;
321}
322
Harald Welte03133942009-02-18 19:51:53 +0000323static void print_state(struct tlv_parsed *tp)
324{
325 if (TLVP_PRESENT(tp, NM_ATT_BS11_BTS_STATE)) {
326 u_int8_t phase, mbccu;
327 if (TLVP_LEN(tp, NM_ATT_BS11_BTS_STATE) >= 1) {
328 phase = *TLVP_VAL(tp, NM_ATT_BS11_BTS_STATE);
329 printf("PHASE: %u %-20s ", phase & 0xf,
330 bts_phase_name(phase));
331 }
332 if (TLVP_LEN(tp, NM_ATT_BS11_BTS_STATE) >= 2) {
333 mbccu = *(TLVP_VAL(tp, NM_ATT_BS11_BTS_STATE)+1);
334 printf("MBCCU0: %-11s MBCCU1: %-11s ",
335 mbccu_load_name(mbccu & 0xf), mbccu_load_name(mbccu >> 4));
336 }
337 }
338 if (TLVP_PRESENT(tp, NM_ATT_BS11_E1_STATE) &&
339 TLVP_LEN(tp, NM_ATT_BS11_E1_STATE) >= 1) {
340 u_int8_t e1_state = *TLVP_VAL(tp, NM_ATT_BS11_E1_STATE);
341 printf("Abis-link: %-9s ", linkstate_name(e1_state & 0xf));
342 }
343 printf("\n");
344}
345
Harald Weltefb81ae92009-02-19 02:59:17 +0000346static char *print_bcd(u_int8_t *bcd, int len)
347{
348 return "FIXME";
349}
350
351static int print_attr(struct tlv_parsed *tp)
352{
353 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_PCB_SERIAL)) {
354 printf("\tBS-11 ESN PCB Serial Number: %s\n",
355 TLVP_VAL(tp, NM_ATT_BS11_ESN_PCB_SERIAL));
356 }
357 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_HW_CODE_NO)) {
358 printf("\tBS-11 ESN Hardware Code Number: %s\n",
359 TLVP_VAL(tp, NM_ATT_BS11_ESN_HW_CODE_NO)+6);
360 }
361 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_FW_CODE_NO)) {
362 printf("\tBS-11 ESN Firmware Code Number: %s\n",
363 TLVP_VAL(tp, NM_ATT_BS11_ESN_FW_CODE_NO)+6);
364 }
365#if 0
366 if (TLVP_PRESENT(tp, NM_ATT_BS11_BOOT_SW_VERS)) {
367 printf("BS-11 Boot Software Version: %s\n",
368 TLVP_VAL(tp, NM_ATT_BS11_BOOT_SW_VERS)+6);
369 }
370#endif
371 if (TLVP_PRESENT(tp, NM_ATT_ABIS_CHANNEL) &&
372 TLVP_LEN(tp, NM_ATT_ABIS_CHANNEL) >= 3) {
373 struct abis_nm_channel *chan = TLVP_VAL(tp, NM_ATT_ABIS_CHANNEL)-1;
374 printf("\tE1 Channel: Port=%u Timeslot=%u ",
375 chan->bts_port, chan->timeslot);
376 if (chan->subslot == 0xff)
377 printf("(Full Slot)\n");
378 else
379 printf("Subslot=%u\n", chan->subslot);
380 }
381 if (TLVP_PRESENT(tp, NM_ATT_TEI))
382 printf("\tTEI: %d\n", *TLVP_VAL(tp, NM_ATT_TEI));
383 if (TLVP_PRESENT(tp, NM_ATT_BS11_TXPWR) &&
384 TLVP_LEN(tp, NM_ATT_BS11_TXPWR) >= 1) {
385 printf("\tTRX Power: %s\n",
386 trx_power_name(*TLVP_VAL(tp, NM_ATT_BS11_TXPWR)));
387 }
388
389 return 0;
390}
391
Harald Welte268bb402009-02-01 19:11:56 +0000392/* handle a response from the BTS to a GET STATE command */
393static int handle_state_resp(enum abis_bs11_phase state)
394{
395 int rc = 0;
396
Harald Welte268bb402009-02-01 19:11:56 +0000397 switch (state) {
398 case BS11_STATE_WARM_UP:
Harald Welte268bb402009-02-01 19:11:56 +0000399 case BS11_STATE_LOAD_SMU_SAFETY:
Harald Welte268bb402009-02-01 19:11:56 +0000400 case BS11_STATE_LOAD_SMU_INTENDED:
Harald Welte268bb402009-02-01 19:11:56 +0000401 case BS11_STATE_LOAD_MBCCU:
402 break;
403 case BS11_STATE_SOFTWARE_RQD:
Harald Welte3b8ba212009-01-29 12:27:58 +0000404 bs11cfg_state = STATE_SWLOAD;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000405 /* send safety load. Use g_bts as private 'param'
406 * argument, so our swload_cbfn can distinguish
407 * a safety load from a regular software */
Harald Welte61e42ad2009-01-18 19:10:46 +0000408 if (file_is_readable(fname_safety))
Harald Welte623d5312009-01-29 21:14:05 +0000409 rc = abis_nm_software_load(g_bts, fname_safety,
Harald Welte3ffd1372009-02-01 22:15:49 +0000410 win_size, param_forced,
411 swload_cbfn, g_bts);
Harald Welte61e42ad2009-01-18 19:10:46 +0000412 else
Harald Welte071f34d2009-01-29 09:24:38 +0000413 fprintf(stderr, "No valid Safety Load file \"%s\"\n",
414 fname_safety);
Harald Welte61e42ad2009-01-18 19:10:46 +0000415 break;
416 case BS11_STATE_WAIT_MIN_CFG:
417 case BS11_STATE_WAIT_MIN_CFG_2:
Harald Welte3b8ba212009-01-29 12:27:58 +0000418 bs11cfg_state = STATE_SWLOAD;
Harald Welte3ffd1372009-02-01 22:15:49 +0000419 rc = create_objects(g_bts);
Harald Welte61e42ad2009-01-18 19:10:46 +0000420 break;
421 case BS11_STATE_MAINTENANCE:
Harald Weltefb81ae92009-02-19 02:59:17 +0000422 if (command) {
423 if (!strcmp(command, "disconnect"))
424 exit(0);
425 else if (!strcmp(command, "reconnect"))
426 rc = abis_nm_bs11_bsc_disconnect(g_bts, 1);
427 else if (!strcmp(command, "software")
428 && bs11cfg_state != STATE_SWLOAD) {
429 bs11cfg_state = STATE_SWLOAD;
430 /* send software (FIXME: over A-bis?) */
431 if (file_is_readable(fname_software))
432 rc = abis_nm_bs11_load_swl(g_bts, fname_software,
433 win_size, param_forced,
434 swload_cbfn);
435 else
436 fprintf(stderr, "No valid Software file \"%s\"\n",
437 fname_software);
438 } else if (!strcmp(command, "delete-trx1")) {
439 abis_nm_bs11_delete_object(g_bts, BS11_OBJ_BBSIG, 1);
440 abis_nm_bs11_delete_object(g_bts, BS11_OBJ_PA, 1);
441 command = NULL;
442 } else if (!strcmp(command, "create-trx1")) {
443 create_trx1(g_bts);
444 command = NULL;
445 } else if (!strcmp(command, "restart")) {
446 abis_nm_bs11_restart(g_bts);
447 command = NULL;
448 }
Harald Welte3ffd1372009-02-01 22:15:49 +0000449 }
Harald Welte61e42ad2009-01-18 19:10:46 +0000450 break;
451 case BS11_STATE_NORMAL:
Harald Weltefb81ae92009-02-19 02:59:17 +0000452 if (command) {
453 if (!strcmp(command, "reconnect"))
454 exit(0);
455 else if (!strcmp(command, "disconnect"))
456 abis_nm_bs11_bsc_disconnect(g_bts, 0);
457 else if (!strcmp(command, "query")) {
458 bs11cfg_state = STATE_QUERY;
459 abis_nm_bs11_get_serno(g_bts);
460 abis_nm_bs11_get_oml_tei_ts(g_bts);
461 abis_nm_bs11_get_trx_power(&g_bts->trx[0]);
462 abis_nm_bs11_get_trx_power(&g_bts->trx[1]);
463 command = NULL;
464 }
465 } else if (param_disconnect) {
Harald Welte1cbfaf52009-02-05 19:30:22 +0000466 param_disconnect = 0;
467 abis_nm_bs11_bsc_disconnect(g_bts, 0);
468 if (param_restart) {
469 param_restart = 0;
470 abis_nm_bs11_restart(g_bts);
471 }
472 }
Harald Weltefb81ae92009-02-19 02:59:17 +0000473 break;
Harald Welte61e42ad2009-01-18 19:10:46 +0000474 default:
Harald Welte61e42ad2009-01-18 19:10:46 +0000475 break;
476 }
Harald Welte3b8ba212009-01-29 12:27:58 +0000477 return rc;
Harald Welte61e42ad2009-01-18 19:10:46 +0000478}
479
Harald Welte5e4d1b32009-02-01 13:36:56 +0000480/* handle a fully-received message/packet from the RS232 port */
Harald Weltec12d52b2009-02-01 21:39:06 +0000481int handle_serial_msg(struct msgb *rx_msg)
Harald Welte14f09342009-01-29 19:28:38 +0000482{
483 struct abis_om_hdr *oh;
484 struct abis_om_fom_hdr *foh;
Harald Welte03133942009-02-18 19:51:53 +0000485 struct tlv_parsed tp;
Harald Welte14f09342009-01-29 19:28:38 +0000486 int rc = -1;
487
Harald Weltec12d52b2009-02-01 21:39:06 +0000488#if 0
Harald Welte14f09342009-01-29 19:28:38 +0000489 if (rx_msg->len < LAPD_HDR_LEN
490 + sizeof(struct abis_om_fom_hdr)
491 + sizeof(struct abis_om_hdr)) {
492 if (!memcmp(rx_msg->data + 2, too_fast,
493 sizeof(too_fast))) {
494 fprintf(stderr, "BS11 tells us we're too "
495 "fast, try --delay bigger than %u\n",
496 delay_ms);
497 return -E2BIG;
498 } else
499 fprintf(stderr, "unknown BS11 message\n");
500 }
Harald Weltec12d52b2009-02-01 21:39:06 +0000501#endif
Harald Welte14f09342009-01-29 19:28:38 +0000502
503 oh = (struct abis_om_hdr *) msgb_l2(rx_msg);
504 foh = (struct abis_om_fom_hdr *) oh->data;
505 switch (foh->msg_type) {
Harald Welte043d04a2009-01-29 23:15:30 +0000506 case NM_MT_BS11_LMT_LOGON_ACK:
Harald Weltefb81ae92009-02-19 02:59:17 +0000507 printf("LMT LOGON: ACK\n\n");
Harald Welte14f09342009-01-29 19:28:38 +0000508 if (bs11cfg_state == STATE_NONE)
509 bs11cfg_state = STATE_LOGON_ACK;
Harald Weltefb81ae92009-02-19 02:59:17 +0000510 rc = abis_nm_bs11_get_state(g_bts);
Harald Welte14f09342009-01-29 19:28:38 +0000511 break;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000512 case NM_MT_BS11_LMT_LOGOFF_ACK:
Harald Weltefb81ae92009-02-19 02:59:17 +0000513 printf("LMT LOGOFF: ACK\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000514 exit(0);
515 break;
Harald Welte14f09342009-01-29 19:28:38 +0000516 case NM_MT_BS11_GET_STATE_ACK:
Harald Weltefb81ae92009-02-19 02:59:17 +0000517 rc = abis_nm_tlv_parse(&tp, foh->data, oh->length-sizeof(*foh));
Harald Welte03133942009-02-18 19:51:53 +0000518 print_state(&tp);
519 if (TLVP_PRESENT(&tp, NM_ATT_BS11_BTS_STATE) &&
520 TLVP_LEN(&tp, NM_ATT_BS11_BTS_STATE) >= 1)
521 rc = handle_state_resp(*TLVP_VAL(&tp, NM_ATT_BS11_BTS_STATE));
Harald Welte14f09342009-01-29 19:28:38 +0000522 break;
Harald Weltefb81ae92009-02-19 02:59:17 +0000523 case NM_MT_GET_ATTR_RESP:
524 printf("\n%s ATTRIBUTES:\n", obj_name(foh));
525 abis_nm_tlv_parse(&tp, foh->data, oh->length-sizeof(*foh));
526 rc = print_attr(&tp);
527 //hexdump(foh->data, oh->length-sizeof(*foh));
528 break;
Harald Welte14f09342009-01-29 19:28:38 +0000529 default:
530 rc = abis_nm_rcvmsg(rx_msg);
531 }
532 if (rc < 0) {
533 perror("ERROR in main loop");
534 //break;
535 }
536 if (rc == 1)
537 return rc;
538
539 switch (bs11cfg_state) {
540 case STATE_NONE:
541 abis_nm_bs11_factory_logon(g_bts, 1);
542 break;
543 case STATE_LOGON_ACK:
Harald Weltefb81ae92009-02-19 02:59:17 +0000544 schedule_timer(&status_timer, 5, 0);
Harald Welte14f09342009-01-29 19:28:38 +0000545 break;
546 default:
547 break;
548 }
549
550 return rc;
551}
552
Harald Welte03133942009-02-18 19:51:53 +0000553int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
554 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
555{
556 return 0;
557}
558
Harald Weltefb81ae92009-02-19 02:59:17 +0000559void status_timer_cb(void *data)
560{
561 abis_nm_bs11_get_state(g_bts);
562}
563
Harald Welte61e42ad2009-01-18 19:10:46 +0000564static void print_banner(void)
565{
566 printf("bs11_config (C) 2009 by Harald Welte and Dieter Spaar\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000567 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
Harald Welte61e42ad2009-01-18 19:10:46 +0000568}
569
570static void print_help(void)
571{
Harald Weltefb81ae92009-02-19 02:59:17 +0000572 printf("bs11_config [options] [command]\n");
573 printf("\nSupported options:\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000574 printf("\t-h --help\t\t\tPrint this help text\n");
575 printf("\t-p --port </dev/ttyXXX>\t\tSpecify serial port\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000576 printf("\t-s --software <file>\t\tSpecify Software file\n");
577 printf("\t-S --safety <file>\t\tSpecify Safety Load file\n");
Harald Weltefb81ae92009-02-19 02:59:17 +0000578 printf("\t-d --delay <ms>\t\tSpecify delay in milliseconds\n");
Harald Welte3ffd1372009-02-01 22:15:49 +0000579 printf("\t-D --disconnect\t\t\tDisconnect BTS from BSC\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000580 printf("\t-w --win-size <num>\t\tSpecify Window Size\n");
Harald Welte3ffd1372009-02-01 22:15:49 +0000581 printf("\t-f --forced\t\t\tForce Software Load\n");
Harald Weltefb81ae92009-02-19 02:59:17 +0000582 printf("\nSupported commands:\n");
583 printf("\tquery\t\tQuery the BS-11 about serial number and configuration\n");
584 printf("\tdisconnect\tDisconnect A-bis link (go into administrative state)\n");
585 printf("\tresconnect\tReconnect A-bis link (go into normal state)\n");
586 printf("\trestart\t\tRestart the BTS\n");
587 printf("\tsoftware\tDownload Software (only in administrative state)\n");
588 printf("\tcreate-trx1\tCreate objects for TRX1 (Danger: Your BS-11 might overheat)\n");
589 printf("\tdelete-trx1\tDelete objects for TRX1\n");
Harald Welte61e42ad2009-01-18 19:10:46 +0000590}
591
592static void handle_options(int argc, char **argv)
593{
Harald Weltefb81ae92009-02-19 02:59:17 +0000594 int option_index = 0;
Harald Welte61e42ad2009-01-18 19:10:46 +0000595 print_banner();
596
597 while (1) {
Harald Weltefb81ae92009-02-19 02:59:17 +0000598 int c;
Harald Welte61e42ad2009-01-18 19:10:46 +0000599 static struct option long_options[] = {
600 { "help", 0, 0, 'h' },
601 { "port", 1, 0, 'p' },
Harald Welte61e42ad2009-01-18 19:10:46 +0000602 { "software", 1, 0, 's' },
603 { "safety", 1, 0, 'S' },
Harald Welte3b8ba212009-01-29 12:27:58 +0000604 { "delay", 1, 0, 'd' },
Harald Welte3ffd1372009-02-01 22:15:49 +0000605 { "disconnect", 0, 0, 'D' },
Harald Welte623d5312009-01-29 21:14:05 +0000606 { "win-size", 1, 0, 'w' },
Harald Welte3ffd1372009-02-01 22:15:49 +0000607 { "forced", 0, 0, 'f' },
Harald Welte1cbfaf52009-02-05 19:30:22 +0000608 { "restart", 0, 0, 'r' },
Harald Welte61e42ad2009-01-18 19:10:46 +0000609 };
610
Harald Welte03133942009-02-18 19:51:53 +0000611 c = getopt_long(argc, argv, "hp:s:S:td:Dw:fra:",
Harald Welte61e42ad2009-01-18 19:10:46 +0000612 long_options, &option_index);
613
614 if (c == -1)
615 break;
616
617 switch (c) {
618 case 'h':
619 print_help();
620 exit(0);
621 case 'p':
622 serial_port = optarg;
623 break;
Harald Welte61e42ad2009-01-18 19:10:46 +0000624 case 's':
625 fname_software = optarg;
626 break;
627 case 'S':
628 fname_safety = optarg;
629 break;
Harald Welte3b8ba212009-01-29 12:27:58 +0000630 case 'd':
631 delay_ms = atoi(optarg);
632 break;
Harald Welte623d5312009-01-29 21:14:05 +0000633 case 'w':
634 win_size = atoi(optarg);
635 break;
Harald Welte3ffd1372009-02-01 22:15:49 +0000636 case 'D':
637 param_disconnect = 1;
638 break;
639 case 'f':
640 param_forced = 1;
641 break;
Harald Welte1cbfaf52009-02-05 19:30:22 +0000642 case 'r':
643 param_disconnect = 1;
644 param_restart = 1;
645 break;
Harald Welte61e42ad2009-01-18 19:10:46 +0000646 default:
647 break;
648 }
649 }
Harald Weltefb81ae92009-02-19 02:59:17 +0000650 if (optind < argc)
651 command = argv[optind];
Harald Welte61e42ad2009-01-18 19:10:46 +0000652}
653
Harald Welte7ed1a642009-02-02 14:45:49 +0000654static int num_sigint;
655
Harald Welte14f09342009-01-29 19:28:38 +0000656static void signal_handler(int signal)
657{
Harald Weltefb81ae92009-02-19 02:59:17 +0000658 fprintf(stdout, "\nsignal %u received\n", signal);
Harald Welte14f09342009-01-29 19:28:38 +0000659
660 switch (signal) {
Harald Welte5e4d1b32009-02-01 13:36:56 +0000661 case SIGINT:
Harald Welte7ed1a642009-02-02 14:45:49 +0000662 num_sigint++;
Harald Welte14f09342009-01-29 19:28:38 +0000663 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Welte7ed1a642009-02-02 14:45:49 +0000664 if (num_sigint >= 3)
665 exit(0);
Harald Welte14f09342009-01-29 19:28:38 +0000666 break;
667 }
668}
669
Harald Welte61e42ad2009-01-18 19:10:46 +0000670int main(int argc, char **argv)
671{
672 struct gsm_network *gsmnet;
Harald Weltef186c462009-01-29 08:45:19 +0000673 int rc;
Harald Welte61e42ad2009-01-18 19:10:46 +0000674
675 handle_options(argc, argv);
676
Harald Welte03133942009-02-18 19:51:53 +0000677 gsmnet = gsm_network_init(1, 1, 1, GSM_BTS_TYPE_BS11);
Harald Welte61e42ad2009-01-18 19:10:46 +0000678 if (!gsmnet) {
679 fprintf(stderr, "Unable to allocate gsm network\n");
680 exit(1);
681 }
682 g_bts = &gsmnet->bts[0];
683
Harald Welte7ed1a642009-02-02 14:45:49 +0000684 rc = rs232_setup(serial_port, delay_ms);
685 if (rc < 0) {
686 fprintf(stderr, "Problem setting up serial port\n");
687 exit(1);
688 }
Harald Welte14f09342009-01-29 19:28:38 +0000689
Harald Welte5e4d1b32009-02-01 13:36:56 +0000690 signal(SIGINT, &signal_handler);
Harald Welte14f09342009-01-29 19:28:38 +0000691
Harald Welte61e42ad2009-01-18 19:10:46 +0000692 abis_nm_bs11_factory_logon(g_bts, 1);
Harald Welte5e4d1b32009-02-01 13:36:56 +0000693 //abis_nm_bs11_get_serno(g_bts);
Harald Welte61e42ad2009-01-18 19:10:46 +0000694
Harald Weltefb81ae92009-02-19 02:59:17 +0000695 status_timer.cb = status_timer_cb;
696
Harald Welte61e42ad2009-01-18 19:10:46 +0000697 while (1) {
Harald Welte14f09342009-01-29 19:28:38 +0000698 bsc_select_main();
Harald Welte61e42ad2009-01-18 19:10:46 +0000699 }
700
701 abis_nm_bs11_factory_logon(g_bts, 0);
702
Harald Welte61e42ad2009-01-18 19:10:46 +0000703 exit(0);
704}