| 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 "device/bluetooth/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 bluez { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // TODO(armansito): Move this constant to cros_system_api. | 18 // TODO(armansito): Move this constant to cros_system_api. |
| 19 const char kValueProperty[] = "Value"; | 19 const char kValueProperty[] = "Value"; |
| 20 | 20 |
| 21 } // namespace | 21 } // namespace |
| 22 | 22 |
| 23 // static | 23 // static |
| 24 const char BluetoothGattDescriptorClient::kNoResponseError[] = | 24 const char BluetoothGattDescriptorClient::kNoResponseError[] = |
| 25 "org.chromium.Error.NoResponse"; | 25 "org.chromium.Error.NoResponse"; |
| 26 // static | 26 // static |
| 27 const char BluetoothGattDescriptorClient::kUnknownDescriptorError[] = | 27 const char BluetoothGattDescriptorClient::kUnknownDescriptorError[] = |
| 28 "org.chromium.Error.UnknownDescriptor"; | 28 "org.chromium.Error.UnknownDescriptor"; |
| 29 | 29 |
| 30 BluetoothGattDescriptorClient::Properties::Properties( | 30 BluetoothGattDescriptorClient::Properties::Properties( |
| 31 dbus::ObjectProxy* object_proxy, | 31 dbus::ObjectProxy* object_proxy, |
| 32 const std::string& interface_name, | 32 const std::string& interface_name, |
| 33 const PropertyChangedCallback&callback) | 33 const PropertyChangedCallback& callback) |
| 34 : dbus::PropertySet(object_proxy, interface_name, callback) { | 34 : dbus::PropertySet(object_proxy, interface_name, callback) { |
| 35 RegisterProperty(bluetooth_gatt_descriptor::kUUIDProperty, &uuid); | 35 RegisterProperty(bluetooth_gatt_descriptor::kUUIDProperty, &uuid); |
| 36 RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty, | 36 RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty, |
| 37 &characteristic); | 37 &characteristic); |
| 38 RegisterProperty(kValueProperty, &value); | 38 RegisterProperty(kValueProperty, &value); |
| 39 } | 39 } |
| 40 | 40 |
| 41 BluetoothGattDescriptorClient::Properties::~Properties() { | 41 BluetoothGattDescriptorClient::Properties::~Properties() {} |
| 42 } | |
| 43 | 42 |
| 44 // The BluetoothGattDescriptorClient implementation used in production. | 43 // The BluetoothGattDescriptorClient implementation used in production. |
| 45 class BluetoothGattDescriptorClientImpl | 44 class BluetoothGattDescriptorClientImpl |
| 46 : public BluetoothGattDescriptorClient, | 45 : public BluetoothGattDescriptorClient, |
| 47 public dbus::ObjectManager::Interface { | 46 public dbus::ObjectManager::Interface { |
| 48 public: | 47 public: |
| 49 BluetoothGattDescriptorClientImpl() | 48 BluetoothGattDescriptorClientImpl() |
| 50 : object_manager_(NULL), | 49 : object_manager_(NULL), weak_ptr_factory_(this) {} |
| 51 weak_ptr_factory_(this) { | |
| 52 } | |
| 53 | 50 |
| 54 ~BluetoothGattDescriptorClientImpl() override { | 51 ~BluetoothGattDescriptorClientImpl() override { |
| 55 object_manager_->UnregisterInterface( | 52 object_manager_->UnregisterInterface( |
| 56 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface); | 53 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface); |
| 57 } | 54 } |
| 58 | 55 |
| 59 // BluetoothGattDescriptorClientImpl override. | 56 // BluetoothGattDescriptorClientImpl override. |
| 60 void AddObserver(BluetoothGattDescriptorClient::Observer* observer) override { | 57 void AddObserver(BluetoothGattDescriptorClient::Observer* observer) override { |
| 61 DCHECK(observer); | 58 DCHECK(observer); |
| 62 observers_.AddObserver(observer); | 59 observers_.AddObserver(observer); |
| 63 } | 60 } |
| 64 | 61 |
| 65 // BluetoothGattDescriptorClientImpl override. | 62 // BluetoothGattDescriptorClientImpl override. |
| 66 void RemoveObserver( | 63 void RemoveObserver( |
| 67 BluetoothGattDescriptorClient::Observer* observer) override { | 64 BluetoothGattDescriptorClient::Observer* observer) override { |
| 68 DCHECK(observer); | 65 DCHECK(observer); |
| 69 observers_.RemoveObserver(observer); | 66 observers_.RemoveObserver(observer); |
| 70 } | 67 } |
| 71 | 68 |
| 72 // BluetoothGattDescriptorClientImpl override. | 69 // BluetoothGattDescriptorClientImpl override. |
| 73 std::vector<dbus::ObjectPath> GetDescriptors() override { | 70 std::vector<dbus::ObjectPath> GetDescriptors() override { |
| 74 DCHECK(object_manager_); | 71 DCHECK(object_manager_); |
| 75 return object_manager_->GetObjectsWithInterface( | 72 return object_manager_->GetObjectsWithInterface( |
| 76 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface); | 73 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface); |
| 77 } | 74 } |
| 78 | 75 |
| 79 // BluetoothGattDescriptorClientImpl override. | 76 // BluetoothGattDescriptorClientImpl override. |
| 80 Properties* GetProperties(const dbus::ObjectPath& object_path) override { | 77 Properties* GetProperties(const dbus::ObjectPath& object_path) override { |
| 81 DCHECK(object_manager_); | 78 DCHECK(object_manager_); |
| 82 return static_cast<Properties*>( | 79 return static_cast<Properties*>(object_manager_->GetProperties( |
| 83 object_manager_->GetProperties( | 80 object_path, |
| 84 object_path, | 81 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface)); |
| 85 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface)); | |
| 86 } | 82 } |
| 87 | 83 |
| 88 // BluetoothGattDescriptorClientImpl override. | 84 // BluetoothGattDescriptorClientImpl override. |
| 89 void ReadValue(const dbus::ObjectPath& object_path, | 85 void ReadValue(const dbus::ObjectPath& object_path, |
| 90 const ValueCallback& callback, | 86 const ValueCallback& callback, |
| 91 const ErrorCallback& error_callback) override { | 87 const ErrorCallback& error_callback) override { |
| 92 dbus::ObjectProxy* object_proxy = | 88 dbus::ObjectProxy* object_proxy = |
| 93 object_manager_->GetObjectProxy(object_path); | 89 object_manager_->GetObjectProxy(object_path); |
| 94 if (!object_proxy) { | 90 if (!object_proxy) { |
| 95 error_callback.Run(kUnknownDescriptorError, ""); | 91 error_callback.Run(kUnknownDescriptorError, ""); |
| 96 return; | 92 return; |
| 97 } | 93 } |
| 98 | 94 |
| 99 dbus::MethodCall method_call( | 95 dbus::MethodCall method_call( |
| 100 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, | 96 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, |
| 101 bluetooth_gatt_descriptor::kReadValue); | 97 bluetooth_gatt_descriptor::kReadValue); |
| 102 | 98 |
| 103 object_proxy->CallMethodWithErrorCallback( | 99 object_proxy->CallMethodWithErrorCallback( |
| 104 &method_call, | 100 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 105 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 106 base::Bind(&BluetoothGattDescriptorClientImpl::OnValueSuccess, | 101 base::Bind(&BluetoothGattDescriptorClientImpl::OnValueSuccess, |
| 107 weak_ptr_factory_.GetWeakPtr(), | 102 weak_ptr_factory_.GetWeakPtr(), callback), |
| 108 callback), | |
| 109 base::Bind(&BluetoothGattDescriptorClientImpl::OnError, | 103 base::Bind(&BluetoothGattDescriptorClientImpl::OnError, |
| 110 weak_ptr_factory_.GetWeakPtr(), | 104 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 111 error_callback)); | |
| 112 } | 105 } |
| 113 | 106 |
| 114 // BluetoothGattDescriptorClientImpl override. | 107 // BluetoothGattDescriptorClientImpl override. |
| 115 void WriteValue(const dbus::ObjectPath& object_path, | 108 void WriteValue(const dbus::ObjectPath& object_path, |
| 116 const std::vector<uint8>& value, | 109 const std::vector<uint8>& value, |
| 117 const base::Closure& callback, | 110 const base::Closure& callback, |
| 118 const ErrorCallback& error_callback) override { | 111 const ErrorCallback& error_callback) override { |
| 119 dbus::ObjectProxy* object_proxy = | 112 dbus::ObjectProxy* object_proxy = |
| 120 object_manager_->GetObjectProxy(object_path); | 113 object_manager_->GetObjectProxy(object_path); |
| 121 if (!object_proxy) { | 114 if (!object_proxy) { |
| 122 error_callback.Run(kUnknownDescriptorError, ""); | 115 error_callback.Run(kUnknownDescriptorError, ""); |
| 123 return; | 116 return; |
| 124 } | 117 } |
| 125 | 118 |
| 126 dbus::MethodCall method_call( | 119 dbus::MethodCall method_call( |
| 127 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, | 120 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, |
| 128 bluetooth_gatt_descriptor::kWriteValue); | 121 bluetooth_gatt_descriptor::kWriteValue); |
| 129 dbus::MessageWriter writer(&method_call); | 122 dbus::MessageWriter writer(&method_call); |
| 130 writer.AppendArrayOfBytes(value.data(), value.size()); | 123 writer.AppendArrayOfBytes(value.data(), value.size()); |
| 131 | 124 |
| 132 object_proxy->CallMethodWithErrorCallback( | 125 object_proxy->CallMethodWithErrorCallback( |
| 133 &method_call, | 126 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 134 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 135 base::Bind(&BluetoothGattDescriptorClientImpl::OnSuccess, | 127 base::Bind(&BluetoothGattDescriptorClientImpl::OnSuccess, |
| 136 weak_ptr_factory_.GetWeakPtr(), | 128 weak_ptr_factory_.GetWeakPtr(), callback), |
| 137 callback), | |
| 138 base::Bind(&BluetoothGattDescriptorClientImpl::OnError, | 129 base::Bind(&BluetoothGattDescriptorClientImpl::OnError, |
| 139 weak_ptr_factory_.GetWeakPtr(), | 130 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 140 error_callback)); | |
| 141 } | 131 } |
| 142 | 132 |
| 143 // dbus::ObjectManager::Interface override. | 133 // dbus::ObjectManager::Interface override. |
| 144 dbus::PropertySet* CreateProperties( | 134 dbus::PropertySet* CreateProperties( |
| 145 dbus::ObjectProxy* object_proxy, | 135 dbus::ObjectProxy* object_proxy, |
| 146 const dbus::ObjectPath& object_path, | 136 const dbus::ObjectPath& object_path, |
| 147 const std::string& interface_name) override { | 137 const std::string& interface_name) override { |
| 148 Properties* properties = new Properties( | 138 Properties* properties = new Properties( |
| 149 object_proxy, | 139 object_proxy, interface_name, |
| 150 interface_name, | |
| 151 base::Bind(&BluetoothGattDescriptorClientImpl::OnPropertyChanged, | 140 base::Bind(&BluetoothGattDescriptorClientImpl::OnPropertyChanged, |
| 152 weak_ptr_factory_.GetWeakPtr(), | 141 weak_ptr_factory_.GetWeakPtr(), object_path)); |
| 153 object_path)); | |
| 154 return static_cast<dbus::PropertySet*>(properties); | 142 return static_cast<dbus::PropertySet*>(properties); |
| 155 } | 143 } |
| 156 | 144 |
| 157 // dbus::ObjectManager::Interface override. | 145 // dbus::ObjectManager::Interface override. |
| 158 void ObjectAdded(const dbus::ObjectPath& object_path, | 146 void ObjectAdded(const dbus::ObjectPath& object_path, |
| 159 const std::string& interface_name) override { | 147 const std::string& interface_name) override { |
| 160 VLOG(2) << "Remote GATT descriptor added: " << object_path.value(); | 148 VLOG(2) << "Remote GATT descriptor added: " << object_path.value(); |
| 161 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 149 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
| 162 GattDescriptorAdded(object_path)); | 150 GattDescriptorAdded(object_path)); |
| 163 } | 151 } |
| 164 | 152 |
| 165 // dbus::ObjectManager::Interface override. | 153 // dbus::ObjectManager::Interface override. |
| 166 void ObjectRemoved(const dbus::ObjectPath& object_path, | 154 void ObjectRemoved(const dbus::ObjectPath& object_path, |
| 167 const std::string& interface_name) override { | 155 const std::string& interface_name) override { |
| 168 VLOG(2) << "Remote GATT descriptor removed: " << object_path.value(); | 156 VLOG(2) << "Remote GATT descriptor removed: " << object_path.value(); |
| 169 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 157 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, |
| 170 GattDescriptorRemoved(object_path)); | 158 GattDescriptorRemoved(object_path)); |
| 171 } | 159 } |
| 172 | 160 |
| 173 protected: | 161 protected: |
| 174 // chromeos::DBusClient override. | 162 // chromeos::DBusClient override. |
| 175 void Init(dbus::Bus* bus) override { | 163 void Init(dbus::Bus* bus) override { |
| 176 object_manager_ = bus->GetObjectManager( | 164 object_manager_ = bus->GetObjectManager( |
| 177 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | 165 bluetooth_object_manager::kBluetoothObjectManagerServiceName, |
| 178 dbus::ObjectPath( | 166 dbus::ObjectPath( |
| 179 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | 167 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); |
| 180 object_manager_->RegisterInterface( | 168 object_manager_->RegisterInterface( |
| 181 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, | 169 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, this); |
| 182 this); | |
| 183 } | 170 } |
| 184 | 171 |
| 185 private: | 172 private: |
| 186 // Called by dbus::PropertySet when a property value is changed, either by | 173 // Called by dbus::PropertySet when a property value is changed, either by |
| 187 // result of a signal or response to a GetAll() or Get() call. Informs | 174 // result of a signal or response to a GetAll() or Get() call. Informs |
| 188 // observers. | 175 // observers. |
| 189 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, | 176 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 190 const std::string& property_name) { | 177 const std::string& property_name) { |
| 191 VLOG(2) << "Remote GATT descriptor property changed: " | 178 VLOG(2) << "Remote GATT descriptor property changed: " |
| 192 << object_path.value() << ": " << property_name; | 179 << object_path.value() << ": " << property_name; |
| 193 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer, observers_, | 180 FOR_EACH_OBSERVER( |
| 194 GattDescriptorPropertyChanged(object_path, | 181 BluetoothGattDescriptorClient::Observer, observers_, |
| 195 property_name)); | 182 GattDescriptorPropertyChanged(object_path, property_name)); |
| 196 } | 183 } |
| 197 | 184 |
| 198 // Called when a response for a successful method call is received. | 185 // Called when a response for a successful method call is received. |
| 199 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | 186 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
| 200 DCHECK(response); | 187 DCHECK(response); |
| 201 callback.Run(); | 188 callback.Run(); |
| 202 } | 189 } |
| 203 | 190 |
| 204 // Called when a descriptor value response for a successful method call is | 191 // Called when a descriptor value response for a successful method call is |
| 205 // received. | 192 // received. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 | 232 |
| 246 // Weak pointer factory for generating 'this' pointers that might live longer | 233 // Weak pointer factory for generating 'this' pointers that might live longer |
| 247 // than we do. | 234 // than we do. |
| 248 // Note: This should remain the last member so it'll be destroyed and | 235 // Note: This should remain the last member so it'll be destroyed and |
| 249 // invalidate its weak pointers before any other members are destroyed. | 236 // invalidate its weak pointers before any other members are destroyed. |
| 250 base::WeakPtrFactory<BluetoothGattDescriptorClientImpl> weak_ptr_factory_; | 237 base::WeakPtrFactory<BluetoothGattDescriptorClientImpl> weak_ptr_factory_; |
| 251 | 238 |
| 252 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorClientImpl); | 239 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorClientImpl); |
| 253 }; | 240 }; |
| 254 | 241 |
| 255 BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() { | 242 BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() {} |
| 256 } | |
| 257 | 243 |
| 258 BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() { | 244 BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() {} |
| 259 } | |
| 260 | 245 |
| 261 // static | 246 // static |
| 262 BluetoothGattDescriptorClient* BluetoothGattDescriptorClient::Create() { | 247 BluetoothGattDescriptorClient* BluetoothGattDescriptorClient::Create() { |
| 263 return new BluetoothGattDescriptorClientImpl(); | 248 return new BluetoothGattDescriptorClientImpl(); |
| 264 } | 249 } |
| 265 | 250 |
| 266 } // namespace chromeos | 251 } // namespace bluez |
| OLD | NEW |