Skip to content

Commit 7cf4049

Browse files
committed
Code refactoring
1 parent 23d740d commit 7cf4049

File tree

4 files changed

+109
-142
lines changed

4 files changed

+109
-142
lines changed

data/.keep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This folder is left empty to be able to generate an FS image with: pio run -e esp32dev -t buildfs

embed/website.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ <h6 id="clickable_file_input">or<br />click to select (.bin) file</h6>
403403
<div class="info_text_value" id="info_text_build">Unknown</div>
404404
</div>
405405
</div>
406-
<button id="restart_button">Cancel SafeBoot</button>
406+
<button id="restart_button">Restart</button>
407407
</div>
408408
</div>
409409
<script>

src/HTTPUpdateServer.h

Lines changed: 0 additions & 113 deletions
This file was deleted.

src/main.cpp

Lines changed: 107 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55

66
#include <ArduinoOTA.h>
7-
#include <HTTPUpdateServer.h>
87
#include <HardwareSerial.h>
98
#include <MycilaESPConnect.h>
9+
#include <StreamString.h>
10+
#include <WebServer.h>
1011

1112
#include <esp_ota_ops.h>
1213
#include <esp_partition.h>
@@ -21,10 +22,16 @@
2122
#define LOG(format, ...)
2223
#endif
2324

25+
extern const char* __COMPILED_APP_VERSION__;
26+
extern const uint8_t update_html_start[] asm("_binary__pio_embed_website_html_gz_start");
27+
extern const uint8_t update_html_end[] asm("_binary__pio_embed_website_html_gz_end");
28+
static const char* successResponse = "Update Success! Rebooting...";
29+
static const char* cancelResponse = "Rebooting...";
30+
2431
static WebServer webServer(80);
25-
static HTTPUpdateServer httpUpdater;
2632
static Mycila::ESPConnect espConnect;
2733
static Mycila::ESPConnect::Config espConnectConfig;
34+
static StreamString updaterError;
2835

