blob: 0be7f7baecaf9aacb9ee355a64f7f2559409ad46 [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 Weltef186c462009-01-29 08:45:19 +000032#include <termios.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>
42
43static const u_int8_t obj_li_attr[] = {
44 0xa0, 0x09, 0x00,
45 0xab, 0x00,
46 0xac, 0x00,
47};
48static const u_int8_t obj_bbsig0_attr[] = {
49 0x3d, 0x02, 0x00, 0x00,
50 0x3f, 0x01, 0x00,
51};
52static const u_int8_t obj_pa0_attr[] = {
53 NM_ATT_BS11_TXPWR, 0x01, BS11_TRX_POWER_30mW,
54};
55static const char *trx1_password = "1111111111";
56#define TEI_OML 25
57
58/* create all objects for an initial configuration */
59static int create_objects(struct gsm_bts *bts, int trx1)
60{
Harald Weltecfe6dce2009-01-19 16:06:43 +000061 //abis_nm_bs11_factory_logon(bts, 1);
Harald Welte61e42ad2009-01-18 19:10:46 +000062 abis_nm_bs11_create_object(bts, BS11_OBJ_LI, 0, sizeof(obj_li_attr),
63 obj_li_attr);
64 abis_nm_bs11_create_object(bts, BS11_OBJ_GPSU, 0, 0, NULL);
65 abis_nm_bs11_create_object(bts, BS11_OBJ_ALCO, 0, 0, NULL);
66 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 0,
67 sizeof(obj_bbsig0_attr), obj_bbsig0_attr);
68 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 0,
69 sizeof(obj_pa0_attr), obj_pa0_attr);
70 if (trx1) {
71 u_int8_t bbsig1_attr[sizeof(obj_bbsig0_attr)+12];
72 u_int8_t *cur = bbsig1_attr;
73
74 abis_nm_bs11_set_trx1_pw(bts, trx1_password);
75
76 cur = tlv_put(cur, NM_ATT_BS11_PASSWORD, 10,
77 (u_int8_t *)trx1_password);
78 memcpy(cur, obj_bbsig0_attr, sizeof(obj_bbsig0_attr));
79 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 1,
80 sizeof(bbsig1_attr), bbsig1_attr);
81
82 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 1,
83 sizeof(obj_pa0_attr), obj_pa0_attr);
84 }
85
86 abis_nm_bs11_create_envaBTSE(bts, 0);
87 abis_nm_bs11_create_envaBTSE(bts, 1);
88 abis_nm_bs11_create_envaBTSE(bts, 2);
89 abis_nm_bs11_create_envaBTSE(bts, 3);
90
91 abis_nm_bs11_conn_oml(bts, 0, 1, 0xff);
92 abis_nm_bs11_set_oml_tei(bts, TEI_OML);
93
94 abis_nm_bs11_set_trx_power(&bts->trx[0], BS11_TRX_POWER_30mW);
95
96 if (trx1)
97 abis_nm_bs11_set_trx_power(&bts->trx[1], BS11_TRX_POWER_30mW);
98
Harald Weltecfe6dce2009-01-19 16:06:43 +000099 //abis_nm_bs11_factory_logon(bts, 0);
Harald Welte61e42ad2009-01-18 19:10:46 +0000100
101 return 0;
102}
103
104static char *serial_port = "/dev/ttyUSB0";
105static char *fname_safety = "BTSBMC76.SWI";
106static char *fname_software = "HS011106.SWL";
107static int serial_fd = -1;
108static int have_trx1 = 0;
109static struct gsm_bts *g_bts;
110
111/* adaption layer from GSM 08.59 + 12.21 to RS232 */
112
113#define LAPD_HDR_LEN 10
114
115/* callback from abis_nm */
116int _abis_nm_sendmsg(struct msgb *msg)
117{
118 int written;
119 u_int8_t *lapd;
120
121 msg->l2h = msg->data;
122
123 /* prepend LAPD header */
124 lapd = msgb_push(msg, LAPD_HDR_LEN);
125
126 lapd[0] = 0x00;
127 lapd[1] = msg->len - 2; /* length of bytes startign at lapd[2] */
128 lapd[2] = 0x00;
129 lapd[3] = 0x07;
130 lapd[4] = 0x01;
131 lapd[5] = 0x3e;
132 lapd[6] = 0x00;
133 lapd[7] = 0x00;
134 lapd[8] = msg->len - 10; /* length of bytes starting at lapd[10] */
135 lapd[9] = lapd[8] ^ 0x38;
136
137 fprintf(stdout, "TX: ");
138 hexdump(msg->data, msg->len);
139
140 /* send over serial line */
141 written = write(serial_fd, msg->data, msg->len);
142 if (written < msg->len) {
143 perror("short write:");
144 msgb_free(msg);
145 return -1;
146 }
147
148 msgb_free(msg);
149
150 return 0;
151}
152
153#define SERIAL_ALLOC_SIZE 300
154
155/* receive an entire message from the serial port */
156static struct msgb *serial_read_msg(void)
157{
158 struct msgb *msg = msgb_alloc(SERIAL_ALLOC_SIZE);
159 int rc;
160
161 if (!msg)
162 return NULL;
163
164 /* first read two byes to obtain length */
165 while (msg->len < 2) {
166 rc = read(serial_fd, msg->tail, 2 - msg->len);
167 if (rc < 0) {
168 perror("reading from serial port");
169 msgb_free(msg);
170 return NULL;
171 }
172 msgb_put(msg, rc);
173 }
174 if (msg->data[0] != 0)
175 fprintf(stderr, "Invalid header byte 0: 0x%02x\n",
176 msg->data[0]);
177
178 /* second byte is LAPD payload length */
179 if (msg->data[1] < LAPD_HDR_LEN + sizeof(struct abis_om_fom_hdr) +
180 sizeof(struct abis_om_hdr))
181 fprintf(stderr, "Invalied header byte 1(len): %u\n",
182 msg->data[1]);
183
184 while (msg->len < 2 + msg->data[1]) {
185 rc = read(serial_fd, msg->tail, 2 + msg->data[1] - msg->len);
186 if (rc < 0) {
187 perror("reading from serial port");
188 msgb_free(msg);
189 return NULL;
190 }
191 msgb_put(msg, rc);
192 }
193
194 msg->l2h = msg->data + LAPD_HDR_LEN;
195
196 fprintf(stdout, "RX: ");
197 hexdump(msg->l2h, msg->len - (msg->l2h - msg->data));
198
199 return msg;
200}
201
202static int file_is_readable(const char *fname)
203{
204 int rc;
205 struct stat st;
206
207 rc = stat(fname, &st);
208 if (rc < 0)
209 return 0;
210
211 if (S_ISREG(st.st_mode) && (st.st_mode & S_IRUSR))
212 return 1;
213
214 return 0;
215}
216
217
218static int handle_state_resp(u_int8_t state)
219{
220 printf("STATE: ");
221 switch (state) {
222 case BS11_STATE_WARM_UP:
223 printf("Warm Up...\n");
224 sleep(5);
225 break;
226 case BS11_STATE_LOAD_SMU_SAFETY:
227 printf("Load SMU Safety...\n");
228 sleep(5);
229 break;
230 case BS11_STATE_SOFTWARE_RQD:
231 printf("Software required...\n");
232 /* send safety load */
233 if (file_is_readable(fname_safety))
234 abis_nm_software_load(g_bts, fname_safety, 8);
235 else
Harald Welte071f34d2009-01-29 09:24:38 +0000236 fprintf(stderr, "No valid Safety Load file \"%s\"\n",
237 fname_safety);
Harald Welte61e42ad2009-01-18 19:10:46 +0000238 break;
239 case BS11_STATE_WAIT_MIN_CFG:
240 case BS11_STATE_WAIT_MIN_CFG_2:
241 printf("Wait minimal config...\n");
242 create_objects(g_bts, have_trx1);
243 break;
244 case BS11_STATE_MAINTENANCE:
245 printf("Maintenance...\n");
246 /* send software (FIXME: over A-bis?) */
247 if (file_is_readable(fname_software))
248 abis_nm_software_load(g_bts, fname_software, 8);
249 else
Harald Welte071f34d2009-01-29 09:24:38 +0000250 fprintf(stderr, "No valid Software file \"%s\"\n",
251 fname_software);
Harald Welte61e42ad2009-01-18 19:10:46 +0000252 break;
253 case BS11_STATE_NORMAL:
254 printf("Normal...\n");
255 return 1;
256 default:
257 printf("Unknown state 0x%02u\n", state);
258 sleep(5);
259 break;
260 }
261 return 0;
262}
263
264static void print_banner(void)
265{
266 printf("bs11_config (C) 2009 by Harald Welte and Dieter Spaar\n");
267 printf("THIS SOFTWARE IS FREE SOFTWARE WIH NO WARRANTY\n\n");
268}
269
270static void print_help(void)
271{
272 printf("Supported arguments:\n");
273 printf("\t--help\t\t\t-h\tPrint this help text\n");
274 printf("\t--port /dev/ttyXXX\t-p\tSpecify serial port\n");
275 printf("\t--with-trx1\t\t-t\tAssume the BS-11 has 2 TRX\n");
276 printf("\t--software file\t\t-s\tSpecify Software file\n");
277 printf("\t--safety file\t\t-s\tSpecify Safety Load file\n");
278}
279
280static void handle_options(int argc, char **argv)
281{
282 print_banner();
283
284 while (1) {
285 int option_index = 0, c;
286 static struct option long_options[] = {
287 { "help", 0, 0, 'h' },
288 { "port", 1, 0, 'p' },
289 { "with-trx1", 0, 0, 't' },
290 { "software", 1, 0, 's' },
291 { "safety", 1, 0, 'S' },
292 };
293
294 c = getopt_long(argc, argv, "hp:s:S:t",
295 long_options, &option_index);
296
297 if (c == -1)
298 break;
299
300 switch (c) {
301 case 'h':
302 print_help();
303 exit(0);
304 case 'p':
305 serial_port = optarg;
306 break;
307 case 't':
308 have_trx1 = 1;
309 break;
310 case 's':
311 fname_software = optarg;
312 break;
313 case 'S':
314 fname_safety = optarg;
315 break;
316 default:
317 break;
318 }
319 }
320}
321
Harald Welte071f34d2009-01-29 09:24:38 +0000322enum bs11_state {
323 STATE_NONE,
324 STATE_LOGON_WAIT,
325 STATE_LOGON_ACK,
326 STATE_SWLOAD,
327};
328
Harald Welte61e42ad2009-01-18 19:10:46 +0000329int main(int argc, char **argv)
330{
331 struct gsm_network *gsmnet;
Harald Weltef186c462009-01-29 08:45:19 +0000332 struct termios tio;
Harald Welte071f34d2009-01-29 09:24:38 +0000333 enum bs11_state state = STATE_NONE;
Harald Weltef186c462009-01-29 08:45:19 +0000334 int rc;
Harald Welte61e42ad2009-01-18 19:10:46 +0000335
336 handle_options(argc, argv);
337
338 serial_fd = open(serial_port, O_RDWR);
339 if (serial_fd < 0) {
340 perror("cannot open serial port:");
341 exit(1);
342 }
343
Harald Weltef186c462009-01-29 08:45:19 +0000344 /* set baudrate */
345 rc = tcgetattr(serial_fd, &tio);
346 if (rc < 0) {
347 perror("tcgetattr()");
348 exit(1);
349 }
350 cfsetispeed(&tio, B19200);
351 cfsetospeed(&tio, B19200);
Harald Welte071f34d2009-01-29 09:24:38 +0000352 tio.c_cflag |= (CREAD | CLOCAL | CS8);
353 tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
354 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
355 tio.c_iflag |= (INPCK | ISTRIP);
356 tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR);
Harald Weltef186c462009-01-29 08:45:19 +0000357 rc = tcsetattr(serial_fd, TCSADRAIN, &tio);
358 if (rc < 0) {
359 perror("tcsetattr()");
360 exit(1);
361 }
Harald Welte61e42ad2009-01-18 19:10:46 +0000362
363 gsmnet = gsm_network_init(1, 1, 1);
364 if (!gsmnet) {
365 fprintf(stderr, "Unable to allocate gsm network\n");
366 exit(1);
367 }
368 g_bts = &gsmnet->bts[0];
369
370 abis_nm_bs11_factory_logon(g_bts, 1);
371
372 while (1) {
373 struct msgb *rx_msg;
374 struct abis_om_hdr *oh;
375 struct abis_om_fom_hdr *foh;
Harald Weltef186c462009-01-29 08:45:19 +0000376 rc = -1;
Harald Welte61e42ad2009-01-18 19:10:46 +0000377
378 rx_msg = serial_read_msg();
379
380 oh = (struct abis_om_hdr *) msgb_l2(rx_msg);
381 foh = (struct abis_om_fom_hdr *) oh->data;
382 switch (foh->msg_type) {
Harald Welte071f34d2009-01-29 09:24:38 +0000383 case NM_MT_BS11_FACTORY_LOGON_ACK:
384 printf("FACTORY LOGON: ACK\n");
385 if (state == STATE_NONE)
386 state = STATE_LOGON_ACK;
387 rc = 0;
388 break;
Harald Welte61e42ad2009-01-18 19:10:46 +0000389 case NM_MT_BS11_GET_STATE_ACK:
390 rc = handle_state_resp(foh->data[2]);
391 break;
392 default:
393 rc = abis_nm_rcvmsg(rx_msg);
394 }
395 if (rc < 0) {
396 perror("in main loop");
397 break;
398 }
399 if (rc == 1)
400 break;
Harald Welte071f34d2009-01-29 09:24:38 +0000401
402 switch (state) {
403 case STATE_NONE:
404 abis_nm_bs11_factory_logon(g_bts, 1);
405 break;
406 case STATE_LOGON_ACK:
407 abis_nm_bs11_get_state(g_bts);
408 break;
409 default:
410 break;
411 }
Harald Welte61e42ad2009-01-18 19:10:46 +0000412 }
413
414 abis_nm_bs11_factory_logon(g_bts, 0);
415
416 close(serial_fd);
417 exit(0);
418}