diff --git a/src/app/server/AppDelegate.h b/src/app/server/AppDelegate.h index 56c4587f66139e..091a4149fcef7d 100644 --- a/src/app/server/AppDelegate.h +++ b/src/app/server/AppDelegate.h @@ -42,11 +42,16 @@ class AppDelegate * This is called when there is an error in establishing a commissioning session (such as, when an invalid passcode is provided) * * @param err CHIP_ERROR indicating the error that occurred during session establishment + * + * @return true if the commissioning window should be closed, false otherwise */ - virtual void OnCommissioningSessionEstablishmentError(CHIP_ERROR err) {} + virtual bool OnCommissioningSessionEstablishmentError(CHIP_ERROR err) { return false; } /** - * This is called when the commissioning session establishment stops + * This is called in addition to OnCommissioningSessionEstablishmentError i.e. when there is an error in establishing a + * commissioning session AND the commissioning window is closed. The window may be closed because + * OnCommissioningSessionEstablishmentError returned 'true', the commissioning attempts limit was reached or listening for PASE + * failed. */ virtual void OnCommissioningSessionStopped() {} diff --git a/src/app/server/CommissioningWindowManager.cpp b/src/app/server/CommissioningWindowManager.cpp index da428bbacf67f2..3e02e60cfd770d 100644 --- a/src/app/server/CommissioningWindowManager.cpp +++ b/src/app/server/CommissioningWindowManager.cpp @@ -151,17 +151,17 @@ void CommissioningWindowManager::HandleFailedAttempt(CHIP_ERROR err) mServer->GetBleLayerObject()->CloseAllBleConnections(); #endif - CHIP_ERROR prevErr = err; - if (mFailedCommissioningAttempts < kMaxFailedCommissioningAttempts) + bool closeWindow = true; + if (mAppDelegate != nullptr) { - // If the number of commissioning attempts has not exceeded maximum - // retries, let's start listening for commissioning connections again. - err = AdvertiseAndListenForPASE(); + closeWindow = mAppDelegate->OnCommissioningSessionEstablishmentError(err); } - if (mAppDelegate != nullptr) + if (!closeWindow && mFailedCommissioningAttempts < kMaxFailedCommissioningAttempts) { - mAppDelegate->OnCommissioningSessionEstablishmentError(prevErr); + // If the number of commissioning attempts has not exceeded maximum + // retries, let's start listening for commissioning connections again. + err = AdvertiseAndListenForPASE(); } if (err != CHIP_NO_ERROR)