Skip to content

Commit

Permalink
[THREAD] Bring clear all Srp Client host and Services to the GenericT…
Browse files Browse the repository at this point in the history
…hreadStackManagerImpl (#32215)

* Bring the functionality to clear all Srp Client host and Services to the GenericThreadStackManagerImpl

* Fix Tizen build

* Update src/platform/Tizen/ThreadStackManagerImpl.cpp

Co-authored-by: Arkadiusz Bokowy <[email protected]>

* Add method documentation and address comments

* Restyled by whitespace

---------

Co-authored-by: Arkadiusz Bokowy <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Jun 5, 2024
1 parent 598b349 commit 2523289
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 64 deletions.
35 changes: 35 additions & 0 deletions src/include/platform/ThreadStackManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ class ThreadStackManager
CHIP_ERROR RemoveSrpService(const char * aInstanceName, const char * aName);
CHIP_ERROR InvalidateAllSrpServices(); ///< Mark all SRP services as invalid
CHIP_ERROR RemoveInvalidSrpServices(); ///< Remove SRP services marked as invalid

/*
* @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
*
* A generic implementation is provided in `GenericThreadStackManagerImpl_OpenThread` with the SoC OT stack
*/
CHIP_ERROR ClearAllSrpHostAndServices();

/*
* @brief Used to synchronize on the SRP server response confirming the clearing of the host and service entries
* Should be called in ClearAllSrpHostAndServices once the request is sent.
*/
void WaitOnSrpClearAllComplete();

/*
* @brief Notify that the SRP server confirmed the clearing of the host and service entries
* Should be called in the SRP Client set callback in the removal confirmation.
*/
void NotifySrpClearAllComplete();
CHIP_ERROR SetupSrpHost(const char * aHostName);
CHIP_ERROR ClearSrpHost(const char * aHostName);
CHIP_ERROR SetSrpDnsCallbacks(DnsAsyncReturnCallback aInitCallback, DnsAsyncReturnCallback aErrorCallback, void * aContext);
Expand Down Expand Up @@ -289,6 +309,21 @@ inline CHIP_ERROR ThreadStackManager::RemoveInvalidSrpServices()
return static_cast<ImplClass *>(this)->_RemoveInvalidSrpServices();
}

inline CHIP_ERROR ThreadStackManager::ClearAllSrpHostAndServices()
{
return static_cast<ImplClass *>(this)->_ClearAllSrpHostAndServices();
}

inline void ThreadStackManager::WaitOnSrpClearAllComplete()
{
return static_cast<ImplClass *>(this)->_WaitOnSrpClearAllComplete();
}

inline void ThreadStackManager::NotifySrpClearAllComplete()
{
return static_cast<ImplClass *>(this)->_NotifySrpClearAllComplete();
}

inline CHIP_ERROR ThreadStackManager::SetupSrpHost(const char * aHostName)
{
return static_cast<ImplClass *>(this)->_SetupSrpHost(aHostName);
Expand Down
24 changes: 24 additions & 0 deletions src/platform/ESP32/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ void ThreadStackManagerImpl::_UnlockThreadStack()
esp_openthread_lock_release();
}

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void ThreadStackManagerImpl::_WaitOnSrpClearAllComplete()
{
// Only 1 task can be blocked on a srpClearAll request
if (mSrpClearAllRequester == nullptr)
{
mSrpClearAllRequester = xTaskGetCurrentTaskHandle();
// Wait on OnSrpClientNotification which confirms the clearing is done.
// It will notify this current task with NotifySrpClearAllComplete.
// However, we won't wait more than 2s.
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(2000));
mSrpClearAllRequester = nullptr;
}
}

