From 41a38ce02699008f2130a2a254d39612a9fcff7f Mon Sep 17 00:00:00 2001 From: rongli Date: Sat, 29 Apr 2017 13:03:20 +0800 Subject: [PATCH] add limitation on some commissioner relative operations (#1670) * add limitation on some commissioner relative operations * update cert scripts to start commissioner rightly --- include/openthread/commissioner.h | 74 +++++++++++-------- src/core/meshcop/announce_begin_client.cpp | 4 +- src/core/meshcop/commissioner.cpp | 6 +- src/core/meshcop/energy_scan_client.cpp | 4 +- src/core/meshcop/panid_query_client.cpp | 4 +- .../thread-cert/Cert_9_2_07_DelayTimer.py | 2 + .../Cert_9_2_09_PendingPartition.py | 2 + .../Cert_9_2_10_PendingPartition.py | 2 + .../thread-cert/Cert_9_2_11_MasterKey.py | 2 + .../thread-cert/Cert_9_2_12_Announce.py | 2 + .../thread-cert/Cert_9_2_13_EnergyScan.py | 2 + .../thread-cert/Cert_9_2_14_PanIdQuery.py | 2 + .../Cert_9_2_15_PendingPartition.py | 2 + .../Cert_9_2_16_ActivePendingPartition.py | 2 + .../Cert_9_2_18_RollBackActiveTimestamp.py | 2 + 15 files changed, 78 insertions(+), 34 deletions(-) diff --git a/include/openthread/commissioner.h b/include/openthread/commissioner.h index 92a614062..b5b2c4f59 100644 --- a/include/openthread/commissioner.h +++ b/include/openthread/commissioner.h @@ -64,7 +64,7 @@ OTAPI ThreadError OTCALL otCommissionerStart(otInstance *aInstance); * * @param[in] aInstance A pointer to an OpenThread instance. * - * @retval kThreadError_None Successfully started the Commissioner role. + * @retval kThreadError_None Successfully stopped the Commissioner role. * */ OTAPI ThreadError OTCALL otCommissionerStop(otInstance *aInstance); @@ -80,6 +80,9 @@ OTAPI ThreadError OTCALL otCommissionerStop(otInstance *aInstance); * @retval kThreadError_None Successfully added the Joiner. * @retval kThreadError_NoBufs No buffers available to add the Joiner. * @retval kThreadError_InvalidArgs @p aExtAddress or @p aPSKd is invalid. + * @retval kThreadError_InvalidState The commissioner is not active. + * + * @note Only use this after successfully started the Commissioner role by otCommissionerStart(). * */ OTAPI ThreadError OTCALL otCommissionerAddJoiner(otInstance *aInstance, const otExtAddress *aExtAddress, @@ -91,9 +94,12 @@ OTAPI ThreadError OTCALL otCommissionerAddJoiner(otInstance *aInstance, const ot * @param[in] aInstance A pointer to an OpenThread instance. * @param[in] aExtAddress A pointer to the Joiner's extended address or NULL for any Joiner. * - * @retval kThreadError_None Successfully added the Joiner. + * @retval kThreadError_None Successfully removed the Joiner. * @retval kThreadError_NotFound The Joiner specified by @p aExtAddress was not found. * @retval kThreadError_InvalidArgs @p aExtAddress is invalid. + * @retval kThreadError_InvalidState The commissioner is not active. + * + * @note Only use this after successfully started the Commissioner role by otCommissionerStart(). * */ OTAPI ThreadError OTCALL otCommissionerRemoveJoiner(otInstance *aIntsance, const otExtAddress *aExtAddress); @@ -101,9 +107,10 @@ OTAPI ThreadError OTCALL otCommissionerRemoveJoiner(otInstance *aIntsance, const /** * This function sets the Provisioning URL. * - * @param[in] aProvisioningUrl A pointer to the Provisioning URL (may be NULL). + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aProvisioningUrl A pointer to the Provisioning URL (may be NULL). * - * @retval kThreadError_None Successfully added the Joiner. + * @retval kThreadError_None Successfully set the Provisioning URL. * @retval kThreadError_InvalidArgs @p aProvisioningUrl is invalid. * */ @@ -112,14 +119,17 @@ OTAPI ThreadError OTCALL otCommissionerSetProvisioningUrl(otInstance *aInstance, /** * This function sends an Announce Begin message. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aChannelMask The channel mask value. - * @param[in] aCount The number of energy measurements per channel. - * @param[in] aPeriod The time between energy measurements (milliseconds). - * @param[in] aAddress A pointer to the IPv6 destination. + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aChannelMask The channel mask value. + * @param[in] aCount The number of energy measurements per channel. + * @param[in] aPeriod The time between energy measurements (milliseconds). + * @param[in] aAddress A pointer to the IPv6 destination. * - * @retval kThreadError_None Successfully enqueued the Announce Begin message. - * @retval kThreadError_NoBufs Insufficient buffers to generate an Announce Begin message. + * @retval kThreadError_None Successfully enqueued the Announce Begin message. + * @retval kThreadError_NoBufs Insufficient buffers to generate an Announce Begin message. + * @retval kThreadError_InvalidState The commissioner is not active. + * + * @note Only use this after successfully started the Commissioner role by otCommissionerStart(). * */ OTAPI ThreadError OTCALL otCommissionerAnnounceBegin(otInstance *aInstance, uint32_t aChannelMask, uint8_t aCount, @@ -141,17 +151,20 @@ typedef void (OTCALL *otCommissionerEnergyReportCallback)(uint32_t aChannelMask, /** * This function sends an Energy Scan Query message. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aChannelMask The channel mask value. - * @param[in] aCount The number of energy measurements per channel. - * @param[in] aPeriod The time between energy measurements (milliseconds). - * @param[in] aScanDuration The scan duration for each energy measurement (milliseconds). - * @param[in] aAddress A pointer to the IPv6 destination. - * @param[in] aCallback A pointer to a function called on receiving an Energy Report message. - * @param[in] aContext A pointer to application-specific context. + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aChannelMask The channel mask value. + * @param[in] aCount The number of energy measurements per channel. + * @param[in] aPeriod The time between energy measurements (milliseconds). + * @param[in] aScanDuration The scan duration for each energy measurement (milliseconds). + * @param[in] aAddress A pointer to the IPv6 destination. + * @param[in] aCallback A pointer to a function called on receiving an Energy Report message. + * @param[in] aContext A pointer to application-specific context. * - * @retval kThreadError_None Successfully enqueued the Energy Scan Query message. - * @retval kThreadError_NoBufs Insufficient buffers to generate an Energy Scan Query message. + * @retval kThreadError_None Successfully enqueued the Energy Scan Query message. + * @retval kThreadError_NoBufs Insufficient buffers to generate an Energy Scan Query message. + * @retval kThreadError_InvalidState The commissioner is not active. + * + * @note Only use this after successfully started the Commissioner role by otCommissionerStart(). * */ OTAPI ThreadError OTCALL otCommissionerEnergyScan(otInstance *aInstance, uint32_t aChannelMask, uint8_t aCount, @@ -171,15 +184,18 @@ typedef void (OTCALL *otCommissionerPanIdConflictCallback)(uint16_t aPanId, uint /** * This function sends a PAN ID Query message. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aPanId The PAN ID to query. - * @param[in] aChannelMask The channel mask value. - * @param[in] aAddress A pointer to the IPv6 destination. - * @param[in] aCallback A pointer to a function called on receiving an Energy Report message. - * @param[in] aContext A pointer to application-specific context. + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aPanId The PAN ID to query. + * @param[in] aChannelMask The channel mask value. + * @param[in] aAddress A pointer to the IPv6 destination. + * @param[in] aCallback A pointer to a function called on receiving an Energy Report message. + * @param[in] aContext A pointer to application-specific context. * - * @retval kThreadError_None Successfully enqueued the PAN ID Query message. - * @retval kThreadError_NoBufs Insufficient buffers to generate a PAN ID Query message. + * @retval kThreadError_None Successfully enqueued the PAN ID Query message. + * @retval kThreadError_NoBufs Insufficient buffers to generate a PAN ID Query message. + * @retval kThreadError_InvalidState The commissioner is not active. + * + * @note Only use this after successfully started the Commissioner role by otCommissionerStart(). * */ OTAPI ThreadError OTCALL otCommissionerPanIdQuery(otInstance *aInstance, uint16_t aPanId, uint32_t aChannelMask, diff --git a/src/core/meshcop/announce_begin_client.cpp b/src/core/meshcop/announce_begin_client.cpp index 636a98081..4081ebc4a 100644 --- a/src/core/meshcop/announce_begin_client.cpp +++ b/src/core/meshcop/announce_begin_client.cpp @@ -76,7 +76,9 @@ ThreadError AnnounceBeginClient::SendRequest(uint32_t aChannelMask, uint8_t aCou MeshCoP::PeriodTlv period; Ip6::MessageInfo messageInfo; - Message *message; + Message *message = NULL; + + VerifyOrExit(mNetif.GetCommissioner().GetState() == kCommissionerStateActive, error = kThreadError_InvalidState); header.Init(aAddress.IsMulticast() ? kCoapTypeNonConfirmable : kCoapTypeConfirmable, kCoapRequestPost); diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 971f5e7cc..9ab8a336a 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -222,6 +222,8 @@ ThreadError Commissioner::AddJoiner(const Mac::ExtAddress *aExtAddress, const ch { ThreadError error = kThreadError_NoBufs; + VerifyOrExit(mState == kCommissionerStateActive, error = kThreadError_InvalidState); + otLogFuncEntryMsg("%llX, %s", (aExtAddress ? HostSwap64(*reinterpret_cast(aExtAddress)) : 0), aPSKd); VerifyOrExit(strlen(aPSKd) <= Dtls::kPskMaxLength, error = kThreadError_InvalidArgs); RemoveJoiner(aExtAddress, 0); // remove imediately @@ -263,6 +265,8 @@ ThreadError Commissioner::RemoveJoiner(const Mac::ExtAddress *aExtAddress, uint3 { ThreadError error = kThreadError_NotFound; + VerifyOrExit(mState == kCommissionerStateActive, error = kThreadError_InvalidState); + otLogFuncEntryMsg("%llX", (aExtAddress ? HostSwap64(*reinterpret_cast(aExtAddress)) : 0)); for (size_t i = 0; i < sizeof(mJoiners) / sizeof(mJoiners[0]); i++) @@ -667,8 +671,6 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Header *aHeader, Message * mTransmitAttempts = 0; mTimer.Start(Timer::SecToMsec(kKeepAliveTimeout) / 2); - SendCommissionerSet(); - exit: if (retransmit) diff --git a/src/core/meshcop/energy_scan_client.cpp b/src/core/meshcop/energy_scan_client.cpp index 5c43e8c7c..207a13605 100644 --- a/src/core/meshcop/energy_scan_client.cpp +++ b/src/core/meshcop/energy_scan_client.cpp @@ -85,7 +85,9 @@ ThreadError EnergyScanClient::SendQuery(uint32_t aChannelMask, uint8_t aCount, u MeshCoP::PeriodTlv period; MeshCoP::ScanDurationTlv scanDuration; Ip6::MessageInfo messageInfo; - Message *message; + Message *message = NULL; + + VerifyOrExit(mNetif.GetCommissioner().GetState() == kCommissionerStateActive, error = kThreadError_InvalidState); header.Init(aAddress.IsMulticast() ? kCoapTypeNonConfirmable : kCoapTypeConfirmable, kCoapRequestPost); diff --git a/src/core/meshcop/panid_query_client.cpp b/src/core/meshcop/panid_query_client.cpp index 40a649db3..3bffbf30f 100644 --- a/src/core/meshcop/panid_query_client.cpp +++ b/src/core/meshcop/panid_query_client.cpp @@ -79,7 +79,9 @@ ThreadError PanIdQueryClient::SendQuery(uint16_t aPanId, uint32_t aChannelMask, MeshCoP::ChannelMask0Tlv channelMask; MeshCoP::PanIdTlv panId; Ip6::MessageInfo messageInfo; - Message *message; + Message *message = NULL; + + VerifyOrExit(mNetif.GetCommissioner().GetState() == kCommissionerStateActive, error = kThreadError_InvalidState); header.Init(aAddress.IsMulticast() ? kCoapTypeNonConfirmable : kCoapTypeConfirmable, kCoapRequestPost); diff --git a/tests/scripts/thread-cert/Cert_9_2_07_DelayTimer.py b/tests/scripts/thread-cert/Cert_9_2_07_DelayTimer.py index c28e46c01..5347b654a 100755 --- a/tests/scripts/thread-cert/Cert_9_2_07_DelayTimer.py +++ b/tests/scripts/thread-cert/Cert_9_2_07_DelayTimer.py @@ -87,6 +87,8 @@ class Cert_9_2_7_DelayTimer(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER].start() time.sleep(10) diff --git a/tests/scripts/thread-cert/Cert_9_2_09_PendingPartition.py b/tests/scripts/thread-cert/Cert_9_2_09_PendingPartition.py index 7bcf8f327..1c1cb559e 100755 --- a/tests/scripts/thread-cert/Cert_9_2_09_PendingPartition.py +++ b/tests/scripts/thread-cert/Cert_9_2_09_PendingPartition.py @@ -90,6 +90,8 @@ class Cert_9_2_09_PendingPartition(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5) diff --git a/tests/scripts/thread-cert/Cert_9_2_10_PendingPartition.py b/tests/scripts/thread-cert/Cert_9_2_10_PendingPartition.py index db58f1463..6cdbd3a08 100755 --- a/tests/scripts/thread-cert/Cert_9_2_10_PendingPartition.py +++ b/tests/scripts/thread-cert/Cert_9_2_10_PendingPartition.py @@ -96,6 +96,8 @@ class Cert_9_2_10_PendingPartition(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5) diff --git a/tests/scripts/thread-cert/Cert_9_2_11_MasterKey.py b/tests/scripts/thread-cert/Cert_9_2_11_MasterKey.py index 68392911f..1e2822be5 100755 --- a/tests/scripts/thread-cert/Cert_9_2_11_MasterKey.py +++ b/tests/scripts/thread-cert/Cert_9_2_11_MasterKey.py @@ -95,6 +95,8 @@ class Cert_9_2_11_MasterKey(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5) diff --git a/tests/scripts/thread-cert/Cert_9_2_12_Announce.py b/tests/scripts/thread-cert/Cert_9_2_12_Announce.py index 38f1a62b8..f716e0e17 100755 --- a/tests/scripts/thread-cert/Cert_9_2_12_Announce.py +++ b/tests/scripts/thread-cert/Cert_9_2_12_Announce.py @@ -92,6 +92,8 @@ class Cert_9_2_12_Announce(unittest.TestCase): self.nodes[LEADER1].start() self.nodes[LEADER1].set_state('leader') self.assertEqual(self.nodes[LEADER1].get_state(), 'leader') + self.nodes[LEADER1].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5) diff --git a/tests/scripts/thread-cert/Cert_9_2_13_EnergyScan.py b/tests/scripts/thread-cert/Cert_9_2_13_EnergyScan.py index 4483768bb..fde409dbd 100755 --- a/tests/scripts/thread-cert/Cert_9_2_13_EnergyScan.py +++ b/tests/scripts/thread-cert/Cert_9_2_13_EnergyScan.py @@ -80,6 +80,8 @@ class Cert_9_2_13_EnergyScan(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5) diff --git a/tests/scripts/thread-cert/Cert_9_2_14_PanIdQuery.py b/tests/scripts/thread-cert/Cert_9_2_14_PanIdQuery.py index c84652d5f..fcac3b8b5 100755 --- a/tests/scripts/thread-cert/Cert_9_2_14_PanIdQuery.py +++ b/tests/scripts/thread-cert/Cert_9_2_14_PanIdQuery.py @@ -80,6 +80,8 @@ class Cert_9_2_14_PanIdQuery(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5) diff --git a/tests/scripts/thread-cert/Cert_9_2_15_PendingPartition.py b/tests/scripts/thread-cert/Cert_9_2_15_PendingPartition.py index 93dff69a6..978582442 100755 --- a/tests/scripts/thread-cert/Cert_9_2_15_PendingPartition.py +++ b/tests/scripts/thread-cert/Cert_9_2_15_PendingPartition.py @@ -88,6 +88,8 @@ class Cert_9_2_15_PendingPartition(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5) diff --git a/tests/scripts/thread-cert/Cert_9_2_16_ActivePendingPartition.py b/tests/scripts/thread-cert/Cert_9_2_16_ActivePendingPartition.py index 0dbc57b27..1647ffa07 100755 --- a/tests/scripts/thread-cert/Cert_9_2_16_ActivePendingPartition.py +++ b/tests/scripts/thread-cert/Cert_9_2_16_ActivePendingPartition.py @@ -89,6 +89,8 @@ class Cert_9_2_16_ActivePendingPartition(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5) diff --git a/tests/scripts/thread-cert/Cert_9_2_18_RollBackActiveTimestamp.py b/tests/scripts/thread-cert/Cert_9_2_18_RollBackActiveTimestamp.py index 9f3a1f7d2..687c72cf7 100755 --- a/tests/scripts/thread-cert/Cert_9_2_18_RollBackActiveTimestamp.py +++ b/tests/scripts/thread-cert/Cert_9_2_18_RollBackActiveTimestamp.py @@ -104,6 +104,8 @@ class Cert_9_2_18_RollBackActiveTimestamp(unittest.TestCase): self.nodes[COMMISSIONER].start() time.sleep(5) self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'router') + self.nodes[COMMISSIONER].commissioner_start() + time.sleep(3) self.nodes[ROUTER1].start() time.sleep(5)