luat_lib_wlan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. #ifdef LUAT_USE_DRV_WLAN
  306. if (id == 2)
  307. id = 0;
  308. else if (id == 3)
  309. id = 1;
  310. else
  311. {
  312. LLOGE("mac parm id error %d", id);
  313. return 0;
  314. }
  315. int ret = luat_drv_wlan_set_mac(id, mac);
  316. // LLOGD("l_wlan_set_mac %02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  317. // LLOGD("set mac result %d", ret);
  318. #else
  319. int ret = luat_wlan_set_mac(id, mac);
  320. #endif
  321. lua_pushboolean(L, ret == 0 ? 1 : 0);
  322. return 1;
  323. }
  324. /*
  325. 获取ip,仅STATION或APSTA模式下有意义
  326. @api wlan.getIP()
  327. @return string ip地址,当前仅返回ipv4地址,例如 "192.168.1.25"
  328. */
  329. static int l_wlan_get_ip(lua_State* L){
  330. char tmpbuff[16] = {0};
  331. #ifdef LUAT_USE_NETDRV
  332. int id = luaL_optinteger(L, 1, 0);
  333. if (id == 0) {
  334. id = NW_ADAPTER_INDEX_LWIP_WIFI_STA;
  335. }
  336. else {
  337. id = NW_ADAPTER_INDEX_LWIP_WIFI_AP;
  338. }
  339. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  340. if (netdrv == NULL ||netdrv->netif == NULL) {
  341. return 0;
  342. }
  343. ipaddr_ntoa_r(&netdrv->netif->ip_addr, tmpbuff, 16);
  344. #else
  345. luat_wlan_get_ip(luaL_optinteger(L, 1, 0), tmpbuff);
  346. #endif
  347. lua_pushstring(L, tmpbuff);
  348. return 1;
  349. }
  350. /*
  351. 启动AP
  352. @api wlan.createAP(ssid, passwd, gateway, netmask, channel, opts)
  353. @string AP的SSID,必填
  354. @string AP的密码,可选
  355. @string AP的网关地址, 默认192.168.4.1
  356. @string AP的网关掩码, 默认255.255.255.0
  357. @int AP建立的通道, 默认6
  358. @table AP的配置选项, 可选
  359. @return bool 成功创建返回true,否则返回false
  360. @usage
  361. -- 注意, 调用本AP时,若wifi模式为STATION,会自动切换成 APSTA
  362. wlan.createAP("luatos1234", "12341234")
  363. -- 设置网关IP,掩码, 通道, 2023.7.13 新增, BSP未必支持
  364. -- wlan.createAP("luatos1234", "12341234", "192.168.4.1", "255.255.255.0", 6)
  365. -- opts更多配置项, 2024.3.5新增
  366. --[[
  367. {
  368. hidden = false, -- 是否隐藏SSID, 默认false,不隐藏
  369. max_conn = 4 -- 最大客户端数量, 默认4
  370. }
  371. ]]
  372. */
  373. #include "lwip/opt.h"
  374. #include "lwip/ip_addr.h"
  375. #include "lwip/netif.h"
  376. static int l_wlan_ap_start(lua_State *L) {
  377. size_t ssid_len = 0;
  378. size_t password_len = 0;
  379. luat_wlan_apinfo_t apinfo = {0};
  380. const char* ssid = luaL_checklstring(L, 1, &ssid_len);
  381. const char* password = luaL_optlstring(L, 2, "", &password_len);
  382. const char* gateway = luaL_optstring(L, 3, "192.168.4.1");
  383. const char* netmask = luaL_optstring(L, 4, "255.255.255.0");
  384. if (strlen(gateway) > 7) {
  385. to_ipv4(gateway, apinfo.gateway);
  386. }
  387. if (strlen(netmask) > 7) {
  388. to_ipv4(netmask, apinfo.netmask);
  389. }
  390. apinfo.channel = (uint8_t)luaL_optinteger(L, 5, 6);
  391. if (ssid_len < 1) {
  392. LLOGE("ssid MUST NOT EMTRY");
  393. return 0;
  394. }
  395. if (ssid_len > 32) {
  396. LLOGE("ssid too long [%s]", ssid);
  397. return 0;
  398. }
  399. if (password_len > 63) {
  400. LLOGE("password too long [%s]", password);
  401. return 0;
  402. }
  403. if (lua_istable(L, 6)) {
  404. lua_getfield(L, 6, "hidden");
  405. apinfo.hidden = lua_toboolean(L, -1);
  406. lua_pop(L, 1);
  407. lua_getfield(L, 6, "max_conn");
  408. apinfo.max_conn = lua_tonumber(L, -1);
  409. lua_pop(L, 1);
  410. }
  411. memcpy(apinfo.ssid, ssid, ssid_len);
  412. memcpy(apinfo.password, password, password_len);
  413. #ifdef LUAT_USE_DRV_WLAN
  414. int ret = luat_drv_wlan_ap_start(&apinfo);
  415. #else
  416. int ret = luat_wlan_ap_start(&apinfo);
  417. #endif
  418. if (ret)
  419. LLOGD("apstart ret %d", ret);
  420. lua_pushboolean(L, ret == 0 ? 1 : 0);
  421. return 1;
  422. }
  423. /**
  424. 关闭AP功能
  425. @api wlan.stopAP()
  426. @return bool 成功创建返回true,否则返回false
  427. @usage
  428. wlan.stopAP()
  429. */
  430. static int l_wlan_ap_stop(lua_State *L) {
  431. #ifdef LUAT_USE_DRV_WLAN
  432. int ret = luat_drv_wlan_ap_stop();
  433. #else
  434. int ret = luat_wlan_ap_stop();
  435. #endif
  436. if (ret)
  437. LLOGD("apstop ret %d", ret);
  438. lua_pushboolean(L, ret == 0 ? 1 : 0);
  439. return 1;
  440. }
  441. /*
  442. 获取信息,如AP的bssid,信号强度, STA联网后可获取
  443. @api wlan.getInfo()
  444. @return table 详情,键值对形式
  445. @usage
  446. log.info("wlan", "info", json.encode(wlan.getInfo()))
  447. --[[
  448. 典型输出
  449. {
  450. "bssid" : "xxxxxx",
  451. "rssi" : -89,
  452. "gw" : "192.168.1.1"
  453. }
  454. ]]
  455. */
  456. static int l_wlan_get_info(lua_State *L) {
  457. uint8_t buff[48] = {0};
  458. char buff2[32] = {0};
  459. int rssi = 0;
  460. lua_newtable(L);
  461. #ifdef LUAT_USE_DRV_WLAN
  462. luat_drv_wlan_get_ap_bssid((char*)buff);
  463. #else
  464. luat_wlan_get_ap_bssid((char*)buff);
  465. #endif
  466. sprintf_(buff2, "%02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  467. lua_pushstring(L, buff2);
  468. lua_setfield(L, -2, "bssid");
  469. memset(buff, 0, 48);
  470. #ifdef LUAT_USE_DRV_WLAN
  471. luat_drv_wlan_get_ap_gateway((char*)buff);
  472. #else
  473. luat_wlan_get_ap_gateway((char*)buff);
  474. #endif
  475. lua_pushstring(L, (const char*)buff);
  476. lua_setfield(L, -2, "gw");
  477. #ifdef LUAT_USE_DRV_WLAN
  478. rssi = luat_drv_wlan_get_ap_rssi();
  479. #else
  480. rssi = luat_wlan_get_ap_rssi();
  481. #endif
  482. lua_pushinteger(L, rssi);
  483. lua_setfield(L, -2, "rssi");
  484. return 1;
  485. }
  486. /*
  487. 读取或设置省电模式
  488. @api wlan.powerSave(mode)
  489. @int 省电模式,可选, 传入就是设置, 例如wlan.PS_NONE
  490. @return int 当前省电模式/设置后的省电模式
  491. @usage
  492. -- 请查阅常量表 PS_NONE/PS_MIN_MODEM/PS_MAX_MODEM
  493. log.info("wlan", "PS", wlan.powerSave(wlan.PS_NONE))
  494. -- 本API于 2023.03.31 新增
  495. */
  496. static int l_wlan_powerSave(lua_State *L) {
  497. int mode = 0;
  498. if (lua_isinteger(L, 1)) {
  499. mode = luaL_checkinteger(L, 1);
  500. luat_wlan_set_ps(mode);
  501. }
  502. mode = luat_wlan_get_ps();
  503. lua_pushinteger(L, mode);
  504. return 1;
  505. }
  506. /*
  507. 读取或设置Hostname
  508. @api wlan.hostname(id, new_name)
  509. @int STA为0, AP为1. 本参数需要2025.2.25及之后编译的固件
  510. @string 新的hostname,可选, 传入就是设置
  511. @return string 当前的hostname或者设置后的hostname
  512. @usage
  513. -- 本API于 2023.07.23 新增
  514. -- 本函数应该在wlan.init之前设置好, 最晚应早于wlan.connect
  515. -- hostname的默认值是 "LUATOS_" + 设备的MAC值
  516. -- 例如: LUATOS_0022EECC2399
  517. -- 老写法, 直接设置STA的hostname
  518. wlan.hostname("我的wifi物联网设备")
  519. -- 新的API, 支持设置STA或AP的hostname, 也可以分别取
  520. wlan.hostname(1, "myhost")
  521. wlan.hostname(0) -- 取STA的hostname
  522. */
  523. static int l_wlan_get_set_hostname(lua_State *L) {
  524. int id = 0;
  525. if (lua_type(L, 1) == LUA_TNUMBER) {
  526. id = luaL_checkinteger(L, 1);
  527. if (lua_type(L, 2) == LUA_TSTRING) {
  528. size_t len = 0;
  529. const char* hostname = luaL_checklstring(L, 2, &len);
  530. if (len > 0) {
  531. if (len > 31) {
  532. LLOGE("hostname is too long");
  533. return 0;
  534. }
  535. luat_wlan_set_hostname(id, hostname);
  536. }
  537. }
  538. }
  539. if (lua_type(L, 1) == LUA_TSTRING) {
  540. size_t len = 0;
  541. const char* hostname = luaL_checklstring(L, 1, &len);
  542. if (len > 0) {
  543. if (len > 31) {
  544. LLOGE("hostname is too long");
  545. return 0;
  546. }
  547. luat_wlan_set_hostname(0, hostname);
  548. }
  549. }
  550. const char* tmp = luat_wlan_get_hostname(id);
  551. lua_pushstring(L, tmp);
  552. return 1;
  553. }
  554. /*
  555. 设置Station模式下的IP获取模式
  556. @api wlan.staIp(dhcp_enable, ip, netmask, gateway)
  557. @bool 是否启用DHCP,默认是true
  558. @string 本机IP地址,例如192.168.2.200, 禁用DHCP时必填
  559. @string 本机IP掩码,例如255.255.255.0, 禁用DHCP时必填
  560. @string 本机IP网关,例如192.168.2.1, 禁用DHCP时必填
  561. @return bool 成功返回true,否则返回false
  562. @usage
  563. -- 本API于 2023.10.06 新增
  564. -- 本函数需要在wlan.init之后才允许调用
  565. -- 启用DHCP, 默认也是启用DHCP,这里是演示API使用
  566. wlan.staIp(true)
  567. -- 禁用DHCP,自行设置IP/掩码/网关
  568. wlan.staIp(false, "192.168.2.200", "255.255.255.0", "192.168.2.1")
  569. */
  570. static int l_wlan_set_sta_ip(lua_State *L) {
  571. luat_wlan_station_info_t info = {
  572. .dhcp_enable = 1
  573. };
  574. const char *data = NULL;
  575. size_t len = 0;
  576. // 是否DHCP
  577. if (lua_isinteger(L, 1))
  578. info.dhcp_enable = luaL_optinteger(L, 1, 1);
  579. else if (lua_isboolean(L, 1))
  580. info.dhcp_enable = lua_toboolean(L, 1);
  581. // 本地IP
  582. data = luaL_optlstring(L, 2, "192.168.1.201", &len);
  583. to_ipv4(data, info.ipv4_addr);
  584. // 掩码
  585. data = luaL_optlstring(L, 3, "255.255.255.0", &len);
  586. to_ipv4(data, info.ipv4_netmask);
  587. // 网关
  588. data = luaL_optlstring(L, 4, "192.168.1.1", &len);
  589. to_ipv4(data, info.ipv4_gateway);
  590. int ret = luat_wlan_set_station_ip(&info);
  591. lua_pushboolean(L, ret == 0 ? 1 : 0);
  592. return 1;
  593. }
  594. #include "rotable2.h"
  595. static const rotable_Reg_t reg_wlan[] =
  596. {
  597. { "init", ROREG_FUNC(l_wlan_init)},
  598. { "scan", ROREG_FUNC(l_wlan_scan)},
  599. { "scanResult", ROREG_FUNC(l_wlan_scan_result)},
  600. #ifndef LUAT_USE_WLAN_SCANONLY
  601. { "mode", ROREG_FUNC(l_wlan_mode)},
  602. { "setMode", ROREG_FUNC(l_wlan_mode)},
  603. { "ready", ROREG_FUNC(l_wlan_ready)},
  604. { "connect", ROREG_FUNC(l_wlan_connect)},
  605. { "disconnect", ROREG_FUNC(l_wlan_disconnect)},
  606. // 配网相关
  607. { "smartconfig", ROREG_FUNC(l_wlan_smartconfig)},
  608. { "getIP", ROREG_FUNC(l_wlan_get_ip)},
  609. { "getInfo", ROREG_FUNC(l_wlan_get_info)},
  610. { "getMac", ROREG_FUNC(l_wlan_get_mac)},
  611. { "setMac", ROREG_FUNC(l_wlan_set_mac)},
  612. { "hostname", ROREG_FUNC(l_wlan_get_set_hostname)},
  613. { "powerSave", ROREG_FUNC(l_wlan_powerSave)},
  614. { "staIp", ROREG_FUNC(l_wlan_set_sta_ip)},
  615. // AP相关
  616. { "createAP", ROREG_FUNC(l_wlan_ap_start)},
  617. { "stopAP", ROREG_FUNC(l_wlan_ap_stop)},
  618. // wifi模式
  619. //@const NONE WLAN模式,停用
  620. {"NONE", ROREG_INT(LUAT_WLAN_MODE_NULL)},
  621. //@const STATION WLAN模式,STATION模式,主动连AP
  622. {"STATION", ROREG_INT(LUAT_WLAN_MODE_STA)},
  623. //@const AP WLAN模式,AP模式,接受STATION连接
  624. {"AP", ROREG_INT(LUAT_WLAN_MODE_AP)},
  625. //@const AP WLAN模式,混合模式
  626. {"STATIONAP", ROREG_INT(LUAT_WLAN_MODE_APSTA)},
  627. // 配网模式
  628. //@const STOP 停止配网
  629. {"STOP", ROREG_INT(LUAT_SC_TYPE_STOP)},
  630. //@const ESPTOUCH esptouch配网, V1
  631. {"ESPTOUCH", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH)},
  632. //@const AIRKISS Airkiss配网, 微信常用
  633. {"AIRKISS", ROREG_INT(LUAT_SC_TYPE_AIRKISS)},
  634. //@const ESPTOUCH_AIRKISS esptouch和Airkiss混合配网
  635. {"ESPTOUCH_AIRKISS", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_AIRKISS)},
  636. //@const ESPTOUCH_V2 esptouch配网, V2, 未测试
  637. {"ESPTOUCH_V2", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_V2)},
  638. //@const PS_NONE 关闭省电模式
  639. {"PS_NONE", ROREG_INT(0)},
  640. //@const PS_MIN_MODEM 最小Modem省电模式
  641. {"PS_MIN_MODEM", ROREG_INT(1)},
  642. //@const PS_MAX_MODEM 最大Modem省电模式
  643. {"PS_MAX_MODEM", ROREG_INT(2)},
  644. #endif
  645. { NULL, ROREG_INT(0)}
  646. };
  647. LUAMOD_API int luaopen_wlan( lua_State *L ) {
  648. luat_newlib2(L, reg_wlan);
  649. return 1;
  650. }