roo_wifi
API Documentation for roo_wifi
Loading...
Searching...
No Matches
controller.h
Go to the documentation of this file.
1#pragma once
2
3#include <inttypes.h>
4
5#include <algorithm>
6#include <string>
7#include <vector>
8
9#include "roo_collections/flat_small_hash_set.h"
10#include "roo_scheduler.h"
12#include "roo_wifi/hal/store.h"
13
14namespace roo_wifi {
15
16/// High-level Wi-Fi controller that manages scanning and connections.
18 public:
19 /// Summary of a scanned network.
20 struct Network {
21 Network() : ssid(), open(false), rssi(-128) {}
22
23 std::string ssid;
24 bool open;
25 int8_t rssi;
26 };
27
28 /// Listener for controller events.
29 class Listener {
30 public:
31 Listener() = default;
32 virtual ~Listener() = default;
33
34 virtual void onEnableChanged(bool enabled) {}
35 virtual void onScanStarted() {}
36 virtual void onScanCompleted() {}
37 virtual void onCurrentNetworkChanged() {}
39
40 private:
41 friend class Controller;
42 };
43
44 /// Creates a controller using the provided store, interface, and scheduler.
45 Controller(Store& store, Interface& interface,
46 roo_scheduler::Scheduler& scheduler);
47
48 /// Destroys the controller and detaches listeners.
50
51 /// Initializes the controller and registers for interface events.
52 void begin();
53
54 /// Adds a listener for controller events.
55 void addListener(Listener* listener);
56
57 /// Removes a previously added listener.
58 void removeListener(Listener* listener);
59
60 /// Returns the number of non-current networks in the scan list.
61 int otherScannedNetworksCount() const;
62
63 /// Returns the current network (may be empty if disconnected).
64 const Network& currentNetwork() const;
65
66 /// Returns a network by SSID, or nullptr if not found.
67 const Network* lookupNetwork(const std::string& ssid) const;
68
69 /// Returns the connection status of the current network.
71
72 /// Returns the ith non-current network in the scan list.
73 const Network& otherNetwork(int idx) const;
74
75 /// Starts a scan. Returns false if a scan could not be started.
76 bool startScan();
77
78 /// Returns true when the current scan has completed.
79 bool isScanCompleted() const { return interface_.scanCompleted(); }
80 /// Returns true when the interface is enabled.
81 bool isEnabled() const { return enabled_; }
82
83 /// Returns true when a connection is in progress.
84 bool isConnecting() const { return connecting_; }
85
86 /// Toggles the enabled/disabled state and persists it in the store.
87 void toggleEnabled();
88
89 /// Notifies listeners that enable state changed.
91
92 /// Looks up a stored password for the given SSID.
93 bool getStoredPassword(const std::string& ssid, std::string& passwd) const;
94
95 /// Temporarily disables periodic refresh and event processing.
96 void pause();
97
98 /// Resumes periodic refresh and event processing.
99 void resume();
100
101 /// Stores a password for the given SSID.
102 void setPassword(const std::string& ssid, const std::string& passwd);
103
104 /// Connects using stored SSID/password values.
105 bool connect();
106
107 /// Connects to the specified SSID/password.
108 bool connect(const std::string& ssid, const std::string& passwd);
109
110 /// Disconnects the current connection.
111 void disconnect();
112
113 /// Forgets the password and SSID association.
114 void forget(const std::string& ssid);
115
116 private:
117 class WifiListener : public Interface::EventListener {
118 public:
119 WifiListener(Controller& wifi) : wifi_(wifi) {}
120
121 void onEvent(Interface::EventType type) {
122 switch (type) {
124 wifi_.onScanCompleted();
125 break;
126 }
127 default: {
128 wifi_.onConnectionStateChanged(type);
129 break;
130 }
131 }
132 }
133
134 private:
135 Controller& wifi_;
136 };
137
138 friend class WifiListener;
139
140 void onConnectionStateChanged(Interface::EventType type);
141
142 void periodicRefreshCurrentNetwork();
143
144 void refreshCurrentNetwork();
145
146 void updateCurrentNetwork(const std::string& ssid, bool open, int8_t rssi,
147 ConnectionStatus status, bool force_notify);
148
149 void onScanCompleted();
150
151 Store& store_;
152 Interface& interface_;
153 bool enabled_;
154 Network current_network_;
155 int16_t current_network_index_;
156 ConnectionStatus current_network_status_;
157 std::vector<Network> all_networks_;
158 WifiListener wifi_listener_;
159 roo_collections::FlatSmallHashSet<Listener*> model_listeners_;
160 bool connecting_;
161
162 roo_scheduler::SingletonTask start_scan_;
163 roo_scheduler::SingletonTask refresh_current_network_;
164};
165
166} // namespace roo_wifi
Listener for controller events.
Definition controller.h:29
virtual void onCurrentNetworkChanged()
Definition controller.h:37
virtual void onEnableChanged(bool enabled)
Definition controller.h:34
virtual void onConnectionStateChanged(Interface::EventType type)
Definition controller.h:38
High-level Wi-Fi controller that manages scanning and connections.
Definition controller.h:17
const Network & currentNetwork() const
Returns the current network (may be empty if disconnected).
void pause()
Temporarily disables periodic refresh and event processing.
ConnectionStatus currentNetworkStatus() const
Returns the connection status of the current network.
void toggleEnabled()
Toggles the enabled/disabled state and persists it in the store.
void begin()
Initializes the controller and registers for interface events.
void resume()
Resumes periodic refresh and event processing.
bool isEnabled() const
Returns true when the interface is enabled.
Definition controller.h:81
bool isConnecting() const
Returns true when a connection is in progress.
Definition controller.h:84
bool connect()
Connects using stored SSID/password values.
void removeListener(Listener *listener)
Removes a previously added listener.
void forget(const std::string &ssid)
Forgets the password and SSID association.
bool startScan()
Starts a scan. Returns false if a scan could not be started.
void disconnect()
Disconnects the current connection.
~Controller()
Destroys the controller and detaches listeners.
bool getStoredPassword(const std::string &ssid, std::string &passwd) const
Looks up a stored password for the given SSID.
friend class WifiListener
Definition controller.h:138
void setPassword(const std::string &ssid, const std::string &passwd)
Stores a password for the given SSID.
bool isScanCompleted() const
Returns true when the current scan has completed.
Definition controller.h:79
int otherScannedNetworksCount() const
Returns the number of non-current networks in the scan list.
void addListener(Listener *listener)
Adds a listener for controller events.
const Network * lookupNetwork(const std::string &ssid) const
Returns a network by SSID, or nullptr if not found.
void notifyEnableChanged()
Notifies listeners that enable state changed.
const Network & otherNetwork(int idx) const
Returns the ith non-current network in the scan list.
Listener for interface events.
Definition interface.h:84
Abstraction for interacting with the hardware Wi-Fi interface.
Definition interface.h:70
EventType
Interface event types.
Definition interface.h:73
virtual bool scanCompleted() const =0
Returns true if the last scan has completed.
Abstraction for persistently storing Wi-Fi controller data.
Definition store.h:14
ConnectionStatus
Wi-Fi connection status.
Definition interface.h:42
Summary of a scanned network.
Definition controller.h:20