Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_adapter_mac.h" | 5 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 6 | 6 |
| 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> | 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> |
| 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> | 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 #include "device/bluetooth/bluetooth_discovery_session.h" | 29 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 30 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | 30 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" |
| 31 #include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h" | 31 #include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h" |
| 32 #include "device/bluetooth/bluetooth_socket_mac.h" | 32 #include "device/bluetooth/bluetooth_socket_mac.h" |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 // The frequency with which to poll the adapter for updates. | 36 // The frequency with which to poll the adapter for updates. |
| 37 const int kPollIntervalMs = 500; | 37 const int kPollIntervalMs = 500; |
| 38 | 38 |
| 39 std::string GetCentralManagerStateString(CBCentralManagerState state) { | |
|
ortuno
2017/03/14 00:34:33
I feel bad for adding these strings to the binary
jlebel
2017/03/15 01:08:40
This should not be a big source of problems, so we
| |
| 40 switch (state) { | |
| 41 case CBCentralManagerStateUnknown: | |
| 42 return "CBCentralManagerStateUnknown"; | |
| 43 case CBCentralManagerStateResetting: | |
| 44 return "CBCentralManagerStateResetting"; | |
| 45 case CBCentralManagerStateUnsupported: | |
| 46 return "CBCentralManagerStateUnsupported"; | |
| 47 case CBCentralManagerStateUnauthorized: | |
| 48 return "CBCentralManagerStateUnauthorized"; | |
| 49 case CBCentralManagerStatePoweredOff: | |
| 50 return "CBCentralManagerStatePoweredOff"; | |
| 51 case CBCentralManagerStatePoweredOn: | |
| 52 return "CBCentralManagerStatePoweredOn"; | |
| 53 } | |
| 54 DCHECK(false); | |
| 55 return std::string("Unknown central manager state ") + std::to_string(state); | |
| 56 } | |
| 57 | |
| 39 } // namespace | 58 } // namespace |
| 40 | 59 |
| 41 namespace device { | 60 namespace device { |
| 42 | 61 |
| 43 // static | 62 // static |
| 44 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( | 63 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( |
| 45 const InitCallback& init_callback) { | 64 const InitCallback& init_callback) { |
| 46 return BluetoothAdapterMac::CreateAdapter(); | 65 return BluetoothAdapterMac::CreateAdapter(); |
| 47 } | 66 } |
| 48 | 67 |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 void BluetoothAdapterMac::LowEnergyDeviceUpdated( | 531 void BluetoothAdapterMac::LowEnergyDeviceUpdated( |
| 513 CBPeripheral* peripheral, | 532 CBPeripheral* peripheral, |
| 514 NSDictionary* advertisement_data, | 533 NSDictionary* advertisement_data, |
| 515 int rssi) { | 534 int rssi) { |
| 516 BluetoothLowEnergyDeviceMac* device_mac = | 535 BluetoothLowEnergyDeviceMac* device_mac = |
| 517 GetBluetoothLowEnergyDeviceMac(peripheral); | 536 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 518 // If has no entry in the map, create new device and insert into |devices_|, | 537 // If has no entry in the map, create new device and insert into |devices_|, |
| 519 // otherwise update the existing device. | 538 // otherwise update the existing device. |
| 520 const bool is_new_device = device_mac == nullptr; | 539 const bool is_new_device = device_mac == nullptr; |
| 521 if (is_new_device) { | 540 if (is_new_device) { |
| 522 VLOG(1) << "LowEnergyDeviceUpdated new device"; | |
| 523 // A new device has been found. | 541 // A new device has been found. |
| 524 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); | 542 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); |
| 543 VLOG(1) << device_mac->ToString() << ": New Device."; | |
| 525 } else if (DoesCollideWithKnownDevice(peripheral, device_mac)) { | 544 } else if (DoesCollideWithKnownDevice(peripheral, device_mac)) { |
| 526 return; | 545 return; |
| 527 } | 546 } |
| 528 | 547 |
| 529 DCHECK(device_mac); | 548 DCHECK(device_mac); |
| 549 VLOG(1) << device_mac->ToString() << ": Device updated with " | |
|
ortuno
2017/03/14 00:34:33
Make this a VLOG(3). We are going to get tens of t
jlebel
2017/03/15 01:08:40
Done.
| |
| 550 << base::SysNSStringToUTF8([advertisement_data description]); | |
| 530 | 551 |
| 531 // Get Advertised UUIDs | 552 // Get Advertised UUIDs |
| 532 BluetoothDevice::UUIDList advertised_uuids; | 553 BluetoothDevice::UUIDList advertised_uuids; |
| 533 NSArray* service_uuids = | 554 NSArray* service_uuids = |
| 534 [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; | 555 [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; |
| 535 for (CBUUID* uuid in service_uuids) { | 556 for (CBUUID* uuid in service_uuids) { |
| 536 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); | 557 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); |
| 537 } | 558 } |
| 538 NSArray* overflow_service_uuids = [advertisement_data | 559 NSArray* overflow_service_uuids = [advertisement_data |
| 539 objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey]; | 560 objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey]; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 569 devices_[device_address] = base::WrapUnique(device_mac); | 590 devices_[device_address] = base::WrapUnique(device_mac); |
| 570 for (auto& observer : observers_) | 591 for (auto& observer : observers_) |
| 571 observer.DeviceAdded(this, device_mac); | 592 observer.DeviceAdded(this, device_mac); |
| 572 } else { | 593 } else { |
| 573 for (auto& observer : observers_) | 594 for (auto& observer : observers_) |
| 574 observer.DeviceChanged(this, device_mac); | 595 observer.DeviceChanged(this, device_mac); |
| 575 } | 596 } |
| 576 } | 597 } |
| 577 | 598 |
| 578 // TODO(krstnmnlsn): Implement. crbug.com/511025 | 599 // TODO(krstnmnlsn): Implement. crbug.com/511025 |
| 579 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() {} | 600 void BluetoothAdapterMac::LowEnergyCentralManagerUpdatedState() { |
| 601 VLOG(1) << "Central manager state updated " | |
| 602 << GetCentralManagerStateString([low_energy_central_manager_ state]); | |
| 603 } | |
| 580 | 604 |
| 581 void BluetoothAdapterMac::AddPairedDevices() { | 605 void BluetoothAdapterMac::AddPairedDevices() { |
| 582 // Add any new paired devices. | 606 // Add any new paired devices. |
| 583 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { | 607 for (IOBluetoothDevice* device in [IOBluetoothDevice pairedDevices]) { |
| 584 // pairedDevices sometimes includes unknown devices that are not paired. | 608 // pairedDevices sometimes includes unknown devices that are not paired. |
| 585 // Radar issue with id 2282763004 has been filed about it. | 609 // Radar issue with id 2282763004 has been filed about it. |
| 586 if ([device isPaired]) { | 610 if ([device isPaired]) { |
| 587 ClassicDeviceAdded(device); | 611 ClassicDeviceAdded(device); |
| 588 } | 612 } |
| 589 } | 613 } |
| 590 } | 614 } |
| 591 | 615 |
| 592 std::vector<BluetoothDevice*> | 616 std::vector<BluetoothDevice*> |
| 593 BluetoothAdapterMac::RetrieveGattConnectedDevicesWithService( | 617 BluetoothAdapterMac::RetrieveGattConnectedDevicesWithService( |
| 594 const BluetoothUUID* uuid) { | 618 const BluetoothUUID* uuid) { |
| 595 NSArray* cbUUIDs = nil; | 619 NSArray* cbUUIDs = nil; |
| 596 if (!uuid) { | 620 if (!uuid) { |
| 597 // It is not possible to ask for all connected peripherals with | 621 // It is not possible to ask for all connected peripherals with |
| 598 // -[CBCentralManager retrieveConnectedPeripheralsWithServices:] by passing | 622 // -[CBCentralManager retrieveConnectedPeripheralsWithServices:] by passing |
| 599 // nil. To try to get most of the peripherals, the search is done with | 623 // nil. To try to get most of the peripherals, the search is done with |
| 600 // Generic Access service. | 624 // Generic Access service. |
| 601 CBUUID* genericAccessServiceUUID = [CBUUID UUIDWithString:@"1800"]; | 625 CBUUID* genericAccessServiceUUID = [CBUUID UUIDWithString:@"1800"]; |
| 602 cbUUIDs = @[ genericAccessServiceUUID ]; | 626 cbUUIDs = @[ genericAccessServiceUUID ]; |
| 603 } else { | 627 } else { |
| 604 NSString* uuidString = | 628 NSString* uuidString = |
| 605 base::SysUTF8ToNSString(uuid->canonical_value().c_str()); | 629 base::SysUTF8ToNSString(uuid->canonical_value().c_str()); |
| 606 cbUUIDs = @[ [CBUUID UUIDWithString:uuidString] ]; | 630 cbUUIDs = @[ [CBUUID UUIDWithString:uuidString] ]; |
| 607 } | 631 } |
| 632 VLOG(1) << "RetrieveGattConnectedDevicesWithService " << uuid->value(); | |
|
ortuno
2017/03/14 00:34:33
uuid could be null so let's do the logging inside
jlebel
2017/03/15 01:08:40
Done.
| |
| 608 NSArray* peripherals = [low_energy_central_manager_ | 633 NSArray* peripherals = [low_energy_central_manager_ |
| 609 retrieveConnectedPeripheralsWithServices:cbUUIDs]; | 634 retrieveConnectedPeripheralsWithServices:cbUUIDs]; |
| 610 std::vector<BluetoothDevice*> connected_devices; | 635 std::vector<BluetoothDevice*> connected_devices; |
| 611 for (CBPeripheral* peripheral in peripherals) { | 636 for (CBPeripheral* peripheral in peripherals) { |
| 612 BluetoothLowEnergyDeviceMac* device_mac = | 637 BluetoothLowEnergyDeviceMac* device_mac = |
| 613 GetBluetoothLowEnergyDeviceMac(peripheral); | 638 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 614 const bool is_new_device = device_mac == nullptr; | 639 const bool is_new_device = device_mac == nullptr; |
| 615 | 640 |
| 616 if (!is_new_device && DoesCollideWithKnownDevice(peripheral, device_mac)) { | 641 if (!is_new_device && DoesCollideWithKnownDevice(peripheral, device_mac)) { |
| 617 continue; | 642 continue; |
| 618 } | 643 } |
| 619 if (is_new_device) { | 644 if (is_new_device) { |
| 620 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); | 645 device_mac = new BluetoothLowEnergyDeviceMac(this, peripheral); |
| 621 std::string device_address = | 646 std::string device_address = |
| 622 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 647 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 623 devices_[device_address] = base::WrapUnique(device_mac); | 648 devices_[device_address] = base::WrapUnique(device_mac); |
| 624 for (auto& observer : observers_) { | 649 for (auto& observer : observers_) { |
| 625 observer.DeviceAdded(this, device_mac); | 650 observer.DeviceAdded(this, device_mac); |
| 626 } | 651 } |
| 627 } | 652 } |
| 628 connected_devices.push_back(device_mac); | 653 connected_devices.push_back(device_mac); |
| 654 VLOG(1) << " " << device_mac->ToString(); | |
|
ortuno
2017/03/14 00:34:33
Please follow the same pattern for all logs:
VLOG
jlebel
2017/03/15 01:08:40
Done.
| |
| 629 } | 655 } |
| 630 return connected_devices; | 656 return connected_devices; |
| 631 } | 657 } |
| 632 | 658 |
| 633 void BluetoothAdapterMac::CreateGattConnection( | 659 void BluetoothAdapterMac::CreateGattConnection( |
| 634 BluetoothLowEnergyDeviceMac* device_mac) { | 660 BluetoothLowEnergyDeviceMac* device_mac) { |
| 635 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ | 661 [low_energy_central_manager_ connectPeripheral:device_mac->peripheral_ |
| 636 options:nil]; | 662 options:nil]; |
| 637 } | 663 } |
| 638 | 664 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 654 } | 680 } |
| 655 | 681 |
| 656 void BluetoothAdapterMac::DidFailToConnectPeripheral(CBPeripheral* peripheral, | 682 void BluetoothAdapterMac::DidFailToConnectPeripheral(CBPeripheral* peripheral, |
| 657 NSError* error) { | 683 NSError* error) { |
| 658 BluetoothLowEnergyDeviceMac* device_mac = | 684 BluetoothLowEnergyDeviceMac* device_mac = |
| 659 GetBluetoothLowEnergyDeviceMac(peripheral); | 685 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 660 if (!device_mac) { | 686 if (!device_mac) { |
| 661 [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; | 687 [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; |
| 662 return; | 688 return; |
| 663 } | 689 } |
| 664 VLOG(1) << "Failed to connect to peripheral"; | 690 VLOG(1) << device_mac->ToString() << ": Failed to connect to peripheral."; |
| 665 BluetoothDevice::ConnectErrorCode error_code = | 691 BluetoothDevice::ConnectErrorCode error_code = |
| 666 BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN; | 692 BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN; |
| 667 if (error) { | 693 if (error) { |
| 668 error_code = BluetoothDeviceMac::GetConnectErrorCodeFromNSError(error); | 694 error_code = BluetoothDeviceMac::GetConnectErrorCodeFromNSError(error); |
| 669 VLOG(1) << "Bluetooth error, domain: " << error.domain.UTF8String | 695 VLOG(1) << device_mac->ToString() |
| 696 << ": Bluetooth error, domain: " << error.domain.UTF8String | |
| 670 << ", error code: " << error.code | 697 << ", error code: " << error.code |
| 671 << ", converted into: " << error_code; | 698 << ", converted into: " << error_code; |
| 672 } | 699 } |
| 673 device_mac->DidFailToConnectGatt(error_code); | 700 device_mac->DidFailToConnectGatt(error_code); |
| 674 } | 701 } |
| 675 | 702 |
| 676 void BluetoothAdapterMac::DidDisconnectPeripheral(CBPeripheral* peripheral, | 703 void BluetoothAdapterMac::DidDisconnectPeripheral(CBPeripheral* peripheral, |
| 677 NSError* error) { | 704 NSError* error) { |
| 678 BluetoothLowEnergyDeviceMac* device_mac = | 705 BluetoothLowEnergyDeviceMac* device_mac = |
| 679 GetBluetoothLowEnergyDeviceMac(peripheral); | 706 GetBluetoothLowEnergyDeviceMac(peripheral); |
| 680 if (!device_mac) { | 707 if (!device_mac) { |
| 681 [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; | 708 [low_energy_central_manager_ cancelPeripheralConnection:peripheral]; |
| 682 return; | 709 return; |
| 683 } | 710 } |
| 684 VLOG(1) << "Disconnected from peripheral."; | 711 VLOG(1) << device_mac->ToString() << ": Disconnected from peripheral."; |
| 685 if (error) { | 712 if (error) { |
| 686 VLOG(1) << "Bluetooth error, domain: " << error.domain.UTF8String | 713 VLOG(1) << device_mac->ToString() |
| 714 << ": Bluetooth error, domain: " << error.domain.UTF8String | |
| 687 << ", error code: " << error.code; | 715 << ", error code: " << error.code; |
| 688 } | 716 } |
| 689 device_mac->DidDisconnectPeripheral(error); | 717 device_mac->DidDisconnectPeripheral(error); |
| 690 } | 718 } |
| 691 | 719 |
| 692 BluetoothLowEnergyDeviceMac* | 720 BluetoothLowEnergyDeviceMac* |
| 693 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { | 721 BluetoothAdapterMac::GetBluetoothLowEnergyDeviceMac(CBPeripheral* peripheral) { |
| 694 std::string device_address = | 722 std::string device_address = |
| 695 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 723 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 696 auto iter = devices_.find(device_address); | 724 auto iter = devices_.find(device_address); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 716 // hash the probability of this occuring with 10,000 devices | 744 // hash the probability of this occuring with 10,000 devices |
| 717 // simultaneously present is 1e-6 (see | 745 // simultaneously present is 1e-6 (see |
| 718 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We | 746 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We |
| 719 // ignore the second device by returning. | 747 // ignore the second device by returning. |
| 720 return true; | 748 return true; |
| 721 } | 749 } |
| 722 return false; | 750 return false; |
| 723 } | 751 } |
| 724 | 752 |
| 725 } // namespace device | 753 } // namespace device |
| OLD | NEW |