luat_lib_wlan.c 18 KB

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