luat_wlan_air101.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_wlan.h"
  5. #include <string.h>
  6. #include "wm_irq.h"
  7. #include "tls_sys.h"
  8. #include "wm_ram_config.h"
  9. #include "wm_efuse.h"
  10. #include "wm_regs.h"
  11. #include "wm_wifi.h"
  12. #include "wm_netif.h"
  13. #include "wm_debug.h"
  14. #define LUAT_LOG_TAG "wlan"
  15. #include "luat_log.h"
  16. #include "lwip/netif.h"
  17. #include "luat_network_adapter.h"
  18. #include "luat_timer.h"
  19. #include "net_lwip.h"
  20. #include "lwip/tcp.h"
  21. void net_lwip_set_link_state(uint8_t adapter_index, uint8_t updown);
  22. #define SCAN_DONE (0x73)
  23. #define ONESHOT_RESULT (0x74)
  24. static int wlan_init;
  25. static int wlan_state;
  26. char luat_sta_hostname[32];
  27. extern const char WiFiVer[];
  28. extern u8 tx_gain_group[];
  29. extern void *tls_wl_init(u8 *tx_gain, u8 *mac_addr, u8 *hwver);
  30. extern int wpa_supplicant_init(u8 *mac_addr);
  31. extern void tls_pmu_chipsleep_callback(int sleeptime);
  32. static int l_wlan_cb(lua_State*L, void* ptr) {
  33. (void)ptr;
  34. u8 ssid[33]= {0};
  35. u8 pwd[65] = {0};
  36. char sta_ip[16] = {0};
  37. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  38. lua_getglobal(L, "sys_pub");
  39. switch (msg->arg1)
  40. {
  41. case NETIF_IP_NET_UP:
  42. #ifdef LUAT_USE_NETWORK
  43. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 1);
  44. #endif
  45. luat_wlan_get_ip(0, sta_ip);
  46. LLOGD("sta ip %s", sta_ip);
  47. lua_pushstring(L, "IP_READY");
  48. lua_pushstring(L, sta_ip);
  49. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  50. lua_call(L, 3, 0);
  51. break;
  52. case NETIF_WIFI_DISCONNECTED:
  53. #ifdef LUAT_USE_NETWORK
  54. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 0);
  55. #endif
  56. lua_pushstring(L, "IP_LOSE");
  57. lua_call(L, 1, 0); // 暂时只发个IP_LOSE
  58. break;
  59. case SCAN_DONE :
  60. lua_pushstring(L, "WLAN_SCAN_DONE");
  61. lua_call(L, 1, 0);
  62. break;
  63. case ONESHOT_RESULT:
  64. lua_pushstring(L, "SC_RESULT");
  65. tls_wifi_get_oneshot_ssidpwd(ssid, pwd);
  66. LLOGD("oneshot %s %s", ssid, pwd);
  67. lua_pushstring(L, (const char*)ssid);
  68. lua_pushstring(L, (const char*)pwd);
  69. lua_call(L, 3, 0);
  70. break;
  71. default:
  72. break;
  73. }
  74. return 0;
  75. }
  76. static void netif_event_cb(u8 status) {
  77. rtos_msg_t msg = {0};
  78. struct tls_param_ip ip_param;
  79. // LLOGD("netif_event %d", status);
  80. msg.handler = l_wlan_cb;
  81. switch (status)
  82. {
  83. case NETIF_WIFI_JOIN_FAILED:
  84. LLOGI("join failed");
  85. tls_auto_reconnect(3);
  86. break;
  87. case NETIF_WIFI_DISCONNECTED:
  88. wlan_state = 0;
  89. LLOGI("disconnected");
  90. msg.arg1 = status;
  91. luat_msgbus_put(&msg, 0);
  92. break;
  93. case NETIF_WIFI_JOIN_SUCCESS :
  94. wlan_state = 1;
  95. LLOGI("join success");
  96. tls_param_get(TLS_PARAM_ID_IP, (void *)&ip_param, false);
  97. if (!ip_param.dhcp_enable) {
  98. LLOGI("dhcp is disable, so 'join success' as 'IP_READY'");
  99. msg.arg1 = status;
  100. luat_msgbus_put(&msg, 0);
  101. }
  102. break;
  103. case NETIF_IP_NET_UP :
  104. LLOGI("IP READY");
  105. msg.arg1 = status;
  106. luat_msgbus_put(&msg, 0);
  107. break;
  108. case NETIF_WIFI_SOFTAP_SUCCESS :
  109. LLOGI("softap create success");
  110. #ifdef LUAT_USE_NETWORK
  111. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 1);
  112. #endif
  113. break;
  114. case NETIF_WIFI_SOFTAP_FAILED:
  115. LLOGI("softap create failed");
  116. #ifdef LUAT_USE_NETWORK
  117. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 0);
  118. #endif
  119. break;
  120. case NETIF_WIFI_SOFTAP_CLOSED:
  121. LLOGI("softap create closed");
  122. #ifdef LUAT_USE_NETWORK
  123. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 0);
  124. #endif
  125. break;
  126. case NETIF_IP_NET2_UP :
  127. LLOGI("softap netif up");
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. int luat_wlan_init(luat_wlan_config_t *conf) {
  134. (void)conf;
  135. if (wlan_init == 0) {
  136. //------------------------------------
  137. u8 enable = 0;
  138. u8 mac_addr[6];
  139. tls_get_tx_gain(&tx_gain_group[0]);
  140. TLS_DBGPRT_INFO("tx gain ");
  141. TLS_DBGPRT_DUMP((char *)(&tx_gain_group[0]), 27);
  142. if (tls_wifi_mem_cfg(WIFI_MEM_START_ADDR, 6, 4)) /*wifi tx&rx mem customized interface*/
  143. {
  144. TLS_DBGPRT_INFO("wl mem initial failured\n");
  145. }
  146. tls_ft_param_get(CMD_WIFI_MAC, mac_addr, 6);
  147. TLS_DBGPRT_INFO("mac addr ");
  148. TLS_DBGPRT_DUMP((char *)(&mac_addr[0]), 6);
  149. if(tls_wl_init(NULL, &mac_addr[0], NULL) == NULL)
  150. {
  151. TLS_DBGPRT_INFO("wl driver initial failured\n");
  152. }
  153. if (wpa_supplicant_init(mac_addr))
  154. {
  155. TLS_DBGPRT_INFO("supplicant initial failured\n");
  156. }
  157. /*wifi-temperature compensation,default:open*/
  158. if (tls_wifi_get_tempcomp_flag() != 0)
  159. tls_wifi_set_tempcomp_flag(0);
  160. if (tls_wifi_get_psm_chipsleep_flag() != 0)
  161. tls_wifi_set_psm_chipsleep_flag(0);
  162. tls_wifi_psm_chipsleep_cb_register((tls_wifi_psm_chipsleep_callback)tls_pmu_chipsleep_callback, NULL, NULL);
  163. u8 wireless_protocol = 0;
  164. tls_param_get(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, TRUE);
  165. // LLOGD("wireless_protocol %d", wireless_protocol);
  166. if (TLS_PARAM_IEEE80211_INFRA != wireless_protocol)
  167. {
  168. wireless_protocol = TLS_PARAM_IEEE80211_INFRA;
  169. tls_param_set(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, FALSE);
  170. }
  171. //tls_wifi_enable_log(1);
  172. luat_wlan_get_hostname(0); // 调用一下就行
  173. wlan_init = 1;
  174. #ifdef LUAT_USE_ZLINK_WLAN
  175. extern void luat_zlink_wlan_init(void);
  176. luat_zlink_wlan_init();
  177. #else
  178. tls_ethernet_init();
  179. tls_sys_init();
  180. tls_netif_add_status_event(netif_event_cb);
  181. tls_wifi_scan_result_cb_register(NULL);
  182. #endif
  183. //-----------------------------------
  184. #ifdef LUAT_USE_NETWORK
  185. struct netif *et0 = tls_get_netif();
  186. net_lwip_set_netif(NW_ADAPTER_INDEX_LWIP_WIFI_STA, et0);
  187. #if TLS_CONFIG_AP
  188. extern struct netif *nif4apsta;
  189. if (nif4apsta) {
  190. net_lwip_set_netif(NW_ADAPTER_INDEX_LWIP_WIFI_AP, nif4apsta);
  191. net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_AP);
  192. }
  193. #endif
  194. net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  195. // 确保DHCP是默认开启
  196. struct tls_param_ip ip_param;
  197. tls_param_get(TLS_PARAM_ID_IP, (void *)&ip_param, false);
  198. if (!ip_param.dhcp_enable) {
  199. ip_param.dhcp_enable = 1;
  200. tls_param_set(TLS_PARAM_ID_IP, (void *)&ip_param, false);
  201. }
  202. #endif
  203. }
  204. tls_wifi_set_psflag(FALSE, FALSE);
  205. return 0;
  206. }
  207. int luat_wlan_mode(luat_wlan_config_t *conf) {
  208. #if TLS_CONFIG_AP
  209. // 不需要设置, 反正都能用
  210. (void)conf;
  211. if (conf->mode == LUAT_WLAN_MODE_STA) {
  212. tls_wifi_softap_destroy();
  213. }
  214. // else if (conf->mode == LUAT_WLAN_MODE_AP) {
  215. // tls_wifi_disconnect();
  216. // }
  217. #endif
  218. return 0;
  219. }
  220. int luat_wlan_ready(void) {
  221. return wlan_state;
  222. }
  223. int luat_wlan_connect(luat_wlan_conninfo_t* info) {
  224. tls_wifi_connect((u8*)info->ssid, strlen(info->ssid), (u8*)info->password, strlen(info->password));
  225. u8 opt = WIFI_AUTO_CNT_FLAG_SET;
  226. u8 mode = WIFI_AUTO_CNT_ON;
  227. tls_wifi_auto_connect_flag(opt, &mode);
  228. return 0;
  229. }
  230. int luat_wlan_disconnect(void) {
  231. u8 opt = WIFI_AUTO_CNT_FLAG_SET;
  232. u8 mode = WIFI_AUTO_CNT_OFF;
  233. tls_wifi_auto_connect_flag(opt, &mode);
  234. tls_wifi_disconnect();
  235. return 0;
  236. }
  237. static void scan_event_cb(void *ptmr, void *parg) {
  238. (void)ptmr;
  239. (void)parg;
  240. rtos_msg_t msg = {0};
  241. msg.handler = l_wlan_cb;
  242. msg.arg1 = SCAN_DONE;
  243. luat_msgbus_put(&msg, 0);
  244. }
  245. int luat_wlan_scan(void) {
  246. int ret = tls_wifi_scan();
  247. if (ret)
  248. LLOGD("tls_wifi_scan %d", ret);
  249. static tls_os_timer_t *scan_timer = NULL;
  250. tls_os_timer_create(&scan_timer, scan_event_cb, NULL, 3000, 0, NULL);
  251. tls_os_timer_start(scan_timer);
  252. return ret;
  253. }
  254. int luat_wlan_scan_get_result(luat_wlan_scan_result_t *results, size_t ap_limit) {
  255. size_t buffsize = ap_limit * 48 + 8;
  256. u8* buff = luat_heap_malloc(buffsize);
  257. if (buff == NULL)
  258. return 0;
  259. memset(buff, 0, buffsize);
  260. tls_wifi_get_scan_rslt((u8*)buff, buffsize);
  261. struct tls_scan_bss_t *wsr = (struct tls_scan_bss_t*)buff;
  262. struct tls_bss_info_t *bss_info;
  263. if (ap_limit > wsr->count)
  264. ap_limit = wsr->count;
  265. bss_info = wsr->bss;
  266. for (size_t i = 0; i < ap_limit; i++)
  267. {
  268. memset(results[i].ssid, 0, 33);
  269. memcpy(results[i].ssid, bss_info[i].ssid, bss_info[i].ssid_len);
  270. memcpy(results[i].bssid, bss_info[i].bssid, ETH_ALEN);
  271. results[i].rssi = bss_info[i].rssi;
  272. results[i].ch = bss_info[i].channel;
  273. // break;
  274. }
  275. luat_heap_free(buff);
  276. return ap_limit;
  277. }
  278. void luat_sc_callback(enum tls_wifi_oneshot_result_type type) {
  279. if (type == WM_WIFI_ONESHOT_TYPE_SSIDPWD) {
  280. LLOGD("oneshot Got!!");
  281. rtos_msg_t msg = {.handler = l_wlan_cb, .arg1=ONESHOT_RESULT};
  282. luat_msgbus_put(&msg, 0);
  283. }
  284. }
  285. extern u8 gucssidData[];
  286. extern u8 gucpwdData[];
  287. int luat_wlan_smartconfig_start(int tp) {
  288. (void)tp;
  289. gucssidData[0] = 0;
  290. gucpwdData[0] = 0;
  291. tls_wifi_oneshot_result_cb_register(luat_sc_callback);
  292. return tls_wifi_set_oneshot_flag(1);
  293. }
  294. int luat_wlan_smartconfig_stop(void) {
  295. return tls_wifi_set_oneshot_flag(0);
  296. }
  297. // 数据类
  298. int luat_wlan_get_mac(int id, char* mac) {
  299. if (id == 0)
  300. tls_get_mac_addr((u8*)mac);
  301. else
  302. tls_ft_param_get(CMD_WIFI_MACAP, mac, 6);
  303. return 0;
  304. }
  305. int luat_wlan_set_mac(int id, const char* mac_addr) {
  306. u8 mac[8] = {0};
  307. int ret = 0;
  308. if (id == 0) {
  309. tls_get_mac_addr(mac);
  310. }
  311. else {
  312. tls_ft_param_get(CMD_WIFI_MACAP, mac, 6);
  313. }
  314. if (memcmp(mac_addr, mac, 6) == 0) {
  315. // 完全相同, 不需要设置
  316. return 0;
  317. }
  318. if (id == 0) {
  319. ret = tls_set_mac_addr(mac_addr);
  320. }
  321. else {
  322. ret = tls_ft_param_set(CMD_WIFI_MACAP, mac_addr, 6);
  323. }
  324. return ret;
  325. }
  326. int luat_wlan_get_ip(int type, char* data) {
  327. (void)type;
  328. struct netif *et0 = tls_get_netif();
  329. if (et0 == NULL || ip_addr_isany(&et0->ip_addr))
  330. return -1;
  331. ipaddr_ntoa_r(&et0->ip_addr, data, 16);
  332. return 0;
  333. }
  334. // 设置和获取省电模式
  335. int luat_wlan_set_ps(int mode) {
  336. tls_wifi_set_psm_chipsleep_flag(mode == 0 ? 0 : 1);
  337. return 0;
  338. }
  339. int luat_wlan_get_ps(void) {
  340. return tls_wifi_get_psm_chipsleep_flag();
  341. }
  342. int luat_wlan_get_ap_bssid(char* buff) {
  343. struct tls_curr_bss_t bss = {0};
  344. tls_wifi_get_current_bss(&bss);
  345. memcpy(buff, bss.bssid, ETH_ALEN);
  346. return 0;
  347. }
  348. int luat_wlan_get_ap_rssi(void) {
  349. struct tls_curr_bss_t bss = {0};
  350. tls_wifi_get_current_bss(&bss);
  351. return bss.rssi;
  352. }
  353. int luat_wlan_get_ap_gateway(char* buff) {
  354. struct netif *et0 = tls_get_netif();
  355. if (et0 == NULL || ip_addr_isany(&et0->gw))
  356. return -1;
  357. ipaddr_ntoa_r(&et0->gw, buff, 16);
  358. return 0;
  359. }
  360. // AP类
  361. int luat_wlan_ap_start(luat_wlan_apinfo_t *apinfo2) {
  362. int ret = 0;
  363. struct tls_softap_info_t apinfo = {0};
  364. struct tls_ip_info_t ipinfo = {
  365. .ip_addr = {192, 168, 4, 1},
  366. .netmask = {255, 255, 255, 0}
  367. };
  368. if (apinfo2->gateway[0]) {
  369. memcpy(ipinfo.ip_addr, apinfo2->gateway, 4);
  370. }
  371. if (apinfo2->netmask[0]) {
  372. memcpy(ipinfo.netmask, apinfo2->netmask, 4);
  373. }
  374. memcpy(apinfo.ssid, apinfo2->ssid, strlen(apinfo2->ssid) + 1);
  375. if (strlen(apinfo2->password)) {
  376. apinfo.keyinfo.format = 1;
  377. apinfo.keyinfo.key_len = strlen(apinfo2->password);
  378. apinfo.keyinfo.index = 1;
  379. memcpy(apinfo.keyinfo.key, apinfo2->password, strlen(apinfo2->password)+1);
  380. apinfo.encrypt = IEEE80211_ENCRYT_CCMP_WPA2;
  381. }
  382. else {
  383. apinfo.encrypt = IEEE80211_ENCRYT_NONE;
  384. }
  385. if (apinfo2->channel > 0)
  386. apinfo.channel = apinfo2->channel;
  387. else {
  388. apinfo.channel = 6;
  389. }
  390. LLOGD("AP GW %d.%d.%d.%d MASK %d.%d.%d.%d",
  391. ipinfo.ip_addr[0],ipinfo.ip_addr[1],ipinfo.ip_addr[2],ipinfo.ip_addr[3],
  392. ipinfo.netmask[0],ipinfo.netmask[1],ipinfo.netmask[2],ipinfo.netmask[3]
  393. );
  394. // ----------------------------
  395. u8 mac[6] = {0};
  396. tls_get_mac_addr(mac);
  397. sprintf_((char*)ipinfo.dnsname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  398. //------------------------------
  399. u8 wireless_protocol = 0;
  400. tls_param_get(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, TRUE);
  401. // LLOGD("wireless_protocol %d", wireless_protocol);
  402. if (TLS_PARAM_IEEE80211_SOFTAP != wireless_protocol)
  403. {
  404. wireless_protocol = TLS_PARAM_IEEE80211_SOFTAP;
  405. tls_param_set(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, FALSE);
  406. }
  407. ret = tls_wifi_softap_create(&apinfo, &ipinfo);
  408. if (ret)
  409. LLOGD("tls_wifi_softap_create %d", ret);
  410. return ret;
  411. }
  412. extern u8 *wpa_supplicant_get_mac(void);
  413. const char* luat_wlan_get_hostname(int id) {
  414. (void)id;
  415. if (luat_sta_hostname[0] == 0) {
  416. u8* mac_addr = wpa_supplicant_get_mac();
  417. sprintf_(luat_sta_hostname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  418. }
  419. return (const char*)luat_sta_hostname;
  420. }
  421. int luat_wlan_set_hostname(int id, const char* hostname) {
  422. (void)id;
  423. if (hostname == NULL || hostname[0] == 0) {
  424. return 0;
  425. }
  426. memcpy(luat_sta_hostname, hostname, strlen(hostname) + 1);
  427. return 0;
  428. }
  429. int luat_wlan_ap_stop(void) {
  430. #if TLS_CONFIG_AP
  431. tls_wifi_softap_destroy();
  432. #endif
  433. return 0;
  434. }
  435. int luat_wlan_set_station_ip(luat_wlan_station_info_t *info) {
  436. struct tls_ethif *ethif;
  437. struct tls_param_ip param_ip;
  438. if (info == NULL) {
  439. return -1;
  440. }
  441. ethif=tls_netif_get_ethif();
  442. if (ethif == NULL)
  443. {
  444. LLOGE("call wlan.init() first!!");
  445. return -1;
  446. }
  447. if (info->dhcp_enable) {
  448. LLOGD("dhcp enable");
  449. }
  450. else {
  451. LLOGD("dhcp disable");
  452. LLOGD("Sta IP %d.%d.%d.%d MASK %d.%d.%d.%d GW %d.%d.%d.%d",
  453. info->ipv4_addr[0], info->ipv4_addr[1], info->ipv4_addr[2], info->ipv4_addr[3],
  454. info->ipv4_netmask[0],info->ipv4_netmask[1],info->ipv4_netmask[2],info->ipv4_netmask[3],
  455. info->ipv4_gateway[0],info->ipv4_gateway[1],info->ipv4_gateway[2],info->ipv4_gateway[3]
  456. );
  457. }
  458. if (WM_WIFI_JOINED == tls_wifi_get_state())
  459. {
  460. if (info->dhcp_enable) {
  461. /* enable dhcp */
  462. tls_dhcp_start();
  463. } else {
  464. tls_dhcp_stop();
  465. MEMCPY((char *)ip_2_ip4(&ethif->ip_addr) , info->ipv4_addr, 4);
  466. // MEMCPY((char *)ip_2_ip4(&ethif->dns1), &params->dns1, 4);
  467. MEMCPY((char *)ip_2_ip4(&ethif->netmask), info->ipv4_netmask, 4);
  468. MEMCPY((char *)ip_2_ip4(&ethif->gw), info->ipv4_gateway, 4);
  469. tls_netif_set_addr(&ethif->ip_addr, &ethif->netmask, &ethif->gw);
  470. }
  471. }
  472. /* update flash params */
  473. param_ip.dhcp_enable = info->dhcp_enable;
  474. MEMCPY((char *)param_ip.gateway, info->ipv4_gateway, 4);
  475. MEMCPY((char *)param_ip.ip, info->ipv4_addr, 4);
  476. MEMCPY((char *)param_ip.netmask, info->ipv4_netmask, 4);
  477. tls_param_set(TLS_PARAM_ID_IP, (void *)&param_ip, false);
  478. return 0;
  479. }