| 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 object_path)); | 211 object_path)); |
| 204 return static_cast<dbus::PropertySet*>(properties); | 212 return static_cast<dbus::PropertySet*>(properties); |
| 205 } | 213 } |
| 206 | 214 |
| 207 // dbus::ObjectManager::Interface override. | 215 // dbus::ObjectManager::Interface override. |
| 208 void ObjectAdded(const dbus::ObjectPath& object_path, | 216 void ObjectAdded(const dbus::ObjectPath& object_path, |
| 209 const std::string& interface_name) override { | 217 const std::string& interface_name) override { |
| 210 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); | 218 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); |
| 211 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 219 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
| 212 GattCharacteristicAdded(object_path)); | 220 GattCharacteristicAdded(object_path)); |
| 213 | |
| 214 // Connect the "ValueUpdated" signal. | |
| 215 dbus::ObjectProxy* object_proxy = | |
| 216 object_manager_->GetObjectProxy(object_path); | |
| 217 DCHECK(object_proxy); | |
| 218 | |
| 219 object_proxy->ConnectToSignal( | |
| 220 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | |
| 221 bluetooth_gatt_characteristic::kValueUpdatedSignal, | |
| 222 base::Bind(&BluetoothGattCharacteristicClientImpl::ValueUpdatedReceived, | |
| 223 weak_ptr_factory_.GetWeakPtr(), | |
| 224 object_path), | |
| 225 base::Bind( | |
| 226 &BluetoothGattCharacteristicClientImpl::ValueUpdatedConnected, | |
| 227 weak_ptr_factory_.GetWeakPtr())); | |
| 228 } | 221 } |
| 229 | 222 |
| 230 // dbus::ObjectManager::Interface override. | 223 // dbus::ObjectManager::Interface override. |
| 231 void ObjectRemoved(const dbus::ObjectPath& object_path, | 224 void ObjectRemoved(const dbus::ObjectPath& object_path, |
| 232 const std::string& interface_name) override { | 225 const std::string& interface_name) override { |
| 233 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value(); | 226 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value(); |
| 234 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 227 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
| 235 GattCharacteristicRemoved(object_path)); | 228 GattCharacteristicRemoved(object_path)); |
| 236 } | 229 } |
| 237 | 230 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 253 // observers. | 246 // observers. |
| 254 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, | 247 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 255 const std::string& property_name) { | 248 const std::string& property_name) { |
| 256 VLOG(2) << "Remote GATT characteristic property changed: " | 249 VLOG(2) << "Remote GATT characteristic property changed: " |
| 257 << object_path.value() << ": " << property_name; | 250 << object_path.value() << ": " << property_name; |
| 258 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 251 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
| 259 GattCharacteristicPropertyChanged(object_path, | 252 GattCharacteristicPropertyChanged(object_path, |
| 260 property_name)); | 253 property_name)); |
| 261 } | 254 } |
| 262 | 255 |
| 263 // Called by dbus:: when a "ValueUpdated" signal is received. | |
| 264 void ValueUpdatedReceived(const dbus::ObjectPath& object_path, | |
| 265 dbus::Signal* signal) { | |
| 266 DCHECK(signal); | |
| 267 const uint8* bytes = NULL; | |
| 268 size_t length = 0; | |
| 269 dbus::MessageReader reader(signal); | |
| 270 if (!reader.PopArrayOfBytes(&bytes, &length)) { | |
| 271 LOG(WARNING) << "ValueUpdated signal has incorrect parameters: " | |
| 272 << signal->ToString(); | |
| 273 return; | |
| 274 } | |
| 275 | |
| 276 std::vector<uint8> value; | |
| 277 if (bytes) | |
| 278 value.assign(bytes, bytes + length); | |
| 279 | |
| 280 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, | |
| 281 observers_, | |
| 282 GattCharacteristicValueUpdated(object_path, value)); | |
| 283 } | |
| 284 | |
| 285 // Called by dbus:: when the "ValueUpdated" signal is initially connected. | |
| 286 void ValueUpdatedConnected(const std::string& interface_name, | |
| 287 const std::string& signal_name, | |
| 288 bool success) { | |
| 289 LOG_IF(WARNING, !success) << "Failed to connect to the ValueUpdated signal"; | |
| 290 } | |
| 291 | |
| 292 // Called when a response for successful method call is received. | 256 // Called when a response for successful method call is received. |
| 293 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | 257 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
| 294 DCHECK(response); | 258 DCHECK(response); |
| 295 callback.Run(); | 259 callback.Run(); |
| 296 } | 260 } |
| 297 | 261 |
| 298 // Called when a characteristic value response for a successful method call | 262 // Called when a characteristic value response for a successful method call |
| 299 // is received. | 263 // is received. |
| 300 void OnValueSuccess(const ValueCallback& callback, dbus::Response* response) { | 264 void OnValueSuccess(const ValueCallback& callback, dbus::Response* response) { |
| 301 DCHECK(response); | 265 DCHECK(response); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 | 316 |
| 353 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() { | 317 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() { |
| 354 } | 318 } |
| 355 | 319 |
| 356 // static | 320 // static |
| 357 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { | 321 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { |
| 358 return new BluetoothGattCharacteristicClientImpl(); | 322 return new BluetoothGattCharacteristicClientImpl(); |
| 359 } | 323 } |
| 360 | 324 |
| 361 } // namespace chromeos | 325 } // namespace chromeos |
| OLD | NEW |