21 return rssi >= -55 ? 4 : rssi >= -65 ? 3 : rssi >= -75 ? 2 : rssi >= -85 ? 1 : 0;
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()) {
33 }
else if (n.rssi > it->rssi) {
37 std::sort(out.begin(), out.end(), [](
const WifiNetwork& a,
const WifiNetwork& b) {
return a.rssi > b.rssi; });
42
43
44
45
46
47
48
49
60 virtual bool scanDone(std::vector<WifiNetwork>& out) = 0;
62 virtual void connect(
const char* ssid,
const char* password) = 0;
66 virtual std::string
ssid() = 0;
68 virtual std::string
ip() {
return ""; }
76
77
78
79
83 networks = {{
"HomeNetwork", -48,
true}, {
"Office", -63,
true}, {
"CoffeeShop", -71,
false},
84 {
"Neighbour", -84,
true}, {
"Office", -70,
true}};
102 scanStart_ = millis();
106 bool scanDone(std::vector<WifiNetwork>& out)
override {
107 if (!scanning_)
return true;
108 if (millis() - scanStart_ <
scanMs)
return false;
111 normalizeNetworks(out);
115 void connect(
const char* ssid,
const char* pw)
override {
116 ssid_ = ssid !=
nullptr ? ssid :
"";
117 pending_ = pw !=
nullptr ? pw :
"";
119 connectStart_ = millis();
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);
129 if (ok) saved_ =
true;
134 std::string
ssid()
override {
return ssid_; }
135 std::string
ip()
override {
return state_ == State::kConnected ?
"192.168.1.42" :
""; }
137 void forget()
override { saved_ =
false; }
141 std::string ssid_, pending_;
143 bool scanning_ =
false;
144 uint32_t scanStart_ = 0, connectStart_ = 0;
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 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