luat_lib_wlan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. lua_pushstring(L, "WLAN_READY");
  340. lua_call(L, 1, 0);
  341. // 额外发送一个通用事件 NET_READY
  342. lua_getglobal(L, "sys_pub");
  343. lua_pushstring(L, "NET_READY");
  344. lua_call(L, 1, 0);
  345. break;
  346. case RT_WLAN_EVT_SCAN_DONE: // 扫描完成
  347. lua_pushstring(L, "WLAN_SCAN_DONE");
  348. lua_call(L, 1, 0);
  349. break;
  350. case RT_WLAN_EVT_STA_CONNECTED: // 连上wifi路由器/热点,但还没拿到ip
  351. lua_pushstring(L, "WLAN_STA_CONNECTED");
  352. lua_pushinteger(L, 1);
  353. lua_call(L, 2, 0);
  354. break;
  355. case RT_WLAN_EVT_STA_CONNECTED_FAIL: // 没有连上wifi路由器/热点,通常是密码错误
  356. lua_pushstring(L, "WLAN_STA_CONNECTED");
  357. lua_pushinteger(L, 0);
  358. lua_call(L, 2, 0);
  359. break;
  360. case RT_WLAN_EVT_STA_DISCONNECTED: // 从wifi路由器/热点断开了
  361. lua_pushstring(L, "WLAN_STA_DISCONNECTED");
  362. lua_call(L, 1, 0);
  363. break;
  364. case RT_WLAN_EVT_AP_START:
  365. lua_pushstring(L, "WLAN_AP_START");
  366. lua_call(L, 1, 0);
  367. break;
  368. case RT_WLAN_EVT_AP_STOP:
  369. lua_pushstring(L, "WLAN_AP_STOP");
  370. lua_call(L, 1, 0);
  371. break;
  372. case RT_WLAN_EVT_AP_ASSOCIATED:
  373. lua_pushstring(L, "WLAN_AP_ASSOCIATED");
  374. lua_call(L, 1, 0);
  375. break;
  376. case RT_WLAN_EVT_AP_DISASSOCIATED:
  377. lua_pushstring(L, "WLAN_AP_DISASSOCIATED");
  378. lua_call(L, 1, 0);
  379. break;
  380. default:
  381. break;
  382. }
  383. lua_pushinteger(L, 0);
  384. return 1;
  385. }
  386. // 注册回调
  387. static void wlan_cb(int event, struct rt_wlan_buff *buff, void *parameter) {
  388. rtos_msg_t msg;
  389. LOG_I("wlan event -> %d", event);
  390. msg.handler = l_wlan_handler;
  391. msg.ptr = NULL;
  392. msg.arg1 = event;
  393. msg.arg2 = 0;
  394. if (event == RT_WLAN_EVT_SCAN_DONE) {
  395. struct rt_wlan_scan_result *result = buff->data;
  396. }
  397. luat_msgbus_put(&msg, 1);
  398. }
  399. static void reg_wlan_callbacks(void) {
  400. rt_wlan_register_event_handler(RT_WLAN_EVT_READY, wlan_cb, RT_NULL);
  401. rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_DONE, wlan_cb, RT_NULL);
  402. //rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT, wlan_cb, RT_NULL);
  403. rt_wlan_register_event_handler(RT_WLAN_EVT_STA_CONNECTED, wlan_cb, RT_NULL);
  404. rt_wlan_register_event_handler(RT_WLAN_EVT_STA_CONNECTED_FAIL, wlan_cb, RT_NULL);
  405. rt_wlan_register_event_handler(RT_WLAN_EVT_STA_DISCONNECTED, wlan_cb, RT_NULL);
  406. rt_wlan_register_event_handler(RT_WLAN_EVT_AP_START, wlan_cb, RT_NULL);
  407. rt_wlan_register_event_handler(RT_WLAN_EVT_AP_STOP, wlan_cb, RT_NULL);
  408. rt_wlan_register_event_handler(RT_WLAN_EVT_AP_ASSOCIATED, wlan_cb, RT_NULL);
  409. rt_wlan_register_event_handler(RT_WLAN_EVT_AP_DISASSOCIATED, wlan_cb, RT_NULL);
  410. }
  411. // ----------------------------
  412. //-----------------------------
  413. static int luat_PW_msghandler(lua_State *L, void* ptr) {
  414. lua_getglobal(L, "sys_pub");
  415. if (!lua_isnil(L, -1)) {
  416. lua_pushstring(L, "WLAN_PW_RE");
  417. if (ptr == RT_NULL) {
  418. lua_call(L, 1, 0);
  419. }
  420. else {
  421. lua_pushstring(L, jinfo.ssid);
  422. lua_pushstring(L, jinfo.passwd);
  423. lua_call(L, 3, 0);
  424. }
  425. }
  426. // 给rtos.recv方法返回个空数据
  427. lua_pushinteger(L, 0);
  428. return 1;
  429. }
  430. static void _PW_callback(int state, unsigned char *_ssid, unsigned char *_passwd) {
  431. LOG_I("oneshot/airkiss callback state=%ld", state);
  432. if (_ssid != RT_NULL) {
  433. LOG_I("oneshot/airkiss ssid %s", _ssid);
  434. }
  435. if (_passwd != RT_NULL) {
  436. LOG_I("oneshot/airkiss key %s", _passwd);
  437. }
  438. rt_memset(&jinfo, 0, sizeof(struct join_info));
  439. if (state == 0) {
  440. rt_strncpy(jinfo.ssid, _ssid, rt_strlen(_ssid));
  441. if (_passwd)
  442. {
  443. rt_strncpy(jinfo.passwd, _passwd, rt_strlen(_passwd));
  444. }
  445. }
  446. // 发送msgbus消息
  447. rtos_msg_t msg;
  448. msg.handler = luat_PW_msghandler;
  449. msg.ptr = state == 0 ? (void*)1 : RT_NULL;
  450. luat_msgbus_put(&msg, 1);
  451. }
  452. //--------------------------
  453. // 联盛德的oneshot配网, 需要较多固定内存,默认不启用
  454. #ifdef WM_USING_ONESHOT
  455. static int32_t oneshot_autojoin = 0;
  456. static int32_t oneshot_re;
  457. /*
  458. 启动配网过程,支持UDP/SOCKET/APWEB配网
  459. @api wlan.oneShotStart(mode,ssid,passwd)
  460. @int 配网模式: 0-UDP配网, 1-SOCKET配网, 2-AP网页配网
  461. @string AP网页配网时的SSID,默认值为luatos
  462. @string AP网页配网时的密钥,默认值为12345678
  463. @return boolean 启动成功返回true,否则返回false
  464. @usage
  465. -- UDP配网,需要下载联德盛测试APP,2.0版本
  466. wlan.oneShotStart(0)
  467. @usage
  468. -- SOCKET配网,需要下载联德盛测试APP,2.0版本
  469. wlan.oneShotStart(1)
  470. @usage
  471. -- AP网页配网,手机搜索wifi "W600APWEB", 密码12345678. 连上之后,保持wifi连接,浏览器访问 192.168.168.1, 按提示输入.
  472. wlan.oneShotStart(2, "W600APWEB", "12345678")
  473. @usage
  474. -- 监听配网信息
  475. sys.subscribe("WLAN_PW_RE", function(ssid, passwd)
  476. if ssid then
  477. log.info("wlan", "Got ssid and passwd", ssid, passwd)
  478. else
  479. log.info("wlan", "oneshot fail")
  480. end
  481. end)
  482. */
  483. static int l_wlan_oneshot_start(lua_State *L) {
  484. WM_ONESHOT_MODE mode = (WM_ONESHOT_MODE)luaL_optinteger(L, 1, WM_UDP);
  485. oneshot_autojoin = luaL_optinteger(L, 2, 1);
  486. LLOGD("oneshot mode=%d\n", mode);
  487. if (mode == WM_APWEB) {
  488. const char* ssid = luaL_optstring(L, 2, "luatos");
  489. const char* passwd = luaL_optstring(L, 3, "12345678");
  490. LLOGD("APWEB ssid=%s passwd=%s", ssid, passwd);
  491. rt_wlan_set_mode("wlan0", RT_WLAN_STATION);
  492. rt_wlan_set_mode("wlan1", RT_WLAN_AP);
  493. rt_wlan_start_ap(ssid, passwd);
  494. }
  495. else {
  496. rt_wlan_set_mode("wlan0", RT_WLAN_STATION);
  497. }
  498. int re = wm_oneshot_start(mode, _PW_callback);
  499. LLOGD("oneshot start re=%ld\n", re);
  500. lua_pushboolean(L, re == 0);
  501. return 1;
  502. }
  503. /*
  504. 停止配网, 通常不需要调用
  505. @api wlan.oneshotStop()
  506. @return nil 总是没有返回值
  507. @usage
  508. -- 停止配网
  509. wlan.oneshotStop()
  510. */
  511. static int l_wlan_oneshot_stop(lua_State *L) {
  512. wm_oneshot_stop();
  513. return 0;
  514. }
  515. /*
  516. 查询配网状态
  517. @api wlan.oneshotState()
  518. @return boolean 配网中返回true,否则返回false
  519. @usage
  520. -- 查询
  521. if wlan.oneshotState() then
  522. log.info("wlan", "配网中")
  523. end
  524. */
  525. static int l_wlan_oneshot_state(lua_State *L) {
  526. lua_pushboolean(L, wm_oneshot_get());
  527. return 1;
  528. }
  529. #endif
  530. //--------------------------
  531. static int l_wlan_join_info(lua_State *L) {
  532. if (jinfo.ssid[0] != RT_NULL) {
  533. lua_pushstring(L, jinfo.ssid);
  534. if (jinfo.passwd[0] != RT_NULL) {
  535. lua_pushstring(L, jinfo.passwd);
  536. return 2;
  537. }
  538. return 1;
  539. }
  540. return 0;
  541. }
  542. /*
  543. 获取wifi信号强度值rssi
  544. @function wlan.rssi()
  545. @return int 如果是station模式,返回正的rssi值,否则返回负值
  546. @usage
  547. -- 信号强度
  548. log.info("wlan", wlan.rssi())
  549. */
  550. static int l_wlan_rssi(lua_State* L) {
  551. lua_pushinteger(L, rt_wlan_get_rssi());
  552. return 1;
  553. }
  554. /*
  555. 启动airkiss配网线程
  556. @function wlan.airkiss_start()
  557. @return re 启动成功返回1,否则返回0
  558. @usage
  559. -- 启动airkiss配网
  560. wlan.airkiss_start()
  561. @usage
  562. -- 监听配网信息
  563. sys.subscribe("WLAN_PW_RE", function(ssid, passwd)
  564. if ssid then
  565. log.info("wlan", "Got ssid and passwd", ssid, passwd)
  566. else
  567. log.info("wlan", "oneshot fail")
  568. end
  569. end)
  570. */
  571. #include "airkiss.h"
  572. static int l_wlan_airkiss_start(lua_State* L){
  573. rt_wlan_set_mode("wlan0", RT_WLAN_STATION);
  574. airkiss_set_callback(_PW_callback);
  575. lua_pushinteger(L, airkiss_start());
  576. return 1;
  577. }
  578. #include "rotable.h"
  579. static const rotable_Reg reg_wlan[] =
  580. {
  581. { "getMode" , l_wlan_get_mode , 0},
  582. { "setMode" , l_wlan_set_mode , 0},
  583. { "connect" , l_wlan_connect , 0},
  584. { "disconnect",l_wlan_disconnect , 0},
  585. { "connected" ,l_wlan_connected , 0},
  586. { "ready" , l_wlan_ready , 0},
  587. { "autoreconnect", l_wlan_autoreconnect, 0},
  588. { "scan", l_wlan_scan, 0},
  589. { "scan_get_info", l_wlan_scan_get_result, 0},
  590. { "scanResult", l_wlan_scan_get_result, 0},
  591. { "getMac", l_wlan_get_mac, 0},
  592. { "get_mac", l_wlan_get_mac, 0},
  593. { "get_mac_raw", l_wlan_get_mac_raw, 0},
  594. { "rssi", l_wlan_rssi, 0},
  595. //{ "set_mac", l_wlan_set_mac},
  596. // ---- oneshot
  597. #ifdef WM_USING_ONESHOT
  598. { "oneshotStart" , l_wlan_oneshot_start , 0},
  599. { "oneshotStop" , l_wlan_oneshot_stop , 0},
  600. { "oneshotState" , l_wlan_oneshot_state , 0},
  601. #endif
  602. { "lastInfo" , l_wlan_join_info , 0},
  603. { "airkiss_start", l_wlan_airkiss_start, 0},
  604. // ---
  605. { "NONE", NULL , RT_WLAN_NONE},
  606. { "STATION", NULL , RT_WLAN_STATION},
  607. { "AP", NULL , RT_WLAN_AP},
  608. { NULL, NULL , 0}
  609. };
  610. LUAMOD_API int luaopen_wlan( lua_State *L ) {
  611. reg_wlan_callbacks();
  612. luat_newlib(L, reg_wlan);
  613. // uint8_t* flash_addr = (uint8_t*) 0x8000000;
  614. // LOG_HEX("FLASH", 8, flash_addr, 0x80);
  615. // LLOGD("mac => %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
  616. // *(flash_addr+12),
  617. // *(flash_addr+13),
  618. // *(flash_addr+14),
  619. // *(flash_addr+15),
  620. // *(flash_addr+16),
  621. // *(flash_addr+17),
  622. // *(flash_addr+18),
  623. // *(flash_addr+19)
  624. // );
  625. return 1;
  626. }
  627. #endif
  628. #endif