Wifi FTM stops after 2 or 3 minutes

JJ8428
Posts: 9
Joined: Fri Jul 28, 2023 3:05 am

Wifi FTM stops after 2 or 3 minutes

Postby JJ8428 » Thu Aug 10, 2023 4:54 pm

Hey, I have 2 ESP32s2 on my hand. One is acting as an initiator. The other is acting as the responder. The wifi FTM works for a while, however, after 2 or 3 minutes, the FTM stops. I'm not exactly sure what is going on. I don't believe my code has any data leaks, and I browsed the forums to find a similar post (viewtopic.php?t=24226), but there is no solution or follow up afterwards. Does anyone have any input on what I could be overlooking? I am fairly certain its the responder having some issue. I attached 2 screenshots which shows what each board with their respective code, outputs to the terminal. In the responder screenshot, everything seems fine, but for the initiator screenshot, it just stops in the middle of a FTM request to the SoftAP at the responder.

FTM Responder Code: Just serves as a Soft AP for the FTM initiator to connect to
  1. #include "esp_log.h"
  2. #include "esp_err.h"
  3. #include "esp_wifi.h"
  4. #include "nvs_flash.h"
  5. #include "sdkconfig.h"
  6. #include <string.h>
  7.  
  8. // Wifi Credentials
  9. const char* SSID = "JJ8428";
  10. const char* PWD = "Renualt88";
  11.  
  12. wifi_config_t g_ap_config = {
  13.     .ap.max_connection = 1,
  14.     .ap.authmode = WIFI_AUTH_WPA2_PSK,
  15.     .ap.ftm_responder = true
  16. };
  17.  
  18. static const char *TAG_AP = "ftm_ap";
  19.  
  20. static void event_handler(void *arg, esp_event_base_t event_base,
  21.     int32_t event_id, void *event_data)
  22. {
  23.     if (event_id == WIFI_EVENT_AP_START) {
  24.         ESP_LOGI(TAG_AP, "SoftAP started with FTM Responder support");
  25.     } else if (event_id == WIFI_EVENT_AP_STOP) {
  26.         ESP_LOGI(TAG_AP, "SoftAP stopped");
  27.     }
  28. }
  29.  
  30. static void wifi_ap()
  31. {
  32.     // Check if Wifi modules are ready/functional
  33.     ESP_ERROR_CHECK(esp_netif_init());
  34.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  35.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  36.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  37.  
  38.     strlcpy((char*) g_ap_config.ap.ssid, SSID, strlen(SSID) + 1);
  39.     strlcpy((char*) g_ap_config.ap.password, PWD, strlen(PWD) + 1);
  40.  
  41.     // SoftAP Wifi configurations
  42.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  43.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &g_ap_config));
  44.     esp_wifi_set_bandwidth(ESP_IF_WIFI_AP, WIFI_BW_HT40); // SoftAP Bandwidth: 20 MHz
  45.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  46.  
  47.     // Log an informational message indicating the start of the SoftAP configuration.
  48.     ESP_LOGI(TAG_AP, "SoftAP Configurued: SSID - %s, Password - %s",
  49.         SSID, PWD
  50.     );
  51.  
  52.     // Start the Wifi modules
  53.     ESP_ERROR_CHECK(esp_wifi_start());
  54. }
  55.  
  56. void app_main(void)
  57. {
  58.     esp_err_t ret = nvs_flash_init();
  59.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  60.         ESP_ERROR_CHECK(nvs_flash_erase());
  61.         ret = nvs_flash_init();
  62.     }
  63.     ESP_ERROR_CHECK(ret);
  64.  
  65.     /*
  66.     esp_event_handler_instance_t instance_any_id;
  67.     ESP_ERROR_CHECK(esp_event_handler_instance_register(
  68.             WIFI_EVENT,
  69.             ESP_EVENT_ANY_ID,
  70.             &event_handler,
  71.             NULL,
  72.             &instance_any_id
  73.         )
  74.     );
  75.     */
  76.    
  77.     // Start the SoftAP and keep the code looping to host the SoftAP forever
  78.     wifi_ap();
  79.     while (true) {
  80.         vTaskDelay(3000 / portTICK_PERIOD_MS);
  81.         printf(xPortGetFreeHeapSize());
  82.     }
  83. }
