itwt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /* itwt Example
  7. This example code is in the Public Domain (or CC0 licensed, at your option.)
  8. Unless required by applicable law or agreed to in writing, this
  9. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  10. CONDITIONS OF ANY KIND, either express or implied.
  11. */
  12. /*
  13. this example shows how to use itwt
  14. set a router or a AP using the same SSID&PASSWORD as configuration of this example.
  15. start esp32c6 and when it connected to AP it will setup itwt.
  16. */
  17. #include <netdb.h>
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/event_groups.h"
  20. #include "esp_wifi.h"
  21. #include "esp_log.h"
  22. #include "esp_event.h"
  23. #include "nvs_flash.h"
  24. #include "esp_console.h"
  25. #include "cmd_system.h"
  26. #include "wifi_cmd.h"
  27. #include "esp_wifi_he.h"
  28. #include "esp_pm.h"
  29. #include "esp_timer.h"
  30. /*******************************************************
  31. * Constants
  32. *******************************************************/
  33. static const char *TAG = "itwt";
  34. /*******************************************************
  35. * Structures
  36. *******************************************************/
  37. /*******************************************************
  38. * Variable Definitions
  39. *******************************************************/
  40. /*set the ssid and password via "idf.py menuconfig"*/
  41. #define DEFAULT_SSID CONFIG_EXAMPLE_WIFI_SSID
  42. #define DEFAULT_PWD CONFIG_EXAMPLE_WIFI_PASSWORD
  43. #define ITWT_SETUP_SUCCESS 1
  44. #if CONFIG_EXAMPLE_ITWT_TRIGGER_ENABLE
  45. uint8_t trigger_enabled = 1;
  46. #else
  47. uint8_t trigger_enabled = 0;
  48. #endif
  49. #if CONFIG_EXAMPLE_ITWT_ANNOUNCED
  50. uint8_t flow_type_announced = 1;
  51. #else
  52. uint8_t flow_type_announced = 0;
  53. #endif
  54. esp_netif_t *netif_sta = NULL;
  55. const int CONNECTED_BIT = BIT0;
  56. const int DISCONNECTED_BIT = BIT1;
  57. EventGroupHandle_t wifi_event_group;
  58. /*******************************************************
  59. * Function Declarations
  60. *******************************************************/
  61. /*******************************************************
  62. * Function Definitions
  63. *******************************************************/
  64. static void example_set_static_ip(esp_netif_t *netif)
  65. {
  66. #if CONFIG_EXAMPLE_ENABLE_STATIC_IP
  67. if (esp_netif_dhcpc_stop(netif) != ESP_OK) {
  68. ESP_LOGE(TAG, "Failed to stop dhcp client");
  69. return;
  70. }
  71. esp_netif_ip_info_t ip;
  72. memset(&ip, 0 , sizeof(esp_netif_ip_info_t));
  73. ip.ip.addr = ipaddr_addr(CONFIG_EXAMPLE_STATIC_IP_ADDR);
  74. ip.netmask.addr = ipaddr_addr(CONFIG_EXAMPLE_STATIC_NETMASK_ADDR);
  75. ip.gw.addr = ipaddr_addr(CONFIG_EXAMPLE_STATIC_GW_ADDR);
  76. if (esp_netif_set_ip_info(netif, &ip) != ESP_OK) {
  77. ESP_LOGE(TAG, "Failed to set ip info");
  78. return;
  79. }
  80. ESP_LOGI(TAG, "Success to set static ip: %s, netmask: %s, gw: %s",
  81. CONFIG_EXAMPLE_STATIC_IP_ADDR, CONFIG_EXAMPLE_STATIC_NETMASK_ADDR, CONFIG_EXAMPLE_STATIC_GW_ADDR);
  82. #endif
  83. }
  84. static const char *itwt_probe_status_to_str(wifi_itwt_probe_status_t status)
  85. {
  86. switch (status) {
  87. case ITWT_PROBE_FAIL: return "itwt probe fail";
  88. case ITWT_PROBE_SUCCESS: return "itwt probe success";
  89. case ITWT_PROBE_TIMEOUT: return "itwt probe timeout";
  90. case ITWT_PROBE_STA_DISCONNECTED: return "Sta disconnected";
  91. default: return "Unknown status";
  92. }
  93. }
  94. static void got_ip_handler(void *arg, esp_event_base_t event_base,
  95. int32_t event_id, void *event_data)
  96. {
  97. xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT);
  98. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  99. /* setup a trigger-based announce individual TWT agreement. */
  100. wifi_phy_mode_t phymode;
  101. wifi_config_t sta_cfg = { 0, };
  102. esp_wifi_get_config(WIFI_IF_STA, &sta_cfg);
  103. esp_wifi_sta_get_negotiated_phymode(&phymode);
  104. if (phymode == WIFI_PHY_MODE_HE20) {
  105. esp_err_t err = ESP_OK;
  106. wifi_twt_setup_config_t setup_config = {
  107. .setup_cmd = TWT_REQUEST,
  108. .flow_id = 0,
  109. .twt_id = CONFIG_EXAMPLE_ITWT_ID,
  110. .flow_type = flow_type_announced ? 0 : 1,
  111. .min_wake_dura = CONFIG_EXAMPLE_ITWT_MIN_WAKE_DURA,
  112. .wake_duration_unit = CONFIG_EXAMPLE_ITWT_WAKE_DURATION_UNIT,
  113. .wake_invl_expn = CONFIG_EXAMPLE_ITWT_WAKE_INVL_EXPN,
  114. .wake_invl_mant = CONFIG_EXAMPLE_ITWT_WAKE_INVL_MANT,
  115. .trigger = trigger_enabled,
  116. .timeout_time_ms = CONFIG_EXAMPLE_ITWT_SETUP_TIMEOUT_TIME_MS,
  117. };
  118. err = esp_wifi_sta_itwt_setup(&setup_config);
  119. if (err != ESP_OK) {
  120. ESP_LOGE(TAG, "itwt setup failed, err:0x%x", err);
  121. }
  122. } else {
  123. ESP_LOGE(TAG, "Must be in 11ax mode to support itwt");
  124. }
  125. }
  126. static void start_handler(void *arg, esp_event_base_t event_base,
  127. int32_t event_id, void *event_data)
  128. {
  129. ESP_LOGI(TAG, "sta connect to %s", DEFAULT_SSID);
  130. esp_wifi_connect();
  131. }
  132. static void disconnect_handler(void *arg, esp_event_base_t event_base,
  133. int32_t event_id, void *event_data)
  134. {
  135. ESP_LOGI(TAG, "sta disconnect, reconnect...");
  136. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  137. esp_wifi_connect();
  138. }
  139. static void itwt_setup_handler(void *arg, esp_event_base_t event_base,
  140. int32_t event_id, void *event_data)
  141. {
  142. wifi_event_sta_itwt_setup_t *setup = (wifi_event_sta_itwt_setup_t *) event_data;
  143. if (setup->status == ITWT_SETUP_SUCCESS) {
  144. /* TWT Wake Interval = TWT Wake Interval Mantissa * (2 ^ TWT Wake Interval Exponent) */
  145. ESP_LOGI(TAG, "<WIFI_EVENT_ITWT_SETUP>twt_id:%d, flow_id:%d, %s, %s, wake_dura:%d, wake_dura_unit:%d, wake_invl_e:%d, wake_invl_m:%d", setup->config.twt_id,
  146. setup->config.flow_id, setup->config.trigger ? "trigger-enabled" : "non-trigger-enabled", setup->config.flow_type ? "unannounced" : "announced",
  147. setup->config.min_wake_dura, setup->config.wake_duration_unit, setup->config.wake_invl_expn, setup->config.wake_invl_mant);
  148. ESP_LOGI(TAG, "<WIFI_EVENT_ITWT_SETUP>target wake time:%lld, wake duration:%d us, service period:%d us", setup->target_wake_time, setup->config.min_wake_dura << (setup->config.wake_duration_unit == 1 ? 10 : 8),
  149. setup->config.wake_invl_mant << setup->config.wake_invl_expn);
  150. } else {
  151. if (setup->status == ESP_ERR_WIFI_TWT_SETUP_TIMEOUT) {
  152. ESP_LOGE(TAG, "<WIFI_EVENT_ITWT_SETUP>twt_id:%d, timeout of receiving twt setup response frame", setup->config.twt_id);
  153. } else if (setup->status == ESP_ERR_WIFI_TWT_SETUP_TXFAIL) {
  154. ESP_LOGE(TAG, "<WIFI_EVENT_ITWT_SETUP>twt_id:%d, twt setup frame tx failed, reason: %d", setup->config.twt_id, setup->reason);
  155. } else if (setup->status == ESP_ERR_WIFI_TWT_SETUP_REJECT) {
  156. ESP_LOGE(TAG, "<WIFI_EVENT_ITWT_SETUP>twt_id:%d, twt setup request was rejected, setup cmd: %d", setup->config.twt_id, setup->config.setup_cmd);
  157. } else {
  158. ESP_LOGE(TAG, "<WIFI_EVENT_ITWT_SETUP>twt_id:%d, twt setup failed, status: %d", setup->config.twt_id, setup->status);
  159. }
  160. }
  161. }
  162. static void itwt_teardown_handler(void *arg, esp_event_base_t event_base,
  163. int32_t event_id, void *event_data)
  164. {
  165. wifi_event_sta_itwt_teardown_t *teardown = (wifi_event_sta_itwt_teardown_t *) event_data;
  166. ESP_LOGI(TAG, "<WIFI_EVENT_ITWT_TEARDOWN>flow_id %d%s", teardown->flow_id, (teardown->flow_id == 8) ? "(all twt)" : "");
  167. }
  168. static void itwt_suspend_handler(void *arg, esp_event_base_t event_base,
  169. int32_t event_id, void *event_data)
  170. {
  171. wifi_event_sta_itwt_suspend_t *suspend = (wifi_event_sta_itwt_suspend_t *) event_data;
  172. ESP_LOGI(TAG, "<WIFI_EVENT_ITWT_SUSPEND>status:%d, flow_id_bitmap:0x%x, actual_suspend_time_ms:[%lu %lu %lu %lu %lu %lu %lu %lu]",
  173. suspend->status, suspend->flow_id_bitmap,
  174. suspend->actual_suspend_time_ms[0], suspend->actual_suspend_time_ms[1], suspend->actual_suspend_time_ms[2], suspend->actual_suspend_time_ms[3],
  175. suspend->actual_suspend_time_ms[4], suspend->actual_suspend_time_ms[5], suspend->actual_suspend_time_ms[6], suspend->actual_suspend_time_ms[7]);
  176. }
  177. static void itwt_probe_handler(void *arg, esp_event_base_t event_base,
  178. int32_t event_id, void *event_data)
  179. {
  180. wifi_event_sta_itwt_probe_t *probe = (wifi_event_sta_itwt_probe_t *) event_data;
  181. ESP_LOGI(TAG, "<WIFI_EVENT_ITWT_PROBE>status:%s, reason:0x%x", itwt_probe_status_to_str(probe->status), probe->reason);
  182. }
  183. static void wifi_itwt(void)
  184. {
  185. ESP_ERROR_CHECK(esp_netif_init());
  186. wifi_event_group = xEventGroupCreate();
  187. ESP_ERROR_CHECK(esp_event_loop_create_default());
  188. netif_sta = esp_netif_create_default_wifi_sta();
  189. assert(netif_sta);
  190. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  191. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  192. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  193. WIFI_EVENT_STA_START,
  194. &start_handler,
  195. NULL,
  196. NULL));
  197. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  198. WIFI_EVENT_STA_DISCONNECTED,
  199. &disconnect_handler,
  200. NULL,
  201. NULL));
  202. ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
  203. IP_EVENT_STA_GOT_IP,
  204. &got_ip_handler,
  205. NULL,
  206. NULL));
  207. /* itwt */
  208. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  209. WIFI_EVENT_ITWT_SETUP,
  210. &itwt_setup_handler,
  211. NULL,
  212. NULL));
  213. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  214. WIFI_EVENT_ITWT_TEARDOWN,
  215. &itwt_teardown_handler,
  216. NULL,
  217. NULL));
  218. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  219. WIFI_EVENT_ITWT_SUSPEND,
  220. &itwt_suspend_handler,
  221. NULL,
  222. NULL));
  223. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  224. WIFI_EVENT_ITWT_PROBE,
  225. &itwt_probe_handler,
  226. NULL,
  227. NULL));
  228. wifi_config_t wifi_config = {
  229. .sta = {
  230. .ssid = DEFAULT_SSID,
  231. .password = DEFAULT_PWD,
  232. },
  233. };
  234. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  235. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  236. esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT20);
  237. esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AX);
  238. esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
  239. #if CONFIG_EXAMPLE_ENABLE_STATIC_IP
  240. example_set_static_ip(netif_sta);
  241. #endif
  242. ESP_ERROR_CHECK(esp_wifi_start());
  243. #if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_STATS
  244. #if CONFIG_ESP_WIFI_ENABLE_WIFI_RX_MU_STATS
  245. esp_wifi_enable_rx_statistics(true, true);
  246. #else
  247. esp_wifi_enable_rx_statistics(true, false);
  248. #endif
  249. #endif
  250. #if CONFIG_ESP_WIFI_ENABLE_WIFI_TX_STATS
  251. esp_wifi_enable_tx_statistics(ESP_WIFI_ACI_VO, true); //VO, mgmt
  252. esp_wifi_enable_tx_statistics(ESP_WIFI_ACI_BE, true); //BE, data
  253. #endif
  254. }
  255. void app_main(void)
  256. {
  257. // Initialize NVS
  258. esp_err_t ret = nvs_flash_init();
  259. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  260. ESP_ERROR_CHECK(nvs_flash_erase());
  261. ret = nvs_flash_init();
  262. }
  263. #if CONFIG_PM_ENABLE
  264. // Configure dynamic frequency scaling:
  265. // maximum and minimum frequencies are set in sdkconfig,
  266. // automatic light sleep is enabled if tickless idle support is enabled.
  267. esp_pm_config_t pm_config = {
  268. .max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ,
  269. .min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ,
  270. #if CONFIG_FREERTOS_USE_TICKLESS_IDLE
  271. .light_sleep_enable = true
  272. #endif
  273. };
  274. ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
  275. ESP_ERROR_CHECK( ret );
  276. #else
  277. printf("\n =================================================\n");
  278. printf(" | Test WiFi 6 itwt |\n");
  279. printf(" =================================================\n\n");
  280. esp_console_repl_t *repl = NULL;
  281. esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
  282. repl_config.prompt = "itwt>";
  283. // init console REPL environment
  284. #if CONFIG_ESP_CONSOLE_UART
  285. esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
  286. ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
  287. #elif CONFIG_ESP_CONSOLE_USB_CDC
  288. esp_console_dev_usb_cdc_config_t cdc_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
  289. ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&cdc_config, &repl_config, &repl));
  290. #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  291. esp_console_dev_usb_serial_jtag_config_t usbjtag_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
  292. ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&usbjtag_config, &repl_config, &repl));
  293. #endif
  294. // start console REPL
  295. ESP_ERROR_CHECK(esp_console_start_repl(repl));
  296. #endif
  297. //start wifi
  298. wifi_itwt();
  299. // register commands
  300. register_system();
  301. register_wifi_itwt();
  302. register_wifi_stats();
  303. }