blob: 7ade295990fdad80e44a65ce498f164b15e7be91 [file] [log] [blame]
Vitaly Buka7ce499f2015-06-09 08:04:11 -07001// 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 Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_
6#define LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_
Vitaly Buka7ce499f2015-06-09 08:04:11 -07007
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 Buka912b6982015-07-06 11:13:03 -070018#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 Buka7ce499f2015-06-09 08:04:11 -070022
Vitaly Bukab6f015a2015-07-09 14:59:23 -070023namespace weave {
24namespace privet {
Vitaly Buka7ce499f2015-06-09 08:04:11 -070025
26class ApManagerClient;
27class CloudDelegate;
28class DeviceDelegate;
29class ShillClient;
30
31class WifiBootstrapManager : public WifiDelegate,
32 public CloudDelegate::Observer {
33 public:
Vitaly Buka0fa51e52015-07-10 00:12:25 -070034 using State = WifiSetupState;
Vitaly Buka7ce499f2015-06-09 08:04:11 -070035
36 using StateListener = base::Callback<void(State)>;
37
Vitaly Bukab56872f2015-06-21 18:39:34 -070038 WifiBootstrapManager(const std::string& last_configured_ssid,
Vitaly Buka557d4522015-07-18 20:43:58 -070039 const std::string& test_privet_ssid,
Vitaly Buka7ce499f2015-06-09 08:04:11 -070040 ShillClient* shill_client,
41 ApManagerClient* ap_manager_client,
Vitaly Bukae27f5522015-06-19 18:58:19 -070042 CloudDelegate* gcd);
Vitaly Buka7ce499f2015-06-09 08:04:11 -070043 ~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 Buka557d4522015-07-18 20:43:58 -070080 std::string GenerateSsid() const;
81
Vitaly Buka7ce499f2015-06-09 08:04:11 -070082 // 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 Buka0fa51e52015-07-10 00:12:25 -070095 State state_{State::kDisabled};
Vitaly Buka7ce499f2015-06-09 08:04:11 -070096 // 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 Buka7ce499f2015-06-09 08:04:11 -0700100 ShillClient* shill_client_;
101 ApManagerClient* ap_manager_client_;
102 WifiSsidGenerator ssid_generator_;
103
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700104 std::vector<StateListener> state_listeners_;
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700105 bool currently_online_{false};
106 std::string last_configured_ssid_;
Vitaly Buka557d4522015-07-18 20:43:58 -0700107 std::string test_privet_ssid_;
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700108
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 Bukab6f015a2015-07-09 14:59:23 -0700119} // namespace privet
120} // namespace weave
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700121
Vitaly Buka912b6982015-07-06 11:13:03 -0700122#endif // LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_