blob: 1219185aa648de00bcc208b1c3ca6e64cef76d82 [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>
32
33#include <sys/types.h>
34#include <sys/stat.h>
35
36#include <openbsc/gsm_data.h>
37#include <openbsc/abis_nm.h>
38#include <openbsc/msgb.h>
39#include <openbsc/tlv.h>
40#include <openbsc/debug.h>
41
42static const u_int8_t obj_li_attr[] = {
43 0xa0, 0x09, 0x00,
44 0xab, 0x00,
45 0xac, 0x00,
46};
47static const u_int8_t obj_bbsig0_attr[] = {
48 0x3d, 0x02, 0x00, 0x00,
49 0x3f, 0x01, 0x00,
50};
51static const u_int8_t obj_pa0_attr[] = {
52 NM_ATT_BS11_TXPWR, 0x01, BS11_TRX_POWER_30mW,
53};
54static const char *trx1_password = "1111111111";
55#define TEI_OML 25
56
57/* create all objects for an initial configuration */
58static int create_objects(struct gsm_bts *bts, int trx1)
59{
60 abis_nm_bs11_factory_logon(bts, 1);
61 abis_nm_bs11_create_object(bts, BS11_OBJ_LI, 0, sizeof(obj_li_attr),
62 obj_li_attr);
63 abis_nm_bs11_create_object(bts, BS11_OBJ_GPSU, 0, 0, NULL);
64 abis_nm_bs11_create_object(bts, BS11_OBJ_ALCO, 0, 0, NULL);
65 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 0,
66 sizeof(obj_bbsig0_attr), obj_bbsig0_attr);
67 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 0,
68 sizeof(obj_pa0_attr), obj_pa0_attr);
69 if (trx1) {
70 u_int8_t bbsig1_attr[sizeof(obj_bbsig0_attr)+12];
71 u_int8_t *cur = bbsig1_attr;
72
73 abis_nm_bs11_set_trx1_pw(bts, trx1_password);
74
75 cur = tlv_put(cur, NM_ATT_BS11_PASSWORD, 10,
76 (u_int8_t *)trx1_password);
77 memcpy(cur, obj_bbsig0_attr, sizeof(obj_bbsig0_attr));
78 abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 1,
79 sizeof(bbsig1_attr), bbsig1_attr);
80
81 abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 1,
82 sizeof(obj_pa0_attr), obj_pa0_attr);
83 }
84
85 abis_nm_bs11_create_envaBTSE(bts, 0);
86 abis_nm_bs11_create_envaBTSE(bts, 1);
87 abis_nm_bs11_create_envaBTSE(bts, 2);
88 abis_nm_bs11_create_envaBTSE(bts, 3);
89
90 abis_nm_bs11_conn_oml(bts, 0, 1, 0xff);
91 abis_nm_bs11_set_oml_tei(bts, TEI_OML);
92
93 abis_nm_bs11_set_trx_power(&bts->trx[0], BS11_TRX_POWER_30mW);
94
95 if (trx1)
96 abis_nm_bs11_set_trx_power(&bts->trx[1], BS11_TRX_POWER_30mW);
97
98 abis_nm_bs11_factory_logon(bts, 0);
99
100 return 0;
101}
102
103static char *serial_port = "/dev/ttyUSB0";
104static char *fname_safety = "BTSBMC76.SWI";
105static char *fname_software = "HS011106.SWL";
106static int serial_fd = -1;
107static int have_trx1 = 0;
108static struct gsm_bts *g_bts;
109
110/* adaption layer from GSM 08.59 + 12.21 to RS232 */
111
112#define LAPD_HDR_LEN 10
113
114/* callback from abis_nm */
115int _abis_nm_sendmsg(struct msgb *msg)
116{
117 int written;
118 u_int8_t *lapd;
119
120 msg->l2h = msg->data;
121
122 /* prepend LAPD header */
123 lapd = msgb_push(msg, LAPD_HDR_LEN);
124
125 lapd[0] = 0x00;
126 lapd[1] = msg->len - 2; /* length of bytes startign at lapd[2] */
127 lapd[2] = 0x00;
128 lapd[3] = 0x07;
129 lapd[4] = 0x01;
130 lapd[5] = 0x3e;
131 lapd[6] = 0x00;
132 lapd[7] = 0x00;
133 lapd[8] = msg->len - 10; /* length of bytes starting at lapd[10] */
134 lapd[9] = lapd[8] ^ 0x38;
135
136 fprintf(stdout, "TX: ");
137 hexdump(msg->data, msg->len);
138
139 /* send over serial line */
140 written = write(serial_fd, msg->data, msg->len);
141 if (written < msg->len) {
142 perror("short write:");
143 msgb_free(msg);
144 return -1;
145 }
146
147 msgb_free(msg);
148
149 return 0;
150}
151
152#define SERIAL_ALLOC_SIZE 300
153
154/* receive an entire message from the serial port */
155static struct msgb *serial_read_msg(void)
156{
157 struct msgb *msg = msgb_alloc(SERIAL_ALLOC_SIZE);
158 int rc;
159
160 if (!msg)
161 return NULL;
162
163 /* first read two byes to obtain length */
164 while (msg->len < 2) {
165 rc = read(serial_fd, msg->tail, 2 - msg->len);
166 if (rc < 0) {
167 perror("reading from serial port");
168 msgb_free(msg);
169 return NULL;
170 }
171 msgb_put(msg, rc);
172 }
173 if (msg->data[0] != 0)
174 fprintf(stderr, "Invalid header byte 0: 0x%02x\n",
175 msg->data[0]);
176
177 /* second byte is LAPD payload length */
178 if (msg->data[1] < LAPD_HDR_LEN + sizeof(struct abis_om_fom_hdr) +
179 sizeof(struct abis_om_hdr))
180 fprintf(stderr, "Invalied header byte 1(len): %u\n",
181 msg->data[1]);
182
183 while (msg->len < 2 + msg->data[1]) {
184 rc = read(serial_fd, msg->tail, 2 + msg->data[1] - msg->len);
185 if (rc < 0) {
186 perror("reading from serial port");
187 msgb_free(msg);
188 return NULL;
189 }
190 msgb_put(msg, rc);
191 }
192
193 msg->l2h = msg->data + LAPD_HDR_LEN;
194
195 fprintf(stdout, "RX: ");
196 hexdump(msg->l2h, msg->len - (msg->l2h - msg->data));
197
198 return msg;
199}
200
201static int file_is_readable(const char *fname)
202{
203 int rc;
204 struct stat st;
205
206 rc = stat(fname, &st);
207 if (rc < 0)
208 return 0;
209
210 if (S_ISREG(st.st_mode) && (st.st_mode & S_IRUSR))
211 return 1;
212
213 return 0;
214}
215
216
217static int handle_state_resp(u_int8_t state)
218{
219 printf("STATE: ");
220 switch (state) {
221 case BS11_STATE_WARM_UP:
222 printf("Warm Up...\n");
223 sleep(5);
224 break;
225 case BS11_STATE_LOAD_SMU_SAFETY:
226 printf("Load SMU Safety...\n");
227 sleep(5);
228 break;
229 case BS11_STATE_SOFTWARE_RQD:
230 printf("Software required...\n");
231 /* send safety load */
232 if (file_is_readable(fname_safety))
233 abis_nm_software_load(g_bts, fname_safety, 8);
234 else
235 fprintf(stderr, "No valid Safety Load file\n");
236 break;
237 case BS11_STATE_WAIT_MIN_CFG:
238 case BS11_STATE_WAIT_MIN_CFG_2:
239 printf("Wait minimal config...\n");
240 create_objects(g_bts, have_trx1);
241 break;
242 case BS11_STATE_MAINTENANCE:
243 printf("Maintenance...\n");
244 /* send software (FIXME: over A-bis?) */
245 if (file_is_readable(fname_software))
246 abis_nm_software_load(g_bts, fname_software, 8);
247 else
248 fprintf(stderr, "No valid Software file\n");
249 break;
250 case BS11_STATE_NORMAL:
251 printf("Normal...\n");
252 return 1;
253 default:
254 printf("Unknown state 0x%02u\n", state);
255 sleep(5);
256 break;
257 }
258 return 0;
259}
260
261static void print_banner(void)
262{
263 printf("bs11_config (C) 2009 by Harald Welte and Dieter Spaar\n");
264 printf("THIS SOFTWARE IS FREE SOFTWARE WIH NO WARRANTY\n\n");
265}
266
267static void print_help(void)
268{
269 printf("Supported arguments:\n");
270 printf("\t--help\t\t\t-h\tPrint this help text\n");
271 printf("\t--port /dev/ttyXXX\t-p\tSpecify serial port\n");
272 printf("\t--with-trx1\t\t-t\tAssume the BS-11 has 2 TRX\n");
273 printf("\t--software file\t\t-s\tSpecify Software file\n");
274 printf("\t--safety file\t\t-s\tSpecify Safety Load file\n");
275}
276
277static void handle_options(int argc, char **argv)
278{
279 print_banner();
280
281 while (1) {
282 int option_index = 0, c;
283 static struct option long_options[] = {
284 { "help", 0, 0, 'h' },
285 { "port", 1, 0, 'p' },
286 { "with-trx1", 0, 0, 't' },
287 { "software", 1, 0, 's' },
288 { "safety", 1, 0, 'S' },
289 };
290
291 c = getopt_long(argc, argv, "hp:s:S:t",
292 long_options, &option_index);
293
294 if (c == -1)
295 break;
296
297 switch (c) {
298 case 'h':
299 print_help();
300 exit(0);
301 case 'p':
302 serial_port = optarg;
303 break;
304 case 't':
305 have_trx1 = 1;
306 break;
307 case 's':
308 fname_software = optarg;
309 break;
310 case 'S':
311 fname_safety = optarg;
312 break;
313 default:
314 break;
315 }
316 }
317}
318
319int main(int argc, char **argv)
320{
321 struct gsm_network *gsmnet;
322
323 handle_options(argc, argv);
324
325 serial_fd = open(serial_port, O_RDWR);
326 if (serial_fd < 0) {
327 perror("cannot open serial port:");
328 exit(1);
329 }
330
331 /* FIXME: set baudrate */
332
333 gsmnet = gsm_network_init(1, 1, 1);
334 if (!gsmnet) {
335 fprintf(stderr, "Unable to allocate gsm network\n");
336 exit(1);
337 }
338 g_bts = &gsmnet->bts[0];
339
340 abis_nm_bs11_factory_logon(g_bts, 1);
341
342 while (1) {
343 struct msgb *rx_msg;
344 struct abis_om_hdr *oh;
345 struct abis_om_fom_hdr *foh;
346 int rc = -1;
347
348 rx_msg = serial_read_msg();
349
350 oh = (struct abis_om_hdr *) msgb_l2(rx_msg);
351 foh = (struct abis_om_fom_hdr *) oh->data;
352 switch (foh->msg_type) {
353 case NM_MT_BS11_GET_STATE_ACK:
354 rc = handle_state_resp(foh->data[2]);
355 break;
356 default:
357 rc = abis_nm_rcvmsg(rx_msg);
358 }
359 if (rc < 0) {
360 perror("in main loop");
361 break;
362 }
363 if (rc == 1)
364 break;
365 }
366
367 abis_nm_bs11_factory_logon(g_bts, 0);
368
369 close(serial_fd);
370 exit(0);
371}