FTM Initiator Code: Performs wifi FTM measurements within a while loop near the end. There are some extra imports and variables not used, but I have some additional functions commented out that relate to the example code.
  1. #include <errno.h>
  2. #include <string.h>
  3. #include <inttypes.h>
  4. #include <stdio.h>
  5. #include "nvs_flash.h"
  6. #include "cmd_system.h"
  7. #include "argtable3/argtable3.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/event_groups.h"
  10. #include "esp_event.h"
  11. #include "esp_log.h"
  12. #include "esp_err.h"
  13. #include "esp_wifi.h"
  14. #include "esp_console.h"
  15. #include "esp_mac.h"
  16.  
  17. const char *SSID = "JJ8428";
  18.  
  19. typedef struct {
  20.     struct arg_lit *initiator;
  21.     struct arg_int *frm_count;
  22.     struct arg_int *burst_period;
  23.     struct arg_str *ssid;
  24. } wifi_ftm_args_t;
  25.  
  26. static wifi_ftm_args_t ftm_args;
  27.  
  28. static const char *TAG_STA = "ftm_station";
  29.  
  30. static EventGroupHandle_t s_ftm_event_group;
  31. static const int FTM_REPORT_BIT = BIT0;
  32. static const int FTM_FAILURE_BIT = BIT1;
  33. static uint32_t s_rtt_est, s_dist_est;
  34.  
  35. const int g_report_lvl = 0;
  36.  
  37. uint16_t g_scan_ap_num;
  38. wifi_ap_record_t *g_ap_list_buffer;
  39.  
  40. static void event_handler(void *arg, esp_event_base_t event_base,
  41.                           int32_t event_id, void *event_data)
  42. {
  43.     if (event_id == WIFI_EVENT_FTM_REPORT) {
  44.         wifi_event_ftm_report_t *event = (wifi_event_ftm_report_t *) event_data;
  45.         if (event->status == FTM_STATUS_SUCCESS) {
  46.             s_rtt_est = event->rtt_est;
  47.             s_dist_est = event->dist_est;
  48.             xEventGroupSetBits(s_ftm_event_group, FTM_REPORT_BIT);
  49.         }
  50.     }
  51. }
  52.  
  53. void initialize_wifi(void)
  54. {
  55.     esp_log_level_set("wifi", ESP_LOG_WARN);
  56.  
  57.     ESP_ERROR_CHECK(esp_netif_init());
  58.     s_ftm_event_group = xEventGroupCreate();
  59.     ESP_ERROR_CHECK( esp_event_loop_create_default());
  60.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  61.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  62.  
  63.     esp_event_handler_instance_t instance_any_id;
  64.     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  65.                                                         ESP_EVENT_ANY_ID,
  66.                                                         &event_handler,
  67.                                                         NULL,
  68.                                                         &instance_any_id));
  69.  
  70.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  71.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
  72.     ESP_ERROR_CHECK(esp_wifi_start());
  73. }
  74.  
  75. // wifi_ap_record_t *find_ftm_responder_ap(const char *ssid)
  76. wifi_ap_record_t *find_ftm_responder_ap()
  77. {
  78.     // Make this defined outside as a const as opposed to declaring it everytime
  79.     wifi_scan_config_t scan_config = { 0 };
  80.     scan_config.ssid = (uint8_t *) SSID;
  81.  
  82.     ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  83.     esp_wifi_scan_start(&scan_config, true);
  84.     esp_wifi_scan_get_ap_num(&g_scan_ap_num);
  85.  
  86.     g_ap_list_buffer = malloc(g_scan_ap_num * sizeof(wifi_ap_record_t));
  87.     esp_wifi_scan_get_ap_records(&g_scan_ap_num, (wifi_ap_record_t *)g_ap_list_buffer);
  88.     for (int i = 0; i < g_scan_ap_num; i++) {
  89.         if (strcmp((const char *)g_ap_list_buffer[i].ssid, SSID) == 0)
  90.             return &g_ap_list_buffer[i];
  91.     }
  92.     return NULL;
  93. }
  94.  
  95.  
  96. void app_main(void)
  97. {
  98.     esp_err_t ret = nvs_flash_init();
  99.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  100.         ESP_ERROR_CHECK(nvs_flash_erase());
  101.         ret = nvs_flash_init();
  102.     }
  103.     ESP_ERROR_CHECK(ret);
  104.  
  105.     initialize_wifi();
  106.  
  107.     wifi_ap_record_t *ap_record = find_ftm_responder_ap();
  108.     free(g_ap_list_buffer);
  109.  
  110.     wifi_ftm_initiator_cfg_t ftmi_cfg = {
  111.         .frm_count = 32,
  112.         .burst_period = 2,
  113.     };
  114.  
  115.     memcpy(ftmi_cfg.resp_mac, ap_record->bssid, 6);
  116.     ftmi_cfg.channel = ap_record->primary;
  117.    
  118.     while (true) {
  119.         ESP_LOGI(TAG_STA, "Requesting FTM session with Frm Count - %d, Burst Period - %dmSec (0: No Preference)",
  120.              ftmi_cfg.frm_count, ftmi_cfg.burst_period*100);
  121.  
  122.         esp_wifi_ftm_initiate_session(&ftmi_cfg);
  123.         EventBits_t bits = xEventGroupWaitBits(s_ftm_event_group, FTM_REPORT_BIT | FTM_FAILURE_BIT,
  124.                                             pdTRUE, pdFALSE, portMAX_DELAY);
  125.         if (bits & FTM_REPORT_BIT) {
  126.             ESP_LOGI(TAG_STA, "Estimated RTT - %" PRId32 " nSec, Estimated Distance - %" PRId32 ".%02" PRId32 " meters",
  127.                             s_rtt_est, s_dist_est / 100, s_dist_est % 100);
  128.         } else {
  129.             ESP_LOGI(TAG_STA, "FTM Failed");
  130.         }
  131.     }
  132. }
