1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

[BREAKING] wifi: remove pseudo-modes for shutdown, expose ::[resumeFrom]shutdown() (#7956)

* wifi: remove pseudo-modes for shutdown

make shutdown and resumeFromShutdown public
removes extra code from the mode handler and include method description
in the docs

* typo

* dup

* typos

* reference only shutdown() & resumeFromShutdown()

prefer to have some specific function, don't simply chain into sleep
update examples and docs

* safeguard raw sdk config usage
This commit is contained in:
Max Prokhorov
2021-05-15 20:49:35 +03:00
committed by GitHub
parent e9820c1f27
commit b0ece8cac4
7 changed files with 123 additions and 101 deletions

View File

@ -1,5 +1,5 @@
// Demonstrate the use of WiFi.mode(WIFI_SHUTDOWN)/WiFi.mode(WIFI_RESUME)
// Demonstrate the use of WiFi.shutdown() and WiFi.resumeFromShutdown()
// Released to public domain
// Current on WEMOS D1 mini (including: LDO, usbserial chip):
@ -39,7 +39,7 @@ void setup() {
ESP.rtcUserMemoryRead(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&state), sizeof(state));
unsigned long start = millis();
if (!WiFi.mode(WIFI_RESUME, &state)
if (!WiFi.resumeFromShutdown(state)
|| (WiFi.waitForConnectResult(10000) != WL_CONNECTED)) {
Serial.println("Cannot resume WiFi connection, connecting via begin...");
WiFi.persistent(false);
@ -63,7 +63,7 @@ void setup() {
// Here you can do whatever you need to do that needs a WiFi connection.
// ---
WiFi.mode(WIFI_SHUTDOWN, &state);
WiFi.shutdown(state);
ESP.rtcUserMemoryWrite(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&state), sizeof(state));
// ---
@ -77,4 +77,4 @@ void setup() {
void loop() {
// Nothing to do here.
}
}

View File

@ -56,6 +56,8 @@ enableSTA KEYWORD2
enableAP KEYWORD2
forceSleepBegin KEYWORD2
forceSleepWake KEYWORD2
shutdown KEYWORD2
resumeFromShutdown KEYWORD2
#ESP8266WiFi
printDiag KEYWORD2

View File

@ -401,21 +401,10 @@ bool ESP8266WiFiGenericClass::getPersistent(){
* set new mode
* @param m WiFiMode_t
*/
bool ESP8266WiFiGenericClass::mode(WiFiMode_t m, WiFiState* state) {
if (m == WIFI_SHUTDOWN) {
return shutdown(0, state);
}
else if (m == WIFI_RESUME) {
return resumeFromShutdown(state);
}
else if (m & ~(WIFI_STA | WIFI_AP))
bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) {
if (m & ~(WIFI_STA | WIFI_AP)) {
// any other bits than legacy disallowed
return false;
// m is now WIFI_STA, WIFI_AP or WIFI_AP_STA
if (state)
{
DEBUG_WIFI("core: state is useless without SHUTDOWN or RESUME\n");
}
if(_persistent){
@ -719,37 +708,37 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca
esp_schedule(); // break delay in hostByName
}
uint32_t ESP8266WiFiGenericClass::shutdownCRC (const WiFiState* state)
uint32_t ESP8266WiFiGenericClass::shutdownCRC (const WiFiState& state)
{
return state? crc32(&state->state, sizeof(state->state)): 0;
return crc32(&state.state, sizeof(state.state));
}
bool ESP8266WiFiGenericClass::shutdownValidCRC (const WiFiState* state)
bool ESP8266WiFiGenericClass::shutdownValidCRC (const WiFiState& state)
{
return state && (crc32(&state->state, sizeof(state->state)) == state->crc);
return crc32(&state.state, sizeof(state.state)) == state.crc;
}
bool ESP8266WiFiGenericClass::shutdown (uint32 sleepUs, WiFiState* state)
bool ESP8266WiFiGenericClass::shutdown (WiFiState& state, uint32 sleepUs)
{
bool persistent = _persistent;
WiFiMode_t before_off_mode = getMode();
if ((before_off_mode & WIFI_STA) && state)
if (before_off_mode & WIFI_STA)
{
bool ret = wifi_get_ip_info(STATION_IF, &state->state.ip);
bool ret = wifi_get_ip_info(STATION_IF, &state.state.ip);
if (!ret)
{
DEBUG_WIFI("core: error with wifi_get_ip_info(STATION_IF)\n");
return false;
}
memset(state->state.fwconfig.bssid, 0xff, 6);
ret = wifi_station_get_config(&state->state.fwconfig);
memset(state.state.fwconfig.bssid, 0xff, 6);
ret = wifi_station_get_config(&state.state.fwconfig);
if (!ret)
{
DEBUG_WIFI("core: error with wifi_station_get_config\n");
return false;
}
state->state.channel = wifi_get_channel();
state.state.channel = wifi_get_channel();
}
// disable persistence in FW so in case of power failure
@ -766,57 +755,63 @@ bool ESP8266WiFiGenericClass::shutdown (uint32 sleepUs, WiFiState* state)
}
// WiFi is now in force-sleep mode
// finish filling state and process crc
if (state)
state.state.persistent = persistent;
state.state.mode = before_off_mode;
uint8_t i = 0;
for (auto& ntp: state.state.ntp)
{
// finish filling state and process crc
state->state.persistent = persistent;
state->state.mode = before_off_mode;
uint8_t i = 0;
for (auto& ntp: state->state.ntp)
{
ntp = *sntp_getserver(i++);
}
i = 0;
for (auto& dns: state->state.dns)
dns = WiFi.dnsIP(i++);
state->crc = shutdownCRC(state);
DEBUG_WIFI("core: state is saved\n");
ntp = *sntp_getserver(i++);
}
i = 0;
for (auto& dns: state.state.dns)
{
dns = WiFi.dnsIP(i++);
}
state.crc = shutdownCRC(state);
DEBUG_WIFI("core: state is saved\n");
return true;
}
bool ESP8266WiFiGenericClass::resumeFromShutdown (WiFiState* state)
bool ESP8266WiFiGenericClass::shutdown (WiFiState& state) {
return shutdown(state, 0);
}
bool ESP8266WiFiGenericClass::resumeFromShutdown (WiFiState& state)
{
if (wifi_fpm_get_sleep_type() != NONE_SLEEP_T) {
wifi_fpm_do_wakeup();
wifi_fpm_close();
}
if (!state || shutdownCRC(state) != state->crc)
if (shutdownCRC(state) != state.crc)
{
DEBUG_WIFI("core: resume: no state or bad crc\n");
DEBUG_WIFI("core: resume: bad crc\n");
return false;
}
persistent(state->state.persistent);
persistent(state.state.persistent);
if (!mode(state->state.mode))
if (!mode(state.state.mode))
{
DEBUG_WIFI("core: resume: can't set wifi mode to %d\n", state->state.mode);
DEBUG_WIFI("core: resume: can't set wifi mode to %d\n", state.state.mode);
return false;
}
if (state->state.mode & WIFI_STA)
if (state.state.mode & WIFI_STA)
{
IPAddress local(state->state.ip.ip);
IPAddress local(state.state.ip.ip);
if (local)
{
DEBUG_WIFI("core: resume: static address '%s'\n", local.toString().c_str());
WiFi.config(state->state.ip.ip, state->state.ip.gw, state->state.ip.netmask, state->state.dns[0], state->state.dns[1]);
WiFi.config(state.state.ip.ip, state.state.ip.gw, state.state.ip.netmask, state.state.dns[0], state.state.dns[1]);
uint8_t i = 0;
for (const auto& ntp: state->state.ntp)
for (const auto& ntp: state.state.ntp)
{
IPAddress ip(ntp);
if (ip.isSet())
@ -826,10 +821,23 @@ bool ESP8266WiFiGenericClass::resumeFromShutdown (WiFiState* state)
}
}
}
auto beginResult = WiFi.begin((const char*)state->state.fwconfig.ssid,
(const char*)state->state.fwconfig.password,
state->state.channel,
state->state.fwconfig.bssid,
String ssid;
{
const char* ptr = reinterpret_cast<const char*>(state.state.fwconfig.ssid);
ssid.concat(ptr, strnlen(ptr, sizeof(station_config::ssid)));
}
String pass;
{
const char* ptr = reinterpret_cast<const char*>(state.state.fwconfig.password);
pass.concat(ptr, strnlen(ptr, sizeof(station_config::password)));
}
auto beginResult = WiFi.begin(ssid.c_str(),
pass.c_str(),
state.state.channel,
state.state.fwconfig.bssid,
true);
if (beginResult == WL_CONNECT_FAILED)
{
@ -843,14 +851,14 @@ bool ESP8266WiFiGenericClass::resumeFromShutdown (WiFiState* state)
}
}
if (state->state.mode & WIFI_AP)
if (state.state.mode & WIFI_AP)
{
DEBUG_WIFI("core: resume AP mode TODO\n");
return false;
}
// success, invalidate saved state
state->crc++;
state.crc++;
return true;
}

View File

@ -122,7 +122,7 @@ class ESP8266WiFiGenericClass {
static void persistent(bool persistent);
bool mode(WiFiMode_t, WiFiState* state = nullptr);
bool mode(WiFiMode_t);
WiFiMode_t getMode();
bool enableSTA(bool enable);
@ -131,21 +131,23 @@ class ESP8266WiFiGenericClass {
bool forceSleepBegin(uint32 sleepUs = 0);
bool forceSleepWake();
static uint32_t shutdownCRC (const WiFiState* state);
static bool shutdownValidCRC (const WiFiState* state);
// wrappers around mode() and forceSleepBegin/Wake
// - sleepUs is WiFi.forceSleepBegin() parameter, 0 means forever
// - saveState is the user's state to hold configuration on restore
bool shutdown(WiFiState& stateSave);
bool shutdown(WiFiState& stateSave, uint32 sleepUs);
bool resumeFromShutdown(WiFiState& savedState);
static bool shutdownValidCRC (const WiFiState& state);
static void preinitWiFiOff () __attribute__((deprecated("WiFi is off by default at boot, use enableWiFiAtBoot() for legacy behavior")));
protected:
static bool _persistent;
static WiFiMode_t _forceSleepLastMode;
static void _eventCallback(void *event);
static uint32_t shutdownCRC (const WiFiState& state);
// called by WiFi.mode(SHUTDOWN/RESTORE, state)
// - sleepUs is WiFi.forceSleepBegin() parameter, 0 = forever
// - saveState is the user's state to hold configuration on restore
bool shutdown (uint32 sleepUs = 0, WiFiState* stateSave = nullptr);
bool resumeFromShutdown (WiFiState* savedState = nullptr);
static void _eventCallback(void *event);
// ----------------------------------------------------------------------------------------------
// ------------------------------------ Generic Network function --------------------------------

View File

@ -34,8 +34,7 @@
typedef enum WiFiMode
{
WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3,
/* these two pseudo modes are experimental: */ WIFI_SHUTDOWN = 4, WIFI_RESUME = 8
WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3
} WiFiMode_t;
typedef enum WiFiPhyMode