Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/bluetooth/bluetooth_gatt_characteristic_bluez.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "device/bluetooth/bluetooth_adapter_bluez.h" | |
| 13 #include "device/bluetooth/bluetooth_device.h" | |
| 14 #include "device/bluetooth/bluetooth_gatt_descriptor_bluez.h" | |
| 15 #include "device/bluetooth/bluetooth_gatt_notify_session_bluez.h" | |
| 16 #include "device/bluetooth/bluetooth_gatt_service_bluez.h" | |
| 17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
| 18 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 19 | |
| 20 namespace bluez { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Stream operator for logging vector<uint8_t>. | |
| 25 std::ostream& operator<<(std::ostream& out, const std::vector<uint8_t> bytes) { | |
| 26 out << "["; | |
| 27 for (std::vector<uint8_t>::const_iterator iter = bytes.begin(); | |
| 28 iter != bytes.end(); ++iter) { | |
| 29 out << base::StringPrintf("%02X", *iter); | |
| 30 } | |
| 31 return out << "]"; | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 BluetoothGattCharacteristicBlueZ::BluetoothGattCharacteristicBlueZ( | |
| 37 BluetoothGattServiceBlueZ* service, | |
| 38 const dbus::ObjectPath& object_path) | |
| 39 : object_path_(object_path), service_(service), weak_ptr_factory_(this) { | |
| 40 VLOG(1) << "Creating GATT characteristic with identifier: " << GetIdentifier() | |
| 41 << ", UUID: " << GetUUID().canonical_value(); | |
| 42 } | |
| 43 | |
| 44 BluetoothGattCharacteristicBlueZ::~BluetoothGattCharacteristicBlueZ() {} | |
| 45 | |
| 46 std::string BluetoothGattCharacteristicBlueZ::GetIdentifier() const { | |
| 47 return object_path_.value(); | |
| 48 } | |
| 49 | |
| 50 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.
| |
| 51 bluez::BluetoothGattCharacteristicClient::Properties* properties = | |
| 52 bluez::BluezDBusManager::Get() | |
| 53 ->GetBluetoothGattCharacteristicClient() | |
| 54 ->GetProperties(object_path_); | |
| 55 DCHECK(properties); | |
| 56 return device::BluetoothUUID(properties->uuid.value()); | |
| 57 } | |
| 58 | |
| 59 const std::vector<uint8_t>& BluetoothGattCharacteristicBlueZ::GetValue() const { | |
| 60 bluez::BluetoothGattCharacteristicClient::Properties* properties = | |
| 61 bluez::BluezDBusManager::Get() | |
| 62 ->GetBluetoothGattCharacteristicClient() | |
| 63 ->GetProperties(object_path_); | |
| 64 | |
| 65 DCHECK(properties); | |
| 66 | |
| 67 return properties->value.value(); | |
| 68 } | |
| 69 | |
| 70 device::BluetoothGattService* BluetoothGattCharacteristicBlueZ::GetService() | |
| 71 const { | |
| 72 return service_; | |
| 73 } | |
| 74 | |
| 75 device::BluetoothGattCharacteristic::Properties | |
| 76 BluetoothGattCharacteristicBlueZ::GetProperties() const { | |
| 77 bluez::BluetoothGattCharacteristicClient::Properties* properties = | |
| 78 bluez::BluezDBusManager::Get() | |
| 79 ->GetBluetoothGattCharacteristicClient() | |
| 80 ->GetProperties(object_path_); | |
| 81 DCHECK(properties); | |
| 82 | |
| 83 Properties props = PROPERTY_NONE; | |
| 84 const std::vector<std::string>& flags = properties->flags.value(); | |
| 85 for (std::vector<std::string>::const_iterator iter = flags.begin(); | |
| 86 iter != flags.end(); ++iter) { | |
| 87 if (*iter == bluetooth_gatt_characteristic::kFlagBroadcast) | |
| 88 props |= PROPERTY_BROADCAST; | |
| 89 if (*iter == bluetooth_gatt_characteristic::kFlagRead) | |
| 90 props |= PROPERTY_READ; | |
| 91 if (*iter == bluetooth_gatt_characteristic::kFlagWriteWithoutResponse) | |
| 92 props |= PROPERTY_WRITE_WITHOUT_RESPONSE; | |
| 93 if (*iter == bluetooth_gatt_characteristic::kFlagWrite) | |
| 94 props |= PROPERTY_WRITE; | |
| 95 if (*iter == bluetooth_gatt_characteristic::kFlagNotify) | |
| 96 props |= PROPERTY_NOTIFY; | |
| 97 if (*iter == bluetooth_gatt_characteristic::kFlagIndicate) | |
| 98 props |= PROPERTY_INDICATE; | |
| 99 if (*iter == bluetooth_gatt_characteristic::kFlagAuthenticatedSignedWrites) | |
| 100 props |= PROPERTY_AUTHENTICATED_SIGNED_WRITES; | |
| 101 if (*iter == bluetooth_gatt_characteristic::kFlagExtendedProperties) | |
| 102 props |= PROPERTY_EXTENDED_PROPERTIES; | |
| 103 if (*iter == bluetooth_gatt_characteristic::kFlagReliableWrite) | |
| 104 props |= PROPERTY_RELIABLE_WRITE; | |
| 105 if (*iter == bluetooth_gatt_characteristic::kFlagWritableAuxiliaries) | |
| 106 props |= PROPERTY_WRITABLE_AUXILIARIES; | |
| 107 } | |
| 108 | |
| 109 return props; | |
| 110 } | |
| 111 | |
| 112 device::BluetoothGattCharacteristic::Permissions | |
| 113 BluetoothGattCharacteristicBlueZ::GetPermissions() const { | |
| 114 // TODO(armansito): Once BlueZ defines the permissions, return the correct | |
| 115 // values here. | |
| 116 return PERMISSION_NONE; | |
| 117 } | |
| 118 | |
| 119 bool BluetoothGattCharacteristicBlueZ::IsNotifying() const { | |
| 120 bluez::BluetoothGattCharacteristicClient::Properties* properties = | |
| 121 bluez::BluezDBusManager::Get() | |
| 122 ->GetBluetoothGattCharacteristicClient() | |
| 123 ->GetProperties(object_path_); | |
| 124 DCHECK(properties); | |
| 125 | |
| 126 return properties->notifying.value(); | |
| 127 } | |
| 128 | |
| 129 std::vector<device::BluetoothGattDescriptor*> | |
| 130 BluetoothGattCharacteristicBlueZ::GetDescriptors() const { | |
| 131 std::vector<device::BluetoothGattDescriptor*> descriptors; | |
| 132 for (DescriptorMap::const_iterator iter = descriptors_.begin(); | |
| 133 iter != descriptors_.end(); ++iter) | |
| 134 descriptors.push_back(iter->second); | |
| 135 return descriptors; | |
| 136 } | |
| 137 | |
| 138 device::BluetoothGattDescriptor* | |
| 139 BluetoothGattCharacteristicBlueZ::GetDescriptor( | |
| 140 const std::string& identifier) const { | |
| 141 DescriptorMap::const_iterator iter = | |
| 142 descriptors_.find(dbus::ObjectPath(identifier)); | |
| 143 if (iter == descriptors_.end()) | |
| 144 return NULL; | |
| 145 return iter->second; | |
| 146 } | |
| 147 | |
| 148 void BluetoothGattCharacteristicBlueZ::ReadRemoteCharacteristic( | |
| 149 const ValueCallback& callback, | |
| 150 const ErrorCallback& error_callback) { | |
| 151 VLOG(1) << "Sending GATT characteristic read request to characteristic: " | |
| 152 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() | |
| 153 << "."; | |
| 154 | |
| 155 bluez::BluezDBusManager::Get() | |
| 156 ->GetBluetoothGattCharacteristicClient() | |
| 157 ->ReadValue(object_path_, callback, | |
| 158 base::Bind(&BluetoothGattCharacteristicBlueZ::OnError, | |
| 159 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 160 } | |
| 161 | |
| 162 void BluetoothGattCharacteristicBlueZ::WriteRemoteCharacteristic( | |
| 163 const std::vector<uint8_t>& new_value, | |
| 164 const base::Closure& callback, | |
| 165 const ErrorCallback& error_callback) { | |
| 166 VLOG(1) << "Sending GATT characteristic write request to characteristic: " | |
| 167 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() | |
| 168 << ", with value: " << new_value << "."; | |
| 169 | |
| 170 bluez::BluezDBusManager::Get() | |
| 171 ->GetBluetoothGattCharacteristicClient() | |
| 172 ->WriteValue(object_path_, new_value, callback, | |
| 173 base::Bind(&BluetoothGattCharacteristicBlueZ::OnError, | |
| 174 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 175 } | |
| 176 | |
| 177 void BluetoothGattCharacteristicBlueZ::OnError( | |
| 178 const ErrorCallback& error_callback, | |
| 179 const std::string& error_name, | |
| 180 const std::string& error_message) { | |
| 181 VLOG(1) << "Operation failed: " << error_name | |
| 182 << ", message: " << error_message; | |
| 183 error_callback.Run( | |
| 184 BluetoothGattServiceBlueZ::DBusErrorToServiceError(error_name)); | |
| 185 } | |
| 186 | |
| 187 } // namespace bluez | |
| OLD | NEW |