Skip to content

Commit

Permalink
OpenThread: clear the previous srp host and services without blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
DejinChen committed Aug 28, 2024
1 parent 3e9e9db commit 72f1cba
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/include/platform/ThreadStackManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <inet/IPAddress.h>
#include <lib/support/Span.h>
#include <platform/NetworkCommissioning.h>
#include <functional>

namespace chip {

Expand Down Expand Up @@ -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<CHIP_ERROR(void)> & 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
Expand Down Expand Up @@ -315,6 +322,11 @@ inline CHIP_ERROR ThreadStackManager::ClearAllSrpHostAndServices()
return static_cast<ImplClass *>(this)->_ClearAllSrpHostAndServices();
}

inline CHIP_ERROR ThreadStackManager::ClearAllSrpHostAndServicesWithoutBlocking(const std::function<CHIP_ERROR(void)> & aCallback)
{
return static_cast<ImplClass *>(this)->_ClearAllSrpHostAndServicesWithoutBlocking(aCallback);
}

inline void ThreadStackManager::WaitOnSrpClearAllComplete()
{
return static_cast<ImplClass *>(this)->_WaitOnSrpClearAllComplete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class GenericThreadStackManagerImpl_OpenThread
CHIP_ERROR _InvalidateAllSrpServices();
CHIP_ERROR _RemoveInvalidSrpServices();
CHIP_ERROR _ClearAllSrpHostAndServices();
CHIP_ERROR _ClearAllSrpHostAndServicesWithoutBlocking(const std::function<CHIP_ERROR(void)> & aCallback);

CHIP_ERROR _SetupSrpHost(const char * aHostName);
CHIP_ERROR _ClearSrpHost(const char * aHostName);
Expand Down Expand Up @@ -155,6 +156,7 @@ class GenericThreadStackManagerImpl_OpenThread
uint64_t mOverrunCount = 0;
bool mIsAttached = false;
bool mTemporaryRxOnWhenIdle = false;
static std::function<CHIP_ERROR(void)> mpClearSrpHostServiceCallback;

NetworkCommissioning::ThreadDriver::ScanCallback * mpScanCallback;
NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * mpConnectCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,12 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnSrpClientNotificatio
ThreadStackMgrImpl().NotifySrpClearAllComplete();
ThreadStackMgrImpl().mIsSrpClearAllRequested = false;
}

if (mpClearSrpHostServiceCallback)
{
mpClearSrpHostServiceCallback();
mpClearSrpHostServiceCallback = nullptr;
}
}
}

Expand Down Expand Up @@ -1664,6 +1670,30 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_ClearAllSrpHost
return error;
}

template <class ImplClass>
std::function<CHIP_ERROR(void)> GenericThreadStackManagerImpl_OpenThread<ImplClass>::mpClearSrpHostServiceCallback = nullptr;

template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_ClearAllSrpHostAndServicesWithoutBlocking(
const std::function<CHIP_ERROR(void)> & 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 <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_SetupSrpHost(const char * aHostName)
{
Expand Down

0 comments on commit 72f1cba

Please sign in to comment.