| 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" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
| 48 } | 48 } |
| 49 | 49 |
| 50 virtual ~BluetoothGattCharacteristicClientImpl() { | 50 virtual ~BluetoothGattCharacteristicClientImpl() { |
| 51 object_manager_->UnregisterInterface( | 51 object_manager_->UnregisterInterface( |
| 52 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); | 52 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // BluetoothGattCharacteristicClient override. | 55 // BluetoothGattCharacteristicClient override. |
| 56 virtual void AddObserver( | 56 virtual void AddObserver( |
| 57 BluetoothGattCharacteristicClient::Observer* observer) OVERRIDE { | 57 BluetoothGattCharacteristicClient::Observer* observer) override { |
| 58 DCHECK(observer); | 58 DCHECK(observer); |
| 59 observers_.AddObserver(observer); | 59 observers_.AddObserver(observer); |
| 60 } | 60 } |
| 61 | 61 |
| 62 // BluetoothGattCharacteristicClient override. | 62 // BluetoothGattCharacteristicClient override. |
| 63 virtual void RemoveObserver( | 63 virtual void RemoveObserver( |
| 64 BluetoothGattCharacteristicClient::Observer* observer) OVERRIDE { | 64 BluetoothGattCharacteristicClient::Observer* observer) override { |
| 65 DCHECK(observer); | 65 DCHECK(observer); |
| 66 observers_.RemoveObserver(observer); | 66 observers_.RemoveObserver(observer); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // BluetoothGattCharacteristicClient override. | 69 // BluetoothGattCharacteristicClient override. |
| 70 virtual std::vector<dbus::ObjectPath> GetCharacteristics() OVERRIDE { | 70 virtual std::vector<dbus::ObjectPath> GetCharacteristics() override { |
| 71 DCHECK(object_manager_); | 71 DCHECK(object_manager_); |
| 72 return object_manager_->GetObjectsWithInterface( | 72 return object_manager_->GetObjectsWithInterface( |
| 73 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); | 73 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // BluetoothGattCharacteristicClient override. | 76 // BluetoothGattCharacteristicClient override. |
| 77 virtual Properties* GetProperties( | 77 virtual Properties* GetProperties( |
| 78 const dbus::ObjectPath& object_path) OVERRIDE { | 78 const dbus::ObjectPath& object_path) override { |
| 79 DCHECK(object_manager_); | 79 DCHECK(object_manager_); |
| 80 return static_cast<Properties*>( | 80 return static_cast<Properties*>( |
| 81 object_manager_->GetProperties( | 81 object_manager_->GetProperties( |
| 82 object_path, | 82 object_path, |
| 83 bluetooth_gatt_characteristic:: | 83 bluetooth_gatt_characteristic:: |
| 84 kBluetoothGattCharacteristicInterface)); | 84 kBluetoothGattCharacteristicInterface)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // BluetoothGattCharacteristicClient override. | 87 // BluetoothGattCharacteristicClient override. |
| 88 virtual void ReadValue(const dbus::ObjectPath& object_path, | 88 virtual void ReadValue(const dbus::ObjectPath& object_path, |
| 89 const ValueCallback& callback, | 89 const ValueCallback& callback, |
| 90 const ErrorCallback& error_callback) OVERRIDE { | 90 const ErrorCallback& error_callback) override { |
| 91 dbus::ObjectProxy* object_proxy = | 91 dbus::ObjectProxy* object_proxy = |
| 92 object_manager_->GetObjectProxy(object_path); | 92 object_manager_->GetObjectProxy(object_path); |
| 93 if (!object_proxy) { | 93 if (!object_proxy) { |
| 94 error_callback.Run(kUnknownCharacteristicError, ""); | 94 error_callback.Run(kUnknownCharacteristicError, ""); |
| 95 return; | 95 return; |
| 96 } | 96 } |
| 97 | 97 |
| 98 dbus::MethodCall method_call( | 98 dbus::MethodCall method_call( |
| 99 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 99 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
| 100 bluetooth_gatt_characteristic::kReadValue); | 100 bluetooth_gatt_characteristic::kReadValue); |
| 101 | 101 |
| 102 object_proxy->CallMethodWithErrorCallback( | 102 object_proxy->CallMethodWithErrorCallback( |
| 103 &method_call, | 103 &method_call, |
| 104 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 104 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 105 base::Bind(&BluetoothGattCharacteristicClientImpl::OnValueSuccess, | 105 base::Bind(&BluetoothGattCharacteristicClientImpl::OnValueSuccess, |
| 106 weak_ptr_factory_.GetWeakPtr(), | 106 weak_ptr_factory_.GetWeakPtr(), |
| 107 callback), | 107 callback), |
| 108 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, | 108 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, |
| 109 weak_ptr_factory_.GetWeakPtr(), | 109 weak_ptr_factory_.GetWeakPtr(), |
| 110 error_callback)); | 110 error_callback)); |
| 111 } | 111 } |
| 112 | 112 |
| 113 // BluetoothGattCharacteristicClient override. | 113 // BluetoothGattCharacteristicClient override. |
| 114 virtual void WriteValue(const dbus::ObjectPath& object_path, | 114 virtual void WriteValue(const dbus::ObjectPath& object_path, |
| 115 const std::vector<uint8>& value, | 115 const std::vector<uint8>& value, |
| 116 const base::Closure& callback, | 116 const base::Closure& callback, |
| 117 const ErrorCallback& error_callback) OVERRIDE { | 117 const ErrorCallback& error_callback) override { |
| 118 dbus::ObjectProxy* object_proxy = | 118 dbus::ObjectProxy* object_proxy = |
| 119 object_manager_->GetObjectProxy(object_path); | 119 object_manager_->GetObjectProxy(object_path); |
| 120 if (!object_proxy) { | 120 if (!object_proxy) { |
| 121 error_callback.Run(kUnknownCharacteristicError, ""); | 121 error_callback.Run(kUnknownCharacteristicError, ""); |
| 122 return; | 122 return; |
| 123 } | 123 } |
| 124 | 124 |
| 125 dbus::MethodCall method_call( | 125 dbus::MethodCall method_call( |
| 126 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 126 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
| 127 bluetooth_gatt_characteristic::kWriteValue); | 127 bluetooth_gatt_characteristic::kWriteValue); |
| 128 dbus::MessageWriter writer(&method_call); | 128 dbus::MessageWriter writer(&method_call); |
| 129 writer.AppendArrayOfBytes(value.data(), value.size()); | 129 writer.AppendArrayOfBytes(value.data(), value.size()); |
| 130 | 130 |
| 131 object_proxy->CallMethodWithErrorCallback( | 131 object_proxy->CallMethodWithErrorCallback( |
| 132 &method_call, | 132 &method_call, |
| 133 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 133 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 134 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, | 134 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, |
| 135 weak_ptr_factory_.GetWeakPtr(), | 135 weak_ptr_factory_.GetWeakPtr(), |
| 136 callback), | 136 callback), |
| 137 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, | 137 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, |
| 138 weak_ptr_factory_.GetWeakPtr(), | 138 weak_ptr_factory_.GetWeakPtr(), |
| 139 error_callback)); | 139 error_callback)); |
| 140 } | 140 } |
| 141 | 141 |
| 142 // BluetoothGattCharacteristicClient override. | 142 // BluetoothGattCharacteristicClient override. |
| 143 virtual void StartNotify(const dbus::ObjectPath& object_path, | 143 virtual void StartNotify(const dbus::ObjectPath& object_path, |
| 144 const base::Closure& callback, | 144 const base::Closure& callback, |
| 145 const ErrorCallback& error_callback) OVERRIDE { | 145 const ErrorCallback& error_callback) override { |
| 146 dbus::ObjectProxy* object_proxy = | 146 dbus::ObjectProxy* object_proxy = |
| 147 object_manager_->GetObjectProxy(object_path); | 147 object_manager_->GetObjectProxy(object_path); |
| 148 if (!object_proxy) { | 148 if (!object_proxy) { |
| 149 error_callback.Run(kUnknownCharacteristicError, ""); | 149 error_callback.Run(kUnknownCharacteristicError, ""); |
| 150 return; | 150 return; |
| 151 } | 151 } |
| 152 | 152 |
| 153 dbus::MethodCall method_call( | 153 dbus::MethodCall method_call( |
| 154 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 154 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
| 155 bluetooth_gatt_characteristic::kStartNotify); | 155 bluetooth_gatt_characteristic::kStartNotify); |
| 156 | 156 |
| 157 object_proxy->CallMethodWithErrorCallback( | 157 object_proxy->CallMethodWithErrorCallback( |
| 158 &method_call, | 158 &method_call, |
| 159 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 159 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 160 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, | 160 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, |
| 161 weak_ptr_factory_.GetWeakPtr(), | 161 weak_ptr_factory_.GetWeakPtr(), |
| 162 callback), | 162 callback), |
| 163 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, | 163 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, |
| 164 weak_ptr_factory_.GetWeakPtr(), | 164 weak_ptr_factory_.GetWeakPtr(), |
| 165 error_callback)); | 165 error_callback)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 // BluetoothGattCharacteristicClient override. | 168 // BluetoothGattCharacteristicClient override. |
| 169 virtual void StopNotify(const dbus::ObjectPath& object_path, | 169 virtual void StopNotify(const dbus::ObjectPath& object_path, |
| 170 const base::Closure& callback, | 170 const base::Closure& callback, |
| 171 const ErrorCallback& error_callback) OVERRIDE { | 171 const ErrorCallback& error_callback) override { |
| 172 dbus::ObjectProxy* object_proxy = | 172 dbus::ObjectProxy* object_proxy = |
| 173 object_manager_->GetObjectProxy(object_path); | 173 object_manager_->GetObjectProxy(object_path); |
| 174 if (!object_proxy) { | 174 if (!object_proxy) { |
| 175 error_callback.Run(kUnknownCharacteristicError, ""); | 175 error_callback.Run(kUnknownCharacteristicError, ""); |
| 176 return; | 176 return; |
| 177 } | 177 } |
| 178 | 178 |
| 179 dbus::MethodCall method_call( | 179 dbus::MethodCall method_call( |
| 180 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 180 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
| 181 bluetooth_gatt_characteristic::kStopNotify); | 181 bluetooth_gatt_characteristic::kStopNotify); |
| 182 | 182 |
| 183 object_proxy->CallMethodWithErrorCallback( | 183 object_proxy->CallMethodWithErrorCallback( |
| 184 &method_call, | 184 &method_call, |
| 185 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 185 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 186 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, | 186 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, |
| 187 weak_ptr_factory_.GetWeakPtr(), | 187 weak_ptr_factory_.GetWeakPtr(), |
| 188 callback), | 188 callback), |
| 189 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, | 189 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, |
| 190 weak_ptr_factory_.GetWeakPtr(), | 190 weak_ptr_factory_.GetWeakPtr(), |
| 191 error_callback)); | 191 error_callback)); |
| 192 } | 192 } |
| 193 | 193 |
| 194 // dbus::ObjectManager::Interface override. | 194 // dbus::ObjectManager::Interface override. |
| 195 virtual dbus::PropertySet* CreateProperties( | 195 virtual dbus::PropertySet* CreateProperties( |
| 196 dbus::ObjectProxy *object_proxy, | 196 dbus::ObjectProxy *object_proxy, |
| 197 const dbus::ObjectPath& object_path, | 197 const dbus::ObjectPath& object_path, |
| 198 const std::string& interface_name) OVERRIDE { | 198 const std::string& interface_name) override { |
| 199 Properties* properties = new Properties( | 199 Properties* properties = new Properties( |
| 200 object_proxy, | 200 object_proxy, |
| 201 interface_name, | 201 interface_name, |
| 202 base::Bind(&BluetoothGattCharacteristicClientImpl::OnPropertyChanged, | 202 base::Bind(&BluetoothGattCharacteristicClientImpl::OnPropertyChanged, |
| 203 weak_ptr_factory_.GetWeakPtr(), | 203 weak_ptr_factory_.GetWeakPtr(), |
| 204 object_path)); | 204 object_path)); |
| 205 return static_cast<dbus::PropertySet*>(properties); | 205 return static_cast<dbus::PropertySet*>(properties); |
| 206 } | 206 } |
| 207 | 207 |
| 208 // dbus::ObjectManager::Interface override. | 208 // dbus::ObjectManager::Interface override. |
| 209 virtual void ObjectAdded(const dbus::ObjectPath& object_path, | 209 virtual void ObjectAdded(const dbus::ObjectPath& object_path, |
| 210 const std::string& interface_name) OVERRIDE { | 210 const std::string& interface_name) override { |
| 211 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); | 211 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); |
| 212 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 212 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
| 213 GattCharacteristicAdded(object_path)); | 213 GattCharacteristicAdded(object_path)); |
| 214 | 214 |
| 215 // Connect the "ValueUpdated" signal. | 215 // Connect the "ValueUpdated" signal. |
| 216 dbus::ObjectProxy* object_proxy = | 216 dbus::ObjectProxy* object_proxy = |
| 217 object_manager_->GetObjectProxy(object_path); | 217 object_manager_->GetObjectProxy(object_path); |
| 218 DCHECK(object_proxy); | 218 DCHECK(object_proxy); |
| 219 | 219 |
| 220 object_proxy->ConnectToSignal( | 220 object_proxy->ConnectToSignal( |
| 221 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 221 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
| 222 bluetooth_gatt_characteristic::kValueUpdatedSignal, | 222 bluetooth_gatt_characteristic::kValueUpdatedSignal, |
| 223 base::Bind(&BluetoothGattCharacteristicClientImpl::ValueUpdatedReceived, | 223 base::Bind(&BluetoothGattCharacteristicClientImpl::ValueUpdatedReceived, |
| 224 weak_ptr_factory_.GetWeakPtr(), | 224 weak_ptr_factory_.GetWeakPtr(), |
| 225 object_path), | 225 object_path), |
| 226 base::Bind( | 226 base::Bind( |
| 227 &BluetoothGattCharacteristicClientImpl::ValueUpdatedConnected, | 227 &BluetoothGattCharacteristicClientImpl::ValueUpdatedConnected, |
| 228 weak_ptr_factory_.GetWeakPtr())); | 228 weak_ptr_factory_.GetWeakPtr())); |
| 229 } | 229 } |
| 230 | 230 |
| 231 // dbus::ObjectManager::Interface override. | 231 // dbus::ObjectManager::Interface override. |
| 232 virtual void ObjectRemoved(const dbus::ObjectPath& object_path, | 232 virtual void ObjectRemoved(const dbus::ObjectPath& object_path, |
| 233 const std::string& interface_name) OVERRIDE { | 233 const std::string& interface_name) override { |
| 234 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value(); | 234 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value(); |
| 235 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 235 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
| 236 GattCharacteristicRemoved(object_path)); | 236 GattCharacteristicRemoved(object_path)); |
| 237 } | 237 } |
| 238 | 238 |
| 239 protected: | 239 protected: |
| 240 // chromeos::DBusClient override. | 240 // chromeos::DBusClient override. |
| 241 virtual void Init(dbus::Bus* bus) OVERRIDE { | 241 virtual void Init(dbus::Bus* bus) override { |
| 242 object_manager_ = bus->GetObjectManager( | 242 object_manager_ = bus->GetObjectManager( |
| 243 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | 243 bluetooth_object_manager::kBluetoothObjectManagerServiceName, |
| 244 dbus::ObjectPath( | 244 dbus::ObjectPath( |
| 245 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | 245 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); |
| 246 object_manager_->RegisterInterface( | 246 object_manager_->RegisterInterface( |
| 247 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 247 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
| 248 this); | 248 this); |
| 249 } | 249 } |
| 250 | 250 |
| 251 private: | 251 private: |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 | 353 |
| 354 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() { | 354 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() { |
| 355 } | 355 } |
| 356 | 356 |
| 357 // static | 357 // static |
| 358 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { | 358 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { |
| 359 return new BluetoothGattCharacteristicClientImpl(); | 359 return new BluetoothGattCharacteristicClientImpl(); |
| 360 } | 360 } |
| 361 | 361 |
| 362 } // namespace chromeos | 362 } // namespace chromeos |
| OLD | NEW |