system/bluetooth
Revision | 4b5b3bcfc601d8ac802372dd09f317ecd4457969 (tree) |
---|---|
Time | 2011-12-19 15:35:49 |
Author | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
bluedroid: fix several issues to enable a bluetooth device
* set_bluetooth_power(): check the rfkill state before set it. If the
* bt_enable(): try to disable and re-enable eeepc-bluetooth if unable
Change-Id: Ib21637acf17ab2f3f8d4379ebd570f0cf85189da
@@ -82,7 +82,6 @@ static int init_rfkill() { | ||
82 | 82 | } |
83 | 83 | |
84 | 84 | static int check_bluetooth_power() { |
85 | - int sz; | |
86 | 85 | int fd = -1; |
87 | 86 | int ret = -1; |
88 | 87 | char buffer; |
@@ -97,8 +96,7 @@ static int check_bluetooth_power() { | ||
97 | 96 | errno); |
98 | 97 | goto out; |
99 | 98 | } |
100 | - sz = read(fd, &buffer, 1); | |
101 | - if (sz != 1) { | |
99 | + if (read(fd, &buffer, 1) != 1) { | |
102 | 100 | LOGE("read(%s) failed: %s (%d)", rfkill_state_path, strerror(errno), |
103 | 101 | errno); |
104 | 102 | goto out; |
@@ -119,14 +117,14 @@ out: | ||
119 | 117 | } |
120 | 118 | |
121 | 119 | static int set_bluetooth_power(int on) { |
122 | - int sz; | |
123 | 120 | int fd = -1; |
124 | - int ret = -1; | |
121 | + int ret = check_bluetooth_power(); | |
125 | 122 | const char buffer = (on ? '1' : '0'); |
126 | 123 | |
127 | - if (rfkill_state_path[0] == '\0') { | |
128 | - if (init_rfkill()) goto out; | |
129 | - } | |
124 | + if (ret < 0) | |
125 | + return ret; | |
126 | + else if (ret == on) | |
127 | + return 0; | |
130 | 128 | |
131 | 129 | fd = open(rfkill_state_path, O_WRONLY); |
132 | 130 | if (fd < 0) { |
@@ -134,8 +132,7 @@ static int set_bluetooth_power(int on) { | ||
134 | 132 | strerror(errno), errno); |
135 | 133 | goto out; |
136 | 134 | } |
137 | - sz = write(fd, &buffer, 1); | |
138 | - if (sz < 0) { | |
135 | + if (write(fd, &buffer, 1) != 1) { | |
139 | 136 | LOGE("write(%s) failed: %s (%d)", rfkill_state_path, strerror(errno), |
140 | 137 | errno); |
141 | 138 | goto out; |
@@ -147,7 +144,7 @@ out: | ||
147 | 144 | return ret; |
148 | 145 | } |
149 | 146 | |
150 | -static inline int create_hci_sock() { | |
147 | +static int create_hci_sock() { | |
151 | 148 | int sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); |
152 | 149 | if (sk < 0) { |
153 | 150 | LOGE("Failed to create bluetooth hci socket: %s (%d)", |
@@ -163,8 +160,6 @@ int bt_enable() { | ||
163 | 160 | int hci_sock = -1; |
164 | 161 | int attempt; |
165 | 162 | |
166 | - if (set_bluetooth_power(1) < 0) goto out; | |
167 | - | |
168 | 163 | LOGI("Starting hciattach daemon"); |
169 | 164 | if (property_set("ctl.start", "hciattach") < 0) { |
170 | 165 | LOGE("Failed to start hciattach"); |
@@ -174,13 +169,19 @@ int bt_enable() { | ||
174 | 169 | |
175 | 170 | // Try for 10 seconds, this can only succeed once hciattach has sent the |
176 | 171 | // firmware and then turned on hci device via HCIUARTSETPROTO ioctl |
177 | - for (attempt = 1000; attempt > 0; attempt--) { | |
172 | + for (attempt = 10; attempt > 0; --attempt) { | |
173 | + int res = set_bluetooth_power(1); | |
174 | + usleep(900000); | |
175 | + if (res < 0) { | |
176 | + bt_disable(); | |
177 | + continue; | |
178 | + } | |
178 | 179 | hci_sock = create_hci_sock(); |
179 | 180 | if (hci_sock < 0) goto out; |
180 | 181 | |
181 | 182 | ret = ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID); |
182 | 183 | |
183 | - LOGI("bt_enable: ret: %d, errno: %d", ret, errno); | |
184 | + LOGI("bt_enable: ret: %d, errno: %s (%d)", ret, strerror(errno), errno); | |
184 | 185 | if (!ret) { |
185 | 186 | break; |
186 | 187 | } else if (errno == EALREADY) { |