src: use new libosmogsm and include/osmocom/[gsm|core] path to headers
[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 #include <osmocom/bb/common/sap_interface.h>
28
29 #include <osmocom/core/utils.h>
30
31 #include <sys/socket.h>
32 #include <sys/un.h>
33
34 #include <arpa/inet.h>
35
36 #define _GNU_SOURCE
37 #include <unistd.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <stdlib.h>
41
42 #define GSM_SAP_LENGTH 300
43 #define GSM_SAP_HEADROOM 32
44
45 static int sap_read(struct bsc_fd *fd)
46 {
47         struct msgb *msg;
48         u_int16_t len;
49         int rc;
50         struct osmocom_ms *ms = (struct osmocom_ms *) fd->data;
51
52         msg = msgb_alloc_headroom(GSM_SAP_LENGTH+GSM_SAP_HEADROOM, GSM_SAP_HEADROOM, "Layer2");
53         if (!msg) {
54                 LOGP(DSAP, 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, "SAP socket failed\n");
61                 msgb_free(msg);
62                 if (rc >= 0)
63                         rc = -EIO;
64                 sap_close(ms);
65                 return rc;
66         }
67
68         len = ntohs(len);
69         if (len > GSM_SAP_LENGTH) {
70                 LOGP(DSAP, LOGL_ERROR, "Length is too big: %u\n", len);
71                 msgb_free(msg);
72                 return -EINVAL;
73         }
74
75
76         msg->l1h = msgb_put(msg, len);
77         rc = read(fd->fd, msg->l1h, msgb_l1len(msg));
78         if (rc != msgb_l1len(msg)) {
79                 LOGP(DSAP, LOGL_ERROR, "Can not read data: len=%d rc=%d "
80                      "errno=%d\n", len, rc, errno);
81                 msgb_free(msg);
82                 return rc;
83         }
84
85         if (ms->sap_entity.msg_handler)
86                 ms->sap_entity.msg_handler(msg, ms);
87
88         return 0;
89 }
90
91 static int sap_write(struct bsc_fd *fd, struct msgb *msg)
92 {
93         int rc;
94
95         if (fd->fd <= 0)
96                 return -EINVAL;
97
98         rc = write(fd->fd, msg->data, msg->len);
99         if (rc != msg->len) {
100                 LOGP(DSAP, LOGL_ERROR, "Failed to write data: rc: %d\n", rc);
101                 return rc;
102         }
103
104         return 0;
105 }
106
107 int sap_open(struct osmocom_ms *ms, const char *socket_path)
108 {
109         int rc;
110         struct sockaddr_un local;
111
112         ms->sap_wq.bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
113         if (ms->sap_wq.bfd.fd < 0) {
114                 fprintf(stderr, "Failed to create unix domain socket.\n");
115                 return ms->sap_wq.bfd.fd;
116         }
117
118         local.sun_family = AF_UNIX;
119         strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
120         local.sun_path[sizeof(local.sun_path) - 1] = '\0';
121
122         rc = connect(ms->sap_wq.bfd.fd, (struct sockaddr *) &local,
123                      sizeof(local.sun_family) + strlen(local.sun_path));
124         if (rc < 0) {
125                 fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
126                 close(ms->sap_wq.bfd.fd);
127                 return rc;
128         }
129
130         write_queue_init(&ms->sap_wq, 100);
131         ms->sap_wq.bfd.data = ms;
132         ms->sap_wq.bfd.when = BSC_FD_READ;
133         ms->sap_wq.read_cb = sap_read;
134         ms->sap_wq.write_cb = sap_write;
135
136         rc = bsc_register_fd(&ms->sap_wq.bfd);
137         if (rc != 0) {
138                 fprintf(stderr, "Failed to register fd.\n");
139                 return rc;
140         }
141
142         return 0;
143 }
144
145 int sap_close(struct osmocom_ms *ms)
146 {
147         if (ms->sap_wq.bfd.fd <= 0)
148                 return -EINVAL;
149
150         close(ms->sap_wq.bfd.fd);
151         ms->sap_wq.bfd.fd = -1;
152         bsc_unregister_fd(&ms->sap_wq.bfd);
153
154         return 0;
155 }
156
157 int osmosap_send(struct osmocom_ms *ms, struct msgb *msg)
158 {
159         uint16_t *len;
160
161         if (ms->sap_wq.bfd.fd <= 0)
162                 return -EINVAL;
163
164         DEBUGP(DSAP, "Sending: '%s'\n", hexdump(msg->data, msg->len));
165
166         if (msg->l1h != msg->data)
167                 LOGP(DSAP, LOGL_ERROR, "Message SAP header != Message Data\n");
168         
169         /* prepend 16bit length before sending */
170         len = (uint16_t *) msgb_push(msg, sizeof(*len));
171         *len = htons(msg->len - sizeof(*len));
172
173         if (write_queue_enqueue(&ms->sap_wq, msg) != 0) {
174                 LOGP(DSAP, LOGL_ERROR, "Failed to enqueue msg.\n");
175                 msgb_free(msg);
176                 return -1;
177         }
178
179         return 0;
180 }
181
182 /* register message handler for messages that are sent from L2->L3 */
183 int osmosap_register_handler(struct osmocom_ms *ms, osmosap_cb_t cb)
184 {
185         ms->sap_entity.msg_handler = cb;
186
187         return 0;
188 }
189