diff --git a/app/Makefile b/app/Makefile index 850d98f4..26dca44a 100644 --- a/app/Makefile +++ b/app/Makefile @@ -126,6 +126,7 @@ LINKFLAGS_eagle.app.v6 = \ -lwpa2 \ -lsmartconfig \ -lcrypto \ + -lwps \ $(DEP_LIBS_eagle.app.v6) \ -Wl,--end-group \ -lm diff --git a/app/include/user_modules.h b/app/include/user_modules.h index 0dad1e1e..cf4f5d56 100644 --- a/app/include/user_modules.h +++ b/app/include/user_modules.h @@ -68,6 +68,7 @@ //#define LUA_USE_MODULES_UCG //#define LUA_USE_MODULES_WEBSOCKET #define LUA_USE_MODULES_WIFI +//#define LUA_USE_MODULES_WPS //#define LUA_USE_MODULES_WS2801 //#define LUA_USE_MODULES_WS2812 diff --git a/app/modules/wps.c b/app/modules/wps.c new file mode 100644 index 00000000..0513eaca --- /dev/null +++ b/app/modules/wps.c @@ -0,0 +1,73 @@ +// Module for WPS +// by F.J. Exoo + +#include "module.h" +#include "lauxlib.h" +#include "platform.h" + +static int wps_callback_ref; + +// Lua: wps.disable() +static int ICACHE_FLASH_ATTR wps_disable(lua_State* L) +{ + wifi_wps_disable(); + return 0; +} + +// Lua: wps.enable() +static int ICACHE_FLASH_ATTR wps_enable(lua_State* L) +{ + wifi_wps_enable(WPS_TYPE_PBC); + return 0; +} + +// WPS start callback function +LOCAL void ICACHE_FLASH_ATTR user_wps_status_cb(int status) +{ + lua_State *L = lua_getstate(); + + if (wps_callback_ref != LUA_NOREF) { + lua_rawgeti(L, LUA_REGISTRYINDEX, wps_callback_ref); + lua_pushinteger(L, status); + lua_call(L, 1, 0); + } +} + +// Lua: wps.start( function()) +static int ICACHE_FLASH_ATTR wps_start(lua_State* L) +{ + // retrieve callback arg (optional) + luaL_unref(L, LUA_REGISTRYINDEX, wps_callback_ref); + + wps_callback_ref = LUA_NOREF; + + if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) + wps_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); + else + return luaL_error (L, "Argument not a function"); + + wifi_set_wps_cb(user_wps_status_cb); + wifi_wps_start(); + return 0; +} + +// Module function map +const LUA_REG_TYPE wps_map[] = { + { LSTRKEY( "disable" ), LFUNCVAL( wps_disable ) }, + { LSTRKEY( "enable" ), LFUNCVAL( wps_enable ) }, + { LSTRKEY( "start" ), LFUNCVAL( wps_start ) }, + { LSTRKEY( "SUCCESS" ), LNUMVAL( WPS_CB_ST_SUCCESS ) }, + { LSTRKEY( "FAILED" ), LNUMVAL( WPS_CB_ST_FAILED ) }, + { LSTRKEY( "TIMEOUT" ), LNUMVAL( WPS_CB_ST_TIMEOUT ) }, + { LSTRKEY( "WEP" ), LNUMVAL( WPS_CB_ST_WEP ) }, + { LSTRKEY( "SCAN_ERR" ), LNUMVAL( 4 ) }, // WPS_CB_ST_SCAN_ERR + { LNILKEY, LNILVAL } +}; + +int luaopen_wps( lua_State *L ) +{ + return 0; +} + +NODEMCU_MODULE(WPS, "wps", wps_map, luaopen_wps); + diff --git a/docs/en/modules/wps.md b/docs/en/modules/wps.md new file mode 100644 index 00000000..cea8659b --- /dev/null +++ b/docs/en/modules/wps.md @@ -0,0 +1,63 @@ +# WPS Module +| Since | Origin / Contributor | Maintainer | Source | +| :----- | :-------------------- | :---------- | :------ | +| 2017-01-01 | [Frank Exoo](https://github.com/FrankX0) | [Frank Exoo](https://github.com/FrankX0) | [wps.c](../../../app/modules/wps.c)| + +[WPS](https://en.wikipedia.org/wiki/Wi-Fi_Protected_Setup) allows devices to be added to an existing network without entering the network credentials. + +## wps.disable() +Disable WiFi WPS function. + +#### Syntax +`wps.disable()` + +#### Parameters +none + +#### Returns +`nil` + +## wps.enable() +Enable WiFi WPS function. + +#### Syntax +`wps.enable()` + +#### Parameters +none + +#### Returns +`nil` + +## wps.start() +Start WiFi WPS function. WPS must be enabled prior calling this function. + +#### Syntax +`wps.start([function(status)])` + +#### Parameters +- `function(status)` callback function for when the WPS function ends. + +#### Returns +`nil` + +#### Example +```lua +wps.enable() +wps.start(function(status) + if status == wps.SUCCESS then + print("SUCCESS!") + elseif status == wps.FAILED then + print("Failed") + elseif status == wps.TIMEOUT then + print("Timeout") + elseif status == wps.WEP then + print("WEP not supported") + elseif status == wps.SCAN_ERR then + print("WPS AP not found") + else + print(status) + end + wps.disable() +end) +``` diff --git a/mkdocs.yml b/mkdocs.yml index a030cd36..45bfebfa 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -85,6 +85,7 @@ pages: - 'ucg': 'en/modules/ucg.md' - 'websocket': 'en/modules/websocket.md' - 'wifi': 'en/modules/wifi.md' + - 'wps': 'en/modules/wps.md' - 'ws2801': 'en/modules/ws2801.md' - 'ws2812': 'en/modules/ws2812.md' #- Deutsch: