diff --git a/src/platform/ESP32/NetworkCommissioningDriver.cpp b/src/platform/ESP32/NetworkCommissioningDriver.cpp index 6e43839eaf9e80..ced3d347a39531 100644 --- a/src/platform/ESP32/NetworkCommissioningDriver.cpp +++ b/src/platform/ESP32/NetworkCommissioningDriver.cpp @@ -177,23 +177,47 @@ void ESPWiFiDriver::OnConnectWiFiNetwork() { if (mpConnectCallback) { + DeviceLayer::SystemLayer().CancelTimer(OnConnectWiFiNetworkFailed, NULL); mpConnectCallback->OnResult(Status::kSuccess, CharSpan(), 0); mpConnectCallback = nullptr; } } +void ESPWiFiDriver::OnConnectWiFiNetworkFailed() +{ + if (mpConnectCallback) + { + mpConnectCallback->OnResult(Status::kNetworkNotFound, CharSpan(), 0); + mpConnectCallback = nullptr; + } +} + +void ESPWiFiDriver::OnConnectWiFiNetworkFailed(chip::System::Layer * aLayer, void * aAppState) +{ + CHIP_ERROR error = chip::DeviceLayer::Internal::ESP32Utils::ClearWiFiStationProvision(); + if (error != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "ClearWiFiStationProvision failed: %s", chip::ErrorStr(error)); + } + ESPWiFiDriver::GetInstance().OnConnectWiFiNetworkFailed(); +} + void ESPWiFiDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) { - CHIP_ERROR err = CHIP_NO_ERROR; - Status networkingStatus = Status::kSuccess; + CHIP_ERROR err = CHIP_NO_ERROR; + Status networkingStatus = Status::kSuccess; + const uint32_t secToMiliSec = 1000; VerifyOrExit(NetworkMatch(mStagingNetwork, networkId), networkingStatus = Status::kNetworkIDNotFound); VerifyOrExit(mpConnectCallback == nullptr, networkingStatus = Status::kUnknownError); ChipLogProgress(NetworkProvisioning, "ESP NetworkCommissioningDelegate: SSID: %.*s", static_cast(networkId.size()), networkId.data()); - err = ConnectWiFiNetwork(reinterpret_cast(mStagingNetwork.ssid), mStagingNetwork.ssidLen, + err = ConnectWiFiNetwork(reinterpret_cast(mStagingNetwork.ssid), mStagingNetwork.ssidLen, reinterpret_cast(mStagingNetwork.credentials), mStagingNetwork.credentialsLen); + + err = DeviceLayer::SystemLayer().StartTimer( + static_cast(kWiFiConnectNetworkTimeoutSeconds * secToMiliSec), OnConnectWiFiNetworkFailed, NULL); mpConnectCallback = callback; exit: if (err != CHIP_NO_ERROR) diff --git a/src/platform/ESP32/NetworkCommissioningDriver.h b/src/platform/ESP32/NetworkCommissioningDriver.h index f150959f1a30b5..fd3f1ddaa2ed64 100644 --- a/src/platform/ESP32/NetworkCommissioningDriver.h +++ b/src/platform/ESP32/NetworkCommissioningDriver.h @@ -109,6 +109,8 @@ class ESPWiFiDriver final : public WiFiDriver CHIP_ERROR ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen); void OnConnectWiFiNetwork(); + void OnConnectWiFiNetworkFailed(); + static void OnConnectWiFiNetworkFailed(chip::System::Layer * aLayer, void * aAppState); void OnScanWiFiNetworkDone(); void OnNetworkStatusChange();