1 /* Main method of the layer2 stack */
2 /* (C) 2010 by Holger Hans Peter Freyther
3 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <osmocom/osmocom_data.h>
24 #include <osmocom/osmocom_layer2.h>
25 #include <osmocom/lapdm.h>
27 #include <osmocom/debug.h>
28 #include <osmocore/msgb.h>
29 #include <osmocore/talloc.h>
30 #include <osmocore/select.h>
32 #include <arpa/inet.h>
34 #include <sys/socket.h>
44 #include "gsmtap_util.h"
46 #define GSM_L2_LENGTH 256
48 static void *l2_ctx = NULL;
49 static char *socket_path = "/tmp/osmocom_l2";
50 static struct osmocom_ms *ms = NULL;
52 static int layer2_read(struct bsc_fd *fd, unsigned int flags)
58 msg = msgb_alloc(GSM_L2_LENGTH, "Layer2");
60 fprintf(stderr, "Failed to allocate msg.\n");
64 rc = read(fd->fd, &len, sizeof(len));
65 if (rc < sizeof(len)) {
66 fprintf(stderr, "Short read. Error.\n");
71 if (len > GSM_L2_LENGTH) {
72 fprintf(stderr, "Length is too big: %u\n", len);
78 /* blocking read for the poor... we can starve in here... */
79 msg->l1h = msgb_put(msg, len);
80 rc = read(fd->fd, msg->l1h, msgb_l1len(msg));
81 if (rc != msgb_l1len(msg)) {
82 fprintf(stderr, "Can not read data: len=%d rc=%d errno=%d\n", len, rc, errno);
87 osmo_recv((struct osmocom_ms *) fd->data, msg);
92 int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
97 LOGP(DMUX, LOGL_INFO, "Sending: '%s'\n", hexdump(msg->data, msg->len));
100 len = (uint16_t *) msgb_push(msg, sizeof(*len));
101 *len = htons(msg->len - sizeof(*len));
103 /* TODO: just enqueue the message and wait for ready write.. */
104 rc = write(ms->bfd.fd, msg->data, msg->len);
105 if (rc != msg->len) {
106 fprintf(stderr, "Failed to write data: rc: %d\n", rc);
115 static void print_usage()
117 printf("Usage: ./layer2\n");
120 static void print_help()
122 printf(" Some help...\n");
123 printf(" -h --help this text\n");
124 printf(" -s --socket /tmp/osmocom_l2. Path to the unix domain socket\n");
125 printf(" -a --arfcn NR. The ARFCN to be used for layer2.\n");
128 static void handle_options(int argc, char **argv)
131 int option_index = 0, c;
132 static struct option long_options[] = {
134 {"socket", 1, 0, 's'},
135 {"arfcn", 1, 0, 'a'},
139 c = getopt_long(argc, argv, "hs:a:",
140 long_options, &option_index);
151 socket_path = talloc_strdup(l2_ctx, optarg);
154 ms->arfcn = atoi(optarg);
162 int main(int argc, char **argv)
165 struct sockaddr_un local;
166 struct sockaddr_in gsmtap;
168 l2_ctx = talloc_named_const(NULL, 1, "layer2 context");
170 ms = talloc_zero(l2_ctx, struct osmocom_ms);
172 fprintf(stderr, "Failed to allocate MS\n");
178 handle_options(argc, argv);
180 ms->bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
181 if (ms->bfd.fd < 0) {
182 fprintf(stderr, "Failed to create unix domain socket.\n");
186 local.sun_family = AF_UNIX;
187 strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
188 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
190 rc = connect(ms->bfd.fd, (struct sockaddr *) &local,
191 sizeof(local.sun_family) + strlen(local.sun_path));
193 fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
197 ms->bfd.when = BSC_FD_READ;
198 ms->bfd.cb = layer2_read;
201 lapdm_init(&ms->lapdm_dcch, ms);
202 lapdm_init(&ms->lapdm_acch, ms);
204 if (bsc_register_fd(&ms->bfd) != 0) {
205 fprintf(stderr, "Failed to register fd.\n");
211 fprintf(stderr, "Failed during gsmtap_init()\n");