diff --git a/src/include/platform/ThreadStackManager.h b/src/include/platform/ThreadStackManager.h index 86d895ec1cfe52..cda3c7acdccbd5 100644 --- a/src/include/platform/ThreadStackManager.h +++ b/src/include/platform/ThreadStackManager.h @@ -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); @@ -289,6 +309,21 @@ inline CHIP_ERROR ThreadStackManager::RemoveInvalidSrpServices() return static_cast(this)->_RemoveInvalidSrpServices(); } +inline CHIP_ERROR ThreadStackManager::ClearAllSrpHostAndServices() +{ + return static_cast(this)->_ClearAllSrpHostAndServices(); +} + +inline void ThreadStackManager::WaitOnSrpClearAllComplete() +{ + return static_cast(this)->_WaitOnSrpClearAllComplete(); +} + +inline void ThreadStackManager::NotifySrpClearAllComplete() +{ + return static_cast(this)->_NotifySrpClearAllComplete(); +} + inline CHIP_ERROR ThreadStackManager::SetupSrpHost(const char * aHostName) { return static_cast(this)->_SetupSrpHost(aHostName); diff --git a/src/platform/ESP32/ThreadStackManagerImpl.cpp b/src/platform/ESP32/ThreadStackManagerImpl.cpp index eed07af6d68af1..0449607999668b 100644 --- a/src/platform/ESP32/ThreadStackManagerImpl.cpp +++ b/src/platform/ESP32/ThreadStackManagerImpl.cpp @@ -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. diff --git a/src/platform/ESP32/ThreadStackManagerImpl.h b/src/platform/ESP32/ThreadStackManagerImpl.h index 46795d658d6535..1287e7a5f3a1ba 100644 --- a/src/platform/ESP32/ThreadStackManagerImpl.h +++ b/src/platform/ESP32/ThreadStackManagerImpl.h @@ -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; }; diff --git a/src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.h b/src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.h index 870fcf2ed80657..059b701f6bda05 100644 --- a/src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.h +++ b/src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.h @@ -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; @@ -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. diff --git a/src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.hpp b/src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.hpp index 4dbee81b23af01..9008258989375e 100644 --- a/src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.hpp +++ b/src/platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.hpp @@ -101,6 +101,32 @@ void GenericThreadStackManagerImpl_FreeRTOS::_UnlockThreadStack(void) xSemaphoreGive(mThreadStackLock); } +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT +template +void GenericThreadStackManagerImpl_FreeRTOS::_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 +void GenericThreadStackManagerImpl_FreeRTOS::_NotifySrpClearAllComplete() +{ + if (mSrpClearAllRequester) + { + xTaskNotifyGive(mSrpClearAllRequester); + } +} +#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + template void GenericThreadStackManagerImpl_FreeRTOS::SignalThreadActivityPending() { diff --git a/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp b/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp index f0393a795ef63a..21297d75cf5252 100644 --- a/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp +++ b/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.cpp @@ -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::DoInit(NULL); exit: @@ -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) diff --git a/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h b/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h index cdb2cb8ee65fdc..5cd3fef1c60239 100644 --- a/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h +++ b/src/platform/Infineon/CYW30739/ThreadStackManagerImpl.h @@ -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. @@ -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. diff --git a/src/platform/Linux/ThreadStackManagerImpl.h b/src/platform/Linux/ThreadStackManagerImpl.h index 977c6b94d0027f..e262837dd84c76 100755 --- a/src/platform/Linux/ThreadStackManagerImpl.h +++ b/src/platform/Linux/ThreadStackManagerImpl.h @@ -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); diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h index 161c2b202a8310..8ce0805182dcaa 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h @@ -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); @@ -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); diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp index acc0c78d0c8eb7..21c44dd00279e3 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp @@ -1307,6 +1307,12 @@ void GenericThreadStackManagerImpl_OpenThread::OnSrpClientNotificatio ThreadStackMgrImpl().mSrpClient.mIsInitialized = true; ThreadStackMgrImpl().mSrpClient.mInitializedCallback(ThreadStackMgrImpl().mSrpClient.mCallbackContext, CHIP_NO_ERROR); + + if (ThreadStackMgrImpl().mIsSrpClearAllRequested) + { + ThreadStackMgrImpl().NotifySrpClearAllComplete(); + ThreadStackMgrImpl().mIsSrpClearAllRequested = false; + } } } @@ -1618,6 +1624,35 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_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 +CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_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 CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_SetupSrpHost(const char * aHostName) { diff --git a/src/platform/Tizen/ThreadStackManagerImpl.cpp b/src/platform/Tizen/ThreadStackManagerImpl.cpp index 47a4548507e332..e6252d92926190 100644 --- a/src/platform/Tizen/ThreadStackManagerImpl.cpp +++ b/src/platform/Tizen/ThreadStackManagerImpl.cpp @@ -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")); diff --git a/src/platform/Tizen/ThreadStackManagerImpl.h b/src/platform/Tizen/ThreadStackManagerImpl.h index 05b300f574114b..07092f43b320eb 100644 --- a/src/platform/Tizen/ThreadStackManagerImpl.h +++ b/src/platform/Tizen/ThreadStackManagerImpl.h @@ -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); @@ -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); diff --git a/src/platform/Zephyr/ThreadStackManagerImpl.cpp b/src/platform/Zephyr/ThreadStackManagerImpl.cpp index 3cfcfe3b7888b2..95bdfc8ea7044a 100644 --- a/src/platform/Zephyr/ThreadStackManagerImpl.cpp +++ b/src/platform/Zephyr/ThreadStackManagerImpl.cpp @@ -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; } @@ -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 diff --git a/src/platform/Zephyr/ThreadStackManagerImpl.h b/src/platform/Zephyr/ThreadStackManagerImpl.h index c0c66b492c6b1d..ec9feb8b121d3e 100644 --- a/src/platform/Zephyr/ThreadStackManagerImpl.h +++ b/src/platform/Zephyr/ThreadStackManagerImpl.h @@ -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() {} @@ -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. diff --git a/src/platform/silabs/ConfigurationManagerImpl.cpp b/src/platform/silabs/ConfigurationManagerImpl.cpp index a3e1778c11ac3f..8412075de3a914 100644 --- a/src/platform/silabs/ConfigurationManagerImpl.cpp +++ b/src/platform/silabs/ConfigurationManagerImpl.cpp @@ -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(); diff --git a/src/platform/silabs/ThreadStackManagerImpl.h b/src/platform/silabs/ThreadStackManagerImpl.h index b1436b3b75b27e..c56990fb0d3712 100644 --- a/src/platform/silabs/ThreadStackManagerImpl.h +++ b/src/platform/silabs/ThreadStackManagerImpl.h @@ -72,19 +72,11 @@ class ThreadStackManagerImpl final : public ThreadStackManager, using ThreadStackManager::InitThreadStack; CHIP_ERROR InitThreadStack(otInstance * otInst); -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT - void RemoveAllSrpServices(); -#endif private: // ===== Methods that implement the ThreadStackManager abstract interface. CHIP_ERROR _InitThreadStack(void); -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT - static void OnSrpClientRemoveCallback(otError aError, const otSrpClientHostInfo * aHostInfo, - const otSrpClientService * aServices, const otSrpClientService * aRemovedServices, - void * aContext); -#endif // ===== Members for internal use by the following friends. friend ThreadStackManager & ::chip::DeviceLayer::ThreadStackMgr(void); @@ -94,9 +86,6 @@ class ThreadStackManagerImpl final : public ThreadStackManager, static ThreadStackManagerImpl sInstance; static bool IsInitialized(); -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT - TaskHandle_t srpRemoveRequester = nullptr; -#endif // ===== Private members for use by this class only. diff --git a/src/platform/silabs/efr32/ThreadStackManagerImpl.cpp b/src/platform/silabs/efr32/ThreadStackManagerImpl.cpp index c4556036034e78..83767f9a0e1fb8 100644 --- a/src/platform/silabs/efr32/ThreadStackManagerImpl.cpp +++ b/src/platform/silabs/efr32/ThreadStackManagerImpl.cpp @@ -66,58 +66,6 @@ bool ThreadStackManagerImpl::IsInitialized() return sInstance.mThreadStackLock != NULL; } -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT -/* - * @brief Notifies `RemoveAllSrpServices` that the Srp Client removal has completed - * and unblock the calling task. - * - * No data is processed. - */ -void ThreadStackManagerImpl::OnSrpClientRemoveCallback(otError aError, const otSrpClientHostInfo * aHostInfo, - const otSrpClientService * aServices, - const otSrpClientService * aRemovedServices, void * aContext) -{ - if (ThreadStackMgrImpl().srpRemoveRequester) - { - xTaskNotifyGive(ThreadStackMgrImpl().srpRemoveRequester); - } -} - -/* - * @brief This is a utility function to remove all Thread client Srp services - * established between the device and the srp server (in most cases the OTBR). - * The calling task is blocked until OnSrpClientRemoveCallback. - * - * Note: This function is meant to be used during the factory reset sequence. - * It overrides the generic SrpClient callback `OnSrpClientNotification` with - * OnSrpClientRemoveCallback which doesn't process any of the callback data. - * - * If there is a usecase where this function would be needed in a non-Factory reset context, - * OnSrpClientRemoveCallback should be extended and tied back with the GenericThreadStackManagerImpl_OpenThread - * management of the srp clients. - */ -void ThreadStackManagerImpl::RemoveAllSrpServices() -{ - // This check ensure that only one srp services removal is running - if (ThreadStackMgrImpl().srpRemoveRequester == nullptr) - { - srpRemoveRequester = xTaskGetCurrentTaskHandle(); - otSrpClientSetCallback(OTInstance(), &OnSrpClientRemoveCallback, nullptr); - InvalidateAllSrpServices(); - if (RemoveInvalidSrpServices() == CHIP_NO_ERROR) - { - // Wait for the OnSrpClientRemoveCallback. - ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(2000)); - } - else - { - ChipLogError(DeviceLayer, "Failed to remove srp services"); - } - ThreadStackMgrImpl().srpRemoveRequester = nullptr; - } -} -#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/telink/ThreadStackManagerImpl.cpp b/src/platform/telink/ThreadStackManagerImpl.cpp index e66735b1de68f2..13ba8be424563b 100644 --- a/src/platform/telink/ThreadStackManagerImpl.cpp +++ b/src/platform/telink/ThreadStackManagerImpl.cpp @@ -68,6 +68,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; } @@ -87,6 +91,18 @@ 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 + CHIP_ERROR ThreadStackManagerImpl::_AttachToThreadNetwork(const Thread::OperationalDataset & dataset, NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * callback) diff --git a/src/platform/telink/ThreadStackManagerImpl.h b/src/platform/telink/ThreadStackManagerImpl.h index d1040542f3c616..21db9972f62dec 100644 --- a/src/platform/telink/ThreadStackManagerImpl.h +++ b/src/platform/telink/ThreadStackManagerImpl.h @@ -74,6 +74,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() {} @@ -95,6 +99,10 @@ class ThreadStackManagerImpl final : public ThreadStackManager, bool mRadioBlocked; bool mReadyToAttach; +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + k_sem mSrpClearAllSemaphore; +#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + NetworkCommissioning::ThreadDriver::ScanCallback * mpScanCallback; }; diff --git a/src/platform/webos/ThreadStackManagerImpl.h b/src/platform/webos/ThreadStackManagerImpl.h index 876c339cceb6ae..a21d4cbf551251 100644 --- a/src/platform/webos/ThreadStackManagerImpl.h +++ b/src/platform/webos/ThreadStackManagerImpl.h @@ -50,6 +50,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);