mirror of
https://github.com/espressif/openthread.git
synced 2026-10-29 10:44:00 +00:00
Clean up Commissioning Data TLV processing. (#1051)
This commit is contained in:
@@ -400,31 +400,17 @@ ThreadError DatasetManager::Set(Coap::Header &aHeader, Message &aMessage, const
|
||||
// verify session id is the same
|
||||
if (tlvType == Tlv::kCommissionerSessionId)
|
||||
{
|
||||
uint8_t *cur;
|
||||
uint8_t *end;
|
||||
uint8_t length;
|
||||
CommissionerSessionIdTlv *sessionId;
|
||||
uint16_t rxSessionId;
|
||||
|
||||
isUpdateFromCommissioner = true;
|
||||
cur = mNetworkDataLeader.GetCommissioningData(length);
|
||||
end = cur + length;
|
||||
|
||||
while (cur < end)
|
||||
{
|
||||
Tlv *data = reinterpret_cast<Tlv *>(cur);
|
||||
sessionId = static_cast<CommissionerSessionIdTlv *>(mNetworkDataLeader.GetCommissioningDataSubTlv(
|
||||
Tlv::kCommissionerSessionId));
|
||||
aMessage.Read(offset + sizeof(tlv), sizeof(rxSessionId), &rxSessionId);
|
||||
|
||||
if (data->GetType() == Tlv::kCommissionerSessionId)
|
||||
{
|
||||
uint16_t sessionId;
|
||||
uint16_t rxSessionId;
|
||||
|
||||
sessionId = static_cast<CommissionerSessionIdTlv *>(data)->GetCommissionerSessionId();
|
||||
aMessage.Read(offset + sizeof(tlv), sizeof(rxSessionId), &rxSessionId);
|
||||
VerifyOrExit(sessionId == rxSessionId, state = StateTlv::kReject);
|
||||
break;
|
||||
}
|
||||
|
||||
cur += sizeof(Tlv) + data->GetLength();
|
||||
}
|
||||
VerifyOrExit(sessionId != NULL && sessionId->GetCommissionerSessionId() == rxSessionId,
|
||||
state = StateTlv::kReject);
|
||||
}
|
||||
|
||||
// verify that TLV data size is less than maximum TLV value size
|
||||
@@ -512,38 +498,19 @@ ThreadError DatasetManager::Set(Coap::Header &aHeader, Message &aMessage, const
|
||||
// notify commissioner if update is from thread device
|
||||
if (!isUpdateFromCommissioner)
|
||||
{
|
||||
uint8_t *cur;
|
||||
uint8_t *end;
|
||||
uint8_t length;
|
||||
uint16_t locator = 0xffff;
|
||||
BorderAgentLocatorTlv *borderAgentLocator;
|
||||
Ip6::Address destination;
|
||||
|
||||
cur = mNetworkDataLeader.GetCommissioningData(length);
|
||||
VerifyOrExit(cur != NULL && length != 0, ;);
|
||||
end = cur + length;
|
||||
|
||||
while (cur < end)
|
||||
{
|
||||
Tlv *data = reinterpret_cast<Tlv *>(cur);
|
||||
|
||||
if (data->GetType() == Tlv::kBorderAgentLocator)
|
||||
{
|
||||
locator = static_cast<BorderAgentLocatorTlv *>(data)->GetBorderAgentLocator();
|
||||
break;
|
||||
}
|
||||
|
||||
cur += sizeof(Tlv) + data->GetLength();
|
||||
}
|
||||
|
||||
// verify that Border Agent Locator should be available
|
||||
VerifyOrExit(locator != 0xffff, ;);
|
||||
borderAgentLocator = static_cast<BorderAgentLocatorTlv *>(mNetworkDataLeader.GetCommissioningDataSubTlv(
|
||||
Tlv::kBorderAgentLocator));
|
||||
VerifyOrExit(borderAgentLocator != NULL,);
|
||||
|
||||
memset(&destination, 0, sizeof(destination));
|
||||
destination = mNetif.GetMle().GetMeshLocal16();
|
||||
destination.mFields.m16[4] = HostSwap16(0x0000);
|
||||
destination.mFields.m16[5] = HostSwap16(0x00ff);
|
||||
destination.mFields.m16[6] = HostSwap16(0xfe00);
|
||||
destination.mFields.m16[7] = HostSwap16(locator);
|
||||
destination.mFields.m16[7] = HostSwap16(borderAgentLocator->GetBorderAgentLocator());
|
||||
|
||||
mNetif.GetLeader().SendDatasetChanged(destination);
|
||||
}
|
||||
|
||||
@@ -77,14 +77,12 @@ void JoinerRouter::HandleNetifStateChanged(uint32_t aFlags, void *aContext)
|
||||
|
||||
void JoinerRouter::HandleNetifStateChanged(uint32_t aFlags)
|
||||
{
|
||||
uint8_t length;
|
||||
|
||||
VerifyOrExit(mNetif.GetMle().GetDeviceMode() & Mle::ModeTlv::kModeFFD, ;);
|
||||
VerifyOrExit(aFlags & OT_THREAD_NETDATA_UPDATED, ;);
|
||||
|
||||
mNetif.GetIp6Filter().RemoveUnsecurePort(mSocket.GetSockName().mPort);
|
||||
|
||||
if (mNetif.GetNetworkDataLeader().GetCommissioningData(length) != NULL)
|
||||
if (mNetif.GetNetworkDataLeader().GetCommissioningData() != NULL)
|
||||
{
|
||||
Ip6::SockAddr sockaddr;
|
||||
|
||||
@@ -106,27 +104,14 @@ exit:
|
||||
|
||||
ThreadError JoinerRouter::GetBorderAgentRloc(uint16_t &aRloc)
|
||||
{
|
||||
ThreadError error = kThreadError_NotFound;
|
||||
uint8_t *cur;
|
||||
uint8_t *end;
|
||||
uint8_t length;
|
||||
ThreadError error = kThreadError_None;
|
||||
BorderAgentLocatorTlv *borderAgentLocator;
|
||||
|
||||
VerifyOrExit((cur = mNetif.GetNetworkDataLeader().GetCommissioningData(length)) != NULL, ;);
|
||||
borderAgentLocator = static_cast<BorderAgentLocatorTlv *>(mNetif.GetNetworkDataLeader().GetCommissioningDataSubTlv(
|
||||
Tlv::kBorderAgentLocator));
|
||||
VerifyOrExit(borderAgentLocator != NULL, error = kThreadError_NotFound);
|
||||
|
||||
end = cur + length;
|
||||
|
||||
while (cur < end)
|
||||
{
|
||||
Tlv *tlv = reinterpret_cast<Tlv *>(cur);
|
||||
|
||||
if (tlv->GetType() == Tlv::kBorderAgentLocator)
|
||||
{
|
||||
aRloc = reinterpret_cast<BorderAgentLocatorTlv *>(tlv)->GetBorderAgentLocator();
|
||||
ExitNow(error = kThreadError_None);
|
||||
}
|
||||
|
||||
cur += sizeof(Tlv) + tlv->GetLength();
|
||||
}
|
||||
aRloc = borderAgentLocator->GetBorderAgentLocator();
|
||||
|
||||
exit:
|
||||
return error;
|
||||
@@ -134,30 +119,19 @@ exit:
|
||||
|
||||
uint16_t JoinerRouter::GetJoinerUdpPort(void)
|
||||
{
|
||||
uint16_t joinerUdpPort = OPENTHREAD_CONFIG_JOINER_UDP_PORT;
|
||||
uint8_t *cur;
|
||||
uint8_t *end;
|
||||
uint8_t length;
|
||||
uint16_t rval = OPENTHREAD_CONFIG_JOINER_UDP_PORT;
|
||||
JoinerUdpPortTlv *joinerUdpPort;
|
||||
|
||||
VerifyOrExit(!mIsJoinerPortConfigured, joinerUdpPort = mJoinerUdpPort);
|
||||
VerifyOrExit((cur = mNetif.GetNetworkDataLeader().GetCommissioningData(length)) != NULL, ;);
|
||||
VerifyOrExit(!mIsJoinerPortConfigured, rval = mJoinerUdpPort);
|
||||
|
||||
end = cur + length;
|
||||
joinerUdpPort = static_cast<JoinerUdpPortTlv *>(mNetif.GetNetworkDataLeader().GetCommissioningDataSubTlv(
|
||||
Tlv::kJoinerUdpPort));
|
||||
VerifyOrExit(joinerUdpPort != NULL,);
|
||||
|
||||
while (cur < end)
|
||||
{
|
||||
Tlv *tlv = reinterpret_cast<Tlv *>(cur);
|
||||
|
||||
if (tlv->GetType() == Tlv::kJoinerUdpPort)
|
||||
{
|
||||
ExitNow(joinerUdpPort = reinterpret_cast<JoinerUdpPortTlv *>(tlv)->GetUdpPort());
|
||||
}
|
||||
|
||||
cur += sizeof(Tlv) + tlv->GetLength();
|
||||
}
|
||||
rval = joinerUdpPort->GetUdpPort();
|
||||
|
||||
exit:
|
||||
return joinerUdpPort;
|
||||
return rval;
|
||||
}
|
||||
|
||||
ThreadError JoinerRouter::SetJoinerUdpPort(uint16_t aJoinerUdpPort)
|
||||
|
||||
@@ -252,11 +252,9 @@ void EnergyScanServer::HandleNetifStateChanged(uint32_t aFlags, void *aContext)
|
||||
|
||||
void EnergyScanServer::HandleNetifStateChanged(uint32_t aFlags)
|
||||
{
|
||||
uint8_t length;
|
||||
|
||||
if ((aFlags & OT_THREAD_NETDATA_UPDATED) != 0 &&
|
||||
!mActive &&
|
||||
mNetif.GetNetworkDataLeader().GetCommissioningData(length) == NULL)
|
||||
mNetif.GetNetworkDataLeader().GetCommissioningData() == NULL)
|
||||
{
|
||||
mActive = false;
|
||||
mTimer.Stop();
|
||||
|
||||
@@ -2582,8 +2582,7 @@ ThreadError MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, u
|
||||
MeshCoP::ExtendedPanIdTlv extPanId;
|
||||
MeshCoP::NetworkNameTlv networkName;
|
||||
MeshCoP::JoinerUdpPortTlv joinerUdpPort;
|
||||
uint8_t *cur;
|
||||
uint8_t length;
|
||||
MeshCoP::Tlv *steeringData;
|
||||
|
||||
VerifyOrExit((message = NewMessage()) != NULL, ;);
|
||||
message->SetLinkSecurityEnabled(false);
|
||||
@@ -2623,22 +2622,11 @@ ThreadError MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, u
|
||||
SuccessOrExit(error = message->Append(&networkName, sizeof(tlv) + networkName.GetLength()));
|
||||
|
||||
// Steering Data TLV
|
||||
if ((cur = mNetif.GetNetworkDataLeader().GetCommissioningData(length)) != NULL)
|
||||
steeringData = mNetif.GetNetworkDataLeader().GetCommissioningDataSubTlv(MeshCoP::Tlv::kSteeringData);
|
||||
|
||||
if (steeringData != NULL)
|
||||
{
|
||||
uint8_t *end = cur + length;
|
||||
|
||||
while (cur < end)
|
||||
{
|
||||
MeshCoP::Tlv *meshcop = reinterpret_cast<MeshCoP::Tlv *>(cur);
|
||||
|
||||
if (meshcop->GetType() == MeshCoP::Tlv::kSteeringData)
|
||||
{
|
||||
SuccessOrExit(message->Append(meshcop, sizeof(*meshcop) + meshcop->GetLength()));
|
||||
break;
|
||||
}
|
||||
|
||||
cur += sizeof(*meshcop) + meshcop->GetLength();
|
||||
}
|
||||
SuccessOrExit(message->Append(steeringData, sizeof(*steeringData) + steeringData->GetLength()));
|
||||
}
|
||||
|
||||
// Joiner UDP Port TLV
|
||||
|
||||
@@ -453,18 +453,43 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
uint8_t *LeaderBase::GetCommissioningData(uint8_t &aLength)
|
||||
NetworkDataTlv *LeaderBase::GetCommissioningData(void)
|
||||
{
|
||||
uint8_t *rval = NULL;
|
||||
NetworkDataTlv *cur = reinterpret_cast<NetworkDataTlv *>(mTlvs);
|
||||
NetworkDataTlv *end = reinterpret_cast<NetworkDataTlv *>(mTlvs + mLength);
|
||||
|
||||
for (NetworkDataTlv *cur = reinterpret_cast<NetworkDataTlv *>(mTlvs);
|
||||
cur < reinterpret_cast<NetworkDataTlv *>(mTlvs + mLength);
|
||||
cur = cur->GetNext())
|
||||
for (; cur < end; cur = cur->GetNext())
|
||||
{
|
||||
if (cur->GetType() == NetworkDataTlv::kTypeCommissioningData)
|
||||
{
|
||||
aLength = cur->GetLength();
|
||||
ExitNow(rval = cur->GetValue());
|
||||
ExitNow();
|
||||
}
|
||||
}
|
||||
|
||||
cur = NULL;
|
||||
|
||||
exit:
|
||||
return cur;
|
||||
}
|
||||
|
||||
MeshCoP::Tlv *LeaderBase::GetCommissioningDataSubTlv(MeshCoP::Tlv::Type aType)
|
||||
{
|
||||
MeshCoP::Tlv *rval = NULL;
|
||||
NetworkDataTlv *commissioningDataTlv;
|
||||
MeshCoP::Tlv *cur;
|
||||
MeshCoP::Tlv *end;
|
||||
|
||||
commissioningDataTlv = GetCommissioningData();
|
||||
VerifyOrExit(commissioningDataTlv != NULL,);
|
||||
|
||||
cur = reinterpret_cast<MeshCoP::Tlv *>(commissioningDataTlv->GetValue());
|
||||
end = reinterpret_cast<MeshCoP::Tlv *>(commissioningDataTlv->GetValue() + commissioningDataTlv->GetLength());
|
||||
|
||||
for (; cur < end; cur = cur->GetNext())
|
||||
{
|
||||
if (cur->GetType() == aType)
|
||||
{
|
||||
ExitNow(rval = cur);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -172,12 +172,20 @@ public:
|
||||
/**
|
||||
* This method returns a pointer to the Commissioning Data.
|
||||
*
|
||||
* @param[out] aLength The length of the Commissioning Data in bytes.
|
||||
*
|
||||
* @returns A pointer to the Commissioning Data or NULL if no Commissioning Data exists.
|
||||
*
|
||||
*/
|
||||
uint8_t *GetCommissioningData(uint8_t &aLength);
|
||||
NetworkDataTlv *GetCommissioningData(void);
|
||||
|
||||
/**
|
||||
* This method returns a pointer to the Commissioning Data Sub-TLV.
|
||||
*
|
||||
* @param[in] aType The TLV type value.
|
||||
*
|
||||
* @returns A pointer to the Commissioning Data Sub-TLV or NULL if no Sub-TLV exists.
|
||||
*
|
||||
*/
|
||||
MeshCoP::Tlv *GetCommissioningDataSubTlv(MeshCoP::Tlv::Type aType);
|
||||
|
||||
/**
|
||||
* This method adds Commissioning Data to the Thread Network Data.
|
||||
|
||||
Reference in New Issue
Block a user