From d36e2308be3c1164e0aa93284b157c99c415ee65 Mon Sep 17 00:00:00 2001 From: Chih-Wei Huang Date: Fri, 6 Nov 2009 18:42:29 +0800 Subject: [PATCH] bluedroid: fix several issues to enable a bluetooth device * init_rfkill(): use the first rkilll interface (eeepc-bluetooth) * set_bluetooth_power(): check the rfkill state before set it. If the state is already the expected value, don't write to it. This avoids the permission issue after wakeup for external bluetooth dongle. * bt_enable(): try to disable and re-enable eeepc-bluetooth if unable to turn on it, since the rfkill id may has changed. --- bluedroid/bluetooth.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/bluedroid/bluetooth.c b/bluedroid/bluetooth.c index c2ee4a3..747daaa 100644 --- a/bluedroid/bluetooth.c +++ b/bluedroid/bluetooth.c @@ -61,7 +61,7 @@ static int init_rfkill() { while((entry = readdir(sysdir)) != NULL) { if (!(strcmp(".", entry->d_name) && strcmp("..", entry->d_name))) continue; - snprintf(path, sizeof(path), "%s/%s/name", sysrfkill, entry->d_name); + snprintf(path, sizeof(path), "%s/%s/type", sysrfkill, entry->d_name); fd = open(path, O_RDONLY); if (fd < 0) { LOGW("open(%s) failed: %s (%d)\n", path, strerror(errno), errno); @@ -69,7 +69,7 @@ static int init_rfkill() { } sz = read(fd, &buf, sizeof(buf)); close(fd); - if (sz >= 3 && memcmp(buf, "hci", 3) == 0) { + if (sz >= 9 && memcmp(buf, "bluetooth", 9) == 0) { char *sp; if (asprintf(&sp, "/sys/class/rfkill/%s/state", entry->d_name) > 0) { LOGI("found bluetooth at %s\n", path); @@ -84,7 +84,6 @@ static int init_rfkill() { } static int check_bluetooth_power() { - int sz; int fd = -1; int ret = -1; char buffer; @@ -99,8 +98,7 @@ static int check_bluetooth_power() { errno); goto out; } - sz = read(fd, &buffer, 1); - if (sz != 1) { + if (read(fd, &buffer, 1) != 1) { LOGE("read(%s) failed: %s (%d)", rfkill_state_path, strerror(errno), errno); goto out; @@ -121,14 +119,14 @@ out: } static int set_bluetooth_power(int on) { - int sz; int fd = -1; - int ret = -1; + int ret = check_bluetooth_power(); const char buffer = (on ? '1' : '0'); - if (rfkill_state_path == NULL) { - if (init_rfkill()) goto out; - } + if (ret < 0) + return ret; + else if (ret == on) + return 0; fd = open(rfkill_state_path, O_WRONLY); if (fd < 0) { @@ -136,8 +134,7 @@ static int set_bluetooth_power(int on) { strerror(errno), errno); goto out; } - sz = write(fd, &buffer, 1); - if (sz < 0) { + if (write(fd, &buffer, 1) != 1) { LOGE("write(%s) failed: %s (%d)", rfkill_state_path, strerror(errno), errno); goto out; @@ -149,7 +146,7 @@ out: return ret; } -static inline int create_hci_sock() { +static int create_hci_sock() { int sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (sk < 0) { LOGE("Failed to create bluetooth hci socket: %s (%d)", @@ -165,8 +162,6 @@ int bt_enable() { int hci_sock = -1; int attempt; - if (set_bluetooth_power(1) < 0) goto out; - LOGI("Starting hciattach daemon"); if (property_set("ctl.start", "hciattach") < 0) { LOGE("Failed to start hciattach"); @@ -175,13 +170,20 @@ int bt_enable() { // Try for 10 seconds, this can only succeed once hciattach has sent the // firmware and then turned on hci device via HCIUARTSETPROTO ioctl - for (attempt = 1000; attempt > 0; attempt--) { + for (attempt = 10; attempt > 0; --attempt) { + int res = set_bluetooth_power(1); + sleep(1); + if (res < 0) { + bt_disable(); + continue; + } hci_sock = create_hci_sock(); if (hci_sock < 0) goto out; - if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) { + if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) break; - } + + LOGE("ioctl failed: %s (%d)", strerror(errno), errno); close(hci_sock); usleep(10000); // 10 ms retry delay } -- 2.11.0