| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_chromeos.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 12 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 13 #include "device/bluetooth/bluetooth_device.h" | 13 #include "device/bluetooth/bluetooth_device.h" |
| 14 #include "device/bluetooth/bluetooth_gatt_notify_session_chromeos.h" | 14 #include "device/bluetooth/bluetooth_gatt_notify_session_chromeos.h" |
| 15 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
| 15 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" | 16 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" |
| 16 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" | 17 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" | 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 | 19 |
| 19 namespace chromeos { | 20 namespace chromeos { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Stream operator for logging vector<uint8>. | 24 // Stream operator for logging vector<uint8>. |
| 24 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { | 25 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 BluetoothRemoteGattCharacteristicChromeOS( | 37 BluetoothRemoteGattCharacteristicChromeOS( |
| 37 BluetoothRemoteGattServiceChromeOS* service, | 38 BluetoothRemoteGattServiceChromeOS* service, |
| 38 const dbus::ObjectPath& object_path) | 39 const dbus::ObjectPath& object_path) |
| 39 : object_path_(object_path), | 40 : object_path_(object_path), |
| 40 service_(service), | 41 service_(service), |
| 41 num_notify_sessions_(0), | 42 num_notify_sessions_(0), |
| 42 notify_call_pending_(false), | 43 notify_call_pending_(false), |
| 43 weak_ptr_factory_(this) { | 44 weak_ptr_factory_(this) { |
| 44 VLOG(1) << "Creating remote GATT characteristic with identifier: " | 45 VLOG(1) << "Creating remote GATT characteristic with identifier: " |
| 45 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 46 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 46 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | |
| 47 AddObserver(this); | |
| 48 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 47 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> |
| 49 AddObserver(this); | 48 AddObserver(this); |
| 50 | 49 |
| 51 // Add all known GATT characteristic descriptors. | 50 // Add all known GATT characteristic descriptors. |
| 52 const std::vector<dbus::ObjectPath>& gatt_descs = | 51 const std::vector<dbus::ObjectPath>& gatt_descs = |
| 53 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 52 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> |
| 54 GetDescriptors(); | 53 GetDescriptors(); |
| 55 for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_descs.begin(); | 54 for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_descs.begin(); |
| 56 iter != gatt_descs.end(); ++iter) | 55 iter != gatt_descs.end(); ++iter) |
| 57 GattDescriptorAdded(*iter); | 56 GattDescriptorAdded(*iter); |
| 58 } | 57 } |
| 59 | 58 |
| 60 BluetoothRemoteGattCharacteristicChromeOS:: | 59 BluetoothRemoteGattCharacteristicChromeOS:: |
| 61 ~BluetoothRemoteGattCharacteristicChromeOS() { | 60 ~BluetoothRemoteGattCharacteristicChromeOS() { |
| 62 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 61 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> |
| 63 RemoveObserver(this); | 62 RemoveObserver(this); |
| 64 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | |
| 65 RemoveObserver(this); | |
| 66 | 63 |
| 67 // Clean up all the descriptors. There isn't much point in notifying service | 64 // Clean up all the descriptors. There isn't much point in notifying service |
| 68 // observers for each descriptor that gets removed, so just delete them. | 65 // observers for each descriptor that gets removed, so just delete them. |
| 69 for (DescriptorMap::iterator iter = descriptors_.begin(); | 66 for (DescriptorMap::iterator iter = descriptors_.begin(); |
| 70 iter != descriptors_.end(); ++iter) | 67 iter != descriptors_.end(); ++iter) |
| 71 delete iter->second; | 68 delete iter->second; |
| 72 | 69 |
| 73 // Report an error for all pending calls to StartNotifySession. | 70 // Report an error for all pending calls to StartNotifySession. |
| 74 while (!pending_start_notify_calls_.empty()) { | 71 while (!pending_start_notify_calls_.empty()) { |
| 75 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | 72 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 90 DCHECK(properties); | 87 DCHECK(properties); |
| 91 return device::BluetoothUUID(properties->uuid.value()); | 88 return device::BluetoothUUID(properties->uuid.value()); |
| 92 } | 89 } |
| 93 | 90 |
| 94 bool BluetoothRemoteGattCharacteristicChromeOS::IsLocal() const { | 91 bool BluetoothRemoteGattCharacteristicChromeOS::IsLocal() const { |
| 95 return false; | 92 return false; |
| 96 } | 93 } |
| 97 | 94 |
| 98 const std::vector<uint8>& | 95 const std::vector<uint8>& |
| 99 BluetoothRemoteGattCharacteristicChromeOS::GetValue() const { | 96 BluetoothRemoteGattCharacteristicChromeOS::GetValue() const { |
| 100 return cached_value_; | 97 BluetoothGattCharacteristicClient::Properties* properties = |
| 98 DBusThreadManager::Get() |
| 99 ->GetBluetoothGattCharacteristicClient() |
| 100 ->GetProperties(object_path_); |
| 101 |
| 102 DCHECK(properties); |
| 103 |
| 104 return properties->value.value(); |
| 101 } | 105 } |
| 102 | 106 |
| 103 device::BluetoothGattService* | 107 device::BluetoothGattService* |
| 104 BluetoothRemoteGattCharacteristicChromeOS::GetService() const { | 108 BluetoothRemoteGattCharacteristicChromeOS::GetService() const { |
| 105 return service_; | 109 return service_; |
| 106 } | 110 } |
| 107 | 111 |
| 108 device::BluetoothGattCharacteristic::Properties | 112 device::BluetoothGattCharacteristic::Properties |
| 109 BluetoothRemoteGattCharacteristicChromeOS::GetProperties() const { | 113 BluetoothRemoteGattCharacteristicChromeOS::GetProperties() const { |
| 110 BluetoothGattCharacteristicClient::Properties* properties = | 114 BluetoothGattCharacteristicClient::Properties* properties = |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 } | 195 } |
| 192 | 196 |
| 193 void BluetoothRemoteGattCharacteristicChromeOS::ReadRemoteCharacteristic( | 197 void BluetoothRemoteGattCharacteristicChromeOS::ReadRemoteCharacteristic( |
| 194 const ValueCallback& callback, | 198 const ValueCallback& callback, |
| 195 const ErrorCallback& error_callback) { | 199 const ErrorCallback& error_callback) { |
| 196 VLOG(1) << "Sending GATT characteristic read request to characteristic: " | 200 VLOG(1) << "Sending GATT characteristic read request to characteristic: " |
| 197 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() | 201 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
| 198 << "."; | 202 << "."; |
| 199 | 203 |
| 200 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()->ReadValue( | 204 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()->ReadValue( |
| 201 object_path_, | 205 object_path_, callback, |
| 202 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnValueSuccess, | |
| 203 weak_ptr_factory_.GetWeakPtr(), | |
| 204 callback), | |
| 205 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnError, | 206 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnError, |
| 206 weak_ptr_factory_.GetWeakPtr(), | 207 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 207 error_callback)); | |
| 208 } | 208 } |
| 209 | 209 |
| 210 void BluetoothRemoteGattCharacteristicChromeOS::WriteRemoteCharacteristic( | 210 void BluetoothRemoteGattCharacteristicChromeOS::WriteRemoteCharacteristic( |
| 211 const std::vector<uint8>& new_value, | 211 const std::vector<uint8>& new_value, |
| 212 const base::Closure& callback, | 212 const base::Closure& callback, |
| 213 const ErrorCallback& error_callback) { | 213 const ErrorCallback& error_callback) { |
| 214 VLOG(1) << "Sending GATT characteristic write request to characteristic: " | 214 VLOG(1) << "Sending GATT characteristic write request to characteristic: " |
| 215 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() | 215 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
| 216 << ", with value: " << new_value << "."; | 216 << ", with value: " << new_value << "."; |
| 217 | 217 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 object_path_, | 308 object_path_, |
| 309 base::Bind( | 309 base::Bind( |
| 310 &BluetoothRemoteGattCharacteristicChromeOS::OnStopNotifySuccess, | 310 &BluetoothRemoteGattCharacteristicChromeOS::OnStopNotifySuccess, |
| 311 weak_ptr_factory_.GetWeakPtr(), | 311 weak_ptr_factory_.GetWeakPtr(), |
| 312 callback), | 312 callback), |
| 313 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnStopNotifyError, | 313 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnStopNotifyError, |
| 314 weak_ptr_factory_.GetWeakPtr(), | 314 weak_ptr_factory_.GetWeakPtr(), |
| 315 callback)); | 315 callback)); |
| 316 } | 316 } |
| 317 | 317 |
| 318 void BluetoothRemoteGattCharacteristicChromeOS::GattCharacteristicValueUpdated( | |
| 319 const dbus::ObjectPath& object_path, | |
| 320 const std::vector<uint8>& value) { | |
| 321 if (object_path != object_path_) | |
| 322 return; | |
| 323 | |
| 324 cached_value_ = value; | |
| 325 | |
| 326 VLOG(1) << "GATT characteristic value has changed: " << object_path.value() | |
| 327 << ": " << value; | |
| 328 DCHECK(service_); | |
| 329 service_->NotifyCharacteristicValueChanged(this, value); | |
| 330 } | |
| 331 | |
| 332 void BluetoothRemoteGattCharacteristicChromeOS::GattDescriptorAdded( | 318 void BluetoothRemoteGattCharacteristicChromeOS::GattDescriptorAdded( |
| 333 const dbus::ObjectPath& object_path) { | 319 const dbus::ObjectPath& object_path) { |
| 334 if (descriptors_.find(object_path) != descriptors_.end()) { | 320 if (descriptors_.find(object_path) != descriptors_.end()) { |
| 335 VLOG(1) << "Remote GATT characteristic descriptor already exists: " | 321 VLOG(1) << "Remote GATT characteristic descriptor already exists: " |
| 336 << object_path.value(); | 322 << object_path.value(); |
| 337 return; | 323 return; |
| 338 } | 324 } |
| 339 | 325 |
| 340 BluetoothGattDescriptorClient::Properties* properties = | 326 BluetoothGattDescriptorClient::Properties* properties = |
| 341 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 327 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 BluetoothRemoteGattDescriptorChromeOS* descriptor = iter->second; | 359 BluetoothRemoteGattDescriptorChromeOS* descriptor = iter->second; |
| 374 DCHECK(descriptor->object_path() == object_path); | 360 DCHECK(descriptor->object_path() == object_path); |
| 375 descriptors_.erase(iter); | 361 descriptors_.erase(iter); |
| 376 | 362 |
| 377 DCHECK(service_); | 363 DCHECK(service_); |
| 378 service_->NotifyDescriptorAddedOrRemoved(this, descriptor, false /* added */); | 364 service_->NotifyDescriptorAddedOrRemoved(this, descriptor, false /* added */); |
| 379 | 365 |
| 380 delete descriptor; | 366 delete descriptor; |
| 381 } | 367 } |
| 382 | 368 |
| 383 void BluetoothRemoteGattCharacteristicChromeOS::OnValueSuccess( | 369 void BluetoothRemoteGattCharacteristicChromeOS::GattDescriptorPropertyChanged( |
| 384 const ValueCallback& callback, | 370 const dbus::ObjectPath& object_path, |
| 385 const std::vector<uint8>& value) { | 371 const std::string& property_name) { |
| 386 VLOG(1) << "Characteristic value read: " << value; | 372 DescriptorMap::iterator iter = descriptors_.find(object_path); |
| 387 cached_value_ = value; | 373 if (iter == descriptors_.end()) { |
| 374 VLOG(2) << "Unknown descriptor removed: " << object_path.value(); |
| 375 return; |
| 376 } |
| 377 |
| 378 BluetoothGattDescriptorClient::Properties* properties = |
| 379 DBusThreadManager::Get() |
| 380 ->GetBluetoothGattDescriptorClient() |
| 381 ->GetProperties(object_path); |
| 382 |
| 383 DCHECK(properties); |
| 384 |
| 385 if (property_name != properties->value.name()) |
| 386 return; |
| 388 | 387 |
| 389 DCHECK(service_); | 388 DCHECK(service_); |
| 390 service_->NotifyCharacteristicValueChanged(this, cached_value_); | 389 service_->NotifyDescriptorValueChanged(this, iter->second, |
| 391 | 390 properties->value.value()); |
| 392 callback.Run(value); | |
| 393 } | 391 } |
| 394 | 392 |
| 395 void BluetoothRemoteGattCharacteristicChromeOS::OnError( | 393 void BluetoothRemoteGattCharacteristicChromeOS::OnError( |
| 396 const ErrorCallback& error_callback, | 394 const ErrorCallback& error_callback, |
| 397 const std::string& error_name, | 395 const std::string& error_name, |
| 398 const std::string& error_message) { | 396 const std::string& error_message) { |
| 399 VLOG(1) << "Operation failed: " << error_name << ", message: " | 397 VLOG(1) << "Operation failed: " << error_name << ", message: " |
| 400 << error_message; | 398 << error_message; |
| 401 error_callback.Run( | 399 error_callback.Run( |
| 402 BluetoothRemoteGattServiceChromeOS::DBusErrorToServiceError(error_name)); | 400 BluetoothRemoteGattServiceChromeOS::DBusErrorToServiceError(error_name)); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 | 469 |
| 472 void BluetoothRemoteGattCharacteristicChromeOS::ProcessStartNotifyQueue() { | 470 void BluetoothRemoteGattCharacteristicChromeOS::ProcessStartNotifyQueue() { |
| 473 while (!pending_start_notify_calls_.empty()) { | 471 while (!pending_start_notify_calls_.empty()) { |
| 474 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | 472 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); |
| 475 pending_start_notify_calls_.pop(); | 473 pending_start_notify_calls_.pop(); |
| 476 StartNotifySession(callbacks.first, callbacks.second); | 474 StartNotifySession(callbacks.first, callbacks.second); |
| 477 } | 475 } |
| 478 } | 476 } |
| 479 | 477 |
| 480 } // namespace chromeos | 478 } // namespace chromeos |
| OLD | NEW |