Pārlūkot izejas kodu

Add WPS module (#1694)

* WPS functionality added

* WPS module switched off by default

* Update mkdocs.yml
FrankX 7 gadi atpakaļ
vecāks
revīzija
378e5eb0ad
5 mainītis faili ar 139 papildinājumiem un 0 dzēšanām
  1. 1 0
      app/Makefile
  2. 1 0
      app/include/user_modules.h
  3. 73 0
      app/modules/wps.c
  4. 63 0
      docs/en/modules/wps.md
  5. 1 0
      mkdocs.yml

+ 1 - 0
app/Makefile

@@ -126,6 +126,7 @@ LINKFLAGS_eagle.app.v6 = 			\
 	-lwpa2					\
 	-lsmartconfig 				\
 	-lcrypto				\
+	-lwps					\
 	$(DEP_LIBS_eagle.app.v6) 		\
 	-Wl,--end-group 			\
 	-lm

+ 1 - 0
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
 

+ 73 - 0
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);
+

+ 63 - 0
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)
+```

+ 1 - 0
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: