Skip to content

Commit

Permalink
wifi: turned off on boot
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Mar 9, 2023
1 parent 0ab8c85 commit 28f3b7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 59 deletions.
75 changes: 16 additions & 59 deletions code/espurna/wifi.cpp
Expand Up @@ -256,7 +256,7 @@ namespace internal {
// current task and is free to take up another one. Allow to toggle OFF for the whole module,
// discarding any actions involving an active WiFi. Default is ON

bool enabled { true };
bool enabled { false };
ActionsQueue actions;

} // namespace internal
Expand Down Expand Up @@ -455,67 +455,11 @@ String NAME (size_t id) {\

EXACT_VALUE(sleep, settings::sleep)
EXACT_VALUE(txPower, settings::txPower)
EXACT_VALUE(forcedSleep, settings::forcedSleep)
EXACT_VALUE(forcedSleepPin, settings::forcedSleepPin)
EXACT_VALUE(forcedSleepLevel, settings::forcedSleepLevel)

} // namespace internal
} // namespace query
} // namespace settings

// ::forceSleepBegin() remembers the previous mode and ::forceSleepWake() calls station connect when it has STA in it :/
// while we *do* set opmode to 0 to avoid this uncertainty, preper to call wake through SDK instead of the Arduino wrapper
//
// 0xFFFFFFF is a magic number per the NONOS API reference, 3.7.5 wifi_fpm_do_sleep:
// > If sleep_time_in_us is 0xFFFFFFF, the ESP8266 will sleep till be woke up as below:
// > • If wifi_fpm_set_sleep_type is set to be LIGHT_SLEEP_T, ESP8266 can wake up by GPIO.
// > • If wifi_fpm_set_sleep_type is set to be MODEM_SLEEP_T, ESP8266 can wake up by wifi_fpm_do_wakeup.
//
// In our case, both sleep modes are indefinite when OFF action is executed.
//
// TODO(esp8266): Wake-up GPIO pin is set to INPUT, should it be PULLUP when target level is LOW?
// Wake-up GPIO pin is *not registered* through usual means, so any overlaps needs to be handled manually.
//
// TODO(esp32): Null mode turns off radio, no need for MODEM sleep

bool sleep(sleep_type_t type) {
if (!enabled() && (opmode() == OpmodeNull)) {
if (type == LIGHT_SLEEP_T) {
const auto pin = settings::forcedSleepPin();
if (pin == GPIO_NONE) {
return false;
}

pinMode(pin, INPUT);
gpio_pin_wakeup_enable(pin, settings::forcedSleepLevel());
}

wifi_fpm_set_sleep_type(type);
yield();
wifi_fpm_open();
yield();

if (0 == wifi_fpm_do_sleep(0xFFFFFFF)) {
delay(10);
return true;
}
}

return false;
}

bool wakeup() {
if (wifi_fpm_get_sleep_type() != NONE_SLEEP_T) {
wifi_fpm_do_wakeup();
wifi_fpm_close();
delay(10);
return true;
}

return false;
}


// We are guaranteed to have '\0' when <32 b/c the SDK zeroes out the data
// But, these are byte arrays, not C strings. When ssid_len is available, use it.
// When not, we are still expecting the <32 arrays to have '\0' at the end and we manually
Expand Down Expand Up @@ -2739,7 +2683,6 @@ State handleAction(State& state, Action action) {
case Action::TurnOn:
if (!wifi::enabled()) {
wifi::enable();
wifi::wakeup();
wifi::settings::configure();
}
break;
Expand Down Expand Up @@ -2957,12 +2900,14 @@ void setup() {
internal::init();

migrateVersion(settings::migrate);
settings::configure();
settings::query::setup();

action(wifi::Action::TurnOn);

#if SYSTEM_CHECK_ENABLED
if (!systemCheck()) {
actions() = wifi::ActionsQueue{};
action(wifi::Action::TurnOn);
action(wifi::Action::AccessPointStart);
}
#endif
Expand Down Expand Up @@ -3042,6 +2987,18 @@ void wifiStartAp() {
espurna::wifi::Action::AccessPointStart);
}

bool wifiDisabled() {
return espurna::wifi::opmode()
== espurna::wifi::OpmodeNull;
}

void wifiDisable() {
espurna::wifi::ap::fallback::remove();
espurna::wifi::sta::scan::periodic::stop();
espurna::wifi::ensure_opmode(
espurna::wifi::OpmodeNull);
}

void wifiTurnOff() {
espurna::wifi::action(
espurna::wifi::Action::TurnOff);
Expand Down
3 changes: 3 additions & 0 deletions code/espurna/wifi.h
Expand Up @@ -97,6 +97,9 @@ void wifiToggleSta();
void wifiDisconnect();

// Toggle WiFi modem
bool wifiDisabled();
void wifiDisable();

void wifiTurnOff();
void wifiTurnOn();

Expand Down

0 comments on commit 28f3b7d

Please sign in to comment.