From 72f1cba11ab93d723977cfb67d8da49b133fa2f6 Mon Sep 17 00:00:00 2001 From: chendejin Date: Wed, 28 Aug 2024 15:19:25 +0800 Subject: [PATCH] OpenThread: clear the previous srp host and services without blocking --- src/include/platform/ThreadStackManager.h | 12 ++++++++ ...enericNetworkCommissioningThreadDriver.cpp | 10 +++++-- ...GenericThreadStackManagerImpl_OpenThread.h | 2 ++ ...nericThreadStackManagerImpl_OpenThread.hpp | 30 +++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/include/platform/ThreadStackManager.h b/src/include/platform/ThreadStackManager.h index f9f7aed04e4253..d58025983617f7 100644 --- a/src/include/platform/ThreadStackManager.h +++ b/src/include/platform/ThreadStackManager.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace chip { @@ -123,6 +124,12 @@ class ThreadStackManager CHIP_ERROR InvalidateAllSrpServices(); ///< Mark all SRP services as invalid CHIP_ERROR RemoveInvalidSrpServices(); ///< Remove SRP services marked as invalid + /** + * @brief Used to clear all thread SRP host and services established between the SRP server and client. + * The task will not be blocked during clearing operation. aCallback will be invoked after clearing. + */ + CHIP_ERROR ClearAllSrpHostAndServicesWithoutBlocking(const std::function & aCallback); + /* * @brief Utility function to clear all thread SRP host and services established between the SRP server and client. * It is expected that a transaction is done between the SRP server and client so the clear request is applied on both ends @@ -315,6 +322,11 @@ inline CHIP_ERROR ThreadStackManager::ClearAllSrpHostAndServices() return static_cast(this)->_ClearAllSrpHostAndServices(); } +inline CHIP_ERROR ThreadStackManager::ClearAllSrpHostAndServicesWithoutBlocking(const std::function & aCallback) +{ + return static_cast(this)->_ClearAllSrpHostAndServicesWithoutBlocking(aCallback); +} + inline void ThreadStackManager::WaitOnSrpClearAllComplete() { return static_cast(this)->_WaitOnSrpClearAllComplete(); diff --git a/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp b/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp index ed5d7ede410f39..46993cc945bba6 100644 --- a/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp +++ b/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp @@ -195,10 +195,14 @@ void GenericThreadDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * c if (ThreadStackMgrImpl().GetThreadProvision(currentDataset) == CHIP_NO_ERROR) { // Clear the previous srp host and services - if (!currentDataset.AsByteSpan().data_equal(mStagingNetwork.AsByteSpan()) && - ThreadStackMgrImpl().ClearAllSrpHostAndServices() != CHIP_NO_ERROR) + if (!currentDataset.AsByteSpan().data_equal(mStagingNetwork.AsByteSpan())) { - status = Status::kUnknownError; + ThreadStackMgrImpl().ClearAllSrpHostAndServicesWithoutBlocking([this, callback]() -> CHIP_ERROR { + return DeviceLayer::ThreadStackMgrImpl().AttachToThreadNetwork(mStagingNetwork, callback); + }); + + // AttachToThreadNetwork will be operated in OnSrpClientNotification if host and services have been removed. + return; } } else diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h index 169820c103e828..2db6a7ac175709 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h @@ -125,6 +125,7 @@ class GenericThreadStackManagerImpl_OpenThread CHIP_ERROR _InvalidateAllSrpServices(); CHIP_ERROR _RemoveInvalidSrpServices(); CHIP_ERROR _ClearAllSrpHostAndServices(); + CHIP_ERROR _ClearAllSrpHostAndServicesWithoutBlocking(const std::function & aCallback); CHIP_ERROR _SetupSrpHost(const char * aHostName); CHIP_ERROR _ClearSrpHost(const char * aHostName); @@ -155,6 +156,7 @@ class GenericThreadStackManagerImpl_OpenThread uint64_t mOverrunCount = 0; bool mIsAttached = false; bool mTemporaryRxOnWhenIdle = false; + static std::function mpClearSrpHostServiceCallback; NetworkCommissioning::ThreadDriver::ScanCallback * mpScanCallback; NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * mpConnectCallback; diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp index 6916c096c9ce50..ccef079dd186fc 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp @@ -1323,6 +1323,12 @@ void GenericThreadStackManagerImpl_OpenThread::OnSrpClientNotificatio ThreadStackMgrImpl().NotifySrpClearAllComplete(); ThreadStackMgrImpl().mIsSrpClearAllRequested = false; } + + if (mpClearSrpHostServiceCallback) + { + mpClearSrpHostServiceCallback(); + mpClearSrpHostServiceCallback = nullptr; + } } } @@ -1664,6 +1670,30 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_ClearAllSrpHost return error; } +template +std::function GenericThreadStackManagerImpl_OpenThread::mpClearSrpHostServiceCallback = nullptr; + +template +CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_ClearAllSrpHostAndServicesWithoutBlocking( + const std::function & aCallback) +{ + VerifyOrReturnError(mOTInst, CHIP_ERROR_INCORRECT_STATE); + CHIP_ERROR error = CHIP_NO_ERROR; + Impl()->LockThreadStack(); + if (!mpClearSrpHostServiceCallback && aCallback) + { + error = + MapOpenThreadError(otSrpClientRemoveHostAndServices(mOTInst, true /*aRemoveKeyLease*/, true /*aSendUnregToServer*/)); + mpClearSrpHostServiceCallback = aCallback; + Impl()->UnlockThreadStack(); + } + else + { + Impl()->UnlockThreadStack(); + } + return error; +} + template CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_SetupSrpHost(const char * aHostName) {