From 1f40da076afade97f9bb93112d16bb7a76fe6c99 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Wed, 23 Dec 2020 04:17:41 +0100 Subject: [PATCH] Add HASP_USE_CONFIG option --- platformio.ini | 1 + src/hasp.cpp | 3 +- src/hasp_config.cpp | 6 +- src/hasp_config.h | 6 +- src/hasp_debug.cpp | 60 +++++---- src/hasp_debug.h | 2 + src/hasp_dispatch.cpp | 65 +++++---- src/hasp_gpio.cpp | 2 + src/hasp_gpio.h | 3 + src/hasp_gui.cpp | 2 + src/hasp_gui.h | 4 +- src/hasp_http.cpp | 307 +++++++++++++++++++++--------------------- src/hasp_http.h | 2 + src/hasp_mdns.cpp | 2 + src/hasp_mdns.h | 2 + src/hasp_mqtt.cpp | 2 + src/hasp_mqtt.h | 2 + src/hasp_oobe.cpp | 5 +- src/hasp_oobe.h | 6 +- src/hasp_telnet.cpp | 2 + src/hasp_telnet.h | 3 +- src/hasp_wifi.cpp | 3 +- src/hasp_wifi.h | 2 + src/main.cpp | 7 +- 24 files changed, 280 insertions(+), 219 deletions(-) diff --git a/platformio.ini b/platformio.ini index b5b850a6..27230118 100644 --- a/platformio.ini +++ b/platformio.ini @@ -60,6 +60,7 @@ build_flags = -D HASP_VERSION_MAJOR=0 -D HASP_VERSION_MINOR=3 -D HASP_VERSION_REVISION=1 + -D HASP_USE_CONFIG=1 ; Native application, not library ; -- Shared library dependencies in all environments ; Warning : don't put comments after github links => causes infinite download loop diff --git a/src/hasp.cpp b/src/hasp.cpp index 3b984dba..a7f37861 100644 --- a/src/hasp.cpp +++ b/src/hasp.cpp @@ -527,7 +527,7 @@ void haspLoadPage(const char * pages) } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - +#if HASP_USE_CONFIG > 0 bool haspGetConfig(const JsonObject & settings) { bool changed = false; @@ -584,3 +584,4 @@ bool haspSetConfig(const JsonObject & settings) return changed; } +#endif // HASP_USE_CONFIG diff --git a/src/hasp_config.cpp b/src/hasp_config.cpp index 4bb37968..65f34bc0 100644 --- a/src/hasp_config.cpp +++ b/src/hasp_config.cpp @@ -1,6 +1,8 @@ /* MIT License - Copyright (c) 2020 Francis Van Roie For full license information read the LICENSE file in the project folder */ +#if HASP_USE_CONFIG > 0 + #include "ArduinoJson.h" #include "StreamUtils.h" // For EEPromStream @@ -478,4 +480,6 @@ bool configClearEeprom() #else return false; #endif -} \ No newline at end of file +} + +#endif // HAS_USE_CONFIG \ No newline at end of file diff --git a/src/hasp_config.h b/src/hasp_config.h index cf228c22..9a05d065 100644 --- a/src/hasp_config.h +++ b/src/hasp_config.h @@ -1,6 +1,8 @@ /* MIT License - Copyright (c) 2020 Francis Van Roie For full license information read the LICENSE file in the project folder */ +#if HASP_USE_CONFIG > 0 + #ifndef HASP_CONFIG_H #define HASP_CONFIG_H @@ -59,4 +61,6 @@ const char F_GPIO_CONFIG[] PROGMEM = "config"; const char HASP_CONFIG_FILE[] PROGMEM = "/config.json"; -#endif \ No newline at end of file +#endif + +#endif // HASP_USE_CONFIG \ No newline at end of file diff --git a/src/hasp_debug.cpp b/src/hasp_debug.cpp index dda4446a..2c8501d5 100644 --- a/src/hasp_debug.cpp +++ b/src/hasp_debug.cpp @@ -19,12 +19,12 @@ //#include "time.h" #if defined(ARDUINO_ARCH_ESP8266) -#include // sntp_servermode_dhcp() -#include -#include + #include // sntp_servermode_dhcp() + #include + #include #elif defined(ARDUINO_ARCH_ESP32) -#include -#include + #include + #include #endif #include "hasp.h" @@ -35,27 +35,27 @@ #include "hasp_dispatch.h" #ifdef USE_CONFIG_OVERRIDE -#include "user_config_override.h" + #include "user_config_override.h" #endif #ifndef SERIAL_SPEED -#define SERIAL_SPEED 115200 + #define SERIAL_SPEED 115200 #endif #if HASP_USE_SYSLOG > 0 -#include + #include -#ifndef SYSLOG_SERVER -#define SYSLOG_SERVER "" -#endif + #ifndef SYSLOG_SERVER + #define SYSLOG_SERVER "" + #endif -#ifndef SYSLOG_PORT -#define SYSLOG_PORT 514 -#endif + #ifndef SYSLOG_PORT + #define SYSLOG_PORT 514 + #endif -#ifndef APP_NAME -#define APP_NAME "HASP" -#endif + #ifndef APP_NAME + #define APP_NAME "HASP" + #endif // variables for debug stream writer // static String debugOutput((char *)0); @@ -70,7 +70,7 @@ uint8_t debugSyslogProtocol = 0; // A UDP instance to let us send and receive packets over UDP WiFiUDP * syslogClient; -#define SYSLOG_PROTO_IETF 0 + #define SYSLOG_PROTO_IETF 0 // Create a new syslog instance with LOG_KERN facility // Syslog syslog(syslogClient, SYSLOG_SERVER, SYSLOG_PORT, MQTT_CLIENT, APP_NAME, LOG_KERN); @@ -193,6 +193,7 @@ void debugStop() if(debugSerialStarted) Serial.flush(); } +#if HASP_USE_CONFIG > 0 bool debugGetConfig(const JsonObject & settings) { bool changed = false; @@ -203,7 +204,7 @@ bool debugGetConfig(const JsonObject & settings) if(debugTelePeriod != settings[FPSTR(F_DEBUG_TELEPERIOD)].as()) changed = true; settings[FPSTR(F_DEBUG_TELEPERIOD)] = debugTelePeriod; -#if HASP_USE_SYSLOG > 0 + #if HASP_USE_SYSLOG > 0 if(strcmp(debugSyslogHost, settings[FPSTR(F_CONFIG_HOST)].as().c_str()) != 0) changed = true; settings[FPSTR(F_CONFIG_HOST)] = debugSyslogHost; @@ -215,7 +216,7 @@ bool debugGetConfig(const JsonObject & settings) if(debugSyslogFacility != settings[FPSTR(F_CONFIG_LOG)].as()) changed = true; settings[FPSTR(F_CONFIG_LOG)] = debugSyslogFacility; -#endif + #endif if(changed) configOutput(settings, TAG_DEBG); return changed; @@ -241,7 +242,7 @@ bool debugSetConfig(const JsonObject & settings) changed |= configSet(debugTelePeriod, settings[FPSTR(F_DEBUG_TELEPERIOD)], F("debugTelePeriod")); /* Syslog Settings*/ -#if HASP_USE_SYSLOG > 0 + #if HASP_USE_SYSLOG > 0 if(!settings[FPSTR(F_CONFIG_HOST)].isNull()) { changed |= strcmp(debugSyslogHost, settings[FPSTR(F_CONFIG_HOST)]) != 0; strncpy(debugSyslogHost, settings[FPSTR(F_CONFIG_HOST)], sizeof(debugSyslogHost)); @@ -249,10 +250,11 @@ bool debugSetConfig(const JsonObject & settings) changed |= configSet(debugSyslogPort, settings[FPSTR(F_CONFIG_PORT)], F("debugSyslogPort")); changed |= configSet(debugSyslogProtocol, settings[FPSTR(F_CONFIG_PROTOCOL)], F("debugSyslogProtocol")); changed |= configSet(debugSyslogFacility, settings[FPSTR(F_CONFIG_LOG)], F("debugSyslogFacility")); -#endif + #endif return changed; } +#endif // HASP_USE_CONFIG inline void debugSendAnsiCode(const __FlashStringHelper * code, Print * _logOutput) { @@ -546,9 +548,9 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput) } debugPrintHaspMemory(level, _logOutput); -#if LV_MEM_CUSTOM == 0 + #if LV_MEM_CUSTOM == 0 debugPrintLvglMemory(level, _logOutput); -#endif + #endif } return; } @@ -605,15 +607,19 @@ void debugPreSetup(JsonObject settings) Log.setPrefix(debugPrintPrefix); // Uncomment to get timestamps as prefix Log.setSuffix(debugPrintSuffix); // Uncomment to get newline as suffix - uint32_t baudrate = settings[FPSTR(F_CONFIG_BAUD)].as() * 10; + uint32_t baudrate = 0; +#if HASP_USE_CONFIG > 0 + baudrate = settings[FPSTR(F_CONFIG_BAUD)].as() * 10; +#endif + if(baudrate == 0) baudrate = SERIAL_SPEED; if(baudrate >= 9600u) { /* the baudrates are stored divided by 10 */ #if defined(STM32F4xx) -#ifndef STM32_SERIAL1 // Define what Serial port to use for log output + #ifndef STM32_SERIAL1 // Define what Serial port to use for log output Serial.setRx(PA3); // User Serial2 Serial.setTx(PA2); -#endif + #endif #endif Serial.begin(baudrate); /* prepare for possible serial debug */ delay(10); diff --git a/src/hasp_debug.h b/src/hasp_debug.h index 2f4b28bd..06fd4851 100644 --- a/src/hasp_debug.h +++ b/src/hasp_debug.h @@ -24,8 +24,10 @@ void debugStopSyslog(void); // void syslogSend(uint8_t log, const char * debugText); /* ===== Read/Write Configuration ===== */ +#if HASP_USE_CONFIG > 0 bool debugGetConfig(const JsonObject & settings); bool debugSetConfig(const JsonObject & settings); +#endif // void debugPrintPrefix(int level, Print * _logOutput); // void debugPrintSuffix(int level, Print * _logOutput); diff --git a/src/hasp_dispatch.cpp b/src/hasp_dispatch.cpp index b25101f7..312245f6 100644 --- a/src/hasp_dispatch.cpp +++ b/src/hasp_dispatch.cpp @@ -8,7 +8,6 @@ #include "hasp_dispatch.h" #include "hasp_network.h" // for network_get_status() -#include "hasp_config.h" #include "hasp_debug.h" #include "hasp_object.h" #include "hasp_gui.h" @@ -18,6 +17,10 @@ #include "hasp_hal.h" #include "hasp.h" +#if HASP_USE_CONFIG > 0 + #include "hasp_config.h" +#endif + extern unsigned long debugLastMillis; // UpdateStatus timer uint8_t nCommands = 0; @@ -123,13 +126,16 @@ void dispatch_command(const char * topic, const char * payload) } else if(topic == strstr_P(topic, PSTR("p["))) { dispatch_process_button_attribute(topic, payload); -#if HASP_USE_WIFI > 0 +#if HASP_USE_CONFIG > 0 + + #if HASP_USE_WIFI > 0 } else if(!strcmp_P(topic, F_CONFIG_SSID) || !strcmp_P(topic, F_CONFIG_PASS)) { DynamicJsonDocument settings(45); settings[topic] = payload; wifiSetConfig(settings.as()); -#endif + #endif // HASP_USE_WIFI + #if HASP_USE_MQTT > 0 } else if(!strcmp_P(topic, PSTR("mqtthost")) || !strcmp_P(topic, PSTR("mqttport")) || !strcmp_P(topic, PSTR("mqttport")) || !strcmp_P(topic, PSTR("mqttuser")) || !strcmp_P(topic, PSTR("hostname"))) { @@ -137,11 +143,12 @@ void dispatch_command(const char * topic, const char * payload) // memset(item, 0, sizeof(item)); // strncpy(item, topic + 4, 4); -#if HASP_USE_MQTT > 0 DynamicJsonDocument settings(45); settings[topic + 4] = payload; mqttSetConfig(settings.as()); -#endif + #endif // HASP_USE_MQTT + +#endif // HASP_USE_CONFIG } else { if(strlen(payload) == 0) { @@ -167,11 +174,13 @@ void dispatch_topic_payload(const char * topic, const char * payload) return; } +#if HASP_USE_CONFIG > 0 if(topic == strstr_P(topic, PSTR("config/"))) { // startsWith command/ topic += 7u; dispatch_config(topic, (char *)payload); return; } +#endif dispatch_command(topic, (char *)payload); // dispatch as is } @@ -237,6 +246,7 @@ void IRAM_ATTR dispatch_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, co dispatch_state_msg(F("json"), payload); } +#if HASP_USE_CONFIG > 0 // Get or Set a part of the config.json file static void dispatch_config(const char * topic, const char * payload) { @@ -281,42 +291,42 @@ static void dispatch_config(const char * topic, const char * payload) haspGetConfig(settings); } -#if HASP_USE_WIFI > 0 + #if HASP_USE_WIFI > 0 else if(strcasecmp_P(topic, PSTR("wifi")) == 0) { if(update) wifiSetConfig(settings); else wifiGetConfig(settings); } -#if HASP_USE_MQTT > 0 + #if HASP_USE_MQTT > 0 else if(strcasecmp_P(topic, PSTR("mqtt")) == 0) { if(update) mqttSetConfig(settings); else mqttGetConfig(settings); } -#endif -#if HASP_USE_TELNET > 0 - // else if(strcasecmp_P(topic, PSTR("telnet")) == 0) - // telnetGetConfig(settings[F("telnet")]); -#endif -#if HASP_USE_MDNS > 0 + #endif + #if HASP_USE_TELNET > 0 + // else if(strcasecmp_P(topic, PSTR("telnet")) == 0) + // telnetGetConfig(settings[F("telnet")]); + #endif + #if HASP_USE_MDNS > 0 else if(strcasecmp_P(topic, PSTR("mdns")) == 0) { if(update) mdnsSetConfig(settings); else mdnsGetConfig(settings); } -#endif -#if HASP_USE_HTTP > 0 + #endif + #if HASP_USE_HTTP > 0 else if(strcasecmp_P(topic, PSTR("http")) == 0) { if(update) httpSetConfig(settings); else httpGetConfig(settings); } -#endif -#endif + #endif + #endif // Send output if(!update) { @@ -325,6 +335,7 @@ static void dispatch_config(const char * topic, const char * payload) dispatch_state_msg(F("config"), buffer); } } +#endif // HASP_USE_CONFIG /********************************************** Input Events *******************************************/ // Map events to either ON or OFF (UP or DOWN) @@ -415,12 +426,12 @@ static inline void dispatch_state_msg(const __FlashStringHelper * subtopic, cons #if !defined(HASP_USE_MQTT) && !defined(HASP_USE_TASMOTA_SLAVE) Log.notice(TAG_MSGR, F("%s => %s"), String(subtopic).c_str(), payload); #else -#if HASP_USE_MQTT > 0 + #if HASP_USE_MQTT > 0 mqtt_send_state(subtopic, payload); -#endif -#if HASP_USE_TASMOTA_SLAVE > 0 + #endif + #if HASP_USE_TASMOTA_SLAVE > 0 slave_send_state(subtopic, payload); -#endif + #endif #endif } @@ -603,7 +614,9 @@ void dispatch_web_update(const char *, const char * espOtaUrl) // restart the device void dispatch_reboot(bool saveConfig) { +#if HASP_USE_CONFIG > 0 if(saveConfig) configWriteConfig(); +#endif #if HASP_USE_MQTT > 0 mqttStop(); // Stop the MQTT Client first #endif @@ -632,10 +645,10 @@ void dispatch_output_statusupdate(const char *, const char *) snprintf_P(data, sizeof(data), PSTR("{\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"), buffer, long(millis() / 1000)); -#if HASP_USE_WIFI > 0 + #if HASP_USE_WIFI > 0 network_get_statusupdate(buffer, sizeof(buffer)); strcat(data, buffer); -#endif + #endif snprintf_P(buffer, sizeof(buffer), PSTR("\"heapFree\":%u,\"heapFrag\":%u,\"espCore\":\"%s\","), halGetFreeHeap(), halGetHeapFragmentation(), halGetCoreVersion().c_str()); strcat(data, buffer); @@ -643,10 +656,10 @@ void dispatch_output_statusupdate(const char *, const char *) haspGetPage(), (HASP_NUM_PAGES)); strcat(data, buffer); -#if defined(ARDUINO_ARCH_ESP8266) + #if defined(ARDUINO_ARCH_ESP8266) snprintf_P(buffer, sizeof(buffer), PSTR("\"espVcc\":%.2f,"), (float)ESP.getVcc() / 1000); strcat(data, buffer); -#endif + #endif snprintf_P(buffer, sizeof(buffer), PSTR("\"tftDriver\":\"%s\",\"tftWidth\":%u,\"tftHeight\":%u}"), halDisplayDriverName().c_str(), (TFT_WIDTH), (TFT_HEIGHT)); @@ -723,7 +736,9 @@ void dispatchSetup() dispatch_add_command(PSTR("restart"), dispatch_reboot); dispatch_add_command(PSTR("screenshot"), dispatch_screenshot); dispatch_add_command(PSTR("factoryreset"), dispatch_factory_reset); +#if HASP_USE_CONFIG > 0 dispatch_add_command(PSTR("setupap"), oobeFakeSetup); +#endif /* WARNING: remember to expand the commands array when adding new commands */ } diff --git a/src/hasp_gpio.cpp b/src/hasp_gpio.cpp index 08c34fdb..583ba853 100644 --- a/src/hasp_gpio.cpp +++ b/src/hasp_gpio.cpp @@ -437,6 +437,7 @@ hasp_gpio_config_t gpioGetPinConfig(uint8_t num) } //////////////////////////////////////////////////////////////////////////////////////////////////// +#if HASP_USE_CONFIG > 0 bool gpioGetConfig(const JsonObject & settings) { bool changed = false; @@ -511,3 +512,4 @@ bool gpioSetConfig(const JsonObject & settings) return changed; } +#endif // HASP_USE_CONFIG \ No newline at end of file diff --git a/src/hasp_gpio.h b/src/hasp_gpio.h index a8b37c08..8de46c2a 100644 --- a/src/hasp_gpio.h +++ b/src/hasp_gpio.h @@ -30,8 +30,11 @@ bool gpioInUse(uint8_t gpio); bool gpioConfigInUse(uint8_t num); int8_t gpioGetFreeConfigId(); hasp_gpio_config_t gpioGetPinConfig(uint8_t num); + +#if HASP_USE_CONFIG > 0 bool gpioGetConfig(const JsonObject & settings); bool gpioSetConfig(const JsonObject & settings); +#endif #define HASP_GPIO_FREE 0x00 #define HASP_GPIO_USED 0x01 diff --git a/src/hasp_gui.cpp b/src/hasp_gui.cpp index 980883b1..4166ff8a 100644 --- a/src/hasp_gui.cpp +++ b/src/hasp_gui.cpp @@ -730,6 +730,7 @@ int8_t guiGetDim() } //////////////////////////////////////////////////////////////////////////////////////////////////// +#if HASP_USE_CONFIG > 0 bool guiGetConfig(const JsonObject & settings) { bool changed = false; @@ -845,6 +846,7 @@ bool guiSetConfig(const JsonObject & settings) return changed; } +#endif // HASP_USE_CONFIG /* **************************** SCREENSHOTS ************************************** */ #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 || HASP_USE_HTTP > 0 diff --git a/src/hasp_gui.h b/src/hasp_gui.h index 35c4abec..3659beaf 100644 --- a/src/hasp_gui.h +++ b/src/hasp_gui.h @@ -32,9 +32,9 @@ bool guiGetBacklight(); bool IRAM_ATTR guiCheckSleep(); /* ===== Read/Write Configuration ===== */ +#if HASP_USE_CONFIG > 0 bool guiGetConfig(const JsonObject & settings); bool guiSetConfig(const JsonObject & settings); - -// lv_res_t guiChangeTheme(uint8_t themeid, uint16_t hue, String font, uint8_t fontsize); +#endif #endif \ No newline at end of file diff --git a/src/hasp_http.cpp b/src/hasp_http.cpp index 6ef24a5f..637f1308 100644 --- a/src/hasp_http.cpp +++ b/src/hasp_http.cpp @@ -899,7 +899,8 @@ void handleFileList() } #endif -//////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////// + #if HASP_USE_CONFIG > 0 void webHandleConfig() { // http://plate01/config if(!httpIsAuthenticated(F("config"))) return; @@ -914,10 +915,10 @@ void webHandleConfig() if(save == String(PSTR("hasp"))) { haspSetConfig(settings.as()); - #if HASP_USE_MQTT > 0 + #if HASP_USE_MQTT > 0 } else if(save == String(PSTR("mqtt"))) { mqttSetConfig(settings.as()); - #endif + #endif } else if(save == String(PSTR("gui"))) { settings[FPSTR(F_GUI_POINTER)] = webServer.hasArg(PSTR("pointer")); @@ -932,20 +933,20 @@ void webHandleConfig() // Password might have changed if(!httpIsAuthenticated(F("config"))) return; - #if HASP_USE_WIFI > 0 + #if HASP_USE_WIFI > 0 } else if(save == String(PSTR("wifi"))) { wifiSetConfig(settings.as()); - #endif + #endif } } } - // Reboot after saving wifi config in AP mode - #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) + // Reboot after saving wifi config in AP mode + #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.getMode() != WIFI_STA) { httpHandleReboot(); } - #endif + #endif { String httpMessage((char *)0); @@ -954,15 +955,15 @@ void webHandleConfig() httpMessage += httpGetNodename(); httpMessage += F("
"); - #if HASP_USE_WIFI > 0 + #if HASP_USE_WIFI > 0 httpMessage += F("

