Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 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> |
| 13 | #include <base/files/file_path.h> |
| 14 | #include <base/macros.h> |
| 15 | #include <base/memory/weak_ptr.h> |
| 16 | #include <base/scoped_observer.h> |
| 17 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 18 | #include "libweave/src/privet/cloud_delegate.h" |
| 19 | #include "libweave/src/privet/privet_types.h" |
| 20 | #include "libweave/src/privet/wifi_delegate.h" |
| 21 | #include "libweave/src/privet/wifi_ssid_generator.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 22 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 23 | namespace weave { |
| 24 | namespace privet { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 25 | |
| 26 | class ApManagerClient; |
| 27 | class CloudDelegate; |
| 28 | class DeviceDelegate; |
| 29 | class ShillClient; |
| 30 | |
| 31 | class WifiBootstrapManager : public WifiDelegate, |
| 32 | public CloudDelegate::Observer { |
| 33 | public: |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 34 | using State = WifiSetupState; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 35 | |
| 36 | using StateListener = base::Callback<void(State)>; |
| 37 | |
Vitaly Buka | b56872f | 2015-06-21 18:39:34 -0700 | [diff] [blame] | 38 | WifiBootstrapManager(const std::string& last_configured_ssid, |
Vitaly Buka | 557d452 | 2015-07-18 20:43:58 -0700 | [diff] [blame^] | 39 | const std::string& test_privet_ssid, |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 40 | ShillClient* shill_client, |
| 41 | ApManagerClient* ap_manager_client, |
Vitaly Buka | e27f552 | 2015-06-19 18:58:19 -0700 | [diff] [blame] | 42 | CloudDelegate* gcd); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 43 | ~WifiBootstrapManager() override = default; |
| 44 | virtual void Init(); |
| 45 | void RegisterStateListener(const StateListener& listener); |
| 46 | |
| 47 | // Overrides from WifiDelegate. |
| 48 | const ConnectionState& GetConnectionState() const override; |
| 49 | const SetupState& GetSetupState() const override; |
| 50 | bool ConfigureCredentials(const std::string& ssid, |
| 51 | const std::string& passphrase, |
| 52 | chromeos::ErrorPtr* error) override; |
| 53 | std::string GetCurrentlyConnectedSsid() const override; |
| 54 | std::string GetHostedSsid() const override; |
| 55 | std::set<WifiType> GetTypes() const override; |
| 56 | |
| 57 | // Overrides from CloudDelegate::Observer. |
| 58 | void OnDeviceInfoChanged() override; |
| 59 | |
| 60 | private: |
| 61 | // These Start* tasks: |
| 62 | // 1) Do state appropriate work for entering the indicated state. |
| 63 | // 2) Update the state variable to reflect that we're in a new state |
| 64 | // 3) Call StateListeners to notify that we've transitioned. |
| 65 | // These End* tasks perform cleanup on leaving indicated state. |
| 66 | void StartBootstrapping(); |
| 67 | void EndBootstrapping(); |
| 68 | |
| 69 | void StartConnecting(const std::string& ssid, const std::string& passphrase); |
| 70 | void EndConnecting(); |
| 71 | |
| 72 | void StartMonitoring(); |
| 73 | void EndMonitoring(); |
| 74 | |
| 75 | // Update the current state, post tasks to notify listeners accordingly to |
| 76 | // the MessageLoop. |
| 77 | void UpdateState(State new_state); |
| 78 | void NotifyStateListeners(State new_state) const; |
| 79 | |
Vitaly Buka | 557d452 | 2015-07-18 20:43:58 -0700 | [diff] [blame^] | 80 | std::string GenerateSsid() const; |
| 81 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 82 | // If we've been bootstrapped successfully before, and we're bootstrapping |
| 83 | // again because we slipped offline for a sufficiently longtime, we want |
| 84 | // to return to monitoring mode periodically in case our connectivity issues |
| 85 | // were temporary. |
| 86 | void OnBootstrapTimeout(); |
| 87 | void OnConnectTimeout(); |
| 88 | void OnConnectSuccess(const std::string& ssid); |
| 89 | void OnConnectivityChange(bool is_connected); |
| 90 | void OnMonitorTimeout(); |
| 91 | void UpdateConnectionState(); |
| 92 | |
| 93 | // Initialization could be delayed if ssid_generator_ is not ready. |
| 94 | bool is_initialized_{false}; |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 95 | State state_{State::kDisabled}; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 96 | // Setup state is the temporal state of the most recent bootstrapping attempt. |
| 97 | // It is not persisted to disk. |
| 98 | SetupState setup_state_{SetupState::kNone}; |
| 99 | ConnectionState connection_state_{ConnectionState::kDisabled}; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 100 | ShillClient* shill_client_; |
| 101 | ApManagerClient* ap_manager_client_; |
| 102 | WifiSsidGenerator ssid_generator_; |
| 103 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 104 | std::vector<StateListener> state_listeners_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 105 | bool currently_online_{false}; |
| 106 | std::string last_configured_ssid_; |
Vitaly Buka | 557d452 | 2015-07-18 20:43:58 -0700 | [diff] [blame^] | 107 | std::string test_privet_ssid_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 108 | |
| 109 | ScopedObserver<CloudDelegate, CloudDelegate::Observer> cloud_observer_{this}; |
| 110 | |
| 111 | // Helps to reset irrelevant tasks switching state. |
| 112 | base::WeakPtrFactory<WifiBootstrapManager> tasks_weak_factory_{this}; |
| 113 | |
| 114 | base::WeakPtrFactory<WifiBootstrapManager> lifetime_weak_factory_{this}; |
| 115 | |
| 116 | DISALLOW_COPY_AND_ASSIGN(WifiBootstrapManager); |
| 117 | }; |
| 118 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 119 | } // namespace privet |
| 120 | } // namespace weave |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 121 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 122 | #endif // LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ |