Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: device/bluetooth/bluetooth_low_energy_discovery_manager_mac.mm

Issue 2253223002: bluetooth: Implement hardware filtering on macOS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_discovery_manager_mac.h" 5 #include "device/bluetooth/bluetooth_low_energy_discovery_manager_mac.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/mac/mac_util.h" 9 #include "base/mac/mac_util.h"
10 #include "base/mac/sdk_forward_declarations.h" 10 #include "base/mac/sdk_forward_declarations.h"
11 #include "base/strings/sys_string_conversions.h" 11 #include "base/strings/sys_string_conversions.h"
12 #include "device/bluetooth/bluetooth_adapter_mac.h" 12 #include "device/bluetooth/bluetooth_adapter_mac.h"
13 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" 13 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
14 14
15 namespace device { 15 namespace device {
16 16
17 BluetoothLowEnergyDiscoveryManagerMac:: 17 BluetoothLowEnergyDiscoveryManagerMac::
18 ~BluetoothLowEnergyDiscoveryManagerMac() { 18 ~BluetoothLowEnergyDiscoveryManagerMac() {
19 } 19 }
20 20
21 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const { 21 bool BluetoothLowEnergyDiscoveryManagerMac::IsDiscovering() const {
22 return discovering_; 22 return discovering_;
23 } 23 }
24 24
25 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery( 25 void BluetoothLowEnergyDiscoveryManagerMac::StartDiscovery(
26 BluetoothDevice::UUIDList services_uuids) { 26 std::set<BluetoothUUID> services_uuids) {
Jeffrey Yasskin 2016/08/23 21:37:04 Since this doesn't modify the input argument, it s
27 discovering_ = true; 27 // Update Scan UUIDs.
28 pending_ = true; 28 bool should_update_scan = false;
29 services_uuids_ = services_uuids; 29 for (const auto& uuid : services_uuids) {
30 TryStartDiscovery(); 30 size_t& count = scan_uuids_to_filter_count_[uuid];
31 count++;
32
33 if (count == 1) {
34 should_update_scan = true;
35 }
36 }
37
38 // Try to scan only if there are new services or we haven't
39 // started discovery yet.
40 if (should_update_scan || !discovering_) {
Jeffrey Yasskin 2016/08/23 21:37:04 If we're not discovering, but there are no service
41 pending_ = true;
42 discovering_ = true;
43 TryStartDiscovery();
44 }
31 } 45 }
32 46
33 void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() { 47 void BluetoothLowEnergyDiscoveryManagerMac::TryStartDiscovery() {
34 if (!discovering_) { 48 if (!discovering_) {
35 VLOG(1) << "TryStartDiscovery !discovering_"; 49 VLOG(1) << "TryStartDiscovery !discovering_";
36 return; 50 return;
37 } 51 }
38 52
39 if (!pending_) { 53 if (!pending_) {
40 VLOG(1) << "TryStartDiscovery !pending_"; 54 VLOG(1) << "TryStartDiscovery !pending_";
41 return; 55 return;
42 } 56 }
43 57
44 if (!central_manager_) { 58 if (!central_manager_) {
45 VLOG(1) << "TryStartDiscovery !central_manager_"; 59 VLOG(1) << "TryStartDiscovery !central_manager_";
46 return; 60 return;
47 } 61 }
48 62
49 if ([central_manager_ state] != CBCentralManagerStatePoweredOn) { 63 if ([central_manager_ state] != CBCentralManagerStatePoweredOn) {
50 VLOG(1) << "TryStartDiscovery != CBCentralManagerStatePoweredOn"; 64 VLOG(1) << "TryStartDiscovery != CBCentralManagerStatePoweredOn";
51 return; 65 return;
52 } 66 }
53 67
54 // Converts the services UUIDs to a CoreBluetooth data structure. 68 // Converts the services UUIDs to a CoreBluetooth data structure.
55 NSMutableArray* services = nil; 69 NSMutableArray* services = nil;
56 if (!services_uuids_.empty()) { 70 if (!scan_uuids_to_filter_count_.empty()) {
57 services = [NSMutableArray array]; 71 services = [NSMutableArray array];
58 for (auto& service_uuid : services_uuids_) { 72 VLOG(1) << "Scanning for UUIDs: ";
59 NSString* uuidString = 73 for (const auto& uuid_to_count : scan_uuids_to_filter_count_) {
60 base::SysUTF8ToNSString(service_uuid.canonical_value().c_str()); 74 const std::string& uuid_str = uuid_to_count.first.canonical_value();
75 VLOG(1) << uuid_str;
76 NSString* uuidString = base::SysUTF8ToNSString(uuid_str.c_str());
61 CBUUID* uuid = [CBUUID UUIDWithString:uuidString]; 77 CBUUID* uuid = [CBUUID UUIDWithString:uuidString];
62 [services addObject:uuid]; 78 [services addObject:uuid];
63 } 79 }
64 }; 80 };
65 81
66 VLOG(1) << "TryStartDiscovery scanForPeripheralsWithServices"; 82 VLOG(1) << "TryStartDiscovery scanForPeripheralsWithServices";
67 // Start a scan with the Allow Duplicates option so that we get notified 83 // Start a scan with the Allow Duplicates option so that we get notified
68 // of each new Advertisement Packet. This allows us to provide up to date 84 // of each new Advertisement Packet. This allows us to provide up to date
69 // values for RSSI, Advertised Services, Advertised Data, etc. 85 // values for RSSI, Advertised Services, Advertised Data, etc.
70 [central_manager_ 86 [central_manager_
71 scanForPeripheralsWithServices:services 87 scanForPeripheralsWithServices:services
72 options:@{ 88 options:@{
73 CBCentralManagerScanOptionAllowDuplicatesKey : 89 CBCentralManagerScanOptionAllowDuplicatesKey :
74 @YES 90 @YES
75 }]; 91 }];
76 pending_ = false; 92 pending_ = false;
77 } 93 }
78 94
95 void BluetoothLowEnergyDiscoveryManagerMac::RemoveDiscoveryUUIDs(
96 std::set<BluetoothUUID> uuids_to_remove) {
97 bool did_remove_service = false;
98 for (const auto& uuid_to_remove : uuids_to_remove) {
99 auto it = scan_uuids_to_filter_count_.find(uuid_to_remove);
100 DCHECK(it != scan_uuids_to_filter_count_.end());
101 DCHECK_LT(0u, it->second);
102 it->second--;
103 if (it->second == 0) {
104 scan_uuids_to_filter_count_.erase(it);
105 did_remove_service = true;
106 }
107 }
108 if (did_remove_service) {
109 pending_ = true;
110 TryStartDiscovery();
Jeffrey Yasskin 2016/08/23 21:37:04 Shouldn't RemoveDiscoveryUUIDs stop discovery when
111 }
112 }
113
79 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() { 114 void BluetoothLowEnergyDiscoveryManagerMac::StopDiscovery() {
80 VLOG(1) << "StopDiscovery"; 115 VLOG(1) << "StopDiscovery";
116 scan_uuids_to_filter_count_.clear();
81 if (discovering_ && !pending_) { 117 if (discovering_ && !pending_) {
82 [central_manager_ stopScan]; 118 [central_manager_ stopScan];
83 } 119 }
84 discovering_ = false; 120 discovering_ = false;
85 } 121 }
86 122
87 void BluetoothLowEnergyDiscoveryManagerMac::SetCentralManager( 123 void BluetoothLowEnergyDiscoveryManagerMac::SetCentralManager(
88 CBCentralManager* central_manager) { 124 CBCentralManager* central_manager) {
89 central_manager_ = central_manager; 125 central_manager_ = central_manager;
90 } 126 }
(...skipping 12 matching lines...) Expand all
103 } 139 }
104 140
105 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac( 141 BluetoothLowEnergyDiscoveryManagerMac::BluetoothLowEnergyDiscoveryManagerMac(
106 Observer* observer) 142 Observer* observer)
107 : observer_(observer) { 143 : observer_(observer) {
108 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); 144 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable());
109 discovering_ = false; 145 discovering_ = false;
110 } 146 }
111 147
112 } // namespace device 148 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698