luat_wlan_air101.c 8.4 KB

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