Skip to content
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

[Draft] [ESP32]: added Wi-Fi interface enabled support #33859

Closed
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
5 changes: 5 additions & 0 deletions src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ CHIP_ERROR ConnectivityManagerImpl::InitWiFi()
mWiFiStationMode = kWiFiStationMode_Disabled;
mWiFiStationState = kWiFiStationState_NotConnected;
mWiFiStationReconnectInterval = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL);
if (!NetworkCommissioning::ESPWiFiDriver::GetInstance().GetEnabled())
{
// If InterfaceEnabled is false, set mode to ApplicationControlled in order not to connect Wi-Fi automatically.
mWiFiStationMode = kWiFiStationMode_ApplicationControlled;
}

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
mLastAPDemandTime = System::Clock::kZero;
Expand Down
49 changes: 49 additions & 0 deletions src/platform/ESP32/NetworkCommissioningDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ CHIP_ERROR ESPWiFiDriver::CommitConfiguration()
CHIP_ERROR ESPWiFiDriver::RevertConfiguration()
{
mStagingNetwork = mSavedNetwork;
if (!GetEnabled())
{
// When reverting, set InterfaceEnabled to default value (true).
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Delete(kInterfaceEnabled));
}
return CHIP_NO_ERROR;
}

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

CHIP_ERROR ESPWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen)
{
if (!GetEnabled())
{
// Set InterfaceEnabled to default value (true).
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Delete(kInterfaceEnabled));
}

// If device is already connected to WiFi, then disconnect the WiFi,
// clear the WiFi configurations and add the newly provided WiFi configurations.
if (chip::DeviceLayer::Internal::ESP32Utils::IsStationProvisioned())
Expand Down Expand Up @@ -307,6 +318,44 @@ void ESPWiFiDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * callbac
}
}

CHIP_ERROR ESPWiFiDriver::SetEnabled(bool enabled)
{
if (enabled == GetEnabled())
{
return CHIP_NO_ERROR;
}

ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kInterfaceEnabled, &enabled, sizeof(enabled)));

if (!enabled)
{
if (chip::DeviceLayer::Internal::ESP32Utils::IsStationProvisioned())
{
ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface");
esp_err_t err = esp_wifi_disconnect();
if (err != ESP_OK)
{
ChipLogError(DeviceLayer, "esp_wifi_disconnect() failed: %s", esp_err_to_name(err));
return chip::DeviceLayer::Internal::ESP32Utils::MapError(err);
}
return ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_ApplicationControlled);
}
}
else
{
ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Enabled));
}
return CHIP_NO_ERROR;
}

bool ESPWiFiDriver::GetEnabled()
{
bool value;
// InterfaceEnabled default value is true.
VerifyOrReturnValue(PersistedStorage::KeyValueStoreMgr().Get(kInterfaceEnabled, &value, sizeof(value)) == CHIP_NO_ERROR, true);
return value;
}

CHIP_ERROR ESPWiFiDriver::StartScanWiFiNetworks(ByteSpan ssid)
{
esp_err_t err = ESP_OK;
Expand Down
3 changes: 3 additions & 0 deletions src/platform/ESP32/NetworkCommissioningDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class ESPWiFiDriver final : public WiFiDriver
// BaseDriver
NetworkIterator * GetNetworks() override { return new WiFiNetworkIterator(this); }
CHIP_ERROR Init(NetworkStatusChangeCallback * networkStatusChangeCallback) override;
CHIP_ERROR SetEnabled(bool enabled) override;
bool GetEnabled() override;
void Shutdown() override;

// WirelessDriver
Expand Down Expand Up @@ -131,6 +133,7 @@ class ESPWiFiDriver final : public WiFiDriver
}

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

Expand Down
Loading