drv_wifi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-07-25 WCX1024979076 1st version
  9. */
  10. #include <rtthread.h>
  11. #include <string.h>
  12. #include <rtdevice.h>
  13. #ifdef RT_USING_WIFI
  14. #include "drv_wifi.h"
  15. #include "esp_system.h"
  16. #include "esp_wifi.h"
  17. #include "esp_event.h"
  18. #include "esp_log.h"
  19. #include "esp_timer.h"
  20. #include "nvs_flash.h"
  21. #include "esp_private/wifi.h"
  22. #define DBG_LEVEL DBG_LOG
  23. #define LOG_TAG "DRV.WIFI"
  24. #include <rtdbg.h>
  25. #define MAX_ADDR_LEN (6)
  26. struct drv_wifi
  27. {
  28. struct rt_wlan_device *wlan;
  29. wifi_interface_t wifi_if;
  30. rt_uint8_t dev_addr[MAX_ADDR_LEN];
  31. };
  32. static const struct rt_wlan_dev_ops ops;
  33. static struct drv_wifi wifi_sta;
  34. static struct drv_wifi wifi_ap;
  35. #define EXAMPLE_ESP_MAXIMUM_RETRY 5
  36. #define DEFAULT_SCAN_LIST_SIZE 3
  37. static int s_retry_num = 0;
  38. wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
  39. static rt_err_t drv_wlan_scan_stop(struct rt_wlan_device *wlan);
  40. static void wifi_event_handler(void *arg, esp_event_base_t event_base,
  41. int32_t event_id, void *event_data)
  42. {
  43. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) {
  44. rt_wlan_dev_indicate_event_handle(wifi_ap.wlan, RT_WLAN_DEV_EVT_AP_START, RT_NULL);
  45. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STOP) {
  46. rt_wlan_dev_indicate_event_handle(wifi_ap.wlan, RT_WLAN_DEV_EVT_AP_STOP, RT_NULL);
  47. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
  48. wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *) event_data;
  49. rt_wlan_dev_indicate_event_handle(wifi_ap.wlan, RT_WLAN_DEV_EVT_AP_ASSOCIATED, RT_NULL);
  50. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) {
  51. wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *) event_data;
  52. rt_wlan_dev_indicate_event_handle(wifi_ap.wlan, RT_WLAN_DEV_EVT_AP_DISASSOCIATED, RT_NULL);
  53. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  54. esp_wifi_connect();
  55. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
  56. rt_wlan_dev_indicate_event_handle(wifi_sta.wlan, RT_WLAN_DEV_EVT_CONNECT, RT_NULL);
  57. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  58. if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
  59. esp_wifi_connect();
  60. s_retry_num++;
  61. } else {
  62. rt_wlan_dev_indicate_event_handle(wifi_sta.wlan, RT_WLAN_DEV_EVT_CONNECT_FAIL, RT_NULL);
  63. }
  64. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
  65. drv_wlan_scan_stop(RT_NULL);
  66. }
  67. }
  68. static esp_err_t wifi_sta_receive(void *buffer, uint16_t len, void *eb)
  69. {
  70. rt_wlan_dev_report_data(wifi_sta.wlan, buffer, len);
  71. return RT_EOK;
  72. }
  73. static esp_err_t wifi_ap_receive(void *buffer, uint16_t len, void *eb)
  74. {
  75. rt_wlan_dev_report_data(wifi_ap.wlan, buffer, len);
  76. return RT_EOK;
  77. }
  78. void wifi_init_sta(void)
  79. {
  80. }
  81. void wifi_init_softap(void)
  82. {
  83. }
  84. /* Initialize Wi-Fi as sta and set scan method */
  85. static rt_err_t drv_wlan_scan(struct rt_wlan_device *wlan, struct rt_scan_info *scan_info)
  86. {
  87. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  88. ESP_ERROR_CHECK(esp_wifi_start());
  89. esp_wifi_scan_start(NULL, false);
  90. }
  91. static rt_err_t drv_wlan_init(struct rt_wlan_device *wlan)
  92. {
  93. // 初始化时钟以及事件处理
  94. esp_timer_init();
  95. esp_event_loop_create_default();
  96. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  97. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  98. esp_event_handler_instance_t instance_any_id;
  99. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  100. ESP_EVENT_ANY_ID,
  101. &wifi_event_handler,
  102. NULL,
  103. &instance_any_id));
  104. // 注册收到内容回调
  105. ESP_ERROR_CHECK(esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_sta_receive));
  106. ESP_ERROR_CHECK(esp_wifi_internal_reg_rxcb(WIFI_IF_AP, wifi_ap_receive));
  107. return RT_EOK;
  108. }
  109. static rt_err_t drv_wlan_mode(struct rt_wlan_device *wlan, rt_wlan_mode_t mode)
  110. {
  111. if (mode == RT_WLAN_STATION) {
  112. wifi_init_sta();
  113. } else {
  114. wifi_init_softap();
  115. }
  116. return RT_EOK;
  117. }
  118. static rt_err_t drv_wlan_join(struct rt_wlan_device *wlan, struct rt_sta_info *sta_info)
  119. {
  120. wifi_config_t wifi_config = {
  121. .sta = {
  122. .ssid = "",
  123. .password = "",
  124. /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
  125. * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
  126. * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
  127. * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
  128. */
  129. .threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK,
  130. .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
  131. },
  132. };
  133. rt_memcpy(wifi_config.sta.ssid, sta_info->ssid.val, sta_info->ssid.len);
  134. rt_memcpy(wifi_config.sta.password, sta_info->key.val, sta_info->key.len);
  135. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  136. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  137. ESP_ERROR_CHECK(esp_wifi_start());
  138. return RT_EOK;
  139. }
  140. static rt_err_t drv_wlan_softap(struct rt_wlan_device *wlan, struct rt_ap_info *ap_info)
  141. {
  142. wifi_config_t wifi_config = {
  143. .ap = {
  144. .ssid = "",
  145. .ssid_len = 0,
  146. .channel = 3,
  147. .password = "",
  148. .max_connection = 3,
  149. .authmode = WIFI_AUTH_WPA_WPA2_PSK,
  150. .pmf_cfg = {
  151. .required = false,
  152. },
  153. },
  154. };
  155. rt_memcpy(wifi_config.ap.ssid, ap_info->ssid.val, ap_info->ssid.len);
  156. rt_memcpy(wifi_config.ap.password, ap_info->key.val, ap_info->key.len);
  157. wifi_config.ap.ssid_len = ap_info->ssid.len;
  158. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  159. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
  160. ESP_ERROR_CHECK(esp_wifi_start());
  161. drv_wlan_scan_stop(RT_NULL);
  162. return RT_EOK;
  163. }
  164. static rt_err_t drv_wlan_disconnect(struct rt_wlan_device *wlan)
  165. {
  166. ESP_ERROR_CHECK(esp_wifi_disconnect());
  167. return RT_EOK;
  168. }
  169. static rt_err_t drv_wlan_ap_stop(struct rt_wlan_device *wlan)
  170. {
  171. return RT_EOK;
  172. }
  173. static rt_err_t drv_wlan_ap_deauth(struct rt_wlan_device *wlan, rt_uint8_t mac[])
  174. {
  175. return RT_EOK;
  176. }
  177. static rt_err_t drv_wlan_scan_stop(struct rt_wlan_device *wlan)
  178. {
  179. struct rt_wlan_info wlan_info;
  180. struct rt_wlan_buff buff;
  181. uint16_t number = DEFAULT_SCAN_LIST_SIZE;
  182. uint16_t ap_count = 0;
  183. memset(ap_info, 0, sizeof(ap_info));
  184. ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
  185. ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
  186. for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i++) {
  187. rt_memset(&wlan_info, 0, sizeof(wlan_info));
  188. rt_memcpy(&wlan_info.bssid[0], ap_info[i].bssid, 6);
  189. rt_memcpy(wlan_info.ssid.val, ap_info[i].ssid , strlen(ap_info[i].ssid));
  190. wlan_info.ssid.len = strlen(ap_info[i].ssid);
  191. wlan_info.hidden = 0;
  192. wlan_info.channel = (rt_int16_t)ap_info[i].primary;
  193. wlan_info.rssi = -(char)(0x100 - ap_info[i].rssi);
  194. wlan_info.band = RT_802_11_BAND_2_4GHZ;
  195. wlan_info.security = SECURITY_UNKNOWN;
  196. buff.data = &wlan_info;
  197. buff.len = sizeof(wlan_info);
  198. rt_wlan_dev_indicate_event_handle(wifi_sta.wlan, RT_WLAN_DEV_EVT_SCAN_REPORT, &buff);
  199. }
  200. esp_wifi_scan_stop();
  201. return RT_EOK;
  202. }
  203. static int drv_wlan_get_rssi(struct rt_wlan_device *wlan)
  204. {
  205. return 0;
  206. }
  207. static rt_err_t drv_wlan_set_powersave(struct rt_wlan_device *wlan, int level)
  208. {
  209. return RT_EOK;
  210. }
  211. static int drv_wlan_get_powersave(struct rt_wlan_device *wlan)
  212. {
  213. return 0;
  214. }
  215. static rt_err_t drv_wlan_cfg_promisc(struct rt_wlan_device *wlan, rt_bool_t start)
  216. {
  217. return RT_EOK;
  218. }
  219. static rt_err_t drv_wlan_cfg_filter(struct rt_wlan_device *wlan, struct rt_wlan_filter *filter)
  220. {
  221. return -RT_EINVAL;/* not support */
  222. }
  223. static rt_err_t drv_wlan_set_channel(struct rt_wlan_device *wlan, int channel)
  224. {
  225. return RT_EOK;
  226. }
  227. static int drv_wlan_get_channel(struct rt_wlan_device *wlan)
  228. {
  229. return 0;
  230. }
  231. static rt_err_t drv_wlan_set_country(struct rt_wlan_device *wlan, rt_country_code_t country_code)
  232. {
  233. return RT_EOK;
  234. }
  235. static rt_country_code_t drv_wlan_get_country(struct rt_wlan_device *wlan)
  236. {
  237. return 0; //RT_EOK;
  238. }
  239. static rt_err_t drv_wlan_set_mac(struct rt_wlan_device *wlan, rt_uint8_t mac[])
  240. {
  241. return RT_EOK;
  242. }
  243. static rt_err_t drv_wlan_get_mac(struct rt_wlan_device *wlan, rt_uint8_t mac[])
  244. {
  245. return RT_EOK;
  246. }
  247. static int drv_wlan_recv(struct rt_wlan_device *wlan, void *buff, int len)
  248. {
  249. return RT_EOK;
  250. }
  251. static int drv_wlan_send(struct rt_wlan_device *wlan, void *buff, int len)
  252. {
  253. struct drv_wifi* wifi = wlan->user_data;
  254. esp_wifi_internal_tx(wifi->wifi_if, buff, len);
  255. return RT_EOK;
  256. }
  257. static const struct rt_wlan_dev_ops ops =
  258. {
  259. .wlan_init = drv_wlan_init,
  260. .wlan_mode = drv_wlan_mode,
  261. .wlan_scan = drv_wlan_scan,
  262. .wlan_join = drv_wlan_join,
  263. .wlan_softap = drv_wlan_softap,
  264. .wlan_disconnect = drv_wlan_disconnect,
  265. .wlan_ap_stop = drv_wlan_ap_stop,
  266. .wlan_ap_deauth = drv_wlan_ap_deauth,
  267. .wlan_scan_stop = drv_wlan_scan_stop,
  268. .wlan_get_rssi = drv_wlan_get_rssi,
  269. .wlan_set_powersave = drv_wlan_set_powersave,
  270. .wlan_get_powersave = drv_wlan_get_powersave,
  271. .wlan_cfg_promisc = drv_wlan_cfg_promisc,
  272. .wlan_cfg_filter = drv_wlan_cfg_filter,
  273. .wlan_set_channel = drv_wlan_set_channel,
  274. .wlan_get_channel = drv_wlan_get_channel,
  275. .wlan_set_country = drv_wlan_set_country,
  276. .wlan_get_country = drv_wlan_get_country,
  277. .wlan_set_mac = drv_wlan_set_mac,
  278. .wlan_get_mac = drv_wlan_get_mac,
  279. .wlan_recv = drv_wlan_recv,
  280. .wlan_send = drv_wlan_send,
  281. };
  282. int rt_hw_wifi_init(void)
  283. {
  284. // 初始化nvs_flash
  285. esp_err_t esp_ret = nvs_flash_init();
  286. if (esp_ret == ESP_ERR_NVS_NO_FREE_PAGES || esp_ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  287. ESP_ERROR_CHECK(nvs_flash_erase());
  288. esp_ret = nvs_flash_init();
  289. }
  290. ESP_ERROR_CHECK(esp_ret);
  291. // 向系统注册
  292. static struct rt_wlan_device wlan;
  293. static struct rt_wlan_device wlan2;
  294. rt_memset(&wifi_sta, 0, sizeof(wifi_sta));
  295. wifi_sta.wifi_if = WIFI_IF_STA;
  296. rt_err_t ret = rt_wlan_dev_register(&wlan, RT_WLAN_DEVICE_STA_NAME, &ops, 0, &wifi_sta);
  297. wifi_sta.wlan = &wlan;
  298. rt_memset(&wifi_ap, 0, sizeof(wifi_ap));
  299. wifi_ap.wifi_if = WIFI_IF_AP;
  300. ret |= rt_wlan_dev_register(&wlan2, RT_WLAN_DEVICE_AP_NAME, &ops, 0, &wifi_ap);
  301. wifi_ap.wlan = &wlan2;
  302. return ret;
  303. }
  304. INIT_DEVICE_EXPORT(rt_hw_wifi_init);
  305. #endif /* RT_USING_WIFI */