From b7df29080d847eb5e8a4a171362f94219199e3fe Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 23 Apr 2026 08:01:44 +0200 Subject: [PATCH] [mle] use entry count instead of byte length in `RouteTlv` (#12957) This commit renames `GetRouteDataLength()` and `SetRouteDataLength()` to `GetRouteDataEntryCount()` and `SetRouteDataEntryCount()` in the `RouteTlv` class. When `OPENTHREAD_CONFIG_MLE_LONG_ROUTES_ENABLE` is enabled, the route data entries use a packed format (12 bits or 1.5 bytes per entry). Consequently, the byte length of the route data field in the TLV is no longer equal to the number of route entries. This change ensures that `GetRouteDataEntryCount()` correctly calculates the number of entries from the TLV length and `SetRouteDataEntryCount()` sets the TLV length correctly based on the entry count. --- src/core/thread/mle_tlvs.cpp | 2 +- src/core/thread/mle_tlvs.hpp | 24 ++++++++++++------------ src/core/thread/router_table.cpp | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/core/thread/mle_tlvs.cpp b/src/core/thread/mle_tlvs.cpp index 4f2024963..b2db40274 100644 --- a/src/core/thread/mle_tlvs.cpp +++ b/src/core/thread/mle_tlvs.cpp @@ -61,7 +61,7 @@ bool RouteTlv::IsValid(void) const VerifyOrExit(GetLength() >= sizeof(mRouterIdMask)); VerifyOrExit(mRouterIdMask.IsValid()); - isValid = (GetRouteDataLength() >= mRouterIdMask.DetermineAllocatedCount()); + isValid = (GetRouteDataEntryCount() >= mRouterIdMask.DetermineAllocatedCount()); exit: return isValid; diff --git a/src/core/thread/mle_tlvs.hpp b/src/core/thread/mle_tlvs.hpp index 1dc4fba4e..59f9e93ec 100644 --- a/src/core/thread/mle_tlvs.hpp +++ b/src/core/thread/mle_tlvs.hpp @@ -291,18 +291,18 @@ public: bool IsSingleton(void) const { return IsValid() && (mRouterIdMask.DetermineAllocatedCount() <= 1); } /** - * Returns the Route Data Length value. + * Returns the number of Route Data entries in the Route TLV. * - * @returns The Route Data Length value. + * @returns The Route Data Entry Count. */ - uint8_t GetRouteDataLength(void) const { return GetLength() - sizeof(mRouterIdMask); } + uint8_t GetRouteDataEntryCount(void) const { return GetLength() - sizeof(mRouterIdMask); } /** - * Sets the Route Data Length value. + * Sets the Route Data entry count. * - * @param[in] aLength The Route Data Length value. + * @param[in] aCount The number of Route Data entries in the Route TLV. */ - void SetRouteDataLength(uint8_t aLength) { SetLength(sizeof(mRouterIdMask) + aLength); } + void SetRouteDataEntryCount(uint8_t aCount) { SetLength(sizeof(mRouterIdMask) + aCount); } /** * Returns the Route Cost value for a given Router index. @@ -437,18 +437,18 @@ public: void SetRouterId(uint8_t aRouterId) { mRouterIdMask.Add(aRouterId); } /** - * Returns the Route Data Length value. + * Returns the number of Route Data entries in the Route TLV. * - * @returns The Route Data Length value in bytes + * @returns The Route Data Entry Count. */ - uint8_t GetRouteDataLength(void) const { return GetLength() - sizeof(mRouterIdMask); } + uint8_t GetRouteDataEntryCount(void) const { return (GetLength() - sizeof(mRouterIdMask)) * 2 / 3; } /** - * Sets the Route Data Length value. + * Sets the Route Data entry count. * - * @param[in] aLength The Route Data Length value in number of router entries + * @param[in] aCount The number of Route Data entries in the Route TLV. */ - void SetRouteDataLength(uint8_t aLength) { SetLength(sizeof(mRouterIdMask) + aLength + (aLength + 1) / 2); } + void SetRouteDataEntryCount(uint8_t aCount) { SetLength(sizeof(mRouterIdMask) + aCount + (aCount + 1) / 2); } /** * Returns the Route Cost value for a given Router index. diff --git a/src/core/thread/router_table.cpp b/src/core/thread/router_table.cpp index 31ec6b859..523990b99 100644 --- a/src/core/thread/router_table.cpp +++ b/src/core/thread/router_table.cpp @@ -841,7 +841,7 @@ void RouterTable::FillRouteTlv(Mle::RouteTlv &aRouteTlv, const Neighbor *aNeighb routerIndex++; } - aRouteTlv.SetRouteDataLength(routerIndex); + aRouteTlv.SetRouteDataEntryCount(routerIndex); } void RouterTable::HandleTimeTick(void)