From 9d9df478bd288bca508be3816b547f6f832b04e9 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Thu, 15 Aug 2019 18:05:26 +0300 Subject: [PATCH] Fix WEB_SUPPORT=0 and missing definitions (#1853) * clean-up ws declarations, remove old stubs * always fw declare callback types * wsPost is only possible with WEB_SUPPORT * some more ws methods --- code/espurna/config/prototypes.h | 116 +++++++++++++++---------------- code/espurna/ntp.ino | 4 +- code/espurna/ws.ino | 25 +++++++ 3 files changed, 83 insertions(+), 62 deletions(-) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 490ca309..4f1ea570 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -57,13 +58,12 @@ void systemStabilityCounter(uint8_t); // ----------------------------------------------------------------------------- // API // ----------------------------------------------------------------------------- + +using api_get_callback_f = std::function; +using api_put_callback_f = std::function ; + #if WEB_SUPPORT - typedef std::function api_get_callback_f; - typedef std::function api_put_callback_f; void apiRegister(const char * key, api_get_callback_f getFn, api_put_callback_f putFn = NULL); -#else - #define api_get_callback_f void * - #define api_put_callback_f void * #endif // ----------------------------------------------------------------------------- @@ -134,6 +134,7 @@ bool gpioReleaseLock(unsigned char gpio); // Homeassistant // ----------------------------------------------------------------------------- struct ha_config_t; +void haSetup(); // ----------------------------------------------------------------------------- // I2C @@ -163,12 +164,12 @@ void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len); // ----------------------------------------------------------------------------- // MQTT // ----------------------------------------------------------------------------- + +using mqtt_callback_f = std::function; + #if MQTT_SUPPORT - typedef std::function mqtt_callback_f; void mqttRegister(mqtt_callback_f callback); String mqttMagnitude(char * topic); -#else - #define mqtt_callback_f void * #endif // ----------------------------------------------------------------------------- @@ -252,33 +253,45 @@ char* strnstr(const char*, const char*, size_t); // ----------------------------------------------------------------------------- // WebServer // ----------------------------------------------------------------------------- + +class AsyncClient; +class AsyncWebServer; + #if WEB_SUPPORT #include AsyncWebServer * webServer(); #else - #define AsyncWebServerRequest void - #define ArRequestHandlerFunction void - #define AsyncWebSocketClient void - #define AsyncWebSocket void - #define AwsEventType void * + class AsyncWebServerRequest; + class ArRequestHandlerFunction; + class AsyncWebSocketClient; + class AsyncWebSocket; + class AwsEventType; #endif -typedef std::function web_body_callback_f; -typedef std::function web_request_callback_f; -void webBodyRegister(web_body_callback_f callback); -void webRequestRegister(web_request_callback_f callback); + +using web_body_callback_f = std::function; +using web_request_callback_f = std::function; +void webBodyRegister(web_body_callback_f); +void webRequestRegister(web_request_callback_f); // ----------------------------------------------------------------------------- // WebSockets // ----------------------------------------------------------------------------- -#if WEB_SUPPORT - using ws_on_send_callback_f = std::function; - using ws_on_action_callback_f = std::function; - using ws_on_keycheck_callback_f = std::function; - using ws_on_send_callback_list_t = std::vector; - using ws_on_action_callback_list_t = std::vector; - using ws_on_keycheck_callback_list_t = std::vector; +// TODO: pending configuration headers refactoring... here for now +struct ws_counter_t; +struct ws_data_t; +struct ws_debug_t; +struct ws_callbacks_t; + +using ws_on_send_callback_f = std::function; +using ws_on_action_callback_f = std::function; +using ws_on_keycheck_callback_f = std::function; +using ws_on_send_callback_list_t = std::vector; +using ws_on_action_callback_list_t = std::vector; +using ws_on_keycheck_callback_list_t = std::vector; + +#if WEB_SUPPORT struct ws_callbacks_t { ws_on_send_callback_list_t on_visible; ws_on_send_callback_list_t on_connected; @@ -287,39 +300,25 @@ void webRequestRegister(web_request_callback_f callback); ws_on_action_callback_list_t on_action; ws_on_keycheck_callback_list_t on_keycheck; - ws_callbacks_t& onVisible(ws_on_send_callback_f cb) { - on_visible.push_back(cb); - return *this; - } - - ws_callbacks_t& onConnected(ws_on_send_callback_f cb) { - on_connected.push_back(cb); - return *this; - } - - ws_callbacks_t& onData(ws_on_send_callback_f cb) { - on_data.push_back(cb); - return *this; - } - - ws_callbacks_t& onAction(ws_on_action_callback_f cb) { - on_action.push_back(cb); - return *this; - } - - ws_callbacks_t& onKeyCheck(ws_on_keycheck_callback_f cb) { - on_keycheck.push_back(cb); - return *this; - } - + ws_callbacks_t& onVisible(ws_on_send_callback_f); + ws_callbacks_t& onConnected(ws_on_send_callback_f); + ws_callbacks_t& onData(ws_on_send_callback_f); + ws_callbacks_t& onAction(ws_on_action_callback_f); + ws_callbacks_t& onKeyCheck(ws_on_keycheck_callback_f); }; ws_callbacks_t& wsRegister(); - void wsSend(uint32_t, JsonObject& root); - void wsSend(JsonObject& root); - void wsSend(ws_on_send_callback_f sender); + void wsSetup(); + void wsSend(uint32_t, const char*); + void wsSend(uint32_t, JsonObject&); + void wsSend(JsonObject&); + void wsSend(ws_on_send_callback_f); + + void wsSend_P(PGM_P); + void wsSend_P(uint32_t, PGM_P); + void wsPost(const ws_on_send_callback_f&); void wsPost(const ws_on_send_callback_list_t&); void wsPost(uint32_t, const ws_on_send_callback_list_t&); @@ -332,27 +331,22 @@ void webRequestRegister(web_request_callback_f callback); bool wsConnected(); bool wsConnected(uint32_t); bool wsDebugSend(const char*, const char*); -#else - #define ws_on_send_callback_f void * - #define ws_on_action_callback_f void * - #define ws_on_receive_callback_f void * #endif // ----------------------------------------------------------------------------- // WIFI // ----------------------------------------------------------------------------- -#include "JustWifi.h" -typedef std::function wifi_callback_f; +#include +using wifi_callback_f = std::function; void wifiRegister(wifi_callback_f callback); bool wifiConnected(); +// ----------------------------------------------------------------------------- // THERMOSTAT // ----------------------------------------------------------------------------- +using thermostat_callback_f = std::function; #if THERMOSTAT_SUPPORT - typedef std::function thermostat_callback_f; void thermostatRegister(thermostat_callback_f callback); -#else - #define thermostat_callback_f void * #endif // ----------------------------------------------------------------------------- diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 564e5682..0d1f8cba 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -239,7 +239,9 @@ void ntpSetup() { } else if (error == invalidAddress) { DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n")); } - wsPost(_ntpWebSocketOnData); + #if WEB_SUPPORT + wsPost(_ntpWebSocketOnData); + #endif } else { _ntp_report = true; setTime(NTPw.getLastNTPSync()); diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index acdb840c..3e967de6 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -22,6 +22,31 @@ Ticker _web_defer; // WS callbacks // ----------------------------------------------------------------------------- +ws_callbacks_t& ws_callbacks_t::onVisible(ws_on_send_callback_f cb) { + on_visible.push_back(cb); + return *this; +} + +ws_callbacks_t& ws_callbacks_t::onConnected(ws_on_send_callback_f cb) { + on_connected.push_back(cb); + return *this; +} + +ws_callbacks_t& ws_callbacks_t::onData(ws_on_send_callback_f cb) { + on_data.push_back(cb); + return *this; +} + +ws_callbacks_t& ws_callbacks_t::onAction(ws_on_action_callback_f cb) { + on_action.push_back(cb); + return *this; +} + +ws_callbacks_t& ws_callbacks_t::onKeyCheck(ws_on_keycheck_callback_f cb) { + on_keycheck.push_back(cb); + return *this; +} + ws_callbacks_t _ws_callbacks; struct ws_counter_t {