| 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 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 if (!IsGattConnected()) { | 223 if (!IsGattConnected()) { |
| 224 // Don't create services if the device disconnected. | 224 // Don't create services if the device disconnected. |
| 225 VLOG(1) << *this << ": DidDiscoverPrimaryServices, gatt not connected."; | 225 VLOG(1) << *this << ": DidDiscoverPrimaryServices, gatt not connected."; |
| 226 return; | 226 return; |
| 227 } | 227 } |
| 228 VLOG(1) << *this << ": DidDiscoverPrimaryServices, pending count: " | 228 VLOG(1) << *this << ": DidDiscoverPrimaryServices, pending count: " |
| 229 << discovery_pending_count_; | 229 << discovery_pending_count_; |
| 230 | 230 |
| 231 for (CBService* cb_service in GetPeripheral().services) { | 231 for (CBService* cb_service in GetPeripheral().services) { |
| 232 BluetoothRemoteGattServiceMac* gatt_service = | 232 BluetoothRemoteGattServiceMac* gatt_service = |
| 233 GetBluetoothRemoteGattService(cb_service); | 233 GetBluetoothRemoteGattServiceMac(cb_service); |
| 234 if (!gatt_service) { | 234 if (!gatt_service) { |
| 235 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, | 235 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, |
| 236 true /* is_primary */); | 236 true /* is_primary */); |
| 237 auto result_iter = gatt_services_.insert(std::make_pair( | 237 auto result_iter = gatt_services_.insert(std::make_pair( |
| 238 gatt_service->GetIdentifier(), base::WrapUnique(gatt_service))); | 238 gatt_service->GetIdentifier(), base::WrapUnique(gatt_service))); |
| 239 DCHECK(result_iter.second); | 239 DCHECK(result_iter.second); |
| 240 VLOG(1) << *gatt_service << ": New service."; | 240 VLOG(1) << *gatt_service << ": New service."; |
| 241 adapter_->NotifyGattServiceAdded(gatt_service); | 241 adapter_->NotifyGattServiceAdded(gatt_service); |
| 242 } else { | 242 } else { |
| 243 VLOG(1) << *gatt_service << ": Known service."; | 243 VLOG(1) << *gatt_service << ": Known service."; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 272 } | 272 } |
| 273 if (IsGattServicesDiscoveryComplete()) { | 273 if (IsGattServicesDiscoveryComplete()) { |
| 274 // This should never happen, just in case it happens with a device, this | 274 // This should never happen, just in case it happens with a device, this |
| 275 // notification should be ignored. | 275 // notification should be ignored. |
| 276 VLOG(1) << *this | 276 VLOG(1) << *this |
| 277 << ": Discovery complete, ignoring DidDiscoverCharacteristics."; | 277 << ": Discovery complete, ignoring DidDiscoverCharacteristics."; |
| 278 return; | 278 return; |
| 279 } | 279 } |
| 280 | 280 |
| 281 BluetoothRemoteGattServiceMac* gatt_service = | 281 BluetoothRemoteGattServiceMac* gatt_service = |
| 282 GetBluetoothRemoteGattService(cb_service); | 282 GetBluetoothRemoteGattServiceMac(cb_service); |
| 283 DCHECK(gatt_service); | 283 DCHECK(gatt_service); |
| 284 gatt_service->DidDiscoverCharacteristics(); | 284 gatt_service->DidDiscoverCharacteristics(); |
| 285 SendNotificationIfDiscoveryComplete(); | 285 SendNotificationIfDiscoveryComplete(); |
| 286 } | 286 } |
| 287 | 287 |
| 288 void BluetoothLowEnergyDeviceMac::DidModifyServices( | 288 void BluetoothLowEnergyDeviceMac::DidModifyServices( |
| 289 NSArray* invalidatedServices) { | 289 NSArray* invalidatedServices) { |
| 290 VLOG(1) << *this << ": DidModifyServices: " | 290 VLOG(1) << *this << ": DidModifyServices: " |
| 291 << " invalidated services " | 291 << " invalidated services " |
| 292 << base::SysNSStringToUTF8([invalidatedServices description]); | 292 << base::SysNSStringToUTF8([invalidatedServices description]); |
| 293 for (CBService* cb_service in invalidatedServices) { | 293 for (CBService* cb_service in invalidatedServices) { |
| 294 BluetoothRemoteGattServiceMac* gatt_service = | 294 BluetoothRemoteGattServiceMac* gatt_service = |
| 295 GetBluetoothRemoteGattService(cb_service); | 295 GetBluetoothRemoteGattServiceMac(cb_service); |
| 296 DCHECK(gatt_service); | 296 DCHECK(gatt_service); |
| 297 VLOG(1) << gatt_service->GetUUID().canonical_value(); | 297 VLOG(1) << gatt_service->GetUUID().canonical_value(); |
| 298 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 298 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
| 299 std::move(gatt_services_[gatt_service->GetIdentifier()]); | 299 std::move(gatt_services_[gatt_service->GetIdentifier()]); |
| 300 gatt_services_.erase(gatt_service->GetIdentifier()); | 300 gatt_services_.erase(gatt_service->GetIdentifier()); |
| 301 adapter_->NotifyGattServiceRemoved(scoped_service.get()); | 301 adapter_->NotifyGattServiceRemoved(scoped_service.get()); |
| 302 } | 302 } |
| 303 device_uuids_.ClearServiceUUIDs(); | 303 device_uuids_.ClearServiceUUIDs(); |
| 304 SetGattServicesDiscoveryComplete(false); | 304 SetGattServicesDiscoveryComplete(false); |
| 305 adapter_->NotifyDeviceChanged(this); | 305 adapter_->NotifyDeviceChanged(this); |
| 306 DiscoverPrimaryServices(); | 306 DiscoverPrimaryServices(); |
| 307 } | 307 } |
| 308 | 308 |
| 309 void BluetoothLowEnergyDeviceMac::DidUpdateValue( | 309 void BluetoothLowEnergyDeviceMac::DidUpdateValue( |
| 310 CBCharacteristic* characteristic, | 310 CBCharacteristic* characteristic, |
| 311 NSError* error) { | 311 NSError* error) { |
| 312 BluetoothRemoteGattServiceMac* gatt_service = | 312 BluetoothRemoteGattServiceMac* gatt_service = |
| 313 GetBluetoothRemoteGattService(characteristic.service); | 313 GetBluetoothRemoteGattServiceMac(characteristic.service); |
| 314 DCHECK(gatt_service); | 314 DCHECK(gatt_service); |
| 315 gatt_service->DidUpdateValue(characteristic, error); | 315 gatt_service->DidUpdateValue(characteristic, error); |
| 316 } | 316 } |
| 317 | 317 |
| 318 void BluetoothLowEnergyDeviceMac::DidWriteValue( | 318 void BluetoothLowEnergyDeviceMac::DidWriteValue( |
| 319 CBCharacteristic* characteristic, | 319 CBCharacteristic* characteristic, |
| 320 NSError* error) { | 320 NSError* error) { |
| 321 BluetoothRemoteGattServiceMac* gatt_service = | 321 BluetoothRemoteGattServiceMac* gatt_service = |
| 322 GetBluetoothRemoteGattService(characteristic.service); | 322 GetBluetoothRemoteGattServiceMac(characteristic.service); |
| 323 DCHECK(gatt_service); | 323 DCHECK(gatt_service); |
| 324 gatt_service->DidWriteValue(characteristic, error); | 324 gatt_service->DidWriteValue(characteristic, error); |
| 325 } | 325 } |
| 326 | 326 |
| 327 void BluetoothLowEnergyDeviceMac::DidUpdateNotificationState( | 327 void BluetoothLowEnergyDeviceMac::DidUpdateNotificationState( |
| 328 CBCharacteristic* characteristic, | 328 CBCharacteristic* characteristic, |
| 329 NSError* error) { | 329 NSError* error) { |
| 330 BluetoothRemoteGattServiceMac* gatt_service = | 330 BluetoothRemoteGattServiceMac* gatt_service = |
| 331 GetBluetoothRemoteGattService(characteristic.service); | 331 GetBluetoothRemoteGattServiceMac(characteristic.service); |
| 332 DCHECK(gatt_service); | 332 DCHECK(gatt_service); |
| 333 gatt_service->DidUpdateNotificationState(characteristic, error); | 333 gatt_service->DidUpdateNotificationState(characteristic, error); |
| 334 } | 334 } |
| 335 | 335 |
| 336 void BluetoothLowEnergyDeviceMac::DidDiscoverDescriptors( | 336 void BluetoothLowEnergyDeviceMac::DidDiscoverDescriptors( |
| 337 CBCharacteristic* cb_characteristic, | 337 CBCharacteristic* cb_characteristic, |
| 338 NSError* error) { | 338 NSError* error) { |
| 339 if (error) { | 339 if (error) { |
| 340 // TODO(http://crbug.com/609320): Need to pass the error. | 340 // TODO(http://crbug.com/609320): Need to pass the error. |
| 341 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 341 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
| 342 VLOG(1) << *this << ": Can't discover descriptors: " | 342 VLOG(1) << *this << ": Can't discover descriptors: " |
| 343 << BluetoothAdapterMac::String(error); | 343 << BluetoothAdapterMac::String(error); |
| 344 return; | 344 return; |
| 345 } | 345 } |
| 346 if (!IsGattConnected()) { | 346 if (!IsGattConnected()) { |
| 347 VLOG(1) << *this << ": DidDiscoverDescriptors, disconnected."; | 347 VLOG(1) << *this << ": DidDiscoverDescriptors, disconnected."; |
| 348 // Don't discover descriptors if the device disconnected. | 348 // Don't discover descriptors if the device disconnected. |
| 349 return; | 349 return; |
| 350 } | 350 } |
| 351 if (IsGattServicesDiscoveryComplete()) { | 351 if (IsGattServicesDiscoveryComplete()) { |
| 352 // This should never happen, just in case it happens with a device, this | 352 // This should never happen, just in case it happens with a device, this |
| 353 // notification should be ignored. | 353 // notification should be ignored. |
| 354 VLOG(1) << *this | 354 VLOG(1) << *this |
| 355 << ": Discovery complete, ignoring DidDiscoverDescriptors."; | 355 << ": Discovery complete, ignoring DidDiscoverDescriptors."; |
| 356 return; | 356 return; |
| 357 } | 357 } |
| 358 BluetoothRemoteGattServiceMac* gatt_service = | 358 BluetoothRemoteGattServiceMac* gatt_service = |
| 359 GetBluetoothRemoteGattService(cb_characteristic.service); | 359 GetBluetoothRemoteGattServiceMac(cb_characteristic.service); |
| 360 DCHECK(gatt_service); | 360 DCHECK(gatt_service); |
| 361 gatt_service->DidDiscoverDescriptors(cb_characteristic); | 361 gatt_service->DidDiscoverDescriptors(cb_characteristic); |
| 362 SendNotificationIfDiscoveryComplete(); | 362 SendNotificationIfDiscoveryComplete(); |
| 363 } | 363 } |
| 364 | 364 |
| 365 void BluetoothLowEnergyDeviceMac::DidUpdateValueForDescriptor( | 365 void BluetoothLowEnergyDeviceMac::DidUpdateValueForDescriptor( |
| 366 CBDescriptor* cb_descriptor, | 366 CBDescriptor* cb_descriptor, |
| 367 NSError* error) { | 367 NSError* error) { |
| 368 BluetoothRemoteGattDescriptorMac* gatt_descriptor = | 368 BluetoothRemoteGattDescriptorMac* gatt_descriptor = |
| 369 GetBluetoothRemoteGattDescriptor(cb_descriptor); | 369 GetBluetoothRemoteGattDescriptorMac(cb_descriptor); |
| 370 DCHECK(gatt_descriptor); | 370 DCHECK(gatt_descriptor); |
| 371 gatt_descriptor->DidUpdateValueForDescriptor(error); | 371 gatt_descriptor->DidUpdateValueForDescriptor(error); |
| 372 } | 372 } |
| 373 | 373 |
| 374 void BluetoothLowEnergyDeviceMac::DidWriteValueForDescriptor( | 374 void BluetoothLowEnergyDeviceMac::DidWriteValueForDescriptor( |
| 375 CBDescriptor* cb_descriptor, | 375 CBDescriptor* cb_descriptor, |
| 376 NSError* error) { | 376 NSError* error) { |
| 377 BluetoothRemoteGattDescriptorMac* gatt_descriptor = | 377 BluetoothRemoteGattDescriptorMac* gatt_descriptor = |
| 378 GetBluetoothRemoteGattDescriptor(cb_descriptor); | 378 GetBluetoothRemoteGattDescriptorMac(cb_descriptor); |
| 379 DCHECK(gatt_descriptor); | 379 DCHECK(gatt_descriptor); |
| 380 gatt_descriptor->DidWriteValueForDescriptor(error); | 380 gatt_descriptor->DidWriteValueForDescriptor(error); |
| 381 } | 381 } |
| 382 | 382 |
| 383 // static | 383 // static |
| 384 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 384 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
| 385 CBPeripheral* peripheral) { | 385 CBPeripheral* peripheral) { |
| 386 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 386 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 387 NSUUID* uuid = [peripheral identifier]; | 387 NSUUID* uuid = [peripheral identifier]; |
| 388 NSString* uuidString = [uuid UUIDString]; | 388 NSString* uuidString = [uuid UUIDString]; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 | 444 |
| 445 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { | 445 device::BluetoothAdapterMac* BluetoothLowEnergyDeviceMac::GetMacAdapter() { |
| 446 return static_cast<BluetoothAdapterMac*>(this->adapter_); | 446 return static_cast<BluetoothAdapterMac*>(this->adapter_); |
| 447 } | 447 } |
| 448 | 448 |
| 449 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { | 449 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
| 450 return peripheral_; | 450 return peripheral_; |
| 451 } | 451 } |
| 452 | 452 |
| 453 device::BluetoothRemoteGattServiceMac* | 453 device::BluetoothRemoteGattServiceMac* |
| 454 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( | 454 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattServiceMac( |
| 455 CBService* cb_service) const { | 455 CBService* cb_service) const { |
| 456 for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) { | 456 for (auto it = gatt_services_.begin(); it != gatt_services_.end(); ++it) { |
| 457 device::BluetoothRemoteGattService* gatt_service = it->second.get(); | 457 device::BluetoothRemoteGattService* gatt_service = it->second.get(); |
| 458 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | 458 device::BluetoothRemoteGattServiceMac* gatt_service_mac = |
| 459 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | 459 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); |
| 460 if (gatt_service_mac->GetService() == cb_service) | 460 if (gatt_service_mac->GetService() == cb_service) |
| 461 return gatt_service_mac; | 461 return gatt_service_mac; |
| 462 } | 462 } |
| 463 return nullptr; | 463 return nullptr; |
| 464 } | 464 } |
| 465 | 465 |
| 466 device::BluetoothRemoteGattDescriptorMac* | 466 device::BluetoothRemoteGattDescriptorMac* |
| 467 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattDescriptor( | 467 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattDescriptorMac( |
| 468 CBDescriptor* cb_descriptor) const { | 468 CBDescriptor* cb_descriptor) const { |
| 469 CBCharacteristic* cb_characteristic = cb_descriptor.characteristic; | 469 CBCharacteristic* cb_characteristic = cb_descriptor.characteristic; |
| 470 device::BluetoothRemoteGattServiceMac* gatt_service = | 470 device::BluetoothRemoteGattServiceMac* gatt_service = |
| 471 GetBluetoothRemoteGattService(cb_characteristic.service); | 471 GetBluetoothRemoteGattServiceMac(cb_characteristic.service); |
| 472 DCHECK(gatt_service); | 472 DCHECK(gatt_service); |
| 473 device::BluetoothRemoteGattCharacteristicMac* gatt_characteristic = | 473 device::BluetoothRemoteGattCharacteristicMac* gatt_characteristic = |
| 474 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); | 474 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
| 475 DCHECK(gatt_characteristic); | 475 DCHECK(gatt_characteristic); |
| 476 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( | 476 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( |
| 477 cb_descriptor); | 477 cb_descriptor); |
| 478 } | 478 } |
| 479 | 479 |
| 480 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 480 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
| 481 connected_ = false; | 481 connected_ = false; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 512 // TODO(crbug.com/703878): Should use | 512 // TODO(crbug.com/703878): Should use |
| 513 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. | 513 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
| 514 base::Optional<std::string> name = device.GetName(); | 514 base::Optional<std::string> name = device.GetName(); |
| 515 const char* is_gatt_connected = | 515 const char* is_gatt_connected = |
| 516 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; | 516 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; |
| 517 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" | 517 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
| 518 << &device << ", " << is_gatt_connected << ", \"" | 518 << &device << ", " << is_gatt_connected << ", \"" |
| 519 << name.value_or("Unnamed device") << "\">"; | 519 << name.value_or("Unnamed device") << "\">"; |
| 520 } | 520 } |
| 521 } // namespace device | 521 } // namespace device |
| OLD | NEW |