| 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_descriptor_bluez.h" | 5 #include "device/bluetooth/bluetooth_gatt_descriptor_bluez.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.h" | 10 #include "device/bluetooth/bluetooth_gatt_characteristic_bluez.h" |
| 11 #include "device/bluetooth/bluetooth_remote_gatt_service_bluez.h" | 11 #include "device/bluetooth/bluetooth_gatt_service_bluez.h" |
| 12 #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_client.h" | 12 #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_client.h" |
| 13 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 13 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 14 | 14 |
| 15 namespace bluez { | 15 namespace bluez { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Stream operator for logging vector<uint8_t>. | 19 // Stream operator for logging vector<uint8_t>. |
| 20 std::ostream& operator<<(std::ostream& out, const std::vector<uint8_t> bytes) { | 20 std::ostream& operator<<(std::ostream& out, const std::vector<uint8_t> bytes) { |
| 21 out << "["; | 21 out << "["; |
| 22 for (std::vector<uint8_t>::const_iterator iter = bytes.begin(); | 22 for (std::vector<uint8_t>::const_iterator iter = bytes.begin(); |
| 23 iter != bytes.end(); ++iter) { | 23 iter != bytes.end(); ++iter) { |
| 24 out << base::StringPrintf("%02X", *iter); | 24 out << base::StringPrintf("%02X", *iter); |
| 25 } | 25 } |
| 26 return out << "]"; | 26 return out << "]"; |
| 27 } | 27 } |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 BluetoothRemoteGattDescriptorBlueZ::BluetoothRemoteGattDescriptorBlueZ( | 31 BluetoothGattDescriptorBlueZ::BluetoothGattDescriptorBlueZ( |
| 32 BluetoothRemoteGattCharacteristicBlueZ* characteristic, | 32 BluetoothGattCharacteristicBlueZ* characteristic, |
| 33 const dbus::ObjectPath& object_path) | 33 const dbus::ObjectPath& object_path, |
| 34 bool is_local) |
| 34 : object_path_(object_path), | 35 : object_path_(object_path), |
| 35 characteristic_(characteristic), | 36 characteristic_(characteristic), |
| 37 is_local_(is_local), |
| 36 weak_ptr_factory_(this) { | 38 weak_ptr_factory_(this) { |
| 37 VLOG(1) << "Creating remote GATT descriptor with identifier: " | 39 VLOG(1) << "Creating remote GATT descriptor with identifier: " |
| 38 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 40 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 39 } | 41 } |
| 40 | 42 |
| 41 BluetoothRemoteGattDescriptorBlueZ::~BluetoothRemoteGattDescriptorBlueZ() {} | 43 BluetoothGattDescriptorBlueZ::~BluetoothGattDescriptorBlueZ() {} |
| 42 | 44 |
| 43 std::string BluetoothRemoteGattDescriptorBlueZ::GetIdentifier() const { | 45 std::string BluetoothGattDescriptorBlueZ::GetIdentifier() const { |
| 44 return object_path_.value(); | 46 return object_path_.value(); |
| 45 } | 47 } |
| 46 | 48 |
| 47 device::BluetoothUUID BluetoothRemoteGattDescriptorBlueZ::GetUUID() const { | 49 device::BluetoothUUID BluetoothGattDescriptorBlueZ::GetUUID() const { |
| 48 bluez::BluetoothGattDescriptorClient::Properties* properties = | 50 bluez::BluetoothGattDescriptorClient::Properties* properties = |
| 49 bluez::BluezDBusManager::Get() | 51 bluez::BluezDBusManager::Get() |
| 50 ->GetBluetoothGattDescriptorClient() | 52 ->GetBluetoothGattDescriptorClient() |
| 51 ->GetProperties(object_path_); | 53 ->GetProperties(object_path_); |
| 52 DCHECK(properties); | 54 DCHECK(properties); |
| 53 return device::BluetoothUUID(properties->uuid.value()); | 55 return device::BluetoothUUID(properties->uuid.value()); |
| 54 } | 56 } |
| 55 | 57 |
| 56 bool BluetoothRemoteGattDescriptorBlueZ::IsLocal() const { | 58 bool BluetoothGattDescriptorBlueZ::IsLocal() const { |
| 57 return false; | 59 return is_local_; |
| 58 } | 60 } |
| 59 | 61 |
| 60 const std::vector<uint8_t>& BluetoothRemoteGattDescriptorBlueZ::GetValue() | 62 const std::vector<uint8_t>& BluetoothGattDescriptorBlueZ::GetValue() const { |
| 61 const { | |
| 62 bluez::BluetoothGattDescriptorClient::Properties* properties = | 63 bluez::BluetoothGattDescriptorClient::Properties* properties = |
| 63 bluez::BluezDBusManager::Get() | 64 bluez::BluezDBusManager::Get() |
| 64 ->GetBluetoothGattDescriptorClient() | 65 ->GetBluetoothGattDescriptorClient() |
| 65 ->GetProperties(object_path_); | 66 ->GetProperties(object_path_); |
| 66 | 67 |
| 67 DCHECK(properties); | 68 DCHECK(properties); |
| 68 | 69 |
| 69 return properties->value.value(); | 70 return properties->value.value(); |
| 70 } | 71 } |
| 71 | 72 |
| 72 device::BluetoothGattCharacteristic* | 73 device::BluetoothGattCharacteristic* |
| 73 BluetoothRemoteGattDescriptorBlueZ::GetCharacteristic() const { | 74 BluetoothGattDescriptorBlueZ::GetCharacteristic() const { |
| 74 return characteristic_; | 75 return characteristic_; |
| 75 } | 76 } |
| 76 | 77 |
| 77 device::BluetoothGattCharacteristic::Permissions | 78 device::BluetoothGattCharacteristic::Permissions |
| 78 BluetoothRemoteGattDescriptorBlueZ::GetPermissions() const { | 79 BluetoothGattDescriptorBlueZ::GetPermissions() const { |
| 79 // TODO(armansito): Once BlueZ defines the permissions, return the correct | 80 // TODO(armansito): Once BlueZ defines the permissions, return the correct |
| 80 // values here. | 81 // values here. |
| 81 return device::BluetoothGattCharacteristic::PERMISSION_NONE; | 82 return device::BluetoothGattCharacteristic::PERMISSION_NONE; |
| 82 } | 83 } |
| 83 | 84 |
| 84 void BluetoothRemoteGattDescriptorBlueZ::ReadRemoteDescriptor( | 85 void BluetoothGattDescriptorBlueZ::ReadRemoteDescriptor( |
| 85 const ValueCallback& callback, | 86 const ValueCallback& callback, |
| 86 const ErrorCallback& error_callback) { | 87 const ErrorCallback& error_callback) { |
| 87 VLOG(1) << "Sending GATT characteristic descriptor read request to " | 88 VLOG(1) << "Sending GATT characteristic descriptor read request to " |
| 88 << "descriptor: " << GetIdentifier() | 89 << "descriptor: " << GetIdentifier() |
| 89 << ", UUID: " << GetUUID().canonical_value(); | 90 << ", UUID: " << GetUUID().canonical_value(); |
| 90 | 91 |
| 91 bluez::BluezDBusManager::Get()->GetBluetoothGattDescriptorClient()->ReadValue( | 92 bluez::BluezDBusManager::Get()->GetBluetoothGattDescriptorClient()->ReadValue( |
| 92 object_path_, callback, | 93 object_path_, callback, |
| 93 base::Bind(&BluetoothRemoteGattDescriptorBlueZ::OnError, | 94 base::Bind(&BluetoothGattDescriptorBlueZ::OnError, |
| 94 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 95 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 95 } | 96 } |
| 96 | 97 |
| 97 void BluetoothRemoteGattDescriptorBlueZ::WriteRemoteDescriptor( | 98 void BluetoothGattDescriptorBlueZ::WriteRemoteDescriptor( |
| 98 const std::vector<uint8_t>& new_value, | 99 const std::vector<uint8_t>& new_value, |
| 99 const base::Closure& callback, | 100 const base::Closure& callback, |
| 100 const ErrorCallback& error_callback) { | 101 const ErrorCallback& error_callback) { |
| 101 VLOG(1) << "Sending GATT characteristic descriptor write request to " | 102 VLOG(1) << "Sending GATT characteristic descriptor write request to " |
| 102 << "characteristic: " << GetIdentifier() | 103 << "characteristic: " << GetIdentifier() |
| 103 << ", UUID: " << GetUUID().canonical_value() | 104 << ", UUID: " << GetUUID().canonical_value() |
| 104 << ", with value: " << new_value << "."; | 105 << ", with value: " << new_value << "."; |
| 105 | 106 |
| 106 bluez::BluezDBusManager::Get() | 107 bluez::BluezDBusManager::Get() |
| 107 ->GetBluetoothGattDescriptorClient() | 108 ->GetBluetoothGattDescriptorClient() |
| 108 ->WriteValue(object_path_, new_value, callback, | 109 ->WriteValue(object_path_, new_value, callback, |
| 109 base::Bind(&BluetoothRemoteGattDescriptorBlueZ::OnError, | 110 base::Bind(&BluetoothGattDescriptorBlueZ::OnError, |
| 110 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 111 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 111 } | 112 } |
| 112 | 113 |
| 113 void BluetoothRemoteGattDescriptorBlueZ::OnError( | 114 void BluetoothGattDescriptorBlueZ::OnError(const ErrorCallback& error_callback, |
| 114 const ErrorCallback& error_callback, | 115 const std::string& error_name, |
| 115 const std::string& error_name, | 116 const std::string& error_message) { |
| 116 const std::string& error_message) { | |
| 117 VLOG(1) << "Operation failed: " << error_name | 117 VLOG(1) << "Operation failed: " << error_name |
| 118 << ", message: " << error_message; | 118 << ", message: " << error_message; |
| 119 | 119 |
| 120 error_callback.Run( | 120 error_callback.Run( |
| 121 BluetoothRemoteGattServiceBlueZ::DBusErrorToServiceError(error_name)); | 121 BluetoothGattServiceBlueZ::DBusErrorToServiceError(error_name)); |
| 122 } | 122 } |
| 123 | 123 |
| 124 } // namespace bluez | 124 } // namespace bluez |
| OLD | NEW |