-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathwebinterface.cpp
More file actions
172 lines (157 loc) · 5.24 KB
/
Copy pathwebinterface.cpp
File metadata and controls
172 lines (157 loc) · 5.24 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
#include "webinterface.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <WiFiAP.h>
#include <ESPmDNS.h>
#include <Update.h>
#include "parameters.h"
#include "romfs.h"
#include "check_firmware.h"
#include "status.h"
static WebServer server(80);
/*
serve files from ROMFS
*/
class ROMFS_Handler : public RequestHandler
{
bool canHandle(HTTPMethod method, String uri) {
if (uri == "/") {
uri = "/index.html";
}
uri = "web" + uri;
if (ROMFS::exists(uri.c_str())) {
return true;
}
return false;
}
bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) {
if (requestUri == "/") {
requestUri = "/index.html";
}
String uri = "web" + requestUri;
Serial.printf("handle: '%s'\n", requestUri.c_str());
// work out content type
const char *content_type = "text/html";
const struct {
const char *extension;
const char *content_type;
} extensions[] = {
{ ".js", "text/javascript" },
{ ".jpg", "image/jpeg" },
{ ".png", "image/png" },
{ ".css", "text/css" },
};
for (const auto &e : extensions) {
if (uri.endsWith(e.extension)) {
content_type = e.content_type;
break;
}
}
auto *f = ROMFS::find_stream(uri.c_str());
if (f != nullptr) {
server.sendHeader("Content-Encoding", "gzip");
server.streamFile(*f, content_type);
delete f;
return true;
}
return false;
}
} ROMFS_Handler;
/*
serve files from ROMFS
*/
class AJAX_Handler : public RequestHandler
{
bool canHandle(HTTPMethod method, String uri) {
return uri == "/ajax/status.json";
}
bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) {
if (requestUri != "/ajax/status.json") {
return false;
}
server.send(200, "application/json", status_json());
return true;
}
} AJAX_Handler;
/*
init web server
*/
void WebInterface::init(void)
{
Serial.printf("WAP start %s %s\n", g.wifi_ssid, g.wifi_password);
IPAddress myIP = WiFi.softAPIP();
server.addHandler( &AJAX_Handler );
server.addHandler( &ROMFS_Handler );
/*handling uploading firmware file */
server.on("/update", HTTP_POST, []() {
if (Update.hasError()) {
server.sendHeader("Connection", "close");
server.send(500, "text/plain","FAIL");
Serial.printf("Update Failed: Update function has errors\n");
delay(5000);
} else {
server.sendHeader("Connection", "close");
server.send(200, "text/plain","OK");
Serial.printf("Update Success: \nRebooting...\n");
delay(1000);
ESP.restart();
}
}, [this]() {
HTTPUpload& upload = server.upload();
static const esp_partition_t* partition_new_firmware = esp_ota_get_next_update_partition(NULL); //get OTA partion to which we will write new firmware file;
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
lead_len = 0;
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
if (lead_len < sizeof(lead_bytes)) {
uint32_t n = sizeof(lead_bytes)-lead_len;
if (n > upload.currentSize) {
n = upload.currentSize;
}
memcpy(&lead_bytes[lead_len], upload.buf, n);
lead_len += n;
}
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
// write extra bytes to force flush of the buffer before we check signature
uint32_t extra = SPI_FLASH_SEC_SIZE+1;
while (extra--) {
uint8_t ff = 0xff;
Update.write(&ff, 1);
}
if (!CheckFirmware::check_OTA_next(partition_new_firmware, lead_bytes, lead_len)) {
Serial.printf("Update Failed: firmware checks have errors\n");
server.sendHeader("Connection", "close");
server.send(500, "text/plain","FAIL");
delay(5000);
} else if (Update.end(true)) {
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
server.sendHeader("Connection", "close");
server.send(200, "text/plain","OK");
} else {
Update.printError(Serial);
Serial.printf("Update Failed: Update.end function has errors\n");
server.sendHeader("Connection", "close");
server.send(500, "text/plain","FAIL");
delay(5000);
}
}
});
Serial.printf("WAP started\n");
server.begin();
}
void WebInterface::update()
{
if (!initialised) {
init();
initialised = true;
}
server.handleClient();
}