luat_wlan_air101.c 14 KB

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