3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library 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 GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <sys/socket.h>
39 #include "ipc-common.h"
43 struct service_handler {
44 const struct ipc_handler *handler;
49 struct service_handler *services;
62 ipc_disconnect_cb disconnect_cb;
63 void *disconnect_cb_data;
66 static void ipc_disconnect(struct ipc *ipc, bool in_cleanup)
69 g_source_remove(ipc->cmd_watch);
74 g_io_channel_shutdown(ipc->cmd_io, TRUE, NULL);
75 g_io_channel_unref(ipc->cmd_io);
79 if (ipc->notif_watch) {
80 g_source_remove(ipc->notif_watch);
85 g_io_channel_shutdown(ipc->notif_io, TRUE, NULL);
86 g_io_channel_unref(ipc->notif_io);
93 if (ipc->disconnect_cb)
94 ipc->disconnect_cb(ipc->disconnect_cb_data);
97 static int ipc_handle_msg(struct service_handler *handlers, size_t max_index,
98 const void *buf, ssize_t len)
100 const struct ipc_hdr *msg = buf;
101 const struct ipc_handler *handler;
103 if (len < (ssize_t) sizeof(*msg)) {
104 DBG("message too small (%zd bytes)", len);
108 if (len != (ssize_t) (sizeof(*msg) + msg->len)) {
109 DBG("message malformed (%zd bytes)", len);
113 /* if service is valid */
114 if (msg->service_id > max_index) {
115 DBG("unknown service (0x%x)", msg->service_id);
119 /* if service is registered */
120 if (!handlers[msg->service_id].handler) {
121 DBG("service not registered (0x%x)", msg->service_id);
125 /* if opcode is valid */
126 if (msg->opcode == IPC_OP_STATUS ||
127 msg->opcode > handlers[msg->service_id].size) {
128 DBG("invalid opcode 0x%x for service 0x%x", msg->opcode,
133 /* opcode is table offset + 1 */
134 handler = &handlers[msg->service_id].handler[msg->opcode - 1];
136 /* if payload size is valid */
137 if ((handler->var_len && handler->data_len > msg->len) ||
138 (!handler->var_len && handler->data_len != msg->len)) {
139 DBG("invalid size for opcode 0x%x service 0x%x",
140 msg->opcode, msg->service_id);
144 handler->handler(msg->payload, msg->len);
149 static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
152 struct ipc *ipc = user_data;
158 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
159 info("IPC: command socket closed");
165 fd = g_io_channel_unix_get_fd(io);
167 ret = read(fd, buf, sizeof(buf));
169 error("IPC: command read failed (%s)", strerror(errno));
173 err = ipc_handle_msg(ipc->services, ipc->service_max, buf, ret);
175 error("IPC: failed to handle message (%s)", strerror(-err));
182 ipc_disconnect(ipc, false);
187 static gboolean notif_watch_cb(GIOChannel *io, GIOCondition cond,
190 struct ipc *ipc = user_data;
192 info("IPC: notification socket closed");
194 ipc->notif_watch = 0;
196 ipc_disconnect(ipc, false);
201 static GIOChannel *ipc_connect(const char *path, size_t size,
202 GIOFunc connect_cb, void *user_data)
204 struct sockaddr_un addr;
209 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
211 error("IPC: failed to create socket: %d (%s)", errno,
216 io = g_io_channel_unix_new(sk);
218 g_io_channel_set_close_on_unref(io, TRUE);
219 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
221 memset(&addr, 0, sizeof(addr));
222 addr.sun_family = AF_UNIX;
224 memcpy(addr.sun_path, path, size);
226 connect(sk, (struct sockaddr *) &addr, sizeof(addr));
228 cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
230 g_io_add_watch(io, cond, connect_cb, user_data);
235 static gboolean notif_connect_cb(GIOChannel *io, GIOCondition cond,
238 struct ipc *ipc = user_data;
242 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
243 error("IPC: notification socket connect failed");
245 ipc_disconnect(ipc, false);
250 cond = G_IO_ERR | G_IO_HUP | G_IO_NVAL;
252 ipc->notif_watch = g_io_add_watch(io, cond, notif_watch_cb, ipc);
254 cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
256 ipc->cmd_watch = g_io_add_watch(ipc->cmd_io, cond, cmd_watch_cb, ipc);
258 info("IPC: successfully connected (with notifications)");
263 static gboolean cmd_connect_cb(GIOChannel *io, GIOCondition cond,
266 struct ipc *ipc = user_data;
270 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
271 error("IPC: command socket connect failed");
272 ipc_disconnect(ipc, false);
277 if (ipc->notifications) {
278 ipc->notif_io = ipc_connect(ipc->path, ipc->size,
279 notif_connect_cb, ipc);
281 ipc_disconnect(ipc, false);
286 cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
288 ipc->cmd_watch = g_io_add_watch(ipc->cmd_io, cond, cmd_watch_cb, ipc);
290 info("IPC: successfully connected (without notifications)");
295 struct ipc *ipc_init(const char *path, size_t size, int max_service_id,
297 ipc_disconnect_cb cb, void *cb_data)
301 ipc = g_new0(struct ipc, 1);
303 ipc->services = g_new0(struct service_handler, max_service_id + 1);
304 ipc->service_max = max_service_id;
309 ipc->notifications = notifications;
311 ipc->cmd_io = ipc_connect(path, size, cmd_connect_cb, ipc);
313 g_free(ipc->services);
318 ipc->disconnect_cb = cb;
319 ipc->disconnect_cb_data = cb_data;
324 void ipc_cleanup(struct ipc *ipc)
326 ipc_disconnect(ipc, true);
328 g_free(ipc->services);
332 static void ipc_send(int sk, uint8_t service_id, uint8_t opcode, uint16_t len,
338 char cmsgbuf[CMSG_SPACE(sizeof(int))];
339 struct cmsghdr *cmsg;
341 memset(&msg, 0, sizeof(msg));
342 memset(&m, 0, sizeof(m));
343 memset(cmsgbuf, 0, sizeof(cmsgbuf));
345 m.service_id = service_id;
350 iv[0].iov_len = sizeof(m);
352 iv[1].iov_base = param;
359 msg.msg_control = cmsgbuf;
360 msg.msg_controllen = sizeof(cmsgbuf);
362 cmsg = CMSG_FIRSTHDR(&msg);
363 cmsg->cmsg_level = SOL_SOCKET;
364 cmsg->cmsg_type = SCM_RIGHTS;
365 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
367 /* Initialize the payload */
368 memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
371 if (sendmsg(sk, &msg, 0) < 0) {
372 error("IPC send failed :%s", strerror(errno));
374 /* TODO disconnect IPC here when this function becomes static */
379 void ipc_send_rsp(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
385 sk = g_io_channel_unix_get_fd(ipc->cmd_io);
387 if (status == IPC_STATUS_SUCCESS) {
388 ipc_send(sk, service_id, opcode, 0, NULL, -1);
394 ipc_send(sk, service_id, IPC_OP_STATUS, sizeof(s), &s, -1);
397 void ipc_send_rsp_full(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
398 uint16_t len, void *param, int fd)
400 ipc_send(g_io_channel_unix_get_fd(ipc->cmd_io), service_id, opcode, len,
404 void ipc_send_notif(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
405 uint16_t len, void *param)
407 return ipc_send_notif_with_fd(ipc, service_id, opcode, len, param, -1);
410 void ipc_send_notif_with_fd(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
411 uint16_t len, void *param, int fd)
413 if (!ipc || !ipc->notif_io)
416 ipc_send(g_io_channel_unix_get_fd(ipc->notif_io), service_id, opcode,
420 void ipc_register(struct ipc *ipc, uint8_t service,
421 const struct ipc_handler *handlers, uint8_t size)
423 if (service > ipc->service_max)
426 ipc->services[service].handler = handlers;
427 ipc->services[service].size = size;
430 void ipc_unregister(struct ipc *ipc, uint8_t service)
432 if (service > ipc->service_max)
435 ipc->services[service].handler = NULL;
436 ipc->services[service].size = 0;