diff --git a/src/core/api/thread_api.cpp b/src/core/api/thread_api.cpp index 190d035c3..49bbdceb1 100644 --- a/src/core/api/thread_api.cpp +++ b/src/core/api/thread_api.cpp @@ -346,38 +346,7 @@ uint16_t otThreadGetRloc16(otInstance *aInstance) otError otThreadGetParentInfo(otInstance *aInstance, otRouterInfo *aParentInfo) { - Error error = kErrorNone; - Router *parent; - - OT_ASSERT(aParentInfo != nullptr); - - // Reference device needs get the original parent's info even after the node state changed. -#if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - VerifyOrExit(AsCoreType(aInstance).Get().IsChild(), error = kErrorInvalidState); -#endif - - parent = &AsCoreType(aInstance).Get().GetParent(); - - aParentInfo->mExtAddress = parent->GetExtAddress(); - aParentInfo->mRloc16 = parent->GetRloc16(); - aParentInfo->mRouterId = Mle::Mle::RouterIdFromRloc16(parent->GetRloc16()); - aParentInfo->mNextHop = parent->GetNextHop(); - aParentInfo->mPathCost = parent->GetCost(); - aParentInfo->mLinkQualityIn = parent->GetLinkInfo().GetLinkQuality(); - aParentInfo->mLinkQualityOut = parent->GetLinkQualityOut(); - aParentInfo->mAge = static_cast(Time::MsecToSec(TimerMilli::GetNow() - parent->GetLastHeard())); - aParentInfo->mAllocated = true; - aParentInfo->mLinkEstablished = parent->IsStateValid(); - aParentInfo->mVersion = parent->GetVersion(); -#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE - aParentInfo->mCslClockAccuracy = parent->GetCslClockAccuracy(); - aParentInfo->mCslUncertainty = parent->GetCslUncertainty(); -#endif - -#if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE -exit: -#endif - return error; + return AsCoreType(aInstance).Get().GetParentInfo(AsCoreType(aParentInfo)); } otError otThreadGetParentAverageRssi(otInstance *aInstance, int8_t *aParentRssi) diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 0a59e1bd1..82ade7dd0 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -3819,6 +3819,24 @@ uint16_t Mle::GetNextHop(uint16_t aDestination) const return (mParent.IsStateValid()) ? mParent.GetRloc16() : static_cast(Mac::kShortAddrInvalid); } +Error Mle::GetParentInfo(Router::Info &aParentInfo) const +{ + Error error = kErrorNone; + + // Skip the check for reference device since it needs to get the + // original parent's info even after role change. + +#if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE + VerifyOrExit(IsChild(), error = kErrorInvalidState); +#endif + + aParentInfo.SetFrom(mParent); + ExitNow(); + +exit: + return error; +} + bool Mle::IsRoutingLocator(const Ip6::Address &aAddress) const { return IsMeshLocalAddress(aAddress) && aAddress.GetIid().IsRoutingLocator(); diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index accd72bd3..062c0a956 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -417,6 +417,17 @@ public: */ Router &GetParent(void) { return mParent; } + /** + * The method retrieves information about the parent. + * + * @param[out] aParentInfo Reference to a parent information structure. + * + * @retval kErrorNone Successfully retrieved the parent info and updated @p aParentInfo. + * @retval kErrorInvalidState Device role is not child. + * + */ + Error GetParentInfo(Router::Info &aParentInfo) const; + /** * This method get the parent candidate. * diff --git a/src/core/thread/topology.cpp b/src/core/thread/topology.cpp index 0eef0a0bc..dc3e75b16 100644 --- a/src/core/thread/topology.cpp +++ b/src/core/thread/topology.cpp @@ -524,6 +524,11 @@ void Router::Info::SetFrom(const Router &aRouter) mLinkQualityIn = aRouter.GetLinkInfo().GetLinkQuality(); mLinkQualityOut = aRouter.GetLinkQualityOut(); mAge = static_cast(Time::MsecToSec(TimerMilli::GetNow() - aRouter.GetLastHeard())); + mVersion = aRouter.GetVersion(); +#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE + mCslClockAccuracy = aRouter.GetCslClockAccuracy(); + mCslUncertainty = aRouter.GetCslUncertainty(); +#endif } void Router::Clear(void)