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