Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ The json format is
* Set/Get AP channel
`[ESP108]<channel> json=<no> pwd=<admin password>`

* Set/Get sta rejoin from setup fallback which can be ON, OFF
`[ESP109]<state> json=<no> pwd=<admin password>`

* Set/Get radio state which can be WIFI-STA, WIFI-AP, BT, ETH-STA, ETH-AP, OFF
`[ESP110]<state> json=<no> pwd=<admin password>`

Expand Down
1 change: 1 addition & 0 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
40 changes: 40 additions & 0 deletions docs/espXXX.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]<mode> json=<no> pwd=<admin/user password>`

* json=no
the output format
can be in JSON or plain text

* pwd=<admin password>
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]"
Expand Down
4 changes: 3 additions & 1 deletion esp3d/src/core/commands/ESP0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "
Expand Down Expand Up @@ -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,
Expand Down
93 changes: 93 additions & 0 deletions esp3d/src/core/commands/ESP109.cpp
Original file line number Diff line number Diff line change
@@ -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]<state> json=<no> pwd=<user/admin password>
#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
5 changes: 5 additions & 0 deletions esp3d/src/core/commands/ESP400.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions esp3d/src/core/esp3d_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]<state>pwd=<admin password>
case 109:
ESP109(cmd_params_pos, msg);
break;
#endif // WIFI_FEATURE

#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
Expand Down
1 change: 1 addition & 0 deletions esp3d/src/core/esp3d_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions esp3d/src/core/esp3d_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions esp3d/src/include/esp3d_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 "********"
Expand Down
77 changes: 77 additions & 0 deletions esp3d/src/modules/wifi/wificonfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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()) {
Expand Down
Loading