| 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 using device::BluetoothDevice; | 24 using device::BluetoothDevice; |
| 24 using device::BluetoothLowEnergyDeviceMac; | 25 using device::BluetoothLowEnergyDeviceMac; |
| 25 | 26 |
| 26 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | 27 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 VLOG(1) << *this | 209 VLOG(1) << *this |
| 209 << ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ " | 210 << ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ " |
| 210 << discovery_pending_count_; | 211 << discovery_pending_count_; |
| 211 discovery_pending_count_ = 0; | 212 discovery_pending_count_ = 0; |
| 212 return; | 213 return; |
| 213 } | 214 } |
| 214 if (error) { | 215 if (error) { |
| 215 // TODO(http://crbug.com/609320): Need to pass the error. | 216 // TODO(http://crbug.com/609320): Need to pass the error. |
| 216 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 217 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
| 217 // a device services. | 218 // a device services. |
| 219 LogNSErrorToHistogram( |
| 220 error, WebBluetoothMacOSAPIs:: |
| 221 WEB_BLUETOOTH_MACOS_APIS_DID_DISCOVER_PRIMARY_SERVICES); |
| 218 VLOG(1) << *this << ": Can't discover primary services: " | 222 VLOG(1) << *this << ": Can't discover primary services: " |
| 219 << BluetoothAdapterMac::String(error); | 223 << BluetoothAdapterMac::String(error); |
| 220 return; | 224 return; |
| 221 } | 225 } |
| 222 | 226 |
| 223 if (!IsGattConnected()) { | 227 if (!IsGattConnected()) { |
| 224 // Don't create services if the device disconnected. | 228 // Don't create services if the device disconnected. |
| 225 VLOG(1) << *this << ": DidDiscoverPrimaryServices, gatt not connected."; | 229 VLOG(1) << *this << ": DidDiscoverPrimaryServices, gatt not connected."; |
| 226 return; | 230 return; |
| 227 } | 231 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 251 gatt_service_mac->DiscoverCharacteristics(); | 255 gatt_service_mac->DiscoverCharacteristics(); |
| 252 } | 256 } |
| 253 SendNotificationIfDiscoveryComplete(); | 257 SendNotificationIfDiscoveryComplete(); |
| 254 } | 258 } |
| 255 } | 259 } |
| 256 | 260 |
| 257 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( | 261 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( |
| 258 CBService* cb_service, | 262 CBService* cb_service, |
| 259 NSError* error) { | 263 NSError* error) { |
| 260 if (error) { | 264 if (error) { |
| 265 LogNSErrorToHistogram( |
| 266 error, WebBluetoothMacOSAPIs:: |
| 267 WEB_BLUETOOTH_MACOS_APIS_DID_DISCOVER_CHARACTERISTICS); |
| 261 // TODO(http://crbug.com/609320): Need to pass the error. | 268 // TODO(http://crbug.com/609320): Need to pass the error. |
| 262 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 269 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
| 263 VLOG(1) << *this << ": Can't discover characteristics: " | 270 VLOG(1) << *this << ": Can't discover characteristics: " |
| 264 << BluetoothAdapterMac::String(error); | 271 << BluetoothAdapterMac::String(error); |
| 265 return; | 272 return; |
| 266 } | 273 } |
| 267 | 274 |
| 268 if (!IsGattConnected()) { | 275 if (!IsGattConnected()) { |
| 269 VLOG(1) << *this << ": DidDiscoverCharacteristics, gatt disconnected."; | 276 VLOG(1) << *this << ": DidDiscoverCharacteristics, gatt disconnected."; |
| 270 // Don't create characteristics if the device disconnected. | 277 // Don't create characteristics if the device disconnected. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 BluetoothRemoteGattServiceMac* gatt_service = | 337 BluetoothRemoteGattServiceMac* gatt_service = |
| 331 GetBluetoothRemoteGattService(characteristic.service); | 338 GetBluetoothRemoteGattService(characteristic.service); |
| 332 DCHECK(gatt_service); | 339 DCHECK(gatt_service); |
| 333 gatt_service->DidUpdateNotificationState(characteristic, error); | 340 gatt_service->DidUpdateNotificationState(characteristic, error); |
| 334 } | 341 } |
| 335 | 342 |
| 336 void BluetoothLowEnergyDeviceMac::DidDiscoverDescriptors( | 343 void BluetoothLowEnergyDeviceMac::DidDiscoverDescriptors( |
| 337 CBCharacteristic* cb_characteristic, | 344 CBCharacteristic* cb_characteristic, |
| 338 NSError* error) { | 345 NSError* error) { |
| 339 if (error) { | 346 if (error) { |
| 347 LogNSErrorToHistogram( |
| 348 error, WebBluetoothMacOSAPIs:: |
| 349 WEB_BLUETOOTH_MACOS_APIS_DID_DISCOVER_DESCRIPTORS); |
| 340 // TODO(http://crbug.com/609320): Need to pass the error. | 350 // TODO(http://crbug.com/609320): Need to pass the error. |
| 341 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 351 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
| 342 VLOG(1) << *this << ": Can't discover descriptors: " | 352 VLOG(1) << *this << ": Can't discover descriptors: " |
| 343 << BluetoothAdapterMac::String(error); | 353 << BluetoothAdapterMac::String(error); |
| 344 return; | 354 return; |
| 345 } | 355 } |
| 346 if (!IsGattConnected()) { | 356 if (!IsGattConnected()) { |
| 347 VLOG(1) << *this << ": DidDiscoverDescriptors, disconnected."; | 357 VLOG(1) << *this << ": DidDiscoverDescriptors, disconnected."; |
| 348 // Don't discover descriptors if the device disconnected. | 358 // Don't discover descriptors if the device disconnected. |
| 349 return; | 359 return; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( | 486 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( |
| 477 cb_descriptor); | 487 cb_descriptor); |
| 478 } | 488 } |
| 479 | 489 |
| 480 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 490 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
| 481 connected_ = false; | 491 connected_ = false; |
| 482 VLOG(1) << *this << ": Disconnected from peripheral."; | 492 VLOG(1) << *this << ": Disconnected from peripheral."; |
| 483 if (error) { | 493 if (error) { |
| 484 VLOG(1) << *this | 494 VLOG(1) << *this |
| 485 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); | 495 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); |
| 496 LogNSErrorToHistogram( |
| 497 error, WebBluetoothMacOSAPIs:: |
| 498 WEB_BLUETOOTH_MACOS_APIS_DID_DISCONNECT_PERIPHERAL); |
| 486 } | 499 } |
| 487 SetGattServicesDiscoveryComplete(false); | 500 SetGattServicesDiscoveryComplete(false); |
| 488 // Removing all services at once to ensure that calling GetGattService on | 501 // Removing all services at once to ensure that calling GetGattService on |
| 489 // removed service in GattServiceRemoved returns null. | 502 // removed service in GattServiceRemoved returns null. |
| 490 GattServiceMap gatt_services_swapped; | 503 GattServiceMap gatt_services_swapped; |
| 491 gatt_services_swapped.swap(gatt_services_); | 504 gatt_services_swapped.swap(gatt_services_); |
| 492 gatt_services_swapped.clear(); | 505 gatt_services_swapped.clear(); |
| 493 device_uuids_.ClearServiceUUIDs(); | 506 device_uuids_.ClearServiceUUIDs(); |
| 494 // There are two cases in which this function will be called: | 507 // There are two cases in which this function will be called: |
| 495 // 1. When the connection to the device breaks (either because | 508 // 1. When the connection to the device breaks (either because |
| (...skipping 16 matching lines...) Expand all Loading... |
| 512 // TODO(crbug.com/703878): Should use | 525 // TODO(crbug.com/703878): Should use |
| 513 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. | 526 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
| 514 base::Optional<std::string> name = device.GetName(); | 527 base::Optional<std::string> name = device.GetName(); |
| 515 const char* is_gatt_connected = | 528 const char* is_gatt_connected = |
| 516 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; | 529 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; |
| 517 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" | 530 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
| 518 << &device << ", " << is_gatt_connected << ", \"" | 531 << &device << ", " << is_gatt_connected << ", \"" |
| 519 << name.value_or("Unnamed device") << "\">"; | 532 << name.value_or("Unnamed device") << "\">"; |
| 520 } | 533 } |
| 521 } // namespace device | 534 } // namespace device |
| OLD | NEW |