luat_lib_wlan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. @module wlan
  3. @summary wifi操作
  4. @catalog 外设API
  5. @version 1.0
  6. @date 2022.09.30
  7. @demo wlan
  8. @tag LUAT_USE_WLAN
  9. @usage
  10. --[[
  11. 提醒:
  12. 对于仅支持wifiscan的模块, 仅 init/scan/scanResult 函数是可用的
  13. 例如: Air780EXXX等仅支持wifiscan
  14. ]]
  15. */
  16. #include "luat_base.h"
  17. #include "luat_wlan.h"
  18. #ifdef LUAT_USE_DRV_WLAN
  19. #include "luat/drv_wlan.h"
  20. #endif
  21. #define LUAT_LOG_TAG "wlan"
  22. #include "luat_log.h"
  23. uint32_t ipaddr_addr(const char *cp);
  24. static inline void to_ipv4(const char* data, uint8_t* dst) {
  25. uint32_t tmpip = ipaddr_addr(data);
  26. dst[3] = (tmpip >> 24) & 0xFF;
  27. dst[2] = (tmpip >> 16) & 0xFF;
  28. dst[1] = (tmpip >> 8) & 0xFF;
  29. dst[0] = (tmpip >> 0) & 0xFF;
  30. }
  31. /*
  32. 初始化
  33. @api wlan.init()
  34. @return bool 成功返回true,否则返回false
  35. */
  36. static int l_wlan_init(lua_State* L){
  37. #ifdef LUAT_USE_DRV_WLAN
  38. int ret = luat_drv_wlan_init(NULL);
  39. #else
  40. int ret = luat_wlan_init(NULL);
  41. #endif
  42. lua_pushboolean(L, ret == 0 ? 1 : 0);
  43. return 1;
  44. }
  45. /*
  46. 设置wifi模式
  47. @api wlan.setMode(mode)
  48. @int wifi模式
  49. @return bool 成功返回true,否则返回false
  50. @usage
  51. -- 设置为AP模式, 广播ssid, 接收wifi客户端的链接
  52. wlan.setMode(wlan.AP)
  53. -- 设置为STATION模式, 也是初始化后的默认模式
  54. wlan.setMode(wlan.STATION)
  55. -- 混合模式, 做AP又做STATION
  56. wlan.setMode(wlan.APSTA)
  57. */
  58. static int l_wlan_mode(lua_State* L){
  59. int mode = LUAT_WLAN_MODE_STA;
  60. if (lua_isinteger(L, 1)) {
  61. mode = lua_tointeger(L, 1);
  62. }
  63. else if (lua_isinteger(L, 2)) {
  64. mode = lua_tointeger(L, 2);
  65. }
  66. if (mode <= LUAT_WLAN_MODE_NULL || mode >= LUAT_WLAN_MODE_MAX) {
  67. mode = LUAT_WLAN_MODE_STA;
  68. }
  69. luat_wlan_config_t conf = {
  70. .mode = mode
  71. };
  72. #ifdef LUAT_USE_DRV_WLAN
  73. int ret = luat_drv_wlan_mode(&conf);
  74. #else
  75. int ret = luat_wlan_mode(&conf);
  76. #endif
  77. lua_pushboolean(L, ret == 0 ? 1 : 0);
  78. return 1;
  79. }
  80. /*
  81. 作为STATION时,是否已经连接上AP,且获取IP成功
  82. @api wlan.ready()
  83. @return bool 已经连接成功返回true,否则返回false
  84. */
  85. static int l_wlan_ready(lua_State* L){
  86. #ifdef LUAT_USE_DRV_WLAN
  87. lua_pushboolean(L, luat_drv_wlan_ready());
  88. #else
  89. lua_pushboolean(L, luat_wlan_ready());
  90. #endif
  91. return 1;
  92. }
  93. /*
  94. 作为STATION时,连接到指定AP
  95. @api wlan.connect(ssid, password, auto_reconnect, bssid)
  96. @string AP的ssid
  97. @string AP的password,可选
  98. @int 0关闭自动重连,1开启自动重连.当前强制开启自动重连
  99. @string AP的bssid,可选,必须是6字节
  100. @return bool 发起连接成功返回true,否则返回false.注意,不代表连接AP成功!!
  101. @usage
  102. -- 普通模式,带密码
  103. wlan.connect("myap", "12345678")
  104. -- 普通模式,不带密码
  105. wlan.connect("myap")
  106. -- 特殊模式, 重用之前的ssid和密码,本次直接连接
  107. -- 注意, 前提是本次上电后已经传过ssid和或password,否则必失败
  108. wlan.connect()
  109. -- 特殊模式, 使用ssid和密码,本次连接指定bssid, 2024.5.7新增
  110. local bssid = string.fromHex("00182946365f")
  111. wlan.connect("myap", "12345678", 1, bssid)
  112. */
  113. static int l_wlan_connect(lua_State* L){
  114. const char* ssid = luaL_optstring(L, 1, "");
  115. const char* password = luaL_optstring(L, 2, "");
  116. size_t len = 0;
  117. luat_wlan_conninfo_t info = {0};
  118. info.auto_reconnection = 1;
  119. memcpy(info.ssid, ssid, strlen(ssid));
  120. memcpy(info.password, password, strlen(password));
  121. const char* bssid = luaL_optlstring(L, 4, "", &len);
  122. if (len == 6) {
  123. memcpy(info.bssid, bssid, 6);
  124. }
  125. #ifdef LUAT_USE_DRV_WLAN
  126. int ret = luat_drv_wlan_connect(&info);
  127. #else
  128. int ret = luat_wlan_connect(&info);
  129. #endif
  130. lua_pushboolean(L, ret == 0 ? 1 : 0);
  131. return 1;
  132. }
  133. /*
  134. 作为STATION时,断开AP
  135. @api wlan.disconnect()
  136. */
  137. static int l_wlan_disconnect(lua_State* L){
  138. (void)L;
  139. #ifdef LUAT_USE_DRV_WLAN
  140. luat_drv_wlan_disconnect();
  141. #else
  142. luat_wlan_disconnect();
  143. #endif
  144. return 0;
  145. }
  146. /*
  147. 扫描wifi频段
  148. @api wlan.scan()
  149. @usage
  150. -- 注意, wlan.scan()是异步API,启动扫描后会马上返回
  151. -- wifi扫描成功后, 会有WLAN_SCAN_DONE消息, 读取即可
  152. sys.subscribe("WLAN_SCAN_DONE", function ()
  153. local results = wlan.scanResult()
  154. log.info("scan", "results", #results)
  155. for k,v in pairs(results) do
  156. log.info("scan", v["ssid"], v["rssi"], (v["bssid"]:toHex()))
  157. end
  158. end)
  159. -- 下面演示的是初始化wifi后定时扫描,请按实际业务需求修改
  160. sys.taskInit(function()
  161. sys.wait(1000)
  162. wlan.init()
  163. while 1 do
  164. wlan.scan()
  165. sys.wait(15000)
  166. end
  167. end)
  168. */
  169. static int l_wlan_scan(lua_State* L){
  170. (void)L;
  171. #ifdef LUAT_USE_DRV_WLAN
  172. luat_drv_wlan_scan();
  173. #else
  174. luat_wlan_scan();
  175. #endif
  176. return 0;
  177. }
  178. /*
  179. 获取wifi扫描结果
  180. @api wlan.scanResult()
  181. @return table 扫描结果
  182. @usage
  183. -- 用法请查阅 wlan.scan() 函数
  184. */
  185. static int l_wlan_scan_result(lua_State* L) {
  186. int ap_limit = luaL_optinteger(L, 1, 20);
  187. if (ap_limit > 32)
  188. ap_limit = 32;
  189. else if (ap_limit < 8)
  190. ap_limit = 8;
  191. lua_newtable(L);
  192. luat_wlan_scan_result_t *results = luat_heap_malloc(sizeof(luat_wlan_scan_result_t) * ap_limit);
  193. if (results == NULL) {
  194. LLOGE("out of memory when malloc scan result");
  195. return 1;
  196. }
  197. memset(results, 0, sizeof(luat_wlan_scan_result_t) * ap_limit);
  198. #ifdef LUAT_USE_DRV_WLAN
  199. int len = luat_drv_wlan_scan_get_result(results, ap_limit);
  200. #else
  201. int len = luat_wlan_scan_get_result(results, ap_limit);
  202. #endif
  203. for (int i = 0; i < len; i++)
  204. {
  205. lua_newtable(L);
  206. lua_pushstring(L, (const char *)results[i].ssid);
  207. lua_setfield(L, -2, "ssid");
  208. // lua_pushfstring(L, "%02X%02X%02X%02X%02X%02X", results[i].bssid[0],
  209. // results[i].bssid[1],
  210. // results[i].bssid[2],
  211. // results[i].bssid[3],
  212. // results[i].bssid[4],
  213. // results[i].bssid[5]);
  214. lua_pushlstring(L, (const char *)results[i].bssid, 6);
  215. lua_setfield(L, -2, "bssid");
  216. lua_pushinteger(L, results[i].ch);
  217. lua_setfield(L, -2, "channel");
  218. lua_pushinteger(L, results[i].rssi);
  219. lua_setfield(L, -2, "rssi");
  220. lua_seti(L, -2, i + 1);
  221. }
  222. luat_heap_free(results);
  223. return 1;
  224. }
  225. /*
  226. 配网
  227. @api wlan.smartconfig(mode)
  228. @int 配网模式, 默认为esptouch, 若传0则主动停止配网
  229. @return bool 启动成功或停止成功, 返回true, 否则返回false
  230. @usage
  231. wlan.smartconfig()
  232. local ret, ssid, passwd = sys.waitUntil("SC_RESULT", 180*1000) -- 最多等3分钟
  233. log.info("sc", ret, ssid, passwd)
  234. -- 详细用法请查看demo
  235. */
  236. static int l_wlan_smartconfig(lua_State *L) {
  237. int tp = luaL_optinteger(L, 1, LUAT_SC_TYPE_ESPTOUCH);
  238. if (tp == LUAT_SC_TYPE_STOP) {
  239. luat_wlan_smartconfig_stop();
  240. lua_pushboolean(L, 1);
  241. }
  242. else {
  243. int ret = luat_wlan_smartconfig_start(tp);
  244. lua_pushboolean(L, ret == 0 ? 1 : 0);
  245. }
  246. return 1;
  247. }
  248. /*
  249. 获取mac
  250. @api wlan.getMac(tp, hexstr)
  251. @int 设置何种mac地址,对ESP32系列来说,只能设置STA的地址,即0,默认值也是0
  252. @bool 是否转HEX字符, 默认是true,即输出hex字符串
  253. @return string MAC地址,十六进制字符串形式 "AABBCCDDEEFF" 或原始数据
  254. log.info("wlan mac", wlan.getMac())
  255. */
  256. static int l_wlan_get_mac(lua_State* L){
  257. uint8_t tmp[6] = {0};
  258. char tmpbuff[16] = {0};
  259. luat_wlan_get_mac(luaL_optinteger(L, 1, 0), (char*)tmp);
  260. if (lua_isboolean(L, 2) && !lua_toboolean(L, 2)) {
  261. lua_pushlstring(L, (const char*)tmp, 6);
  262. }
  263. else {
  264. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]);
  265. lua_pushstring(L, tmpbuff);
  266. }
  267. return 1;
  268. }
  269. /*
  270. 设置mac
  271. @api wlan.setMac(tp, mac)
  272. @int 设置何种mac地址,对ESP32系列来说,只能设置STA的地址,即0
  273. @string 待设置的MAC地址,长度6字节
  274. @return bool 成功返回true,否则返回false
  275. @usage
  276. -- 设置MAC地址, 2023-03-01之后编译的固件可用
  277. local mac = string.fromHex("F01122334455")
  278. wlan.setMac(0, mac)
  279. -- 部分模块支持恢复默认MAC, 例如esp32系列
  280. -- 在2023-11-01 之后编译的固件可用
  281. local mac = string.fromHex("000000000000")
  282. wlan.setMac(0, mac)
  283. */
  284. static int l_wlan_set_mac(lua_State* L){
  285. // int id = luaL_optinteger(L, 1, 0);
  286. const char* mac = luaL_checkstring(L, 2);
  287. int ret = luat_wlan_set_mac(luaL_optinteger(L, 1, 0), mac);
  288. lua_pushboolean(L, ret == 0 ? 1 : 0);
  289. return 1;
  290. }
  291. /*
  292. 获取ip,仅STATION或APSTA模式下有意义
  293. @api wlan.getIP()
  294. @return string ip地址,当前仅返回ipv4地址,例如 "192.168.1.25"
  295. */
  296. static int l_wlan_get_ip(lua_State* L){
  297. char tmpbuff[16] = {0};
  298. luat_wlan_get_ip(luaL_optinteger(L, 1, 0), tmpbuff);
  299. lua_pushstring(L, tmpbuff);
  300. return 1;
  301. }
  302. /*
  303. 启动AP
  304. @api wlan.createAP(ssid, passwd, gateway, netmask, channel, opts)
  305. @string AP的SSID,必填
  306. @string AP的密码,可选
  307. @string AP的网关地址, 默认192.168.4.1
  308. @string AP的网关掩码, 默认255.255.255.0
  309. @int AP建立的通道, 默认6
  310. @table AP的配置选项, 可选
  311. @return bool 成功创建返回true,否则返回false
  312. @usage
  313. -- 注意, 调用本AP时,若wifi模式为STATION,会自动切换成 APSTA
  314. wlan.createAP("luatos1234", "12341234")
  315. -- 设置网关IP,掩码, 通道, 2023.7.13 新增, BSP未必支持
  316. -- wlan.createAP("luatos1234", "12341234", "192.168.4.1", "255.255.255.0", 6)
  317. -- opts更多配置项, 2024.3.5新增
  318. --[[
  319. {
  320. hidden = false, -- 是否隐藏SSID, 默认false,不隐藏
  321. max_conn = 4 -- 最大客户端数量, 默认4
  322. }
  323. ]]
  324. */
  325. #include "lwip/opt.h"
  326. #include "lwip/ip_addr.h"
  327. #include "lwip/netif.h"
  328. static int l_wlan_ap_start(lua_State *L) {
  329. size_t ssid_len = 0;
  330. size_t password_len = 0;
  331. luat_wlan_apinfo_t apinfo = {0};
  332. const char* ssid = luaL_checklstring(L, 1, &ssid_len);
  333. const char* password = luaL_optlstring(L, 2, "", &password_len);
  334. const char* gateway = luaL_optstring(L, 3, "192.168.4.1");
  335. const char* netmask = luaL_optstring(L, 4, "255.255.255.0");
  336. if (strlen(gateway) > 7) {
  337. to_ipv4(gateway, apinfo.gateway);
  338. }
  339. if (strlen(netmask) > 7) {
  340. to_ipv4(netmask, apinfo.netmask);
  341. }
  342. apinfo.channel = (uint8_t)luaL_optinteger(L, 5, 6);
  343. if (ssid_len < 1) {
  344. LLOGE("ssid MUST NOT EMTRY");
  345. return 0;
  346. }
  347. if (ssid_len > 32) {
  348. LLOGE("ssid too long [%s]", ssid);
  349. return 0;
  350. }
  351. if (password_len > 63) {
  352. LLOGE("password too long [%s]", password);
  353. return 0;
  354. }
  355. if (lua_istable(L, 6)) {
  356. lua_getfield(L, 6, "hidden");
  357. apinfo.hidden = lua_toboolean(L, -1);
  358. lua_pop(L, 1);
  359. lua_getfield(L, 6, "max_conn");
  360. apinfo.max_conn = lua_tonumber(L, -1);
  361. lua_pop(L, 1);
  362. }
  363. memcpy(apinfo.ssid, ssid, ssid_len);
  364. memcpy(apinfo.password, password, password_len);
  365. #ifdef LUAT_USE_DRV_WLAN
  366. int ret = luat_drv_wlan_ap_start(&apinfo);
  367. #else
  368. int ret = luat_wlan_ap_start(&apinfo);
  369. #endif
  370. if (ret)
  371. LLOGD("apstart ret %d", ret);
  372. lua_pushboolean(L, ret == 0 ? 1 : 0);
  373. return 1;
  374. }
  375. /**
  376. 关闭AP功能
  377. @api wlan.stopAP()
  378. @return bool 成功创建返回true,否则返回false
  379. @usage
  380. wlan.stopAP()
  381. */
  382. static int l_wlan_ap_stop(lua_State *L) {
  383. #ifdef LUAT_USE_DRV_WLAN
  384. int ret = luat_drv_wlan_ap_stop();
  385. #else
  386. int ret = luat_wlan_ap_stop();
  387. #endif
  388. if (ret)
  389. LLOGD("apstop ret %d", ret);
  390. lua_pushboolean(L, ret == 0 ? 1 : 0);
  391. return 1;
  392. }
  393. /*
  394. 获取信息,如AP的bssid,信号强度, STA联网后可获取
  395. @api wlan.getInfo()
  396. @return table 详情,键值对形式
  397. @usage
  398. log.info("wlan", "info", json.encode(wlan.getInfo()))
  399. --[[
  400. 典型输出
  401. {
  402. "bssid" : "xxxxxx",
  403. "rssi" : -89,
  404. "gw" : "192.168.1.1"
  405. }
  406. ]]
  407. */
  408. static int l_wlan_get_info(lua_State *L) {
  409. uint8_t buff[48] = {0};
  410. char buff2[32] = {0};
  411. lua_newtable(L);
  412. luat_wlan_get_ap_bssid((char*)buff);
  413. sprintf_(buff2, "%02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  414. lua_pushstring(L, buff2);
  415. lua_setfield(L, -2, "bssid");
  416. memset(buff, 0, 48);
  417. luat_wlan_get_ap_gateway((char*)buff);
  418. lua_pushstring(L, (const char*)buff);
  419. lua_setfield(L, -2, "gw");
  420. lua_pushinteger(L, luat_wlan_get_ap_rssi());
  421. lua_setfield(L, -2, "rssi");
  422. return 1;
  423. }
  424. /*
  425. 读取或设置省电模式
  426. @api wlan.powerSave(mode)
  427. @int 省电模式,可选, 传入就是设置, 例如wlan.PS_NONE
  428. @return int 当前省电模式/设置后的省电模式
  429. @usage
  430. -- 请查阅常量表 PS_NONE/PS_MIN_MODEM/PS_MAX_MODEM
  431. log.info("wlan", "PS", wlan.powerSave(wlan.PS_NONE))
  432. -- 本API于 2023.03.31 新增
  433. */
  434. static int l_wlan_powerSave(lua_State *L) {
  435. int mode = 0;
  436. if (lua_isinteger(L, 1)) {
  437. mode = luaL_checkinteger(L, 1);
  438. luat_wlan_set_ps(mode);
  439. }
  440. mode = luat_wlan_get_ps();
  441. lua_pushinteger(L, mode);
  442. return 1;
  443. }
  444. /*
  445. 读取或设置Hostname
  446. @api wlan.hostname(id, new_name)
  447. @int STA为0, AP为1. 本参数需要2025.2.25及之后编译的固件
  448. @string 新的hostname,可选, 传入就是设置
  449. @return string 当前的hostname或者设置后的hostname
  450. @usage
  451. -- 本API于 2023.07.23 新增
  452. -- 本函数应该在wlan.init之前设置好, 最晚应早于wlan.connect
  453. -- hostname的默认值是 "LUATOS_" + 设备的MAC值
  454. -- 例如: LUATOS_0022EECC2399
  455. -- 老写法, 直接设置STA的hostname
  456. wlan.hostname("我的wifi物联网设备")
  457. -- 新的API, 支持设置STA或AP的hostname, 也可以分别取
  458. wlan.hostname(1, "myhost")
  459. wlan.hostname(0) -- 取STA的hostname
  460. */
  461. static int l_wlan_get_set_hostname(lua_State *L) {
  462. int id = 0;
  463. if (lua_type(L, 1) == LUA_TNUMBER) {
  464. id = luaL_checkinteger(L, 1);
  465. if (lua_type(L, 2) == LUA_TSTRING) {
  466. size_t len = 0;
  467. const char* hostname = luaL_checklstring(L, 2, &len);
  468. if (len > 0) {
  469. if (len > 31) {
  470. LLOGE("hostname is too long");
  471. return 0;
  472. }
  473. luat_wlan_set_hostname(id, hostname);
  474. }
  475. }
  476. }
  477. if (lua_type(L, 1) == LUA_TSTRING) {
  478. size_t len = 0;
  479. const char* hostname = luaL_checklstring(L, 1, &len);
  480. if (len > 0) {
  481. if (len > 31) {
  482. LLOGE("hostname is too long");
  483. return 0;
  484. }
  485. luat_wlan_set_hostname(0, hostname);
  486. }
  487. }
  488. const char* tmp = luat_wlan_get_hostname(id);
  489. lua_pushstring(L, tmp);
  490. return 1;
  491. }
  492. /*
  493. 设置Station模式下的IP获取模式
  494. @api wlan.staIp(dhcp_enable, ip, netmask, gateway)
  495. @bool 是否启用DHCP,默认是true
  496. @string 本机IP地址,例如192.168.2.200, 禁用DHCP时必填
  497. @string 本机IP掩码,例如255.255.255.0, 禁用DHCP时必填
  498. @string 本机IP网关,例如192.168.2.1, 禁用DHCP时必填
  499. @return bool 成功返回true,否则返回false
  500. @usage
  501. -- 本API于 2023.10.06 新增
  502. -- 本函数需要在wlan.init之后才允许调用
  503. -- 启用DHCP, 默认也是启用DHCP,这里是演示API使用
  504. wlan.staIp(true)
  505. -- 禁用DHCP,自行设置IP/掩码/网关
  506. wlan.staIp(false, "192.168.2.200", "255.255.255.0", "192.168.2.1")
  507. */
  508. static int l_wlan_set_sta_ip(lua_State *L) {
  509. luat_wlan_station_info_t info = {
  510. .dhcp_enable = 1
  511. };
  512. const char *data = NULL;
  513. size_t len = 0;
  514. // 是否DHCP
  515. if (lua_isinteger(L, 1))
  516. info.dhcp_enable = luaL_optinteger(L, 1, 1);
  517. else if (lua_isboolean(L, 1))
  518. info.dhcp_enable = lua_toboolean(L, 1);
  519. // 本地IP
  520. data = luaL_optlstring(L, 2, "192.168.1.201", &len);
  521. to_ipv4(data, info.ipv4_addr);
  522. // 掩码
  523. data = luaL_optlstring(L, 3, "255.255.255.0", &len);
  524. to_ipv4(data, info.ipv4_netmask);
  525. // 网关
  526. data = luaL_optlstring(L, 4, "192.168.1.1", &len);
  527. to_ipv4(data, info.ipv4_gateway);
  528. int ret = luat_wlan_set_station_ip(&info);
  529. lua_pushboolean(L, ret == 0 ? 1 : 0);
  530. return 1;
  531. }
  532. #include "rotable2.h"
  533. static const rotable_Reg_t reg_wlan[] =
  534. {
  535. { "init", ROREG_FUNC(l_wlan_init)},
  536. { "scan", ROREG_FUNC(l_wlan_scan)},
  537. { "scanResult", ROREG_FUNC(l_wlan_scan_result)},
  538. #ifndef LUAT_USE_WLAN_SCANONLY
  539. { "mode", ROREG_FUNC(l_wlan_mode)},
  540. { "setMode", ROREG_FUNC(l_wlan_mode)},
  541. { "ready", ROREG_FUNC(l_wlan_ready)},
  542. { "connect", ROREG_FUNC(l_wlan_connect)},
  543. { "disconnect", ROREG_FUNC(l_wlan_disconnect)},
  544. // 配网相关
  545. { "smartconfig", ROREG_FUNC(l_wlan_smartconfig)},
  546. { "getIP", ROREG_FUNC(l_wlan_get_ip)},
  547. { "getInfo", ROREG_FUNC(l_wlan_get_info)},
  548. { "getMac", ROREG_FUNC(l_wlan_get_mac)},
  549. { "setMac", ROREG_FUNC(l_wlan_set_mac)},
  550. { "hostname", ROREG_FUNC(l_wlan_get_set_hostname)},
  551. { "powerSave", ROREG_FUNC(l_wlan_powerSave)},
  552. { "staIp", ROREG_FUNC(l_wlan_set_sta_ip)},
  553. // AP相关
  554. { "createAP", ROREG_FUNC(l_wlan_ap_start)},
  555. { "stopAP", ROREG_FUNC(l_wlan_ap_stop)},
  556. // wifi模式
  557. //@const NONE WLAN模式,停用
  558. {"NONE", ROREG_INT(LUAT_WLAN_MODE_NULL)},
  559. //@const STATION WLAN模式,STATION模式,主动连AP
  560. {"STATION", ROREG_INT(LUAT_WLAN_MODE_STA)},
  561. //@const AP WLAN模式,AP模式,接受STATION连接
  562. {"AP", ROREG_INT(LUAT_WLAN_MODE_AP)},
  563. //@const AP WLAN模式,混合模式
  564. {"STATIONAP", ROREG_INT(LUAT_WLAN_MODE_APSTA)},
  565. // 配网模式
  566. //@const STOP 停止配网
  567. {"STOP", ROREG_INT(LUAT_SC_TYPE_STOP)},
  568. //@const ESPTOUCH esptouch配网, V1
  569. {"ESPTOUCH", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH)},
  570. //@const AIRKISS Airkiss配网, 微信常用
  571. {"AIRKISS", ROREG_INT(LUAT_SC_TYPE_AIRKISS)},
  572. //@const ESPTOUCH_AIRKISS esptouch和Airkiss混合配网
  573. {"ESPTOUCH_AIRKISS", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_AIRKISS)},
  574. //@const ESPTOUCH_V2 esptouch配网, V2, 未测试
  575. {"ESPTOUCH_V2", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_V2)},
  576. //@const PS_NONE 关闭省电模式
  577. {"PS_NONE", ROREG_INT(0)},
  578. //@const PS_MIN_MODEM 最小Modem省电模式
  579. {"PS_MIN_MODEM", ROREG_INT(1)},
  580. //@const PS_MAX_MODEM 最大Modem省电模式
  581. {"PS_MAX_MODEM", ROREG_INT(2)},
  582. #endif
  583. { NULL, ROREG_INT(0)}
  584. };
  585. LUAMOD_API int luaopen_wlan( lua_State *L ) {
  586. luat_newlib2(L, reg_wlan);
  587. return 1;
  588. }