Skip to content
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

Merged
merged 6 commits into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 35 additions & 10 deletions app/modules/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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));
Copy link
Member

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.

Copy link
Contributor Author

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).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah -- it looks OK.

if(strlen(config[i].password) >= 0) /* WPA = min 8, WEP = min 5 ASCII characters for a 40-bit key */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's 5 for the 40-bit WEP, then shouldn't 5 be the new minimum?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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);
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 )
{
Expand All @@ -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" );
Expand All @@ -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" );
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 ) },
Expand Down
21 changes: 20 additions & 1 deletion docs/en/modules/wifi.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useful for factory-reset scenarios

The function node.restore() should be used to ensure complete erasure of WiFi configuration, since it completely overwrites the system parameter sectors that store WiFi configuration and other configuration information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole time, I never knew about node.restore().

Reset default settings of following APIs: wifi_station_set_auto_connect, wifi_set_phy_mode, wifi_softap_set_config related, wifi_station_set_config related, wifi_set_opmode, and APs’ information recorded by #define AP_CACHE.


#### 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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down