luat_wlan_air101.c 7.2 KB

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