blob: 7605d93e0866a1569dc0eb5adf4110d657c94ada [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 Welte099d6102009-02-21 12:59:58 +000043#include <openbsc/rs232.h>
Harald Welte61e42ad2009-01-18 19:10:46 +000044
Harald Welte3b8ba212009-01-29 12:27:58 +000045/* state of our bs11_config application */
46enum bs11cfg_state {
47 STATE_NONE,
48 STATE_LOGON_WAIT,
49 STATE_LOGON_ACK,
50 STATE_SWLOAD,
Harald Weltefb81ae92009-02-19 02:59:17 +000051 STATE_QUERY,
Harald Welte3b8ba212009-01-29 12:27:58 +000052};
53static enum bs11cfg_state bs11cfg_state = STATE_NONE;
Harald Weltefb81ae92009-02-19 02:59:17 +000054static char *command;
55struct timer_list status_timer;
Harald Welte3b8ba212009-01-29 12:27:58 +000056
Harald Welte61e42ad2009-01-18 19:10:46 +000057static const u_int8_t obj_li_attr[] = {
Harald Welte5e4d1b32009-02-01 13:36:56 +000058 NM_ATT_BS11_BIT_ERR_THESH, 0x09, 0x00,
59 NM_ATT_BS11_L1_PROT_TYPE, 0x00,
60 NM_ATT_BS11_LINE_CFG, 0x00,
Harald Welte61e42ad2009-01-18 19:10:46 +000061};
62static const u_int8_t obj_bbsig0_attr[] = {
Harald Welte5e4d1b32009-02-01 13:36:56 +000063 NM_ATT_BS11_RSSI_OFFS, 0x02, 0x00, 0x00,
64 NM_ATT_BS11_DIVERSITY, 0x01, 0x00,
Harald Welte61e42ad2009-01-18 19:10:46 +000065};
66static const u_int8_t obj_pa0_attr[] = {
Harald Welte5e4d1b32009-02-01 13:36:56 +000067 NM_ATT_BS11_TXPWR, 0x01, BS11_TRX_POWER_GSM_30mW,
Harald Welte61e42ad2009-01-18 19:10:46 +000068};
69static const char *trx1_password = "1111111111";
70#define TEI_OML 25
71
Harald Welte3b8ba212009-01-29 12:27:58 +000072static const u_int8_t too_fast[] = { 0x12, 0x80, 0x00, 0x00, 0x02, 0x02 };
73
Harald Welte14f09342009-01-29 19:28:38 +000074
Harald Weltec12d52b2009-02-01 21:39:06 +000075int handle_serial_msg(struct msgb *rx_msg);
Harald Welte14f09342009-01-29 19:28:38 +000076
Harald Welte1cbfaf52009-02-05 19:30:22 +000077/* create all objects for an initial configuration */
78static int create_objects(struct gsm_bts *bts)
Harald Welte268bb402009-02-01 19:11:56 +000079{
Harald Welte1cbfaf52009-02-05 19:30:22 +000080 fprintf(stdout, "Crating Objects for minimal config\n");
Harald Welte61e42ad2009-01-18 19:10:46 +000081 abis_nm_bs11_create_object(bts, BS11_OBJ_LI, 0, sizeof(obj_li_attr),
82 obj_li_attr);
83 abis_nm_bs11_create_object(bts, BS11_OBJ_GPSU, 0, 0, NULL);
84 abis_nm_bs11_create_object(bts, BS11_OBJ_ALCO, 0, 0, NULL);
Harald Welte623d5312009-01-29 21:14:05 +000085 abis_nm_bs11_create_object(bts, BS11_OBJ_CCLK, 0, 0, NULL);
Harald Welte61e42ad2009-01-18 19:10:46 +000086 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 0,
87 sizeof(obj_bbsig0_attr), obj_bbsig0_attr);
88 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 0,
89 sizeof(obj_pa0_attr), obj_pa0_attr);
Harald Welte61e42ad2009-01-18 19:10:46 +000090 abis_nm_bs11_create_envaBTSE(bts, 0);
91 abis_nm_bs11_create_envaBTSE(bts, 1);
92 abis_nm_bs11_create_envaBTSE(bts, 2);
93 abis_nm_bs11_create_envaBTSE(bts, 3);
94
Harald Welteb6c92ae2009-02-21 20:15:32 +000095 abis_nm_bs11_conn_oml_tei(bts, 0, 1, 0xff, TEI_OML);
Harald Welte61e42ad2009-01-18 19:10:46 +000096
Harald Welte5e4d1b32009-02-01 13:36:56 +000097 abis_nm_bs11_set_trx_power(&bts->trx[0], BS11_TRX_POWER_GSM_30mW);
Harald Welte61e42ad2009-01-18 19:10:46 +000098
Harald Welte1cbfaf52009-02-05 19:30:22 +000099 sleep(1);
100
Harald Weltefb81ae92009-02-19 02:59:17 +0000101 abis_nm_bs11_set_trx1_pw(bts, trx1_password);
102
103 sleep(1);
104
105 return 0;
106}
107
108static int create_trx1(struct gsm_bts *bts)
109{
110 u_int8_t bbsig1_attr[sizeof(obj_bbsig0_attr)+12];
111 u_int8_t *cur = bbsig1_attr;
112
Harald Welte1cbfaf52009-02-05 19:30:22 +0000113 fprintf(stdout, "Crating Objects for TRX1\n");
114
115 abis_nm_bs11_set_trx1_pw(bts, trx1_password);
116
117 sleep(1);
Harald Weltefb81ae92009-02-19 02:59:17 +0000118
Harald Welte1cbfaf52009-02-05 19:30:22 +0000119 cur = tlv_put(cur, NM_ATT_BS11_PASSWORD, 10,
120 (u_int8_t *)trx1_password);
121 memcpy(cur, obj_bbsig0_attr, sizeof(obj_bbsig0_attr));
122 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 1,
123 sizeof(bbsig1_attr), bbsig1_attr);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000124 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 1,
125 sizeof(obj_pa0_attr), obj_pa0_attr);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000126 abis_nm_bs11_set_trx_power(&bts->trx[1], BS11_TRX_POWER_GSM_30mW);
Harald Weltefb81ae92009-02-19 02:59:17 +0000127
Harald Welte61e42ad2009-01-18 19:10:46 +0000128 return 0;
129}
130
131static char *serial_port = "/dev/ttyUSB0";
132static char *fname_safety = "BTSBMC76.SWI";
133static char *fname_software = "HS011106.SWL";
Harald Welte623d5312009-01-29 21:14:05 +0000134static int delay_ms = 0;
Harald Welte623d5312009-01-29 21:14:05 +0000135static int win_size = 8;
Harald Welte3ffd1372009-02-01 22:15:49 +0000136static int param_disconnect = 0;
Harald Welte1cbfaf52009-02-05 19:30:22 +0000137static int param_restart = 0;
Harald Welte3ffd1372009-02-01 22:15:49 +0000138static int param_forced = 0;
Harald Welte61e42ad2009-01-18 19:10:46 +0000139static struct gsm_bts *g_bts;
140
Harald Welte61e42ad2009-01-18 19:10:46 +0000141static int file_is_readable(const char *fname)
142{
143 int rc;
144 struct stat st;
145
146 rc = stat(fname, &st);
147 if (rc < 0)
148 return 0;
149
150 if (S_ISREG(st.st_mode) && (st.st_mode & S_IRUSR))
151 return 1;
152
153 return 0;
154}
155
Harald Welte3ffd1372009-02-01 22:15:49 +0000156static int percent;
157static int percent_old;
158
Harald Welte5e4d1b32009-02-01 13:36:56 +0000159/* callback function passed to the ABIS OML code */
160static int swload_cbfn(unsigned int hook, unsigned int event, struct msgb *msg,
161 void *data, void *param)
162{
163 if (hook != GSM_HOOK_NM_SWLOAD)
164 return 0;
Harald Welte61e42ad2009-01-18 19:10:46 +0000165
Harald Welte5e4d1b32009-02-01 13:36:56 +0000166 switch (event) {
167 case NM_MT_LOAD_INIT_ACK:
168 fprintf(stdout, "Software Load Initiate ACK\n");
169 break;
170 case NM_MT_LOAD_INIT_NACK:
171 fprintf(stderr, "ERROR: Software Load Initiate NACK\n");
172 exit(5);
173 break;
174 case NM_MT_LOAD_END_ACK:
Harald Welte1cbfaf52009-02-05 19:30:22 +0000175 if (data) {
Harald Welte03133942009-02-18 19:51:53 +0000176 /* we did a safety load and must activate it */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000177 abis_nm_software_activate(g_bts, fname_safety,
178 swload_cbfn, g_bts);
Harald Welte03133942009-02-18 19:51:53 +0000179 sleep(5);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000180 }
Harald Welte5e4d1b32009-02-01 13:36:56 +0000181 break;
182 case NM_MT_LOAD_END_NACK:
183 fprintf(stderr, "ERROR: Software Load End NACK\n");
184 exit(3);
185 break;
186 case NM_MT_ACTIVATE_SW_NACK:
187 fprintf(stderr, "ERROR: Activate Software NACK\n");
188 exit(4);
189 break;
190 case NM_MT_ACTIVATE_SW_ACK:
191 bs11cfg_state = STATE_NONE;
192
193 break;
Harald Welte3ffd1372009-02-01 22:15:49 +0000194 case NM_MT_LOAD_SEG_ACK:
195 percent = abis_nm_software_load_status(g_bts);
196 if (percent > percent_old)
197 printf("Software Download Progress: %d%%\n", percent);
198 percent_old = percent;
199 break;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000200 }
201 return 0;
202}
203
Harald Welte268bb402009-02-01 19:11:56 +0000204static const char *bs11_link_state[] = {
205 [0x00] = "Down",
206 [0x01] = "Up",
207 [0x02] = "Restoring",
208};
209
210static const char *linkstate_name(u_int8_t linkstate)
Harald Welte61e42ad2009-01-18 19:10:46 +0000211{
Harald Welte268bb402009-02-01 19:11:56 +0000212 if (linkstate > ARRAY_SIZE(bs11_link_state))
213 return "Unknown";
Harald Welte3b8ba212009-01-29 12:27:58 +0000214
Harald Welte268bb402009-02-01 19:11:56 +0000215 return bs11_link_state[linkstate];
216}
Harald Welte3b8ba212009-01-29 12:27:58 +0000217
Harald Welte268bb402009-02-01 19:11:56 +0000218static const char *mbccu_load[] = {
219 [0] = "No Load",
220 [1] = "Load BTSCAC",
221 [2] = "Load BTSDRX",
222 [3] = "Load BTSBBX",
223 [4] = "Load BTSARC",
224 [5] = "Load",
225};
226
227static const char *mbccu_load_name(u_int8_t linkstate)
228{
229 if (linkstate > ARRAY_SIZE(mbccu_load))
230 return "Unknown";
231
232 return mbccu_load[linkstate];
233}
234
Harald Welte03133942009-02-18 19:51:53 +0000235static const char *bts_phase_name(u_int8_t phase)
Harald Welte268bb402009-02-01 19:11:56 +0000236{
Harald Welte268bb402009-02-01 19:11:56 +0000237 switch (phase) {
Harald Welte61e42ad2009-01-18 19:10:46 +0000238 case BS11_STATE_WARM_UP:
Harald Weltec12d52b2009-02-01 21:39:06 +0000239 case BS11_STATE_WARM_UP_2:
Harald Welte03133942009-02-18 19:51:53 +0000240 return "Warm Up";
Harald Welte61e42ad2009-01-18 19:10:46 +0000241 break;
242 case BS11_STATE_LOAD_SMU_SAFETY:
Harald Welte03133942009-02-18 19:51:53 +0000243 return "Load SMU Safety";
Harald Welte268bb402009-02-01 19:11:56 +0000244 break;
245 case BS11_STATE_LOAD_SMU_INTENDED:
Harald Welte03133942009-02-18 19:51:53 +0000246 return "Load SMU Intended";
Harald Welte268bb402009-02-01 19:11:56 +0000247 break;
248 case BS11_STATE_LOAD_MBCCU:
Harald Welte03133942009-02-18 19:51:53 +0000249 return "Load MBCCU";
Harald Welte61e42ad2009-01-18 19:10:46 +0000250 break;
251 case BS11_STATE_SOFTWARE_RQD:
Harald Welte03133942009-02-18 19:51:53 +0000252 return "Software required";
Harald Welte268bb402009-02-01 19:11:56 +0000253 break;
254 case BS11_STATE_WAIT_MIN_CFG:
255 case BS11_STATE_WAIT_MIN_CFG_2:
Harald Welte03133942009-02-18 19:51:53 +0000256 return "Wait minimal config";
Harald Welte268bb402009-02-01 19:11:56 +0000257 break;
258 case BS11_STATE_MAINTENANCE:
Harald Welte03133942009-02-18 19:51:53 +0000259 return "Maintenance";
Harald Welte268bb402009-02-01 19:11:56 +0000260 break;
261 case BS11_STATE_NORMAL:
Harald Welte03133942009-02-18 19:51:53 +0000262 return "Normal";
263 break;
264 case BS11_STATE_ABIS_LOAD:
265 return "Abis load";
Harald Welte268bb402009-02-01 19:11:56 +0000266 break;
267 default:
Harald Welte03133942009-02-18 19:51:53 +0000268 return "Unknown";
Harald Welte268bb402009-02-01 19:11:56 +0000269 break;
270 }
271}
272
Harald Weltefb81ae92009-02-19 02:59:17 +0000273static const char *trx_power_name(u_int8_t pwr)
274{
275 switch (pwr) {
276 case BS11_TRX_POWER_GSM_2W:
277 return "2W (GSM)";
278 case BS11_TRX_POWER_GSM_250mW:
279 return "250mW (GSM)";
280 case BS11_TRX_POWER_GSM_80mW:
281 return "80mW (GSM)";
282 case BS11_TRX_POWER_GSM_30mW:
283 return "30mW (GSM)";
284 case BS11_TRX_POWER_DCS_3W:
285 return "3W (DCS)";
286 case BS11_TRX_POWER_DCS_1W6:
287 return "1.6W (DCS)";
288 case BS11_TRX_POWER_DCS_500mW:
289 return "500mW (DCS)";
290 case BS11_TRX_POWER_DCS_160mW:
291 return "160mW (DCS)";
292 default:
293 return "unknown value";
294 }
295}
296
297static const char *obj_name(struct abis_om_fom_hdr *foh)
298{
299 static char retbuf[256];
300
301 retbuf[0] = 0;
302
303 switch (foh->obj_class) {
304 case NM_OC_BS11:
305 strcat(retbuf, "BS11 ");
306 switch (foh->obj_inst.bts_nr) {
307 case BS11_OBJ_PA:
308 sprintf(retbuf+strlen(retbuf), "Power Amplifier %d ",
309 foh->obj_inst.ts_nr);
310 break;
311 }
312 break;
313 case NM_OC_SITE_MANAGER:
314 strcat(retbuf, "SITE MANAGER ");
315 break;
316 }
317 return retbuf;
318}
319
Harald Welte03133942009-02-18 19:51:53 +0000320static void print_state(struct tlv_parsed *tp)
321{
322 if (TLVP_PRESENT(tp, NM_ATT_BS11_BTS_STATE)) {
323 u_int8_t phase, mbccu;
324 if (TLVP_LEN(tp, NM_ATT_BS11_BTS_STATE) >= 1) {
325 phase = *TLVP_VAL(tp, NM_ATT_BS11_BTS_STATE);
326 printf("PHASE: %u %-20s ", phase & 0xf,
327 bts_phase_name(phase));
328 }
329 if (TLVP_LEN(tp, NM_ATT_BS11_BTS_STATE) >= 2) {
330 mbccu = *(TLVP_VAL(tp, NM_ATT_BS11_BTS_STATE)+1);
331 printf("MBCCU0: %-11s MBCCU1: %-11s ",
332 mbccu_load_name(mbccu & 0xf), mbccu_load_name(mbccu >> 4));
333 }
334 }
335 if (TLVP_PRESENT(tp, NM_ATT_BS11_E1_STATE) &&
336 TLVP_LEN(tp, NM_ATT_BS11_E1_STATE) >= 1) {
337 u_int8_t e1_state = *TLVP_VAL(tp, NM_ATT_BS11_E1_STATE);
338 printf("Abis-link: %-9s ", linkstate_name(e1_state & 0xf));
339 }
340 printf("\n");
341}
342
Harald Weltefb81ae92009-02-19 02:59:17 +0000343static int print_attr(struct tlv_parsed *tp)
344{
345 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_PCB_SERIAL)) {
346 printf("\tBS-11 ESN PCB Serial Number: %s\n",
347 TLVP_VAL(tp, NM_ATT_BS11_ESN_PCB_SERIAL));
348 }
349 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_HW_CODE_NO)) {
350 printf("\tBS-11 ESN Hardware Code Number: %s\n",
351 TLVP_VAL(tp, NM_ATT_BS11_ESN_HW_CODE_NO)+6);
352 }
353 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_FW_CODE_NO)) {
354 printf("\tBS-11 ESN Firmware Code Number: %s\n",
355 TLVP_VAL(tp, NM_ATT_BS11_ESN_FW_CODE_NO)+6);
356 }
357#if 0
358 if (TLVP_PRESENT(tp, NM_ATT_BS11_BOOT_SW_VERS)) {
359 printf("BS-11 Boot Software Version: %s\n",
360 TLVP_VAL(tp, NM_ATT_BS11_BOOT_SW_VERS)+6);
361 }
362#endif
363 if (TLVP_PRESENT(tp, NM_ATT_ABIS_CHANNEL) &&
364 TLVP_LEN(tp, NM_ATT_ABIS_CHANNEL) >= 3) {
Harald Welte099d6102009-02-21 12:59:58 +0000365 struct abis_nm_channel *chan =
366 (struct abis_nm_channel*) TLVP_VAL(tp, NM_ATT_ABIS_CHANNEL)-1;
Harald Weltefb81ae92009-02-19 02:59:17 +0000367 printf("\tE1 Channel: Port=%u Timeslot=%u ",
368 chan->bts_port, chan->timeslot);
369 if (chan->subslot == 0xff)
370 printf("(Full Slot)\n");
371 else
372 printf("Subslot=%u\n", chan->subslot);
373 }
374 if (TLVP_PRESENT(tp, NM_ATT_TEI))
375 printf("\tTEI: %d\n", *TLVP_VAL(tp, NM_ATT_TEI));
376 if (TLVP_PRESENT(tp, NM_ATT_BS11_TXPWR) &&
377 TLVP_LEN(tp, NM_ATT_BS11_TXPWR) >= 1) {
378 printf("\tTRX Power: %s\n",
379 trx_power_name(*TLVP_VAL(tp, NM_ATT_BS11_TXPWR)));
380 }
381
382 return 0;
383}
384
Harald Welte268bb402009-02-01 19:11:56 +0000385/* handle a response from the BTS to a GET STATE command */
386static int handle_state_resp(enum abis_bs11_phase state)
387{
388 int rc = 0;
389
Harald Welte268bb402009-02-01 19:11:56 +0000390 switch (state) {
391 case BS11_STATE_WARM_UP:
Harald Welte268bb402009-02-01 19:11:56 +0000392 case BS11_STATE_LOAD_SMU_SAFETY:
Harald Welte268bb402009-02-01 19:11:56 +0000393 case BS11_STATE_LOAD_SMU_INTENDED:
Harald Welte268bb402009-02-01 19:11:56 +0000394 case BS11_STATE_LOAD_MBCCU:
395 break;
396 case BS11_STATE_SOFTWARE_RQD:
Harald Welte3b8ba212009-01-29 12:27:58 +0000397 bs11cfg_state = STATE_SWLOAD;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000398 /* send safety load. Use g_bts as private 'param'
399 * argument, so our swload_cbfn can distinguish
400 * a safety load from a regular software */
Harald Welte61e42ad2009-01-18 19:10:46 +0000401 if (file_is_readable(fname_safety))
Harald Welte623d5312009-01-29 21:14:05 +0000402 rc = abis_nm_software_load(g_bts, fname_safety,
Harald Welte3ffd1372009-02-01 22:15:49 +0000403 win_size, param_forced,
404 swload_cbfn, g_bts);
Harald Welte61e42ad2009-01-18 19:10:46 +0000405 else
Harald Welte071f34d2009-01-29 09:24:38 +0000406 fprintf(stderr, "No valid Safety Load file \"%s\"\n",
407 fname_safety);
Harald Welte61e42ad2009-01-18 19:10:46 +0000408 break;
409 case BS11_STATE_WAIT_MIN_CFG:
410 case BS11_STATE_WAIT_MIN_CFG_2:
Harald Welte3b8ba212009-01-29 12:27:58 +0000411 bs11cfg_state = STATE_SWLOAD;
Harald Welte3ffd1372009-02-01 22:15:49 +0000412 rc = create_objects(g_bts);
Harald Welte61e42ad2009-01-18 19:10:46 +0000413 break;
414 case BS11_STATE_MAINTENANCE:
Harald Weltefb81ae92009-02-19 02:59:17 +0000415 if (command) {
416 if (!strcmp(command, "disconnect"))
Harald Weltef80b7d32009-02-19 03:11:13 +0000417 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000418 else if (!strcmp(command, "reconnect"))
419 rc = abis_nm_bs11_bsc_disconnect(g_bts, 1);
420 else if (!strcmp(command, "software")
421 && bs11cfg_state != STATE_SWLOAD) {
422 bs11cfg_state = STATE_SWLOAD;
423 /* send software (FIXME: over A-bis?) */
424 if (file_is_readable(fname_software))
425 rc = abis_nm_bs11_load_swl(g_bts, fname_software,
426 win_size, param_forced,
427 swload_cbfn);
428 else
429 fprintf(stderr, "No valid Software file \"%s\"\n",
430 fname_software);
431 } else if (!strcmp(command, "delete-trx1")) {
432 abis_nm_bs11_delete_object(g_bts, BS11_OBJ_BBSIG, 1);
433 abis_nm_bs11_delete_object(g_bts, BS11_OBJ_PA, 1);
Harald Weltef80b7d32009-02-19 03:11:13 +0000434 sleep(5);
435 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000436 command = NULL;
437 } else if (!strcmp(command, "create-trx1")) {
438 create_trx1(g_bts);
Harald Weltef80b7d32009-02-19 03:11:13 +0000439 sleep(5);
440 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000441 command = NULL;
Harald Welteb6c92ae2009-02-21 20:15:32 +0000442 } else if (!strcmp(command, "oml-tei")) {
443 abis_nm_bs11_conn_oml_tei(g_bts, 0, 1, 0xff, TEI_OML);
Harald Weltefb81ae92009-02-19 02:59:17 +0000444 } else if (!strcmp(command, "restart")) {
445 abis_nm_bs11_restart(g_bts);
446 command = NULL;
447 }
Harald Welte3ffd1372009-02-01 22:15:49 +0000448 }
Harald Welte61e42ad2009-01-18 19:10:46 +0000449 break;
450 case BS11_STATE_NORMAL:
Harald Weltefb81ae92009-02-19 02:59:17 +0000451 if (command) {
452 if (!strcmp(command, "reconnect"))
Harald Weltef80b7d32009-02-19 03:11:13 +0000453 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000454 else if (!strcmp(command, "disconnect"))
455 abis_nm_bs11_bsc_disconnect(g_bts, 0);
456 else if (!strcmp(command, "query")) {
457 bs11cfg_state = STATE_QUERY;
458 abis_nm_bs11_get_serno(g_bts);
459 abis_nm_bs11_get_oml_tei_ts(g_bts);
460 abis_nm_bs11_get_trx_power(&g_bts->trx[0]);
461 abis_nm_bs11_get_trx_power(&g_bts->trx[1]);
Harald Weltef80b7d32009-02-19 03:11:13 +0000462 sleep(5);
463 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000464 command = NULL;
465 }
466 } else if (param_disconnect) {
Harald Welte1cbfaf52009-02-05 19:30:22 +0000467 param_disconnect = 0;
468 abis_nm_bs11_bsc_disconnect(g_bts, 0);
469 if (param_restart) {
470 param_restart = 0;
471 abis_nm_bs11_restart(g_bts);
472 }
473 }
Harald Weltefb81ae92009-02-19 02:59:17 +0000474 break;
Harald Welte61e42ad2009-01-18 19:10:46 +0000475 default:
Harald Welte61e42ad2009-01-18 19:10:46 +0000476 break;
477 }
Harald Welte3b8ba212009-01-29 12:27:58 +0000478 return rc;
Harald Welte61e42ad2009-01-18 19:10:46 +0000479}
480
Harald Welte5e4d1b32009-02-01 13:36:56 +0000481/* handle a fully-received message/packet from the RS232 port */
Harald Weltec12d52b2009-02-01 21:39:06 +0000482int handle_serial_msg(struct msgb *rx_msg)
Harald Welte14f09342009-01-29 19:28:38 +0000483{
484 struct abis_om_hdr *oh;
485 struct abis_om_fom_hdr *foh;
Harald Welte03133942009-02-18 19:51:53 +0000486 struct tlv_parsed tp;
Harald Welte14f09342009-01-29 19:28:38 +0000487 int rc = -1;
488
Harald Weltec12d52b2009-02-01 21:39:06 +0000489#if 0
Harald Welte14f09342009-01-29 19:28:38 +0000490 if (rx_msg->len < LAPD_HDR_LEN
491 + sizeof(struct abis_om_fom_hdr)
492 + sizeof(struct abis_om_hdr)) {
493 if (!memcmp(rx_msg->data + 2, too_fast,
494 sizeof(too_fast))) {
495 fprintf(stderr, "BS11 tells us we're too "
496 "fast, try --delay bigger than %u\n",
497 delay_ms);
498 return -E2BIG;
499 } else
500 fprintf(stderr, "unknown BS11 message\n");
501 }
Harald Weltec12d52b2009-02-01 21:39:06 +0000502#endif
Harald Welte14f09342009-01-29 19:28:38 +0000503
504 oh = (struct abis_om_hdr *) msgb_l2(rx_msg);
505 foh = (struct abis_om_fom_hdr *) oh->data;
506 switch (foh->msg_type) {
Harald Welte043d04a2009-01-29 23:15:30 +0000507 case NM_MT_BS11_LMT_LOGON_ACK:
Harald Weltefb81ae92009-02-19 02:59:17 +0000508 printf("LMT LOGON: ACK\n\n");
Harald Welte14f09342009-01-29 19:28:38 +0000509 if (bs11cfg_state == STATE_NONE)
510 bs11cfg_state = STATE_LOGON_ACK;
Harald Weltefb81ae92009-02-19 02:59:17 +0000511 rc = abis_nm_bs11_get_state(g_bts);
Harald Welte14f09342009-01-29 19:28:38 +0000512 break;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000513 case NM_MT_BS11_LMT_LOGOFF_ACK:
Harald Weltefb81ae92009-02-19 02:59:17 +0000514 printf("LMT LOGOFF: ACK\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000515 exit(0);
516 break;
Harald Welte14f09342009-01-29 19:28:38 +0000517 case NM_MT_BS11_GET_STATE_ACK:
Harald Weltefb81ae92009-02-19 02:59:17 +0000518 rc = abis_nm_tlv_parse(&tp, foh->data, oh->length-sizeof(*foh));
Harald Welte03133942009-02-18 19:51:53 +0000519 print_state(&tp);
520 if (TLVP_PRESENT(&tp, NM_ATT_BS11_BTS_STATE) &&
521 TLVP_LEN(&tp, NM_ATT_BS11_BTS_STATE) >= 1)
522 rc = handle_state_resp(*TLVP_VAL(&tp, NM_ATT_BS11_BTS_STATE));
Harald Welte14f09342009-01-29 19:28:38 +0000523 break;
Harald Weltefb81ae92009-02-19 02:59:17 +0000524 case NM_MT_GET_ATTR_RESP:
525 printf("\n%s ATTRIBUTES:\n", obj_name(foh));
526 abis_nm_tlv_parse(&tp, foh->data, oh->length-sizeof(*foh));
527 rc = print_attr(&tp);
528 //hexdump(foh->data, oh->length-sizeof(*foh));
529 break;
Harald Welte14f09342009-01-29 19:28:38 +0000530 default:
531 rc = abis_nm_rcvmsg(rx_msg);
532 }
533 if (rc < 0) {
534 perror("ERROR in main loop");
535 //break;
536 }
537 if (rc == 1)
538 return rc;
539
540 switch (bs11cfg_state) {
541 case STATE_NONE:
542 abis_nm_bs11_factory_logon(g_bts, 1);
543 break;
544 case STATE_LOGON_ACK:
Harald Weltefb81ae92009-02-19 02:59:17 +0000545 schedule_timer(&status_timer, 5, 0);
Harald Welte14f09342009-01-29 19:28:38 +0000546 break;
547 default:
548 break;
549 }
550
551 return rc;
552}
553
Harald Welte03133942009-02-18 19:51:53 +0000554int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
555 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
556{
557 return 0;
558}
559
Harald Weltefb81ae92009-02-19 02:59:17 +0000560void status_timer_cb(void *data)
561{
562 abis_nm_bs11_get_state(g_bts);
563}
564
Harald Welte61e42ad2009-01-18 19:10:46 +0000565static void print_banner(void)
566{
567 printf("bs11_config (C) 2009 by Harald Welte and Dieter Spaar\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000568 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
Harald Welte61e42ad2009-01-18 19:10:46 +0000569}
570
571static void print_help(void)
572{
Harald Weltefb81ae92009-02-19 02:59:17 +0000573 printf("bs11_config [options] [command]\n");
574 printf("\nSupported options:\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000575 printf("\t-h --help\t\t\tPrint this help text\n");
576 printf("\t-p --port </dev/ttyXXX>\t\tSpecify serial port\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000577 printf("\t-s --software <file>\t\tSpecify Software file\n");
578 printf("\t-S --safety <file>\t\tSpecify Safety Load file\n");
Harald Weltefb81ae92009-02-19 02:59:17 +0000579 printf("\t-d --delay <ms>\t\tSpecify delay in milliseconds\n");
Harald Welte3ffd1372009-02-01 22:15:49 +0000580 printf("\t-D --disconnect\t\t\tDisconnect BTS from BSC\n");
Harald Welte5e4d1b32009-02-01 13:36:56 +0000581 printf("\t-w --win-size <num>\t\tSpecify Window Size\n");
Harald Welte3ffd1372009-02-01 22:15:49 +0000582 printf("\t-f --forced\t\t\tForce Software Load\n");
Harald Weltefb81ae92009-02-19 02:59:17 +0000583 printf("\nSupported commands:\n");
584 printf("\tquery\t\tQuery the BS-11 about serial number and configuration\n");
585 printf("\tdisconnect\tDisconnect A-bis link (go into administrative state)\n");
586 printf("\tresconnect\tReconnect A-bis link (go into normal state)\n");
587 printf("\trestart\t\tRestart the BTS\n");
588 printf("\tsoftware\tDownload Software (only in administrative state)\n");
589 printf("\tcreate-trx1\tCreate objects for TRX1 (Danger: Your BS-11 might overheat)\n");
590 printf("\tdelete-trx1\tDelete objects for TRX1\n");
Harald Welte61e42ad2009-01-18 19:10:46 +0000591}
592
593static void handle_options(int argc, char **argv)
594{
Harald Weltefb81ae92009-02-19 02:59:17 +0000595 int option_index = 0;
Harald Welte61e42ad2009-01-18 19:10:46 +0000596 print_banner();
597
598 while (1) {
Harald Weltefb81ae92009-02-19 02:59:17 +0000599 int c;
Harald Welte61e42ad2009-01-18 19:10:46 +0000600 static struct option long_options[] = {
601 { "help", 0, 0, 'h' },
602 { "port", 1, 0, 'p' },
Harald Welte61e42ad2009-01-18 19:10:46 +0000603 { "software", 1, 0, 's' },
604 { "safety", 1, 0, 'S' },
Harald Welte3b8ba212009-01-29 12:27:58 +0000605 { "delay", 1, 0, 'd' },
Harald Welte3ffd1372009-02-01 22:15:49 +0000606 { "disconnect", 0, 0, 'D' },
Harald Welte623d5312009-01-29 21:14:05 +0000607 { "win-size", 1, 0, 'w' },
Harald Welte3ffd1372009-02-01 22:15:49 +0000608 { "forced", 0, 0, 'f' },
Harald Welte1cbfaf52009-02-05 19:30:22 +0000609 { "restart", 0, 0, 'r' },
Harald Welte61e42ad2009-01-18 19:10:46 +0000610 };
611
Harald Welte03133942009-02-18 19:51:53 +0000612 c = getopt_long(argc, argv, "hp:s:S:td:Dw:fra:",
Harald Welte61e42ad2009-01-18 19:10:46 +0000613 long_options, &option_index);
614
615 if (c == -1)
616 break;
617
618 switch (c) {
619 case 'h':
620 print_help();
621 exit(0);
622 case 'p':
623 serial_port = optarg;
624 break;
Harald Welte61e42ad2009-01-18 19:10:46 +0000625 case 's':
626 fname_software = optarg;
627 break;
628 case 'S':
629 fname_safety = optarg;
630 break;
Harald Welte3b8ba212009-01-29 12:27:58 +0000631 case 'd':
632 delay_ms = atoi(optarg);
633 break;
Harald Welte623d5312009-01-29 21:14:05 +0000634 case 'w':
635 win_size = atoi(optarg);
636 break;
Harald Welte3ffd1372009-02-01 22:15:49 +0000637 case 'D':
638 param_disconnect = 1;
639 break;
640 case 'f':
641 param_forced = 1;
642 break;
Harald Welte1cbfaf52009-02-05 19:30:22 +0000643 case 'r':
644 param_disconnect = 1;
645 param_restart = 1;
646 break;
Harald Welte61e42ad2009-01-18 19:10:46 +0000647 default:
648 break;
649 }
650 }
Harald Weltefb81ae92009-02-19 02:59:17 +0000651 if (optind < argc)
652 command = argv[optind];
Harald Welte61e42ad2009-01-18 19:10:46 +0000653}
654
Harald Welte7ed1a642009-02-02 14:45:49 +0000655static int num_sigint;
656
Harald Welte14f09342009-01-29 19:28:38 +0000657static void signal_handler(int signal)
658{
Harald Weltefb81ae92009-02-19 02:59:17 +0000659 fprintf(stdout, "\nsignal %u received\n", signal);
Harald Welte14f09342009-01-29 19:28:38 +0000660
661 switch (signal) {
Harald Welte5e4d1b32009-02-01 13:36:56 +0000662 case SIGINT:
Harald Welte7ed1a642009-02-02 14:45:49 +0000663 num_sigint++;
Harald Welte14f09342009-01-29 19:28:38 +0000664 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Welte7ed1a642009-02-02 14:45:49 +0000665 if (num_sigint >= 3)
666 exit(0);
Harald Welte14f09342009-01-29 19:28:38 +0000667 break;
668 }
669}
670
Harald Welte61e42ad2009-01-18 19:10:46 +0000671int main(int argc, char **argv)
672{
673 struct gsm_network *gsmnet;
Harald Weltef186c462009-01-29 08:45:19 +0000674 int rc;
Harald Welte61e42ad2009-01-18 19:10:46 +0000675
676 handle_options(argc, argv);
677
Harald Welte03133942009-02-18 19:51:53 +0000678 gsmnet = gsm_network_init(1, 1, 1, GSM_BTS_TYPE_BS11);
Harald Welte61e42ad2009-01-18 19:10:46 +0000679 if (!gsmnet) {
680 fprintf(stderr, "Unable to allocate gsm network\n");
681 exit(1);
682 }
683 g_bts = &gsmnet->bts[0];
684
Harald Welte7ed1a642009-02-02 14:45:49 +0000685 rc = rs232_setup(serial_port, delay_ms);
686 if (rc < 0) {
687 fprintf(stderr, "Problem setting up serial port\n");
688 exit(1);
689 }
Harald Welte14f09342009-01-29 19:28:38 +0000690
Harald Welte5e4d1b32009-02-01 13:36:56 +0000691 signal(SIGINT, &signal_handler);
Harald Welte14f09342009-01-29 19:28:38 +0000692
Harald Welte61e42ad2009-01-18 19:10:46 +0000693 abis_nm_bs11_factory_logon(g_bts, 1);
Harald Welte5e4d1b32009-02-01 13:36:56 +0000694 //abis_nm_bs11_get_serno(g_bts);
Harald Welte61e42ad2009-01-18 19:10:46 +0000695
Harald Weltefb81ae92009-02-19 02:59:17 +0000696 status_timer.cb = status_timer_cb;
697
Harald Welte61e42ad2009-01-18 19:10:46 +0000698 while (1) {
Harald Welte14f09342009-01-29 19:28:38 +0000699 bsc_select_main();
Harald Welte61e42ad2009-01-18 19:10:46 +0000700 }
701
702 abis_nm_bs11_factory_logon(g_bts, 0);
703
Harald Welte61e42ad2009-01-18 19:10:46 +0000704 exit(0);
705}