Skip to content

Commit bedc32b

Browse files
committed
ESP32: added Wi-Fi interface enabled support
1 parent 47097e0 commit bedc32b

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ CHIP_ERROR ConnectivityManagerImpl::InitWiFi()
391391
mWiFiStationMode = kWiFiStationMode_Disabled;
392392
mWiFiStationState = kWiFiStationState_NotConnected;
393393
mWiFiStationReconnectInterval = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL);
394+
if (!NetworkCommissioning::ESPWiFiDriver::GetInstance().GetEnabled())
395+
{
396+
// If InterfaceEnabled is false, set mode to ApplicationControlled in order not to connect Wi-Fi automatically.
397+
mWiFiStationMode = kWiFiStationMode_ApplicationControlled;
398+
}
394399

395400
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
396401
mLastAPDemandTime = System::Clock::kZero;

src/platform/ESP32/NetworkCommissioningDriver.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ CHIP_ERROR ESPWiFiDriver::CommitConfiguration()
147147
CHIP_ERROR ESPWiFiDriver::RevertConfiguration()
148148
{
149149
mStagingNetwork = mSavedNetwork;
150+
if (!GetEnabled())
151+
{
152+
// When reverting, set InterfaceEnabled to default value (true).
153+
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Delete(kInterfaceEnabled));
154+
}
150155
return CHIP_NO_ERROR;
151156
}
152157

@@ -196,6 +201,12 @@ Status ESPWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, MutableC
196201

197202
CHIP_ERROR ESPWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen)
198203
{
204+
if (!GetEnabled())
205+
{
206+
// Set InterfaceEnabled to default value (true).
207+
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Delete(kInterfaceEnabled));
208+
}
209+
199210
// If device is already connected to WiFi, then disconnect the WiFi,
200211
// clear the WiFi configurations and add the newly provided WiFi configurations.
201212
if (chip::DeviceLayer::Internal::ESP32Utils::IsStationProvisioned())
@@ -307,6 +318,44 @@ void ESPWiFiDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * callbac
307318
}
308319
}
309320

321+
CHIP_ERROR ESPWiFiDriver::SetEnabled(bool enabled)
322+
{
323+
if (enabled == GetEnabled())
324+
{
325+
return CHIP_NO_ERROR;
326+
}
327+
328+
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kInterfaceEnabled, &enabled, sizeof(enabled)));
329+
330+
if (!enabled)
331+
{
332+
if (chip::DeviceLayer::Internal::ESP32Utils::IsStationProvisioned())
333+
{
334+
ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface");
335+
esp_err_t err = esp_wifi_disconnect();
336+
if (err != ESP_OK)
337+
{
338+
ChipLogError(DeviceLayer, "esp_wifi_disconnect() failed: %s", esp_err_to_name(err));
339+
return chip::DeviceLayer::Internal::ESP32Utils::MapError(err);
340+
}
341+
return ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_ApplicationControlled);
342+
}
343+
}
344+
else
345+
{
346+
ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Enabled));
347+
}
348+
return CHIP_NO_ERROR;
349+
}
350+
351+
bool ESPWiFiDriver::GetEnabled()
352+
{
353+
bool value;
354+
// InterfaceEnabled default value is true.
355+
VerifyOrReturnValue(PersistedStorage::KeyValueStoreMgr().Get(kInterfaceEnabled, &value, sizeof(value)) == CHIP_NO_ERROR, true);
356+
return value;
357+
}
358+
310359
CHIP_ERROR ESPWiFiDriver::StartScanWiFiNetworks(ByteSpan ssid)
311360
{
312361
esp_err_t err = ESP_OK;

src/platform/ESP32/NetworkCommissioningDriver.h

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class ESPWiFiDriver final : public WiFiDriver
9494
// BaseDriver
9595
NetworkIterator * GetNetworks() override { return new WiFiNetworkIterator(this); }
9696
CHIP_ERROR Init(NetworkStatusChangeCallback * networkStatusChangeCallback) override;
97+
CHIP_ERROR SetEnabled(bool enabled) override;
98+
bool GetEnabled() override;
9799
void Shutdown() override;
98100

99101
// WirelessDriver
@@ -131,6 +133,7 @@ class ESPWiFiDriver final : public WiFiDriver
131133
}
132134

133135
private:
136+
static constexpr const char * kInterfaceEnabled = "g/esp/en";
134137
bool NetworkMatch(const WiFiNetwork & network, ByteSpan networkId);
135138
CHIP_ERROR StartScanWiFiNetworks(ByteSpan ssid);
136139

0 commit comments

Comments
 (0)