| 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_device.h" | 17 #include "device/bluetooth/bluetooth_device.h" |
| 18 #include "device/bluetooth/bluetooth_low_energy_peripheral_delegate.h" | 18 #include "device/bluetooth/bluetooth_low_energy_peripheral_delegate.h" |
| 19 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | 19 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| 20 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" | 20 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" |
| 21 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 21 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 22 | 22 |
| 23 using device::BluetoothDevice; | 23 namespace device { |
| 24 using device::BluetoothLowEnergyDeviceMac; | |
| 25 | 24 |
| 26 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | 25 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
| 27 BluetoothAdapterMac* adapter, | 26 BluetoothAdapterMac* adapter, |
| 28 CBPeripheral* peripheral) | 27 CBPeripheral* peripheral) |
| 29 : BluetoothDeviceMac(adapter), | 28 : BluetoothDeviceMac(adapter), |
| 30 peripheral_(peripheral, base::scoped_policy::RETAIN), | 29 peripheral_(peripheral, base::scoped_policy::RETAIN), |
| 31 connected_(false), | 30 connected_(false), |
| 32 discovery_pending_count_(0) { | 31 discovery_pending_count_(0) { |
| 33 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 32 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 34 DCHECK(peripheral_); | 33 DCHECK(peripheral_); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } | 176 } |
| 178 | 177 |
| 179 void BluetoothLowEnergyDeviceMac::ConnectToService( | 178 void BluetoothLowEnergyDeviceMac::ConnectToService( |
| 180 const BluetoothUUID& uuid, | 179 const BluetoothUUID& uuid, |
| 181 const ConnectToServiceCallback& callback, | 180 const ConnectToServiceCallback& callback, |
| 182 const ConnectToServiceErrorCallback& error_callback) { | 181 const ConnectToServiceErrorCallback& error_callback) { |
| 183 NOTIMPLEMENTED(); | 182 NOTIMPLEMENTED(); |
| 184 } | 183 } |
| 185 | 184 |
| 186 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( | 185 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( |
| 187 const device::BluetoothUUID& uuid, | 186 const BluetoothUUID& uuid, |
| 188 const ConnectToServiceCallback& callback, | 187 const ConnectToServiceCallback& callback, |
| 189 const ConnectToServiceErrorCallback& error_callback) { | 188 const ConnectToServiceErrorCallback& error_callback) { |
| 190 NOTIMPLEMENTED(); | 189 NOTIMPLEMENTED(); |
| 191 } | 190 } |
| 192 | 191 |
| 193 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { | 192 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { |
| 194 if (!IsGattConnected()) { | 193 if (!IsGattConnected()) { |
| 195 GetMacAdapter()->CreateGattConnection(this); | 194 GetMacAdapter()->CreateGattConnection(this); |
| 196 } | 195 } |
| 197 } | 196 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 gatt_service->GetIdentifier(), base::WrapUnique(gatt_service))); | 237 gatt_service->GetIdentifier(), base::WrapUnique(gatt_service))); |
| 239 DCHECK(result_iter.second); | 238 DCHECK(result_iter.second); |
| 240 VLOG(1) << *gatt_service << ": New service."; | 239 VLOG(1) << *gatt_service << ": New service."; |
| 241 adapter_->NotifyGattServiceAdded(gatt_service); | 240 adapter_->NotifyGattServiceAdded(gatt_service); |
| 242 } else { | 241 } else { |
| 243 VLOG(1) << *gatt_service << ": Known service."; | 242 VLOG(1) << *gatt_service << ": Known service."; |
| 244 } | 243 } |
| 245 } | 244 } |
| 246 if (discovery_pending_count_ == 0) { | 245 if (discovery_pending_count_ == 0) { |
| 247 for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) { | 246 for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) { |
| 248 device::BluetoothRemoteGattService* gatt_service = it->second.get(); | 247 BluetoothRemoteGattService* gatt_service = it->second.get(); |
| 249 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | 248 BluetoothRemoteGattServiceMac* gatt_service_mac = |
| 250 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | 249 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); |
| 251 gatt_service_mac->DiscoverCharacteristics(); | 250 gatt_service_mac->DiscoverCharacteristics(); |
| 252 } | 251 } |
| 253 SendNotificationIfDiscoveryComplete(); | 252 SendNotificationIfDiscoveryComplete(); |
| 254 } | 253 } |
| 255 } | 254 } |
| 256 | 255 |
| 257 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( | 256 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( |
| 258 CBService* cb_service, | 257 CBService* cb_service, |
| 259 NSError* error) { | 258 NSError* error) { |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 }) == gatt_services_.end(); | 434 }) == gatt_services_.end(); |
| 436 if (discovery_complete) { | 435 if (discovery_complete) { |
| 437 VLOG(1) << *this << ": Discovery complete."; | 436 VLOG(1) << *this << ": Discovery complete."; |
| 438 device_uuids_.ReplaceServiceUUIDs(gatt_services_); | 437 device_uuids_.ReplaceServiceUUIDs(gatt_services_); |
| 439 SetGattServicesDiscoveryComplete(true); | 438 SetGattServicesDiscoveryComplete(true); |
| 440 adapter_->NotifyGattServicesDiscovered(this); | 439 adapter_->NotifyGattServicesDiscovered(this); |
| 441 adapter_->NotifyDeviceChanged(this); | 440 adapter_->NotifyDeviceChanged(this); |
| 442 } | 441 } |
| 443 } | 442 } |
| 444 | 443 |
| 445 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { | 444 BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { |
| 446 return static_cast<BluetoothAdapterMac*>(this->adapter_); | 445 return static_cast<BluetoothAdapterMac*>(this->adapter_); |
| 447 } | 446 } |
| 448 | 447 |
| 449 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { | 448 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
| 450 return peripheral_; | 449 return peripheral_; |
| 451 } | 450 } |
| 452 | 451 |
| 453 device::BluetoothRemoteGattServiceMac* | 452 BluetoothRemoteGattServiceMac* |
| 454 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattServiceMac( | 453 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattServiceMac( |
| 455 CBService* cb_service) const { | 454 CBService* cb_service) const { |
| 456 for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) { | 455 for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) { |
| 457 device::BluetoothRemoteGattService* gatt_service = it->second.get(); | 456 BluetoothRemoteGattService* gatt_service = it->second.get(); |
| 458 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | 457 BluetoothRemoteGattServiceMac* gatt_service_mac = |
| 459 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | 458 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); |
| 460 if (gatt_service_mac->GetService() == cb_service) | 459 if (gatt_service_mac->GetService() == cb_service) |
| 461 return gatt_service_mac; | 460 return gatt_service_mac; |
| 462 } | 461 } |
| 463 return nullptr; | 462 return nullptr; |
| 464 } | 463 } |
| 465 | 464 |
| 466 device::BluetoothRemoteGattDescriptorMac* | 465 BluetoothRemoteGattDescriptorMac* |
| 467 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattDescriptorMac( | 466 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattDescriptorMac( |
| 468 CBDescriptor* cb_descriptor) const { | 467 CBDescriptor* cb_descriptor) const { |
| 469 CBCharacteristic* cb_characteristic = cb_descriptor.characteristic; | 468 CBCharacteristic* cb_characteristic = cb_descriptor.characteristic; |
| 470 device::BluetoothRemoteGattServiceMac* gatt_service = | 469 BluetoothRemoteGattServiceMac* gatt_service = |
| 471 GetBluetoothRemoteGattServiceMac(cb_characteristic.service); | 470 GetBluetoothRemoteGattServiceMac(cb_characteristic.service); |
| 472 DCHECK(gatt_service); | 471 DCHECK(gatt_service); |
| 473 device::BluetoothRemoteGattCharacteristicMac* gatt_characteristic = | 472 BluetoothRemoteGattCharacteristicMac* gatt_characteristic = |
| 474 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); | 473 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
| 475 DCHECK(gatt_characteristic); | 474 DCHECK(gatt_characteristic); |
| 476 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( | 475 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( |
| 477 cb_descriptor); | 476 cb_descriptor); |
| 478 } | 477 } |
| 479 | 478 |
| 480 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 479 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
| 481 connected_ = false; | 480 connected_ = false; |
| 482 VLOG(1) << *this << ": Disconnected from peripheral."; | 481 VLOG(1) << *this << ": Disconnected from peripheral."; |
| 483 if (error) { | 482 if (error) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 498 if (create_gatt_connection_error_callbacks_.empty()) { | 497 if (create_gatt_connection_error_callbacks_.empty()) { |
| 499 // If there are no pending callbacks then the connection broke (#1). | 498 // If there are no pending callbacks then the connection broke (#1). |
| 500 DidDisconnectGatt(); | 499 DidDisconnectGatt(); |
| 501 return; | 500 return; |
| 502 } | 501 } |
| 503 // Else we canceled the connection request (#2). | 502 // Else we canceled the connection request (#2). |
| 504 // TODO(http://crbug.com/585897): Need to pass the error. | 503 // TODO(http://crbug.com/585897): Need to pass the error. |
| 505 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); | 504 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); |
| 506 } | 505 } |
| 507 | 506 |
| 508 namespace device { | |
| 509 | |
| 510 std::ostream& operator<<(std::ostream& out, | 507 std::ostream& operator<<(std::ostream& out, |
| 511 const BluetoothLowEnergyDeviceMac& device) { | 508 const BluetoothLowEnergyDeviceMac& device) { |
| 512 // TODO(crbug.com/703878): Should use | 509 // TODO(crbug.com/703878): Should use |
| 513 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. | 510 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
| 514 base::Optional<std::string> name = device.GetName(); | 511 base::Optional<std::string> name = device.GetName(); |
| 515 const char* is_gatt_connected = | 512 const char* is_gatt_connected = |
| 516 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; | 513 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; |
| 517 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" | 514 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
| 518 << &device << ", " << is_gatt_connected << ", \"" | 515 << &device << ", " << is_gatt_connected << ", \"" |
| 519 << name.value_or("Unnamed device") << "\">"; | 516 << name.value_or("Unnamed device") << "\">"; |
| 520 } | 517 } |
| 518 |
| 521 } // namespace device | 519 } // namespace device |
| OLD | NEW |