luat_lib_wlan.c 21 KB

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