Add initial commissioning commands support to the NCP (#1393)

* Add initial commissioning commands support to the NCP

* Make GetPropertyHandler_THREAD_COMMISSIONER_ENABLED send true only if commissioner active, move commissioner state enum to oepnthread/types.h

* Use enum to return commissioner state, make spinel property return true only if Commissioner State Active

* Fix spinel documentation
This commit is contained in:
pszkotak
2017-03-29 20:37:21 +02:00
committed by Jonathan Hui
parent 3c86327f70
commit 2fa7a13c1b
11 changed files with 648 additions and 441 deletions
@@ -233,3 +233,24 @@ The leader network data.
* Packed-Encoding: `D`
The stable leader network data.
### PROP 5391: PROP_THREAD_JOINERS {#prop-thread-joiners}
* Type: Read-Write
* Packed-Encoding: `A(t(ULE))`
Data per item is:
* `U`: PSKd
* `L`: Timeout in seconds
* `E`: Extended/long address (optional)
Passess Pre-Shared Key for the Device to the NCP in the commissioning process.
When the Extended address is ommited all Devices which provided a valid PSKd are allowed to join the Thread Network.
### PROP 5392: PROP_THREAD_COMMISSIONER_ENABLED {#prop-thread-commissioner-enabled}
* Type: Read-Write
* Packed-Encoding: `b`
Set to true to enable the native commissioner. It is mandatory before adding the joiner to the network.
+12
View File
@@ -224,6 +224,18 @@ OTAPI ThreadError OTCALL otCommissionerSendMgmtSet(otInstance *, const otCommiss
*/
OTAPI uint16_t OTCALL otCommissionerGetSessionId(otInstance *);
/**
* This function returns the Commissioner State.
*
* @param[in] aInstance A pointer to an OpenThread instance.
*
* @retval kCommissionerStateDisabled Commissioner disabled.
* @retval kCommissionerStatePetition Becoming the commissioner.
* @retval kCommissionerStateActive Commissioner enabled.
*
*/
OTAPI otCommissionerState OTCALL otCommissionerGetState(otInstance *);
/**
* This method generates PSKc.
*
+12
View File
@@ -282,6 +282,18 @@ enum
OT_SECURITY_POLICY_BEACONS = 1 << 3, ///< Beacons enabled
};
/**
* This enumeration defines the Commissioner State.
*
*/
typedef enum otCommissionerState
{
kCommissionerStateDisabled = 0,
kCommissionerStatePetition = 1,
kCommissionerStateActive = 2,
} otCommissionerState;
/**
* This type represents Channel Mask Page 0.
*
+5
View File
@@ -110,6 +110,11 @@ uint16_t otCommissionerGetSessionId(otInstance *aInstance)
return aInstance->mThreadNetif.GetCommissioner().GetSessionId();
}
otCommissionerState otCommissionerGetState(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetCommissioner().GetState();
}
ThreadError otCommissionerGeneratePSKc(otInstance *aInstance, const char *aPassPhrase, const char *aNetworkName,
const uint8_t *aExtPanId, uint8_t *aPSKc)
{
+19 -18
View File
@@ -55,6 +55,7 @@
#include <thread/thread_netif.hpp>
#include <thread/thread_tlvs.hpp>
#include <thread/thread_uris.hpp>
#include <openthread/types.h>
using Thread::Encoding::BigEndian::HostSwap64;
@@ -65,7 +66,7 @@ Commissioner::Commissioner(ThreadNetif &aThreadNetif):
mAnnounceBegin(aThreadNetif),
mEnergyScan(aThreadNetif),
mPanIdQuery(aThreadNetif),
mState(kStateDisabled),
mState(kCommissionerStateDisabled),
mJoinerPort(0),
mJoinerRloc(0),
mJoinerExpirationTimer(aThreadNetif.GetIp6().mTimerScheduler, HandleJoinerExpirationTimer, this),
@@ -93,11 +94,11 @@ ThreadError Commissioner::Start(void)
ThreadError error = kThreadError_None;
otLogFuncEntry();
VerifyOrExit(mState == kStateDisabled, error = kThreadError_InvalidState);
VerifyOrExit(mState == kCommissionerStateDisabled, error = kThreadError_InvalidState);
SuccessOrExit(error = mNetif.GetSecureCoapServer().Start(SendRelayTransmit, this));
mState = kStatePetition;
mState = kCommissionerStatePetition;
mTransmitAttempts = 0;
SendPetition();
@@ -112,11 +113,11 @@ ThreadError Commissioner::Stop(void)
ThreadError error = kThreadError_None;
otLogFuncEntry();
VerifyOrExit(mState != kStateDisabled, error = kThreadError_InvalidState);
VerifyOrExit(mState != kCommissionerStateDisabled, error = kThreadError_InvalidState);
mNetif.GetSecureCoapServer().Stop();
mState = kStateDisabled;
mState = kCommissionerStateDisabled;
mTransmitAttempts = 0;
mTimer.Stop();
@@ -137,7 +138,7 @@ ThreadError Commissioner::SendCommissionerSet(void)
SteeringDataTlv steeringData;
otLogFuncEntry();
VerifyOrExit(mState == kStateActive, error = kThreadError_InvalidState);
VerifyOrExit(mState == kCommissionerStateActive, error = kThreadError_InvalidState);
memset(&dataset, 0, sizeof(dataset));
@@ -292,7 +293,7 @@ uint16_t Commissioner::GetSessionId(void) const
return mSessionId;
}
uint8_t Commissioner::GetState(void) const
otCommissionerState Commissioner::GetState(void) const
{
return mState;
}
@@ -306,14 +307,14 @@ void Commissioner::HandleTimer(void)
{
switch (mState)
{
case kStateDisabled:
case kCommissionerStateDisabled:
break;
case kStatePetition:
case kCommissionerStatePetition:
SendPetition();
break;
case kStateActive:
case kCommissionerStateActive:
SendKeepAlive();
break;
}
@@ -614,7 +615,7 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Header *aHeader, Message *
otLogFuncEntry();
VerifyOrExit(mState == kStatePetition, mState = kStateDisabled);
VerifyOrExit(mState == kCommissionerStatePetition, mState = kCommissionerStateDisabled);
VerifyOrExit(aResult == kThreadError_None &&
aHeader->GetCode() == kCoapResponseChanged, retransmit = true);
@@ -623,13 +624,13 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Header *aHeader, Message *
SuccessOrExit(Tlv::GetTlv(*aMessage, Tlv::kState, sizeof(state), state));
VerifyOrExit(state.IsValid(), ;);
VerifyOrExit(state.GetState() == StateTlv::kAccept, mState = kStateDisabled);
VerifyOrExit(state.GetState() == StateTlv::kAccept, mState = kCommissionerStateDisabled);
SuccessOrExit(Tlv::GetTlv(*aMessage, Tlv::kCommissionerSessionId, sizeof(sessionId), sessionId));
VerifyOrExit(sessionId.IsValid(), ;);
mSessionId = sessionId.GetCommissionerSessionId();
mState = kStateActive;
mState = kCommissionerStateActive;
mTransmitAttempts = 0;
mTimer.Start(Timer::SecToMsec(kKeepAliveTimeout) / 2);
@@ -641,7 +642,7 @@ exit:
{
if (mTransmitAttempts >= kPetitionRetryCount)
{
mState = kStateDisabled;
mState = kCommissionerStateDisabled;
}
else
{
@@ -671,7 +672,7 @@ ThreadError Commissioner::SendKeepAlive(void)
VerifyOrExit((message = mNetif.GetCoapClient().NewMeshCoPMessage(header)) != NULL, error = kThreadError_NoBufs);
state.Init();
state.SetState(mState == kStateActive ? StateTlv::kAccept : StateTlv::kReject);
state.SetState(mState == kCommissionerStateActive ? StateTlv::kAccept : StateTlv::kReject);
SuccessOrExit(error = message->Append(&state, sizeof(state)));
sessionId.Init();
@@ -713,16 +714,16 @@ void Commissioner::HandleLeaderKeepAliveResponse(Coap::Header *aHeader, Message
otLogFuncEntry();
VerifyOrExit(mState == kStateActive, mState = kStateDisabled);
VerifyOrExit(mState == kCommissionerStateActive, mState = kCommissionerStateDisabled);
VerifyOrExit(aResult == kThreadError_None &&
aHeader->GetCode() == kCoapResponseChanged, mState = kStateDisabled);
aHeader->GetCode() == kCoapResponseChanged, mState = kCommissionerStateDisabled);
otLogInfoMeshCoP(GetInstance(), "received Leader Petition response");
SuccessOrExit(Tlv::GetTlv(*aMessage, Tlv::kState, sizeof(state), state));
VerifyOrExit(state.IsValid(), ;);
VerifyOrExit(state.GetState() == StateTlv::kAccept, mState = kStateDisabled);
VerifyOrExit(state.GetState() == StateTlv::kAccept, mState = kCommissionerStateDisabled);
mTimer.Start(Timer::SecToMsec(kKeepAliveTimeout) / 2);
+8 -15
View File
@@ -140,23 +140,16 @@ public:
uint16_t GetSessionId(void) const;
/**
* Commissioner State.
*
*/
enum
{
kStateDisabled = 0,
kStatePetition = 1,
kStateActive = 2,
};
/**
* This method returns the Commissioner State.
* This function returns the Commissioner State.
*
* @returns The Commissioner State.
* @param[in] aInstance A pointer to an OpenThread instance.
*
* @retval kCommissionerStateDisabled Commissioner disabled.
* @retval kCommissionerStatePetition Becoming the commissioner.
* @retval kCommissionerStateActive Commissioner enabled.
*
*/
uint8_t GetState(void) const;
otCommissionerState GetState(void) const;
/**
* This method sends MGMT_COMMISSIONER_GET.
@@ -260,7 +253,7 @@ private:
ThreadError SendPetition(void);
ThreadError SendKeepAlive(void);
uint8_t mState;
otCommissionerState mState;
struct Joiner
{
+2 -1
View File
@@ -53,6 +53,7 @@
#include <thread/thread_tlvs.hpp>
#include <thread/thread_uris.hpp>
#include <meshcop/leader.hpp>
#include <openthread/types.h>
namespace Thread {
namespace MeshCoP {
@@ -584,7 +585,7 @@ ThreadError DatasetManager::SendSetRequest(const otOperationalDataset &aDataset,
#if OPENTHREAD_ENABLE_COMMISSIONER
bool isCommissioner;
isCommissioner = mNetif.GetCommissioner().GetState() != Commissioner::kStateDisabled ? true : false;
isCommissioner = mNetif.GetCommissioner().GetState() != kCommissionerStateDisabled ? true : false;
if (isCommissioner)
{
+135
View File
@@ -47,6 +47,10 @@
#include "openthread/jam_detection.h"
#endif
#if OPENTHREAD_ENABLE_COMMISSIONER
#include "meshcop/commissioner.hpp"
#endif
#include "openthread/platform/radio.h"
#include "openthread/platform/misc.h"
@@ -154,6 +158,10 @@ const NcpBase::GetPropertyHandlerEntry NcpBase::mGetPropertyHandlerTable[] =
{ SPINEL_PROP_THREAD_ALLOW_LOCAL_NET_DATA_CHANGE, &NcpBase::GetPropertyHandler_THREAD_ALLOW_LOCAL_NET_DATA_CHANGE },
{ SPINEL_PROP_THREAD_ROUTER_ROLE_ENABLED, &NcpBase::GetPropertyHandler_THREAD_ROUTER_ROLE_ENABLED },
#if OPENTHREAD_ENABLE_COMMISSIONER
{ SPINEL_PROP_THREAD_COMMISSIONER_ENABLED, &NcpBase::GetPropertyHandler_THREAD_COMMISSIONER_ENABLED },
#endif
{ SPINEL_PROP_MAC_WHITELIST, &NcpBase::GetPropertyHandler_MAC_WHITELIST },
{ SPINEL_PROP_MAC_WHITELIST_ENABLED, &NcpBase::GetPropertyHandler_MAC_WHITELIST_ENABLED },
{ SPINEL_PROP_THREAD_MODE, &NcpBase::GetPropertyHandler_THREAD_MODE },
@@ -312,6 +320,10 @@ const NcpBase::SetPropertyHandlerEntry NcpBase::mSetPropertyHandlerTable[] =
#if OPENTHREAD_ENABLE_LEGACY
{ SPINEL_PROP_NEST_LEGACY_ULA_PREFIX, &NcpBase::SetPropertyHandler_NEST_LEGACY_ULA_PREFIX },
#endif
#if OPENTHREAD_ENABLE_COMMISSIONER
{ SPINEL_PROP_THREAD_COMMISSIONER_ENABLED, &NcpBase::SetPropertyHandler_THREAD_COMMISSIONER_ENABLED },
#endif
};
const NcpBase::InsertPropertyHandlerEntry NcpBase::mInsertPropertyHandlerTable[] =
@@ -325,6 +337,10 @@ const NcpBase::InsertPropertyHandlerEntry NcpBase::mInsertPropertyHandlerTable[]
{ SPINEL_PROP_THREAD_ON_MESH_NETS, &NcpBase::InsertPropertyHandler_THREAD_ON_MESH_NETS },
{ SPINEL_PROP_THREAD_ASSISTING_PORTS, &NcpBase::InsertPropertyHandler_THREAD_ASSISTING_PORTS },
#if OPENTHREAD_ENABLE_COMMISSIONER
{ SPINEL_PROP_THREAD_JOINERS, &NcpBase::NcpBase::InsertPropertyHandler_THREAD_JOINERS },
#endif
{ SPINEL_PROP_CNTR_RESET, &NcpBase::SetPropertyHandler_CNTR_RESET },
{ SPINEL_PROP_MAC_WHITELIST, &NcpBase::InsertPropertyHandler_MAC_WHITELIST },
@@ -3364,6 +3380,23 @@ ThreadError NcpBase::GetPropertyHandler_THREAD_NETWORK_ID_TIMEOUT(uint8_t header
);
}
#if OPENTHREAD_ENABLE_COMMISSIONER
ThreadError NcpBase::GetPropertyHandler_THREAD_COMMISSIONER_ENABLED(uint8_t header, spinel_prop_key_t key)
{
bool isEnabled = false;
if (otCommissionerGetState(mInstance) == kCommissionerStateActive)
isEnabled = true;
return SendPropertyUpdate(
header,
SPINEL_CMD_PROP_VALUE_IS,
key,
SPINEL_DATATYPE_BOOL_S,
isEnabled
);
}
#endif
ThreadError NcpBase::GetPropertyHandler_NET_REQUIRE_JOIN_EXISTING(uint8_t header, spinel_prop_key_t key)
{
return SendPropertyUpdate(
@@ -4802,6 +4835,42 @@ ThreadError NcpBase::SetPropertyHandler_CNTR_RESET(uint8_t header, spinel_prop_k
return SendLastStatus(header, ThreadErrorToSpinelStatus(errorCode));
}
#if OPENTHREAD_ENABLE_COMMISSIONER
ThreadError NcpBase::SetPropertyHandler_THREAD_COMMISSIONER_ENABLED(uint8_t header, spinel_prop_key_t key, const uint8_t *value_ptr,
uint16_t value_len)
{
bool value = false;
spinel_ssize_t parsedLength;
ThreadError errorCode = kThreadError_None;
(void)key;
parsedLength = spinel_datatype_unpack(
value_ptr,
value_len,
SPINEL_DATATYPE_BOOL_S,
&value
);
if (parsedLength > 0)
{
if (value == false)
{
errorCode = otCommissionerStop(mInstance);
}
else
{
errorCode = otCommissionerStart(mInstance);
}
}
else
{
errorCode = kThreadError_Parse;
}
return SendLastStatus(header, ThreadErrorToSpinelStatus(errorCode));
}
#endif // OPENTHREAD_ENABLE_COMMISSIONER
ThreadError NcpBase::SetPropertyHandler_MAC_WHITELIST(uint8_t header, spinel_prop_key_t key, const uint8_t *value_ptr,
uint16_t value_len)
{
@@ -6048,6 +6117,72 @@ ThreadError NcpBase::InsertPropertyHandler_MAC_WHITELIST(uint8_t header, spinel_
return errorCode;
}
#if OPENTHREAD_ENABLE_COMMISSIONER
ThreadError NcpBase::InsertPropertyHandler_THREAD_JOINERS(uint8_t header, spinel_prop_key_t key,
const uint8_t *value_ptr, uint16_t value_len)
{
spinel_ssize_t parsedLength;
ThreadError errorCode = kThreadError_None;
otExtAddress *ot_ext_address = NULL;
const char *aPSKd = NULL;
uint32_t joiner_timeout = 0;
VerifyOrExit(
mAllowLocalNetworkDataChange == true,
errorCode = SendLastStatus(header, SPINEL_STATUS_INVALID_STATE)
);
parsedLength = spinel_datatype_unpack(
value_ptr,
value_len,
"ULE",
&aPSKd,
&joiner_timeout,
&ot_ext_address
);
if (parsedLength <= 0)
{
parsedLength = spinel_datatype_unpack(
value_ptr,
value_len,
"UL",
&aPSKd,
&joiner_timeout
);
ot_ext_address = NULL;
}
if (parsedLength > 0)
{
errorCode = otCommissionerAddJoiner(mInstance, ot_ext_address, aPSKd, joiner_timeout);
if (errorCode == kThreadError_None)
{
errorCode = SendPropertyUpdate(
header,
SPINEL_CMD_PROP_VALUE_INSERTED,
key,
value_ptr,
value_len
);
}
else
{
errorCode = SendLastStatus(header, ThreadErrorToSpinelStatus(errorCode));
}
}
else
{
errorCode = SendLastStatus(header, SPINEL_STATUS_PARSE_ERROR);
}
exit:
return errorCode;
}
#endif // OPENTHREAD_ENABLE_COMMISSIONER
// ----------------------------------------------------------------------------
// MARK: Individual Property Removers
// ----------------------------------------------------------------------------
+12
View File
@@ -394,6 +394,10 @@ private:
ThreadError GetPropertyHandler_DEBUG_TEST_ASSERT(uint8_t header, spinel_prop_key_t key);
ThreadError GetPropertyHandler_DEBUG_NCP_LOG_LEVEL(uint8_t header, spinel_prop_key_t key);
#if OPENTHREAD_ENABLE_COMMISSIONER
ThreadError GetPropertyHandler_THREAD_COMMISSIONER_ENABLED(uint8_t header, spinel_prop_key_t key);
#endif
#if OPENTHREAD_ENABLE_JAM_DETECTION
ThreadError GetPropertyHandler_JAM_DETECT_ENABLE(uint8_t header, spinel_prop_key_t key);
ThreadError GetPropertyHandler_JAM_DETECTED(uint8_t header, spinel_prop_key_t key);
@@ -513,6 +517,10 @@ private:
uint16_t value_len);
ThreadError SetPropertyHandler_DEBUG_NCP_LOG_LEVEL(uint8_t header, spinel_prop_key_t key, const uint8_t *value_ptr,
uint16_t value_len);
#if OPENTHREAD_ENABLE_COMMISSIONER
ThreadError SetPropertyHandler_THREAD_COMMISSIONER_ENABLED(uint8_t header, spinel_prop_key_t key,
const uint8_t *value_ptr, uint16_t value_len);
#endif
#if OPENTHREAD_ENABLE_JAM_DETECTION
ThreadError SetPropertyHandler_JAM_DETECT_ENABLE(uint8_t header, spinel_prop_key_t key, const uint8_t *value_ptr,
@@ -551,6 +559,10 @@ private:
const uint8_t *value_ptr, uint16_t value_len);
ThreadError InsertPropertyHandler_MAC_WHITELIST(uint8_t header, spinel_prop_key_t key, const uint8_t *value_ptr,
uint16_t value_len);
#if OPENTHREAD_ENABLE_COMMISSIONER
ThreadError InsertPropertyHandler_THREAD_JOINERS(uint8_t header, spinel_prop_key_t key, const uint8_t *value_ptr,
uint16_t value_len);
#endif
#if OPENTHREAD_ENABLE_RAW_LINK_API
ThreadError RemovePropertyHandler_MAC_SRC_MATCH_SHORT_ADDRESSES(uint8_t header, spinel_prop_key_t key,
+8
View File
@@ -1163,6 +1163,14 @@ spinel_prop_key_to_cstr(spinel_prop_key_t prop_key)
ret = "PROP_THREAD_ALLOW_LOCAL_NET_DATA_CHANGE";
break;
case SPINEL_PROP_THREAD_JOINERS:
ret = "SPINEL_PROP_THREAD_JOINERS";
break;
case SPINEL_PROP_THREAD_COMMISSIONER_ENABLED:
ret = "SPINEL_PROP_THREAD_COMMISSIONER_ENABLED";
break;
case SPINEL_PROP_THREAD_RLOC16_DEBUG_PASSTHRU:
ret = "PROP_THREAD_RLOC16_DEBUG_PASSTHRU";
break;
+414 -407
View File
File diff suppressed because it is too large Load Diff