mirror of
https://github.com/espressif/openthread.git
synced 2026-08-01 00:27:47 +00:00
[network-diag] simplify ChannelPagesTlv and diag data parsing (#12706)
This commit simplifies appending `ChannelPagesTlv` using the standard `Tlv::Append<>()` with the the array of supported channel pages as the TLV value. In addition, a `ReadDiagData()` helper method is introduced in the `NetworkDiagnostic::Client` to unify and simplify how `otNetworkDiagData` arrays (e.g. `mNetworkData`, `mChannelPages`) are parsed and populated from read TLVs.
This commit is contained in:
@@ -435,22 +435,8 @@ Error Server::AppendDiagTlv(uint8_t aTlvType, Message &aMessage)
|
||||
break;
|
||||
|
||||
case Tlv::kChannelPages:
|
||||
{
|
||||
ChannelPagesTlv tlv;
|
||||
uint8_t length = 0;
|
||||
|
||||
tlv.Init();
|
||||
|
||||
for (uint8_t page : Radio::kSupportedChannelPages)
|
||||
{
|
||||
tlv.GetChannelPages()[length++] = page;
|
||||
}
|
||||
|
||||
tlv.SetLength(length);
|
||||
error = tlv.AppendTo(aMessage);
|
||||
|
||||
error = Tlv::Append<ChannelPagesTlv>(aMessage, Radio::kSupportedChannelPages, Radio::kNumChannelPages);
|
||||
break;
|
||||
}
|
||||
|
||||
case Tlv::kNonPreferredChannels:
|
||||
{
|
||||
@@ -1172,6 +1158,14 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
void Client::ReadDiagData(DiagData &aDiagData, const Message &aMessage, const Tlv::Info &aTlvInfo)
|
||||
{
|
||||
OffsetRange offsetRange = aTlvInfo.GetValueOffsetRange();
|
||||
|
||||
offsetRange.ShrinkLength(GetArrayLength(aDiagData.m8));
|
||||
aDiagData.mCount = static_cast<uint8_t>(aMessage.ReadBytes(offsetRange, aDiagData.m8));
|
||||
}
|
||||
|
||||
void Client::ParseMacCounters(const MacCountersTlv &aMacCountersTlv, otNetworkDiagMacCounters &aMacCounters)
|
||||
{
|
||||
aMacCounters.mIfInUnknownProtos = aMacCountersTlv.GetIfInUnknownProtos();
|
||||
@@ -1272,8 +1266,7 @@ Error Client::GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator,
|
||||
"NetworkData array in `otNetworkDiagTlv` is too small");
|
||||
|
||||
VerifyOrExit(tlvInfo.GetLength() <= NetworkData::NetworkData::kMaxSize, error = kErrorParse);
|
||||
aDiagTlv.mData.mNetworkData.mCount = static_cast<uint8_t>(tlvInfo.GetLength());
|
||||
aMessage.ReadBytes(tlvInfo.GetValueOffsetRange(), aDiagTlv.mData.mNetworkData.m8);
|
||||
ReadDiagData(aDiagTlv.mData.mNetworkData, aMessage, tlvInfo);
|
||||
break;
|
||||
|
||||
case Tlv::kIp6AddressList:
|
||||
@@ -1344,10 +1337,7 @@ Error Client::GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator,
|
||||
}
|
||||
|
||||
case Tlv::kChannelPages:
|
||||
aDiagTlv.mData.mChannelPages.mCount =
|
||||
static_cast<uint8_t>(Min(tlvInfo.GetLength(), GetArrayLength(aDiagTlv.mData.mChannelPages.m8)));
|
||||
aMessage.ReadBytes(tlvInfo.GetValueOffset(), aDiagTlv.mData.mChannelPages.m8,
|
||||
aDiagTlv.mData.mChannelPages.mCount);
|
||||
ReadDiagData(aDiagTlv.mData.mChannelPages, aMessage, tlvInfo);
|
||||
break;
|
||||
|
||||
case Tlv::kMaxChildTimeout:
|
||||
|
||||
@@ -281,6 +281,7 @@ public:
|
||||
uint16_t GetLastQueryId(void) const { return mQueryId; }
|
||||
|
||||
private:
|
||||
typedef otNetworkDiagData DiagData;
|
||||
typedef otNetworkDiagIp6AddrList Ip6AddrList;
|
||||
typedef otNetworkDiagMacCounters MacCounters;
|
||||
|
||||
@@ -302,6 +303,7 @@ private:
|
||||
|
||||
template <Uri kUri> void HandleTmf(Coap::Msg &aMsg);
|
||||
|
||||
static void ReadDiagData(DiagData &aDiagData, const Message &aMessage, const Tlv::Info &aTlvInfo);
|
||||
static void ParseIp6AddrList(Ip6AddrList &aIp6Addrs, const Message &aMessage, OffsetRange aOffsetRange);
|
||||
static void ParseMacCounters(const MacCountersTlv &aMacCountersTlv, MacCounters &aMacCounters);
|
||||
|
||||
|
||||
@@ -591,43 +591,9 @@ private:
|
||||
} OT_TOOL_PACKED_END;
|
||||
|
||||
/**
|
||||
* Implements Channel Pages TLV generation and parsing.
|
||||
* Defines Channel Pages TLV constants and types.
|
||||
*/
|
||||
OT_TOOL_PACKED_BEGIN
|
||||
class ChannelPagesTlv : public Tlv, public TlvInfo<Tlv::kChannelPages>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Initializes the TLV.
|
||||
*/
|
||||
void Init(void)
|
||||
{
|
||||
SetType(kChannelPages);
|
||||
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
|
||||
{
|
||||
// At least one channel page must be included.
|
||||
return GetLength() >= 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pointer to the list of Channel Pages.
|
||||
*
|
||||
* @returns A pointer to the list of Channel Pages.
|
||||
*/
|
||||
uint8_t *GetChannelPages(void) { return mChannelPages; }
|
||||
|
||||
private:
|
||||
uint8_t mChannelPages[Radio::kNumChannelPages];
|
||||
} OT_TOOL_PACKED_END;
|
||||
typedef TlvInfo<Tlv::kChannelPages> ChannelPagesTlv;
|
||||
|
||||
/**
|
||||
* Defines Type List TLV constants and types.
|
||||
|
||||
Reference in New Issue
Block a user