summaryrefslogtreecommitdiffstats
path: root/src/host/layer2/src/layer2_main.c
blob: c84cce495c161c59a591bf6b3d2a03abd60564dc (plain) (blame)
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
/* Main method of the layer2 stack */
/* (C) 2010 by Holger Hans Peter Freyther
 *
 * 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 <osmocom/osmocom_layer2.h>
#include <osmocom/osmocom_data.h>

#include <osmocom/debug.h>
#include <osmocom/msgb.h>
#include <osmocom/talloc.h>

#include <arpa/inet.h>

#include <sys/socket.h>
#include <sys/un.h>

#define _GNU_SOURCE
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#define GSM_L2_LENGTH 256

static void *l2_ctx = NULL;
static char *socket_path = "/tmp/osmocom_l2";
static struct osmocom_ms *ms = NULL;

static int layer2_read(struct bsc_fd *fd, unsigned int flags)
{
	struct msgb *msg;
	u_int16_t len;
	int rc;

	msg = msgb_alloc(GSM_L2_LENGTH, "Layer2");
	if (!msg) {
		fprintf(stderr, "Failed to allocate msg.\n");
		return -1;
	}

	rc = read(fd->fd, &len, sizeof(len));
	if (rc < sizeof(len)) {
		fprintf(stderr, "Short read. Error.\n");
		exit(2);
	}

	if (ntohs(len) > GSM_L2_LENGTH) {
		fprintf(stderr, "Length is too big: %u\n", ntohs(len));
		msgb_free(msg);
		return -1;
	}


	/* blocking read for the poor... we can starve in here... */
	msg->l2h = msgb_put(msg, ntohs(len));
	rc = read(fd->fd, msg->l2h, msgb_l2len(msg));
	if (rc != msgb_l2len(msg)) {
		fprintf(stderr, "Can not read data: rc: %d errno: %d\n", rc, errno);
		msgb_free(msg);
		return -1;
	}

	osmo_recv((struct osmocom_ms *) fd->data, msg);
	msgb_free(msg);
	return 0;
}

int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
{
	int rc;
	uint16_t *len;

	LOGP(DMUX, LOGL_INFO, "Sending: '%s'\n", hexdump(msg->data, msg->len));

	
	len = (uint16_t *) msgb_push(msg, sizeof(*len));
	*len = htons(msg->len - sizeof(*len));

	/* TODO: just enqueue the message and wait for ready write.. */
	rc = write(ms->bfd.fd, msg->data, msg->len);
	if (rc != msg->len) {
		fprintf(stderr, "Failed to write data: rc: %d\n", rc);
		msgb_free(msg);
		return -1;
	}

	msgb_free(msg);
	return 0;
}

static void print_usage()
{
	printf("Usage: ./layer2\n");
}

static void print_help()
{
	printf(" Some help...\n");
	printf("  -h --help this text\n");
	printf("  -s --socket /tmp/osmocom_l2. Path to the unix domain socket\n");
	printf("  -a --arfcn NR. The ARFCN to be used for layer2.\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'},
			{"socket", 1, 0, 's'},
			{"arfcn", 1, 0, 'a'},
			{0, 0, 0, 0},
		};

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

		switch (c) {
		case 'h':
			print_usage();
			print_help();
			exit(0);
			break;
		case 's':
			socket_path = talloc_strdup(l2_ctx, optarg);
			break;
		case 'a':
			ms->arfcn = atoi(optarg);
			break;
		default:
			break;
		}
	}
}

int main(int argc, char **argv)
{
	int rc;
	struct sockaddr_un local;

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

	ms = talloc_zero(l2_ctx, struct osmocom_ms);
	if (!ms) {
		fprintf(stderr, "Failed to allocate MS\n");
		exit(1);
	}

	ms->arfcn = 871;

	handle_options(argc, argv);

	ms->bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (ms->bfd.fd < 0) {
		fprintf(stderr, "Failed to create unix domain socket.\n");
		exit(1);
	}

	local.sun_family = AF_UNIX;
	strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
	local.sun_path[sizeof(local.sun_path) - 1] = '\0';

	rc = connect(ms->bfd.fd, (struct sockaddr *) &local,
		     sizeof(local.sun_family) + strlen(local.sun_path));
	if (rc < 0) {
		fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
		exit(1);
	}

	ms->bfd.when = BSC_FD_READ;
	ms->bfd.cb = layer2_read;
	ms->bfd.data = ms;

	if (bsc_register_fd(&ms->bfd) != 0) {
		fprintf(stderr, "Failed to register fd.\n");
		exit(1);
	}

	while (1) {
		bsc_select_main(0);
	}


	return 0;
}