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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 (!pending_start_notify_calls_.empty()) { | 139 if (!pending_start_notify_calls_.empty()) { |
140 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); | 140 pending_start_notify_calls_.push_back( |
| 141 std::make_pair(callback, error_callback)); |
141 return; | 142 return; |
142 } | 143 } |
143 | 144 |
144 Properties properties = GetProperties(); | 145 Properties properties = GetProperties(); |
145 | 146 |
146 bool hasNotify = properties & PROPERTY_NOTIFY; | 147 bool hasNotify = properties & PROPERTY_NOTIFY; |
147 bool hasIndicate = properties & PROPERTY_INDICATE; | 148 bool hasIndicate = properties & PROPERTY_INDICATE; |
148 | 149 |
149 if (!hasNotify && !hasIndicate) { | 150 if (!hasNotify && !hasIndicate) { |
150 LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; | 151 LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; |
(...skipping 24 matching lines...) Expand all Loading... |
175 FROM_HERE, | 176 FROM_HERE, |
176 base::Bind(error_callback, | 177 base::Bind(error_callback, |
177 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | 178 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); |
178 return; | 179 return; |
179 } | 180 } |
180 | 181 |
181 std::vector<uint8_t> value; | 182 std::vector<uint8_t> value; |
182 value.push_back(hasNotify ? 1 : 2); | 183 value.push_back(hasNotify ? 1 : 2); |
183 value.push_back(0); | 184 value.push_back(0); |
184 | 185 |
185 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); | 186 pending_start_notify_calls_.push_back( |
| 187 std::make_pair(callback, error_callback)); |
186 descriptor->WriteRemoteDescriptor( | 188 descriptor->WriteRemoteDescriptor( |
187 value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: | 189 value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: |
188 OnStartNotifySessionSuccess, | 190 OnStartNotifySessionSuccess, |
189 base::Unretained(this)), | 191 base::Unretained(this)), |
190 base::Bind( | 192 base::Bind( |
191 &BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError, | 193 &BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError, |
192 base::Unretained(this))); | 194 base::Unretained(this))); |
193 } | 195 } |
194 | 196 |
195 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( | 197 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 247 |
246 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( | 248 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( |
247 JNIEnv* env, | 249 JNIEnv* env, |
248 const JavaParamRef<jobject>& jcaller, | 250 const JavaParamRef<jobject>& jcaller, |
249 const JavaParamRef<jbyteArray>& value) { | 251 const JavaParamRef<jbyteArray>& value) { |
250 base::android::JavaByteArrayToByteVector(env, value, &value_); | 252 base::android::JavaByteArrayToByteVector(env, value, &value_); |
251 adapter_->NotifyGattCharacteristicValueChanged(this, value_); | 253 adapter_->NotifyGattCharacteristicValueChanged(this, value_); |
252 } | 254 } |
253 | 255 |
254 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionSuccess() { | 256 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionSuccess() { |
255 while (!pending_start_notify_calls_.empty()) { | 257 std::vector<PendingStartNotifyCall> reentrant_safe_callbacks; |
256 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | 258 reentrant_safe_callbacks.swap(pending_start_notify_calls_); |
257 pending_start_notify_calls_.pop(); | 259 |
| 260 for (const auto& callback_pair : reentrant_safe_callbacks) { |
258 scoped_ptr<device::BluetoothGattNotifySession> notify_session( | 261 scoped_ptr<device::BluetoothGattNotifySession> notify_session( |
259 new BluetoothGattNotifySessionAndroid(instance_id_)); | 262 new BluetoothGattNotifySessionAndroid(instance_id_)); |
260 callbacks.first.Run(std::move(notify_session)); | 263 callback_pair.first.Run(std::move(notify_session)); |
261 } | 264 } |
262 } | 265 } |
263 | 266 |
264 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError( | 267 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError( |
265 BluetoothGattService::GattErrorCode error) { | 268 BluetoothGattService::GattErrorCode error) { |
266 while (!pending_start_notify_calls_.empty()) { | 269 std::vector<PendingStartNotifyCall> reentrant_safe_callbacks; |
267 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | 270 reentrant_safe_callbacks.swap(pending_start_notify_calls_); |
268 pending_start_notify_calls_.pop(); | 271 |
269 callbacks.second.Run(error); | 272 for (auto const& callback_pair : reentrant_safe_callbacks) { |
| 273 callback_pair.second.Run(error); |
270 } | 274 } |
271 } | 275 } |
272 | 276 |
273 void BluetoothRemoteGattCharacteristicAndroid::OnRead( | 277 void BluetoothRemoteGattCharacteristicAndroid::OnRead( |
274 JNIEnv* env, | 278 JNIEnv* env, |
275 const JavaParamRef<jobject>& jcaller, | 279 const JavaParamRef<jobject>& jcaller, |
276 int32_t status, | 280 int32_t status, |
277 const JavaParamRef<jbyteArray>& value) { | 281 const JavaParamRef<jbyteArray>& value) { |
278 read_pending_ = false; | 282 read_pending_ = false; |
279 | 283 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 349 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
346 const { | 350 const { |
347 if (!descriptors_.empty()) | 351 if (!descriptors_.empty()) |
348 return; | 352 return; |
349 | 353 |
350 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 354 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
351 AttachCurrentThread(), j_characteristic_.obj()); | 355 AttachCurrentThread(), j_characteristic_.obj()); |
352 } | 356 } |
353 | 357 |
354 } // namespace device | 358 } // namespace device |
OLD | NEW |