From 453157c5db46e973eed05d0fcd1f0cd2acbea501 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 6 Aug 2021 18:52:47 +0300 Subject: [PATCH] all: more static symbols --- code/espurna/api.cpp | 9 +- code/espurna/button.cpp | 54 ++++++---- code/espurna/crash.cpp | 2 +- code/espurna/garland.cpp | 1 + code/espurna/gpio.cpp | 6 +- code/espurna/led.cpp | 53 ++++++---- code/espurna/main.cpp | 26 +++-- code/espurna/mqtt.cpp | 32 ++++++ code/espurna/ntp.cpp | 28 +++++- code/espurna/relay.cpp | 52 +++++++++- code/espurna/rtcmem.cpp | 4 + code/espurna/scheduler.cpp | 2 + code/espurna/sensor.cpp | 196 ++++++++++++++++++++++--------------- code/espurna/sensor.h | 10 +- code/espurna/settings.cpp | 6 +- code/espurna/terminal.cpp | 7 +- code/espurna/web.cpp | 12 ++- code/espurna/ws.cpp | 44 ++++++--- 18 files changed, 383 insertions(+), 161 deletions(-) diff --git a/code/espurna/api.cpp b/code/espurna/api.cpp index 723ceb03..91a14b8b 100644 --- a/code/espurna/api.cpp +++ b/code/espurna/api.cpp @@ -234,6 +234,8 @@ size_t ApiRequest::wildcards() const { #if API_SUPPORT +namespace { + bool _apiAccepts(AsyncWebServerRequest* request, const __FlashStringHelper* str) { auto* header = request->getHeader(F("Accept")); if (header) { @@ -658,10 +660,6 @@ private: ApiBasicHandler _put; }; -// ----------------------------------------------------------------------------- - -namespace { - std::forward_list _apis; template @@ -674,6 +672,8 @@ void _apiRegister(const String& path, Callback&& get, Callback&& put) { } // namespace +// ----------------------------------------------------------------------------- + void apiRegister(const String& path, ApiBasicHandler&& get, ApiBasicHandler&& put) { _apiRegister(path, std::move(get), std::move(put)); } @@ -718,4 +718,3 @@ bool apiError(ApiRequest& request) { } #endif // API_SUPPORT - diff --git a/code/espurna/button.cpp b/code/espurna/button.cpp index 427d10a8..f42ad1f0 100644 --- a/code/espurna/button.cpp +++ b/code/espurna/button.cpp @@ -168,6 +168,8 @@ ButtonAction convert(const String& value) { // ----------------------------------------------------------------------------- +namespace { + constexpr ButtonAction _buttonDecodeEventAction(const ButtonActions& actions, ButtonEvent event) { return ( (event == ButtonEvent::Pressed) ? actions.pressed : @@ -193,18 +195,6 @@ constexpr ButtonEvent _buttonMapReleased(uint8_t count, unsigned long length, un ); } -ButtonActions _buttonConstructActions(size_t index) { - return { - button::build::press(index), - button::build::release(index), - button::build::click(index), - button::build::doubleClick(index), - button::build::longClick(index), - button::build::longLongClick(index), - button::build::tripleClick(index) - }; -} - debounce_event::types::Config _buttonRuntimeConfig(size_t index) { return { getSetting({"btnMode", index}, button::build::mode(index)), @@ -213,9 +203,7 @@ debounce_event::types::Config _buttonRuntimeConfig(size_t index) { }; } -int _buttonEventNumber(ButtonEvent event) { - return static_cast(event); -} +} // namespace // ----------------------------------------------------------------------------- @@ -265,7 +253,7 @@ ButtonEvent button_t::loop() { return ButtonEvent::None; } -std::vector _buttons; +static std::vector _buttons; // ----------------------------------------------------------------------------- @@ -275,6 +263,8 @@ size_t buttonCount() { #if MQTT_SUPPORT +namespace { + std::bitset _buttons_mqtt_send_all( button::build::mqttSendAllEvents() ? std::numeric_limits::max() @@ -286,12 +276,16 @@ std::bitset _buttons_mqtt_retain( : std::numeric_limits::min() ); +} // namespace + #endif // ----------------------------------------------------------------------------- #if RELAY_SUPPORT +namespace { + std::vector _button_relays; size_t _buttonRelay(size_t id) { @@ -323,6 +317,8 @@ void _buttonRelayAction(size_t id, ButtonAction action) { } } +} // namespace + #endif // RELAY_SUPPORT // ----------------------------------------------------------------------------- @@ -353,13 +349,13 @@ bool _buttonWebSocketOnKeyCheck(const char * key, JsonVariant&) { //------------------------------------------------------------------------------ -ButtonEventHandler _button_custom_action { nullptr }; +static ButtonEventHandler _button_custom_action { nullptr }; void buttonSetCustomAction(ButtonEventHandler handler) { _button_custom_action = handler; } -std::forward_list _button_notify_event; +static std::forward_list _button_notify_event; void buttonOnEvent(ButtonEventHandler handler) { _button_notify_event.push_front(handler); @@ -373,10 +369,14 @@ ButtonAction buttonAction(size_t id, ButtonEvent event) { : ButtonAction::None; } +namespace { + // Note that we don't directly return F(...), but use a temporary to assign it conditionally // (ref. https://github.com/esp8266/Arduino/pull/6950 "PROGMEM footprint cleanup for responseCodeToString") // In this particular case, saves 76 bytes (120 vs 44) +#if DEBUG_SUPPORT || MQTT_SUPPORT + String _buttonEventString(ButtonEvent event) { const __FlashStringHelper* ptr = nullptr; switch (event) { @@ -408,10 +408,14 @@ String _buttonEventString(ButtonEvent event) { return String(ptr); } +#endif + +} // namespace + void buttonEvent(size_t id, ButtonEvent event) { DEBUG_MSG_P(PSTR("[BUTTON] Button #%u event %d (%s)\n"), - id, _buttonEventNumber(event), _buttonEventString(event).c_str() + id, static_cast(event), _buttonEventString(event).c_str() ); if (event == ButtonEvent::None) { @@ -511,6 +515,8 @@ void buttonEvent(size_t id, ButtonEvent event) { } +namespace { + void _buttonConfigure() { auto buttons = _buttons.size(); @@ -556,6 +562,8 @@ unsigned long _buttonGetDelay(const char* key, size_t index, unsigned long defau return result; } +} // namespace + void buttonLoop() { for (size_t id = 0; id < _buttons.size(); ++id) { auto event = _buttons[id].loop(); @@ -573,6 +581,8 @@ void buttonLoop() { #if BUTTON_PROVIDER_ANALOG_SUPPORT +namespace { + class AnalogPin final : public BasePin { public: static constexpr int RangeFrom { 0 }; @@ -696,8 +706,12 @@ private: std::vector AnalogPin::pins; +} // namespace + #endif // BUTTON_PROVIDER_ANALOG_SUPPORT +namespace { + BasePinPtr _buttonGpioPin(size_t index, ButtonProvider provider) { BasePinPtr result; @@ -802,6 +816,8 @@ void _buttonSettingsMigrate(int version) { } } +} // namespace + bool buttonAdd() { const size_t index { buttonCount() }; if ((index + 1) < ButtonsMax) { diff --git a/code/espurna/crash.cpp b/code/espurna/crash.cpp index 4e577283..10e7c2a1 100644 --- a/code/espurna/crash.cpp +++ b/code/espurna/crash.cpp @@ -26,7 +26,7 @@ Copyright (C) 2019-2020 by Maxim Prokhorov #include +#include #include #include diff --git a/code/espurna/gpio.cpp b/code/espurna/gpio.cpp index 6c40af9c..10ae8328 100644 --- a/code/espurna/gpio.cpp +++ b/code/espurna/gpio.cpp @@ -155,6 +155,8 @@ BasePinPtr gpioRegister(unsigned char gpio) { #if WEB_SUPPORT +namespace { + void _gpioWebSocketOnVisible(JsonObject& root) { JsonObject& config = root.createNestedObject("gpioConfig"); @@ -184,7 +186,9 @@ void _gpioWebSocketOnVisible(JsonObject& root) { } } -#endif +} // namespace + +#endif // WEB_SUPPORT void gpioSetup() { #if WEB_SUPPORT diff --git a/code/espurna/led.cpp b/code/espurna/led.cpp index 9932fde7..6d30293a 100644 --- a/code/espurna/led.cpp +++ b/code/espurna/led.cpp @@ -21,6 +21,8 @@ Copyright (C) 2019-2021 by Maxim Prokhorov _leds; bool _led_update { false }; +} // namespace + // ----------------------------------------------------------------------------- namespace settings { @@ -166,6 +170,7 @@ LedMode convert(const String& value) { // ----------------------------------------------------------------------------- namespace led { +namespace { namespace settings { unsigned char pin(size_t id) { @@ -189,8 +194,11 @@ LedPattern pattern(size_t id) { } } // namespace settings +} // namespace } // namespace led +namespace { + bool _ledStatus(Led& led) { return led.started() || led.status(); } @@ -219,14 +227,13 @@ bool _ledStatus(Led& led, bool status) { return result; } +[[gnu::unused]] void _ledPattern(Led& led, LedPattern&& pattern) { led.pattern(std::move(pattern)); _ledStatus(led, true); } -bool _ledToggle(Led& led) { - return _ledStatus(led, !_ledStatus(led)); -} +} // namespace bool ledStatus(size_t id, bool status) { if (id < ledCount()) { @@ -244,6 +251,8 @@ bool ledStatus(size_t id) { return false; } +namespace { + const LedDelay& _ledDelayFromName(LedDelayName name) { switch (name) { case LedDelayName::NetworkConnected: @@ -300,8 +309,12 @@ inline void _ledBlink(Led& led, LedDelayName name) { _ledBlink(led, _ledDelayFromName(name)); } +} // namespace + #if WEB_SUPPORT +namespace { + bool _ledWebSocketOnKeyCheck(const char * key, JsonVariant& value) { return (strncmp(key, "led", 3) == 0); } @@ -346,9 +359,14 @@ void _ledWebSocketOnConnected(JsonObject& root) { } } -#endif +} // namespace + +#endif // WEB_SUPPORT + +namespace { #if MQTT_SUPPORT + void _ledMQTTCallback(unsigned int type, const char* topic, const char* payload) { if (type == MQTT_CONNECT_EVENT) { char buffer[strlen(MQTT_TOPIC_LED) + 3]; @@ -382,7 +400,7 @@ void _ledMQTTCallback(unsigned int type, const char* topic, const char* payload) _ledStatus(led, (value == PayloadStatus::On)); return; case PayloadStatus::Toggle: - _ledToggle(led); + _ledStatus(led, !_ledStatus(led)); return; case PayloadStatus::Unknown: _ledPattern(led, _ledLoadPattern(payload)); @@ -391,7 +409,7 @@ void _ledMQTTCallback(unsigned int type, const char* topic, const char* payload) } } -#endif +#endif // MQTT_SUPPORT #if RELAY_SUPPORT std::vector _led_relays; @@ -408,6 +426,18 @@ void _ledConfigure() { _led_update = true; } +void _ledSettingsMigrate(int version) { + if (version < 5) { + delSettingPrefix({ + "ledGPIO", + "ledGpio", + "ledLogic" + }); + } +} + +} // namespace + // ----------------------------------------------------------------------------- size_t ledCount() { @@ -537,16 +567,6 @@ void ledLoop() { } -void _ledSettingsMigrate(int version) { - if (version < 5) { - delSettingPrefix({ - "ledGPIO", - "ledGpio", - "ledLogic" - }); - } -} - void ledSetup() { migrateVersion(_ledSettingsMigrate); _leds.reserve(led::build::preconfiguredLeds()); @@ -593,5 +613,4 @@ void ledSetup() { } } - #endif // LED_SUPPORT diff --git a/code/espurna/main.cpp b/code/espurna/main.cpp index 191dc0f7..d4e23479 100644 --- a/code/espurna/main.cpp +++ b/code/espurna/main.cpp @@ -23,6 +23,12 @@ along with this program. If not, see . #include "espurna.h" #include "main.h" +// ----------------------------------------------------------------------------- +// GENERAL CALLBACKS +// ----------------------------------------------------------------------------- + +namespace { + std::vector _loop_callbacks; std::vector _reload_callbacks; @@ -32,9 +38,7 @@ unsigned long _loop_delay = 0; constexpr unsigned long LoopDelayMin { 10ul }; constexpr unsigned long LoopDelayMax { 300ul }; -// ----------------------------------------------------------------------------- -// GENERAL CALLBACKS -// ----------------------------------------------------------------------------- +} // namespace void espurnaRegisterLoop(LoopCallback callback) { _loop_callbacks.push_back(callback); @@ -48,12 +52,6 @@ void espurnaReload() { _reload_config = true; } -void _espurnaReload() { - for (const auto& callback : _reload_callbacks) { - callback(); - } -} - unsigned long espurnaLoopDelay() { return _loop_delay; } @@ -62,10 +60,20 @@ void espurnaLoopDelay(unsigned long loop_delay) { _loop_delay = loop_delay; } +namespace { + constexpr unsigned long _loopDelay() { return LOOP_DELAY_TIME; } +void _espurnaReload() { + for (const auto& callback : _reload_callbacks) { + callback(); + } +} + +} // namespace + // ----------------------------------------------------------------------------- // BOOTING // ----------------------------------------------------------------------------- diff --git a/code/espurna/mqtt.cpp b/code/espurna/mqtt.cpp index ae260223..4bfc1cc5 100644 --- a/code/espurna/mqtt.cpp +++ b/code/espurna/mqtt.cpp @@ -37,6 +37,8 @@ Updated secure client support by Niek van der Maas < mail at niekvandermaas dot // ----------------------------------------------------------------------------- +namespace { + #if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT AsyncMqttClient _mqtt; @@ -115,6 +117,8 @@ String _mqtt_payload_offline; std::forward_list _mqtt_callbacks; +} // namespace + // ----------------------------------------------------------------------------- // Settings // ----------------------------------------------------------------------------- @@ -326,6 +330,8 @@ uint16_t mfln() { // JSON payload // ----------------------------------------------------------------------------- +namespace { + struct MqttPayload { MqttPayload() = delete; MqttPayload(const MqttPayload&) = default; @@ -360,10 +366,14 @@ size_t _mqtt_json_payload_count { 0ul }; std::forward_list _mqtt_json_payload; Ticker _mqtt_json_payload_flush; +} // namespace + // ----------------------------------------------------------------------------- // Secure client handlers // ----------------------------------------------------------------------------- +namespace { + #if SECURE_CLIENT == SECURE_CLIENT_AXTLS SecureClientConfig _mqtt_sc_config { "MQTT", @@ -389,10 +399,14 @@ SecureClientConfig _mqtt_sc_config { }; #endif +} // namespace + // ----------------------------------------------------------------------------- // Client configuration & setup // ----------------------------------------------------------------------------- +namespace { + // TODO: MQTT standard has some weird rules about session persistance on the broker // ref. 3.1.2.4 Clean Session, where we are uniquely identified by the client-id: // - subscriptions that are no longer useful are still there @@ -717,10 +731,14 @@ void _mqttInfo() { } +} // namespace + // ----------------------------------------------------------------------------- // WEB // ----------------------------------------------------------------------------- +namespace { + #if WEB_SUPPORT bool _mqttWebSocketOnKeyCheck(const char * key, JsonVariant& value) { @@ -758,10 +776,14 @@ void _mqttWebSocketOnConnected(JsonObject& root) { #endif +} // namespace + // ----------------------------------------------------------------------------- // SETTINGS // ----------------------------------------------------------------------------- +namespace { + #if TERMINAL_SUPPORT void _mqttInitCommands() { @@ -781,10 +803,14 @@ void _mqttInitCommands() { #endif // TERMINAL_SUPPORT +} // namespace + // ----------------------------------------------------------------------------- // MQTT Callbacks // ----------------------------------------------------------------------------- +namespace { + void _mqttCallback(unsigned int type, const char * topic, const char * payload) { if (type == MQTT_CONNECT_EVENT) { mqttSubscribe(MQTT_TOPIC_ACTION); @@ -1016,6 +1042,8 @@ void _mqttOnMessage(char* topic, char* payload, unsigned int len) { #endif // MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT +} // namespace + // ----------------------------------------------------------------------------- // Public API // ----------------------------------------------------------------------------- @@ -1353,6 +1381,8 @@ void mqttSendStatus() { // Initialization // ----------------------------------------------------------------------------- +namespace { + void _mqttConnect() { // Do not connect if already connected or still trying to connect if (_mqtt.connected() || (_mqtt_state != AsyncClientState::Disconnected)) return; @@ -1402,6 +1432,8 @@ void _mqttConnect() { } +} // namespace + void mqttLoop() { #if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT _mqttConnect(); diff --git a/code/espurna/ntp.cpp b/code/espurna/ntp.cpp index 3350d4f8..14b59bf8 100644 --- a/code/espurna/ntp.cpp +++ b/code/espurna/ntp.cpp @@ -37,11 +37,13 @@ static_assert( // Arduino/esp8266 lwip2 custom functions that can be redefined // Must return time in milliseconds, legacy settings are in seconds. -String _ntp_server; +namespace { uint32_t _ntp_startup_delay = (NTP_START_DELAY * 1000); uint32_t _ntp_update_delay = (NTP_UPDATE_INTERVAL * 1000); +} // namespace + uint32_t sntp_startup_delay_MS_rfc_not_less_than_60000() { return _ntp_startup_delay; } @@ -53,6 +55,8 @@ uint32_t sntp_update_delay_MS_rfc_not_less_than_15000() { // We also must shim TimeLib functions until everything else is ported. // We can't sometimes avoid TimeLib as dependancy though, which would be really bad +namespace { + static bool _ntp_synced = false; static time_t _ntp_last = 0; @@ -69,6 +73,8 @@ void _ntpTmCache(time_t ts) { } } +} // namespace + int hour(time_t ts) { _ntpTmCache(ts); return _ntp_tm_local.tm_hour; @@ -147,6 +153,8 @@ time_t now() { // ----------------------------------------------------------------------------- +namespace { + #if WEB_SUPPORT bool _ntpWebSocketOnKeyCheck(const char * key, JsonVariant& value) { @@ -182,6 +190,8 @@ String _ntpGetServer() { return server; } +} // namespace + NtpInfo ntpInfo() { NtpInfo result; @@ -207,6 +217,10 @@ NtpInfo ntpInfo() { return result; } +namespace { + +String _ntp_server; + void _ntpReport() { if (!ntpSynced()) { DEBUG_MSG_P(PSTR("[NTP] Not synced\n")); @@ -259,6 +273,8 @@ void _ntpConfigure() { } } +} // namespace + // ----------------------------------------------------------------------------- bool ntpSynced() { @@ -294,10 +310,12 @@ String ntpDateTime() { // ----------------------------------------------------------------------------- +namespace { + using NtpTickCallbacks = std::forward_list; NtpTickCallbacks _ntp_tick_callbacks; -static Ticker _ntp_tick; +Ticker _ntp_tick; void _ntpTickSchedule(int offset); @@ -368,8 +386,12 @@ void _ntpSetTimestamp(time_t ts) { settimeofday(&tv, &tz); } +} // namespace + // ----------------------------------------------------------------------------- +namespace { + void _ntpConvertLegacyOffsets() { bool save { true }; bool found { false }; @@ -421,6 +443,8 @@ void _ntpConvertLegacyOffsets() { delSetting("ntpRegion"); } +} // namespace + void ntpOnTick(NtpTickCallback callback) { _ntp_tick_callbacks.push_front(callback); } diff --git a/code/espurna/relay.cpp b/code/espurna/relay.cpp index 41765765..a3ffb7b4 100644 --- a/code/espurna/relay.cpp +++ b/code/espurna/relay.cpp @@ -209,6 +209,8 @@ String serialize(RelayMaskHelper mask) { // RELAY CONTROL // ----------------------------------------------------------------------------- +namespace { + RelayProviderBase* _relayDummyProvider(); struct relay_t { @@ -286,6 +288,8 @@ String _relay_rpc_payload_toggle; #endif // MQTT_SUPPORT || API_SUPPORT +} // namespace + // ----------------------------------------------------------------------------- // RELAY PROVIDERS // ----------------------------------------------------------------------------- @@ -329,6 +333,8 @@ struct DummyProvider : public RelayProviderBase { } }; +namespace { + RelayProviderBase* _relayDummyProvider() { static DummyProvider provider; return &provider; @@ -569,10 +575,14 @@ private: #endif // RELAY_PROVIDER_STM_SUPPORT +} // namespace + // ----------------------------------------------------------------------------- // UTILITY // ----------------------------------------------------------------------------- +namespace { + bool _relayTryParseId(const char* p, size_t& id) { return tryParseId(p, relayCount, id); } @@ -733,6 +743,8 @@ void _relaySyncUnlock() { } } +} // namespace + // ----------------------------------------------------------------------------- // RELAY // ----------------------------------------------------------------------------- @@ -977,6 +989,8 @@ void relaySync(size_t target) { _relay_sync_reent = false; } +namespace { + RelayMaskHelper _relayMaskCurrent() { RelayMaskHelper mask; for (size_t id = 0; id < _relays.size(); ++id) { @@ -985,6 +999,8 @@ RelayMaskHelper _relayMaskCurrent() { return mask; } +} // namespace + void relaySave(bool persist) { // Persist only to rtcmem, unless requested to save to settings auto mask = _relayMaskCurrent(); @@ -1043,6 +1059,8 @@ PayloadStatus relayParsePayload(const char * payload) { #endif } +namespace { + void _relaySettingsMigrate(int version) { if (version < 5) { // just a rename @@ -1171,12 +1189,16 @@ void _relayConfigure() { #endif // MQTT_SUPPORT } +} // namespace + //------------------------------------------------------------------------------ // WEBSOCKETS //------------------------------------------------------------------------------ #if WEB_SUPPORT +namespace { + bool _relayWebSocketOnKeyCheck(const char * key, JsonVariant& value) { return (strncmp(key, "relay", 5) == 0); } @@ -1283,6 +1305,8 @@ void _relayWebSocketOnAction(uint32_t client_id, const char* action, JsonObject& } } +} // namespace + void relaySetupWS() { wsRegister() .onVisible(_relayWebSocketOnVisible) @@ -1300,6 +1324,8 @@ void relaySetupWS() { #if API_SUPPORT +namespace { + template bool _relayApiTryHandle(ApiRequest& request, T&& callback) { auto id_param = request.wildcard(0); @@ -1311,6 +1337,8 @@ bool _relayApiTryHandle(ApiRequest& request, T&& callback) { return callback(id); } +} // namespace + void relaySetupAPI() { if (!relayCount()) { @@ -1574,8 +1602,6 @@ void _relayMqttPublishCustomTopic(size_t id) { mqttSendRaw(topic.c_str(), relayPayload(status)); } -} // namespace - void _relayMqttReport(size_t id) { if (_relays[id].report) { _relays[id].report = false; @@ -1594,6 +1620,8 @@ void _relayMqttReportAll() { } } +} // namespace + void relayStatusWrap(size_t id, PayloadStatus value, bool is_group_topic) { #if MQTT_SUPPORT const auto forward = mqttForward(); @@ -1618,6 +1646,8 @@ void relayStatusWrap(size_t id, PayloadStatus value, bool is_group_topic) { } } +namespace { + bool _relayMqttHeartbeat(heartbeat::Mask mask) { if (mask & heartbeat::Report::Relay) _relayMqttReportAll(); @@ -1659,6 +1689,8 @@ void _relayMqttHandleDisconnect() { }); } +} // namespace + void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) { static bool connected { false }; @@ -1735,6 +1767,8 @@ void relaySetupMQTT() { #if TERMINAL_SUPPORT +namespace { + void _relayInitCommands() { terminalRegisterCommand(F("RELAY"), [](const terminal::CommandContext& ctx) { @@ -1803,10 +1837,14 @@ void _relayInitCommands() { } +} // namespace + #endif // TERMINAL_SUPPORT //------------------------------------------------------------------------------ +namespace { + void _relayReport(size_t id [[gnu::unused]], bool status [[gnu::unused]]) { for (auto& change : _relay_status_change) { change(id, status); @@ -1875,10 +1913,14 @@ void _relayProcess(bool mode) { } } +} // namespace + //------------------------------------------------------------------------------ // Setup //------------------------------------------------------------------------------ +namespace { + void _relayLoop() { _relayProcess(false); _relayProcess(true); @@ -1890,6 +1932,8 @@ void _relayLoop() { #endif } +} // namespace + // Dummy relays for virtual light switches (hardware-less), Sonoff Dual, Sonoff RF Bridge and Tuya void relaySetupDummy(size_t size, bool reconfigure) { @@ -1910,6 +1954,8 @@ void relaySetupDummy(size_t size, bool reconfigure) { } } +namespace { + constexpr size_t _relayAdhocPins() { return 0 #if RELAY1_PIN != GPIO_NONE @@ -2016,6 +2062,8 @@ void _relaySetup() { relaySetupDummy(getSetting("relayDummy", relay::build::dummyCount())); } +} // namespace + void relaySetup() { migrateVersion(_relaySettingsMigrate); diff --git a/code/espurna/rtcmem.cpp b/code/espurna/rtcmem.cpp index a9c4881e..bcc09ebb 100644 --- a/code/espurna/rtcmem.cpp +++ b/code/espurna/rtcmem.cpp @@ -10,6 +10,8 @@ Copyright (C) 2019 by Maxim Prokhorov volatile RtcmemData* Rtcmem = reinterpret_cast(RTCMEM_ADDR); +namespace { + bool _rtcmem_status = false; void _rtcmemErase() { @@ -91,6 +93,8 @@ void _rtcmemInitCommands() { #endif +} // namespace + bool rtcmemStatus() { return _rtcmem_status; } diff --git a/code/espurna/scheduler.cpp b/code/espurna/scheduler.cpp index 2adb2473..0d2f7bda 100644 --- a/code/espurna/scheduler.cpp +++ b/code/espurna/scheduler.cpp @@ -23,6 +23,7 @@ Adapted by Xose Pérez // ----------------------------------------------------------------------------- namespace scheduler { +namespace { namespace build { constexpr size_t max() { @@ -572,6 +573,7 @@ void check(time_t timestamp, const Schedules& schedules) { } } +} // namespace } // namespace scheduler // ----------------------------------------------------------------------------- diff --git a/code/espurna/sensor.cpp b/code/espurna/sensor.cpp index 81d179a8..01b3533a 100644 --- a/code/espurna/sensor.cpp +++ b/code/espurna/sensor.cpp @@ -3,13 +3,16 @@ SENSOR MODULE Copyright (C) 2016-2019 by Xose Pérez +Copyright (C) 2020-2021 by Maxim Prokhorov */ -#include "sensor.h" +#include "espurna.h" #if SENSOR_SUPPORT +#include "sensor.h" + #include "api.h" #include "domoticz.h" #include "i2c.h" @@ -28,7 +31,7 @@ Copyright (C) 2016-2019 by Xose Pérez //-------------------------------------------------------------------------------- -// TODO: namespace { ... } ? sensor ctors need to work though +namespace { #include "filters/LastFilter.h" #include "filters/MaxFilter.h" @@ -36,6 +39,8 @@ Copyright (C) 2016-2019 by Xose Pérez #include "filters/MovingAverageFilter.h" #include "filters/SumFilter.h" +} // namespace + #include "sensors/BaseSensor.h" #include "sensors/BaseEmonSensor.h" #include "sensors/BaseAnalogEmonSensor.h" @@ -217,10 +222,10 @@ Copyright (C) 2016-2019 by Xose Pérez //-------------------------------------------------------------------------------- -struct sensor_magnitude_t { - - private: +namespace { +class sensor_magnitude_t { +private: constexpr static double _unset = std::numeric_limits::quiet_NaN(); static unsigned char _counts[MAGNITUDE_MAX]; @@ -231,8 +236,7 @@ struct sensor_magnitude_t { other.filter = nullptr; } - public: - +public: static unsigned char counts(unsigned char type) { return _counts[type]; } @@ -274,7 +278,6 @@ struct sensor_magnitude_t { double correction { 0.0 }; // Value correction (applied when processing) double zero_threshold { _unset }; // Reset value to zero when below threshold (applied when reading) - }; static_assert( @@ -289,6 +292,8 @@ static_assert( unsigned char sensor_magnitude_t::_counts[MAGNITUDE_MAX] = {0}; +} // namespace + namespace sensor { // Base units @@ -383,6 +388,8 @@ void Energy::reset() { } // namespace sensor +namespace { + // ----------------------------------------------------------------------------- // Configuration // ----------------------------------------------------------------------------- @@ -407,10 +414,14 @@ constexpr bool _magnitudeCanUseCorrection(unsigned char type) { ); } +} // namespace + // ----------------------------------------------------------------------------- // Energy persistence // ----------------------------------------------------------------------------- +namespace { + std::vector _sensor_save_count; unsigned char _sensor_save_every = SENSOR_SAVE_EVERY; @@ -493,10 +504,6 @@ sensor::Energy _sensorEnergyTotal(unsigned char index) { return result; } -sensor::Energy sensorEnergyTotal() { - return _sensorEnergyTotal(0); -} - void _sensorResetEnergyTotal(unsigned char index) { delSetting({"eneTotal", index}); delSetting({"eneTime", index}); @@ -534,7 +541,17 @@ void _magnitudeSaveEnergyTotal(sensor_magnitude_t& magnitude, bool persistent) { } } -// --------------------------------------------------------------------------- +} // namespace + +sensor::Energy sensorEnergyTotal() { + return _sensorEnergyTotal(0); +} + +// ----------------------------------------------------------------------------- +// Data processing +// ----------------------------------------------------------------------------- + +namespace { std::vector _sensors; std::vector _magnitudes; @@ -544,26 +561,10 @@ bool _sensor_realtime = API_REAL_TIME_VALUES; unsigned long _sensor_read_interval = 1000 * SENSOR_READ_INTERVAL; unsigned char _sensor_report_every = SENSOR_REPORT_EVERY; -// --------------------------------------------------------------------------- - using MagnitudeReadHandlers = std::forward_list; - MagnitudeReadHandlers _magnitude_read_handlers; - -void sensorOnMagnitudeRead(MagnitudeReadHandler handler) { - _magnitude_read_handlers.push_front(handler); -} - MagnitudeReadHandlers _magnitude_report_handlers; -void sensorOnMagnitudeReport(MagnitudeReadHandler handler) { - _magnitude_report_handlers.push_front(handler); -} - -// ----------------------------------------------------------------------------- -// Private -// ----------------------------------------------------------------------------- - BaseFilter* _magnitudeCreateFilter(unsigned char type, size_t size) { BaseFilter* filter { nullptr }; @@ -649,7 +650,7 @@ unsigned char _sensorUnitDecimals(sensor::Unit unit) { } } -String magnitudeTopic(unsigned char type) { +String _magnitudeTopic(unsigned char type) { const __FlashStringHelper* result = nullptr; @@ -778,10 +779,6 @@ String magnitudeTopic(unsigned char type) { } -String _magnitudeTopic(const sensor_magnitude_t& magnitude) { - return magnitudeTopic(magnitude.type); -} - String _magnitudeUnits(const sensor_magnitude_t& magnitude) { const __FlashStringHelper* result = nullptr; @@ -867,11 +864,15 @@ String _magnitudeUnits(const sensor_magnitude_t& magnitude) { } +} // namespace + String magnitudeUnits(unsigned char index) { if (index >= magnitudeCount()) return String(); return _magnitudeUnits(_magnitudes[index]); } +namespace { + // Choose unit based on type of magnitude we use sensor::Unit _magnitudeUnitFilter(const sensor_magnitude_t& magnitude, sensor::Unit updated) { @@ -1135,41 +1136,17 @@ return_defaults: } +} // namespace + #if WEB_SUPPORT +namespace { + bool _sensorWebSocketOnKeyCheck(const char* key, JsonVariant&) { return _sensorMatchKeyPrefix(key); } -// Used by modules to generate magnitude_id<->module_id mapping for the WebUI -// WS produces tuples Magnitudes that contain type, sensor's global index and module's index -// Settings use Magnitude keys to allow us to retrieve module's index - -void sensorWebSocketMagnitudes(JsonObject& root, const String& prefix) { - const String wsKey = prefix + F("Magnitudes"); - const String confKey = wsKey.substring(0, wsKey.length() - 1); - - JsonObject& namedList = root.createNestedObject(wsKey); - - static const char* const keys[] PROGMEM = { - "type", "index_global", "index_module" - }; - - JsonArray& schema = namedList.createNestedArray("schema"); - schema.copyFrom(keys, sizeof(keys) / sizeof(*keys)); - - JsonArray& values = namedList.createNestedArray("values"); - for (size_t index = 0; index < _magnitudes.size(); ++index) { - JsonArray& tuple = values.createNestedArray(); - - auto& magnitude = _magnitudes[index]; - tuple.add(magnitude.type); - tuple.add(magnitude.index_global); - tuple.add(getSetting({confKey, index}, 0)); - } -} - -String sensorError(unsigned char error) { +String _sensorError(unsigned char error) { const __FlashStringHelper* result = nullptr; @@ -1211,7 +1188,7 @@ String sensorError(unsigned char error) { } -String magnitudeName(unsigned char type) { +String _magnitudeName(unsigned char type) { const __FlashStringHelper* result = nullptr; @@ -1352,7 +1329,7 @@ void _sensorWebSocketTypes(JsonObject& root) { JsonArray& value = values.createNestedArray(); value.add(type); value.add(_magnitudeSettingsPrefix(type)); - value.add(magnitudeName(type)); + value.add(_magnitudeName(type)); }); } @@ -1369,7 +1346,7 @@ void _sensorWebSocketErrors(JsonObject& root) { _sensorForEachError([&](unsigned char type) { JsonArray& value = values.createNestedArray(); value.add(type); - value.add(sensorError(type)); + value.add(_sensorError(type)); }); } @@ -1498,12 +1475,44 @@ void _sensorWebSocketOnConnected(JsonObject& root) { } } +} // namespace + +// Used by modules to generate magnitude_id<->module_id mapping for the WebUI +// WS produces tuples Magnitudes that contain type, sensor's global index and module's index +// Settings use Magnitude keys to allow us to retrieve module's index + +void sensorWebSocketMagnitudes(JsonObject& root, const String& prefix) { + const String wsKey = prefix + F("Magnitudes"); + const String confKey = wsKey.substring(0, wsKey.length() - 1); + + JsonObject& namedList = root.createNestedObject(wsKey); + + static const char* const keys[] PROGMEM = { + "type", "index_global", "index_module" + }; + + JsonArray& schema = namedList.createNestedArray("schema"); + schema.copyFrom(keys, sizeof(keys) / sizeof(*keys)); + + JsonArray& values = namedList.createNestedArray("values"); + for (size_t index = 0; index < _magnitudes.size(); ++index) { + JsonArray& tuple = values.createNestedArray(); + + auto& magnitude = _magnitudes[index]; + tuple.add(magnitude.type); + tuple.add(magnitude.index_global); + tuple.add(getSetting({confKey, index}, 0)); + } +} + #endif // WEB_SUPPORT #if API_SUPPORT +namespace { + String _sensorApiMagnitudeName(sensor_magnitude_t& magnitude) { - String name = magnitudeTopic(magnitude.type); + String name = _magnitudeTopic(magnitude.type); if (SENSOR_USE_INDEX || (sensor_magnitude_t::counts(magnitude.type) > 1)) name = name + "/" + String(magnitude.index_global); return name; @@ -1542,7 +1551,6 @@ bool _sensorApiTryHandle(ApiRequest& request, unsigned char type, T&& callback) } void _sensorApiSetup() { - apiRegister(F("magnitudes"), [](ApiRequest&, JsonObject& root) { JsonArray& magnitudes = root.createNestedArray("magnitudes"); @@ -1558,7 +1566,7 @@ void _sensorApiSetup() { ); _magnitudeForEachCounted([](unsigned char type) { - String pattern = magnitudeTopic(type); + String pattern = _magnitudeTopic(type); if (SENSOR_USE_INDEX || (sensor_magnitude_t::counts(type) > 1)) { pattern += "/+"; } @@ -1589,15 +1597,18 @@ void _sensorApiSetup() { apiRegister(pattern, std::move(get), std::move(put)); }); - } +} // namespace + #endif // API_SUPPORT == 1 #if MQTT_SUPPORT +namespace { + void _sensorMqttCallback(unsigned int type, const char* topic, char* payload) { - static const auto energy_topic = magnitudeTopic(MAGNITUDE_ENERGY); + static const auto energy_topic = _magnitudeTopic(MAGNITUDE_ENERGY); switch (type) { case MQTT_MESSAGE_EVENT: { String t = mqttMagnitude((char *) topic); @@ -1628,10 +1639,14 @@ void _sensorMqttCallback(unsigned int type, const char* topic, char* payload) { } } +} // namespace + #endif // MQTT_SUPPORT == 1 #if TERMINAL_SUPPORT +namespace { + void _sensorInitCommands() { terminalRegisterCommand(F("MAGNITUDES"), [](const terminal::CommandContext&) { char last[64]; @@ -1642,7 +1657,7 @@ void _sensorInitCommands() { dtostrf(magnitude.reported, 1, magnitude.decimals, reported); DEBUG_MSG_P(PSTR("[SENSOR] %2u * %s/%u @ %s (last:%s, reported:%s)\n"), index, - magnitudeTopic(magnitude.type).c_str(), + _magnitudeTopic(magnitude.type).c_str(), magnitude.index_global, _magnitudeDescription(magnitude).c_str(), last, reported @@ -1652,8 +1667,12 @@ void _sensorInitCommands() { }); } +} // namespace + #endif // TERMINAL_SUPPORT == 1 +namespace { + void _sensorTick() { for (auto* sensor : _sensors) { sensor->tick(); @@ -1678,10 +1697,14 @@ void _sensorPost() { } } +} // namespace + // ----------------------------------------------------------------------------- // Sensor initialization // ----------------------------------------------------------------------------- +namespace { + void _sensorLoad() { /* @@ -2311,7 +2334,7 @@ void _sensorLoad() { String _magnitudeTopicIndex(const sensor_magnitude_t& magnitude) { char buffer[32] = {0}; - String topic { magnitudeTopic(magnitude.type) }; + String topic { _magnitudeTopic(magnitude.type) }; if (SENSOR_USE_INDEX || (sensor_magnitude_t::counts(magnitude.type) > 1)) { snprintf(buffer, sizeof(buffer), "%s/%u", topic.c_str(), magnitude.index_global); } else { @@ -2329,7 +2352,7 @@ void _sensorReport(unsigned char index, const sensor_magnitude_t& magnitude) { dtostrf(magnitude.reported, 1, magnitude.decimals, buffer); for (auto& handler : _magnitude_report_handlers) { - handler(magnitudeTopic(magnitude.type), magnitude.index_global, magnitude.reported, buffer); + handler(_magnitudeTopic(magnitude.type), magnitude.index_global, magnitude.reported, buffer); } #if MQTT_SUPPORT @@ -2365,7 +2388,6 @@ void _sensorReport(unsigned char index, const sensor_magnitude_t& magnitude) { } void _sensorInit() { - _sensors_ready = true; for (auto& sensor : _sensors) { @@ -2405,7 +2427,7 @@ void _sensorInit() { } DEBUG_MSG_P(PSTR("[SENSOR] -> %s:%u\n"), - magnitudeTopic(magnitude.type).c_str(), + _magnitudeTopic(magnitude.type).c_str(), sensor_magnitude_t::counts(magnitude.type)); } @@ -2425,9 +2447,10 @@ void _sensorInit() { } } - } +} // namespace + namespace settings { namespace internal { @@ -2453,6 +2476,8 @@ String serialize(sensor::Unit unit) { } // namespace internal } // namespace settings +namespace { + void _sensorConfigure() { // General sensor settings for reporting and saving @@ -2654,10 +2679,19 @@ void _sensorConfigure() { } +} // namespace + // ----------------------------------------------------------------------------- // Public // ----------------------------------------------------------------------------- +void sensorOnMagnitudeRead(MagnitudeReadHandler handler) { + _magnitude_read_handlers.push_front(handler); +} +void sensorOnMagnitudeReport(MagnitudeReadHandler handler) { + _magnitude_report_handlers.push_front(handler); +} + unsigned char sensorCount() { return _sensors.size(); } @@ -2673,6 +2707,10 @@ unsigned char magnitudeType(unsigned char index) { return MAGNITUDE_NONE; } +String magnitudeTopic(unsigned char type) { + return _magnitudeTopic(type); +} + double sensor::Value::get() { return _sensor_realtime ? last : reported; } @@ -2906,7 +2944,7 @@ void sensorLoop() { char buffer[64]; dtostrf(value_show, 1, magnitude.decimals, buffer); for (auto& handler : _magnitude_read_handlers) { - handler(magnitudeTopic(magnitude.type), magnitude.index_global, value_show, buffer); + handler(_magnitudeTopic(magnitude.type), magnitude.index_global, value_show, buffer); } } @@ -2920,7 +2958,7 @@ void sensorLoop() { dtostrf(value_show, 1, magnitude.decimals, buffer); DEBUG_MSG_P(PSTR("[SENSOR] %s - %s: %s%s\n"), _magnitudeDescription(magnitude).c_str(), - magnitudeTopic(magnitude.type).c_str(), + _magnitudeTopic(magnitude.type).c_str(), buffer, _magnitudeUnits(magnitude).c_str() ); diff --git a/code/espurna/sensor.h b/code/espurna/sensor.h index 711c7d80..bdf6df25 100644 --- a/code/espurna/sensor.h +++ b/code/espurna/sensor.h @@ -9,12 +9,7 @@ Copyright (C) 2020 by Maxim Prokhorov #pragma once -#include "espurna.h" - -//-------------------------------------------------------------------------------- - namespace sensor { - namespace type { static constexpr unsigned char Base { 0 }; @@ -132,7 +127,9 @@ struct Value { unsigned char decimals; }; -} +} // namespace sensor + +//-------------------------------------------------------------------------------- using MagnitudeReadHandler = void(*)(const String&, unsigned char, double, const char*); void sensorOnMagnitudeRead(MagnitudeReadHandler handler); @@ -162,4 +159,3 @@ void sensorWebSocketMagnitudes(JsonObject& root, const String& prefix); unsigned char sensorCount(); void sensorSetup(); void sensorLoop(); - diff --git a/code/espurna/settings.cpp b/code/espurna/settings.cpp index 351438eb..2c535857 100644 --- a/code/espurna/settings.cpp +++ b/code/espurna/settings.cpp @@ -25,7 +25,7 @@ namespace settings { // Depending on features enabled, we may end up with different left boundary // Settings are written right-to-left, so we only have issues when there are a lot of key-values // XXX: slightly hacky, because we EEPROMr.length() is 0 before we enter setup() code -kvs_type kv_store( +static kvs_type kv_store( EepromStorage{}, #if DEBUG_SUPPORT EepromReservedSize + CrashReservedSize, @@ -475,6 +475,8 @@ void settingsProcessConfig(const settings_cfg_list_t& config, settings_filter_t #if TERMINAL_SUPPORT +namespace { + void _settingsInitCommands() { terminalRegisterCommand(F("CONFIG"), [](const terminal::CommandContext& ctx) { // TODO: enough of a buffer? @@ -579,6 +581,8 @@ void _settingsInitCommands() { #endif } +} // namespace + #endif void settingsSetup() { diff --git a/code/espurna/terminal.cpp b/code/espurna/terminal.cpp index bad41782..539bc1ce 100644 --- a/code/espurna/terminal.cpp +++ b/code/espurna/terminal.cpp @@ -140,8 +140,6 @@ constexpr size_t _terminalBufferSize() { return TERMINAL_SHARED_BUFFER_SIZE; } -namespace { - using Io = TerminalIO<_terminalBufferSize()>; Io _io; @@ -152,12 +150,10 @@ terminal::Terminal _terminal(_io, Io::capacity()); constexpr size_t SerialRxBufferSize { 128u }; char _serial_rx_buffer[SerialRxBufferSize]; -static unsigned char _serial_rx_pointer = 0; +unsigned char _serial_rx_pointer = 0; #endif // SERIAL_RX_ENABLED -} // namespace - // ----------------------------------------------------------------------------- // Commands // ----------------------------------------------------------------------------- @@ -348,7 +344,6 @@ private: uint32_t _sectors; }; - void _terminalInitCommands() { terminalRegisterCommand(F("COMMANDS"), _terminalHelpCommand); diff --git a/code/espurna/web.cpp b/code/espurna/web.cpp index 5361c131..84dd8633 100644 --- a/code/espurna/web.cpp +++ b/code/espurna/web.cpp @@ -30,6 +30,8 @@ Copyright (C) 2016-2019 by Xose Pérez #if WEB_EMBEDDED +namespace { + #if WEBUI_IMAGE == WEBUI_IMAGE_SMALL #include "static/index.small.html.gz.h" #elif WEBUI_IMAGE == WEBUI_IMAGE_LIGHT @@ -52,6 +54,8 @@ Copyright (C) 2016-2019 by Xose Pérez #include "static/index.all.html.gz.h" #endif +} // namespace + #endif // WEB_EMBEDDED #if WEB_SSL_ENABLED @@ -59,7 +63,6 @@ Copyright (C) 2016-2019 by Xose Pérez #include "static/server.key.h" #endif // WEB_SSL_ENABLED - AsyncWebPrint::AsyncWebPrint(const AsyncWebPrintConfig& config, AsyncWebServerRequest* request) : mimeType(config.mimeType), backlogCountMax(config.backlogCountMax), @@ -206,6 +209,8 @@ size_t AsyncWebPrint::write(const uint8_t* data, size_t size) { // ----------------------------------------------------------------------------- +namespace { + AsyncWebServer* _server; char _last_modified[50]; std::vector * _webConfigBuffer; @@ -216,10 +221,14 @@ std::vector _web_body_callbacks; constexpr unsigned long WebConfigBufferMax { 4096ul }; +} // namespace + // ----------------------------------------------------------------------------- // HOOKS // ----------------------------------------------------------------------------- +namespace { + void _webRequestAuth(AsyncWebServerRequest* request) { request->requestAuthentication(getSetting("hostname", getIdentifier()).c_str(), true); } @@ -546,6 +555,7 @@ void _onBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t i } +} // namespace // ----------------------------------------------------------------------------- diff --git a/code/espurna/ws.cpp b/code/espurna/ws.cpp index 4e9aadf8..06e10fda 100644 --- a/code/espurna/ws.cpp +++ b/code/espurna/ws.cpp @@ -21,12 +21,12 @@ Copyright (C) 2016-2019 by Xose Pérez #include "libs/WebSocketIncommingBuffer.h" -AsyncWebSocket _ws("/ws"); - // ----------------------------------------------------------------------------- // Periodic updates // ----------------------------------------------------------------------------- +namespace { + uint32_t _ws_last_update = 0; void _wsResetUpdateTimer() { @@ -73,13 +73,20 @@ void _wsDoUpdate(const bool connected) { } } +} // namespace + // ----------------------------------------------------------------------------- // WS callbacks // ----------------------------------------------------------------------------- +namespace { + +AsyncWebSocket _ws("/ws"); std::queue _ws_queue; ws_callbacks_t _ws_callbacks; +} // namespace + void wsPost(uint32_t client_id, ws_on_send_callback_f&& cb) { _ws_queue.emplace(client_id, std::move(cb)); } @@ -96,11 +103,15 @@ void wsPost(const ws_on_send_callback_f& cb) { wsPost(0, cb); } +namespace { + template void _wsPostCallbacks(uint32_t client_id, T&& cbs, WsPostponedCallbacks::Mode mode) { _ws_queue.emplace(client_id, std::forward(cbs), mode); } +} // namespace + void wsPostAll(uint32_t client_id, ws_on_send_callback_list_t&& cbs) { _wsPostCallbacks(client_id, std::move(cbs), WsPostponedCallbacks::Mode::All); } @@ -164,6 +175,8 @@ ws_callbacks_t& ws_callbacks_t::onKeyCheck(ws_on_keycheck_callback_f cb) { // WS authentication // ----------------------------------------------------------------------------- +namespace { + constexpr size_t WsMaxClients { WS_MAX_CLIENTS }; WsTicket _ws_tickets[WsMaxClients]; @@ -219,16 +232,22 @@ bool _wsAuth(AsyncWebSocketClient* client) { return false; } +} // namespace + // ----------------------------------------------------------------------------- // Debug // ----------------------------------------------------------------------------- + #if DEBUG_WEB_SUPPORT -constexpr size_t WsDebugMessagesMax = 8; +namespace { +constexpr size_t WsDebugMessagesMax = 8; WsDebug _ws_debug(WsDebugMessagesMax); +} // namespace + void WsDebug::send(bool connected) { if (!connected && _flush) { clear(); @@ -266,6 +285,12 @@ bool wsDebugSend(const char* prefix, const char* message) { #endif +// ----------------------------------------------------------------------------- +// Store indexed key (key0, key1, etc.) from array +// ----------------------------------------------------------------------------- + +namespace { + // Check the existing setting before saving it // TODO: this should know of the default values, somehow? bool _wsStore(const String& key, const String& value) { @@ -276,10 +301,6 @@ bool _wsStore(const String& key, const String& value) { return false; } -// ----------------------------------------------------------------------------- -// Store indexed key (key0, key1, etc.) from array -// ----------------------------------------------------------------------------- - bool _wsStore(const String& prefix, JsonArray& values) { bool changed { false }; @@ -595,11 +616,10 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy } -// TODO: make this generic loop method to queue important ws messages? -// or, if something uses ticker / async ctx to send messages, -// it needs a retry mechanism built into the callback object void _wsHandlePostponedCallbacks(bool connected) { - + // TODO: make this generic loop method to queue important ws messages? + // or, if something uses ticker / async ctx to send messages, + // it needs a retry mechanism built into the callback object if (!connected && !_ws_queue.empty()) { _ws_queue.pop(); return; @@ -661,6 +681,8 @@ void _wsLoop() { #endif } +} // namespace + // ----------------------------------------------------------------------------- // Public API // -----------------------------------------------------------------------------