Skip to content

Commit

Permalink
[meshcop-leader] simplify adding CommissioningData (openthread#9540)
Browse files Browse the repository at this point in the history
This commit contains smaller changes in` MeshCoP::Leader` class:
- Changes `CommissioningData` to be a `private` type in this class
- Adds `Init()` to `CommisioningData()` to init all sub-tlvs.
  • Loading branch information
abtink authored Oct 18, 2023
1 parent 6bfe59d commit 47272e4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
36 changes: 22 additions & 14 deletions src/core/meshcop/meshcop_leader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,8 @@ template <> void Leader::HandleTmf<kUriLeaderPetition>(Coap::Message &aMessage,
ResignCommissioner();
}

data.mBorderAgentLocator.Init();
data.mBorderAgentLocator.SetBorderAgentLocator(aMessageInfo.GetPeerAddr().GetIid().GetLocator());

data.mCommissionerSessionId.Init();
data.mCommissionerSessionId.SetCommissionerSessionId(++mSessionId);

data.mSteeringData.Init();
data.mSteeringData.SetLength(1);
data.mSteeringData.Clear();

SuccessOrExit(
Get<NetworkData::Leader>().SetCommissioningData(reinterpret_cast<uint8_t *>(&data), data.GetLength()));
data.Init(aMessageInfo.GetPeerAddr().GetIid().GetLocator(), ++mSessionId);
SuccessOrExit(Get<NetworkData::Leader>().SetCommissioningData(&data, data.GetLength()));

mCommissionerId = commissionerId;

Expand Down Expand Up @@ -263,8 +253,7 @@ void Leader::SetEmptyCommissionerData(void)
sessionIdTlv.Init();
sessionIdTlv.SetCommissionerSessionId(++mSessionId);

IgnoreError(Get<NetworkData::Leader>().SetCommissioningData(reinterpret_cast<uint8_t *>(&sessionIdTlv),
sizeof(Tlv) + sessionIdTlv.GetLength()));
IgnoreError(Get<NetworkData::Leader>().SetCommissioningData(&sessionIdTlv, sizeof(CommissionerSessionIdTlv)));
}

void Leader::ResignCommissioner(void)
Expand All @@ -275,6 +264,25 @@ void Leader::ResignCommissioner(void)
LogInfo("commissioner inactive");
}

void Leader::CommissioningData::Init(uint16_t aBorderAgentRloc16, uint16_t aSessionId)
{
mBorderAgentLocatorTlv.Init();
mBorderAgentLocatorTlv.SetBorderAgentLocator(aBorderAgentRloc16);

mSessionIdTlv.Init();
mSessionIdTlv.SetCommissionerSessionId(aSessionId);

mSteeringDataTlv.Init();
mSteeringDataTlv.SetLength(1);
mSteeringDataTlv.Clear();
}

uint8_t Leader::CommissioningData::GetLength(void) const
{
return static_cast<uint8_t>(sizeof(BorderAgentLocatorTlv) + sizeof(CommissionerSessionIdTlv) +
mSteeringDataTlv.GetSize());
}

} // namespace MeshCoP
} // namespace ot

Expand Down
28 changes: 13 additions & 15 deletions src/core/meshcop/meshcop_leader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@
namespace ot {
namespace MeshCoP {

OT_TOOL_PACKED_BEGIN
class CommissioningData
{
public:
uint8_t GetLength(void) const
{
return sizeof(Tlv) + mBorderAgentLocator.GetLength() + sizeof(Tlv) + mCommissionerSessionId.GetLength() +
sizeof(Tlv) + mSteeringData.GetLength();
}

BorderAgentLocatorTlv mBorderAgentLocator;
CommissionerSessionIdTlv mCommissionerSessionId;
SteeringDataTlv mSteeringData;
} OT_TOOL_PACKED_END;

class Leader : public InstanceLocator, private NonCopyable
{
friend class Tmf::Agent;
Expand Down Expand Up @@ -113,6 +98,19 @@ class Leader : public InstanceLocator, private NonCopyable
private:
static constexpr uint32_t kTimeoutLeaderPetition = 50; // TIMEOUT_LEAD_PET (seconds)

OT_TOOL_PACKED_BEGIN
class CommissioningData
{
public:
void Init(uint16_t aBorderAgentRloc16, uint16_t aSessionId);
uint8_t GetLength(void) const;

private:
BorderAgentLocatorTlv mBorderAgentLocatorTlv;
CommissionerSessionIdTlv mSessionIdTlv;
SteeringDataTlv mSteeringDataTlv;
} OT_TOOL_PACKED_END;

void HandleTimer(void);

template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
Expand Down
2 changes: 1 addition & 1 deletion src/core/thread/network_data_leader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ Error LeaderBase::SetNetworkData(uint8_t aVersion,
return error;
}

Error LeaderBase::SetCommissioningData(const uint8_t *aValue, uint8_t aValueLength)
Error LeaderBase::SetCommissioningData(const void *aValue, uint8_t aValueLength)
{
Error error = kErrorNone;
CommissioningDataTlv *commissioningDataTlv;
Expand Down
2 changes: 1 addition & 1 deletion src/core/thread/network_data_leader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class LeaderBase : public MutableNetworkData
* @retval kErrorNoBufs Insufficient space to add the Commissioning Data.
*
*/
Error SetCommissioningData(const uint8_t *aValue, uint8_t aValueLength);
Error SetCommissioningData(const void *aValue, uint8_t aValueLength);

/**
* Checks if the steering data includes a Joiner.
Expand Down

0 comments on commit 47272e4

Please sign in to comment.