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>
28 #include <osmocore/utils.h>
30 #include <sys/socket.h>
33 #include <arpa/inet.h>
41 #define GSM_L2_LENGTH 256
42 #define GSM_L2_HEADROOM 32
46 static int layer2_read(struct bsc_fd *fd)
52 msg = msgb_alloc_headroom(GSM_L2_LENGTH+GSM_L2_HEADROOM, GSM_L2_HEADROOM, "Layer2");
54 LOGP(DL1C, LOGL_ERROR, "Failed to allocate msg.\n");
58 rc = read(fd->fd, &len, sizeof(len));
59 if (rc < sizeof(len)) {
60 fprintf(stderr, "Layer2 socket failed\n");
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)
93 rc = write(fd->fd, msg->data, msg->len);
95 LOGP(DL1C, LOGL_ERROR, "Failed to write data: rc: %d\n", rc);
102 int layer2_open(struct osmocom_ms *ms, const char *socket_path)
105 struct sockaddr_un local;
107 ms->l2_wq.bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
108 if (ms->l2_wq.bfd.fd < 0) {
109 fprintf(stderr, "Failed to create unix domain socket.\n");
110 return ms->l2_wq.bfd.fd;
113 local.sun_family = AF_UNIX;
114 strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
115 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
117 rc = connect(ms->l2_wq.bfd.fd, (struct sockaddr *) &local,
118 sizeof(local.sun_family) + strlen(local.sun_path));
120 fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
121 close(ms->l2_wq.bfd.fd);
125 write_queue_init(&ms->l2_wq, 100);
126 ms->l2_wq.bfd.data = ms;
127 ms->l2_wq.bfd.when = BSC_FD_READ;
128 ms->l2_wq.read_cb = layer2_read;
129 ms->l2_wq.write_cb = layer2_write;
131 rc = bsc_register_fd(&ms->l2_wq.bfd);
133 fprintf(stderr, "Failed to register fd.\n");
140 int layer2_close(struct osmocom_ms *ms)
142 close(ms->l2_wq.bfd.fd);
143 bsc_unregister_fd(&ms->l2_wq.bfd);
148 int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
152 DEBUGP(DL1C, "Sending: '%s'\n", hexdump(msg->data, msg->len));
154 if (msg->l1h != msg->data)
155 LOGP(DL1C, LOGL_ERROR, "Message L1 header != Message Data\n");
157 /* prepend 16bit length before sending */
158 len = (uint16_t *) msgb_push(msg, sizeof(*len));
159 *len = htons(msg->len - sizeof(*len));
161 if (write_queue_enqueue(&ms->l2_wq, msg) != 0) {
162 LOGP(DL1C, LOGL_ERROR, "Failed to enqueue msg.\n");