aboutsummaryrefslogtreecommitdiffstats
path: root/src/bs11_config.c
blob: e05c609aecdcaafdcd3474c03996fd11123f43ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* Siemens BS-11 microBTS configuration tool */

/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * This software is based on ideas (but not code) of BS11Config 
 * (C) 2009 by Dieter Spaar <spaar@mirider.augusta.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <termios.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <openbsc/gsm_data.h>
#include <openbsc/abis_nm.h>
#include <openbsc/msgb.h>
#include <openbsc/tlv.h>
#include <openbsc/debug.h>

static const u_int8_t obj_li_attr[] = { 
	0xa0, 0x09, 0x00,
	0xab, 0x00, 
	0xac, 0x00,
};
static const u_int8_t obj_bbsig0_attr[] = {
	0x3d, 0x02, 0x00, 0x00,
	0x3f, 0x01, 0x00,
};
static const u_int8_t obj_pa0_attr[] = {
	NM_ATT_BS11_TXPWR, 0x01, BS11_TRX_POWER_30mW,
};
static const char *trx1_password = "1111111111";
#define TEI_OML	25

/* create all objects for an initial configuration */
static int create_objects(struct gsm_bts *bts, int trx1)
{
	//abis_nm_bs11_factory_logon(bts, 1);
	abis_nm_bs11_create_object(bts, BS11_OBJ_LI, 0, sizeof(obj_li_attr),
				   obj_li_attr);
	abis_nm_bs11_create_object(bts, BS11_OBJ_GPSU, 0, 0, NULL);
	abis_nm_bs11_create_object(bts, BS11_OBJ_ALCO, 0, 0, NULL);
	abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 0,
				   sizeof(obj_bbsig0_attr), obj_bbsig0_attr);
	abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 0,
				   sizeof(obj_pa0_attr), obj_pa0_attr);
	if (trx1) {
		u_int8_t bbsig1_attr[sizeof(obj_bbsig0_attr)+12];
		u_int8_t *cur = bbsig1_attr;
		
		abis_nm_bs11_set_trx1_pw(bts, trx1_password);

		cur = tlv_put(cur, NM_ATT_BS11_PASSWORD, 10,
			      (u_int8_t *)trx1_password);
		memcpy(cur, obj_bbsig0_attr, sizeof(obj_bbsig0_attr));
		abis_nm_bs11_create_object(bts, BS11_OBJ_BBSIG, 1,
					   sizeof(bbsig1_attr), bbsig1_attr);

		abis_nm_bs11_create_object(bts, BS11_OBJ_PA, 1,
					   sizeof(obj_pa0_attr), obj_pa0_attr);
	}

	abis_nm_bs11_create_envaBTSE(bts, 0);
	abis_nm_bs11_create_envaBTSE(bts, 1);
	abis_nm_bs11_create_envaBTSE(bts, 2);
	abis_nm_bs11_create_envaBTSE(bts, 3);

	abis_nm_bs11_conn_oml(bts, 0, 1, 0xff);
	abis_nm_bs11_set_oml_tei(bts, TEI_OML);

	abis_nm_bs11_set_trx_power(&bts->trx[0], BS11_TRX_POWER_30mW);

	if (trx1)
		abis_nm_bs11_set_trx_power(&bts->trx[1], BS11_TRX_POWER_30mW);

	//abis_nm_bs11_factory_logon(bts, 0);
	
	return 0;
}

static char *serial_port = "/dev/ttyUSB0";
static char *fname_safety = "BTSBMC76.SWI";
static char *fname_software = "HS011106.SWL";
static int serial_fd = -1;
static int have_trx1 = 0;
static struct gsm_bts *g_bts;

/* adaption layer from GSM 08.59 + 12.21 to RS232 */

#define LAPD_HDR_LEN	10

/* callback from abis_nm */
int _abis_nm_sendmsg(struct msgb *msg)
{
	int written;
	u_int8_t *lapd;

	msg->l2h = msg->data;

	/* prepend LAPD header */
	lapd = msgb_push(msg, LAPD_HDR_LEN);

	lapd[0] = 0x00;
	lapd[1] = msg->len - 2; /* length of bytes startign at lapd[2] */
	lapd[2] = 0x00;
	lapd[3] = 0x07;
	lapd[4] = 0x01;
	lapd[5] = 0x3e;
	lapd[6] = 0x00;
	lapd[7] = 0x00;
	lapd[8] = msg->len - 10; /* length of bytes starting at lapd[10] */
	lapd[9] = lapd[8] ^ 0x38;

	fprintf(stdout, "TX: ");
	hexdump(msg->data, msg->len);

	/* send over serial line */
	written = write(serial_fd, msg->data, msg->len);
	if (written < msg->len) {
		perror("short write:");
		msgb_free(msg);
		return -1;
	}

	msgb_free(msg);

	return 0;
}

#define SERIAL_ALLOC_SIZE	300

/* receive an entire message from the serial port */
static struct msgb *serial_read_msg(void)
{
	struct msgb *msg = msgb_alloc(SERIAL_ALLOC_SIZE);
	int rc;

	if (!msg)
		return NULL;

	/* first read two byes to obtain length */
	while (msg->len < 2) {
		rc = read(serial_fd, msg->tail, 2 - msg->len);
		if (rc < 0) {
			perror("reading from serial port");
			msgb_free(msg);
			return NULL;
		}
		msgb_put(msg, rc);
	}
	if (msg->data[0] != 0)
		fprintf(stderr, "Invalid header byte 0: 0x%02x\n",
			msg->data[0]);

	/* second byte is LAPD payload length */
	if (msg->data[1] < LAPD_HDR_LEN + sizeof(struct abis_om_fom_hdr) +
			   sizeof(struct abis_om_hdr))
		fprintf(stderr, "Invalied header byte 1(len): %u\n",
			msg->data[1]);
	printf("length = %u\n", msg->data[1]);