2936
static String getChipIDStr() {
3037
uint32_t chipId = 0;
@@ -36,33 +43,87 @@ static String getChipIDStr() {
3643
return espId;
3744
}
3845

39-
void setup() {
40-
#ifdef SAFEBOOT_LOGGING
41-
Serial.begin(115200);
42-
#if ARDUINO_USB_CDC_ON_BOOT
43-
Serial.setTxTimeoutMs(0);
44-
delay(100);
45-
#else
46-
while (!Serial)
47-
yield();
48-
#endif
49-
#endif
46+
static void start_web_server() {
47+
webServer.on("/cancel", HTTP_POST, [&]() {
48+
webServer.send(200, "text/plain", cancelResponse);
49+
webServer.client().stop();
50+
delay(500);
51+
ESP.restart(); }, [&]() {});
5052

51-
LOG("Version: %s\n", __COMPILED_APP_VERSION__);
53+
webServer.on("/", HTTP_GET, [&]() {
54+
webServer.sendHeader("Content-Encoding", "gzip");
55+
webServer.send_P(200, "text/html", reinterpret_cast<const char*>(update_html_start), update_html_end - update_html_start);
56+
});
5257

53-
// Set next boot partition
54-
const esp_partition_t* partition = esp_partition_find_first(esp_partition_type_t::ESP_PARTITION_TYPE_APP, esp_partition_subtype_t::ESP_PARTITION_SUBTYPE_APP_OTA_0, nullptr);
55-
if (partition) {
56-
esp_ota_set_boot_partition(partition);
57-
}
58+
webServer.on("/", HTTP_POST, [&]() {
59+
if (Update.hasError()) {
60+
webServer.send(500, "text/plain", "Update error: " + updaterError);
61+
} else {
62+
webServer.client().setNoDelay(true);
63+
webServer.send(200, "text/plain", successResponse);
64+
webServer.client().stop();
65+
delay(500);
66+
ESP.restart();
67+
} }, [&]() {
68+
// handler for the file upload, gets the sketch bytes, and writes
69+
// them through the Update object
70+
HTTPUpload& upload = webServer.upload();
71+
72+
if (upload.status == UPLOAD_FILE_START) {
73+
updaterError.clear();
74+
int otaMode = U_FLASH;
75+
if (webServer.hasArg("mode") && webServer.arg("mode") == "1") {
76+
otaMode = U_SPIFFS;
77+
}
78+
LOG("Mode: %d\n", otaMode);
79+
if (!Update.begin(UPDATE_SIZE_UNKNOWN, otaMode)) { // start with max available size
80+
Update.printError(updaterError);
81+
}
82+
} else if (upload.status == UPLOAD_FILE_WRITE && !updaterError.length()) {
83+
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
84+
Update.printError(updaterError);
85+
}
86+
} else if (upload.status == UPLOAD_FILE_END && !updaterError.length()) {
87+
if (!Update.end(true)) { // true to set the size to the current progress
88+
Update.printError(updaterError);
89+
}
90+
} else if (upload.status == UPLOAD_FILE_ABORTED) {
91+
Update.end();
92+
}
93+
delay(0); });
94+
95+
webServer.on("/chipspecs", HTTP_GET, [&]() {
96+
String chipSpecs = ESP.getChipModel();
97+
chipSpecs += " (" + String(ESP.getFlashChipSize() >> 20) + " MB)";
98+
webServer.send(200, "text/plain", chipSpecs.c_str());
99+
});
100+
101+
webServer.on("/sbversion", HTTP_GET, [&]() {
102+
webServer.send(200, "text/plain", __COMPILED_APP_VERSION__);
103+
});
58104

59-
// setup routes
60-
httpUpdater.setup(&webServer, "/");
61105
webServer.onNotFound([]() {
62106
webServer.sendHeader("Location", "/");
63107
webServer.send(302, "text/plain", "");
64108
});
65109

110+
webServer.begin();
111+
112+
#ifndef MYCILA_SAFEBOOT_NO_MDNS
113+
MDNS.addService("http", "tcp", 80);
114+
#endif
115+
116+
LOG("Web Server started\n");
117+
}
118+
119+
static void set_next_partition_to_boot() {
120+
const esp_partition_t* partition = esp_partition_find_first(esp_partition_type_t::ESP_PARTITION_TYPE_APP, esp_partition_subtype_t::ESP_PARTITION_SUBTYPE_APP_OTA_0, nullptr);
121+
if (partition) {
122+
esp_ota_set_boot_partition(partition);
123+
}
124+
}
125+
126+
static void start_network_manager() {
66127
// load ESPConnect configuration
67128
espConnect.loadConfiguration(espConnectConfig);
68129
espConnect.setBlocking(true);
@@ -105,25 +166,43 @@ void setup() {
105166
}
106167
LOG("IP: %s\n", espConnect.getIPAddress().toString().c_str());
107168
LOG("Hostname: %s\n", espConnect.getHostname().c_str());
169+
}
108170

109-
// start http
110-
webServer.begin();
111-
LOG("Web Server started\n");
112-
171+
static void start_mdns() {
113172
#ifndef MYCILA_SAFEBOOT_NO_MDNS
114-
// Start mDNS
115173
MDNS.begin(espConnectConfig.hostname.c_str());
116-
MDNS.addService("http", "tcp", 80);
174+
LOG("mDNS started\n");
117175
#endif
176+
}
118177

119-
// Start OTA
178+
static void start_arduino_ota() {
120179
ArduinoOTA.setHostname(espConnectConfig.hostname.c_str());
121180
ArduinoOTA.setRebootOnSuccess(true);
122181
ArduinoOTA.setMdnsEnabled(true);
123182
ArduinoOTA.begin();
124183
LOG("OTA Server started on port 3232\n");
125184
}
126185

186+
void setup() {
187+
#ifdef SAFEBOOT_LOGGING
188+
Serial.begin(115200);
189+
#if ARDUINO_USB_CDC_ON_BOOT
190+
Serial.setTxTimeoutMs(0);
191+
delay(100);
192+
#else
193+
while (!Serial)
194+
yield();
195+
#endif
196+
#endif
197+
198+
LOG("Version: %s\n", __COMPILED_APP_VERSION__);
199+
set_next_partition_to_boot();
200+
start_network_manager();
201+
start_mdns();
202+
start_web_server();
203+
start_arduino_ota();
204+
}
205+
127206
void loop() {
128207
webServer.handleClient();
129208
ArduinoOTA.handle();

0 commit comments

Comments
 (0)