Chromium Code Reviews| 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 "chromeos/dbus/bluetooth_gatt_descriptor_client.h" | 5 #include "chromeos/dbus/bluetooth_gatt_descriptor_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/observer_list.h" | 9 #include "base/observer_list.h" |
| 10 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
| 11 #include "dbus/object_manager.h" | 11 #include "dbus/object_manager.h" |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | 12 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 13 | 13 |
| 14 namespace chromeos { | 14 namespace chromeos { |
| 15 | 15 |
| 16 namespace { | |
| 17 | |
| 18 // TODO(armansito): Move these definitions to service_constants.h | |
| 19 const char kReadValue[] = "ReadValue"; | |
| 20 const char kWriteValue[] = "WriteValue"; | |
| 21 | |
| 22 } // namespace | |
|
keybuk
2014/05/30 22:23:41
Again, no reason not to do this before submitting
armansito
2014/06/05 06:13:51
Done.
| |
| 23 | |
| 24 // static | |
| 25 const char BluetoothGattDescriptorClient::kNoResponseError[] = | |
| 26 "org.chromium.Error.NoResponse"; | |
| 27 // static | |
| 28 const char BluetoothGattDescriptorClient::kUnknownDescriptorError[] = | |
| 29 "org.chromium.Error.UnknownDescriptor"; | |
| 30 | |
| 16 BluetoothGattDescriptorClient::Properties::Properties( | 31 BluetoothGattDescriptorClient::Properties::Properties( |
| 17 dbus::ObjectProxy* object_proxy, | 32 dbus::ObjectProxy* object_proxy, |
| 18 const std::string& interface_name, | 33 const std::string& interface_name, |
| 19 const PropertyChangedCallback&callback) | 34 const PropertyChangedCallback&callback) |
| 20 : dbus::PropertySet(object_proxy, interface_name, callback) { | 35 : dbus::PropertySet(object_proxy, interface_name, callback) { |
| 21 RegisterProperty(bluetooth_gatt_descriptor::kUUIDProperty, &uuid); | 36 RegisterProperty(bluetooth_gatt_descriptor::kUUIDProperty, &uuid); |
| 22 RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty, | 37 RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty, |
| 23 &characteristic); | 38 &characteristic); |
| 24 RegisterProperty(bluetooth_gatt_descriptor::kValueProperty, &value); | |
| 25 } | 39 } |
| 26 | 40 |
| 27 BluetoothGattDescriptorClient::Properties::~Properties() { | 41 BluetoothGattDescriptorClient::Properties::~Properties() { |
| 28 } | 42 } |
| 29 | 43 |
| 30 // The BluetoothGattDescriptorClient implementation used in production. | 44 // The BluetoothGattDescriptorClient implementation used in production. |
| 31 class BluetoothGattDescriptorClientImpl | 45 class BluetoothGattDescriptorClientImpl |
| 32 : public BluetoothGattDescriptorClient, | 46 : public BluetoothGattDescriptorClient, |
| 33 public dbus::ObjectManager::Interface { | 47 public dbus::ObjectManager::Interface { |
| 34 public: | 48 public: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 // BluetoothGattDescriptorClientImpl override. | 80 // BluetoothGattDescriptorClientImpl override. |
| 67 virtual Properties* GetProperties( | 81 virtual Properties* GetProperties( |
| 68 const dbus::ObjectPath& object_path) OVERRIDE { | 82 const dbus::ObjectPath& object_path) OVERRIDE { |
| 69 DCHECK(object_manager_); | 83 DCHECK(object_manager_); |
| 70 return static_cast<Properties*>( | 84 return static_cast<Properties*>( |
| 71 object_manager_->GetProperties( | 85 object_manager_->GetProperties( |
| 72 object_path, | 86 object_path, |
| 73 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface)); | 87 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface)); |
| 74 } | 88 } |
| 75 | 89 |
| 90 // BluetoothGattDescriptorClientImpl override. | |
| 91 virtual void ReadValue(const dbus::ObjectPath& object_path, | |
| 92 const ValueCallback& callback, | |
| 93 const ErrorCallback& error_callback) OVERRIDE { | |
| 94 dbus::ObjectProxy* object_proxy = | |
| 95 object_manager_->GetObjectProxy(object_path); | |
| 96 if (!object_proxy) { | |
| 97 error_callback.Run(kUnknownDescriptorError, ""); | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 dbus::MethodCall method_call( | |
| 102 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, | |
| 103 kReadValue); | |
| 104 | |
| 105 object_proxy->CallMethodWithErrorCallback( | |
| 106 &method_call, | |
| 107 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 108 base::Bind(&BluetoothGattDescriptorClientImpl::OnValueSuccess, | |
| 109 weak_ptr_factory_.GetWeakPtr(), | |
| 110 callback), | |
| 111 base::Bind(&BluetoothGattDescriptorClientImpl::OnError, | |
| 112 weak_ptr_factory_.GetWeakPtr(), | |
| 113 error_callback)); | |
| 114 } | |
| 115 | |
| 116 // BluetoothGattDescriptorClientImpl override. | |
| 117 virtual void WriteValue(const dbus::ObjectPath& object_path, | |
| 118 const std::vector<uint8>& value, | |
| 119 const base::Closure& callback, | |
| 120 const ErrorCallback& error_callback) OVERRIDE { | |
| 121 dbus::ObjectProxy* object_proxy = | |
| 122 object_manager_->GetObjectProxy(object_path); | |
| 123 if (!object_proxy) { | |
| 124 error_callback.Run(kUnknownDescriptorError, ""); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 dbus::MethodCall method_call( | |
| 129 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, | |
| 130 kWriteValue); | |
| 131 dbus::MessageWriter writer(&method_call); | |
| 132 writer.AppendArrayOfBytes(value.data(), value.size()); | |
| 133 | |
| 134 object_proxy->CallMethodWithErrorCallback( | |
| 135 &method_call, | |
| 136 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 137 base::Bind(&BluetoothGattDescriptorClientImpl::OnSuccess, | |
| 138 weak_ptr_factory_.GetWeakPtr(), | |
| 139 callback), | |
| 140 base::Bind(&BluetoothGattDescriptorClientImpl::OnError, | |
| 141 weak_ptr_factory_.GetWeakPtr(), | |
| 142 error_callback)); | |
| 143 } | |
| 144 | |
| 76 // dbus::ObjectManager::Interface override. | 145 // dbus::ObjectManager::Interface override. |
| 77 virtual dbus::PropertySet* CreateProperties( | 146 virtual dbus::PropertySet* CreateProperties( |
| 78 dbus::ObjectProxy *object_proxy, | 147 dbus::ObjectProxy *object_proxy, |
| 79 const dbus::ObjectPath& object_path, | 148 const dbus::ObjectPath& object_path, |
| 80 const std::string& interface_name) OVERRIDE { | 149 const std::string& interface_name) OVERRIDE { |
| 81 Properties* properties = new Properties( | 150 Properties* properties = new Properties( |
| 82 object_proxy, | 151 object_proxy, |
| 83 interface_name, | 152 interface_name, |
| 84 base::Bind(&BluetoothGattDescriptorClientImpl::OnPropertyChanged, | 153 base::Bind(&BluetoothGattDescriptorClientImpl::OnPropertyChanged, |
| 85 weak_ptr_factory_.GetWeakPtr(), | 154 weak_ptr_factory_.GetWeakPtr(), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 // observers. | 190 // observers. |
| 122 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, | 191 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 123 const std::string& property_name) { | 192 const std::string& property_name) { |
| 124 VLOG(2) << "Remote GATT descriptor property changed: " | 193 VLOG(2) << "Remote GATT descriptor property changed: " |
| 125 << object_path.value() << ": " << property_name; | 194 << object_path.value() << ": " << property_name; |
| 126 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 195 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
| 127 GattDescriptorPropertyChanged(object_path, | 196 GattDescriptorPropertyChanged(object_path, |
| 128 property_name)); | 197 property_name)); |
| 129 } | 198 } |
| 130 | 199 |
| 200 // Called when a response for a successful method call is received. | |
| 201 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | |
| 202 DCHECK(response); | |
| 203 callback.Run(); | |
| 204 } | |
| 205 | |
| 206 // Called when a descriptor value response for a successful method call is | |
| 207 // received. | |
| 208 void OnValueSuccess(const ValueCallback& callback, dbus::Response* response) { | |
| 209 DCHECK(response); | |
| 210 dbus::MessageReader reader(response); | |
| 211 | |
| 212 const uint8* bytes = NULL; | |
| 213 size_t length = 0; | |
| 214 | |
| 215 if (!reader.PopArrayOfBytes(&bytes, &length)) | |
| 216 VLOG(2) << "Error reading array of bytes in ValueCallback"; | |
| 217 | |
| 218 std::vector<uint8> value; | |
| 219 | |
| 220 if (bytes) | |
| 221 value.assign(bytes, bytes + length); | |
| 222 | |
| 223 callback.Run(value); | |
| 224 } | |
| 225 | |
| 226 // Called when a response for a failed method call is received. | |
| 227 void OnError(const ErrorCallback& error_callback, | |
| 228 dbus::ErrorResponse* response) { | |
| 229 // Error response has optional error message argument. | |
| 230 std::string error_name; | |
| 231 std::string error_message; | |
| 232 if (response) { | |
| 233 dbus::MessageReader reader(response); | |
| 234 error_name = response->GetErrorName(); | |
| 235 reader.PopString(&error_message); | |
| 236 } else { | |
| 237 error_name = kNoResponseError; | |
| 238 error_message = ""; | |
| 239 } | |
| 240 error_callback.Run(error_name, error_message); | |
| 241 } | |
| 242 | |
| 131 dbus::ObjectManager* object_manager_; | 243 dbus::ObjectManager* object_manager_; |
| 132 | 244 |
| 133 // List of observers interested in event notifications from us. | 245 // List of observers interested in event notifications from us. |
| 134 ObserverList<BluetoothGattDescriptorClient::Observer> observers_; | 246 ObserverList<BluetoothGattDescriptorClient::Observer> observers_; |
| 135 | 247 |
| 136 // Weak pointer factory for generating 'this' pointers that might live longer | 248 // Weak pointer factory for generating 'this' pointers that might live longer |
| 137 // than we do. | 249 // than we do. |
| 138 // Note: This should remain the last member so it'll be destroyed and | 250 // Note: This should remain the last member so it'll be destroyed and |
| 139 // invalidate its weak pointers before any other members are destroyed. | 251 // invalidate its weak pointers before any other members are destroyed. |
| 140 base::WeakPtrFactory<BluetoothGattDescriptorClientImpl> weak_ptr_factory_; | 252 base::WeakPtrFactory<BluetoothGattDescriptorClientImpl> weak_ptr_factory_; |
| 141 | 253 |
| 142 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorClientImpl); | 254 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorClientImpl); |
| 143 }; | 255 }; |
| 144 | 256 |
| 145 BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() { | 257 BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() { |
| 146 } | 258 } |
| 147 | 259 |
| 148 BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() { | 260 BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() { |
| 149 } | 261 } |
| 150 | 262 |
| 151 // static | 263 // static |
| 152 BluetoothGattDescriptorClient* BluetoothGattDescriptorClient::Create() { | 264 BluetoothGattDescriptorClient* BluetoothGattDescriptorClient::Create() { |
| 153 return new BluetoothGattDescriptorClientImpl(); | 265 return new BluetoothGattDescriptorClientImpl(); |
| 154 } | 266 } |
| 155 | 267 |
| 156 } // namespace chromeos | 268 } // namespace chromeos |
| OLD | NEW |