Additionally, I faced the same issue on Arduino, but when I made a post on that here, nobody responded, so I thought the ESP-IDF would resolve the issue.

I should also be clear I have not touched the menu config at all.
Attachments
resp_screenshot.png
resp_screenshot.png (58.35 KiB) Viewed 1814 times
init_screenshot.png
init_screenshot.png (63.01 KiB) Viewed 1814 times

JJ8428
Posts: 9
Joined: Fri Jul 28, 2023 3:05 am

Re: Wifi FTM stops after 2 or 3 minutes

Postby JJ8428 » Fri Aug 11, 2023 4:39 pm

Just an update to anyone coming across from this, I did some more experimentation and I found out the culprit is the FTM initiator. FTM initiator has a data leak and I isolated the line causing the issue:
  1. bits = xEventGroupWaitBits(s_ftm_event_group, FTM_REPORT_BIT | FTM_FAILURE_BIT,
  2.                                             pdTRUE, pdFALSE, portMAX_DELAY);

JJ8428
Posts: 9
Joined: Fri Jul 28, 2023 3:05 am

Re: Wifi FTM stops after 2 or 3 minutes

Postby JJ8428 » Wed Aug 16, 2023 12:28 am

Fixed the data leak. It took me for a while but I have to delete the FTM report even though I never use it?
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/event_groups.h"
  3. #include "esp_event.h"
  4. #include "esp_log.h"
  5. #include "esp_err.h"
  6. #include "esp_wifi.h"
  7. #include "esp_mac.h"
  8. #include "nvs_flash.h"
  9. #include "sdkconfig.h"
  10. #include <string.h>
  11.  
  12. const char *SSID = "JJ8428";
  13.  
  14. wifi_ftm_initiator_cfg_t ftmi_cfg = {
  15.     .frm_count = 8,
  16.     .burst_period = 5,
  17. };
  18.  
  19. static const char *TAG_STA = "ftm_station";
  20.  
  21. static EventGroupHandle_t s_ftm_event_group;
  22. static const int FTM_REPORT_BIT = BIT0;
  23. static const int FTM_FAILURE_BIT = BIT1;
  24. static wifi_ftm_report_entry_t *s_ftm_report;
  25. static uint32_t s_rtt_est, s_dist_est;
  26.  
  27. uint16_t g_scan_ap_num;
  28. wifi_ap_record_t *g_ap_list_buffer;
  29.  
  30. EventBits_t bits;
  31.  
  32. static void event_handler(void *arg, esp_event_base_t event_base,
  33.                           int32_t event_id, void *event_data)
  34. {
  35.     if (event_id == WIFI_EVENT_FTM_REPORT) {
  36.         wifi_event_ftm_report_t *event = (wifi_event_ftm_report_t *) event_data;
  37.  
  38.         if (event->status == FTM_STATUS_SUCCESS) {
  39.             s_rtt_est = event->rtt_est;
  40.             s_dist_est = event->dist_est;
  41.             s_ftm_report = event->ftm_report_data;
  42.             xEventGroupSetBits(s_ftm_event_group, FTM_REPORT_BIT);
  43.         } else {
  44.             /*
  45.             ESP_LOGI(TAG_STA, "FTM procedure with Peer("MACSTR") failed! (Status - %d)",
  46.                 MAC2STR(event->peer_mac), event->status);
  47.             */
  48.             xEventGroupSetBits(s_ftm_event_group, FTM_FAILURE_BIT);
  49.         }
  50.     }
  51. }
  52.  
  53. void initialize_wifi(void)
  54. {
  55.     // esp_log_level_set("wifi", ESP_LOG_WARN);
  56.     esp_log_level_set("wifi", ESP_LOG_NONE);
  57.  
  58.     ESP_ERROR_CHECK(esp_netif_init());
  59.     s_ftm_event_group = xEventGroupCreate();
  60.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  61.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  62.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  63.  
  64.     esp_event_handler_instance_t instance_any_id;
  65.     ESP_ERROR_CHECK(
  66.         esp_event_handler_instance_register(
  67.             WIFI_EVENT,
  68.             ESP_EVENT_ANY_ID,
  69.             &event_handler,
  70.             NULL,
  71.             &instance_any_id
  72.         )
  73.     );
  74.  
  75.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  76.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
  77.     ESP_ERROR_CHECK(esp_wifi_start());
  78. }
  79.  
  80. wifi_ap_record_t *find_ftm_responder_ap()
  81. {
  82.     wifi_scan_config_t scan_config = { 0 };
  83.     scan_config.ssid = (uint8_t *) SSID;
  84.  
  85.     ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  86.     esp_wifi_scan_start(&scan_config, true);
  87.     esp_wifi_scan_get_ap_num(&g_scan_ap_num);
  88.  
  89.     g_ap_list_buffer = malloc(g_scan_ap_num * sizeof(wifi_ap_record_t));
  90.     esp_wifi_scan_get_ap_records(&g_scan_ap_num, (wifi_ap_record_t *)g_ap_list_buffer);
  91.     for (int i = 0; i < g_scan_ap_num; i++) {
  92.         if (strcmp((const char *)g_ap_list_buffer[i].ssid, SSID) == 0) {
  93.             return &g_ap_list_buffer[i];
  94.         }
  95.     }
  96.     free(g_ap_list_buffer);
  97.     return NULL;
  98. }
  99.  
  100.  
  101. void app_main(void)
  102. {
  103.     esp_err_t ret = nvs_flash_init();
  104.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  105.         ESP_ERROR_CHECK(nvs_flash_erase());
  106.         ret = nvs_flash_init();
  107.     }
  108.     ESP_ERROR_CHECK(ret);
  109.  
  110.     initialize_wifi();
  111.  
  112.     wifi_ap_record_t *ap_record = find_ftm_responder_ap();
  113.     if (ap_record == NULL) {
  114.         ESP_LOGI(TAG_STA, "No FTM Responder with the SSID: %s",
  115.             SSID);
  116.         return;
  117.     }
  118.  
  119.     memcpy(ftmi_cfg.resp_mac, ap_record->bssid, 6);
  120.     ftmi_cfg.channel = ap_record->primary;
  121.    
  122.     while (true) {
  123.         /*
  124.         ESP_LOGI(TAG_STA, "Requesting FTM session with Frm Count - %d, Burst Period - %dmSec (0: No Preference)",
  125.             ftmi_cfg.frm_count, ftmi_cfg.burst_period*100);
  126.         ESP_LOGI(TAG_STA,"Free Heap Size: %zu bytes\n",
  127.             (size_t)esp_get_free_heap_size());
  128.         */
  129.  
  130.         esp_wifi_ftm_initiate_session(&ftmi_cfg);
  131.  
  132.         bits = xEventGroupWaitBits(s_ftm_event_group, FTM_REPORT_BIT | FTM_FAILURE_BIT, pdTRUE, pdFALSE, portMAX_DELAY);
  133.         if (bits & FTM_REPORT_BIT) {
  134.             xEventGroupClearBits(s_ftm_event_group, FTM_REPORT_BIT);
  135.             free(s_ftm_report);
  136.             ESP_LOGI(TAG_STA, "{\"est_RTT\": %" PRId32 ", \"est_dist\": %" PRId32 ".%02" PRId32 "}",
  137.                 s_rtt_est, s_dist_est / 100, s_dist_est % 100);
  138.         } else {
  139.             xEventGroupClearBits(s_ftm_event_group, FTM_FAILURE_BIT);
  140.             ESP_LOGI(TAG_STA, "FTM Failed");
  141.         }
  142.  
  143.         esp_wifi_ftm_end_session();
  144.     }
  145. }

L.Marethyu
Posts: 1
Joined: Mon Feb 19, 2024 8:50 am

Re: Wifi FTM stops after 2 or 3 minutes

Postby L.Marethyu » Mon Feb 19, 2024 9:27 am

Thank you so much, you have no idea how much you helped me with this.

Who is online

Users browsing this forum: Bing [Bot], Gaston1980, Majestic-12 [Bot] and 384 guests