"); - #endif + #endif - #if HASP_USE_MQTT > 0 + #if HASP_USE_MQTT > 0 httpMessage += F("

"); - #endif + #endif httpMessage += F("

"); @@ -973,10 +974,10 @@ void webHandleConfig() httpMessage += F("

"); - #if HASP_USE_GPIO > 0 + #if HASP_USE_GPIO > 0 httpMessage += F("

"); - #endif + #endif httpMessage += F("

"); @@ -994,8 +995,8 @@ void webHandleConfig() webSendFooter(); } - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_MQTT > 0 + //////////////////////////////////////////////////////////////////////////////////////////////////// + #if HASP_USE_MQTT > 0 void webHandleMqttConfig() { // http://plate01/config/mqtt if(!httpIsAuthenticated(F("config/mqtt"))) return; @@ -1042,7 +1043,7 @@ void webHandleMqttConfig() // httpMessage.clear(); webSendFooter(); } - #endif + #endif //////////////////////////////////////////////////////////////////////////////////////////////////// void webHandleGuiConfig() @@ -1090,7 +1091,7 @@ void webHandleGuiConfig() int8_t bcklpin = settings[FPSTR(F_GUI_BACKLIGHTPIN)].as(); httpMessage += F("

Backlight Control

"); httpMessage += F("

