-
Notifications
You must be signed in to change notification settings - Fork 34
/
WiFiSettings.cpp
407 lines (350 loc) · 13.3 KB
/
WiFiSettings.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "WiFiSettings.h"
#ifdef ESP32
#define ESPFS SPIFFS
#include <SPIFFS.h>
#include <WiFi.h>
#include <WebServer.h>
#include <esp_task_wdt.h>
#elif ESP8266
#define ESPFS LittleFS
#include <LittleFS.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define WebServer ESP8266WebServer
#define esp_task_wdt_reset wdt_reset
#define wifi_auth_mode_t uint8_t // wl_enc_type
#define WIFI_AUTH_OPEN ENC_TYPE_NONE
#define WIFI_AUTH_WPA2_ENTERPRISE -1337 // not available on ESP8266
#define setHostname hostname
#define INADDR_NONE IPAddress(0,0,0,0)
#else
#error "This library only supports ESP32 and ESP8266"
#endif
#include <DNSServer.h>
#include <limits.h>
#include <vector>
#define Sprintf(f, ...) ({ char* s; asprintf(&s, f, __VA_ARGS__); String r = s; free(s); r; })
namespace { // Helpers
String slurp(const String& fn) {
File f = ESPFS.open(fn, "r");
String r = f.readString();
f.close();
return r;
}
void spurt(const String& fn, const String& content) {
File f = ESPFS.open(fn, "w");
f.print(content);
f.close();
}
String pwgen() {
const char* passchars = "ABCEFGHJKLMNPRSTUXYZabcdefhkmnorstvxz23456789-#@%^";
String password = "";
for (int i = 0; i < 16; i++) {
// Note: no seed needed for ESP8266 and ESP32 hardware RNG
password.concat( passchars[random(strlen(passchars))] );
}
return password;
}
String html_entities(const String& raw) {
String r;
for (unsigned int i = 0; i < raw.length(); i++) {
char c = raw.charAt(i);
if (c >= '!' && c <= 'z' && c != '&' && c != '<' && c != '>' && c != '\'' && c != '"') {
// printable non-whitespace ascii minus html and {}
r += c;
} else {
r += Sprintf("&#%d;", raw.charAt(i));
}
}
return r;
}
struct WiFiSettingsParameter {
String name;
String label;
String value;
String init;
long min = LONG_MIN;
long max = LONG_MAX;
String filename() { String fn = "/"; fn += name; return fn; }
virtual void store(const String& v) { value = v; spurt(filename(), v); }
void fill() { value = slurp(filename()); }
virtual String html() = 0;
};
struct WiFiSettingsString : WiFiSettingsParameter {
String html() {
String h = F("<label>{label}:<br><input name='{name}' value='{value}' placeholder='{init}' minlength={min} maxlength={max}></label>");
h.replace("{name}", html_entities(name));
h.replace("{value}", html_entities(value));
h.replace("{init}", html_entities(init));
h.replace("{label}", html_entities(label));
h.replace("{min}", String(min));
h.replace("{max}", String(max));
return h;
}
};
struct WiFiSettingsInt : WiFiSettingsParameter {
String html() {
String h = F("<label>{label}:<br><input type=number step=1 min={min} max={max} name='{name}' value='{value}' placeholder='{init}'></label>");
h.replace("{name}", html_entities(name));
h.replace("{value}", html_entities(value));
h.replace("{init}", html_entities(init));
h.replace("{label}", html_entities(label));
h.replace("{min}", String(min));
h.replace("{max}", String(max));
return h;
}
};
struct WiFiSettingsBool : WiFiSettingsParameter {
String html() {
String h = F("<label><input type=checkbox name='{name}' value=1{checked}> {label} (default: {init})</label>");
h.replace("{name}", html_entities(name));
h.replace("{checked}", value.toInt() ? " checked" : "");
h.replace("{init}", init.toInt() ? "☑" : "☐");
h.replace("{label}", html_entities(label));
return h;
}
virtual void store(String v) {
value = v.length() ? "1" : "0";
spurt(filename(), value);
}
};
struct std::vector<WiFiSettingsParameter*> params;
}
String WiFiSettingsClass::string(const String& name, const String& init, const String& label) {
begin();
struct WiFiSettingsString* x = new WiFiSettingsString();
x->name = name;
x->label = label.length() ? label : name;
x->init = init;
x->fill();
params.push_back(x);
return x->value.length() ? x->value : x->init;
}
String WiFiSettingsClass::string(const String& name, unsigned int max_length, const String& init, const String& label) {
String rv = string(name, init, label);
params.back()->max = max_length;
return rv;
}
String WiFiSettingsClass::string(const String& name, unsigned int min_length, unsigned int max_length, const String& init, const String& label) {
String rv = string(name, init, label);
params.back()->min = min_length;
params.back()->max = max_length;
return rv;
}
long WiFiSettingsClass::integer(const String& name, long init, const String& label) {
begin();
struct WiFiSettingsInt* x = new WiFiSettingsInt();
x->name = name;
x->label = label.length() ? label : name;
x->init = init;
x->fill();
params.push_back(x);
return (x->value.length() ? x->value : x->init).toInt();
}
long WiFiSettingsClass::integer(const String& name, long min, long max, long init, const String& label) {
long rv = integer(name, init, label);
params.back()->min = min;
params.back()->max = max;
return rv;
}
bool WiFiSettingsClass::checkbox(const String& name, bool init, const String& label) {
begin();
struct WiFiSettingsBool* x = new WiFiSettingsBool();
x->name = name;
x->label = label.length() ? label : name;
x->init = String((int) init);
x->fill();
// Apply default immediately because a checkbox has no placeholder to
// show the default, and other UI elements aren't sufficiently pretty.
if (! x->value.length()) x->value = x->init;
params.push_back(x);
return x->value.toInt();
}
void WiFiSettingsClass::portal() {
static WebServer http(80);
static DNSServer dns;
static int num_networks = -1;
begin();
#ifdef ESP32
WiFi.disconnect(true, true); // reset state so .scanNetworks() works
#else
WiFi.disconnect(true);
#endif
Serial.println(F("Starting access point for configuration portal."));
if (secure && password.length()) {
Serial.printf("SSID: '%s', Password: '%s'\n", hostname.c_str(), password.c_str());
WiFi.softAP(hostname.c_str(), password.c_str());
} else {
Serial.printf("SSID: '%s'\n", hostname.c_str());
WiFi.softAP(hostname.c_str());
}
delay(500);
dns.setTTL(0);
dns.start(53, "*", WiFi.softAPIP());
if (onPortal) onPortal();
Serial.println(WiFi.softAPIP().toString());
http.on("/", HTTP_GET, [this]() {
http.setContentLength(CONTENT_LENGTH_UNKNOWN);
http.send(200, "text/html");
http.sendContent(F("<!DOCTYPE html>\n<meta charset=UTF-8><title>"));
http.sendContent(html_entities(hostname));
http.sendContent(F("</title>"
"<meta name=viewport content='width=device-width,initial-scale=1'>"
"<style>"
"*{box-sizing:border-box} "
"html{background:#444;font:10pt sans-serif}"
"body{background:#ccc;color:black;max-width:30em;padding:1em;margin:1em auto}"
"a:link{color:#000} "
"label{clear:both}"
"select,input:not([type^=c]){display:block;width:100%;border:1px solid #444;padding:.3ex}"
"input[type^=s]{width:auto;background:#de1;padding:1ex;border:1px solid #000;border-radius:1ex}"
"[type^=c]{float:left}"
":not([type^=s]):focus{outline:2px solid #d1ed1e}"
"</style>"
"<form action=/restart method=post>Hello, my name is "
));
http.sendContent(html_entities(hostname));
http.sendContent(F(
"."
"<input type=submit value='Restart device'>"
"</form>"
"<hr>"
"<h2>Configuration</h2>"
"<form method=post>"
"<label>SSID:<br><b class=s>Scanning for WiFi networks...</b>"
));
if (num_networks < 0) num_networks = WiFi.scanNetworks();
Serial.printf("%d WiFi networks found.\n", num_networks);
http.sendContent(F(
"<style>.s{display:none}</style>" // hide "scanning"
"<select name=ssid onchange=\"document.getElementsByName('password')[0].value=''\">"
));
String current = slurp("/wifi-ssid");
bool found = false;
for (int i = 0; i < num_networks; i++) {
String opt = F("<option value='{ssid}'{sel}>{ssid} {lock} {1x}</option>");
String ssid = WiFi.SSID(i);
wifi_auth_mode_t mode = WiFi.encryptionType(i);
opt.replace("{sel}", ssid == current && !found ? " selected" : "");
opt.replace("{ssid}", html_entities(ssid));
opt.replace("{lock}", mode != WIFI_AUTH_OPEN ? "🔒" : "");
opt.replace("{1x}", mode == WIFI_AUTH_WPA2_ENTERPRISE ? "(won't work: 802.1x is not supported)" : "");
http.sendContent(opt);
if (ssid == current) found = true;
}
if (!found && current.length()) {
String opt = F("<option value='{ssid}' selected>{ssid} (⚠ not in range)</option>");
opt.replace("{ssid}", html_entities(current));
http.sendContent(opt);
}
http.sendContent(F("</select></label> "
"<a href=/rescan onclick=\"this.innerHTML='scanning...';\">rescan</a>"
"<p><label>WiFi WEP/WPA password:<br>"
"<input name=password value='"
));
if (slurp("/wifi-password").length()) http.sendContent("##**##**##**");
http.sendContent(F("'></label><hr>"));
for (auto p : params) {
http.sendContent(p->html());
http.sendContent("<p>");
}
http.sendContent(F("<input type=submit value=Save></form>"));
});
http.on("/", HTTP_POST, [this]() {
spurt("/wifi-ssid", http.arg("ssid"));
String pw = http.arg("password");
if (pw != "##**##**##**") {
spurt("/wifi-password", pw);
}
for (auto p : params) p->store(http.arg(p->name));
http.sendHeader("Location", "/");
http.send(302, "text/plain", "ok");
if (onConfigSaved) onConfigSaved();
});
http.on("/restart", HTTP_POST, [this]() {
http.send(200, "text/plain", "Doei!");
if (onRestart) onRestart();
ESP.restart();
});
http.on("/rescan", HTTP_GET, [this]() {
http.sendHeader("Location", "/");
http.send(302, "text/plain", "wait for it...");
num_networks = WiFi.scanNetworks();
});
http.onNotFound([this]() {
http.sendHeader("Location", "http://" + hostname + "/");
http.send(302, "text/plain", "hoi");
});
http.begin();
for (;;) {
http.handleClient();
dns.processNextRequest();
if (onPortalWaitLoop) onPortalWaitLoop();
esp_task_wdt_reset();
delay(1);
}
}
bool WiFiSettingsClass::connect(bool portal, int wait_seconds) {
begin();
WiFi.mode(WIFI_STA);
String ssid = slurp("/wifi-ssid");
String pw = slurp("/wifi-password");
if (ssid.length() == 0 && portal) {
Serial.println("First contact!\n");
this->portal();
}
Serial.printf("Connecting to WiFi SSID '%s'", ssid.c_str());
if (onConnect) onConnect();
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); // arduino-esp32 #2537
WiFi.setHostname(hostname.c_str());
WiFi.begin(ssid.c_str(), pw.c_str());
unsigned long starttime = millis();
while (WiFi.status() != WL_CONNECTED && (wait_seconds < 0 || (millis() - starttime) < wait_seconds * 1000)) {
Serial.print(".");
delay(onWaitLoop ? onWaitLoop() : 100);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println(" failed.");
if (onFailure) onFailure();
if (portal) this->portal();
return false;
}
Serial.println(WiFi.localIP().toString());
if (onSuccess) onSuccess();
return true;
}
void WiFiSettingsClass::begin() {
if (begun) return;
begun = true;
// These things can't go in the constructor because the constructor runs
// before ESPFS.begin()
if (!secure) {
secure = checkbox(
"WiFiSettings-secure",
false,
F("Protect the configuration portal with a WiFi password")
);
}
if (!password.length()) {
password = string(
"WiFiSettings-password",
8, 63,
"",
F("WiFi password for the configuration portal")
);
if (password == "") {
// With regular 'init' semantics, the password would be changed
// all the time.
password = pwgen();
params.back()->store(password);
}
}
}
WiFiSettingsClass::WiFiSettingsClass() {
#ifdef ESP32
hostname = Sprintf("esp32-%06" PRIx64, ESP.getEfuseMac() >> 24);
#else
hostname = Sprintf("esp8266-%06" PRIx32, ESP.getChipId());
#endif
}
WiFiSettingsClass WiFiSettings;