Skip to content

Allow scan in AP mode #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 24 additions & 31 deletions esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "esp_wifi_driver.h"
#include "esp_event.h"
#include "esp_mac.h"
#include "esp_check.h"
#include "wifi_defs.h"
#include <time.h>
#include <sys/time.h>
Expand Down Expand Up @@ -485,8 +486,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,
switch(event_id) {

case WIFI_EVENT_STA_START:
ESP_LOGI(TAG, "station started and disabled softap mode");
softap_started = 0;
ESP_LOGI(TAG, "station started");
sta_init_flag = 1;
break;

Expand Down Expand Up @@ -527,16 +527,17 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,
break;

case WIFI_EVENT_SCAN_DONE:
ESP_LOGI(TAG, "wifi scanning done");
handle_scan_event();
break;

case WIFI_EVENT_AP_START:
ESP_LOGI(TAG, "softap started and disabled station mode");
sta_init_flag = 0;
ESP_LOGI(TAG, "softap started");
softap_started = 1;
break;

case WIFI_EVENT_AP_STOP:
ESP_LOGI(TAG, "softap stopped");
softap_started = 0;
break;

Expand Down Expand Up @@ -979,7 +980,7 @@ int process_start_scan(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
config_present = true;
}

if (sta_init_flag) {
if (sta_init_flag || softap_started) {
/* Trigger scan */
if (config_present)
ret = esp_wifi_scan_start(&params, false);
Expand Down Expand Up @@ -1274,7 +1275,7 @@ int process_disconnect(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
if (sta_init_flag && wifi_mode == WIFI_MODE_STA)
esp_wifi_deauthenticate_internal(cmd_disconnect->reason_code);
} else if (if_type == ESP_AP_IF) {
if (softap_started && wifi_mode == WIFI_MODE_AP)
if (softap_started && (wifi_mode == WIFI_MODE_AP || wifi_mode == WIFI_MODE_APSTA))
ieee80211_delete_node(cmd_disconnect->mac);
}

Expand Down Expand Up @@ -1536,7 +1537,7 @@ int process_init_interface(uint8_t if_type, uint8_t *payload, uint16_t payload_l
if (if_type == ESP_STA_IF) {
mode = WIFI_MODE_STA;
} else {
mode = WIFI_MODE_AP;
mode = WIFI_MODE_APSTA;
}

ret = esp_wifi_set_mode(mode);
Expand Down Expand Up @@ -1759,7 +1760,7 @@ int process_add_key(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
int process_set_mode(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
{
esp_err_t ret = ESP_OK;
uint8_t cmd_status = CMD_RESPONSE_SUCCESS;
uint8_t cmd_status = CMD_RESPONSE_FAIL;
struct cmd_config_mode *mode = (struct cmd_config_mode *) payload;
wifi_mode_t old_mode;

Expand All @@ -1771,40 +1772,32 @@ int process_set_mode(uint8_t if_type, uint8_t *payload, uint16_t payload_len)

if (old_mode == mode->mode) {
ESP_LOGI(TAG, "old mode and new modes are same, return");
goto send_resp;
goto send_ok;
}
ret = esp_wifi_stop();
if (ret) {
ESP_LOGE(TAG, "Failed to stop wifi\n");
}

if (mode->mode == WIFI_MODE_AP) {
ret = esp_wifi_set_mac(WIFI_IF_STA, dummy_mac);
} else if (mode->mode == WIFI_MODE_STA){
ret = esp_wifi_set_mac(WIFI_IF_AP, dummy_mac2);
}

ESP_LOGI(TAG, "Setting mode=%d \n", mode->mode);
ret = esp_wifi_set_mode(mode->mode);
if (mode->mode == WIFI_MODE_AP) {
ret = esp_wifi_set_mac(WIFI_IF_AP, dev_mac);
if (mode->mode == WIFI_MODE_AP || mode->mode == WIFI_MODE_APSTA) {
ESP_LOGI(TAG, "Setting APSTA mode");
ESP_GOTO_ON_ERROR(esp_wifi_set_mac(WIFI_IF_STA, dummy_mac), send_err, TAG, "Setting MAC on STA failed");
ESP_GOTO_ON_ERROR(esp_wifi_set_mode(WIFI_MODE_APSTA), send_err, TAG, "Setting mode to APSTA failed");
ESP_GOTO_ON_ERROR(esp_wifi_set_mac(WIFI_IF_AP, dev_mac), send_err, TAG, "Setting MAC on AP failed");
} else if (mode->mode == WIFI_MODE_STA){
ret = esp_wifi_set_mac(WIFI_IF_STA, dev_mac);
ESP_LOGI(TAG, "Setting STA mode");
ESP_GOTO_ON_ERROR(esp_wifi_set_mac(WIFI_IF_AP, dummy_mac2), send_err, TAG, "Setting MAC on AP failed");
ESP_GOTO_ON_ERROR(esp_wifi_set_mode(WIFI_MODE_STA), send_err, TAG, "Setting mode to STA failed");
ESP_GOTO_ON_ERROR(esp_wifi_set_mac(WIFI_IF_STA, dev_mac), send_err, TAG, "Setting MAC on STA failed");
}

ESP_ERROR_CHECK(esp_wifi_start());
ESP_GOTO_ON_ERROR(esp_wifi_start(), send_err, TAG, "Cannot start wifi");

if (ret) {
ESP_LOGE(TAG, "Failed to set mode\n");
cmd_status = CMD_RESPONSE_FAIL;
goto send_resp;
}

send_resp:
ret = send_command_resp(if_type, CMD_SET_MODE, cmd_status, (uint8_t *)&mode->mode,
send_ok:
cmd_status = CMD_RESPONSE_SUCCESS;
send_err:
return send_command_resp(if_type, CMD_SET_MODE, cmd_status, (uint8_t *)&mode->mode,
sizeof(uint16_t), sizeof(struct command_header));

return ret;
}

int process_set_ie(uint8_t if_type, uint8_t *payload, uint16_t payload_len)
Expand Down
6 changes: 5 additions & 1 deletion esp_hosted_ng/host/esp_cfg80211.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,12 @@ static int esp_nl_mode_to_esp_iface(enum nl80211_iftype type)
}

static int8_t esp_get_mode_from_iface_type(int iface_type) {
// Numbers returned bellow have the following meaning
// 3 => WIFI_MODE_APSTA
// 1 => WIFI_MODE_STA

if (iface_type == ESP_AP_IF) {
return 2;
return 3;
}

return 1;
Expand Down
10 changes: 0 additions & 10 deletions esp_hosted_ng/host/esp_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1611,11 +1611,6 @@ int internal_scan_request(struct esp_wifi_device *priv, char *ssid,
return -EBUSY;
}

if (priv->if_type != ESP_STA_IF) {
esp_err("Invalid interface\n");
return -EINVAL;
}

if (test_bit(ESP_CLEANUP_IN_PROGRESS, &priv->adapter->state_flags)) {
esp_err("%u cleanup in progress, return", __LINE__);
return -EBUSY;
Expand Down Expand Up @@ -1670,11 +1665,6 @@ int cmd_scan_request(struct esp_wifi_device *priv, struct cfg80211_scan_request
return -EINVAL;
}

if (priv->if_type != ESP_STA_IF) {
esp_err("Invalid interface\n");
return -EINVAL;
}

if (test_bit(ESP_CLEANUP_IN_PROGRESS, &priv->adapter->state_flags)) {
esp_err("%u cleanup in progress, return", __LINE__);
return -EBUSY;
Expand Down
11 changes: 4 additions & 7 deletions esp_hosted_ng/host/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,11 @@ struct esp_wifi_device *get_priv_from_payload_header(
continue;
}

if (priv->if_type == header->if_type &&
priv->if_num == header->if_num) {
if (priv->if_num == header->if_num) {
return priv;
} else if (priv->if_type == header->if_type) {
esp_err("dropping pkt, priv ifnum=%d, header ifnum=%d\n", priv->if_num, header->if_num);
} else {
esp_err("dropping pkt, priv iftype=%d, header iftype=%d\n", priv->if_type, header->if_type);
}
} else {
esp_err("dropping pkt, priv iftype=%d ifnum=%d, header iftype=%d ifnum=%d\n", priv->if_type, priv->if_num, header->if_type, header->if_num);
}
}
return NULL;
}
Expand Down