1 // Copyright 2015 The Weave 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 5 #ifndef LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ 6 #define LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ 7 8 #include <set> 9 #include <string> 10 #include <vector> 11 12 #include <base/callback.h> 13 #include <base/macros.h> 14 #include <base/memory/weak_ptr.h> 15 #include <base/scoped_observer.h> 16 #include <base/time/time.h> 17 18 #include "src/privet/privet_types.h" 19 #include "src/privet/wifi_delegate.h" 20 #include "src/privet/wifi_ssid_generator.h" 21 22 namespace weave { 23 24 class Config; 25 26 namespace provider { 27 class Network; 28 class TaskRunner; 29 class Wifi; 30 } 31 32 namespace privet { 33 34 class CloudDelegate; 35 class DeviceDelegate; 36 37 class WifiBootstrapManager : public WifiDelegate { 38 public: 39 enum class State { 40 kDisabled, 41 kBootstrapping, 42 kMonitoring, 43 kConnecting, 44 }; 45 46 WifiBootstrapManager(Config* config, 47 provider::TaskRunner* task_runner, 48 provider::Network* shill_client, 49 provider::Wifi* wifi, 50 CloudDelegate* gcd); 51 ~WifiBootstrapManager() override = default; 52 virtual void Init(); 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, 59 ErrorPtr* error) override; 60 std::string GetCurrentlyConnectedSsid() const override; 61 std::string GetHostedSsid() const override; 62 std::set<WifiType> GetTypes() const override; 63 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 76 void StartMonitoring(const base::TimeDelta& timeout); 77 void ContinueMonitoring(const base::TimeDelta& timeout); 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); 83 84 std::string GenerateSsid() const; 85 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(); 91 void OnConnectDone(const std::string& ssid, ErrorPtr error); 92 void OnConnectTimeout(); 93 void OnConnectivityChange(); 94 void OnMonitorTimeout(); 95 void UpdateConnectionState(); 96 97 State state_{State::kDisabled}; 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}; 102 Config* config_{nullptr}; 103 provider::TaskRunner* task_runner_{nullptr}; 104 provider::Network* network_{nullptr}; 105 provider::Wifi* wifi_{nullptr}; 106 WifiSsidGenerator ssid_generator_; 107 base::Time monitor_until_; 108 109 bool currently_online_{false}; 110 std::string privet_ssid_; 111 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 120 } // namespace privet 121 } // namespace weave 122 123 #endif // LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ 124