mirror of
https://github.com/espressif/openthread.git
synced 2026-08-07 03:07:47 +00:00
[commissioner] add State and JoinerEvent enums (c++ style constants) (#5521)
This commit is contained in:
@@ -201,7 +201,7 @@ otCommissionerState otCommissionerGetState(otInstance *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
|
||||
|
||||
@@ -70,7 +70,7 @@ Commissioner::Commissioner(Instance &aInstance)
|
||||
, mAnnounceBegin(aInstance)
|
||||
, mEnergyScan(aInstance)
|
||||
, mPanIdQuery(aInstance)
|
||||
, mState(OT_COMMISSIONER_STATE_DISABLED)
|
||||
, mState(kStateDisabled)
|
||||
, mStateCallback(nullptr)
|
||||
, mJoinerCallback(nullptr)
|
||||
, mCallbackContext(nullptr)
|
||||
@@ -87,9 +87,10 @@ Commissioner::Commissioner(Instance &aInstance)
|
||||
mProvisioningUrl[0] = '\0';
|
||||
}
|
||||
|
||||
void Commissioner::SetState(otCommissionerState aState)
|
||||
void Commissioner::SetState(State aState)
|
||||
{
|
||||
otCommissionerState oldState = mState;
|
||||
State oldState = mState;
|
||||
|
||||
OT_UNUSED_VARIABLE(oldState);
|
||||
|
||||
SuccessOrExit(Get<Notifier>().Update(mState, aState, kEventCommissionerStateChanged));
|
||||
@@ -98,14 +99,14 @@ void Commissioner::SetState(otCommissionerState aState)
|
||||
|
||||
if (mStateCallback)
|
||||
{
|
||||
mStateCallback(mState, mCallbackContext);
|
||||
mStateCallback(static_cast<otCommissionerState>(mState), mCallbackContext);
|
||||
}
|
||||
|
||||
exit:
|
||||
return;
|
||||
}
|
||||
|
||||
void Commissioner::SignalJoinerEvent(otCommissionerJoinerEvent aEvent, const Joiner *aJoiner) const
|
||||
void Commissioner::SignalJoinerEvent(JoinerEvent aEvent, const Joiner *aJoiner) const
|
||||
{
|
||||
otJoinerInfo joinerInfo;
|
||||
Mac::ExtAddress joinerId;
|
||||
@@ -128,7 +129,8 @@ void Commissioner::SignalJoinerEvent(otCommissionerJoinerEvent aEvent, const Joi
|
||||
noJoinerId = true;
|
||||
}
|
||||
|
||||
mJoinerCallback(aEvent, &joinerInfo, noJoinerId ? nullptr : &joinerId, mCallbackContext);
|
||||
mJoinerCallback(static_cast<otCommissionerJoinerEvent>(aEvent), &joinerInfo, noJoinerId ? nullptr : &joinerId,
|
||||
mCallbackContext);
|
||||
|
||||
exit:
|
||||
return;
|
||||
@@ -155,7 +157,7 @@ void Commissioner::HandleCoapsConnected(bool aConnected, void *aContext)
|
||||
|
||||
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)
|
||||
@@ -290,7 +292,7 @@ void Commissioner::RemoveJoinerEntry(Commissioner::Joiner &aJoiner)
|
||||
SendCommissionerSet();
|
||||
|
||||
LogJoinerEntry("Removed", joinerCopy);
|
||||
SignalJoinerEvent(OT_COMMISSIONER_JOINER_REMOVED, &joinerCopy);
|
||||
SignalJoinerEvent(kJoinerEventRemoved, &joinerCopy);
|
||||
}
|
||||
|
||||
otError Commissioner::Start(otCommissionerStateCallback aStateCallback,
|
||||
@@ -300,7 +302,7 @@ otError Commissioner::Start(otCommissionerStateCallback aStateCallback,
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
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
|
||||
error = Get<MeshCoP::BorderAgent>().Stop();
|
||||
@@ -316,7 +318,7 @@ otError Commissioner::Start(otCommissionerStateCallback aStateCallback,
|
||||
mTransmitAttempts = 0;
|
||||
|
||||
SuccessOrExit(error = SendPetition());
|
||||
SetState(OT_COMMISSIONER_STATE_PETITION);
|
||||
SetState(kStatePetition);
|
||||
|
||||
exit:
|
||||
if (error != OT_ERROR_NONE)
|
||||
@@ -336,25 +338,25 @@ otError Commissioner::Stop(bool aResign)
|
||||
otError error = OT_ERROR_NONE;
|
||||
bool needResign = false;
|
||||
|
||||
VerifyOrExit(mState != OT_COMMISSIONER_STATE_DISABLED, error = OT_ERROR_ALREADY);
|
||||
VerifyOrExit(mState != kStateDisabled, error = OT_ERROR_ALREADY);
|
||||
|
||||
Get<Coap::CoapSecure>().Stop();
|
||||
|
||||
if (mState == OT_COMMISSIONER_STATE_ACTIVE)
|
||||
if (mState == kStateActive)
|
||||
{
|
||||
Get<ThreadNetif>().RemoveUnicastAddress(mCommissionerAloc);
|
||||
RemoveCoapResources();
|
||||
ClearJoiners();
|
||||
needResign = true;
|
||||
}
|
||||
else if (mState == OT_COMMISSIONER_STATE_PETITION)
|
||||
else if (mState == kStatePetition)
|
||||
{
|
||||
mTransmitAttempts = 0;
|
||||
}
|
||||
|
||||
mTimer.Stop();
|
||||
|
||||
SetState(OT_COMMISSIONER_STATE_DISABLED);
|
||||
SetState(kStateDisabled);
|
||||
|
||||
if (needResign && aResign)
|
||||
{
|
||||
@@ -407,7 +409,7 @@ void Commissioner::SendCommissionerSet(void)
|
||||
otError error = OT_ERROR_NONE;
|
||||
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));
|
||||
|
||||
@@ -444,7 +446,7 @@ otError Commissioner::AddJoiner(const Mac::ExtAddress *aEui64,
|
||||
otError error = OT_ERROR_NONE;
|
||||
Joiner *joiner;
|
||||
|
||||
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE);
|
||||
VerifyOrExit(mState == kStateActive, error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
if (aDiscerner != nullptr)
|
||||
{
|
||||
@@ -549,7 +551,7 @@ otError Commissioner::RemoveJoiner(const Mac::ExtAddress *aEui64, const JoinerDi
|
||||
otError error = OT_ERROR_NONE;
|
||||
Joiner *joiner;
|
||||
|
||||
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE);
|
||||
VerifyOrExit(mState == kStateActive, error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
if (aDiscerner != nullptr)
|
||||
{
|
||||
@@ -618,14 +620,14 @@ void Commissioner::HandleTimer(void)
|
||||
{
|
||||
switch (mState)
|
||||
{
|
||||
case OT_COMMISSIONER_STATE_DISABLED:
|
||||
case kStateDisabled:
|
||||
break;
|
||||
|
||||
case OT_COMMISSIONER_STATE_PETITION:
|
||||
case kStatePetition:
|
||||
IgnoreError(SendPetition());
|
||||
break;
|
||||
|
||||
case OT_COMMISSIONER_STATE_ACTIVE:
|
||||
case kStateActive:
|
||||
SendKeepAlive();
|
||||
break;
|
||||
}
|
||||
@@ -894,9 +896,9 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Message * aMessage
|
||||
uint8_t state;
|
||||
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,
|
||||
retransmit = (mState == OT_COMMISSIONER_STATE_PETITION));
|
||||
retransmit = (mState == kStatePetition));
|
||||
|
||||
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
|
||||
// this could happen if commissioner is stopped by API during petitioning
|
||||
if (mState == OT_COMMISSIONER_STATE_DISABLED)
|
||||
if (mState == kStateDisabled)
|
||||
{
|
||||
SendKeepAlive(mSessionId);
|
||||
ExitNow();
|
||||
@@ -917,7 +919,7 @@ void Commissioner::HandleLeaderPetitionResponse(Coap::Message * aMessage
|
||||
Get<ThreadNetif>().AddUnicastAddress(mCommissionerAloc);
|
||||
|
||||
AddCoapResources();
|
||||
SetState(OT_COMMISSIONER_STATE_ACTIVE);
|
||||
SetState(kStateActive);
|
||||
|
||||
mTransmitAttempts = 0;
|
||||
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->SetPayloadMarker());
|
||||
|
||||
SuccessOrExit(
|
||||
error = Tlv::AppendUint8Tlv(*message, Tlv::kState,
|
||||
(mState == OT_COMMISSIONER_STATE_ACTIVE) ? StateTlv::kAccept : StateTlv::kReject));
|
||||
SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState,
|
||||
(mState == kStateActive) ? StateTlv::kAccept : StateTlv::kReject));
|
||||
|
||||
SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kCommissionerSessionId, aSessionId));
|
||||
|
||||
@@ -997,7 +998,7 @@ void Commissioner::HandleLeaderKeepAliveResponse(Coap::Message * aMessag
|
||||
|
||||
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,
|
||||
IgnoreError(Stop(/* aResign */ false)));
|
||||
|
||||
@@ -1030,7 +1031,7 @@ void Commissioner::HandleRelayReceive(Coap::Message &aMessage, const Ip6::Messag
|
||||
uint16_t offset;
|
||||
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);
|
||||
|
||||
@@ -1056,7 +1057,7 @@ void Commissioner::HandleRelayReceive(Coap::Message &aMessage, const Ip6::Messag
|
||||
mActiveJoiner = joiner;
|
||||
|
||||
LogJoinerEntry("Starting new session with", *joiner);
|
||||
SignalJoinerEvent(OT_COMMISSIONER_JOINER_START, joiner);
|
||||
SignalJoinerEvent(kJoinerEventStart, joiner);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1170,7 +1171,7 @@ void Commissioner::SendJoinFinalizeResponse(const Coap::Message &aRequest, State
|
||||
|
||||
SuccessOrExit(error = Get<Coap::CoapSecure>().SendMessage(*message, joinerMessageInfo));
|
||||
|
||||
SignalJoinerEvent(OT_COMMISSIONER_JOINER_FINALIZE, mActiveJoiner);
|
||||
SignalJoinerEvent(kJoinerEventFinalize, mActiveJoiner);
|
||||
|
||||
if ((mActiveJoiner != nullptr) && (mActiveJoiner->mType != Joiner::kTypeAny))
|
||||
{
|
||||
@@ -1246,7 +1247,7 @@ exit:
|
||||
|
||||
void Commissioner::ApplyMeshLocalPrefix(void)
|
||||
{
|
||||
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, OT_NOOP);
|
||||
VerifyOrExit(mState == kStateActive, OT_NOOP);
|
||||
|
||||
Get<ThreadNetif>().RemoveUnicastAddress(mCommissionerAloc);
|
||||
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)
|
||||
|
||||
const char *Commissioner::StateToString(otCommissionerState aState)
|
||||
const char *Commissioner::StateToString(State aState)
|
||||
{
|
||||
const char *str = "Unknown";
|
||||
|
||||
switch (aState)
|
||||
{
|
||||
case OT_COMMISSIONER_STATE_DISABLED:
|
||||
case kStateDisabled:
|
||||
str = "disabled";
|
||||
break;
|
||||
case OT_COMMISSIONER_STATE_PETITION:
|
||||
case kStatePetition:
|
||||
str = "petition";
|
||||
break;
|
||||
case OT_COMMISSIONER_STATE_ACTIVE:
|
||||
case kStateActive:
|
||||
str = "active";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return str;
|
||||
|
||||
@@ -59,6 +59,17 @@ namespace MeshCoP {
|
||||
class Commissioner : public InstanceLocator
|
||||
{
|
||||
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.
|
||||
*
|
||||
@@ -236,7 +247,7 @@ public:
|
||||
* @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.
|
||||
@@ -244,19 +255,15 @@ public:
|
||||
* @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.
|
||||
*
|
||||
* @retval OT_COMMISSIONER_STATE_DISABLED Commissioner disabled.
|
||||
* @retval OT_COMMISSIONER_STATE_PETITION Becoming the commissioner.
|
||||
* @retval OT_COMMISSIONER_STATE_ACTIVE Commissioner enabled.
|
||||
* @returns The Commissioner State.
|
||||
*
|
||||
*/
|
||||
otCommissionerState GetState(void) const { return mState; }
|
||||
State GetState(void) const { return mState; }
|
||||
|
||||
/**
|
||||
* This method sends MGMT_COMMISSIONER_GET.
|
||||
@@ -327,6 +334,15 @@ private:
|
||||
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
|
||||
{
|
||||
enum Type : uint8_t
|
||||
@@ -423,11 +439,11 @@ private:
|
||||
void SendKeepAlive(void);
|
||||
void SendKeepAlive(uint16_t aSessionId);
|
||||
|
||||
void SetState(otCommissionerState aState);
|
||||
void SignalJoinerEvent(otCommissionerJoinerEvent aEvent, const Joiner *aJoiner) const;
|
||||
void SetState(State aState);
|
||||
void SignalJoinerEvent(JoinerEvent aEvent, 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];
|
||||
|
||||
@@ -452,7 +468,7 @@ private:
|
||||
|
||||
char mProvisioningUrl[OT_PROVISIONING_URL_MAX_SIZE + 1]; // + 1 is for null char at end of string.
|
||||
|
||||
otCommissionerState mState;
|
||||
State mState;
|
||||
|
||||
otCommissionerStateCallback mStateCallback;
|
||||
otCommissionerJoinerCallback mJoinerCallback;
|
||||
|
||||
Reference in New Issue
Block a user