From d6de72f1e5472128a1c63adfec86942a376d2515 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Mon, 19 Oct 2020 19:35:58 +0200 Subject: [PATCH] Code size optimization --- tasmota/xdrv_01_webserver.ino | 61 ++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index eb0644c3f..2464ad9dc 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -844,33 +844,56 @@ void ExecuteWebCommand(char* svalue, uint32_t source) ExecuteCommand(svalue, SRC_IGNORE); } +// replace the series of `Webserver->on()` with a table in PROGMEM +typedef struct WebServerDispatch_t { + char uri[3]; // the prefix "/" is added automatically + uint8_t method; + void (*handler)(void); +} WebServerDispatch_t; + +const WebServerDispatch_t WebServerDispatch[] PROGMEM = { + { "", HTTP_ANY, HandleRoot }, + { "up", HTTP_ANY, HandleUpgradeFirmware }, + { "u1", HTTP_ANY, HandleUpgradeFirmwareStart }, // OTA + { "u2", HTTP_OPTIONS, HandlePreflightRequest }, + { "u3", HTTP_ANY, HandleUploadDone }, + { "cs", HTTP_GET, HandleConsole }, + { "cs", HTTP_OPTIONS, HandlePreflightRequest }, + { "cm", HTTP_ANY, HandleHttpCommand }, +#ifndef FIRMWARE_MINIMAL + { "cn", HTTP_ANY, HandleConfiguration }, + { "md", HTTP_ANY, HandleModuleConfiguration }, + { "wi", HTTP_ANY, HandleWifiConfiguration }, + { "lg", HTTP_ANY, HandleLoggingConfiguration }, + { "tp", HTTP_ANY, HandleTemplateConfiguration }, + { "co", HTTP_ANY, HandleOtherConfiguration }, + { "dl", HTTP_ANY, HandleBackupConfiguration }, + { "rs", HTTP_ANY, HandleRestoreConfiguration }, + { "rt", HTTP_ANY, HandleResetConfiguration }, + { "in", HTTP_ANY, HandleInformation }, +#endif // Not FIRMWARE_MINIMAL +}; + void StartWebserver(int type, IPAddress ipweb) { if (!Settings.web_refresh) { Settings.web_refresh = HTTP_REFRESH_TIME; } if (!Web.state) { if (!Webserver) { Webserver = new ESP8266WebServer((HTTP_MANAGER == type || HTTP_MANAGER_RESET_ONLY == type) ? 80 : WEB_PORT); - Webserver->on("/", HandleRoot); + // call `Webserver->on()` on each entry + for (uint32_t i=0; ion(uri, (HTTPMethod) method, line.handler); + } Webserver->onNotFound(HandleNotFound); - Webserver->on("/up", HandleUpgradeFirmware); - Webserver->on("/u1", HandleUpgradeFirmwareStart); // OTA - Webserver->on("/u2", HTTP_POST, HandleUploadDone, HandleUploadLoop); - Webserver->on("/u2", HTTP_OPTIONS, HandlePreflightRequest); - Webserver->on("/u3", HandleUploadDone); - Webserver->on("/cs", HTTP_GET, HandleConsole); - Webserver->on("/cs", HTTP_OPTIONS, HandlePreflightRequest); - Webserver->on("/cm", HandleHttpCommand); + Webserver->on("/u2", HTTP_POST, HandleUploadDone, HandleUploadLoop); // this call requires 2 functions so we keep a direct call #ifndef FIRMWARE_MINIMAL - Webserver->on("/cn", HandleConfiguration); - Webserver->on("/md", HandleModuleConfiguration); - Webserver->on("/wi", HandleWifiConfiguration); - Webserver->on("/lg", HandleLoggingConfiguration); - Webserver->on("/tp", HandleTemplateConfiguration); - Webserver->on("/co", HandleOtherConfiguration); - Webserver->on("/dl", HandleBackupConfiguration); - Webserver->on("/rs", HandleRestoreConfiguration); - Webserver->on("/rt", HandleResetConfiguration); - Webserver->on("/in", HandleInformation); XdrvCall(FUNC_WEB_ADD_HANDLER); XsnsCall(FUNC_WEB_ADD_HANDLER); #endif // Not FIRMWARE_MINIMAL