void ThreadStackManagerImpl::_NotifySrpClearAllComplete()
{
if (mSrpClearAllRequester)
{
xTaskNotifyGive(mSrpClearAllRequester);
}
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

void ThreadStackManagerImpl::_ProcessThreadActivity()
{
// Intentionally empty.
Expand Down
11 changes: 11 additions & 0 deletions src/platform/ESP32/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,21 @@ class ThreadStackManagerImpl final : public ThreadStackManager,
void _OnCHIPoBLEAdvertisingStart();
void _OnCHIPoBLEAdvertisingStop();

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void _WaitOnSrpClearAllComplete();
void _NotifySrpClearAllComplete();
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
// ===== Methods that override the GenericThreadStackMa

private:
friend ThreadStackManager & ::chip::DeviceLayer::ThreadStackMgr(void);
friend ThreadStackManagerImpl & ::chip::DeviceLayer::ThreadStackMgrImpl(void);
static ThreadStackManagerImpl sInstance;

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
TaskHandle_t mSrpClearAllRequester = nullptr;
#endif

ThreadStackManagerImpl() = default;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class GenericThreadStackManagerImpl_FreeRTOS
bool _TryLockThreadStack(void);
void _UnlockThreadStack(void);

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void _WaitOnSrpClearAllComplete();
void _NotifySrpClearAllComplete();
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
// ===== Members available to the implementation subclass.

SemaphoreHandle_t mThreadStackLock;
Expand All @@ -88,6 +92,10 @@ class GenericThreadStackManagerImpl_FreeRTOS
#if defined(CHIP_CONFIG_FREERTOS_USE_STATIC_SEMAPHORE) && CHIP_CONFIG_FREERTOS_USE_STATIC_SEMAPHORE
StaticSemaphore_t mThreadStackLockMutex;
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
TaskHandle_t mSrpClearAllRequester = nullptr;
#endif
};

// Instruct the compiler to instantiate the template only when explicitly told to do so.
Expand Down
26 changes: 26 additions & 0 deletions src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ void GenericThreadStackManagerImpl_FreeRTOS<ImplClass>::_UnlockThreadStack(void)
xSemaphoreGive(mThreadStackLock);
}

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
template <class ImplClass>
void GenericThreadStackManagerImpl_FreeRTOS<ImplClass>::_WaitOnSrpClearAllComplete()
{
// Only 1 task can be blocked on a srpClearAll request
if (mSrpClearAllRequester == nullptr)
{
mSrpClearAllRequester = xTaskGetCurrentTaskHandle();
// Wait on OnSrpClientNotification which confirms the slearing is done.
// It will notify this current task with NotifySrpClearAllComplete.
// However, we won't wait more than 2s.
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(2000));
mSrpClearAllRequester = nullptr;
}
}

