Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / android / ipc.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2013-2014  Intel Corporation. All rights reserved.
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stddef.h>
29 #include <errno.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <stdbool.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <unistd.h>
37 #include <glib.h>
38
39 #include "ipc-common.h"
40 #include "ipc.h"
41 #include "src/log.h"
42
43 struct service_handler {
44         const struct ipc_handler *handler;
45         uint8_t size;
46 };
47
48 struct ipc {
49         struct service_handler *services;
50         int service_max;
51
52         const char *path;
53         size_t size;
54
55         GIOChannel *cmd_io;
56         guint cmd_watch;
57
58         bool notifications;
59         GIOChannel *notif_io;
60         guint notif_watch;
61
62         ipc_disconnect_cb disconnect_cb;
63         void *disconnect_cb_data;
64 };
65
66 static void ipc_disconnect(struct ipc *ipc, bool in_cleanup)
67 {
68         if (ipc->cmd_watch) {
69                 g_source_remove(ipc->cmd_watch);
70                 ipc->cmd_watch = 0;
71         }
72
73         if (ipc->cmd_io) {
74                 g_io_channel_shutdown(ipc->cmd_io, TRUE, NULL);
75                 g_io_channel_unref(ipc->cmd_io);
76                 ipc->cmd_io = NULL;
77         }
78
79         if (ipc->notif_watch) {
80                 g_source_remove(ipc->notif_watch);
81                 ipc->notif_watch = 0;
82         }
83
84         if (ipc->notif_io) {
85                 g_io_channel_shutdown(ipc->notif_io, TRUE, NULL);
86                 g_io_channel_unref(ipc->notif_io);
87                 ipc->notif_io = NULL;
88         }
89
90         if (in_cleanup)
91                 return;
92
93         if (ipc->disconnect_cb)
94                 ipc->disconnect_cb(ipc->disconnect_cb_data);
95 }
96
97 static int ipc_handle_msg(struct service_handler *handlers, size_t max_index,
98                                                 const void *buf, ssize_t len)
99 {
100         const struct ipc_hdr *msg = buf;
101         const struct ipc_handler *handler;
102
103         if (len < (ssize_t) sizeof(*msg)) {
104                 DBG("message too small (%zd bytes)", len);
105                 return -EBADMSG;
106         }
107
108         if (len != (ssize_t) (sizeof(*msg) + msg->len)) {
109                 DBG("message malformed (%zd bytes)", len);
110                 return -EBADMSG;
111         }
112
113         /* if service is valid */
114         if (msg->service_id > max_index) {
115                 DBG("unknown service (0x%x)", msg->service_id);
116                 return -EOPNOTSUPP;
117         }
118
119         /* if service is registered */
120         if (!handlers[msg->service_id].handler) {
121                 DBG("service not registered (0x%x)", msg->service_id);
122                 return -EOPNOTSUPP;
123         }
124
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,
129                                                         msg->service_id);
130                 return -EOPNOTSUPP;
131         }
132
133         /* opcode is table offset + 1 */
134         handler = &handlers[msg->service_id].handler[msg->opcode - 1];
135
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);
141                 return -EMSGSIZE;
142         }
143
144         handler->handler(msg->payload, msg->len);
145
146         return 0;
147 }
148
149 static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
150                                                         gpointer user_data)
151 {
152         struct ipc *ipc = user_data;
153
154         char buf[IPC_MTU];
155         ssize_t ret;
156         int fd, err;
157
158         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
159                 info("IPC: command socket closed");
160
161                 ipc->cmd_watch = 0;
162                 goto fail;
163         }
164
165         fd = g_io_channel_unix_get_fd(io);
166
167         ret = read(fd, buf, sizeof(buf));
168         if (ret < 0) {
169                 error("IPC: command read failed (%s)", strerror(errno));
170                 goto fail;
171         }
172
173         err = ipc_handle_msg(ipc->services, ipc->service_max, buf, ret);
174         if (err < 0) {
175                 error("IPC: failed to handle message (%s)", strerror(-err));
176                 goto fail;
177         }
178
179         return TRUE;
180
181 fail:
182         ipc_disconnect(ipc, false);
183
184         return FALSE;
185 }
186
187 static gboolean notif_watch_cb(GIOChannel *io, GIOCondition cond,
188                                                         gpointer user_data)
189 {
190         struct ipc *ipc = user_data;
191
192         info("IPC: notification socket closed");
193
194         ipc->notif_watch = 0;
195
196         ipc_disconnect(ipc, false);
197
198         return FALSE;
199 }
200
201 static GIOChannel *ipc_connect(const char *path, size_t size,
202                                         GIOFunc connect_cb, void *user_data)
203 {
204         struct sockaddr_un addr;
205         GIOCondition cond;
206         GIOChannel *io;
207         int sk;
208
209         sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
210         if (sk < 0) {
211                 error("IPC: failed to create socket: %d (%s)", errno,
212                                                         strerror(errno));
213                 return NULL;
214         }
215
216         io = g_io_channel_unix_new(sk);
217
218         g_io_channel_set_close_on_unref(io, TRUE);
219         g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
220
221         memset(&addr, 0, sizeof(addr));
222         addr.sun_family = AF_UNIX;
223
224         memcpy(addr.sun_path, path, size);
225
226         connect(sk, (struct sockaddr *) &addr, sizeof(addr));
227
228         cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
229
230         g_io_add_watch(io, cond, connect_cb, user_data);
231
232         return io;
233 }
234
235 static gboolean notif_connect_cb(GIOChannel *io, GIOCondition cond,
236                                                         gpointer user_data)
237 {
238         struct ipc *ipc = user_data;
239
240         DBG("");
241
242         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
243                 error("IPC: notification socket connect failed");
244
245                 ipc_disconnect(ipc, false);
246
247                 return FALSE;
248         }
249
250         cond = G_IO_ERR | G_IO_HUP | G_IO_NVAL;
251
252         ipc->notif_watch = g_io_add_watch(io, cond, notif_watch_cb, ipc);
253
254         cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
255
256         ipc->cmd_watch = g_io_add_watch(ipc->cmd_io, cond, cmd_watch_cb, ipc);
257
258         info("IPC: successfully connected (with notifications)");
259
260         return FALSE;
261 }
262
263 static gboolean cmd_connect_cb(GIOChannel *io, GIOCondition cond,
264                                                         gpointer user_data)
265 {
266         struct ipc *ipc = user_data;
267
268         DBG("");
269
270         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
271                 error("IPC: command socket connect failed");
272                 ipc_disconnect(ipc, false);
273
274                 return FALSE;
275         }
276
277         if (ipc->notifications) {
278                 ipc->notif_io = ipc_connect(ipc->path, ipc->size,
279                                                         notif_connect_cb, ipc);
280                 if (!ipc->notif_io)
281                         ipc_disconnect(ipc, false);
282
283                 return FALSE;
284         }
285
286         cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
287
288         ipc->cmd_watch = g_io_add_watch(ipc->cmd_io, cond, cmd_watch_cb, ipc);
289
290         info("IPC: successfully connected (without notifications)");
291
292         return FALSE;
293 }
294
295 struct ipc *ipc_init(const char *path, size_t size, int max_service_id,
296                                         bool notifications,
297                                         ipc_disconnect_cb cb, void *cb_data)
298 {
299         struct ipc *ipc;
300
301         ipc = g_new0(struct ipc, 1);
302
303         ipc->services = g_new0(struct service_handler, max_service_id + 1);
304         ipc->service_max = max_service_id;
305
306         ipc->path = path;
307         ipc->size = size;
308
309         ipc->notifications = notifications;
310
311         ipc->cmd_io = ipc_connect(path, size, cmd_connect_cb, ipc);
312         if (!ipc->cmd_io) {
313                 g_free(ipc->services);
314                 g_free(ipc);
315                 return NULL;
316         }
317
318         ipc->disconnect_cb = cb;
319         ipc->disconnect_cb_data = cb_data;
320
321         return ipc;
322 }
323
324 void ipc_cleanup(struct ipc *ipc)
325 {
326         ipc_disconnect(ipc, true);
327
328         g_free(ipc->services);
329         g_free(ipc);
330 }
331
332 static void ipc_send(int sk, uint8_t service_id, uint8_t opcode, uint16_t len,
333                                                         void *param, int fd)
334 {
335         struct msghdr msg;
336         struct iovec iv[2];
337         struct ipc_hdr m;
338         char cmsgbuf[CMSG_SPACE(sizeof(int))];
339         struct cmsghdr *cmsg;
340
341         memset(&msg, 0, sizeof(msg));
342         memset(&m, 0, sizeof(m));
343         memset(cmsgbuf, 0, sizeof(cmsgbuf));
344
345         m.service_id = service_id;
346         m.opcode = opcode;
347         m.len = len;
348
349         iv[0].iov_base = &m;
350         iv[0].iov_len = sizeof(m);
351
352         iv[1].iov_base = param;
353         iv[1].iov_len = len;
354
355         msg.msg_iov = iv;
356         msg.msg_iovlen = 2;
357
358         if (fd >= 0) {
359                 msg.msg_control = cmsgbuf;
360                 msg.msg_controllen = sizeof(cmsgbuf);
361
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));
366
367                 /* Initialize the payload */
368                 memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
369         }
370
371         if (sendmsg(sk, &msg, 0) < 0) {
372                 error("IPC send failed :%s", strerror(errno));
373
374                 /* TODO disconnect IPC here when this function becomes static */
375                 raise(SIGTERM);
376         }
377 }
378
379 void ipc_send_rsp(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
380                                                                 uint8_t status)
381 {
382         struct ipc_status s;
383         int sk;
384
385         sk = g_io_channel_unix_get_fd(ipc->cmd_io);
386
387         if (status == IPC_STATUS_SUCCESS) {
388                 ipc_send(sk, service_id, opcode, 0, NULL, -1);
389                 return;
390         }
391
392         s.code = status;
393
394         ipc_send(sk, service_id, IPC_OP_STATUS, sizeof(s), &s, -1);
395 }
396
397 void ipc_send_rsp_full(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
398                                         uint16_t len, void *param, int fd)
399 {
400         ipc_send(g_io_channel_unix_get_fd(ipc->cmd_io), service_id, opcode, len,
401                                                                 param, fd);
402 }
403
404 void ipc_send_notif(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
405                                                 uint16_t len, void *param)
406 {
407         return ipc_send_notif_with_fd(ipc, service_id, opcode, len, param, -1);
408 }
409
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)
412 {
413         if (!ipc || !ipc->notif_io)
414                 return;
415
416         ipc_send(g_io_channel_unix_get_fd(ipc->notif_io), service_id, opcode,
417                                                                 len, param, fd);
418 }
419
420 void ipc_register(struct ipc *ipc, uint8_t service,
421                         const struct ipc_handler *handlers, uint8_t size)
422 {
423         if (service > ipc->service_max)
424                 return;
425
426         ipc->services[service].handler = handlers;
427         ipc->services[service].size = size;
428 }
429
430 void ipc_unregister(struct ipc *ipc, uint8_t service)
431 {
432         if (service > ipc->service_max)
433                 return;
434
435         ipc->services[service].handler = NULL;
436         ipc->services[service].size = 0;
437 }