Skip to content

Commit 494a486

Browse files
authored
[net-diag] convert MAC and MLE counters TLVs to SimpleTlvInfo (openthread#13157)
This commit updates `MacCountersTlv` and `MleCountersTlv` to use the `SimpleTlvInfo` template. The original classes are replaced with `MacCountersTlvValue` and `MleCountersTlvValue` which only represent the TLV values. This helps simplify the TLV parsing and appending logic and more importantly allows the TLV value formats to be reused.
1 parent 0693bce commit 494a486

3 files changed

Lines changed: 39 additions & 54 deletions

File tree

src/core/thread/net_diag.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,19 @@ Error Server::AppendDiagTlv(uint8_t aTlvType, Message &aMessage)
371371

372372
case Tlv::kMacCounters:
373373
{
374-
MacCountersTlv tlv;
374+
MacCountersTlvValue tlvValue;
375375

376-
tlv.Init(Get<Mac::Mac>().GetCounters());
377-
error = tlv.AppendTo(aMessage);
376+
tlvValue.InitFrom(Get<Mac::Mac>().GetCounters());
377+
error = Tlv::Append<MacCountersTlv>(aMessage, tlvValue);
378378
break;
379379
}
380380

381381
case Tlv::kMleCounters:
382382
{
383-
MleCountersTlv tlv;
383+
MleCountersTlvValue tlvValue;
384384

385-
tlv.Init(Get<Mle::Mle>().GetCounters());
386-
error = tlv.AppendTo(aMessage);
385+
tlvValue.InitFrom(Get<Mle::Mle>().GetCounters());
386+
error = Tlv::Append<MleCountersTlv>(aMessage, tlvValue);
387387
break;
388388
}
389389

@@ -1195,21 +1195,19 @@ Error Client::GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator,
11951195

11961196
case Tlv::kMacCounters:
11971197
{
1198-
MacCountersTlv macCountersTlv;
1198+
MacCountersTlvValue tlvValue;
11991199

1200-
SuccessOrExit(error = aMessage.Read(offset, macCountersTlv));
1201-
VerifyOrExit(macCountersTlv.IsValid(), error = kErrorParse);
1202-
macCountersTlv.Read(aDiagTlv.mData.mMacCounters);
1200+
SuccessOrExit(error = tlvInfo.Read<MacCountersTlv>(aMessage, tlvValue));
1201+
tlvValue.Read(aDiagTlv.mData.mMacCounters);
12031202
break;
12041203
}
12051204

12061205
case Tlv::kMleCounters:
12071206
{
1208-
MleCountersTlv mleCoutersTlv;
1207+
MleCountersTlvValue tlvValue;
12091208

1210-
SuccessOrExit(error = aMessage.Read(offset, mleCoutersTlv));
1211-
VerifyOrExit(mleCoutersTlv.IsValid(), error = kErrorParse);
1212-
mleCoutersTlv.Read(aDiagTlv.mData.mMleCounters);
1209+
SuccessOrExit(error = tlvInfo.Read<MleCountersTlv>(aMessage, tlvValue));
1210+
tlvValue.Read(aDiagTlv.mData.mMleCounters);
12131211
break;
12141212
}
12151213

src/core/thread/net_diag_tlvs.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,10 @@ void AnswerTlvValue::Init(uint16_t aIndex, IsLastFlag aIsLastFlag)
214214
}
215215

216216
//---------------------------------------------------------------------------------------------------------------------
217-
// MacCountersTlv
217+
// MacCountersTlvValue
218218

