TinyMaterialDesign
Loading...
Searching...
No Matches
WifiControl.h
Go to the documentation of this file.
1#pragma once
2#include <stdint.h>
3
4#include <algorithm>
5#include <string>
6#include <vector>
7
8namespace tinymd {
9
10/// A WiFi network found by a scan.
12 std::string ssid;
13 /// Signal strength in dBm (e.g. -45 strong, -85 weak).
14 int rssi = -100;
15 /// Needs a password.
16 bool secure = true;
17};
18
19/// Signal strength as 0-4 bars (see drawSignalBars()).
20inline int wifiBars(int rssi) {
21 return rssi >= -55 ? 4 : rssi >= -65 ? 3 : rssi >= -75 ? 2 : rssi >= -85 ? 1 : 0;
22}
23
24/// Keeps the strongest entry of each SSID (hidden networks are dropped),
25/// sorted by signal strength.
26inline void normalizeNetworks(std::vector<WifiNetwork>& list) {
27 std::vector<WifiNetwork> out;
28 for (const WifiNetwork& n : list) {
29 if (n.ssid.empty()) continue;
30 auto it = std::find_if(out.begin(), out.end(), [&](const WifiNetwork& o) { return o.ssid == n.ssid; });
31 if (it == out.end()) {
32 out.push_back(n);
33 } else if (n.rssi > it->rssi) {
34 *it = n;
35 }
36 }
37 std::sort(out.begin(), out.end(), [](const WifiNetwork& a, const WifiNetwork& b) { return a.rssi > b.rssi; });
38 list = out;
39}
40
41/**
42 * @brief The WiFi of a device as WifiLoginPage uses it: scan, connect and
43 * the stored credentials. All calls must return quickly - the scan and the
44 * connection run in the background and are polled.
45 *
46 * Implementations: Esp32Wifi (TinyMaterialDesign/Wifi/WifiEsp32.h, not
47 * included by TinyMaterialDesign.h) and SimulatedWifi (desktop, tests); for
48 * another board derive from this class.
49 */
51 public:
53
54 virtual ~WifiControl() = default;
55
56 /// Starts a scan in the background.
57 virtual bool startScan() = 0;
58 /// True when the scan has finished; `out` then holds the networks
59 /// (strongest first, each SSID once - see normalizeNetworks()).
60 virtual bool scanDone(std::vector<WifiNetwork>& out) = 0;
61 /// Connects in the background; the credentials are stored when it works.
62 virtual void connect(const char* ssid, const char* password) = 0;
63 virtual void disconnect() = 0;
64 virtual State state() = 0;
65 /// The network that is connected or that is being connected to.
66 virtual std::string ssid() = 0;
67 /// The IP address while connected.
68 virtual std::string ip() { return ""; }
69 /// Credentials are stored (they are used at the start).
70 virtual bool hasCredentials() = 0;
71 /// Removes the stored credentials.
72 virtual void forget() {}
73};
74
75/**
76 * @brief WiFi without hardware, for the desktop and tests: a fixed list of
77 * networks; connecting succeeds after `connectMs` if the network is open or
78 * the password is `password` ("demo").
79 */
80class SimulatedWifi : public WifiControl {
81 public:
83 networks = {{"HomeNetwork", -48, true}, {"Office", -63, true}, {"CoffeeShop", -71, false},
84 {"Neighbour", -84, true}, {"Office", -70, true}};
85 }
86
88 /// The password that works for the secure networks.
89 std::string password = "demo";
90 uint32_t scanMs = 800;
91 uint32_t connectMs = 1500;
92
93 /// Starts connected (with stored credentials).
94 void setConnected(const char* ssid) {
95 ssid_ = ssid;
96 saved_ = true;
97 state_ = State::kConnected;
98 }
99
100 bool startScan() override {
101 scanning_ = true;
102 scanStart_ = millis();
103 return true;
104 }
105
106 bool scanDone(std::vector<WifiNetwork>& out) override {
107 if (!scanning_) return true;
108 if (millis() - scanStart_ < scanMs) return false;
109 scanning_ = false;
110 out = networks;
111 normalizeNetworks(out);
112 return true;
113 }
114
115 void connect(const char* ssid, const char* pw) override {
116 ssid_ = ssid != nullptr ? ssid : "";
117 pending_ = pw != nullptr ? pw : "";
118 state_ = State::kConnecting;
119 connectStart_ = millis();
120 }
121
122 void disconnect() override { state_ = State::kDisconnected; }
123
124 State state() override {
125 if (state_ == State::kConnecting && millis() - connectStart_ >= connectMs) {
126 auto it = std::find_if(networks.begin(), networks.end(), [&](const WifiNetwork& n) { return n.ssid == ssid_; });
127 const bool ok = it != networks.end() && (!it->secure || pending_ == password);
128 state_ = ok ? State::kConnected : State::kFailed;
129 if (ok) saved_ = true;
130 }
131 return state_;
132 }
133
134 std::string ssid() override { return ssid_; }
135 std::string ip() override { return state_ == State::kConnected ? "192.168.1.42" : ""; }
136 bool hasCredentials() override { return saved_; }
137 void forget() override { saved_ = false; }
138
139 private:
141 std::string ssid_, pending_;
142 bool saved_ = false;
143 bool scanning_ = false;
144 uint32_t scanStart_ = 0, connectStart_ = 0;
145};
146
147} // namespace tinymd
WiFi without hardware, for the desktop and tests: a fixed list of networks; connecting succeeds after...
Definition: WifiControl.h:80
void disconnect() override
Definition: WifiControl.h:122
std::string ssid() override
The network that is connected or that is being connected to.
Definition: WifiControl.h:134
State state() override
Definition: WifiControl.h:124
std::string ip() override
The IP address while connected.
Definition: WifiControl.h:135
void setConnected(const char *ssid)
Starts connected (with stored credentials).
Definition: WifiControl.h:94
std::vector< WifiNetwork > networks
Definition: WifiControl.h:87
uint32_t scanMs
Definition: WifiControl.h:90
std::string password
The password that works for the secure networks.
Definition: WifiControl.h:89
SimulatedWifi()
Definition: WifiControl.h:82
bool startScan() override
Starts a scan in the background.
Definition: WifiControl.h:100
bool hasCredentials() override
Credentials are stored (they are used at the start).
Definition: WifiControl.h:136
void connect(const char *ssid, const char *pw) override
Connects in the background; the credentials are stored when it works.
Definition: WifiControl.h:115
uint32_t connectMs
Definition: WifiControl.h:91
bool scanDone(std::vector< WifiNetwork > &out) override
Definition: WifiControl.h:106
void forget() override
Removes the stored credentials.
Definition: WifiControl.h:137
The WiFi of a device as WifiLoginPage uses it: scan, connect and the stored credentials....
Definition: WifiControl.h:50
virtual State state()=0
virtual std::string ip()
The IP address while connected.
Definition: WifiControl.h:68
virtual ~WifiControl()=default
virtual bool hasCredentials()=0
Credentials are stored (they are used at the start).
State
Definition: WifiControl.h:52
virtual void connect(const char *ssid, const char *password)=0
Connects in the background; the credentials are stored when it works.
virtual std::string ssid()=0
The network that is connected or that is being connected to.
virtual bool scanDone(std::vector< WifiNetwork > &out)=0
virtual bool startScan()=0
Starts a scan in the background.
virtual void disconnect()=0
virtual void forget()
Removes the stored credentials.
Definition: WifiControl.h:72
Definition: AnchorLayout.h:4
void normalizeNetworks(std::vector< WifiNetwork > &list)
Definition: WifiControl.h:26
int wifiBars(int rssi)
Signal strength as 0-4 bars (see drawSignalBars()).
Definition: WifiControl.h:20
A WiFi network found by a scan.
Definition: WifiControl.h:11
std::string ssid
Definition: WifiControl.h:12
int rssi
Signal strength in dBm (e.g. -45 strong, -85 weak).
Definition: WifiControl.h:14
bool secure
Needs a password.
Definition: WifiControl.h:16