From f51673676325edd4498de19554a4fd9b9bea3b39 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 3 Nov 2020 16:03:26 -0800 Subject: [PATCH] [tlvs] new simplified model to process/append TLVs (#5755) This commit adds template flavors of `Append()`, `Find()`, and `Read()` in the common base `Tlv` class. This helps simplify the processing and generating of TLVs in other modules and also adds build-time type check for the value of simple TLVs (build error when incorrect TLV Value type is used). All `Tlv` sub-classes now inherit from `TlvInfo` which provides a constant `kType` specifying the TLV Type value. Simple TLVs which contain a single value also track their TLV Value type by inheriting from either `UintTlvInfo` (when their value is an integral (unsigned int) type) or `SimpleTlvInfo` (when their value is non-integral). This commit also add `IsSame()` to `TypeTraits` to check whether two given types are the same. It also adds template flavors of `HostSwap()` in `Encoding` module. --- src/core/backbone_router/bbr_manager.cpp | 59 ++-- src/core/common/encoding.hpp | 90 +++++- src/core/common/tlvs.cpp | 82 ++--- src/core/common/tlvs.hpp | 328 +++++++++++++------- src/core/common/type_traits.hpp | 18 ++ src/core/meshcop/announce_begin_client.cpp | 8 +- src/core/meshcop/border_agent.cpp | 12 +- src/core/meshcop/commissioner.cpp | 42 ++- src/core/meshcop/dataset_manager.cpp | 3 +- src/core/meshcop/dataset_manager_ftd.cpp | 16 +- src/core/meshcop/energy_scan_client.cpp | 10 +- src/core/meshcop/joiner.cpp | 7 +- src/core/meshcop/joiner_router.cpp | 27 +- src/core/meshcop/meshcop_leader.cpp | 12 +- src/core/meshcop/meshcop_tlvs.hpp | 270 ++++++---------- src/core/meshcop/panid_query_client.cpp | 8 +- src/core/thread/address_resolver.cpp | 28 +- src/core/thread/announce_begin_server.cpp | 4 +- src/core/thread/discover_scanner.cpp | 5 +- src/core/thread/dua_manager.cpp | 20 +- src/core/thread/energy_scan_server.cpp | 6 +- src/core/thread/link_metrics.cpp | 2 +- src/core/thread/link_metrics_tlvs.hpp | 13 +- src/core/thread/mle.cpp | 94 +++--- src/core/thread/mle_router.cpp | 123 ++++---- src/core/thread/mle_tlvs.hpp | 124 +++++++- src/core/thread/mlr_manager.cpp | 8 +- src/core/thread/network_data.cpp | 2 +- src/core/thread/network_data_leader_ftd.cpp | 6 +- src/core/thread/network_diagnostic.cpp | 34 +- src/core/thread/network_diagnostic_tlvs.hpp | 60 +++- src/core/thread/panid_query_server.cpp | 4 +- src/core/thread/thread_tlvs.hpp | 60 +++- 33 files changed, 893 insertions(+), 692 deletions(-) diff --git a/src/core/backbone_router/bbr_manager.cpp b/src/core/backbone_router/bbr_manager.cpp index 3056d323f..b14155908 100644 --- a/src/core/backbone_router/bbr_manager.cpp +++ b/src/core/backbone_router/bbr_manager.cpp @@ -158,7 +158,7 @@ void Manager::HandleMulticastListenerRegistration(const Coap::Message &aMessage, // TODO: (MLR) send configured MLR response for Reference Device - if (ThreadTlv::FindUint16Tlv(aMessage, ThreadTlv::kCommissionerSessionId, commissionerSessionId) == OT_ERROR_NONE) + if (Tlv::Find(aMessage, commissionerSessionId) == OT_ERROR_NONE) { const MeshCoP::CommissionerSessionIdTlv *commissionerSessionIdTlv = static_cast( @@ -171,11 +171,11 @@ void Manager::HandleMulticastListenerRegistration(const Coap::Message &aMessage, hasCommissionerSessionIdTlv = true; } - processTimeoutTlv = hasCommissionerSessionIdTlv && - (ThreadTlv::FindUint32Tlv(aMessage, ThreadTlv::kTimeout, timeout) == OT_ERROR_NONE); + processTimeoutTlv = + hasCommissionerSessionIdTlv && (Tlv::Find(aMessage, timeout) == OT_ERROR_NONE); - VerifyOrExit(ThreadTlv::FindTlvValueOffset(aMessage, IPv6AddressesTlv::kIPv6Addresses, addressesOffset, - addressesLength) == OT_ERROR_NONE, + VerifyOrExit(Tlv::FindTlvValueOffset(aMessage, IPv6AddressesTlv::kIPv6Addresses, addressesOffset, + addressesLength) == OT_ERROR_NONE, error = OT_ERROR_PARSE); VerifyOrExit(addressesLength % sizeof(Ip6::Address) == 0, status = ThreadStatusTlv::kMlrGeneralFailure); VerifyOrExit(addressesLength / sizeof(Ip6::Address) <= kIPv6AddressesNumMax, @@ -279,7 +279,7 @@ void Manager::SendMulticastListenerRegistrationResponse(const Coap::Message & SuccessOrExit(message->SetDefaultResponseHeader(aMessage)); SuccessOrExit(message->SetPayloadMarker()); - SuccessOrExit(Tlv::AppendUint8Tlv(*message, ThreadTlv::kStatus, aStatus)); + SuccessOrExit(Tlv::Append(*message, aStatus)); if (aFailedAddressNum > 0) { @@ -324,7 +324,7 @@ void Manager::SendBackboneMulticastListenerRegistration(const Ip6::Address *aAdd SuccessOrExit(error = message->Append(addressesTlv)); SuccessOrExit(error = message->AppendBytes(aAddresses, sizeof(Ip6::Address) * aAddressNum)); - SuccessOrExit(error = ThreadTlv::AppendUint32Tlv(*message, ThreadTlv::kTimeout, aTimeout)); + SuccessOrExit(error = Tlv::Append(*message, aTimeout)); messageInfo.SetPeerAddr(Get().GetAllNetworkBackboneRoutersAddress()); messageInfo.SetPeerPort(BackboneRouter::kBackboneUdpPort); // TODO: Provide API for configuring Backbone COAP port. @@ -355,8 +355,8 @@ void Manager::HandleDuaRegistration(const Coap::Message &aMessage, const Ip6::Me VerifyOrExit(aMessageInfo.GetPeerAddr().GetIid().IsRoutingLocator(), error = OT_ERROR_DROP); VerifyOrExit(aMessage.IsConfirmablePostRequest(), error = OT_ERROR_PARSE); - SuccessOrExit(error = Tlv::FindTlv(aMessage, ThreadTlv::kTarget, &target, sizeof(target))); - SuccessOrExit(error = Tlv::FindTlv(aMessage, ThreadTlv::kMeshLocalEid, &meshLocalIid, sizeof(meshLocalIid))); + SuccessOrExit(error = Tlv::Find(aMessage, target)); + SuccessOrExit(error = Tlv::Find(aMessage, meshLocalIid)); #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE if (mDuaResponseIsSpecified && (mDuaResponseTargetMlIid.IsUnspecified() || mDuaResponseTargetMlIid == meshLocalIid)) @@ -377,8 +377,7 @@ void Manager::HandleDuaRegistration(const Coap::Message &aMessage, const Ip6::Me VerifyOrExit(isPrimary, status = ThreadStatusTlv::kDuaNotPrimary); VerifyOrExit(Get().IsDomainUnicast(target), status = ThreadStatusTlv::kDuaInvalid); - hasLastTransactionTime = - (Tlv::FindUint32Tlv(aMessage, ThreadTlv::kLastTransactionTime, lastTransactionTime) == OT_ERROR_NONE); + hasLastTransactionTime = (Tlv::Find(aMessage, lastTransactionTime) == OT_ERROR_NONE); switch (mNdProxyTable.Register(target.GetIid(), meshLocalIid, aMessageInfo.GetPeerAddr().GetIid().GetLocator(), hasLastTransactionTime ? &lastTransactionTime : nullptr)) @@ -429,8 +428,8 @@ void Manager::SendDuaRegistrationResponse(const Coap::Message & aMessage, SuccessOrExit(message->SetDefaultResponseHeader(aMessage)); SuccessOrExit(message->SetPayloadMarker()); - SuccessOrExit(Tlv::AppendUint8Tlv(*message, ThreadTlv::kStatus, aStatus)); - SuccessOrExit(Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aTarget, sizeof(aTarget))); + SuccessOrExit(Tlv::Append(*message, aStatus)); + SuccessOrExit(Tlv::Append(*message, aTarget)); SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); @@ -503,11 +502,11 @@ otError Manager::SendBackboneQuery(const Ip6::Address &aDua, uint16_t aRloc16) SuccessOrExit(error = message->InitAsNonConfirmablePost(UriPath::kBackboneQuery)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = ThreadTlv::AppendTlv(*message, ThreadTlv::kTarget, &aDua, sizeof(aDua))); + SuccessOrExit(error = Tlv::Append(*message, aDua)); if (aRloc16 != Mac::kShortAddrInvalid) { - SuccessOrExit(error = ThreadTlv::AppendUint16Tlv(*message, ThreadTlv::kRloc16, aRloc16)); + SuccessOrExit(error = Tlv::Append(*message, aRloc16)); } messageInfo.SetPeerAddr(Get().GetAllDomainBackboneRoutersAddress()); @@ -543,9 +542,9 @@ void Manager::HandleBackboneQuery(const Coap::Message &aMessage, const Ip6::Mess VerifyOrExit(Get().IsPrimary(), error = OT_ERROR_INVALID_STATE); VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), error = OT_ERROR_PARSE); - SuccessOrExit(error = ThreadTlv::FindTlv(aMessage, ThreadTlv::kTarget, &dua, sizeof(dua))); + SuccessOrExit(error = Tlv::Find(aMessage, dua)); - error = ThreadTlv::FindUint16Tlv(aMessage, ThreadTlv::kRloc16, rloc16); + error = Tlv::Find(aMessage, rloc16); VerifyOrExit(error == OT_ERROR_NONE || error == OT_ERROR_NOT_FOUND); otLogInfoBbr("Received BB.qry from %s for %s (rloc16=%04x)", aMessageInfo.GetPeerAddr().ToString().AsCString(), @@ -583,15 +582,14 @@ void Manager::HandleBackboneAnswer(const Coap::Message &aMessage, const Ip6::Mes proactive = !aMessage.IsConfirmable(); - SuccessOrExit(error = ThreadTlv::FindTlv(aMessage, ThreadTlv::kTarget, &dua, sizeof(dua))); - SuccessOrExit(error = ThreadTlv::FindTlv(aMessage, ThreadTlv::kMeshLocalEid, &meshLocalIid, sizeof(meshLocalIid))); + SuccessOrExit(error = Tlv::Find(aMessage, dua)); + SuccessOrExit(error = Tlv::Find(aMessage, meshLocalIid)); + SuccessOrExit(error = Tlv::Find(aMessage, timeSinceLastTransaction)); + SuccessOrExit(error = - ThreadTlv::FindUint32Tlv(aMessage, ThreadTlv::kLastTransactionTime, timeSinceLastTransaction)); + Tlv::FindTlvValueOffset(aMessage, ThreadTlv::kNetworkName, networkNameOffset, networkNameLength)); - SuccessOrExit( - error = ThreadTlv::FindTlvValueOffset(aMessage, ThreadTlv::kNetworkName, networkNameOffset, networkNameLength)); - - error = ThreadTlv::FindUint16Tlv(aMessage, ThreadTlv::kRloc16, srcRloc16); + error = Tlv::Find(aMessage, srcRloc16); VerifyOrExit(error == OT_ERROR_NONE || error == OT_ERROR_NOT_FOUND); if (proactive) @@ -649,24 +647,21 @@ otError Manager::SendBackboneAnswer(const Ip6::Address & aDstAddr, UriPath::kBackboneAnswer)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = ThreadTlv::AppendTlv(*message, ThreadTlv::kTarget, &aDua, sizeof(aDua))); + SuccessOrExit(error = Tlv::Append(*message, aDua)); - SuccessOrExit(error = - ThreadTlv::AppendTlv(*message, ThreadTlv::kMeshLocalEid, &aMeshLocalIid, sizeof(aMeshLocalIid))); + SuccessOrExit(error = Tlv::Append(*message, aMeshLocalIid)); - SuccessOrExit(error = - ThreadTlv::AppendUint32Tlv(*message, ThreadTlv::kLastTransactionTime, aTimeSinceLastTransaction)); + SuccessOrExit(error = Tlv::Append(*message, aTimeSinceLastTransaction)); { const Mac::NameData nameData = Get().GetNetworkName().GetAsData(); - SuccessOrExit(error = ThreadTlv::AppendTlv(*message, ThreadTlv::kNetworkName, nameData.GetBuffer(), - nameData.GetLength())); + SuccessOrExit(error = Tlv::Append(*message, nameData.GetBuffer(), nameData.GetLength())); } if (aSrcRloc16 != Mac::kShortAddrInvalid) { - SuccessOrExit(ThreadTlv::AppendUint16Tlv(*message, ThreadTlv::kRloc16, aSrcRloc16)); + SuccessOrExit(Tlv::Append(*message, aSrcRloc16)); } messageInfo.SetPeerAddr(aDstAddr); diff --git a/src/core/common/encoding.hpp b/src/core/common/encoding.hpp index 9c30e7361..b67d77999 100644 --- a/src/core/common/encoding.hpp +++ b/src/core/common/encoding.hpp @@ -122,7 +122,36 @@ inline uint64_t HostSwap64(uint64_t v) #endif // LITTLE_ENDIAN /** - * This function reads a `uint16_t` value from a given buffer assuming big-ending encoding. + * This template function performs host swap on a given unsigned integer value assuming big-endian encoding. + * + * @tparam UintType The unsigned int type. + * + * @param aValue The value to host swap. + * + * @returns The host swapped value. + * + */ +template UintType HostSwap(UintType aValue); + +template <> inline uint8_t HostSwap(uint8_t aValue) +{ + return aValue; +} +template <> inline uint16_t HostSwap(uint16_t aValue) +{ + return HostSwap16(aValue); +} +template <> inline uint32_t HostSwap(uint32_t aValue) +{ + return HostSwap32(aValue); +} +template <> inline uint64_t HostSwap(uint64_t aValue) +{ + return HostSwap64(aValue); +} + +/** + * This function reads a `uint16_t` value from a given buffer assuming big-endian encoding. * * @param[in] aBuffer Pointer to buffer to read from. * @@ -135,7 +164,7 @@ inline uint16_t ReadUint16(const uint8_t *aBuffer) } /** - * This function reads a `uint32_t` value from a given buffer assuming big-ending encoding. + * This function reads a `uint32_t` value from a given buffer assuming big-endian encoding. * * @param[in] aBuffer Pointer to buffer to read from. * @@ -149,7 +178,7 @@ inline uint32_t ReadUint32(const uint8_t *aBuffer) } /** - * This function reads a 24-bit integer value from a given buffer assuming big-ending encoding. + * This function reads a 24-bit integer value from a given buffer assuming big-endian encoding. * * @param[in] aBuffer Pointer to buffer to read from. * @@ -163,7 +192,7 @@ inline uint32_t ReadUint24(const uint8_t *aBuffer) } /** - * This function reads a `uint64_t` value from a given buffer assuming big-ending encoding. + * This function reads a `uint64_t` value from a given buffer assuming big-endian encoding. * * @param[in] aBuffer Pointer to buffer to read from. * @@ -179,7 +208,7 @@ inline uint64_t ReadUint64(const uint8_t *aBuffer) } /** - * This function writes a `uint16_t` value to a given buffer using big-ending encoding. + * This function writes a `uint16_t` value to a given buffer using big-endian encoding. * * @param[in] aValue The value to write to buffer. * @param[out] aBuffer Pointer to buffer where the value will be written. @@ -192,7 +221,7 @@ inline void WriteUint16(uint16_t aValue, uint8_t *aBuffer) } /** - * This function writes a 24-bit integer value to a given buffer using big-ending encoding. + * This function writes a 24-bit integer value to a given buffer using big-endian encoding. * * @param[in] aValue The value to write to buffer. * @param[out] aBuffer Pointer to buffer where the value will be written. @@ -206,7 +235,7 @@ inline void WriteUint24(uint32_t aValue, uint8_t *aBuffer) } /** - * This function writes a `uint32_t` value to a given buffer using big-ending encoding. + * This function writes a `uint32_t` value to a given buffer using big-endian encoding. * * @param[in] aValue The value to write to buffer. * @param[out] aBuffer Pointer to buffer where the value will be written. @@ -221,7 +250,7 @@ inline void WriteUint32(uint32_t aValue, uint8_t *aBuffer) } /** - * This function writes a `uint64_t` value to a given buffer using big-ending encoding. + * This function writes a `uint64_t` value to a given buffer using big-endian encoding. * * @param[in] aValue The value to write to buffer. * @param[out] aBuffer Pointer to buffer where the value will be written. @@ -276,7 +305,36 @@ inline uint64_t HostSwap64(uint64_t v) #endif /** - * This function reads a `uint16_t` value from a given buffer assuming little-ending encoding. + * This template function performs host swap on a given unsigned integer value assuming little-endian encoding. + * + * @tparam UintType The unsigned int type. + * + * @param aValue The value to host swap. + * + * @returns The host swapped value. + * + */ +template UintType HostSwap(UintType aValue); + +template <> inline uint8_t HostSwap(uint8_t aValue) +{ + return aValue; +} +template <> inline uint16_t HostSwap(uint16_t aValue) +{ + return HostSwap16(aValue); +} +template <> inline uint32_t HostSwap(uint32_t aValue) +{ + return HostSwap32(aValue); +} +template <> inline uint64_t HostSwap(uint64_t aValue) +{ + return HostSwap64(aValue); +} + +/** + * This function reads a `uint16_t` value from a given buffer assuming little-endian encoding. * * @param[in] aBuffer Pointer to buffer to read from. * @@ -289,7 +347,7 @@ inline uint16_t ReadUint16(const uint8_t *aBuffer) } /** - * This function reads a 24-bit integer value from a given buffer assuming little-ending encoding. + * This function reads a 24-bit integer value from a given buffer assuming little-endian encoding. * * @param[in] aBuffer Pointer to buffer to read from. * @@ -303,7 +361,7 @@ inline uint32_t ReadUint24(const uint8_t *aBuffer) } /** - * This function reads a `uint32_t` value from a given buffer assuming little-ending encoding. + * This function reads a `uint32_t` value from a given buffer assuming little-endian encoding. * * @param[in] aBuffer Pointer to buffer to read from. * @@ -317,7 +375,7 @@ inline uint32_t ReadUint32(const uint8_t *aBuffer) } /** - * This function reads a `uint64_t` value from a given buffer assuming little-ending encoding. + * This function reads a `uint64_t` value from a given buffer assuming little-endian encoding. * * @param[in] aBuffer Pointer to buffer to read from. * @@ -333,7 +391,7 @@ inline uint64_t ReadUint64(const uint8_t *aBuffer) } /** - * This function writes a `uint16_t` value to a given buffer using little-ending encoding. + * This function writes a `uint16_t` value to a given buffer using little-endian encoding. * * @param[in] aValue The value to write to buffer. * @param[out] aBuffer Pointer to buffer where the value will be written. @@ -346,7 +404,7 @@ inline void WriteUint16(uint16_t aValue, uint8_t *aBuffer) } /** - * This function writes a 24-bit integer value to a given buffer using little-ending encoding. + * This function writes a 24-bit integer value to a given buffer using little-endian encoding. * * @param[in] aValue The value to write to buffer. * @param[out] aBuffer Pointer to buffer where the value will be written. @@ -360,7 +418,7 @@ inline void WriteUint24(uint32_t aValue, uint8_t *aBuffer) } /** - * This function writes a `uint32_t` value to a given buffer using little-ending encoding. + * This function writes a `uint32_t` value to a given buffer using little-endian encoding. * * @param[in] aValue The value to write to buffer. * @param[out] aBuffer Pointer to buffer where the value will be written. @@ -375,7 +433,7 @@ inline void WriteUint32(uint32_t aValue, uint8_t *aBuffer) } /** - * This function writes a `uint64_t` value to a given buffer using little-ending encoding. + * This function writes a `uint64_t` value to a given buffer using little-endian encoding. * * @param[in] aValue The value to write to buffer. * @param[out] aBuffer Pointer to buffer where the value will be written. diff --git a/src/core/common/tlvs.cpp b/src/core/common/tlvs.cpp index 3353b42bb..f119744f0 100644 --- a/src/core/common/tlvs.cpp +++ b/src/core/common/tlvs.cpp @@ -37,9 +37,6 @@ #include "common/debug.hpp" #include "common/message.hpp" -using ot::Encoding::BigEndian::HostSwap16; -using ot::Encoding::BigEndian::HostSwap32; - namespace ot { uint32_t Tlv::GetSize(void) const @@ -171,32 +168,21 @@ exit: return error; } -otError Tlv::ReadUint8Tlv(const Message &aMessage, uint16_t aOffset, uint8_t &aValue) -{ - return ReadTlv(aMessage, aOffset, &aValue, sizeof(uint8_t)); -} - -otError Tlv::ReadUint16Tlv(const Message &aMessage, uint16_t aOffset, uint16_t &aValue) +template otError Tlv::ReadUintTlv(const Message &aMessage, uint16_t aOffset, UintType &aValue) { otError error; - SuccessOrExit(error = ReadTlv(aMessage, aOffset, &aValue, sizeof(uint16_t))); - aValue = HostSwap16(aValue); + SuccessOrExit(error = ReadTlv(aMessage, aOffset, &aValue, sizeof(aValue))); + aValue = Encoding::BigEndian::HostSwap(aValue); exit: return error; } -otError Tlv::ReadUint32Tlv(const Message &aMessage, uint16_t aOffset, uint32_t &aValue) -{ - otError error; - - SuccessOrExit(error = ReadTlv(aMessage, aOffset, &aValue, sizeof(uint32_t))); - aValue = HostSwap32(aValue); - -exit: - return error; -} +// Explicit instantiations of `ReadUintTlv<>()` +template otError Tlv::ReadUintTlv(const Message &aMessage, uint16_t aOffset, uint8_t &aValue); +template otError Tlv::ReadUintTlv(const Message &aMessage, uint16_t aOffset, uint16_t &aValue); +template otError Tlv::ReadUintTlv(const Message &aMessage, uint16_t aOffset, uint32_t &aValue); otError Tlv::ReadTlv(const Message &aMessage, uint16_t aOffset, void *aValue, uint8_t aLength) { @@ -213,41 +199,22 @@ exit: return error; } -otError Tlv::FindUint8Tlv(const Message &aMessage, uint8_t aType, uint8_t &aValue) +template otError Tlv::FindUintTlv(const Message &aMessage, uint8_t aType, UintType &aValue) { otError error = OT_ERROR_NONE; uint16_t offset; SuccessOrExit(error = FindTlvOffset(aMessage, aType, offset)); - error = ReadUint8Tlv(aMessage, offset, aValue); + error = ReadUintTlv(aMessage, offset, aValue); exit: return error; } -otError Tlv::FindUint16Tlv(const Message &aMessage, uint8_t aType, uint16_t &aValue) -{ - otError error = OT_ERROR_NONE; - uint16_t offset; - - SuccessOrExit(error = FindTlvOffset(aMessage, aType, offset)); - error = ReadUint16Tlv(aMessage, offset, aValue); - -exit: - return error; -} - -otError Tlv::FindUint32Tlv(const Message &aMessage, uint8_t aType, uint32_t &aValue) -{ - otError error = OT_ERROR_NONE; - uint16_t offset; - - SuccessOrExit(error = FindTlvOffset(aMessage, aType, offset)); - error = ReadUint32Tlv(aMessage, offset, aValue); - -exit: - return error; -} +// Explicit instantiations of `FindUintTlv<>()` +template otError Tlv::FindUintTlv(const Message &aMessage, uint8_t aType, uint8_t &aValue); +template otError Tlv::FindUintTlv(const Message &aMessage, uint8_t aType, uint16_t &aValue); +template otError Tlv::FindUintTlv(const Message &aMessage, uint8_t aType, uint32_t &aValue); otError Tlv::FindTlv(const Message &aMessage, uint8_t aType, void *aValue, uint8_t aLength) { @@ -263,26 +230,17 @@ exit: return error; } -otError Tlv::AppendUint8Tlv(Message &aMessage, uint8_t aType, uint8_t aValue) +template otError Tlv::AppendUintTlv(Message &aMessage, uint8_t aType, UintType aValue) { - uint8_t value8 = aValue; + UintType value = Encoding::BigEndian::HostSwap(aValue); - return AppendTlv(aMessage, aType, &value8, sizeof(uint8_t)); + return AppendTlv(aMessage, aType, &value, sizeof(UintType)); } -otError Tlv::AppendUint16Tlv(Message &aMessage, uint8_t aType, uint16_t aValue) -{ - uint16_t value16 = HostSwap16(aValue); - - return AppendTlv(aMessage, aType, &value16, sizeof(uint16_t)); -} - -otError Tlv::AppendUint32Tlv(Message &aMessage, uint8_t aType, uint32_t aValue) -{ - uint32_t value32 = HostSwap32(aValue); - - return AppendTlv(aMessage, aType, &value32, sizeof(uint32_t)); -} +// Explicit instantiations of `AppendUintTlv<>()` +template otError Tlv::AppendUintTlv(Message &aMessage, uint8_t aType, uint8_t aValue); +template otError Tlv::AppendUintTlv(Message &aMessage, uint8_t aType, uint16_t aValue); +template otError Tlv::AppendUintTlv(Message &aMessage, uint8_t aType, uint32_t aValue); otError Tlv::AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength) { diff --git a/src/core/common/tlvs.hpp b/src/core/common/tlvs.hpp index 8fe2cd45c..24472cfff 100644 --- a/src/core/common/tlvs.hpp +++ b/src/core/common/tlvs.hpp @@ -41,6 +41,7 @@ #include #include "common/encoding.hpp" +#include "common/type_traits.hpp" namespace ot { @@ -176,45 +177,6 @@ public: */ otError AppendTo(Message &aMessage) const; - /** - * This static method reads a TLV from a message at a given offset with TLV's value as an `uint8_t`. - * - * @param[in] aMessage The message to read from. - * @param[in] aOffset The offset into the message pointing to the start of the TLV. - * @param[out] aValue A reference to a `uint8_t` to output the TLV's value. - * - * @retval OT_ERROR_NONE Successfully read the TLV and updated @p aValue. - * @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed. - * - */ - static otError ReadUint8Tlv(const Message &aMessage, uint16_t aOffset, uint8_t &aValue); - - /** - * This static method reads a TLV from a message at a given offset with TLV's value as an `uint16_t`. - * - * @param[in] aMessage The message to read from. - * @param[in] aOffset The offset into the message pointing to the start of the TLV. - * @param[out] aValue A reference to a `uint16_t` to output the TLV's value. - * - * @retval OT_ERROR_NONE Successfully read the TLV and updated @p aValue. - * @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed. - * - */ - static otError ReadUint16Tlv(const Message &aMessage, uint16_t aOffset, uint16_t &aValue); - - /** - * This static method reads a TLV from a message at a given offset with TLV's value as an `uint32_t`. - * - * @param[in] aMessage The message to read from. - * @param[in] aOffset The offset into the message pointing to the start of the TLV. - * @param[out] aValue A reference to a `uint32_t` to output the TLV's value. - * - * @retval OT_ERROR_NONE Successfully read the TLV and updated @p aValue. - * @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed. - * - */ - static otError ReadUint32Tlv(const Message &aMessage, uint16_t aOffset, uint32_t &aValue); - /** * This static method reads a TLV in a message at a given offset expecting a minimum length for the value. * @@ -230,7 +192,45 @@ public: static otError ReadTlv(const Message &aMessage, uint16_t aOffset, void *aValue, uint8_t aMinLength); /** - * This static method reads the requested TLV out of @p aMessage. + * This static method reads a simple TLV with a single non-integral value in a message at a given offset. + * + * @tparam SimpleTlvType The simple TLV type to read (must be a sub-class of `SimpleTlvInfo`). + * + * @param[in] aMessage The message to read from. + * @param[in] aOffset The offset into the message pointing to the start of the TLV. + * @param[out] aValue A reference to the value object to output the read value. + * + * @retval OT_ERROR_NONE Successfully read the TLV and updated the @p aValue. + * @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed. + * + */ + template + static otError Read(const Message &aMessage, uint16_t aOffset, typename SimpleTlvType::ValueType &aValue) + { + return ReadTlv(aMessage, aOffset, &aValue, sizeof(aValue)); + } + + /** + * This static method reads a simple TLV with a single integral value in a message at a given offset. + * + * @tparam UintTlvType The simple TLV type to read (must be a sub-class of `SimpleTlvInfo`). + * + * @param[in] aMessage The message to read from. + * @param[in] aOffset The offset into the message pointing to the start of the TLV. + * @param[out] aValue A reference to an unsigned int to output the read value. + * + * @retval OT_ERROR_NONE Successfully read the TLV and updated the @p aValue. + * @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed. + * + */ + template + static otError Read(const Message &aMessage, uint16_t aOffset, typename UintTlvType::UintValueType &aValue) + { + return ReadUintTlv(aMessage, aOffset, aValue); + } + + /** + * This static method searches for and reads a requested TLV out of a given message. * * This method can be used independent of whether the read TLV (from message) is an Extended TLV or not. * @@ -245,6 +245,25 @@ public: */ static otError FindTlv(const Message &aMessage, uint8_t aType, uint16_t aMaxSize, Tlv &aTlv); + /** + * This static method searches for and reads a requested TLV out of a given message. + * + * This method can be used independent of whether the read TLV (from message) is an Extended TLV or not. + * + * @tparam TlvType The TlvType to search for (must be a sub-class of `Tlv`). + * + * @param[in] aMessage A reference to the message. + * @param[out] aTlv A reference to the TLV that will be copied to. + * + * @retval OT_ERROR_NONE Successfully copied the TLV. + * @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType. + * + */ + template static otError FindTlv(const Message &aMessage, TlvType &aTlv) + { + return FindTlv(aMessage, TlvType::kType, sizeof(TlvType), aTlv); + } + /** * This static method obtains the offset of a TLV within @p aMessage. * @@ -276,48 +295,6 @@ public: */ static otError FindTlvValueOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset, uint16_t &aLength); - /** - * This static method searches for a TLV with a given type in a message and reads its value as an `uint8_t`. - * - * @param[in] aMessage A reference to the message. - * @param[in] aType The TLV type to search for. - * @param[out] aValue A reference to a `uint8_t` to output the TLV's value. - * - * @retval OT_ERROR_NONE Successfully found the TLV and updated @p aValue. - * @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType. - * @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed. - * - */ - static otError FindUint8Tlv(const Message &aMessage, uint8_t aType, uint8_t &aValue); - - /** - * This static method searches for a TLV with a given type in a message and reads its value as an `uint16_t`. - * - * @param[in] aMessage A reference to the message. - * @param[in] aType The TLV type to search for. - * @param[out] aValue A reference to a `uint16_t` to output the TLV's value. - * - * @retval OT_ERROR_NONE Successfully found the TLV and updated @p aValue. - * @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType. - * @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed. - * - */ - static otError FindUint16Tlv(const Message &aMessage, uint8_t aType, uint16_t &aValue); - - /** - * This static method searches for a TLV with a given type in a message and reads its value as an `uint32_t`. - * - * @param[in] aMessage A reference to the message. - * @param[in] aType The TLV type to search for. - * @param[out] aValue A reference to a `uint32_t` to output the TLV's value. - * - * @retval OT_ERROR_NONE Successfully found the TLV and updated @p aValue. - * @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType. - * @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed. - * - */ - static otError FindUint32Tlv(const Message &aMessage, uint8_t aType, uint32_t &aValue); - /** * This static method searches for a TLV with a given type in a message, ensures its length is same or larger than * an expected minimum value, and then reads its value into a given buffer. @@ -328,6 +305,8 @@ public: * If the TLV length is larger than @p aLength, the TLV is considered valid, but only the first @p aLength bytes * of the value are read and copied into the @p aValue buffer. * + * @tparam TlvType The TLV type to find. + * * @param[in] aMessage A reference to the message. * @param[in] aType The TLV type to search for. * @param[out] aValue A buffer to output the value (must contain at least @p aLength bytes). @@ -338,60 +317,69 @@ public: * @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed. * */ - static otError FindTlv(const Message &aMessage, uint8_t aType, void *aValue, uint8_t aLength); + template static otError Find(const Message &aMessage, void *aValue, uint8_t aLength) + { + return FindTlv(aMessage, TlvType::kType, aValue, aLength); + } /** - * This static method appends a simple TLV with a given type and an `uint8_t` value to a message. + * This static method searches for a simple TLV with a single non-integral value in a message, ensures its length is + * same or larger than the expected `ValueType` object size, and then reads its value into a value object reference. * - * On success this method grows the message by the size of the TLV. + * If the TLV length is smaller than the size of @p aValue, the TLV is considered invalid. In this case, this + * method returns `OT_ERROR_PARSE` and the @p aValue is not updated. * - * @param[in] aMessage A reference to the message to append to. - * @param[in] aType The TLV type. - * @param[in] aValue The TLV value (`uint8_t`). + * If the TLV length is larger than the size of @p aValue, the TLV is considered valid, but the size of + * `ValueType` bytes are read and copied into the @p aValue. * - * @retval OT_ERROR_NONE Successfully appended the TLV to the message. - * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. + * @tparam SimpleTlvType The simple TLV type to find (must be a sub-class of `SimpleTlvInfo`) + * + * @param[in] aMessage A reference to the message. + * @param[in] aType The TLV type to search for. + * @param[out] aValue A reference to the value object to output the read value. + * + * @retval OT_ERROR_NONE The TLV was found and read successfully. @p aValue is updated. + * @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType. + * @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed. * */ - static otError AppendUint8Tlv(Message &aMessage, uint8_t aType, uint8_t aValue); + template + static otError Find(const Message &aMessage, typename SimpleTlvType::ValueType &aValue) + { + return FindTlv(aMessage, SimpleTlvType::kType, &aValue, sizeof(aValue)); + } /** - * This static method appends a simple TLV with a given type and an `uint16_t` value to a message. + * This static method searches for a simple TLV with a single integral value in a message, and then reads its value + * into a given `uint` reference variable. * - * On success this method grows the message by the size of the TLV. + * If the TLV length is smaller than size of integral value, the TLV is considered invalid. In this case, this + * method returns `OT_ERROR_PARSE` and the @p aValue is not updated. * - * @param[in] aMessage A reference to the message to append to. - * @param[in] aType The TLV type. - * @param[in] aValue The TLV value (`uint16_t`). + * @tparam UintTlvType The simple TLV type to find (must be a sub-class of `UintTlvInfo`) * - * @retval OT_ERROR_NONE Successfully appended the TLV to the message. - * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. + * @param[in] aMessage A reference to the message. + * @param[out] aValue A reference to an unsigned int value to output the TLV's value. + * + * @retval OT_ERROR_NONE The TLV was found and read successfully. @p aValue is updated. + * @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType. + * @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed. * */ - static otError AppendUint16Tlv(Message &aMessage, uint8_t aType, uint16_t aValue); - - /** - * This static method appends a (simple) TLV with a given type and an `uint32_t` value to a message. - * - * On success this method grows the message by the size of the TLV. - * - * @param[in] aMessage A reference to the message to append to. - * @param[in] aType The TLV type. - * @param[in] aValue The TLV value (`uint32_t`). - * - * @retval OT_ERROR_NONE Successfully appended the TLV to the message. - * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. - * - */ - static otError AppendUint32Tlv(Message &aMessage, uint8_t aType, uint32_t aValue); + template + static otError Find(const Message &aMessage, typename UintTlvType::UintValueType &aValue) + { + return FindUintTlv(aMessage, UintTlvType::kType, aValue); + } /** * This static method appends a TLV with a given type and value to a message. * * On success this method grows the message by the size of the TLV. * + * @tparam TlvType The TLV type to append. + * * @param[in] aMessage A reference to the message to append to. - * @param[in] aType The TLV type. * @param[in] aValue A buffer containing the TLV value. * @param[in] aLength The value length (in bytes). * @@ -399,7 +387,49 @@ public: * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. * */ - static otError AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength); + template static otError Append(Message &aMessage, const void *aValue, uint8_t aLength) + { + return AppendTlv(aMessage, TlvType::kType, aValue, aLength); + } + + /** + * This static method appends a simple TLV with a single (non-integral) value to a message. + * + * On success this method grows the message by the size of the TLV. + * + * @tparam SimpleTlvType The simple TLV type to append (must be a sub-class of `SimpleTlvInfo`) + * + * @param[in] aMessage A reference to the message to append to. + * @param[in] aValue A reference to the object containing TLV's value. + * + * @retval OT_ERROR_NONE Successfully appended the TLV to the message. + * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. + * + */ + template + static otError Append(Message &aMessage, const typename SimpleTlvType::ValueType &aValue) + { + return AppendTlv(aMessage, SimpleTlvType::kType, &aValue, sizeof(aValue)); + } + + /** + * This static method appends a simple TLV with a single integral value to a message. + * + * On success this method grows the message by the size of the TLV. + * + * @tparam UintTlvType The simple TLV type to append (must be a sub-class of `UintTlvInfo`) + * + * @param[in] aMessage A reference to the message to append to. + * @param[in] aValue An unsigned int value to use as TLV's value. + * + * @retval OT_ERROR_NONE Successfully appended the TLV to the message. + * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. + * + */ + template static otError Append(Message &aMessage, typename UintTlvType::UintValueType aValue) + { + return AppendUintTlv(aMessage, UintTlvType::kType, aValue); + } protected: enum @@ -431,6 +461,13 @@ private: uint16_t * aSize, bool * aIsExtendedTlv); + static otError FindTlv(const Message &aMessage, uint8_t aType, void *aValue, uint8_t aLength); + static otError AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength); + template + static otError ReadUintTlv(const Message &aMessage, uint16_t aOffset, UintType &aValue); + template static otError FindUintTlv(const Message &aMessage, uint8_t aTyle, UintType &aValue); + template static otError AppendUintTlv(Message &aMessage, uint8_t aType, UintType aValue); + uint8_t mType; uint8_t mLength; } OT_TOOL_PACKED_END; @@ -461,6 +498,65 @@ private: uint16_t mLength; } OT_TOOL_PACKED_END; +/** + * This class defines constants for a TLV. + * + * @tparam kTlvTypeValue The TLV Type value. + * + */ +template class TlvInfo +{ +public: + enum : uint8_t + { + kType = kTlvTypeValue, ///< The TLV Type value. + }; +}; + +/** + * This class defines constants and types for a simple TLV with an unsigned int value type. + * + * This class and its sub-classes are intended to be used as the template type in `Tlv::Append()`, and + * the related `Tlv::FindTlv()` and `Tlv::ReadTlv()`. + * + * @tparam kTlvTypeValue The TLV Type value. + * @tparam UintType The TLV Value's type (must be an unsigned int, i.e. uint8_t, uint16_t, or uint32_t). + * + */ +template class UintTlvInfo : public TlvInfo +{ +public: + static_assert(TypeTraits::IsSame::kValue || TypeTraits::IsSame::kValue || + TypeTraits::IsSame::kValue, + "UintTlv must be used used with unsigned int value type"); + + typedef UintType UintValueType; ///< The TLV Value unsigned int type. +}; + +/** + * This class defines constants and types for a simple TLV with a single value. + * + * This class and its sub-classes are intended to be used as the template type in `Tlv::Append()`, + * and the related `Tlv::FindTlv()` and `Tlv::ReadTlv()`. + * + * @tparam kTlvTypeValue The TLV Type value. + * @tparam TlvValueType The TLV Value's type (must not be an integral type). + * + */ +template class SimpleTlvInfo : public TlvInfo +{ +public: + static_assert(!TypeTraits::IsPointer::kValue, "TlvValueType must not be a pointer"); + static_assert(!TypeTraits::IsSame::kValue, "SimpleTlv must not use int value type"); + static_assert(!TypeTraits::IsSame::kValue, "SimpleTlv must not use int value type"); + static_assert(!TypeTraits::IsSame::kValue, "SimpleTlv must not use int value type"); + static_assert(!TypeTraits::IsSame::kValue, "SimpleTlv must not use int value type"); + static_assert(!TypeTraits::IsSame::kValue, "SimpleTlv must not use int value type"); + static_assert(!TypeTraits::IsSame::kValue, "SimpleTlv must not use int value type"); + + typedef TlvValueType ValueType; ///< The TLV Value type. +}; + } // namespace ot #endif // TLVS_HPP_ diff --git a/src/core/common/type_traits.hpp b/src/core/common/type_traits.hpp index 13e1ed79d..105ff3fcf 100644 --- a/src/core/common/type_traits.hpp +++ b/src/core/common/type_traits.hpp @@ -86,6 +86,24 @@ template struct IsPointer : TrueValue { }; +/** + * This type indicates whether or not a given template `FirstType is the same as `SecondType`. + * + * The `constexpr` expression `IsSame::kValue` would be `true` when the two types are the same, + * otherwise it would be `false`. + * + * @tparam FirstType The first type. + * @tparam SecondType The second type. + * + */ +template struct IsSame : public FalseValue +{ +}; + +template struct IsSame : public TrueValue +{ +}; + } // namespace TypeTraits } // namespace ot diff --git a/src/core/meshcop/announce_begin_client.cpp b/src/core/meshcop/announce_begin_client.cpp index 75f7a0e14..23160f502 100644 --- a/src/core/meshcop/announce_begin_client.cpp +++ b/src/core/meshcop/announce_begin_client.cpp @@ -69,15 +69,15 @@ otError AnnounceBeginClient::SendRequest(uint32_t aChannelMask, SuccessOrExit(error = message->InitAsPost(aAddress, UriPath::kAnnounceBegin)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerSessionId, - Get().GetSessionId())); + SuccessOrExit( + error = Tlv::Append(*message, Get().GetSessionId())); channelMask.Init(); channelMask.SetChannelMask(aChannelMask); SuccessOrExit(error = channelMask.AppendTo(*message)); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, MeshCoP::Tlv::kCount, aCount)); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kPeriod, aPeriod)); + SuccessOrExit(error = Tlv::Append(*message, aCount)); + SuccessOrExit(error = Tlv::Append(*message, aPeriod)); messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(aAddress); diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index d8cad40e9..e39604033 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -174,13 +174,13 @@ void BorderAgent::HandleCoapResponse(ForwardContext &aForwardContext, const Coap { uint8_t state; - SuccessOrExit(error = Tlv::FindUint8Tlv(*aResponse, Tlv::kState, state)); + SuccessOrExit(error = Tlv::Find(*aResponse, state)); if (state == StateTlv::kAccept) { uint16_t sessionId; - SuccessOrExit(error = Tlv::FindUint16Tlv(*aResponse, Tlv::kCommissionerSessionId, sessionId)); + SuccessOrExit(error = Tlv::Find(*aResponse, sessionId)); IgnoreError(Get().GetCommissionerAloc(mCommissionerAloc.GetAddress(), sessionId)); Get().AddUnicastAddress(mCommissionerAloc); @@ -330,8 +330,7 @@ void BorderAgent::HandleProxyTransmit(const Coap::Message &aMessage) messageInfo.SetSockAddr(mCommissionerAloc.GetAddress()); messageInfo.SetPeerPort(tlv.GetDestinationPort()); - SuccessOrExit( - error = Tlv::FindTlv(aMessage, Tlv::kIPv6Address, messageInfo.GetPeerAddr().mFields.m8, sizeof(Ip6::Address))); + SuccessOrExit(error = Tlv::Find(aMessage, messageInfo.GetPeerAddr())); SuccessOrExit(error = Get().SendDatagram(*message, messageInfo, Ip6::kProtoUdp)); otLogInfoMeshCoP("Proxy transmit sent"); @@ -373,8 +372,7 @@ bool BorderAgent::HandleUdpReceive(const Message &aMessage, const Ip6::MessageIn aMessage.CopyTo(aMessage.GetOffset(), offset, udpLength, *message); } - SuccessOrExit(error = - Tlv::AppendTlv(*message, Tlv::kIPv6Address, &aMessageInfo.GetPeerAddr(), sizeof(Ip6::Address))); + SuccessOrExit(error = Tlv::Append(*message, aMessageInfo.GetPeerAddr())); SuccessOrExit(error = Get().SendMessage(*message, Get().GetMessageInfo())); @@ -451,7 +449,7 @@ void BorderAgent::HandleRelayTransmit(const Coap::Message &aMessage) VerifyOrExit(aMessage.IsNonConfirmablePostRequest()); - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kJoinerRouterLocator, joinerRouterRloc)); + SuccessOrExit(error = Tlv::Find(aMessage, joinerRouterRloc)); VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 393dc5a0d..90ca3dabf 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -755,24 +755,23 @@ otError Commissioner::SendMgmtCommissionerSetRequest(const otCommissioningDatase if (aDataset.mIsLocatorSet) { - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kBorderAgentLocator, aDataset.mLocator)); + SuccessOrExit(error = Tlv::Append(*message, aDataset.mLocator)); } if (aDataset.mIsSessionIdSet) { - SuccessOrExit(error = - Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerSessionId, aDataset.mSessionId)); + SuccessOrExit(error = Tlv::Append(*message, aDataset.mSessionId)); } if (aDataset.mIsSteeringDataSet) { - SuccessOrExit(error = Tlv::AppendTlv(*message, MeshCoP::Tlv::kSteeringData, aDataset.mSteeringData.m8, - aDataset.mSteeringData.mLength)); + SuccessOrExit( + error = Tlv::Append(*message, aDataset.mSteeringData.m8, aDataset.mSteeringData.mLength)); } if (aDataset.mIsJoinerUdpPortSet) { - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kJoinerUdpPort, aDataset.mJoinerUdpPort)); + SuccessOrExit(error = Tlv::Append(*message, aDataset.mJoinerUdpPort)); } if (aLength > 0) @@ -877,10 +876,10 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Message * aMessage otLogInfoMeshCoP("received Leader Petition response"); - SuccessOrExit(Tlv::FindUint8Tlv(*aMessage, Tlv::kState, state)); + SuccessOrExit(Tlv::Find(*aMessage, state)); VerifyOrExit(state == StateTlv::kAccept, IgnoreError(Stop(/* aResign */ false))); - SuccessOrExit(Tlv::FindUint16Tlv(*aMessage, Tlv::kCommissionerSessionId, mSessionId)); + SuccessOrExit(Tlv::Find(*aMessage, mSessionId)); // reject this session by sending KeepAlive reject if commissioner is in disabled state // this could happen if commissioner is stopped by API during petitioning @@ -930,10 +929,10 @@ void Commissioner::SendKeepAlive(uint16_t aSessionId) SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kLeaderKeepAlive)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState, - (mState == kStateActive) ? StateTlv::kAccept : StateTlv::kReject)); + SuccessOrExit( + error = Tlv::Append(*message, (mState == kStateActive) ? StateTlv::kAccept : StateTlv::kReject)); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kCommissionerSessionId, aSessionId)); + SuccessOrExit(error = Tlv::Append(*message, aSessionId)); messageInfo.SetSockAddr(Get().GetMeshLocal16()); SuccessOrExit(error = Get().GetLeaderAloc(messageInfo.GetPeerAddr())); @@ -971,7 +970,7 @@ void Commissioner::HandleLeaderKeepAliveResponse(Coap::Message * aMessag otLogInfoMeshCoP("received Leader keep-alive response"); - SuccessOrExit(Tlv::FindUint8Tlv(*aMessage, Tlv::kState, state)); + SuccessOrExit(Tlv::Find(*aMessage, state)); VerifyOrExit(state == StateTlv::kAccept, IgnoreError(Stop(/* aResign */ false))); mTimer.Start(Time::SecToMsec(kKeepAliveTimeout) / 2); @@ -1002,9 +1001,9 @@ void Commissioner::HandleRelayReceive(Coap::Message &aMessage, const Ip6::Messag VerifyOrExit(aMessage.IsNonConfirmablePostRequest()); - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kJoinerUdpPort, joinerPort)); - SuccessOrExit(error = Tlv::FindTlv(aMessage, Tlv::kJoinerIid, &joinerIid, sizeof(joinerIid))); - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kJoinerRouterLocator, joinerRloc)); + SuccessOrExit(error = Tlv::Find(aMessage, joinerPort)); + SuccessOrExit(error = Tlv::Find(aMessage, joinerIid)); + SuccessOrExit(error = Tlv::Find(aMessage, joinerRloc)); SuccessOrExit(error = Tlv::FindTlvValueOffset(aMessage, Tlv::kJoinerDtlsEncapsulation, offset, length)); VerifyOrExit(length <= aMessage.GetLength() - offset, error = OT_ERROR_PARSE); @@ -1084,7 +1083,7 @@ void Commissioner::HandleJoinerFinalize(Coap::Message &aMessage, const Ip6::Mess otLogInfoMeshCoP("received joiner finalize"); - if (Tlv::FindTlv(aMessage, Tlv::kProvisioningUrl, sizeof(provisioningUrl), provisioningUrl) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, provisioningUrl) == OT_ERROR_NONE) { uint8_t len = static_cast(StringLength(mProvisioningUrl, sizeof(mProvisioningUrl))); @@ -1122,7 +1121,7 @@ void Commissioner::SendJoinFinalizeResponse(const Coap::Message &aRequest, State message->SetOffset(message->GetLength()); message->SetSubType(Message::kSubTypeJoinerFinalizeResponse); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState, static_cast(aState))); + SuccessOrExit(error = Tlv::Append(*message, aState)); joinerMessageInfo.SetPeerAddr(Get().GetMeshLocal64()); joinerMessageInfo.GetPeerAddr().SetIid(mJoinerIid); @@ -1173,14 +1172,13 @@ otError Commissioner::SendRelayTransmit(Message &aMessage, const Ip6::MessageInf SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kRelayTx)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kJoinerUdpPort, mJoinerPort)); - SuccessOrExit(error = Tlv::AppendTlv(*message, Tlv::kJoinerIid, &mJoinerIid, sizeof(mJoinerIid))); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kJoinerRouterLocator, mJoinerRloc)); + SuccessOrExit(error = Tlv::Append(*message, mJoinerPort)); + SuccessOrExit(error = Tlv::Append(*message, mJoinerIid)); + SuccessOrExit(error = Tlv::Append(*message, mJoinerRloc)); if (aMessage.GetSubType() == Message::kSubTypeJoinerFinalizeResponse) { - SuccessOrExit( - error = Tlv::AppendTlv(*message, Tlv::kJoinerRouterKek, Get().GetKek().GetKey(), Kek::kSize)); + SuccessOrExit(error = Tlv::Append(*message, Get().GetKek())); } tlv.SetType(Tlv::kJoinerDtlsEncapsulation); diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 6552cc436..5403cf2b3 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -487,8 +487,7 @@ otError DatasetManager::SendSetRequest(const Dataset::Info &aDatasetInfo, const if (!hasSessionId) { - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kCommissionerSessionId, - Get().GetSessionId())); + SuccessOrExit(error = Tlv::Append(*message, Get().GetSessionId())); } } diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index c5b11bf84..02affdd23 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -107,14 +107,14 @@ otError DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInf type = (GetType() == Dataset::kActive) ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp; - if (Tlv::FindTlv(aMessage, Tlv::kActiveTimestamp, sizeof(activeTimestamp), activeTimestamp) != OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, activeTimestamp) != OT_ERROR_NONE) { ExitNow(); } VerifyOrExit(activeTimestamp.IsValid()); - if (Tlv::FindTlv(aMessage, Tlv::kPendingTimestamp, sizeof(pendingTimestamp), pendingTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, pendingTimestamp) == OT_ERROR_NONE) { VerifyOrExit(pendingTimestamp.IsValid()); } @@ -126,7 +126,7 @@ otError DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInf VerifyOrExit(mLocal.Compare(timestamp) > 0); // check channel - if (Tlv::FindTlv(aMessage, Tlv::kChannel, sizeof(channel), channel) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, channel) == OT_ERROR_NONE) { VerifyOrExit(channel.IsValid()); @@ -137,20 +137,20 @@ otError DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInf } // check PAN ID - if (Tlv::FindUint16Tlv(aMessage, Tlv::kPanId, panId) == OT_ERROR_NONE && panId != Get().GetPanId()) + if (Tlv::Find(aMessage, panId) == OT_ERROR_NONE && panId != Get().GetPanId()) { doesAffectConnectivity = true; } // check mesh local prefix - if (Tlv::FindTlv(aMessage, Tlv::kMeshLocalPrefix, &meshLocalPrefix, sizeof(meshLocalPrefix)) == OT_ERROR_NONE && + if (Tlv::Find(aMessage, meshLocalPrefix) == OT_ERROR_NONE && meshLocalPrefix != Get().GetMeshLocalPrefix()) { doesAffectConnectivity = true; } // check network master key - if (Tlv::FindTlv(aMessage, Tlv::kNetworkMasterKey, &masterKey, sizeof(masterKey)) == OT_ERROR_NONE) + if (Tlv::Find(aMessage, masterKey) == OT_ERROR_NONE) { hasMasterKey = true; @@ -171,7 +171,7 @@ otError DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInf } // check commissioner session id - if (Tlv::FindUint16Tlv(aMessage, Tlv::kCommissionerSessionId, sessionId) == OT_ERROR_NONE) + if (Tlv::Find(aMessage, sessionId) == OT_ERROR_NONE) { const CommissionerSessionIdTlv *localId; @@ -281,7 +281,7 @@ void DatasetManager::SendSetResponse(const Coap::Message & aRequest, SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState, static_cast(aState))); + SuccessOrExit(error = Tlv::Append(*message, aState)); SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); diff --git a/src/core/meshcop/energy_scan_client.cpp b/src/core/meshcop/energy_scan_client.cpp index df2078246..f27987843 100644 --- a/src/core/meshcop/energy_scan_client.cpp +++ b/src/core/meshcop/energy_scan_client.cpp @@ -77,16 +77,16 @@ otError EnergyScanClient::SendQuery(uint32_t aChannelM SuccessOrExit(error = message->InitAsPost(aAddress, UriPath::kEnergyScan)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerSessionId, - Get().GetSessionId())); + SuccessOrExit( + error = Tlv::Append(*message, Get().GetSessionId())); channelMask.Init(); channelMask.SetChannelMask(aChannelMask); SuccessOrExit(error = channelMask.AppendTo(*message)); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, MeshCoP::Tlv::kCount, aCount)); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kPeriod, aPeriod)); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kScanDuration, aScanDuration)); + SuccessOrExit(error = Tlv::Append(*message, aCount)); + SuccessOrExit(error = Tlv::Append(*message, aPeriod)); + SuccessOrExit(error = Tlv::Append(*message, aScanDuration)); messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(aAddress); diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index 81b13f978..e1f310b41 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -441,7 +441,7 @@ otError Joiner::PrepareJoinerFinalizeMessage(const char *aProvisioningUrl, SuccessOrExit(error = mFinalizeMessage->SetPayloadMarker()); mFinalizeMessage->SetOffset(mFinalizeMessage->GetLength()); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*mFinalizeMessage, Tlv::kState, StateTlv::kAccept)); + SuccessOrExit(error = Tlv::Append(*mFinalizeMessage, StateTlv::kAccept)); vendorNameTlv.Init(); vendorNameTlv.SetVendorName(aVendorName); @@ -535,7 +535,7 @@ void Joiner::HandleJoinerFinalizeResponse(Coap::Message & aMessage, VerifyOrExit(mState == kStateConnected && aResult == OT_ERROR_NONE && aMessage.IsAck() && aMessage.GetCode() == Coap::kCodeChanged); - SuccessOrExit(Tlv::FindUint8Tlv(aMessage, Tlv::kState, state)); + SuccessOrExit(Tlv::Find(aMessage, state)); SetState(kStateEntrust); mTimer.Start(kReponseTimeout); @@ -569,8 +569,7 @@ void Joiner::HandleJoinerEntrust(Coap::Message &aMessage, const Ip6::MessageInfo datasetInfo.Clear(); - SuccessOrExit( - error = Tlv::FindTlv(aMessage, Tlv::kNetworkMasterKey, &datasetInfo.UpdateMasterKey(), sizeof(MasterKey))); + SuccessOrExit(error = Tlv::Find(aMessage, datasetInfo.UpdateMasterKey())); datasetInfo.SetChannel(Get().GetPanChannel()); datasetInfo.SetPanId(Get().GetPanId()); diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index b8c12b998..7ceb9be47 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -146,10 +146,9 @@ void JoinerRouter::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &a SuccessOrExit(error = message->InitAsNonConfirmablePost(UriPath::kRelayRx)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kJoinerUdpPort, aMessageInfo.GetPeerPort())); - SuccessOrExit(error = Tlv::AppendTlv(*message, Tlv::kJoinerIid, &aMessageInfo.GetPeerAddr().GetIid(), - Ip6::InterfaceIdentifier::kSize)); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kJoinerRouterLocator, Get().GetRloc16())); + SuccessOrExit(error = Tlv::Append(*message, aMessageInfo.GetPeerPort())); + SuccessOrExit(error = Tlv::Append(*message, aMessageInfo.GetPeerAddr().GetIid())); + SuccessOrExit(error = Tlv::Append(*message, Get().GetRloc16())); tlv.SetType(Tlv::kJoinerDtlsEncapsulation); tlv.SetLength(aMessage.GetLength() - aMessage.GetOffset()); @@ -195,8 +194,8 @@ void JoinerRouter::HandleRelayTransmit(Coap::Message &aMessage, const Ip6::Messa otLogInfoMeshCoP("Received relay transmit"); - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kJoinerUdpPort, joinerPort)); - SuccessOrExit(error = Tlv::FindTlv(aMessage, Tlv::kJoinerIid, &joinerIid, sizeof(joinerIid))); + SuccessOrExit(error = Tlv::Find(aMessage, joinerPort)); + SuccessOrExit(error = Tlv::Find(aMessage, joinerIid)); SuccessOrExit(error = Tlv::FindTlvValueOffset(aMessage, Tlv::kJoinerDtlsEncapsulation, offset, length)); @@ -210,7 +209,7 @@ void JoinerRouter::HandleRelayTransmit(Coap::Message &aMessage, const Ip6::Messa SuccessOrExit(error = mSocket.SendTo(*message, messageInfo)); - if (Tlv::FindTlv(aMessage, Tlv::kJoinerRouterKek, &kek, sizeof(kek)) == OT_ERROR_NONE) + if (Tlv::Find(aMessage, kek) == OT_ERROR_NONE) { otLogInfoMeshCoP("Received kek"); @@ -327,14 +326,9 @@ Coap::Message *JoinerRouter::PrepareJoinerEntrustMessage(void) SuccessOrExit(error = message->SetPayloadMarker()); message->SetSubType(Message::kSubTypeJoinerEntrust); - SuccessOrExit( - error = Tlv::AppendTlv(*message, Tlv::kNetworkMasterKey, &Get().GetMasterKey(), sizeof(MasterKey))); - - SuccessOrExit(error = Tlv::AppendTlv(*message, Tlv::kMeshLocalPrefix, &Get().GetMeshLocalPrefix(), - sizeof(otMeshLocalPrefix))); - - SuccessOrExit(error = Tlv::AppendTlv(*message, Tlv::kExtendedPanId, &Get().GetExtendedPanId(), - sizeof(Mac::ExtendedPanId))); + SuccessOrExit(error = Tlv::Append(*message, Get().GetMasterKey())); + SuccessOrExit(error = Tlv::Append(*message, Get().GetMeshLocalPrefix())); + SuccessOrExit(error = Tlv::Append(*message, Get().GetExtendedPanId())); networkName.Init(); networkName.SetNetworkName(Get().GetNetworkName().GetAsData()); @@ -386,8 +380,7 @@ Coap::Message *JoinerRouter::PrepareJoinerEntrustMessage(void) SuccessOrExit(error = securityPolicy.AppendTo(*message)); } - SuccessOrExit( - error = Tlv::AppendUint32Tlv(*message, Tlv::kNetworkKeySequence, Get().GetCurrentKeySequence())); + SuccessOrExit(error = Tlv::Append(*message, Get().GetCurrentKeySequence())); exit: FreeAndNullMessageOnError(message, error); diff --git a/src/core/meshcop/meshcop_leader.cpp b/src/core/meshcop/meshcop_leader.cpp index 913d95bba..9822afdc3 100644 --- a/src/core/meshcop/meshcop_leader.cpp +++ b/src/core/meshcop/meshcop_leader.cpp @@ -81,7 +81,7 @@ void Leader::HandlePetition(Coap::Message &aMessage, const Ip6::MessageInfo &aMe otLogInfoMeshCoP("received petition"); VerifyOrExit(Get().IsRoutingLocator(aMessageInfo.GetPeerAddr())); - SuccessOrExit(Tlv::FindTlv(aMessage, Tlv::kCommissionerId, sizeof(commissionerId), commissionerId)); + SuccessOrExit(Tlv::FindTlv(aMessage, commissionerId)); if (mTimer.IsRunning()) { @@ -131,7 +131,7 @@ void Leader::SendPetitionResponse(const Coap::Message & aRequest, SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState, static_cast(aState))); + SuccessOrExit(error = Tlv::Append(*message, aState)); if (mTimer.IsRunning()) { @@ -140,7 +140,7 @@ void Leader::SendPetitionResponse(const Coap::Message & aRequest, if (aState == StateTlv::kAccept) { - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kCommissionerSessionId, mSessionId)); + SuccessOrExit(error = Tlv::Append(*message, mSessionId)); } SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); @@ -167,9 +167,9 @@ void Leader::HandleKeepAlive(Coap::Message &aMessage, const Ip6::MessageInfo &aM otLogInfoMeshCoP("received keep alive"); - SuccessOrExit(Tlv::FindUint8Tlv(aMessage, Tlv::kState, state)); + SuccessOrExit(Tlv::Find(aMessage, state)); - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, Tlv::kCommissionerSessionId, sessionId)); + SuccessOrExit(Tlv::Find(aMessage, sessionId)); borderAgentLocator = static_cast( Get().GetCommissioningDataSubTlv(Tlv::kBorderAgentLocator)); @@ -215,7 +215,7 @@ void Leader::SendKeepAliveResponse(const Coap::Message & aRequest, SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState, static_cast(aState))); + SuccessOrExit(error = Tlv::Append(*message, aState)); SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); diff --git a/src/core/meshcop/meshcop_tlvs.hpp b/src/core/meshcop/meshcop_tlvs.hpp index 2cab07290..aa77ddbec 100644 --- a/src/core/meshcop/meshcop_tlvs.hpp +++ b/src/core/meshcop/meshcop_tlvs.hpp @@ -103,7 +103,7 @@ public: kVendorData = OT_MESHCOP_TLV_VENDOR_DATA_TLV, ///< meshcop Vendor Data TLV kVendorStackVersion = OT_MESHCOP_TLV_VENDOR_STACK_VERSION_TLV, ///< meshcop Vendor Stack Version TLV kUdpEncapsulation = OT_MESHCOP_TLV_UDP_ENCAPSULATION_TLV, ///< meshcop UDP encapsulation TLV - kIPv6Address = OT_MESHCOP_TLV_IPV6_ADDRESS_TLV, ///< meshcop IPv6 address TLV + kIp6Address = OT_MESHCOP_TLV_IPV6_ADDRESS_TLV, ///< meshcop IPv6 address TLV kPendingTimestamp = OT_MESHCOP_TLV_PENDINGTIMESTAMP, ///< Pending Timestamp TLV kDelayTimer = OT_MESHCOP_TLV_DELAYTIMER, ///< Delay Timer TLV kChannelMask = OT_MESHCOP_TLV_CHANNELMASK, ///< Channel Mask TLV @@ -166,28 +166,23 @@ public: } /** - * This static method searches for a TLV with a given type in a message, ensures its length is same or larger than - * an expected minimum value, and then reads its value into a given buffer. + * This static method reads the requested TLV out of @p aMessage. * - * If the TLV length is smaller than the minimum length @p aLength, the TLV is considered invalid. In this case, - * this method returns `OT_ERROR_PARSE` and the @p aValue buffer is not updated. + * This method can be used independent of whether the read TLV (from message) is an Extended TLV or not. * - * If the TLV length is larger than @p aLength, the TLV is considered valid, but only the first @p aLength bytes - * of the value are read and copied into the @p aValue buffer. + * @tparam TlvType The TlvType to search for (must be a sub-class of `Tlv`). * - * @param[in] aMessage A reference to the message. - * @param[in] aType The TLV type to search for. - * @param[out] aValue A buffer to output the value (must contain at least @p aLength bytes). - * @param[in] aLength The expected (minimum) length of the TLV value. + * @param[in] aMessage A reference to the message. + * @param[out] aTlv A reference to the TLV that will be copied to. * - * @retval OT_ERROR_NONE The TLV was found and read successfully. @p aValue is updated. + * @retval OT_ERROR_NONE Successfully copied the TLV. * @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType. - * @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed. * */ - static otError FindTlv(const Message &aMessage, Type aType, void *aValue, uint8_t aLength) + + template static otError FindTlv(const Message &aMessage, TlvType &aTlv) { - return ot::Tlv::FindTlv(aMessage, aType, aValue, aLength); + return ot::Tlv::FindTlv(aMessage, aTlv); } /** @@ -284,19 +279,62 @@ public: void SetType(MeshCoP::Tlv::Type aType) { ot::ExtendedTlv::SetType(static_cast(aType)); } } OT_TOOL_PACKED_END; +/** + * This class defines Commissioner UDP Port TLV constants and types. + * + */ +typedef UintTlvInfo CommissionerUdpPortTlv; + +/** + * This class defines IPv6 Address TLV constants and types. + * + */ +typedef SimpleTlvInfo Ip6AddressTlv; + +/** + * This class defines Joiner IID TLV constants and types. + * + */ +typedef SimpleTlvInfo JoinerIidTlv; + +/** + * This class defines Joiner Router Locator TLV constants and types. + * + */ +typedef UintTlvInfo JoinerRouterLocatorTlv; + +/** + * This class defines Joiner Router KEK TLV constants and types. + * + */ +typedef SimpleTlvInfo JoinerRouterKekTlv; + +/** + * This class defines Count TLV constants and types. + * + */ +typedef UintTlvInfo CountTlv; + +/** + * This class defines Period TLV constants and types. + * + */ +typedef UintTlvInfo PeriodTlv; + +/** + * This class defines Scan Duration TLV constants and types. + * + */ +typedef UintTlvInfo ScanDurationTlv; + /** * This class implements Channel TLV generation and parsing. * */ OT_TOOL_PACKED_BEGIN -class ChannelTlv : public Tlv +class ChannelTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kChannel, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -359,14 +397,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class PanIdTlv : public Tlv +class PanIdTlv : public Tlv, public UintTlvInfo { public: - enum - { - kType = kPanId, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -411,14 +444,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class ExtendedPanIdTlv : public Tlv +class ExtendedPanIdTlv : public Tlv, public SimpleTlvInfo { public: - enum - { - kType = kExtendedPanId, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -463,14 +491,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class NetworkNameTlv : public Tlv +class NetworkNameTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kNetworkName, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -515,14 +538,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class PskcTlv : public Tlv +class PskcTlv : public Tlv, public SimpleTlvInfo { public: - enum - { - kType = kPskc, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -567,14 +585,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class NetworkMasterKeyTlv : public Tlv +class NetworkMasterKeyTlv : public Tlv, public SimpleTlvInfo { public: - enum - { - kType = kNetworkMasterKey, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -619,14 +632,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class NetworkKeySequenceTlv : public Tlv +class NetworkKeySequenceTlv : public Tlv, public UintTlvInfo { public: - enum - { - kType = kNetworkKeySequence, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -671,14 +679,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class MeshLocalPrefixTlv : public Tlv +class MeshLocalPrefixTlv : public Tlv, public SimpleTlvInfo { public: - enum - { - kType = kMeshLocalPrefix, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -733,14 +736,9 @@ class SteeringData; * */ OT_TOOL_PACKED_BEGIN -class SteeringDataTlv : public Tlv +class SteeringDataTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kSteeringData, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -795,14 +793,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class BorderAgentLocatorTlv : public Tlv +class BorderAgentLocatorTlv : public Tlv, public UintTlvInfo { public: - enum - { - kType = kBorderAgentLocator, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -847,13 +840,12 @@ private: * */ OT_TOOL_PACKED_BEGIN -class CommissionerIdTlv : public Tlv +class CommissionerIdTlv : public Tlv, public TlvInfo { public: enum { - kType = kCommissionerId, ///< The TLV Type. - kMaxLength = 64, ///< maximum length (bytes) + kMaxLength = 64, ///< maximum length (bytes) }; /** @@ -907,7 +899,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class CommissionerSessionIdTlv : public Tlv +class CommissionerSessionIdTlv : public Tlv, public UintTlvInfo { public: enum @@ -959,14 +951,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class SecurityPolicyTlv : public Tlv +class SecurityPolicyTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kSecurityPolicy, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1037,14 +1024,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class ActiveTimestampTlv : public Tlv, public Timestamp +class ActiveTimestampTlv : public Tlv, public Timestamp, public SimpleTlvInfo { public: - enum - { - kType = kActiveTimestamp, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1071,14 +1053,9 @@ public: * */ OT_TOOL_PACKED_BEGIN -class StateTlv : public Tlv +class StateTlv : public Tlv, public UintTlvInfo { public: - enum - { - kType = kState, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1102,7 +1079,7 @@ public: * State values. * */ - enum State + enum State : uint8_t { kReject = 0xff, ///< Reject (-1) kPending = 0, ///< Pending @@ -1134,14 +1111,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class JoinerUdpPortTlv : public Tlv +class JoinerUdpPortTlv : public Tlv, public UintTlvInfo { public: - enum - { - kType = kJoinerUdpPort, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1186,14 +1158,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class PendingTimestampTlv : public Tlv, public Timestamp +class PendingTimestampTlv : public Tlv, public Timestamp, public SimpleTlvInfo { public: - enum - { - kType = kPendingTimestamp, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1220,14 +1187,9 @@ public: * */ OT_TOOL_PACKED_BEGIN -class DelayTimerTlv : public Tlv +class DelayTimerTlv : public Tlv, public UintTlvInfo { public: - enum - { - kType = kDelayTimer, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1450,14 +1412,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class ChannelMaskBaseTlv : public Tlv +class ChannelMaskBaseTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kChannelMask, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1553,14 +1510,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class EnergyListTlv : public Tlv +class EnergyListTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kEnergyList, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1586,12 +1538,11 @@ public: * */ OT_TOOL_PACKED_BEGIN -class ProvisioningUrlTlv : public Tlv +class ProvisioningUrlTlv : public Tlv, public TlvInfo { public: enum { - kType = kProvisioningUrl, ///< The TLV Type. kMaxLength = OT_PROVISIONING_URL_MAX_SIZE, ///< Maximum number of chars in the Provisioning URL string. }; @@ -1663,14 +1614,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class VendorNameTlv : public Tlv +class VendorNameTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kVendorName, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1732,14 +1678,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class VendorModelTlv : public Tlv +class VendorModelTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kVendorModel, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1801,14 +1742,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class VendorSwVersionTlv : public Tlv +class VendorSwVersionTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kVendorSwVersion, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1870,14 +1806,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class VendorDataTlv : public Tlv +class VendorDataTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kVendorData, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -1939,14 +1870,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class VendorStackVersionTlv : public Tlv +class VendorStackVersionTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kVendorStackVersion, ///< The TLV Type. - }; - /** * Default constructor. * @@ -2096,14 +2022,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class UdpEncapsulationTlv : public ExtendedTlv +class UdpEncapsulationTlv : public ExtendedTlv, public TlvInfo { public: - enum - { - kType = MeshCoP::Tlv::kUdpEncapsulation, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -2181,14 +2102,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class DiscoveryRequestTlv : public Tlv +class DiscoveryRequestTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kDiscoveryRequest, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -2273,14 +2189,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class DiscoveryResponseTlv : public Tlv +class DiscoveryResponseTlv : public Tlv, public TlvInfo { public: - enum - { - kType = kDiscoveryResponse, ///< The TLV Type. - }; - /** * This method initializes the TLV. * @@ -2365,12 +2276,11 @@ private: * */ OT_TOOL_PACKED_BEGIN -class JoinerAdvertisementTlv : public Tlv +class JoinerAdvertisementTlv : public Tlv, public TlvInfo { public: enum { - kType = kJoinerAdvertisement, ///< The TLV Type. kAdvDataMaxLength = OT_JOINER_ADVDATA_MAX_LENGTH, ///< The Max Length of AdvData }; diff --git a/src/core/meshcop/panid_query_client.cpp b/src/core/meshcop/panid_query_client.cpp index 90fad3951..4598d0f96 100644 --- a/src/core/meshcop/panid_query_client.cpp +++ b/src/core/meshcop/panid_query_client.cpp @@ -74,14 +74,14 @@ otError PanIdQueryClient::SendQuery(uint16_t aPanId, SuccessOrExit(error = message->InitAsPost(aAddress, UriPath::kPanIdQuery)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerSessionId, - Get().GetSessionId())); + SuccessOrExit( + error = Tlv::Append(*message, Get().GetSessionId())); channelMask.Init(); channelMask.SetChannelMask(aChannelMask); SuccessOrExit(error = channelMask.AppendTo(*message)); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kPanId, aPanId)); + SuccessOrExit(error = Tlv::Append(*message, aPanId)); messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(aAddress); @@ -114,7 +114,7 @@ void PanIdQueryClient::HandleConflict(Coap::Message &aMessage, const Ip6::Messag otLogInfoMeshCoP("received panid conflict"); - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, MeshCoP::Tlv::kPanId, panId)); + SuccessOrExit(Tlv::Find(aMessage, panId)); VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0); diff --git a/src/core/thread/address_resolver.cpp b/src/core/thread/address_resolver.cpp index e20441a6d..58cd35e10 100644 --- a/src/core/thread/address_resolver.cpp +++ b/src/core/thread/address_resolver.cpp @@ -533,7 +533,7 @@ otError AddressResolver::SendAddressQuery(const Ip6::Address &aEid) SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kAddressQuery)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aEid, sizeof(aEid))); + SuccessOrExit(error = Tlv::Append(*message, aEid)); messageInfo.GetPeerAddr().SetToRealmLocalAllRoutersMulticast(); @@ -581,11 +581,11 @@ void AddressResolver::HandleAddressNotification(Coap::Message &aMessage, const I VerifyOrExit(aMessage.IsConfirmablePostRequest()); - SuccessOrExit(Tlv::FindTlv(aMessage, ThreadTlv::kTarget, &target, sizeof(target))); - SuccessOrExit(Tlv::FindTlv(aMessage, ThreadTlv::kMeshLocalEid, &meshLocalIid, sizeof(meshLocalIid))); - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, ThreadTlv::kRloc16, rloc16)); + SuccessOrExit(Tlv::Find(aMessage, target)); + SuccessOrExit(Tlv::Find(aMessage, meshLocalIid)); + SuccessOrExit(Tlv::Find(aMessage, rloc16)); - switch (Tlv::FindUint32Tlv(aMessage, ThreadTlv::kLastTransactionTime, lastTransactionTime)) + switch (Tlv::Find(aMessage, lastTransactionTime)) { case OT_ERROR_NONE: break; @@ -651,8 +651,8 @@ void AddressResolver::SendAddressError(const Ip6::Address & aTarget, SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kAddressError)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aTarget, sizeof(aTarget))); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kMeshLocalEid, &aMeshLocalIid, sizeof(aMeshLocalIid))); + SuccessOrExit(error = Tlv::Append(*message, aTarget)); + SuccessOrExit(error = Tlv::Append(*message, aMeshLocalIid)); if (aDestination == nullptr) { @@ -705,8 +705,8 @@ void AddressResolver::HandleAddressError(Coap::Message &aMessage, const Ip6::Mes } } - SuccessOrExit(error = Tlv::FindTlv(aMessage, ThreadTlv::kTarget, &target, sizeof(target))); - SuccessOrExit(error = Tlv::FindTlv(aMessage, ThreadTlv::kMeshLocalEid, &meshLocalIid, sizeof(meshLocalIid))); + SuccessOrExit(error = Tlv::Find(aMessage, target)); + SuccessOrExit(error = Tlv::Find(aMessage, meshLocalIid)); for (const Ip6::NetifUnicastAddress *address = Get().GetUnicastAddresses(); address; address = address->GetNext()) @@ -773,7 +773,7 @@ void AddressResolver::HandleAddressQuery(Coap::Message &aMessage, const Ip6::Mes VerifyOrExit(aMessage.IsNonConfirmablePostRequest()); - SuccessOrExit(Tlv::FindTlv(aMessage, ThreadTlv::kTarget, &target, sizeof(target))); + SuccessOrExit(Tlv::Find(aMessage, target)); otLogInfoArp("Received address query from 0x%04x for target %s", aMessageInfo.GetPeerAddr().GetIid().GetLocator(), target.ToString().AsCString()); @@ -830,13 +830,13 @@ void AddressResolver::SendAddressQueryResponse(const Ip6::Address & a SuccessOrExit(error = message->AppendUriPathOptions(UriPath::kAddressNotify)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aTarget, sizeof(aTarget))); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kMeshLocalEid, &aMeshLocalIid, sizeof(aMeshLocalIid))); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, ThreadTlv::kRloc16, Get().GetRloc16())); + SuccessOrExit(error = Tlv::Append(*message, aTarget)); + SuccessOrExit(error = Tlv::Append(*message, aMeshLocalIid)); + SuccessOrExit(error = Tlv::Append(*message, Get().GetRloc16())); if (aLastTransactionTime != nullptr) { - SuccessOrExit(error = Tlv::AppendUint32Tlv(*message, ThreadTlv::kLastTransactionTime, *aLastTransactionTime)); + SuccessOrExit(error = Tlv::Append(*message, *aLastTransactionTime)); } messageInfo.SetPeerAddr(aDestination); diff --git a/src/core/thread/announce_begin_server.cpp b/src/core/thread/announce_begin_server.cpp index 76fd6a8d9..9183df4d9 100644 --- a/src/core/thread/announce_begin_server.cpp +++ b/src/core/thread/announce_begin_server.cpp @@ -75,8 +75,8 @@ void AnnounceBeginServer::HandleRequest(Coap::Message &aMessage, const Ip6::Mess VerifyOrExit(aMessage.IsPostRequest()); VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0); - SuccessOrExit(Tlv::FindUint8Tlv(aMessage, MeshCoP::Tlv::kCount, count)); - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, MeshCoP::Tlv::kPeriod, period)); + SuccessOrExit(Tlv::Find(aMessage, count)); + SuccessOrExit(Tlv::Find(aMessage, period)); SendAnnounce(mask, count, period); diff --git a/src/core/thread/discover_scanner.cpp b/src/core/thread/discover_scanner.cpp index f27167f58..fa183fcd5 100644 --- a/src/core/thread/discover_scanner.cpp +++ b/src/core/thread/discover_scanner.cpp @@ -331,7 +331,8 @@ void DiscoverScanner::HandleDiscoveryResponse(const Message &aMessage, const Ip6 break; case MeshCoP::Tlv::kExtendedPanId: - SuccessOrExit(error = Tlv::ReadTlv(aMessage, offset, &result.mExtendedPanId, sizeof(Mac::ExtendedPanId))); + SuccessOrExit(error = Tlv::Read( + aMessage, offset, static_cast(result.mExtendedPanId))); break; case MeshCoP::Tlv::kNetworkName: @@ -364,7 +365,7 @@ void DiscoverScanner::HandleDiscoveryResponse(const Message &aMessage, const Ip6 break; case MeshCoP::Tlv::kJoinerUdpPort: - SuccessOrExit(error = Tlv::ReadUint16Tlv(aMessage, offset, result.mJoinerUdpPort)); + SuccessOrExit(error = Tlv::Read(aMessage, offset, result.mJoinerUdpPort)); break; default: diff --git a/src/core/thread/dua_manager.cpp b/src/core/thread/dua_manager.cpp index 1e45fcf29..ae5a71192 100644 --- a/src/core/thread/dua_manager.cpp +++ b/src/core/thread/dua_manager.cpp @@ -443,9 +443,8 @@ void DuaManager::PerformNextRegistration(void) if (mDuaState == kToRegister && mDelay.mFields.mRegistrationDelay == 0) { dua = GetDomainUnicastAddress(); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &dua, sizeof(dua))); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kMeshLocalEid, &mle.GetMeshLocal64().GetIid(), - sizeof(Ip6::InterfaceIdentifier))); + SuccessOrExit(error = Tlv::Append(*message, dua)); + SuccessOrExit(error = Tlv::Append(*message, mle.GetMeshLocal64().GetIid())); mDuaState = kRegistering; mLastRegistrationTime = TimerMilli::GetNow(); } @@ -477,12 +476,11 @@ void DuaManager::PerformNextRegistration(void) OT_ASSERT(duaPtr != nullptr); dua = *duaPtr; - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &dua, sizeof(dua))); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kMeshLocalEid, &child->GetMeshLocalIid(), - sizeof(Ip6::InterfaceIdentifier))); + SuccessOrExit(error = Tlv::Append(*message, dua)); + SuccessOrExit(error = Tlv::Append(*message, child->GetMeshLocalIid())); lastTransactionTime = Time::MsecToSec(TimerMilli::GetNow() - child->GetLastHeard()); - SuccessOrExit(error = Tlv::AppendUint32Tlv(*message, ThreadTlv::kLastTransactionTime, lastTransactionTime)); + SuccessOrExit(error = Tlv::Append(*message, lastTransactionTime)); #endif // OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE } @@ -585,8 +583,8 @@ otError DuaManager::ProcessDuaResponse(Coap::Message &aMessage) } else { - SuccessOrExit(error = Tlv::FindUint8Tlv(aMessage, ThreadTlv::kStatus, status)); - SuccessOrExit(error = Tlv::FindTlv(aMessage, ThreadTlv::kTarget, &target, sizeof(target))); + SuccessOrExit(error = Tlv::Find(aMessage, status)); + SuccessOrExit(error = Tlv::Find(aMessage, target)); } #if OPENTHREAD_CONFIG_DUA_ENABLE @@ -672,8 +670,8 @@ void DuaManager::SendAddressNotification(Ip6::Address & aAddress, SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kDuaRegistrationNotify)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, ThreadTlv::kStatus, static_cast(aStatus))); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aAddress, sizeof(aAddress))); + SuccessOrExit(error = Tlv::Append(*message, aStatus)); + SuccessOrExit(error = Tlv::Append(*message, aAddress)); messageInfo.GetPeerAddr().SetToRoutingLocator(Get().GetMeshLocalPrefix(), aChild.GetRloc16()); messageInfo.SetPeerPort(Tmf::kUdpPort); diff --git a/src/core/thread/energy_scan_server.cpp b/src/core/thread/energy_scan_server.cpp index 177effc54..e6d1e7da6 100644 --- a/src/core/thread/energy_scan_server.cpp +++ b/src/core/thread/energy_scan_server.cpp @@ -77,9 +77,9 @@ void EnergyScanServer::HandleRequest(Coap::Message &aMessage, const Ip6::Message VerifyOrExit(aMessage.IsPostRequest()); - SuccessOrExit(Tlv::FindUint8Tlv(aMessage, MeshCoP::Tlv::kCount, count)); - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, MeshCoP::Tlv::kPeriod, period)); - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, MeshCoP::Tlv::kScanDuration, scanDuration)); + SuccessOrExit(Tlv::Find(aMessage, count)); + SuccessOrExit(Tlv::Find(aMessage, period)); + SuccessOrExit(Tlv::Find(aMessage, scanDuration)); VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0); diff --git a/src/core/thread/link_metrics.cpp b/src/core/thread/link_metrics.cpp index 2f6015ae9..224ab0233 100644 --- a/src/core/thread/link_metrics.cpp +++ b/src/core/thread/link_metrics.cpp @@ -233,7 +233,7 @@ otError LinkMetrics::AppendLinkMetricsReport(Message &aMessage, const Message &a switch (tlv.GetType()) { case kLinkMetricsQueryId: - SuccessOrExit(error = Tlv::ReadUint8Tlv(aRequestMessage, offset, queryId)); + SuccessOrExit(error = Tlv::Read(aRequestMessage, offset, queryId)); hasQueryId = true; break; diff --git a/src/core/thread/link_metrics_tlvs.hpp b/src/core/thread/link_metrics_tlvs.hpp index 1a2e6056b..822467833 100644 --- a/src/core/thread/link_metrics_tlvs.hpp +++ b/src/core/thread/link_metrics_tlvs.hpp @@ -70,12 +70,17 @@ enum Type : uint8_t kEnhancedACKConfiguration = 7, ///< Enhanced ACK Configuration Sub-TLV }; +/** + * This class defines Link Metrics Query ID TLV constants and types. + * + */ +typedef UintTlvInfo LinkMetricsQueryIdTlv; + /** * This class implements Link Metrics Type Id Flags generation and parsing. * */ -OT_TOOL_PACKED_BEGIN -class LinkMetricsTypeIdFlags +OT_TOOL_PACKED_BEGIN class LinkMetricsTypeIdFlags { public: /** @@ -215,7 +220,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class LinkMetricsReportSubTlv : public Tlv +class LinkMetricsReportSubTlv : public Tlv, public TlvInfo { public: /** @@ -306,7 +311,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class LinkMetricsQueryOptionsTlv : public Tlv +class LinkMetricsQueryOptionsTlv : public Tlv, public TlvInfo { public: /** diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 00a2aeeec..1821ebdfe 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1014,37 +1014,37 @@ exit: otError Mle::AppendSourceAddress(Message &aMessage) const { - return Tlv::AppendUint16Tlv(aMessage, Tlv::kSourceAddress, GetRloc16()); + return Tlv::Append(aMessage, GetRloc16()); } otError Mle::AppendStatus(Message &aMessage, StatusTlv::Status aStatus) { - return Tlv::AppendUint8Tlv(aMessage, Tlv::kStatus, static_cast(aStatus)); + return Tlv::Append(aMessage, aStatus); } otError Mle::AppendMode(Message &aMessage, DeviceMode aMode) { - return Tlv::AppendUint8Tlv(aMessage, Tlv::kMode, aMode.Get()); + return Tlv::Append(aMessage, aMode.Get()); } otError Mle::AppendTimeout(Message &aMessage, uint32_t aTimeout) { - return Tlv::AppendUint32Tlv(aMessage, Tlv::kTimeout, aTimeout); + return Tlv::Append(aMessage, aTimeout); } otError Mle::AppendChallenge(Message &aMessage, const Challenge &aChallenge) { - return Tlv::AppendTlv(aMessage, Tlv::kChallenge, aChallenge.mBuffer, aChallenge.mLength); + return Tlv::Append(aMessage, aChallenge.mBuffer, aChallenge.mLength); } otError Mle::AppendChallenge(Message &aMessage, const uint8_t *aChallenge, uint8_t aChallengeLength) { - return Tlv::AppendTlv(aMessage, Tlv::kChallenge, aChallenge, aChallengeLength); + return Tlv::Append(aMessage, aChallenge, aChallengeLength); } otError Mle::AppendResponse(Message &aMessage, const Challenge &aResponse) { - return Tlv::AppendTlv(aMessage, Tlv::kResponse, aResponse.mBuffer, aResponse.mLength); + return Tlv::Append(aMessage, aResponse.mBuffer, aResponse.mLength); } otError Mle::ReadChallengeOrResponse(const Message &aMessage, uint8_t aTlvType, Challenge &aBuffer) @@ -1080,17 +1080,17 @@ otError Mle::ReadResponse(const Message &aMessage, Challenge &aResponse) otError Mle::AppendLinkFrameCounter(Message &aMessage) { - return Tlv::AppendUint32Tlv(aMessage, Tlv::kLinkFrameCounter, Get().GetMacFrameCounter()); + return Tlv::Append(aMessage, Get().GetMacFrameCounter()); } otError Mle::AppendMleFrameCounter(Message &aMessage) { - return Tlv::AppendUint32Tlv(aMessage, Tlv::kMleFrameCounter, Get().GetMleFrameCounter()); + return Tlv::Append(aMessage, Get().GetMleFrameCounter()); } otError Mle::AppendAddress16(Message &aMessage, uint16_t aRloc16) { - return Tlv::AppendUint16Tlv(aMessage, Tlv::kAddress16, aRloc16); + return Tlv::Append(aMessage, aRloc16); } otError Mle::AppendLeaderData(Message &aMessage) @@ -1111,7 +1111,7 @@ otError Mle::ReadLeaderData(const Message &aMessage, LeaderData &aLeaderData) otError error; LeaderDataTlv leaderDataTlv; - SuccessOrExit(error = Tlv::FindTlv(aMessage, Tlv::kLeaderData, sizeof(leaderDataTlv), leaderDataTlv)); + SuccessOrExit(error = Tlv::FindTlv(aMessage, leaderDataTlv)); VerifyOrExit(leaderDataTlv.IsValid(), error = OT_ERROR_PARSE); leaderDataTlv.Get(aLeaderData); @@ -1130,7 +1130,7 @@ otError Mle::AppendNetworkData(Message &aMessage, bool aStableOnly) length = sizeof(networkData); IgnoreError(Get().GetNetworkData(aStableOnly, networkData, length)); - error = Tlv::AppendTlv(aMessage, Tlv::kNetworkData, networkData, length); + error = Tlv::Append(aMessage, networkData, length); exit: return error; @@ -1138,7 +1138,7 @@ exit: otError Mle::AppendTlvRequest(Message &aMessage, const uint8_t *aTlvs, uint8_t aTlvsLength) { - return Tlv::AppendTlv(aMessage, Tlv::kTlvRequest, aTlvs, aTlvsLength); + return Tlv::Append(aMessage, aTlvs, aTlvsLength); } otError Mle::FindTlvRequest(const Message &aMessage, RequestedTlvs &aRequestedTlvs) @@ -1163,17 +1163,17 @@ exit: otError Mle::AppendScanMask(Message &aMessage, uint8_t aScanMask) { - return Tlv::AppendUint8Tlv(aMessage, Tlv::kScanMask, aScanMask); + return Tlv::Append(aMessage, aScanMask); } otError Mle::AppendLinkMargin(Message &aMessage, uint8_t aLinkMargin) { - return Tlv::AppendUint8Tlv(aMessage, Tlv::kLinkMargin, aLinkMargin); + return Tlv::Append(aMessage, aLinkMargin); } otError Mle::AppendVersion(Message &aMessage) { - return Tlv::AppendUint16Tlv(aMessage, Tlv::kVersion, kThreadVersion); + return Tlv::Append(aMessage, kThreadVersion); } bool Mle::HasUnregisteredAddress(void) @@ -1352,7 +1352,7 @@ otError Mle::AppendTimeParameter(Message &aMessage) otError Mle::AppendXtalAccuracy(Message &aMessage) { - return Tlv::AppendUint16Tlv(aMessage, Tlv::kXtalAccuracy, otPlatTimeGetXtalAccuracy()); + return Tlv::Append(aMessage, otPlatTimeGetXtalAccuracy()); } #endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE @@ -1415,8 +1415,8 @@ exit: otError Mle::AppendCslTimeout(Message &aMessage) { OT_ASSERT(Get().IsCslEnabled()); - return Tlv::AppendUint32Tlv(aMessage, Tlv::kCslTimeout, - Get().GetCslTimeout() == 0 ? mTimeout : Get().GetCslTimeout()); + return Tlv::Append(aMessage, Get().GetCslTimeout() == 0 ? mTimeout + : Get().GetCslTimeout()); } #endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE @@ -2393,7 +2393,7 @@ void Mle::SendAnnounce(uint8_t aChannel, bool aOrphanAnnounce, const Ip6::Addres SuccessOrExit(error = AppendActiveTimestamp(*message)); } - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kPanId, Get().GetPanId())); + SuccessOrExit(error = Tlv::Append(*message, Get().GetPanId())); SuccessOrExit(error = SendMessage(*message, aDestination)); @@ -2795,7 +2795,7 @@ void Mle::HandleAdvertisement(const Message &aMessage, const Ip6::MessageInfo &a uint16_t delay; // Source Address - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, sourceAddress)); Log(kMessageReceive, kTypeAdvertisement, aMessageInfo.GetPeerAddr(), sourceAddress); @@ -2839,7 +2839,7 @@ void Mle::HandleAdvertisement(const Message &aMessage, const Ip6::MessageInfo &a { RouteTlv route; - if ((Tlv::FindTlv(aMessage, Tlv::kRoute, sizeof(route), route) == OT_ERROR_NONE) && route.IsValid()) + if ((Tlv::FindTlv(aMessage, route) == OT_ERROR_NONE) && route.IsValid()) { // Overwrite Route Data IgnoreError(Get().ProcessRouteTlv(route)); @@ -2955,7 +2955,7 @@ otError Mle::HandleLeaderData(const Message &aMessage, const Ip6::MessageInfo &a } // Active Timestamp - if (Tlv::FindTlv(aMessage, Tlv::kActiveTimestamp, sizeof(activeTimestamp), activeTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, activeTimestamp) == OT_ERROR_NONE) { const MeshCoP::Timestamp *timestamp; @@ -2976,7 +2976,7 @@ otError Mle::HandleLeaderData(const Message &aMessage, const Ip6::MessageInfo &a } // Pending Timestamp - if (Tlv::FindTlv(aMessage, Tlv::kPendingTimestamp, sizeof(pendingTimestamp), pendingTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, pendingTimestamp) == OT_ERROR_NONE) { const MeshCoP::Timestamp *timestamp; @@ -3166,12 +3166,12 @@ void Mle::HandleParentResponse(const Message &aMessage, const Ip6::MessageInfo & #endif // Source Address - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, sourceAddress)); Log(kMessageReceive, kTypeParentResponse, aMessageInfo.GetPeerAddr(), sourceAddress); // Version - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kVersion, version)); + SuccessOrExit(error = Tlv::Find(aMessage, version)); VerifyOrExit(version >= OT_THREAD_VERSION_1_1, error = OT_ERROR_PARSE); // Response @@ -3189,7 +3189,7 @@ void Mle::HandleParentResponse(const Message &aMessage, const Ip6::MessageInfo & SuccessOrExit(error = ReadLeaderData(aMessage, leaderData)); // Link Margin - SuccessOrExit(error = Tlv::FindUint8Tlv(aMessage, Tlv::kLinkMargin, linkMarginFromTlv)); + SuccessOrExit(error = Tlv::Find(aMessage, linkMarginFromTlv)); linkMargin = LinkQualityInfo::ConvertRssToLinkMargin(Get().GetNoiseFloor(), linkInfo->GetRss()); @@ -3201,7 +3201,7 @@ void Mle::HandleParentResponse(const Message &aMessage, const Ip6::MessageInfo & linkQuality = LinkQualityInfo::ConvertLinkMarginToLinkQuality(linkMargin); // Connectivity - SuccessOrExit(error = Tlv::FindTlv(aMessage, Tlv::kConnectivity, sizeof(connectivity), connectivity)); + SuccessOrExit(error = Tlv::FindTlv(aMessage, connectivity)); VerifyOrExit(connectivity.IsValid(), error = OT_ERROR_PARSE); // Share data with application, if requested. @@ -3279,10 +3279,10 @@ void Mle::HandleParentResponse(const Message &aMessage, const Ip6::MessageInfo & } // Link Frame Counter - SuccessOrExit(error = Tlv::FindUint32Tlv(aMessage, Tlv::kLinkFrameCounter, linkFrameCounter)); + SuccessOrExit(error = Tlv::Find(aMessage, linkFrameCounter)); // Mle Frame Counter - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kMleFrameCounter, mleFrameCounter)) + switch (Tlv::Find(aMessage, mleFrameCounter)) { case OT_ERROR_NONE: break; @@ -3296,7 +3296,7 @@ void Mle::HandleParentResponse(const Message &aMessage, const Ip6::MessageInfo & #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE // Time Parameter - if (Tlv::FindTlv(aMessage, Tlv::kTimeParameter, sizeof(timeParameter), timeParameter) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, timeParameter) == OT_ERROR_NONE) { VerifyOrExit(timeParameter.IsValid()); @@ -3364,7 +3364,7 @@ void Mle::HandleChildIdResponse(const Message & aMessage, uint16_t offset; // Source Address - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, sourceAddress)); Log(kMessageReceive, kTypeChildIdResponse, aMessageInfo.GetPeerAddr(), sourceAddress); @@ -3376,14 +3376,14 @@ void Mle::HandleChildIdResponse(const Message & aMessage, SuccessOrExit(error = ReadLeaderData(aMessage, leaderData)); // ShortAddress - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kAddress16, shortAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, shortAddress)); // Network Data error = Tlv::FindTlvOffset(aMessage, Tlv::kNetworkData, networkDataOffset); SuccessOrExit(error); // Active Timestamp - if (Tlv::FindTlv(aMessage, Tlv::kActiveTimestamp, sizeof(activeTimestamp), activeTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, activeTimestamp) == OT_ERROR_NONE) { VerifyOrExit(activeTimestamp.IsValid(), error = OT_ERROR_PARSE); @@ -3403,7 +3403,7 @@ void Mle::HandleChildIdResponse(const Message & aMessage, } // Pending Timestamp - if (Tlv::FindTlv(aMessage, Tlv::kPendingTimestamp, sizeof(pendingTimestamp), pendingTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, pendingTimestamp) == OT_ERROR_NONE) { VerifyOrExit(pendingTimestamp.IsValid(), error = OT_ERROR_PARSE); @@ -3449,7 +3449,7 @@ void Mle::HandleChildIdResponse(const Message & aMessage, { RouteTlv route; - if (Tlv::FindTlv(aMessage, Tlv::kRoute, sizeof(route), route) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, route) == OT_ERROR_NONE) { SuccessOrExit(error = Get().ProcessRouteTlv(route)); } @@ -3483,7 +3483,7 @@ void Mle::HandleChildUpdateRequest(const Message &aMessage, const Ip6::MessageIn uint8_t numTlvs = 0; // Source Address - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, sourceAddress)); Log(kMessageReceive, kTypeChildUpdateRequestOfParent, aMessageInfo.GetPeerAddr(), sourceAddress); @@ -3505,7 +3505,7 @@ void Mle::HandleChildUpdateRequest(const Message &aMessage, const Ip6::MessageIn { uint8_t status; - switch (Tlv::FindUint8Tlv(aMessage, Tlv::kStatus, status)) + switch (Tlv::Find(aMessage, status)) { case OT_ERROR_NONE: VerifyOrExit(status != StatusTlv::kError, IgnoreError(BecomeDetached())); @@ -3590,22 +3590,22 @@ void Mle::HandleChildUpdateResponse(const Message & aMessage, } // Status - if (Tlv::FindUint8Tlv(aMessage, Tlv::kStatus, status) == OT_ERROR_NONE) + if (Tlv::Find(aMessage, status) == OT_ERROR_NONE) { IgnoreError(BecomeDetached()); ExitNow(); } // Mode - SuccessOrExit(error = Tlv::FindUint8Tlv(aMessage, Tlv::kMode, mode)); + SuccessOrExit(error = Tlv::Find(aMessage, mode)); VerifyOrExit(DeviceMode(mode) == mDeviceMode, error = OT_ERROR_DROP); switch (mRole) { case kRoleDetached: - SuccessOrExit(error = Tlv::FindUint32Tlv(aMessage, Tlv::kLinkFrameCounter, linkFrameCounter)); + SuccessOrExit(error = Tlv::Find(aMessage, linkFrameCounter)); - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kMleFrameCounter, mleFrameCounter)) + switch (Tlv::Find(aMessage, mleFrameCounter)) { case OT_ERROR_NONE: break; @@ -3629,7 +3629,7 @@ void Mle::HandleChildUpdateResponse(const Message & aMessage, case kRoleChild: // Source Address - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, sourceAddress)); if (RouterIdFromRloc16(sourceAddress) != RouterIdFromRloc16(GetRloc16())) { @@ -3641,7 +3641,7 @@ void Mle::HandleChildUpdateResponse(const Message & aMessage, SuccessOrExit(error = HandleLeaderData(aMessage, aMessageInfo)); // Timeout optional - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kTimeout, timeout)) + switch (Tlv::Find(aMessage, timeout)) { case OT_ERROR_NONE: mTimeout = timeout; @@ -3697,15 +3697,15 @@ void Mle::HandleAnnounce(const Message &aMessage, const Ip6::MessageInfo &aMessa Log(kMessageReceive, kTypeAnnounce, aMessageInfo.GetPeerAddr()); - SuccessOrExit(error = Tlv::FindTlv(aMessage, Tlv::kChannel, sizeof(channelTlv), channelTlv)); + SuccessOrExit(error = Tlv::FindTlv(aMessage, channelTlv)); VerifyOrExit(channelTlv.IsValid(), error = OT_ERROR_PARSE); channel = static_cast(channelTlv.GetChannel()); - SuccessOrExit(error = Tlv::FindTlv(aMessage, Tlv::kActiveTimestamp, sizeof(timestamp), timestamp)); + SuccessOrExit(error = Tlv::FindTlv(aMessage, timestamp)); VerifyOrExit(timestamp.IsValid(), error = OT_ERROR_PARSE); - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kPanId, panId)); + SuccessOrExit(error = Tlv::Find(aMessage, panId)); localTimestamp = Get().GetTimestamp(); diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 3e03be1a1..a5cc6cbca 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -575,7 +575,7 @@ void MleRouter::HandleLinkRequest(const Message &aMessage, const Ip6::MessageInf SuccessOrExit(error = ReadChallenge(aMessage, challenge)); // Version - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kVersion, version)); + SuccessOrExit(error = Tlv::Find(aMessage, version)); VerifyOrExit(version >= OT_THREAD_VERSION_1_1, error = OT_ERROR_PARSE); // Leader Data @@ -591,7 +591,7 @@ void MleRouter::HandleLinkRequest(const Message &aMessage, const Ip6::MessageInf } // Source Address - switch (Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)) + switch (Tlv::Find(aMessage, sourceAddress)) { case OT_ERROR_NONE: if (IsActiveRouter(sourceAddress)) @@ -647,7 +647,7 @@ void MleRouter::HandleLinkRequest(const Message &aMessage, const Ip6::MessageInf #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE if (neighbor != nullptr) { - if (Tlv::FindTlv(aMessage, Tlv::kTimeRequest, sizeof(timeRequest), timeRequest) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, timeRequest) == OT_ERROR_NONE) { neighbor->SetTimeSyncEnabled(true); } @@ -797,7 +797,7 @@ otError MleRouter::HandleLinkAccept(const Message & aMessage, uint8_t linkMargin; // Source Address - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, sourceAddress)); Log(kMessageReceive, aRequest ? kTypeLinkAcceptAndRequest : kTypeLinkAccept, aMessageInfo.GetPeerAddr(), sourceAddress); @@ -835,14 +835,14 @@ otError MleRouter::HandleLinkAccept(const Message & aMessage, } // Version - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kVersion, version)); + SuccessOrExit(error = Tlv::Find(aMessage, version)); VerifyOrExit(version >= OT_THREAD_VERSION_1_1, error = OT_ERROR_PARSE); // Link-Layer Frame Counter - SuccessOrExit(error = Tlv::FindUint32Tlv(aMessage, Tlv::kLinkFrameCounter, linkFrameCounter)); + SuccessOrExit(error = Tlv::Find(aMessage, linkFrameCounter)); // MLE Frame Counter - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kMleFrameCounter, mleFrameCounter)) + switch (Tlv::Find(aMessage, mleFrameCounter)) { case OT_ERROR_NONE: break; @@ -854,7 +854,7 @@ otError MleRouter::HandleLinkAccept(const Message & aMessage, } // Link Margin - switch (Tlv::FindUint8Tlv(aMessage, Tlv::kLinkMargin, linkMargin)) + switch (Tlv::Find(aMessage, linkMargin)) { case OT_ERROR_NONE: break; @@ -876,7 +876,7 @@ otError MleRouter::HandleLinkAccept(const Message & aMessage, case kRoleDetached: // Address16 - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kAddress16, address16)); + SuccessOrExit(error = Tlv::Find(aMessage, address16)); VerifyOrExit(GetRloc16() == address16, error = OT_ERROR_DROP); // Leader Data @@ -927,7 +927,7 @@ otError MleRouter::HandleLinkAccept(const Message & aMessage, } // Route (optional) - if (Tlv::FindTlv(aMessage, Tlv::kRoute, sizeof(route), route) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, route) == OT_ERROR_NONE) { VerifyOrExit(route.IsValid(), error = OT_ERROR_PARSE); SuccessOrExit(error = ProcessRouteTlv(route)); @@ -1147,13 +1147,13 @@ otError MleRouter::HandleAdvertisement(const Message & aMessage, aMessageInfo.GetPeerAddr().GetIid().ConvertToExtAddress(extAddr); // Source Address - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, sourceAddress)); // Leader Data SuccessOrExit(error = ReadLeaderData(aMessage, leaderData)); // Route Data (optional) - if (Tlv::FindTlv(aMessage, Tlv::kRoute, sizeof(route), route) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, route) == OT_ERROR_NONE) { VerifyOrExit(route.IsValid(), error = OT_ERROR_PARSE); } @@ -1603,11 +1603,11 @@ void MleRouter::HandleParentRequest(const Message &aMessage, const Ip6::MessageI aMessageInfo.GetPeerAddr().GetIid().ConvertToExtAddress(extAddr); // Version - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kVersion, version)); + SuccessOrExit(error = Tlv::Find(aMessage, version)); VerifyOrExit(version >= OT_THREAD_VERSION_1_1, error = OT_ERROR_PARSE); // Scan Mask - SuccessOrExit(error = Tlv::FindUint8Tlv(aMessage, Tlv::kScanMask, scanMask)); + SuccessOrExit(error = Tlv::Find(aMessage, scanMask)); switch (mRole) { @@ -1642,7 +1642,7 @@ void MleRouter::HandleParentRequest(const Message &aMessage, const Ip6::MessageI child->ResetLinkFailures(); child->SetState(Neighbor::kStateParentRequest); #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE - if (Tlv::FindTlv(aMessage, Tlv::kTimeRequest, sizeof(timeRequest), timeRequest) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, timeRequest) == OT_ERROR_NONE) { child->SetTimeSyncEnabled(true); } @@ -2207,7 +2207,7 @@ void MleRouter::HandleChildIdRequest(const Message & aMessage, VerifyOrExit(child != nullptr, error = OT_ERROR_ALREADY); // Version - SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kVersion, version)); + SuccessOrExit(error = Tlv::Find(aMessage, version)); VerifyOrExit(version >= OT_THREAD_VERSION_1_1, error = OT_ERROR_PARSE); // Response @@ -2221,10 +2221,10 @@ void MleRouter::HandleChildIdRequest(const Message & aMessage, Get().RemoveMessages(*child, Message::kSubTypeMleDataResponse); // Link-Layer Frame Counter - SuccessOrExit(error = Tlv::FindUint32Tlv(aMessage, Tlv::kLinkFrameCounter, linkFrameCounter)); + SuccessOrExit(error = Tlv::Find(aMessage, linkFrameCounter)); // MLE Frame Counter - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kMleFrameCounter, mleFrameCounter)) + switch (Tlv::Find(aMessage, mleFrameCounter)) { case OT_ERROR_NONE: break; @@ -2236,11 +2236,11 @@ void MleRouter::HandleChildIdRequest(const Message & aMessage, } // Mode - SuccessOrExit(error = Tlv::FindUint8Tlv(aMessage, Tlv::kMode, modeBitmask)); + SuccessOrExit(error = Tlv::Find(aMessage, modeBitmask)); mode.Set(modeBitmask); // Timeout - SuccessOrExit(error = Tlv::FindUint32Tlv(aMessage, Tlv::kTimeout, timeout)); + SuccessOrExit(error = Tlv::Find(aMessage, timeout)); // TLV Request SuccessOrExit(error = FindTlvRequest(aMessage, requestedTlvs)); @@ -2249,7 +2249,7 @@ void MleRouter::HandleChildIdRequest(const Message & aMessage, // Active Timestamp activeTimestamp.SetLength(0); - if (Tlv::FindTlv(aMessage, Tlv::kActiveTimestamp, sizeof(activeTimestamp), activeTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, activeTimestamp) == OT_ERROR_NONE) { VerifyOrExit(activeTimestamp.IsValid(), error = OT_ERROR_PARSE); } @@ -2257,7 +2257,7 @@ void MleRouter::HandleChildIdRequest(const Message & aMessage, // Pending Timestamp pendingTimestamp.SetLength(0); - if (Tlv::FindTlv(aMessage, Tlv::kPendingTimestamp, sizeof(pendingTimestamp), pendingTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, pendingTimestamp) == OT_ERROR_NONE) { VerifyOrExit(pendingTimestamp.IsValid(), error = OT_ERROR_PARSE); } @@ -2367,7 +2367,7 @@ void MleRouter::HandleChildUpdateRequest(const Message & aMessage, Log(kMessageReceive, kTypeChildUpdateRequestOfChild, aMessageInfo.GetPeerAddr()); // Mode - SuccessOrExit(error = Tlv::FindUint8Tlv(aMessage, Tlv::kMode, modeBitmask)); + SuccessOrExit(error = Tlv::Find(aMessage, modeBitmask)); mode.Set(modeBitmask); // Challenge @@ -2435,7 +2435,7 @@ void MleRouter::HandleChildUpdateRequest(const Message & aMessage, } // Timeout - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kTimeout, timeout)) + switch (Tlv::Find(aMessage, timeout)) { case OT_ERROR_NONE: if (child->GetTimeout() != timeout) @@ -2480,12 +2480,12 @@ void MleRouter::HandleChildUpdateRequest(const Message & aMessage, CslChannelTlv cslChannel; uint32_t cslTimeout; - if (Tlv::FindUint32Tlv(aMessage, Tlv::kCslTimeout, cslTimeout) == OT_ERROR_NONE) + if (Tlv::Find(aMessage, cslTimeout) == OT_ERROR_NONE) { child->SetCslTimeout(cslTimeout); } - if (Tlv::FindTlv(aMessage, Tlv::kCslChannel, sizeof(cslChannel), cslChannel) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, cslChannel) == OT_ERROR_NONE) { child->SetCslChannel(static_cast(cslChannel.GetChannel())); } @@ -2573,7 +2573,7 @@ void MleRouter::HandleChildUpdateResponse(const Message & aMessage, Log(kMessageReceive, kTypeChildUpdateResponseOfChild, aMessageInfo.GetPeerAddr(), child->GetRloc16()); // Source Address - switch (Tlv::FindUint16Tlv(aMessage, Tlv::kSourceAddress, sourceAddress)) + switch (Tlv::Find(aMessage, sourceAddress)) { case OT_ERROR_NONE: if (child->GetRloc16() != sourceAddress) @@ -2592,7 +2592,7 @@ void MleRouter::HandleChildUpdateResponse(const Message & aMessage, } // Status - switch (Tlv::FindUint8Tlv(aMessage, Tlv::kStatus, status)) + switch (Tlv::Find(aMessage, status)) { case OT_ERROR_NONE: VerifyOrExit(status != StatusTlv::kError, RemoveNeighbor(*child)); @@ -2605,7 +2605,7 @@ void MleRouter::HandleChildUpdateResponse(const Message & aMessage, // Link-Layer Frame Counter - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kLinkFrameCounter, linkFrameCounter)) + switch (Tlv::Find(aMessage, linkFrameCounter)) { case OT_ERROR_NONE: child->SetLinkFrameCounter(linkFrameCounter); @@ -2618,7 +2618,7 @@ void MleRouter::HandleChildUpdateResponse(const Message & aMessage, } // MLE Frame Counter - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kMleFrameCounter, mleFrameCounter)) + switch (Tlv::Find(aMessage, mleFrameCounter)) { case OT_ERROR_NONE: child->SetMleFrameCounter(mleFrameCounter); @@ -2630,7 +2630,7 @@ void MleRouter::HandleChildUpdateResponse(const Message & aMessage, } // Timeout - switch (Tlv::FindUint32Tlv(aMessage, Tlv::kTimeout, timeout)) + switch (Tlv::Find(aMessage, timeout)) { case OT_ERROR_NONE: child->SetTimeout(timeout); @@ -2697,7 +2697,7 @@ void MleRouter::HandleDataRequest(const Message & aMessage, // Active Timestamp activeTimestamp.SetLength(0); - if (Tlv::FindTlv(aMessage, Tlv::kActiveTimestamp, sizeof(activeTimestamp), activeTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, activeTimestamp) == OT_ERROR_NONE) { VerifyOrExit(activeTimestamp.IsValid(), error = OT_ERROR_PARSE); } @@ -2705,7 +2705,7 @@ void MleRouter::HandleDataRequest(const Message & aMessage, // Pending Timestamp pendingTimestamp.SetLength(0); - if (Tlv::FindTlv(aMessage, Tlv::kPendingTimestamp, sizeof(pendingTimestamp), pendingTimestamp) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, pendingTimestamp) == OT_ERROR_NONE) { VerifyOrExit(pendingTimestamp.IsValid(), error = OT_ERROR_PARSE); } @@ -2848,7 +2848,7 @@ void MleRouter::HandleDiscoveryRequest(const Message &aMessage, const Ip6::Messa break; case MeshCoP::Tlv::kExtendedPanId: - SuccessOrExit(error = Tlv::ReadTlv(aMessage, offset, &extPanId, sizeof(extPanId))); + SuccessOrExit(error = Tlv::Read(aMessage, offset, extPanId)); VerifyOrExit(Get().GetExtendedPanId() != extPanId, error = OT_ERROR_DROP); break; @@ -2920,8 +2920,7 @@ otError MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, uint1 if (Get().IsNativeCommissioningAllowed()) { - SuccessOrExit( - error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerUdpPort, MeshCoP::kBorderAgentUdpPort)); + SuccessOrExit(error = Tlv::Append(*message, MeshCoP::kBorderAgentUdpPort)); discoveryResponse.SetNativeCommissioner(true); } @@ -2933,8 +2932,7 @@ otError MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, uint1 SuccessOrExit(error = discoveryResponse.AppendTo(*message)); // Extended PAN ID TLV - SuccessOrExit(error = Tlv::AppendTlv(*message, MeshCoP::Tlv::kExtendedPanId, Get().GetExtendedPanId().m8, - sizeof(Mac::ExtendedPanId))); + SuccessOrExit(error = Tlv::Append(*message, Get().GetExtendedPanId())); // Network Name TLV networkName.Init(); @@ -2946,8 +2944,8 @@ otError MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, uint1 // Otherwise use the one from commissioning data. if (!mSteeringData.IsEmpty()) { - SuccessOrExit(error = Tlv::AppendTlv(*message, MeshCoP::Tlv::kSteeringData, mSteeringData.GetData(), - mSteeringData.GetLength())); + SuccessOrExit(error = Tlv::Append(*message, mSteeringData.GetData(), + mSteeringData.GetLength())); } else #endif @@ -2962,9 +2960,8 @@ otError MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, uint1 } } - // Joiner UDP Port TLV - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kJoinerUdpPort, - Get().GetJoinerUdpPort())); + SuccessOrExit( + error = Tlv::Append(*message, Get().GetJoinerUdpPort())); tlv.SetLength(static_cast(message->GetLength() - startOffset)); message->Write(startOffset - sizeof(tlv), tlv); @@ -3549,16 +3546,14 @@ otError MleRouter::SendAddressSolicit(ThreadStatusTlv::Status aStatus) SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kAddressSolicit)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kExtMacAddress, Get().GetExtAddress().m8, - sizeof(Mac::ExtAddress))); + SuccessOrExit(error = Tlv::Append(*message, Get().GetExtAddress())); if (IsRouterIdValid(mPreviousRouterId)) { - SuccessOrExit(error = - Tlv::AppendUint16Tlv(*message, ThreadTlv::kRloc16, Rloc16FromRouterId(mPreviousRouterId))); + SuccessOrExit(error = Tlv::Append(*message, Rloc16FromRouterId(mPreviousRouterId))); } - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, ThreadTlv::kStatus, static_cast(aStatus))); + SuccessOrExit(error = Tlv::Append(*message, aStatus)); #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE SuccessOrExit(error = AppendXtalAccuracy(*message)); @@ -3590,10 +3585,9 @@ void MleRouter::SendAddressRelease(void) SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kAddressRelease)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, ThreadTlv::kRloc16, Rloc16FromRouterId(mRouterId))); + SuccessOrExit(error = Tlv::Append(*message, Rloc16FromRouterId(mRouterId))); - SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kExtMacAddress, Get().GetExtAddress().m8, - sizeof(Mac::ExtAddress))); + SuccessOrExit(error = Tlv::Append(*message, Get().GetExtAddress())); messageInfo.SetSockAddr(GetMeshLocal16()); SuccessOrExit(error = GetLeaderAddress(messageInfo.GetPeerAddr())); @@ -3637,7 +3631,7 @@ void MleRouter::HandleAddressSolicitResponse(Coap::Message * aMessage, Log(kMessageReceive, kTypeAddressReply, aMessageInfo->GetPeerAddr()); - SuccessOrExit(Tlv::FindUint8Tlv(*aMessage, ThreadTlv::kStatus, status)); + SuccessOrExit(Tlv::Find(*aMessage, status)); if (status != ThreadStatusTlv::kSuccess) { @@ -3656,10 +3650,10 @@ void MleRouter::HandleAddressSolicitResponse(Coap::Message * aMessage, ExitNow(); } - SuccessOrExit(Tlv::FindUint16Tlv(*aMessage, ThreadTlv::kRloc16, rloc16)); + SuccessOrExit(Tlv::Find(*aMessage, rloc16)); routerId = RouterIdFromRloc16(rloc16); - SuccessOrExit(ThreadTlv::FindTlv(*aMessage, ThreadTlv::kRouterMask, sizeof(routerMaskTlv), routerMaskTlv)); + SuccessOrExit(Tlv::FindTlv(*aMessage, routerMaskTlv)); VerifyOrExit(routerMaskTlv.IsValid()); // assign short address @@ -3736,13 +3730,12 @@ void MleRouter::HandleAddressSolicit(Coap::Message &aMessage, const Ip6::Message Log(kMessageReceive, kTypeAddressSolicit, aMessageInfo.GetPeerAddr()); - SuccessOrExit(error = ThreadTlv::FindTlv(aMessage, ThreadTlv::kExtMacAddress, &extAddress, sizeof(extAddress))); - - SuccessOrExit(error = Tlv::FindUint8Tlv(aMessage, ThreadTlv::kStatus, status)); + SuccessOrExit(error = Tlv::Find(aMessage, extAddress)); + SuccessOrExit(error = Tlv::Find(aMessage, status)); #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE // In a time sync enabled network, all routers' xtal accuracy must be less than the threshold. - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, Tlv::kXtalAccuracy, xtalAccuracy)); + SuccessOrExit(Tlv::Find(aMessage, xtalAccuracy)); VerifyOrExit(xtalAccuracy <= Get().GetXtalThreshold()); #endif @@ -3770,7 +3763,7 @@ void MleRouter::HandleAddressSolicit(Coap::Message &aMessage, const Ip6::Message OT_UNREACHABLE_CODE(break); } - switch (Tlv::FindUint16Tlv(aMessage, ThreadTlv::kRloc16, rloc16)) + switch (Tlv::Find(aMessage, rloc16)) { case OT_ERROR_NONE: router = mRouterTable.Allocate(RouterIdFromRloc16(rloc16)); @@ -3821,13 +3814,12 @@ void MleRouter::SendAddressSolicitResponse(const Coap::Message & aRequest, SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, ThreadTlv::kStatus, - aRouter == nullptr ? ThreadStatusTlv::kNoAddressAvailable - : ThreadStatusTlv::kSuccess)); + SuccessOrExit(error = Tlv::Append( + *message, aRouter == nullptr ? ThreadStatusTlv::kNoAddressAvailable : ThreadStatusTlv::kSuccess)); if (aRouter != nullptr) { - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, ThreadTlv::kRloc16, aRouter->GetRloc16())); + SuccessOrExit(error = Tlv::Append(*message, aRouter->GetRloc16())); routerMaskTlv.Init(); routerMaskTlv.SetIdSequence(mRouterTable.GetRouterIdSequence()); @@ -3861,9 +3853,8 @@ void MleRouter::HandleAddressRelease(Coap::Message &aMessage, const Ip6::Message Log(kMessageReceive, kTypeAddressRelease, aMessageInfo.GetPeerAddr()); - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, ThreadTlv::kRloc16, rloc16)); - - SuccessOrExit(Tlv::FindTlv(aMessage, ThreadTlv::kExtMacAddress, &extAddress, sizeof(extAddress))); + SuccessOrExit(Tlv::Find(aMessage, rloc16)); + SuccessOrExit(Tlv::Find(aMessage, extAddress)); routerId = RouterIdFromRloc16(rloc16); router = mRouterTable.GetRouter(routerId); diff --git a/src/core/thread/mle_tlvs.hpp b/src/core/thread/mle_tlvs.hpp index e5e0d1a24..3000e427e 100644 --- a/src/core/thread/mle_tlvs.hpp +++ b/src/core/thread/mle_tlvs.hpp @@ -139,6 +139,96 @@ public: } OT_TOOL_PACKED_END; +/** + * This class defines Source Address TLV constants and types. + * + */ +typedef UintTlvInfo SourceAddressTlv; + +/** + * This class defines Mode TLV constants and types. + * + */ +typedef UintTlvInfo ModeTlv; + +/** + * This class defines Timeout TLV constants and types. + * + */ +typedef UintTlvInfo TimeoutTlv; + +/** + * This class defines Challenge TLV constants and types. + * + */ +typedef TlvInfo ChallengeTlv; + +/** + * This class defines Response TLV constants and types. + * + */ +typedef TlvInfo ResponseTlv; + +/** + * This class defines Link Frame Counter TLV constants and types. + * + */ +typedef UintTlvInfo LinkFrameCounterTlv; + +/** + * This class defines MLE Frame Counter TLV constants and types. + * + */ +typedef UintTlvInfo MleFrameCounterTlv; + +/** + * This class defines Address16 TLV constants and types. + * + */ +typedef UintTlvInfo Address16Tlv; + +/** + * This class defines Network Data TLV constants and types. + * + */ +typedef TlvInfo NetworkDataTlv; + +/** + * This class defines TLV Request TLV constants and types. + * + */ +typedef TlvInfo TlvRequestTlv; + +/** + * This class defines Link Margin TLV constants and types. + * + */ +typedef UintTlvInfo LinkMarginTlv; + +/** + * This class defines Version TLV constants and types. + * + */ +typedef UintTlvInfo VersionTlv; + +/** + * This class defines PAN ID TLV constants and types. + * + */ +typedef UintTlvInfo PanIdTlv; + +/** + * This class defines CSL Timeout TLV constants and types. + * + */ +typedef UintTlvInfo CslTimeoutTlv; + +/** + * This class defines XTAL Accuracy TLV constants and types. + * + */ +typedef UintTlvInfo XtalAccuracyTlv; + #if !OPENTHREAD_CONFIG_MLE_LONG_ROUTES_ENABLE /** @@ -146,7 +236,7 @@ public: * */ OT_TOOL_PACKED_BEGIN -class RouteTlv : public Tlv +class RouteTlv : public Tlv, public TlvInfo { public: enum @@ -333,7 +423,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class RouteTlv : public Tlv +class RouteTlv : public Tlv, public TlvInfo { public: /** @@ -556,7 +646,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class LeaderDataTlv : public Tlv +class LeaderDataTlv : public Tlv, public TlvInfo { public: /** @@ -620,7 +710,7 @@ private: * This class implements Scan Mask TLV generation and parsing. * */ -class ScanMaskTlv +class ScanMaskTlv : public UintTlvInfo { public: enum @@ -655,7 +745,7 @@ public: * */ OT_TOOL_PACKED_BEGIN -class ConnectivityTlv : public Tlv +class ConnectivityTlv : public Tlv, public TlvInfo { public: /** @@ -880,12 +970,12 @@ private: * This class specifies Status TLV status values. * */ -struct StatusTlv +struct StatusTlv : public UintTlvInfo { /** * Status values. */ - enum Status + enum Status : uint8_t { kError = 1, ///< Error. }; @@ -991,7 +1081,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class ChannelTlv : public Tlv +class ChannelTlv : public Tlv, public TlvInfo { public: /** @@ -1056,7 +1146,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class TimeRequestTlv : public Tlv +class TimeRequestTlv : public Tlv, public TlvInfo { public: /** @@ -1084,7 +1174,7 @@ public: * */ OT_TOOL_PACKED_BEGIN -class TimeParameterTlv : public Tlv +class TimeParameterTlv : public Tlv, public TlvInfo { public: /** @@ -1150,7 +1240,9 @@ private: * */ OT_TOOL_PACKED_BEGIN -class ActiveTimestampTlv : public Tlv, public MeshCoP::Timestamp +class ActiveTimestampTlv : public Tlv, + public MeshCoP::Timestamp, + public SimpleTlvInfo { public: /** @@ -1159,7 +1251,7 @@ public: */ void Init(void) { - SetType(Mle::Tlv::kActiveTimestamp); + SetType(Tlv::kActiveTimestamp); SetLength(sizeof(*this) - sizeof(Tlv)); Timestamp::Init(); } @@ -1179,7 +1271,9 @@ public: * */ OT_TOOL_PACKED_BEGIN -class PendingTimestampTlv : public Tlv, public MeshCoP::Timestamp +class PendingTimestampTlv : public Tlv, + public MeshCoP::Timestamp, + public SimpleTlvInfo { public: /** @@ -1188,7 +1282,7 @@ public: */ void Init(void) { - SetType(Mle::Tlv::kPendingTimestamp); + SetType(Tlv::kPendingTimestamp); SetLength(sizeof(*this) - sizeof(Tlv)); Timestamp::Init(); } @@ -1209,7 +1303,7 @@ public: * */ OT_TOOL_PACKED_BEGIN -class CslChannelTlv : public Tlv +class CslChannelTlv : public Tlv, public TlvInfo { public: /** diff --git a/src/core/thread/mlr_manager.cpp b/src/core/thread/mlr_manager.cpp index 5508a7a77..3231a9943 100644 --- a/src/core/thread/mlr_manager.cpp +++ b/src/core/thread/mlr_manager.cpp @@ -407,13 +407,13 @@ otError MlrManager::SendMulticastListenerRegistrationMessage(const otIp6Address #if OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE if (Get().IsActive()) { - SuccessOrExit(error = ThreadTlv::AppendUint16Tlv(*message, ThreadTlv::kCommissionerSessionId, - Get().GetSessionId())); + SuccessOrExit( + error = Tlv::Append(*message, Get().GetSessionId())); } if (aTimeout != nullptr) { - SuccessOrExit(error = Tlv::AppendUint32Tlv(*message, ThreadTlv::kTimeout, *aTimeout)); + SuccessOrExit(error = Tlv::Append(*message, *aTimeout)); } #else OT_ASSERT(aTimeout == nullptr); @@ -505,7 +505,7 @@ otError MlrManager::ParseMulticastListenerRegistrationResponse(otError aR VerifyOrExit(aResult == OT_ERROR_NONE && aMessage != nullptr, error = OT_ERROR_PARSE); VerifyOrExit(aMessage->GetCode() == Coap::kCodeChanged, error = OT_ERROR_PARSE); - SuccessOrExit(error = Tlv::FindUint8Tlv(*aMessage, ThreadTlv::kStatus, aStatus)); + SuccessOrExit(error = Tlv::Find(*aMessage, aStatus)); if (ThreadTlv::FindTlvValueOffset(*aMessage, IPv6AddressesTlv::kIPv6Addresses, addressesOffset, addressesLength) == OT_ERROR_NONE) diff --git a/src/core/thread/network_data.cpp b/src/core/thread/network_data.cpp index 0fd78ca73..7dc6cd088 100644 --- a/src/core/thread/network_data.cpp +++ b/src/core/thread/network_data.cpp @@ -822,7 +822,7 @@ otError NetworkData::SendServerDataNotification(uint16_t aRloc16, Coap::Response if (aRloc16 != Mac::kShortAddrInvalid) { - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, ThreadTlv::kRloc16, aRloc16)); + SuccessOrExit(error = Tlv::Append(*message, aRloc16)); } IgnoreError(Get().GetLeaderAloc(messageInfo.GetPeerAddr())); diff --git a/src/core/thread/network_data_leader_ftd.cpp b/src/core/thread/network_data_leader_ftd.cpp index 03bb52fc8..c07db939f 100644 --- a/src/core/thread/network_data_leader_ftd.cpp +++ b/src/core/thread/network_data_leader_ftd.cpp @@ -146,7 +146,7 @@ void Leader::HandleServerData(Coap::Message &aMessage, const Ip6::MessageInfo &a VerifyOrExit(aMessageInfo.GetPeerAddr().GetIid().IsRoutingLocator()); - switch (Tlv::FindUint16Tlv(aMessage, ThreadTlv::kRloc16, rloc16)) + switch (Tlv::Find(aMessage, rloc16)) { case OT_ERROR_NONE: RemoveBorderRouter(rloc16, kMatchModeRloc16); @@ -157,7 +157,7 @@ void Leader::HandleServerData(Coap::Message &aMessage, const Ip6::MessageInfo &a ExitNow(); } - if (ThreadTlv::FindTlv(aMessage, ThreadTlv::kThreadNetworkData, sizeof(networkData), networkData) == OT_ERROR_NONE) + if (Tlv::FindTlv(aMessage, networkData) == OT_ERROR_NONE) { VerifyOrExit(networkData.IsValid()); RegisterNetworkData(aMessageInfo.GetPeerAddr().GetIid().GetLocator(), networkData.GetTlvs(), @@ -369,7 +369,7 @@ void Leader::SendCommissioningSetResponse(const Coap::Message & aRequest, SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); - SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, MeshCoP::Tlv::kState, static_cast(aState))); + SuccessOrExit(error = Tlv::Append(*message, aState)); SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index d94bcb69b..7c6ca346d 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -104,7 +104,7 @@ otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, if (aCount > 0) { - SuccessOrExit(error = Tlv::AppendTlv(*message, NetworkDataTlv::kTypeList, aTlvTypes, aCount)); + SuccessOrExit(error = Tlv::Append(*message, aTlvTypes, aCount)); } if (aDestination.IsLinkLocal() || aDestination.IsLinkLocalMulticast()) @@ -298,22 +298,21 @@ otError NetworkDiagnostic::FillRequestedTlvs(const Message & aRequest, switch (type) { case NetworkDiagnosticTlv::kExtMacAddress: - SuccessOrExit( - error = Tlv::AppendTlv(aResponse, type, &Get().GetExtAddress(), sizeof(Mac::ExtAddress))); + SuccessOrExit(error = Tlv::Append(aResponse, Get().GetExtAddress())); break; case NetworkDiagnosticTlv::kAddress16: - SuccessOrExit(error = Tlv::AppendUint16Tlv(aResponse, type, Get().GetRloc16())); + SuccessOrExit(error = Tlv::Append(aResponse, Get().GetRloc16())); break; case NetworkDiagnosticTlv::kMode: - SuccessOrExit(error = Tlv::AppendUint8Tlv(aResponse, type, Get().GetDeviceMode().Get())); + SuccessOrExit(error = Tlv::Append(aResponse, Get().GetDeviceMode().Get())); break; case NetworkDiagnosticTlv::kTimeout: if (!Get().IsRxOnWhenIdle()) { - SuccessOrExit(error = Tlv::AppendUint32Tlv(aResponse, type, Get().GetTimeout())); + SuccessOrExit(error = Tlv::Append(aResponse, Get().GetTimeout())); } break; @@ -360,7 +359,7 @@ otError NetworkDiagnostic::FillRequestedTlvs(const Message & aRequest, uint8_t length = sizeof(netData); IgnoreError(Get().GetNetworkData(/* aStableOnly */ false, netData, length)); - SuccessOrExit(error = Tlv::AppendTlv(aResponse, type, netData, length)); + SuccessOrExit(error = Tlv::Append(aResponse, netData, length)); break; } @@ -436,7 +435,7 @@ otError NetworkDiagnostic::FillRequestedTlvs(const Message & aRequest, if (Get().GetMaxChildTimeout(maxTimeout) == OT_ERROR_NONE) { - SuccessOrExit(error = Tlv::AppendUint32Tlv(aResponse, type, maxTimeout)); + SuccessOrExit(error = Tlv::Append(aResponse, maxTimeout)); } break; @@ -585,7 +584,7 @@ otError NetworkDiagnostic::SendDiagnosticReset(const Ip6::Address &aDestination, if (aCount > 0) { - SuccessOrExit(error = Tlv::AppendTlv(*message, NetworkDataTlv::kTypeList, aTlvTypes, aCount)); + SuccessOrExit(error = Tlv::Append(*message, aTlvTypes, aCount)); } if (aDestination.IsLinkLocal() || aDestination.IsLinkLocalMulticast()) @@ -743,25 +742,25 @@ otError NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, switch (tlv.GetType()) { case NetworkDiagnosticTlv::kExtMacAddress: - SuccessOrExit( - error = Tlv::ReadTlv(aMessage, offset, &aNetworkDiagTlv.mData.mExtAddress, sizeof(Mac::ExtAddress))); + SuccessOrExit(error = Tlv::Read( + aMessage, offset, static_cast(aNetworkDiagTlv.mData.mExtAddress))); break; case NetworkDiagnosticTlv::kAddress16: - SuccessOrExit(error = Tlv::ReadUint16Tlv(aMessage, offset, aNetworkDiagTlv.mData.mAddr16)); + SuccessOrExit(error = Tlv::Read(aMessage, offset, aNetworkDiagTlv.mData.mAddr16)); break; case NetworkDiagnosticTlv::kMode: { uint8_t mode; - SuccessOrExit(error = Tlv::ReadUint8Tlv(aMessage, offset, mode)); + SuccessOrExit(error = Tlv::Read(aMessage, offset, mode)); ParseMode(Mle::DeviceMode(mode), aNetworkDiagTlv.mData.mMode); break; } case NetworkDiagnosticTlv::kTimeout: - SuccessOrExit(error = Tlv::ReadUint32Tlv(aMessage, offset, aNetworkDiagTlv.mData.mTimeout)); + SuccessOrExit(error = Tlv::Read(aMessage, offset, aNetworkDiagTlv.mData.mTimeout)); break; case NetworkDiagnosticTlv::kConnectivity: @@ -840,11 +839,11 @@ otError NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, } case NetworkDiagnosticTlv::kBatteryLevel: - SuccessOrExit(error = Tlv::ReadUint8Tlv(aMessage, offset, aNetworkDiagTlv.mData.mBatteryLevel)); + SuccessOrExit(error = Tlv::Read(aMessage, offset, aNetworkDiagTlv.mData.mBatteryLevel)); break; case NetworkDiagnosticTlv::kSupplyVoltage: - SuccessOrExit(error = Tlv::ReadUint16Tlv(aMessage, offset, aNetworkDiagTlv.mData.mSupplyVoltage)); + SuccessOrExit(error = Tlv::Read(aMessage, offset, aNetworkDiagTlv.mData.mSupplyVoltage)); break; case NetworkDiagnosticTlv::kChildTable: @@ -876,7 +875,8 @@ otError NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, } case NetworkDiagnosticTlv::kMaxChildTimeout: - SuccessOrExit(error = Tlv::ReadUint32Tlv(aMessage, offset, aNetworkDiagTlv.mData.mMaxChildTimeout)); + SuccessOrExit(error = + Tlv::Read(aMessage, offset, aNetworkDiagTlv.mData.mMaxChildTimeout)); break; default: diff --git a/src/core/thread/network_diagnostic_tlvs.hpp b/src/core/thread/network_diagnostic_tlvs.hpp index 94d934b88..1bb44c3d6 100644 --- a/src/core/thread/network_diagnostic_tlvs.hpp +++ b/src/core/thread/network_diagnostic_tlvs.hpp @@ -118,12 +118,54 @@ public: } OT_TOOL_PACKED_END; +/** + * This class defines Extended MAC Address TLV constants and types. + * + */ +typedef SimpleTlvInfo ExtMacAddressTlv; + +/** + * This class defines Address16 TLV constants and types. + * + */ +typedef UintTlvInfo Address16Tlv; + +/** + * This class defines Mode TLV constants and types. + * + */ +typedef UintTlvInfo ModeTlv; + +/** + * This class defines Timeout TLV constants and types. + * + */ +typedef UintTlvInfo TimeoutTlv; + +/** + * This class defines Battery Level TLV constants and types. + * + */ +typedef UintTlvInfo BatteryLevelTlv; + +/** + * This class defines Supply Voltage TLV constants and types. + * + */ +typedef UintTlvInfo SupplyVoltageTlv; + +/** + * This class defines Max Child Timeout TLV constants and types. + * + */ +typedef UintTlvInfo MaxChildTimeoutTlv; + /** * This class implements Connectivity TLV generation and parsing. * */ OT_TOOL_PACKED_BEGIN -class ConnectivityTlv : public NetworkDiagnosticTlv +class ConnectivityTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** @@ -346,7 +388,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class RouteTlv : public NetworkDiagnosticTlv +class RouteTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** @@ -514,7 +556,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class LeaderDataTlv : public NetworkDiagnosticTlv +class LeaderDataTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** @@ -629,7 +671,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class NetworkDataTlv : public NetworkDiagnosticTlv +class NetworkDataTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** @@ -676,7 +718,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class Ip6AddressListTlv : public NetworkDiagnosticTlv +class Ip6AddressListTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** @@ -718,7 +760,7 @@ public: * */ OT_TOOL_PACKED_BEGIN -class MacCountersTlv : public NetworkDiagnosticTlv +class MacCountersTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** @@ -1020,7 +1062,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class ChildTableTlv : public NetworkDiagnosticTlv +class ChildTableTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** @@ -1089,7 +1131,7 @@ public: * */ OT_TOOL_PACKED_BEGIN -class ChannelPagesTlv : public NetworkDiagnosticTlv +class ChannelPagesTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** @@ -1132,7 +1174,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class TypeListTlv : public NetworkDiagnosticTlv +class TypeListTlv : public NetworkDiagnosticTlv, public TlvInfo { public: /** diff --git a/src/core/thread/panid_query_server.cpp b/src/core/thread/panid_query_server.cpp index 23b96f332..8d4b0823f 100644 --- a/src/core/thread/panid_query_server.cpp +++ b/src/core/thread/panid_query_server.cpp @@ -71,7 +71,7 @@ void PanIdQueryServer::HandleQuery(Coap::Message &aMessage, const Ip6::MessageIn VerifyOrExit(aMessage.IsPostRequest()); VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0); - SuccessOrExit(Tlv::FindUint16Tlv(aMessage, MeshCoP::Tlv::kPanId, panId)); + SuccessOrExit(Tlv::Find(aMessage, panId)); mChannelMask = mask; mCommissioner = aMessageInfo.GetPeerAddr(); @@ -124,7 +124,7 @@ void PanIdQueryServer::SendConflict(void) channelMask.SetChannelMask(mChannelMask); SuccessOrExit(error = channelMask.AppendTo(*message)); - SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kPanId, mPanId)); + SuccessOrExit(error = Tlv::Append(*message, mPanId)); messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(mCommissioner); diff --git a/src/core/thread/thread_tlvs.hpp b/src/core/thread/thread_tlvs.hpp index 99dd7a266..fd112a0c3 100644 --- a/src/core/thread/thread_tlvs.hpp +++ b/src/core/thread/thread_tlvs.hpp @@ -104,17 +104,65 @@ public: } OT_TOOL_PACKED_END; /** - * This class defines Status TLV constants. + * This class defines Target TLV constants and types. * */ -class ThreadStatusTlv +typedef SimpleTlvInfo ThreadTargetTlv; + +/** + * This class defines Extended MAC Address TLV constants and types. + * + */ +typedef SimpleTlvInfo ThreadExtMacAddressTlv; + +/** + * This class defines RLOC16 TLV constants and types. + * + */ +typedef UintTlvInfo ThreadRloc16Tlv; + +/** + * This class defines ML-EID TLV constants and types. + * + */ +typedef SimpleTlvInfo ThreadMeshLocalEidTlv; + +/** + * This class defines Time Since Last Transaction TLV constants and types. + * + */ +typedef UintTlvInfo ThreadLastTransactionTimeTlv; + +/** + * This class defines Timeout TLV constants and types. + * + */ +typedef UintTlvInfo ThreadTimeoutTlv; + +/** + * This class defines Network Name TLV constants and types. + * + */ +typedef TlvInfo ThreadNetworkNameTlv; + +/** + * This class defines Commissioner Session ID TLV constants and types. + * + */ +typedef UintTlvInfo ThreadCommissionerSessionIdTlv; + +/** + * This class defines Status TLV constants and types. + * + */ +class ThreadStatusTlv : public UintTlvInfo { public: /** * Status values. * */ - enum Status + enum Status : uint8_t { kSuccess = 0, ///< Success. kNoAddressAvailable = 1, ///< No address available. @@ -158,7 +206,7 @@ public: * This class implements Router Mask TLV generation and parsing. * */ -class ThreadRouterMaskTlv : public ThreadTlv +class ThreadRouterMaskTlv : public ThreadTlv, public TlvInfo { public: /** @@ -223,7 +271,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class ThreadNetworkDataTlv : public ThreadTlv +class ThreadNetworkDataTlv : public ThreadTlv, public TlvInfo { public: /** @@ -268,7 +316,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class IPv6AddressesTlv : public ThreadTlv +class IPv6AddressesTlv : public ThreadTlv, public TlvInfo { public: /**