@@ -147,6 +147,11 @@ CHIP_ERROR ESPWiFiDriver::CommitConfiguration()
147
147
CHIP_ERROR ESPWiFiDriver::RevertConfiguration ()
148
148
{
149
149
mStagingNetwork = mSavedNetwork ;
150
+ if (!GetEnabled ())
151
+ {
152
+ // When reverting, set InterfaceEnabled to default value (true).
153
+ ReturnErrorOnFailure (PersistedStorage::KeyValueStoreMgr ().Delete (kInterfaceEnabled ));
154
+ }
150
155
return CHIP_NO_ERROR;
151
156
}
152
157
@@ -196,6 +201,12 @@ Status ESPWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, MutableC
196
201
197
202
CHIP_ERROR ESPWiFiDriver::ConnectWiFiNetwork (const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen)
198
203
{
204
+ if (!GetEnabled ())
205
+ {
206
+ // Set InterfaceEnabled to default value (true).
207
+ ReturnErrorOnFailure (PersistedStorage::KeyValueStoreMgr ().Delete (kInterfaceEnabled ));
208
+ }
209
+
199
210
// If device is already connected to WiFi, then disconnect the WiFi,
200
211
// clear the WiFi configurations and add the newly provided WiFi configurations.
201
212
if (chip::DeviceLayer::Internal::ESP32Utils::IsStationProvisioned ())
@@ -307,6 +318,44 @@ void ESPWiFiDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * callbac
307
318
}
308
319
}
309
320
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
+
310
359
CHIP_ERROR ESPWiFiDriver::StartScanWiFiNetworks (ByteSpan ssid)
311
360
{
312
361
esp_err_t err = ESP_OK;
0 commit comments