"); @@ -1124,8 +1125,8 @@ void webHandleGuiConfig() if(webServer.hasArg(F("action"))) dispatch_text_line(webServer.arg(F("action")).c_str()); } - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_WIFI > 0 + //////////////////////////////////////////////////////////////////////////////////////////////////// + #if HASP_USE_WIFI > 0 void webHandleWifiConfig() { // http://plate01/config/wifi if(!httpIsAuthenticated(F("config/wifi"))) return; @@ -1150,26 +1151,26 @@ void webHandleWifiConfig() } httpMessage += F("'>

"); - #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) + #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.getMode() == WIFI_STA) { httpMessage += PSTR("

"); } - #endif + #endif webSendPage(httpGetNodename(), httpMessage.length(), false); webServer.sendContent(httpMessage); - #if defined(STM32F4xx) + #if defined(STM32F4xx) httpMessage = ""; - #else + #else httpMessage.clear(); - #endif + #endif webSendFooter(); } - #endif + #endif - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_HTTP > 0 + //////////////////////////////////////////////////////////////////////////////////////////////////// + #if HASP_USE_HTTP > 0 void webHandleHttpConfig() { // http://plate01/config/http if(!httpIsAuthenticated(F("config/http"))) return; @@ -1204,10 +1205,10 @@ void webHandleHttpConfig() // httpMessage.clear(); webSendFooter(); } - #endif + #endif - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if defined(HASP_USE_GPIO) && (HASP_USE_GPIO > 0) + //////////////////////////////////////////////////////////////////////////////////////////////////// + #if defined(HASP_USE_GPIO) && (HASP_USE_GPIO > 0) void webHandleGpioConfig() { // http://plate01/config/gpio if(!httpIsAuthenticated(F("config/gpio"))) return; @@ -1407,7 +1408,7 @@ void webHandleGpioOptions() if(webServer.hasArg(F("action"))) dispatch_text_line(webServer.arg(F("action")).c_str()); // Security check } - #endif // HASP_USE_GPIO + #endif // HASP_USE_GPIO //////////////////////////////////////////////////////////////////////////////////////////////////// void webHandleDebugConfig() @@ -1440,7 +1441,7 @@ void webHandleDebugConfig() httpMessage += settings[FPSTR(F_DEBUG_TELEPERIOD)].as(); httpMessage += F("'>

