Skip to content

Commit

Permalink
wifi: system check just after booting
Browse files Browse the repository at this point in the history
helper would block 2nd action, wifi is not enabled yet
fixing behaviour introduced in 28f3b7d

also make boot action optional
  • Loading branch information
mcspr committed Apr 8, 2023
1 parent add82d3 commit aa2476e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
3 changes: 3 additions & 0 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,9 @@
#define WIFI_OUTPUT_POWER_DBM 20.0f
#endif

#ifndef WIFI_BOOT_MODE
#define WIFI_BOOT_MODE WIFI_ENABLED
#endif

// -----------------------------------------------------------------------------
// WEB
Expand Down
4 changes: 4 additions & 0 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
//------------------------------------------------------------------------------
// PWM
//------------------------------------------------------------------------------

#define PWM_PROVIDER_NONE 0
#define PWM_PROVIDER_GENERIC 1
#define PWM_PROVIDER_ARDUINO 2
Expand Down Expand Up @@ -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
71 changes: 50 additions & 21 deletions code/espurna/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApMode> ApModeOptions[] PROGMEM {
{ApMode::Disabled, Disabled},
{ApMode::Enabled, Enabled},
{ApMode::Disabled, wifi::settings::options::Disabled},
{ApMode::Enabled, wifi::settings::options::Enabled},
{ApMode::Fallback, Fallback},
};

Expand Down Expand Up @@ -117,6 +128,17 @@ static constexpr espurna::settings::options::Enumeration<sleep_type_t> SleepType
namespace settings {
namespace internal {

template<>
wifi::BootMode convert(const String& value) {
return convert<bool>(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<bool>(value)
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -357,6 +380,7 @@ void action(Action value) {
break;
case Action::TurnOff:
case Action::TurnOn:
case Action::Boot:
break;
}

Expand All @@ -373,10 +397,6 @@ State handle_action(State state, T&& handler) {
return state;
}

ActionsQueue& actions() {
return internal::actions;
}

namespace debug {

String error(ScanError error) {
Expand Down Expand Up @@ -484,6 +504,7 @@ namespace keys {

PROGMEM_STRING(TxPower, "wifiTxPwr");
PROGMEM_STRING(Sleep, "wifiSleep");
PROGMEM_STRING(Boot, "wifiBoot");

} // namespace keys

Expand All @@ -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 {

Expand All @@ -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
Expand Down Expand Up @@ -2195,7 +2221,7 @@ void configure() {
namespace settings {
namespace query {

static constexpr std::array<espurna::settings::query::Setting, 10> Settings PROGMEM {
static constexpr std::array<espurna::settings::query::Setting, 11> 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},
Expand All @@ -2205,7 +2231,9 @@ static constexpr std::array<espurna::settings::query::Setting, 10> 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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions code/espurna/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa2476e

Please sign in to comment.