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_characteristic_client.h" | 5 #include "chromeos/dbus/bluetooth_gatt_characteristic_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 this constant to cros_system_api. |
| 19 const char kValueProperty[] = "Value"; |
| 20 |
| 21 } // namespace |
| 22 |
16 // static | 23 // static |
17 const char BluetoothGattCharacteristicClient::kNoResponseError[] = | 24 const char BluetoothGattCharacteristicClient::kNoResponseError[] = |
18 "org.chromium.Error.NoResponse"; | 25 "org.chromium.Error.NoResponse"; |
19 // static | 26 // static |
20 const char BluetoothGattCharacteristicClient::kUnknownCharacteristicError[] = | 27 const char BluetoothGattCharacteristicClient::kUnknownCharacteristicError[] = |
21 "org.chromium.Error.UnknownCharacteristic"; | 28 "org.chromium.Error.UnknownCharacteristic"; |
22 | 29 |
23 BluetoothGattCharacteristicClient::Properties::Properties( | 30 BluetoothGattCharacteristicClient::Properties::Properties( |
24 dbus::ObjectProxy* object_proxy, | 31 dbus::ObjectProxy* object_proxy, |
25 const std::string& interface_name, | 32 const std::string& interface_name, |
26 const PropertyChangedCallback& callback) | 33 const PropertyChangedCallback& callback) |
27 : dbus::PropertySet(object_proxy, interface_name, callback) { | 34 : dbus::PropertySet(object_proxy, interface_name, callback) { |
28 RegisterProperty(bluetooth_gatt_characteristic::kUUIDProperty, &uuid); | 35 RegisterProperty(bluetooth_gatt_characteristic::kUUIDProperty, &uuid); |
29 RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty, &service); | 36 RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty, &service); |
| 37 RegisterProperty(kValueProperty, &value); |
30 RegisterProperty(bluetooth_gatt_characteristic::kNotifyingProperty, | 38 RegisterProperty(bluetooth_gatt_characteristic::kNotifyingProperty, |
31 ¬ifying); | 39 ¬ifying); |
32 RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty, &flags); | 40 RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty, &flags); |
33 RegisterProperty(bluetooth_gatt_characteristic::kDescriptorsProperty, | 41 RegisterProperty(bluetooth_gatt_characteristic::kDescriptorsProperty, |
34 &descriptors); | 42 &descriptors); |
35 } | 43 } |
36 | 44 |
37 BluetoothGattCharacteristicClient::Properties::~Properties() { | 45 BluetoothGattCharacteristicClient::Properties::~Properties() { |
38 } | 46 } |
39 | 47 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 object_path)); | 212 object_path)); |
205 return static_cast<dbus::PropertySet*>(properties); | 213 return static_cast<dbus::PropertySet*>(properties); |
206 } | 214 } |
207 | 215 |
208 // dbus::ObjectManager::Interface override. | 216 // dbus::ObjectManager::Interface override. |
209 virtual void ObjectAdded(const dbus::ObjectPath& object_path, | 217 virtual void ObjectAdded(const dbus::ObjectPath& object_path, |
210 const std::string& interface_name) override { | 218 const std::string& interface_name) override { |
211 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); | 219 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); |
212 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 220 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
213 GattCharacteristicAdded(object_path)); | 221 GattCharacteristicAdded(object_path)); |
214 | |
215 // Connect the "ValueUpdated" signal. | |
216 dbus::ObjectProxy* object_proxy = | |
217 object_manager_->GetObjectProxy(object_path); | |
218 DCHECK(object_proxy); | |
219 | |
220 object_proxy->ConnectToSignal( | |
221 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | |
222 bluetooth_gatt_characteristic::kValueUpdatedSignal, | |
223 base::Bind(&BluetoothGattCharacteristicClientImpl::ValueUpdatedReceived, | |
224 weak_ptr_factory_.GetWeakPtr(), | |
225 object_path), | |
226 base::Bind( | |
227 &BluetoothGattCharacteristicClientImpl::ValueUpdatedConnected, | |
228 weak_ptr_factory_.GetWeakPtr())); | |
229 } | 222 } |
230 | 223 |
231 // dbus::ObjectManager::Interface override. | 224 // dbus::ObjectManager::Interface override. |
232 virtual void ObjectRemoved(const dbus::ObjectPath& object_path, | 225 virtual void ObjectRemoved(const dbus::ObjectPath& object_path, |
233 const std::string& interface_name) override { | 226 const std::string& interface_name) override { |
234 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value(); | 227 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value(); |
235 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 228 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
236 GattCharacteristicRemoved(object_path)); | 229 GattCharacteristicRemoved(object_path)); |
237 } | 230 } |
238 | 231 |
(...skipping 15 matching lines...) Expand all Loading... |
254 // observers. | 247 // observers. |
255 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, | 248 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, |
256 const std::string& property_name) { | 249 const std::string& property_name) { |
257 VLOG(2) << "Remote GATT characteristic property changed: " | 250 VLOG(2) << "Remote GATT characteristic property changed: " |
258 << object_path.value() << ": " << property_name; | 251 << object_path.value() << ": " << property_name; |
259 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 252 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
260 GattCharacteristicPropertyChanged(object_path, | 253 GattCharacteristicPropertyChanged(object_path, |
261 property_name)); | 254 property_name)); |
262 } | 255 } |
263 | 256 |
264 // Called by dbus:: when a "ValueUpdated" signal is received. | |
265 void ValueUpdatedReceived(const dbus::ObjectPath& object_path, | |
266 dbus::Signal* signal) { | |
267 DCHECK(signal); | |
268 const uint8* bytes = NULL; | |
269 size_t length = 0; | |
270 dbus::MessageReader reader(signal); | |
271 if (!reader.PopArrayOfBytes(&bytes, &length)) { | |
272 LOG(WARNING) << "ValueUpdated signal has incorrect parameters: " | |
273 << signal->ToString(); | |
274 return; | |
275 } | |
276 | |
277 std::vector<uint8> value; | |
278 if (bytes) | |
279 value.assign(bytes, bytes + length); | |
280 | |
281 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, | |
282 observers_, | |
283 GattCharacteristicValueUpdated(object_path, value)); | |
284 } | |
285 | |
286 // Called by dbus:: when the "ValueUpdated" signal is initially connected. | |
287 void ValueUpdatedConnected(const std::string& interface_name, | |
288 const std::string& signal_name, | |
289 bool success) { | |
290 LOG_IF(WARNING, !success) << "Failed to connect to the ValueUpdated signal"; | |
291 } | |
292 | |
293 // Called when a response for successful method call is received. | 257 // Called when a response for successful method call is received. |
294 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | 258 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
295 DCHECK(response); | 259 DCHECK(response); |
296 callback.Run(); | 260 callback.Run(); |
297 } | 261 } |
298 | 262 |
299 // Called when a characteristic value response for a successful method call | 263 // Called when a characteristic value response for a successful method call |
300 // is received. | 264 // is received. |
301 void OnValueSuccess(const ValueCallback& callback, dbus::Response* response) { | 265 void OnValueSuccess(const ValueCallback& callback, dbus::Response* response) { |
302 DCHECK(response); | 266 DCHECK(response); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 | 317 |
354 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() { | 318 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() { |
355 } | 319 } |
356 | 320 |
357 // static | 321 // static |
358 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { | 322 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { |
359 return new BluetoothGattCharacteristicClientImpl(); | 323 return new BluetoothGattCharacteristicClientImpl(); |
360 } | 324 } |
361 | 325 |
362 } // namespace chromeos | 326 } // namespace chromeos |
OLD | NEW |