From aa2476e4a72e2fd0e9d2e7a16857b567305f43d0 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 7 Apr 2023 18:38:17 +0300 Subject: [PATCH] wifi: system check just after booting helper would block 2nd action, wifi is not enabled yet fixing behaviour introduced in 28f3b7da8 also make boot action optional --- code/espurna/config/general.h | 3 ++ code/espurna/config/types.h | 4 ++ code/espurna/wifi.cpp | 71 ++++++++++++++++++++++++----------- code/espurna/wifi.h | 9 ++++- 4 files changed, 64 insertions(+), 23 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 01e4b269..e0e2bddb 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -636,6 +636,9 @@ #define WIFI_OUTPUT_POWER_DBM 20.0f #endif +#ifndef WIFI_BOOT_MODE +#define WIFI_BOOT_MODE WIFI_ENABLED +#endif // ----------------------------------------------------------------------------- // WEB diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index 8ead2eb5..c8adb529 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -261,6 +261,7 @@ //------------------------------------------------------------------------------ // PWM //------------------------------------------------------------------------------ + #define PWM_PROVIDER_NONE 0 #define PWM_PROVIDER_GENERIC 1 #define PWM_PROVIDER_ARDUINO 2 @@ -434,3 +435,6 @@ #define WIFI_SLEEP_MODE_NONE NONE_SLEEP_T #define WIFI_SLEEP_MODE_MODEM MODEM_SLEEP_T #define WIFI_SLEEP_MODE_LIGHT LIGHT_SLEEP_T + +#define WIFI_DISABLED BootMode::Disabled +#define WIFI_ENABLED BootMode::Enabled diff --git a/code/espurna/wifi.cpp b/code/espurna/wifi.cpp index be19c69f..bd03a7aa 100644 --- a/code/espurna/wifi.cpp +++ b/code/espurna/wifi.cpp @@ -75,19 +75,30 @@ constexpr sleep_type_t sleep() { return compat::arduino_sleep(WIFI_SLEEP_MODE); } +constexpr BootMode bootMode() { + return WIFI_BOOT_MODE; +} + } // namespace build -namespace ap { namespace settings { namespace options { PROGMEM_STRING(Disabled, "off"); PROGMEM_STRING(Enabled, "on"); + +} // namespace options +} // namespace settings + +namespace ap { +namespace settings { +namespace options { + PROGMEM_STRING(Fallback, "fallback"); static constexpr espurna::settings::options::Enumeration ApModeOptions[] PROGMEM { - {ApMode::Disabled, Disabled}, - {ApMode::Enabled, Enabled}, + {ApMode::Disabled, wifi::settings::options::Disabled}, + {ApMode::Enabled, wifi::settings::options::Enabled}, {ApMode::Fallback, Fallback}, }; @@ -117,6 +128,17 @@ static constexpr espurna::settings::options::Enumeration SleepType namespace settings { namespace internal { +template<> +wifi::BootMode convert(const String& value) { + return convert(value) + ? wifi::BootMode::Enabled + : wifi::BootMode::Disabled; +} + +String serialize(wifi::BootMode mode) { + return serialize(mode == wifi::BootMode::Enabled); +} + template<> wifi::StaMode convert(const String& value) { return convert(value) @@ -234,14 +256,15 @@ enum class ScanError { }; enum class Action { - StationConnect, - StationContinueConnect, - StationTryConnectBetter, - StationDisconnect, AccessPointFallback, AccessPointFallbackCheck, AccessPointStart, AccessPointStop, + Boot, + StationConnect, + StationContinueConnect, + StationDisconnect, + StationTryConnectBetter, TurnOff, TurnOn, }; @@ -357,6 +380,7 @@ void action(Action value) { break; case Action::TurnOff: case Action::TurnOn: + case Action::Boot: break; } @@ -373,10 +397,6 @@ State handle_action(State state, T&& handler) { return state; } -ActionsQueue& actions() { - return internal::actions; -} - namespace debug { String error(ScanError error) { @@ -484,6 +504,7 @@ namespace keys { PROGMEM_STRING(TxPower, "wifiTxPwr"); PROGMEM_STRING(Sleep, "wifiSleep"); +PROGMEM_STRING(Boot, "wifiBoot"); } // namespace keys @@ -495,6 +516,10 @@ sleep_type_t sleep() { return getSetting(keys::Sleep, build::sleep()); } +BootMode bootMode() { + return getSetting(keys::Boot, build::bootMode()); +} + namespace query { namespace internal { @@ -510,6 +535,7 @@ String NAME (size_t id) {\ EXACT_VALUE(sleep, settings::sleep) EXACT_VALUE(txPower, settings::txPower) +EXACT_VALUE(bootMode, settings::bootMode) } // namespace internal } // namespace query @@ -2195,7 +2221,7 @@ void configure() { namespace settings { namespace query { -static constexpr std::array Settings PROGMEM { +static constexpr std::array Settings PROGMEM { {{ap::settings::keys::Ssid, ap::settings::ssid}, {ap::settings::keys::Passphrase, ap::settings::passphrase}, {ap::settings::keys::Captive, ap::settings::query::internal::captive}, @@ -2205,7 +2231,9 @@ static constexpr std::array Settings PROG {sta::scan::settings::keys::Enabled, sta::scan::settings::query::enabled}, {sta::scan::periodic::settings::keys::Threshold, sta::scan::periodic::settings::query::threshold}, {settings::keys::TxPower, query::internal::txPower}, - {settings::keys::Sleep, query::internal::sleep}} + {settings::keys::Sleep, query::internal::sleep}, + {settings::keys::Boot, query::internal::bootMode}, + } }; // indexed settings for 'sta' connections @@ -2760,9 +2788,16 @@ State handle_action(State state, Action action) { } break; + case Action::Boot: case Action::TurnOn: if (!wifi::enabled()) { wifi::enable(); +#if SYSTEM_CHECK_ENABLED + if ((action == Action::Boot) && !systemCheck()) { + wifi::action(Action::AccessPointStart); + break; + } +#endif settings::configure(); } break; @@ -2979,15 +3014,9 @@ void setup() { migrateVersion(settings::migrate); settings::query::setup(); - action(Action::TurnOn); - -#if SYSTEM_CHECK_ENABLED - if (!systemCheck()) { - actions() = wifi::ActionsQueue{}; - action(Action::TurnOn); - action(Action::AccessPointStart); + if (BootMode::Enabled == settings::bootMode()) { + action(Action::Boot); } -#endif #if DEBUG_SUPPORT wifiRegister([](Event event) { diff --git a/code/espurna/wifi.h b/code/espurna/wifi.h index 3c41244f..d537391d 100644 --- a/code/espurna/wifi.h +++ b/code/espurna/wifi.h @@ -56,15 +56,20 @@ enum class Event { using EventCallback = void(*)(Event event); +enum class BootMode { + Disabled, + Enabled, +}; + enum class StaMode { Disabled, - Enabled + Enabled, }; enum class ApMode { Disabled, Enabled, - Fallback + Fallback, }; } // namespace wifi