-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/wifi clear config #1761
Changes from 2 commits
d7339e8
1fd6d38
7447e9c
29d08dd
d61bd67
6f7c774
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -478,7 +478,7 @@ static int wifi_setip( lua_State* L, uint8_t mode ) | |
return 1; | ||
} | ||
|
||
// Lua: wifi.sta.getaplist | ||
// Lua: wifi.sta.getapinfo | ||
static int wifi_station_get_ap_info4lua( lua_State* L ) | ||
{ | ||
struct station_config config[5]; | ||
|
@@ -506,8 +506,8 @@ static int wifi_station_get_ap_info4lua( lua_State* L ) | |
c_sprintf(debug_temp, " %-6d %-32s ", i, temp); | ||
#endif | ||
|
||
memset(temp, 0, sizeof(temp)); | ||
if(strlen(config[i].password) >= 8) | ||
memset(temp, 0, sizeof(temp)); | ||
if(strlen(config[i].password) >= 0) /* WPA = min 8, WEP = min 5 ASCII characters for a 40-bit key */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #1706. tl;dr: 5 seems to be the smallest that would ever be used, but the SDK doesn't enforce a minimum. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect that this ought to be >= 1. I can't think of any way that strlen could return < 0. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, strlen will never be <0. Looking at this now after having cooled down from when I made the change, I think >0 is what is appropriate for this code that is copying the passwords only if they exist. The question becomes, should "pwd" be included the Lua table even if there is no password for the entry? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd leave out the pwd field if there is no password. |
||
{ | ||
memcpy(temp, config[i].password, sizeof(config[i].password)); | ||
lua_pushstring(L, temp); | ||
|
@@ -517,7 +517,7 @@ static int wifi_station_get_ap_info4lua( lua_State* L ) | |
c_sprintf(debug_temp + strlen(debug_temp), "%-64s ", temp); | ||
#endif | ||
|
||
memset(temp, 0, sizeof(temp)); | ||
memset(temp, 0, sizeof(temp)); | ||
if (config[i].bssid_set) | ||
{ | ||
c_sprintf(temp, MACSTR, MAC2STR(config[i].bssid)); | ||
|
@@ -607,7 +607,7 @@ static int wifi_station_getconfig( lua_State* L, bool get_flash_cfg) | |
lua_pushstring(L, temp); | ||
lua_setfield(L, -2, "ssid"); | ||
|
||
if(strlen(sta_conf.password) >= 8) | ||
if(strlen(sta_conf.password) >= 0) /* WPA = min 8, WEP = min 5 ASCII characters for a 40-bit key */ | ||
{ | ||
memset(temp, 0, sizeof(temp)); | ||
memcpy(temp, sta_conf.password, sizeof(sta_conf.password)); | ||
|
@@ -652,6 +652,30 @@ static int wifi_station_getconfig_default(lua_State *L) | |
return wifi_station_getconfig(L, true); | ||
} | ||
|
||
// Lua: wifi.sta.clearconfig() | ||
static int wifi_station_clear_config ( lua_State* L ) | ||
{ | ||
struct station_config sta_conf; | ||
bool auto_connect=true; | ||
bool save_to_flash=true; | ||
|
||
memset(sta_conf.ssid, 0, sizeof(sta_conf.ssid)); | ||
memset(sta_conf.password, 0, sizeof(sta_conf.password)); | ||
memset(sta_conf.bssid, 0, sizeof(sta_conf.bssid)); | ||
sta_conf.bssid_set=0; | ||
|
||
wifi_station_disconnect(); | ||
|
||
bool config_success; | ||
if(save_to_flash) config_success = wifi_station_set_config(&sta_conf); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please format so that else lines up with if. The clauses should be on lines by themselves. I would prefer to use { and } in each case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was existing code - not changed for this PR. I'm all for applying the Boy Scout Rule, but think it makes it harder for reviewers to see what exactly is new. |
||
else config_success = wifi_station_set_config_current(&sta_conf); | ||
|
||
wifi_station_set_auto_connect((uint8)0); | ||
|
||
lua_pushboolean(L, config_success); | ||
return 1; | ||
} | ||
|
||
// Lua: wifi.sta.config() | ||
static int wifi_station_config( lua_State* L ) | ||
{ | ||
|
@@ -673,7 +697,7 @@ static int wifi_station_config( lua_State* L ) | |
if( lua_isstring(L, -1) ) | ||
{ | ||
const char *ssid = luaL_checklstring( L, -1, &sl ); | ||
luaL_argcheck(L, ((sl>=1 && sl<=sizeof(sta_conf.ssid)) ), 1, "ssid: length:1-32"); | ||
luaL_argcheck(L, ((sl>=0 && sl<=sizeof(sta_conf.ssid)) ), 1, "ssid: length:0-32"); /* Zero-length SSID is valid as a way to clear config */ | ||
memcpy(sta_conf.ssid, ssid, sl); | ||
} | ||
else return luaL_argerror( L, 1, "ssid:not string" ); | ||
|
@@ -687,7 +711,7 @@ static int wifi_station_config( lua_State* L ) | |
if( lua_isstring(L, -1) ) | ||
{ | ||
const char *pwd = luaL_checklstring( L, -1, &pl ); | ||
luaL_argcheck(L, ((pl>=8 && pl<=sizeof(sta_conf.password)) ), 1, "pwd: length:8-64"); | ||
luaL_argcheck(L, ((pl>=0 && pl<=sizeof(sta_conf.password)) ), 1, "pwd: length:0-64"); /* WPA = min 8, WEP = min 5 ASCII characters for a 40-bit key */ | ||
memcpy(sta_conf.password, pwd, pl); | ||
} | ||
else return luaL_argerror( L, 1, "pwd:not string" ); | ||
|
@@ -729,15 +753,15 @@ static int wifi_station_config( lua_State* L ) | |
lua_pop(L, 1); | ||
|
||
} | ||
else //to be depreciated | ||
else //to be deprecated | ||
{ | ||
const char *ssid = luaL_checklstring( L, 1, &sl ); | ||
luaL_argcheck(L, ((sl>=1 && sl<sizeof(sta_conf.ssid)) ), 1, "length:1-32"); | ||
luaL_argcheck(L, (sl>=0 && sl<sizeof(sta_conf.ssid)), 1, "length:0-32"); /* Zero-length SSID is valid as a way to clear config */ | ||
|
||
memcpy(sta_conf.ssid, ssid, sl); | ||
|
||
const char *password = luaL_checklstring( L, 2, &pl ); | ||
luaL_argcheck(L, (pl==0||(pl>=8 && pl<=sizeof(sta_conf.password)) ), 2, "length:0 or 8-64"); | ||
luaL_argcheck(L, (pl>=0 && pl<=sizeof(sta_conf.password)), 2, "length:0-64"); /* WPA = min 8, WEP = min 5 ASCII characters for a 40-bit key */ | ||
|
||
memcpy(sta_conf.password, password, pl); | ||
|
||
|
@@ -1433,6 +1457,7 @@ static int wifi_ap_dhcp_stop( lua_State* L ) | |
static const LUA_REG_TYPE wifi_station_map[] = { | ||
{ LSTRKEY( "autoconnect" ), LFUNCVAL( wifi_station_setauto ) }, | ||
{ LSTRKEY( "changeap" ), LFUNCVAL( wifi_station_change_ap ) }, | ||
{ LSTRKEY( "clearconfig"), LFUNCVAL( wifi_station_clear_config ) }, | ||
{ LSTRKEY( "config" ), LFUNCVAL( wifi_station_config ) }, | ||
{ LSTRKEY( "connect" ), LFUNCVAL( wifi_station_connect4lua ) }, | ||
{ LSTRKEY( "disconnect" ), LFUNCVAL( wifi_station_disconnect4lua ) }, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,6 +270,24 @@ wifi.sta.changeap(4) | |
- [`wifi.sta.getapinfo()`](#wifistagetapinfo) | ||
- [`wifi.sta.getapindex()`](#wifistagetapindex) | ||
|
||
## wifi.sta.clearconfig() | ||
|
||
Clears the currently saved WiFi station configuration. This will erase the saved configuration from the flash (useful for | ||
factory-reset scenarios, or to prepare for [End-User Setup](enduser-setup) so that the SoftAP can lock onto a single channel) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole time, I never knew about
|
||
|
||
#### Syntax | ||
`wifi.sta.clearconfig()` | ||
|
||
#### Parameters | ||
none | ||
|
||
#### Returns | ||
- `true` Success | ||
- `false` Failure | ||
|
||
#### See also | ||
- [`wifi.sta.config()`](#wifistaconfig) | ||
|
||
## wifi.sta.config() | ||
|
||
Sets the WiFi station configuration. | ||
|
@@ -280,7 +298,7 @@ Sets the WiFi station configuration. | |
#### Parameters | ||
- `station_config` table containing configuration data for station | ||
- `ssid` string which is less than 32 bytes. | ||
- `pwd` string which is 8-64 or 0 bytes. Empty string indicates an open WiFi access point. | ||
- `pwd` string which is 0-64. Empty string indicates an open WiFi access point. _Note: WPA requires a minimum of 8-characters, but the ESP8266 can also connect to a WEP access point (a 40-bit WEP key can be provided as its corresponding 5-character ASCII string)._ | ||
- `auto` defaults to true | ||
- `true` to enable auto connect and connect to access point, hence with `auto=true` there's no need to call [`wifi.sta.connect()`](#wifistaconnect) | ||
- `false` to disable auto connect and remain disconnected from access point | ||
|
@@ -332,6 +350,7 @@ wifi.sta.config(station_cfg) | |
``` | ||
|
||
#### See also | ||
- [`wifi.sta.clearconfig()`](#wifistaclearconfig) | ||
- [`wifi.sta.connect()`](#wifistaconnect) | ||
- [`wifi.sta.disconnect()`](#wifistadisconnect) | ||
- [`wifi.sta.apinfo()`](#wifistaapinfo) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wonder -- is the size of the ssid field the same as the password field? If not, then trouble is nearby. Would prefer to eliminate the temp variable and just do a pushlstring of the correct length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the SDK docs, ssid=32, password=64. Looks like they're defining temp's length here as the max password length, and zeroing out before each write (so it is big enough to hold null-terminated strings for both password and ssid).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah -- it looks OK.