diff --git a/docs/Commands.md b/docs/Commands.md index ec8cc67e..d6d6c6f4 100644 --- a/docs/Commands.md +++ b/docs/Commands.md @@ -46,6 +46,9 @@ The json format is * Set/Get AP channel `[ESP108] json= pwd=` +* Set/Get sta rejoin from setup fallback which can be ON, OFF + `[ESP109] json= pwd=` + * Set/Get radio state which can be WIFI-STA, WIFI-AP, BT, ETH-STA, ETH-AP, OFF `[ESP110] json= pwd=` diff --git a/docs/authentication.md b/docs/authentication.md index 1b24830c..83e780d3 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -81,6 +81,7 @@ Here the scope of right for each authentication level: | ESP106 | No | No | Set | Set | | ESP107 | No | No | Get | Get/Set | | ESP108 | No | No | Get | Get/Set | +| ESP109 | No | No | Get | Get/Set | | ESP110 | No | No | Get | Get/Set | | ESP111 | No | No | Get | Get | | ESP112 | No | No | Get | Get/Set | diff --git a/docs/espXXX.md b/docs/espXXX.md index 70a8d271..dc5d9366 100644 --- a/docs/espXXX.md +++ b/docs/espXXX.md @@ -375,6 +375,46 @@ the admin password if authentication is enabled * `status` status of command, should be `ok` * `data` content of response, here the channel ++++ +archetype = "section" +title = "[ESP109]" +weight = 800 ++++ +Get/Set station rejoin from the setup fallback which can be ON, OFF + +When it is ON and the station connection failed over to the setup access point, ESP3D looks for the station network every 2 minutes (only while no client is connected to the access point) and reconnects when it is visible again. If that attempt fails it goes back to setup mode. `Waiting for STA network` and `STA network found, reconnecting` are sent to all clients, including serial. + +## Input +`[ESP109] json= pwd=` + +* json=no +the output format +can be in JSON or plain text + +* pwd= +the admin password if authentication is enabled + +* mode + * if mode is empty, it will display current mode + * if mode is not empty, it will set the setting mode: `ON` or `OFF` + + +## Output + +- In json format + +```json +{ + "cmd":"109", + "status":"ok", + "data":"OFF" +} +``` + +* `cmd` Id of requested command, should be `109` +* `status` status of command, should be `ok` +* `data` content of response, here the mode + +++ archetype = "section" title = "[ESP110]" diff --git a/esp3d/src/core/commands/ESP0.cpp b/esp3d/src/core/commands/ESP0.cpp index 8f5da15d..f4df62be 100644 --- a/esp3d/src/core/commands/ESP0.cpp +++ b/esp3d/src/core/commands/ESP0.cpp @@ -35,6 +35,8 @@ const char* help[] = { "[ESP106](Password) - set AP password", "[ESP107](IP) - display/set AP IP", "[ESP108](Chanel) - display/set AP chanel", + "[ESP109](State) - display/set sta rejoin from setup fallback which can " + "be ON, OFF", #endif // WIFI_FEATURE #if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE) "[ESP110](State) - display/set radio state which can be BT, WIFI-STA, " @@ -206,7 +208,7 @@ const uint cmdlist[] = { #if defined(WIFI_FEATURE) 102, 103, 104, - 105, 106, 107, 108, + 105, 106, 107, 108, 109, #endif // WIFI_FEATURE #if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE) 110, diff --git a/esp3d/src/core/commands/ESP109.cpp b/esp3d/src/core/commands/ESP109.cpp new file mode 100644 index 00000000..d22d4863 --- /dev/null +++ b/esp3d/src/core/commands/ESP109.cpp @@ -0,0 +1,93 @@ +/* + ESP109.cpp - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + This code is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This code is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with This code; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "../../include/esp3d_config.h" +#if defined(WIFI_FEATURE) +#include "../../modules/authentication/authentication_service.h" +#include "../esp3d_commands.h" +#include "../esp3d_settings.h" + +// Get/Set STA rejoin from the setup fallback which can be ON, OFF +//[ESP109] json= pwd= +#define COMMAND_ID 109 +void ESP3DCommands::ESP109(int cmd_params_pos, ESP3DMessage* msg) { + ESP3DClientType target = msg->origin; + ESP3DRequest requestId = msg->request_id; + (void)requestId; + msg->target = target; + msg->origin = ESP3DClientType::command; + bool hasError = false; + String error_msg = "Invalid parameters"; + String ok_msg = "ok"; + bool json = hasTag(msg, cmd_params_pos, "json"); + String tmpstr; + uint8_t byteValue = (uint8_t)-1; +#if defined(AUTHENTICATION_FEATURE) + if (msg->authentication_level == ESP3DAuthenticationLevel::guest) { + msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated; + dispatchAuthenticationError(msg, COMMAND_ID, json); + return; + } +#endif // AUTHENTICATION_FEATURE + tmpstr = get_clean_param(msg, cmd_params_pos); + if (tmpstr.length() == 0) { + byteValue = ESP3DSettings::readByte(ESP_STA_FALLBACK_RETRY); + if (byteValue == (uint8_t)ESP3DState::off) { + ok_msg = "OFF"; + } else if (byteValue == (uint8_t)ESP3DState::on) { + ok_msg = "ON"; + } else { + ok_msg = "Unknown"; + } + } else { +#if defined(AUTHENTICATION_FEATURE) + if (msg->authentication_level != ESP3DAuthenticationLevel::admin) { + dispatchAuthenticationError(msg, COMMAND_ID, json); + return; + } +#endif // AUTHENTICATION_FEATURE + if (tmpstr == "OFF") { + byteValue = (uint8_t)ESP3DState::off; + } else if (tmpstr == "ON") { + byteValue = (uint8_t)ESP3DState::on; + } else { + byteValue = (uint8_t)-1; // unknow flag so put outof range value + } + esp3d_log("got %s param for a value of %d", tmpstr.c_str(), byteValue); + // isValidByteSetting() accepts any value for on/off settings, so only + // ON and OFF are written + if (byteValue == (uint8_t)ESP3DState::off || + byteValue == (uint8_t)ESP3DState::on) { + if (!ESP3DSettings::writeByte(ESP_STA_FALLBACK_RETRY, byteValue)) { + hasError = true; + error_msg = "Set value failed"; + } + } else { + hasError = true; + error_msg = "Invalid parameter"; + } + } + + if (!dispatchAnswer(msg, COMMAND_ID, json, hasError, + hasError ? error_msg.c_str() : ok_msg.c_str())) { + esp3d_log_e("Error sending response to clients"); + } +} + +#endif // WIFI_FEATURE \ No newline at end of file diff --git a/esp3d/src/core/commands/ESP400.cpp b/esp3d/src/core/commands/ESP400.cpp index a1ff9e66..98e9c5d9 100644 --- a/esp3d/src/core/commands/ESP400.cpp +++ b/esp3d/src/core/commands/ESP400.cpp @@ -300,6 +300,11 @@ void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) { "sta fallback mode", FallbackValues, FallbackLabels, sizeof(FallbackValues) / sizeof(char*), -1, -1, -1, nullptr, true, target, requestId); + // Rejoin the STA network from the setup fallback when it is back + dispatchSetting(json, "network/sta", ESP_STA_FALLBACK_RETRY, + "sta fallback retry", YesNoValues, YesNoLabels, + sizeof(YesNoValues) / sizeof(char*), -1, -1, -1, nullptr, + true, target, requestId); #endif // WIFI_FEATURE #if defined(WIFI_FEATURE) // AP SSID network/ap diff --git a/esp3d/src/core/esp3d_commands.cpp b/esp3d/src/core/esp3d_commands.cpp index bc6bd5a2..9b6dff61 100644 --- a/esp3d/src/core/esp3d_commands.cpp +++ b/esp3d/src/core/esp3d_commands.cpp @@ -437,6 +437,11 @@ void ESP3DCommands::execute_internal_command(int cmd, int cmd_params_pos, case 108: ESP108(cmd_params_pos, msg); break; + // Rejoin STA network from the setup fallback + //[ESP109]pwd= + case 109: + ESP109(cmd_params_pos, msg); + break; #endif // WIFI_FEATURE #if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE) diff --git a/esp3d/src/core/esp3d_commands.h b/esp3d/src/core/esp3d_commands.h index 3925b957..63601734 100644 --- a/esp3d/src/core/esp3d_commands.h +++ b/esp3d/src/core/esp3d_commands.h @@ -61,6 +61,7 @@ class ESP3DCommands { void ESP106(int cmd_params_pos, ESP3DMessage* msg); void ESP107(int cmd_params_pos, ESP3DMessage* msg); void ESP108(int cmd_params_pos, ESP3DMessage* msg); + void ESP109(int cmd_params_pos, ESP3DMessage* msg); #endif // WIFI_FEATURE #if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE) void ESP110(int cmd_params_pos, ESP3DMessage* msg); diff --git a/esp3d/src/core/esp3d_settings.cpp b/esp3d/src/core/esp3d_settings.cpp index 8802da5c..2fc128f8 100644 --- a/esp3d/src/core/esp3d_settings.cpp +++ b/esp3d/src/core/esp3d_settings.cpp @@ -145,6 +145,7 @@ #define DEFAULT_AUTO_NOTIFICATION_STATE "1" #define DEFAULT_SECURE_SERIAL "1" #define DEFAULT_BOOT_RADIO_STATE "1" +#define DEFAULT_STA_FALLBACK_RETRY "0" // default int values #define DEFAULT_ESP_INT "0" @@ -204,6 +205,7 @@ uint16_t ESP3DSettingsData[] = {ESP_RADIO_MODE, ESP_STA_SSID, ESP_BOOT_RADIO_STATE, ESP_STA_FALLBACK_MODE, + ESP_STA_FALLBACK_RETRY, ESP_AP_SSID, ESP_AP_PASSWORD, ESP_STA_IP_VALUE, @@ -284,6 +286,14 @@ bool ESP3DSettings::begin() { if (GetSettingsVersion() == -1) { return false; } +#if defined(WIFI_FEATURE) + // Settings saved by older builds have no value here yet (0xFF) + uint8_t staFallbackRetry = readByte(ESP_STA_FALLBACK_RETRY); + if (staFallbackRetry != (uint8_t)ESP3DState::off && + staFallbackRetry != (uint8_t)ESP3DState::on) { + writeByte(ESP_STA_FALLBACK_RETRY, atoi(DEFAULT_STA_FALLBACK_RETRY)); + } +#endif // WIFI_FEATURE // get target FW ESP3DSettings::GetFirmwareTarget(true); ESP3DSettings::isVerboseBoot(true); @@ -964,6 +974,7 @@ bool ESP3DSettings::isValidByteSetting(uint8_t value, case ESP_SECURE_SERIAL: case ESP_CALIBRATION: case ESP_BOOT_RADIO_STATE: + case ESP_STA_FALLBACK_RETRY: if (value == (uint8_t)ESP3DState::off || value == (uint8_t)ESP3DState::on) { return true; @@ -1179,6 +1190,7 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr( case ESP_SECURE_SERIAL: case ESP_BOOT_RADIO_STATE: case ESP_STA_FALLBACK_MODE: + case ESP_STA_FALLBACK_RETRY: case ESP_ETH_STA_FALLBACK_MODE: case ESP_SERIAL_BRIDGE_ON: case ESP_STA_IP_MODE: @@ -1268,6 +1280,7 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr( case ESP_BOOT_RADIO_STATE: case ESP_ETH_STA_FALLBACK_MODE: case ESP_STA_FALLBACK_MODE: + case ESP_STA_FALLBACK_RETRY: case ESP_SERIAL_BRIDGE_ON: case ESP_ETH_STA_IP_MODE: case ESP_STA_IP_MODE: @@ -1436,6 +1449,9 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr( case ESP_STA_FALLBACK_MODE: setting.default_val = DEFAULT_STA_FALLBACK_MODE; break; + case ESP_STA_FALLBACK_RETRY: + setting.default_val = DEFAULT_STA_FALLBACK_RETRY; + break; case ESP_SERIAL_BRIDGE_ON: setting.default_val = DEFAULT_SERIAL_BRIDGE_ON; break; diff --git a/esp3d/src/include/esp3d_defines.h b/esp3d/src/include/esp3d_defines.h index 152f4dde..0bbbe30f 100644 --- a/esp3d/src/include/esp3d_defines.h +++ b/esp3d/src/include/esp3d_defines.h @@ -141,6 +141,7 @@ typedef uint ESP3DSettingIndex; #define ESP_ETH_STA_GATEWAY_VALUE 1245 // 4 bytes xxx.xxx.xxx.xxx #define ESP_ETH_STA_DNS_VALUE 1249 // 4 bytes xxx.xxx.xxx.xxx #define ESP_USB_SERIAL_BAUD_RATE 1253 // 4 bytes= int +#define ESP_STA_FALLBACK_RETRY 1257 // 1 byte = flag // Hidden password #define HIDDEN_PASSWORD "********" diff --git a/esp3d/src/modules/wifi/wificonfig.cpp b/esp3d/src/modules/wifi/wificonfig.cpp index 867dc90c..76ff5b63 100644 --- a/esp3d/src/modules/wifi/wificonfig.cpp +++ b/esp3d/src/modules/wifi/wificonfig.cpp @@ -39,8 +39,16 @@ esp_netif_t *get_esp_interface_netif(esp_interface_t interface); const uint8_t DEFAULT_AP_MASK_VALUE[] = {255, 255, 255, 0}; +// How often the setup fallback looks for the STA network again +#ifndef STA_FALLBACK_RETRY_INTERVAL +#define STA_FALLBACK_RETRY_INTERVAL 120000 +#endif // STA_FALLBACK_RETRY_INTERVAL + IPAddress WiFiConfig::_ap_gateway; IPAddress WiFiConfig::_ap_subnet; +bool WiFiConfig::_inStaFallback = false; +bool WiFiConfig::_retryScanRunning = false; +uint32_t WiFiConfig::_lastRetry = 0; const char* WiFiConfig::hostname() { static String tmp; @@ -310,6 +318,8 @@ bool WiFiConfig::started() { return (WiFi.getMode() != WIFI_OFF); } bool WiFiConfig::begin(int8_t& espMode) { bool res = false; end(); + _inStaFallback = false; + _retryScanRunning = false; esp3d_log("Starting Wifi Config"); if (ESP3DSettings::isVerboseBoot()) { esp3d_commands.dispatch("Starting WiFi", ESP3DClientType::all_clients, @@ -342,6 +352,16 @@ bool WiFiConfig::begin(int8_t& espMode) { } esp3d_log("Starting AP mode in setup mode"); res = StartAP(true); + _inStaFallback = res; + _lastRetry = millis(); + if (res && ESP3DSettings::readByte(ESP_STA_FALLBACK_RETRY) == + (uint8_t)ESP3DState::on) { + esp3d_commands.dispatch("Waiting for STA network", + ESP3DClientType::all_clients, no_id, + ESP3DMessageType::unique, + ESP3DClientType::system, + ESP3DAuthenticationLevel::admin); + } } else { // let setup to handle the change if (ESP3DSettings::isVerboseBoot()) { @@ -380,6 +400,12 @@ void WiFiConfig::end() { */ void WiFiConfig::handle() { + if (_inStaFallback) { + handleFallbackRetry(); + if (_retryScanRunning || !_inStaFallback) { + return; + } + } // to avoid mixed mode if (WiFi.getMode() == WIFI_AP_STA) { if (WiFi.scanComplete() != WIFI_SCAN_RUNNING) { @@ -388,6 +414,57 @@ void WiFiConfig::handle() { } } +/** + * While the setup AP runs as STA fallback, look for the STA network now and + * then and restart the network when it is visible again. Only when enabled, + * and only while no client uses the setup AP, because scanning changes + * channels and would disturb that client. + */ +void WiFiConfig::handleFallbackRetry() { + if (_retryScanRunning) { + int16_t count = WiFi.scanComplete(); + if (count == WIFI_SCAN_RUNNING) { + return; + } + _retryScanRunning = false; + _lastRetry = millis(); + bool found = false; + if (count > 0) { + String SSID = ESP3DSettings::readString(ESP_STA_SSID); + for (int16_t i = 0; i < count && !found; i++) { + found = (WiFi.SSID(i) == SSID); + } + } + WiFi.scanDelete(); + if (found) { + esp3d_log("STA network is back, leaving setup mode"); + esp3d_commands.dispatch("STA network found, reconnecting", + ESP3DClientType::all_clients, no_id, + ESP3DMessageType::unique, ESP3DClientType::system, + ESP3DAuthenticationLevel::admin); + // Falls back to setup mode again if the connection fails + NetConfig::begin(); + } + return; + } + if (ESP3DSettings::readByte(ESP_STA_FALLBACK_RETRY) != + (uint8_t)ESP3DState::on || + ESP3DSettings::readByte(ESP_RADIO_MODE) != ESP_WIFI_STA) { + return; + } + if (millis() - _lastRetry < STA_FALLBACK_RETRY_INTERVAL) { + return; + } + _lastRetry = millis(); + if (WiFi.softAPgetStationNum() > 0) { + return; + } + esp3d_log("Looking for the STA network"); + if (WiFi.scanNetworks(true) == WIFI_SCAN_RUNNING) { + _retryScanRunning = true; + } +} + const char* WiFiConfig::getSleepModeString() { #ifdef ARDUINO_ARCH_ESP32 if (WiFi.getSleep()) { diff --git a/esp3d/src/modules/wifi/wificonfig.h b/esp3d/src/modules/wifi/wificonfig.h index f5b0eb18..bd5e6b26 100644 --- a/esp3d/src/modules/wifi/wificonfig.h +++ b/esp3d/src/modules/wifi/wificonfig.h @@ -78,8 +78,12 @@ class WiFiConfig { private: static bool ConnectSTA2AP(); + static void handleFallbackRetry(); static IPAddress _ap_gateway; static IPAddress _ap_subnet; + static bool _inStaFallback; + static bool _retryScanRunning; + static uint32_t _lastRetry; }; #endif //_WIFI_CONFIG_H