mirror of
https://github.com/espressif/openthread.git
synced 2026-09-15 13:40:06 +00:00
[router-table] add new methods to help iterate over router entries (#3527)
This commit is contained in:
committed by
Jonathan Hui
parent
e71bd6267e
commit
144270105d
@@ -52,29 +52,12 @@ RouterTable::Iterator::Iterator(Instance &aInstance)
|
||||
|
||||
void RouterTable::Iterator::Reset(void)
|
||||
{
|
||||
RouterTable &routerTable = GetInstance().Get<RouterTable>();
|
||||
|
||||
mRouter = &routerTable.mRouters[0];
|
||||
|
||||
if (mRouter->GetRloc16() == 0xffff)
|
||||
{
|
||||
mRouter = NULL;
|
||||
}
|
||||
mRouter = GetInstance().Get<RouterTable>().GetFirstEntry();
|
||||
}
|
||||
|
||||
void RouterTable::Iterator::Advance(void)
|
||||
{
|
||||
RouterTable &routerTable = GetInstance().Get<RouterTable>();
|
||||
Router * listEnd = &routerTable.mRouters[Mle::kMaxRouters];
|
||||
|
||||
VerifyOrExit(mRouter != NULL);
|
||||
|
||||
mRouter++;
|
||||
|
||||
VerifyOrExit(mRouter < listEnd && mRouter->GetRloc16() != 0xffff, mRouter = NULL);
|
||||
|
||||
exit:
|
||||
return;
|
||||
mRouter = GetInstance().Get<RouterTable>().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++;
|
||||
}
|
||||
|
||||
@@ -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<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];
|
||||
uint8_t mAllocatedRouterIds[BitVectorBytes(Mle::kMaxRouterId)];
|
||||
|
||||
Reference in New Issue
Block a user