| 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_chromeos.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "chromeos/dbus/bluetooth_gatt_descriptor_client.h" | 10 #include "chromeos/dbus/bluetooth_gatt_descriptor_client.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" | 12 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
| 13 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" |
| 13 | 14 |
| 14 namespace chromeos { | 15 namespace chromeos { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Stream operator for logging vector<uint8>. | 19 // Stream operator for logging vector<uint8>. |
| 19 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { | 20 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { |
| 20 out << "["; | 21 out << "["; |
| 21 for (std::vector<uint8>::const_iterator iter = bytes.begin(); | 22 for (std::vector<uint8>::const_iterator iter = bytes.begin(); |
| 22 iter != bytes.end(); ++iter) { | 23 iter != bytes.end(); ++iter) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 52 DCHECK(properties); | 53 DCHECK(properties); |
| 53 return device::BluetoothUUID(properties->uuid.value()); | 54 return device::BluetoothUUID(properties->uuid.value()); |
| 54 } | 55 } |
| 55 | 56 |
| 56 bool BluetoothRemoteGattDescriptorChromeOS::IsLocal() const { | 57 bool BluetoothRemoteGattDescriptorChromeOS::IsLocal() const { |
| 57 return false; | 58 return false; |
| 58 } | 59 } |
| 59 | 60 |
| 60 const std::vector<uint8>& | 61 const std::vector<uint8>& |
| 61 BluetoothRemoteGattDescriptorChromeOS::GetValue() const { | 62 BluetoothRemoteGattDescriptorChromeOS::GetValue() const { |
| 62 BluetoothGattDescriptorClient::Properties* properties = | 63 return cached_value_; |
| 63 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | |
| 64 GetProperties(object_path_); | |
| 65 DCHECK(properties); | |
| 66 return properties->value.value(); | |
| 67 } | 64 } |
| 68 | 65 |
| 69 device::BluetoothGattCharacteristic* | 66 device::BluetoothGattCharacteristic* |
| 70 BluetoothRemoteGattDescriptorChromeOS::GetCharacteristic() const { | 67 BluetoothRemoteGattDescriptorChromeOS::GetCharacteristic() const { |
| 71 return characteristic_; | 68 return characteristic_; |
| 72 } | 69 } |
| 73 | 70 |
| 74 device::BluetoothGattCharacteristic::Permissions | 71 device::BluetoothGattCharacteristic::Permissions |
| 75 BluetoothRemoteGattDescriptorChromeOS::GetPermissions() const { | 72 BluetoothRemoteGattDescriptorChromeOS::GetPermissions() const { |
| 76 // TODO(armansito): Once BlueZ defines the permissions, return the correct | 73 // TODO(armansito): Once BlueZ defines the permissions, return the correct |
| 77 // values here. | 74 // values here. |
| 78 return device::BluetoothGattCharacteristic::kPermissionNone; | 75 return device::BluetoothGattCharacteristic::kPermissionNone; |
| 79 } | 76 } |
| 80 | 77 |
| 81 void BluetoothRemoteGattDescriptorChromeOS::ReadRemoteDescriptor( | 78 void BluetoothRemoteGattDescriptorChromeOS::ReadRemoteDescriptor( |
| 82 const ValueCallback& callback, | 79 const ValueCallback& callback, |
| 83 const ErrorCallback& error_callback) { | 80 const ErrorCallback& error_callback) { |
| 84 VLOG(1) << "Sending GATT characteristic descriptor read request to " | 81 VLOG(1) << "Sending GATT characteristic descriptor read request to " |
| 85 << "descriptor: " << GetIdentifier() << ", UUID: " | 82 << "descriptor: " << GetIdentifier() << ", UUID: " |
| 86 << GetUUID().canonical_value(); | 83 << GetUUID().canonical_value(); |
| 87 BluetoothGattDescriptorClient::Properties* properties = | 84 |
| 88 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 85 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->ReadValue( |
| 89 GetProperties(object_path_); | 86 object_path_, |
| 90 DCHECK(properties); | 87 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnValueSuccess, |
| 91 properties->value.Get( | |
| 92 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnGetValue, | |
| 93 weak_ptr_factory_.GetWeakPtr(), | 88 weak_ptr_factory_.GetWeakPtr(), |
| 94 callback, error_callback)); | 89 callback), |
| 90 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError, |
| 91 weak_ptr_factory_.GetWeakPtr(), |
| 92 error_callback)); |
| 95 } | 93 } |
| 96 | 94 |
| 97 void BluetoothRemoteGattDescriptorChromeOS::WriteRemoteDescriptor( | 95 void BluetoothRemoteGattDescriptorChromeOS::WriteRemoteDescriptor( |
| 98 const std::vector<uint8>& new_value, | 96 const std::vector<uint8>& new_value, |
| 99 const base::Closure& callback, | 97 const base::Closure& callback, |
| 100 const ErrorCallback& error_callback) { | 98 const ErrorCallback& error_callback) { |
| 101 VLOG(1) << "Sending GATT characteristic descriptor write request to " | 99 VLOG(1) << "Sending GATT characteristic descriptor write request to " |
| 102 << "characteristic: " << GetIdentifier() << ", UUID: " | 100 << "characteristic: " << GetIdentifier() << ", UUID: " |
| 103 << GetUUID().canonical_value() << ", with value: " | 101 << GetUUID().canonical_value() << ", with value: " |
| 104 << new_value << "."; | 102 << new_value << "."; |
| 105 BluetoothGattDescriptorClient::Properties* properties = | 103 |
| 106 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 104 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->WriteValue( |
| 107 GetProperties(object_path_); | 105 object_path_, |
| 108 DCHECK(properties); | |
| 109 properties->value.Set( | |
| 110 new_value, | 106 new_value, |
| 111 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnSetValue, | 107 callback, |
| 108 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError, |
| 112 weak_ptr_factory_.GetWeakPtr(), | 109 weak_ptr_factory_.GetWeakPtr(), |
| 113 callback, error_callback)); | 110 error_callback)); |
| 114 } | 111 } |
| 115 | 112 |
| 116 void BluetoothRemoteGattDescriptorChromeOS::OnGetValue( | 113 void BluetoothRemoteGattDescriptorChromeOS::OnValueSuccess( |
| 117 const ValueCallback& callback, | 114 const ValueCallback& callback, |
| 118 const ErrorCallback& error_callback, | 115 const std::vector<uint8>& value) { |
| 119 bool success) { | 116 VLOG(1) << "Descriptor value read: " << value; |
| 120 if (!success) { | 117 cached_value_ = value; |
| 121 VLOG(1) << "Failed to read the value from the remote descriptor."; | |
| 122 error_callback.Run(); | |
| 123 return; | |
| 124 } | |
| 125 | 118 |
| 126 VLOG(1) << "Read value of remote descriptor."; | 119 DCHECK(characteristic_); |
| 127 BluetoothGattDescriptorClient::Properties* properties = | 120 BluetoothRemoteGattServiceChromeOS* service = |
| 128 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()-> | 121 static_cast<BluetoothRemoteGattServiceChromeOS*>( |
| 129 GetProperties(object_path_); | 122 characteristic_->GetService()); |
| 130 DCHECK(properties); | 123 DCHECK(service); |
| 131 callback.Run(properties->value.value()); | 124 service->NotifyDescriptorValueChanged(characteristic_, this, value); |
| 125 callback.Run(value); |
| 132 } | 126 } |
| 133 | 127 |
| 134 void BluetoothRemoteGattDescriptorChromeOS::OnSetValue( | 128 void BluetoothRemoteGattDescriptorChromeOS::OnError( |
| 135 const base::Closure& callback, | |
| 136 const ErrorCallback& error_callback, | 129 const ErrorCallback& error_callback, |
| 137 bool success) { | 130 const std::string& error_name, |
| 138 if (!success) { | 131 const std::string& error_message) { |
| 139 VLOG(1) << "Failed to write the value of remote descriptor."; | 132 VLOG(1) << "Operation failed: " << error_name |
| 140 error_callback.Run(); | 133 << ", message: " << error_message; |
| 141 return; | 134 error_callback.Run(); |
| 142 } | |
| 143 | |
| 144 VLOG(1) << "Wrote value of remote descriptor."; | |
| 145 callback.Run(); | |
| 146 } | 135 } |
| 147 | 136 |
| 148 } // namespace chromeos | 137 } // namespace chromeos |
| OLD | NEW |