	while (msg->len < 2 + msg->data[1]) {
		printf("msg->len(%u), waiting for %u\n", msg->len, 2 + msg->data[1] - msg->len);
		rc = read(serial_fd, msg->tail, 2 + msg->data[1] - msg->len);
		if (rc < 0) {
			perror("reading from serial port");
			msgb_free(msg);
			return NULL;
		}
		msgb_put(msg, rc);
	}

	msg->l2h = msg->data + LAPD_HDR_LEN;

	fprintf(stdout, "RX: ");
	hexdump(msg->l2h, msg->len - (msg->l2h - msg->data));

	return msg;
}

static int file_is_readable(const char *fname)
{
	int rc;
	struct stat st;

	rc = stat(fname, &st);
	if (rc < 0)
		return 0;

	if (S_ISREG(st.st_mode) && (st.st_mode & S_IRUSR))
		return 1;

	return 0;
}


static int handle_state_resp(u_int8_t state)
{
	printf("STATE: ");
	switch (state) {
	case BS11_STATE_WARM_UP:
		printf("Warm Up...\n");
		sleep(5);
		break;
	case BS11_STATE_LOAD_SMU_SAFETY:
		printf("Load SMU Safety...\n");
		sleep(5);
		break;
	case BS11_STATE_SOFTWARE_RQD:
		printf("Software required...\n");
		/* send safety load */
		if (file_is_readable(fname_safety))
			abis_nm_software_load(g_bts, fname_safety, 8);
		else
			fprintf(stderr, "No valid Safety Load file\n");
		break;
	case BS11_STATE_WAIT_MIN_CFG:
	case BS11_STATE_WAIT_MIN_CFG_2:
		printf("Wait minimal config...\n");
		create_objects(g_bts, have_trx1);
		break;
	case BS11_STATE_MAINTENANCE:
		printf("Maintenance...\n");
		/* send software (FIXME: over A-bis?) */
		if (file_is_readable(fname_software))
			abis_nm_software_load(g_bts, fname_software, 8);
		else
			fprintf(stderr, "No valid Software file\n");
		break;
	case BS11_STATE_NORMAL:
		printf("Normal...\n");
		return 1;
	default:
		printf("Unknown state 0x%02u\n", state);
		sleep(5);
		break;
	}
	return 0;
}

static void print_banner(void)
{
	printf("bs11_config (C) 2009 by Harald Welte and Dieter Spaar\n");
	printf("THIS SOFTWARE IS FREE SOFTWARE WIH NO WARRANTY\n\n");
}

static void print_help(void)
{
	printf("Supported arguments:\n");
	printf("\t--help\t\t\t-h\tPrint this help text\n");
	printf("\t--port /dev/ttyXXX\t-p\tSpecify serial port\n");
	printf("\t--with-trx1\t\t-t\tAssume the BS-11 has 2 TRX\n");
	printf("\t--software file\t\t-s\tSpecify Software file\n");
	printf("\t--safety file\t\t-s\tSpecify Safety Load file\n");
}

static void handle_options(int argc, char **argv)
{
	print_banner();

	while (1) {
		int option_index = 0, c;
		static struct option long_options[] = {
			{ "help", 0, 0, 'h' },
			{ "port", 1, 0, 'p' },
			{ "with-trx1", 0, 0, 't' },
			{ "software", 1, 0, 's' },
			{ "safety", 1, 0, 'S' },
		};

		c = getopt_long(argc, argv, "hp:s:S:t",
				long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_help();
			exit(0);
		case 'p':
			serial_port = optarg;
			break;
		case 't':
			have_trx1 = 1;
			break;
		case 's':
			fname_software = optarg;
			break;
		case 'S':
			fname_safety = optarg;
			break;
		default:
			break;
		}
	}
}

int main(int argc, char **argv)
{
	struct gsm_network *gsmnet;
	struct termios tio;
	int rc;

	handle_options(argc, argv);

	serial_fd = open(serial_port, O_RDWR);
	if (serial_fd < 0) {
		perror("cannot open serial port:");
		exit(1);
	}

	/* set baudrate */
	rc = tcgetattr(serial_fd, &tio);
	if (rc < 0) {
		perror("tcgetattr()");
		exit(1);
	}
	cfsetispeed(&tio, B19200);
	cfsetospeed(&tio, B19200);
	tio.c_cflag |= CREAD | CLOCAL;
	rc = tcsetattr(serial_fd, TCSADRAIN, &tio);
	if (rc < 0) {
		perror("tcsetattr()");
		exit(1);
	}

	gsmnet = gsm_network_init(1, 1, 1);
	if (!gsmnet) {
		fprintf(stderr, "Unable to allocate gsm network\n");
		exit(1);
	}
	g_bts = &gsmnet->bts[0];

	abis_nm_bs11_factory_logon(g_bts, 1);

	while (1) {
		struct msgb *rx_msg;
		struct abis_om_hdr *oh;
		struct abis_om_fom_hdr *foh;
		rc = -1;

		rx_msg = serial_read_msg();

		oh = (struct abis_om_hdr *) msgb_l2(rx_msg);
		foh = (struct abis_om_fom_hdr *) oh->data;
		switch (foh->msg_type) {
		case NM_MT_BS11_GET_STATE_ACK:
			rc = handle_state_resp(foh->data[2]);
			break;
		default:
			rc = abis_nm_rcvmsg(rx_msg);
		}
		if (rc < 0) {
			perror("in main loop");
			break;
		}
		if (rc == 1)
			break;
	}

	abis_nm_bs11_factory_logon(g_bts, 0);

	close(serial_fd);
	exit(0);
}