Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/privet/wifi_bootstrap_manager.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 6 | |
| 7 | #include <base/logging.h> |
| 8 | #include <base/memory/weak_ptr.h> |
Vitaly Buka | 7b382ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 9 | #include <weave/enum_to_string.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 10 | #include <weave/provider/network.h> |
| 11 | #include <weave/provider/task_runner.h> |
| 12 | #include <weave/provider/wifi.h> |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 13 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 14 | #include "src/bind_lambda.h" |
| 15 | #include "src/privet/constants.h" |
| 16 | #include "src/config.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 17 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 18 | namespace weave { |
| 19 | namespace privet { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 20 | |
Vitaly Buka | c8ba228 | 2015-10-01 17:42:40 -0700 | [diff] [blame] | 21 | namespace { |
| 22 | |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 23 | const int kMonitoringWithSsidTimeoutSeconds = 15; |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 24 | const int kMonitoringTimeoutSeconds = 120; |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 25 | const int kBootstrapTimeoutSeconds = 600; |
| 26 | const int kConnectingTimeoutSeconds = 180; |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 27 | |
Vitaly Buka | c8ba228 | 2015-10-01 17:42:40 -0700 | [diff] [blame] | 28 | const EnumToStringMap<WifiBootstrapManager::State>::Map kWifiSetupStateMap[] = { |
| 29 | {WifiBootstrapManager::State::kDisabled, "disabled"}, |
| 30 | {WifiBootstrapManager::State::kBootstrapping, "waiting"}, |
| 31 | {WifiBootstrapManager::State::kMonitoring, "monitoring"}, |
| 32 | {WifiBootstrapManager::State::kConnecting, "connecting"}, |
| 33 | }; |
| 34 | } |
| 35 | |
Vitaly Buka | 35f317d | 2015-09-27 22:54:39 -0700 | [diff] [blame] | 36 | using provider::Network; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 37 | |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 38 | WifiBootstrapManager::WifiBootstrapManager(Config* config, |
Vitaly Buka | f86b4d5 | 2015-09-28 15:12:03 -0700 | [diff] [blame] | 39 | provider::TaskRunner* task_runner, |
| 40 | provider::Network* network, |
| 41 | provider::Wifi* wifi, |
| 42 | CloudDelegate* gcd) |
| 43 | : config_{config}, |
| 44 | task_runner_{task_runner}, |
Vitaly Buka | f9630fb | 2015-08-12 21:15:40 -0700 | [diff] [blame] | 45 | network_{network}, |
Vitaly Buka | b1041e7 | 2015-09-21 15:26:51 -0700 | [diff] [blame] | 46 | wifi_{wifi}, |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 47 | ssid_generator_{gcd, this} { |
| 48 | CHECK(config_); |
Vitaly Buka | b1041e7 | 2015-09-21 15:26:51 -0700 | [diff] [blame] | 49 | CHECK(network_); |
| 50 | CHECK(task_runner_); |
| 51 | CHECK(wifi_); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void WifiBootstrapManager::Init() { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 55 | UpdateConnectionState(); |
Vitaly Buka | 3ab6f6e | 2015-09-24 13:16:16 -0700 | [diff] [blame] | 56 | network_->AddConnectionChangedCallback( |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 57 | base::Bind(&WifiBootstrapManager::OnConnectivityChange, |
| 58 | lifetime_weak_factory_.GetWeakPtr())); |
Vitaly Buka | f86b4d5 | 2015-09-28 15:12:03 -0700 | [diff] [blame] | 59 | if (config_->GetSettings().last_configured_ssid.empty()) { |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 60 | // Give implementation some time to figure out state. |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 61 | StartMonitoring( |
| 62 | base::TimeDelta::FromSeconds(kMonitoringWithSsidTimeoutSeconds)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 63 | } else { |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 64 | StartMonitoring(base::TimeDelta::FromSeconds(kMonitoringTimeoutSeconds)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 65 | } |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 68 | void WifiBootstrapManager::StartBootstrapping() { |
Vitaly Buka | efad5b2 | 2015-10-08 10:02:14 -0700 | [diff] [blame] | 69 | if (network_->GetConnectionState() == Network::State::kOnline) { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 70 | // If one of the devices we monitor for connectivity is online, we need not |
| 71 | // start an AP. For most devices, this is a situation which happens in |
| 72 | // testing when we have an ethernet connection. If you need to always |
| 73 | // start an AP to bootstrap WiFi credentials, then add your WiFi interface |
| 74 | // to the device whitelist. |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 75 | StartMonitoring(base::TimeDelta::FromSeconds(kMonitoringTimeoutSeconds)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 79 | UpdateState(State::kBootstrapping); |
Vitaly Buka | f86b4d5 | 2015-09-28 15:12:03 -0700 | [diff] [blame] | 80 | if (!config_->GetSettings().last_configured_ssid.empty()) { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 81 | // If we have been configured before, we'd like to periodically take down |
| 82 | // our AP and find out if we can connect again. Many kinds of failures are |
| 83 | // transient, and having an AP up prohibits us from connecting as a client. |
Vitaly Buka | f9630fb | 2015-08-12 21:15:40 -0700 | [diff] [blame] | 84 | task_runner_->PostDelayedTask( |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 85 | FROM_HERE, base::Bind(&WifiBootstrapManager::OnBootstrapTimeout, |
| 86 | tasks_weak_factory_.GetWeakPtr()), |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 87 | base::TimeDelta::FromSeconds(kBootstrapTimeoutSeconds)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 88 | } |
| 89 | // TODO(vitalybuka): Add SSID probing. |
Vitaly Buka | b6c8a3e | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 90 | privet_ssid_ = GenerateSsid(); |
| 91 | CHECK(!privet_ssid_.empty()); |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 92 | |
| 93 | VLOG(1) << "Starting AP with SSID: " << privet_ssid_; |
Vitaly Buka | 1fd619a | 2015-09-24 11:46:05 -0700 | [diff] [blame] | 94 | wifi_->StartAccessPoint(privet_ssid_); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void WifiBootstrapManager::EndBootstrapping() { |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 98 | VLOG(1) << "Stopping AP"; |
Vitaly Buka | 1fd619a | 2015-09-24 11:46:05 -0700 | [diff] [blame] | 99 | wifi_->StopAccessPoint(); |
Vitaly Buka | b6c8a3e | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 100 | privet_ssid_.clear(); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void WifiBootstrapManager::StartConnecting(const std::string& ssid, |
| 104 | const std::string& passphrase) { |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 105 | VLOG(1) << "Attempting connect to SSID:" << ssid; |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 106 | UpdateState(State::kConnecting); |
Vitaly Buka | f9630fb | 2015-08-12 21:15:40 -0700 | [diff] [blame] | 107 | task_runner_->PostDelayedTask( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 108 | FROM_HERE, base::Bind(&WifiBootstrapManager::OnConnectTimeout, |
| 109 | tasks_weak_factory_.GetWeakPtr()), |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 110 | base::TimeDelta::FromSeconds(kConnectingTimeoutSeconds)); |
Vitaly Buka | 1fd619a | 2015-09-24 11:46:05 -0700 | [diff] [blame] | 111 | wifi_->Connect(ssid, passphrase, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 112 | base::Bind(&WifiBootstrapManager::OnConnectDone, |
| 113 | tasks_weak_factory_.GetWeakPtr(), ssid)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Vitaly Buka | 4ebd329 | 2015-09-23 18:04:17 -0700 | [diff] [blame] | 116 | void WifiBootstrapManager::EndConnecting() {} |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 117 | |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 118 | void WifiBootstrapManager::StartMonitoring(const base::TimeDelta& timeout) { |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 119 | monitor_until_ = {}; |
| 120 | ContinueMonitoring(timeout); |
| 121 | } |
| 122 | |
| 123 | void WifiBootstrapManager::ContinueMonitoring(const base::TimeDelta& timeout) { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 124 | VLOG(1) << "Monitoring connectivity."; |
Vitaly Buka | 387b4f4 | 2015-07-30 23:55:13 -0700 | [diff] [blame] | 125 | // We already have a callback in place with |network_| to update our |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 126 | // connectivity state. See OnConnectivityChange(). |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 127 | UpdateState(State::kMonitoring); |
Vitaly Buka | ca365fb | 2015-09-15 17:38:41 -0700 | [diff] [blame] | 128 | |
Vitaly Buka | efad5b2 | 2015-10-08 10:02:14 -0700 | [diff] [blame] | 129 | if (network_->GetConnectionState() == Network::State::kOnline) { |
Vitaly Buka | caf42bd | 2015-09-16 11:23:23 -0700 | [diff] [blame] | 130 | monitor_until_ = {}; |
Vitaly Buka | ca365fb | 2015-09-15 17:38:41 -0700 | [diff] [blame] | 131 | } else { |
Vitaly Buka | caf42bd | 2015-09-16 11:23:23 -0700 | [diff] [blame] | 132 | if (monitor_until_.is_null()) { |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 133 | monitor_until_ = base::Time::Now() + timeout; |
Vitaly Buka | caf42bd | 2015-09-16 11:23:23 -0700 | [diff] [blame] | 134 | VLOG(2) << "Waiting for connection until: " << monitor_until_; |
| 135 | } |
Vitaly Buka | ca365fb | 2015-09-15 17:38:41 -0700 | [diff] [blame] | 136 | |
| 137 | // Schedule timeout timer taking into account already offline time. |
| 138 | task_runner_->PostDelayedTask( |
| 139 | FROM_HERE, base::Bind(&WifiBootstrapManager::OnMonitorTimeout, |
| 140 | tasks_weak_factory_.GetWeakPtr()), |
| 141 | monitor_until_ - base::Time::Now()); |
| 142 | } |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Vitaly Buka | 4ebd329 | 2015-09-23 18:04:17 -0700 | [diff] [blame] | 145 | void WifiBootstrapManager::EndMonitoring() {} |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 146 | |
| 147 | void WifiBootstrapManager::UpdateState(State new_state) { |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 148 | VLOG(3) << "Switching state from " << EnumToString(state_) << " to " |
| 149 | << EnumToString(new_state); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 150 | // Abort irrelevant tasks. |
| 151 | tasks_weak_factory_.InvalidateWeakPtrs(); |
| 152 | |
| 153 | switch (state_) { |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 154 | case State::kDisabled: |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 155 | break; |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 156 | case State::kBootstrapping: |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 157 | EndBootstrapping(); |
| 158 | break; |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 159 | case State::kMonitoring: |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 160 | EndMonitoring(); |
| 161 | break; |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 162 | case State::kConnecting: |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 163 | EndConnecting(); |
| 164 | break; |
| 165 | } |
| 166 | |
Vitaly Buka | f86b4d5 | 2015-09-28 15:12:03 -0700 | [diff] [blame] | 167 | state_ = new_state; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Vitaly Buka | 557d452 | 2015-07-18 20:43:58 -0700 | [diff] [blame] | 170 | std::string WifiBootstrapManager::GenerateSsid() const { |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 171 | const std::string& ssid = config_->GetSettings().test_privet_ssid; |
| 172 | return ssid.empty() ? ssid_generator_.GenerateSsid() : ssid; |
Vitaly Buka | 557d452 | 2015-07-18 20:43:58 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 175 | const ConnectionState& WifiBootstrapManager::GetConnectionState() const { |
| 176 | return connection_state_; |
| 177 | } |
| 178 | |
| 179 | const SetupState& WifiBootstrapManager::GetSetupState() const { |
| 180 | return setup_state_; |
| 181 | } |
| 182 | |
| 183 | bool WifiBootstrapManager::ConfigureCredentials(const std::string& ssid, |
| 184 | const std::string& passphrase, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 185 | ErrorPtr* error) { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 186 | setup_state_ = SetupState{SetupState::kInProgress}; |
Alex Vakulenko | 464f93e | 2015-09-11 15:32:39 -0700 | [diff] [blame] | 187 | // Since we are changing network, we need to let the web server send out the |
| 188 | // response to the HTTP request leading to this action. So, we are waiting |
| 189 | // a bit before mocking with network set up. |
Vitaly Buka | f9630fb | 2015-08-12 21:15:40 -0700 | [diff] [blame] | 190 | task_runner_->PostDelayedTask( |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 191 | FROM_HERE, base::Bind(&WifiBootstrapManager::StartConnecting, |
| 192 | tasks_weak_factory_.GetWeakPtr(), ssid, passphrase), |
Alex Vakulenko | 464f93e | 2015-09-11 15:32:39 -0700 | [diff] [blame] | 193 | base::TimeDelta::FromSeconds(1)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 194 | return true; |
| 195 | } |
| 196 | |
| 197 | std::string WifiBootstrapManager::GetCurrentlyConnectedSsid() const { |
| 198 | // TODO(vitalybuka): Get from shill, if possible. |
Vitaly Buka | f86b4d5 | 2015-09-28 15:12:03 -0700 | [diff] [blame] | 199 | return config_->GetSettings().last_configured_ssid; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | std::string WifiBootstrapManager::GetHostedSsid() const { |
Vitaly Buka | b6c8a3e | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 203 | return privet_ssid_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | std::set<WifiType> WifiBootstrapManager::GetTypes() const { |
| 207 | // TODO(wiley) This should do some system work to figure this out. |
| 208 | return {WifiType::kWifi24}; |
| 209 | } |
| 210 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 211 | void WifiBootstrapManager::OnConnectDone(const std::string& ssid, |
| 212 | ErrorPtr error) { |
| 213 | if (error) { |
| 214 | Error::AddTo(&error, FROM_HERE, errors::kDomain, errors::kInvalidState, |
| 215 | "Failed to connect to provided network"); |
| 216 | setup_state_ = SetupState{std::move(error)}; |
| 217 | return StartBootstrapping(); |
| 218 | } |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 219 | VLOG(1) << "Wifi was connected successfully"; |
Vitaly Buka | f86b4d5 | 2015-09-28 15:12:03 -0700 | [diff] [blame] | 220 | Config::Transaction change{config_}; |
| 221 | change.set_last_configured_ssid(ssid); |
| 222 | change.Commit(); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 223 | setup_state_ = SetupState{SetupState::kSuccess}; |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 224 | StartMonitoring(base::TimeDelta::FromSeconds(kMonitoringTimeoutSeconds)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 227 | void WifiBootstrapManager::OnConnectTimeout() { |
| 228 | ErrorPtr error; |
| 229 | Error::AddTo(&error, FROM_HERE, errors::kDomain, errors::kInvalidState, |
| 230 | "Timeout connecting to provided network"); |
| 231 | setup_state_ = SetupState{std::move(error)}; |
| 232 | return StartBootstrapping(); |
| 233 | } |
| 234 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 235 | void WifiBootstrapManager::OnBootstrapTimeout() { |
| 236 | VLOG(1) << "Bootstrapping has timed out."; |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 237 | StartMonitoring(base::TimeDelta::FromSeconds(kMonitoringTimeoutSeconds)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Vitaly Buka | bf6840a | 2015-09-21 13:38:56 -0700 | [diff] [blame] | 240 | void WifiBootstrapManager::OnConnectivityChange() { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 241 | UpdateConnectionState(); |
| 242 | |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 243 | if (state_ == State::kMonitoring || |
Vitaly Buka | 7e1d8b2 | 2015-09-21 15:08:09 -0700 | [diff] [blame] | 244 | (state_ != State::kDisabled && |
Vitaly Buka | efad5b2 | 2015-10-08 10:02:14 -0700 | [diff] [blame] | 245 | network_->GetConnectionState() == Network::State::kOnline)) { |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 246 | ContinueMonitoring(base::TimeDelta::FromSeconds(kMonitoringTimeoutSeconds)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | void WifiBootstrapManager::OnMonitorTimeout() { |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 251 | VLOG(1) << "Spent too long offline. Entering bootstrap mode."; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 252 | // TODO(wiley) Retrieve relevant errors from shill. |
| 253 | StartBootstrapping(); |
| 254 | } |
| 255 | |
| 256 | void WifiBootstrapManager::UpdateConnectionState() { |
| 257 | connection_state_ = ConnectionState{ConnectionState::kUnconfigured}; |
Vitaly Buka | 35f317d | 2015-09-27 22:54:39 -0700 | [diff] [blame] | 258 | |
| 259 | Network::State service_state{network_->GetConnectionState()}; |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 260 | VLOG(3) << "New network state: " << EnumToString(service_state); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 261 | switch (service_state) { |
Vitaly Buka | 35f317d | 2015-09-27 22:54:39 -0700 | [diff] [blame] | 262 | case Network::State::kOffline: |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 263 | connection_state_ = ConnectionState{ConnectionState::kOffline}; |
| 264 | return; |
Vitaly Buka | efad5b2 | 2015-10-08 10:02:14 -0700 | [diff] [blame] | 265 | case Network::State::kError: { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 266 | // TODO(wiley) Pull error information from somewhere. |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 267 | ErrorPtr error; |
| 268 | Error::AddTo(&error, FROM_HERE, errors::kDomain, errors::kInvalidState, |
| 269 | "Unknown WiFi error"); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 270 | connection_state_ = ConnectionState{std::move(error)}; |
| 271 | return; |
| 272 | } |
Vitaly Buka | 35f317d | 2015-09-27 22:54:39 -0700 | [diff] [blame] | 273 | case Network::State::kConnecting: |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 274 | connection_state_ = ConnectionState{ConnectionState::kConnecting}; |
| 275 | return; |
Vitaly Buka | efad5b2 | 2015-10-08 10:02:14 -0700 | [diff] [blame] | 276 | case Network::State::kOnline: |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 277 | connection_state_ = ConnectionState{ConnectionState::kOnline}; |
| 278 | return; |
| 279 | } |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 280 | ErrorPtr error; |
| 281 | Error::AddToPrintf(&error, FROM_HERE, errors::kDomain, errors::kInvalidState, |
| 282 | "Unknown network state: %s", |
| 283 | EnumToString(service_state).c_str()); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 284 | connection_state_ = ConnectionState{std::move(error)}; |
| 285 | } |
| 286 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 287 | } // namespace privet |
Vitaly Buka | c8ba228 | 2015-10-01 17:42:40 -0700 | [diff] [blame] | 288 | |
| 289 | template <> |
| 290 | LIBWEAVE_EXPORT |
| 291 | EnumToStringMap<privet::WifiBootstrapManager::State>::EnumToStringMap() |
| 292 | : EnumToStringMap(privet::kWifiSetupStateMap) {} |
| 293 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 294 | } // namespace weave |