1 /* Main method of the layer2 stack */
2 /* (C) 2010 by Holger Hans Peter Freyther
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <osmocom/osmocom_layer2.h>
23 #include <osmocom/osmocom_data.h>
25 #include <osmocom/debug.h>
26 #include <osmocom/msgb.h>
27 #include <osmocom/talloc.h>
29 #include <arpa/inet.h>
31 #include <sys/socket.h>
41 #define GSM_L2_LENGTH 256
43 static void *l2_ctx = NULL;
44 static char *socket_path = "/tmp/osmocom_l2";
45 static struct osmocom_ms *ms = NULL;
47 static int layer2_read(struct bsc_fd *fd, unsigned int flags)
53 msg = msgb_alloc(GSM_L2_LENGTH, "Layer2");
55 fprintf(stderr, "Failed to allocate msg.\n");
59 rc = read(fd->fd, &len, sizeof(len));
60 if (rc < sizeof(len)) {
61 fprintf(stderr, "Short read. Error.\n");
65 if (ntohs(len) > GSM_L2_LENGTH) {
66 fprintf(stderr, "Length is too big: %u\n", ntohs(len));
72 /* blocking read for the poor... we can starve in here... */
73 msg->l2h = msgb_put(msg, ntohs(len));
74 rc = read(fd->fd, msg->l2h, msgb_l2len(msg));
75 if (rc != msgb_l2len(msg)) {
76 fprintf(stderr, "Can not read data: rc: %d errno: %d\n", rc, errno);
81 osmo_recv((struct osmocom_ms *) fd->data, msg);
86 int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
91 LOGP(DMUX, LOGL_INFO, "Sending: '%s'\n", hexdump(msg->data, msg->len));
94 len = (uint16_t *) msgb_push(msg, sizeof(*len));
95 *len = htons(msg->len - sizeof(*len));
97 /* TODO: just enqueue the message and wait for ready write.. */
98 rc = write(ms->bfd.fd, msg->data, msg->len);
100 fprintf(stderr, "Failed to write data: rc: %d\n", rc);
109 static void print_usage()
111 printf("Usage: ./layer2\n");
114 static void print_help()
116 printf(" Some help...\n");
117 printf(" -h --help this text\n");
118 printf(" -s --socket /tmp/osmocom_l2. Path to the unix domain socket\n");
119 printf(" -a --arfcn NR. The ARFCN to be used for layer2.\n");
122 static void handle_options(int argc, char **argv)
125 int option_index = 0, c;
126 static struct option long_options[] = {
128 {"socket", 1, 0, 's'},
129 {"arfcn", 1, 0, 'a'},
133 c = getopt_long(argc, argv, "hs:a:",
134 long_options, &option_index);
145 socket_path = talloc_strdup(l2_ctx, optarg);
148 ms->arfcn = atoi(optarg);
156 int main(int argc, char **argv)
159 struct sockaddr_un local;
161 l2_ctx = talloc_named_const(NULL, 1, "layer2 context");
163 ms = talloc_zero(l2_ctx, struct osmocom_ms);
165 fprintf(stderr, "Failed to allocate MS\n");
171 handle_options(argc, argv);
173 ms->bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
174 if (ms->bfd.fd < 0) {
175 fprintf(stderr, "Failed to create unix domain socket.\n");
179 local.sun_family = AF_UNIX;
180 strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
181 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
183 rc = connect(ms->bfd.fd, (struct sockaddr *) &local,
184 sizeof(local.sun_family) + strlen(local.sun_path));
186 fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
190 ms->bfd.when = BSC_FD_READ;
191 ms->bfd.cb = layer2_read;
194 if (bsc_register_fd(&ms->bfd) != 0) {
195 fprintf(stderr, "Failed to register fd.\n");