OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_win.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 BTH_LE_GATT_CHARACTERISTIC* characteristic_info, | 21 BTH_LE_GATT_CHARACTERISTIC* characteristic_info, |
22 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) | 22 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) |
23 : parent_service_(parent_service), | 23 : parent_service_(parent_service), |
24 characteristic_info_(characteristic_info), | 24 characteristic_info_(characteristic_info), |
25 ui_task_runner_(ui_task_runner), | 25 ui_task_runner_(ui_task_runner), |
26 characteristic_added_notified_(false), | 26 characteristic_added_notified_(false), |
27 characteristic_value_read_or_write_in_progress_(false), | 27 characteristic_value_read_or_write_in_progress_(false), |
28 gatt_event_registeration_in_progress_(false), | 28 gatt_event_registeration_in_progress_(false), |
29 gatt_event_handle_(nullptr), | 29 gatt_event_handle_(nullptr), |
30 weak_ptr_factory_(this) { | 30 weak_ptr_factory_(this) { |
31 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 31 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
32 DCHECK(parent_service_); | 32 DCHECK(parent_service_); |
33 DCHECK(characteristic_info_); | 33 DCHECK(characteristic_info_); |
34 | 34 |
35 task_manager_ = | 35 task_manager_ = |
36 parent_service_->GetWinAdapter()->GetWinBluetoothTaskManager(); | 36 parent_service_->GetWinAdapter()->GetWinBluetoothTaskManager(); |
37 DCHECK(task_manager_); | 37 DCHECK(task_manager_); |
38 characteristic_uuid_ = | 38 characteristic_uuid_ = |
39 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( | 39 BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid( |
40 characteristic_info_->CharacteristicUuid); | 40 characteristic_info_->CharacteristicUuid); |
41 characteristic_identifier_ = | 41 characteristic_identifier_ = |
42 parent_service_->GetIdentifier() + "_" + | 42 parent_service_->GetIdentifier() + "_" + |
43 std::to_string(characteristic_info_->AttributeHandle); | 43 std::to_string(characteristic_info_->AttributeHandle); |
44 Update(); | 44 Update(); |
45 } | 45 } |
46 | 46 |
47 BluetoothRemoteGattCharacteristicWin::~BluetoothRemoteGattCharacteristicWin() { | 47 BluetoothRemoteGattCharacteristicWin::~BluetoothRemoteGattCharacteristicWin() { |
48 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 48 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
49 | 49 |
50 ClearIncludedDescriptors(); | 50 ClearIncludedDescriptors(); |
51 | 51 |
52 if (gatt_event_handle_ != nullptr) { | 52 if (gatt_event_handle_ != nullptr) { |
53 task_manager_->PostUnregisterGattCharacteristicValueChangedEvent( | 53 task_manager_->PostUnregisterGattCharacteristicValueChangedEvent( |
54 gatt_event_handle_); | 54 gatt_event_handle_); |
55 gatt_event_handle_ = nullptr; | 55 gatt_event_handle_ = nullptr; |
56 } | 56 } |
57 parent_service_->GetWinAdapter()->NotifyGattCharacteristicRemoved(this); | 57 parent_service_->GetWinAdapter()->NotifyGattCharacteristicRemoved(this); |
58 | 58 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 const std::string& identifier) const { | 147 const std::string& identifier) const { |
148 GattDescriptorMap::const_iterator it = included_descriptors_.find(identifier); | 148 GattDescriptorMap::const_iterator it = included_descriptors_.find(identifier); |
149 if (it != included_descriptors_.end()) | 149 if (it != included_descriptors_.end()) |
150 return it->second.get(); | 150 return it->second.get(); |
151 return nullptr; | 151 return nullptr; |
152 } | 152 } |
153 | 153 |
154 void BluetoothRemoteGattCharacteristicWin::StartNotifySession( | 154 void BluetoothRemoteGattCharacteristicWin::StartNotifySession( |
155 const NotifySessionCallback& callback, | 155 const NotifySessionCallback& callback, |
156 const ErrorCallback& error_callback) { | 156 const ErrorCallback& error_callback) { |
157 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 157 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
158 | 158 |
159 if (IsNotifying()) { | 159 if (IsNotifying()) { |
160 std::unique_ptr<BluetoothGattNotifySession> notify_session( | 160 std::unique_ptr<BluetoothGattNotifySession> notify_session( |
161 new BluetoothGattNotifySession(weak_ptr_factory_.GetWeakPtr())); | 161 new BluetoothGattNotifySession(weak_ptr_factory_.GetWeakPtr())); |
162 ui_task_runner_->PostTask( | 162 ui_task_runner_->PostTask( |
163 FROM_HERE, | 163 FROM_HERE, |
164 base::Bind(callback, base::Passed(std::move(notify_session)))); | 164 base::Bind(callback, base::Passed(std::move(notify_session)))); |
165 return; | 165 return; |
166 } | 166 } |
167 | 167 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // TODO(http://crbug.com/636270): Remove this method and use the base version. | 215 // TODO(http://crbug.com/636270): Remove this method and use the base version. |
216 // Instead, we should implement SubscribeToNotifications and | 216 // Instead, we should implement SubscribeToNotifications and |
217 // UnsubscribeFromNotifications. | 217 // UnsubscribeFromNotifications. |
218 | 218 |
219 ui_task_runner_->PostTask(FROM_HERE, callback); | 219 ui_task_runner_->PostTask(FROM_HERE, callback); |
220 } | 220 } |
221 | 221 |
222 void BluetoothRemoteGattCharacteristicWin::ReadRemoteCharacteristic( | 222 void BluetoothRemoteGattCharacteristicWin::ReadRemoteCharacteristic( |
223 const ValueCallback& callback, | 223 const ValueCallback& callback, |
224 const ErrorCallback& error_callback) { | 224 const ErrorCallback& error_callback) { |
225 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 225 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
226 | 226 |
227 if (!characteristic_info_.get()->IsReadable) { | 227 if (!characteristic_info_.get()->IsReadable) { |
228 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED); | 228 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED); |
229 return; | 229 return; |
230 } | 230 } |
231 | 231 |
232 if (characteristic_value_read_or_write_in_progress_) { | 232 if (characteristic_value_read_or_write_in_progress_) { |
233 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS); | 233 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS); |
234 return; | 234 return; |
235 } | 235 } |
236 | 236 |
237 characteristic_value_read_or_write_in_progress_ = true; | 237 characteristic_value_read_or_write_in_progress_ = true; |
238 read_characteristic_value_callbacks_ = | 238 read_characteristic_value_callbacks_ = |
239 std::make_pair(callback, error_callback); | 239 std::make_pair(callback, error_callback); |
240 task_manager_->PostReadGattCharacteristicValue( | 240 task_manager_->PostReadGattCharacteristicValue( |
241 parent_service_->GetServicePath(), characteristic_info_.get(), | 241 parent_service_->GetServicePath(), characteristic_info_.get(), |
242 base::Bind(&BluetoothRemoteGattCharacteristicWin:: | 242 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
243 OnReadRemoteCharacteristicValueCallback, | 243 OnReadRemoteCharacteristicValueCallback, |
244 weak_ptr_factory_.GetWeakPtr())); | 244 weak_ptr_factory_.GetWeakPtr())); |
245 } | 245 } |
246 | 246 |
247 void BluetoothRemoteGattCharacteristicWin::WriteRemoteCharacteristic( | 247 void BluetoothRemoteGattCharacteristicWin::WriteRemoteCharacteristic( |
248 const std::vector<uint8_t>& value, | 248 const std::vector<uint8_t>& value, |
249 const base::Closure& callback, | 249 const base::Closure& callback, |
250 const ErrorCallback& error_callback) { | 250 const ErrorCallback& error_callback) { |
251 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 251 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
252 | 252 |
253 if (!characteristic_info_.get()->IsWritable) { | 253 if (!characteristic_info_.get()->IsWritable) { |
254 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED); | 254 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED); |
255 return; | 255 return; |
256 } | 256 } |
257 | 257 |
258 if (characteristic_value_read_or_write_in_progress_) { | 258 if (characteristic_value_read_or_write_in_progress_) { |
259 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS); | 259 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS); |
260 return; | 260 return; |
261 } | 261 } |
262 | 262 |
263 characteristic_value_read_or_write_in_progress_ = true; | 263 characteristic_value_read_or_write_in_progress_ = true; |
264 write_characteristic_value_callbacks_ = | 264 write_characteristic_value_callbacks_ = |
265 std::make_pair(callback, error_callback); | 265 std::make_pair(callback, error_callback); |
266 task_manager_->PostWriteGattCharacteristicValue( | 266 task_manager_->PostWriteGattCharacteristicValue( |
267 parent_service_->GetServicePath(), characteristic_info_.get(), value, | 267 parent_service_->GetServicePath(), characteristic_info_.get(), value, |
268 base::Bind(&BluetoothRemoteGattCharacteristicWin:: | 268 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
269 OnWriteRemoteCharacteristicValueCallback, | 269 OnWriteRemoteCharacteristicValueCallback, |
270 weak_ptr_factory_.GetWeakPtr())); | 270 weak_ptr_factory_.GetWeakPtr())); |
271 } | 271 } |
272 | 272 |
273 void BluetoothRemoteGattCharacteristicWin::Update() { | 273 void BluetoothRemoteGattCharacteristicWin::Update() { |
274 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 274 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
275 | 275 |
276 task_manager_->PostGetGattIncludedDescriptors( | 276 task_manager_->PostGetGattIncludedDescriptors( |
277 parent_service_->GetServicePath(), characteristic_info_.get(), | 277 parent_service_->GetServicePath(), characteristic_info_.get(), |
278 base::Bind(&BluetoothRemoteGattCharacteristicWin:: | 278 base::Bind(&BluetoothRemoteGattCharacteristicWin:: |
279 OnGetIncludedDescriptorsCallback, | 279 OnGetIncludedDescriptorsCallback, |
280 weak_ptr_factory_.GetWeakPtr())); | 280 weak_ptr_factory_.GetWeakPtr())); |
281 } | 281 } |
282 | 282 |
283 uint16_t BluetoothRemoteGattCharacteristicWin::GetAttributeHandle() const { | 283 uint16_t BluetoothRemoteGattCharacteristicWin::GetAttributeHandle() const { |
284 return characteristic_info_->AttributeHandle; | 284 return characteristic_info_->AttributeHandle; |
(...skipping 12 matching lines...) Expand all Loading... |
297 const base::Closure& callback, | 297 const base::Closure& callback, |
298 const ErrorCallback& error_callback) { | 298 const ErrorCallback& error_callback) { |
299 // TODO(http://crbug.com/636270): Implement this method | 299 // TODO(http://crbug.com/636270): Implement this method |
300 NOTIMPLEMENTED(); | 300 NOTIMPLEMENTED(); |
301 } | 301 } |
302 | 302 |
303 void BluetoothRemoteGattCharacteristicWin::OnGetIncludedDescriptorsCallback( | 303 void BluetoothRemoteGattCharacteristicWin::OnGetIncludedDescriptorsCallback( |
304 std::unique_ptr<BTH_LE_GATT_DESCRIPTOR> descriptors, | 304 std::unique_ptr<BTH_LE_GATT_DESCRIPTOR> descriptors, |
305 uint16_t num, | 305 uint16_t num, |
306 HRESULT hr) { | 306 HRESULT hr) { |
307 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 307 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
308 | 308 |
309 UpdateIncludedDescriptors(descriptors.get(), num); | 309 UpdateIncludedDescriptors(descriptors.get(), num); |
310 if (!characteristic_added_notified_) { | 310 if (!characteristic_added_notified_) { |
311 characteristic_added_notified_ = true; | 311 characteristic_added_notified_ = true; |
312 parent_service_->GetWinAdapter()->NotifyGattCharacteristicAdded(this); | 312 parent_service_->GetWinAdapter()->NotifyGattCharacteristicAdded(this); |
313 } | 313 } |
314 } | 314 } |
315 | 315 |
316 void BluetoothRemoteGattCharacteristicWin::UpdateIncludedDescriptors( | 316 void BluetoothRemoteGattCharacteristicWin::UpdateIncludedDescriptors( |
317 PBTH_LE_GATT_DESCRIPTOR descriptors, | 317 PBTH_LE_GATT_DESCRIPTOR descriptors, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 return true; | 379 return true; |
380 } | 380 } |
381 } | 381 } |
382 return false; | 382 return false; |
383 } | 383 } |
384 | 384 |
385 void BluetoothRemoteGattCharacteristicWin:: | 385 void BluetoothRemoteGattCharacteristicWin:: |
386 OnReadRemoteCharacteristicValueCallback( | 386 OnReadRemoteCharacteristicValueCallback( |
387 std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value, | 387 std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value, |
388 HRESULT hr) { | 388 HRESULT hr) { |
389 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 389 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
390 | 390 |
391 std::pair<ValueCallback, ErrorCallback> callbacks; | 391 std::pair<ValueCallback, ErrorCallback> callbacks; |
392 callbacks.swap(read_characteristic_value_callbacks_); | 392 callbacks.swap(read_characteristic_value_callbacks_); |
393 if (FAILED(hr)) { | 393 if (FAILED(hr)) { |
394 callbacks.second.Run(HRESULTToGattErrorCode(hr)); | 394 callbacks.second.Run(HRESULTToGattErrorCode(hr)); |
395 } else { | 395 } else { |
396 characteristic_value_.clear(); | 396 characteristic_value_.clear(); |
397 for (ULONG i = 0; i < value->DataSize; i++) | 397 for (ULONG i = 0; i < value->DataSize; i++) |
398 characteristic_value_.push_back(value->Data[i]); | 398 characteristic_value_.push_back(value->Data[i]); |
399 | 399 |
400 callbacks.first.Run(characteristic_value_); | 400 callbacks.first.Run(characteristic_value_); |
401 } | 401 } |
402 characteristic_value_read_or_write_in_progress_ = false; | 402 characteristic_value_read_or_write_in_progress_ = false; |
403 } | 403 } |
404 | 404 |
405 void BluetoothRemoteGattCharacteristicWin:: | 405 void BluetoothRemoteGattCharacteristicWin:: |
406 OnWriteRemoteCharacteristicValueCallback(HRESULT hr) { | 406 OnWriteRemoteCharacteristicValueCallback(HRESULT hr) { |
407 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 407 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
408 | 408 |
409 std::pair<base::Closure, ErrorCallback> callbacks; | 409 std::pair<base::Closure, ErrorCallback> callbacks; |
410 callbacks.swap(write_characteristic_value_callbacks_); | 410 callbacks.swap(write_characteristic_value_callbacks_); |
411 if (FAILED(hr)) { | 411 if (FAILED(hr)) { |
412 callbacks.second.Run(HRESULTToGattErrorCode(hr)); | 412 callbacks.second.Run(HRESULTToGattErrorCode(hr)); |
413 } else { | 413 } else { |
414 callbacks.first.Run(); | 414 callbacks.first.Run(); |
415 } | 415 } |
416 characteristic_value_read_or_write_in_progress_ = false; | 416 characteristic_value_read_or_write_in_progress_ = false; |
417 } | 417 } |
(...skipping 13 matching lines...) Expand all Loading... |
431 return BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH; | 431 return BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH; |
432 case E_BLUETOOTH_ATT_REQUEST_NOT_SUPPORTED: | 432 case E_BLUETOOTH_ATT_REQUEST_NOT_SUPPORTED: |
433 return BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED; | 433 return BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED; |
434 default: | 434 default: |
435 return BluetoothRemoteGattService::GATT_ERROR_FAILED; | 435 return BluetoothRemoteGattService::GATT_ERROR_FAILED; |
436 } | 436 } |
437 } | 437 } |
438 | 438 |
439 void BluetoothRemoteGattCharacteristicWin::OnGattCharacteristicValueChanged( | 439 void BluetoothRemoteGattCharacteristicWin::OnGattCharacteristicValueChanged( |
440 std::unique_ptr<std::vector<uint8_t>> new_value) { | 440 std::unique_ptr<std::vector<uint8_t>> new_value) { |
441 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 441 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
442 | 442 |
443 characteristic_value_.assign(new_value->begin(), new_value->end()); | 443 characteristic_value_.assign(new_value->begin(), new_value->end()); |
444 parent_service_->GetWinAdapter()->NotifyGattCharacteristicValueChanged( | 444 parent_service_->GetWinAdapter()->NotifyGattCharacteristicValueChanged( |
445 this, characteristic_value_); | 445 this, characteristic_value_); |
446 } | 446 } |
447 | 447 |
448 void BluetoothRemoteGattCharacteristicWin::GattEventRegistrationCallback( | 448 void BluetoothRemoteGattCharacteristicWin::GattEventRegistrationCallback( |
449 BLUETOOTH_GATT_EVENT_HANDLE event_handle, | 449 BLUETOOTH_GATT_EVENT_HANDLE event_handle, |
450 HRESULT hr) { | 450 HRESULT hr) { |
451 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 451 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
452 | 452 |
453 gatt_event_registeration_in_progress_ = false; | 453 gatt_event_registeration_in_progress_ = false; |
454 std::vector<std::pair<NotifySessionCallback, ErrorCallback>> callbacks; | 454 std::vector<std::pair<NotifySessionCallback, ErrorCallback>> callbacks; |
455 callbacks.swap(start_notify_session_callbacks_); | 455 callbacks.swap(start_notify_session_callbacks_); |
456 if (SUCCEEDED(hr)) { | 456 if (SUCCEEDED(hr)) { |
457 gatt_event_handle_ = event_handle; | 457 gatt_event_handle_ = event_handle; |
458 for (const auto& callback : callbacks) { | 458 for (const auto& callback : callbacks) { |
459 callback.first.Run(base::MakeUnique<BluetoothGattNotifySession>( | 459 callback.first.Run(base::MakeUnique<BluetoothGattNotifySession>( |
460 weak_ptr_factory_.GetWeakPtr())); | 460 weak_ptr_factory_.GetWeakPtr())); |
461 } | 461 } |
462 } else { | 462 } else { |
463 for (const auto& callback : callbacks) | 463 for (const auto& callback : callbacks) |
464 callback.second.Run(HRESULTToGattErrorCode(hr)); | 464 callback.second.Run(HRESULTToGattErrorCode(hr)); |
465 } | 465 } |
466 } | 466 } |
467 | 467 |
468 void BluetoothRemoteGattCharacteristicWin::ClearIncludedDescriptors() { | 468 void BluetoothRemoteGattCharacteristicWin::ClearIncludedDescriptors() { |
469 // Explicitly reset to null to ensure that calling GetDescriptor() on the | 469 // Explicitly reset to null to ensure that calling GetDescriptor() on the |
470 // removed descriptor in GattDescriptorRemoved() returns null. | 470 // removed descriptor in GattDescriptorRemoved() returns null. |
471 for (auto& entry : included_descriptors_) | 471 for (auto& entry : included_descriptors_) |
472 entry.second.reset(); | 472 entry.second.reset(); |
473 included_descriptors_.clear(); | 473 included_descriptors_.clear(); |
474 } | 474 } |
475 | 475 |
476 } // namespace device. | 476 } // namespace device. |
OLD | NEW |