luat_lib_wlan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. /*
  17. 初始化
  18. @api wlan.init()
  19. @return bool 成功返回true,否则返回false
  20. */
  21. static int l_wlan_init(lua_State* L){
  22. int ret = luat_wlan_init(NULL);
  23. lua_pushboolean(L, ret == 0 ? 1 : 0);
  24. return 1;
  25. }
  26. /*
  27. 设置wifi模式
  28. @api wlan.setMode(mode)
  29. @int wifi模式
  30. @return bool 成功返回true,否则返回false
  31. @usage
  32. -- 设置为AP模式, 广播ssid, 接收wifi客户端的链接
  33. wlan.setMode(wlan.AP)
  34. -- 设置为STATION模式, 也是初始化后的默认模式
  35. wlan.setMode(wlan.STATION)
  36. -- 混合模式, 做AP又做STATION
  37. wlan.setMode(wlan.APSTA)
  38. */
  39. static int l_wlan_mode(lua_State* L){
  40. int mode = LUAT_WLAN_MODE_STA;
  41. if (lua_isinteger(L, 1)) {
  42. mode = lua_tointeger(L, 1);
  43. }
  44. else if (lua_isinteger(L, 2)) {
  45. mode = lua_tointeger(L, 2);
  46. }
  47. if (mode <= LUAT_WLAN_MODE_NULL || mode >= LUAT_WLAN_MODE_MAX) {
  48. mode = LUAT_WLAN_MODE_STA;
  49. }
  50. // switch (mode)
  51. // {
  52. // case LUAT_WLAN_MODE_NULL:
  53. // LLOGD("wlan mode NULL");
  54. // break;
  55. // case LUAT_WLAN_MODE_STA:
  56. // LLOGD("wlan mode STATION");
  57. // break;
  58. // case LUAT_WLAN_MODE_AP:
  59. // LLOGD("wlan mode AP");
  60. // break;
  61. // case LUAT_WLAN_MODE_APSTA:
  62. // LLOGD("wlan mode AP-STATION");
  63. // break;
  64. // default:
  65. // break;
  66. // }
  67. luat_wlan_config_t conf = {
  68. .mode = mode
  69. };
  70. int ret = luat_wlan_mode(&conf);
  71. lua_pushboolean(L, ret == 0 ? 1 : 0);
  72. return 1;
  73. }
  74. /*
  75. 作为STATION时,是否已经连接上AP,且获取IP成功
  76. @api wlan.ready()
  77. @return bool 已经连接成功返回true,否则返回false
  78. */
  79. static int l_wlan_ready(lua_State* L){
  80. lua_pushboolean(L, luat_wlan_ready());
  81. return 1;
  82. }
  83. /*
  84. 作为STATION时,连接到指定AP
  85. @api wlan.connect(ssid, password)
  86. @string AP的ssid
  87. @string AP的password,可选
  88. @return bool 发起连接成功返回true,否则返回false.注意,不代表连接AP成功!!
  89. @usage
  90. -- 普通模式,带密码
  91. wlan.connect("myap", "12345678")
  92. -- 普通模式,不带密码
  93. wlan.connect("myap")
  94. -- 特殊模式, 重用之前的ssid和密码,本次直接连接
  95. -- 注意, 前提是本次上电后已经传过ssid和或password,否则必失败
  96. wlan.connect()
  97. */
  98. static int l_wlan_connect(lua_State* L){
  99. const char* ssid = luaL_optstring(L, 1, "");
  100. const char* password = luaL_optstring(L, 2, "");
  101. luat_wlan_conninfo_t info = {0};
  102. info.auto_reconnection = 1;
  103. memcpy(info.ssid, ssid, strlen(ssid));
  104. memcpy(info.password, password, strlen(password));
  105. int ret = luat_wlan_connect(&info);
  106. lua_pushboolean(L, ret == 0 ? 1 : 0);
  107. return 1;
  108. }
  109. /*
  110. 作为STATION时,断开AP
  111. @api wlan.disconnect()
  112. */
  113. static int l_wlan_disconnect(lua_State* L){
  114. (void)L;
  115. luat_wlan_disconnect();
  116. return 0;
  117. }
  118. /*
  119. 扫描wifi频段
  120. @api wlan.scan()
  121. @usage
  122. -- 注意, wlan.scan()是异步API,启动扫描后会马上返回
  123. -- wifi扫描成功后, 会有WLAN_SCAN_DONE消息, 读取即可
  124. sys.subscribe("WLAN_SCAN_DONE", function ()
  125. local results = wlan.scanResult()
  126. log.info("scan", "results", #results)
  127. for k,v in pairs(results) do
  128. log.info("scan", v["ssid"], v["rssi"], (v["bssid"]:toHex()))
  129. end
  130. end)
  131. -- 下面演示的是初始化wifi后定时扫描,请按实际业务需求修改
  132. sys.taskInit(function()
  133. sys.wait(1000)
  134. wlan.init()
  135. while 1 do
  136. wlan.scan()
  137. sys.wait(15000)
  138. end
  139. end)
  140. */
  141. static int l_wlan_scan(lua_State* L){
  142. (void)L;
  143. luat_wlan_scan();
  144. return 0;
  145. }
  146. /*
  147. 获取wifi扫描结果
  148. @api wlan.scanResult()
  149. @return table 扫描结果
  150. @usage
  151. -- 用法请查阅 wlan.scan() 函数
  152. */
  153. static int l_wlan_scan_result(lua_State* L) {
  154. int ap_limit = luaL_optinteger(L, 1, 20);
  155. if (ap_limit > 32)
  156. ap_limit = 32;
  157. else if (ap_limit < 8)
  158. ap_limit = 8;
  159. lua_newtable(L);
  160. luat_wlan_scan_result_t *results = luat_heap_malloc(sizeof(luat_wlan_scan_result_t) * ap_limit);
  161. if (results == NULL) {
  162. LLOGE("out of memory when malloc scan result");
  163. return 1;
  164. }
  165. memset(results, 0, sizeof(luat_wlan_scan_result_t) * ap_limit);
  166. int len = luat_wlan_scan_get_result(results, ap_limit);
  167. for (int i = 0; i < len; i++)
  168. {
  169. lua_newtable(L);
  170. lua_pushstring(L, (const char *)results[i].ssid);
  171. lua_setfield(L, -2, "ssid");
  172. // lua_pushfstring(L, "%02X%02X%02X%02X%02X%02X", results[i].bssid[0],
  173. // results[i].bssid[1],
  174. // results[i].bssid[2],
  175. // results[i].bssid[3],
  176. // results[i].bssid[4],
  177. // results[i].bssid[5]);
  178. lua_pushlstring(L, (const char *)results[i].bssid, 6);
  179. lua_setfield(L, -2, "bssid");
  180. lua_pushinteger(L, results[i].ch);
  181. lua_setfield(L, -2, "channel");
  182. lua_pushinteger(L, results[i].rssi);
  183. lua_setfield(L, -2, "rssi");
  184. lua_seti(L, -2, i + 1);
  185. }
  186. luat_heap_free(results);
  187. return 1;
  188. }
  189. /*
  190. 配网
  191. @api wlan.smartconfig(mode)
  192. @int 配网模式, 默认为esptouch, 若传0则主动停止配网
  193. @return bool 启动成功或停止成功, 返回true, 否则返回false
  194. @usage
  195. wlan.smartconfig()
  196. local ret, ssid, passwd = sys.waitUntil("SC_RESULT", 180*1000) -- 最多等3分钟
  197. log.info("sc", ret, ssid, passwd)
  198. -- 详细用法请查看demo
  199. */
  200. static int l_wlan_smartconfig(lua_State *L) {
  201. int tp = luaL_optinteger(L, 1, LUAT_SC_TYPE_ESPTOUCH);
  202. if (tp == LUAT_SC_TYPE_STOP) {
  203. luat_wlan_smartconfig_stop();
  204. lua_pushboolean(L, 1);
  205. }
  206. else {
  207. int ret = luat_wlan_smartconfig_start(tp);
  208. lua_pushboolean(L, ret == 0 ? 1 : 0);
  209. }
  210. return 1;
  211. }
  212. /*
  213. 获取mac
  214. @api wlan.getMac(tp, hexstr)
  215. @int 设置何种mac地址,对ESP32系列来说,只能设置STA的地址,即0,默认值也是0
  216. @bool 是否转HEX字符, 默认是true,即输出hex字符串
  217. @return string MAC地址,十六进制字符串形式 "AABBCCDDEEFF" 或原始数据
  218. log.info("wlan mac", wlan.getMac())
  219. */
  220. static int l_wlan_get_mac(lua_State* L){
  221. char tmp[6] = {0};
  222. char tmpbuff[16] = {0};
  223. luat_wlan_get_mac(luaL_optinteger(L, 1, 0), tmp);
  224. if (lua_isboolean(L, 2) && !lua_toboolean(L, 2)) {
  225. lua_pushlstring(L, tmp, 6);
  226. }
  227. else {
  228. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]);
  229. lua_pushstring(L, tmpbuff);
  230. }
  231. return 1;
  232. }
  233. /*
  234. 设置mac
  235. @api wlan.setMac(tp, mac)
  236. @int 设置何种mac地址,对ESP32系列来说,只能设置STA的地址,即0
  237. @string 待设置的MAC地址,长度6字节
  238. @return bool 成功返回true,否则返回false
  239. @usage
  240. -- 设置MAC地址, 2023-03-01之后编译的固件可用
  241. local mac = string.fromHex("F01122334455")
  242. wlan.setMac(0, mac)
  243. */
  244. static int l_wlan_set_mac(lua_State* L){
  245. // int id = luaL_optinteger(L, 1, 0);
  246. const char* mac = luaL_checkstring(L, 2);
  247. int ret = luat_wlan_set_mac(luaL_optinteger(L, 1, 0), mac);
  248. lua_pushboolean(L, ret == 0 ? 1 : 0);
  249. return 1;
  250. }
  251. /*
  252. 获取ip,仅STATION或APSTA模式下有意义
  253. @api wlan.getIP()
  254. @return string ip地址,当前仅返回ipv4地址,例如 "192.168.1.25"
  255. */
  256. static int l_wlan_get_ip(lua_State* L){
  257. char tmpbuff[16] = {0};
  258. luat_wlan_get_ip(luaL_optinteger(L, 1, 0), tmpbuff);
  259. lua_pushstring(L, tmpbuff);
  260. return 1;
  261. }
  262. /*
  263. 启动AP
  264. @api wlan.createAP(ssid, passwd, gateway, netmask, channel)
  265. @string AP的SSID,必填
  266. @string AP的密码,可选
  267. @string AP的网关地址, 默认192.168.4.1
  268. @string AP的网关掩码, 默认255.255.255.0
  269. @int AP建立的通道, 默认6
  270. @return bool 成功创建返回true,否则返回false
  271. @usage
  272. -- 注意, 调用本AP时,若wifi模式为STATION,会自动切换成 APSTA
  273. wlan.createAP("uiot", "12345678")
  274. -- 设置网关IP,掩码, 通道, 2023.7.13 新增, BSP未必支持
  275. -- wlan.createAP("uiot", "12345678", "192.168.4.1", "255.255.255.0", 6)
  276. */
  277. #include "lwip/opt.h"
  278. #include "lwip/ip_addr.h"
  279. #include "lwip/netif.h"
  280. static int l_wlan_ap_start(lua_State *L) {
  281. size_t ssid_len = 0;
  282. size_t password_len = 0;
  283. luat_wlan_apinfo_t apinfo = {0};
  284. const char* ssid = luaL_checklstring(L, 1, &ssid_len);
  285. const char* password = luaL_optlstring(L, 2, "", &password_len);
  286. const char* gateway = luaL_optstring(L, 3, "192.168.4.1");
  287. const char* netmask = luaL_optstring(L, 4, "255.255.255.0");
  288. if (strlen(gateway) > 7) {
  289. uint32_t tmpip = ipaddr_addr(gateway);
  290. apinfo.gateway[3] = (tmpip >> 24) & 0xFF;
  291. apinfo.gateway[2] = (tmpip >> 16) & 0xFF;
  292. apinfo.gateway[1] = (tmpip >> 8) & 0xFF;
  293. apinfo.gateway[0] = (tmpip >> 0) & 0xFF;
  294. }
  295. if (strlen(netmask) > 7) {
  296. uint32_t tmpip = ipaddr_addr(netmask);
  297. apinfo.netmask[3] = (tmpip >> 24) & 0xFF;
  298. apinfo.netmask[2] = (tmpip >> 16) & 0xFF;
  299. apinfo.netmask[1] = (tmpip >> 8) & 0xFF;
  300. apinfo.netmask[0] = (tmpip >> 0) & 0xFF;
  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. #include "rotable2.h"
  415. static const rotable_Reg_t reg_wlan[] =
  416. {
  417. { "init", ROREG_FUNC(l_wlan_init)},
  418. { "scan", ROREG_FUNC(l_wlan_scan)},
  419. { "scanResult", ROREG_FUNC(l_wlan_scan_result)},
  420. #ifndef LUAT_USE_WLAN_SCANONLY
  421. { "mode", ROREG_FUNC(l_wlan_mode)},
  422. { "setMode", ROREG_FUNC(l_wlan_mode)},
  423. { "ready", ROREG_FUNC(l_wlan_ready)},
  424. { "connect", ROREG_FUNC(l_wlan_connect)},
  425. { "disconnect", ROREG_FUNC(l_wlan_disconnect)},
  426. // 配网相关
  427. { "smartconfig", ROREG_FUNC(l_wlan_smartconfig)},
  428. { "getIP", ROREG_FUNC(l_wlan_get_ip)},
  429. { "getInfo", ROREG_FUNC(l_wlan_get_info)},
  430. { "getMac", ROREG_FUNC(l_wlan_get_mac)},
  431. { "setMac", ROREG_FUNC(l_wlan_set_mac)},
  432. { "hostname", ROREG_FUNC(l_wlan_get_set_hostname)},
  433. { "powerSave", ROREG_FUNC(l_wlan_powerSave)},
  434. // AP相关
  435. { "createAP", ROREG_FUNC(l_wlan_ap_start)},
  436. { "stopAP", ROREG_FUNC(l_wlan_ap_stop)},
  437. // wifi模式
  438. //@const NONE WLAN模式,停用
  439. {"NONE", ROREG_INT(LUAT_WLAN_MODE_NULL)},
  440. //@const STATION WLAN模式,STATION模式,主动连AP
  441. {"STATION", ROREG_INT(LUAT_WLAN_MODE_STA)},
  442. //@const AP WLAN模式,AP模式,接受STATION连接
  443. {"AP", ROREG_INT(LUAT_WLAN_MODE_AP)},
  444. //@const AP WLAN模式,混合模式
  445. {"STATIONAP", ROREG_INT(LUAT_WLAN_MODE_APSTA)},
  446. // 配网模式
  447. //@const STOP 停止配网
  448. {"STOP", ROREG_INT(LUAT_SC_TYPE_STOP)},
  449. //@const ESPTOUCH esptouch配网, V1
  450. {"ESPTOUCH", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH)},
  451. //@const AIRKISS Airkiss配网, 微信常用
  452. {"AIRKISS", ROREG_INT(LUAT_SC_TYPE_AIRKISS)},
  453. //@const ESPTOUCH_AIRKISS esptouch和Airkiss混合配网
  454. {"ESPTOUCH_AIRKISS", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_AIRKISS)},
  455. //@const ESPTOUCH_V2 esptouch配网, V2, 未测试
  456. {"ESPTOUCH_V2", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_V2)},
  457. //@const PS_NONE 关闭省电模式
  458. {"PS_NONE", ROREG_INT(0)},
  459. //@const PS_MIN_MODEM 最小Modem省电模式
  460. {"PS_MIN_MODEM", ROREG_INT(1)},
  461. //@const PS_MAX_MODEM 最大Modem省电模式
  462. {"PS_MAX_MODEM", ROREG_INT(2)},
  463. #endif
  464. { NULL, ROREG_INT(0)}
  465. };
  466. LUAMOD_API int luaopen_wlan( lua_State *L ) {
  467. luat_newlib2(L, reg_wlan);
  468. return 1;
  469. }