template <class ImplClass>
void GenericThreadStackManagerImpl_FreeRTOS<ImplClass>::_NotifySrpClearAllComplete()
{
if (mSrpClearAllRequester)
{
xTaskNotifyGive(mSrpClearAllRequester);
}
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

template <class ImplClass>
void GenericThreadStackManagerImpl_FreeRTOS<ImplClass>::SignalThreadActivityPending()
{
Expand Down
23 changes: 23 additions & 0 deletions src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack()
VerifyOrExit(result == WICED_SUCCESS, err = CHIP_ERROR_INTERNAL);
otSysInit(0, NULL);

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
mSrpClearAllSemaphore = wiced_rtos_create_mutex();
VerifyOrExit(mSrpClearAllSemaphore != nullptr, err = CHIP_ERROR_NO_MEMORY);

result = wiced_rtos_init_mutex(mSrpClearAllSemaphore);
VerifyOrExit(result == WICED_SUCCESS, err = CHIP_ERROR_INTERNAL);
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

err = GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::DoInit(NULL);

exit:
Expand Down Expand Up @@ -95,6 +103,21 @@ void ThreadStackManagerImpl::_UnlockThreadStack()
VerifyOrReturn(result == WICED_SUCCESS || result == WICED_NOT_OWNED, ChipLogError(DeviceLayer, "%s %x", __func__, result));
}

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void ThreadStackManagerImpl::_WaitOnSrpClearAllComplete()
{
const wiced_result_t result = wiced_rtos_lock_mutex(mSrpClearAllSemaphore);
VerifyOrReturn(result == WICED_SUCCESS, ChipLogError(DeviceLayer, "%s %x", __func__, result));
}

void ThreadStackManagerImpl::_NotifySrpClearAllComplete()
{
const wiced_result_t result = wiced_rtos_unlock_mutex(mSrpClearAllSemaphore);
VerifyOrReturn(result == WICED_SUCCESS || result == WICED_NOT_OWNED, ChipLogError(DeviceLayer, "%s %x", __func__, result));
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
// ===== Methods that override the GenericThreadStackMa

void ThreadStackManagerImpl::ThreadTaskMain(void)
{
while (true)
Expand Down
8 changes: 8 additions & 0 deletions src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class ThreadStackManagerImpl final : public ThreadStackManager,
bool _TryLockThreadStack();
void _UnlockThreadStack();

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void _WaitOnSrpClearAllComplete();
void _NotifySrpClearAllComplete();
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
// ===== Methods that override the GenericThreadStackMa
private:
// ===== Members for internal use by the following friends.

Expand All @@ -67,6 +72,9 @@ class ThreadStackManagerImpl final : public ThreadStackManager,
wiced_thread_t * mThread;
EventFlags mEventFlags;
wiced_mutex_t * mMutex;
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
wiced_mutex_t * mSrpClearAllSemaphore;
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
static ThreadStackManagerImpl sInstance;

// ===== Private members for use by this class only.
Expand Down
5 changes: 5 additions & 0 deletions src/platform/Linux/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class ThreadStackManagerImpl : public ThreadStackManager
bool _TryLockThreadStack() { return false; } // Intentionally left blank
void _UnlockThreadStack() {} // Intentionally left blank

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void _WaitOnSrpClearAllComplete() {}
void _NotifySrpClearAllComplete() {}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

bool _HaveRouteToAddress(const Inet::IPAddress & destAddr);

void _OnPlatformEvent(const ChipDeviceEvent * event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class GenericThreadStackManagerImpl_OpenThread
CHIP_ERROR _RemoveSrpService(const char * aInstanceName, const char * aName);
CHIP_ERROR _InvalidateAllSrpServices();
CHIP_ERROR _RemoveInvalidSrpServices();
CHIP_ERROR _ClearAllSrpHostAndServices();

CHIP_ERROR _SetupSrpHost(const char * aHostName);
CHIP_ERROR _ClearSrpHost(const char * aHostName);
Expand Down Expand Up @@ -203,6 +204,8 @@ class GenericThreadStackManagerImpl_OpenThread

SrpClient mSrpClient;

bool mIsSrpClearAllRequested = false;

static void OnSrpClientNotification(otError aError, const otSrpClientHostInfo * aHostInfo, const otSrpClientService * aServices,
const otSrpClientService * aRemovedServices, void * aContext);
static void OnSrpClientStateChange(const otSockAddr * aServerSockAddr, void * aContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,12 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnSrpClientNotificatio
ThreadStackMgrImpl().mSrpClient.mIsInitialized = true;
ThreadStackMgrImpl().mSrpClient.mInitializedCallback(ThreadStackMgrImpl().mSrpClient.mCallbackContext,
CHIP_NO_ERROR);

if (ThreadStackMgrImpl().mIsSrpClearAllRequested)
{
ThreadStackMgrImpl().NotifySrpClearAllComplete();
ThreadStackMgrImpl().mIsSrpClearAllRequested = false;
}
}
}

Expand Down Expand Up @@ -1618,6 +1624,35 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_RemoveInvalidSr
return error;
}

/*
* @brief This is a utility function to remove all Thread client srp host and services
* established between the device and the srp server (in most cases the OTBR).
* The calling task is blocked until OnSrpClientNotification which confims the client received the request.
* The blocking mechanism is defined by the platform implementation of `WaitOnSrpClearAllComplete` and `NotifySrpClearAllComplete`
*
* Note: This function is meant to be used during the factory reset sequence.
*
*/
template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_ClearAllSrpHostAndServices()
{
CHIP_ERROR error = CHIP_NO_ERROR;
Impl()->LockThreadStack();
if (!mIsSrpClearAllRequested)
{
error =
MapOpenThreadError(otSrpClientRemoveHostAndServices(mOTInst, true /*aRemoveKeyLease*/, true /*aSendUnregToServer*/));
mIsSrpClearAllRequested = true;
Impl()->UnlockThreadStack();
Impl()->WaitOnSrpClearAllComplete();
}
else
{
Impl()->UnlockThreadStack();
}
return error;
}

template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_SetupSrpHost(const char * aHostName)
{
Expand Down
10 changes: 10 additions & 0 deletions src/platform/Tizen/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,16 @@ CHIP_ERROR ThreadStackManagerImpl::_RemoveInvalidSrpServices()
return CHIP_NO_ERROR;
}

CHIP_ERROR ThreadStackManagerImpl::_ClearAllSrpHostAndServices()
{
for (auto it = mSrpClientServices.begin(); it != mSrpClientServices.end();)
{
ReturnErrorOnFailure(_RemoveSrpService(it->mInstanceName, it->mName));
it = mSrpClientServices.erase(it);
}
return CHIP_NO_ERROR;
}

void ThreadStackManagerImpl::_ThreadIpAddressCb(int index, char * ipAddr, thread_ipaddr_type_e ipAddrType, void * userData)
{
VerifyOrReturn(ipAddr != nullptr, ChipLogError(DeviceLayer, "FAIL: Invalid argument: Thread ipAddr not found"));
Expand Down
6 changes: 6 additions & 0 deletions src/platform/Tizen/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class ThreadStackManagerImpl : public ThreadStackManager
bool _TryLockThreadStack() { return false; } // Intentionally left blank
void _UnlockThreadStack() {} // Intentionally left blank

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void _WaitOnSrpClearAllComplete() {}
void _NotifySrpClearAllComplete() {}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

bool _HaveRouteToAddress(const Inet::IPAddress & destAddr);

void _OnPlatformEvent(const ChipDeviceEvent * event);
Expand Down Expand Up @@ -115,6 +120,7 @@ class ThreadStackManagerImpl : public ThreadStackManager
CHIP_ERROR _RemoveSrpService(const char * aInstanceName, const char * aName);
CHIP_ERROR _InvalidateAllSrpServices();
CHIP_ERROR _RemoveInvalidSrpServices();
CHIP_ERROR _ClearAllSrpHostAndServices();
CHIP_ERROR _SetupSrpHost(const char * aHostName);
CHIP_ERROR _ClearSrpHost(const char * aHostName);
CHIP_ERROR _SetSrpDnsCallbacks(DnsAsyncReturnCallback aInitCallback, DnsAsyncReturnCallback aErrorCallback, void * aContext);
Expand Down
16 changes: 16 additions & 0 deletions src/platform/Zephyr/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack()
return MapOpenThreadError(otError);
});

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
k_sem_init(&mSrpClearAllSemaphore, 0, 1);
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

return CHIP_NO_ERROR;
}

