aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-bb/main.c
blob: 076a6538bafc2673fb11cc8e6c448d69a27ea646 (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
/* (C) 2011 by Andreas Eversberg <jolly@eversberg.eu>
 *
 * All Rights Reserved
 *
 * 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 <netinet/in.h>
#include <osmocore/talloc.h>
#include <osmocore/signal.h>
#include <osmocore/timer.h>
#include <osmocore/select.h>
#include <osmocore/signal.h>
#include <osmo-bts/logging.h>
//#include <osmocom/bb/common/osmocom_data.h>
#include <osmo-bts/support.h>
#include <osmo-bts/abis.h>
#include <osmo-bts/rtp.h>
#include <osmo-bts/bts.h>

#include <net/if.h>

#define _GNU_SOURCE
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <string.h>
#include <time.h>

struct log_target *stderr_target;
char *debugs = "DL1C:DLAPDM:DABIS:DOML:DRSL:DSUM";

void *l23_ctx = NULL;
static struct osmocom_bts *bts;
int debug_set = 0;
char *software_version = "0.0";
uint8_t abis_mac[6] = { 0, 1, 2, 3, 4, 5 };
char *bsc_host = "localhost";
char *bts_id = "1801/0";
int quit = 0;

// FIXME this is a hack
static void get_mac(void)
{
	struct if_nameindex *ifn = if_nameindex();
	struct ifreq ifr;
	int sock;
	int ret;

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock < 0)
		return;

	memset(&ifr, 0, sizeof(ifr));
	if (!ifn)
		return;
	while (ifn->if_name) {
		strncpy(ifr.ifr_name, ifn->if_name, sizeof(ifr.ifr_name)-1);
		ret = ioctl(sock, SIOCGIFHWADDR, &ifr);
		if (ret == 0 && !!memcmp(ifr.ifr_hwaddr.sa_data,
					"\0\0\0\0\0\0", 6)) {
			memcpy(abis_mac, ifr.ifr_hwaddr.sa_data, 6);
			printf("Using MAC address of %s: "
				"'%02x:%02x:%02x:%02x:%02x:%02x'\n",
				ifn->if_name,
				abis_mac[0], abis_mac[1], abis_mac[2],
				abis_mac[3], abis_mac[4], abis_mac[5]);
			break;
		}
		ifn++;
	}
//	if_freenameindex(ifn);
}

static void print_usage(const char *app)
{
	printf("Usage: %s [option]\n", app);
	printf("  -h --help		this text\n");
	printf("  -d --debug		Change debug flags. (-d %s)\n", debugs);
	printf("  -i --bsc-ip           IP address of the BSC\n");
}

static void handle_options(int argc, char **argv)
{
	while (1) {
		int option_index = 0, c;
		static struct option long_options[] = {
			{"help", 0, 0, 'h'},
			{"debug", 1, 0, 'd'},
			{"bsc-ip", 1, 0, 'i'},
			{0, 0, 0, 0},
		};

		c = getopt_long(argc, argv, "hd:i:",
				long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_usage(argv[0]);
			exit(0);
		case 'd':
			log_parse_category_mask(stderr_target, optarg);
			debug_set = 1;
			break;
		case 'i':
			bsc_host = strdup(optarg);
			break;
		default:
			break;
		}
	}
}

void sighandler(int sigset)
{
	if (sigset == SIGHUP || sigset == SIGPIPE)
		return;

	fprintf(stderr, "Signal %d recevied.\n", sigset);

	/* in case there is a lockup during exit */
	signal(SIGINT, SIG_DFL);
	signal(SIGHUP, SIG_DFL);
	signal(SIGTERM, SIG_DFL);
	signal(SIGPIPE, SIG_DFL);

	quit = 1;
//	dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
}

int main(int argc, char **argv)
{
	int ret;
	struct hostent *hostent;
	struct in_addr *ina;
	uint32_t bsc_ip;
	uint8_t maskv_tx[2], maskv_rx[2];

	printf("((*))\n");
	printf("  |\n");
	printf(" / \\ OsmoBTS\n");

	get_mac();
	bts_support_init();

	srand(time(NULL));

	log_init(&log_info);
	stderr_target = log_target_create_stderr();
	log_add_target(stderr_target);
	log_set_all_filter(stderr_target, 1);

	l23_ctx = talloc_named_const(NULL, 1, "layer2 context");

	handle_options(argc, argv);

	if (!debug_set)
		log_parse_category_mask(stderr_target, debugs);
	log_set_log_level(stderr_target, LOGL_INFO);

	hostent = gethostbyname(bsc_host);
	if (!hostent) {
		fprintf(stderr, "Failed to resolve BSC hostname '%s'.\n",
			bsc_host);
	}
	ina = (struct in_addr *) hostent->h_addr;
	bsc_ip = ntohl(ina->s_addr);
	printf("Using BSC at IP: '%d.%d.%d.%d'\n", bsc_ip >> 24,
		(bsc_ip >> 16) & 0xff, (bsc_ip >> 8) & 0xff, bsc_ip & 0xff);

	printf("Using BTS ID: '%s'\n", bts_id);
	bts = create_bts(1, bts_id);
	if (!bts)
		exit(-1);
	maskv_tx[0] = 0x55;
	maskv_rx[0] = 0x55;
	ret = create_ms(bts->trx[0], 1, maskv_tx, maskv_rx);
	if (ret < 0)
		goto fail;
	ret = abis_open(&bts->link, bsc_ip);
	if (ret < 0)
		goto fail;

	signal(SIGINT, sighandler);
	signal(SIGHUP, sighandler);
	signal(SIGTERM, sighandler);
	signal(SIGPIPE, sighandler);

	while (!quit) {
		work_bts(bts);
		bsc_select_main(0);
	}

fail:
	destroy_bts(bts);

	return 0;
}