luat_lib_wlan.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. luat_wlan_disconnect();
  115. return 0;
  116. }
  117. /*
  118. 扫描wifi频段
  119. @api wlan.scan()
  120. @usage
  121. -- 注意, wlan.scan()是异步API,启动扫描后会马上返回
  122. -- wifi扫描成功后, 会有WLAN_SCAN_DONE消息, 读取即可
  123. sys.subscribe("WLAN_SCAN_DONE", function ()
  124. local results = wlan.scanResult()
  125. log.info("scan", "results", #results)
  126. for k,v in pairs(results) do
  127. log.info("scan", v["ssid"], v["rssi"], (v["bssid"]:toHex()))
  128. end
  129. end)
  130. -- 下面演示的是初始化wifi后定时扫描,请按实际业务需求修改
  131. sys.taskInit(function()
  132. sys.wait(1000)
  133. wlan.init()
  134. while 1 do
  135. wlan.scan()
  136. sys.wait(15000)
  137. end
  138. end)
  139. */
  140. static int l_wlan_scan(lua_State* L){
  141. luat_wlan_scan();
  142. return 0;
  143. }
  144. /*
  145. 获取wifi扫描结果
  146. @api wlan.scanResult()
  147. @return table 扫描结果
  148. @usage
  149. -- 用法请查阅 wlan.scan() 函数
  150. */
  151. static int l_wlan_scan_result(lua_State* L) {
  152. int ap_limit = luaL_optinteger(L, 1, 20);
  153. if (ap_limit > 32)
  154. ap_limit = 32;
  155. else if (ap_limit < 8)
  156. ap_limit = 8;
  157. lua_newtable(L);
  158. luat_wlan_scan_result_t *results = luat_heap_malloc(sizeof(luat_wlan_scan_result_t) * ap_limit);
  159. if (results == NULL) {
  160. LLOGE("out of memory when malloc scan result");
  161. return 1;
  162. }
  163. memset(results, 0, sizeof(luat_wlan_scan_result_t) * ap_limit);
  164. int len = luat_wlan_scan_get_result(results, ap_limit);
  165. for (size_t i = 0; i < len; i++)
  166. {
  167. lua_newtable(L);
  168. lua_pushstring(L, (const char *)results[i].ssid);
  169. lua_setfield(L, -2, "ssid");
  170. // lua_pushfstring(L, "%02X%02X%02X%02X%02X%02X", results[i].bssid[0],
  171. // results[i].bssid[1],
  172. // results[i].bssid[2],
  173. // results[i].bssid[3],
  174. // results[i].bssid[4],
  175. // results[i].bssid[5]);
  176. lua_pushlstring(L, (const char *)results[i].bssid, 6);
  177. lua_setfield(L, -2, "bssid");
  178. lua_pushinteger(L, results[i].ch);
  179. lua_setfield(L, -2, "channel");
  180. lua_pushinteger(L, results[i].rssi);
  181. lua_setfield(L, -2, "rssi");
  182. lua_seti(L, -2, i + 1);
  183. }
  184. luat_heap_free(results);
  185. return 1;
  186. }
  187. /*
  188. 配网
  189. @api wlan.smartconfig(mode)
  190. @int 配网模式, 默认为esptouch, 若传0则主动停止配网
  191. @return bool 启动成功或停止成功, 返回true, 否则返回false
  192. @usage
  193. wlan.smartconfig()
  194. local ret, ssid, passwd = sys.waitUntil("SC_RESULT", 180*1000) -- 最多等3分钟
  195. log.info("sc", ret, ssid, passwd)
  196. -- 详细用法请查看demo
  197. */
  198. static int l_wlan_smartconfig(lua_State *L) {
  199. int tp = luaL_optinteger(L, 1, LUAT_SC_TYPE_ESPTOUCH);
  200. if (tp == LUAT_SC_TYPE_STOP) {
  201. luat_wlan_smartconfig_stop();
  202. lua_pushboolean(L, 1);
  203. }
  204. else {
  205. int ret = luat_wlan_smartconfig_start(tp);
  206. lua_pushboolean(L, ret == 0 ? 1 : 0);
  207. }
  208. return 1;
  209. }
  210. /*
  211. 获取mac
  212. @api wlan.getMac()
  213. @return string MAC地址,十六进制字符串形式 "AABBCCDDEEFF"
  214. */
  215. static int l_wlan_get_mac(lua_State* L){
  216. char tmp[6] = {0};
  217. char tmpbuff[16] = {0};
  218. luat_wlan_get_mac(luaL_optinteger(L, 1, 0), tmp);
  219. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]);
  220. lua_pushstring(L, tmpbuff);
  221. return 1;
  222. }
  223. /*
  224. 设置mac
  225. @api wlan.setMac(tp, mac)
  226. @int 设置何种mac地址,对ESP32系列来说,只能设置STA的地址,即0
  227. @string 待设置的MAC地址
  228. @return bool 成功返回true,否则返回false
  229. @usage
  230. -- 设置MAC地址, 2023-03-01之后编译的固件可用
  231. local mac = string.fromHex("F01122334455")
  232. wlan.setMac(0, mac)
  233. */
  234. static int l_wlan_set_mac(lua_State* L){
  235. // int id = luaL_optinteger(L, 1, 0);
  236. const char* mac = luaL_checkstring(L, 2);
  237. int ret = luat_wlan_set_mac(luaL_optinteger(L, 1, 0), mac);
  238. lua_pushboolean(L, ret == 0 ? 1 : 0);
  239. return 1;
  240. }
  241. /*
  242. 获取ip,仅STATION或APSTA模式下有意义
  243. @api wlan.getIP()
  244. @return string ip地址,当前仅返回ipv4地址,例如 "192.168.1.25"
  245. */
  246. static int l_wlan_get_ip(lua_State* L){
  247. char tmpbuff[16] = {0};
  248. luat_wlan_get_ip(luaL_optinteger(L, 1, 0), tmpbuff);
  249. lua_pushstring(L, tmpbuff);
  250. return 1;
  251. }
  252. /*
  253. 启动AP
  254. @api wlan.createAP(ssid, passwd, gateway, netmask, channel)
  255. @string AP的SSID,必填
  256. @string AP的密码,可选
  257. @string AP的网关地址, 默认192.168.4.1
  258. @string AP的网关掩码, 默认255.255.255.0
  259. @int AP建立的通道, 默认6
  260. @return bool 成功创建返回true,否则返回false
  261. @usage
  262. -- 注意, 调用本AP时,若wifi模式为STATION,会自动切换成 APSTA
  263. wlan.createAP("uiot", "12345678")
  264. -- 设置网关IP,掩码, 通道, 2023.7.13 新增, BSP未必支持
  265. -- wlan.createAP("uiot", "12345678", "192.168.4.1", "255.255.255.0", 6)
  266. */
  267. #include "lwip/opt.h"
  268. #include "lwip/ip_addr.h"
  269. #include "lwip/netif.h"
  270. static int l_wlan_ap_start(lua_State *L) {
  271. size_t ssid_len = 0;
  272. size_t password_len = 0;
  273. luat_wlan_apinfo_t apinfo = {0};
  274. const char* ssid = luaL_checklstring(L, 1, &ssid_len);
  275. const char* password = luaL_optlstring(L, 2, "", &password_len);
  276. const char* gateway = luaL_optstring(L, 3, "192.168.4.1");
  277. const char* netmask = luaL_optstring(L, 4, "255.255.255.0");
  278. if (strlen(gateway) > 7) {
  279. uint32_t tmpip = ipaddr_addr(gateway);
  280. apinfo.gateway[3] = (tmpip >> 24) & 0xFF;
  281. apinfo.gateway[2] = (tmpip >> 16) & 0xFF;
  282. apinfo.gateway[1] = (tmpip >> 8) & 0xFF;
  283. apinfo.gateway[0] = (tmpip >> 0) & 0xFF;
  284. }
  285. if (strlen(netmask) > 7) {
  286. uint32_t tmpip = ipaddr_addr(netmask);
  287. apinfo.netmask[3] = (tmpip >> 24) & 0xFF;
  288. apinfo.netmask[2] = (tmpip >> 16) & 0xFF;
  289. apinfo.netmask[1] = (tmpip >> 8) & 0xFF;
  290. apinfo.netmask[0] = (tmpip >> 0) & 0xFF;
  291. }
  292. apinfo.channel = (uint8_t)luaL_optinteger(L, 5, 6);
  293. if (ssid_len < 1) {
  294. LLOGE("ssid MUST NOT EMTRY");
  295. return 0;
  296. }
  297. if (ssid_len > 32) {
  298. LLOGE("ssid too long [%s]", ssid);
  299. return 0;
  300. }
  301. if (password_len > 63) {
  302. LLOGE("password too long [%s]", password);
  303. return 0;
  304. }
  305. memcpy(apinfo.ssid, ssid, ssid_len);
  306. memcpy(apinfo.password, password, password_len);
  307. int ret = luat_wlan_ap_start(&apinfo);
  308. LLOGD("apstart ret %d", ret);
  309. lua_pushboolean(L, ret == 0 ? 1 : 0);
  310. return 1;
  311. }
  312. /*
  313. 获取信息,如AP的bssid,信号强度
  314. @api wlan.getInfo()
  315. @return table 详情,键值对形式
  316. @usage
  317. log.info("wlan", "info", json.encode(wlan.getInfo()))
  318. --[[
  319. 典型输出
  320. {
  321. "bssid" : "xxxxxx",
  322. "rssi" : -89,
  323. "gw" : "192.168.1.1"
  324. }
  325. ]]
  326. */
  327. static int l_wlan_get_info(lua_State *L) {
  328. char buff[48] = {0};
  329. char buff2[32] = {0};
  330. lua_newtable(L);
  331. luat_wlan_get_ap_bssid(buff);
  332. sprintf_(buff2, "%02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  333. lua_pushstring(L, buff2);
  334. lua_setfield(L, -2, "bssid");
  335. memset(buff, 0, 48);
  336. luat_wlan_get_ap_gateway(buff);
  337. lua_pushstring(L, buff);
  338. lua_setfield(L, -2, "gw");
  339. lua_pushinteger(L, luat_wlan_get_ap_rssi());
  340. lua_setfield(L, -2, "rssi");
  341. return 1;
  342. }
  343. /*
  344. 读取或设置省电模式
  345. @api wlan.powerSave(mode)
  346. @int 省电模式,可选, 传入就是设置, 例如wlan.PS_NONE
  347. @return int 当前省电模式/设置后的省电模式
  348. @usage
  349. -- 请查阅常量表 PS_NONE/PS_MIN_MODEM/PS_MAX_MODEM
  350. log.info("wlan", "PS", wlan.powerSave(wlan.PS_NONE))
  351. -- 本API于 2023.03.31 新增
  352. */
  353. static int l_wlan_powerSave(lua_State *L) {
  354. int mode = 0;
  355. if (lua_isinteger(L, 1)) {
  356. mode = luaL_checkinteger(L, 1);
  357. luat_wlan_set_ps(mode);
  358. }
  359. mode = luat_wlan_get_ps();
  360. lua_pushinteger(L, mode);
  361. return 1;
  362. }
  363. /*
  364. 读取或设置Hostname
  365. @api wlan.hostname(new_name)
  366. @string 新的hostname,可选, 传入就是设置
  367. @return string 当前的hostname或者设置后的hostname
  368. @usage
  369. -- 本API于 2023.07.23 新增
  370. -- 本函数应该在wlan.init之前设置好, 最晚应早于wlan.connect
  371. -- hostname的默认值是 "LUATOS_" + 设备的MAC值
  372. -- 例如: LUATOS_0022EECC2399
  373. wlan.hostname("我的wifi物联网设备")
  374. */
  375. static int l_wlan_get_set_hostname(lua_State *L) {
  376. if (lua_isstring(L, 1)) {
  377. size_t len = 0;
  378. const char* hostname = luaL_checklstring(L, 1, &len);
  379. if (len > 0) {
  380. if (len > 31) {
  381. LLOGE("hostname is too long");
  382. return 0;
  383. }
  384. luat_wlan_set_hostname(0, hostname);
  385. }
  386. }
  387. const char* tmp = luat_wlan_get_hostname(0);
  388. lua_pushstring(L, tmp);
  389. return 1;
  390. }
  391. #include "rotable2.h"
  392. static const rotable_Reg_t reg_wlan[] =
  393. {
  394. { "init", ROREG_FUNC(l_wlan_init)},
  395. { "scan", ROREG_FUNC(l_wlan_scan)},
  396. { "scanResult", ROREG_FUNC(l_wlan_scan_result)},
  397. #ifndef LUAT_USE_WLAN_SCANONLY
  398. { "mode", ROREG_FUNC(l_wlan_mode)},
  399. { "setMode", ROREG_FUNC(l_wlan_mode)},
  400. { "ready", ROREG_FUNC(l_wlan_ready)},
  401. { "connect", ROREG_FUNC(l_wlan_connect)},
  402. { "disconnect", ROREG_FUNC(l_wlan_disconnect)},
  403. // 配网相关
  404. { "smartconfig", ROREG_FUNC(l_wlan_smartconfig)},
  405. { "getIP", ROREG_FUNC(l_wlan_get_ip)},
  406. { "getInfo", ROREG_FUNC(l_wlan_get_info)},
  407. { "getMac", ROREG_FUNC(l_wlan_get_mac)},
  408. { "setMac", ROREG_FUNC(l_wlan_set_mac)},
  409. { "hostname", ROREG_FUNC(l_wlan_get_set_hostname)},
  410. { "powerSave", ROREG_FUNC(l_wlan_powerSave)},
  411. // AP相关
  412. { "createAP", ROREG_FUNC(l_wlan_ap_start)},
  413. // wifi模式
  414. //@const NONE WLAN模式,停用
  415. {"NONE", ROREG_INT(LUAT_WLAN_MODE_NULL)},
  416. //@const STATION WLAN模式,STATION模式,主动连AP
  417. {"STATION", ROREG_INT(LUAT_WLAN_MODE_STA)},
  418. //@const AP WLAN模式,AP模式,接受STATION连接
  419. {"AP", ROREG_INT(LUAT_WLAN_MODE_AP)},
  420. //@const AP WLAN模式,混合模式
  421. {"STATIONAP", ROREG_INT(LUAT_WLAN_MODE_APSTA)},
  422. // 配网模式
  423. //@const STOP 停止配网
  424. {"STOP", ROREG_INT(LUAT_SC_TYPE_STOP)},
  425. //@const ESPTOUCH esptouch配网, V1
  426. {"ESPTOUCH", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH)},
  427. //@const AIRKISS Airkiss配网, 微信常用
  428. {"AIRKISS", ROREG_INT(LUAT_SC_TYPE_AIRKISS)},
  429. //@const ESPTOUCH_AIRKISS esptouch和Airkiss混合配网
  430. {"ESPTOUCH_AIRKISS", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_AIRKISS)},
  431. //@const ESPTOUCH_V2 esptouch配网, V2, 未测试
  432. {"ESPTOUCH_V2", ROREG_INT(LUAT_SC_TYPE_ESPTOUCH_V2)},
  433. //@const PS_NONE 关闭省电模式
  434. {"PS_NONE", ROREG_INT(0)},
  435. //@const PS_MIN_MODEM 最小Modem省电模式
  436. {"PS_MIN_MODEM", ROREG_INT(1)},
  437. //@const PS_MAX_MODEM 最大Modem省电模式
  438. {"PS_MAX_MODEM", ROREG_INT(2)},
  439. #endif
  440. { NULL, ROREG_INT(0)}
  441. };
  442. LUAMOD_API int luaopen_wlan( lua_State *L ) {
  443. luat_newlib2(L, reg_wlan);
  444. return 1;
  445. }