Skip to content

Commit 4bfb051

Browse files
committed
Handled states of external AP configuration
1 parent ed6252f commit 4bfb051

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMod
5656
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
5757
{
5858
wifi_mode_t curWiFiMode;
59-
mWiFiStationMode = (esp_wifi_get_mode(&curWiFiMode) == ESP_OK && (curWiFiMode == WIFI_MODE_STA))
59+
mWiFiStationMode =
60+
(esp_wifi_get_mode(&curWiFiMode) == ESP_OK && (curWiFiMode == WIFI_MODE_APSTA || curWiFiMode == WIFI_MODE_STA))
61+
6062
? kWiFiStationMode_Enabled
6163
: kWiFiStationMode_Disabled;
6264
}
@@ -76,6 +78,8 @@ CHIP_ERROR ConnectivityManagerImpl::_SetWiFiStationMode(WiFiStationMode val)
7678

7779
if (val != kWiFiStationMode_ApplicationControlled)
7880
{
81+
ReturnErrorOnFailure(Internal::ESP32Utils::EnableStationMode());
82+
7983
DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
8084
}
8185

@@ -332,6 +336,9 @@ CHIP_ERROR ConnectivityManagerImpl::InitWiFi()
332336

333337
// TODO Initialize the Chip Addressing and Routing Module.
334338

339+
// Ensure that ESP station mode is enabled.
340+
ReturnErrorOnFailure(Internal::ESP32Utils::EnableStationMode());
341+
335342
// If there is no persistent station provision...
336343
if (!IsWiFiStationProvisioned())
337344
{
@@ -366,6 +373,7 @@ CHIP_ERROR ConnectivityManagerImpl::InitWiFi()
366373
}
367374
}
368375

376+
ReturnErrorOnFailure(Internal::ESP32Utils::MapError(esp_wifi_set_mode(WIFI_MODE_STA)));
369377
// Queue work items to bootstrap the AP and station state machines once the Chip event loop is running.
370378
ReturnErrorOnFailure(DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL));
371379

@@ -469,6 +477,9 @@ void ConnectivityManagerImpl::DriveStationState()
469477
{
470478
// Ensure that the ESP WiFi layer is started.
471479
ReturnOnFailure(Internal::ESP32Utils::StartWiFiLayer());
480+
481+
// Ensure that station mode is enabled in the ESP WiFi layer.
482+
ReturnOnFailure(Internal::ESP32Utils::EnableStationMode());
472483
}
473484

474485
// Determine if the ESP WiFi layer thinks the station interface is currently connected.

src/platform/ESP32/ESP32Utils.cpp

+38-12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,36 @@ CHIP_ERROR ESP32Utils::IsStationEnabled(bool & staEnabled)
5353
return CHIP_NO_ERROR;
5454
}
5555

56+
CHIP_ERROR ESP32Utils::EnableStationMode(void)
57+
{
58+
wifi_mode_t curWiFiMode;
59+
60+
// Get the current ESP WiFI mode.
61+
esp_err_t err = esp_wifi_get_mode(&curWiFiMode);
62+
if (err != ESP_OK)
63+
{
64+
ChipLogError(DeviceLayer, "esp_wifi_get_mode() failed: %s", esp_err_to_name(err));
65+
return ESP32Utils::MapError(err);
66+
}
67+
68+
// If station mode is not already enabled (implying the current mode is WIFI_MODE_AP), change
69+
// the mode to WIFI_MODE_APSTA.
70+
if (curWiFiMode == WIFI_MODE_AP)
71+
{
72+
ChipLogProgress(DeviceLayer, "Changing ESP WiFi mode: %s -> %s", WiFiModeToStr(WIFI_MODE_AP),
73+
WiFiModeToStr(WIFI_MODE_APSTA));
74+
75+
err = esp_wifi_set_mode(WIFI_MODE_APSTA);
76+
if (err != ESP_OK)
77+
{
78+
ChipLogError(DeviceLayer, "esp_wifi_set_mode() failed: %s", esp_err_to_name(err));
79+
return ESP32Utils::MapError(err);
80+
}
81+
}
82+
83+
return CHIP_NO_ERROR;
84+
}
85+
5686
bool ESP32Utils::IsStationProvisioned(void)
5787
{
5888
wifi_config_t stationConfig;
@@ -126,6 +156,10 @@ const char * ESP32Utils::WiFiModeToStr(wifi_mode_t wifiMode)
126156
return "NULL";
127157
case WIFI_MODE_STA:
128158
return "STA";
159+
case WIFI_MODE_AP:
160+
return "AP";
161+
case WIFI_MODE_APSTA:
162+
return "STA+AP";
129163
default:
130164
return "(unknown)";
131165
}
@@ -173,6 +207,10 @@ CHIP_ERROR ESP32Utils::SetWiFiStationProvision(const Internal::DeviceNetworkInfo
173207
char wifiSSID[kMaxWiFiSSIDLength + 1];
174208
size_t netInfoSSIDLen = strlen(netInfo.WiFiSSID);
175209

210+
// Ensure that ESP station mode is enabled. This is required before esp_wifi_set_config(ESP_IF_WIFI_STA,...)
211+
// can be called.
212+
ReturnErrorOnFailure(ESP32Utils::EnableStationMode());
213+
176214
// Enforce that wifiSSID is null terminated before copying it
177215
memcpy(wifiSSID, netInfo.WiFiSSID, min(netInfoSSIDLen + 1, sizeof(wifiSSID)));
178216
if (netInfoSSIDLen + 1 < sizeof(wifiSSID))
@@ -244,18 +282,6 @@ CHIP_ERROR ESP32Utils::InitWiFiStack(void)
244282
return ESP32Utils::MapError(err);
245283
}
246284

247-
esp_wifi_get_mode(&mode);
248-
if (mode == WIFI_MODE_AP)
249-
{
250-
esp_fill_random(ap_mac, sizeof(ap_mac));
251-
/* Bit 0 of the first octet of MAC Address should always be 0 */
252-
ap_mac[0] &= (uint8_t) ~0x01;
253-
err = esp_wifi_set_mac(WIFI_IF_AP, ap_mac);
254-
if (err != ESP_OK)
255-
{
256-
return ESP32Utils::MapError(err);
257-
}
258-
}
259285
err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
260286
if (err != ESP_OK)
261287
{

src/platform/ESP32/ESP32Utils.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ESP32Utils
3333
static bool IsStationProvisioned(void);
3434
static CHIP_ERROR IsStationConnected(bool & connected);
3535
static CHIP_ERROR StartWiFiLayer(void);
36+
static CHIP_ERROR EnableStationMode(void);
3637
static int OrderScanResultsByRSSI(const void * _res1, const void * _res2);
3738
static const char * WiFiModeToStr(wifi_mode_t wifiMode);
3839
static struct netif * GetNetif(const char * ifKey);

0 commit comments

Comments
 (0)