Chromium Code Reviews| Index: device/bluetooth/bluetooth_gatt_characteristic_bluez.cc |
| diff --git a/device/bluetooth/bluetooth_gatt_characteristic_bluez.cc b/device/bluetooth/bluetooth_gatt_characteristic_bluez.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..93010703c80881f7946f800499e60be0784b074b |
| --- /dev/null |
| +++ b/device/bluetooth/bluetooth_gatt_characteristic_bluez.cc |
| @@ -0,0 +1,187 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/bluetooth/bluetooth_gatt_characteristic_bluez.h" |
| + |
| +#include <limits> |
| +#include <utility> |
| + |
| +#include "base/logging.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "device/bluetooth/bluetooth_adapter_bluez.h" |
| +#include "device/bluetooth/bluetooth_device.h" |
| +#include "device/bluetooth/bluetooth_gatt_descriptor_bluez.h" |
| +#include "device/bluetooth/bluetooth_gatt_notify_session_bluez.h" |
| +#include "device/bluetooth/bluetooth_gatt_service_bluez.h" |
| +#include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| +#include "third_party/cros_system_api/dbus/service_constants.h" |
| + |
| +namespace bluez { |
| + |
| +namespace { |
| + |
| +// Stream operator for logging vector<uint8_t>. |
| +std::ostream& operator<<(std::ostream& out, const std::vector<uint8_t> bytes) { |
| + out << "["; |
| + for (std::vector<uint8_t>::const_iterator iter = bytes.begin(); |
| + iter != bytes.end(); ++iter) { |
| + out << base::StringPrintf("%02X", *iter); |
| + } |
| + return out << "]"; |
| +} |
| + |
| +} // namespace |
| + |
| +BluetoothGattCharacteristicBlueZ::BluetoothGattCharacteristicBlueZ( |
| + BluetoothGattServiceBlueZ* service, |
| + const dbus::ObjectPath& object_path) |
| + : object_path_(object_path), service_(service), weak_ptr_factory_(this) { |
| + VLOG(1) << "Creating GATT characteristic with identifier: " << GetIdentifier() |
| + << ", UUID: " << GetUUID().canonical_value(); |
| +} |
| + |
| +BluetoothGattCharacteristicBlueZ::~BluetoothGattCharacteristicBlueZ() {} |
| + |
| +std::string BluetoothGattCharacteristicBlueZ::GetIdentifier() const { |
| + return object_path_.value(); |
| +} |
| + |
| +device::BluetoothUUID BluetoothGattCharacteristicBlueZ::GetUUID() const { |
|
ortuno
2016/04/13 14:40:27
It looks like these implementation are specific to
rkc
2016/04/13 22:47:25
Done.
|
| + bluez::BluetoothGattCharacteristicClient::Properties* properties = |
| + bluez::BluezDBusManager::Get() |
| + ->GetBluetoothGattCharacteristicClient() |
| + ->GetProperties(object_path_); |
| + DCHECK(properties); |
| + return device::BluetoothUUID(properties->uuid.value()); |
| +} |
| + |
| +const std::vector<uint8_t>& BluetoothGattCharacteristicBlueZ::GetValue() const { |
| + bluez::BluetoothGattCharacteristicClient::Properties* properties = |
| + bluez::BluezDBusManager::Get() |
| + ->GetBluetoothGattCharacteristicClient() |
| + ->GetProperties(object_path_); |
| + |
| + DCHECK(properties); |
| + |
| + return properties->value.value(); |
| +} |
| + |
| +device::BluetoothGattService* BluetoothGattCharacteristicBlueZ::GetService() |
| + const { |
| + return service_; |
| +} |
| + |
| +device::BluetoothGattCharacteristic::Properties |
| +BluetoothGattCharacteristicBlueZ::GetProperties() const { |
| + bluez::BluetoothGattCharacteristicClient::Properties* properties = |
| + bluez::BluezDBusManager::Get() |
| + ->GetBluetoothGattCharacteristicClient() |
| + ->GetProperties(object_path_); |
| + DCHECK(properties); |
| + |
| + Properties props = PROPERTY_NONE; |
| + const std::vector<std::string>& flags = properties->flags.value(); |
| + for (std::vector<std::string>::const_iterator iter = flags.begin(); |
| + iter != flags.end(); ++iter) { |
| + if (*iter == bluetooth_gatt_characteristic::kFlagBroadcast) |
| + props |= PROPERTY_BROADCAST; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagRead) |
| + props |= PROPERTY_READ; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagWriteWithoutResponse) |
| + props |= PROPERTY_WRITE_WITHOUT_RESPONSE; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagWrite) |
| + props |= PROPERTY_WRITE; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagNotify) |
| + props |= PROPERTY_NOTIFY; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagIndicate) |
| + props |= PROPERTY_INDICATE; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagAuthenticatedSignedWrites) |
| + props |= PROPERTY_AUTHENTICATED_SIGNED_WRITES; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagExtendedProperties) |
| + props |= PROPERTY_EXTENDED_PROPERTIES; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagReliableWrite) |
| + props |= PROPERTY_RELIABLE_WRITE; |
| + if (*iter == bluetooth_gatt_characteristic::kFlagWritableAuxiliaries) |
| + props |= PROPERTY_WRITABLE_AUXILIARIES; |
| + } |
| + |
| + return props; |
| +} |
| + |
| +device::BluetoothGattCharacteristic::Permissions |
| +BluetoothGattCharacteristicBlueZ::GetPermissions() const { |
| + // TODO(armansito): Once BlueZ defines the permissions, return the correct |
| + // values here. |
| + return PERMISSION_NONE; |
| +} |
| + |
| +bool BluetoothGattCharacteristicBlueZ::IsNotifying() const { |
| + bluez::BluetoothGattCharacteristicClient::Properties* properties = |
| + bluez::BluezDBusManager::Get() |
| + ->GetBluetoothGattCharacteristicClient() |
| + ->GetProperties(object_path_); |
| + DCHECK(properties); |
| + |
| + return properties->notifying.value(); |
| +} |
| + |
| +std::vector<device::BluetoothGattDescriptor*> |
| +BluetoothGattCharacteristicBlueZ::GetDescriptors() const { |
| + std::vector<device::BluetoothGattDescriptor*> descriptors; |
| + for (DescriptorMap::const_iterator iter = descriptors_.begin(); |
| + iter != descriptors_.end(); ++iter) |
| + descriptors.push_back(iter->second); |
| + return descriptors; |
| +} |
| + |
| +device::BluetoothGattDescriptor* |
| +BluetoothGattCharacteristicBlueZ::GetDescriptor( |
| + const std::string& identifier) const { |
| + DescriptorMap::const_iterator iter = |
| + descriptors_.find(dbus::ObjectPath(identifier)); |
| + if (iter == descriptors_.end()) |
| + return NULL; |
| + return iter->second; |
| +} |
| + |
| +void BluetoothGattCharacteristicBlueZ::ReadRemoteCharacteristic( |
| + const ValueCallback& callback, |
| + const ErrorCallback& error_callback) { |
| + VLOG(1) << "Sending GATT characteristic read request to characteristic: " |
| + << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
| + << "."; |
| + |
| + bluez::BluezDBusManager::Get() |
| + ->GetBluetoothGattCharacteristicClient() |
| + ->ReadValue(object_path_, callback, |
| + base::Bind(&BluetoothGattCharacteristicBlueZ::OnError, |
| + weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| +} |
| + |
| +void BluetoothGattCharacteristicBlueZ::WriteRemoteCharacteristic( |
| + const std::vector<uint8_t>& new_value, |
| + const base::Closure& callback, |
| + const ErrorCallback& error_callback) { |
| + VLOG(1) << "Sending GATT characteristic write request to characteristic: " |
| + << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
| + << ", with value: " << new_value << "."; |
| + |
| + bluez::BluezDBusManager::Get() |
| + ->GetBluetoothGattCharacteristicClient() |
| + ->WriteValue(object_path_, new_value, callback, |
| + base::Bind(&BluetoothGattCharacteristicBlueZ::OnError, |
| + weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| +} |
| + |
| +void BluetoothGattCharacteristicBlueZ::OnError( |
| + const ErrorCallback& error_callback, |
| + const std::string& error_name, |
| + const std::string& error_message) { |
| + VLOG(1) << "Operation failed: " << error_name |
| + << ", message: " << error_message; |
| + error_callback.Run( |
| + BluetoothGattServiceBlueZ::DBusErrorToServiceError(error_name)); |
| +} |
| + |
| +} // namespace bluez |