28356daa51849ba6de97d50e1e47763373a1801d
[osmocom-bb.git] / src / host / layer23 / src / common / l1l2_interface.c
1 /* Layer 1 socket interface of layer2/3 stack */
2
3 /* (C) 2010 by Holger Hans Peter Freyther
4  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5  *
6  * All Rights Reserved
7  *
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.
12  *
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.
17  *
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.
21  *
22  */
23
24 #include <osmocom/bb/common/osmocom_data.h>
25 #include <osmocom/bb/common/l1ctl.h>
26 #include <osmocom/bb/common/logging.h>
27
28 #include <osmocore/utils.h>
29
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
33 #include <arpa/inet.h>
34
35 #define _GNU_SOURCE
36 #include <unistd.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <stdlib.h>
40
41 #define GSM_L2_LENGTH 256
42 #define GSM_L2_HEADROOM 32
43
44 extern int quit;
45
46 static int layer2_read(struct bsc_fd *fd)
47 {
48         struct msgb *msg;
49         u_int16_t len;
50         int rc;
51
52         msg = msgb_alloc_headroom(GSM_L2_LENGTH+GSM_L2_HEADROOM, GSM_L2_HEADROOM, "Layer2");
53         if (!msg) {
54                 LOGP(DL1C, LOGL_ERROR, "Failed to allocate msg.\n");
55                 return -ENOMEM;
56         }
57
58         rc = read(fd->fd, &len, sizeof(len));
59         if (rc < sizeof(len)) {
60                 fprintf(stderr, "Layer2 socket failed\n");
61                 if (rc >= 0)
62                         rc = -EIO;
63                 quit = rc;
64                 return rc;
65         }
66
67         len = ntohs(len);
68         if (len > GSM_L2_LENGTH) {
69                 LOGP(DL1C, LOGL_ERROR, "Length is too big: %u\n", len);
70                 msgb_free(msg);
71                 return -EINVAL;
72         }
73
74
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);
80                 msgb_free(msg);
81                 return rc;
82         }
83
84         l1ctl_recv((struct osmocom_ms *) fd->data, msg);
85
86         return 0;
87 }
88
89 static int layer2_write(struct bsc_fd *fd, struct msgb *msg)
90 {
91         int rc;
92
93         rc = write(fd->fd, msg->data, msg->len);
94         if (rc != msg->len) {
95                 LOGP(DL1C, LOGL_ERROR, "Failed to write data: rc: %d\n", rc);
96                 return rc;
97         }
98
99         return 0;
100 }
101
102 int layer2_open(struct osmocom_ms *ms, const char *socket_path)
103 {
104         int rc;
105         struct sockaddr_un local;
106
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;
111         }
112
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';
116
117         rc = connect(ms->l2_wq.bfd.fd, (struct sockaddr *) &local,
118                      sizeof(local.sun_family) + strlen(local.sun_path));
119         if (rc < 0) {
120                 fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
121                 close(ms->l2_wq.bfd.fd);
122                 return rc;
123         }
124
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;
130
131         rc = bsc_register_fd(&ms->l2_wq.bfd);
132         if (rc != 0) {
133                 fprintf(stderr, "Failed to register fd.\n");
134                 return rc;
135         }
136
137         return 0;
138 }
139
140 int layer2_close(struct osmocom_ms *ms)
141 {
142         close(ms->l2_wq.bfd.fd);
143         bsc_unregister_fd(&ms->l2_wq.bfd);
144
145         return 0;
146 }
147
148 int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
149 {
150         uint16_t *len;
151
152         DEBUGP(DL1C, "Sending: '%s'\n", hexdump(msg->data, msg->len));
153
154         if (msg->l1h != msg->data)
155                 LOGP(DL1C, LOGL_ERROR, "Message L1 header != Message Data\n");
156         
157         /* prepend 16bit length before sending */
158         len = (uint16_t *) msgb_push(msg, sizeof(*len));
159         *len = htons(msg->len - sizeof(*len));
160
161         if (write_queue_enqueue(&ms->l2_wq, msg) != 0) {
162                 LOGP(DL1C, LOGL_ERROR, "Failed to enqueue msg.\n");
163                 msgb_free(msg);
164                 return -1;
165         }
166
167         return 0;
168 }
169
170