1 /* BTSAP socket interface of layer2/3 stack */
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>
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.
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.
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.
25 #include <osmocom/bb/common/osmocom_data.h>
26 #include <osmocom/bb/common/logging.h>
28 #include <osmocore/util.h>
30 #include <sys/socket.h>
33 #include <arpa/inet.h>
41 #define GSM_SAP_LENGTH 300
42 #define GSM_SAP_HEADROOM 32
46 static int sap_read(struct bsc_fd *fd)
51 struct osmocom_ms *ms = (struct osmocom_ms *) fd->data;
53 msg = msgb_alloc_headroom(GSM_SAP_LENGTH+GSM_SAP_HEADROOM, GSM_SAP_HEADROOM, "Layer2");
55 LOGP(DSAP, LOGL_ERROR, "Failed to allocate msg.\n");
59 rc = read(fd->fd, &len, sizeof(len));
60 if (rc < sizeof(len)) {
61 fprintf(stderr, "SAP socket failed\n");
69 if (len > GSM_SAP_LENGTH) {
70 LOGP(DSAP, LOGL_ERROR, "Length is too big: %u\n", len);
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);
85 if (ms->sap_entity.msg_handler)
86 ms->sap_entity.msg_handler(msg, ms);
91 static int sap_write(struct bsc_fd *fd, struct msgb *msg)
95 rc = write(fd->fd, msg->data, msg->len);
97 LOGP(DSAP, LOGL_ERROR, "Failed to write data: rc: %d\n", rc);
104 int sap_open(struct osmocom_ms *ms, const char *socket_path)
107 struct sockaddr_un local;
109 ms->sap_wq.bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
110 if (ms->sap_wq.bfd.fd < 0) {
111 fprintf(stderr, "Failed to create unix domain socket.\n");
112 return ms->sap_wq.bfd.fd;
115 local.sun_family = AF_UNIX;
116 strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
117 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
119 rc = connect(ms->sap_wq.bfd.fd, (struct sockaddr *) &local,
120 sizeof(local.sun_family) + strlen(local.sun_path));
122 fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
123 close(ms->sap_wq.bfd.fd);
127 write_queue_init(&ms->sap_wq, 100);
128 ms->sap_wq.bfd.data = ms;
129 ms->sap_wq.bfd.when = BSC_FD_READ;
130 ms->sap_wq.read_cb = sap_read;
131 ms->sap_wq.write_cb = sap_write;
133 rc = bsc_register_fd(&ms->sap_wq.bfd);
135 fprintf(stderr, "Failed to register fd.\n");
142 int sap_close(struct osmocom_ms *ms)
144 if (ms->sap_wq.bfd.fd <= 0)
147 close(ms->sap_wq.bfd.fd);
148 bsc_unregister_fd(&ms->sap_wq.bfd);
153 int osmosap_send(struct osmocom_ms *ms, struct msgb *msg)
157 if (ms->sap_wq.bfd.fd <= 0)
160 DEBUGP(DSAP, "Sending: '%s'\n", hexdump(msg->data, msg->len));
162 if (msg->l1h != msg->data)
163 LOGP(DSAP, LOGL_ERROR, "Message SAP header != Message Data\n");
165 /* prepend 16bit length before sending */
166 len = (uint16_t *) msgb_push(msg, sizeof(*len));
167 *len = htons(msg->len - sizeof(*len));
169 if (write_queue_enqueue(&ms->sap_wq, msg) != 0) {
170 LOGP(DSAP, LOGL_ERROR, "Failed to enqueue msg.\n");
178 /* register message handler for messages that are sent from L2->L3 */
179 int osmosap_register_handler(struct osmocom_ms *ms, osmosap_cb_t cb)
181 ms->sap_entity.msg_handler = cb;