[router-table] add new methods to help iterate over router entries (#3527)

This commit is contained in:
Abtin Keshavarzian
2019-01-29 12:12:56 -08:00
committed by Jonathan Hui
parent e71bd6267e
commit 144270105d
2 changed files with 54 additions and 76 deletions
+46 -75
View File
@@ -52,29 +52,12 @@ RouterTable::Iterator::Iterator(Instance &aInstance)
void RouterTable::Iterator::Reset(void) void RouterTable::Iterator::Reset(void)
{ {
RouterTable &routerTable = GetInstance().Get<RouterTable>(); mRouter = GetInstance().Get<RouterTable>().GetFirstEntry();
mRouter = &routerTable.mRouters[0];
if (mRouter->GetRloc16() == 0xffff)
{
mRouter = NULL;
}
} }
void RouterTable::Iterator::Advance(void) void RouterTable::Iterator::Advance(void)
{ {
RouterTable &routerTable = GetInstance().Get<RouterTable>(); mRouter = GetInstance().Get<RouterTable>().GetNextEntry(mRouter);
Router * listEnd = &routerTable.mRouters[Mle::kMaxRouters];
VerifyOrExit(mRouter != NULL);
mRouter++;
VerifyOrExit(mRouter < listEnd && mRouter->GetRloc16() != 0xffff, mRouter = NULL);
exit:
return;
} }
RouterTable::RouterTable(Instance &aInstance) RouterTable::RouterTable(Instance &aInstance)
@@ -86,6 +69,26 @@ RouterTable::RouterTable(Instance &aInstance)
Clear(); 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) void RouterTable::Clear(void)
{ {
memset(mAllocatedRouterIds, 0, sizeof(mAllocatedRouterIds)); memset(mAllocatedRouterIds, 0, sizeof(mAllocatedRouterIds));
@@ -271,19 +274,12 @@ otError RouterTable::Release(uint8_t aRouterId)
mRouterIdReuseDelay[aRouterId] = Mle::kRouterIdReuseDelay; 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->GetNextHop() == rloc16)
if (router.GetRloc16() == 0xffff)
{ {
break; router->SetNextHop(Mle::kInvalidRouterId);
} router->SetCost(0);
if (router.GetNextHop() == rloc16)
{
router.SetNextHop(Mle::kInvalidRouterId);
router.SetCost(0);
} }
} }
@@ -307,21 +303,14 @@ void RouterTable::RemoveNeighbor(Router &aRouter)
aRouter.SetLinkQualityOut(0); aRouter.SetLinkQualityOut(0);
aRouter.SetLastHeard(TimerMilli::GetNow()); 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->GetNextHop() == aRouter.GetRouterId())
if (cur.GetRloc16() == 0xffff)
{ {
break; cur->SetNextHop(Mle::kInvalidRouterId);
} cur->SetCost(0);
if (cur.GetNextHop() == aRouter.GetRouterId()) if (GetLinkCost(*cur) >= Mle::kMaxRouteCost)
{
cur.SetNextHop(Mle::kInvalidRouterId);
cur.SetCost(0);
if (GetLinkCost(cur) >= Mle::kMaxRouteCost)
{ {
netif.GetMle().ResetAdvertiseInterval(); netif.GetMle().ResetAdvertiseInterval();
} }
@@ -341,16 +330,9 @@ uint8_t RouterTable::GetActiveLinkCount(void) const
{ {
uint8_t activeLinks = 0; 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 (router->GetState() == Neighbor::kStateValid)
if (cur.GetRloc16() == 0xffff)
{
break;
}
if (cur.GetState() == Neighbor::kStateValid)
{ {
activeLinks++; activeLinks++;
} }
@@ -365,11 +347,10 @@ Router *RouterTable::GetNeighbor(uint16_t aRloc16)
VerifyOrExit(aRloc16 != GetNetif().GetMle().GetRloc16()); 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(); ExitNow();
} }
} }
@@ -384,11 +365,10 @@ Router *RouterTable::GetNeighbor(const Mac::ExtAddress &aExtAddress)
VerifyOrExit(aExtAddress != GetNetif().GetMac().GetExtAddress()); 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(); ExitNow();
} }
} }
@@ -399,41 +379,32 @@ exit:
const Router *RouterTable::GetRouter(uint8_t aRouterId) const const Router *RouterTable::GetRouter(uint8_t aRouterId) const
{ {
const Router *rval = NULL; const Router *router = NULL;
uint16_t rloc16 = Mle::Mle::GetRloc16(aRouterId); 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]; break;
ExitNow();
} }
} }
exit: return router;
return rval;
} }
Router *RouterTable::GetRouter(const Mac::ExtAddress &aExtAddress) Router *RouterTable::GetRouter(const Mac::ExtAddress &aExtAddress)
{ {
Router *router = NULL; 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; break;
} }
if (mRouters[i].GetExtAddress() == aExtAddress)
{
router = &mRouters[i];
ExitNow();
}
} }
exit:
return router; return router;
} }
@@ -488,9 +459,9 @@ uint8_t RouterTable::GetNeighborCount(void) const
{ {
uint8_t count = 0; 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++; count++;
} }
+8 -1
View File
@@ -332,7 +332,14 @@ public:
void ProcessTimerTick(void); void ProcessTimerTick(void);
private: 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<Router *>(const_cast<const RouterTable *>(this)->GetFirstEntry()); }
Router *GetNextEntry(Router *aRouter)
{
return const_cast<Router *>(const_cast<const RouterTable *>(this)->GetNextEntry(aRouter));
}
Router mRouters[Mle::kMaxRouters]; Router mRouters[Mle::kMaxRouters];
uint8_t mAllocatedRouterIds[BitVectorBytes(Mle::kMaxRouterId)]; uint8_t mAllocatedRouterIds[BitVectorBytes(Mle::kMaxRouterId)];