Expand All @@ -85,5 +89,17 @@ void ThreadStackManagerImpl::_UnlockThreadStack()
openthread_api_mutex_unlock(openthread_get_default_context());
}

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void ThreadStackManagerImpl::_WaitOnSrpClearAllComplete()
{
k_sem_take(&mSrpClearAllSemaphore, K_SECONDS(2));
}

void ThreadStackManagerImpl::_NotifySrpClearAllComplete()
{
k_sem_give(&mSrpClearAllSemaphore);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

} // namespace DeviceLayer
} // namespace chip
8 changes: 8 additions & 0 deletions src/platform/Zephyr/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class ThreadStackManagerImpl final : public ThreadStackManager,
bool _TryLockThreadStack();
void _UnlockThreadStack();

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
void _WaitOnSrpClearAllComplete();
void _NotifySrpClearAllComplete();
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
// ===== Methods that override the GenericThreadStackManagerImpl_OpenThread abstract interface.

void _ProcessThreadActivity() {}
Expand All @@ -83,6 +87,10 @@ class ThreadStackManagerImpl final : public ThreadStackManager,
friend ThreadStackManager & ::chip::DeviceLayer::ThreadStackMgr(void);
friend ThreadStackManagerImpl & ::chip::DeviceLayer::ThreadStackMgrImpl(void);

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
k_sem mSrpClearAllSemaphore;
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

static ThreadStackManagerImpl sInstance;

// ===== Private members for use by this class only.
Expand Down
2 changes: 1 addition & 1 deletion src/platform/silabs/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
ThreadStackMgrImpl().RemoveAllSrpServices();
ThreadStackMgr().ClearAllSrpHostAndServices();
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
ChipLogProgress(DeviceLayer, "Clearing Thread provision");
ThreadStackMgr().ErasePersistentInfo();
Expand Down
Loading

0 comments on commit 2523289

Please sign in to comment.