| 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 RecordDidDiscoverPrimaryServicesResult(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 RecordDidDiscoverCharacteristicsResult(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 BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac = | 344 BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac = |
| 342 GetBluetoothRemoteGattCharacteristicMac(characteristic); | 345 GetBluetoothRemoteGattCharacteristicMac(characteristic); |
| 343 DCHECK(gatt_characteristic_mac); | 346 DCHECK(gatt_characteristic_mac); |
| 344 gatt_characteristic_mac->DidUpdateNotificationState(error); | 347 gatt_characteristic_mac->DidUpdateNotificationState(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 RecordDidDiscoverDescriptorsResult(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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 GetBluetoothRemoteGattServiceMac(cb_service); | 499 GetBluetoothRemoteGattServiceMac(cb_service); |
| 496 if (!gatt_service_mac) { | 500 if (!gatt_service_mac) { |
| 497 return nullptr; | 501 return nullptr; |
| 498 } | 502 } |
| 499 return gatt_service_mac->GetBluetoothRemoteGattDescriptorMac(cb_descriptor); | 503 return gatt_service_mac->GetBluetoothRemoteGattDescriptorMac(cb_descriptor); |
| 500 } | 504 } |
| 501 | 505 |
| 502 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 506 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
| 503 connected_ = false; | 507 connected_ = false; |
| 504 VLOG(1) << *this << ": Disconnected from peripheral."; | 508 VLOG(1) << *this << ": Disconnected from peripheral."; |
| 509 RecordDidDisconnectPeripheralResult(error); |
| 505 if (error) { | 510 if (error) { |
| 506 VLOG(1) << *this | 511 VLOG(1) << *this |
| 507 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); | 512 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); |
| 508 } | 513 } |
| 509 SetGattServicesDiscoveryComplete(false); | 514 SetGattServicesDiscoveryComplete(false); |
| 510 // Removing all services at once to ensure that calling GetGattService on | 515 // Removing all services at once to ensure that calling GetGattService on |
| 511 // removed service in GattServiceRemoved returns null. | 516 // removed service in GattServiceRemoved returns null. |
| 512 GattServiceMap gatt_services_swapped; | 517 GattServiceMap gatt_services_swapped; |
| 513 gatt_services_swapped.swap(gatt_services_); | 518 gatt_services_swapped.swap(gatt_services_); |
| 514 gatt_services_swapped.clear(); | 519 gatt_services_swapped.clear(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 533 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. | 538 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
| 534 base::Optional<std::string> name = device.GetName(); | 539 base::Optional<std::string> name = device.GetName(); |
| 535 const char* is_gatt_connected = | 540 const char* is_gatt_connected = |
| 536 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; | 541 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; |
| 537 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" | 542 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
| 538 << &device << ", " << is_gatt_connected << ", \"" | 543 << &device << ", " << is_gatt_connected << ", \"" |
| 539 << name.value_or("Unnamed device") << "\">"; | 544 << name.value_or("Unnamed device") << "\">"; |
| 540 } | 545 } |
| 541 | 546 |
| 542 } // namespace device | 547 } // namespace device |
| OLD | NEW |