aboutsummaryrefslogtreecommitdiff
path: root/src/privet/wifi_bootstrap_manager.h
blob: 62a77c2c82516b05dcaf3d6c5affbad6743b9a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_
#define LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_

#include <set>
#include <string>
#include <vector>

#include <base/callback.h>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
#include <base/scoped_observer.h>
#include <base/time/time.h>

#include "src/privet/privet_types.h"
#include "src/privet/wifi_delegate.h"
#include "src/privet/wifi_ssid_generator.h"

namespace weave {

class Config;

namespace provider {
class Network;
class TaskRunner;
class Wifi;
}

namespace privet {

class CloudDelegate;
class DeviceDelegate;

class WifiBootstrapManager : public WifiDelegate {
 public:
  enum class State {
    kDisabled,
    kBootstrapping,
    kMonitoring,
    kConnecting,
  };

  WifiBootstrapManager(Config* config,
                       provider::TaskRunner* task_runner,
                       provider::Network* shill_client,
                       provider::Wifi* wifi,
                       CloudDelegate* gcd);
  ~WifiBootstrapManager() override = default;
  virtual void Init();

  // Overrides from WifiDelegate.
  const ConnectionState& GetConnectionState() const override;
  const SetupState& GetSetupState() const override;
  bool ConfigureCredentials(const std::string& ssid,
                            const std::string& passphrase,
                            ErrorPtr* error) override;
  std::string GetCurrentlyConnectedSsid() const override;
  std::string GetHostedSsid() const override;
  std::set<WifiType> GetTypes() const override;

 private:
  // These Start* tasks:
  //   1) Do state appropriate work for entering the indicated state.
  //   2) Update the state variable to reflect that we're in a new state
  //   3) Call StateListeners to notify that we've transitioned.
  // These End* tasks perform cleanup on leaving indicated state.
  void StartBootstrapping();
  void EndBootstrapping();

  void StartConnecting(const std::string& ssid, const std::string& passphrase);
  void EndConnecting();

  void StartMonitoring(const base::TimeDelta& timeout);
  void ContinueMonitoring(const base::TimeDelta& timeout);
  void EndMonitoring();

  // Update the current state, post tasks to notify listeners accordingly to
  // the MessageLoop.
  void UpdateState(State new_state);

  std::string GenerateSsid() const;

  // If we've been bootstrapped successfully before, and we're bootstrapping
  // again because we slipped offline for a sufficiently longtime, we want
  // to return to monitoring mode periodically in case our connectivity issues
  // were temporary.
  void OnBootstrapTimeout();
  void OnConnectDone(const std::string& ssid, ErrorPtr error);
  void OnConnectTimeout();
  void OnConnectivityChange();
  void OnMonitorTimeout();
  void UpdateConnectionState();

  State state_{State::kDisabled};
  // Setup state is the temporal state of the most recent bootstrapping attempt.
  // It is not persisted to disk.
  SetupState setup_state_{SetupState::kNone};
  ConnectionState connection_state_{ConnectionState::kDisabled};
  Config* config_{nullptr};
  provider::TaskRunner* task_runner_{nullptr};
  provider::Network* network_{nullptr};
  provider::Wifi* wifi_{nullptr};
  WifiSsidGenerator ssid_generator_;
  base::Time monitor_until_;

  bool currently_online_{false};
  std::string privet_ssid_;

  // Helps to reset irrelevant tasks switching state.
  base::WeakPtrFactory<WifiBootstrapManager> tasks_weak_factory_{this};

  base::WeakPtrFactory<WifiBootstrapManager> lifetime_weak_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(WifiBootstrapManager);
};

}  // namespace privet
}  // namespace weave

#endif  // LIBWEAVE_SRC_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_