luat_wlan_air101.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. u8 ssid[33]= {0};
  28. u8 pwd[65] = {0};
  29. char sta_ip[16] = {0};
  30. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  31. lua_getglobal(L, "sys_pub");
  32. switch (msg->arg1)
  33. {
  34. case NETIF_IP_NET_UP:
  35. #ifdef LUAT_USE_NETWORK
  36. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 1);
  37. #endif
  38. luat_wlan_get_ip(0, sta_ip);
  39. LLOGD("sta ip %s", sta_ip);
  40. lua_pushstring(L, "IP_READY");
  41. lua_pushstring(L, sta_ip);
  42. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  43. lua_call(L, 3, 0);
  44. break;
  45. case NETIF_WIFI_DISCONNECTED:
  46. #ifdef LUAT_USE_NETWORK
  47. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_STA, 0);
  48. #endif
  49. lua_pushstring(L, "IP_LOSE");
  50. lua_call(L, 1, 0); // 暂时只发个IP_LOSE
  51. break;
  52. case SCAN_DONE :
  53. lua_pushstring(L, "WLAN_SCAN_DONE");
  54. lua_call(L, 1, 0);
  55. break;
  56. case ONESHOT_RESULT:
  57. lua_pushstring(L, "SC_RESULT");
  58. tls_wifi_get_oneshot_ssidpwd(ssid, pwd);
  59. LLOGD("oneshot %s %s", ssid, pwd);
  60. lua_pushstring(L, ssid);
  61. lua_pushstring(L, pwd);
  62. lua_call(L, 3, 0);
  63. break;
  64. default:
  65. break;
  66. }
  67. return 0;
  68. }
  69. static void netif_event_cb(u8 status) {
  70. rtos_msg_t msg = {0};
  71. LLOGD("netif_event %d", status);
  72. msg.handler = l_wlan_cb;
  73. switch (status)
  74. {
  75. case NETIF_WIFI_JOIN_FAILED:
  76. LLOGI("join failed");
  77. tls_auto_reconnect(3);
  78. break;
  79. case NETIF_WIFI_DISCONNECTED:
  80. wlan_state = 0;
  81. LLOGI("disconnected");
  82. msg.arg1 = status;
  83. luat_msgbus_put(&msg, 0);
  84. break;
  85. case NETIF_WIFI_JOIN_SUCCESS :
  86. wlan_state = 1;
  87. LLOGI("join success");
  88. break;
  89. case NETIF_IP_NET_UP :
  90. LLOGI("IP READY");
  91. msg.arg1 = status;
  92. luat_msgbus_put(&msg, 0);
  93. break;
  94. case NETIF_WIFI_SOFTAP_SUCCESS :
  95. LLOGI("softap create success");
  96. #ifdef LUAT_USE_NETWORK
  97. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 1);
  98. #endif
  99. break;
  100. case NETIF_WIFI_SOFTAP_FAILED:
  101. LLOGI("softap create failed");
  102. #ifdef LUAT_USE_NETWORK
  103. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 0);
  104. #endif
  105. break;
  106. case NETIF_WIFI_SOFTAP_CLOSED:
  107. LLOGI("softap create closed");
  108. #ifdef LUAT_USE_NETWORK
  109. net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_WIFI_AP, 0);
  110. #endif
  111. break;
  112. case NETIF_IP_NET2_UP :
  113. LLOGI("softap netif up");
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. int luat_wlan_init(luat_wlan_config_t *conf) {
  120. if (wlan_init == 0) {
  121. u8 wireless_protocol = 0;
  122. tls_param_get(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, TRUE);
  123. // LLOGD("wireless_protocol %d", wireless_protocol);
  124. if (TLS_PARAM_IEEE80211_INFRA != wireless_protocol)
  125. {
  126. wireless_protocol = TLS_PARAM_IEEE80211_INFRA;
  127. tls_param_set(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, FALSE);
  128. }
  129. //tls_wifi_enable_log(1);
  130. luat_wlan_get_hostname(0); // 调用一下就行
  131. wlan_init = 1;
  132. tls_netif_add_status_event(netif_event_cb);
  133. tls_wifi_scan_result_cb_register(NULL);
  134. #ifdef LUAT_USE_NETWORK
  135. struct netif *et0 = tls_get_netif();
  136. net_lwip_set_netif(et0, NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  137. #if TLS_CONFIG_AP
  138. extern struct netif *nif4apsta;
  139. if (nif4apsta) {
  140. net_lwip_set_netif(nif4apsta, NW_ADAPTER_INDEX_LWIP_WIFI_AP);
  141. net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_AP);
  142. }
  143. #endif
  144. net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_WIFI_STA);
  145. #endif
  146. }
  147. tls_wifi_set_psflag(FALSE, FALSE);
  148. return 0;
  149. }
  150. int luat_wlan_mode(luat_wlan_config_t *conf) {
  151. // 不需要设置, 反正都能用
  152. return 0;
  153. }
  154. int luat_wlan_ready(void) {
  155. return wlan_state;
  156. }
  157. int luat_wlan_connect(luat_wlan_conninfo_t* info) {
  158. tls_wifi_connect(info->ssid, strlen(info->ssid), info->password, strlen(info->password));
  159. u8 opt = WIFI_AUTO_CNT_FLAG_SET;
  160. u8 mode = WIFI_AUTO_CNT_ON;
  161. tls_wifi_auto_connect_flag(opt, &mode);
  162. return 0;
  163. }
  164. int luat_wlan_disconnect(void) {
  165. u8 opt = WIFI_AUTO_CNT_FLAG_SET;
  166. u8 mode = WIFI_AUTO_CNT_OFF;
  167. tls_wifi_auto_connect_flag(opt, &mode);
  168. tls_wifi_disconnect();
  169. return 0;
  170. }
  171. static void scan_event_cb(void *ptmr, void *parg) {
  172. rtos_msg_t msg = {0};
  173. msg.handler = l_wlan_cb;
  174. msg.arg1 = SCAN_DONE;
  175. luat_msgbus_put(&msg, 0);
  176. }
  177. int luat_wlan_scan(void) {
  178. int ret = tls_wifi_scan();
  179. LLOGD("tls_wifi_scan %d", ret);
  180. static tls_os_timer_t *scan_timer = NULL;
  181. tls_os_timer_create(&scan_timer, scan_event_cb, NULL, 3000, 0, NULL);
  182. tls_os_timer_start(scan_timer);
  183. return ret;
  184. }
  185. int luat_wlan_scan_get_result(luat_wlan_scan_result_t *results, int ap_limit) {
  186. size_t buffsize = ap_limit * 48 + 8;
  187. u8* buff = luat_heap_malloc(buffsize);
  188. if (buff == NULL)
  189. return 0;
  190. memset(buff, 0, buffsize);
  191. tls_wifi_get_scan_rslt((u8*)buff, buffsize);
  192. struct tls_scan_bss_t *wsr = (struct tls_scan_bss_t*)buff;
  193. struct tls_bss_info_t *bss_info;
  194. if (ap_limit > wsr->count)
  195. ap_limit = wsr->count;
  196. bss_info = wsr->bss;
  197. for (size_t i = 0; i < ap_limit; i++)
  198. {
  199. memset(results[i].ssid, 0, 33);
  200. memcpy(results[i].ssid, bss_info[i].ssid, bss_info[i].ssid_len);
  201. memcpy(results[i].bssid, bss_info[i].bssid, ETH_ALEN);
  202. results[i].rssi = bss_info[i].rssi;
  203. results[i].ch = bss_info[i].channel;
  204. // break;
  205. }
  206. luat_heap_free(buff);
  207. return ap_limit;
  208. }
  209. static void oneshot_result_callback(enum tls_wifi_oneshot_result_type type) {
  210. if (type == WM_WIFI_ONESHOT_TYPE_SSIDPWD) {
  211. LLOGD("oneshot Got!!");
  212. rtos_msg_t msg = {.handler = l_wlan_cb, .arg1=ONESHOT_RESULT};
  213. luat_msgbus_put(&msg, 0);
  214. }
  215. }
  216. int luat_wlan_smartconfig_start(int tp) {
  217. tls_wifi_oneshot_result_cb_register(oneshot_result_callback);
  218. return tls_wifi_set_oneshot_flag(1);
  219. }
  220. int luat_wlan_smartconfig_stop(void) {
  221. return tls_wifi_set_oneshot_flag(0);
  222. }
  223. // 数据类
  224. int luat_wlan_get_mac(int id, char* mac) {
  225. tls_get_mac_addr(mac);
  226. return 0;
  227. }
  228. int luat_wlan_set_mac(int id, char* mac) {
  229. tls_set_mac_addr(mac);
  230. return 0;
  231. }
  232. int luat_wlan_get_ip(int type, char* data) {
  233. struct netif *et0 = tls_get_netif();
  234. if (et0 == NULL || et0->ip_addr.addr == 0)
  235. return -1;
  236. ipaddr_ntoa_r(&et0->ip_addr, data, 16);
  237. return 0;
  238. }
  239. // 设置和获取省电模式
  240. int luat_wlan_set_ps(int mode) {
  241. tls_wifi_set_psm_chipsleep_flag(mode == 0 ? 0 : 1);
  242. return 0;
  243. }
  244. int luat_wlan_get_ps(void) {
  245. return tls_wifi_get_psm_chipsleep_flag();
  246. }
  247. int luat_wlan_get_ap_bssid(char* buff) {
  248. struct tls_curr_bss_t bss = {0};
  249. tls_wifi_get_current_bss(&bss);
  250. memcpy(buff, bss.bssid, ETH_ALEN);
  251. return 0;
  252. }
  253. int luat_wlan_get_ap_rssi(void) {
  254. struct tls_curr_bss_t bss = {0};
  255. tls_wifi_get_current_bss(&bss);
  256. return bss.rssi;
  257. }
  258. int luat_wlan_get_ap_gateway(char* buff) {
  259. struct netif *et0 = tls_get_netif();
  260. if (et0 == NULL || et0->ip_addr.addr == 0)
  261. return -1;
  262. ipaddr_ntoa_r(&et0->gw, buff, 16);
  263. return 0;
  264. }
  265. // AP类
  266. int luat_wlan_ap_start(luat_wlan_apinfo_t *apinfo2) {
  267. int ret = 0;
  268. struct tls_softap_info_t apinfo = {0};
  269. struct tls_ip_info_t ipinfo = {
  270. .ip_addr = {192, 168, 4, 1},
  271. .netmask = {255, 255, 255, 0}
  272. };
  273. if (apinfo2->gateway > 0) {
  274. memcpy(ipinfo.ip_addr, apinfo2->gateway, 4);
  275. }
  276. if (apinfo2->netmask > 0) {
  277. memcpy(ipinfo.netmask, apinfo2->netmask, 4);
  278. }
  279. memcpy(apinfo.ssid, apinfo2->ssid, strlen(apinfo2->ssid) + 1);
  280. if (strlen(apinfo2->password)) {
  281. apinfo.keyinfo.format = 1;
  282. apinfo.keyinfo.key_len = strlen(apinfo2->password);
  283. apinfo.keyinfo.index = 1;
  284. memcpy(apinfo.keyinfo.key, apinfo2->password, strlen(apinfo2->password)+1);
  285. apinfo.encrypt = IEEE80211_ENCRYT_CCMP_WPA2;
  286. }
  287. else {
  288. apinfo.encrypt = IEEE80211_ENCRYT_NONE;
  289. }
  290. if (apinfo2->channel > 0)
  291. apinfo.channel = apinfo2->channel;
  292. else {
  293. apinfo.channel = 6;
  294. }
  295. LLOGD("AP GW %d.%d.%d.%d MASK %d.%d.%d.%d",
  296. ipinfo.ip_addr[0],ipinfo.ip_addr[1],ipinfo.ip_addr[2],ipinfo.ip_addr[3],
  297. ipinfo.netmask[0],ipinfo.netmask[1],ipinfo.netmask[2],ipinfo.netmask[3]
  298. );
  299. // ----------------------------
  300. u8 mac[6] = {0};
  301. tls_get_mac_addr(mac);
  302. sprintf_(ipinfo.dnsname, "LUATOS_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  303. //------------------------------
  304. u8 wireless_protocol = 0;
  305. tls_param_get(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, TRUE);
  306. // LLOGD("wireless_protocol %d", wireless_protocol);
  307. if (TLS_PARAM_IEEE80211_SOFTAP != wireless_protocol)
  308. {
  309. wireless_protocol = TLS_PARAM_IEEE80211_SOFTAP;
  310. tls_param_set(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, FALSE);
  311. }
  312. ret = tls_wifi_softap_create(&apinfo, &ipinfo);
  313. LLOGD("tls_wifi_softap_create %d", ret);
  314. return ret;
  315. }
  316. const char* luat_wlan_get_hostname(int id) {
  317. if (luat_sta_hostname[0] == 0) {
  318. u8* mac_addr = wpa_supplicant_get_mac();
  319. 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]);
  320. }
  321. return (const char*)luat_sta_hostname;
  322. }
  323. int luat_wlan_set_hostname(int id, char* hostname) {
  324. if (hostname == NULL || hostname[0] == 0) {
  325. return 0;
  326. }
  327. memcpy(luat_sta_hostname, hostname, strlen(hostname) + 1);
  328. return 0;
  329. }