driver: wifi: esp32: add status information

Adds Wi-Fi driver connection status details.

Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
This commit is contained in:
Sylvio Alves 2022-10-11 11:32:57 -03:00 committed by Carles Cufí
parent 016e14a3d8
commit 0b32c1f851

View file

@ -42,12 +42,22 @@ enum esp32_state_flag {
ESP32_AP_STOPPED,
};
struct esp32_wifi_status {
char ssid[WIFI_SSID_MAX_LEN + 1];
char pass[WIFI_PSK_MAX_LEN + 1];
wifi_auth_mode_t security;
bool connected;
uint8_t channel;
int rssi;
};
struct esp32_wifi_runtime {
uint8_t mac_addr[6];
uint8_t frame_buf[NET_ETH_MAX_FRAME_SIZE];
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
struct net_stats_eth stats;
#endif
struct esp32_wifi_status status;
scan_result_cb_t scan_cb;
uint8_t state;
};
@ -308,6 +318,9 @@ static int esp32_wifi_connect(const struct device *dev,
data->state = ESP32_STA_CONNECTING;
memcpy(data->status.ssid, params->ssid, params->ssid_length);
data->status.ssid[params->ssid_length] = '\0';
wifi_config_t wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config_t));
@ -319,13 +332,23 @@ static int esp32_wifi_connect(const struct device *dev,
memcpy(wifi_config.sta.password, params->psk, params->psk_length);
wifi_config.sta.password[params->psk_length] = '\0';
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
data->status.security = WIFI_AUTH_WPA2_PSK;
} else if (params->security == WIFI_SECURITY_TYPE_NONE) {
wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
data->status.security = WIFI_AUTH_OPEN;
} else {
LOG_ERR("Authentication method not supported");
return -EIO;
}
if (params->channel == WIFI_CHANNEL_ANY) {
wifi_config.sta.channel = 0U;
data->status.channel = 0U;
} else {
wifi_config.sta.channel = params->channel;
data->status.channel = params->channel;
}
wifi_config.sta.pmf_cfg.capable = true;
wifi_config.sta.pmf_cfg.required = false;
@ -369,24 +392,30 @@ static int esp32_wifi_scan(const struct device *dev, scan_result_cb_t cb)
static int esp32_wifi_ap_enable(const struct device *dev,
struct wifi_connect_req_params *params)
{
struct esp32_wifi_data *data = dev->data;
struct esp32_wifi_runtime *data = dev->data;
esp_err_t ret = 0;
/* Build Wi-Fi configuration for AP mode */
wifi_config_t wifi_config = {
.ap = {
.max_connection = 5,
.channel = params->channel
},
};
memcpy(data->status.ssid, params->ssid, params->ssid_length);
data->status.ssid[params->ssid_length] = '\0';
strncpy((char *) wifi_config.ap.ssid, params->ssid, params->ssid_length);
if (params->psk_length == 0) {
memset(wifi_config.ap.password, 0, sizeof(wifi_config.ap.password));
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
data->status.security = WIFI_AUTH_OPEN;
} else {
strncpy((char *) wifi_config.ap.password, params->psk, params->psk_length);
wifi_config.ap.authmode = WIFI_AUTH_WPA2_PSK;
data->status.security = WIFI_AUTH_WPA2_PSK;
}
/* Start Wi-Fi in AP mode with configuration built above */
@ -403,8 +432,6 @@ static int esp32_wifi_ap_enable(const struct device *dev,
static int esp32_wifi_ap_disable(const struct device *dev)
{
struct esp32_wifi_data *data = dev->data;
esp_err_t ret = esp_wifi_set_mode(ESP32_WIFI_MODE_NULL);
ret |= esp_wifi_start();
@ -416,6 +443,82 @@ static int esp32_wifi_ap_disable(const struct device *dev)
return 0;
};
static int esp32_wifi_status(const struct device *dev, struct wifi_iface_status *status)
{
struct esp32_wifi_runtime *data = dev->data;
wifi_mode_t mode;
wifi_config_t conf;
wifi_ap_record_t ap_info;
switch (data->state) {
case ESP32_STA_STOPPED:
case ESP32_AP_STOPPED:
status->state = WIFI_STATE_INACTIVE;
break;
case ESP32_STA_STARTED:
case ESP32_AP_DISCONNECTED:
status->state = WIFI_STATE_DISCONNECTED;
break;
case ESP32_STA_CONNECTING:
status->state = WIFI_STATE_SCANNING;
break;
case ESP32_STA_CONNECTED:
case ESP32_AP_CONNECTED:
status->state = WIFI_STATE_COMPLETED;
break;
default:
break;
}
strcpy(status->ssid, data->status.ssid);
status->ssid_len = strlen(data->status.ssid);
status->band = WIFI_FREQ_BAND_2_4_GHZ;
status->link_mode = WIFI_LINK_MODE_UNKNOWN;
status->mfp = WIFI_MFP_DISABLE;
if (esp_wifi_get_mode(&mode) == ESP_OK) {
if (mode == ESP32_WIFI_MODE_STA) {
esp_wifi_get_config(ESP_IF_WIFI_STA, &conf);
esp_wifi_sta_get_ap_info(&ap_info);
status->iface_mode = WIFI_MODE_INFRA;
status->channel = ap_info.primary;
status->rssi = ap_info.rssi;
memcpy(status->bssid, ap_info.bssid, WIFI_MAC_ADDR_LEN);
if (ap_info.phy_11n) {
status->link_mode = WIFI_4;
} else if (ap_info.phy_11g) {
status->link_mode |= WIFI_3;
} else if (ap_info.phy_11b) {
status->link_mode = WIFI_1;
}
} else if (mode == ESP32_WIFI_MODE_AP) {
esp_wifi_get_config(ESP_IF_WIFI_AP, &conf);
status->iface_mode = WIFI_MODE_AP;
status->link_mode = WIFI_LINK_MODE_UNKNOWN;
status->channel = conf.ap.channel;
} else {
status->iface_mode = WIFI_MODE_UNKNOWN;
status->link_mode = WIFI_LINK_MODE_UNKNOWN;
status->channel = 0;
}
}
switch (data->status.security) {
case WIFI_AUTH_OPEN:
status->security = WIFI_SECURITY_TYPE_NONE;
break;
case WIFI_AUTH_WPA2_PSK:
status->security = WIFI_SECURITY_TYPE_PSK;
break;
default:
status->security = WIFI_SECURITY_TYPE_UNKNOWN;
}
return 0;
}
static void esp32_wifi_init(struct net_if *iface)
{
const struct device *dev = net_if_get_device(iface);
@ -486,6 +589,7 @@ static const struct net_wifi_mgmt_offload esp32_api = {
.disconnect = esp32_wifi_disconnect,
.ap_enable = esp32_wifi_ap_enable,
.ap_disable = esp32_wifi_ap_disable,
.iface_status = esp32_wifi_status,
};
NET_DEVICE_DT_INST_DEFINE(0,