OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chromeos/components/tether/ble_scanner.h" | 5 #include "chromeos/components/tether/ble_scanner.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "chromeos/components/tether/ble_constants.h" | 10 #include "chromeos/components/tether/ble_constants.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 | 38 |
39 for (size_t i = 0; i < string.size(); i++) { | 39 for (size_t i = 0; i < string.size(); i++) { |
40 ss << static_cast<int>(string.data()[i]); | 40 ss << static_cast<int>(string.data()[i]); |
41 } | 41 } |
42 | 42 |
43 return ss.str(); | 43 return ss.str(); |
44 } | 44 } |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 BleScanner::DelegateImpl::DelegateImpl() {} | 48 BleScanner::ServiceDataProviderImpl::ServiceDataProviderImpl() {} |
49 | 49 |
50 BleScanner::DelegateImpl::~DelegateImpl() {} | 50 BleScanner::ServiceDataProviderImpl::~ServiceDataProviderImpl() {} |
51 | 51 |
52 bool BleScanner::DelegateImpl::IsBluetoothAdapterAvailable() const { | 52 const std::vector<uint8_t>* |
53 return device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 53 BleScanner::ServiceDataProviderImpl::GetServiceDataForUUID( |
54 } | |
55 | |
56 void BleScanner::DelegateImpl::GetAdapter( | |
57 const device::BluetoothAdapterFactory::AdapterCallback& callback) { | |
58 device::BluetoothAdapterFactory::GetAdapter(callback); | |
59 } | |
60 | |
61 const std::vector<uint8_t>* BleScanner::DelegateImpl::GetServiceDataForUUID( | |
62 const device::BluetoothUUID& service_uuid, | |
63 device::BluetoothDevice* bluetooth_device) { | 54 device::BluetoothDevice* bluetooth_device) { |
64 return bluetooth_device->GetServiceDataForUUID(service_uuid); | 55 return bluetooth_device->GetServiceDataForUUID( |
| 56 device::BluetoothUUID(kAdvertisingServiceUuid)); |
65 } | 57 } |
66 | 58 |
67 BleScanner::BleScanner( | 59 BleScanner::BleScanner( |
| 60 scoped_refptr<device::BluetoothAdapter> adapter, |
68 const LocalDeviceDataProvider* local_device_data_provider) | 61 const LocalDeviceDataProvider* local_device_data_provider) |
69 : BleScanner(base::MakeUnique<DelegateImpl>(), | 62 : BleScanner(base::MakeUnique<ServiceDataProviderImpl>(), |
| 63 adapter, |
70 cryptauth::EidGenerator::GetInstance(), | 64 cryptauth::EidGenerator::GetInstance(), |
71 local_device_data_provider) {} | 65 local_device_data_provider) {} |
72 | 66 |
73 BleScanner::~BleScanner() {} | |
74 | |
75 BleScanner::BleScanner( | 67 BleScanner::BleScanner( |
76 std::unique_ptr<Delegate> delegate, | 68 std::unique_ptr<ServiceDataProvider> service_data_provider, |
| 69 scoped_refptr<device::BluetoothAdapter> adapter, |
77 const cryptauth::EidGenerator* eid_generator, | 70 const cryptauth::EidGenerator* eid_generator, |
78 const LocalDeviceDataProvider* local_device_data_provider) | 71 const LocalDeviceDataProvider* local_device_data_provider) |
79 : delegate_(std::move(delegate)), | 72 : service_data_provider_(std::move(service_data_provider)), |
| 73 adapter_(adapter), |
80 eid_generator_(eid_generator), | 74 eid_generator_(eid_generator), |
81 local_device_data_provider_(local_device_data_provider), | 75 local_device_data_provider_(local_device_data_provider), |
82 is_initializing_adapter_(false), | |
83 is_initializing_discovery_session_(false), | 76 is_initializing_discovery_session_(false), |
84 discovery_session_(nullptr), | 77 discovery_session_(nullptr), |
85 weak_ptr_factory_(this) {} | 78 weak_ptr_factory_(this) { |
| 79 adapter_->AddObserver(this); |
| 80 } |
| 81 |
| 82 BleScanner::~BleScanner() { |
| 83 adapter_->RemoveObserver(this); |
| 84 } |
86 | 85 |
87 bool BleScanner::RegisterScanFilterForDevice( | 86 bool BleScanner::RegisterScanFilterForDevice( |
88 const cryptauth::RemoteDevice& remote_device) { | 87 const cryptauth::RemoteDevice& remote_device) { |
89 if (!delegate_->IsBluetoothAdapterAvailable()) { | |
90 PA_LOG(ERROR) << "Bluetooth is not supported on this platform."; | |
91 return false; | |
92 } | |
93 | |
94 if (registered_remote_devices_.size() >= kMaxConcurrentAdvertisements) { | 88 if (registered_remote_devices_.size() >= kMaxConcurrentAdvertisements) { |
95 // Each scan filter corresponds to an advertisement. Thus, the number of | 89 // Each scan filter corresponds to an advertisement. Thus, the number of |
96 // concurrent advertisements cannot exceed the maximum number of concurrent | 90 // concurrent advertisements cannot exceed the maximum number of concurrent |
97 // advertisements. | 91 // advertisements. |
| 92 PA_LOG(WARNING) << "Attempted to start a scan for a new device when the " |
| 93 << "maximum number of devices have already been " |
| 94 << "registered."; |
98 return false; | 95 return false; |
99 } | 96 } |
100 | 97 |
101 std::vector<cryptauth::BeaconSeed> local_device_beacon_seeds; | 98 std::vector<cryptauth::BeaconSeed> local_device_beacon_seeds; |
102 if (!local_device_data_provider_->GetLocalDeviceData( | 99 if (!local_device_data_provider_->GetLocalDeviceData( |
103 nullptr, &local_device_beacon_seeds)) { | 100 nullptr, &local_device_beacon_seeds)) { |
104 // If the local device's beacon seeds could not be fetched, a scan filter | 101 PA_LOG(WARNING) << "Error fetching the local device's beacon seeds. Cannot " |
105 // cannot be generated. | 102 << "generate scan without beacon seeds."; |
106 return false; | 103 return false; |
107 } | 104 } |
108 | 105 |
109 std::unique_ptr<cryptauth::EidGenerator::EidData> scan_filters = | 106 std::unique_ptr<cryptauth::EidGenerator::EidData> scan_filters = |
110 eid_generator_->GenerateBackgroundScanFilter(local_device_beacon_seeds); | 107 eid_generator_->GenerateBackgroundScanFilter(local_device_beacon_seeds); |
111 if (!scan_filters) { | 108 if (!scan_filters) { |
112 // If a background scan filter cannot be generated, give up. | 109 PA_LOG(WARNING) << "Error generating background scan filters. Cannot scan"; |
113 return false; | 110 return false; |
114 } | 111 } |
115 | 112 |
116 registered_remote_devices_.push_back(remote_device); | 113 registered_remote_devices_.push_back(remote_device); |
117 UpdateDiscoveryStatus(); | 114 UpdateDiscoveryStatus(); |
118 | 115 |
119 return true; | 116 return true; |
120 } | 117 } |
121 | 118 |
122 bool BleScanner::UnregisterScanFilterForDevice( | 119 bool BleScanner::UnregisterScanFilterForDevice( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 DCHECK_EQ(adapter_.get(), adapter); | 167 DCHECK_EQ(adapter_.get(), adapter); |
171 HandleDeviceUpdated(bluetooth_device); | 168 HandleDeviceUpdated(bluetooth_device); |
172 } | 169 } |
173 | 170 |
174 void BleScanner::UpdateDiscoveryStatus() { | 171 void BleScanner::UpdateDiscoveryStatus() { |
175 if (registered_remote_devices_.empty()) { | 172 if (registered_remote_devices_.empty()) { |
176 StopDiscoverySession(); | 173 StopDiscoverySession(); |
177 return; | 174 return; |
178 } | 175 } |
179 | 176 |
180 if (is_initializing_adapter_) { | |
181 return; | |
182 } else if (!adapter_) { | |
183 InitializeBluetoothAdapter(); | |
184 return; | |
185 } | |
186 | |
187 if (!adapter_->IsPowered()) { | 177 if (!adapter_->IsPowered()) { |
188 // If the adapter has powered off, no devices can be discovered. | 178 // If the adapter has powered off, no devices can be discovered. |
189 StopDiscoverySession(); | 179 StopDiscoverySession(); |
190 return; | 180 return; |
191 } | 181 } |
192 | 182 |
193 if (is_initializing_discovery_session_) { | 183 if (is_initializing_discovery_session_) { |
194 return; | 184 return; |
195 } else if (!discovery_session_ || | 185 } else if (!discovery_session_ || |
196 (discovery_session_ && !discovery_session_->IsActive())) { | 186 (discovery_session_ && !discovery_session_->IsActive())) { |
197 StartDiscoverySession(); | 187 StartDiscoverySession(); |
198 } | 188 } |
199 } | 189 } |
200 | 190 |
201 void BleScanner::InitializeBluetoothAdapter() { | |
202 PA_LOG(INFO) << "Initializing Bluetooth adapter."; | |
203 is_initializing_adapter_ = true; | |
204 delegate_->GetAdapter(base::Bind(&BleScanner::OnAdapterInitialized, | |
205 weak_ptr_factory_.GetWeakPtr())); | |
206 } | |
207 | |
208 void BleScanner::OnAdapterInitialized( | |
209 scoped_refptr<device::BluetoothAdapter> adapter) { | |
210 DCHECK(is_initializing_adapter_ && !discovery_session_ && | |
211 !is_initializing_discovery_session_); | |
212 PA_LOG(INFO) << "Bluetooth adapter initialized."; | |
213 is_initializing_adapter_ = false; | |
214 | |
215 adapter_ = adapter; | |
216 adapter_->AddObserver(this); | |
217 | |
218 UpdateDiscoveryStatus(); | |
219 } | |
220 | |
221 void BleScanner::StartDiscoverySession() { | 191 void BleScanner::StartDiscoverySession() { |
222 DCHECK(adapter_); | 192 DCHECK(adapter_); |
223 PA_LOG(INFO) << "Starting discovery session."; | 193 PA_LOG(INFO) << "Starting discovery session."; |
224 is_initializing_discovery_session_ = true; | 194 is_initializing_discovery_session_ = true; |
225 | 195 |
226 // Discover only low energy (LE) devices with strong enough signal. | 196 // Discover only low energy (LE) devices with strong enough signal. |
227 std::unique_ptr<device::BluetoothDiscoveryFilter> filter = | 197 std::unique_ptr<device::BluetoothDiscoveryFilter> filter = |
228 base::MakeUnique<device::BluetoothDiscoveryFilter>( | 198 base::MakeUnique<device::BluetoothDiscoveryFilter>( |
229 device::BLUETOOTH_TRANSPORT_LE); | 199 device::BLUETOOTH_TRANSPORT_LE); |
230 filter->SetRSSI(kMinDiscoveryRSSI); | 200 filter->SetRSSI(kMinDiscoveryRSSI); |
(...skipping 24 matching lines...) Expand all Loading... |
255 } | 225 } |
256 | 226 |
257 PA_LOG(WARNING) << "Stopping discovery session."; | 227 PA_LOG(WARNING) << "Stopping discovery session."; |
258 discovery_session_.reset(); | 228 discovery_session_.reset(); |
259 } | 229 } |
260 | 230 |
261 void BleScanner::HandleDeviceUpdated( | 231 void BleScanner::HandleDeviceUpdated( |
262 device::BluetoothDevice* bluetooth_device) { | 232 device::BluetoothDevice* bluetooth_device) { |
263 DCHECK(bluetooth_device); | 233 DCHECK(bluetooth_device); |
264 | 234 |
265 const std::vector<uint8_t>* service_data = delegate_->GetServiceDataForUUID( | 235 const std::vector<uint8_t>* service_data = |
266 device::BluetoothUUID(kAdvertisingServiceUuid), bluetooth_device); | 236 service_data_provider_->GetServiceDataForUUID(bluetooth_device); |
267 if (!service_data || service_data->size() < kMinNumBytesInServiceData) { | 237 if (!service_data || service_data->size() < kMinNumBytesInServiceData) { |
268 // If there is no service data or the service data is of insufficient | 238 // If there is no service data or the service data is of insufficient |
269 // length, there is not enough information to create a connection. | 239 // length, there is not enough information to create a connection. |
270 return; | 240 return; |
271 } | 241 } |
272 | 242 |
273 // Convert the service data from a std::vector<uint8_t> to a std::string. | 243 // Convert the service data from a std::vector<uint8_t> to a std::string. |
274 std::string service_data_str; | 244 std::string service_data_str; |
275 char* string_contents_ptr = | 245 char* string_contents_ptr = |
276 base::WriteInto(&service_data_str, service_data->size() + 1); | 246 base::WriteInto(&service_data_str, service_data->size() + 1); |
(...skipping 26 matching lines...) Expand all Loading... |
303 } else { | 273 } else { |
304 PA_LOG(INFO) << "Received advertisement remote device, but could not " | 274 PA_LOG(INFO) << "Received advertisement remote device, but could not " |
305 << "identify the device. Service data: " | 275 << "identify the device. Service data: " |
306 << StringToHexOfContents(service_data) << "."; | 276 << StringToHexOfContents(service_data) << "."; |
307 } | 277 } |
308 } | 278 } |
309 | 279 |
310 } // namespace tether | 280 } // namespace tether |
311 | 281 |
312 } // namespace chromeos | 282 } // namespace chromeos |
OLD | NEW |