[network-data] enhance OnMeshPrefix/ExternalRoute/Service configs (#5315)

This commit updates `OnMeshPrefixConfig` and `ExternalRouteConfig` to
be `Equatable`. It also adds a `ServerConfig` and adds overloads of
operator `==` to `Service/Server` configs. It also adds new method
`SetFrom()` to all configuration types to set them from a given set of
related TLVs.
This commit is contained in:
Abtin Keshavarzian
2020-08-01 10:35:47 -07:00
committed by GitHub
parent 114a0e7dc7
commit cca72a660f
2 changed files with 176 additions and 51 deletions
+72 -45
View File
@@ -47,6 +47,72 @@
namespace ot {
namespace NetworkData {
void OnMeshPrefixConfig::SetFrom(const PrefixTlv & aPrefixTlv,
const BorderRouterTlv & aBorderRouterTlv,
const BorderRouterEntry &aBorderRouterEntry)
{
Clear();
aPrefixTlv.CopyPrefixTo(GetPrefix());
mPreference = aBorderRouterEntry.GetPreference();
mPreferred = aBorderRouterEntry.IsPreferred();
mSlaac = aBorderRouterEntry.IsSlaac();
mDhcp = aBorderRouterEntry.IsDhcp();
mConfigure = aBorderRouterEntry.IsConfigure();
mDefaultRoute = aBorderRouterEntry.IsDefaultRoute();
mOnMesh = aBorderRouterEntry.IsOnMesh();
mStable = aBorderRouterTlv.IsStable();
mRloc16 = aBorderRouterEntry.GetRloc();
mNdDns = aBorderRouterEntry.IsNdDns();
mDp = aBorderRouterEntry.IsDp();
}
void ExternalRouteConfig::SetFrom(Instance & aInstance,
const PrefixTlv & aPrefixTlv,
const HasRouteTlv & aHasRouteTlv,
const HasRouteEntry &aHasRouteEntry)
{
Clear();
aPrefixTlv.CopyPrefixTo(GetPrefix());
mPreference = aHasRouteEntry.GetPreference();
mStable = aHasRouteTlv.IsStable();
mRloc16 = aHasRouteEntry.GetRloc();
mNextHopIsThisDevice = (aHasRouteEntry.GetRloc() == aInstance.Get<Mle::MleRouter>().GetRloc16());
}
bool ServiceConfig::ServerConfig::operator==(const ServerConfig &aOther) const
{
return (mStable == aOther.mStable) && (mServerDataLength == aOther.mServerDataLength) &&
(memcmp(mServerData, aOther.mServerData, mServerDataLength) == 0);
}
void ServiceConfig::ServerConfig::SetFrom(const ServerTlv &aServerTlv)
{
mStable = aServerTlv.IsStable();
mRloc16 = aServerTlv.GetServer16();
mServerDataLength = aServerTlv.GetServerDataLength();
memcpy(&mServerData, aServerTlv.GetServerData(), mServerDataLength);
}
bool ServiceConfig::operator==(const ServiceConfig &aOther) const
{
return (mEnterpriseNumber == aOther.mEnterpriseNumber) && (mServiceDataLength == aOther.mServiceDataLength) &&
(memcmp(mServiceData, aOther.mServiceData, mServiceDataLength) == 0) &&
(GetServerConfig() == aOther.GetServerConfig());
}
void ServiceConfig::SetFrom(const ServiceTlv &aServiceTlv, const ServerTlv &aServerTlv)
{
Clear();
mServiceId = aServiceTlv.GetServiceId();
mEnterpriseNumber = aServiceTlv.GetEnterpriseNumber();
mServiceDataLength = aServiceTlv.GetServiceDataLength();
memcpy(&mServiceData, aServiceTlv.GetServiceData(), mServiceDataLength);
GetServerConfig().SetFrom(aServerTlv);
}
NetworkData::NetworkData(Instance &aInstance, Type aType)
: InstanceLocator(aInstance)
, mType(aType)
@@ -239,20 +305,7 @@ otError NetworkData::Iterate(Iterator &aIterator, uint16_t aRloc16, Config &aCon
aConfig.mExternalRoute = nullptr;
aConfig.mService = nullptr;
aConfig.mOnMeshPrefix->Clear();
prefixTlv->CopyPrefixTo(aConfig.mOnMeshPrefix->GetPrefix());
aConfig.mOnMeshPrefix->mPreference = borderRouterEntry->GetPreference();
aConfig.mOnMeshPrefix->mPreferred = borderRouterEntry->IsPreferred();
aConfig.mOnMeshPrefix->mSlaac = borderRouterEntry->IsSlaac();
aConfig.mOnMeshPrefix->mDhcp = borderRouterEntry->IsDhcp();
aConfig.mOnMeshPrefix->mConfigure = borderRouterEntry->IsConfigure();
aConfig.mOnMeshPrefix->mDefaultRoute = borderRouterEntry->IsDefaultRoute();
aConfig.mOnMeshPrefix->mOnMesh = borderRouterEntry->IsOnMesh();
aConfig.mOnMeshPrefix->mStable = borderRouter->IsStable();
aConfig.mOnMeshPrefix->mRloc16 = borderRouterEntry->GetRloc();
aConfig.mOnMeshPrefix->mNdDns = borderRouterEntry->IsNdDns();
aConfig.mOnMeshPrefix->mDp = borderRouterEntry->IsDp();
aConfig.mOnMeshPrefix->SetFrom(*prefixTlv, *borderRouter, *borderRouterEntry);
ExitNow(error = OT_ERROR_NONE);
}
@@ -278,14 +331,7 @@ otError NetworkData::Iterate(Iterator &aIterator, uint16_t aRloc16, Config &aCon
aConfig.mOnMeshPrefix = nullptr;
aConfig.mService = nullptr;
aConfig.mExternalRoute->Clear();
prefixTlv->CopyPrefixTo(aConfig.mExternalRoute->GetPrefix());
aConfig.mExternalRoute->mPreference = hasRouteEntry->GetPreference();
aConfig.mExternalRoute->mStable = hasRoute->IsStable();
aConfig.mExternalRoute->mRloc16 = hasRouteEntry->GetRloc();
aConfig.mExternalRoute->mNextHopIsThisDevice =
(hasRouteEntry->GetRloc() == Get<Mle::MleRouter>().GetRloc16());
aConfig.mExternalRoute->SetFrom(GetInstance(), *prefixTlv, *hasRoute, *hasRouteEntry);
ExitNow(error = OT_ERROR_NONE);
}
@@ -315,20 +361,7 @@ otError NetworkData::Iterate(Iterator &aIterator, uint16_t aRloc16, Config &aCon
{
aConfig.mOnMeshPrefix = nullptr;
aConfig.mExternalRoute = nullptr;
aConfig.mService->Clear();
aConfig.mService->mServiceId = service->GetServiceId();
aConfig.mService->mEnterpriseNumber = service->GetEnterpriseNumber();
aConfig.mService->mServiceDataLength = service->GetServiceDataLength();
memcpy(&aConfig.mService->mServiceData, service->GetServiceData(),
service->GetServiceDataLength());
aConfig.mService->mServerConfig.mStable = server->IsStable();
aConfig.mService->mServerConfig.mServerDataLength = server->GetServerDataLength();
memcpy(&aConfig.mService->mServerConfig.mServerData, server->GetServerData(),
server->GetServerDataLength());
aConfig.mService->mServerConfig.mRloc16 = server->GetServer16();
aConfig.mService->SetFrom(*service, *server);
iterator.MarkEntryAsNotNew();
@@ -369,7 +402,7 @@ bool NetworkData::ContainsOnMeshPrefixes(const NetworkData &aCompare, uint16_t a
while ((error = GetNextOnMeshPrefix(innerIterator, aRloc16, innerConfig)) == OT_ERROR_NONE)
{
if (memcmp(&outerConfig, &innerConfig, sizeof(outerConfig)) == 0)
if (outerConfig == innerConfig)
{
break;
}
@@ -399,7 +432,7 @@ bool NetworkData::ContainsExternalRoutes(const NetworkData &aCompare, uint16_t a
while ((error = GetNextExternalRoute(innerIterator, aRloc16, innerConfig)) == OT_ERROR_NONE)
{
if (memcmp(&outerConfig, &innerConfig, sizeof(outerConfig)) == 0)
if (outerConfig == innerConfig)
{
break;
}
@@ -429,13 +462,7 @@ bool NetworkData::ContainsServices(const NetworkData &aCompare, uint16_t aRloc16
while ((error = GetNextService(innerIterator, aRloc16, innerConfig)) == OT_ERROR_NONE)
{
if ((outerConfig.mEnterpriseNumber == innerConfig.mEnterpriseNumber) &&
(outerConfig.mServiceDataLength == innerConfig.mServiceDataLength) &&
(memcmp(outerConfig.mServiceData, innerConfig.mServiceData, outerConfig.mServiceDataLength) == 0) &&
(outerConfig.mServerConfig.mStable == innerConfig.mServerConfig.mStable) &&
(outerConfig.mServerConfig.mServerDataLength == innerConfig.mServerConfig.mServerDataLength) &&
(memcmp(outerConfig.mServerConfig.mServerData, innerConfig.mServerConfig.mServerData,
outerConfig.mServerConfig.mServerDataLength) == 0))
if (outerConfig == innerConfig)
{
break;
}
+104 -6
View File
@@ -41,6 +41,7 @@
#include "coap/coap.hpp"
#include "common/clearable.hpp"
#include "common/equatable.hpp"
#include "common/locator.hpp"
#include "common/timer.hpp"
#include "net/udp6.hpp"
@@ -100,11 +101,15 @@ typedef otNetworkDataIterator Iterator;
* This class represents an On Mesh Prefix (Border Router) configuration.
*
*/
class OnMeshPrefixConfig : public otBorderRouterConfig, public Clearable<OnMeshPrefixConfig>
class OnMeshPrefixConfig : public otBorderRouterConfig,
public Clearable<OnMeshPrefixConfig>,
public Equatable<OnMeshPrefixConfig>
{
friend class NetworkData;
public:
/**
* This method get the prefix.
* This method gets the prefix.
*
* @return The prefix.
*
@@ -112,23 +117,32 @@ public:
const Ip6::Prefix &GetPrefix(void) const { return static_cast<const Ip6::Prefix &>(mPrefix); }
/**
* This method get the prefix.
* This method gets the prefix.
*
* @return The prefix.
*
*/
Ip6::Prefix &GetPrefix(void) { return static_cast<Ip6::Prefix &>(mPrefix); }
private:
void SetFrom(const PrefixTlv & aPrefixTlv,
const BorderRouterTlv & aBorderRouterTlv,
const BorderRouterEntry &aBorderRouterEntry);
};
/**
* This class represents an External Route configuration.
*
*/
class ExternalRouteConfig : public otExternalRouteConfig, public Clearable<ExternalRouteConfig>
class ExternalRouteConfig : public otExternalRouteConfig,
public Clearable<ExternalRouteConfig>,
public Equatable<ExternalRouteConfig>
{
friend class NetworkData;
public:
/**
* This method get the prefix.
* This method gets the prefix.
*
* @return The prefix.
*
@@ -136,12 +150,18 @@ public:
const Ip6::Prefix &GetPrefix(void) const { return static_cast<const Ip6::Prefix &>(mPrefix); }
/**
* This method get the prefix.
* This method gets the prefix.
*
* @return The prefix.
*
*/
Ip6::Prefix &GetPrefix(void) { return static_cast<Ip6::Prefix &>(mPrefix); }
private:
void SetFrom(Instance & aInstance,
const PrefixTlv & aPrefixTlv,
const HasRouteTlv & aHasRouteTlv,
const HasRouteEntry &aEntry);
};
/**
@@ -150,6 +170,84 @@ public:
*/
class ServiceConfig : public otServiceConfig, public Clearable<ServiceConfig>
{
friend class NetworkData;
public:
/**
* This class represents a Server configuration.
*
*/
class ServerConfig : public otServerConfig
{
friend class ServiceConfig;
public:
/**
* This method overloads operator `==` to evaluate whether or not two `ServerConfig` instances are equal.
*
* @param[in] aOther The other `ServerConfig` instance to compare with.
*
* @retval TRUE If the two `ServerConfig` instances are equal.
* @retval FALSE If the two `ServerConfig` instances are not equal.
*
*/
bool operator==(const ServerConfig &aOther) const;
/**
* This method overloads operator `!=` to evaluate whether or not two `ServerConfig` instances are unequal.
*
* @param[in] aOther The other `ServerConfig` instance to compare with.
*
* @retval TRUE If the two `ServerConfig` instances are unequal.
* @retval FALSE If the two `ServerConfig` instances are not unequal.
*
*/
bool operator!=(const ServerConfig &aOther) const { return !(*this == aOther); }
private:
void SetFrom(const ServerTlv &aServerTlv);
};
/**
* This method gets the Server configuration.
*
* @returns The Server configuration.
*
*/
const ServerConfig &GetServerConfig(void) const { return static_cast<const ServerConfig &>(mServerConfig); }
/**
* This method gets the Server configuration.
*
* @returns The Server configuration.
*
*/
ServerConfig &GetServerConfig(void) { return static_cast<ServerConfig &>(mServerConfig); }
/**
* This method overloads operator `==` to evaluate whether or not two `ServiceConfig` instances are equal.
*
* @param[in] aOther The other `ServiceConfig` instance to compare with.
*
* @retval TRUE If the two `ServiceConfig` instances are equal.
* @retval FALSE If the two `ServiceConfig` instances are not equal.
*
*/
bool operator==(const ServiceConfig &aOther) const;
/**
* This method overloads operator `!=` to evaluate whether or not two `ServiceConfig` instances are unequal.
*
* @param[in] aOther The other `ServiceConfig` instance to compare with.
*
* @retval TRUE If the two `ServiceConfig` instances are unequal.
* @retval FALSE If the two `ServiceConfig` instances are not unequal.
*
*/
bool operator!=(const ServiceConfig &aOther) const { return !(*this == aOther); }
private:
void SetFrom(const ServiceTlv &aServiceTlv, const ServerTlv &aServerTlv);
};
/**