"); - #if HASP_USE_SYSLOG > 0 + #if HASP_USE_SYSLOG > 0 httpMessage += F("Syslog Hostame (optional)() == 1) httpMessage += F(" checked"); httpMessage += F(">BSD (RFC 3164)"); - #endif + #endif httpMessage += F("

"); @@ -1496,24 +1497,24 @@ void webHandleHaspConfig() httpMessage += F("

UI Theme (required)
"); httpMessage += F("Hue

"); httpMessage += F("

Default Font

"); httpMessage += F("

Startup Layout (optional) 0 +void webHandleSaveConfig() +{ + if(!httpIsAuthenticated(F("saveConfig"))) return; + + configWriteConfig(); +} + //////////////////////////////////////////////////////////////////////////////////////////////////// void httpHandleResetConfig() { // http://plate01/resetConfig @@ -1713,6 +1716,7 @@ void httpHandleResetConfig() dispatch_reboot(false); // Do not save the current config } } + #endif // HASP_USE_CONFIG void httpStart() { @@ -1750,109 +1754,96 @@ void httpSetup() { // httpSetConfig(settings); - #if HASP_USE_WIFI > 0 - #if !defined(STM32F4xx) - if(WiFi.getMode() != WIFI_STA) { - Log.notice(TAG_HTTP, F("Wifi access point")); - webServer.on(F("/"), webHandleWifiConfig); - } else { - #endif - #endif - - webServer.on(F("/page/"), []() { - String pageid = webServer.arg(F("page")); - webServer.send(200, PSTR("text/plain"), "Page: '" + pageid + "'"); - haspSetPage(pageid.toInt()); - }); - - #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 - webServer.on(F("/list"), HTTP_GET, handleFileList); - // load editor - webServer.on(F("/edit"), HTTP_GET, []() { - if(!handleFileRead("/edit.htm")) { - char mimetype[16]; - snprintf(mimetype, sizeof(mimetype), PSTR("text/plain")); - webServer.send_P(404, mimetype, PSTR("FileNotFound")); - } - }); - webServer.on(F("/edit"), HTTP_PUT, handleFileCreate); - webServer.on(F("/edit"), HTTP_DELETE, handleFileDelete); - // first callback is called after the request has ended with all parsed arguments - // second callback handles file uploads at that location - webServer.on( - F("/edit"), HTTP_POST, - []() { - webServer.send(200, "text/plain", ""); - Log.verbose(TAG_HTTP, F("Headers: %d"), webServer.headers()); - }, - handleFileUpload); - #endif - - // get heap status, analog input value and all GPIO statuses in one json call - /*webServer.on(F("/all"), HTTP_GET, []() { - String json; - json.reserve(128); - json += F("{\"heap\":"); - json += String(ESP.getFreeHeap()); - json += F(", \"analog\":"); - json += String(analogRead(A0)); - json += F("}"); - - char mimetype[128]; - sprintf(mimetype, PSTR("text/json")); - webServer.send(200, mimetype, json); - json.clear(); - });*/ - - webServer.on(F("/"), webHandleRoot); - webServer.on(F("/info"), webHandleInfo); - - webServer.on(F("/config/hasp"), webHandleHaspConfig); - webServer.on(F("/config/http"), webHandleHttpConfig); - webServer.on(F("/config/gui"), webHandleGuiConfig); - webServer.on(F("/config/debug"), webHandleDebugConfig); - #if HASP_USE_MQTT > 0 - webServer.on(F("/config/mqtt"), webHandleMqttConfig); - #endif - #if HASP_USE_WIFI > 0 - webServer.on(F("/config/wifi"), webHandleWifiConfig); - #endif - #if HASP_USE_GPIO > 0 - webServer.on(F("/config/gpio"), webHandleGpioConfig); - webServer.on(F("/config/gpio/options"), webHandleGpioOptions); - #endif - webServer.on(F("/screenshot"), webHandleScreenshot); - webServer.on(F("/saveConfig"), webHandleSaveConfig); - webServer.on(F("/resetConfig"), httpHandleResetConfig); - webServer.on(F("/firmware"), webHandleFirmware); - #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) - webServer.on( - F("/update"), HTTP_POST, - []() { - webServer.send(200, "text/plain", ""); - Log.verbose(TAG_HTTP, F("Total size: %s"), webServer.hostHeader().c_str()); - }, - webHandleFirmwareUpdate); - webServer.on(F("/espfirmware"), httpHandleEspFirmware); - #endif - webServer.on(F("/reboot"), httpHandleReboot); - webServer.onNotFound(httpHandleNotFound); - #if HASP_USE_WIFI > 0 - #if !defined(STM32F4xx) - } - #endif - #endif - - // Shared pages - webServer.on(F("/about"), webHandleAbout); - webServer.on(F("/config"), webHandleConfig); - webServer.onNotFound(httpHandleNotFound); - // ask server to track these headers const char * headerkeys[] = {"Content-Length"}; // "Authentication" size_t headerkeyssize = sizeof(headerkeys) / sizeof(char *); webServer.collectHeaders(headerkeys, headerkeyssize); + // Shared pages + webServer.on(F("/about"), webHandleAbout); + webServer.onNotFound(httpHandleNotFound); + + #if HASP_USE_WIFI > 0 + #if !defined(STM32F4xx) + + #if HASP_USE_CONFIG > 0 + if(WiFi.getMode() != WIFI_STA) { + Log.notice(TAG_HTTP, F("Wifi access point")); + webServer.on(F("/"), webHandleWifiConfig); + return; + } + + #endif + #endif + #endif + + webServer.on(F("/page/"), []() { + String pageid = webServer.arg(F("page")); + webServer.send(200, PSTR("text/plain"), "Page: '" + pageid + "'"); + haspSetPage(pageid.toInt()); + }); + + #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 + webServer.on(F("/list"), HTTP_GET, handleFileList); + // load editor + webServer.on(F("/edit"), HTTP_GET, []() { + if(!handleFileRead("/edit.htm")) { + char mimetype[16]; + snprintf(mimetype, sizeof(mimetype), PSTR("text/plain")); + webServer.send_P(404, mimetype, PSTR("FileNotFound")); + } + }); + webServer.on(F("/edit"), HTTP_PUT, handleFileCreate); + webServer.on(F("/edit"), HTTP_DELETE, handleFileDelete); + // first callback is called after the request has ended with all parsed arguments + // second callback handles file uploads at that location + webServer.on( + F("/edit"), HTTP_POST, + []() { + webServer.send(200, "text/plain", ""); + Log.verbose(TAG_HTTP, F("Headers: %d"), webServer.headers()); + }, + handleFileUpload); + #endif + + webServer.on(F("/"), webHandleRoot); + webServer.on(F("/info"), webHandleInfo); + webServer.on(F("/screenshot"), webHandleScreenshot); + webServer.on(F("/firmware"), webHandleFirmware); + webServer.on(F("/reboot"), httpHandleReboot); + webServer.onNotFound(httpHandleNotFound); + + #if HASP_USE_CONFIG > 0 + webServer.on(F("/config/hasp"), webHandleHaspConfig); + webServer.on(F("/config/http"), webHandleHttpConfig); + webServer.on(F("/config/gui"), webHandleGuiConfig); + webServer.on(F("/config/debug"), webHandleDebugConfig); + #if HASP_USE_MQTT > 0 + webServer.on(F("/config/mqtt"), webHandleMqttConfig); + #endif + #if HASP_USE_WIFI > 0 + webServer.on(F("/config/wifi"), webHandleWifiConfig); + #endif + #if HASP_USE_GPIO > 0 + webServer.on(F("/config/gpio"), webHandleGpioConfig); + webServer.on(F("/config/gpio/options"), webHandleGpioOptions); + #endif + webServer.on(F("/saveConfig"), webHandleSaveConfig); + webServer.on(F("/resetConfig"), httpHandleResetConfig); + webServer.on(F("/config"), webHandleConfig); + #endif // HASP_USE_CONFIG + + #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) + webServer.on( + F("/update"), HTTP_POST, + []() { + webServer.send(200, "text/plain", ""); + Log.verbose(TAG_HTTP, F("Total size: %s"), webServer.hostHeader().c_str()); + }, + webHandleFirmwareUpdate); + webServer.on(F("/espfirmware"), httpHandleEspFirmware); + #endif + Log.trace(TAG_HTTP, F("Setup Complete")); // webStart(); Wait for network connection } @@ -1886,6 +1877,7 @@ void httpEvery5Seconds() } //////////////////////////////////////////////////////////////////////////////////////////////////// + #if HASP_USE_CONFIG > 0 bool httpGetConfig(const JsonObject & settings) { bool changed = false; @@ -1932,6 +1924,7 @@ bool httpSetConfig(const JsonObject & settings) return changed; } + #endif // HASP_USE_CONFIG size_t httpClientWrite(const uint8_t * buf, size_t size) { diff --git a/src/hasp_http.h b/src/hasp_http.h index 1bf1dd66..8ef7213a 100644 --- a/src/hasp_http.h +++ b/src/hasp_http.h @@ -15,7 +15,9 @@ void httpStop(void); size_t httpClientWrite(const uint8_t * buf, size_t size); // Screenshot Write Data +#if HASP_USE_CONFIG > 0 bool httpGetConfig(const JsonObject & settings); bool httpSetConfig(const JsonObject & settings); +#endif // HASP_USE_CONFIG #endif \ No newline at end of file diff --git a/src/hasp_mdns.cpp b/src/hasp_mdns.cpp index 7a427f81..ebced002 100644 --- a/src/hasp_mdns.cpp +++ b/src/hasp_mdns.cpp @@ -83,6 +83,7 @@ void mdnsStop() #endif } + #if HASP_USE_CONFIG > 0 bool mdnsGetConfig(const JsonObject & settings) { bool changed = false; @@ -108,5 +109,6 @@ bool mdnsSetConfig(const JsonObject & settings) return changed; } + #endif // HASP_USE_CONFIG #endif // HASP_USE_MDNS diff --git a/src/hasp_mdns.h b/src/hasp_mdns.h index e8bde2f3..296d8cd6 100644 --- a/src/hasp_mdns.h +++ b/src/hasp_mdns.h @@ -13,7 +13,9 @@ void mdnsStart(void); void mdnsStop(void); /* ===== Read/Write Configuration ===== */ +#if HASP_USE_CONFIG > 0 bool mdnsGetConfig(const JsonObject & settings); bool mdnsSetConfig(const JsonObject & settings); +#endif #endif \ No newline at end of file diff --git a/src/hasp_mqtt.cpp b/src/hasp_mqtt.cpp index 3f047a2e..e6c93ed4 100644 --- a/src/hasp_mqtt.cpp +++ b/src/hasp_mqtt.cpp @@ -409,6 +409,7 @@ void mqttStop() } } +#if HASP_USE_CONFIG > 0 bool mqttGetConfig(const JsonObject & settings) { bool changed = false; @@ -493,5 +494,6 @@ bool mqttSetConfig(const JsonObject & settings) return changed; } +#endif // HASP_USE_CONFIG #endif // HASP_USE_MQTT diff --git a/src/hasp_mqtt.h b/src/hasp_mqtt.h index 9b8abb14..e63f0bba 100644 --- a/src/hasp_mqtt.h +++ b/src/hasp_mqtt.h @@ -17,8 +17,10 @@ void IRAM_ATTR mqtt_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const bool IRAM_ATTR mqttIsConnected(); +#if HASP_USE_CONFIG > 0 bool mqttGetConfig(const JsonObject & settings); bool mqttSetConfig(const JsonObject & settings); +#endif String mqttGetNodename(void); diff --git a/src/hasp_oobe.cpp b/src/hasp_oobe.cpp index a95f229b..6c70e349 100644 --- a/src/hasp_oobe.cpp +++ b/src/hasp_oobe.cpp @@ -1,6 +1,8 @@ /* MIT License - Copyright (c) 2020 Francis Van Roie For full license information read the LICENSE file in the project folder */ +#if HASP_USE_CONFIG > 0 + #include "hasp_conf.h" #include "lvgl.h" @@ -362,4 +364,5 @@ void oobeFakeSetup(const char *, const char *) Log.trace(TAG_OOBE, F("Already calibrated")); } #endif -} \ No newline at end of file +} +#endif // HASP_USE_CONFIG \ No newline at end of file diff --git a/src/hasp_oobe.h b/src/hasp_oobe.h index f4b0630e..8e3e7f4b 100644 --- a/src/hasp_oobe.h +++ b/src/hasp_oobe.h @@ -1,6 +1,10 @@ /* MIT License - Copyright (c) 2020 Francis Van Roie For full license information read the LICENSE file in the project folder */ +#if HASP_USE_CONFIG > 0 + void oobeSetAutoCalibrate(bool cal); bool oobeSetup(); -void oobeFakeSetup(const char *, const char *); // for testing purposes only \ No newline at end of file +void oobeFakeSetup(const char *, const char *); // for testing purposes only + +#endif // HASP_USE_CONFIG diff --git a/src/hasp_telnet.cpp b/src/hasp_telnet.cpp index 564a51ca..462a78a0 100644 --- a/src/hasp_telnet.cpp +++ b/src/hasp_telnet.cpp @@ -313,6 +313,7 @@ void IRAM_ATTR telnetLoop() #endif } +#if HASP_USE_CONFIG > 0 bool telnetGetConfig(const JsonObject & settings) { bool changed = false; @@ -345,5 +346,6 @@ bool telnetSetConfig(const JsonObject & settings) return changed; } +#endif // HASP_USE_CONFIG #endif \ No newline at end of file diff --git a/src/hasp_telnet.h b/src/hasp_telnet.h index a06b5f13..143e5059 100644 --- a/src/hasp_telnet.h +++ b/src/hasp_telnet.h @@ -22,9 +22,10 @@ void telnetStop(void); /* ===== Getter and Setter Functions ===== */ /* ===== Read/Write Configuration ===== */ - +#if HASP_USE_CONFIG > 0 bool telnetSetConfig(const JsonObject & settings); bool telnetGetConfig(const JsonObject & settings); +#endif #define TELNET_UNAUTHENTICATED 0 #define TELNET_USERNAME_OK 10 diff --git a/src/hasp_wifi.cpp b/src/hasp_wifi.cpp index d6f5b1aa..e0f2d2d7 100644 --- a/src/hasp_wifi.cpp +++ b/src/hasp_wifi.cpp @@ -508,7 +508,7 @@ void wifi_get_statusupdate(char * buffer, size_t len) } /* ============ Confiuration =============================================================== */ - +#if HASP_USE_CONFIG > 0 bool wifiGetConfig(const JsonObject & settings) { bool changed = false; @@ -549,5 +549,6 @@ bool wifiSetConfig(const JsonObject & settings) return changed; } +#endif // HASP_USE_CONFIG #endif \ No newline at end of file diff --git a/src/hasp_wifi.h b/src/hasp_wifi.h index 91c8e70a..5364150f 100644 --- a/src/hasp_wifi.h +++ b/src/hasp_wifi.h @@ -15,7 +15,9 @@ void wifiStop(void); bool wifiValidateSsid(const char * ssid, const char * pass); void wifi_get_statusupdate(char * buffer, size_t len); +#if HASP_USE_CONFIG > 0 bool wifiGetConfig(const JsonObject & settings); bool wifiSetConfig(const JsonObject & settings); +#endif #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bf451f99..6cf9ff5d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,7 +36,9 @@ void setup() /**************************** * Read & Apply User Configuration ***************************/ +#if HASP_USE_CONFIG > 0 configSetup(); // also runs debugPreSetup(), debugSetup() and debugStart() +#endif dispatchSetup(); guiSetup(); @@ -57,7 +59,10 @@ void setup() networkSetup(); #endif - if(!oobeSetup()) { +#if HASP_USE_CONFIG > 0 + if(!oobeSetup()) +#endif + { haspSetup(); }