Skip to content

Commit

Permalink
[netdata-leader] simplify HandleTmf<kUriCommissionerGet> (openthrea…
Browse files Browse the repository at this point in the history
…d#9546)

This commit simplifies the handling of `kUriCommissionerGet` TMF
message and preparation of its response.
  • Loading branch information
abtink authored Oct 19, 2023
1 parent a69c2db commit 95b0869
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 55 deletions.
10 changes: 5 additions & 5 deletions src/core/thread/network_data_leader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ class LeaderBase : public MutableNetworkData
void SignalNetDataChanged(void);
const CommissioningDataTlv *FindCommissioningData(void) const;
CommissioningDataTlv *FindCommissioningData(void) { return AsNonConst(AsConst(this)->FindCommissioningData()); }
const MeshCoP::Tlv *FindCommissioningDataSubTlv(uint8_t aType) const;
MeshCoP::Tlv *FindCommissioningDataSubTlv(uint8_t aType)
{
return AsNonConst(AsConst(this)->FindCommissioningDataSubTlv(aType));
}

uint8_t mStableVersion;
uint8_t mVersion;
Expand All @@ -347,11 +352,6 @@ class LeaderBase : public MutableNetworkData
Error SteeringDataCheck(const FilterIndexes &aFilterIndexes) const;
void GetContextForMeshLocalPrefix(Lowpan::Context &aContext) const;
Error ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::Type aType, uint16_t &aValue) const;
const MeshCoP::Tlv *FindCommissioningDataSubTlv(uint8_t aType) const;
MeshCoP::Tlv *FindCommissioningDataSubTlv(uint8_t aType)
{
return AsNonConst(AsConst(this)->FindCommissioningDataSubTlv(aType));
}

uint8_t mTlvBuffer[kMaxSize];
uint8_t mMaxLength;
Expand Down
73 changes: 26 additions & 47 deletions src/core/thread/network_data_leader_ftd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,74 +284,53 @@ template <> void Leader::HandleTmf<kUriCommissionerSet>(Coap::Message &aMessage,

template <> void Leader::HandleTmf<kUriCommissionerGet>(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
uint16_t length = 0;
uint16_t offset;
uint16_t length;
uint16_t offset;
Coap::Message *response = nullptr;

VerifyOrExit(Get<Mle::Mle>().IsLeader() && !mWaitingForNetDataSync);

SuccessOrExit(Tlv::FindTlvValueOffset(aMessage, MeshCoP::Tlv::kGet, offset, length));
aMessage.SetOffset(offset);
response = Get<Tmf::Agent>().NewPriorityResponseMessage(aMessage);
VerifyOrExit(response != nullptr);

exit:
if (Get<Mle::MleRouter>().IsLeader())
if (Tlv::FindTlvValueOffset(aMessage, MeshCoP::Tlv::kGet, offset, length) == kErrorNone)
{
SendCommissioningGetResponse(aMessage, length, aMessageInfo);
}
}
// Append the requested sub-TLV types given in Get TLV.

void Leader::SendCommissioningGetResponse(const Coap::Message &aRequest,
uint16_t aLength,
const Ip6::MessageInfo &aMessageInfo)
{
Error error = kErrorNone;
Coap::Message *message;
CommissioningDataTlv *commDataTlv;
uint8_t *data = nullptr;
uint8_t length = 0;
for (; length > 0; offset++, length--)
{
uint8_t type;
const MeshCoP::Tlv *subTlv;

message = Get<Tmf::Agent>().NewPriorityResponseMessage(aRequest);
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
IgnoreError(aMessage.Read(offset, type));

commDataTlv = FindCommissioningData();
subTlv = FindCommissioningDataSubTlv(type);

if (commDataTlv != nullptr)
{
data = commDataTlv->GetValue();
length = commDataTlv->GetLength();
}

VerifyOrExit(data && length, error = kErrorDrop);

if (aLength == 0)
{
SuccessOrExit(error = message->AppendBytes(data, length));
if (subTlv != nullptr)
{
SuccessOrExit(subTlv->AppendTo(*response));
}
}
}
else
{
for (uint16_t index = 0; index < aLength; index++)
{
uint8_t type;
// Append all sub-TLVs in the Commissioning Data.

IgnoreError(aRequest.Read(aRequest.GetOffset() + index, type));
CommissioningDataTlv *dataTlv = FindCommissioningData();

for (MeshCoP::Tlv *cur = reinterpret_cast<MeshCoP::Tlv *>(data);
cur < reinterpret_cast<MeshCoP::Tlv *>(data + length); cur = cur->GetNext())
{
if (cur->GetType() == type)
{
SuccessOrExit(error = cur->AppendTo(*message));
break;
}
}
if (dataTlv != nullptr)
{
SuccessOrExit(response->AppendBytes(dataTlv->GetValue(), dataTlv->GetLength()));
}
}

SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, aMessageInfo));
SuccessOrExit(Get<Tmf::Agent>().SendMessage(*response, aMessageInfo));
response = nullptr; // `SendMessage` takes ownership on success

LogInfo("Sent %s response", UriToString<kUriCommissionerGet>());

exit:
FreeMessageOnError(message, error);
FreeMessage(response);
}

void Leader::SendCommissioningSetResponse(const Coap::Message &aRequest,
Expand Down
3 changes: 0 additions & 3 deletions src/core/thread/network_data_leader_ftd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,6 @@ class Leader : public LeaderBase, private NonCopyable
UpdateStatus UpdateService(ServiceTlv &aService);
UpdateStatus UpdateTlv(NetworkDataTlv &aTlv, const NetworkDataTlv *aSubTlvs);

void SendCommissioningGetResponse(const Coap::Message &aRequest,
uint16_t aLength,
const Ip6::MessageInfo &aMessageInfo);
void SendCommissioningSetResponse(const Coap::Message &aRequest,
const Ip6::MessageInfo &aMessageInfo,
MeshCoP::StateTlv::State aState);
Expand Down

0 comments on commit 95b0869

Please sign in to comment.