-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_ota.cpp
284 lines (256 loc) · 8.36 KB
/
setup_ota.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
#include "setup_ota.h"
extern ESP8266WebServer server;
extern Config config;
extern int packetCounter;
/***************************************************************************/
static String getContentType(const String& path) {
if (path.endsWith(".html")) return "text/html";
else if (path.endsWith(".htm")) return "text/html";
else if (path.endsWith(".css")) return "text/css";
else if (path.endsWith(".txt")) return "text/plain";
else if (path.endsWith(".js")) return "application/javascript";
else if (path.endsWith(".png")) return "image/png";
else if (path.endsWith(".gif")) return "image/gif";
else if (path.endsWith(".jpg")) return "image/jpeg";
else if (path.endsWith(".jpeg")) return "image/jpeg";
else if (path.endsWith(".ico")) return "image/x-icon";
else if (path.endsWith(".svg")) return "image/svg+xml";
else if (path.endsWith(".xml")) return "text/xml";
else if (path.endsWith(".pdf")) return "application/pdf";
else if (path.endsWith(".zip")) return "application/zip";
else if (path.endsWith(".gz")) return "application/x-gzip";
else if (path.endsWith(".json")) return "application/json";
return "application/octet-stream";
}
/***************************************************************************/
bool initialConfig() {
config.universe = 1;
config.offset = 0;
config.pixels = 12;
config.leds = 4;
config.white = 0;
config.brightness = 255;
config.hsv = 0;
config.mode = 1;
config.reverse = 0;
config.speed = 8;
config.position = 1;
return true;
}
bool loadConfig() {
Serial.println("loadConfig");
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
Serial.println("set pointer");
std::unique_ptr<char[]> buf(new char[size]);
Serial.println("read bytes");
configFile.readBytes(buf.get(), size);
Serial.println("close");
configFile.close();
Serial.println("jsonBuffer");
StaticJsonBuffer<300> jsonBuffer;
Serial.println("parseObject");
JsonObject& root = jsonBuffer.parseObject(buf.get());
Serial.println("root.success");
if (!root.success()) {
Serial.println("Failed to parse config file");
return false;
}
Serial.println("JSON_TO_CONFIG universe");
JSON_TO_CONFIG(universe, "universe");
JSON_TO_CONFIG(offset, "offset");
JSON_TO_CONFIG(pixels, "pixels");
JSON_TO_CONFIG(leds, "leds");
JSON_TO_CONFIG(white, "white");
JSON_TO_CONFIG(brightness, "brightness");
JSON_TO_CONFIG(hsv, "hsv");
JSON_TO_CONFIG(mode, "mode");
JSON_TO_CONFIG(reverse, "reverse");
JSON_TO_CONFIG(speed, "speed");
JSON_TO_CONFIG(position, "position");
Serial.println("loadConfig return");
return true;
}
bool saveConfig() {
Serial.println("saveConfig");
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
CONFIG_TO_JSON(universe, "universe");
CONFIG_TO_JSON(offset, "offset");
CONFIG_TO_JSON(pixels, "pixels");
CONFIG_TO_JSON(leds, "leds");
CONFIG_TO_JSON(white, "white");
CONFIG_TO_JSON(brightness, "brightness");
CONFIG_TO_JSON(hsv, "hsv");
CONFIG_TO_JSON(mode, "mode");
CONFIG_TO_JSON(reverse, "reverse");
CONFIG_TO_JSON(speed, "speed");
CONFIG_TO_JSON(position, "position");
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
return false;
}
else {
Serial.println("Writing to config file");
root.printTo(configFile);
configFile.close();
return true;
}
}
/***************************************************************************/
void handleUpdate1() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}
void handleUpdate2() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.setDebugOutput(true);
WiFiUDP::stopAll();
Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
yield();
}
void handleDirList() {
Serial.println("handleDirList");
String str = "";
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
str += dir.fileName();
str += " ";
str += dir.fileSize();
str += " bytes\r\n";
}
server.send(200, "text/plain", str);
}
void handleNotFound() {
Serial.println("handleNotFound");
if (SPIFFS.exists(server.uri())) {
handleStaticFile(server.uri());
}
else {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
}
void handleRedirect(String filename) {
char buf[64];
filename.toCharArray(buf, 64);
handleRedirect(filename);
}
void handleRedirect(const char * filename) {
Serial.println("handleRedirect");
server.sendHeader("Location", String(filename), true);
server.send(302, "text/plain", "");
}
void handleStaticFile(String filename) {
char buf[64];
filename.toCharArray(buf, 64);
handleStaticFile(buf);
}
void handleStaticFile(const char * filename) {
Serial.println("handleStaticFile");
File f = SPIFFS.open(filename, "r");
Serial.print("Opening file ");
Serial.print(filename);
if (!f) {
Serial.println(" FAILED");
server.send(500, "text/html", "File not found");
} else {
Serial.println(" OK");
String content = f.readString();
f.close();
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, getContentType(String(filename)), content);
}
}
void handleJSON() {
Serial.println("handleJSON");
String message = "HTTP Request\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
Serial.println(message);
// this gets called in response to either a PUT or a POST
if (server.hasArg("plain")) {
// parse it as JSON object
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(server.arg("plain"));
if (!root.success()) {
handleStaticFile("/reload_failed.html");
return;
}
JSON_TO_CONFIG(universe, "universe");
JSON_TO_CONFIG(offset, "offset");
JSON_TO_CONFIG(pixels, "pixels");
JSON_TO_CONFIG(leds, "leds");
JSON_TO_CONFIG(white, "white");
JSON_TO_CONFIG(brightness, "brightness");
JSON_TO_CONFIG(hsv, "hsv");
JSON_TO_CONFIG(mode, "mode");
JSON_TO_CONFIG(reverse, "reverse");
JSON_TO_CONFIG(speed, "speed");
JSON_TO_CONFIG(position, "position");
handleStaticFile("/reload_success.html");
}
else {
// parse it as key1=val1&key2=val2&key3=val3
KEYVAL_TO_CONFIG(universe, "universe");
KEYVAL_TO_CONFIG(offset, "offset");
KEYVAL_TO_CONFIG(pixels, "pixels");
KEYVAL_TO_CONFIG(leds, "leds");
KEYVAL_TO_CONFIG(white, "white");
KEYVAL_TO_CONFIG(brightness, "brightness");
KEYVAL_TO_CONFIG(hsv, "hsv");
KEYVAL_TO_CONFIG(mode, "mode");
KEYVAL_TO_CONFIG(reverse, "reverse");
KEYVAL_TO_CONFIG(speed, "speed");
KEYVAL_TO_CONFIG(position, "position");
handleStaticFile("/reload_success.html");
}
saveConfig();
}