blob: 9fadc45c9f12b7476ed1c7749eb02fe4c4edb8db [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
95 abis_nm_bs11_conn_oml(bts, 0, 1, 0xff);
96 abis_nm_bs11_set_oml_tei(bts, TEI_OML);
97
Harald Welte5e4d1b32009-02-01 13:36:56 +000098 abis_nm_bs11_set_trx_power(&bts->trx[0], BS11_TRX_POWER_GSM_30mW);
Harald Welte61e42ad2009-01-18 19:10:46 +000099
Harald Welte1cbfaf52009-02-05 19:30:22 +0000100 sleep(1);
101
Harald Weltefb81ae92009-02-19 02:59:17 +0000102 abis_nm_bs11_set_trx1_pw(bts, trx1_password);
103
104 sleep(1);
105
106 return 0;
107}
108
109static int create_trx1(struct gsm_bts *bts)
110{
111 u_int8_t bbsig1_attr[sizeof(obj_bbsig0_attr)+12];
112 u_int8_t *cur = bbsig1_attr;
113
Harald Welte1cbfaf52009-02-05 19:30:22 +0000114 fprintf(stdout, "Crating Objects for TRX1\n");
115
116 abis_nm_bs11_set_trx1_pw(bts, trx1_password);
117
118 sleep(1);
Harald Weltefb81ae92009-02-19 02:59:17 +0000119
Harald Welte1cbfaf52009-02-05 19:30:22 +0000120 cur = tlv_put(cur, NM_ATT_BS11_PASSWORD, 10,
121 (u_int8_t *)trx1_password);
122 memcpy(cur, obj_bbsig0_attr, sizeof(obj_bbsig0_attr));
123 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 1,
124 sizeof(bbsig1_attr), bbsig1_attr);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000125 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 1,
126 sizeof(obj_pa0_attr), obj_pa0_attr);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000127 abis_nm_bs11_set_trx_power(&bts->trx[1], BS11_TRX_POWER_GSM_30mW);
Harald Weltefb81ae92009-02-19 02:59:17 +0000128
Harald Welte61e42ad2009-01-18 19:10:46 +0000129 return 0;
130}
131
132static char *serial_port = "/dev/ttyUSB0";
133static char *fname_safety = "BTSBMC76.SWI";
134static char *fname_software = "HS011106.SWL";
Harald Welte623d5312009-01-29 21:14:05 +0000135static int delay_ms = 0;
Harald Welte623d5312009-01-29 21:14:05 +0000136static int win_size = 8;
Harald Welte3ffd1372009-02-01 22:15:49 +0000137static int param_disconnect = 0;
Harald Welte1cbfaf52009-02-05 19:30:22 +0000138static int param_restart = 0;
Harald Welte3ffd1372009-02-01 22:15:49 +0000139static int param_forced = 0;
Harald Welte61e42ad2009-01-18 19:10:46 +0000140static struct gsm_bts *g_bts;
141
Harald Welte61e42ad2009-01-18 19:10:46 +0000142static int file_is_readable(const char *fname)
143{
144 int rc;
145 struct stat st;
146
147 rc = stat(fname, &st);
148 if (rc < 0)
149 return 0;
150
151 if (S_ISREG(st.st_mode) && (st.st_mode & S_IRUSR))
152 return 1;
153
154 return 0;
155}
156
Harald Welte3ffd1372009-02-01 22:15:49 +0000157static int percent;
158static int percent_old;
159
Harald Welte5e4d1b32009-02-01 13:36:56 +0000160/* callback function passed to the ABIS OML code */
161static int swload_cbfn(unsigned int hook, unsigned int event, struct msgb *msg,
162 void *data, void *param)
163{
164 if (hook != GSM_HOOK_NM_SWLOAD)
165 return 0;
Harald Welte61e42ad2009-01-18 19:10:46 +0000166
Harald Welte5e4d1b32009-02-01 13:36:56 +0000167 switch (event) {
168 case NM_MT_LOAD_INIT_ACK:
169 fprintf(stdout, "Software Load Initiate ACK\n");
170 break;
171 case NM_MT_LOAD_INIT_NACK:
172 fprintf(stderr, "ERROR: Software Load Initiate NACK\n");
173 exit(5);
174 break;
175 case NM_MT_LOAD_END_ACK:
Harald Welte1cbfaf52009-02-05 19:30:22 +0000176 if (data) {
Harald Welte03133942009-02-18 19:51:53 +0000177 /* we did a safety load and must activate it */
Harald Welte5e4d1b32009-02-01 13:36:56 +0000178 abis_nm_software_activate(g_bts, fname_safety,
179 swload_cbfn, g_bts);
Harald Welte03133942009-02-18 19:51:53 +0000180 sleep(5);
Harald Welte1cbfaf52009-02-05 19:30:22 +0000181 }
Harald Welte5e4d1b32009-02-01 13:36:56 +0000182 break;
183 case NM_MT_LOAD_END_NACK:
184 fprintf(stderr, "ERROR: Software Load End NACK\n");
185 exit(3);
186 break;
187 case NM_MT_ACTIVATE_SW_NACK:
188 fprintf(stderr, "ERROR: Activate Software NACK\n");
189 exit(4);
190 break;
191 case NM_MT_ACTIVATE_SW_ACK:
192 bs11cfg_state = STATE_NONE;
193
194 break;
Harald Welte3ffd1372009-02-01 22:15:49 +0000195 case NM_MT_LOAD_SEG_ACK:
196 percent = abis_nm_software_load_status(g_bts);
197 if (percent > percent_old)
198 printf("Software Download Progress: %d%%\n", percent);
199 percent_old = percent;
200 break;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000201 }
202 return 0;
203}
204
Harald Welte268bb402009-02-01 19:11:56 +0000205static const char *bs11_link_state[] = {
206 [0x00] = "Down",
207 [0x01] = "Up",
208 [0x02] = "Restoring",
209};
210
211static const char *linkstate_name(u_int8_t linkstate)
Harald Welte61e42ad2009-01-18 19:10:46 +0000212{
Harald Welte268bb402009-02-01 19:11:56 +0000213 if (linkstate > ARRAY_SIZE(bs11_link_state))
214 return "Unknown";
Harald Welte3b8ba212009-01-29 12:27:58 +0000215
Harald Welte268bb402009-02-01 19:11:56 +0000216 return bs11_link_state[linkstate];
217}
Harald Welte3b8ba212009-01-29 12:27:58 +0000218
Harald Welte268bb402009-02-01 19:11:56 +0000219static const char *mbccu_load[] = {
220 [0] = "No Load",
221 [1] = "Load BTSCAC",
222 [2] = "Load BTSDRX",
223 [3] = "Load BTSBBX",
224 [4] = "Load BTSARC",
225 [5] = "Load",
226};
227
228static const char *mbccu_load_name(u_int8_t linkstate)
229{
230 if (linkstate > ARRAY_SIZE(mbccu_load))
231 return "Unknown";
232
233 return mbccu_load[linkstate];
234}
235
Harald Welte03133942009-02-18 19:51:53 +0000236static const char *bts_phase_name(u_int8_t phase)
Harald Welte268bb402009-02-01 19:11:56 +0000237{
Harald Welte268bb402009-02-01 19:11:56 +0000238 switch (phase) {
Harald Welte61e42ad2009-01-18 19:10:46 +0000239 case BS11_STATE_WARM_UP:
Harald Weltec12d52b2009-02-01 21:39:06 +0000240 case BS11_STATE_WARM_UP_2:
Harald Welte03133942009-02-18 19:51:53 +0000241 return "Warm Up";
Harald Welte61e42ad2009-01-18 19:10:46 +0000242 break;
243 case BS11_STATE_LOAD_SMU_SAFETY:
Harald Welte03133942009-02-18 19:51:53 +0000244 return "Load SMU Safety";
Harald Welte268bb402009-02-01 19:11:56 +0000245 break;
246 case BS11_STATE_LOAD_SMU_INTENDED:
Harald Welte03133942009-02-18 19:51:53 +0000247 return "Load SMU Intended";
Harald Welte268bb402009-02-01 19:11:56 +0000248 break;
249 case BS11_STATE_LOAD_MBCCU:
Harald Welte03133942009-02-18 19:51:53 +0000250 return "Load MBCCU";
Harald Welte61e42ad2009-01-18 19:10:46 +0000251 break;
252 case BS11_STATE_SOFTWARE_RQD:
Harald Welte03133942009-02-18 19:51:53 +0000253 return "Software required";
Harald Welte268bb402009-02-01 19:11:56 +0000254 break;
255 case BS11_STATE_WAIT_MIN_CFG:
256 case BS11_STATE_WAIT_MIN_CFG_2:
Harald Welte03133942009-02-18 19:51:53 +0000257 return "Wait minimal config";
Harald Welte268bb402009-02-01 19:11:56 +0000258 break;
259 case BS11_STATE_MAINTENANCE:
Harald Welte03133942009-02-18 19:51:53 +0000260 return "Maintenance";
Harald Welte268bb402009-02-01 19:11:56 +0000261 break;
262 case BS11_STATE_NORMAL:
Harald Welte03133942009-02-18 19:51:53 +0000263 return "Normal";
264 break;
265 case BS11_STATE_ABIS_LOAD:
266 return "Abis load";
Harald Welte268bb402009-02-01 19:11:56 +0000267 break;
268 default:
Harald Welte03133942009-02-18 19:51:53 +0000269 return "Unknown";
Harald Welte268bb402009-02-01 19:11:56 +0000270 break;
271 }
272}
273
Harald Weltefb81ae92009-02-19 02:59:17 +0000274static const char *trx_power_name(u_int8_t pwr)
275{
276 switch (pwr) {
277 case BS11_TRX_POWER_GSM_2W:
278 return "2W (GSM)";
279 case BS11_TRX_POWER_GSM_250mW:
280 return "250mW (GSM)";
281 case BS11_TRX_POWER_GSM_80mW:
282 return "80mW (GSM)";
283 case BS11_TRX_POWER_GSM_30mW:
284 return "30mW (GSM)";
285 case BS11_TRX_POWER_DCS_3W:
286 return "3W (DCS)";
287 case BS11_TRX_POWER_DCS_1W6:
288 return "1.6W (DCS)";
289 case BS11_TRX_POWER_DCS_500mW:
290 return "500mW (DCS)";
291 case BS11_TRX_POWER_DCS_160mW:
292 return "160mW (DCS)";
293 default:
294 return "unknown value";
295 }
296}
297
298static const char *obj_name(struct abis_om_fom_hdr *foh)
299{
300 static char retbuf[256];
301
302 retbuf[0] = 0;
303
304 switch (foh->obj_class) {
305 case NM_OC_BS11:
306 strcat(retbuf, "BS11 ");
307 switch (foh->obj_inst.bts_nr) {
308 case BS11_OBJ_PA:
309 sprintf(retbuf+strlen(retbuf), "Power Amplifier %d ",
310 foh->obj_inst.ts_nr);
311 break;
312 }
313 break;
314 case NM_OC_SITE_MANAGER:
315 strcat(retbuf, "SITE MANAGER ");
316 break;
317 }
318 return retbuf;
319}
320
Harald Welte03133942009-02-18 19:51:53 +0000321static void print_state(struct tlv_parsed *tp)
322{
323 if (TLVP_PRESENT(tp, NM_ATT_BS11_BTS_STATE)) {
324 u_int8_t phase, mbccu;
325 if (TLVP_LEN(tp, NM_ATT_BS11_BTS_STATE) >= 1) {
326 phase = *TLVP_VAL(tp, NM_ATT_BS11_BTS_STATE);
327 printf("PHASE: %u %-20s ", phase & 0xf,
328 bts_phase_name(phase));
329 }
330 if (TLVP_LEN(tp, NM_ATT_BS11_BTS_STATE) >= 2) {
331 mbccu = *(TLVP_VAL(tp, NM_ATT_BS11_BTS_STATE)+1);
332 printf("MBCCU0: %-11s MBCCU1: %-11s ",
333 mbccu_load_name(mbccu & 0xf), mbccu_load_name(mbccu >> 4));
334 }
335 }
336 if (TLVP_PRESENT(tp, NM_ATT_BS11_E1_STATE) &&
337 TLVP_LEN(tp, NM_ATT_BS11_E1_STATE) >= 1) {
338 u_int8_t e1_state = *TLVP_VAL(tp, NM_ATT_BS11_E1_STATE);
339 printf("Abis-link: %-9s ", linkstate_name(e1_state & 0xf));
340 }
341 printf("\n");
342}
343
Harald Weltefb81ae92009-02-19 02:59:17 +0000344static int print_attr(struct tlv_parsed *tp)
345{
346 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_PCB_SERIAL)) {
347 printf("\tBS-11 ESN PCB Serial Number: %s\n",
348 TLVP_VAL(tp, NM_ATT_BS11_ESN_PCB_SERIAL));
349 }
350 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_HW_CODE_NO)) {
351 printf("\tBS-11 ESN Hardware Code Number: %s\n",
352 TLVP_VAL(tp, NM_ATT_BS11_ESN_HW_CODE_NO)+6);
353 }
354 if (TLVP_PRESENT(tp, NM_ATT_BS11_ESN_FW_CODE_NO)) {
355 printf("\tBS-11 ESN Firmware Code Number: %s\n",
356 TLVP_VAL(tp, NM_ATT_BS11_ESN_FW_CODE_NO)+6);
357 }
358#if 0
359 if (TLVP_PRESENT(tp, NM_ATT_BS11_BOOT_SW_VERS)) {
360 printf("BS-11 Boot Software Version: %s\n",
361 TLVP_VAL(tp, NM_ATT_BS11_BOOT_SW_VERS)+6);
362 }
363#endif
364 if (TLVP_PRESENT(tp, NM_ATT_ABIS_CHANNEL) &&
365 TLVP_LEN(tp, NM_ATT_ABIS_CHANNEL) >= 3) {
Harald Welte099d6102009-02-21 12:59:58 +0000366 struct abis_nm_channel *chan =
367 (struct abis_nm_channel*) TLVP_VAL(tp, NM_ATT_ABIS_CHANNEL)-1;
Harald Weltefb81ae92009-02-19 02:59:17 +0000368 printf("\tE1 Channel: Port=%u Timeslot=%u ",
369 chan->bts_port, chan->timeslot);
370 if (chan->subslot == 0xff)
371 printf("(Full Slot)\n");
372 else
373 printf("Subslot=%u\n", chan->subslot);
374 }
375 if (TLVP_PRESENT(tp, NM_ATT_TEI))
376 printf("\tTEI: %d\n", *TLVP_VAL(tp, NM_ATT_TEI));
377 if (TLVP_PRESENT(tp, NM_ATT_BS11_TXPWR) &&
378 TLVP_LEN(tp, NM_ATT_BS11_TXPWR) >= 1) {
379 printf("\tTRX Power: %s\n",
380 trx_power_name(*TLVP_VAL(tp, NM_ATT_BS11_TXPWR)));
381 }
382
383 return 0;
384}
385
Harald Welte268bb402009-02-01 19:11:56 +0000386/* handle a response from the BTS to a GET STATE command */
387static int handle_state_resp(enum abis_bs11_phase state)
388{
389 int rc = 0;
390
Harald Welte268bb402009-02-01 19:11:56 +0000391 switch (state) {
392 case BS11_STATE_WARM_UP:
Harald Welte268bb402009-02-01 19:11:56 +0000393 case BS11_STATE_LOAD_SMU_SAFETY:
Harald Welte268bb402009-02-01 19:11:56 +0000394 case BS11_STATE_LOAD_SMU_INTENDED:
Harald Welte268bb402009-02-01 19:11:56 +0000395 case BS11_STATE_LOAD_MBCCU:
396 break;
397 case BS11_STATE_SOFTWARE_RQD:
Harald Welte3b8ba212009-01-29 12:27:58 +0000398 bs11cfg_state = STATE_SWLOAD;
Harald Welte5e4d1b32009-02-01 13:36:56 +0000399 /* send safety load. Use g_bts as private 'param'
400 * argument, so our swload_cbfn can distinguish
401 * a safety load from a regular software */
Harald Welte61e42ad2009-01-18 19:10:46 +0000402 if (file_is_readable(fname_safety))
Harald Welte623d5312009-01-29 21:14:05 +0000403 rc = abis_nm_software_load(g_bts, fname_safety,
Harald Welte3ffd1372009-02-01 22:15:49 +0000404 win_size, param_forced,
405 swload_cbfn, g_bts);
Harald Welte61e42ad2009-01-18 19:10:46 +0000406 else
Harald Welte071f34d2009-01-29 09:24:38 +0000407 fprintf(stderr, "No valid Safety Load file \"%s\"\n",
408 fname_safety);
Harald Welte61e42ad2009-01-18 19:10:46 +0000409 break;
410 case BS11_STATE_WAIT_MIN_CFG:
411 case BS11_STATE_WAIT_MIN_CFG_2:
Harald Welte3b8ba212009-01-29 12:27:58 +0000412 bs11cfg_state = STATE_SWLOAD;
Harald Welte3ffd1372009-02-01 22:15:49 +0000413 rc = create_objects(g_bts);
Harald Welte61e42ad2009-01-18 19:10:46 +0000414 break;
415 case BS11_STATE_MAINTENANCE:
Harald Weltefb81ae92009-02-19 02:59:17 +0000416 if (command) {
417 if (!strcmp(command, "disconnect"))
Harald Weltef80b7d32009-02-19 03:11:13 +0000418 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000419 else if (!strcmp(command, "reconnect"))
420 rc = abis_nm_bs11_bsc_disconnect(g_bts, 1);
421 else if (!strcmp(command, "software")
422 && bs11cfg_state != STATE_SWLOAD) {
423 bs11cfg_state = STATE_SWLOAD;
424 /* send software (FIXME: over A-bis?) */
425 if (file_is_readable(fname_software))
426 rc = abis_nm_bs11_load_swl(g_bts, fname_software,
427 win_size, param_forced,
428 swload_cbfn);
429 else
430 fprintf(stderr, "No valid Software file \"%s\"\n",
431 fname_software);
432 } else if (!strcmp(command, "delete-trx1")) {
433 abis_nm_bs11_delete_object(g_bts, BS11_OBJ_BBSIG, 1);
434 abis_nm_bs11_delete_object(g_bts, BS11_OBJ_PA, 1);
Harald Weltef80b7d32009-02-19 03:11:13 +0000435 sleep(5);
436 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000437 command = NULL;
438 } else if (!strcmp(command, "create-trx1")) {
439 create_trx1(g_bts);
Harald Weltef80b7d32009-02-19 03:11:13 +0000440 sleep(5);
441 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000442 command = NULL;
443 } else if (!strcmp(command, "restart")) {
444 abis_nm_bs11_restart(g_bts);
445 command = NULL;
446 }
Harald Welte3ffd1372009-02-01 22:15:49 +0000447 }
Harald Welte61e42ad2009-01-18 19:10:46 +0000448 break;
449 case BS11_STATE_NORMAL:
Harald Weltefb81ae92009-02-19 02:59:17 +0000450 if (command) {
451 if (!strcmp(command, "reconnect"))
Harald Weltef80b7d32009-02-19 03:11:13 +0000452 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000453 else if (!strcmp(command, "disconnect"))
454 abis_nm_bs11_bsc_disconnect(g_bts, 0);
455 else if (!strcmp(command, "query")) {
456 bs11cfg_state = STATE_QUERY;
457 abis_nm_bs11_get_serno(g_bts);
458 abis_nm_bs11_get_oml_tei_ts(g_bts);
459 abis_nm_bs11_get_trx_power(&g_bts->trx[0]);
460 abis_nm_bs11_get_trx_power(&g_bts->trx[1]);
Harald Weltef80b7d32009-02-19 03:11:13 +0000461 sleep(5);
462 abis_nm_bs11_factory_logon(g_bts, 0);
Harald Weltefb81ae92009-02-19 02:59:17 +0000463 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}