OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_low_energy_device_mac.h" | 5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
6 | 6 |
7 #import <CoreFoundation/CoreFoundation.h> | 7 #import <CoreFoundation/CoreFoundation.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { | 185 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { |
186 GetMacAdapter()->DisconnectGatt(this); | 186 GetMacAdapter()->DisconnectGatt(this); |
187 } | 187 } |
188 | 188 |
189 void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) { | 189 void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) { |
190 --discovery_pending_count_; | 190 --discovery_pending_count_; |
191 if (discovery_pending_count_ < 0) { | 191 if (discovery_pending_count_ < 0) { |
192 // This should never happens, just in case it happens with a device, | 192 // This should never happen, just in case it happens with a device, |
193 // discovery_pending_count_ is set back to 0. | 193 // discovery_pending_count_ is set back to 0. |
194 VLOG(1) << *this | 194 VLOG(1) << *this |
195 << ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ " | 195 << ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ " |
196 << discovery_pending_count_; | 196 << discovery_pending_count_; |
197 discovery_pending_count_ = 0; | 197 discovery_pending_count_ = 0; |
198 return; | 198 return; |
199 } | 199 } |
200 if (error) { | 200 if (error) { |
201 // TODO(http://crbug.com/609320): Need to pass the error. | 201 // TODO(http://crbug.com/609320): Need to pass the error. |
202 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 202 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 VLOG(1) << *this << ": Can't discover characteristics: " | 249 VLOG(1) << *this << ": Can't discover characteristics: " |
250 << BluetoothAdapterMac::String(error); | 250 << BluetoothAdapterMac::String(error); |
251 return; | 251 return; |
252 } | 252 } |
253 | 253 |
254 if (!IsGattConnected()) { | 254 if (!IsGattConnected()) { |
255 VLOG(1) << *this << ": DidDiscoverCharacteristics, gatt disconnected."; | 255 VLOG(1) << *this << ": DidDiscoverCharacteristics, gatt disconnected."; |
256 // Don't create characteristics if the device disconnected. | 256 // Don't create characteristics if the device disconnected. |
257 return; | 257 return; |
258 } | 258 } |
| 259 if (IsGattServicesDiscoveryComplete()) { |
| 260 // This should never happen, just in case it happens with a device, this |
| 261 // notification should be ignored. |
| 262 VLOG(1) << *this |
| 263 << ": Discovery complete, ignoring DidDiscoverCharacteristics."; |
| 264 return; |
| 265 } |
259 | 266 |
260 BluetoothRemoteGattServiceMac* gatt_service = | 267 BluetoothRemoteGattServiceMac* gatt_service = |
261 GetBluetoothRemoteGattService(cb_service); | 268 GetBluetoothRemoteGattService(cb_service); |
262 DCHECK(gatt_service); | 269 DCHECK(gatt_service); |
263 gatt_service->DidDiscoverCharacteristics(); | 270 gatt_service->DidDiscoverCharacteristics(); |
264 SendNotificationIfDiscoveryComplete(); | 271 SendNotificationIfDiscoveryComplete(); |
265 } | 272 } |
266 | 273 |
267 void BluetoothLowEnergyDeviceMac::DidModifyServices( | 274 void BluetoothLowEnergyDeviceMac::DidModifyServices( |
268 NSArray* invalidatedServices) { | 275 NSArray* invalidatedServices) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 327 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
321 VLOG(1) << *this << ": Can't discover descriptors: " | 328 VLOG(1) << *this << ": Can't discover descriptors: " |
322 << BluetoothAdapterMac::String(error); | 329 << BluetoothAdapterMac::String(error); |
323 return; | 330 return; |
324 } | 331 } |
325 if (!IsGattConnected()) { | 332 if (!IsGattConnected()) { |
326 VLOG(1) << *this << ": DidDiscoverDescriptors, disconnected."; | 333 VLOG(1) << *this << ": DidDiscoverDescriptors, disconnected."; |
327 // Don't discover descriptors if the device disconnected. | 334 // Don't discover descriptors if the device disconnected. |
328 return; | 335 return; |
329 } | 336 } |
| 337 if (IsGattServicesDiscoveryComplete()) { |
| 338 // This should never happen, just in case it happens with a device, this |
| 339 // notification should be ignored. |
| 340 VLOG(1) << *this |
| 341 << ": Discovery complete, ignoring DidDiscoverDescriptors."; |
| 342 return; |
| 343 } |
330 BluetoothRemoteGattServiceMac* gatt_service = | 344 BluetoothRemoteGattServiceMac* gatt_service = |
331 GetBluetoothRemoteGattService(cb_characteristic.service); | 345 GetBluetoothRemoteGattService(cb_characteristic.service); |
332 DCHECK(gatt_service); | 346 DCHECK(gatt_service); |
333 gatt_service->DidDiscoverDescriptors(cb_characteristic); | 347 gatt_service->DidDiscoverDescriptors(cb_characteristic); |
334 SendNotificationIfDiscoveryComplete(); | 348 SendNotificationIfDiscoveryComplete(); |
335 } | 349 } |
336 | 350 |
337 void BluetoothLowEnergyDeviceMac::DidUpdateValueForDescriptor( | 351 void BluetoothLowEnergyDeviceMac::DidUpdateValueForDescriptor( |
338 CBDescriptor* cb_descriptor, | 352 CBDescriptor* cb_descriptor, |
339 NSError* error) { | 353 NSError* error) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 } | 387 } |
374 | 388 |
375 void BluetoothLowEnergyDeviceMac::DiscoverPrimaryServices() { | 389 void BluetoothLowEnergyDeviceMac::DiscoverPrimaryServices() { |
376 VLOG(1) << *this << ": DiscoverPrimaryServices, pending count " | 390 VLOG(1) << *this << ": DiscoverPrimaryServices, pending count " |
377 << discovery_pending_count_; | 391 << discovery_pending_count_; |
378 ++discovery_pending_count_; | 392 ++discovery_pending_count_; |
379 [GetPeripheral() discoverServices:nil]; | 393 [GetPeripheral() discoverServices:nil]; |
380 } | 394 } |
381 | 395 |
382 void BluetoothLowEnergyDeviceMac::SendNotificationIfDiscoveryComplete() { | 396 void BluetoothLowEnergyDeviceMac::SendNotificationIfDiscoveryComplete() { |
| 397 DCHECK(!IsGattServicesDiscoveryComplete()); |
383 // Notify when all services have been discovered. | 398 // Notify when all services have been discovered. |
384 bool discovery_complete = | 399 bool discovery_complete = |
385 discovery_pending_count_ == 0 && | 400 discovery_pending_count_ == 0 && |
386 std::find_if_not( | 401 std::find_if_not( |
387 gatt_services_.begin(), | 402 gatt_services_.begin(), |
388 gatt_services_.end(), [](GattServiceMap::value_type & pair) { | 403 gatt_services_.end(), [](GattServiceMap::value_type & pair) { |
389 BluetoothRemoteGattService* gatt_service = pair.second.get(); | 404 BluetoothRemoteGattService* gatt_service = pair.second.get(); |
390 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) | 405 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) |
391 ->IsDiscoveryComplete(); | 406 ->IsDiscoveryComplete(); |
392 }) == gatt_services_.end(); | 407 }) == gatt_services_.end(); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 std::ostream& operator<<(std::ostream& out, | 481 std::ostream& operator<<(std::ostream& out, |
467 const BluetoothLowEnergyDeviceMac& device) { | 482 const BluetoothLowEnergyDeviceMac& device) { |
468 // TODO(crbug.com/703878): Should use | 483 // TODO(crbug.com/703878): Should use |
469 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. | 484 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
470 base::Optional<std::string> name = device.GetName(); | 485 base::Optional<std::string> name = device.GetName(); |
471 const char* name_cstr = name ? name->c_str() : ""; | 486 const char* name_cstr = name ? name->c_str() : ""; |
472 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" | 487 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
473 << &device << ", \"" << name_cstr << "\">"; | 488 << &device << ", \"" << name_cstr << "\">"; |
474 } | 489 } |
475 } // namespace device | 490 } // namespace device |
OLD | NEW |