[commissioner] add State and JoinerEvent enums (c++ style constants) (#5521)

This commit is contained in:
Abtin Keshavarzian
2020-09-14 08:38:52 -07:00
committed by GitHub
parent 5bffd8bc4d
commit a481fe6bdb
3 changed files with 67 additions and 52 deletions
+1 -1
View File
@@ -201,7 +201,7 @@ otCommissionerState otCommissionerGetState(otInstance *aInstance)
{ {
Instance &instance = *static_cast<Instance *>(aInstance); Instance &instance = *static_cast<Instance *>(aInstance);
return instance.Get<MeshCoP::Commissioner>().GetState(); return static_cast<otCommissionerState>(instance.Get<MeshCoP::Commissioner>().GetState());
} }
#endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE #endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE
+37 -38
View File
@@ -70,7 +70,7 @@ Commissioner::Commissioner(Instance &aInstance)
, mAnnounceBegin(aInstance) , mAnnounceBegin(aInstance)
, mEnergyScan(aInstance) , mEnergyScan(aInstance)
, mPanIdQuery(aInstance) , mPanIdQuery(aInstance)
, mState(OT_COMMISSIONER_STATE_DISABLED) , mState(kStateDisabled)
, mStateCallback(nullptr) , mStateCallback(nullptr)
, mJoinerCallback(nullptr) , mJoinerCallback(nullptr)
, mCallbackContext(nullptr) , mCallbackContext(nullptr)
@@ -87,9 +87,10 @@ Commissioner::Commissioner(Instance &aInstance)
mProvisioningUrl[0] = '\0'; mProvisioningUrl[0] = '\0';
} }
void Commissioner::SetState(otCommissionerState aState) void Commissioner::SetState(State aState)
{ {
otCommissionerState oldState = mState; State oldState = mState;
OT_UNUSED_VARIABLE(oldState); OT_UNUSED_VARIABLE(oldState);
SuccessOrExit(Get<Notifier>().Update(mState, aState, kEventCommissionerStateChanged)); SuccessOrExit(Get<Notifier>().Update(mState, aState, kEventCommissionerStateChanged));
@@ -98,14 +99,14 @@ void Commissioner::SetState(otCommissionerState aState)
if (mStateCallback) if (mStateCallback)
{ {
mStateCallback(mState, mCallbackContext); mStateCallback(static_cast<otCommissionerState>(mState), mCallbackContext);
} }
exit: exit:
return; return;
} }
void Commissioner::SignalJoinerEvent(otCommissionerJoinerEvent aEvent, const Joiner *aJoiner) const void Commissioner::SignalJoinerEvent(JoinerEvent aEvent, const Joiner *aJoiner) const
{ {
otJoinerInfo joinerInfo; otJoinerInfo joinerInfo;
Mac::ExtAddress joinerId; Mac::ExtAddress joinerId;
@@ -128,7 +129,8 @@ void Commissioner::SignalJoinerEvent(otCommissionerJoinerEvent aEvent, const Joi
noJoinerId = true; noJoinerId = true;
} }
mJoinerCallback(aEvent, &joinerInfo, noJoinerId ? nullptr : &joinerId, mCallbackContext); mJoinerCallback(static_cast<otCommissionerJoinerEvent>(aEvent), &joinerInfo, noJoinerId ? nullptr : &joinerId,
mCallbackContext);
exit: exit:
return; return;
@@ -155,7 +157,7 @@ void Commissioner::HandleCoapsConnected(bool aConnected, void *aContext)
void Commissioner::HandleCoapsConnected(bool aConnected) void Commissioner::HandleCoapsConnected(bool aConnected)
{ {
SignalJoinerEvent(aConnected ? OT_COMMISSIONER_JOINER_CONNECTED : OT_COMMISSIONER_JOINER_END, mActiveJoiner); SignalJoinerEvent(aConnected ? kJoinerEventConnected : kJoinerEventEnd, mActiveJoiner);
} }
Commissioner::Joiner *Commissioner::GetUnusedJoinerEntry(void) Commissioner::Joiner *Commissioner::GetUnusedJoinerEntry(void)
@@ -290,7 +292,7 @@ void Commissioner::RemoveJoinerEntry(Commissioner::Joiner &aJoiner)
SendCommissionerSet(); SendCommissionerSet();
LogJoinerEntry("Removed", joinerCopy); LogJoinerEntry("Removed", joinerCopy);
SignalJoinerEvent(OT_COMMISSIONER_JOINER_REMOVED, &joinerCopy); SignalJoinerEvent(kJoinerEventRemoved, &joinerCopy);
} }
otError Commissioner::Start(otCommissionerStateCallback aStateCallback, otError Commissioner::Start(otCommissionerStateCallback aStateCallback,
@@ -300,7 +302,7 @@ otError Commissioner::Start(otCommissionerStateCallback aStateCallback,
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
VerifyOrExit(Get<Mle::MleRouter>().IsAttached(), error = OT_ERROR_INVALID_STATE); VerifyOrExit(Get<Mle::MleRouter>().IsAttached(), error = OT_ERROR_INVALID_STATE);
VerifyOrExit(mState == OT_COMMISSIONER_STATE_DISABLED, error = OT_ERROR_ALREADY); VerifyOrExit(mState == kStateDisabled, error = OT_ERROR_ALREADY);
#if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
error = Get<MeshCoP::BorderAgent>().Stop(); error = Get<MeshCoP::BorderAgent>().Stop();
@@ -316,7 +318,7 @@ otError Commissioner::Start(otCommissionerStateCallback aStateCallback,
mTransmitAttempts = 0; mTransmitAttempts = 0;
SuccessOrExit(error = SendPetition()); SuccessOrExit(error = SendPetition());
SetState(OT_COMMISSIONER_STATE_PETITION); SetState(kStatePetition);
exit: exit:
if (error != OT_ERROR_NONE) if (error != OT_ERROR_NONE)
@@ -336,25 +338,25 @@ otError Commissioner::Stop(bool aResign)
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
bool needResign = false; bool needResign = false;
VerifyOrExit(mState != OT_COMMISSIONER_STATE_DISABLED, error = OT_ERROR_ALREADY); VerifyOrExit(mState != kStateDisabled, error = OT_ERROR_ALREADY);
Get<Coap::CoapSecure>().Stop(); Get<Coap::CoapSecure>().Stop();
if (mState == OT_COMMISSIONER_STATE_ACTIVE) if (mState == kStateActive)
{ {
Get<ThreadNetif>().RemoveUnicastAddress(mCommissionerAloc); Get<ThreadNetif>().RemoveUnicastAddress(mCommissionerAloc);
RemoveCoapResources(); RemoveCoapResources();
ClearJoiners(); ClearJoiners();
needResign = true; needResign = true;
} }
else if (mState == OT_COMMISSIONER_STATE_PETITION) else if (mState == kStatePetition)
{ {
mTransmitAttempts = 0; mTransmitAttempts = 0;
} }
mTimer.Stop(); mTimer.Stop();
SetState(OT_COMMISSIONER_STATE_DISABLED); SetState(kStateDisabled);
if (needResign && aResign) if (needResign && aResign)
{ {
@@ -407,7 +409,7 @@ void Commissioner::SendCommissionerSet(void)
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
otCommissioningDataset dataset; otCommissioningDataset dataset;
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE); VerifyOrExit(mState == kStateActive, error = OT_ERROR_INVALID_STATE);
memset(&dataset, 0, sizeof(dataset)); memset(&dataset, 0, sizeof(dataset));
@@ -444,7 +446,7 @@ otError Commissioner::AddJoiner(const Mac::ExtAddress *aEui64,
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
Joiner *joiner; Joiner *joiner;
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE); VerifyOrExit(mState == kStateActive, error = OT_ERROR_INVALID_STATE);
if (aDiscerner != nullptr) if (aDiscerner != nullptr)
{ {
@@ -549,7 +551,7 @@ otError Commissioner::RemoveJoiner(const Mac::ExtAddress *aEui64, const JoinerDi
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
Joiner *joiner; Joiner *joiner;
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE); VerifyOrExit(mState == kStateActive, error = OT_ERROR_INVALID_STATE);
if (aDiscerner != nullptr) if (aDiscerner != nullptr)
{ {
@@ -618,14 +620,14 @@ void Commissioner::HandleTimer(void)
{ {
switch (mState) switch (mState)
{ {
case OT_COMMISSIONER_STATE_DISABLED: case kStateDisabled:
break; break;
case OT_COMMISSIONER_STATE_PETITION: case kStatePetition:
IgnoreError(SendPetition()); IgnoreError(SendPetition());
break; break;
case OT_COMMISSIONER_STATE_ACTIVE: case kStateActive:
SendKeepAlive(); SendKeepAlive();
break; break;
} }
@@ -894,9 +896,9 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Message * aMessage
uint8_t state; uint8_t state;
bool retransmit = false; bool retransmit = false;
VerifyOrExit(mState != OT_COMMISSIONER_STATE_ACTIVE, OT_NOOP); VerifyOrExit(mState != kStateActive, OT_NOOP);
VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == Coap::kCodeChanged, VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == Coap::kCodeChanged,
retransmit = (mState == OT_COMMISSIONER_STATE_PETITION)); retransmit = (mState == kStatePetition));
otLogInfoMeshCoP("received Leader Petition response"); otLogInfoMeshCoP("received Leader Petition response");
@@ -907,7 +909,7 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Message * aMessage
// reject this session by sending KeepAlive reject if commissioner is in disabled state // reject this session by sending KeepAlive reject if commissioner is in disabled state
// this could happen if commissioner is stopped by API during petitioning // this could happen if commissioner is stopped by API during petitioning
if (mState == OT_COMMISSIONER_STATE_DISABLED) if (mState == kStateDisabled)
{ {
SendKeepAlive(mSessionId); SendKeepAlive(mSessionId);
ExitNow(); ExitNow();
@@ -917,7 +919,7 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Message * aMessage
Get<ThreadNetif>().AddUnicastAddress(mCommissionerAloc); Get<ThreadNetif>().AddUnicastAddress(mCommissionerAloc);
AddCoapResources(); AddCoapResources();
SetState(OT_COMMISSIONER_STATE_ACTIVE); SetState(kStateActive);
mTransmitAttempts = 0; mTransmitAttempts = 0;
mTimer.Start(Time::SecToMsec(kKeepAliveTimeout) / 2); mTimer.Start(Time::SecToMsec(kKeepAliveTimeout) / 2);
@@ -953,9 +955,8 @@ void Commissioner::SendKeepAlive(uint16_t aSessionId)
SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kLeaderKeepAlive)); SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kLeaderKeepAlive));
SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = message->SetPayloadMarker());
SuccessOrExit( SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState,
error = Tlv::AppendUint8Tlv(*message, Tlv::kState, (mState == kStateActive) ? StateTlv::kAccept : StateTlv::kReject));
(mState == OT_COMMISSIONER_STATE_ACTIVE) ? StateTlv::kAccept : StateTlv::kReject));
SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kCommissionerSessionId, aSessionId)); SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kCommissionerSessionId, aSessionId));
@@ -997,7 +998,7 @@ void Commissioner::HandleLeaderKeepAliveResponse(Coap::Message * aMessag
uint8_t state; uint8_t state;
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, OT_NOOP); VerifyOrExit(mState == kStateActive, OT_NOOP);
VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == Coap::kCodeChanged, VerifyOrExit(aResult == OT_ERROR_NONE && aMessage->GetCode() == Coap::kCodeChanged,
IgnoreError(Stop(/* aResign */ false))); IgnoreError(Stop(/* aResign */ false)));
@@ -1030,7 +1031,7 @@ void Commissioner::HandleRelayReceive(Coap::Message &aMessage, const Ip6::Messag
uint16_t offset; uint16_t offset;
uint16_t length; uint16_t length;
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE); VerifyOrExit(mState == kStateActive, error = OT_ERROR_INVALID_STATE);
VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), OT_NOOP); VerifyOrExit(aMessage.IsNonConfirmablePostRequest(), OT_NOOP);
@@ -1056,7 +1057,7 @@ void Commissioner::HandleRelayReceive(Coap::Message &aMessage, const Ip6::Messag
mActiveJoiner = joiner; mActiveJoiner = joiner;
LogJoinerEntry("Starting new session with", *joiner); LogJoinerEntry("Starting new session with", *joiner);
SignalJoinerEvent(OT_COMMISSIONER_JOINER_START, joiner); SignalJoinerEvent(kJoinerEventStart, joiner);
} }
else else
{ {
@@ -1170,7 +1171,7 @@ void Commissioner::SendJoinFinalizeResponse(const Coap::Message &aRequest, State
SuccessOrExit(error = Get<Coap::CoapSecure>().SendMessage(*message, joinerMessageInfo)); SuccessOrExit(error = Get<Coap::CoapSecure>().SendMessage(*message, joinerMessageInfo));
SignalJoinerEvent(OT_COMMISSIONER_JOINER_FINALIZE, mActiveJoiner); SignalJoinerEvent(kJoinerEventFinalize, mActiveJoiner);
if ((mActiveJoiner != nullptr) && (mActiveJoiner->mType != Joiner::kTypeAny)) if ((mActiveJoiner != nullptr) && (mActiveJoiner->mType != Joiner::kTypeAny))
{ {
@@ -1246,7 +1247,7 @@ exit:
void Commissioner::ApplyMeshLocalPrefix(void) void Commissioner::ApplyMeshLocalPrefix(void)
{ {
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, OT_NOOP); VerifyOrExit(mState == kStateActive, OT_NOOP);
Get<ThreadNetif>().RemoveUnicastAddress(mCommissionerAloc); Get<ThreadNetif>().RemoveUnicastAddress(mCommissionerAloc);
mCommissionerAloc.GetAddress().SetPrefix(Get<Mle::MleRouter>().GetMeshLocalPrefix()); mCommissionerAloc.GetAddress().SetPrefix(Get<Mle::MleRouter>().GetMeshLocalPrefix());
@@ -1260,23 +1261,21 @@ exit:
#if (OPENTHREAD_CONFIG_LOG_LEVEL >= OT_LOG_LEVEL_INFO) && (OPENTHREAD_CONFIG_LOG_MESHCOP == 1) #if (OPENTHREAD_CONFIG_LOG_LEVEL >= OT_LOG_LEVEL_INFO) && (OPENTHREAD_CONFIG_LOG_MESHCOP == 1)
const char *Commissioner::StateToString(otCommissionerState aState) const char *Commissioner::StateToString(State aState)
{ {
const char *str = "Unknown"; const char *str = "Unknown";
switch (aState) switch (aState)
{ {
case OT_COMMISSIONER_STATE_DISABLED: case kStateDisabled:
str = "disabled"; str = "disabled";
break; break;
case OT_COMMISSIONER_STATE_PETITION: case kStatePetition:
str = "petition"; str = "petition";
break; break;
case OT_COMMISSIONER_STATE_ACTIVE: case kStateActive:
str = "active"; str = "active";
break; break;
default:
break;
} }
return str; return str;
+29 -13
View File
@@ -59,6 +59,17 @@ namespace MeshCoP {
class Commissioner : public InstanceLocator class Commissioner : public InstanceLocator
{ {
public: public:
/**
* This enumeration type represents the Commissioner State.
*
*/
enum State : uint8_t
{
kStateDisabled = OT_COMMISSIONER_STATE_DISABLED, ///< Disabled.
kStatePetition = OT_COMMISSIONER_STATE_PETITION, ///< Petitioning to become a Commissioner.
kStateActive = OT_COMMISSIONER_STATE_ACTIVE, ///< Active Commissioner.
};
/** /**
* This constructor initializes the Commissioner object. * This constructor initializes the Commissioner object.
* *
@@ -236,7 +247,7 @@ public:
* @returns TRUE if the Commissioner role is active, FALSE otherwise. * @returns TRUE if the Commissioner role is active, FALSE otherwise.
* *
*/ */
bool IsActive(void) const { return mState == OT_COMMISSIONER_STATE_ACTIVE; } bool IsActive(void) const { return mState == kStateActive; }
/** /**
* This method indicates whether or not the Commissioner role is disabled. * This method indicates whether or not the Commissioner role is disabled.
@@ -244,19 +255,15 @@ public:
* @returns TRUE if the Commissioner role is disabled, FALSE otherwise. * @returns TRUE if the Commissioner role is disabled, FALSE otherwise.
* *
*/ */
bool IsDisabled(void) const { return mState == OT_COMMISSIONER_STATE_DISABLED; } bool IsDisabled(void) const { return mState == kStateDisabled; }
/** /**
* This function returns the Commissioner State. * This method gets the Commissioner State.
* *
* @param[in] aInstance A pointer to an OpenThread instance. * @returns The Commissioner State.
*
* @retval OT_COMMISSIONER_STATE_DISABLED Commissioner disabled.
* @retval OT_COMMISSIONER_STATE_PETITION Becoming the commissioner.
* @retval OT_COMMISSIONER_STATE_ACTIVE Commissioner enabled.
* *
*/ */
otCommissionerState GetState(void) const { return mState; } State GetState(void) const { return mState; }
/** /**
* This method sends MGMT_COMMISSIONER_GET. * This method sends MGMT_COMMISSIONER_GET.
@@ -327,6 +334,15 @@ private:
kRemoveJoinerDelay = 20, ///< Delay to remove successfully joined joiner kRemoveJoinerDelay = 20, ///< Delay to remove successfully joined joiner
}; };
enum JoinerEvent : uint8_t
{
kJoinerEventStart = OT_COMMISSIONER_JOINER_START,
kJoinerEventConnected = OT_COMMISSIONER_JOINER_CONNECTED,
kJoinerEventFinalize = OT_COMMISSIONER_JOINER_FINALIZE,
kJoinerEventEnd = OT_COMMISSIONER_JOINER_END,
kJoinerEventRemoved = OT_COMMISSIONER_JOINER_REMOVED,
};
struct Joiner struct Joiner
{ {
enum Type : uint8_t enum Type : uint8_t
@@ -423,11 +439,11 @@ private:
void SendKeepAlive(void); void SendKeepAlive(void);
void SendKeepAlive(uint16_t aSessionId); void SendKeepAlive(uint16_t aSessionId);
void SetState(otCommissionerState aState); void SetState(State aState);
void SignalJoinerEvent(otCommissionerJoinerEvent aEvent, const Joiner *aJoiner) const; void SignalJoinerEvent(JoinerEvent aEvent, const Joiner *aJoiner) const;
void LogJoinerEntry(const char *aAction, const Joiner &aJoiner) const; void LogJoinerEntry(const char *aAction, const Joiner &aJoiner) const;
static const char *StateToString(otCommissionerState aState); static const char *StateToString(State aState);
Joiner mJoiners[OPENTHREAD_CONFIG_COMMISSIONER_MAX_JOINER_ENTRIES]; Joiner mJoiners[OPENTHREAD_CONFIG_COMMISSIONER_MAX_JOINER_ENTRIES];
@@ -452,7 +468,7 @@ private:
char mProvisioningUrl[OT_PROVISIONING_URL_MAX_SIZE + 1]; // + 1 is for null char at end of string. char mProvisioningUrl[OT_PROVISIONING_URL_MAX_SIZE + 1]; // + 1 is for null char at end of string.
otCommissionerState mState; State mState;
otCommissionerStateCallback mStateCallback; otCommissionerStateCallback mStateCallback;
otCommissionerJoinerCallback mJoinerCallback; otCommissionerJoinerCallback mJoinerCallback;