diff --git a/src/core/common/encoding.hpp b/src/core/common/encoding.hpp index 981338513..d56c5aafd 100644 --- a/src/core/common/encoding.hpp +++ b/src/core/common/encoding.hpp @@ -167,6 +167,23 @@ inline uint64_t ReadUint64(const uint8_t *aBuffer) (static_cast(aBuffer[6]) << 8) | (static_cast(aBuffer[7]) << 0)); } +/** + * Reads a `UintType` integer value from a given buffer assuming big-endian encoding. + * + * @tparam UintType The unsigned int type. + * + * @param[in] aBuffer Pointer to the buffer to read from. + * + * @returns The `UintType` value read from the buffer. + * + */ +template UintType Read(const uint8_t *aBuffer); + +template <> inline uint8_t Read(const uint8_t *aBuffer) { return *aBuffer; } +template <> inline uint16_t Read(const uint8_t *aBuffer) { return ReadUint16(aBuffer); } +template <> inline uint32_t Read(const uint8_t *aBuffer) { return ReadUint32(aBuffer); } +template <> inline uint64_t Read(const uint8_t *aBuffer) { return ReadUint64(aBuffer); } + /** * Writes a `uint16_t` value to a given buffer using big-endian encoding. * @@ -228,6 +245,22 @@ inline void WriteUint64(uint64_t aValue, uint8_t *aBuffer) aBuffer[7] = (aValue >> 0) & 0xff; } +/** + * Writes a `UintType` integer value to a given buffer assuming big-endian encoding. + * + * @tparam UintType The unsigned int type. + * + * @param[in] aValue The value to write to buffer. + * @param[in] aBuffer Pointer to the buffer to write to. + * + */ +template void Write(UintType aValue, uint8_t *aBuffer); + +template <> inline void Write(uint8_t aValue, uint8_t *aBuffer) { *aBuffer = aValue; } +template <> inline void Write(uint16_t aValue, uint8_t *aBuffer) { WriteUint16(aValue, aBuffer); } +template <> inline void Write(uint32_t aValue, uint8_t *aBuffer) { WriteUint32(aValue, aBuffer); } +template <> inline void Write(uint64_t aValue, uint8_t *aBuffer) { WriteUint64(aValue, aBuffer); } + } // namespace BigEndian namespace LittleEndian { @@ -317,6 +350,23 @@ inline uint64_t ReadUint64(const uint8_t *aBuffer) (static_cast(aBuffer[6]) << 48) | (static_cast(aBuffer[7]) << 56)); } +/** + * Reads a `UintType` integer value from a given buffer assuming little-endian encoding. + * + * @tparam UintType The unsigned int type. + * + * @param[in] aBuffer Pointer to the buffer to read from. + * + * @returns The `UintType` value read from the buffer. + * + */ +template UintType Read(const uint8_t *aBuffer); + +template <> inline uint8_t Read(const uint8_t *aBuffer) { return *aBuffer; } +template <> inline uint16_t Read(const uint8_t *aBuffer) { return ReadUint16(aBuffer); } +template <> inline uint32_t Read(const uint8_t *aBuffer) { return ReadUint32(aBuffer); } +template <> inline uint64_t Read(const uint8_t *aBuffer) { return ReadUint64(aBuffer); } + /** * Writes a `uint16_t` value to a given buffer using little-endian encoding. * @@ -378,6 +428,22 @@ inline void WriteUint64(uint64_t aValue, uint8_t *aBuffer) aBuffer[7] = (aValue >> 56) & 0xff; } +/** + * Writes a `UintType` integer value to a given buffer assuming little-endian encoding. + * + * @tparam UintType The unsigned int type. + * + * @param[in] aValue The value to write to buffer. + * @param[in] aBuffer Pointer to the buffer to write to. + * + */ +template void Write(UintType aValue, uint8_t *aBuffer); + +template <> inline void Write(uint8_t aValue, uint8_t *aBuffer) { *aBuffer = aValue; } +template <> inline void Write(uint16_t aValue, uint8_t *aBuffer) { WriteUint16(aValue, aBuffer); } +template <> inline void Write(uint32_t aValue, uint8_t *aBuffer) { WriteUint32(aValue, aBuffer); } +template <> inline void Write(uint64_t aValue, uint8_t *aBuffer) { WriteUint64(aValue, aBuffer); } + } // namespace LittleEndian } // namespace ot diff --git a/src/core/common/tlvs.hpp b/src/core/common/tlvs.hpp index e72bf46ab..fd2d6791f 100644 --- a/src/core/common/tlvs.hpp +++ b/src/core/common/tlvs.hpp @@ -173,6 +173,74 @@ public: */ Error AppendTo(Message &aMessage) const; + /** + * Reads the value of TLV treating it as a given simple TLV type. + * + * This method requires the TLV to be already validated, in particular, its length MUST NOT be less than the + * required size of the value type. The TLV MUST NOT be extended. If these conditions are not met, the behavior of + * this method is undefined. + * + * @tparam SimpleTlvType The simple TLV type to read (must be a sub-class of `SimpleTlvInfo`). + * + * @returns The TLV value as `SimpleTlvType::ValueType`. + * + */ + template const typename SimpleTlvType::ValueType &ReadValueAs(void) const + { + return *reinterpret_cast(this + 1); + } + + /** + * Reads the value of TLV treating it as a given integer-value TLV type. + * + * This method requires the TLV to be already validated, in particular, its length MUST NOT be less than the + * required size of the value type. The TLV MUST NOT be extended. If these conditions are not met, the behavior of + * this method is undefined. + * + * @tparam UintTlvType The integer simple TLV type to read (must be a sub-class of `UintTlvInfo`). + * + * @returns The TLV value as `UintTlvInfo::UintValueType`. + * + */ + template typename UintTlvType::UintValueType ReadValueAs(void) const + { + return BigEndian::Read(reinterpret_cast(this + 1)); + } + + /** + * Writes the value of TLV treating it as a given simple TLV type. + * + * This method requires the TLV to be already validated, in particular, its length MUST NOT be less than the + * required size of the value type. The TLV MUST NOT be extended. If these conditions are not met, the behavior of + * this method is undefined. + * + * @tparam SimpleTlvType The simple TLV type to read (must be a sub-class of `SimpleTlvInfo`). + * + * @param[in] aValue The new TLV value. + * + */ + template void WriteValueAs(const typename SimpleTlvType::ValueType &aValue) + { + memcpy(this + 1, &aValue, sizeof(aValue)); + } + + /** + * Writes the value of TLV treating it as a given integer-value TLV type. + * + * This method requires the TLV to be already validated, in particular, its length MUST NOT be less than the + * required size of the value type. The TLV MUST NOT be extended. If these conditions are not met, the behavior of + * this method is undefined. + * + * @tparam UintTlvType The integer simple TLV type to read (must be a sub-class of `UintTlvInfo`). + * + * @param[in] aValue The new TLV value. + * + */ + template void WriteValueAs(typename UintTlvType::UintValueType aValue) + { + return BigEndian::Write(aValue, reinterpret_cast(this + 1)); + } + //------------------------------------------------------------------------------------------------------------------ // Static methods for reading/finding/appending TLVs in a `Message`. diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index 1e032eafc..3e47300f4 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -190,7 +190,7 @@ void Dataset::ConvertTo(Info &aDatasetInfo) const switch (cur->GetType()) { case Tlv::kActiveTimestamp: - aDatasetInfo.SetActiveTimestamp(As(cur)->GetTimestamp()); + aDatasetInfo.SetActiveTimestamp(cur->ReadValueAs()); break; case Tlv::kChannel: @@ -210,19 +210,19 @@ void Dataset::ConvertTo(Info &aDatasetInfo) const } case Tlv::kDelayTimer: - aDatasetInfo.SetDelay(As(cur)->GetDelayTimer()); + aDatasetInfo.SetDelay(cur->ReadValueAs()); break; case Tlv::kExtendedPanId: - aDatasetInfo.SetExtendedPanId(As(cur)->GetExtendedPanId()); + aDatasetInfo.SetExtendedPanId(cur->ReadValueAs()); break; case Tlv::kMeshLocalPrefix: - aDatasetInfo.SetMeshLocalPrefix(As(cur)->GetMeshLocalPrefix()); + aDatasetInfo.SetMeshLocalPrefix(cur->ReadValueAs()); break; case Tlv::kNetworkKey: - aDatasetInfo.SetNetworkKey(As(cur)->GetNetworkKey()); + aDatasetInfo.SetNetworkKey(cur->ReadValueAs()); break; case Tlv::kNetworkName: @@ -230,15 +230,15 @@ void Dataset::ConvertTo(Info &aDatasetInfo) const break; case Tlv::kPanId: - aDatasetInfo.SetPanId(As(cur)->GetPanId()); + aDatasetInfo.SetPanId(cur->ReadValueAs()); break; case Tlv::kPendingTimestamp: - aDatasetInfo.SetPendingTimestamp(As(cur)->GetTimestamp()); + aDatasetInfo.SetPendingTimestamp(cur->ReadValueAs()); break; case Tlv::kPskc: - aDatasetInfo.SetPskc(As(cur)->GetPskc()); + aDatasetInfo.SetPskc(cur->ReadValueAs()); break; case Tlv::kSecurityPolicy: @@ -366,21 +366,20 @@ Error Dataset::SetFrom(const Info &aDatasetInfo) Error Dataset::GetTimestamp(Type aType, Timestamp &aTimestamp) const { - Error error = kErrorNone; + Error error = kErrorNone; + const Tlv *tlv; if (aType == kActive) { - const ActiveTimestampTlv *tlv = GetTlv(); - + tlv = GetTlv(Tlv::kActiveTimestamp); VerifyOrExit(tlv != nullptr, error = kErrorNotFound); - aTimestamp = tlv->GetTimestamp(); + aTimestamp = tlv->ReadValueAs(); } else { - const PendingTimestampTlv *tlv = GetTlv(); - + tlv = GetTlv(Tlv::kPendingTimestamp); VerifyOrExit(tlv != nullptr, error = kErrorNotFound); - aTimestamp = tlv->GetTimestamp(); + aTimestamp = tlv->ReadValueAs(); } exit: @@ -479,19 +478,19 @@ Error Dataset::AppendMleDatasetTlv(Type aType, Message &aMessage) const } else if (cur->GetType() == Tlv::kDelayTimer) { - uint32_t elapsed = TimerMilli::GetNow() - mUpdateTime; - DelayTimerTlv delayTimer = *As(cur); + uint32_t elapsed = TimerMilli::GetNow() - mUpdateTime; + uint32_t delayTimer = cur->ReadValueAs(); - if (delayTimer.GetDelayTimer() > elapsed) + if (delayTimer > elapsed) { - delayTimer.SetDelayTimer(delayTimer.GetDelayTimer() - elapsed); + delayTimer -= elapsed; } else { - delayTimer.SetDelayTimer(0); + delayTimer = 0; } - SuccessOrExit(error = delayTimer.AppendTo(aMessage)); + SuccessOrExit(error = Tlv::Append(aMessage, delayTimer)); } else { @@ -545,11 +544,11 @@ Error Dataset::ApplyConfiguration(Instance &aInstance, bool *aIsNetworkKeyUpdate } case Tlv::kPanId: - mac.SetPanId(As(cur)->GetPanId()); + mac.SetPanId(cur->ReadValueAs()); break; case Tlv::kExtendedPanId: - aInstance.Get().SetExtPanId(As(cur)->GetExtendedPanId()); + aInstance.Get().SetExtPanId(cur->ReadValueAs()); break; case Tlv::kNetworkName: @@ -558,30 +557,29 @@ Error Dataset::ApplyConfiguration(Instance &aInstance, bool *aIsNetworkKeyUpdate case Tlv::kNetworkKey: { - const NetworkKeyTlv *key = As(cur); - NetworkKey networkKey; + NetworkKey networkKey; keyManager.GetNetworkKey(networkKey); - if (aIsNetworkKeyUpdated && (key->GetNetworkKey() != networkKey)) + if (aIsNetworkKeyUpdated && (cur->ReadValueAs() != networkKey)) { *aIsNetworkKeyUpdated = true; } - keyManager.SetNetworkKey(key->GetNetworkKey()); + keyManager.SetNetworkKey(cur->ReadValueAs()); break; } #if OPENTHREAD_FTD case Tlv::kPskc: - keyManager.SetPskc(As(cur)->GetPskc()); + keyManager.SetPskc(cur->ReadValueAs()); break; #endif case Tlv::kMeshLocalPrefix: - aInstance.Get().SetMeshLocalPrefix(As(cur)->GetMeshLocalPrefix()); + aInstance.Get().SetMeshLocalPrefix(cur->ReadValueAs()); break; case Tlv::kSecurityPolicy: diff --git a/src/core/meshcop/dataset_local.cpp b/src/core/meshcop/dataset_local.cpp index 0955bf175..11010c49e 100644 --- a/src/core/meshcop/dataset_local.cpp +++ b/src/core/meshcop/dataset_local.cpp @@ -90,9 +90,7 @@ exit: Error DatasetLocal::Read(Dataset &aDataset) const { - DelayTimerTlv *delayTimer; - uint32_t elapsed; - Error error; + Error error; error = Get().ReadOperationalDataset(mType, aDataset); VerifyOrExit(error == kErrorNone, aDataset.mLength = 0); @@ -108,19 +106,25 @@ Error DatasetLocal::Read(Dataset &aDataset) const } else { - delayTimer = aDataset.GetTlv(); - VerifyOrExit(delayTimer); + uint32_t elapsed; + uint32_t delayTimer; + Tlv *tlv = aDataset.GetTlv(Tlv::kDelayTimer); - elapsed = TimerMilli::GetNow() - mUpdateTime; + VerifyOrExit(tlv != nullptr); - if (delayTimer->GetDelayTimer() > elapsed) + elapsed = TimerMilli::GetNow() - mUpdateTime; + delayTimer = tlv->ReadValueAs(); + + if (delayTimer > elapsed) { - delayTimer->SetDelayTimer(delayTimer->GetDelayTimer() - elapsed); + delayTimer -= elapsed; } else { - delayTimer->SetDelayTimer(0); + delayTimer = 0; } + + tlv->WriteValueAs(delayTimer); } aDataset.mUpdateTime = TimerMilli::GetNow(); diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 3e08889ea..034985d90 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -738,41 +738,42 @@ exit: void PendingDatasetManager::StartDelayTimer(void) { - DelayTimerTlv *delayTimer; - Dataset dataset; + Tlv *tlv; + uint32_t delay; + Dataset dataset; IgnoreError(Read(dataset)); mDelayTimer.Stop(); - if ((delayTimer = dataset.GetTlv()) != nullptr) - { - uint32_t delay = delayTimer->GetDelayTimer(); + tlv = dataset.GetTlv(Tlv::kDelayTimer); + VerifyOrExit(tlv != nullptr); - // the Timer implementation does not support the full 32 bit range - if (delay > Timer::kMaxDelay) - { - delay = Timer::kMaxDelay; - } + delay = tlv->ReadValueAs(); - mDelayTimer.StartAt(dataset.GetUpdateTime(), delay); - LogInfo("delay timer started %lu", ToUlong(delay)); - } + // the Timer implementation does not support the full 32 bit range + delay = Min(delay, Timer::kMaxDelay); + + mDelayTimer.StartAt(dataset.GetUpdateTime(), delay); + LogInfo("delay timer started %lu", ToUlong(delay)); + +exit: + return; } void PendingDatasetManager::HandleDelayTimer(void) { - DelayTimerTlv *delayTimer; - Dataset dataset; + Tlv *tlv; + Dataset dataset; IgnoreError(Read(dataset)); // if the Delay Timer value is larger than what our Timer implementation can handle, we have to compute // the remainder and wait some more. - if ((delayTimer = dataset.GetTlv()) != nullptr) + if ((tlv = dataset.GetTlv(Tlv::kDelayTimer)) != nullptr) { uint32_t elapsed = mDelayTimer.GetFireTime() - dataset.GetUpdateTime(); - uint32_t delay = delayTimer->GetDelayTimer(); + uint32_t delay = tlv->ReadValueAs(); if (elapsed < delay) { diff --git a/src/core/meshcop/dataset_manager.hpp b/src/core/meshcop/dataset_manager.hpp index 6637f002c..36162a315 100644 --- a/src/core/meshcop/dataset_manager.hpp +++ b/src/core/meshcop/dataset_manager.hpp @@ -182,6 +182,12 @@ public: #endif protected: + /** + * Default Delay Timer value for a Pending Operational Dataset (ms) + * + */ + static constexpr uint32_t kDefaultDelayTimer = OPENTHREAD_CONFIG_TMF_PENDING_DATASET_DEFAULT_DELAY; + /** * Defines a generic Dataset TLV to read from a message. * diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index 5a2fd2176..73759ecf3 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -203,16 +203,18 @@ Error DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInfo case Tlv::kDelayTimer: { - DelayTimerTlv &delayTimerTlv = As(datasetTlv); + uint32_t delayTimer = datasetTlv.ReadValueAs(); - if (doesAffectNetworkKey && delayTimerTlv.GetDelayTimer() < DelayTimerTlv::kDelayTimerDefault) + if (doesAffectNetworkKey && delayTimer < kDefaultDelayTimer) { - delayTimerTlv.SetDelayTimer(DelayTimerTlv::kDelayTimerDefault); + delayTimer = kDefaultDelayTimer; } - else if (delayTimerTlv.GetDelayTimer() < Get().GetDelayTimerMinimal()) + else { - delayTimerTlv.SetDelayTimer(Get().GetDelayTimerMinimal()); + delayTimer = Max(delayTimer, Get().GetDelayTimerMinimal()); } + + datasetTlv.WriteValueAs(delayTimer); } OT_FALL_THROUGH; diff --git a/src/core/meshcop/dataset_updater.cpp b/src/core/meshcop/dataset_updater.cpp index 4eb8116ec..e34f2205d 100644 --- a/src/core/meshcop/dataset_updater.cpp +++ b/src/core/meshcop/dataset_updater.cpp @@ -143,9 +143,10 @@ void DatasetUpdater::PreparePendingDataset(void) } { - ActiveTimestampTlv *tlv = dataset.GetTlv(); + Timestamp timestamp = dataset.GetTlv(Tlv::kActiveTimestamp)->ReadValueAs(); - tlv->GetTimestamp().AdvanceRandomTicks(); + timestamp.AdvanceRandomTicks(); + dataset.SetTimestamp(Dataset::kActive, timestamp); } SuccessOrExit(error = Get().Save(dataset)); diff --git a/src/core/meshcop/meshcop_leader.cpp b/src/core/meshcop/meshcop_leader.cpp index f2fe3c0f6..64c028d1b 100644 --- a/src/core/meshcop/meshcop_leader.cpp +++ b/src/core/meshcop/meshcop_leader.cpp @@ -58,7 +58,7 @@ RegisterLogModule("MeshCoPLeader"); Leader::Leader(Instance &aInstance) : InstanceLocator(aInstance) , mTimer(aInstance) - , mDelayTimerMinimal(DelayTimerTlv::kDelayTimerMinimal) + , mDelayTimerMinimal(kMinDelayTimer) , mSessionId(Random::NonCrypto::GetUint16()) { } @@ -218,16 +218,13 @@ Error Leader::SetDelayTimerMinimal(uint32_t aDelayTimerMinimal) { Error error = kErrorNone; - VerifyOrExit((aDelayTimerMinimal != 0 && aDelayTimerMinimal < DelayTimerTlv::kDelayTimerDefault), - error = kErrorInvalidArgs); + VerifyOrExit((aDelayTimerMinimal != 0 && aDelayTimerMinimal < kMinDelayTimer), error = kErrorInvalidArgs); mDelayTimerMinimal = aDelayTimerMinimal; exit: return error; } -uint32_t Leader::GetDelayTimerMinimal(void) const { return mDelayTimerMinimal; } - void Leader::HandleTimer(void) { VerifyOrExit(Get().IsLeader()); diff --git a/src/core/meshcop/meshcop_leader.hpp b/src/core/meshcop/meshcop_leader.hpp index 2cf6de13d..50436b3c9 100644 --- a/src/core/meshcop/meshcop_leader.hpp +++ b/src/core/meshcop/meshcop_leader.hpp @@ -95,7 +95,7 @@ public: * @retval the minimal delay timer (in ms). * */ - uint32_t GetDelayTimerMinimal(void) const; + uint32_t GetDelayTimerMinimal(void) const { return mDelayTimerMinimal; } /** * Sets empty Commissioner Data TLV in the Thread Network Data. @@ -104,6 +104,7 @@ public: void SetEmptyCommissionerData(void); private: + static constexpr uint32_t kMinDelayTimer = OPENTHREAD_CONFIG_TMF_PENDING_DATASET_MINIMUM_DELAY; // (msec) static constexpr uint32_t kTimeoutLeaderPetition = 50; // TIMEOUT_LEAD_PET (seconds) OT_TOOL_PACKED_BEGIN diff --git a/src/core/meshcop/meshcop_tlvs.cpp b/src/core/meshcop/meshcop_tlvs.cpp index 04063dfa5..446846f45 100644 --- a/src/core/meshcop/meshcop_tlvs.cpp +++ b/src/core/meshcop/meshcop_tlvs.cpp @@ -45,51 +45,52 @@ namespace MeshCoP { bool Tlv::IsValid(const Tlv &aTlv) { - bool rval = true; + bool isValid = true; + uint8_t minLength = 0; switch (aTlv.GetType()) { - case Tlv::kChannel: - rval = As(aTlv).IsValid(); - break; - case Tlv::kPanId: - rval = As(aTlv).IsValid(); + minLength = sizeof(PanIdTlv::UintValueType); break; - case Tlv::kExtendedPanId: - rval = As(aTlv).IsValid(); + minLength = sizeof(ExtendedPanIdTlv::ValueType); break; - - case Tlv::kNetworkName: - rval = As(aTlv).IsValid(); - break; - - case Tlv::kNetworkKey: - rval = As(aTlv).IsValid(); - break; - case Tlv::kPskc: - rval = As(aTlv).IsValid(); + minLength = sizeof(PskcTlv::ValueType); + break; + case Tlv::kNetworkKey: + minLength = sizeof(NetworkKeyTlv::ValueType); + break; + case Tlv::kMeshLocalPrefix: + minLength = sizeof(MeshLocalPrefixTlv::ValueType); break; - case Tlv::kMeshLocalPrefix: - rval = As(aTlv).IsValid(); + case Tlv::kChannel: + isValid = As(aTlv).IsValid(); + break; + case Tlv::kNetworkName: + isValid = As(aTlv).IsValid(); break; case Tlv::kSecurityPolicy: - rval = As(aTlv).IsValid(); + isValid = As(aTlv).IsValid(); break; case Tlv::kChannelMask: - rval = As(aTlv).IsValid(); + isValid = As(aTlv).IsValid(); break; default: break; } - return rval; + if (minLength > 0) + { + isValid = (aTlv.GetLength() >= minLength); + } + + return isValid; } NameData NetworkNameTlv::GetNetworkName(void) const diff --git a/src/core/meshcop/meshcop_tlvs.hpp b/src/core/meshcop/meshcop_tlvs.hpp index 32121bdb5..dba0b83b1 100644 --- a/src/core/meshcop/meshcop_tlvs.hpp +++ b/src/core/meshcop/meshcop_tlvs.hpp @@ -316,98 +316,16 @@ private: } OT_TOOL_PACKED_END; /** - * Implements PAN ID TLV generation and parsing. + * Defines PAN ID TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class PanIdTlv : public Tlv, public UintTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kPanId); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Returns the PAN ID value. - * - * @returns The PAN ID value. - * - */ - uint16_t GetPanId(void) const { return BigEndian::HostSwap16(mPanId); } - - /** - * Sets the PAN ID value. - * - * @param[in] aPanId The PAN ID value. - * - */ - void SetPanId(uint16_t aPanId) { mPanId = BigEndian::HostSwap16(aPanId); } - -private: - uint16_t mPanId; -} OT_TOOL_PACKED_END; +typedef UintTlvInfo PanIdTlv; /** - * Implements Extended PAN ID TLV generation and parsing. + * Defines Extended PAN ID TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class ExtendedPanIdTlv : public Tlv, public SimpleTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kExtendedPanId); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Returns the Extended PAN ID value. - * - * @returns The Extended PAN ID value. - * - */ - const ExtendedPanId &GetExtendedPanId(void) const { return mExtendedPanId; } - - /** - * Sets the Extended PAN ID value. - * - * @param[in] aExtendedPanId An Extended PAN ID value. - * - */ - void SetExtendedPanId(const ExtendedPanId &aExtendedPanId) { mExtendedPanId = aExtendedPanId; } - -private: - ExtendedPanId mExtendedPanId; -} OT_TOOL_PACKED_END; +typedef SimpleTlvInfo ExtendedPanIdTlv; /** * Implements Network Name TLV generation and parsing. @@ -457,203 +375,28 @@ private: } OT_TOOL_PACKED_END; /** - * Implements PSKc TLV generation and parsing. + * Defines PSKc TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class PskcTlv : public Tlv, public SimpleTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kPskc); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Returns the PSKc value. - * - * @returns The PSKc value. - * - */ - const Pskc &GetPskc(void) const { return mPskc; } - - /** - * Sets the PSKc value. - * - * @param[in] aPskc A pointer to the PSKc value. - * - */ - void SetPskc(const Pskc &aPskc) { mPskc = aPskc; } - -private: - Pskc mPskc; -} OT_TOOL_PACKED_END; +typedef SimpleTlvInfo PskcTlv; /** - * Implements Network Network Key TLV generation and parsing. + * Defines Network Key TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class NetworkKeyTlv : public Tlv, public SimpleTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kNetworkKey); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Returns the Network Network Key value. - * - * @returns The Network Network Key value. - * - */ - const NetworkKey &GetNetworkKey(void) const { return mNetworkKey; } - - /** - * Sets the Network Network Key value. - * - * @param[in] aNetworkKey The Network Network Key. - * - */ - void SetNetworkKey(const NetworkKey &aNetworkKey) { mNetworkKey = aNetworkKey; } - -private: - NetworkKey mNetworkKey; -} OT_TOOL_PACKED_END; +typedef SimpleTlvInfo NetworkKeyTlv; /** - * Implements Network Key Sequence TLV generation and parsing. + * Defines Network Key Sequence TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class NetworkKeySequenceTlv : public Tlv, public UintTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kNetworkKeySequence); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Returns the Network Key Sequence value. - * - * @returns The Network Key Sequence value. - * - */ - uint32_t GetNetworkKeySequence(void) const { return BigEndian::HostSwap32(mNetworkKeySequence); } - - /** - * Sets the Network Key Sequence value. - * - * @param[in] aNetworkKeySequence The Network Key Sequence value. - * - */ - void SetNetworkKeySequence(uint32_t aNetworkKeySequence) - { - mNetworkKeySequence = BigEndian::HostSwap32(aNetworkKeySequence); - } - -private: - uint32_t mNetworkKeySequence; -} OT_TOOL_PACKED_END; +typedef UintTlvInfo NetworkKeySequenceTlv; /** - * Implements Mesh Local Prefix TLV generation and parsing. + * Defines Mesh Local Prefix TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class MeshLocalPrefixTlv : public Tlv, public SimpleTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kMeshLocalPrefix); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Returns the size (in bytes) of the Mesh Local Prefix field. - * - * @returns The size (in bytes) of the Mesh Local Prefix field (8 bytes). - * - */ - uint8_t GetMeshLocalPrefixLength(void) const { return sizeof(mMeshLocalPrefix); } - - /** - * Returns the Mesh Local Prefix value. - * - * @returns The Mesh Local Prefix value. - * - */ - const Ip6::NetworkPrefix &GetMeshLocalPrefix(void) const { return mMeshLocalPrefix; } - - /** - * Sets the Mesh Local Prefix value. - * - * @param[in] aMeshLocalPrefix A pointer to the Mesh Local Prefix value. - * - */ - void SetMeshLocalPrefix(const Ip6::NetworkPrefix &aMeshLocalPrefix) { mMeshLocalPrefix = aMeshLocalPrefix; } - -private: - Ip6::NetworkPrefix mMeshLocalPrefix; -} OT_TOOL_PACKED_END; +typedef SimpleTlvInfo MeshLocalPrefixTlv; class SteeringData; @@ -868,60 +611,10 @@ private: } OT_TOOL_PACKED_END; /** - * Implements Active Timestamp TLV generation and parsing. + * Defines Active Timestamp TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class ActiveTimestampTlv : public Tlv, public SimpleTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kActiveTimestamp); - SetLength(sizeof(*this) - sizeof(Tlv)); - mTimestamp.Clear(); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Gets the timestamp. - * - * @returns The timestamp. - * - */ - const Timestamp &GetTimestamp(void) const { return mTimestamp; } - - /** - * Returns a reference to the timestamp. - * - * @returns A reference to the timestamp. - * - */ - Timestamp &GetTimestamp(void) { return mTimestamp; } - - /** - * Sets the timestamp. - * - * @param[in] aTimestamp The new timestamp. - * - */ - void SetTimestamp(const Timestamp &aTimestamp) { mTimestamp = aTimestamp; } - -private: - Timestamp mTimestamp; -} OT_TOOL_PACKED_END; +typedef SimpleTlvInfo ActiveTimestampTlv; /** * Implements State TLV generation and parsing. @@ -955,168 +648,22 @@ public: }; /** - * Implements Joiner UDP Port TLV generation and parsing. + * Defines Joiner UDP Port TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class JoinerUdpPortTlv : public Tlv, public UintTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kJoinerUdpPort); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Returns the UDP Port value. - * - * @returns The UDP Port value. - * - */ - uint16_t GetUdpPort(void) const { return BigEndian::HostSwap16(mUdpPort); } - - /** - * Sets the UDP Port value. - * - * @param[in] aUdpPort The UDP Port value. - * - */ - void SetUdpPort(uint16_t aUdpPort) { mUdpPort = BigEndian::HostSwap16(aUdpPort); } - -private: - uint16_t mUdpPort; -} OT_TOOL_PACKED_END; +typedef UintTlvInfo JoinerUdpPortTlv; /** - * Implements Pending Timestamp TLV generation and parsing. + * Defines Pending Timestamp TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class PendingTimestampTlv : public Tlv, public SimpleTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kPendingTimestamp); - SetLength(sizeof(*this) - sizeof(Tlv)); - mTimestamp.Clear(); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Gets the timestamp. - * - * @returns The timestamp. - * - */ - const Timestamp &GetTimestamp(void) const { return mTimestamp; } - - /** - * Returns a reference to the timestamp. - * - * @returns A reference to the timestamp. - * - */ - Timestamp &GetTimestamp(void) { return mTimestamp; } - - /** - * Sets the timestamp. - * - * @param[in] aTimestamp The new timestamp. - * - */ - void SetTimestamp(const Timestamp &aTimestamp) { mTimestamp = aTimestamp; } - -private: - Timestamp mTimestamp; -} OT_TOOL_PACKED_END; +typedef SimpleTlvInfo PendingTimestampTlv; /** - * Implements Delay Timer TLV generation and parsing. + * Defines Delay Timer TLV constants and types. * */ -OT_TOOL_PACKED_BEGIN -class DelayTimerTlv : public Tlv, public UintTlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kDelayTimer); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - * - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Returns the Delay Timer value. - * - * @returns The Delay Timer value. - * - */ - uint32_t GetDelayTimer(void) const { return BigEndian::HostSwap32(mDelayTimer); } - - /** - * Sets the Delay Timer value. - * - * @param[in] aDelayTimer The Delay Timer value. - * - */ - void SetDelayTimer(uint32_t aDelayTimer) { mDelayTimer = BigEndian::HostSwap32(aDelayTimer); } - - static constexpr uint32_t kMaxDelayTimer = 259200; ///< Maximum delay timer value for a Pending Dataset in seconds - - /** - * Minimum Delay Timer value for a Pending Operational Dataset (ms) - * - */ - static constexpr uint32_t kDelayTimerMinimal = OPENTHREAD_CONFIG_TMF_PENDING_DATASET_MINIMUM_DELAY; - - /** - * Default Delay Timer value for a Pending Operational Dataset (ms) - * - */ - static constexpr uint32_t kDelayTimerDefault = OPENTHREAD_CONFIG_TMF_PENDING_DATASET_DEFAULT_DELAY; - -private: - uint32_t mDelayTimer; -} OT_TOOL_PACKED_END; +typedef UintTlvInfo DelayTimerTlv; // forward declare ChannelMaskTlv class ChannelMaskTlv;