OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 | 129 |
130 bool BluetoothRemoteGattCharacteristicAndroid::UpdateValue( | 130 bool BluetoothRemoteGattCharacteristicAndroid::UpdateValue( |
131 const std::vector<uint8_t>& value) { | 131 const std::vector<uint8_t>& value) { |
132 NOTIMPLEMENTED(); | 132 NOTIMPLEMENTED(); |
133 return false; | 133 return false; |
134 } | 134 } |
135 | 135 |
136 void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( | 136 void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( |
137 const NotifySessionCallback& callback, | 137 const NotifySessionCallback& callback, |
138 const ErrorCallback& error_callback) { | 138 const ErrorCallback& error_callback) { |
139 if (Java_ChromeBluetoothRemoteGattCharacteristic_startNotifySession( | 139 if (!pending_start_notify_calls_.empty()) { |
ortuno
2016/03/04 17:39:47
Why are we allowing multiple notify session calls?
scheib
2016/03/11 03:17:31
Unlike a read or write that must happen serially,
| |
140 AttachCurrentThread(), j_characteristic_.obj())) { | 140 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); |
141 // TODO(crbug.com/569664): Wait until descriptor write completes before | 141 return; |
142 // reporting success via calling |callback|. | 142 } |
143 scoped_ptr<device::BluetoothGattNotifySession> notify_session( | 143 |
144 new BluetoothGattNotifySessionAndroid(instance_id_)); | 144 Properties properties = GetProperties(); |
145 base::MessageLoop::current()->PostTask( | 145 |
146 FROM_HERE, base::Bind(callback, base::Passed(¬ify_session))); | 146 bool hasNotify = properties & PROPERTY_NOTIFY; |
147 } else { | 147 bool hasIndicate = properties & PROPERTY_INDICATE; |
148 | |
149 if (!hasNotify && !hasIndicate) { | |
150 LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; | |
148 base::MessageLoop::current()->PostTask( | 151 base::MessageLoop::current()->PostTask( |
149 FROM_HERE, | 152 FROM_HERE, |
150 base::Bind(error_callback, | 153 base::Bind(error_callback, |
151 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | 154 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); |
ortuno
2016/03/04 17:39:47
I think this should be GATT_ERROR_NOT_SUPPORTED.
scheib
2016/03/11 03:17:31
Done.
| |
155 return; | |
152 } | 156 } |
157 | |
158 BluetoothGattDescriptor* descriptor = GetDescriptorForUUID( | |
159 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()); | |
160 | |
161 if (!descriptor) { | |
162 LOG(ERROR) | |
163 << "Could not find client characteristic configuration descriptor"; | |
164 base::MessageLoop::current()->PostTask( | |
165 FROM_HERE, | |
166 base::Bind(error_callback, | |
167 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | |
ortuno
2016/03/04 17:39:47
Same here. I think GATT_ERROR_NOT_SUPPORTED is mor
scheib
2016/03/11 03:17:31
Done.
| |
168 return; | |
169 } | |
170 | |
171 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio n( | |
172 AttachCurrentThread(), j_characteristic_.obj(), true)) { | |
173 LOG(ERROR) << "Error enabling characteristic notification"; | |
174 base::MessageLoop::current()->PostTask( | |
175 FROM_HERE, | |
176 base::Bind(error_callback, | |
177 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | |
178 return; | |
179 } | |
180 | |
181 std::vector<uint8_t> value; | |
ortuno
2016/03/04 17:39:47
nit: value(2) // The Client Characteristic Configu
scheib
2016/03/11 03:17:31
Done.
| |
182 value.push_back(hasNotify ? 1 : 2); | |
183 value.push_back(0); | |
184 | |
185 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); | |
186 descriptor->WriteRemoteDescriptor( | |
187 value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: | |
188 OnStartNotifySessionSuccess, | |
189 base::Unretained(this)), | |
ortuno
2016/03/04 17:39:47
Is this safe? I guess it is since the characterist
scheib
2016/03/11 03:17:31
Acknowledged.
| |
190 base::Bind( | |
191 &BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError, | |
192 base::Unretained(this))); | |
153 } | 193 } |
154 | 194 |
155 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( | 195 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( |
156 const ValueCallback& callback, | 196 const ValueCallback& callback, |
157 const ErrorCallback& error_callback) { | 197 const ErrorCallback& error_callback) { |
158 if (read_pending_ || write_pending_) { | 198 if (read_pending_ || write_pending_) { |
159 base::MessageLoop::current()->PostTask( | 199 base::MessageLoop::current()->PostTask( |
160 FROM_HERE, base::Bind(error_callback, | 200 FROM_HERE, base::Bind(error_callback, |
161 BluetoothGattService::GATT_ERROR_IN_PROGRESS)); | 201 BluetoothGattService::GATT_ERROR_IN_PROGRESS)); |
162 return; | 202 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 } | 244 } |
205 | 245 |
206 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( | 246 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( |
207 JNIEnv* env, | 247 JNIEnv* env, |
208 const JavaParamRef<jobject>& jcaller, | 248 const JavaParamRef<jobject>& jcaller, |
209 const JavaParamRef<jbyteArray>& value) { | 249 const JavaParamRef<jbyteArray>& value) { |
210 base::android::JavaByteArrayToByteVector(env, value, &value_); | 250 base::android::JavaByteArrayToByteVector(env, value, &value_); |
211 adapter_->NotifyGattCharacteristicValueChanged(this, value_); | 251 adapter_->NotifyGattCharacteristicValueChanged(this, value_); |
212 } | 252 } |
213 | 253 |
254 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionSuccess() { | |
255 while (!pending_start_notify_calls_.empty()) { | |
scheib
2016/03/02 06:15:20
Protect against reentrant code by swapping the mem
ortuno
2016/03/04 17:39:47
I just realized how easy it is to miss this. Do we
scheib
2016/03/11 03:17:31
Not yet -- we should. Here's a new issue: https://
| |
256 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | |
257 pending_start_notify_calls_.pop(); | |
258 scoped_ptr<device::BluetoothGattNotifySession> notify_session( | |
259 new BluetoothGattNotifySessionAndroid(instance_id_)); | |
260 callbacks.first.Run(std::move(notify_session)); | |
261 } | |
262 } | |
263 | |
264 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError( | |
265 BluetoothGattService::GattErrorCode error) { | |
266 while (!pending_start_notify_calls_.empty()) { | |
267 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | |
268 pending_start_notify_calls_.pop(); | |
269 callbacks.second.Run(error); | |
270 } | |
271 } | |
272 | |
214 void BluetoothRemoteGattCharacteristicAndroid::OnRead( | 273 void BluetoothRemoteGattCharacteristicAndroid::OnRead( |
215 JNIEnv* env, | 274 JNIEnv* env, |
216 const JavaParamRef<jobject>& jcaller, | 275 const JavaParamRef<jobject>& jcaller, |
217 int32_t status, | 276 int32_t status, |
218 const JavaParamRef<jbyteArray>& value) { | 277 const JavaParamRef<jbyteArray>& value) { |
219 read_pending_ = false; | 278 read_pending_ = false; |
220 | 279 |
221 // Clear callbacks before calling to avoid reentrancy issues. | 280 // Clear callbacks before calling to avoid reentrancy issues. |
222 ValueCallback read_callback = read_callback_; | 281 ValueCallback read_callback = read_callback_; |
223 ErrorCallback read_error_callback = read_error_callback_; | 282 ErrorCallback read_error_callback = read_error_callback_; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 345 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
287 const { | 346 const { |
288 if (!descriptors_.empty()) | 347 if (!descriptors_.empty()) |
289 return; | 348 return; |
290 | 349 |
291 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 350 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
292 AttachCurrentThread(), j_characteristic_.obj()); | 351 AttachCurrentThread(), j_characteristic_.obj()); |
293 } | 352 } |
294 | 353 |
295 } // namespace device | 354 } // namespace device |
OLD | NEW |