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
This commit is contained in:
dnc40085 2016-11-06 11:51:01 -08:00 committed by Marcel Stör
parent 77b2e85d09
commit 73773fd8a5
3 changed files with 992 additions and 443 deletions

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,9 @@
#include "c_stdio.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_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)
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__)
#else
#define EVENT_DBG(...) //c_printf(__VA_ARGS__)

View File

@ -24,6 +24,23 @@ Gets the current WiFi channel.
#### Returns
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()
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.
#### See also
[`wifi.getdefaultmode()`](#wifigetdefaultmode)
[`wifi.setmode()`](#wifisetmode)
## wifi.getphymode()
@ -70,15 +88,17 @@ When using the combined Station + AP mode, the same channel will be used for bot
NOTE: WiFi Mode configuration will be retained until changed even if device is turned off.
#### Syntax
`wifi.setmode(mode)`
`wifi.setmode(mode[, save])`
#### 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.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.NULLMODE` to switch off WiFi
- `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.
- `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
current mode after setup
@ -90,6 +110,7 @@ wifi.setmode(wifi.STATION)
#### See also
[`wifi.getmode()`](#wifigetmode)
[`wifi.getdefaultmode()`](#wifigetdefaultmode)
## wifi.setphymode()
@ -132,37 +153,21 @@ physical mode after setup
Configures whether or not WiFi automatically goes to sleep in NULL_MODE. Enabled by default.
!!! 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
`wifi.nullmodesleep(enable)`
`wifi.nullmodesleep([enable])`
#### Parameters
- `enable`
- true: Enable WiFi auto sleep in NULL_MODE. (Default setting)
- false: Disable WiFi auto sleep in NULL_MODE.
- `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.
#### Syntax
`wifi.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 modem 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`.
#### See also
- [`node.dsleep()`](node.md#nodedsleep)
- [`rtctime.dsleep()`](rtctime.md#rtctimedsleep)
- `sleep_enabled` Current/New NULL_MODE sleep setting
- If `wifi.nullmodesleep()` is called with no arguments, current setting is returned.
- If `wifi.nullmodesleep()` is called with `enable` argument, confirmation of new setting is returned.
## wifi.startsmart()
@ -241,61 +246,95 @@ wifi.sta.autoconnect(1)
- [`wifi.sta.connect()`](#wifistaconnect)
- [`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()
Sets the WiFi station configuration.
NOTE: Station configuration will be retained until changed even if device is turned off.
#### Syntax
`wifi.sta.config(ssid, password[, auto[, bssid]])`
`wifi.sta.config(station_config)`
#### Parameters
- `station_config` table containing configuration data for station
- `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.
- `auto` defaults to 1
- 0 to disable auto connect and remain disconnected from access point
- 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
- `pwd` string which is 8-64 or 0 bytes. Empty string indicates an open WiFi access point.
- `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
- `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.
- 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:
- "DE-C1-A5-51-F1-ED"
- "DE:C1:A5:51:F1:ED"
- "AC-1D-1C-B1-0B-22"
- "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
`nil`
- `true` Success
- `false` Failure
#### Example
```lua
-- Connect to access point automatically when in range, `auto` defaults to 1
wifi.sta.config("myssid", "password")
--connect to Access Point (DO NOT save config to flash)
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
wifi.sta.config(station_cfg)
-- Connect to Unsecured access point automatically when in range, `auto` defaults to 1
wifi.sta.config("myssid", "")
--connect to Access Point (DO save config to flash)
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
station_cfg.save=true
wifi.sta.config(station_cfg)
-- Connect to access point, User decides when to connect/disconnect to/from AP due to `auto=0`
wifi.sta.config("myssid", "mypassword", 0)
wifi.sta.connect()
-- ... do some WiFi stuff
wifi.sta.disconnect()
--connect to Access Point with specific MAC address
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
station_cfg.bssid="AA:BB:CC:DD:EE:FF"
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")
--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)
-- Connect to specific access point, User decides when to connect/disconnect to/from AP due to `auto=0`
wifi.sta.config("myssid", "mypassword", 0, "12:34:56:78:90:12")
wifi.sta.connect()
-- ... do some WiFi stuff
wifi.sta.disconnect()
```
#### See also
- [`wifi.sta.connect()`](#wifistaconnect)
- [`wifi.sta.disconnect()`](#wifistadisconnect)
- [`wifi.sta.apinfo()`](#wifistaapinfo)
## wifi.sta.connect()
@ -318,6 +357,10 @@ none
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
`wifi.sta.disconnect()`
@ -535,6 +578,88 @@ wifi.sta.getap(scan_cfg, 1, listap)
#### See also
[`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()
Gets the broadcast address in station mode.
@ -560,17 +685,33 @@ Gets the WiFi station configuration.
`wifi.sta.getconfig()`
#### Parameters
none
- `return_table`
- `true` returns data in a table
- `false` returns data in the old format (default)
#### Returns
ssid, password, bssid_set, bssid
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`
Note: If bssid_set is equal to 0 then bssid is irrelevant
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 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()
print("\nCurrent Station configuration:\nSSID : "..ssid
.."\nPassword : "..password
@ -580,6 +721,55 @@ ssid, password, bssid_set, bssid=nil, nil, nil, nil
```
#### 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.disconnect()`](#wifistadisconnect)
@ -667,6 +857,32 @@ RSSI=wifi.sta.getrssi()
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()
Sets station hostname.
@ -732,6 +948,26 @@ print(wifi.sta.setmac("DE:AD:BE:EF:7A:C0"))
#### See also
[`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()
Gets the current status in station mode.
@ -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.
NOTE: SoftAP Configuration will be retained until changed even if device is turned off.
#### Syntax
`wifi.ap.config(cfg)`
#### Parameters
- `cfg` table to hold configuration
- `ssid` SSID chars 1-32
- `pwd` password chars 8-64
- `auth` authentication method, one of `wifi.OPEN` (default), `wifi.WPA_PSK`, `wifi.WPA2_PSK`, `wifi.WPA_WPA2_PSK`
- `channel` channel number 1-14 default = 6
- `hidden` 0 = not hidden, 1 = hidden, default 0
- `max` maximal number of connections 1-4 default=4
- `hidden` false = not hidden, true = hidden, default = false
- `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
`nil`
- `true` Success
- `false` Failure
#### Example:
```lua
@ -871,6 +1111,102 @@ for mac,ip in pairs(wifi.ap.getclient()) do
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()
Gets IP address, netmask and gateway in AP mode.