1 /* Layer 1 socket interface of layer2/3 stack */
3 /* (C) 2010 by Holger Hans Peter Freyther
4 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <osmocom/bb/common/osmocom_data.h>
25 #include <osmocom/bb/common/l1ctl.h>
26 #include <osmocom/bb/common/logging.h>
27 #include <osmocom/bb/common/l1l2_interface.h>
29 #include <osmocore/utils.h>
31 #include <sys/socket.h>
34 #include <arpa/inet.h>
42 #define GSM_L2_LENGTH 256
43 #define GSM_L2_HEADROOM 32
45 static int layer2_read(struct bsc_fd *fd)
51 msg = msgb_alloc_headroom(GSM_L2_LENGTH+GSM_L2_HEADROOM, GSM_L2_HEADROOM, "Layer2");
53 LOGP(DL1C, LOGL_ERROR, "Failed to allocate msg.\n");
57 rc = read(fd->fd, &len, sizeof(len));
58 if (rc < sizeof(len)) {
59 fprintf(stderr, "Layer2 socket failed\n");
63 layer2_close((struct osmocom_ms *) fd->data);
68 if (len > GSM_L2_LENGTH) {
69 LOGP(DL1C, LOGL_ERROR, "Length is too big: %u\n", len);
75 msg->l1h = msgb_put(msg, len);
76 rc = read(fd->fd, msg->l1h, msgb_l1len(msg));
77 if (rc != msgb_l1len(msg)) {
78 LOGP(DL1C, LOGL_ERROR, "Can not read data: len=%d rc=%d "
79 "errno=%d\n", len, rc, errno);
84 l1ctl_recv((struct osmocom_ms *) fd->data, msg);
89 static int layer2_write(struct bsc_fd *fd, struct msgb *msg)
96 rc = write(fd->fd, msg->data, msg->len);
98 LOGP(DL1C, LOGL_ERROR, "Failed to write data: rc: %d\n", rc);
105 int layer2_open(struct osmocom_ms *ms, const char *socket_path)
108 struct sockaddr_un local;
110 ms->l2_wq.bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
111 if (ms->l2_wq.bfd.fd < 0) {
112 fprintf(stderr, "Failed to create unix domain socket.\n");
113 return ms->l2_wq.bfd.fd;
116 local.sun_family = AF_UNIX;
117 strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
118 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
120 rc = connect(ms->l2_wq.bfd.fd, (struct sockaddr *) &local,
123 fprintf(stderr, "Failed to connect to '%s': %s\n", local.sun_path,
125 close(ms->l2_wq.bfd.fd);
129 write_queue_init(&ms->l2_wq, 100);
130 ms->l2_wq.bfd.data = ms;
131 ms->l2_wq.bfd.when = BSC_FD_READ;
132 ms->l2_wq.read_cb = layer2_read;
133 ms->l2_wq.write_cb = layer2_write;
135 rc = bsc_register_fd(&ms->l2_wq.bfd);
137 fprintf(stderr, "Failed to register fd.\n");
138 close(ms->l2_wq.bfd.fd);
145 int layer2_close(struct osmocom_ms *ms)
147 if (ms->l2_wq.bfd.fd <= 0)
150 close(ms->l2_wq.bfd.fd);
151 ms->l2_wq.bfd.fd = -1;
152 bsc_unregister_fd(&ms->l2_wq.bfd);
157 int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
161 DEBUGP(DL1C, "Sending: '%s'\n", hexdump(msg->data, msg->len));
163 if (msg->l1h != msg->data)
164 LOGP(DL1C, LOGL_ERROR, "Message L1 header != Message Data\n");
166 /* prepend 16bit length before sending */
167 len = (uint16_t *) msgb_push(msg, sizeof(*len));
168 *len = htons(msg->len - sizeof(*len));
170 if (write_queue_enqueue(&ms->l2_wq, msg) != 0) {
171 LOGP(DL1C, LOGL_ERROR, "Failed to enqueue msg.\n");