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 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ |
| 6 | #define LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/callback.h> |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
| 14 | #include <base/memory/weak_ptr.h> |
| 15 | #include <base/scoped_observer.h> |
Vitaly Buka | 658aa36 | 2015-09-15 20:59:12 -0700 | [diff] [blame] | 16 | #include <base/time/time.h> |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 17 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 18 | #include "src/privet/privet_types.h" |
| 19 | #include "src/privet/wifi_delegate.h" |
| 20 | #include "src/privet/wifi_ssid_generator.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 21 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 22 | namespace weave { |
Vitaly Buka | 387b4f4 | 2015-07-30 23:55:13 -0700 | [diff] [blame] | 23 | |
Vitaly Buka | f86b4d5 | 2015-09-28 15:12:03 -0700 | [diff] [blame] | 24 | class Config; |
| 25 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 26 | namespace provider { |
| 27 | class Network; |
Vitaly Buka | 658aa36 | 2015-09-15 20:59:12 -0700 | [diff] [blame] | 28 | class TaskRunner; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 29 | class Wifi; |
| 30 | } |
Vitaly Buka | 387b4f4 | 2015-07-30 23:55:13 -0700 | [diff] [blame] | 31 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 32 | namespace privet { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 33 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 34 | class CloudDelegate; |
| 35 | class DeviceDelegate; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 36 | |
Vitaly Buka | 658aa36 | 2015-09-15 20:59:12 -0700 | [diff] [blame] | 37 | class WifiBootstrapManager : public WifiDelegate { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 38 | public: |
Vitaly Buka | c8ba228 | 2015-10-01 17:42:40 -0700 | [diff] [blame] | 39 | enum class State { |
| 40 | kDisabled, |
| 41 | kBootstrapping, |
| 42 | kMonitoring, |
| 43 | kConnecting, |
| 44 | }; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 45 | |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 46 | WifiBootstrapManager(Config* config, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 47 | provider::TaskRunner* task_runner, |
| 48 | provider::Network* shill_client, |
| 49 | provider::Wifi* wifi, |
Vitaly Buka | e27f552 | 2015-06-19 18:58:19 -0700 | [diff] [blame] | 50 | CloudDelegate* gcd); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 51 | ~WifiBootstrapManager() override = default; |
| 52 | virtual void Init(); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 53 | |
| 54 | // Overrides from WifiDelegate. |
| 55 | const ConnectionState& GetConnectionState() const override; |
| 56 | const SetupState& GetSetupState() const override; |
| 57 | bool ConfigureCredentials(const std::string& ssid, |
| 58 | const std::string& passphrase, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 59 | ErrorPtr* error) override; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 60 | std::string GetCurrentlyConnectedSsid() const override; |
| 61 | std::string GetHostedSsid() const override; |
| 62 | std::set<WifiType> GetTypes() const override; |
| 63 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 64 | private: |
| 65 | // These Start* tasks: |
| 66 | // 1) Do state appropriate work for entering the indicated state. |
| 67 | // 2) Update the state variable to reflect that we're in a new state |
| 68 | // 3) Call StateListeners to notify that we've transitioned. |
| 69 | // These End* tasks perform cleanup on leaving indicated state. |
| 70 | void StartBootstrapping(); |
| 71 | void EndBootstrapping(); |
| 72 | |
| 73 | void StartConnecting(const std::string& ssid, const std::string& passphrase); |
| 74 | void EndConnecting(); |
| 75 | |
Vitaly Buka | a7c7a6b | 2015-10-28 13:12:34 -0700 | [diff] [blame] | 76 | void StartMonitoring(const base::TimeDelta& timeout); |
Vitaly Buka | 65e1f21 | 2015-11-05 15:54:05 -0800 | [diff] [blame] | 77 | void ContinueMonitoring(const base::TimeDelta& timeout); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 78 | void EndMonitoring(); |
| 79 | |
| 80 | // Update the current state, post tasks to notify listeners accordingly to |
| 81 | // the MessageLoop. |
| 82 | void UpdateState(State new_state); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 83 | |
Vitaly Buka | 557d452 | 2015-07-18 20:43:58 -0700 | [diff] [blame] | 84 | std::string GenerateSsid() const; |
| 85 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 86 | // If we've been bootstrapped successfully before, and we're bootstrapping |
| 87 | // again because we slipped offline for a sufficiently longtime, we want |
| 88 | // to return to monitoring mode periodically in case our connectivity issues |
| 89 | // were temporary. |
| 90 | void OnBootstrapTimeout(); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 91 | void OnConnectDone(const std::string& ssid, ErrorPtr error); |
| 92 | void OnConnectTimeout(); |
Vitaly Buka | bf6840a | 2015-09-21 13:38:56 -0700 | [diff] [blame] | 93 | void OnConnectivityChange(); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 94 | void OnMonitorTimeout(); |
| 95 | void UpdateConnectionState(); |
| 96 | |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 97 | State state_{State::kDisabled}; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 98 | // Setup state is the temporal state of the most recent bootstrapping attempt. |
| 99 | // It is not persisted to disk. |
| 100 | SetupState setup_state_{SetupState::kNone}; |
| 101 | ConnectionState connection_state_{ConnectionState::kDisabled}; |
Vitaly Buka | f86b4d5 | 2015-09-28 15:12:03 -0700 | [diff] [blame] | 102 | Config* config_{nullptr}; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 103 | provider::TaskRunner* task_runner_{nullptr}; |
| 104 | provider::Network* network_{nullptr}; |
| 105 | provider::Wifi* wifi_{nullptr}; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 106 | WifiSsidGenerator ssid_generator_; |
Vitaly Buka | ca365fb | 2015-09-15 17:38:41 -0700 | [diff] [blame] | 107 | base::Time monitor_until_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 108 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 109 | bool currently_online_{false}; |
Vitaly Buka | b6c8a3e | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 110 | std::string privet_ssid_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 111 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 112 | // Helps to reset irrelevant tasks switching state. |
| 113 | base::WeakPtrFactory<WifiBootstrapManager> tasks_weak_factory_{this}; |
| 114 | |
| 115 | base::WeakPtrFactory<WifiBootstrapManager> lifetime_weak_factory_{this}; |
| 116 | |
| 117 | DISALLOW_COPY_AND_ASSIGN(WifiBootstrapManager); |
| 118 | }; |
| 119 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 120 | } // namespace privet |
| 121 | } // namespace weave |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 122 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 123 | #endif // LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ |