Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: chromeos/dbus/bluetooth_gatt_characteristic_client.cc

Issue 301093003: device/bluetooth: Update characteristic value D-Bus bindings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed comments. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 these definitions to service_constants.h
19 const char kReadValue[] = "ReadValue";
20 const char kWriteValue[] = "WriteValue";
21 const char kValueUpdatedSignal[] = "ValueUpdated";
22
keybuk 2014/05/30 22:22:40 There doesn't seem to be any reason NOT to do this
armansito 2014/05/30 22:37:20 I guess... At first I wanted to wait until I had t
armansito 2014/06/05 06:14:22 Done.
23 } // namespace
24
25 // static
26 const char BluetoothGattCharacteristicClient::kNoResponseError[] =
27 "org.chromium.Error.NoResponse";
28 // static
29 const char BluetoothGattCharacteristicClient::kUnknownCharacteristicError[] =
30 "org.chromium.Error.UnknownCharacteristic";
31
16 BluetoothGattCharacteristicClient::Properties::Properties( 32 BluetoothGattCharacteristicClient::Properties::Properties(
17 dbus::ObjectProxy* object_proxy, 33 dbus::ObjectProxy* object_proxy,
18 const std::string& interface_name, 34 const std::string& interface_name,
19 const PropertyChangedCallback& callback) 35 const PropertyChangedCallback& callback)
20 : dbus::PropertySet(object_proxy, interface_name, callback) { 36 : dbus::PropertySet(object_proxy, interface_name, callback) {
21 RegisterProperty(bluetooth_gatt_characteristic::kUUIDProperty, &uuid); 37 RegisterProperty(bluetooth_gatt_characteristic::kUUIDProperty, &uuid);
22 RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty, &service); 38 RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty, &service);
23 RegisterProperty(bluetooth_gatt_characteristic::kValueProperty, &value);
24 RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty, &flags); 39 RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty, &flags);
25 } 40 }
26 41
27 BluetoothGattCharacteristicClient::Properties::~Properties() { 42 BluetoothGattCharacteristicClient::Properties::~Properties() {
28 } 43 }
29 44
30 // The BluetoothGattCharacteristicClient implementation used in production. 45 // The BluetoothGattCharacteristicClient implementation used in production.
31 class BluetoothGattCharacteristicClientImpl 46 class BluetoothGattCharacteristicClientImpl
32 : public BluetoothGattCharacteristicClient, 47 : public BluetoothGattCharacteristicClient,
33 public dbus::ObjectManager::Interface { 48 public dbus::ObjectManager::Interface {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 virtual Properties* GetProperties( 82 virtual Properties* GetProperties(
68 const dbus::ObjectPath& object_path) OVERRIDE { 83 const dbus::ObjectPath& object_path) OVERRIDE {
69 DCHECK(object_manager_); 84 DCHECK(object_manager_);
70 return static_cast<Properties*>( 85 return static_cast<Properties*>(
71 object_manager_->GetProperties( 86 object_manager_->GetProperties(
72 object_path, 87 object_path,
73 bluetooth_gatt_characteristic:: 88 bluetooth_gatt_characteristic::
74 kBluetoothGattCharacteristicInterface)); 89 kBluetoothGattCharacteristicInterface));
75 } 90 }
76 91
92 // BluetoothGattCharacteristicClient override.
93 virtual void ReadValue(const dbus::ObjectPath& object_path,
94 const ValueCallback& callback,
95 const ErrorCallback& error_callback) OVERRIDE {
96 dbus::ObjectProxy* object_proxy =
97 object_manager_->GetObjectProxy(object_path);
98 if (!object_proxy) {
99 error_callback.Run(kUnknownCharacteristicError, "");
100 return;
101 }
102
103 dbus::MethodCall method_call(
104 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
105 kReadValue);
106
107 object_proxy->CallMethodWithErrorCallback(
108 &method_call,
109 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
110 base::Bind(&BluetoothGattCharacteristicClientImpl::OnValueSuccess,
111 weak_ptr_factory_.GetWeakPtr(),
112 callback),
113 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError,
114 weak_ptr_factory_.GetWeakPtr(),
115 error_callback));
116 }
117
118 // BluetoothGattCharacteristicClient override.
119 virtual void WriteValue(const dbus::ObjectPath& object_path,
120 const std::vector<uint8>& value,
121 const base::Closure& callback,
122 const ErrorCallback& error_callback) OVERRIDE {
123 dbus::ObjectProxy* object_proxy =
124 object_manager_->GetObjectProxy(object_path);
125 if (!object_proxy) {
126 error_callback.Run(kUnknownCharacteristicError, "");
127 return;
128 }
129
130 dbus::MethodCall method_call(
131 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
132 kWriteValue);
133 dbus::MessageWriter writer(&method_call);
134 writer.AppendArrayOfBytes(value.data(), value.size());
135
136 object_proxy->CallMethodWithErrorCallback(
137 &method_call,
138 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
139 base::Bind(&BluetoothGattCharacteristicClientImpl::OnSuccess,
140 weak_ptr_factory_.GetWeakPtr(),
141 callback),
142 base::Bind(&BluetoothGattCharacteristicClientImpl::OnError,
143 weak_ptr_factory_.GetWeakPtr(),
144 error_callback));
145 }
146
77 // dbus::ObjectManager::Interface override. 147 // dbus::ObjectManager::Interface override.
78 virtual dbus::PropertySet* CreateProperties( 148 virtual dbus::PropertySet* CreateProperties(
79 dbus::ObjectProxy *object_proxy, 149 dbus::ObjectProxy *object_proxy,
80 const dbus::ObjectPath& object_path, 150 const dbus::ObjectPath& object_path,
81 const std::string& interface_name) OVERRIDE { 151 const std::string& interface_name) OVERRIDE {
82 Properties* properties = new Properties( 152 Properties* properties = new Properties(
83 object_proxy, 153 object_proxy,
84 interface_name, 154 interface_name,
85 base::Bind(&BluetoothGattCharacteristicClientImpl::OnPropertyChanged, 155 base::Bind(&BluetoothGattCharacteristicClientImpl::OnPropertyChanged,
86 weak_ptr_factory_.GetWeakPtr(), 156 weak_ptr_factory_.GetWeakPtr(),
87 object_path)); 157 object_path));
88 return static_cast<dbus::PropertySet*>(properties); 158 return static_cast<dbus::PropertySet*>(properties);
89 } 159 }
90 160
91 // dbus::ObjectManager::Interface override. 161 // dbus::ObjectManager::Interface override.
92 virtual void ObjectAdded(const dbus::ObjectPath& object_path, 162 virtual void ObjectAdded(const dbus::ObjectPath& object_path,
93 const std::string& interface_name) OVERRIDE { 163 const std::string& interface_name) OVERRIDE {
94 VLOG(2) << "Remote GATT characteristic added: " << object_path.value(); 164 VLOG(2) << "Remote GATT characteristic added: " << object_path.value();
95 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, 165 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_,
96 GattCharacteristicAdded(object_path)); 166 GattCharacteristicAdded(object_path));
167
168 // Connect the "ValueUpdated" signal.
169 dbus::ObjectProxy* object_proxy =
170 object_manager_->GetObjectProxy(object_path);
171 DCHECK(object_proxy);
172
173 object_proxy->ConnectToSignal(
174 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
175 kValueUpdatedSignal,
176 base::Bind(&BluetoothGattCharacteristicClientImpl::ValueUpdatedReceived,
177 weak_ptr_factory_.GetWeakPtr(),
178 object_path),
179 base::Bind(
180 &BluetoothGattCharacteristicClientImpl::ValueUpdatedConnected,
181 weak_ptr_factory_.GetWeakPtr()));
97 } 182 }
98 183
99 // dbus::ObjectManager::Interface override. 184 // dbus::ObjectManager::Interface override.
100 virtual void ObjectRemoved(const dbus::ObjectPath& object_path, 185 virtual void ObjectRemoved(const dbus::ObjectPath& object_path,
101 const std::string& interface_name) OVERRIDE { 186 const std::string& interface_name) OVERRIDE {
102 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value(); 187 VLOG(2) << "Remote GATT characteristic removed: " << object_path.value();
103 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, 188 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_,
104 GattCharacteristicRemoved(object_path)); 189 GattCharacteristicRemoved(object_path));
105 } 190 }
106 191
(...skipping 15 matching lines...) Expand all
122 // observers. 207 // observers.
123 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path, 208 virtual void OnPropertyChanged(const dbus::ObjectPath& object_path,
124 const std::string& property_name) { 209 const std::string& property_name) {
125 VLOG(2) << "Remote GATT characteristic property changed: " 210 VLOG(2) << "Remote GATT characteristic property changed: "
126 << object_path.value() << ": " << property_name; 211 << object_path.value() << ": " << property_name;
127 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_, 212 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer, observers_,
128 GattCharacteristicPropertyChanged(object_path, 213 GattCharacteristicPropertyChanged(object_path,
129 property_name)); 214 property_name));
130 } 215 }
131 216
217 // Called by dbus:: when a "ValueUpdated" signal is received.
218 void ValueUpdatedReceived(const dbus::ObjectPath& object_path,
219 dbus::Signal* signal) {
220 DCHECK(signal);
221 const uint8* bytes = NULL;
222 size_t length = 0;
223 dbus::MessageReader reader(signal);
224 if (!reader.PopArrayOfBytes(&bytes, &length)) {
225 LOG(WARNING) << "ValueUpdated signal has incorrect parameters: "
226 << signal->ToString();
227 return;
228 }
229
230 std::vector<uint8> value;
231 if (bytes)
232 value.assign(bytes, bytes + length);
233
234 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer,
235 observers_,
236 GattCharacteristicValueUpdated(object_path, value));
237 }
238
239 // Called by dbus:: when the "ValueUpdated" signal is initially connected.
240 void ValueUpdatedConnected(const std::string& interface_name,
241 const std::string& signal_name,
242 bool success) {
243 LOG_IF(WARNING, !success) << "Failed to connect to the ValueUpdated signal";
244 }
245
246 // Called when a response for successful method call is received.
247 void OnSuccess(const base::Closure& callback, dbus::Response* response) {
248 DCHECK(response);
249 callback.Run();
250 }
251
252 // Called when a characteristic value response for a successful method call
253 // is received.
254 void OnValueSuccess(const ValueCallback& callback, dbus::Response* response) {
255 DCHECK(response);
256 dbus::MessageReader reader(response);
257
258 const uint8* bytes = NULL;
259 size_t length = 0;
260
261 if (!reader.PopArrayOfBytes(&bytes, &length))
262 VLOG(2) << "Error reading array of bytes in ValueCallback";
263
264 std::vector<uint8> value;
265
266 if (bytes)
267 value.assign(bytes, bytes + length);
268
269 callback.Run(value);
270 }
271
272 // Called when a response for a failed method call is received.
273 void OnError(const ErrorCallback& error_callback,
274 dbus::ErrorResponse* response) {
275 // Error response has optional error message argument.
276 std::string error_name;
277 std::string error_message;
278 if (response) {
279 dbus::MessageReader reader(response);
280 error_name = response->GetErrorName();
281 reader.PopString(&error_message);
282 } else {
283 error_name = kNoResponseError;
284 error_message = "";
285 }
286 error_callback.Run(error_name, error_message);
287 }
288
132 dbus::ObjectManager* object_manager_; 289 dbus::ObjectManager* object_manager_;
133 290
134 // List of observers interested in event notifications from us. 291 // List of observers interested in event notifications from us.
135 ObserverList<BluetoothGattCharacteristicClient::Observer> observers_; 292 ObserverList<BluetoothGattCharacteristicClient::Observer> observers_;
136 293
137 // Weak pointer factory for generating 'this' pointers that might live longer 294 // Weak pointer factory for generating 'this' pointers that might live longer
138 // than we do. 295 // than we do.
139 // Note: This should remain the last member so it'll be destroyed and 296 // Note: This should remain the last member so it'll be destroyed and
140 // invalidate its weak pointers before any other members are destroyed. 297 // invalidate its weak pointers before any other members are destroyed.
141 base::WeakPtrFactory<BluetoothGattCharacteristicClientImpl> 298 base::WeakPtrFactory<BluetoothGattCharacteristicClientImpl>
142 weak_ptr_factory_; 299 weak_ptr_factory_;
143 300
144 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicClientImpl); 301 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicClientImpl);
145 }; 302 };
146 303
147 BluetoothGattCharacteristicClient::BluetoothGattCharacteristicClient() { 304 BluetoothGattCharacteristicClient::BluetoothGattCharacteristicClient() {
148 } 305 }
149 306
150 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() { 307 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() {
151 } 308 }
152 309
153 // static 310 // static
154 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() { 311 BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() {
155 return new BluetoothGattCharacteristicClientImpl(); 312 return new BluetoothGattCharacteristicClientImpl();
156 } 313 }
157 314
158 } // namespace chromeos 315 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698