Skip to content

Commit

Permalink
Merge pull request #27 from mongoose-os-libs/p24
Browse files Browse the repository at this point in the history
esp32: Call disconnect before scan if in connecting state
  • Loading branch information
rojer authored May 10, 2019
2 parents 9f23514 + 99abe5b commit 71db0f0
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/esp32/esp32_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#include <stdbool.h>
#include <string.h>

#include "dhcpserver/dhcpserver.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "tcpip_adapter.h"
#include "dhcpserver/dhcpserver.h"
#include "lwip/ip_addr.h"
#include "tcpip_adapter.h"

#include "common/cs_dbg.h"
#include "common/cs_file.h"
Expand All @@ -37,6 +37,8 @@

static bool s_inited = false;
static bool s_started = false;
static bool s_connecting = false;

typedef esp_err_t (*wifi_func_t)(void *arg);

esp_err_t esp32_wifi_ev(system_event_t *ev) {
Expand All @@ -52,11 +54,15 @@ esp_err_t esp32_wifi_ev(system_event_t *ev) {
case SYSTEM_EVENT_STA_DISCONNECTED:
dei.ev = MGOS_WIFI_EV_STA_DISCONNECTED;
dei.sta_disconnected.reason = info->disconnected.reason;
// Getting a DISCONNECTED event does not change the internal mode,
// wifi lib still thinks we are connecting until disconnect() is called.
// s_connecting = false;
break;
case SYSTEM_EVENT_STA_CONNECTED:
dei.ev = MGOS_WIFI_EV_STA_CONNECTED;
memcpy(dei.sta_connected.bssid, info->connected.bssid, 6);
dei.sta_connected.channel = info->connected.channel;
s_connecting = false;
break;
case SYSTEM_EVENT_STA_GOT_IP:
dei.ev = MGOS_WIFI_EV_STA_IP_ACQUIRED;
Expand Down Expand Up @@ -142,14 +148,17 @@ static esp_err_t esp32_wifi_ensure_init(void) {
static esp_err_t esp32_wifi_ensure_start(void) {
esp_err_t r = ESP_OK;
if (!s_started) {
s_started = false;
s_connecting = false;
r = esp_wifi_start();
if (r != ESP_OK) {
LOG(LL_ERROR, ("Failed to start WiFi: %d", r));
goto out;
}
s_started = true;
wifi_ps_type_t cur_ps_mode = WIFI_PS_NONE;
wifi_ps_type_t want_ps_mode = (wifi_ps_type_t) mgos_sys_config_get_wifi_sta_ps_mode();
wifi_ps_type_t want_ps_mode =
(wifi_ps_type_t) mgos_sys_config_get_wifi_sta_ps_mode();
esp_wifi_get_ps(&cur_ps_mode);
/* Workaround for https://github.com/espressif/esp-idf/issues/1942 */
if (cur_ps_mode != want_ps_mode) {
Expand Down Expand Up @@ -191,6 +200,7 @@ static esp_err_t esp32_wifi_set_mode(wifi_mode_t mode) {
if (r == ESP_ERR_WIFI_NOT_INIT) r = ESP_OK; /* Nothing to stop. */
if (r == ESP_OK) {
s_started = false;
s_connecting = false;
}
goto out;
}
Expand All @@ -202,6 +212,7 @@ static esp_err_t esp32_wifi_set_mode(wifi_mode_t mode) {
if (s_started) {
if (esp_wifi_stop() == ESP_OK) {
s_started = false;
s_connecting = false;
}
}

Expand Down Expand Up @@ -484,12 +495,16 @@ bool mgos_wifi_dev_ap_setup(const struct mgos_config_wifi_ap *cfg) {

bool mgos_wifi_dev_sta_connect(void) {
if ((esp32_wifi_ensure_init() != ESP_OK) ||
(esp32_wifi_ensure_start() != ESP_OK)) return false;
(esp32_wifi_ensure_start() != ESP_OK))
return false;
wifi_mode_t cur_mode = esp32_wifi_get_mode();
if (cur_mode == WIFI_MODE_NULL || cur_mode == WIFI_MODE_AP) return false;
esp_err_t r = esp_wifi_connect();
if (r != ESP_OK) {
LOG(LL_ERROR, ("WiFi STA: Connect failed: %d", r));
s_connecting = false;
} else {
s_connecting = true;
}
return (r == ESP_OK);
}
Expand All @@ -498,6 +513,7 @@ bool mgos_wifi_dev_sta_disconnect(void) {
wifi_mode_t cur_mode = esp32_wifi_get_mode();
if (cur_mode == WIFI_MODE_NULL || cur_mode == WIFI_MODE_AP) return false;
esp_wifi_disconnect();
s_connecting = false;
/* If we are in station-only mode, stop WiFi task as well. */
if (cur_mode == WIFI_MODE_STA) {
esp_err_t r = esp_wifi_stop();
Expand Down Expand Up @@ -570,9 +586,16 @@ bool mgos_wifi_dev_start_scan(void) {
.show_hidden = false,
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
.scan_time = {
.active = {
.min = 10, .max = 50,
}, }};
.active =
{
.min = 10,
.max = 50,
},
}};
if (s_connecting) {
esp_wifi_disconnect();
s_connecting = false;
}
r = esp_wifi_scan_start(&scan_cfg, false /* block */);
}
return (r == ESP_OK);
Expand Down

0 comments on commit 71db0f0

Please sign in to comment.