luat_lib_wlan.c 19 KB

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