From 494575f8fc43c1d4cc293a1860ab3f5508e91293 Mon Sep 17 00:00:00 2001 From: sarveshkumarv3 <86755931+sarveshkumarv3@users.noreply.github.com> Date: Thu, 21 May 2026 17:18:54 -0700 Subject: [PATCH] [cli] add cli command to clear EID-RLOC cache (#9985) Co-authored-by: Abtin Keshavarzian --- include/openthread/instance.h | 2 +- include/openthread/thread_ftd.h | 9 +++++ src/cli/cli.cpp | 64 +++++++++++++++++++++------------ src/core/api/thread_ftd_api.cpp | 2 ++ 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 43fa05072..3c9a0d70f 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -52,7 +52,7 @@ extern "C" { * * @note This number versions both OpenThread platform and user APIs. */ -#define OPENTHREAD_API_VERSION (596) +#define OPENTHREAD_API_VERSION (597) /** * @addtogroup api-instance diff --git a/include/openthread/thread_ftd.h b/include/openthread/thread_ftd.h index 4d81193c6..5342c9d5e 100644 --- a/include/openthread/thread_ftd.h +++ b/include/openthread/thread_ftd.h @@ -629,6 +629,15 @@ otError otThreadGetRouterInfo(otInstance *aInstance, uint16_t aRouterId, otRoute */ otError otThreadGetNextCacheEntry(otInstance *aInstance, otCacheEntryInfo *aEntryInfo, otCacheEntryIterator *aIterator); +/** + * Clears the EID cache. + * + * Intended for testing only. + * + * @param[in] aInstance A pointer to an OpenThread instance. + */ +void otThreadClearEidCache(otInstance *aInstance); + /** * Get the Thread PSKc * diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 4ae060bac..a36447a04 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -2938,35 +2938,53 @@ void Interpreter::OutputEidCacheEntry(const otCacheEntryInfo &aEntry) OutputNewLine(); } -/** - * @cli eidcache - * @code - * eidcache - * fd49:caf4:a29f:dc0e:97fc:69dd:3c16:df7d 2000 cache canEvict=1 transTime=0 eid=fd49:caf4:a29f:dc0e:97fc:69dd:3c16:df7d - * fd49:caf4:a29f:dc0e:97fc:69dd:3c16:df7f fffe retry canEvict=1 timeout=10 retryDelay=30 - * Done - * @endcode - * @par - * Returns the EID-to-RLOC cache entries. - * @sa otThreadGetNextCacheEntry - */ template <> otError Interpreter::Process(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgs); + otError error = OT_ERROR_NONE; - otCacheEntryIterator iterator; - otCacheEntryInfo entry; - - ClearAllBytes(iterator); - - while (true) + /** + * @cli eidcache + * @code + * eidcache + * fd49:caf4:a29f:dc0e:97fc:69dd:3c16:df7d 2000 cache canEvict=1 transTime=0 + * eid=fd49:caf4:a29f:dc0e:97fc:69dd:3c16:df7d fd49:caf4:a29f:dc0e:97fc:69dd:3c16:df7f fffe retry + * canEvict=1 timeout=10 retryDelay=30 Done + * @endcode + * @par + * Returns the EID-to-RLOC cache entries. + * @sa otThreadGetNextCacheEntry + */ + if (aArgs[0].IsEmpty()) { - SuccessOrExit(otThreadGetNextCacheEntry(GetInstancePtr(), &entry, &iterator)); - OutputEidCacheEntry(entry); - } + otCacheEntryIterator iterator; + otCacheEntryInfo entry; + ClearAllBytes(iterator); + while (true) + { + SuccessOrExit(otThreadGetNextCacheEntry(GetInstancePtr(), &entry, &iterator)); + OutputEidCacheEntry(entry); + } + } + /** + * @cli eidcache clear + * @code + * eidcache clear + * Done + * @endcode + * @par api_copy + * #otThreadClearEidCache + */ + else if (aArgs[0] == "clear") + { + otThreadClearEidCache(GetInstancePtr()); + } + else + { + error = OT_ERROR_INVALID_ARGS; + } exit: - return OT_ERROR_NONE; + return error; } #endif diff --git a/src/core/api/thread_ftd_api.cpp b/src/core/api/thread_ftd_api.cpp index 29629f0f4..72753095a 100644 --- a/src/core/api/thread_ftd_api.cpp +++ b/src/core/api/thread_ftd_api.cpp @@ -259,6 +259,8 @@ otError otThreadGetNextCacheEntry(otInstance *aInstance, otCacheEntryInfo *aEntr AsCoreType(aIterator)); } +void otThreadClearEidCache(otInstance *aInstance) { AsCoreType(aInstance).Get().Clear(); } + #if OPENTHREAD_CONFIG_MLE_STEERING_DATA_SET_OOB_ENABLE void otThreadSetSteeringData(otInstance *aInstance, const otExtAddress *aExtAddress) {