Add HASP_USE_CONFIG option

This commit is contained in:
fvanroie 2020-12-23 04:17:41 +01:00
parent d8481606ff
commit 1f40da076a
24 changed files with 280 additions and 219 deletions

View File

@ -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

View File

@ -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

View File

@ -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
@ -479,3 +481,5 @@ bool configClearEeprom()
return false;
#endif
}
#endif // HAS_USE_CONFIG

View File

@ -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
@ -60,3 +62,5 @@ const char F_GPIO_CONFIG[] PROGMEM = "config";
const char HASP_CONFIG_FILE[] PROGMEM = "/config.json";
#endif
#endif // HASP_USE_CONFIG

View File

@ -193,6 +193,7 @@ void debugStop()
if(debugSerialStarted) Serial.flush();
}
#if HASP_USE_CONFIG > 0
bool debugGetConfig(const JsonObject & settings)
{
bool changed = false;
@ -253,6 +254,7 @@ bool debugSetConfig(const JsonObject & settings)
return changed;
}
#endif // HASP_USE_CONFIG
inline void debugSendAnsiCode(const __FlashStringHelper * code, Print * _logOutput)
{
@ -605,7 +607,11 @@ 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<uint32_t>() * 10;
uint32_t baudrate = 0;
#if HASP_USE_CONFIG > 0
baudrate = settings[FPSTR(F_CONFIG_BAUD)].as<uint32_t>() * 10;
#endif
if(baudrate == 0) baudrate = SERIAL_SPEED;
if(baudrate >= 9600u) { /* the baudrates are stored divided by 10 */

View File

@ -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);

View File

@ -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_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<JsonObject>());
#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<JsonObject>());
#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)
{
@ -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)
@ -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
@ -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 */
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -900,6 +900,7 @@ void handleFileList()
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_CONFIG > 0
void webHandleConfig()
{ // http://plate01/config
if(!httpIsAuthenticated(F("config"))) return;
@ -1571,6 +1572,7 @@ void webHandleHaspConfig()
// httpMessage.clear();
webSendFooter();
}
#endif // HASP_USE_CONFIG
////////////////////////////////////////////////////////////////////////////////////////////////////
void httpHandleNotFound()
@ -1602,14 +1604,6 @@ void httpHandleNotFound()
webServer.send(404, PSTR("text/plain"), httpMessage.c_str());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void webHandleSaveConfig()
{
if(!httpIsAuthenticated(F("saveConfig"))) return;
configWriteConfig();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void webHandleFirmware()
{
@ -1664,6 +1658,15 @@ void httpHandleEspFirmware()
// espStartOta(webServer.arg("espFirmware"));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_CONFIG > 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,12 +1754,26 @@ void httpSetup()
{
// httpSetConfig(settings);
// 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);
} else {
return;
}
#endif
#endif
#endif
@ -1788,25 +1806,14 @@ void httpSetup()
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("/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);
@ -1821,10 +1828,11 @@ void httpSetup()
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);
webServer.on(F("/config"), webHandleConfig);
#endif // HASP_USE_CONFIG
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
webServer.on(
F("/update"), HTTP_POST,
@ -1835,23 +1843,6 @@ void httpSetup()
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);
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)
{

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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"
@ -363,3 +365,4 @@ void oobeFakeSetup(const char *, const char *)
}
#endif
}
#endif // HASP_USE_CONFIG

View File

@ -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
#endif // HASP_USE_CONFIG

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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();
}