-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifi_server.ipp
More file actions
286 lines (220 loc) · 9.86 KB
/
wifi_server.ipp
File metadata and controls
286 lines (220 loc) · 9.86 KB
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
#pragma once
#include "wifi_server.h"
#include "wifi_server_pages.h"
/* hostname for mDNS. Should work at least on windows. Try http://esp8266.local */
static const String myHostname = "modularmonitor.local";
static const IPAddress apIP(8, 8, 8, 8);
static const IPAddress netMsk(255, 255, 255, 0);
constexpr byte DNS_PORT = 53;
inline void __handle_wifi_event(const wifi_page_endpoints& ev)
{
auto& wifi = GET(MyWiFiPortal);
if (!wifi.m_build) {
LOGE(e_LOG_TAG::TAG_WIFI, "WiFi event handler being called without a WiFi set up! How? Unhandled!");
return;
}
const auto isIp = [](String str) {
for (size_t i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (c != '.' && (c < '0' || c > '9')) {
return false;
}
}
return true;
};
const auto toStringIp = [](IPAddress ip) {
String res = "";
for (int i = 0; i < 3; i++) {
res += String((ip >> (8 * i)) & 0xFF) + ".";
}
res += String(((ip >> 8 * 3)) & 0xFF);
return res;
};
const auto redirectAuto = [&] {
wifi.m_build->server.sendHeader("Location", String("http://") + myHostname, true);
wifi.m_build->server.send(302, "text/plain", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
wifi.m_build->server.client().stop(); // Stop is needed because we sent no content length
};
const auto captivePortal = [&] {
if (!isIp(wifi.m_build->server.hostHeader()) && wifi.m_build->server.hostHeader() != myHostname) {
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Captive portal detection, redirecting...");
redirectAuto();
return true;
}
return false;
};
if (captivePortal()) return;
switch(ev) {
case wifi_page_endpoints::NOT_FOUND:
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Requested %s, redirect...", wifi.m_build->server.hostHeader().c_str());
redirectAuto();
break;
case wifi_page_endpoints::ROOT:
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Requested /, replying.");
wifi.m_build->server.send(200, "text/html", get_webserver_home());
break;
case wifi_page_endpoints::CSS:
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Requested /css.css, replying.");
wifi.m_build->server.send(200, "text/css", get_webserver_css());
break;
break;
case wifi_page_endpoints::JS:
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Requested /js.js, replying.");
wifi.m_build->server.send(200, "text/javascript", get_webserver_js());
break;
case wifi_page_endpoints::DATETIME:
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Requested /time, replying.");
wifi.m_build->server.send(200, "application/json", String("{\"time\":") + String(get_time_ms()) + String("}"));
break;
case wifi_page_endpoints::BUILDTIME:
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Requested /build, replying.");
wifi.m_build->server.send(200, "application/json", String("{\"build\":\"") + String(__DATE__) + " " + String(__TIME__) + String("\"}"));
break;
case wifi_page_endpoints::DEVICECOUNT:
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Requested /get_device/count, replying.");
wifi.m_build->server.send(200, "application/json", String("{\"count\":") + String(CS::d2u(CS::device_id::_MAX)) + String("}"));
break;
}
}
inline void __handle_wifi_getdata(const uint8_t dev)
{
//LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Requested data of device %i, replying.", static_cast<int>(dev));
auto& wifi = GET(MyWiFiPortal);
if (!wifi.m_build) {
LOGE(e_LOG_TAG::TAG_WIFI, "WiFi event handler being called without a WiFi set up! How? Unhandled!");
return;
}
auto& sv = wifi.m_build->server;
if (dev >= CS::d2u(CS::device_id::_MAX)) { // cancel on numbers not valid
sv.send(501, "text/plain", ""); // Not Implemented
sv.client().stop();
return;
}
const CS::device_id dev_id = static_cast<CS::device_id>(dev);
const MyI2Ccomm& com = GET(MyI2Ccomm);
constexpr size_t max_time_back = MyI2Ccomm::get_max_history_size();
const unsigned maxx = com.get_device_configurations(dev_id, 0).m_map.size();
const long index = sv.hasArg("index") ? sv.arg("index").toInt() : -1;
// show resumed only (online, has issues)
const bool resumed = sv.hasArg("resumed") ? (sv.arg("resumed") == "true") : (index < 0 || index >= maxx);
String json_build = "{";
if (com.is_device_online(dev_id)) json_build += "\"online\":true,";
else json_build += "\"online\":false,";
if (com.is_device_with_issue(dev_id)) json_build += "\"has_issues\":true,";
else json_build += "\"has_issues\":false,";
json_build += "\"max_index\":" + String(maxx) + ","
"\"name\":\"" + String(CS::d2str(dev_id)) + "\","
"\"last_updated\":" + String(com.get_device_configurations(dev_id, 0).m_update_time);
if (!resumed) {
json_build += ",\"path\":\"" + String(com.get_device_data_in_time(dev_id, 0, static_cast<size_t>(index)).first.c_str()) + "\""
",\"data\":[";
for(int p = static_cast<int>(max_time_back) - 1; p >= 0; --p) {
const i2c_data_pair& it = com.get_device_data_in_time(dev_id, (p + 1) % max_time_back, static_cast<size_t>(index));
//json_build += "{\"name\":\"" + String(it.first.c_str()) + "\",\"value\":" + String(std::strtod(it.second, nullptr)) + "}";
json_build += String(std::strtod(it.second, nullptr), 6);
if (p > 0) json_build += ",";
}
json_build += "]";
}
json_build += "}";
sv.send(200, "application/json", json_build);
}
std::string random_wifi_name();
std::string random_wifi_password();
inline MyWiFiPortal::ref::ref()
: dnsServer(), server(80)
{}
inline void MyWiFiPortal::handle_requests()
{
LOGI(e_LOG_TAG::TAG_WIFI, "WiFi handler thread started!");
if (!m_build) {
LOGE(e_LOG_TAG::TAG_WIFI, "WiFi handler thread got an issue. How is m_build not initialized yet? ABORT!");
vTaskDelete(NULL);
return;
}
while(1) {
m_build->server.handleClient();
SLEEP(50);
}
}
inline MyWiFiPortal::MyWiFiPortal()
//: m_ssid(random_wifi_name()), m_pass(random_wifi_password())
: m_ssid("ESP32TEST"), m_pass("mymom123")
{
const std::string tmp = "WIFI:T:WPA;S:" + m_ssid + ";P:" + m_pass + ";H:false;";
m_qrcode = std::unique_ptr<qrcodegen::QrCode>(new qrcodegen::QrCode(qrcodegen::QrCode::encodeText(tmp.c_str(), qrcodegen::QrCode::Ecc::ECC_LOW)));
}
inline void MyWiFiPortal::start()
{
if (m_thr) return;
LOGI(e_LOG_TAG::TAG_WIFI, "Configuring WiFi Portal...");
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.mode(WIFI_AP);
WiFi.softAP(m_ssid.c_str(), m_pass.c_str());
m_build = std::unique_ptr<ref>(new ref());
m_build->dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
m_build->dnsServer.start(DNS_PORT, "*", apIP);
m_build->server.on("/", [] { __handle_wifi_event(wifi_page_endpoints::ROOT); });
m_build->server.on("/js.js", [] { __handle_wifi_event(wifi_page_endpoints::JS); });
m_build->server.on("/css.css", [] { __handle_wifi_event(wifi_page_endpoints::CSS); });
m_build->server.on("/time", [] { __handle_wifi_event(wifi_page_endpoints::DATETIME); });
m_build->server.on("/build", [] { __handle_wifi_event(wifi_page_endpoints::BUILDTIME); });
m_build->server.on("/get_device/count", [] { __handle_wifi_event(wifi_page_endpoints::DEVICECOUNT); });
for(uint8_t dev = 0; dev < CS::d2u(CS::device_id::_MAX); ++dev) {
m_build->server.on(String("/get_device/") + dev, [cur = dev]() { __handle_wifi_getdata(cur); });
}
// Android fix from "https://github.com/espressif/arduino-esp32/issues/1037"
//m_build->server.on("/generate_204", [] { __handle_wifi_event(wifi_page_endpoints::NOT_FOUND); });
m_build->server.onNotFound([]{ __handle_wifi_event(wifi_page_endpoints::NOT_FOUND); });
m_build->server.begin();
m_thr = async_class_method(MyWiFiPortal, handle_requests, cpu_core_id_for_wifi_setup);
LOGI(e_LOG_TAG::TAG_WIFI, "Loading files into memory (max 5 tries)...");
for(uint8_t tries = 0; tries < 5 && (get_webserver_home().isEmpty() || get_webserver_css().isEmpty() || get_webserver_js().isEmpty()); ++tries) {
LOGI_NOSD(e_LOG_TAG::TAG_WIFI, "Loading WiFi files try %u of 5...", (unsigned)tries);
reload_webserver_items();
}
LOGI(e_LOG_TAG::TAG_WIFI, "Started WiFi Portal handler thread.");
}
inline void MyWiFiPortal::stop()
{
LOGI(e_LOG_TAG::TAG_WIFI, "Stopping WiFi Portal...");
if (m_thr) vTaskDelete(m_thr);
m_thr = nullptr;
m_build->server.stop();
m_build->dnsServer.stop();
m_build.reset();
//WiFi.softAPdisconnect(true); // crashes?
LOGI(e_LOG_TAG::TAG_WIFI, "Stopped WiFi Portal.");
}
inline const qrcodegen::QrCode& MyWiFiPortal::get_qr_code() const
{
return *m_qrcode;
}
inline const std::string& MyWiFiPortal::get_ssid() const
{
return m_ssid;
}
inline const std::string& MyWiFiPortal::get_password() const
{
return m_pass;
}
inline std::string random_wifi_name()
{
std::string res;
if (esp_random() % 2 == 0) res += "fus";
else res += "cli";
for(size_t rc = 0; rc < 6; ++rc) res += (esp_random() % 2 == 0) ? ('A' + (esp_random() % 26)) : ('a' + (esp_random() % 26));
return res;
}
inline std::string random_wifi_password()
{
std::string res;
for(size_t rc = 0; rc < 8; ++rc) {
switch(esp_random() % 3) {
case 0: res += ('A' + (esp_random() % 26)); break;
case 1: res += ('a' + (esp_random() % 26)); break;
case 2: res += ('0' + (esp_random() % 10)); break;
}
}
return res;
}