-
Notifications
You must be signed in to change notification settings - Fork 15
/
bluedroid.c
499 lines (405 loc) · 10.9 KB
/
bluedroid.c
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Copyright (C) 2008 The Android Open Source Project
*
* 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.
*/
#define LOG_TAG "bluedroid"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <cutils/log.h>
#include <cutils/properties.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluedroid/bluetooth.h>
#ifndef HCI_DEV_ID
#define HCI_DEV_ID 0
#endif
#define HCID_START_DELAY_SEC 3
#define HCID_STOP_DELAY_USEC 500000
#define MIN(x,y) (((x)<(y))?(x):(y))
static int rfkill_id = -1;
static char *rfkill_state_path = NULL;
static int init_rfkill() {
char path[64];
char buf[16];
int fd;
int sz;
int id;
for (id = 0; ; id++) {
snprintf(path, sizeof(path), "/sys/class/rfkill/rfkill%d/type", id);
fd = open(path, O_RDONLY);
if (fd < 0) {
ALOGW("open(%s) failed: %s (%d)\n", path, strerror(errno), errno);
return -1;
}
sz = read(fd, &buf, sizeof(buf));
close(fd);
if (sz >= 9 && memcmp(buf, "bluetooth", 9) == 0) {
rfkill_id = id;
break;
}
}
asprintf(&rfkill_state_path, "/sys/class/rfkill/rfkill%d/state", rfkill_id);
return 0;
}
static int check_bluetooth_power() {
int sz;
int fd = -1;
int ret = -1;
char buffer;
if (rfkill_id == -1) {
if (init_rfkill()) goto out;
}
fd = open(rfkill_state_path, O_RDONLY);
if (fd < 0) {
ALOGE("open(%s) failed: %s (%d)", rfkill_state_path, strerror(errno),
errno);
goto out;
}
sz = read(fd, &buffer, 1);
if (sz != 1) {
ALOGE("read(%s) failed: %s (%d)", rfkill_state_path, strerror(errno),
errno);
goto out;
}
switch (buffer) {
case '1':
ret = 1;
break;
case '0':
ret = 0;
break;
}
out:
if (fd >= 0) close(fd);
return ret;
}
static int set_bluetooth_power(int on) {
int sz;
int fd = -1;
int ret = -1;
const char buffer = (on ? '1' : '0');
if (rfkill_id == -1) {
if (init_rfkill()) goto out;
}
fd = open(rfkill_state_path, O_WRONLY);
if (fd < 0) {
ALOGE("open(%s) for write failed: %s (%d)", rfkill_state_path,
strerror(errno), errno);
goto out;
}
sz = write(fd, &buffer, 1);
if (sz < 0) {
ALOGE("write(%s) failed: %s (%d)", rfkill_state_path, strerror(errno),
errno);
goto out;
}
ret = 0;
out:
if (fd >= 0) close(fd);
return ret;
}
static inline int create_hci_sock() {
int sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
if (sk < 0) {
ALOGE("Failed to create bluetooth hci socket: %s (%d)",
strerror(errno), errno);
}
return sk;
}
struct RefBase_s {
unsigned int magic;
unsigned int base;
};
static const char const * REF_FILE = "/tmp/bluedroid_ref";
static const unsigned int MAGIC = 0x55665566;
static int inc_atomic()
{
struct RefBase_s ref = {magic : MAGIC, base : 0};
/*l_type l_whence l_start l_len l_pid*/
struct flock fl = {F_RDLCK, SEEK_SET, 0, 0, 0};
int fd = -1;
int ret = -1;
int magic = MAGIC;
ALOGV("Enter inc_atomic");
if ((fd = open(REF_FILE, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO)) == -1)
{
ALOGW("btdroid warning: open or create bluedroid_ref error");
ret = -2;
goto out;
}
ALOGV("Trying to get lock in inc_atomic....");
if (fcntl(fd, F_SETLKW, &fl) == -1)
{
ALOGW("btdroid waring: F_SETLKW failed");
ret = -1;
goto out;
}
ALOGV("got lock");
if (read(fd, (void *)&ref, sizeof(ref)) == -1)
{
ALOGW("btdroid waring: read reference count error");
ret = -1;
goto out;
}
if(ref.magic != MAGIC)
{
ALOGV("btdroid reference file magic number is 0x5566");
ref.magic = MAGIC;
ref.base = 0;
}
if(ref.base > 3) ALOGW("btdroid waring: btdroid reference count > 3 base = %d", ref.base);
ref.base += 1;
fl.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &fl) == -1)
{
ALOGW("btdroid waring: F_SETLK : F_UNLCK failed");
ret = -1;
goto out;
}
fl.l_type = F_WRLCK;
if (fcntl(fd, F_SETLKW, &fl) == -1)
{
ALOGW("btdroid waring: F_SETLKW : F_WRLCK failed");
ret = -1;
goto out;
}
lseek(fd, 0, SEEK_SET);
if (write(fd, (const void*)&ref, sizeof(ref)) == -1)
{
ALOGW("btdroid waring: write reference count failed");
ret = -1;
goto out;
}
ret = ref.base;
ALOGV("Exit inc_atomic ref = %d", ret);
out:
if(fd != -1) close(fd);
return ret;
}
static int dec_atomic()
{
struct RefBase_s ref = {magic : MAGIC, base : 0};
/*l_type l_whence l_start l_len l_pid*/
struct flock fl = {F_RDLCK, SEEK_SET, 0, 0, 0};
int fd = -1;
int ret = -1;
int magic = MAGIC;
ALOGV("Enter dec_atomic");
if ((fd = open(REF_FILE, O_RDWR)) == -1)
{
ALOGW("btdroid warning: open or create bluedroid_ref error");
ret = -2;
goto out;
}
ALOGV("Trying to get lock in inc_atomic....");
if (fcntl(fd, F_SETLKW, &fl) == -1)
{
ALOGW("btdroid waring: F_SETLKW failed");
ret = -1;
goto out;
}
ALOGV("got lock");
if (read(fd, (void *)&ref, sizeof(ref)) == -1)
{
ALOGW("btdroid waring: read reference count error");
ret = -1;
goto out;
}
if(ref.magic != MAGIC)
{
ALOGV("btdroid reference file magic number is 0x5566");
ref.magic = MAGIC;
ref.base = 1;
}
if(ref.base == 0)
{
ALOGW("btdroid waring: dereference btdroid reference base = %d", ref.base);
ref.base = 1;
}
ref.base -= 1;
fl.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &fl) == -1)
{
ALOGW("btdroid waring: F_SETLK : F_UNLCK failed");
ret = -1;
goto out;
}
fl.l_type = F_WRLCK;
if (fcntl(fd, F_SETLKW, &fl) == -1)
{
ALOGW("btdroid waring: F_SETLKW : F_WRLCK failed");
ret = -1;
goto out;
}
lseek(fd, 0, SEEK_SET);
if (write(fd, (const void*)&ref, sizeof(ref)) == -1)
{
ALOGW("btdroid waring: write reference count failed");
ret = -1;
goto out;
}
ret = ref.base;
ALOGV("Exit inc_atomic ref = %d", ret);
out:
if(fd != -1) close(fd);
return ret;
}
int bt_hw_en() {
ALOGV(__FUNCTION__);
int ret = -1;
int hci_sock = -1;
int attempt;
int refcount = dec_atomic();
if (bt_is_enabled() == 1) {
ALOGI("bt has been enabled already. inc reference count only");
ret = 0;
goto out;
}
if (set_bluetooth_power(1) < 0) {
goto out;
}
ALOGI("Starting hciattach daemon");
if (property_set("ctl.start", "hciattach") < 0) {
ALOGE("Failed to start hciattach");
set_bluetooth_power(0);
goto out;
}
// Try for 10 seconds, this can only succeed once hciattach has sent the
// firmware and then turned on hci device via HCIUARTSETPROTO ioctl
hci_sock = create_hci_sock();
if (hci_sock < 0) goto out;
for (attempt = 1000; attempt > 0; attempt--) {
if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) {
break;
}
usleep(10000); // 10 ms retry delay
}
if (attempt == 0) {
ALOGE("%s: Timeout waiting for HCI device to come up", __FUNCTION__);
set_bluetooth_power(0);
goto out;
}
ret = 0;
out:
if (hci_sock >= 0) close(hci_sock);
if(0 == ret)
{
inc_atomic();
ALOGV("after inc : reference count = %d", refcount);
}
return ret;
}
int bt_hw_dis() {
ALOGV(__FUNCTION__);
int ret = -1;
int refcount = dec_atomic();
int hci_sock = -1;
ALOGV("bt_disable() : reference count now = %d", refcount);
if(refcount > 0)
{
ALOGV("bt_disable: other app using bt so just do noting");
return 0;
}
usleep(HCID_STOP_DELAY_USEC);
hci_sock = create_hci_sock();
if (hci_sock < 0) goto out;
ioctl(hci_sock, HCIDEVDOWN, HCI_DEV_ID);
ALOGI("Stopping hciattach deamon");
if (property_set("ctl.stop", "hciattach") < 0) {
ALOGE("Error stopping hciattach");
goto out;
}
if (set_bluetooth_power(0) < 0) {
goto out;
}
ret = 0;
out:
if (hci_sock >= 0) close(hci_sock);
return ret;
}
int bt_enable() {
ALOGV(__FUNCTION__);
int ret = bt_hw_en();
if (ret < 0) {
return ret;
}
ALOGI("Starting bluetoothd deamon");
if (property_set("ctl.start", "bluetoothd") < 0) {
ALOGE("Failed to start bluetoothd");
bt_hw_dis();
return -1;
}
sleep(HCID_START_DELAY_SEC);
return 0;
}
int bt_disable() {
ALOGV(__FUNCTION__);
ALOGI("Stopping bluetoothd deamon");
if (property_set("ctl.stop", "bluetoothd") < 0) {
ALOGE("Error stopping bluetoothd");
}
usleep(HCID_STOP_DELAY_USEC);
return bt_hw_dis();
}
int bt_is_enabled() {
ALOGV(__FUNCTION__);
int hci_sock = -1;
int ret = -1;
struct hci_dev_info dev_info;
// Check power first
ret = check_bluetooth_power();
// ALOGE("%s: check_bluetooth_power %d",__func__,ret);
if (ret == -1 || ret == 0) goto out;
ret = -1;
// Power is on, now check if the HCI interface is up
hci_sock = create_hci_sock();
// ALOGE("%s: create_hci_sock %d",__func__,hci_sock);
if (hci_sock < 0) goto out;
dev_info.dev_id = HCI_DEV_ID;
ret = ioctl(hci_sock, HCIGETDEVINFO, (void *)&dev_info);
// ALOGE("%s: ioctl(hci_sock, HCIGETDEVINFO, (void *)&dev_info) %d",__func__,ret);
if (ret < 0) {
ret = 0;
goto out;
}
if (dev_info.flags & (1 << (HCI_UP & 31))) {
ret = 1;
} else {
ret = 0;
}
out:
// ALOGE("%s: Return ret: %d",__func__,ret);
if (hci_sock >= 0) close(hci_sock);
return ret;
}
int ba2str(const bdaddr_t *ba, char *str) {
return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);
}
int str2ba(const char *str, bdaddr_t *ba) {
int i;
for (i = 5; i >= 0; i--) {
ba->b[i] = (uint8_t) strtoul(str, &str, 16);
str++;
}
return 0;
}