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