luat_lib_wlan.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "luat_base.h"
  2. #include "luat_wlan.h"
  3. #define LUAT_LOG_TAG "wlan"
  4. #include "luat_log.h"
  5. static int l_wlan_init(lua_State* L){
  6. luat_wlan_init(NULL);
  7. return 0;
  8. }
  9. static int l_wlan_mode(lua_State* L){
  10. int mode = LUAT_WLAN_MODE_STA;
  11. if (lua_isinteger(L, 1)) {
  12. mode = lua_tointeger(L, 1);
  13. }
  14. else if (lua_isinteger(L, 2)) {
  15. mode = lua_tointeger(L, 2);
  16. }
  17. if (mode <= LUAT_WLAN_MODE_NULL || mode >= LUAT_WLAN_MODE_MAX) {
  18. mode = LUAT_WLAN_MODE_STA;
  19. }
  20. // switch (mode)
  21. // {
  22. // case LUAT_WLAN_MODE_NULL:
  23. // LLOGD("wlan mode NULL");
  24. // break;
  25. // case LUAT_WLAN_MODE_STA:
  26. // LLOGD("wlan mode STATION");
  27. // break;
  28. // case LUAT_WLAN_MODE_AP:
  29. // LLOGD("wlan mode AP");
  30. // break;
  31. // case LUAT_WLAN_MODE_APSTA:
  32. // LLOGD("wlan mode AP-STATION");
  33. // break;
  34. // default:
  35. // break;
  36. // }
  37. luat_wlan_config_t conf = {
  38. .mode = mode
  39. };
  40. luat_wlan_mode(&conf);
  41. return 0;
  42. }
  43. static int l_wlan_ready(lua_State* L){
  44. lua_pushboolean(L, luat_wlan_ready());
  45. return 1;
  46. }
  47. static int l_wlan_connect(lua_State* L){
  48. const char* ssid = luaL_optstring(L, 1, "");
  49. const char* password = luaL_optstring(L, 2, "");
  50. luat_wlan_conninfo_t info = {0};
  51. info.auto_reconnection = 1;
  52. memcpy(info.ssid, ssid, strlen(ssid));
  53. memcpy(info.password, password, strlen(password));
  54. luat_wlan_connect(&info);
  55. return 0;
  56. }
  57. static int l_wlan_disconnect(lua_State* L){
  58. luat_wlan_disconnect();
  59. return 0;
  60. }
  61. static int l_wlan_scan(lua_State* L){
  62. luat_wlan_scan();
  63. return 0;
  64. }
  65. static int l_wlan_scan_result(lua_State* L) {
  66. int ap_limit = luaL_optinteger(L, 1, 20);
  67. if (ap_limit > 32)
  68. ap_limit = 32;
  69. else if (ap_limit < 8)
  70. ap_limit = 8;
  71. lua_newtable(L);
  72. luat_wlan_scan_result_t *results = luat_heap_malloc(sizeof(luat_wlan_scan_result_t) * ap_limit);
  73. if (results == NULL) {
  74. LLOGE("out of memory when malloc scan result");
  75. return 1;
  76. }
  77. memset(results, 0, sizeof(luat_wlan_scan_result_t) * ap_limit);
  78. int len = luat_wlan_scan_get_result(results, ap_limit);
  79. for (size_t i = 0; i < len; i++)
  80. {
  81. lua_newtable(L);
  82. lua_pushstring(L, (const char *)results[i].ssid);
  83. lua_setfield(L, -2, "ssid");
  84. // lua_pushfstring(L, "%02X%02X%02X%02X%02X%02X", results[i].bssid[0],
  85. // results[i].bssid[1],
  86. // results[i].bssid[2],
  87. // results[i].bssid[3],
  88. // results[i].bssid[4],
  89. // results[i].bssid[5]);
  90. lua_pushlstring(L, (const char *)results[i].bssid, 6);
  91. lua_setfield(L, -2, "bssid");
  92. lua_pushinteger(L, results[i].ch);
  93. lua_setfield(L, -2, "channel");
  94. lua_pushinteger(L, results[i].rssi);
  95. lua_setfield(L, -2, "rssi");
  96. lua_seti(L, -2, i + 1);
  97. }
  98. luat_heap_free(results);
  99. return 1;
  100. }
  101. static int l_wlan_smartconfig(lua_State *L) {
  102. int tp = luaL_optinteger(L, 1, LUAT_SC_TYPE_ESPTOUCH);
  103. if (tp == LUAT_SC_TYPE_STOP) {
  104. luat_wlan_smartconfig_stop();
  105. return 0;
  106. }
  107. else {
  108. int ret = luat_wlan_smartconfig_start(tp);
  109. lua_pushboolean(L, ret == 0 ? 1 : 0);
  110. return 1;
  111. }
  112. }
  113. // 获取mac
  114. static int l_wlan_get_mac(lua_State* L){
  115. char tmp[6] = {0};
  116. char tmpbuff[16] = {0};
  117. luat_wlan_get_mac(luaL_optinteger(L, 1, 0), tmp);
  118. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]);
  119. lua_pushstring(L, tmpbuff);
  120. return 1;
  121. }
  122. // 启动AP
  123. static int l_wlan_ap_start(lua_State *L) {
  124. size_t ssid_len = 0;
  125. size_t password_len = 0;
  126. luat_wlan_apinfo_t apinfo = {0};
  127. const char* ssid = luaL_checklstring(L, 1, &ssid_len);
  128. const char* password = luaL_optlstring(L, 2, "", &password_len);
  129. if (ssid_len < 1) {
  130. LLOGE("ssid MUST NOT EMTRY");
  131. return 0;
  132. }
  133. if (ssid_len > 32) {
  134. LLOGE("ssid too long [%s]", ssid);
  135. return 0;
  136. }
  137. if (password_len > 63) {
  138. LLOGE("password too long [%s]", password);
  139. return 0;
  140. }
  141. memcpy(apinfo.ssid, ssid, ssid_len);
  142. memcpy(apinfo.password, password, password_len);
  143. int ret = luat_wlan_ap_start(&apinfo);
  144. LLOGD("apstart ret %d", ret);
  145. lua_pushboolean(L, ret == 0 ? 1 : 0);
  146. return 1;
  147. }
  148. #include "rotable2.h"
  149. static const rotable_Reg_t reg_wlan[] =
  150. {
  151. { "init", ROREG_FUNC(l_wlan_init)},
  152. { "mode", ROREG_FUNC(l_wlan_mode)},
  153. { "setMode", ROREG_FUNC(l_wlan_mode)},
  154. // { "setMode", ROREG_FUNC(l_wlan_set_mode)},
  155. // { "getMode", ROREG_FUNC(l_wlan_get_mode)},
  156. { "ready", ROREG_FUNC(l_wlan_ready)},
  157. { "connect", ROREG_FUNC(l_wlan_connect)},
  158. { "disconnect", ROREG_FUNC(l_wlan_disconnect)},
  159. { "scan", ROREG_FUNC(l_wlan_scan)},
  160. { "scanResult", ROREG_FUNC(l_wlan_scan_result)},
  161. // 配网相关
  162. { "smartconfig", ROREG_FUNC(l_wlan_smartconfig)},
  163. { "getMac", ROREG_FUNC(l_wlan_get_mac)},
  164. // AP相关
  165. { "createAP", ROREG_FUNC(l_wlan_ap_start)},
  166. // 常数
  167. {"NONE", ROREG_INT(LUAT_WLAN_MODE_NULL)},
  168. {"STATION", ROREG_INT(LUAT_WLAN_MODE_STA)},
  169. {"AP", ROREG_INT(LUAT_WLAN_MODE_AP)},
  170. {"STATIONAP", ROREG_INT(LUAT_WLAN_MODE_APSTA)},
  171. // smartconfig 配网
  172. {"STOP", ROREG_INT(0)},
  173. {"ESPTOUCH", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH)},
  174. {"AIRKISS", ROREG_INT(LUAT_SC_TYPE_AIRKISS)},
  175. {"ESPTOUCH_AIRKISS", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_AIRKISS)},
  176. {"ESPTOUCH_V2", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_V2)},
  177. { NULL, ROREG_INT(0)}
  178. };
  179. LUAMOD_API int luaopen_wlan( lua_State *L ) {
  180. luat_newlib2(L, reg_wlan);
  181. return 1;
  182. }