system/bluetooth
Revision | bbea219aeba0f80f2e2461d7cd515b4bd86049db (tree) |
---|---|
Time | 2010-04-16 19:21:49 |
Author | Chih-Wei Huang <cwhuang@linu...> |
Commiter | 黃志偉 |
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
* bt_enable(): try to disable and re-enable eeepc-bluetooth if unable
@@ -63,7 +63,7 @@ static int init_rfkill() { | ||
63 | 63 | while((entry = readdir(sysdir)) != NULL) { |
64 | 64 | if (!(strcmp(".", entry->d_name) && strcmp("..", entry->d_name))) |
65 | 65 | 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); | |
67 | 67 | fd = open(path, O_RDONLY); |
68 | 68 | if (fd < 0) { |
69 | 69 | LOGW("open(%s) failed: %s (%d)\n", path, strerror(errno), errno); |
@@ -71,7 +71,7 @@ static int init_rfkill() { | ||
71 | 71 | } |
72 | 72 | sz = read(fd, &buf, sizeof(buf)); |
73 | 73 | close(fd); |
74 | - if (sz >= 3 && memcmp(buf, "hci", 3) == 0) { | |
74 | + if (sz >= 9 && memcmp(buf, "bluetooth", 9) == 0) { | |
75 | 75 | char *sp; |
76 | 76 | if (asprintf(&sp, "/sys/class/rfkill/%s/state", entry->d_name) > 0) { |
77 | 77 | LOGI("found bluetooth at %s\n", path); |
@@ -86,7 +86,6 @@ static int init_rfkill() { | ||
86 | 86 | } |
87 | 87 | |
88 | 88 | static int check_bluetooth_power() { |
89 | - int sz; | |
90 | 89 | int fd = -1; |
91 | 90 | int ret = -1; |
92 | 91 | char buffer; |
@@ -101,8 +100,7 @@ static int check_bluetooth_power() { | ||
101 | 100 | errno); |
102 | 101 | goto out; |
103 | 102 | } |
104 | - sz = read(fd, &buffer, 1); | |
105 | - if (sz != 1) { | |
103 | + if (read(fd, &buffer, 1) != 1) { | |
106 | 104 | LOGE("read(%s) failed: %s (%d)", rfkill_state_path, strerror(errno), |
107 | 105 | errno); |
108 | 106 | goto out; |
@@ -123,14 +121,14 @@ out: | ||
123 | 121 | } |
124 | 122 | |
125 | 123 | static int set_bluetooth_power(int on) { |
126 | - int sz; | |
127 | 124 | int fd = -1; |
128 | - int ret = -1; | |
125 | + int ret = check_bluetooth_power(); | |
129 | 126 | const char buffer = (on ? '1' : '0'); |
130 | 127 | |
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; | |
134 | 132 | |
135 | 133 | fd = open(rfkill_state_path, O_WRONLY); |
136 | 134 | if (fd < 0) { |
@@ -138,8 +136,7 @@ static int set_bluetooth_power(int on) { | ||
138 | 136 | strerror(errno), errno); |
139 | 137 | goto out; |
140 | 138 | } |
141 | - sz = write(fd, &buffer, 1); | |
142 | - if (sz < 0) { | |
139 | + if (write(fd, &buffer, 1) != 1) { | |
143 | 140 | LOGE("write(%s) failed: %s (%d)", rfkill_state_path, strerror(errno), |
144 | 141 | errno); |
145 | 142 | goto out; |
@@ -151,7 +148,7 @@ out: | ||
151 | 148 | return ret; |
152 | 149 | } |
153 | 150 | |
154 | -static inline int create_hci_sock() { | |
151 | +static int create_hci_sock() { | |
155 | 152 | int sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); |
156 | 153 | if (sk < 0) { |
157 | 154 | LOGE("Failed to create bluetooth hci socket: %s (%d)", |
@@ -167,8 +164,6 @@ int bt_enable() { | ||
167 | 164 | int hci_sock = -1; |
168 | 165 | int attempt; |
169 | 166 | |
170 | - if (set_bluetooth_power(1) < 0) goto out; | |
171 | - | |
172 | 167 | LOGI("Starting hciattach daemon"); |
173 | 168 | if (property_set("ctl.start", "hciattach") < 0) { |
174 | 169 | LOGE("Failed to start hciattach"); |
@@ -177,13 +172,20 @@ int bt_enable() { | ||
177 | 172 | |
178 | 173 | // Try for 10 seconds, this can only succeed once hciattach has sent the |
179 | 174 | // 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 | + } | |
181 | 182 | hci_sock = create_hci_sock(); |
182 | 183 | if (hci_sock < 0) goto out; |
183 | 184 | |
184 | - if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) { | |
185 | + if (!ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID)) | |
185 | 186 | break; |
186 | - } | |
187 | + | |
188 | + LOGE("ioctl failed: %s (%d)", strerror(errno), errno); | |
187 | 189 | close(hci_sock); |
188 | 190 | usleep(10000); // 10 ms retry delay |
189 | 191 | } |