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

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.cc

Issue 1415573014: Reland "Add Linux support for the Bluetooth API" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: build fix. Created 5 years, 1 month 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
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 "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "device/bluetooth/bluetooth_adapter_chromeos.h" 11 #include "device/bluetooth/bluetooth_adapter_bluez.h"
12 #include "device/bluetooth/bluetooth_device.h" 12 #include "device/bluetooth/bluetooth_device.h"
13 #include "device/bluetooth/bluetooth_gatt_notify_session_chromeos.h" 13 #include "device/bluetooth/bluetooth_gatt_notify_session_bluez.h"
14 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" 14 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.h"
15 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" 15 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_bluez.h"
16 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" 16 #include "device/bluetooth/bluetooth_remote_gatt_service_bluez.h"
17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" 17 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h" 18 #include "third_party/cros_system_api/dbus/service_constants.h"
19 19
20 namespace chromeos { 20 namespace bluez {
21 21
22 namespace { 22 namespace {
23 23
24 // Stream operator for logging vector<uint8>. 24 // Stream operator for logging vector<uint8>.
25 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { 25 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) {
26 out << "["; 26 out << "[";
27 for (std::vector<uint8>::const_iterator iter = bytes.begin(); 27 for (std::vector<uint8>::const_iterator iter = bytes.begin();
28 iter != bytes.end(); ++iter) { 28 iter != bytes.end(); ++iter) {
29 out << base::StringPrintf("%02X", *iter); 29 out << base::StringPrintf("%02X", *iter);
30 } 30 }
31 return out << "]"; 31 return out << "]";
32 } 32 }
33 33
34 } // namespace 34 } // namespace
35 35
36 BluetoothRemoteGattCharacteristicChromeOS:: 36 BluetoothRemoteGattCharacteristicBlueZ::BluetoothRemoteGattCharacteristicBlueZ(
37 BluetoothRemoteGattCharacteristicChromeOS( 37 BluetoothRemoteGattServiceBlueZ* service,
38 BluetoothRemoteGattServiceChromeOS* service, 38 const dbus::ObjectPath& object_path)
39 const dbus::ObjectPath& object_path)
40 : object_path_(object_path), 39 : object_path_(object_path),
41 service_(service), 40 service_(service),
42 num_notify_sessions_(0), 41 num_notify_sessions_(0),
43 notify_call_pending_(false), 42 notify_call_pending_(false),
44 weak_ptr_factory_(this) { 43 weak_ptr_factory_(this) {
45 VLOG(1) << "Creating remote GATT characteristic with identifier: " 44 VLOG(1) << "Creating remote GATT characteristic with identifier: "
46 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); 45 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
47 bluez::BluezDBusManager::Get() 46 bluez::BluezDBusManager::Get()
48 ->GetBluetoothGattDescriptorClient() 47 ->GetBluetoothGattDescriptorClient()
49 ->AddObserver(this); 48 ->AddObserver(this);
50 49
51 // Add all known GATT characteristic descriptors. 50 // Add all known GATT characteristic descriptors.
52 const std::vector<dbus::ObjectPath>& gatt_descs = 51 const std::vector<dbus::ObjectPath>& gatt_descs =
53 bluez::BluezDBusManager::Get() 52 bluez::BluezDBusManager::Get()
54 ->GetBluetoothGattDescriptorClient() 53 ->GetBluetoothGattDescriptorClient()
55 ->GetDescriptors(); 54 ->GetDescriptors();
56 for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_descs.begin(); 55 for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_descs.begin();
57 iter != gatt_descs.end(); ++iter) 56 iter != gatt_descs.end(); ++iter)
58 GattDescriptorAdded(*iter); 57 GattDescriptorAdded(*iter);
59 } 58 }
60 59
61 BluetoothRemoteGattCharacteristicChromeOS:: 60 BluetoothRemoteGattCharacteristicBlueZ::
62 ~BluetoothRemoteGattCharacteristicChromeOS() { 61 ~BluetoothRemoteGattCharacteristicBlueZ() {
63 bluez::BluezDBusManager::Get() 62 bluez::BluezDBusManager::Get()
64 ->GetBluetoothGattDescriptorClient() 63 ->GetBluetoothGattDescriptorClient()
65 ->RemoveObserver(this); 64 ->RemoveObserver(this);
66 65
67 // Clean up all the descriptors. There isn't much point in notifying service 66 // Clean up all the descriptors. There isn't much point in notifying service
68 // observers for each descriptor that gets removed, so just delete them. 67 // observers for each descriptor that gets removed, so just delete them.
69 for (DescriptorMap::iterator iter = descriptors_.begin(); 68 for (DescriptorMap::iterator iter = descriptors_.begin();
70 iter != descriptors_.end(); ++iter) 69 iter != descriptors_.end(); ++iter)
71 delete iter->second; 70 delete iter->second;
72 71
73 // Report an error for all pending calls to StartNotifySession. 72 // Report an error for all pending calls to StartNotifySession.
74 while (!pending_start_notify_calls_.empty()) { 73 while (!pending_start_notify_calls_.empty()) {
75 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); 74 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front();
76 pending_start_notify_calls_.pop(); 75 pending_start_notify_calls_.pop();
77 callbacks.second.Run(device::BluetoothGattService::GATT_ERROR_FAILED); 76 callbacks.second.Run(device::BluetoothGattService::GATT_ERROR_FAILED);
78 } 77 }
79 } 78 }
80 79
81 std::string BluetoothRemoteGattCharacteristicChromeOS::GetIdentifier() const { 80 std::string BluetoothRemoteGattCharacteristicBlueZ::GetIdentifier() const {
82 return object_path_.value(); 81 return object_path_.value();
83 } 82 }
84 83
85 device::BluetoothUUID 84 device::BluetoothUUID BluetoothRemoteGattCharacteristicBlueZ::GetUUID() const {
86 BluetoothRemoteGattCharacteristicChromeOS::GetUUID() const {
87 bluez::BluetoothGattCharacteristicClient::Properties* properties = 85 bluez::BluetoothGattCharacteristicClient::Properties* properties =
88 bluez::BluezDBusManager::Get() 86 bluez::BluezDBusManager::Get()
89 ->GetBluetoothGattCharacteristicClient() 87 ->GetBluetoothGattCharacteristicClient()
90 ->GetProperties(object_path_); 88 ->GetProperties(object_path_);
91 DCHECK(properties); 89 DCHECK(properties);
92 return device::BluetoothUUID(properties->uuid.value()); 90 return device::BluetoothUUID(properties->uuid.value());
93 } 91 }
94 92
95 bool BluetoothRemoteGattCharacteristicChromeOS::IsLocal() const { 93 bool BluetoothRemoteGattCharacteristicBlueZ::IsLocal() const {
96 return false; 94 return false;
97 } 95 }
98 96
99 const std::vector<uint8>& 97 const std::vector<uint8>& BluetoothRemoteGattCharacteristicBlueZ::GetValue()
100 BluetoothRemoteGattCharacteristicChromeOS::GetValue() const { 98 const {
101 bluez::BluetoothGattCharacteristicClient::Properties* properties = 99 bluez::BluetoothGattCharacteristicClient::Properties* properties =
102 bluez::BluezDBusManager::Get() 100 bluez::BluezDBusManager::Get()
103 ->GetBluetoothGattCharacteristicClient() 101 ->GetBluetoothGattCharacteristicClient()
104 ->GetProperties(object_path_); 102 ->GetProperties(object_path_);
105 103
106 DCHECK(properties); 104 DCHECK(properties);
107 105
108 return properties->value.value(); 106 return properties->value.value();
109 } 107 }
110 108
111 device::BluetoothGattService* 109 device::BluetoothGattService*
112 BluetoothRemoteGattCharacteristicChromeOS::GetService() const { 110 BluetoothRemoteGattCharacteristicBlueZ::GetService() const {
113 return service_; 111 return service_;
114 } 112 }
115 113
116 device::BluetoothGattCharacteristic::Properties 114 device::BluetoothGattCharacteristic::Properties
117 BluetoothRemoteGattCharacteristicChromeOS::GetProperties() const { 115 BluetoothRemoteGattCharacteristicBlueZ::GetProperties() const {
118 bluez::BluetoothGattCharacteristicClient::Properties* properties = 116 bluez::BluetoothGattCharacteristicClient::Properties* properties =
119 bluez::BluezDBusManager::Get() 117 bluez::BluezDBusManager::Get()
120 ->GetBluetoothGattCharacteristicClient() 118 ->GetBluetoothGattCharacteristicClient()
121 ->GetProperties(object_path_); 119 ->GetProperties(object_path_);
122 DCHECK(properties); 120 DCHECK(properties);
123 121
124 Properties props = PROPERTY_NONE; 122 Properties props = PROPERTY_NONE;
125 const std::vector<std::string>& flags = properties->flags.value(); 123 const std::vector<std::string>& flags = properties->flags.value();
126 for (std::vector<std::string>::const_iterator iter = flags.begin(); 124 for (std::vector<std::string>::const_iterator iter = flags.begin();
127 iter != flags.end(); 125 iter != flags.end(); ++iter) {
128 ++iter) {
129 if (*iter == bluetooth_gatt_characteristic::kFlagBroadcast) 126 if (*iter == bluetooth_gatt_characteristic::kFlagBroadcast)
130 props |= PROPERTY_BROADCAST; 127 props |= PROPERTY_BROADCAST;
131 if (*iter == bluetooth_gatt_characteristic::kFlagRead) 128 if (*iter == bluetooth_gatt_characteristic::kFlagRead)
132 props |= PROPERTY_READ; 129 props |= PROPERTY_READ;
133 if (*iter == bluetooth_gatt_characteristic::kFlagWriteWithoutResponse) 130 if (*iter == bluetooth_gatt_characteristic::kFlagWriteWithoutResponse)
134 props |= PROPERTY_WRITE_WITHOUT_RESPONSE; 131 props |= PROPERTY_WRITE_WITHOUT_RESPONSE;
135 if (*iter == bluetooth_gatt_characteristic::kFlagWrite) 132 if (*iter == bluetooth_gatt_characteristic::kFlagWrite)
136 props |= PROPERTY_WRITE; 133 props |= PROPERTY_WRITE;
137 if (*iter == bluetooth_gatt_characteristic::kFlagNotify) 134 if (*iter == bluetooth_gatt_characteristic::kFlagNotify)
138 props |= PROPERTY_NOTIFY; 135 props |= PROPERTY_NOTIFY;
139 if (*iter == bluetooth_gatt_characteristic::kFlagIndicate) 136 if (*iter == bluetooth_gatt_characteristic::kFlagIndicate)
140 props |= PROPERTY_INDICATE; 137 props |= PROPERTY_INDICATE;
141 if (*iter == bluetooth_gatt_characteristic::kFlagAuthenticatedSignedWrites) 138 if (*iter == bluetooth_gatt_characteristic::kFlagAuthenticatedSignedWrites)
142 props |= PROPERTY_AUTHENTICATED_SIGNED_WRITES; 139 props |= PROPERTY_AUTHENTICATED_SIGNED_WRITES;
143 if (*iter == bluetooth_gatt_characteristic::kFlagExtendedProperties) 140 if (*iter == bluetooth_gatt_characteristic::kFlagExtendedProperties)
144 props |= PROPERTY_EXTENDED_PROPERTIES; 141 props |= PROPERTY_EXTENDED_PROPERTIES;
145 if (*iter == bluetooth_gatt_characteristic::kFlagReliableWrite) 142 if (*iter == bluetooth_gatt_characteristic::kFlagReliableWrite)
146 props |= PROPERTY_RELIABLE_WRITE; 143 props |= PROPERTY_RELIABLE_WRITE;
147 if (*iter == bluetooth_gatt_characteristic::kFlagWritableAuxiliaries) 144 if (*iter == bluetooth_gatt_characteristic::kFlagWritableAuxiliaries)
148 props |= PROPERTY_WRITABLE_AUXILIARIES; 145 props |= PROPERTY_WRITABLE_AUXILIARIES;
149 } 146 }
150 147
151 return props; 148 return props;
152 } 149 }
153 150
154 device::BluetoothGattCharacteristic::Permissions 151 device::BluetoothGattCharacteristic::Permissions
155 BluetoothRemoteGattCharacteristicChromeOS::GetPermissions() const { 152 BluetoothRemoteGattCharacteristicBlueZ::GetPermissions() const {
156 // TODO(armansito): Once BlueZ defines the permissions, return the correct 153 // TODO(armansito): Once BlueZ defines the permissions, return the correct
157 // values here. 154 // values here.
158 return PERMISSION_NONE; 155 return PERMISSION_NONE;
159 } 156 }
160 157
161 bool BluetoothRemoteGattCharacteristicChromeOS::IsNotifying() const { 158 bool BluetoothRemoteGattCharacteristicBlueZ::IsNotifying() const {
162 bluez::BluetoothGattCharacteristicClient::Properties* properties = 159 bluez::BluetoothGattCharacteristicClient::Properties* properties =
163 bluez::BluezDBusManager::Get() 160 bluez::BluezDBusManager::Get()
164 ->GetBluetoothGattCharacteristicClient() 161 ->GetBluetoothGattCharacteristicClient()
165 ->GetProperties(object_path_); 162 ->GetProperties(object_path_);
166 DCHECK(properties); 163 DCHECK(properties);
167 164
168 return properties->notifying.value(); 165 return properties->notifying.value();
169 } 166 }
170 167
171 std::vector<device::BluetoothGattDescriptor*> 168 std::vector<device::BluetoothGattDescriptor*>
172 BluetoothRemoteGattCharacteristicChromeOS::GetDescriptors() const { 169 BluetoothRemoteGattCharacteristicBlueZ::GetDescriptors() const {
173 std::vector<device::BluetoothGattDescriptor*> descriptors; 170 std::vector<device::BluetoothGattDescriptor*> descriptors;
174 for (DescriptorMap::const_iterator iter = descriptors_.begin(); 171 for (DescriptorMap::const_iterator iter = descriptors_.begin();
175 iter != descriptors_.end(); ++iter) 172 iter != descriptors_.end(); ++iter)
176 descriptors.push_back(iter->second); 173 descriptors.push_back(iter->second);
177 return descriptors; 174 return descriptors;
178 } 175 }
179 176
180 device::BluetoothGattDescriptor* 177 device::BluetoothGattDescriptor*
181 BluetoothRemoteGattCharacteristicChromeOS::GetDescriptor( 178 BluetoothRemoteGattCharacteristicBlueZ::GetDescriptor(
182 const std::string& identifier) const { 179 const std::string& identifier) const {
183 DescriptorMap::const_iterator iter = 180 DescriptorMap::const_iterator iter =
184 descriptors_.find(dbus::ObjectPath(identifier)); 181 descriptors_.find(dbus::ObjectPath(identifier));
185 if (iter == descriptors_.end()) 182 if (iter == descriptors_.end())
186 return NULL; 183 return NULL;
187 return iter->second; 184 return iter->second;
188 } 185 }
189 186
190 bool BluetoothRemoteGattCharacteristicChromeOS::AddDescriptor( 187 bool BluetoothRemoteGattCharacteristicBlueZ::AddDescriptor(
191 device::BluetoothGattDescriptor* descriptor) { 188 device::BluetoothGattDescriptor* descriptor) {
192 VLOG(1) << "Descriptors cannot be added to a remote GATT characteristic."; 189 VLOG(1) << "Descriptors cannot be added to a remote GATT characteristic.";
193 return false; 190 return false;
194 } 191 }
195 192
196 bool BluetoothRemoteGattCharacteristicChromeOS::UpdateValue( 193 bool BluetoothRemoteGattCharacteristicBlueZ::UpdateValue(
197 const std::vector<uint8>& value) { 194 const std::vector<uint8>& value) {
198 VLOG(1) << "Cannot update the value of a remote GATT characteristic."; 195 VLOG(1) << "Cannot update the value of a remote GATT characteristic.";
199 return false; 196 return false;
200 } 197 }
201 198
202 void BluetoothRemoteGattCharacteristicChromeOS::ReadRemoteCharacteristic( 199 void BluetoothRemoteGattCharacteristicBlueZ::ReadRemoteCharacteristic(
203 const ValueCallback& callback, 200 const ValueCallback& callback,
204 const ErrorCallback& error_callback) { 201 const ErrorCallback& error_callback) {
205 VLOG(1) << "Sending GATT characteristic read request to characteristic: " 202 VLOG(1) << "Sending GATT characteristic read request to characteristic: "
206 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() 203 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value()
207 << "."; 204 << ".";
208 205
209 bluez::BluezDBusManager::Get() 206 bluez::BluezDBusManager::Get()
210 ->GetBluetoothGattCharacteristicClient() 207 ->GetBluetoothGattCharacteristicClient()
211 ->ReadValue( 208 ->ReadValue(object_path_, callback,
212 object_path_, callback, 209 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnError,
213 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnError, 210 weak_ptr_factory_.GetWeakPtr(), error_callback));
214 weak_ptr_factory_.GetWeakPtr(), error_callback));
215 } 211 }
216 212
217 void BluetoothRemoteGattCharacteristicChromeOS::WriteRemoteCharacteristic( 213 void BluetoothRemoteGattCharacteristicBlueZ::WriteRemoteCharacteristic(
218 const std::vector<uint8>& new_value, 214 const std::vector<uint8>& new_value,
219 const base::Closure& callback, 215 const base::Closure& callback,
220 const ErrorCallback& error_callback) { 216 const ErrorCallback& error_callback) {
221 VLOG(1) << "Sending GATT characteristic write request to characteristic: " 217 VLOG(1) << "Sending GATT characteristic write request to characteristic: "
222 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value() 218 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value()
223 << ", with value: " << new_value << "."; 219 << ", with value: " << new_value << ".";
224 220
225 bluez::BluezDBusManager::Get() 221 bluez::BluezDBusManager::Get()
226 ->GetBluetoothGattCharacteristicClient() 222 ->GetBluetoothGattCharacteristicClient()
227 ->WriteValue( 223 ->WriteValue(object_path_, new_value, callback,
228 object_path_, new_value, callback, 224 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnError,
229 base::Bind(&BluetoothRemoteGattCharacteristicChromeOS::OnError, 225 weak_ptr_factory_.GetWeakPtr(), error_callback));
230 weak_ptr_factory_.GetWeakPtr(), error_callback));
231 } 226 }
232 227
233 void BluetoothRemoteGattCharacteristicChromeOS::StartNotifySession( 228 void BluetoothRemoteGattCharacteristicBlueZ::StartNotifySession(
234 const NotifySessionCallback& callback, 229 const NotifySessionCallback& callback,
235 const ErrorCallback& error_callback) { 230 const ErrorCallback& error_callback) {
236 VLOG(1) << __func__; 231 VLOG(1) << __func__;
237 232
238 if (num_notify_sessions_ > 0) { 233 if (num_notify_sessions_ > 0) {
239 // The characteristic might have stopped notifying even though the session 234 // The characteristic might have stopped notifying even though the session
240 // count is nonzero. This means that notifications stopped outside of our 235 // count is nonzero. This means that notifications stopped outside of our
241 // control and we should reset the count. If the characteristic is still 236 // control and we should reset the count. If the characteristic is still
242 // notifying, then return success. Otherwise, reset the count and treat 237 // notifying, then return success. Otherwise, reset the count and treat
243 // this call as if the count were 0. 238 // this call as if the count were 0.
244 if (IsNotifying()) { 239 if (IsNotifying()) {
245 // Check for overflows, though unlikely. 240 // Check for overflows, though unlikely.
246 if (num_notify_sessions_ == std::numeric_limits<size_t>::max()) { 241 if (num_notify_sessions_ == std::numeric_limits<size_t>::max()) {
247 error_callback.Run(device::BluetoothGattService::GATT_ERROR_FAILED); 242 error_callback.Run(device::BluetoothGattService::GATT_ERROR_FAILED);
248 return; 243 return;
249 } 244 }
250 245
251 ++num_notify_sessions_; 246 ++num_notify_sessions_;
252 DCHECK(service_); 247 DCHECK(service_);
253 DCHECK(service_->GetAdapter()); 248 DCHECK(service_->GetAdapter());
254 DCHECK(service_->GetDevice()); 249 DCHECK(service_->GetDevice());
255 scoped_ptr<device::BluetoothGattNotifySession> session( 250 scoped_ptr<device::BluetoothGattNotifySession> session(
256 new BluetoothGattNotifySessionChromeOS( 251 new BluetoothGattNotifySessionBlueZ(
257 service_->GetAdapter(), 252 service_->GetAdapter(), service_->GetDevice()->GetAddress(),
258 service_->GetDevice()->GetAddress(), 253 service_->GetIdentifier(), GetIdentifier(), object_path_));
259 service_->GetIdentifier(),
260 GetIdentifier(),
261 object_path_));
262 callback.Run(session.Pass()); 254 callback.Run(session.Pass());
263 return; 255 return;
264 } 256 }
265 257
266 num_notify_sessions_ = 0; 258 num_notify_sessions_ = 0;
267 } 259 }
268 260
269 // Queue the callbacks if there is a pending call to bluetoothd. 261 // Queue the callbacks if there is a pending call to bluetoothd.
270 if (notify_call_pending_) { 262 if (notify_call_pending_) {
271 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); 263 pending_start_notify_calls_.push(std::make_pair(callback, error_callback));
272 return; 264 return;
273 } 265 }
274 266
275 notify_call_pending_ = true; 267 notify_call_pending_ = true;
276 bluez::BluezDBusManager::Get() 268 bluez::BluezDBusManager::Get()
277 ->GetBluetoothGattCharacteristicClient() 269 ->GetBluetoothGattCharacteristicClient()
278 ->StartNotify( 270 ->StartNotify(
279 object_path_, 271 object_path_,
280 base::Bind( 272 base::Bind(
281 &BluetoothRemoteGattCharacteristicChromeOS::OnStartNotifySuccess, 273 &BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifySuccess,
282 weak_ptr_factory_.GetWeakPtr(), callback), 274 weak_ptr_factory_.GetWeakPtr(), callback),
283 base::Bind( 275 base::Bind(
284 &BluetoothRemoteGattCharacteristicChromeOS::OnStartNotifyError, 276 &BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifyError,
285 weak_ptr_factory_.GetWeakPtr(), error_callback)); 277 weak_ptr_factory_.GetWeakPtr(), error_callback));
286 } 278 }
287 279
288 void BluetoothRemoteGattCharacteristicChromeOS::RemoveNotifySession( 280 void BluetoothRemoteGattCharacteristicBlueZ::RemoveNotifySession(
289 const base::Closure& callback) { 281 const base::Closure& callback) {
290 VLOG(1) << __func__; 282 VLOG(1) << __func__;
291 283
292 if (num_notify_sessions_ > 1) { 284 if (num_notify_sessions_ > 1) {
293 DCHECK(!notify_call_pending_); 285 DCHECK(!notify_call_pending_);
294 --num_notify_sessions_; 286 --num_notify_sessions_;
295 callback.Run(); 287 callback.Run();
296 return; 288 return;
297 } 289 }
298 290
(...skipping 10 matching lines...) Expand all
309 return; 301 return;
310 } 302 }
311 303
312 DCHECK(num_notify_sessions_ == 1); 304 DCHECK(num_notify_sessions_ == 1);
313 notify_call_pending_ = true; 305 notify_call_pending_ = true;
314 bluez::BluezDBusManager::Get() 306 bluez::BluezDBusManager::Get()
315 ->GetBluetoothGattCharacteristicClient() 307 ->GetBluetoothGattCharacteristicClient()
316 ->StopNotify( 308 ->StopNotify(
317 object_path_, 309 object_path_,
318 base::Bind( 310 base::Bind(
319 &BluetoothRemoteGattCharacteristicChromeOS::OnStopNotifySuccess, 311 &BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifySuccess,
320 weak_ptr_factory_.GetWeakPtr(), callback), 312 weak_ptr_factory_.GetWeakPtr(), callback),
321 base::Bind( 313 base::Bind(&BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifyError,
322 &BluetoothRemoteGattCharacteristicChromeOS::OnStopNotifyError, 314 weak_ptr_factory_.GetWeakPtr(), callback));
323 weak_ptr_factory_.GetWeakPtr(), callback));
324 } 315 }
325 316
326 void BluetoothRemoteGattCharacteristicChromeOS::GattDescriptorAdded( 317 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded(
327 const dbus::ObjectPath& object_path) { 318 const dbus::ObjectPath& object_path) {
328 if (descriptors_.find(object_path) != descriptors_.end()) { 319 if (descriptors_.find(object_path) != descriptors_.end()) {
329 VLOG(1) << "Remote GATT characteristic descriptor already exists: " 320 VLOG(1) << "Remote GATT characteristic descriptor already exists: "
330 << object_path.value(); 321 << object_path.value();
331 return; 322 return;
332 } 323 }
333 324
334 bluez::BluetoothGattDescriptorClient::Properties* properties = 325 bluez::BluetoothGattDescriptorClient::Properties* properties =
335 bluez::BluezDBusManager::Get() 326 bluez::BluezDBusManager::Get()
336 ->GetBluetoothGattDescriptorClient() 327 ->GetBluetoothGattDescriptorClient()
337 ->GetProperties(object_path); 328 ->GetProperties(object_path);
338 DCHECK(properties); 329 DCHECK(properties);
339 if (properties->characteristic.value() != object_path_) { 330 if (properties->characteristic.value() != object_path_) {
340 VLOG(3) << "Remote GATT descriptor does not belong to this characteristic."; 331 VLOG(3) << "Remote GATT descriptor does not belong to this characteristic.";
341 return; 332 return;
342 } 333 }
343 334
344 VLOG(1) << "Adding new remote GATT descriptor for GATT characteristic: " 335 VLOG(1) << "Adding new remote GATT descriptor for GATT characteristic: "
345 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); 336 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
346 337
347 BluetoothRemoteGattDescriptorChromeOS* descriptor = 338 BluetoothRemoteGattDescriptorBlueZ* descriptor =
348 new BluetoothRemoteGattDescriptorChromeOS(this, object_path); 339 new BluetoothRemoteGattDescriptorBlueZ(this, object_path);
349 descriptors_[object_path] = descriptor; 340 descriptors_[object_path] = descriptor;
350 DCHECK(descriptor->GetIdentifier() == object_path.value()); 341 DCHECK(descriptor->GetIdentifier() == object_path.value());
351 DCHECK(descriptor->GetUUID().IsValid()); 342 DCHECK(descriptor->GetUUID().IsValid());
352 DCHECK(service_); 343 DCHECK(service_);
353 344
354 service_->NotifyDescriptorAddedOrRemoved(this, descriptor, true /* added */); 345 service_->NotifyDescriptorAddedOrRemoved(this, descriptor, true /* added */);
355 } 346 }
356 347
357 void BluetoothRemoteGattCharacteristicChromeOS::GattDescriptorRemoved( 348 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorRemoved(
358 const dbus::ObjectPath& object_path) { 349 const dbus::ObjectPath& object_path) {
359 DescriptorMap::iterator iter = descriptors_.find(object_path); 350 DescriptorMap::iterator iter = descriptors_.find(object_path);
360 if (iter == descriptors_.end()) { 351 if (iter == descriptors_.end()) {
361 VLOG(2) << "Unknown descriptor removed: " << object_path.value(); 352 VLOG(2) << "Unknown descriptor removed: " << object_path.value();
362 return; 353 return;
363 } 354 }
364 355
365 VLOG(1) << "Removing remote GATT descriptor from characteristic: " 356 VLOG(1) << "Removing remote GATT descriptor from characteristic: "
366 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); 357 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
367 358
368 BluetoothRemoteGattDescriptorChromeOS* descriptor = iter->second; 359 BluetoothRemoteGattDescriptorBlueZ* descriptor = iter->second;
369 DCHECK(descriptor->object_path() == object_path); 360 DCHECK(descriptor->object_path() == object_path);
370 descriptors_.erase(iter); 361 descriptors_.erase(iter);
371 362
372 DCHECK(service_); 363 DCHECK(service_);
373 service_->NotifyDescriptorAddedOrRemoved(this, descriptor, false /* added */); 364 service_->NotifyDescriptorAddedOrRemoved(this, descriptor, false /* added */);
374 365
375 delete descriptor; 366 delete descriptor;
376 } 367 }
377 368
378 void BluetoothRemoteGattCharacteristicChromeOS::GattDescriptorPropertyChanged( 369 void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorPropertyChanged(
379 const dbus::ObjectPath& object_path, 370 const dbus::ObjectPath& object_path,
380 const std::string& property_name) { 371 const std::string& property_name) {
381 DescriptorMap::iterator iter = descriptors_.find(object_path); 372 DescriptorMap::iterator iter = descriptors_.find(object_path);
382 if (iter == descriptors_.end()) { 373 if (iter == descriptors_.end()) {
383 VLOG(2) << "Unknown descriptor removed: " << object_path.value(); 374 VLOG(2) << "Unknown descriptor removed: " << object_path.value();
384 return; 375 return;
385 } 376 }
386 377
387 bluez::BluetoothGattDescriptorClient::Properties* properties = 378 bluez::BluetoothGattDescriptorClient::Properties* properties =
388 bluez::BluezDBusManager::Get() 379 bluez::BluezDBusManager::Get()
389 ->GetBluetoothGattDescriptorClient() 380 ->GetBluetoothGattDescriptorClient()
390 ->GetProperties(object_path); 381 ->GetProperties(object_path);
391 382
392 DCHECK(properties); 383 DCHECK(properties);
393 384
394 if (property_name != properties->value.name()) 385 if (property_name != properties->value.name())
395 return; 386 return;
396 387
397 DCHECK(service_); 388 DCHECK(service_);
398 service_->NotifyDescriptorValueChanged(this, iter->second, 389 service_->NotifyDescriptorValueChanged(this, iter->second,
399 properties->value.value()); 390 properties->value.value());
400 } 391 }
401 392
402 void BluetoothRemoteGattCharacteristicChromeOS::OnError( 393 void BluetoothRemoteGattCharacteristicBlueZ::OnError(
403 const ErrorCallback& error_callback, 394 const ErrorCallback& error_callback,
404 const std::string& error_name, 395 const std::string& error_name,
405 const std::string& error_message) { 396 const std::string& error_message) {
406 VLOG(1) << "Operation failed: " << error_name << ", message: " 397 VLOG(1) << "Operation failed: " << error_name
407 << error_message; 398 << ", message: " << error_message;
408 error_callback.Run( 399 error_callback.Run(
409 BluetoothRemoteGattServiceChromeOS::DBusErrorToServiceError(error_name)); 400 BluetoothRemoteGattServiceBlueZ::DBusErrorToServiceError(error_name));
410 } 401 }
411 402
412 void BluetoothRemoteGattCharacteristicChromeOS::OnStartNotifySuccess( 403 void BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifySuccess(
413 const NotifySessionCallback& callback) { 404 const NotifySessionCallback& callback) {
414 VLOG(1) << "Started notifications from characteristic: " 405 VLOG(1) << "Started notifications from characteristic: "
415 << object_path_.value(); 406 << object_path_.value();
416 DCHECK(num_notify_sessions_ == 0); 407 DCHECK(num_notify_sessions_ == 0);
417 DCHECK(notify_call_pending_); 408 DCHECK(notify_call_pending_);
418 409
419 ++num_notify_sessions_; 410 ++num_notify_sessions_;
420 notify_call_pending_ = false; 411 notify_call_pending_ = false;
421 412
422 // Invoke the queued callbacks for this operation. 413 // Invoke the queued callbacks for this operation.
423 DCHECK(service_); 414 DCHECK(service_);
424 DCHECK(service_->GetDevice()); 415 DCHECK(service_->GetDevice());
425 scoped_ptr<device::BluetoothGattNotifySession> session( 416 scoped_ptr<device::BluetoothGattNotifySession> session(
426 new BluetoothGattNotifySessionChromeOS( 417 new BluetoothGattNotifySessionBlueZ(
427 service_->GetAdapter(), 418 service_->GetAdapter(), service_->GetDevice()->GetAddress(),
428 service_->GetDevice()->GetAddress(), 419 service_->GetIdentifier(), GetIdentifier(), object_path_));
429 service_->GetIdentifier(),
430 GetIdentifier(),
431 object_path_));
432 callback.Run(session.Pass()); 420 callback.Run(session.Pass());
433 421
434 ProcessStartNotifyQueue(); 422 ProcessStartNotifyQueue();
435 } 423 }
436 424
437 void BluetoothRemoteGattCharacteristicChromeOS::OnStartNotifyError( 425 void BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifyError(
438 const ErrorCallback& error_callback, 426 const ErrorCallback& error_callback,
439 const std::string& error_name, 427 const std::string& error_name,
440 const std::string& error_message) { 428 const std::string& error_message) {
441 VLOG(1) << "Failed to start notifications from characteristic: " 429 VLOG(1) << "Failed to start notifications from characteristic: "
442 << object_path_.value() << ": " << error_name << ", " 430 << object_path_.value() << ": " << error_name << ", "
443 << error_message; 431 << error_message;
444 DCHECK(num_notify_sessions_ == 0); 432 DCHECK(num_notify_sessions_ == 0);
445 DCHECK(notify_call_pending_); 433 DCHECK(notify_call_pending_);
446 434
447 notify_call_pending_ = false; 435 notify_call_pending_ = false;
448 436
449 error_callback.Run( 437 error_callback.Run(
450 BluetoothRemoteGattServiceChromeOS::DBusErrorToServiceError(error_name)); 438 BluetoothRemoteGattServiceBlueZ::DBusErrorToServiceError(error_name));
451 439
452 ProcessStartNotifyQueue(); 440 ProcessStartNotifyQueue();
453 } 441 }
454 442
455 void BluetoothRemoteGattCharacteristicChromeOS::OnStopNotifySuccess( 443 void BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifySuccess(
456 const base::Closure& callback) { 444 const base::Closure& callback) {
457 DCHECK(notify_call_pending_); 445 DCHECK(notify_call_pending_);
458 DCHECK(num_notify_sessions_ == 1); 446 DCHECK(num_notify_sessions_ == 1);
459 447
460 notify_call_pending_ = false; 448 notify_call_pending_ = false;
461 --num_notify_sessions_; 449 --num_notify_sessions_;
462 callback.Run(); 450 callback.Run();
463 451
464 ProcessStartNotifyQueue(); 452 ProcessStartNotifyQueue();
465 } 453 }
466 454
467 void BluetoothRemoteGattCharacteristicChromeOS::OnStopNotifyError( 455 void BluetoothRemoteGattCharacteristicBlueZ::OnStopNotifyError(
468 const base::Closure& callback, 456 const base::Closure& callback,
469 const std::string& error_name, 457 const std::string& error_name,
470 const std::string& error_message) { 458 const std::string& error_message) {
471 VLOG(1) << "Call to stop notifications failed for characteristic: " 459 VLOG(1) << "Call to stop notifications failed for characteristic: "
472 << object_path_.value() << ": " << error_name << ", " 460 << object_path_.value() << ": " << error_name << ", "
473 << error_message; 461 << error_message;
474 462
475 // Since this is a best effort operation, treat this as success. 463 // Since this is a best effort operation, treat this as success.
476 OnStopNotifySuccess(callback); 464 OnStopNotifySuccess(callback);
477 } 465 }
478 466
479 void BluetoothRemoteGattCharacteristicChromeOS::ProcessStartNotifyQueue() { 467 void BluetoothRemoteGattCharacteristicBlueZ::ProcessStartNotifyQueue() {
480 while (!pending_start_notify_calls_.empty()) { 468 while (!pending_start_notify_calls_.empty()) {
481 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); 469 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front();
482 pending_start_notify_calls_.pop(); 470 pending_start_notify_calls_.pop();
483 StartNotifySession(callbacks.first, callbacks.second); 471 StartNotifySession(callbacks.first, callbacks.second);
484 } 472 }
485 } 473 }
486 474
487 } // namespace chromeos 475 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698