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_remote_gatt_characteristic_chromeos.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "chromeos/dbus/bluetooth_gatt_characteristic_client.h" |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" |
| 11 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" |
| 12 |
| 13 namespace chromeos { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Stream operator for logging vector<uint8>. |
| 18 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { |
| 19 out << "["; |
| 20 for (std::vector<uint8>::const_iterator iter = bytes.begin(); |
| 21 iter != bytes.end(); ++iter) { |
| 22 out << base::StringPrintf("%02X", *iter); |
| 23 } |
| 24 return out << "]"; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 BluetoothRemoteGattCharacteristicChromeOS:: |
| 30 BluetoothRemoteGattCharacteristicChromeOS( |
| 31 BluetoothRemoteGattServiceChromeOS* service, |
| 32 const dbus::ObjectPath& object_path) |
| 33 : object_path_(object_path), |
| 34 service_(service), |
| 35 weak_ptr_factory_(this) { |
| 36 VLOG(1) << "Creating remote GATT characteristic with identifier: " |
| 37 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
| 38 } |
| 39 |
| 40 BluetoothRemoteGattCharacteristicChromeOS:: |
| 41 ~BluetoothRemoteGattCharacteristicChromeOS() { |
| 42 } |
| 43 |
| 44 std::string BluetoothRemoteGattCharacteristicChromeOS::GetIdentifier() const { |
| 45 return object_path_.value(); |
| 46 } |
| 47 |
| 48 device::BluetoothUUID |
| 49 BluetoothRemoteGattCharacteristicChromeOS::GetUUID() const { |
| 50 BluetoothGattCharacteristicClient::Properties* properties = |
| 51 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 52 GetProperties(object_path_); |
| 53 DCHECK(properties); |
| 54 return device::BluetoothUUID(properties->uuid.value()); |
| 55 } |
| 56 |
| 57 bool BluetoothRemoteGattCharacteristicChromeOS::IsLocal() const { |
| 58 return false; |
| 59 } |
| 60 |
| 61 const std::vector<uint8>& |
| 62 BluetoothRemoteGattCharacteristicChromeOS::GetValue() const { |
| 63 BluetoothGattCharacteristicClient::Properties* properties = |
| 64 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 65 GetProperties(object_path_); |
| 66 DCHECK(properties); |
| 67 return properties->value.value(); |
| 68 } |
| 69 |
| 70 device::BluetoothGattService* |
| 71 BluetoothRemoteGattCharacteristicChromeOS::GetService() const { |
| 72 return service_; |
| 73 } |
| 74 |
| 75 device::BluetoothGattCharacteristic::Properties |
| 76 BluetoothRemoteGattCharacteristicChromeOS::GetProperties() const { |
| 77 // TODO(armansito): Once BlueZ implements properties properly, return those |
| 78 // values here. |
| 79 return kPropertyNone; |
| 80 } |
| 81 |
| 82 device::BluetoothGattCharacteristic::Permissions |
| 83 BluetoothRemoteGattCharacteristicChromeOS::GetPermissions() const { |
| 84 // TODO(armansito): Once BlueZ defines the permissions, return the correct |
| 85 // values here. |
| 86 return kPermissionNone; |
| 87 } |
| 88 |
| 89 std::vector<device::BluetoothGattDescriptor*> |
| 90 BluetoothRemoteGattCharacteristicChromeOS::GetDescriptors() const { |
| 91 // TODO(armansito): Return the actual descriptors here. |
| 92 return std::vector<device::BluetoothGattDescriptor*>(); |
| 93 } |
| 94 |
| 95 bool BluetoothRemoteGattCharacteristicChromeOS::AddDescriptor( |
| 96 device::BluetoothGattDescriptor* descriptor) { |
| 97 VLOG(1) << "Descriptors cannot be added to a remote GATT characteristic."; |
| 98 return false; |
| 99 } |
| 100 |
| 101 bool BluetoothRemoteGattCharacteristicChromeOS::UpdateValue( |
| 102 const std::vector<uint8>& value) { |
| 103 VLOG(1) << "Cannot update the value of a remote GATT characteristic."; |
| 104 return false; |
| 105 } |
| 106 |
| 107 void BluetoothRemoteGattCharacteristicChromeOS::ReadRemoteCharacteristic( |
| 108 const ValueCallback& callback, |
| 109 const ErrorCallback& error_callback) { |
| 110 VLOG(1) << "Sending GATT characteristic read request to characteristic: " |
| 111 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
| 112 << "."; |
| 113 BluetoothGattCharacteristicClient::Properties* properties = |
| 114 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 115 GetProperties(object_path_); |
| 116 DCHECK(properties); |
| 117 properties->value.Get( |
| 118 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnGetValue, |
| 119 weak_ptr_factory_.GetWeakPtr(), |
| 120 callback, error_callback)); |
| 121 } |
| 122 |
| 123 void BluetoothRemoteGattCharacteristicChromeOS::WriteRemoteCharacteristic( |
| 124 const std::vector<uint8>& new_value, |
| 125 const base::Closure& callback, |
| 126 const ErrorCallback& error_callback) { |
| 127 VLOG(1) << "Sending GATT characteristic write request to characteristic: " |
| 128 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() |
| 129 << ", with value: " << new_value << "."; |
| 130 |
| 131 // Permission and bonding are handled by BlueZ so no need check it here. |
| 132 if (new_value.empty()) { |
| 133 VLOG(1) << "Nothing to write."; |
| 134 error_callback.Run(); |
| 135 return; |
| 136 } |
| 137 |
| 138 BluetoothGattCharacteristicClient::Properties* properties = |
| 139 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 140 GetProperties(object_path_); |
| 141 DCHECK(properties); |
| 142 properties->value.Set( |
| 143 new_value, |
| 144 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnSetValue, |
| 145 weak_ptr_factory_.GetWeakPtr(), |
| 146 callback, error_callback)); |
| 147 } |
| 148 |
| 149 void BluetoothRemoteGattCharacteristicChromeOS::OnGetValue( |
| 150 const ValueCallback& callback, |
| 151 const ErrorCallback& error_callback, |
| 152 bool success) { |
| 153 if (!success) { |
| 154 VLOG(1) << "Failed to read the value from the remote characteristic."; |
| 155 error_callback.Run(); |
| 156 return; |
| 157 } |
| 158 |
| 159 VLOG(1) << "Read value of remote characteristic."; |
| 160 BluetoothGattCharacteristicClient::Properties* properties = |
| 161 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
| 162 GetProperties(object_path_); |
| 163 DCHECK(properties); |
| 164 callback.Run(properties->value.value()); |
| 165 } |
| 166 |
| 167 void BluetoothRemoteGattCharacteristicChromeOS::OnSetValue( |
| 168 const base::Closure& callback, |
| 169 const ErrorCallback& error_callback, |
| 170 bool success) { |
| 171 if (!success) { |
| 172 VLOG(1) << "Failed to write the value of remote characteristic."; |
| 173 error_callback.Run(); |
| 174 return; |
| 175 } |
| 176 |
| 177 VLOG(1) << "Wrote value of remote characteristic."; |
| 178 callback.Run(); |
| 179 } |
| 180 |
| 181 } // namespace chromeos |
OLD | NEW |