wps.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Module for WPS
  2. // by F.J. Exoo
  3. #include "module.h"
  4. #include "lauxlib.h"
  5. #include "platform.h"
  6. #include "user_interface.h"
  7. static int wps_callback_ref;
  8. // Lua: wps.disable()
  9. static int ICACHE_FLASH_ATTR wps_disable(lua_State* L)
  10. {
  11. wifi_wps_disable();
  12. return 0;
  13. }
  14. // Lua: wps.enable()
  15. static int ICACHE_FLASH_ATTR wps_enable(lua_State* L)
  16. {
  17. wifi_wps_enable(WPS_TYPE_PBC);
  18. return 0;
  19. }
  20. // WPS start callback function
  21. LOCAL void ICACHE_FLASH_ATTR user_wps_status_cb(int status)
  22. {
  23. lua_State *L = lua_getstate();
  24. if (wps_callback_ref != LUA_NOREF) {
  25. lua_rawgeti(L, LUA_REGISTRYINDEX, wps_callback_ref);
  26. lua_pushinteger(L, status);
  27. luaL_pcallx(L, 1, 0);
  28. }
  29. }
  30. // Lua: wps.start( function())
  31. static int ICACHE_FLASH_ATTR wps_start(lua_State* L)
  32. {
  33. // retrieve callback arg (optional)
  34. luaL_unref(L, LUA_REGISTRYINDEX, wps_callback_ref);
  35. wps_callback_ref = LUA_NOREF;
  36. if (lua_isfunction(L, 1))
  37. wps_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  38. else
  39. return luaL_error (L, "Argument not a function");
  40. wifi_set_wps_cb(user_wps_status_cb);
  41. wifi_wps_start();
  42. return 0;
  43. }
  44. // Module function map
  45. LROT_BEGIN(wps, NULL, 0)
  46. LROT_FUNCENTRY( disable, wps_disable )
  47. LROT_FUNCENTRY( enable, wps_enable )
  48. LROT_FUNCENTRY( start, wps_start )
  49. LROT_NUMENTRY( SUCCESS, WPS_CB_ST_SUCCESS )
  50. LROT_NUMENTRY( FAILED, WPS_CB_ST_FAILED )
  51. LROT_NUMENTRY( TIMEOUT, WPS_CB_ST_TIMEOUT )
  52. LROT_NUMENTRY( WEP, WPS_CB_ST_WEP )
  53. LROT_NUMENTRY( SCAN_ERR, 4 )
  54. LROT_END(wps, NULL, 0)
  55. int luaopen_wps( lua_State *L )
  56. {
  57. return 0;
  58. }
  59. NODEMCU_MODULE(WPS, "wps", wps, luaopen_wps);