Add FTM support and examples (#5272)

Rework of #5266 by @pedrominatel
This commit is contained in:
Me No Dev
2021-06-16 21:24:48 +03:00
committed by GitHub
parent b1bcec08f8
commit cf43d174b7
8 changed files with 366 additions and 6 deletions

View File

@ -81,11 +81,14 @@ static bool softap_config_equal(const wifi_config_t& lhs, const wifi_config_t& r
if(lhs.ap.pairwise_cipher != rhs.ap.pairwise_cipher) {
return false;
}
if(lhs.ap.ftm_responder != rhs.ap.ftm_responder) {
return false;
}
return true;
}
void wifi_softap_config(wifi_config_t *wifi_config, const char * ssid=NULL, const char * password=NULL, uint8_t channel=6, wifi_auth_mode_t authmode=WIFI_AUTH_WPA2_PSK, uint8_t ssid_hidden=0, uint8_t max_connections=4, uint16_t beacon_interval=100){
wifi_config->ap.channel = channel;
void wifi_softap_config(wifi_config_t *wifi_config, const char * ssid=NULL, const char * password=NULL, uint8_t channel=6, wifi_auth_mode_t authmode=WIFI_AUTH_WPA2_PSK, uint8_t ssid_hidden=0, uint8_t max_connections=4, bool ftm_responder=false, uint16_t beacon_interval=100){
wifi_config->ap.channel = channel;
wifi_config->ap.max_connection = max_connections;
wifi_config->ap.beacon_interval = beacon_interval;
wifi_config->ap.ssid_hidden = ssid_hidden;
@ -93,6 +96,7 @@ void wifi_softap_config(wifi_config_t *wifi_config, const char * ssid=NULL, cons
wifi_config->ap.ssid_len = 0;
wifi_config->ap.ssid[0] = 0;
wifi_config->ap.password[0] = 0;
wifi_config->ap.ftm_responder = ftm_responder;
if(ssid != NULL && ssid[0] != 0){
snprintf((char*)wifi_config->ap.ssid, 32, ssid);
wifi_config->ap.ssid_len = strlen(ssid);
@ -117,7 +121,7 @@ void wifi_softap_config(wifi_config_t *wifi_config, const char * ssid=NULL, cons
* @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID)
* @param max_connection Max simultaneous connected clients, 1 - 4.
*/
bool WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection)
bool WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection, bool ftm_responder)
{
if(!WiFi.enableAP(true)) {
@ -140,7 +144,7 @@ bool WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel,
wifi_config_t conf;
wifi_config_t conf_current;
wifi_softap_config(&conf, ssid, passphrase, channel, WIFI_AUTH_WPA2_PSK, ssid_hidden, max_connection);
wifi_softap_config(&conf, ssid, passphrase, channel, WIFI_AUTH_WPA2_PSK, ssid_hidden, max_connection, ftm_responder);
esp_err_t err = esp_wifi_get_config((wifi_interface_t)WIFI_IF_AP, &conf_current);
if(err){
log_e("get AP config failed");

View File

@ -37,7 +37,7 @@ class WiFiAPClass
public:
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4, bool ftm_responder = false);
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
bool softAPdisconnect(bool wifioff = false);

View File

@ -395,6 +395,14 @@ static void _arduino_event_cb(void* arg, esp_event_base_t event_base, int32_t ev
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP) {
arduino_event.event_id = ARDUINO_EVENT_WPS_ER_PBC_OVERLAP;
/*
* FTM
* */
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_FTM_REPORT) {
wifi_event_ftm_report_t * event = (wifi_event_ftm_report_t*)event_data;
arduino_event.event_id = ARDUINO_EVENT_WIFI_FTM_REPORT;
memcpy(&arduino_event.event_info.wifi_ftm_report, event_data, sizeof(wifi_event_ftm_report_t));
/*
* SMART CONFIG
@ -778,7 +786,8 @@ const char * arduino_event_names[] = {
"WIFI_READY",
"SCAN_DONE",
"STA_START", "STA_STOP", "STA_CONNECTED", "STA_DISCONNECTED", "STA_AUTHMODE_CHANGE", "STA_GOT_IP", "STA_GOT_IP6", "STA_LOST_IP",
"AP_START", "AP_STOP", "AP_STACONNECTED", "AP_STADISCONNECTED", "AP_STAIPASSIGNED", "AP_PROBEREQRECVED", "AP_GOT_IP6",
"AP_START", "AP_STOP", "AP_STACONNECTED", "AP_STADISCONNECTED", "AP_STAIPASSIGNED", "AP_PROBEREQRECVED", "AP_GOT_IP6",
"FTM_REPORT",
"ETH_START", "ETH_STOP", "ETH_CONNECTED", "ETH_DISCONNECTED", "ETH_GOT_IP", "ETH_GOT_IP6",
"WPS_ER_SUCCESS", "WPS_ER_FAILED", "WPS_ER_TIMEOUT", "WPS_ER_PIN", "WPS_ER_PBC_OVERLAP",
"SC_SCAN_DONE", "SC_FOUND_CHANNEL", "SC_GOT_SSID_PSWD", "SC_SEND_ACK_DONE",
@ -1125,6 +1134,32 @@ wifi_power_t WiFiGenericClass::getTxPower(){
return (wifi_power_t)power;
}
/**
* Initiate FTM Session.
* @param frm_count Number of FTM frames requested in terms of 4 or 8 bursts (allowed values - 0(No pref), 16, 24, 32, 64)
* @param burst_period Requested time period between consecutive FTM bursts in 100's of milliseconds (allowed values - 0(No pref), 2 - 255)
* @param channel Primary channel of the FTM Responder
* @param mac MAC address of the FTM Responder
* @return true on success
*/
bool WiFiGenericClass::initiateFTM(uint8_t frm_count, uint16_t burst_period, uint8_t channel, const uint8_t * mac) {
wifi_ftm_initiator_cfg_t ftmi_cfg = {
.resp_mac = {0,0,0,0,0,0},
.channel = channel,
.frm_count = frm_count,
.burst_period = burst_period,
};
if(mac != NULL){
memcpy(ftmi_cfg.resp_mac, mac, 6);
}
// Request FTM session with the Responder
if (ESP_OK != esp_wifi_ftm_initiate_session(&ftmi_cfg)) {
log_e("Failed to initiate FTM session");
return false;
}
return true;
}
// -----------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------ Generic Network function ---------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------

View File

@ -51,6 +51,7 @@ typedef enum {
ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED,
ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED,
ARDUINO_EVENT_WIFI_AP_GOT_IP6,
ARDUINO_EVENT_WIFI_FTM_REPORT,
ARDUINO_EVENT_ETH_START,
ARDUINO_EVENT_ETH_STOP,
ARDUINO_EVENT_ETH_CONNECTED,
@ -86,6 +87,7 @@ typedef union {
wifi_event_ap_probe_req_rx_t wifi_ap_probereqrecved;
wifi_event_ap_staconnected_t wifi_ap_staconnected;
wifi_event_ap_stadisconnected_t wifi_ap_stadisconnected;
wifi_event_ftm_report_t wifi_ftm_report;
ip_event_ap_staipassigned_t wifi_ap_staipassigned;
ip_event_got_ip_t got_ip;
ip_event_got_ip6_t got_ip6;
@ -170,6 +172,8 @@ class WiFiGenericClass
bool setTxPower(wifi_power_t power);
wifi_power_t getTxPower();
bool initiateFTM(uint8_t frm_count=16, uint16_t burst_period=2, uint8_t channel=0, const uint8_t * mac=NULL);
static const char * getHostname();
static bool setHostname(const char * hostname);
static bool hostname(const String& aHostname) { return setHostname(aHostname.c_str()); }