[layer23] Added BTSAP socket interface to layer23
[osmocom-bb.git] / src / host / layer23 / src / common / sap_interface.c
1 /* BTSAP 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  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
6  *
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  */
24
25 #include <osmocom/bb/common/osmocom_data.h>
26 #include <osmocom/bb/common/logging.h>
27
28 #include <sys/socket.h>
29 #include <sys/un.h>
30
31 #include <arpa/inet.h>
32
33 #define _GNU_SOURCE
34 #include <unistd.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 #define GSM_SAP_LENGTH 300
40 #define GSM_SAP_HEADROOM 32
41
42 extern int quit;
43
44 static int sap_read(struct bsc_fd *fd)
45 {
46         struct msgb *msg;
47         u_int16_t len;
48         int rc;
49         struct osmocom_ms *ms = (struct osmocom_ms *) fd->data;
50
51         msg = msgb_alloc_headroom(GSM_SAP_LENGTH+GSM_SAP_HEADROOM, GSM_SAP_HEADROOM, "Layer2");
52         if (!msg) {
53                 LOGP(DSAP, LOGL_ERROR, "Failed to allocate msg.\n");
54                 return -ENOMEM;
55         }
56
57         rc = read(fd->fd, &len, sizeof(len));
58         if (rc < sizeof(len)) {
59                 fprintf(stderr, "SAP socket failed\n");
60                 if (rc >= 0)
61                         rc = -EIO;
62                 quit = rc;
63                 return rc;
64         }
65
66         len = ntohs(len);
67         if (len > GSM_SAP_LENGTH) {
68                 LOGP(DSAP, LOGL_ERROR, "Length is too big: %u\n", len);
69                 msgb_free(msg);
70                 return -EINVAL;
71         }
72
73
74         msg->l1h = msgb_put(msg, len);
75         rc = read(fd->fd, msg->l1h, msgb_l1len(msg));
76         if (rc != msgb_l1len(msg)) {
77                 LOGP(DSAP, LOGL_ERROR, "Can not read data: len=%d rc=%d "
78                      "errno=%d\n", len, rc, errno);
79                 msgb_free(msg);
80                 return rc;
81         }
82
83         if (ms->sap_entity.msg_handler)
84                 ms->sap_entity.msg_handler(msg, ms);
85
86         return 0;
87 }
88
89 static int sap_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(DSAP, LOGL_ERROR, "Failed to write data: rc: %d\n", rc);
96                 return rc;
97         }
98
99         return 0;
100 }
101
102 int sap_open(struct osmocom_ms *ms, const char *socket_path)
103 {
104         int rc;
105         struct sockaddr_un local;
106
107         ms->sap_wq.bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
108         if (ms->sap_wq.bfd.fd < 0) {
109                 fprintf(stderr, "Failed to create unix domain socket.\n");
110                 return ms->sap_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->sap_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->sap_wq.bfd.fd);
122                 return rc;
123         }
124
125         write_queue_init(&ms->sap_wq, 100);
126         ms->sap_wq.bfd.data = ms;
127         ms->sap_wq.bfd.when = BSC_FD_READ;
128         ms->sap_wq.read_cb = sap_read;
129         ms->sap_wq.write_cb = sap_write;
130
131         rc = bsc_register_fd(&ms->sap_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 sap_close(struct osmocom_ms *ms)
141 {
142         if (ms->sap_wq.bfd.fd <= 0)
143                 return -EINVAL;
144
145         close(ms->sap_wq.bfd.fd);
146         bsc_unregister_fd(&ms->sap_wq.bfd);
147
148         return 0;
149 }
150
151 int osmosap_send(struct osmocom_ms *ms, struct msgb *msg)
152 {
153         uint16_t *len;
154
155         if (ms->sap_wq.bfd.fd <= 0)
156                 return -EINVAL;
157
158         DEBUGP(DSAP, "Sending: '%s'\n", hexdump(msg->data, msg->len));
159
160         if (msg->l1h != msg->data)
161                 LOGP(DSAP, LOGL_ERROR, "Message SAP header != Message Data\n");
162         
163         /* prepend 16bit length before sending */
164         len = (uint16_t *) msgb_push(msg, sizeof(*len));
165         *len = htons(msg->len - sizeof(*len));
166
167         if (write_queue_enqueue(&ms->sap_wq, msg) != 0) {
168                 LOGP(DSAP, LOGL_ERROR, "Failed to enqueue msg.\n");
169                 msgb_free(msg);
170                 return -1;
171         }
172
173         return 0;
174 }
175
176 /* register message handler for messages that are sent from L2->L3 */
177 int osmosap_register_handler(struct osmocom_ms *ms, osmosap_cb_t cb)
178 {
179         ms->sap_entity.msg_handler = cb;
180
181         return 0;
182 }
183