blob: e05c609aecdcaafdcd3474c03996fd11123f43ed [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]);
Harald Weltef186c462009-01-29 08:45:19 +0000183 printf("length = %u\n", msg->data[1]);
Harald Welte61e42ad2009-01-18 19:10:46 +0000184
185 while (msg->len < 2 + msg->data[1]) {
Harald Weltef186c462009-01-29 08:45:19 +0000186 printf("msg->len(%u), waiting for %u\n", msg->len, 2 + msg->data[1] - msg->len);
Harald Welte61e42ad2009-01-18 19:10:46 +0000187 rc = read(serial_fd, msg->tail, 2 + msg->data[1] - msg->len);
188 if (rc < 0) {
189 perror("reading from serial port");
190 msgb_free(msg);
191 return NULL;
192 }
193 msgb_put(msg, rc);
194 }
195
196 msg->l2h = msg->data + LAPD_HDR_LEN;
197
198 fprintf(stdout, "RX: ");
199 hexdump(msg->l2h, msg->len - (msg->l2h - msg->data));
200
201 return msg;
202}
203
204static int file_is_readable(const char *fname)
205{
206 int rc;
207 struct stat st;
208
209 rc = stat(fname, &st);
210 if (rc < 0)
211 return 0;
212
213 if (S_ISREG(st.st_mode) && (st.st_mode & S_IRUSR))
214 return 1;
215
216 return 0;
217}
218
219
220static int handle_state_resp(u_int8_t state)
221{
222 printf("STATE: ");
223 switch (state) {
224 case BS11_STATE_WARM_UP:
225 printf("Warm Up...\n");
226 sleep(5);
227 break;
228 case BS11_STATE_LOAD_SMU_SAFETY:
229 printf("Load SMU Safety...\n");
230 sleep(5);
231 break;
232 case BS11_STATE_SOFTWARE_RQD:
233 printf("Software required...\n");
234 /* send safety load */
235 if (file_is_readable(fname_safety))
236 abis_nm_software_load(g_bts, fname_safety, 8);
237 else
238 fprintf(stderr, "No valid Safety Load file\n");
239 break;
240 case BS11_STATE_WAIT_MIN_CFG:
241 case BS11_STATE_WAIT_MIN_CFG_2:
242 printf("Wait minimal config...\n");
243 create_objects(g_bts, have_trx1);
244 break;
245 case BS11_STATE_MAINTENANCE:
246 printf("Maintenance...\n");
247 /* send software (FIXME: over A-bis?) */
248 if (file_is_readable(fname_software))
249 abis_nm_software_load(g_bts, fname_software, 8);
250 else
251 fprintf(stderr, "No valid Software file\n");
252 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
322int main(int argc, char **argv)
323{
324 struct gsm_network *gsmnet;
Harald Weltef186c462009-01-29 08:45:19 +0000325 struct termios tio;
326 int rc;
Harald Welte61e42ad2009-01-18 19:10:46 +0000327
328 handle_options(argc, argv);
329
330 serial_fd = open(serial_port, O_RDWR);
331 if (serial_fd < 0) {
332 perror("cannot open serial port:");
333 exit(1);
334 }
335
Harald Weltef186c462009-01-29 08:45:19 +0000336 /* set baudrate */
337 rc = tcgetattr(serial_fd, &tio);
338 if (rc < 0) {
339 perror("tcgetattr()");
340 exit(1);
341 }
342 cfsetispeed(&tio, B19200);
343 cfsetospeed(&tio, B19200);
344 tio.c_cflag |= CREAD | CLOCAL;
345 rc = tcsetattr(serial_fd, TCSADRAIN, &tio);
346 if (rc < 0) {
347 perror("tcsetattr()");
348 exit(1);
349 }
Harald Welte61e42ad2009-01-18 19:10:46 +0000350
351 gsmnet = gsm_network_init(1, 1, 1);
352 if (!gsmnet) {
353 fprintf(stderr, "Unable to allocate gsm network\n");
354 exit(1);
355 }
356 g_bts = &gsmnet->bts[0];
357
358 abis_nm_bs11_factory_logon(g_bts, 1);
359
360 while (1) {
361 struct msgb *rx_msg;
362 struct abis_om_hdr *oh;
363 struct abis_om_fom_hdr *foh;
Harald Weltef186c462009-01-29 08:45:19 +0000364 rc = -1;
Harald Welte61e42ad2009-01-18 19:10:46 +0000365
366 rx_msg = serial_read_msg();
367
368 oh = (struct abis_om_hdr *) msgb_l2(rx_msg);
369 foh = (struct abis_om_fom_hdr *) oh->data;
370 switch (foh->msg_type) {
371 case NM_MT_BS11_GET_STATE_ACK:
372 rc = handle_state_resp(foh->data[2]);
373 break;
374 default:
375 rc = abis_nm_rcvmsg(rx_msg);
376 }
377 if (rc < 0) {
378 perror("in main loop");
379 break;
380 }
381 if (rc == 1)
382 break;
383 }
384
385 abis_nm_bs11_factory_logon(g_bts, 0);
386
387 close(serial_fd);
388 exit(0);
389}