diff --git a/src/core/api/thread_ftd_api.cpp b/src/core/api/thread_ftd_api.cpp index 0ec09b659..b2eda1565 100644 --- a/src/core/api/thread_ftd_api.cpp +++ b/src/core/api/thread_ftd_api.cpp @@ -260,10 +260,8 @@ otError otThreadGetRouterInfo(otInstance *aInstance, uint16_t aRouterId, otRoute otError otThreadGetNextCacheEntry(otInstance *aInstance, otCacheEntryInfo *aEntryInfo, otCacheEntryIterator *aIterator) { - AssertPointerIsNotNull(aIterator); - AssertPointerIsNotNull(aEntryInfo); - - return AsCoreType(aInstance).Get().GetNextCacheEntry(*aEntryInfo, *aIterator); + return AsCoreType(aInstance).Get().GetNextCacheEntry(AsCoreType(aEntryInfo), + AsCoreType(aIterator)); } #if OPENTHREAD_CONFIG_MLE_STEERING_DATA_SET_OOB_ENABLE diff --git a/src/core/thread/address_resolver.cpp b/src/core/thread/address_resolver.cpp index 9876e938a..997276b84 100644 --- a/src/core/thread/address_resolver.cpp +++ b/src/core/thread/address_resolver.cpp @@ -96,11 +96,8 @@ void AddressResolver::Clear(void) Error AddressResolver::GetNextCacheEntry(EntryInfo &aInfo, Iterator &aIterator) const { Error error = kErrorNone; - const CacheEntryList *list; - const CacheEntry * entry; - - list = reinterpret_cast(aIterator.mData[kIteratorListIndex]); - entry = reinterpret_cast(aIterator.mData[kIteratorEntryIndex]); + const CacheEntryList *list = aIterator.GetList(); + const CacheEntry * entry = aIterator.GetEntry(); while (entry == nullptr) { @@ -130,16 +127,16 @@ Error AddressResolver::GetNextCacheEntry(EntryInfo &aInfo, Iterator &aIterator) // Update the iterator then populate the `aInfo`. - aIterator.mData[kIteratorEntryIndex] = entry->GetNext(); - aIterator.mData[kIteratorListIndex] = list; + aIterator.SetEntry(entry->GetNext()); + aIterator.SetList(list); - memset(&aInfo, 0, sizeof(aInfo)); + aInfo.Clear(); aInfo.mTarget = entry->GetTarget(); aInfo.mRloc16 = entry->GetRloc16(); if (list == &mCachedList) { - aInfo.mState = OT_CACHE_ENTRY_STATE_CACHED; + aInfo.mState = MapEnum(EntryInfo::kStateCached); aInfo.mCanEvict = true; aInfo.mValidLastTrans = entry->IsLastTransactionTimeValid(); @@ -154,15 +151,15 @@ Error AddressResolver::GetNextCacheEntry(EntryInfo &aInfo, Iterator &aIterator) if (list == &mSnoopedList) { - aInfo.mState = OT_CACHE_ENTRY_STATE_SNOOPED; + aInfo.mState = MapEnum(EntryInfo::kStateSnooped); } else if (list == &mQueryList) { - aInfo.mState = OT_CACHE_ENTRY_STATE_QUERY; + aInfo.mState = MapEnum(EntryInfo::kStateQuery); } else { - aInfo.mState = OT_CACHE_ENTRY_STATE_RETRY_QUERY; + aInfo.mState = MapEnum(EntryInfo::kStateRetryQuery); } aInfo.mCanEvict = entry->CanEvict(); diff --git a/src/core/thread/address_resolver.hpp b/src/core/thread/address_resolver.hpp index b65fc604d..8ac629975 100644 --- a/src/core/thread/address_resolver.hpp +++ b/src/core/thread/address_resolver.hpp @@ -37,6 +37,7 @@ #include "openthread-core-config.h" #include "coap/coap.hpp" +#include "common/as_core_type.hpp" #include "common/linked_list.hpp" #include "common/locator.hpp" #include "common/non_copyable.hpp" @@ -66,18 +67,42 @@ class AddressResolver : public InstanceLocator, private NonCopyable { friend class TimeTicker; + class CacheEntry; + class CacheEntryList; + public: /** * This type represents an iterator used for iterating through the EID cache table entries. * */ - typedef otCacheEntryIterator Iterator; + class Iterator : public otCacheEntryIterator, public Clearable + { + friend class AddressResolver; + + static constexpr uint8_t kListIndex = 0; + static constexpr uint8_t kEntryIndex = 1; + + const CacheEntry * GetEntry(void) const { return static_cast(mData[kEntryIndex]); } + void SetEntry(const CacheEntry *aEntry) { mData[kEntryIndex] = aEntry; } + const CacheEntryList *GetList(void) const { return static_cast(mData[kListIndex]); } + void SetList(const CacheEntryList *aList) { mData[kListIndex] = aList; } + }; /** * This type represents an EID cache entry. * */ - typedef otCacheEntryInfo EntryInfo; + class EntryInfo : public otCacheEntryInfo, public Clearable + { + public: + enum State : uint8_t ///< Entry state. + { + kStateCached = OT_CACHE_ENTRY_STATE_CACHED, ///< Cached and in-use. + kStateSnooped = OT_CACHE_ENTRY_STATE_SNOOPED, ///< Created by snoop optimization. + kStateQuery = OT_CACHE_ENTRY_STATE_QUERY, ///< Ongoing query for the EID. + kStateRetryQuery = OT_CACHE_ENTRY_STATE_RETRY_QUERY, ///< In retry wait mode. + }; + }; /** * This constructor initializes the object. @@ -215,9 +240,6 @@ private: static constexpr uint16_t kAddressQueryMaxRetryDelay = OPENTHREAD_CONFIG_TMF_ADDRESS_QUERY_MAX_RETRY_DELAY; static constexpr uint16_t kSnoopBlockEvictionTimeout = OPENTHREAD_CONFIG_TMF_SNOOP_CACHE_ENTRY_TIMEOUT; - static constexpr uint8_t kIteratorListIndex = 0; - static constexpr uint8_t kIteratorEntryIndex = 1; - class CacheEntry : public InstanceLocatorInit { public: @@ -281,7 +303,10 @@ private: }; typedef Pool CacheEntryPool; - typedef LinkedList CacheEntryList; + + class CacheEntryList : public LinkedList + { + }; enum EntryChange : uint8_t { @@ -367,6 +392,10 @@ private: * @} */ +DefineCoreType(otCacheEntryIterator, AddressResolver::Iterator); +DefineCoreType(otCacheEntryInfo, AddressResolver::EntryInfo); +DefineMapEnum(otCacheEntryState, AddressResolver::EntryInfo::State); + } // namespace ot #endif // ADDRESS_RESOLVER_HPP_