219-
void MacCountersTlv::Init(const Mac::Counters &aMacCounters)
219+
void MacCountersTlvValue::InitFrom(const Mac::Counters &aMacCounters)
220220
{
221-
SetType(kMacCounters);
222-
SetLength(sizeof(*this) - sizeof(Tlv));
223-
224221
mIfInUnknownProtos = BigEndian::HostSwap32(aMacCounters.mRxOther);
225222
mIfInErrors = BigEndian::HostSwap32(aMacCounters.mRxErrNoFrame + aMacCounters.mRxErrUnknownNeighbor +
226223
aMacCounters.mRxErrInvalidSrcAddr + aMacCounters.mRxErrSec +
@@ -235,7 +232,7 @@ void MacCountersTlv::Init(const Mac::Counters &aMacCounters)
235232
mIfOutDiscards = BigEndian::HostSwap32(aMacCounters.mTxErrBusyChannel);
236233
}
237234

238-
void MacCountersTlv::Read(MacCounters &aDiagMacCounters) const
235+
void MacCountersTlvValue::Read(MacCounters &aDiagMacCounters) const
239236
{
240237
aDiagMacCounters.mIfInUnknownProtos = BigEndian::HostSwap32(mIfInUnknownProtos);
241238
aDiagMacCounters.mIfInErrors = BigEndian::HostSwap32(mIfInErrors);
@@ -249,13 +246,10 @@ void MacCountersTlv::Read(MacCounters &aDiagMacCounters) const
249246
}
250247

251248
//---------------------------------------------------------------------------------------------------------------------
252-
// MleCountersTlv
249+
// MleCountersTlvValue
253250

254-
void MleCountersTlv::Init(const Mle::Counters &aMleCounters)
251+
void MleCountersTlvValue::InitFrom(const Mle::Counters &aMleCounters)
255252
{
256-
SetType(kMleCounters);
257-
SetLength(sizeof(*this) - sizeof(Tlv));
258-
259253
mDisabledRole = BigEndian::HostSwap16(aMleCounters.mDisabledRole);
260254
mDetachedRole = BigEndian::HostSwap16(aMleCounters.mDetachedRole);
261255
mChildRole = BigEndian::HostSwap16(aMleCounters.mChildRole);
@@ -273,7 +267,7 @@ void MleCountersTlv::Init(const Mle::Counters &aMleCounters)
273267
mLeaderTime = BigEndian::HostSwap64(aMleCounters.mLeaderTime);
274268
}
275269

276-
void MleCountersTlv::Read(MleCounters &aDiagMleCounters) const
270+
void MleCountersTlvValue::Read(MleCounters &aDiagMleCounters) const
277271
{
278272
aDiagMleCounters.mDisabledRole = BigEndian::HostSwap16(mDisabledRole);
279273
aDiagMleCounters.mDetachedRole = BigEndian::HostSwap16(mDetachedRole);

src/core/thread/net_diag_tlvs.hpp

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -330,31 +330,23 @@ typedef SimpleTlvInfo<Tlv::kLeaderData, LeaderDataTlvValue> LeaderDataTlv;
330330
typedef otNetworkDiagMacCounters MacCounters;
331331

332332
/**
333-
* Implements Mac Counters TLV generation and parsing.
333+
* Implements Mac Counters TLV value generation and parsing.
334334
*/
335335
OT_TOOL_PACKED_BEGIN
336-
class MacCountersTlv : public Tlv, public TlvInfo<Tlv::kMacCounters>
336+
class MacCountersTlvValue
337337
{
338338
public:
339339
/**
340-
* Initializes the TLV.
340+
* Initializes the TLV value.
341341
*
342342
* @param[in] aMacCounters The MAC counters to initialize the TLV with.
343343
*/
344-
void Init(const Mac::Counters &aMacCounters);
345-
346-
/**
347-
* Indicates whether or not the TLV appears to be well-formed.
348-
*
349-
* @retval TRUE If the TLV appears to be well-formed.
350-
* @retval FALSE If the TLV does not appear to be well-formed.
351-
*/
352-
bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); }
344+
void InitFrom(const Mac::Counters &aMacCounters);
353345

354346
/**
355-
* Reads the counters from TLV.
347+
* Reads the counters from the TLV value.
356348
*
357-
* @param[out] aDiagMacCounters A reference to `NetDiag::MacCounters` to populate.
349+
* @param[out] aDiagMacCounters A reference to `MacCounters` to populate.
358350
*/
359351
void Read(MacCounters &aDiagMacCounters) const;
360352

@@ -370,6 +362,11 @@ class MacCountersTlv : public Tlv, public TlvInfo<Tlv::kMacCounters>
370362
uint32_t mIfOutDiscards;
371363
} OT_TOOL_PACKED_END;
372364

365+
/**
366+
* Defines Mac Counters TLV constants and types.
367+
*/
368+
typedef SimpleTlvInfo<Tlv::kMacCounters, MacCountersTlvValue> MacCountersTlv;
369+
373370
/**
374371
* Implements Child Table TLV Entry generation and parsing.
375372
*/
@@ -870,30 +867,21 @@ typedef SimpleTlvInfo<Tlv::kAnswer, AnswerTlvValue> AnswerTlv;
870867
typedef otNetworkDiagMleCounters MleCounters;
871868

872869
/**
873-
* Implements MLE Counters TLV generation and parsing.
870+
* Implements MLE Counters TLV value generation and parsing.
874871
*/
875872
OT_TOOL_PACKED_BEGIN
876-
class MleCountersTlv : public Tlv, public TlvInfo<Tlv::kMleCounters>
873+
class MleCountersTlvValue
877874
{
878875
public:
879876
/**
880-
* Initializes the TLV.
877+
* Initializes the TLV value.
881878
*
882879
* @param[in] aMleCounters The MLE counters to initialize the TLV with.
883880
*/
884-
void Init(const Mle::Counters &aMleCounters);
881+
void InitFrom(const Mle::Counters &aMleCounters);
885882

886883
/**
887-
* Indicates whether or not the TLV appears to be well-formed.
888-
*
889-
* @retval TRUE If the TLV appears to be well-formed.
890-
* @retval FALSE If the TLV does not appear to be well-formed.
891-
*/
892-
bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); }
893-
894-
/**
895-
*
896-
* Reads the counters from TLV.
884+
* Reads the counters from TLV value
897885
*
898886
* @param[out] aDiagMleCounters A reference to `NetDiag::MleCounters` to populate.
899887
*/
@@ -917,6 +905,11 @@ class MleCountersTlv : public Tlv, public TlvInfo<Tlv::kMleCounters>
917905
uint64_t mLeaderTime; // Milliseconds device has been in leader role.
918906
} OT_TOOL_PACKED_END;
919907

908+
/**
909+
* Defines MLE Counters TLV constants and types.
910+
*/
911+
typedef SimpleTlvInfo<Tlv::kMleCounters, MleCountersTlvValue> MleCountersTlv;
912+
920913
} // namespace NetDiag
921914

922915
DefineCoreType(otNetworkDiagConnectivity, NetDiag::Connectivity);

0 commit comments

Comments
 (0)