Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

system-bluetooth: Commit

system/bluetooth


Commit MetaInfo

Revisionbbea219aeba0f80f2e2461d7cd515b4bd86049db (tree)
Time2010-04-16 19:21:49
AuthorChih-Wei Huang <cwhuang@linu...>
Commiter黃志偉

Log Message

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.

Change Summary

Incremental Difference

--- a/bluedroid/bluetooth.c
+++ b/bluedroid/bluetooth.c
@@ -63,7 +63,7 @@ static int init_rfkill() {
6363 while((entry = readdir(sysdir)) != NULL) {
6464 if (!(strcmp(".", entry->d_name) && strcmp("..", entry->d_name)))
6565 continue;
66- snprintf(path, sizeof(path), "%s/%s/name", sysrfkill, entry->d_name);
66+ snprintf(path, sizeof(path), "%s/%s/type", sysrfkill, entry->d_name);
6767 fd = open(path, O_RDONLY);
6868 if (fd < 0) {
6969 LOGW("open(%s) failed: %s (%d)\n", path, strerror(errno), errno);
@@ -71,7 +71,7 @@ static int init_rfkill() {
7171 }
7272 sz = read(fd, &buf, sizeof(buf));
7373 close(fd);
74- if (sz >= 3 && memcmp(buf, "hci", 3) == 0) {
74+ if (sz >= 9 && memcmp(buf, "bluetooth", 9) == 0) {
7575 char *sp;
7676 if (asprintf(&sp, "/sys/class/rfkill/%s/state", entry->d_name) > 0) {
7777 LOGI("found bluetooth at %s\n", path);
@@ -86,7 +86,6 @@ static int init_rfkill() {
8686 }
8787
8888 static int check_bluetooth_power() {
89- int sz;
9089 int fd = -1;
9190 int ret = -1;
9291 char buffer;
@@ -101,8 +100,7 @@ static int check_bluetooth_power() {
101100 errno);
102101 goto out;
103102 }
104- sz = read(fd, &buffer, 1);
105- if (sz != 1) {
103+ if (read(fd, &buffer, 1) != 1) {
106104 LOGE("read(%s) failed: %s (%d)", rfkill_state_path, strerror(errno),
107105 errno);
108106 goto out;
@@ -123,14 +121,14 @@ out:
123121 }
124122
125123 static int set_bluetooth_power(int on) {
126- int sz;
127124 int fd = -1;
128- int ret = -1;
125+ int ret = check_bluetooth_power();
129126 const char buffer = (on ? '1' : '0');
130127
131- if (rfkill_state_path == NULL) {
132- if (init_rfkill()) goto out;
133- }
128+ if (ret < 0)
129+ return ret;
130+ else if (ret == on)
131+ return 0;
134132
135133 fd = open(rfkill_state_path, O_WRONLY);
136134 if (fd < 0) {
@@ -138,8 +136,7 @@ static int set_bluetooth_power(int on) {
138136 strerror(errno), errno);
139137 goto out;
140138 }
141- sz = write(fd, &buffer, 1);
142- if (sz < 0) {
139+ if (write(fd, &buffer, 1) != 1) {
143140 LOGE("write(%s) failed: %s (%d)", rfkill_state_path, strerror(errno),
144141 errno);
145142 goto out;
@@ -151,7 +148,7 @@ out:
151148 return ret;
152149 }
153150
154-static inline int create_hci_sock() {
151+static int create_hci_sock() {
155152 int sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
156153 if (sk < 0) {
157154 LOGE("Failed to create bluetooth hci socket: %s (%d)",
@@ -167,8 +164,6 @@ int bt_enable() {
167164 int hci_sock = -1;
168165 int attempt;
169166
170- if (set_bluetooth_power(1) < 0) goto out;
171-
172167 LOGI("Starting hciattach daemon");
173168 if (property_set("ctl.start", "hciattach") < 0) {
174169 LOGE("Failed to start hciattach");
@@ -177,13 +172,20 @@ int bt_enable() {
177172
178173 // Try for 10 seconds, this can only succeed once hciattach has sent the
179174 // firmware and then turned on hci device via HCIUARTSETPROTO ioctl
180- for (attempt = 1000; attempt > 0; attempt--) {
175+ for (attempt = 10; attempt > 0; --attempt) {
176+ int res = set_bluetooth_power(1);
177+ sleep(1);
178+ if (res < 0) {
179+ bt_disable();
180+ continue;
181+ }
181182 hci_sock = create_hci_sock();
182183 if (hci_sock < 0) goto out;
183184
184- if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) {
185+ if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID))
185186 break;
186- }
187+
188+ LOGE("ioctl failed: %s (%d)", strerror(errno), errno);
187189 close(hci_sock);
188190 usleep(10000); // 10 ms retry delay
189191 }
Show on old repository browser