luat_lib_wlan.c 16 KB

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