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 "device/bluetooth/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 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 BluetoothGattCharacteristicClient::kNoResponseError[] = | 24 const char BluetoothGattCharacteristicClient::kNoResponseError[] = |
(...skipping 10 matching lines...) Expand all Loading... |
35 RegisterProperty(bluetooth_gatt_characteristic::kUUIDProperty, &uuid); | 35 RegisterProperty(bluetooth_gatt_characteristic::kUUIDProperty, &uuid); |
36 RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty, &service); | 36 RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty, &service); |
37 RegisterProperty(kValueProperty, &value); | 37 RegisterProperty(kValueProperty, &value); |
38 RegisterProperty(bluetooth_gatt_characteristic::kNotifyingProperty, | 38 RegisterProperty(bluetooth_gatt_characteristic::kNotifyingProperty, |
39 ¬ifying); | 39 ¬ifying); |
40 RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty, &flags); | 40 RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty, &flags); |
41 RegisterProperty(bluetooth_gatt_characteristic::kDescriptorsProperty, | 41 RegisterProperty(bluetooth_gatt_characteristic::kDescriptorsProperty, |
42 &descriptors); | 42 &descriptors); |
43 } | 43 } |
44 | 44 |
45 BluetoothGattCharacteristicClient::Properties::~Properties() { | 45 BluetoothGattCharacteristicClient::Properties::~Properties() {} |
46 } | |
47 | 46 |
48 // The BluetoothGattCharacteristicClient implementation used in production. | 47 // The BluetoothGattCharacteristicClient implementation used in production. |
49 class BluetoothGattCharacteristicClientImpl | 48 class BluetoothGattCharacteristicClientImpl |
50 : public BluetoothGattCharacteristicClient, | 49 : public BluetoothGattCharacteristicClient, |
51 public dbus::ObjectManager::Interface { | 50 public dbus::ObjectManager::Interface { |
52 public: | 51 public: |
53 BluetoothGattCharacteristicClientImpl() | 52 BluetoothGattCharacteristicClientImpl() |
54 : object_manager_(NULL), | 53 : object_manager_(NULL), weak_ptr_factory_(this) {} |
55 weak_ptr_factory_(this) { | |
56 } | |
57 | 54 |
58 ~BluetoothGattCharacteristicClientImpl() override { | 55 ~BluetoothGattCharacteristicClientImpl() override { |
59 object_manager_->UnregisterInterface( | 56 object_manager_->UnregisterInterface( |
60 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); | 57 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); |
61 } | 58 } |
62 | 59 |
63 // BluetoothGattCharacteristicClient override. | 60 // BluetoothGattCharacteristicClient override. |
64 void AddObserver( | 61 void AddObserver( |
65 BluetoothGattCharacteristicClient::Observer* observer) override { | 62 BluetoothGattCharacteristicClient::Observer* observer) override { |
66 DCHECK(observer); | 63 DCHECK(observer); |
(...skipping 10 matching lines...) Expand all Loading... |
77 // BluetoothGattCharacteristicClient override. | 74 // BluetoothGattCharacteristicClient override. |
78 std::vector<dbus::ObjectPath> GetCharacteristics() override { | 75 std::vector<dbus::ObjectPath> GetCharacteristics() override { |
79 DCHECK(object_manager_); | 76 DCHECK(object_manager_); |
80 return object_manager_->GetObjectsWithInterface( | 77 return object_manager_->GetObjectsWithInterface( |
81 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); | 78 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); |
82 } | 79 } |
83 | 80 |
84 // BluetoothGattCharacteristicClient override. | 81 // BluetoothGattCharacteristicClient override. |
85 Properties* GetProperties(const dbus::ObjectPath& object_path) override { | 82 Properties* GetProperties(const dbus::ObjectPath& object_path) override { |
86 DCHECK(object_manager_); | 83 DCHECK(object_manager_); |
87 return static_cast<Properties*>( | 84 return static_cast<Properties*>(object_manager_->GetProperties( |
88 object_manager_->GetProperties( | 85 object_path, |
89 object_path, | 86 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface)); |
90 bluetooth_gatt_characteristic:: | |
91 kBluetoothGattCharacteristicInterface)); | |
92 } | 87 } |
93 | 88 |
94 // BluetoothGattCharacteristicClient override. | 89 // BluetoothGattCharacteristicClient override. |
95 void ReadValue(const dbus::ObjectPath& object_path, | 90 void ReadValue(const dbus::ObjectPath& object_path, |
96 const ValueCallback& callback, | 91 const ValueCallback& callback, |
97 const ErrorCallback& error_callback) override { | 92 const ErrorCallback& error_callback) override { |
98 dbus::ObjectProxy* object_proxy = | 93 dbus::ObjectProxy* object_proxy = |
99 object_manager_->GetObjectProxy(object_path); | 94 object_manager_->GetObjectProxy(object_path); |
100 if (!object_proxy) { | 95 if (!object_proxy) { |
101 error_callback.Run(kUnknownCharacteristicError, ""); | 96 error_callback.Run(kUnknownCharacteristicError, ""); |
102 return; | 97 return; |
103 } | 98 } |
104 | 99 |
105 dbus::MethodCall method_call( | 100 dbus::MethodCall method_call( |
106 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 101 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
107 bluetooth_gatt_characteristic::kReadValue); | 102 bluetooth_gatt_characteristic::kReadValue); |
108 | 103 |
109 object_proxy->CallMethodWithErrorCallback( | 104 object_proxy->CallMethodWithErrorCallback( |
110 &method_call, | 105 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
111 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
112 base::Bind(&BluetoothGattCharacteristicClientImpl::OnValueSuccess, | 106 base::Bind(&BluetoothGattCharacteristicClientImpl::OnValueSuccess, |
113 weak_ptr_factory_.GetWeakPtr(), | 107 weak_ptr_factory_.GetWeakPtr(), callback), |
114 callback), | |
115 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, | 108 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, |
116 weak_ptr_factory_.GetWeakPtr(), | 109 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
117 error_callback)); | |
118 } | 110 } |
119 | 111 |
120 // BluetoothGattCharacteristicClient override. | 112 // BluetoothGattCharacteristicClient override. |
121 void WriteValue(const dbus::ObjectPath& object_path, | 113 void WriteValue(const dbus::ObjectPath& object_path, |
122 const std::vector<uint8>& value, | 114 const std::vector<uint8>& value, |
123 const base::Closure& callback, | 115 const base::Closure& callback, |
124 const ErrorCallback& error_callback) override { | 116 const ErrorCallback& error_callback) override { |
125 dbus::ObjectProxy* object_proxy = | 117 dbus::ObjectProxy* object_proxy = |
126 object_manager_->GetObjectProxy(object_path); | 118 object_manager_->GetObjectProxy(object_path); |
127 if (!object_proxy) { | 119 if (!object_proxy) { |
128 error_callback.Run(kUnknownCharacteristicError, ""); | 120 error_callback.Run(kUnknownCharacteristicError, ""); |
129 return; | 121 return; |
130 } | 122 } |
131 | 123 |
132 dbus::MethodCall method_call( | 124 dbus::MethodCall method_call( |
133 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 125 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
134 bluetooth_gatt_characteristic::kWriteValue); | 126 bluetooth_gatt_characteristic::kWriteValue); |
135 dbus::MessageWriter writer(&method_call); | 127 dbus::MessageWriter writer(&method_call); |
136 writer.AppendArrayOfBytes(value.data(), value.size()); | 128 writer.AppendArrayOfBytes(value.data(), value.size()); |
137 | 129 |
138 object_proxy->CallMethodWithErrorCallback( | 130 object_proxy->CallMethodWithErrorCallback( |
139 &method_call, | 131 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
140 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
141 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, | 132 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, |
142 weak_ptr_factory_.GetWeakPtr(), | 133 weak_ptr_factory_.GetWeakPtr(), callback), |
143 callback), | |
144 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, | 134 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, |
145 weak_ptr_factory_.GetWeakPtr(), | 135 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
146 error_callback)); | |
147 } | 136 } |
148 | 137 |
149 // BluetoothGattCharacteristicClient override. | 138 // BluetoothGattCharacteristicClient override. |
150 void StartNotify(const dbus::ObjectPath& object_path, | 139 void StartNotify(const dbus::ObjectPath& object_path, |
151 const base::Closure& callback, | 140 const base::Closure& callback, |
152 const ErrorCallback& error_callback) override { | 141 const ErrorCallback& error_callback) override { |
153 dbus::ObjectProxy* object_proxy = | 142 dbus::ObjectProxy* object_proxy = |
154 object_manager_->GetObjectProxy(object_path); | 143 object_manager_->GetObjectProxy(object_path); |
155 if (!object_proxy) { | 144 if (!object_proxy) { |
156 error_callback.Run(kUnknownCharacteristicError, ""); | 145 error_callback.Run(kUnknownCharacteristicError, ""); |
157 return; | 146 return; |
158 } | 147 } |
159 | 148 |
160 dbus::MethodCall method_call( | 149 dbus::MethodCall method_call( |
161 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 150 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
162 bluetooth_gatt_characteristic::kStartNotify); | 151 bluetooth_gatt_characteristic::kStartNotify); |
163 | 152 |
164 object_proxy->CallMethodWithErrorCallback( | 153 object_proxy->CallMethodWithErrorCallback( |
165 &method_call, | 154 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
166 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
167 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, | 155 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, |
168 weak_ptr_factory_.GetWeakPtr(), | 156 weak_ptr_factory_.GetWeakPtr(), callback), |
169 callback), | |
170 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, | 157 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, |
171 weak_ptr_factory_.GetWeakPtr(), | 158 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
172 error_callback)); | |
173 } | 159 } |
174 | 160 |
175 // BluetoothGattCharacteristicClient override. | 161 // BluetoothGattCharacteristicClient override. |
176 void StopNotify(const dbus::ObjectPath& object_path, | 162 void StopNotify(const dbus::ObjectPath& object_path, |
177 const base::Closure& callback, | 163 const base::Closure& callback, |
178 const ErrorCallback& error_callback) override { | 164 const ErrorCallback& error_callback) override { |
179 dbus::ObjectProxy* object_proxy = | 165 dbus::ObjectProxy* object_proxy = |
180 object_manager_->GetObjectProxy(object_path); | 166 object_manager_->GetObjectProxy(object_path); |
181 if (!object_proxy) { | 167 if (!object_proxy) { |
182 error_callback.Run(kUnknownCharacteristicError, ""); | 168 error_callback.Run(kUnknownCharacteristicError, ""); |
183 return; | 169 return; |
184 } | 170 } |
185 | 171 |
186 dbus::MethodCall method_call( | 172 dbus::MethodCall method_call( |
187 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | 173 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
188 bluetooth_gatt_characteristic::kStopNotify); | 174 bluetooth_gatt_characteristic::kStopNotify); |
189 | 175 |
190 object_proxy->CallMethodWithErrorCallback( | 176 object_proxy->CallMethodWithErrorCallback( |
191 &method_call, | 177 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
192 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
193 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, | 178 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess, |
194 weak_ptr_factory_.GetWeakPtr(), | 179 weak_ptr_factory_.GetWeakPtr(), callback), |
195 callback), | |
196 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, | 180 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError, |
197 weak_ptr_factory_.GetWeakPtr(), | 181 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
198 error_callback)); | |
199 } | 182 } |
200 | 183 |
201 // dbus::ObjectManager::Interface override. | 184 // dbus::ObjectManager::Interface override. |
202 dbus::PropertySet* CreateProperties( | 185 dbus::PropertySet* CreateProperties( |
203 dbus::ObjectProxy* object_proxy, | 186 dbus::ObjectProxy* object_proxy, |
204 const dbus::ObjectPath& object_path, | 187 const dbus::ObjectPath& object_path, |
205 const std::string& interface_name) override { | 188 const std::string& interface_name) override { |
206 Properties* properties = new Properties( | 189 Properties* properties = new Properties( |
207 object_proxy, | 190 object_proxy, interface_name, |
208 interface_name, | |
209 base::Bind(&BluetoothGattCharacteristicClientImpl::OnPropertyChanged, | 191 base::Bind(&BluetoothGattCharacteristicClientImpl::OnPropertyChanged, |
210 weak_ptr_factory_.GetWeakPtr(), | 192 weak_ptr_factory_.GetWeakPtr(), object_path)); |
211 object_path)); | |
212 return static_cast<dbus::PropertySet*>(properties); | 193 return static_cast<dbus::PropertySet*>(properties); |
213 } | 194 } |
214 | 195 |
215 // dbus::ObjectManager::Interface override. | 196 // dbus::ObjectManager::Interface override. |
216 void ObjectAdded(const dbus::ObjectPath& object_path, | 197 void ObjectAdded(const dbus::ObjectPath& object_path, |
217 const std::string& interface_name) override { | 198 const std::string& interface_name) override { |
218 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); | 199 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); |
219 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 200 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, |
220 GattCharacteristicAdded(object_path)); | 201 GattCharacteristicAdded(object_path)); |
221 } | 202 } |
(...skipping 19 matching lines...) Expand all Loading... |
241 } | 222 } |
242 | 223 |
243 private: | 224 private: |
244 // Called by dbus::PropertySet when a property value is changed, either by | 225 // Called by dbus::PropertySet when a property value is changed, either by |
245 // result of a signal or response to a GetAll() or Get() call. Informs | 226 // result of a signal or response to a GetAll() or Get() call. Informs |
246 // observers. | 227 // observers. |
247 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, | 228 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, |
248 const std::string& property_name) { | 229 const std::string& property_name) { |
249 VLOG(2) << "Remote GATT characteristic property changed: " | 230 VLOG(2) << "Remote GATT characteristic property changed: " |
250 << object_path.value() << ": " << property_name; | 231 << object_path.value() << ": " << property_name; |
251 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, | 232 FOR_EACH_OBSERVER( |
252 GattCharacteristicPropertyChanged(object_path, | 233 BluetoothGattCharacteristicClient::Observer, observers_, |
253 property_name)); | 234 GattCharacteristicPropertyChanged(object_path, property_name)); |
254 } | 235 } |
255 | 236 |
256 // Called when a response for successful method call is received. | 237 // Called when a response for successful method call is received. |
257 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | 238 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
258 DCHECK(response); | 239 DCHECK(response); |
259 callback.Run(); | 240 callback.Run(); |
260 } | 241 } |
261 | 242 |
262 // Called when a characteristic value response for a successful method call | 243 // Called when a characteristic value response for a successful method call |
263 // is received. | 244 // is received. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 279 |
299 dbus::ObjectManager* object_manager_; | 280 dbus::ObjectManager* object_manager_; |
300 | 281 |
301 // List of observers interested in event notifications from us. | 282 // List of observers interested in event notifications from us. |
302 base::ObserverList<BluetoothGattCharacteristicClient::Observer> observers_; | 283 base::ObserverList<BluetoothGattCharacteristicClient::Observer> observers_; |
303 | 284 |
304 // Weak pointer factory for generating 'this' pointers that might live longer | 285 // Weak pointer factory for generating 'this' pointers that might live longer |
305 // than we do. | 286 // than we do. |
306 // Note: This should remain the last member so it'll be destroyed and | 287 // Note: This should remain the last member so it'll be destroyed and |
307 // invalidate its weak pointers before any other members are destroyed. | 288 // invalidate its weak pointers before any other members are destroyed. |
308 base::WeakPtrFactory<BluetoothGattCharacteristicClientImpl> | 289 base::WeakPtrFactory<BluetoothGattCharacteristicClientImpl> weak_ptr_factory_; |
309 weak_ptr_factory_; | |
310 | 290 |
311 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicClientImpl); | 291 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicClientImpl); |
312 }; | 292 }; |
313 | 293 |
314 BluetoothGattCharacteristicClient::BluetoothGattCharacteristicClient() { | 294 BluetoothGattCharacteristicClient::BluetoothGattCharacteristicClient() {} |
315 } | |
316 | 295 |
317 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() { | 296 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() {} |
318 } | |
319 | 297 |
320 // static | 298 // static |
321 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { | 299 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { |
322 return new BluetoothGattCharacteristicClientImpl(); | 300 return new BluetoothGattCharacteristicClientImpl(); |
323 } | 301 } |
324 | 302 |
325 } // namespace chromeos | 303 } // namespace bluez |
OLD | NEW |