From 144270105d7fbe0d5947876a9b480ae26335e6aa Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 29 Jan 2019 12:12:56 -0800 Subject: [PATCH] [router-table] add new methods to help iterate over router entries (#3527) --- src/core/thread/router_table.cpp | 121 ++++++++++++------------------- src/core/thread/router_table.hpp | 9 ++- 2 files changed, 54 insertions(+), 76 deletions(-) diff --git a/src/core/thread/router_table.cpp b/src/core/thread/router_table.cpp index dbf7028dd..17b41d6ae 100644 --- a/src/core/thread/router_table.cpp +++ b/src/core/thread/router_table.cpp @@ -52,29 +52,12 @@ RouterTable::Iterator::Iterator(Instance &aInstance) void RouterTable::Iterator::Reset(void) { - RouterTable &routerTable = GetInstance().Get(); - - mRouter = &routerTable.mRouters[0]; - - if (mRouter->GetRloc16() == 0xffff) - { - mRouter = NULL; - } + mRouter = GetInstance().Get().GetFirstEntry(); } void RouterTable::Iterator::Advance(void) { - RouterTable &routerTable = GetInstance().Get(); - Router * listEnd = &routerTable.mRouters[Mle::kMaxRouters]; - - VerifyOrExit(mRouter != NULL); - - mRouter++; - - VerifyOrExit(mRouter < listEnd && mRouter->GetRloc16() != 0xffff, mRouter = NULL); - -exit: - return; + mRouter = GetInstance().Get().GetNextEntry(mRouter); } RouterTable::RouterTable(Instance &aInstance) @@ -86,6 +69,26 @@ RouterTable::RouterTable(Instance &aInstance) Clear(); } +const Router *RouterTable::GetFirstEntry(void) const +{ + const Router *router = &mRouters[0]; + VerifyOrExit(router->GetRloc16() != 0xffff, router = NULL); + +exit: + return router; +} + +const Router *RouterTable::GetNextEntry(const Router *aRouter) const +{ + VerifyOrExit(aRouter != NULL); + aRouter++; + VerifyOrExit(aRouter < &mRouters[Mle::kMaxRouters], aRouter = NULL); + VerifyOrExit(aRouter->GetRloc16() != 0xffff, aRouter = NULL); + +exit: + return aRouter; +} + void RouterTable::Clear(void) { memset(mAllocatedRouterIds, 0, sizeof(mAllocatedRouterIds)); @@ -271,19 +274,12 @@ otError RouterTable::Release(uint8_t aRouterId) mRouterIdReuseDelay[aRouterId] = Mle::kRouterIdReuseDelay; - for (int i = 0; i < Mle::kMaxRouters; i++) + for (Router *router = GetFirstEntry(); router != NULL; router = GetNextEntry(router)) { - Router &router = mRouters[i]; - - if (router.GetRloc16() == 0xffff) + if (router->GetNextHop() == rloc16) { - break; - } - - if (router.GetNextHop() == rloc16) - { - router.SetNextHop(Mle::kInvalidRouterId); - router.SetCost(0); + router->SetNextHop(Mle::kInvalidRouterId); + router->SetCost(0); } } @@ -307,21 +303,14 @@ void RouterTable::RemoveNeighbor(Router &aRouter) aRouter.SetLinkQualityOut(0); aRouter.SetLastHeard(TimerMilli::GetNow()); - for (uint8_t i = 0; i < Mle::kMaxRouters; i++) + for (Router *cur = GetFirstEntry(); cur != NULL; cur = GetNextEntry(cur)) { - Router &cur = mRouters[i]; - - if (cur.GetRloc16() == 0xffff) + if (cur->GetNextHop() == aRouter.GetRouterId()) { - break; - } + cur->SetNextHop(Mle::kInvalidRouterId); + cur->SetCost(0); - if (cur.GetNextHop() == aRouter.GetRouterId()) - { - cur.SetNextHop(Mle::kInvalidRouterId); - cur.SetCost(0); - - if (GetLinkCost(cur) >= Mle::kMaxRouteCost) + if (GetLinkCost(*cur) >= Mle::kMaxRouteCost) { netif.GetMle().ResetAdvertiseInterval(); } @@ -341,16 +330,9 @@ uint8_t RouterTable::GetActiveLinkCount(void) const { uint8_t activeLinks = 0; - for (int i = 0; i < Mle::kMaxRouters; i++) + for (const Router *router = GetFirstEntry(); router != NULL; router = GetNextEntry(router)) { - const Router &cur = mRouters[i]; - - if (cur.GetRloc16() == 0xffff) - { - break; - } - - if (cur.GetState() == Neighbor::kStateValid) + if (router->GetState() == Neighbor::kStateValid) { activeLinks++; } @@ -365,11 +347,10 @@ Router *RouterTable::GetNeighbor(uint16_t aRloc16) VerifyOrExit(aRloc16 != GetNetif().GetMle().GetRloc16()); - for (int i = 0; i < Mle::kMaxRouters; i++) + for (router = GetFirstEntry(); router != NULL; router = GetNextEntry(router)) { - if (mRouters[i].GetState() == Neighbor::kStateValid && mRouters[i].GetRloc16() == aRloc16) + if (router->GetState() == Neighbor::kStateValid && router->GetRloc16() == aRloc16) { - router = &mRouters[i]; ExitNow(); } } @@ -384,11 +365,10 @@ Router *RouterTable::GetNeighbor(const Mac::ExtAddress &aExtAddress) VerifyOrExit(aExtAddress != GetNetif().GetMac().GetExtAddress()); - for (int i = 0; i < Mle::kMaxRouters; i++) + for (router = GetFirstEntry(); router != NULL; router = GetNextEntry(router)) { - if (mRouters[i].GetState() == Neighbor::kStateValid && mRouters[i].GetExtAddress() == aExtAddress) + if (router->GetState() == Neighbor::kStateValid && router->GetExtAddress() == aExtAddress) { - router = &mRouters[i]; ExitNow(); } } @@ -399,41 +379,32 @@ exit: const Router *RouterTable::GetRouter(uint8_t aRouterId) const { - const Router *rval = NULL; + const Router *router = NULL; uint16_t rloc16 = Mle::Mle::GetRloc16(aRouterId); - for (uint8_t i = 0; i < Mle::kMaxRouters; i++) + for (router = GetFirstEntry(); router != NULL; router = GetNextEntry(router)) { - if (mRouters[i].GetRloc16() == rloc16) + if (router->GetRloc16() == rloc16) { - rval = &mRouters[i]; - ExitNow(); + break; } } -exit: - return rval; + return router; } Router *RouterTable::GetRouter(const Mac::ExtAddress &aExtAddress) { Router *router = NULL; - for (int i = 0; i < Mle::kMaxRouters; i++) + for (router = GetFirstEntry(); router != NULL; router = GetNextEntry(router)) { - if (mRouters[i].GetRloc16() == 0xffff) + if (router->GetExtAddress() == aExtAddress) { break; } - - if (mRouters[i].GetExtAddress() == aExtAddress) - { - router = &mRouters[i]; - ExitNow(); - } } -exit: return router; } @@ -488,9 +459,9 @@ uint8_t RouterTable::GetNeighborCount(void) const { uint8_t count = 0; - for (int i = 0; i < Mle::kMaxRouters; i++) + for (const Router *router = GetFirstEntry(); router != NULL; router = GetNextEntry(router)) { - if (mRouters[i].GetState() == Neighbor::kStateValid) + if (router->GetState() == Neighbor::kStateValid) { count++; } diff --git a/src/core/thread/router_table.hpp b/src/core/thread/router_table.hpp index 7e63973af..9433db1a1 100644 --- a/src/core/thread/router_table.hpp +++ b/src/core/thread/router_table.hpp @@ -332,7 +332,14 @@ public: void ProcessTimerTick(void); private: - void UpdateAllocation(void); + void UpdateAllocation(void); + const Router *GetFirstEntry(void) const; + const Router *GetNextEntry(const Router *aRouter) const; + Router *GetFirstEntry(void) { return const_cast(const_cast(this)->GetFirstEntry()); } + Router *GetNextEntry(Router *aRouter) + { + return const_cast(const_cast(this)->GetNextEntry(aRouter)); + } Router mRouters[Mle::kMaxRouters]; uint8_t mAllocatedRouterIds[BitVectorBytes(Mle::kMaxRouterId)];