wifi/ftm: Stability fixes, raw FTM data in event

Update wifi lib with below additions -
1. Add FTM frame formation, IEEE definitions, event and IOCTL.
2. Implementation of FTM bursts, Measurements and RTT calculations
3. Fix Watchdog timeout, crashes with better cleanup of timers
4. Included FTM Report raw data in FTM event

Closes https://github.com/espressif/esp-idf/issues/5059
This commit is contained in:
Nachiket Kukade
2021-01-07 19:02:57 +05:30
parent acb1143409
commit a71976ab54
3 changed files with 68 additions and 14 deletions

View File

@@ -695,13 +695,26 @@ typedef enum {
FTM_STATUS_FAIL, /**< Unknown error during FTM exchange */
} wifi_ftm_status_t;
/** Argument structure for */
typedef struct {
uint8_t dlog_token; /**< Dialog Token of the FTM frame */
int8_t rssi; /**< RSSI of the FTM frame received */
uint32_t rtt; /**< Round Trip Time in pSec with a peer */
uint64_t t1; /**< Time of departure of FTM frame from FTM Responder in pSec */
uint64_t t2; /**< Time of arrival of FTM frame at FTM Initiator in pSec */
uint64_t t3; /**< Time of departure of ACK from FTM Initiator in pSec */
uint64_t t4; /**< Time of arrival of ACK at FTM Responder in pSec */
} wifi_ftm_report_entry_t;
/** Argument structure for WIFI_EVENT_FTM_REPORT event */
typedef struct {
uint8_t peer_mac[6]; /**< MAC address of the FTM Peer */
wifi_ftm_status_t status; /**< Status of the FTM operation */
uint32_t rtt_raw; /**< Raw average Round-Trip-Time with peer in Nano-Seconds */
uint32_t rtt_est; /**< Estimated Round-Trip-Time with peer in Nano-Seconds */
uint32_t dist_est; /**< Estimated one-way distance in Centi-Meters */
uint8_t peer_mac[6]; /**< MAC address of the FTM Peer */
wifi_ftm_status_t status; /**< Status of the FTM operation */
uint32_t rtt_raw; /**< Raw average Round-Trip-Time with peer in Nano-Seconds */
uint32_t rtt_est; /**< Estimated Round-Trip-Time with peer in Nano-Seconds */
uint32_t dist_est; /**< Estimated one-way distance in Centi-Meters */
wifi_ftm_report_entry_t *ftm_report_data; /**< Pointer to FTM Report with multiple entries, should be freed after use */
uint8_t ftm_report_num_entries; /**< Number of entries in the FTM Report data */
} wifi_event_ftm_report_t;
#define WIFI_STATIS_BUFFER (1<<0)

View File

