diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 537fc6989..835bf38fb 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -304,8 +304,9 @@ Error NetworkDiagnostic::FillRequestedTlvs(const Message & aRequest, case NetworkDiagnosticTlv::kConnectivity: { ConnectivityTlv tlv; + tlv.Init(); - Get().FillConnectivityTlv(reinterpret_cast(tlv)); + Get().FillConnectivityTlv(tlv); SuccessOrExit(error = tlv.AppendTo(aResponse)); break; } @@ -313,8 +314,9 @@ Error NetworkDiagnostic::FillRequestedTlvs(const Message & aRequest, case NetworkDiagnosticTlv::kRoute: { RouteTlv tlv; + tlv.Init(); - Get().FillRouteTlv(reinterpret_cast(tlv)); + Get().FillRouteTlv(tlv); SuccessOrExit(error = tlv.AppendTo(aResponse)); break; } @@ -322,16 +324,10 @@ Error NetworkDiagnostic::FillRequestedTlvs(const Message & aRequest, case NetworkDiagnosticTlv::kLeaderData: { - LeaderDataTlv tlv; - const Mle::LeaderData &leaderData = Get().GetLeaderData(); + LeaderDataTlv tlv; tlv.Init(); - tlv.SetPartitionId(leaderData.GetPartitionId()); - tlv.SetWeighting(leaderData.GetWeighting()); - tlv.SetDataVersion(leaderData.GetDataVersion(NetworkData::kFullSet)); - tlv.SetStableDataVersion(leaderData.GetDataVersion(NetworkData::kStableSubset)); - tlv.SetLeaderRouterId(leaderData.GetLeaderRouterId()); - + tlv.Set(Get().GetLeaderData()); SuccessOrExit(error = tlv.AppendTo(aResponse)); break; } @@ -592,27 +588,6 @@ exit: return; } -static inline void ParseMode(const Mle::DeviceMode &aMode, otLinkModeConfig &aLinkModeConfig) -{ - aLinkModeConfig.mRxOnWhenIdle = aMode.IsRxOnWhenIdle(); - aLinkModeConfig.mDeviceType = aMode.IsFullThreadDevice(); - aLinkModeConfig.mNetworkData = (aMode.GetNetworkDataType() == NetworkData::kFullSet); -} - -static inline void ParseConnectivity(const ConnectivityTlv & aConnectivityTlv, - otNetworkDiagConnectivity &aNetworkDiagConnectivity) -{ - aNetworkDiagConnectivity.mParentPriority = aConnectivityTlv.GetParentPriority(); - aNetworkDiagConnectivity.mLinkQuality3 = aConnectivityTlv.GetLinkQuality3(); - aNetworkDiagConnectivity.mLinkQuality2 = aConnectivityTlv.GetLinkQuality2(); - aNetworkDiagConnectivity.mLinkQuality1 = aConnectivityTlv.GetLinkQuality1(); - aNetworkDiagConnectivity.mLeaderCost = aConnectivityTlv.GetLeaderCost(); - aNetworkDiagConnectivity.mIdSequence = aConnectivityTlv.GetIdSequence(); - aNetworkDiagConnectivity.mActiveRouters = aConnectivityTlv.GetActiveRouters(); - aNetworkDiagConnectivity.mSedBufferSize = aConnectivityTlv.GetSedBufferSize(); - aNetworkDiagConnectivity.mSedDatagramCount = aConnectivityTlv.GetSedDatagramCount(); -} - static void ParseRoute(const RouteTlv &aRouteTlv, otNetworkDiagRoute &aNetworkDiagRoute) { uint8_t routeCount = 0; @@ -633,15 +608,6 @@ static void ParseRoute(const RouteTlv &aRouteTlv, otNetworkDiagRoute &aNetworkDi aNetworkDiagRoute.mIdSequence = aRouteTlv.GetRouterIdSequence(); } -static inline void ParseLeaderData(const LeaderDataTlv &aLeaderDataTlv, otLeaderData &aLeaderData) -{ - aLeaderData.mPartitionId = aLeaderDataTlv.GetPartitionId(); - aLeaderData.mWeighting = aLeaderDataTlv.GetWeighting(); - aLeaderData.mDataVersion = aLeaderDataTlv.GetDataVersion(); - aLeaderData.mStableDataVersion = aLeaderDataTlv.GetStableDataVersion(); - aLeaderData.mLeaderRouterId = aLeaderDataTlv.GetLeaderRouterId(); -} - static inline void ParseMacCounters(const MacCountersTlv &aMacCountersTlv, otNetworkDiagMacCounters &aMacCounters) { aMacCounters.mIfInUnknownProtos = aMacCountersTlv.GetIfInUnknownProtos(); @@ -659,7 +625,7 @@ static inline void ParseChildEntry(const ChildTableEntry &aChildTableTlvEntry, o { aChildEntry.mTimeout = aChildTableTlvEntry.GetTimeout(); aChildEntry.mChildId = aChildTableTlvEntry.GetChildId(); - ParseMode(aChildTableTlvEntry.GetMode(), aChildEntry.mMode); + aChildTableTlvEntry.GetMode().Get(aChildEntry.mMode); } Error NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, @@ -692,7 +658,7 @@ Error NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, uint8_t mode; SuccessOrExit(error = Tlv::Read(aMessage, offset, mode)); - ParseMode(Mle::DeviceMode(mode), aNetworkDiagTlv.mData.mMode); + Mle::DeviceMode(mode).Get(aNetworkDiagTlv.mData.mMode); break; } @@ -706,8 +672,7 @@ Error NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, SuccessOrExit(error = aMessage.Read(offset, connectivity)); VerifyOrExit(connectivity.IsValid(), error = kErrorParse); - - ParseConnectivity(connectivity, aNetworkDiagTlv.mData.mConnectivity); + connectivity.GetConnectivity(aNetworkDiagTlv.mData.mConnectivity); break; } @@ -730,8 +695,7 @@ Error NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, SuccessOrExit(error = aMessage.Read(offset, leaderData)); VerifyOrExit(leaderData.IsValid(), error = kErrorParse); - - ParseLeaderData(leaderData, aNetworkDiagTlv.mData.mLeaderData); + leaderData.Get(AsCoreType(&aNetworkDiagTlv.mData.mLeaderData)); break; } diff --git a/src/core/thread/network_diagnostic_tlvs.hpp b/src/core/thread/network_diagnostic_tlvs.hpp index 83526342e..c1a97ccb9 100644 --- a/src/core/thread/network_diagnostic_tlvs.hpp +++ b/src/core/thread/network_diagnostic_tlvs.hpp @@ -43,27 +43,17 @@ #include "common/tlvs.hpp" #include "net/ip6_address.hpp" #include "radio/radio.hpp" +#include "thread/mle_tlvs.hpp" #include "thread/mle_types.hpp" namespace ot { - namespace NetworkDiagnostic { using ot::Encoding::BigEndian::HostSwap16; using ot::Encoding::BigEndian::HostSwap32; /** - * @addtogroup core-mle-tlvs - * - * @brief - * This module includes definitions for generating and processing MLE TLVs. - * - * @{ - * - */ - -/** - * This class implements MLE TLV generation and parsing. + * This class implements Network Diagnostic TLV generation and parsing. * */ OT_TOOL_PACKED_BEGIN @@ -71,7 +61,7 @@ class NetworkDiagnosticTlv : public ot::Tlv { public: /** - * MLE TLV Types. + * Network Diagnostic TLV Types. * */ enum Type : uint8_t @@ -154,224 +144,47 @@ typedef UintTlvInfo SupplyVoltag */ typedef UintTlvInfo MaxChildTimeoutTlv; +typedef otNetworkDiagConnectivity Connectivity; ///< Network Diagnostic Connectivity value. + /** * This class implements Connectivity TLV generation and parsing. * */ OT_TOOL_PACKED_BEGIN -class ConnectivityTlv : public NetworkDiagnosticTlv, public TlvInfo +class ConnectivityTlv : public Mle::ConnectivityTlv { public: + static constexpr uint8_t kType = NetworkDiagnosticTlv::kConnectivity; ///< The TLV Type value. + /** * This method initializes the TLV. * */ void Init(void) { - SetType(kConnectivity); - SetLength(sizeof(*this) - sizeof(NetworkDiagnosticTlv)); + Mle::ConnectivityTlv::Init(); + ot::Tlv::SetType(kType); } /** - * This method indicates whether or not the TLV appears to be well-formed. + * This method retrieves the `Connectivity` value. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @param[out] aConnectivity A reference to `Connectivity` to populate. * */ - bool IsValid(void) const + void GetConnectivity(Connectivity &aConnectivity) const { - return IsSedBufferingIncluded() || (GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv) - - sizeof(mSedBufferSize) - sizeof(mSedDatagramCount)); + aConnectivity.mParentPriority = GetParentPriority(); + aConnectivity.mLinkQuality3 = GetLinkQuality3(); + aConnectivity.mLinkQuality2 = GetLinkQuality2(); + aConnectivity.mLinkQuality1 = GetLinkQuality1(); + aConnectivity.mLeaderCost = GetLeaderCost(); + aConnectivity.mIdSequence = GetIdSequence(); + aConnectivity.mActiveRouters = GetActiveRouters(); + aConnectivity.mSedBufferSize = GetSedBufferSize(); + aConnectivity.mSedDatagramCount = GetSedDatagramCount(); } - /** - * This method indicates whether or not the sed buffer size and datagram count are included. - * - * @retval TRUE If the sed buffer size and datagram count are included. - * @retval FALSE If the sed buffer size and datagram count are not included. - * - */ - bool IsSedBufferingIncluded(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * This method returns the Parent Priority value. - * - * @returns The Parent Priority value. - * - */ - int8_t GetParentPriority(void) const { return (mParentPriority & kParentPriorityMask) >> kParentPriorityOffset; } - - /** - * This method sets the Parent Priority value. - * - * @param[in] aParentPriority The Parent Priority value. - * - */ - void SetParentPriority(int8_t aParentPriority) - { - mParentPriority = (aParentPriority << kParentPriorityOffset) & kParentPriorityMask; - } - - /** - * This method returns the Link Quality 3 value. - * - * @returns The Link Quality 3 value. - * - */ - uint8_t GetLinkQuality3(void) const { return mLinkQuality3; } - - /** - * This method sets the Link Quality 3 value. - * - * @param[in] aLinkQuality The Link Quality 3 value. - * - */ - void SetLinkQuality3(uint8_t aLinkQuality) { mLinkQuality3 = aLinkQuality; } - - /** - * This method returns the Link Quality 2 value. - * - * @returns The Link Quality 2 value. - * - */ - uint8_t GetLinkQuality2(void) const { return mLinkQuality2; } - - /** - * This method sets the Link Quality 2 value. - * - * @param[in] aLinkQuality The Link Quality 2 value. - * - */ - void SetLinkQuality2(uint8_t aLinkQuality) { mLinkQuality2 = aLinkQuality; } - - /** - * This method sets the Link Quality 1 value. - * - * @returns The Link Quality 1 value. - * - */ - uint8_t GetLinkQuality1(void) const { return mLinkQuality1; } - - /** - * This method sets the Link Quality 1 value. - * - * @param[in] aLinkQuality The Link Quality 1 value. - * - */ - void SetLinkQuality1(uint8_t aLinkQuality) { mLinkQuality1 = aLinkQuality; } - - /** - * This method sets the Active Routers value. - * - * @returns The Active Routers value. - * - */ - uint8_t GetActiveRouters(void) const { return mActiveRouters; } - - /** - * This method sets the Active Routers value. - * - * @param[in] aActiveRouters The Active Routers value. - * - */ - void SetActiveRouters(uint8_t aActiveRouters) { mActiveRouters = aActiveRouters; } - - /** - * This method returns the Leader Cost value. - * - * @returns The Leader Cost value. - * - */ - uint8_t GetLeaderCost(void) const { return mLeaderCost; } - - /** - * This method sets the Leader Cost value. - * - * @param[in] aCost The Leader Cost value. - * - */ - void SetLeaderCost(uint8_t aCost) { mLeaderCost = aCost; } - - /** - * This method returns the ID Sequence value. - * - * @returns The ID Sequence value. - * - */ - uint8_t GetIdSequence(void) const { return mIdSequence; } - - /** - * This method sets the ID Sequence value. - * - * @param[in] aSequence The ID Sequence value. - * - */ - void SetIdSequence(uint8_t aSequence) { mIdSequence = aSequence; } - - /** - * This method returns the SED Buffer Size value. - * - * @returns The SED Buffer Size value. - * - */ - uint16_t GetSedBufferSize(void) const - { - uint16_t buffersize = OPENTHREAD_CONFIG_DEFAULT_SED_BUFFER_SIZE; - - if (IsSedBufferingIncluded()) - { - buffersize = HostSwap16(mSedBufferSize); - } - return buffersize; - } - - /** - * This method sets the SED Buffer Size value. - * - * @param[in] aSedBufferSize The SED Buffer Size value. - * - */ - void SetSedBufferSize(uint16_t aSedBufferSize) { mSedBufferSize = HostSwap16(aSedBufferSize); } - - /** - * This method returns the SED Datagram Count value. - * - * @returns The SED Datagram Count value. - * - */ - uint8_t GetSedDatagramCount(void) const - { - uint8_t count = OPENTHREAD_CONFIG_DEFAULT_SED_DATAGRAM_COUNT; - - if (IsSedBufferingIncluded()) - { - count = mSedDatagramCount; - } - return count; - } - - /** - * This method sets the SED Datagram Count value. - * - * @param[in] aSedDatagramCount The SED Datagram Count value. - * - */ - void SetSedDatagramCount(uint8_t aSedDatagramCount) { mSedDatagramCount = aSedDatagramCount; } - -private: - static constexpr uint8_t kParentPriorityOffset = 6; - static constexpr uint8_t kParentPriorityMask = 3 << kParentPriorityOffset; - - uint8_t mParentPriority; - uint8_t mLinkQuality3; - uint8_t mLinkQuality2; - uint8_t mLinkQuality1; - uint8_t mLeaderCost; - uint8_t mIdSequence; - uint8_t mActiveRouters; - uint16_t mSedBufferSize; - uint8_t mSedDatagramCount; } OT_TOOL_PACKED_END; /** @@ -379,165 +192,20 @@ private: * */ OT_TOOL_PACKED_BEGIN -class RouteTlv : public NetworkDiagnosticTlv, public TlvInfo +class RouteTlv : public Mle::RouteTlv { public: + static constexpr uint8_t kType = NetworkDiagnosticTlv::kRoute; ///< The TLV Type value. + /** * This method initializes the TLV. * */ void Init(void) { - SetType(kRoute); - SetLength(sizeof(*this) - sizeof(NetworkDiagnosticTlv)); - mRouterIdMask.Clear(); + Mle::RouteTlv::Init(); + ot::Tlv::SetType(kType); } - - /** - * This method indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(mRouterIdSequence) + sizeof(mRouterIdMask); } - - /** - * This method returns the Router ID Sequence value. - * - * @returns The Router ID Sequence value. - * - */ - uint8_t GetRouterIdSequence(void) const { return mRouterIdSequence; } - - /** - * This method sets the Router ID Sequence value. - * - * @param[in] aSequence The Router ID Sequence value. - * - */ - void SetRouterIdSequence(uint8_t aSequence) { mRouterIdSequence = aSequence; } - - /** - * This method indicates whether or not a Router ID bit is set. - * - * @param[in] aRouterId The Router ID. - * - * @retval TRUE If the Router ID bit is set. - * @retval FALSE If the Router ID bit is not set. - * - */ - bool IsRouterIdSet(uint8_t aRouterId) const { return mRouterIdMask.Contains(aRouterId); } - - /** - * This method sets the Router ID bit. - * - * @param[in] aRouterId The Router ID bit to set. - * - */ - void SetRouterId(uint8_t aRouterId) { mRouterIdMask.Add(aRouterId); } - - /** - * This method returns the Route Data Length value. - * - * @returns The Route Data Length value. - * - */ - uint8_t GetRouteDataLength(void) const { return GetLength() - sizeof(mRouterIdSequence) - sizeof(mRouterIdMask); } - - /** - * This method sets the Route Data Length value. - * - * @param[in] aLength The Route Data Length value. - * - */ - void SetRouteDataLength(uint8_t aLength) { SetLength(sizeof(mRouterIdSequence) + sizeof(mRouterIdMask) + aLength); } - - /** - * This method returns the Route Cost value for a given Router index. - * - * @param[in] aRouterIndex The Router index. - * - * @returns The Route Cost value for a given Router index. - * - */ - uint8_t GetRouteCost(uint8_t aRouterIndex) const { return mRouteData[aRouterIndex] & kRouteCostMask; } - - /** - * This method sets the Route Cost value for a given Router index. - * - * @param[in] aRouterIndex The Router index. - * @param[in] aRouteCost The Route Cost value. - * - */ - void SetRouteCost(uint8_t aRouterIndex, uint8_t aRouteCost) - { - mRouteData[aRouterIndex] = (mRouteData[aRouterIndex] & ~kRouteCostMask) | aRouteCost; - } - - /** - * This method returns the Link Quality In value for a given Router index. - * - * @param[in] aRouterIndex The Router index. - * - * @returns The Link Quality In value for a given Router index. - * - */ - uint8_t GetLinkQualityIn(uint8_t aRouterIndex) const - { - return (mRouteData[aRouterIndex] & kLinkQualityInMask) >> kLinkQualityInOffset; - } - - /** - * This method sets the Link Quality In value for a given Router index. - * - * @param[in] aRouterIndex The Router index. - * @param[in] aLinkQuality The Link Quality In value for a given Router index. - * - */ - void SetLinkQualityIn(uint8_t aRouterIndex, uint8_t aLinkQuality) - { - mRouteData[aRouterIndex] = (mRouteData[aRouterIndex] & ~kLinkQualityInMask) | - ((aLinkQuality << kLinkQualityInOffset) & kLinkQualityInMask); - } - - /** - * This method returns the Link Quality Out value for a given Router index. - * - * @param[in] aRouterIndex The Router index. - * - * @returns The Link Quality Out value for a given Router index. - * - */ - uint8_t GetLinkQualityOut(uint8_t aRouterIndex) const - { - return (mRouteData[aRouterIndex] & kLinkQualityOutMask) >> kLinkQualityOutOffset; - } - - /** - * This method sets the Link Quality Out value for a given Router index. - * - * @param[in] aRouterIndex The Router index. - * @param[in] aLinkQuality The Link Quality Out value for a given Router index. - * - */ - void SetLinkQualityOut(uint8_t aRouterIndex, uint8_t aLinkQuality) - { - mRouteData[aRouterIndex] = (mRouteData[aRouterIndex] & ~kLinkQualityOutMask) | - ((aLinkQuality << kLinkQualityOutOffset) & kLinkQualityOutMask); - } - -private: - static constexpr uint8_t kLinkQualityOutOffset = 6; - static constexpr uint8_t kLinkQualityOutMask = 3 << kLinkQualityOutOffset; - static constexpr uint8_t kLinkQualityInOffset = 4; - static constexpr uint8_t kLinkQualityInMask = 3 << kLinkQualityInOffset; - static constexpr uint8_t kRouteCostOffset = 0; - static constexpr uint8_t kRouteCostMask = 0xf << kRouteCostOffset; - - uint8_t mRouterIdSequence; - Mle::RouterIdSet mRouterIdMask; - uint8_t mRouteData[Mle::kMaxRouterId + 1]; } OT_TOOL_PACKED_END; /** @@ -545,114 +213,20 @@ private: * */ OT_TOOL_PACKED_BEGIN -class LeaderDataTlv : public NetworkDiagnosticTlv, public TlvInfo +class LeaderDataTlv : public Mle::LeaderDataTlv { public: + static constexpr uint8_t kType = NetworkDiagnosticTlv::kLeaderData; ///< The TLV Type value. + /** * This method initializes the TLV. * */ void Init(void) { - SetType(kLeaderData); - SetLength(sizeof(*this) - sizeof(NetworkDiagnosticTlv)); + Mle::LeaderDataTlv::Init(); + ot::Tlv::SetType(kType); } - - /** - * This method indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(NetworkDiagnosticTlv); } - - /** - * This method returns the Partition ID value. - * - * @returns The Partition ID value. - * - */ - uint32_t GetPartitionId(void) const { return HostSwap32(mPartitionId); } - - /** - * This method sets the Partition ID value. - * - * @param[in] aPartitionId The Partition ID value. - * - */ - void SetPartitionId(uint32_t aPartitionId) { mPartitionId = HostSwap32(aPartitionId); } - - /** - * This method returns the Weighting value. - * - * @returns The Weighting value. - * - */ - uint8_t GetWeighting(void) const { return mWeighting; } - - /** - * This method sets the Weighting value. - * - * @param[in] aWeighting The Weighting value. - * - */ - void SetWeighting(uint8_t aWeighting) { mWeighting = aWeighting; } - - /** - * This method returns the Data Version value. - * - * @returns The Data Version value. - * - */ - uint8_t GetDataVersion(void) const { return mDataVersion; } - - /** - * This method sets the Data Version value. - * - * @param[in] aVersion The Data Version value. - * - */ - void SetDataVersion(uint8_t aVersion) { mDataVersion = aVersion; } - - /** - * This method returns the Stable Data Version value. - * - * @returns The Stable Data Version value. - * - */ - uint8_t GetStableDataVersion(void) const { return mStableDataVersion; } - - /** - * This method sets the Stable Data Version value. - * - * @param[in] aVersion The Stable Data Version value. - * - */ - void SetStableDataVersion(uint8_t aVersion) { mStableDataVersion = aVersion; } - - /** - * This method returns the Leader Router ID value. - * - * @returns The Leader Router ID value. - * - */ - uint8_t GetLeaderRouterId(void) const { return mLeaderRouterId; } - - /** - * This method sets the Leader Router ID value. - * - * @param[in] aRouterId The Leader Router ID value. - * - */ - void SetLeaderRouterId(uint8_t aRouterId) { mLeaderRouterId = aRouterId; } - -private: - uint32_t mPartitionId; - uint8_t mWeighting; - uint8_t mDataVersion; - uint8_t mStableDataVersion; - uint8_t mLeaderRouterId; } OT_TOOL_PACKED_END; /** @@ -1172,13 +746,7 @@ public: } } OT_TOOL_PACKED_END; -/** - * @} - * - */ - } // namespace NetworkDiagnostic - } // namespace ot #endif // NETWORK_DIAGNOSTIC_TLVS_HPP_