#include "webinterface.h" #include #include #include #include #include #include #include "parameters.h" #include "romfs.h" #include "check_firmware.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" }, { ".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; #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) typedef struct { String name; String value; } json_table_t; /* create a json string from a table */ static String json_format(const json_table_t *table, uint8_t n) { String s = "{"; for (uint8_t i=0; i 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(lead_bytes, lead_len)) { Serial.printf("failed firmware check\n"); } else if (Update.end(true)) { Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); } else { Update.printError(Serial); } } }); Serial.printf("WAP started\n"); server.begin(); } void WebInterface::update() { if (!initialised) { init(); initialised = true; } server.handleClient(); }