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