| 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" |
| 11 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/mac/sdk_forward_declarations.h" | 12 #include "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "device/bluetooth/bluetooth_adapter_mac.h" | 16 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 17 #include "device/bluetooth/bluetooth_adapter_mac_metrics.h" |
| 17 #include "device/bluetooth/bluetooth_device.h" | 18 #include "device/bluetooth/bluetooth_device.h" |
| 18 #include "device/bluetooth/bluetooth_low_energy_peripheral_delegate.h" | 19 #include "device/bluetooth/bluetooth_low_energy_peripheral_delegate.h" |
| 19 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | 20 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| 20 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" | 21 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" |
| 21 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 22 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 22 | 23 |
| 23 // Remove when Chrome no longer supports 10.12. | 24 // Remove when Chrome no longer supports 10.12. |
| 24 #if defined(MAC_OS_X_VERSION_10_13) | 25 #if defined(MAC_OS_X_VERSION_10_13) |
| 25 | 26 |
| 26 // In the 10.13 SDK, CBPeripheral became a subclass of CBPeer, which defines | 27 // In the 10.13 SDK, CBPeripheral became a subclass of CBPeer, which defines |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 --discovery_pending_count_; | 216 --discovery_pending_count_; |
| 216 if (discovery_pending_count_ < 0) { | 217 if (discovery_pending_count_ < 0) { |
| 217 // This should never happen, just in case it happens with a device, | 218 // This should never happen, just in case it happens with a device, |
| 218 // discovery_pending_count_ is set back to 0. | 219 // discovery_pending_count_ is set back to 0. |
| 219 VLOG(1) << *this | 220 VLOG(1) << *this |
| 220 << ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ " | 221 << ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ " |
| 221 << discovery_pending_count_; | 222 << discovery_pending_count_; |
| 222 discovery_pending_count_ = 0; | 223 discovery_pending_count_ = 0; |
| 223 return; | 224 return; |
| 224 } | 225 } |
| 226 LogDidDiscoverPrimaryServicesErrorToHistogram(error); |
| 225 if (error) { | 227 if (error) { |
| 226 // TODO(http://crbug.com/609320): Need to pass the error. | 228 // TODO(http://crbug.com/609320): Need to pass the error. |
| 227 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 229 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
| 228 // a device services. | 230 // a device services. |
| 229 VLOG(1) << *this << ": Can't discover primary services: " | 231 VLOG(1) << *this << ": Can't discover primary services: " |
| 230 << BluetoothAdapterMac::String(error); | 232 << BluetoothAdapterMac::String(error); |
| 231 return; | 233 return; |
| 232 } | 234 } |
| 233 | 235 |
| 234 if (!IsGattConnected()) { | 236 if (!IsGattConnected()) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 261 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | 263 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); |
| 262 gatt_service_mac->DiscoverCharacteristics(); | 264 gatt_service_mac->DiscoverCharacteristics(); |
| 263 } | 265 } |
| 264 SendNotificationIfDiscoveryComplete(); | 266 SendNotificationIfDiscoveryComplete(); |
| 265 } | 267 } |
| 266 } | 268 } |
| 267 | 269 |
| 268 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( | 270 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( |
| 269 CBService* cb_service, | 271 CBService* cb_service, |
| 270 NSError* error) { | 272 NSError* error) { |
| 273 LogDidDiscoverCharacteristicsErrorToHistogram(error); |
| 271 if (error) { | 274 if (error) { |
| 272 // TODO(http://crbug.com/609320): Need to pass the error. | 275 // TODO(http://crbug.com/609320): Need to pass the error. |
| 273 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 276 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
| 274 VLOG(1) << *this << ": Can't discover characteristics: " | 277 VLOG(1) << *this << ": Can't discover characteristics: " |
| 275 << BluetoothAdapterMac::String(error); | 278 << BluetoothAdapterMac::String(error); |
| 276 return; | 279 return; |
| 277 } | 280 } |
| 278 | 281 |
| 279 if (!IsGattConnected()) { | 282 if (!IsGattConnected()) { |
| 280 VLOG(1) << *this << ": DidDiscoverCharacteristics, gatt disconnected."; | 283 VLOG(1) << *this << ": DidDiscoverCharacteristics, gatt disconnected."; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 NSError* error) { | 343 NSError* error) { |
| 341 BluetoothRemoteGattServiceMac* gatt_service = | 344 BluetoothRemoteGattServiceMac* gatt_service = |
| 342 GetBluetoothRemoteGattServiceMac(characteristic.service); | 345 GetBluetoothRemoteGattServiceMac(characteristic.service); |
| 343 DCHECK(gatt_service); | 346 DCHECK(gatt_service); |
| 344 gatt_service->DidUpdateNotificationState(characteristic, error); | 347 gatt_service->DidUpdateNotificationState(characteristic, error); |
| 345 } | 348 } |
| 346 | 349 |
| 347 void BluetoothLowEnergyDeviceMac::DidDiscoverDescriptors( | 350 void BluetoothLowEnergyDeviceMac::DidDiscoverDescriptors( |
| 348 CBCharacteristic* cb_characteristic, | 351 CBCharacteristic* cb_characteristic, |
| 349 NSError* error) { | 352 NSError* error) { |
| 353 LogDidDiscoverDescriptorsErrorToHistogram(error); |
| 350 if (error) { | 354 if (error) { |
| 351 // TODO(http://crbug.com/609320): Need to pass the error. | 355 // TODO(http://crbug.com/609320): Need to pass the error. |
| 352 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 356 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
| 353 VLOG(1) << *this << ": Can't discover descriptors: " | 357 VLOG(1) << *this << ": Can't discover descriptors: " |
| 354 << BluetoothAdapterMac::String(error); | 358 << BluetoothAdapterMac::String(error); |
| 355 return; | 359 return; |
| 356 } | 360 } |
| 357 if (!IsGattConnected()) { | 361 if (!IsGattConnected()) { |
| 358 VLOG(1) << *this << ": DidDiscoverDescriptors, disconnected."; | 362 VLOG(1) << *this << ": DidDiscoverDescriptors, disconnected."; |
| 359 // Don't discover descriptors if the device disconnected. | 363 // Don't discover descriptors if the device disconnected. |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 BluetoothRemoteGattCharacteristicMac* gatt_characteristic = | 488 BluetoothRemoteGattCharacteristicMac* gatt_characteristic = |
| 485 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); | 489 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
| 486 DCHECK(gatt_characteristic); | 490 DCHECK(gatt_characteristic); |
| 487 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( | 491 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( |
| 488 cb_descriptor); | 492 cb_descriptor); |
| 489 } | 493 } |
| 490 | 494 |
| 491 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 495 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
| 492 connected_ = false; | 496 connected_ = false; |
| 493 VLOG(1) << *this << ": Disconnected from peripheral."; | 497 VLOG(1) << *this << ": Disconnected from peripheral."; |
| 498 LogDidDisconnectPeripheralErrorToHistogram(error); |
| 494 if (error) { | 499 if (error) { |
| 495 VLOG(1) << *this | 500 VLOG(1) << *this |
| 496 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); | 501 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); |
| 497 } | 502 } |
| 498 SetGattServicesDiscoveryComplete(false); | 503 SetGattServicesDiscoveryComplete(false); |
| 499 // Removing all services at once to ensure that calling GetGattService on | 504 // Removing all services at once to ensure that calling GetGattService on |
| 500 // removed service in GattServiceRemoved returns null. | 505 // removed service in GattServiceRemoved returns null. |
| 501 GattServiceMap gatt_services_swapped; | 506 GattServiceMap gatt_services_swapped; |
| 502 gatt_services_swapped.swap(gatt_services_); | 507 gatt_services_swapped.swap(gatt_services_); |
| 503 gatt_services_swapped.clear(); | 508 gatt_services_swapped.clear(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 522 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. | 527 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
| 523 base::Optional<std::string> name = device.GetName(); | 528 base::Optional<std::string> name = device.GetName(); |
| 524 const char* is_gatt_connected = | 529 const char* is_gatt_connected = |
| 525 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; | 530 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; |
| 526 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" | 531 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
| 527 << &device << ", " << is_gatt_connected << ", \"" | 532 << &device << ", " << is_gatt_connected << ", \"" |
| 528 << name.value_or("Unnamed device") << "\">"; | 533 << name.value_or("Unnamed device") << "\">"; |
| 529 } | 534 } |
| 530 | 535 |
| 531 } // namespace device | 536 } // namespace device |
| OLD | NEW |