@@ -53,6 +53,12 @@ static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
const int DISCONNECTED_BIT = BIT1;
static EventGroupHandle_t ftm_event_group;
const int FTM_REPORT_BIT = BIT0;
const int FTM_FAILURE_BIT = BIT1;
wifi_ftm_report_entry_t *g_ftm_report;
uint8_t g_ftm_report_num_entries;
static void scan_done_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
@@ -107,8 +113,17 @@ static void ftm_report_handler(void *arg, esp_event_base_t event_base,
{
wifi_event_ftm_report_t *event = (wifi_event_ftm_report_t *) event_data;
ESP_LOGI(TAG_STA, "Estimated RTT - %d nSec, Estimated Distance - %d.%02d meters", event->rtt_est,
event->dist_est / 100, event->dist_est % 100);
if (event->status == FTM_STATUS_SUCCESS) {
ESP_LOGI(TAG_STA, "Estimated RTT - %d nSec, Estimated Distance - %d.%02d meters", event->rtt_est,
event->dist_est / 100, event->dist_est % 100);
xEventGroupSetBits(ftm_event_group, FTM_REPORT_BIT);
g_ftm_report = event->ftm_report_data;
g_ftm_report_num_entries = event->ftm_report_num_entries;
} else {
ESP_LOGI(TAG_STA, "FTM procedure with Peer("MACSTR") failed! (Status - %d)",
MAC2STR(event->peer_mac), event->status);
xEventGroupSetBits(ftm_event_group, FTM_FAILURE_BIT);
}
}
void initialise_wifi(void)
@@ -122,6 +137,7 @@ void initialise_wifi(void)
ESP_ERROR_CHECK(esp_netif_init());
wifi_event_group = xEventGroupCreate();
ftm_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_create_default() );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
@@ -321,7 +337,7 @@ static int wifi_cmd_ftm(int argc, char **argv)
}
if (ftm_args.burst_period->count != 0) {
if (ftm_args.burst_period->ival[0] > 0 &&
if (ftm_args.burst_period->ival[0] >= 2 &&
ftm_args.burst_period->ival[0] < 256) {
ftmi_cfg.burst_period = ftm_args.burst_period->ival[0];
} else {
@@ -330,9 +346,34 @@ static int wifi_cmd_ftm(int argc, char **argv)
}
ftm_start:
ESP_LOGI(TAG_STA, "Starting FTM Initiator with Frm Count %d, Burst Period - %dmSec",
ftmi_cfg.frm_count, ftmi_cfg.burst_period * 100);
esp_wifi_ftm_start_initiator(&ftmi_cfg);
if (ftmi_cfg.burst_period == 0) {
ESP_LOGI(TAG_STA, "Starting FTM Initiator with Frm Count %d, Burst Period - No Preference",
ftmi_cfg.frm_count);
} else {
ESP_LOGI(TAG_STA, "Starting FTM Initiator with Frm Count %d, Burst Period - %dmSec",
ftmi_cfg.frm_count, ftmi_cfg.burst_period * 100);
}
if (ESP_OK != esp_wifi_ftm_start_initiator(&ftmi_cfg)) {
ESP_LOGE(TAG_STA, "Failed to start FTM session");
return 0;
}
EventBits_t bits = xEventGroupWaitBits(ftm_event_group, FTM_REPORT_BIT | FTM_FAILURE_BIT,
pdFALSE, pdFALSE, portMAX_DELAY);
/* Processing data from FTM session */
if (bits & FTM_REPORT_BIT) {
int i;
for (i = 0; i < g_ftm_report_num_entries; i++) {
/* NOTE: Process FTM report elements here, e.g. g_ftm_report[i].rtt etc */
}
free(g_ftm_report);
g_ftm_report = NULL;
g_ftm_report_num_entries = 0;
xEventGroupClearBits(ftm_event_group, FTM_REPORT_BIT);
} else {
/* Failure case */
}
return 0;
}
@@ -390,7 +431,7 @@ void register_wifi(void)
ftm_args.mode = arg_lit1("I", "ftm_initiator", "FTM Initiator mode");
ftm_args.frm_count = arg_int0("c", "frm_count", "<0/16/24/32/64>", "FTM frames to be exchanged (0: No preference)");
ftm_args.burst_period = arg_int0("p", "burst_period", "<0-255 (x 100 mSec)>", "Periodicity of FTM bursts in 100's of miliseconds (0: No preference)");
ftm_args.burst_period = arg_int0("p", "burst_period", "<2-255 (x 100 mSec)>", "Periodicity of FTM bursts in 100's of miliseconds (0: No preference)");
ftm_args.end = arg_end(1);
const esp_console_cmd_t ftm_cmd = {
@@ -431,7 +472,7 @@ void app_main(void)
printf(" | 1. Print 'help' to gain overview of commands |\n");
printf(" | 2. Use 'scan' command for AP that support FTM |\n");
printf(" | OR |\n");
printf(" | 2. Start SoftAP on another ESP32S2 with 'ap' command |\n");
printf(" | 2. Start SoftAP on another device with 'ap' command |\n");
printf(" | 3. Setup connection with the AP using 'sta' command |\n");
printf(" | 4. Initiate FTM from Station using 'ftm -I' command |\n");
printf(" | |\n");