1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/*
* Copyright (C) 2013 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <pthread.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdbool.h>
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <cutils/properties.h>
#include "hal.h"
#include "hal-msg.h"
#include "hal-log.h"
#include "hal-ipc.h"
#define CONNECT_TIMEOUT (5 * 1000)
#define SERVICE_NAME "bluetoothd"
static int cmd_sk = -1;
static int notif_sk = -1;
static pthread_mutex_t cmd_sk_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t notif_th = 0;
static void notification_dispatch(struct hal_hdr *msg, int fd)
{
switch (msg->service_id) {
case HAL_SERVICE_ID_BLUETOOTH:
bt_notify_adapter(msg->opcode, msg->payload, msg->len);
break;
case HAL_SERVICE_ID_HIDHOST:
bt_notify_hh(msg->opcode, msg->payload, msg->len);
break;
case HAL_SERVICE_ID_A2DP:
bt_notify_av(msg->opcode, msg->payload, msg->len);
break;
default:
DBG("Unhandled notification service=%d opcode=0x%x",
msg->service_id, msg->opcode);
break;
}
}
static void *notification_handler(void *data)
{
struct msghdr msg;
struct iovec iv;
struct cmsghdr *cmsg;
char cmsgbuf[CMSG_SPACE(sizeof(int))];
char buf[BLUEZ_HAL_MTU];
struct hal_hdr *ev = (void *) buf;
ssize_t ret;
int fd;
bt_thread_associate();
while (true) {
memset(&msg, 0, sizeof(msg));
memset(buf, 0, sizeof(buf));
memset(cmsgbuf, 0, sizeof(cmsgbuf));
iv.iov_base = ev;
iv.iov_len = sizeof(buf);
msg.msg_iov = &iv;
msg.msg_iovlen = 1;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
ret = recvmsg(notif_sk, &msg, 0);
if (ret < 0) {
error("Receiving notifications failed, aborting :%s",
strerror(errno));
exit(EXIT_FAILURE);
}
/* socket was shutdown */
if (ret == 0) {
if (cmd_sk == -1)
break;
error("Notification socket closed, aborting");
exit(EXIT_FAILURE);
}
if (ret < (ssize_t) sizeof(*ev)) {
error("Too small notification (%zd bytes), aborting",
ret);
exit(EXIT_FAILURE);
}
if (ev->opcode < HAL_MINIMUM_EVENT) {
error("Invalid notification (0x%x), aborting",
ev->opcode);
exit(EXIT_FAILURE);
}
if (ret != (ssize_t) (sizeof(*ev) + ev->len)) {
error("Malformed notification(%zd bytes), aborting",
ret);
exit(EXIT_FAILURE);
}
fd = -1;
/* Receive auxiliary data in msg */
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET
&& cmsg->cmsg_type == SCM_RIGHTS) {
memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
break;
}
}
notification_dispatch(ev, fd);
}
close(notif_sk);
notif_sk = -1;
bt_thread_disassociate();
DBG("exit");
return NULL;
}
static int accept_connection(int sk)
{
int err;
struct pollfd pfd;
int new_sk;
memset(&pfd, 0 , sizeof(pfd));
pfd.fd = sk;
pfd.events = POLLIN;
err = poll(&pfd, 1, CONNECT_TIMEOUT);
if (err < 0) {
err = errno;
error("Failed to poll: %d (%s)", err, strerror(err));
return -1;
}
if (err == 0) {
error("bluetoothd connect timeout");
return -1;
}
new_sk = accept(sk, NULL, NULL);
if (new_sk < 0) {
err = errno;
error("Failed to accept socket: %d (%s)", err, strerror(err));
return -1;
}
return new_sk;
}
bool hal_ipc_init(void)
{
struct sockaddr_un addr;
int sk;
int err;
sk = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
if (sk < 0) {
err = errno;
error("Failed to create socket: %d (%s)", err,
strerror(err));
return false;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path, BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH));
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
err = errno;
error("Failed to bind socket: %d (%s)", err, strerror(err));
close(sk);
return false;
}
if (listen(sk, 2) < 0) {
err = errno;
error("Failed to listen on socket: %d (%s)", err,
strerror(err));
close(sk);
return false;
}
/* Start Android Bluetooth daemon service */
if (property_set("ctl.start", SERVICE_NAME) < 0) {
error("Failed to start service %s", SERVICE_NAME);
close(sk);
return false;
}
cmd_sk = accept_connection(sk);
if (cmd_sk < 0) {
close(sk);
return false;
}
notif_sk = accept_connection(sk);
if (notif_sk < 0) {
close(sk);
close(cmd_sk);
cmd_sk = -1;
return false;
}
info("bluetoothd connected");
close(sk);
err = pthread_create(¬if_th, NULL, notification_handler, NULL);
if (err < 0) {
notif_th = 0;
error("Failed to start notification thread: %d (%s)", -err,
strerror(-err));
close(cmd_sk);
cmd_sk = -1;
close(notif_sk);
notif_sk = -1;
return false;
}
return true;
}
void hal_ipc_cleanup(void)
{
close(cmd_sk);
cmd_sk = -1;
shutdown(notif_sk, SHUT_RD);
pthread_join(notif_th, NULL);
notif_th = 0;
}
int hal_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len, void *param,
size_t *rsp_len, void *rsp, int *fd)
{
ssize_t ret;
struct msghdr msg;
struct iovec iv[2];
struct hal_hdr cmd;
char cmsgbuf[CMSG_SPACE(sizeof(int))];
struct hal_status s;
size_t s_len = sizeof(s);
if (cmd_sk < 0) {
error("Invalid cmd socket passed to hal_ipc_cmd, aborting");
exit(EXIT_FAILURE);
}
if (!rsp || !rsp_len) {
memset(&s, 0, s_len);
rsp_len = &s_len;
rsp = &s;
}
memset(&msg, 0, sizeof(msg));
memset(&cmd, 0, sizeof(cmd));
cmd.service_id = service_id;
cmd.opcode = opcode;
cmd.len = len;
iv[0].iov_base = &cmd;
iv[0].iov_len = sizeof(cmd);
iv[1].iov_base = param;
iv[1].iov_len = len;
msg.msg_iov = iv;
msg.msg_iovlen = 2;
pthread_mutex_lock(&cmd_sk_mutex);
ret = sendmsg(cmd_sk, &msg, 0);
if (ret < 0) {
error("Sending command failed, aborting :%s", strerror(errno));
pthread_mutex_unlock(&cmd_sk_mutex);
exit(EXIT_FAILURE);
}
memset(&msg, 0, sizeof(msg));
memset(&cmd, 0, sizeof(cmd));
iv[0].iov_base = &cmd;
iv[0].iov_len = sizeof(cmd);
iv[1].iov_base = rsp;
iv[1].iov_len = *rsp_len;
msg.msg_iov = iv;
msg.msg_iovlen = 2;
if (fd) {
memset(cmsgbuf, 0, sizeof(cmsgbuf));
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
}
ret = recvmsg(cmd_sk, &msg, 0);
if (ret < 0) {
error("Receiving command response failed, aborting :%s",
strerror(errno));
pthread_mutex_unlock(&cmd_sk_mutex);
exit(EXIT_FAILURE);
}
pthread_mutex_unlock(&cmd_sk_mutex);
if (ret < (ssize_t) sizeof(cmd)) {
error("Too small response received(%zd bytes), aborting", ret);
exit(EXIT_FAILURE);
}
if (cmd.service_id != service_id) {
error("Invalid service id (%u vs %u), aborting",
cmd.service_id, service_id);
exit(EXIT_FAILURE);
}
if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
error("Malformed response received(%zd bytes), aborting", ret);
exit(EXIT_FAILURE);
}
if (cmd.opcode != opcode && cmd.opcode != HAL_OP_STATUS) {
error("Invalid opcode received (%u vs %u), aborting",
cmd.opcode, opcode);
exit(EXIT_FAILURE);
}
if (cmd.opcode == HAL_OP_STATUS) {
struct hal_status *s = rsp;
return s->code;
}
/* Receive auxiliary data in msg */
if (fd) {
struct cmsghdr *cmsg;
*fd = -1;
for (cmsg = CMSG_FIRSTHDR(&msg); !cmsg;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET
&& cmsg->cmsg_type == SCM_RIGHTS) {
memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
break;
}
}
}
if (rsp_len)
*rsp_len = cmd.len;
return BT_STATUS_SUCCESS;
}
|