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 "components/proximity_auth/proximity_monitor_impl.h" | 5 #include "components/proximity_auth/proximity_monitor_impl.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "base/time/tick_clock.h" | 13 #include "base/time/tick_clock.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "components/proximity_auth/logging/logging.h" | 15 #include "components/proximity_auth/logging/logging.h" |
16 #include "components/proximity_auth/metrics.h" | 16 #include "components/proximity_auth/metrics.h" |
| 17 #include "components/proximity_auth/proximity_auth_pref_manager.h" |
17 #include "components/proximity_auth/proximity_monitor_observer.h" | 18 #include "components/proximity_auth/proximity_monitor_observer.h" |
18 #include "device/bluetooth/bluetooth_adapter.h" | 19 #include "device/bluetooth/bluetooth_adapter.h" |
19 #include "device/bluetooth/bluetooth_adapter_factory.h" | 20 #include "device/bluetooth/bluetooth_adapter_factory.h" |
20 | 21 |
21 using device::BluetoothDevice; | 22 using device::BluetoothDevice; |
22 | 23 |
23 namespace proximity_auth { | 24 namespace proximity_auth { |
24 | 25 |
25 // The time to wait, in milliseconds, between proximity polling iterations. | 26 // The time to wait, in milliseconds, between proximity polling iterations. |
26 const int kPollingTimeoutMs = 250; | 27 const int kPollingTimeoutMs = 250; |
27 | 28 |
28 // The RSSI threshold below which we consider the remote device to not be in | |
29 // proximity. | |
30 // Note: Because this threshold is unconfigurable right now, it is set quite | |
31 // conservatively in order to avoid blocking users. | |
32 const int kRssiThreshold = -70; | |
33 | |
34 // The weight of the most recent RSSI sample. | 29 // The weight of the most recent RSSI sample. |
35 const double kRssiSampleWeight = 0.3; | 30 const double kRssiSampleWeight = 0.3; |
36 | 31 |
| 32 // These are arbitrary labels displayed in the settings page for the user |
| 33 // to select. The correspondence to actual RSSI values is done in the |
| 34 // GetRssiThresholdFromPrefs(). |
| 35 enum ProximityThreshold { kVeryClose = 0, kClose = 1, kFar = 2, kVeryFar = 3 }; |
| 36 |
37 ProximityMonitorImpl::ProximityMonitorImpl( | 37 ProximityMonitorImpl::ProximityMonitorImpl( |
38 cryptauth::Connection* connection, | 38 cryptauth::Connection* connection, |
39 std::unique_ptr<base::TickClock> clock) | 39 std::unique_ptr<base::TickClock> clock, |
| 40 std::unique_ptr<ProximityAuthPrefManager> pref_manager) |
40 : connection_(connection), | 41 : connection_(connection), |
41 remote_device_is_in_proximity_(false), | 42 remote_device_is_in_proximity_(false), |
42 is_active_(false), | 43 is_active_(false), |
43 clock_(std::move(clock)), | 44 clock_(std::move(clock)), |
| 45 pref_manager_(std::move(pref_manager)), |
44 polling_weak_ptr_factory_(this), | 46 polling_weak_ptr_factory_(this), |
45 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
46 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { | 48 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { |
47 device::BluetoothAdapterFactory::GetAdapter( | 49 device::BluetoothAdapterFactory::GetAdapter( |
48 base::Bind(&ProximityMonitorImpl::OnAdapterInitialized, | 50 base::Bind(&ProximityMonitorImpl::OnAdapterInitialized, |
49 weak_ptr_factory_.GetWeakPtr())); | 51 weak_ptr_factory_.GetWeakPtr())); |
50 } else { | 52 } else { |
51 PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " | 53 PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " |
52 << "Bluetooth is unsupported on this platform."; | 54 << "Bluetooth is unsupported on this platform."; |
53 } | 55 } |
54 } | 56 } |
55 | 57 |
56 ProximityMonitorImpl::~ProximityMonitorImpl() { | 58 ProximityMonitorImpl::~ProximityMonitorImpl() { |
57 } | 59 } |
58 | 60 |
59 void ProximityMonitorImpl::Start() { | 61 void ProximityMonitorImpl::Start() { |
60 is_active_ = true; | 62 is_active_ = true; |
| 63 GetRssiThresholdFromPrefs(); |
61 UpdatePollingState(); | 64 UpdatePollingState(); |
62 } | 65 } |
63 | 66 |
64 void ProximityMonitorImpl::Stop() { | 67 void ProximityMonitorImpl::Stop() { |
65 is_active_ = false; | 68 is_active_ = false; |
66 ClearProximityState(); | 69 ClearProximityState(); |
67 UpdatePollingState(); | 70 UpdatePollingState(); |
68 } | 71 } |
69 | 72 |
70 bool ProximityMonitorImpl::IsUnlockAllowed() const { | 73 bool ProximityMonitorImpl::IsUnlockAllowed() const { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } else { | 188 } else { |
186 *rssi_rolling_average_ = | 189 *rssi_rolling_average_ = |
187 weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_); | 190 weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_); |
188 } | 191 } |
189 | 192 |
190 CheckForProximityStateChange(); | 193 CheckForProximityStateChange(); |
191 } | 194 } |
192 | 195 |
193 void ProximityMonitorImpl::CheckForProximityStateChange() { | 196 void ProximityMonitorImpl::CheckForProximityStateChange() { |
194 bool is_now_in_proximity = | 197 bool is_now_in_proximity = |
195 rssi_rolling_average_ && *rssi_rolling_average_ > kRssiThreshold; | 198 rssi_rolling_average_ && *rssi_rolling_average_ > rssi_threshold_; |
196 | 199 |
197 if (rssi_rolling_average_) | 200 if (rssi_rolling_average_) |
198 PA_LOG(INFO) << " Rolling RSSI: " << *rssi_rolling_average_; | 201 PA_LOG(INFO) << " Rolling RSSI: " << *rssi_rolling_average_; |
199 | 202 |
200 if (remote_device_is_in_proximity_ != is_now_in_proximity) { | 203 if (remote_device_is_in_proximity_ != is_now_in_proximity) { |
201 PA_LOG(INFO) << "[Proximity] Updated proximity state: " | 204 PA_LOG(INFO) << "[Proximity] Updated proximity state: " |
202 << (is_now_in_proximity ? "proximate" : "distant"); | 205 << (is_now_in_proximity ? "proximate" : "distant"); |
203 remote_device_is_in_proximity_ = is_now_in_proximity; | 206 remote_device_is_in_proximity_ = is_now_in_proximity; |
204 for (auto& observer : observers_) | 207 for (auto& observer : observers_) |
205 observer.OnProximityStateChanged(); | 208 observer.OnProximityStateChanged(); |
206 } | 209 } |
207 } | 210 } |
208 | 211 |
| 212 void ProximityMonitorImpl::GetRssiThresholdFromPrefs() { |
| 213 int pref_value = pref_manager_->GetProximityThreshold(); |
| 214 ProximityThreshold threshold = static_cast<ProximityThreshold>(pref_value); |
| 215 switch (threshold) { |
| 216 case ProximityThreshold::kVeryClose: |
| 217 rssi_threshold_ = -45; |
| 218 return; |
| 219 case ProximityThreshold::kClose: |
| 220 rssi_threshold_ = -60; |
| 221 return; |
| 222 case ProximityThreshold::kFar: |
| 223 rssi_threshold_ = -70; |
| 224 return; |
| 225 case ProximityThreshold::kVeryFar: |
| 226 rssi_threshold_ = -85; |
| 227 return; |
| 228 default: |
| 229 PA_LOG(WARNING) << "[Proximity] Unmapped proximityThreshold: " |
| 230 << threshold; |
| 231 rssi_threshold_ = -70; |
| 232 return; |
| 233 } |
| 234 } |
| 235 |
209 } // namespace proximity_auth | 236 } // namespace proximity_auth |
OLD | NEW |