Commit 73773fd8 authored by dnc40085's avatar dnc40085 Committed by Marcel Stör
Browse files

Update to the wifi module (#1497)

* Removed inline documentation for several functions and update comments
Since documentation is now part of the repository, the inline
documentation just adds to the already huge wifi.c

* Wifi module: add new functionality, update documentation

Functions Added:
wifi.getdefaultmode(): returns default wifi opmode
wifi.sta.apchange(): select alternate cached AP
wifi.sta.apinfo(): get cached AP list 
wifi.sta.aplimit(): set cached AP limit
wifi.sta.getapindex(): get index of currently configured AP
wifi.sta.getdefaultconfig(): get default station configuration
wifi.ap.getdefaultconfig(): get default AP configuration

functions modified:
wifi.setmode: saving mode to flash is now optional
wifi.sta.config: now accepts table as an argument and save config to
flash is now optional
wifi.sta.getconfig: added option to return table
wifi.ap.config: save config to flash is now optional
wifi.ap.getconfig: added option to return table

Documentation changes:
- Modified documentation to reflect above changes
- Removed unnecessary inline documentation from `wifi.c` 
- Updated documentation for `wifi.sta.disconnect`to address issue #1480 
- Fixed inaccurate documentation for function `wifi.sleeptype`
- Added more details to `wifi.nullmodesleep()`

* Move function `wifi.sleeptype()` to `wifi.sta.sleeptype()`

* Fixed problem where wifi.x.getconfig() returned invalid strings when
ssid or password were set to maximum length.

* fix error in documentation for `wifi.sta.getapindex`

* Renamed some wifi functions
wifi.sta.apinfo -> getapinfo
wifi.sta.aplimit -> setaplimit 
wifi.sta.apchange -> changeap

also organized the wifi_station_map array
parent 77b2e85d
// Module for interfacing with WIFI // Module for interfacing with WIFI
// FIXME: sprintf->snprintf everywhere.
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
...@@ -76,12 +78,8 @@ static void wifi_smart_succeed_cb(sc_status status, void *pdata){ ...@@ -76,12 +78,8 @@ static void wifi_smart_succeed_cb(sc_status status, void *pdata){
#endif // WIFI_SMART_ENABLE #endif // WIFI_SMART_ENABLE
static int wifi_scan_succeed = LUA_NOREF; static int wifi_scan_succeed = LUA_NOREF;
/**
* @brief Wifi ap scan over callback to display. // callback for wifi_station_listap
* @param arg: contain the aps information
* @param status: scan over status
* @retval None
*/
static void wifi_scan_done(void *arg, STATUS status) static void wifi_scan_done(void *arg, STATUS status)
{ {
lua_State* L = lua_getstate(); lua_State* L = lua_getstate();
...@@ -215,23 +213,26 @@ static int wifi_exit_smart( lua_State* L ) ...@@ -215,23 +213,26 @@ static int wifi_exit_smart( lua_State* L )
} }
#endif // WIFI_SMART_ENABLE #endif // WIFI_SMART_ENABLE
// Lua: realmode = setmode(mode) // Lua: wifi.setmode(mode, save_to_flash)
static int wifi_setmode( lua_State* L ) static int wifi_setmode( lua_State* L )
{ {
unsigned mode; unsigned mode;
bool save_to_flash=true;
mode = luaL_checkinteger( L, 1 ); mode = luaL_checkinteger( L, 1 );
luaL_argcheck(L, mode == STATION_MODE || mode == SOFTAP_MODE || mode == STATIONAP_MODE || mode == NULL_MODE, 1, "Invalid mode");
if ( mode != STATION_MODE && mode != SOFTAP_MODE && mode != STATIONAP_MODE && mode != NULL_MODE ) if(!lua_isnoneornil(L, 2))
return luaL_error( L, "wrong arg type" ); {
wifi_set_opmode( (uint8_t)mode); if(!lua_isboolean(L, 2)) luaL_typerror(L, 2, lua_typename(L, LUA_TBOOLEAN));
save_to_flash=lua_toboolean(L, 2);
}
if(save_to_flash) wifi_set_opmode( (uint8_t)mode);
else wifi_set_opmode_current( (uint8_t)mode);
mode = (unsigned)wifi_get_opmode(); mode = (unsigned)wifi_get_opmode();
lua_pushinteger( L, mode ); lua_pushinteger( L, mode );
return 1; return 1;
} }
// Lua: realmode = getmode() // Lua: wifi.getmode()
static int wifi_getmode( lua_State* L ) static int wifi_getmode( lua_State* L )
{ {
unsigned mode; unsigned mode;
...@@ -239,20 +240,17 @@ static int wifi_getmode( lua_State* L ) ...@@ -239,20 +240,17 @@ static int wifi_getmode( lua_State* L )
lua_pushinteger( L, mode ); lua_pushinteger( L, mode );
return 1; return 1;
} }
/**
* wifi.getchannel()
* Description:
* Get current wifi Channel
*
* Syntax:
* wifi.getchannel()
* Parameters:
* nil
*
* Returns:
* Current wifi channel
*/
// Lua: wifi.getdefaultmode()
static int wifi_getdefaultmode( lua_State* L )
{
unsigned mode;
mode = (unsigned)wifi_get_opmode_default();
lua_pushinteger( L, mode );
return 1;
}
// Lua: wifi.getchannel()
static int wifi_getchannel( lua_State* L ) static int wifi_getchannel( lua_State* L )
{ {
unsigned channel; unsigned channel;
...@@ -261,22 +259,7 @@ static int wifi_getchannel( lua_State* L ) ...@@ -261,22 +259,7 @@ static int wifi_getchannel( lua_State* L )
return 1; return 1;
} }
/** // Lua: wifi.setphymode()
* wifi.setphymode()
* Description:
* Set wifi physical mode(802.11 b/g/n)
* Note: SoftAP only supports 802.11 b/g.
* Syntax:
* wifi.setphymode(mode)
* Parameters:
* mode:
* wifi.PHYMODE_B
* wifi.PHYMODE_G
* wifi.PHYMODE_N
* Returns:
* Current physical mode after setup
*/
static int wifi_setphymode( lua_State* L ) static int wifi_setphymode( lua_State* L )
{ {
unsigned mode; unsigned mode;
...@@ -291,18 +274,7 @@ static int wifi_setphymode( lua_State* L ) ...@@ -291,18 +274,7 @@ static int wifi_setphymode( lua_State* L )
return 1; return 1;
} }
/** // Lua: wifi.getphymode()
* wifi.getphymode()
* Description:
* Get wifi physical mode(802.11 b/g/n)
* Syntax:
* wifi.getphymode()
* Parameters:
* nil
* Returns:
* Current physical mode.
*
*/
static int wifi_getphymode( lua_State* L ) static int wifi_getphymode( lua_State* L )
{ {
unsigned mode; unsigned mode;
...@@ -311,7 +283,7 @@ static int wifi_getphymode( lua_State* L ) ...@@ -311,7 +283,7 @@ static int wifi_getphymode( lua_State* L )
return 1; return 1;
} }
//wifi.sleep() // Lua: wifi.sleep()
static int wifi_sleep(lua_State* L) static int wifi_sleep(lua_State* L)
{ {
uint8 desired_sleep_state = 2; uint8 desired_sleep_state = 2;
...@@ -372,7 +344,7 @@ static int wifi_sleep(lua_State* L) ...@@ -372,7 +344,7 @@ static int wifi_sleep(lua_State* L)
return 2; return 2;
} }
//wifi.nullmodesleep() // Lua: wifi.nullmodesleep()
static int wifi_null_mode_auto_sleep(lua_State* L) static int wifi_null_mode_auto_sleep(lua_State* L)
{ {
if (!lua_isnone(L, 1)) if (!lua_isnone(L, 1))
...@@ -459,7 +431,7 @@ static int wifi_getbroadcast( lua_State* L, uint8_t mode ) ...@@ -459,7 +431,7 @@ static int wifi_getbroadcast( lua_State* L, uint8_t mode )
} }
} }
// Used by wifi_setip
static uint32_t parse_key(lua_State* L, const char * key){ static uint32_t parse_key(lua_State* L, const char * key){
lua_getfield(L, 1, key); lua_getfield(L, 1, key);
if( lua_isstring(L, -1) ) // deal with the ip/netmask/gw string if( lua_isstring(L, -1) ) // deal with the ip/netmask/gw string
...@@ -506,24 +478,86 @@ static int wifi_setip( lua_State* L, uint8_t mode ) ...@@ -506,24 +478,86 @@ static int wifi_setip( lua_State* L, uint8_t mode )
return 1; return 1;
} }
// Lua: realtype = sleeptype(type) // Lua: wifi.sta.getaplist
static int wifi_sleeptype( lua_State* L ) static int wifi_station_get_ap_info4lua( lua_State* L )
{ {
unsigned type; struct station_config config[5];
char temp[sizeof(config[0].password)+1]; //max password length + '\0'
if ( lua_isnumber(L, 1) ){ uint8 number_of_aps = wifi_station_get_ap_info(config);
type = lua_tointeger(L, 1);
if ( type != NONE_SLEEP_T && type != LIGHT_SLEEP_T && type != MODEM_SLEEP_T ) #if defined(WIFI_DEBUG)
return luaL_error( L, "wrong arg type" ); char debug_temp[128];
if(!wifi_set_sleep_type(type)){ #endif
lua_pushnil(L); lua_newtable(L);
return 1; lua_pushnumber(L, number_of_aps);
lua_setfield(L, -2, "qty");
WIFI_DBG("\n\t# of APs stored in flash:%d\n", number_of_aps);
WIFI_DBG(" %-6s %-32s %-64s %-17s\n", "index:", "ssid:", "password:", "bssid:");
for(int i=0;i<number_of_aps;i++)
{
lua_newtable(L);
memset(temp, 0, sizeof(temp));
memcpy(temp, config[i].ssid, sizeof(config[i].ssid));
lua_pushstring(L, temp);
lua_setfield(L, -2, "ssid");
#if defined(WIFI_DEBUG)
c_sprintf(debug_temp, " %-6d %-32s ", i, temp);
#endif
memset(temp, 0, sizeof(temp));
if(strlen(config[i].password) >= 8)
{
memcpy(temp, config[i].password, sizeof(config[i].password));
lua_pushstring(L, temp);
lua_setfield(L, -2, "pwd");
} }
#if defined(WIFI_DEBUG)
c_sprintf(debug_temp + strlen(debug_temp), "%-64s ", temp);
#endif
memset(temp, 0, sizeof(temp));
if (config[i].bssid_set)
{
c_sprintf(temp, MACSTR, MAC2STR(config[i].bssid));
lua_pushstring(L, temp);
lua_setfield(L, -2, "bssid");
}
#if defined(WIFI_DEBUG)
WIFI_DBG("%s%-17s \n", debug_temp, temp);
#endif
lua_pushnumber(L, i+1); //Add one, so that AP index follows Lua Conventions
lua_insert(L, -2);
lua_settable(L, -3);
} }
return 1;
}
type = wifi_get_sleep_type(); // Lua: wifi.setapnumber(number_of_aps_to_save)
lua_pushinteger( L, type ); static int wifi_station_ap_number_set4lua( lua_State* L )
return 1; {
unsigned limit=luaL_checkinteger(L, 1);
luaL_argcheck(L, (limit >= 1 && limit <= 5), 1, "Valid range: 1-5");
lua_pushboolean(L, wifi_station_ap_number_set((uint8)limit));
return 1;
}
// Lua: wifi.setapnumber(number_of_aps_to_save)
static int wifi_station_change_ap( lua_State* L )
{
uint8 ap_index=luaL_checkinteger(L, 1);
luaL_argcheck(L, (ap_index >= 1 && ap_index <= 5), 1, "Valid range: 1-5");
lua_pushboolean(L, wifi_station_ap_change(ap_index-1));
return 1;
}
// Lua: wifi.setapnumber(number_of_aps_to_save)
static int wifi_station_get_ap_index( lua_State* L )
{
lua_pushnumber(L, wifi_station_get_current_ap_id()+1);
return 1;
} }
// Lua: wifi.sta.getmac() // Lua: wifi.sta.getmac()
...@@ -551,161 +585,230 @@ static int wifi_station_getbroadcast( lua_State* L ){ ...@@ -551,161 +585,230 @@ static int wifi_station_getbroadcast( lua_State* L ){
return wifi_getbroadcast(L, STATION_IF); return wifi_getbroadcast(L, STATION_IF);
} }
/** // Used by wifi_station_getconfig_xxx
* wifi.sta.getconfig() static int wifi_station_getconfig( lua_State* L, bool get_flash_cfg)
* Description:
* Get current Station configuration.
* Note: if bssid_set==1 STATION is configured to connect to specified BSSID
* if bssid_set==0 specified BSSID address is irrelevant.
* Syntax:
* ssid, pwd, bssid_set, bssid=wifi.sta.getconfig()
* Parameters:
* none
* Returns:
* SSID, Password, BSSID_set, BSSID
*/
static int wifi_station_getconfig( lua_State* L )
{ {
struct station_config sta_conf;
char bssid[17];
wifi_station_get_config(&sta_conf);
if(sta_conf.ssid==0)
{
lua_pushnil(L);
return 1;
}
else
{
lua_pushstring( L, sta_conf.ssid );
lua_pushstring( L, sta_conf.password );
lua_pushinteger( L, sta_conf.bssid_set);
c_sprintf(bssid, MACSTR, MAC2STR(sta_conf.bssid));
lua_pushstring( L, bssid);
return 4;
}
}
/**
* wifi.sta.config()
* Description:
* Set current Station configuration.
* Note: If there are multiple APs with the same ssid, you can connect to a specific one by entering it's MAC address into the "bssid" field.
* Syntax:
* wifi.sta.getconfig(ssid, password) --Set STATION configuration, Auto-connect by default, Connects to any BSSID
* wifi.sta.getconfig(ssid, password, Auto_connect) --Set STATION configuration, Auto-connect(0 or 1), Connects to any BSSID
* wifi.sta.getconfig(ssid, password, bssid) --Set STATION configuration, Auto-connect by default, Connects to specific BSSID
* wifi.sta.getconfig(ssid, password, Auto_connect, bssid) --Set STATION configuration, Auto-connect(0 or 1), Connects to specific BSSID
* Parameters:
* ssid: string which is less than 32 bytes.
* Password: string which is less than 64 bytes.
* Auto_connect: 0 (disable Auto-connect) or 1 (to enable Auto-connect).
* bssid: MAC address of Access Point you would like to connect to.
* Returns:
* Nothing.
*
* Example:
--Connect to Access Point automatically when in range
wifi.sta.getconfig("myssid", "password")
--Connect to Access Point, User decides when to connect/disconnect to/from AP
wifi.sta.getconfig("myssid", "mypassword", 0)
wifi.sta.connect()
--do some wifi stuff
wifi.sta.disconnect()
--Connect to specific Access Point automatically when in range
wifi.sta.getconfig("myssid", "mypassword", "12:34:56:78:90:12")
--Connect to specific Access Point, User decides when to connect/disconnect to/from AP
wifi.sta.getconfig("myssid", "mypassword", 0)
wifi.sta.connect()
--do some wifi stuff
wifi.sta.disconnect()
*
*/
static int wifi_station_config( lua_State* L )
{
size_t sl, pl, ml;
struct station_config sta_conf; struct station_config sta_conf;
int auto_connect=0; char temp[sizeof(sta_conf.password)+1]; //max password length + '\0'
const char *ssid = luaL_checklstring( L, 1, &sl ); if(get_flash_cfg) wifi_station_get_config_default(&sta_conf);
if (sl>32 || ssid == NULL) else wifi_station_get_config(&sta_conf);
return luaL_error( L, "ssid:<32" ); if(sta_conf.ssid==0)
const char *password = luaL_checklstring( L, 2, &pl );
if ((pl!=0 && (pl<8 || pl>64)) || password == NULL)
return luaL_error( L, "pwd:0,8~64" );
if(lua_isnumber(L, 3))
{ {
auto_connect=luaL_checkinteger( L, 3 );; lua_pushnil(L);
if ( auto_connect != 0 && auto_connect != 1) return 1;
return luaL_error( L, "wrong arg type" );
}
else if (lua_isstring(L, 3)&& !(lua_isnumber(L, 3)))
{
lua_pushnil(L);
lua_insert(L, 3);
auto_connect=1;
} }
else else
{ {
if(lua_isnil(L, 3)) if(lua_isboolean(L, 1) && lua_toboolean(L, 1)==true)
return luaL_error( L, "wrong arg type" ); {
auto_connect=1; lua_newtable(L);
memset(temp, 0, sizeof(temp));
memcpy(temp, sta_conf.ssid, sizeof(sta_conf.ssid));
lua_pushstring(L, temp);
lua_setfield(L, -2, "ssid");
if(strlen(sta_conf.password) >= 8)
{
memset(temp, 0, sizeof(temp));
memcpy(temp, sta_conf.password, sizeof(sta_conf.password));
lua_pushstring(L, temp);
lua_setfield(L, -2, "pwd");
}
if(sta_conf.bssid_set==1)
{
memset(temp, 0, sizeof(temp));
c_sprintf(temp, MACSTR, MAC2STR(sta_conf.bssid));
lua_pushstring( L, temp);
lua_setfield(L, -2, "bssid");
}
return 1;
}
else
{
memset(temp, 0, sizeof(temp));
memcpy(temp, sta_conf.ssid, sizeof(sta_conf.ssid));
lua_pushstring(L, temp);
memset(temp, 0, sizeof(temp));
memcpy(temp, sta_conf.password, sizeof(sta_conf.password));
lua_pushstring(L, temp);
lua_pushinteger( L, sta_conf.bssid_set);
c_sprintf(temp, MACSTR, MAC2STR(sta_conf.bssid));
lua_pushstring( L, temp);
return 4;
}
} }
}
// Lua: wifi.sta.getconfig()
static int wifi_station_getconfig_current(lua_State *L)
{
return wifi_station_getconfig(L, false);
}
// Lua: wifi.sta.getdefaultconfig()
static int wifi_station_getconfig_default(lua_State *L)
{
return wifi_station_getconfig(L, true);
}
// Lua: wifi.sta.config()
static int wifi_station_config( lua_State* L )
{
struct station_config sta_conf;
bool auto_connect=true;
bool save_to_flash=true;
size_t sl, pl, ml;
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;
if(lua_isnumber(L, 4)) if(lua_istable(L, 1))
{ {
sta_conf.bssid_set = 0; lua_getfield(L, 1, "ssid");
c_memset(sta_conf.bssid, 0, 6); if (!lua_isnil(L, -1))
{
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");
memcpy(sta_conf.ssid, ssid, sl);
}
else return luaL_argerror( L, 1, "ssid:not string" );
}
else return luaL_argerror( L, 1, "ssid required" );
lua_pop(L, 1);
lua_getfield(L, 1, "pwd");
if (!lua_isnil(L, -1))
{
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");
memcpy(sta_conf.password, pwd, pl);
}
else return luaL_argerror( L, 1, "pwd:not string" );
}
lua_pop(L, 1);
lua_getfield(L, 1, "bssid");
if (!lua_isnil(L, -1))
{
if (lua_isstring(L, -1))
{
const char *macaddr = luaL_checklstring( L, -1, &ml );
luaL_argcheck(L, ((ml==sizeof("AA:BB:CC:DD:EE:FF")-1) ), 1, "bssid: FF:FF:FF:FF:FF:FF");
ets_str2macaddr(sta_conf.bssid, macaddr);
sta_conf.bssid_set = 1;
}
else return luaL_argerror(L, 1, "bssid:not string");
}
lua_pop(L, 1);
lua_getfield(L, 1, "auto");
if (!lua_isnil(L, -1))
{
if (lua_isboolean(L, -1))
{
auto_connect=lua_toboolean(L, -1);
}
else return luaL_argerror(L, 1, "auto:not boolean");
}
lua_pop(L, 1);
lua_getfield(L, 1, "save");
if (!lua_isnil(L, -1))
{
if (lua_isboolean(L, -1)) save_to_flash=lua_toboolean(L, -1);
else return luaL_argerror(L, 1, "save:not boolean");
}
else save_to_flash=false;
lua_pop(L, 1);
} }
else else //to be depreciated
{ {
if (lua_isstring(L, 4)) const char *ssid = luaL_checklstring( L, 1, &sl );
{ luaL_argcheck(L, ((sl>=1 && sl<sizeof(sta_conf.ssid)) ), 1, "length:1-32");
const char *macaddr = luaL_checklstring( L, 4, &ml );
luaL_argcheck(L, ml==17, 1, INVALID_MAC_STR); memcpy(sta_conf.ssid, ssid, sl);
c_memset(sta_conf.bssid, 0, 6);
ets_str2macaddr(sta_conf.bssid, macaddr); const char *password = luaL_checklstring( L, 2, &pl );
sta_conf.bssid_set = 1; luaL_argcheck(L, (pl==0||(pl>=8 && pl<sizeof(sta_conf.password)) ), 2, "length:0 or 8-64");
}
else memcpy(sta_conf.password, password, pl);
{
sta_conf.bssid_set = 0; if(lua_isnumber(L, 3))
c_memset(sta_conf.bssid, 0, 6); {
} lua_Integer lint=luaL_checkinteger( L, 3 );
if ( lint != 0 && lint != 1)
return luaL_error( L, "wrong arg type" );
auto_connect=(bool)lint;
}
else if (lua_isstring(L, 3)&& !(lua_isnumber(L, 3)))
{
lua_pushnil(L);
lua_insert(L, 3);
}
else
{
if(lua_isnil(L, 3))
return luaL_error( L, "wrong arg type" );
auto_connect=1;
}
if(lua_isnumber(L, 4))
{
sta_conf.bssid_set = 0;
memset(sta_conf.bssid, 0, sizeof(sta_conf.bssid));
}
else
{
if (lua_isstring(L, 4))
{
const char *macaddr = luaL_checklstring( L, 4, &ml );
luaL_argcheck(L, ml==sizeof("AA:BB:CC:DD:EE:FF")-1, 1, INVALID_MAC_STR);
memset(sta_conf.bssid, 0, sizeof(sta_conf.bssid));
ets_str2macaddr(sta_conf.bssid, macaddr);
sta_conf.bssid_set = 1;
}
else
{
sta_conf.bssid_set = 0;
memset(sta_conf.bssid, 0, sizeof(sta_conf.bssid));
}
}
} }
c_memset(sta_conf.ssid, 0, 32); #if defined(WIFI_DEBUG)
c_memset(sta_conf.password, 0, 64); char debug_temp[sizeof(sta_conf.password)+1]; //max password length + '\0'
c_memcpy(sta_conf.ssid, ssid, sl);
c_memcpy(sta_conf.password, password, pl);
NODE_DBG(sta_conf.ssid);
NODE_DBG(" %d\n", sl);
NODE_DBG(sta_conf.password);
NODE_DBG(" %d\n", pl);
NODE_DBG(" %d\n", sta_conf.bssid_set);
NODE_DBG( MACSTR, MAC2STR(sta_conf.bssid));
NODE_DBG("\n");
memset(debug_temp, 0, sizeof(debug_temp));
memcpy(debug_temp, sta_conf.ssid, sizeof(sta_conf.ssid));
WIFI_DBG("\n\tsta_conf.ssid=\"%s\" len=%d\n", debug_temp, sl);
memset(debug_temp, 0, sizeof(debug_temp));
memcpy(debug_temp, sta_conf.password, sizeof(sta_conf.password));
WIFI_DBG("\tsta_conf.password=\"%s\" len=%d\n", debug_temp, pl);
WIFI_DBG("\tsta_conf.bssid=\""MACSTR"\"\tbssid_set=%d\n", MAC2STR(sta_conf.bssid), sta_conf.bssid_set);
WIFI_DBG("\tsave_to_flash=%s\n", save_to_flash ? "true":"false");
#endif
wifi_station_disconnect(); wifi_station_disconnect();
wifi_station_set_config(&sta_conf);
if(auto_connect==0) bool config_success;
{ if(save_to_flash) config_success = wifi_station_set_config(&sta_conf);
wifi_station_set_auto_connect(false); else config_success = wifi_station_set_config_current(&sta_conf);
} wifi_station_set_auto_connect((uint8)auto_connect);
else if(auto_connect) wifi_station_connect();
{
wifi_station_set_auto_connect(true); lua_pushboolean(L, config_success);
wifi_station_connect(); return 1;
}
// station_check_connect(0);
return 0;
} }
// Lua: wifi.sta.connect() // Lua: wifi.sta.connect()
...@@ -730,45 +833,10 @@ static int wifi_station_setauto( lua_State* L ) ...@@ -730,45 +833,10 @@ static int wifi_station_setauto( lua_State* L )
a = luaL_checkinteger( L, 1 ); a = luaL_checkinteger( L, 1 );
luaL_argcheck(L, ( a == 0 || a == 1 ), 1, "0 or 1"); luaL_argcheck(L, ( a == 0 || a == 1 ), 1, "0 or 1");
wifi_station_set_auto_connect(a); wifi_station_set_auto_connect(a);
return 0; return 0;
} }
/** // Lua: wifi.sta.listap()
* wifi.sta.listap()
* Description:
* scan and get ap list as a lua table into callback function.
* Syntax:
* wifi.sta.getap(function(table))
* wifi.sta.getap(format, function(table))
* wifi.sta.getap(cfg, function(table))
* wifi.sta.getap(cfg, format, function(table))
* Parameters:
* cfg: table that contains scan configuration
* Format:Select output table format.
* 0 for the old format (SSID : Authmode, RSSI, BSSID, Channel) (Default)
* 1 for the new format (BSSID : SSID, RSSI, Authmode, Channel)
* function(table): a callback function to receive ap table when scan is done
this function receive a table, the key is the ssid,
value is other info in format: authmode,rssi,bssid,channel
* Returns:
* nil
*
* Example:
--original function left intact to preserve backward compatibility
wifi.sta.getap(function(T) for k,v in pairs(T) do print(k..":"..v) end end)
--if no scan configuration is desired cfg can be set to nil or previous example can be used
wifi.sta.getap(nil, function(T) for k,v in pairs(T) do print(k..":"..v) end end)
--scan configuration
scan_cfg={}
scan_cfg.ssid="myssid" --if set to nil, ssid is not filtered
scan_cfg.bssid="AA:AA:AA:AA:AA:AA" --if set to nil, MAC address is not filtered
scan_cfg.channel=0 --if set to nil, channel will default to 0(scans all channels), if set scan will be faster
scan_cfg.show_hidden=1 --if set to nil, show_hidden will default to 0
wifi.sta.getap(scan_cfg, function(T) for k,v in pairs(T) do print(k..":"..v) end end)
*/
static int wifi_station_listap( lua_State* L ) static int wifi_station_listap( lua_State* L )
{ {
if(wifi_get_opmode() == SOFTAP_MODE) if(wifi_get_opmode() == SOFTAP_MODE)
...@@ -915,6 +983,7 @@ static int wifi_station_listap( lua_State* L ) ...@@ -915,6 +983,7 @@ static int wifi_station_listap( lua_State* L )
{ {
unregister_lua_cb(L, &wifi_scan_succeed); unregister_lua_cb(L, &wifi_scan_succeed);
} }
return 0;
} }
// Lua: wifi.sta.gethostname() // Lua: wifi.sta.gethostname()
...@@ -925,6 +994,7 @@ static int wifi_sta_gethostname( lua_State* L ) ...@@ -925,6 +994,7 @@ static int wifi_sta_gethostname( lua_State* L )
return 1; return 1;
} }
// Used by wifi_sta_sethostname_lua and wifi_change_default_hostname
static bool wifi_sta_sethostname(const char *hostname, size_t len) static bool wifi_sta_sethostname(const char *hostname, size_t len)
{ {
//this function follows RFC 952 & RFC 1123 host name standards. //this function follows RFC 952 & RFC 1123 host name standards.
...@@ -945,6 +1015,7 @@ static bool wifi_sta_sethostname(const char *hostname, size_t len) ...@@ -945,6 +1015,7 @@ static bool wifi_sta_sethostname(const char *hostname, size_t len)
return wifi_station_set_hostname((char*)hostname); return wifi_station_set_hostname((char*)hostname);
} }
// Lua: wifi.sta.sethostname()
static int wifi_sta_sethostname_lua( lua_State* L ) static int wifi_sta_sethostname_lua( lua_State* L )
{ {
size_t len; size_t len;
...@@ -953,6 +1024,25 @@ static int wifi_sta_sethostname_lua( lua_State* L ) ...@@ -953,6 +1024,25 @@ static int wifi_sta_sethostname_lua( lua_State* L )
return 0; return 0;
} }
// Lua: wifi.sta.sleeptype(type)
static int wifi_station_sleeptype( lua_State* L )
{
unsigned type;
if ( lua_isnumber(L, 1) ){
type = lua_tointeger(L, 1);
luaL_argcheck(L, (type == NONE_SLEEP_T || type == LIGHT_SLEEP_T || type == MODEM_SLEEP_T), 1, "range:0-2");
if(!wifi_set_sleep_type(type)){
lua_pushnil(L);
return 1;
}
}
type = wifi_get_sleep_type();
lua_pushinteger( L, type );
return 1;
}
// Lua: wifi.sta.status() // Lua: wifi.sta.status()
static int wifi_station_status( lua_State* L ) static int wifi_station_status( lua_State* L )
{ {
...@@ -1021,140 +1111,241 @@ static int wifi_ap_getbroadcast( lua_State* L ){ ...@@ -1021,140 +1111,241 @@ static int wifi_ap_getbroadcast( lua_State* L ){
} }
// Lua: wifi.ap.getconfig() // Lua: wifi.ap.getconfig()
static int wifi_ap_getconfig( lua_State* L ) static int wifi_ap_getconfig( lua_State* L, bool get_flash_cfg)
{ {
struct softap_config config; struct softap_config config;
wifi_softap_get_config(&config); char temp[sizeof(config.password)+1]; //max password length + '\0'
lua_pushstring( L, config.ssid ); if (get_flash_cfg) wifi_softap_get_config_default(&config);
if(config.authmode == AUTH_OPEN) else wifi_softap_get_config(&config);
lua_pushnil(L); if(lua_isboolean(L, 1) && lua_toboolean(L, 1)==true)
{
lua_newtable(L);
memset(temp, 0, sizeof(temp));
memcpy(temp, config.ssid, sizeof(config.ssid));
lua_pushstring(L, temp);
lua_setfield(L, -2, "ssid");
if(config.authmode!=AUTH_OPEN)
{
memset(temp, 0, sizeof(temp));
memcpy(temp, config.password, sizeof(config.password));
lua_pushstring(L, temp);
lua_setfield(L, -2, "pwd");
}
lua_pushnumber(L, config.authmode);
lua_setfield(L, -2, "auth");
lua_pushnumber(L, config.channel);
lua_setfield(L, -2, "channel");
lua_pushboolean(L, (bool)config.ssid_hidden);
lua_setfield(L, -2, "hidden");
lua_pushnumber(L, config.max_connection);
lua_setfield(L, -2, "max");
lua_pushnumber(L, config.beacon_interval);
lua_setfield(L, -2, "beacon");
return 1;
}
else else
lua_pushstring( L, config.password ); {
memset(temp, 0, sizeof(temp));
memcpy(temp, config.ssid, sizeof(config.ssid));
lua_pushstring(L, temp);
if(config.authmode == AUTH_OPEN) lua_pushnil(L);
else
{
memset(temp, 0, sizeof(temp));
memcpy(temp, config.password, sizeof(config.password));
lua_pushstring(L, temp);
}
return 2; return 2;
}
}
// Lua: wifi.sta.getconfig()
static int wifi_ap_getconfig_current(lua_State *L)
{
return wifi_ap_getconfig(L, false);
}
// Lua: wifi.sta.getdefaultconfig()
static int wifi_ap_getconfig_default(lua_State *L)
{
return wifi_ap_getconfig(L, true);
} }
// Lua: wifi.ap.config(table) // Lua: wifi.ap.config(table)
static int wifi_ap_config( lua_State* L ) static int wifi_ap_config( lua_State* L )
{ {
if (!lua_istable(L, 1)) if (!lua_istable(L, 1))
return luaL_error( L, "wrong arg type" ); return luaL_typerror(L, 1, lua_typename(L, LUA_TTABLE));
struct softap_config config; struct softap_config config;
wifi_softap_get_config(&config); bool save_to_flash=true;
size_t sl = 0 , pl = 0;
lua_Integer lint=0;
int Ltype_tmp=LUA_TNONE;
size_t len; memset(config.ssid, 0, sizeof(config.ssid));
memset(config.password, 0, sizeof(config.password));
lua_getfield(L, 1, "ssid"); lua_getfield(L, 1, "ssid");
if (!lua_isnil(L, -1)){ /* found? */ if (!lua_isnil(L, -1)){ /* found? */
if( lua_isstring(L, -1) ) // deal with the ssid string if( lua_isstring(L, -1) ) // deal with the ssid string
{ {
const char *ssid = luaL_checklstring( L, -1, &len ); const char *ssid = luaL_checklstring( L, -1, &sl );
if(len<1 || len>32 || ssid == NULL) luaL_argcheck(L, ((sl>=1 && sl<=sizeof(config.ssid)) ), 1, "ssid: length:1-32");
return luaL_error( L, "ssid:1~32" ); memcpy(config.ssid, ssid, sl);
c_memset(config.ssid, 0, 32); config.ssid_len = sl;
c_memcpy(config.ssid, ssid, len);
NODE_DBG(config.ssid);
NODE_DBG("\n");
config.ssid_len = len;
config.ssid_hidden = 0; config.ssid_hidden = 0;
} }
else else return luaL_argerror( L, 1, "ssid: not string" );
return luaL_error( L, "wrong arg type" );
} }
else else return luaL_argerror( L, 1, "ssid: required" );
return luaL_error( L, "ssid required" ); lua_pop(L, 1);
lua_getfield(L, 1, "pwd"); lua_getfield(L, 1, "pwd");
if (!lua_isnil(L, -1)){ /* found? */ if (!lua_isnil(L, -1)){ /* found? */
if( lua_isstring(L, -1) ) // deal with the password string if( lua_isstring(L, -1) ) // deal with the password string
{ {
const char *pwd = luaL_checklstring( L, -1, &len ); const char *pwd = luaL_checklstring( L, -1, &pl );
if(len<8 || len>64 || pwd == NULL) luaL_argcheck(L, (pl>=8 && pl<=sizeof(config.password)), 1, "pwd: length:0 or 8-64");
return luaL_error( L, "pwd:8~64" ); memcpy(config.password, pwd, pl);
c_memset(config.password, 0, 64);
c_memcpy(config.password, pwd, len);
NODE_DBG(config.password);
NODE_DBG("\n");
config.authmode = AUTH_WPA_WPA2_PSK; config.authmode = AUTH_WPA_WPA2_PSK;
} }
else else
return luaL_error( L, "wrong arg type" ); return luaL_argerror( L, 1, "pwd: not string" );
} }
else{ else{
config.authmode = AUTH_OPEN; config.authmode = AUTH_OPEN;
} }
lua_pop(L, 1);
lua_getfield(L, 1, "auth"); lua_getfield(L, 1, "auth");
if (!lua_isnil(L, -1)) if (!lua_isnil(L, -1))
{ {
config.authmode = (uint8_t)luaL_checkinteger(L, -1); if(lua_isnumber(L, -1))
NODE_DBG("%d\n", config.authmode); {
} lint=luaL_checkinteger(L, -1);
else luaL_argcheck(L, (lint >= 0 && lint < AUTH_MAX), 1, "auth: Range:0-4");
{ config.authmode = (uint8_t)luaL_checkinteger(L, -1);
// keep whatever value resulted from "pwd" logic above }
else return luaL_argerror(L, 1, "auth: not number");
} }
lua_pop(L, 1);
lua_getfield(L, 1, "channel"); lua_getfield(L, 1, "channel");
if (!lua_isnil(L, -1)) if (!lua_isnil(L, -1))
{ {
unsigned channel = luaL_checkinteger(L, -1); if(lua_isnumber(L, -1))
if (channel < 1 || channel > 13) {
return luaL_error( L, "channel:1~13" ); lint=luaL_checkinteger(L, -1);
luaL_argcheck(L, (lint >= 1 && lint <= 13), 1, "channel: Range:1-13");
config.channel = (uint8_t)lint;
}
else luaL_argerror(L, 1, "channel: not number");
config.channel = (uint8_t)channel;
NODE_DBG("%d\n", config.channel);
} }
else else
{ {
config.channel = 6; config.channel = 6;
} }
lua_pop(L, 1);
lua_getfield(L, 1, "hidden"); lua_getfield(L, 1, "hidden");
if (!lua_isnil(L, -1)) if (!lua_isnil(L, -1))
{ {
config.ssid_hidden = (uint8_t)luaL_checkinteger(L, -1); Ltype_tmp=lua_type(L, -1);
NODE_DBG("%d\n", config.ssid_hidden); if(Ltype_tmp==LUA_TNUMBER||Ltype_tmp==LUA_TBOOLEAN)
NODE_DBG("\n"); {
if(Ltype_tmp==LUA_TNUMBER)lint=luaL_checkinteger(L, -1);
if(Ltype_tmp==LUA_TBOOLEAN)lint=(lua_Number)lua_toboolean(L, -1);
luaL_argcheck(L, (lint == 0 || lint==1), 1, "hidden: 0 or 1");
config.ssid_hidden = (uint8_t)lint;
}
else return luaL_argerror(L, 1, "hidden: not boolean");
} }
else else
{ {
config.ssid_hidden = 0; config.ssid_hidden = 0;
} }
lua_pop(L, 1);
lua_getfield(L, 1, "max"); lua_getfield(L, 1, "max");
if (!lua_isnil(L, -1)) if (!lua_isnil(L, -1))
{ {
unsigned max = luaL_checkinteger(L, -1); if(lua_isnumber(L, -1))
if (max < 1 || max > 4) {
return luaL_error( L, "max:1~4" ); lint=luaL_checkinteger(L, -1);
luaL_argcheck(L, (lint >= 1 && lint <= 4), 1, "max: 1-4");
config.max_connection = (uint8_t)max; config.max_connection = (uint8_t)lint;
NODE_DBG("%d\n", config.max_connection); }
else return luaL_argerror(L, 1, "max: not number");
} }
else else
{ {
config.max_connection = 4; config.max_connection = 4;
} }
lua_pop(L, 1);
lua_getfield(L, 1, "beacon"); lua_getfield(L, 1, "beacon");
if (!lua_isnil(L, -1)) if (!lua_isnil(L, -1))
{ {
unsigned beacon = luaL_checkinteger(L, -1); if(lua_isnumber(L, -1))
if (beacon < 100 || beacon > 60000) {
return luaL_error( L, "beacon:100~60000" ); lint=luaL_checkinteger(L, -1);
luaL_argcheck(L, (lint >= 100 && lint <= 60000), 1, "beacon: 100-60000");
config.beacon_interval = (uint16_t)beacon; config.beacon_interval = (uint16_t)lint;
NODE_DBG("%d\n", config.beacon_interval); }
else return luaL_argerror(L, 1, "beacon: not number");
} }
else else
{ {
config.beacon_interval = 100; config.beacon_interval = 100;
} }
lua_pop(L, 1);
wifi_softap_set_config(&config);
// system_restart(); lua_getfield(L, 1, "save");
return 0; if (!lua_isnil(L, -1))
{
if (lua_isboolean(L, -1))
{
save_to_flash=lua_toboolean(L, -1);
}
else return luaL_argerror(L, 1, "save: not boolean");
}
lua_pop(L, 1);
#if defined(WIFI_DEBUG)
char debug_temp[sizeof(config.password)+1];
memset(debug_temp, 0, sizeof(debug_temp));
memcpy(debug_temp, config.ssid, sizeof(config.ssid));
WIFI_DBG("\n\tconfig.ssid=\"%s\" len=%d\n", debug_temp, sl);
memset(debug_temp, 0, sizeof(debug_temp));
memcpy(debug_temp, config.password, sizeof(config.password));
WIFI_DBG("\tconfig.password=\"%s\" len=%d\n", debug_temp, pl);
WIFI_DBG("\tconfig.authmode=%d\n", config.authmode);
WIFI_DBG("\tconfig.channel=%d\n", config.channel);
WIFI_DBG("\tconfig.ssid_hidden=%d\n", config.ssid_hidden);
WIFI_DBG("\tconfig.max_connection=%d\n", config.max_connection);
WIFI_DBG("\tconfig.beacon_interval=%d\n", config.beacon_interval);
WIFI_DBG("\tsave_to_flash=%s\n", save_to_flash ? "true":"false");
#endif
bool config_success;
if(save_to_flash) config_success = wifi_softap_set_config(&config);
else config_success = wifi_softap_set_config_current(&config);
lua_pushboolean(L, config_success);
return 1;
} }
// Lua: table = wifi.ap.getclient() // Lua: table = wifi.ap.getclient()
...@@ -1183,6 +1374,7 @@ static int wifi_ap_listclient( lua_State* L ) ...@@ -1183,6 +1374,7 @@ static int wifi_ap_listclient( lua_State* L )
return 1; return 1;
} }
// Lua: ip = wifi.ap.dhcp.config() // Lua: ip = wifi.ap.dhcp.config()
static int wifi_ap_dhcp_config( lua_State* L ) static int wifi_ap_dhcp_config( lua_State* L )
{ {
...@@ -1220,6 +1412,7 @@ static int wifi_ap_dhcp_config( lua_State* L ) ...@@ -1220,6 +1412,7 @@ static int wifi_ap_dhcp_config( lua_State* L )
return 2; return 2;
} }
// Lua: wifi.ap.dhcp.start() // Lua: wifi.ap.dhcp.start()
static int wifi_ap_dhcp_start( lua_State* L ) static int wifi_ap_dhcp_start( lua_State* L )
{ {
...@@ -1227,6 +1420,7 @@ static int wifi_ap_dhcp_start( lua_State* L ) ...@@ -1227,6 +1420,7 @@ static int wifi_ap_dhcp_start( lua_State* L )
return 1; return 1;
} }
// Lua: wifi.ap.dhcp.stop() // Lua: wifi.ap.dhcp.stop()
static int wifi_ap_dhcp_stop( lua_State* L ) static int wifi_ap_dhcp_stop( lua_State* L )
{ {
...@@ -1234,28 +1428,35 @@ static int wifi_ap_dhcp_stop( lua_State* L ) ...@@ -1234,28 +1428,35 @@ static int wifi_ap_dhcp_stop( lua_State* L )
return 1; return 1;
} }
// Module function map // Module function map
static const LUA_REG_TYPE wifi_station_map[] = { static const LUA_REG_TYPE wifi_station_map[] = {
{ LSTRKEY( "getconfig" ), LFUNCVAL( wifi_station_getconfig ) }, { LSTRKEY( "autoconnect" ), LFUNCVAL( wifi_station_setauto ) },
{ LSTRKEY( "config" ), LFUNCVAL( wifi_station_config ) }, { LSTRKEY( "changeap" ), LFUNCVAL( wifi_station_change_ap ) },
{ LSTRKEY( "connect" ), LFUNCVAL( wifi_station_connect4lua ) }, { LSTRKEY( "config" ), LFUNCVAL( wifi_station_config ) },
{ LSTRKEY( "disconnect" ), LFUNCVAL( wifi_station_disconnect4lua ) }, { LSTRKEY( "connect" ), LFUNCVAL( wifi_station_connect4lua ) },
{ LSTRKEY( "autoconnect" ), LFUNCVAL( wifi_station_setauto ) }, { LSTRKEY( "disconnect" ), LFUNCVAL( wifi_station_disconnect4lua ) },
{ LSTRKEY( "getip" ), LFUNCVAL( wifi_station_getip ) },
{ LSTRKEY( "setip" ), LFUNCVAL( wifi_station_setip ) },
{ LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_station_getbroadcast) },
{ LSTRKEY( "getmac" ), LFUNCVAL( wifi_station_getmac ) },
{ LSTRKEY( "setmac" ), LFUNCVAL( wifi_station_setmac ) },
{ LSTRKEY( "getap" ), LFUNCVAL( wifi_station_listap ) },
{ LSTRKEY( "sethostname" ), LFUNCVAL( wifi_sta_sethostname_lua ) },
{ LSTRKEY( "gethostname" ), LFUNCVAL( wifi_sta_gethostname ) },
{ LSTRKEY( "getrssi" ), LFUNCVAL( wifi_station_getrssi ) },
{ LSTRKEY( "status" ), LFUNCVAL( wifi_station_status ) },
#if defined(WIFI_STATION_STATUS_MONITOR_ENABLE) #if defined(WIFI_STATION_STATUS_MONITOR_ENABLE)
{ LSTRKEY( "eventMonReg" ), LFUNCVAL( wifi_station_event_mon_reg ) }, //declared in wifi_eventmon.c { LSTRKEY( "eventMonReg" ), LFUNCVAL( wifi_station_event_mon_reg ) }, //defined in wifi_eventmon.c
{ LSTRKEY( "eventMonStart" ), LFUNCVAL( wifi_station_event_mon_start ) }, //declared in wifi_eventmon.c { LSTRKEY( "eventMonStart" ), LFUNCVAL( wifi_station_event_mon_start ) }, //defined in wifi_eventmon.c
{ LSTRKEY( "eventMonStop" ), LFUNCVAL( wifi_station_event_mon_stop ) }, //declared in wifi_eventmon.c { LSTRKEY( "eventMonStop" ), LFUNCVAL( wifi_station_event_mon_stop ) }, //defined in wifi_eventmon.c
#endif #endif
{ LSTRKEY( "getap" ), LFUNCVAL( wifi_station_listap ) },
{ LSTRKEY( "getapindex" ), LFUNCVAL( wifi_station_get_ap_index ) },
{ LSTRKEY( "getapinfo" ), LFUNCVAL( wifi_station_get_ap_info4lua ) },
{ LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_station_getbroadcast) },
{ LSTRKEY( "getconfig" ), LFUNCVAL( wifi_station_getconfig_current ) },
{ LSTRKEY( "getdefaultconfig" ), LFUNCVAL( wifi_station_getconfig_default ) },
{ LSTRKEY( "gethostname" ), LFUNCVAL( wifi_sta_gethostname ) },
{ LSTRKEY( "getip" ), LFUNCVAL( wifi_station_getip ) },
{ LSTRKEY( "getmac" ), LFUNCVAL( wifi_station_getmac ) },
{ LSTRKEY( "getrssi" ), LFUNCVAL( wifi_station_getrssi ) },
{ LSTRKEY( "setaplimit" ), LFUNCVAL( wifi_station_ap_number_set4lua ) },
{ LSTRKEY( "sethostname" ), LFUNCVAL( wifi_sta_sethostname_lua ) },
{ LSTRKEY( "setip" ), LFUNCVAL( wifi_station_setip ) },
{ LSTRKEY( "setmac" ), LFUNCVAL( wifi_station_setmac ) },
{ LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_station_sleeptype ) },
{ LSTRKEY( "status" ), LFUNCVAL( wifi_station_status ) },
{ LNILKEY, LNILVAL } { LNILKEY, LNILVAL }
}; };
...@@ -1267,23 +1468,25 @@ static const LUA_REG_TYPE wifi_ap_dhcp_map[] = { ...@@ -1267,23 +1468,25 @@ static const LUA_REG_TYPE wifi_ap_dhcp_map[] = {
}; };
static const LUA_REG_TYPE wifi_ap_map[] = { static const LUA_REG_TYPE wifi_ap_map[] = {
{ LSTRKEY( "config" ), LFUNCVAL( wifi_ap_config ) }, { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_config ) },
{ LSTRKEY( "deauth" ), LFUNCVAL( wifi_ap_deauth ) }, { LSTRKEY( "deauth" ), LFUNCVAL( wifi_ap_deauth ) },
{ LSTRKEY( "getip" ), LFUNCVAL( wifi_ap_getip ) }, { LSTRKEY( "getip" ), LFUNCVAL( wifi_ap_getip ) },
{ LSTRKEY( "setip" ), LFUNCVAL( wifi_ap_setip ) }, { LSTRKEY( "setip" ), LFUNCVAL( wifi_ap_setip ) },
{ LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_ap_getbroadcast) }, { LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_ap_getbroadcast) },
{ LSTRKEY( "getmac" ), LFUNCVAL( wifi_ap_getmac ) }, { LSTRKEY( "getmac" ), LFUNCVAL( wifi_ap_getmac ) },
{ LSTRKEY( "setmac" ), LFUNCVAL( wifi_ap_setmac ) }, { LSTRKEY( "setmac" ), LFUNCVAL( wifi_ap_setmac ) },
{ LSTRKEY( "getclient" ), LFUNCVAL( wifi_ap_listclient ) }, { LSTRKEY( "getclient" ), LFUNCVAL( wifi_ap_listclient ) },
{ LSTRKEY( "getconfig" ), LFUNCVAL( wifi_ap_getconfig ) }, { LSTRKEY( "getconfig" ), LFUNCVAL( wifi_ap_getconfig_current ) },
{ LSTRKEY( "dhcp" ), LROVAL( wifi_ap_dhcp_map ) }, { LSTRKEY( "getdefaultconfig" ), LFUNCVAL( wifi_ap_getconfig_default ) },
//{ LSTRKEY( "__metatable" ), LROVAL( wifi_ap_map ) }, { LSTRKEY( "dhcp" ), LROVAL( wifi_ap_dhcp_map ) },
//{ LSTRKEY( "__metatable" ), LROVAL( wifi_ap_map ) },
{ LNILKEY, LNILVAL } { LNILKEY, LNILVAL }
}; };
static const LUA_REG_TYPE wifi_map[] = { static const LUA_REG_TYPE wifi_map[] = {
{ LSTRKEY( "setmode" ), LFUNCVAL( wifi_setmode ) }, { LSTRKEY( "setmode" ), LFUNCVAL( wifi_setmode ) },
{ LSTRKEY( "getmode" ), LFUNCVAL( wifi_getmode ) }, { LSTRKEY( "getmode" ), LFUNCVAL( wifi_getmode ) },
{ LSTRKEY( "getdefaultmode" ), LFUNCVAL( wifi_getdefaultmode ) },
{ LSTRKEY( "getchannel" ), LFUNCVAL( wifi_getchannel ) }, { LSTRKEY( "getchannel" ), LFUNCVAL( wifi_getchannel ) },
{ LSTRKEY( "setphymode" ), LFUNCVAL( wifi_setphymode ) }, { LSTRKEY( "setphymode" ), LFUNCVAL( wifi_setphymode ) },
{ LSTRKEY( "getphymode" ), LFUNCVAL( wifi_getphymode ) }, { LSTRKEY( "getphymode" ), LFUNCVAL( wifi_getphymode ) },
...@@ -1293,7 +1496,7 @@ static const LUA_REG_TYPE wifi_map[] = { ...@@ -1293,7 +1496,7 @@ static const LUA_REG_TYPE wifi_map[] = {
{ LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) }, { LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) },
{ LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) }, { LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) },
#endif #endif
{ LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_sleeptype ) }, { LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_station_sleeptype ) },
{ LSTRKEY( "sta" ), LROVAL( wifi_station_map ) }, { LSTRKEY( "sta" ), LROVAL( wifi_station_map ) },
{ LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) }, { LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) },
...@@ -1330,6 +1533,7 @@ static const LUA_REG_TYPE wifi_map[] = { ...@@ -1330,6 +1533,7 @@ static const LUA_REG_TYPE wifi_map[] = {
{ LNILKEY, LNILVAL } { LNILKEY, LNILVAL }
}; };
// Used by user_rf_pre_init(user_main.c)
void wifi_change_default_host_name(void) void wifi_change_default_host_name(void)
{ {
uint8 opmode_temp=wifi_get_opmode(); uint8 opmode_temp=wifi_get_opmode();
......
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
#include "c_stdio.h" #include "c_stdio.h"
#include "task/task.h" #include "task/task.h"
//#define WIFI_DEBUG
//#define EVENT_DEBUG
void wifi_add_sprintf_field(lua_State* L, char* name, char* string, ...); void wifi_add_sprintf_field(lua_State* L, char* name, char* string, ...);
void wifi_add_int_field(lua_State* L, char* name, lua_Integer integer); void wifi_add_int_field(lua_State* L, char* name, lua_Integer integer);
...@@ -37,7 +40,13 @@ static inline void unregister_lua_cb(lua_State* L, int* cb_ref) ...@@ -37,7 +40,13 @@ static inline void unregister_lua_cb(lua_State* L, int* cb_ref)
void wifi_change_default_host_name(void); void wifi_change_default_host_name(void);
#ifdef NODE_DEBUG #if defined(WIFI_DEBUG) || defined(NODE_DEBUG)
#define WIFI_DBG(...) c_printf(__VA_ARGS__)
#else
#define WIFI_DBG(...) //c_printf(__VA_ARGS__)
#endif
#if defined(EVENT_DEBUG) || defined(NODE_DEBUG)
#define EVENT_DBG(...) c_printf(__VA_ARGS__) #define EVENT_DBG(...) c_printf(__VA_ARGS__)
#else #else
#define EVENT_DBG(...) //c_printf(__VA_ARGS__) #define EVENT_DBG(...) //c_printf(__VA_ARGS__)
......
...@@ -24,6 +24,23 @@ Gets the current WiFi channel. ...@@ -24,6 +24,23 @@ Gets the current WiFi channel.
#### Returns #### Returns
current WiFi channel current WiFi channel
## wifi.getdefaultmode()
Gets default WiFi operation mode.
#### Syntax
`wifi.getdefaultmode()`
#### Parameters
`nil`
#### Returns
The WiFi mode, as one of the `wifi.STATION`, `wifi.SOFTAP`, `wifi.STATIONAP` or `wifi.NULLMODE` constants.
#### See also
[`wifi.getmode()`](#wifigetmode)
[`wifi.setmode()`](#wifisetmode)
## wifi.getmode() ## wifi.getmode()
Gets WiFi operation mode. Gets WiFi operation mode.
...@@ -38,6 +55,7 @@ Gets WiFi operation mode. ...@@ -38,6 +55,7 @@ Gets WiFi operation mode.
The WiFi mode, as one of the `wifi.STATION`, `wifi.SOFTAP`, `wifi.STATIONAP` or `wifi.NULLMODE` constants. The WiFi mode, as one of the `wifi.STATION`, `wifi.SOFTAP`, `wifi.STATIONAP` or `wifi.NULLMODE` constants.
#### See also #### See also
[`wifi.getdefaultmode()`](#wifigetdefaultmode)
[`wifi.setmode()`](#wifisetmode) [`wifi.setmode()`](#wifisetmode)
## wifi.getphymode() ## wifi.getphymode()
...@@ -67,18 +85,20 @@ Configures the WiFi mode to use. NodeMCU can run in one of four WiFi modes: ...@@ -67,18 +85,20 @@ Configures the WiFi mode to use. NodeMCU can run in one of four WiFi modes:
When using the combined Station + AP mode, the same channel will be used for both networks as the radio can only listen on a single channel. When using the combined Station + AP mode, the same channel will be used for both networks as the radio can only listen on a single channel.
NOTE: WiFi Mode configuration will be retained until changed even if device is turned off. NOTE: WiFi Mode configuration will be retained until changed even if device is turned off.
#### Syntax #### Syntax
`wifi.setmode(mode)` `wifi.setmode(mode[, save])`
#### Parameters #### Parameters
`mode` value should be one of - `mode` value should be one of
- `wifi.STATION` for when the device is connected to a WiFi router. This is often done to give the device access to the Internet.
- `wifi.STATION` for when the device is connected to a WiFi router. This is often done to give the device access to the Internet. - `wifi.SOFTAP` for when the device is acting *only* as an access point. This will allow you to see the device in the list of WiFi networks (unless you hide the SSID, of course). In this mode your computer can connect to the device, creating a local area network. Unless you change the value, the NodeMCU device will be given a local IP address of 192.168.4.1 and assign your computer the next available IP address, such as 192.168.4.2.
- `wifi.SOFTAP` for when the device is acting *only* as an access point. This will allow you to see the device in the list of WiFi networks (unless you hide the SSID, of course). In this mode your computer can connect to the device, creating a local area network. Unless you change the value, the NodeMCU device will be given a local IP address of 192.168.4.1 and assign your computer the next available IP address, such as 192.168.4.2. - `wifi.STATIONAP` is the combination of `wifi.STATION` and `wifi.SOFTAP`. It allows you to create a local WiFi connection *and* connect to another WiFi router.
- `wifi.STATIONAP` is the combination of `wifi.STATION` and `wifi.SOFTAP`. It allows you to create a local WiFi connection *and* connect to another WiFi router. - `wifi.NULLMODE` changing WiFi mode to NULL_MODE will put wifi into a low power state similar to MODEM_SLEEP, provided `wifi.nullmodesleep(false)` has not been called.
- `wifi.NULLMODE` to switch off WiFi - `save` choose whether or not to save wifi mode to flash
- `true` WiFi mode configuration **will** be retained through power cycle. (Default)
- `false` WiFi mode configuration **will not** be retained through power cycle.
#### Returns #### Returns
current mode after setup current mode after setup
...@@ -90,11 +110,12 @@ wifi.setmode(wifi.STATION) ...@@ -90,11 +110,12 @@ wifi.setmode(wifi.STATION)
#### See also #### See also
[`wifi.getmode()`](#wifigetmode) [`wifi.getmode()`](#wifigetmode)
[`wifi.getdefaultmode()`](#wifigetdefaultmode)
## wifi.setphymode() ## wifi.setphymode()
Sets WiFi physical mode. Sets WiFi physical mode.
- `wifi.PHYMODE_B` - `wifi.PHYMODE_B`
802.11b, more range, low Transfer rate, more current draw 802.11b, more range, low Transfer rate, more current draw
- `wifi.PHYMODE_G` - `wifi.PHYMODE_G`
...@@ -116,7 +137,7 @@ Information from the Espressif datasheet v4.3 ...@@ -116,7 +137,7 @@ Information from the Espressif datasheet v4.3
`wifi.setphymode(mode)` `wifi.setphymode(mode)`
#### Parameters #### Parameters
`mode` one of the following `mode` one of the following
- `wifi.PHYMODE_B` - `wifi.PHYMODE_B`
- `wifi.PHYMODE_G` - `wifi.PHYMODE_G`
...@@ -130,39 +151,23 @@ physical mode after setup ...@@ -130,39 +151,23 @@ physical mode after setup
## wifi.nullmodesleep() ## wifi.nullmodesleep()
Configures whether or not WiFi automatically goes to sleep in NULL_MODE. Enabled by default. Configures whether or not WiFi automatically goes to sleep in NULL_MODE. Enabled by default.
#### Syntax
`wifi.nullmodesleep(enable)`
#### Parameters
- `enable`
- true: Enable WiFi auto sleep in NULL_MODE. (Default setting)
- false: Disable WiFi auto sleep in NULL_MODE.
#### Returns
Current/new NULL_MODE sleep setting.
## wifi.sleeptype()
Configures the WiFi modem sleep type. !!! note
This function **does not** store it's setting in flash, if auto sleep in NULL_MODE is not desired, `wifi.nullmodesleep(false)` must be called after powerup, restart, or wake from deep sleep.
#### Syntax #### Syntax
`wifi.sleeptype(type_wanted)` `wifi.nullmodesleep([enable])`
#### Parameters #### Parameters
`type_wanted` one of the following: - `enable`
- `true` Enable WiFi auto sleep in NULL_MODE. (Default setting)
- `wifi.NONE_SLEEP` to keep the modem on at all times - `false` Disable WiFi auto sleep in NULL_MODE.
- `wifi.LIGHT_SLEEP` to allow the modem to power down under some circumstances
- `wifi.MODEM_SLEEP` to power down the modem as much as possible
#### Returns #### Returns
The actual sleep mode set, as one of `wifi.NONE_SLEEP`, `wifi.LIGHT_SLEEP` or `wifi.MODEM_SLEEP`. - `sleep_enabled` Current/New NULL_MODE sleep setting
- If `wifi.nullmodesleep()` is called with no arguments, current setting is returned.
#### See also - If `wifi.nullmodesleep()` is called with `enable` argument, confirmation of new setting is returned.
- [`node.dsleep()`](node.md#nodedsleep)
- [`rtctime.dsleep()`](rtctime.md#rtctimedsleep)
## wifi.startsmart() ## wifi.startsmart()
...@@ -241,61 +246,95 @@ wifi.sta.autoconnect(1) ...@@ -241,61 +246,95 @@ wifi.sta.autoconnect(1)
- [`wifi.sta.connect()`](#wifistaconnect) - [`wifi.sta.connect()`](#wifistaconnect)
- [`wifi.sta.disconnect()`](#wifistadisconnect) - [`wifi.sta.disconnect()`](#wifistadisconnect)
## wifi.sta.changeap()
Select Access Point from list returned by `wifi.sta.getapinfo()`
#### Syntax
`wifi.sta.changeap(ap_index)`
#### Parameters
`ap_index` Index of Access Point you would like to change to. (Range:1-5)
- Corresponds to index used by [`wifi.sta.getapinfo()`](#wifistagetapinfo) and [`wifi.sta.getapindex()`](#wifistagetapindex)
#### Returns
- `true` Success
- `false` Failure
#### Example
```lua
wifi.sta.changeap(4)
```
#### See also
- [`wifi.sta.getapinfo()`](#wifistagetapinfo)
- [`wifi.sta.getapindex()`](#wifistagetapindex)
## wifi.sta.config() ## wifi.sta.config()
Sets the WiFi station configuration. Sets the WiFi station configuration.
NOTE: Station configuration will be retained until changed even if device is turned off.
#### Syntax #### Syntax
`wifi.sta.config(ssid, password[, auto[, bssid]])` `wifi.sta.config(station_config)`
#### Parameters #### Parameters
- `station_config` table containing configuration data for station
- `ssid` string which is less than 32 bytes. - `ssid` string which is less than 32 bytes.
- `password` string which is 8-64 or 0 bytes. Empty string indicates an open WiFi access point. - `pwd` string which is 8-64 or 0 bytes. Empty string indicates an open WiFi access point.
- `auto` defaults to 1 - `auto` defaults to true
- 0 to disable auto connect and remain disconnected from access point - `true` to enable auto connect and connect to access point, hence with `auto=true` there's no need to call [`wifi.sta.connect()`](#wifistaconnect)
- 1 to enable auto connect and connect to access point, hence with `auto=1` there's no need to call [`wifi.sta.connect()`](#wifistaconnect) later - `false` to disable auto connect and remain disconnected from access point
- `bssid` string that contains the MAC address of the access point (optional) - `bssid` string that contains the MAC address of the access point (optional)
- You can set BSSID if you have multiple access points with the same SSID. - You can set BSSID if you have multiple access points with the same SSID.
- Note: if you set BSSID for a specific SSID and would like to configure station to connect to the same SSID only without the BSSID requirement, you MUST first configure to station to a different SSID first, then connect to the desired SSID - Note: if you set BSSID for a specific SSID and would like to configure station to connect to the same SSID only without the BSSID requirement, you MUST first configure to station to a different SSID first, then connect to the desired SSID
- The following formats are valid: - The following formats are valid:
- "DE-C1-A5-51-F1-ED" - "DE:C1:A5:51:F1:ED"
- "AC-1D-1C-B1-0B-22" - "AC-1D-1C-B1-0B-22"
- "DE AD BE EF 7A C0" - "DE AD BE EF 7A C0"
- `save` Save station configuration to flash.
- `true` configuration **will** be retained through power cycle.
- `false` configuration **will not** be retained through power cycle. (Default)
#### Returns #### Returns
`nil` - `true` Success
- `false` Failure
#### Example #### Example
```lua ```lua
-- Connect to access point automatically when in range, `auto` defaults to 1 --connect to Access Point (DO NOT save config to flash)
wifi.sta.config("myssid", "password") station_cfg={}
station_cfg.ssid="NODE-AABBCC"
-- Connect to Unsecured access point automatically when in range, `auto` defaults to 1 station_cfg.pwd="password"
wifi.sta.config("myssid", "") wifi.sta.config(station_cfg)
-- Connect to access point, User decides when to connect/disconnect to/from AP due to `auto=0` --connect to Access Point (DO save config to flash)
wifi.sta.config("myssid", "mypassword", 0) station_cfg={}
wifi.sta.connect() station_cfg.ssid="NODE-AABBCC"
-- ... do some WiFi stuff station_cfg.pwd="password"
wifi.sta.disconnect() station_cfg.save=true
wifi.sta.config(station_cfg)
-- Connect to specific access point automatically when in range, `auto` defaults to 1
wifi.sta.config("myssid", "mypassword", "12:34:56:78:90:12") --connect to Access Point with specific MAC address
station_cfg={}
-- Connect to specific access point, User decides when to connect/disconnect to/from AP due to `auto=0` station_cfg.ssid="NODE-AABBCC"
wifi.sta.config("myssid", "mypassword", 0, "12:34:56:78:90:12") station_cfg.pwd="password"
wifi.sta.connect() station_cfg.bssid="AA:BB:CC:DD:EE:FF"
-- ... do some WiFi stuff wifi.sta.config(station_cfg)
wifi.sta.disconnect()
--configure station but don't connect to Access point
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
station_cfg.auto=false
wifi.sta.config(station_cfg)
``` ```
#### See also #### See also
- [`wifi.sta.connect()`](#wifistaconnect) - [`wifi.sta.connect()`](#wifistaconnect)
- [`wifi.sta.disconnect()`](#wifistadisconnect) - [`wifi.sta.disconnect()`](#wifistadisconnect)
- [`wifi.sta.apinfo()`](#wifistaapinfo)
## wifi.sta.connect() ## wifi.sta.connect()
...@@ -318,6 +357,10 @@ none ...@@ -318,6 +357,10 @@ none
Disconnects from AP in station mode. Disconnects from AP in station mode.
!!! note
Please note that disconnecting from Access Point does not reduce power consumption.
If power saving is your goal, please refer to the description for `wifi.NULLMODE` in the function [`wifi.setmode()`](#wifisetmode) for more details.
#### Syntax #### Syntax
`wifi.sta.disconnect()` `wifi.sta.disconnect()`
...@@ -339,14 +382,14 @@ Registers callbacks for WiFi station status events. ...@@ -339,14 +382,14 @@ Registers callbacks for WiFi station status events.
- `wifi.sta.eventMonReg(wifi_status[, function([previous_state])])` - `wifi.sta.eventMonReg(wifi_status[, function([previous_state])])`
#### Parameters #### Parameters
- `wifi_status` WiFi status you would like to set a callback for: - `wifi_status` WiFi status you would like to set a callback for:
- `wifi.STA_IDLE` - `wifi.STA_IDLE`
- `wifi.STA_CONNECTING` - `wifi.STA_CONNECTING`
- `wifi.STA_WRONGPWD` - `wifi.STA_WRONGPWD`
- `wifi.STA_APNOTFOUND` - `wifi.STA_APNOTFOUND`
- `wifi.STA_FAIL` - `wifi.STA_FAIL`
- `wifi.STA_GOTIP` - `wifi.STA_GOTIP`
- `function` callback function to perform when event occurs - `function` callback function to perform when event occurs
- Note: leaving field blank unregisters callback. - Note: leaving field blank unregisters callback.
- `previous_state` previous wifi_state(0 - 5) - `previous_state` previous wifi_state(0 - 5)
...@@ -354,7 +397,7 @@ Registers callbacks for WiFi station status events. ...@@ -354,7 +397,7 @@ Registers callbacks for WiFi station status events.
`nil` `nil`
#### Example #### Example
```lua ```lua
--register callback --register callback
wifi.sta.eventMonReg(wifi.STA_IDLE, function() print("STATION_IDLE") end) wifi.sta.eventMonReg(wifi.STA_IDLE, function() print("STATION_IDLE") end)
wifi.sta.eventMonReg(wifi.STA_CONNECTING, function() print("STATION_CONNECTING") end) wifi.sta.eventMonReg(wifi.STA_CONNECTING, function() print("STATION_CONNECTING") end)
...@@ -362,16 +405,16 @@ wifi.sta.eventMonReg(wifi.STA_WRONGPWD, function() print("STATION_WRONG_PASSWORD ...@@ -362,16 +405,16 @@ wifi.sta.eventMonReg(wifi.STA_WRONGPWD, function() print("STATION_WRONG_PASSWORD
wifi.sta.eventMonReg(wifi.STA_APNOTFOUND, function() print("STATION_NO_AP_FOUND") end) wifi.sta.eventMonReg(wifi.STA_APNOTFOUND, function() print("STATION_NO_AP_FOUND") end)
wifi.sta.eventMonReg(wifi.STA_FAIL, function() print("STATION_CONNECT_FAIL") end) wifi.sta.eventMonReg(wifi.STA_FAIL, function() print("STATION_CONNECT_FAIL") end)
wifi.sta.eventMonReg(wifi.STA_GOTIP, function() print("STATION_GOT_IP") end) wifi.sta.eventMonReg(wifi.STA_GOTIP, function() print("STATION_GOT_IP") end)
--register callback: use previous state --register callback: use previous state
wifi.sta.eventMonReg(wifi.STA_CONNECTING, function(previous_State) wifi.sta.eventMonReg(wifi.STA_CONNECTING, function(previous_State)
if(previous_State==wifi.STA_GOTIP) then if(previous_State==wifi.STA_GOTIP) then
print("Station lost connection with access point\n\tAttempting to reconnect...") print("Station lost connection with access point\n\tAttempting to reconnect...")
else else
print("STATION_CONNECTING") print("STATION_CONNECTING")
end end
end) end)
--unregister callback --unregister callback
wifi.sta.eventMonReg(wifi.STA_IDLE) wifi.sta.eventMonReg(wifi.STA_IDLE)
``` ```
...@@ -447,7 +490,7 @@ Scans AP list as a Lua table into callback function. ...@@ -447,7 +490,7 @@ Scans AP list as a Lua table into callback function.
#### Parameters #### Parameters
- `cfg` table that contains scan configuration - `cfg` table that contains scan configuration
- `ssid` SSID == nil, don't filter SSID - `ssid` SSID == nil, don't filter SSID
- `bssid` BSSID == nil, don't filter BSSID - `bssid` BSSID == nil, don't filter BSSID
- `channel` channel == 0, scan all channels, otherwise scan set channel (default is 0) - `channel` channel == 0, scan all channels, otherwise scan set channel (default is 0)
- `show_hidden` show_hidden == 1, get info for router with hidden SSID (default is 0) - `show_hidden` show_hidden == 1, get info for router with hidden SSID (default is 0)
...@@ -479,7 +522,7 @@ function listap(t) -- (SSID : Authmode, RSSI, BSSID, Channel) ...@@ -479,7 +522,7 @@ function listap(t) -- (SSID : Authmode, RSSI, BSSID, Channel)
end end
end end
wifi.sta.getap(listap) wifi.sta.getap(listap)
-- print AP list in new format -- print AP list in new format
function listap(t) function listap(t)
for k,v in pairs(t) do for k,v in pairs(t) do
...@@ -507,8 +550,8 @@ function listap(t) ...@@ -507,8 +550,8 @@ function listap(t)
end end
end end
scan_cfg = {} scan_cfg = {}
scan_cfg.ssid = "myssid" scan_cfg.ssid = "myssid"
scan_cfg.bssid = "AA:AA:AA:AA:AA:AA" scan_cfg.bssid = "AA:AA:AA:AA:AA:AA"
scan_cfg.channel = 0 scan_cfg.channel = 0
scan_cfg.show_hidden = 1 scan_cfg.show_hidden = 1
wifi.sta.getap(scan_cfg, 1, listap) wifi.sta.getap(scan_cfg, 1, listap)
...@@ -520,10 +563,10 @@ function listap(t) ...@@ -520,10 +563,10 @@ function listap(t)
print("CURRENT RSSI IS: "..rssi) print("CURRENT RSSI IS: "..rssi)
end end
end end
ssid, tmp, bssid_set, bssid=wifi.sta.getconfig() ssid, tmp, bssid_set, bssid=wifi.sta.getconfig()
scan_cfg = {} scan_cfg = {}
scan_cfg.ssid = ssid scan_cfg.ssid = ssid
if bssid_set == 1 then scan_cfg.bssid = bssid else scan_cfg.bssid = nil end if bssid_set == 1 then scan_cfg.bssid = bssid else scan_cfg.bssid = nil end
scan_cfg.channel = wifi.getchannel() scan_cfg.channel = wifi.getchannel()
scan_cfg.show_hidden = 0 scan_cfg.show_hidden = 0
...@@ -535,6 +578,88 @@ wifi.sta.getap(scan_cfg, 1, listap) ...@@ -535,6 +578,88 @@ wifi.sta.getap(scan_cfg, 1, listap)
#### See also #### See also
[`wifi.sta.getip()`](#wifistagetip) [`wifi.sta.getip()`](#wifistagetip)
## wifi.sta.getapindex()
Get index of current Access Point stored in AP cache.
#### Syntax
`wifi.sta.getapindex()`
#### Parameters
none
#### Returns
`current_index` index of currently selected Access Point. (Range:1-5)
#### Example
```lua
print("the index of the currently selected AP is: "..wifi.sta.getapindex())
```
#### See also
- [`wifi.sta.getapindex()`](#wifistagetapindex)
- [`wifi.sta.apinfo()`](#wifistaapinfo)
- [`wifi.sta.apchange()`](#wifistaapchange)
## wifi.sta.getapinfo()
Get information of APs cached by ESP8266 station.
!!! Note
Any Access Points configured with save disabled `wifi.sta.config({save=false})` will populate this list (appearing to overwrite APs stored in flash) until restart.
#### Syntax
`wifi.sta.getapinfo()`
#### Parameters
`nil`
#### Returns
- `ap_info`
- `qty` quantity of APs returned
- `1-5` index of AP. (the index corresponds to index used by [`wifi.sta.changeap()`](#wifistachangeap) and [`wifi.sta.getapindex()`](#wifistagetapindex))
- `ssid` ssid of Access Point
- `pwd` Password for Access Point
- If no password was configured, the `pwd` field will be `nil`
- `bssid` MAC address of Access Point
- If no MAC address was configured, the `bssid` field will be `nil`
#### Example
```lua
--print stored access point info
do
for k,v in pairs(wifi.sta.getapinfo()) do
if (type(v)=="table") then
print(" "..k.." : "..type(v))
for k,v in pairs(v) do
print("\t\t"..k.." : "..v)
end
else
print(" "..k.." : "..v)
end
end
end
--print stored access point info(formatted)
do
local x=wifi.sta.getapinfo()
local y=wifi.sta.getapindex()
print("\n Number of APs stored in flash:", x.qty)
print(string.format(" %-6s %-32s %-64s %-18s", "index:", "SSID:", "Password:", "BSSID:"))
for i=1, (x.qty), 1 do
print(string.format(" %s%-6d %-32s %-64s %-18s",(i==y and ">" or " "), i, x[i].ssid, x[i].pwd and x[i].pwd or type(nil), x[i].bssid and x[i].bssid or type(nil)))
end
end
```
#### See also
- [`wifi.sta.getapindex()`](#wifistagetapindex)
- [`wifi.sta.setaplimit()`](#wifistasetaplimit)
- [`wifi.sta.changeap()`](#wifistachangeap)
- [`wifi.sta.config()`](#wifistaconfig)
## wifi.sta.getbroadcast() ## wifi.sta.getbroadcast()
Gets the broadcast address in station mode. Gets the broadcast address in station mode.
...@@ -546,7 +671,7 @@ Gets the broadcast address in station mode. ...@@ -546,7 +671,7 @@ Gets the broadcast address in station mode.
`nil` `nil`
#### Returns #### Returns
broadcast address as string, for example "192.168.0.255", broadcast address as string, for example "192.168.0.255",
returns `nil` if IP address = "0.0.0.0". returns `nil` if IP address = "0.0.0.0".
#### See also #### See also
...@@ -554,23 +679,39 @@ returns `nil` if IP address = "0.0.0.0". ...@@ -554,23 +679,39 @@ returns `nil` if IP address = "0.0.0.0".
## wifi.sta.getconfig() ## wifi.sta.getconfig()
Gets the WiFi station configuration. Gets the WiFi station configuration.
#### Syntax #### Syntax
`wifi.sta.getconfig()` `wifi.sta.getconfig()`
#### Parameters #### Parameters
none - `return_table`
- `true` returns data in a table
- `false` returns data in the old format (default)
#### Returns #### Returns
ssid, password, bssid_set, bssid If `return_table` is `true`:
- `config_table`
Note: If bssid_set is equal to 0 then bssid is irrelevant - `ssid` ssid of Access Point.
- `pwd` password to Access Point.
- If no password was configured, the `pwd` field will be `nil`
- `bssid` MAC address of Access Point
- If no MAC address was configured, the `bssid` field will be `nil`
If `return_table` is `false`:
- ssid, password, bssid_set, bssid
- Note: If `bssid_set` is equal to `0` then `bssid` is irrelevant,
#### Example #### Example
```lua ```lua
--Get current Station configuration --Get current Station configuration (NEW FORMAT)
do
local def_sta_config=wifi.sta.getconfig(true)
print(string.format("\tDefault station config\n\tssid:\"%s\"\tpassword:\"%s\"%s", def_sta_config.ssid, def_sta_config.pwd, (type(def_sta_config.bssid)=="string" and "\tbssid:\""..def_sta_config.bssid.."\"" or "")))
end
--Get current Station configuration (OLD FORMAT)
ssid, password, bssid_set, bssid=wifi.sta.getconfig() ssid, password, bssid_set, bssid=wifi.sta.getconfig()
print("\nCurrent Station configuration:\nSSID : "..ssid print("\nCurrent Station configuration:\nSSID : "..ssid
.."\nPassword : "..password .."\nPassword : "..password
...@@ -580,6 +721,55 @@ ssid, password, bssid_set, bssid=nil, nil, nil, nil ...@@ -580,6 +721,55 @@ ssid, password, bssid_set, bssid=nil, nil, nil, nil
``` ```
#### See also #### See also
- [`wifi.sta.getdefaultconfig()`](#wifistagetdefaultconfig)
- [`wifi.sta.connect()`](#wifistaconnect)
- [`wifi.sta.disconnect()`](#wifistadisconnect)
## wifi.sta.getdefaultconfig()
Gets the default WiFi station configuration stored in flash.
#### Syntax
`wifi.sta.getdefaultconfig(return_table)`
#### Parameters
- `return_table`
- `true` returns data in a table
- `false` returns data in the old format (default)
#### Returns
If `return_table` is `true`:
- `config_table`
- `ssid` ssid of Access Point.
- `pwd` password to Access Point.
- If no password was configured, the `pwd` field will be `nil`
- `bssid` MAC address of Access Point
- If no MAC address was configured, the `bssid` field will be `nil`
If `return_table` is `false`:
- ssid, password, bssid_set, bssid
- Note: If `bssid_set` is equal to `0` then `bssid` is irrelevant,
#### Example
```lua
--Get default Station configuration (NEW FORMAT)
do
local def_sta_config=wifi.sta.getdefaultconfig(true)
print(string.format("\tDefault station config\n\tssid:\"%s\"\tpassword:\"%s\"%s", def_sta_config.ssid, def_sta_config.pwd, (type(def_sta_config.bssid)=="string" and "\tbssid:\""..def_sta_config.bssid.."\"" or "")))
end
--Get default Station configuration (OLD FORMAT)
ssid, password, bssid_set, bssid=wifi.sta.getdefaultconfig()
print("\nCurrent Station configuration:\nSSID : "..ssid
.."\nPassword : "..password
.."\nBSSID_set : "..bssid_set
.."\nBSSID: "..bssid.."\n")
ssid, password, bssid_set, bssid=nil, nil, nil, nil
```
#### See also
- [`wifi.sta.getconfig()`](#wifistagetconfig)
- [`wifi.sta.connect()`](#wifistaconnect) - [`wifi.sta.connect()`](#wifistaconnect)
- [`wifi.sta.disconnect()`](#wifistadisconnect) - [`wifi.sta.disconnect()`](#wifistadisconnect)
...@@ -667,6 +857,32 @@ RSSI=wifi.sta.getrssi() ...@@ -667,6 +857,32 @@ RSSI=wifi.sta.getrssi()
print("RSSI is", RSSI) print("RSSI is", RSSI)
``` ```
## wifi.sta.setaplimit()
Set Maximum number of Access Points to store in flash.
- This value is written to flash
!!! Attention
If 5 Access Points are stored and AP limit is set to 4, the AP at index 5 will remain until [`node.restore()`](node.md#noderestore) is called or AP limit is set to 5 and AP is overwritten.
#### Syntax
`wifi.sta.setaplimit(qty)`
#### Parameters
`qty` Quantity of Access Points to store in flash. Range: 1-5 (Default: 5)
#### Returns
- `true` Success
- `false` Failure
#### Example
```lua
wifi.sta.setaplimit(true)
```
#### See also
- [`wifi.sta.getapinfo()`](#wifistagetapinfo)
## wifi.sta.sethostname() ## wifi.sta.sethostname()
Sets station hostname. Sets station hostname.
...@@ -732,6 +948,26 @@ print(wifi.sta.setmac("DE:AD:BE:EF:7A:C0")) ...@@ -732,6 +948,26 @@ print(wifi.sta.setmac("DE:AD:BE:EF:7A:C0"))
#### See also #### See also
[`wifi.sta.setip()`](#wifistasetip) [`wifi.sta.setip()`](#wifistasetip)
## wifi.sta.sleeptype()
Configures the WiFi modem sleep type to be used while station is connected to an Access Point.
!!! note
Does not apply to `wifi.SOFTAP`, `wifi.STATIONAP` or `wifi.NULLMODE`.
#### Syntax
`wifi.sta.sleeptype(type_wanted)`
#### Parameters
`type_wanted` one of the following:
- `wifi.NONE_SLEEP` to keep the modem on at all times
- `wifi.LIGHT_SLEEP` to allow the CPU to power down under some circumstances
- `wifi.MODEM_SLEEP` to power down the modem as much as possible
#### Returns
The actual sleep mode set, as one of `wifi.NONE_SLEEP`, `wifi.LIGHT_SLEEP` or `wifi.MODEM_SLEEP`.
## wifi.sta.status() ## wifi.sta.status()
Gets the current status in station mode. Gets the current status in station mode.
...@@ -758,22 +994,26 @@ number: 0~5 ...@@ -758,22 +994,26 @@ number: 0~5
Sets SSID and password in AP mode. Be sure to make the password at least 8 characters long! If you don't it will default to *no* password and not set the SSID! It will still work as an access point but use a default SSID like e.g. NODE-9997C3. Sets SSID and password in AP mode. Be sure to make the password at least 8 characters long! If you don't it will default to *no* password and not set the SSID! It will still work as an access point but use a default SSID like e.g. NODE-9997C3.
NOTE: SoftAP Configuration will be retained until changed even if device is turned off.
#### Syntax #### Syntax
`wifi.ap.config(cfg)` `wifi.ap.config(cfg)`
#### Parameters #### Parameters
- `ssid` SSID chars 1-32 - `cfg` table to hold configuration
- `pwd` password chars 8-64 - `ssid` SSID chars 1-32
- `auth` authentication method, one of `wifi.OPEN` (default), `wifi.WPA_PSK`, `wifi.WPA2_PSK`, `wifi.WPA_WPA2_PSK` - `pwd` password chars 8-64
- `channel` channel number 1-14 default = 6 - `auth` authentication method, one of `wifi.OPEN` (default), `wifi.WPA_PSK`, `wifi.WPA2_PSK`, `wifi.WPA_WPA2_PSK`
- `hidden` 0 = not hidden, 1 = hidden, default 0 - `channel` channel number 1-14 default = 6
- `max` maximal number of connections 1-4 default=4 - `hidden` false = not hidden, true = hidden, default = false
- `beacon` beacon interval time in range 100-60000, default = 100 - `max` maximum number of connections 1-4 default=4
- `beacon` beacon interval time in range 100-60000, default = 100
- `save` save configuration to flash.
- `true` configuration **will** be retained through power cycle. (Default)
- `false` configuration **will not** be retained through power cycle.
#### Returns #### Returns
`nil` - `true` Success
- `false` Failure
#### Example: #### Example:
```lua ```lua
...@@ -785,7 +1025,7 @@ NOTE: SoftAP Configuration will be retained until changed even if device is turn ...@@ -785,7 +1025,7 @@ NOTE: SoftAP Configuration will be retained until changed even if device is turn
## wifi.ap.deauth() ## wifi.ap.deauth()
Deauths (forcibly removes) a client from the ESP access point by sending a corresponding IEEE802.11 management packet (first) and removing the client from it's data structures (afterwards). Deauths (forcibly removes) a client from the ESP access point by sending a corresponding IEEE802.11 management packet (first) and removing the client from it's data structures (afterwards).
The IEEE802.11 reason code used is 2 for "Previous authentication no longer valid"(AUTH_EXPIRE). The IEEE802.11 reason code used is 2 for "Previous authentication no longer valid"(AUTH_EXPIRE).
...@@ -803,11 +1043,11 @@ Returns true unless called while the ESP is in the STATION opmode ...@@ -803,11 +1043,11 @@ Returns true unless called while the ESP is in the STATION opmode
```lua ```lua
allowed_mac_list={"18:fe:34:00:00:00", "18:fe:34:00:00:01"} allowed_mac_list={"18:fe:34:00:00:00", "18:fe:34:00:00:01"}
wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T) wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T)
print("\n\tAP - STATION CONNECTED".."\n\tMAC: "..T.MAC.."\n\tAID: "..T.AID) print("\n\tAP - STATION CONNECTED".."\n\tMAC: "..T.MAC.."\n\tAID: "..T.AID)
if(allowed_mac_list~=nil) then if(allowed_mac_list~=nil) then
for _, v in pairs(allowed_mac_list) do for _, v in pairs(allowed_mac_list) do
if(v == T.MAC) then return end if(v == T.MAC) then return end
end end
end end
wifi.ap.deauth(T.MAC) wifi.ap.deauth(T.MAC)
...@@ -831,7 +1071,7 @@ Gets broadcast address in AP mode. ...@@ -831,7 +1071,7 @@ Gets broadcast address in AP mode.
none none
#### Returns #### Returns
broadcast address in string, for example "192.168.0.255", broadcast address in string, for example "192.168.0.255",
returns `nil` if IP address = "0.0.0.0". returns `nil` if IP address = "0.0.0.0".
#### Example #### Example
...@@ -871,6 +1111,102 @@ for mac,ip in pairs(wifi.ap.getclient()) do ...@@ -871,6 +1111,102 @@ for mac,ip in pairs(wifi.ap.getclient()) do
end end
``` ```
## wifi.ap.getconfig()
Gets the current SoftAP configuration.
#### Syntax
`wifi.ap.getconfig(return_table)`
#### Parameters
- `return_table`
- `true` returns data in a table
- `false` returns data in the old format (default)
#### Returns
If `return_table` is true:
- `config_table`
- `ssid` Network name
- `pwd` Password
- If no password was configured, the `pwd` field will be `nil`
- `auth` Authentication Method (`wifi.OPEN`, `wifi.WPA_PSK`, `wifi.WPA2_PSK` or `wifi.WPA_WPA2_PSK`)
- `channel` Channel number
- `hidden` `false` = not hidden, `true` = hidden
- `max` Maximum number of client connections
- `beacon` Beacon interval
If `return_table` is false:
ssid, password
Note: If bssid_set is equal to 0 then bssid is irrelevant
#### Example
```lua
--Get SoftAP configuration table (NEW FORMAT)
do
print("\n Current SoftAP configuration:")
for k,v in pairs(wifi.ap.getconfig(true)) do
print(" "..k.." :",v)
end
end
--Get current SoftAP configuration (OLD FORMAT)
do
local ssid, password=wifi.ap.getconfig()
print("\n Current SoftAP configuration:\n SSID : "..ssid..
"\n Password :",password)
ssid, password=nil, nil
end
```
## wifi.ap.getdefaultconfig()
Gets the default SoftAP configuration stored in flash.
#### Syntax
`wifi.ap.getdefaultconfig(return_table)`
#### Parameters
- `return_table`
- `true` returns data in a table
- `false` returns data in the old format (default)
#### Returns
If `return_table` is true:
- `config_table`
- `ssid` Network name
- `pwd` Password
- If no password was configured, the `pwd` field will be `nil`
- `auth` Authentication Method (`wifi.OPEN`, `wifi.WPA_PSK`, `wifi.WPA2_PSK` or `wifi.WPA_WPA2_PSK`)
- `channel` Channel number
- `hidden` `false` = not hidden, `true` = hidden
- `max` Maximum number of client connections
- `beacon` Beacon interval
If `return_table` is false:
ssid, password
Note: If bssid_set is equal to 0 then bssid is irrelevant
#### Example
```lua
--Get default SoftAP configuration table (NEW FORMAT)
do
print("\n Default SoftAP configuration:")
for k,v in pairs(wifi.ap.getdefaultconfig(true)) do
print(" "..k.." :",v)
end
end
--Get default SoftAP configuration (OLD FORMAT)
do
local ssid, password=wifi.ap.getdefaultconfig()
print("\n Default SoftAP configuration:\n SSID : "..ssid..
"\n Password :",password)
ssid, password=nil, nil
end
```
## wifi.ap.getip() ## wifi.ap.getip()
Gets IP address, netmask and gateway in AP mode. Gets IP address, netmask and gateway in AP mode.
...@@ -1077,39 +1413,39 @@ T: Table returned by event. ...@@ -1077,39 +1413,39 @@ T: Table returned by event.
#### Example #### Example
```lua ```lua
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T) wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
T.BSSID.."\n\tChannel: "..T.channel) T.BSSID.."\n\tChannel: "..T.channel)
end) end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
T.BSSID.."\n\treason: "..T.reason) T.BSSID.."\n\treason: "..T.reason)
end) end)
wifi.eventmon.register(wifi.eventmon.STA_AUTHMODE_CHANGE, Function(T) wifi.eventmon.register(wifi.eventmon.STA_AUTHMODE_CHANGE, Function(T)
print("\n\tSTA - AUTHMODE CHANGE".."\n\told_auth_mode: ".. print("\n\tSTA - AUTHMODE CHANGE".."\n\told_auth_mode: "..
T.old_auth_mode.."\n\tnew_auth_mode: "..T.new_auth_mode) T.old_auth_mode.."\n\tnew_auth_mode: "..T.new_auth_mode)
end) end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: ".. print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: "..
T.netmask.."\n\tGateway IP: "..T.gateway) T.netmask.."\n\tGateway IP: "..T.gateway)
end) end)
wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, function() wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, function()
print("\n\tSTA - DHCP TIMEOUT") print("\n\tSTA - DHCP TIMEOUT")
end) end)
wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T) wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T)
print("\n\tAP - STATION CONNECTED".."\n\tMAC: "..T.MAC.."\n\tAID: "..T.AID) print("\n\tAP - STATION CONNECTED".."\n\tMAC: "..T.MAC.."\n\tAID: "..T.AID)
end) end)
wifi.eventmon.register(wifi.eventmon.AP_STADISCONNECTED, function(T) wifi.eventmon.register(wifi.eventmon.AP_STADISCONNECTED, function(T)
print("\n\tAP - STATION DISCONNECTED".."\n\tMAC: "..T.MAC.."\n\tAID: "..T.AID) print("\n\tAP - STATION DISCONNECTED".."\n\tMAC: "..T.MAC.."\n\tAID: "..T.AID)
end) end)
wifi.eventmon.register(wifi.eventmon.AP_PROBEREQRECVED, function(T) wifi.eventmon.register(wifi.eventmon.AP_PROBEREQRECVED, function(T)
print("\n\tAP - STATION DISCONNECTED".."\n\tMAC: ".. T.MAC.."\n\tRSSI: "..T.RSSI) print("\n\tAP - STATION DISCONNECTED".."\n\tMAC: ".. T.MAC.."\n\tRSSI: "..T.RSSI)
end) end)
``` ```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment