2021-03-04 21:22:49 +01:00
|
|
|
#include "boot.h"
|
|
|
|
#include "reset.h"
|
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
|
|
|
|
|
|
|
// start local web server with user interface for maintenance mode
|
|
|
|
// used for manually uploading a firmware file via wifi
|
|
|
|
|
|
|
|
void start_boot_menu(void) {
|
|
|
|
|
|
|
|
uint8_t mac[6];
|
|
|
|
char clientId[20];
|
|
|
|
|
|
|
|
// hash 6 byte MAC to 4 byte hash
|
|
|
|
esp_eth_get_mac(mac);
|
|
|
|
const uint32_t hashedmac = myhash((const char *)mac, 6);
|
|
|
|
snprintf(clientId, 20, "paxcounter_%08x", hashedmac);
|
|
|
|
|
|
|
|
const char *host = clientId;
|
|
|
|
const char *ssid = WIFI_SSID;
|
|
|
|
const char *password = WIFI_PASS;
|
|
|
|
|
2021-03-06 13:55:38 +01:00
|
|
|
RTC_runmode = RUNMODE_NORMAL;
|
|
|
|
|
2021-03-04 21:22:49 +01:00
|
|
|
hw_timer_t *timer = NULL;
|
2021-03-06 13:55:38 +01:00
|
|
|
timer = timerBegin(2, 80, true); // timer 2, div 80, countup
|
|
|
|
timerAttachInterrupt(timer, &esp_restart, true); // callback device reset
|
2021-03-04 21:22:49 +01:00
|
|
|
timerAlarmWrite(timer, BOOTDELAY * 1000000, false); // set time in us
|
|
|
|
timerAlarmEnable(timer); // enable interrupt
|
|
|
|
|
|
|
|
WebServer server(80);
|
|
|
|
|
2021-03-05 17:00:28 +01:00
|
|
|
const char *loginMenu =
|
|
|
|
"<form name='loginForm'>"
|
|
|
|
"<table width='20%' bgcolor='A09F9F' align='center'>"
|
|
|
|
"<tr>"
|
|
|
|
"<td colspan=2>"
|
|
|
|
"<center><font size=4><b>Maintenance Menu</b></font></center>"
|
|
|
|
"<br>"
|
|
|
|
"</td>"
|
|
|
|
"<br>"
|
|
|
|
"<br>"
|
|
|
|
"</tr>"
|
|
|
|
"<tr>"
|
2021-03-06 13:55:38 +01:00
|
|
|
"<td><input type='submit' onclick='start(this.form)' "
|
|
|
|
"value='Start'></td>"
|
2021-03-05 17:00:28 +01:00
|
|
|
"</tr>"
|
|
|
|
"</table>"
|
|
|
|
"</form>"
|
|
|
|
"<script>"
|
|
|
|
"function start(form) {window.open('/serverIndex')}"
|
|
|
|
"</script>";
|
2021-03-04 21:22:49 +01:00
|
|
|
|
|
|
|
const char *serverIndex =
|
|
|
|
"<script "
|
|
|
|
"src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/"
|
|
|
|
"jquery.min.js'></script>"
|
|
|
|
"<form method='POST' action='#' enctype='multipart/form-data' "
|
|
|
|
"id='upload_form'>"
|
|
|
|
"<input type='file' name='update'>"
|
|
|
|
"<input type='submit' value='Update'>"
|
|
|
|
"</form>"
|
|
|
|
"<div id='prg'>progress: 0%</div>"
|
|
|
|
"<script>"
|
|
|
|
"$('form').submit(function(e){"
|
|
|
|
"e.preventDefault();"
|
|
|
|
"var form = $('#upload_form')[0];"
|
|
|
|
"var data = new FormData(form);"
|
|
|
|
" $.ajax({"
|
|
|
|
"url: '/update',"
|
|
|
|
"type: 'POST',"
|
|
|
|
"data: data,"
|
|
|
|
"contentType: false,"
|
|
|
|
"processData:false,"
|
|
|
|
"xhr: function() {"
|
|
|
|
"var xhr = new window.XMLHttpRequest();"
|
|
|
|
"xhr.upload.addEventListener('progress', function(evt) {"
|
|
|
|
"if (evt.lengthComputable) {"
|
|
|
|
"var per = evt.loaded / evt.total;"
|
|
|
|
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
|
|
|
|
"}"
|
|
|
|
"}, false);"
|
|
|
|
"return xhr;"
|
|
|
|
"},"
|
|
|
|
"success:function(d, s) {"
|
|
|
|
"console.log('success!')"
|
|
|
|
"},"
|
|
|
|
"error: function (a, b, c) {"
|
|
|
|
"}"
|
|
|
|
"});"
|
|
|
|
"});"
|
|
|
|
"</script>";
|
|
|
|
|
2021-03-06 16:23:28 +01:00
|
|
|
WiFi.disconnect(true);
|
|
|
|
WiFi.config(INADDR_NONE, INADDR_NONE,
|
|
|
|
INADDR_NONE); // call is only a workaround for bug in WiFi class
|
|
|
|
// see https://github.com/espressif/arduino-esp32/issues/806
|
|
|
|
WiFi.setHostname(host);
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
2021-03-04 21:22:49 +01:00
|
|
|
// Connect to WiFi network
|
2021-03-06 13:55:38 +01:00
|
|
|
// workaround applied here to avoid WIFI_AUTH failure
|
|
|
|
// see https://github.com/espressif/arduino-esp32/issues/2501
|
|
|
|
// 1st try
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() == WL_DISCONNECTED) {
|
2021-03-04 21:22:49 +01:00
|
|
|
delay(500);
|
|
|
|
}
|
2021-03-06 13:55:38 +01:00
|
|
|
// 2nd try
|
|
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:22:49 +01:00
|
|
|
MDNS.begin(host);
|
|
|
|
|
2021-03-05 17:00:28 +01:00
|
|
|
server.on("/", HTTP_GET, [&server, &loginMenu]() {
|
|
|
|
server.sendHeader("Connection", "close");
|
|
|
|
server.send(200, "text/html", loginMenu);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.on("/serverIndex", HTTP_GET, [&server, &serverIndex, &timer]() {
|
|
|
|
timerAlarmWrite(timer, BOOTTIMEOUT * 1000000, false);
|
2021-03-04 21:22:49 +01:00
|
|
|
server.sendHeader("Connection", "close");
|
|
|
|
server.send(200, "text/html", serverIndex);
|
|
|
|
});
|
|
|
|
|
|
|
|
// handling uploading firmware file
|
|
|
|
server.on(
|
|
|
|
"/update", HTTP_POST,
|
|
|
|
[&server]() {
|
|
|
|
server.sendHeader("Connection", "close");
|
|
|
|
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
|
2021-03-06 13:55:38 +01:00
|
|
|
RTC_runmode = Update.hasError() ? RUNMODE_NORMAL : RUNMODE_POWERCYCLE;
|
2021-03-06 16:23:28 +01:00
|
|
|
WiFi.disconnect(true);
|
2021-03-06 13:55:38 +01:00
|
|
|
esp_restart();
|
2021-03-04 21:22:49 +01:00
|
|
|
},
|
|
|
|
[&server, &timer]() {
|
|
|
|
HTTPUpload &upload = server.upload();
|
|
|
|
if (upload.status == UPLOAD_FILE_START) {
|
|
|
|
ESP_LOGI(TAG, "Update: %s\n", upload.filename.c_str());
|
2021-03-05 17:00:28 +01:00
|
|
|
#if (HAS_LED != NOT_A_PIN)
|
|
|
|
#ifndef LED_ACTIVE_LOW
|
|
|
|
if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASH, HAS_LED, HIGH)) {
|
|
|
|
#else
|
|
|
|
if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASH, HAS_LED, LOW)) {
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
|
|
|
|
#endif
|
2021-03-04 21:22:49 +01:00
|
|
|
ESP_LOGE(TAG, "Error: %s", Update.errorString());
|
|
|
|
}
|
|
|
|
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
|
|
|
// flashing firmware to ESP
|
|
|
|
if (Update.write(upload.buf, upload.currentSize) !=
|
|
|
|
upload.currentSize) {
|
|
|
|
ESP_LOGE(TAG, "Error: %s", Update.errorString());
|
|
|
|
}
|
|
|
|
} else if (upload.status == UPLOAD_FILE_END) {
|
|
|
|
if (Update.end(
|
|
|
|
true)) { // true to set the size to the current progress
|
|
|
|
ESP_LOGI(TAG, "Update finished, %u bytes written",
|
|
|
|
upload.totalSize);
|
|
|
|
} else {
|
2021-03-05 20:09:35 +01:00
|
|
|
ESP_LOGE(TAG, "Update failed, status=%d", upload.status);
|
2021-03-04 21:22:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.begin();
|
|
|
|
MDNS.addService("http", "tcp", 80);
|
2021-03-05 17:00:28 +01:00
|
|
|
ESP_LOGI(TAG,
|
|
|
|
"WiFi connected to '%s', open http://%s.local or http://%s in your "
|
|
|
|
"browser",
|
|
|
|
WiFi.SSID().c_str(), clientId, WiFi.localIP().toString().c_str());
|
2021-03-04 21:22:49 +01:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
server.handleClient();
|
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
}
|