luat_lib_wlan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. @module wlan
  3. @summary wifi操作库
  4. @version 1.0
  5. @date 2020.03.30
  6. */
  7. #include "luat_base.h"
  8. #include "luat_timer.h"
  9. #include "luat_malloc.h"
  10. #include "rtthread.h"
  11. #ifdef RT_USING_WIFI
  12. #define DBG_TAG "luat.wlan"
  13. #define DBG_LVL DBG_LOG
  14. #include <rtdbg.h>
  15. #ifdef RT_WLAN_MANAGE_ENABLE
  16. #include "wlan_dev.h"
  17. #include <wlan_mgnt.h>
  18. #include <wlan_prot.h>
  19. #include <wlan_cfg.h>
  20. #ifdef WM_USING_ONESHOT
  21. #include "oneshot.h"
  22. #endif
  23. #define LUAT_LOG_TAG "wlan"
  24. #include "luat_log.h"
  25. /*
  26. 获取wifi模式
  27. @api wlan.getMode(dev)
  28. @string 设备名称,字符串或数值, 可选值0/1, "wlan0","wlan1". 默认"wlan0"
  29. @return int 模式wlan.NONE, wlan.STATION, wlan.AP
  30. @usage
  31. -- 获取wlan0的当前模式
  32. local m = wlan.getMode("wlan0")
  33. */
  34. static int l_wlan_get_mode(lua_State *L) {
  35. char* devname = "wlan0";
  36. if (lua_gettop(L) != 0) {
  37. if (lua_isstring(L, 1)) {
  38. devname = lua_tostring(L, 1);
  39. }
  40. else if (lua_isinteger(L, 1)) {
  41. switch (lua_tointeger(L, 1))
  42. {
  43. case 1:
  44. devname = "wlan1";
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. }
  51. int mode = rt_wlan_get_mode(devname);
  52. lua_pushinteger(L, mode);
  53. return 1;
  54. }
  55. /*
  56. 设置wifi模式,通常不需要设置
  57. @api wlan.setMode(dev, mode)
  58. @string 设备名称,字符串或数值, 可选值0/1, "wlan0","wlan1". 默认"wlan0"
  59. @int 模式wlan.NONE, wlan.STATION, wlan.AP
  60. @return int 设置成功与否,通常不检查
  61. @usage
  62. -- 将wlan设置为wifi客户端模式
  63. wlan.setMode("wlan0",wlan.STATION)
  64. */
  65. static int l_wlan_set_mode(lua_State *L) {
  66. char* devname = "wlan0";
  67. if (lua_gettop(L) != 0) {
  68. if (lua_isstring(L, 1)) {
  69. devname = lua_tostring(L, 1);
  70. }
  71. else if (lua_isinteger(L, 1)) {
  72. if (lua_tointeger(L, 1) == 1) {
  73. devname = "wlan1";
  74. }
  75. }
  76. }
  77. rt_device_t dev = rt_device_find(devname);
  78. if (dev == RT_NULL) {
  79. return 0;
  80. }
  81. int mode = luaL_checkinteger(L, 2);
  82. int re = rt_wlan_set_mode(devname, mode);
  83. lua_pushinteger(L, re);
  84. return 1;
  85. }
  86. typedef struct join_info
  87. {
  88. char ssid[64];
  89. char passwd[64];
  90. } join_info_t;
  91. static join_info_t jinfo = {0};
  92. static void _wlan_connect(void* params) {
  93. rt_wlan_config_autoreconnect(1);
  94. rt_wlan_connect(jinfo.ssid, jinfo.passwd);
  95. }
  96. /*
  97. 连接wifi,成功启动联网线程不等于联网成功!!
  98. @api wlan.connect(ssid,password)
  99. @string ssid wifi的SSID
  100. @string password wifi的密码,可选
  101. @return boolean 如果正常启动联网线程,无返回值,否则返回出错信息.
  102. @usage
  103. -- 连接到uiot,密码1234567890
  104. wlan.connect("uiot", "1234567890")
  105. */
  106. static int l_wlan_connect(lua_State *L) {
  107. // 更新参数
  108. size_t len;
  109. const char* _ssid = luaL_checklstring(L, 1, &len);
  110. rt_strncpy(jinfo.ssid, _ssid, len);
  111. jinfo.ssid[len] = 0x00;
  112. if (lua_isstring(L, 2)) {
  113. const char* _passwd = luaL_checklstring(L, 2, &len);
  114. rt_strncpy(jinfo.passwd, _passwd, len);
  115. jinfo.passwd[len] = 0x00;
  116. }
  117. else {
  118. jinfo.passwd[0] = 0x00;
  119. }
  120. rt_thread_t t = rt_thread_create("wlanj", _wlan_connect, RT_NULL, 1024, 20, 20);
  121. if (t == RT_NULL) {
  122. LOG_E("fail to create wlan-connect thread");
  123. lua_pushinteger(L, 1);
  124. lua_pushstring(L, "fail to create wlan thread");
  125. return 2;
  126. }
  127. if (rt_thread_startup(t) != RT_EOK) {
  128. LOG_E("fail to start wlan-connect thread");
  129. lua_pushinteger(L, 2);
  130. lua_pushstring(L, "fail to start wlan thread");
  131. return 2;
  132. }
  133. // 自动重连
  134. return 0;
  135. }
  136. /*
  137. 断开wifi
  138. @api wlan.disconnect()
  139. @return boolean 成功返回true,否则返回false
  140. @usage
  141. -- 断开wifi连接
  142. wlan.disconnect()
  143. */
  144. static int l_wlan_disconnect(lua_State *L) {
  145. if (rt_wlan_is_connected()) {
  146. rt_wlan_disconnect();
  147. lua_pushboolean(L, 1);
  148. }
  149. else {
  150. lua_pushboolean(L, 0);
  151. }
  152. return 1;
  153. }
  154. /*
  155. 是否已经连上wifi网络
  156. @api wlan.connected()
  157. @return boolean 已连接返回true0,未连接返回false
  158. @usage
  159. -- 连上wifi网络,只代表密码正确, 不一定拿到了ip
  160. wlan.connected()
  161. */
  162. static int l_wlan_connected(lua_State *L) {
  163. lua_pushboolean(L, rt_wlan_is_connected() == 1 ? 1 : 0);
  164. return 1;
  165. }
  166. /*
  167. 设置或查询wifi station是否自动连接
  168. @api wlan.autoreconnect(enable)
  169. @int 传入1启用自动连接(自动重连wifi), 传入0关闭. 不传这个参数就是查询
  170. @return int 已启用自动连接(自动重连wifi)返回1, 否则返回0
  171. @usage
  172. -- 查询自动连接的设置
  173. wlan.autoreconnect()
  174. @usage
  175. -- 设置自动连接
  176. wlan.autoreconnect(1)
  177. */
  178. static int l_wlan_autoreconnect(lua_State *L) {
  179. if (lua_gettop(L) > 0) {
  180. rt_wlan_config_autoreconnect(luaL_checkinteger(L, 1));
  181. }
  182. lua_pushboolean(L, rt_wlan_get_autoreconnect_mode());
  183. return 1;
  184. }
  185. /*
  186. 开始扫网,通常配合wlan.scanResult使用
  187. @api wlan.scan()
  188. @return boolean 启动结果,一般为true
  189. @usage
  190. -- 扫描并查询结果
  191. wlan.scan()
  192. sys.waitUntil("WLAN_SCAN_DONE", 30000)
  193. local re = wlan.scanResult()
  194. for i in ipairs(re) do
  195. log.info("wlan", "info", re[i].ssid, re[i].rssi)
  196. end
  197. */
  198. static int l_wlan_scan(lua_State *L) {
  199. lua_pushboolean(L, rt_wlan_scan() == 0);
  200. return 1;
  201. }
  202. /*
  203. 获取扫网结果,需要先执行wlan.scan,并等待WLAN_SCAN_DONE事件
  204. @api wlan.scanResult(num)
  205. @int 最大结果数量,默认50
  206. @return table 扫描结果的数组
  207. @usage
  208. -- 扫描并查询结果
  209. wlan.scan()
  210. sys.waitUntil("WLAN_SCAN_DONE", 30000)
  211. local re = wlan.scanResult()
  212. for i in ipairs(re) do
  213. log.info("wlan", "info", re[i].ssid, re[i].rssi)
  214. end
  215. */
  216. static int l_wlan_scan_get_result(lua_State *L) {
  217. int num = luaL_optinteger(L, 1, 20); /* 查询扫描结果数量 */
  218. struct rt_wlan_scan_result *result = rt_wlan_scan_get_result();
  219. if (result) {
  220. LOG_I("wlan_scan_get_result >> %ld", result->num);
  221. }
  222. lua_createtable(L, num, 0);
  223. if (result && result->num > 0) {
  224. if (result->num < num) {
  225. num = result->num;
  226. }
  227. for (size_t i = 0; i < num; i++)
  228. {
  229. struct rt_wlan_info info = result->info[i];
  230. lua_pushinteger(L, i+1);
  231. // local info = {}
  232. lua_createtable(L, 0, 8);
  233. // info.ssid = xxx
  234. lua_pushliteral(L, "ssid");
  235. lua_pushlstring(L, info.ssid.val, info.ssid.len);
  236. lua_settable(L, -3);
  237. // info.rssi = xxx
  238. lua_pushliteral(L, "rssi");
  239. lua_pushinteger(L, info.rssi);
  240. lua_settable(L, -3);
  241. // info.security = xxx
  242. lua_pushliteral(L, "security");
  243. lua_pushinteger(L, info.security);
  244. lua_settable(L, -3);
  245. // info.channel = xxx
  246. lua_pushliteral(L, "channel");
  247. lua_pushinteger(L, info.channel);
  248. lua_settable(L, -3);
  249. // info.band = xxx
  250. lua_pushliteral(L, "band");
  251. lua_pushinteger(L, info.band);
  252. lua_settable(L, -3);
  253. // info.bssid = xxx
  254. lua_pushliteral(L, "bssid");
  255. lua_pushlstring(L, info.bssid, 6);
  256. lua_settable(L, -3);
  257. // info.hidden = xxx
  258. lua_pushliteral(L, "hidden");
  259. lua_pushinteger(L, info.hidden);
  260. lua_settable(L, -3);
  261. // re[i+1] = info
  262. lua_settable(L, -3);
  263. }
  264. }
  265. return 1;
  266. }
  267. /*
  268. 获取mac地址
  269. @function wlan.get_mac()
  270. @return string 长度为12的HEX字符串,如果不存在就返回值nil
  271. @usage
  272. -- 获取MAC地址
  273. log.info("wlan", "mac addr", wlan.get_mac())
  274. */
  275. static int l_wlan_get_mac(lua_State *L) {
  276. rt_uint8_t mac[6] = {0};
  277. char buff[14] = {0};
  278. #ifdef BSP_USING_WM_LIBRARIES
  279. memcpy(mac, (uint8_t*) (0x8000000 + 12), 6);
  280. #else
  281. rt_wlan_get_mac(mac);
  282. #endif
  283. if (mac[0] != 0x00) {
  284. rt_snprintf(buff, 14, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  285. lua_pushlstring(L, buff, 12);
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. /*
  291. 获取mac地址,raw格式
  292. @api wlan.get_mac_raw()
  293. @return string 6字节的mac地址串
  294. @usage
  295. -- 查询mac地址, 二进制模式
  296. local mac_raw = wlan.get_mac_raw()
  297. if mac_raw then
  298. log.info("wlan", "mac addr", mac_raw:toHex())
  299. end
  300. */
  301. static int l_wlan_get_mac_raw(lua_State *L) {
  302. rt_uint8_t mac[6] = {0};
  303. #ifdef BSP_USING_WM_LIBRARIES
  304. memcpy(mac, (uint8_t*) (0x8000000 + 12), 6);
  305. #else
  306. rt_wlan_get_mac(mac);
  307. #endif
  308. if (mac[0] != 0x00) {
  309. lua_pushlstring(L, mac, 6);
  310. return 1;
  311. }
  312. return 0;
  313. }
  314. /*
  315. wifi是否已经获取ip
  316. @api wlan.ready()
  317. @return boolean 已经有ip返回true,否则返回false
  318. @usage
  319. -- 查询是否已经wifi联网
  320. if wlan.ready() then
  321. log.info("wlan", "wifi ok", "Let's Rock!")
  322. end
  323. */
  324. static int l_wlan_ready(lua_State *L) {
  325. lua_pushboolean(L, rt_wlan_is_ready());
  326. return 1;
  327. }
  328. static int l_wlan_handler(lua_State* L, void* ptr) {
  329. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  330. int event = msg->arg1;
  331. lua_getglobal(L, "sys_pub");
  332. if (lua_isnil(L, -1)) {
  333. lua_pushinteger(L, 0);
  334. return 1;
  335. }
  336. switch (event)
  337. {
  338. case RT_WLAN_EVT_READY: // 网络就绪
  339. /*
  340. @sys_pub wlan
  341. WIFI就绪
  342. WLAN_READY
  343. @usage
  344. sys.taskInit(function()
  345. sys.waitUntil("WLAN_READY")
  346. end)
  347. */
  348. lua_pushstring(L, "WLAN_READY");
  349. lua_call(L, 1, 0);
  350. // 额外发送一个通用事件 NET_READY
  351. lua_getglobal(L, "sys_pub");
  352. /*
  353. @sys_pub wlan
  354. 网络就绪
  355. NET_READY
  356. @usage
  357. sys.taskInit(function()
  358. sys.waitUntil("NET_READY")
  359. end)
  360. */
  361. lua_pushstring(L, "NET_READY");
  362. lua_call(L, 1, 0);
  363. break;
  364. case RT_WLAN_EVT_SCAN_DONE: // 扫描完成
  365. /*
  366. @sys_pub wlan
  367. 扫描完成
  368. WLAN_SCAN_DONE
  369. @usage
  370. sys.taskInit(function()
  371. sys.waitUntil("WLAN_SCAN_DONE")
  372. end)
  373. */
  374. lua_pushstring(L, "WLAN_SCAN_DONE");
  375. lua_call(L, 1, 0);
  376. break;
  377. case RT_WLAN_EVT_STA_CONNECTED: // 连上wifi路由器/热点,但还没拿到ip
  378. /*
  379. @sys_pub wlan
  380. 连接成功,但还没拿到ip
  381. WLAN_STA_CONNECTED
  382. @usage
  383. sys.taskInit(function()
  384. sys.waitUntil("WLAN_STA_CONNECTED")
  385. end)
  386. */
  387. lua_pushstring(L, "WLAN_STA_CONNECTED");
  388. lua_pushinteger(L, 1);
  389. lua_call(L, 2, 0);
  390. break;
  391. case RT_WLAN_EVT_STA_CONNECTED_FAIL: // 没有连上wifi路由器/热点,通常是密码错误
  392. /*
  393. @sys_pub wlan
  394. 连接失败,通常是密码错误
  395. WLAN_STA_CONNECTED_FAIL
  396. @usage
  397. sys.taskInit(function()
  398. sys.waitUntil("WLAN_STA_CONNECTED_FAIL")
  399. end)
  400. */
  401. lua_pushstring(L, "WLAN_STA_CONNECTED_FAIL");
  402. lua_pushinteger(L, 0);
  403. lua_call(L, 2, 0);
  404. break;
  405. case RT_WLAN_EVT_STA_DISCONNECTED: // 从wifi路由器/热点断开了
  406. /*
  407. @sys_pub wlan
  408. 断开连接
  409. WLAN_STA_DISCONNECTED
  410. @usage
  411. sys.taskInit(function()
  412. sys.waitUntil("WLAN_STA_DISCONNECTED")
  413. end)
  414. */
  415. lua_pushstring(L, "WLAN_STA_DISCONNECTED");
  416. lua_call(L, 1, 0);
  417. break;
  418. case RT_WLAN_EVT_AP_START:
  419. /*
  420. @sys_pub wlan
  421. 热点启动
  422. WLAN_AP_START
  423. @usage
  424. sys.taskInit(function()
  425. sys.waitUntil("WLAN_AP_START")
  426. end)
  427. */
  428. lua_pushstring(L, "WLAN_AP_START");
  429. lua_call(L, 1, 0);
  430. break;
  431. case RT_WLAN_EVT_AP_STOP:
  432. /*
  433. @sys_pub wlan
  434. 热点停止
  435. WLAN_AP_STOP
  436. @usage
  437. sys.taskInit(function()
  438. sys.waitUntil("WLAN_AP_STOP")
  439. end)
  440. */
  441. lua_pushstring(L, "WLAN_AP_STOP");
  442. lua_call(L, 1, 0);
  443. break;
  444. case RT_WLAN_EVT_AP_ASSOCIATED:
  445. /*
  446. @sys_pub wlan
  447. STA 接入
  448. WLAN_AP_ASSOCIATED
  449. @usage
  450. sys.taskInit(function()
  451. sys.waitUntil("WLAN_AP_ASSOCIATED")
  452. end)
  453. */
  454. lua_pushstring(L, "WLAN_AP_ASSOCIATED");
  455. lua_call(L, 1, 0);
  456. break;
  457. case RT_WLAN_EVT_AP_DISASSOCIATED:
  458. /*
  459. @sys_pub wlan
  460. STA 断开
  461. WLAN_AP_DISASSOCIATED
  462. @usage
  463. sys.taskInit(function()
  464. sys.waitUntil("WLAN_AP_DISASSOCIATED")
  465. end)
  466. */
  467. lua_pushstring(L, "WLAN_AP_DISASSOCIATED");
  468. lua_call(L, 1, 0);
  469. break;
  470. default:
  471. break;
  472. }
  473. lua_pushinteger(L, 0);
  474. return 1;
  475. }
  476. // 注册回调
  477. static void wlan_cb(int event, struct rt_wlan_buff *buff, void *parameter) {
  478. rtos_msg_t msg;
  479. LOG_I("wlan event -> %d", event);
  480. msg.handler = l_wlan_handler;
  481. msg.ptr = NULL;
  482. msg.arg1 = event;
  483. msg.arg2 = 0;
  484. if (event == RT_WLAN_EVT_SCAN_DONE) {
  485. struct rt_wlan_scan_result *result = buff->data;
  486. }
  487. luat_msgbus_put(&msg, 1);
  488. }
  489. static void reg_wlan_callbacks(void) {
  490. rt_wlan_register_event_handler(RT_WLAN_EVT_READY, wlan_cb, RT_NULL);
  491. rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_DONE, wlan_cb, RT_NULL);
  492. //rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT, wlan_cb, RT_NULL);
  493. rt_wlan_register_event_handler(RT_WLAN_EVT_STA_CONNECTED, wlan_cb, RT_NULL);
  494. rt_wlan_register_event_handler(RT_WLAN_EVT_STA_CONNECTED_FAIL, wlan_cb, RT_NULL);
  495. rt_wlan_register_event_handler(RT_WLAN_EVT_STA_DISCONNECTED, wlan_cb, RT_NULL);
  496. rt_wlan_register_event_handler(RT_WLAN_EVT_AP_START, wlan_cb, RT_NULL);
  497. rt_wlan_register_event_handler(RT_WLAN_EVT_AP_STOP, wlan_cb, RT_NULL);
  498. rt_wlan_register_event_handler(RT_WLAN_EVT_AP_ASSOCIATED, wlan_cb, RT_NULL);
  499. rt_wlan_register_event_handler(RT_WLAN_EVT_AP_DISASSOCIATED, wlan_cb, RT_NULL);
  500. }
  501. // ----------------------------
  502. //-----------------------------
  503. static int luat_PW_msghandler(lua_State *L, void* ptr) {
  504. lua_getglobal(L, "sys_pub");
  505. if (!lua_isnil(L, -1)) {
  506. /*
  507. @sys_pub wlan
  508. 配网结束
  509. WLAN_PW_RE
  510. @usage
  511. sys.taskInit(function()
  512. sys.waitUntil("WLAN_PW_RE")
  513. end)
  514. */
  515. lua_pushstring(L, "WLAN_PW_RE");
  516. if (ptr == RT_NULL) {
  517. lua_call(L, 1, 0);
  518. }
  519. else {
  520. lua_pushstring(L, jinfo.ssid);
  521. lua_pushstring(L, jinfo.passwd);
  522. lua_call(L, 3, 0);
  523. }
  524. }
  525. // 给rtos.recv方法返回个空数据
  526. lua_pushinteger(L, 0);
  527. return 1;
  528. }
  529. static void _PW_callback(int state, unsigned char *_ssid, unsigned char *_passwd) {
  530. LOG_I("oneshot/airkiss callback state=%ld", state);
  531. if (_ssid != RT_NULL) {
  532. LOG_I("oneshot/airkiss ssid %s", _ssid);
  533. }
  534. if (_passwd != RT_NULL) {
  535. LOG_I("oneshot/airkiss key %s", _passwd);
  536. }
  537. rt_memset(&jinfo, 0, sizeof(struct join_info));
  538. if (state == 0) {
  539. rt_strncpy(jinfo.ssid, _ssid, rt_strlen(_ssid));
  540. if (_passwd)
  541. {
  542. rt_strncpy(jinfo.passwd, _passwd, rt_strlen(_passwd));
  543. }
  544. }
  545. // 发送msgbus消息
  546. rtos_msg_t msg;
  547. msg.handler = luat_PW_msghandler;
  548. msg.ptr = state == 0 ? (void*)1 : RT_NULL;
  549. luat_msgbus_put(&msg, 1);
  550. }
  551. //--------------------------
  552. // 联盛德的oneshot配网, 需要较多固定内存,默认不启用
  553. #ifdef WM_USING_ONESHOT
  554. static int32_t oneshot_autojoin = 0;
  555. static int32_t oneshot_re;
  556. /*
  557. 启动配网过程,支持UDP/SOCKET/APWEB配网
  558. @api wlan.oneShotStart(mode,ssid,passwd)
  559. @int 配网模式: 0-UDP配网, 1-SOCKET配网, 2-AP网页配网
  560. @string AP网页配网时的SSID,默认值为luatos
  561. @string AP网页配网时的密钥,默认值为12345678
  562. @return boolean 启动成功返回true,否则返回false
  563. @usage
  564. -- UDP配网,需要下载联德盛测试APP,2.0版本
  565. wlan.oneShotStart(0)
  566. @usage
  567. -- SOCKET配网,需要下载联德盛测试APP,2.0版本
  568. wlan.oneShotStart(1)
  569. @usage
  570. -- AP网页配网,手机搜索wifi "W600APWEB", 密码12345678. 连上之后,保持wifi连接,浏览器访问 192.168.168.1, 按提示输入.
  571. wlan.oneShotStart(2, "W600APWEB", "12345678")
  572. @usage
  573. -- 监听配网信息
  574. sys.subscribe("WLAN_PW_RE", function(ssid, passwd)
  575. if ssid then
  576. log.info("wlan", "Got ssid and passwd", ssid, passwd)
  577. else
  578. log.info("wlan", "oneshot fail")
  579. end
  580. end)
  581. */
  582. static int l_wlan_oneshot_start(lua_State *L) {
  583. WM_ONESHOT_MODE mode = (WM_ONESHOT_MODE)luaL_optinteger(L, 1, WM_UDP);
  584. oneshot_autojoin = luaL_optinteger(L, 2, 1);
  585. LLOGD("oneshot mode=%d\n", mode);
  586. if (mode == WM_APWEB) {
  587. const char* ssid = luaL_optstring(L, 2, "luatos");
  588. const char* passwd = luaL_optstring(L, 3, "12345678");
  589. LLOGD("APWEB ssid=%s passwd=%s", ssid, passwd);
  590. rt_wlan_set_mode("wlan0", RT_WLAN_STATION);
  591. rt_wlan_set_mode("wlan1", RT_WLAN_AP);
  592. rt_wlan_start_ap(ssid, passwd);
  593. }
  594. else {
  595. rt_wlan_set_mode("wlan0", RT_WLAN_STATION);
  596. }
  597. int re = wm_oneshot_start(mode, _PW_callback);
  598. LLOGD("oneshot start re=%ld\n", re);
  599. lua_pushboolean(L, re == 0);
  600. return 1;
  601. }
  602. /*
  603. 停止配网, 通常不需要调用
  604. @api wlan.oneshotStop()
  605. @return nil 总是没有返回值
  606. @usage
  607. -- 停止配网
  608. wlan.oneshotStop()
  609. */
  610. static int l_wlan_oneshot_stop(lua_State *L) {
  611. wm_oneshot_stop();
  612. return 0;
  613. }
  614. /*
  615. 查询配网状态
  616. @api wlan.oneshotState()
  617. @return boolean 配网中返回true,否则返回false
  618. @usage
  619. -- 查询
  620. if wlan.oneshotState() then
  621. log.info("wlan", "配网中")
  622. end
  623. */
  624. static int l_wlan_oneshot_state(lua_State *L) {
  625. lua_pushboolean(L, wm_oneshot_get());
  626. return 1;
  627. }
  628. #endif
  629. //--------------------------
  630. static int l_wlan_join_info(lua_State *L) {
  631. if (jinfo.ssid[0] != RT_NULL) {
  632. lua_pushstring(L, jinfo.ssid);
  633. if (jinfo.passwd[0] != RT_NULL) {
  634. lua_pushstring(L, jinfo.passwd);
  635. return 2;
  636. }
  637. return 1;
  638. }
  639. return 0;
  640. }
  641. /*
  642. 获取wifi信号强度值rssi
  643. @function wlan.rssi()
  644. @return int 如果是station模式,返回正的rssi值,否则返回负值
  645. @usage
  646. -- 信号强度
  647. log.info("wlan", wlan.rssi())
  648. */
  649. static int l_wlan_rssi(lua_State* L) {
  650. lua_pushinteger(L, rt_wlan_get_rssi());
  651. return 1;
  652. }
  653. /*
  654. 启动airkiss配网线程
  655. @function wlan.airkiss_start()
  656. @return re 启动成功返回1,否则返回0
  657. @usage
  658. -- 启动airkiss配网
  659. wlan.airkiss_start()
  660. @usage
  661. -- 监听配网信息
  662. sys.subscribe("WLAN_PW_RE", function(ssid, passwd)
  663. if ssid then
  664. log.info("wlan", "Got ssid and passwd", ssid, passwd)
  665. else
  666. log.info("wlan", "oneshot fail")
  667. end
  668. end)
  669. */
  670. #include "airkiss.h"
  671. static int l_wlan_airkiss_start(lua_State* L){
  672. rt_wlan_set_mode("wlan0", RT_WLAN_STATION);
  673. airkiss_set_callback(_PW_callback);
  674. lua_pushinteger(L, airkiss_start());
  675. return 1;
  676. }
  677. #include "rotable.h"
  678. static const rotable_Reg reg_wlan[] =
  679. {
  680. { "getMode" , l_wlan_get_mode , 0},
  681. { "setMode" , l_wlan_set_mode , 0},
  682. { "connect" , l_wlan_connect , 0},
  683. { "disconnect",l_wlan_disconnect , 0},
  684. { "connected" ,l_wlan_connected , 0},
  685. { "ready" , l_wlan_ready , 0},
  686. { "autoreconnect", l_wlan_autoreconnect, 0},
  687. { "scan", l_wlan_scan, 0},
  688. { "scan_get_info", l_wlan_scan_get_result, 0},
  689. { "scanResult", l_wlan_scan_get_result, 0},
  690. { "getMac", l_wlan_get_mac, 0},
  691. { "get_mac", l_wlan_get_mac, 0},
  692. { "get_mac_raw", l_wlan_get_mac_raw, 0},
  693. { "rssi", l_wlan_rssi, 0},
  694. //{ "set_mac", l_wlan_set_mac},
  695. // ---- oneshot
  696. #ifdef WM_USING_ONESHOT
  697. { "oneshotStart" , l_wlan_oneshot_start , 0},
  698. { "oneshotStop" , l_wlan_oneshot_stop , 0},
  699. { "oneshotState" , l_wlan_oneshot_state , 0},
  700. #endif
  701. { "lastInfo" , l_wlan_join_info , 0},
  702. { "airkiss_start", l_wlan_airkiss_start, 0},
  703. // ---
  704. { "NONE", NULL , RT_WLAN_NONE},
  705. { "STATION", NULL , RT_WLAN_STATION},
  706. { "AP", NULL , RT_WLAN_AP},
  707. { NULL, NULL , 0}
  708. };
  709. LUAMOD_API int luaopen_wlan( lua_State *L ) {
  710. reg_wlan_callbacks();
  711. luat_newlib(L, reg_wlan);
  712. // uint8_t* flash_addr = (uint8_t*) 0x8000000;
  713. // LOG_HEX("FLASH", 8, flash_addr, 0x80);
  714. // LLOGD("mac => %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
  715. // *(flash_addr+12),
  716. // *(flash_addr+13),
  717. // *(flash_addr+14),
  718. // *(flash_addr+15),
  719. // *(flash_addr+16),
  720. // *(flash_addr+17),
  721. // *(flash_addr+18),
  722. // *(flash_addr+19)
  723. // );
  724. return 1;
  725. }
  726. #endif
  727. #endif