Skip to content

Commit

Permalink
[ccm] Add 'joiner startccm' CLI command to trigger all required opera…
Browse files Browse the repository at this point in the history
…tions for CCM and eventually attaching to the network.
  • Loading branch information
EskoDijk committed Nov 3, 2024
1 parent b940a6d commit 3063384
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 10 deletions.
9 changes: 8 additions & 1 deletion include/openthread/joiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ typedef struct otJoinerDiscerner
/**
* Type defines Join operation identifiers for MeshCoP-Ext. It includes both CCM and non-CCM join operations.
* Identifiers 0-15 SHOULD be used only for operations that go through a Joiner Router. Identifiers >= 16 are
* for operations that don't use a Joiner Router.
* for operations that don't use a Joiner Router or operations composed of multiple sub-operations.
*/
typedef enum otJoinOperation
{
Expand Down Expand Up @@ -116,6 +116,13 @@ typedef enum otJoinOperation
*/
OT_JOIN_OPERATION_BR_CBRSKI = 16,

/**
* CCM do-all operation which will perform AE/cBRSKI, NKP, Thread-start as needed to get
* a node attached to a Thread Network. It is a meta-operation that calls multiple other
* operations under the hood.
*/
OT_JOIN_OPERATION_CCM_ALL = 17,

} otJoinOperation;

/**
Expand Down
23 changes: 21 additions & 2 deletions src/cli/cli_joiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ template <> otError Joiner::Process<Cmd("startae")>(Arg aArgs[])
return error;
}

#if OPENTHREAD_CONFIG_CCM_ENABLE
/**
* @cli joiner startccmbr
* @code
Expand All @@ -213,7 +212,6 @@ template <> otError Joiner::Process<Cmd("startccmbr")>(Arg aArgs[])

return error;
}
#endif

/**
* @cli joiner startnkp
Expand All @@ -235,6 +233,26 @@ template <> otError Joiner::Process<Cmd("startnkp")>(Arg aArgs[])
return error;
}

/**
* @cli joiner startccm
* @code
* joiner startccm
* Done
* @endcode
* @cparam joiner startccm
* @par api_copy
* #otJoinerStartCcm
*/
template <> otError Joiner::Process<Cmd("startccm")>(Arg aArgs[])
{
OT_UNUSED_VARIABLE(aArgs);
otError error;

error = otJoinerStartCcm(GetInstancePtr(), otJoinOperation::OT_JOIN_OPERATION_CCM_ALL, &Joiner::HandleCallback, this);

return error;
}

#endif // OPENTHREAD_CONFIG_CCM_ENABLE

/**
Expand Down Expand Up @@ -295,6 +313,7 @@ otError Joiner::Process(Arg aArgs[])
CmdEntry("start"),
#if OPENTHREAD_CONFIG_CCM_ENABLE
CmdEntry("startae"),
CmdEntry("startccm"),
#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
CmdEntry("startccmbr"),
#endif
Expand Down
77 changes: 75 additions & 2 deletions src/core/meshcop/ccm/joiner_ccm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Error Joiner::StartCcm(Operation aOperation, otJoinerCallback aCallback, void *a
Ip6::SockAddr sockAddr;

VerifyOrExit(mState == kStateIdle, error = kErrorBusy);
VerifyOrExit((Get<ThreadNetif>().IsUp() || aOperation == kOperationCcmBrCbrski) &&
VerifyOrExit((Get<ThreadNetif>().IsUp() || aOperation == kOperationCcmBrCbrski || aOperation == kOperationCcmAll) &&
Get<Mle::Mle>().GetRole() == Mle::kRoleDisabled,
error = kErrorInvalidState);

Expand All @@ -81,13 +81,22 @@ Error Joiner::StartCcm(Operation aOperation, otJoinerCallback aCallback, void *a
SuccessOrExit(error = Get<Credentials>().ConfigureLdevid(&Get<Tmf::SecureAgent>().GetDtls()));
mJoinerSourcePort = kCcmNkpJoinerUdpSourcePort;
break;
case kOperationCcmAll:
break;
default:
ExitNow(error = kErrorInvalidArgs);
}

mJoinerOperation = aOperation;
LogInfo("Start operation %s (%d)", OperationToString(mJoinerOperation), mJoinerOperation);

if (aOperation == kOperationCcmAll)
{
mCallbackCcmAll.Set(aCallback, aContext);
error = StartCcmAll();
ExitNow();
}

#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
if (aOperation == kOperationCcmBrCbrski)
{
Expand Down Expand Up @@ -139,6 +148,45 @@ Error Joiner::StartCcm(Operation aOperation, otJoinerCallback aCallback, void *a
return error;
}

Error Joiner::StartCcmAll(void)
{
Error error;
bool done = false;
bool isNeedAe = !Get<Credentials>().HasOperationalCert();
bool isNeedNkp = !Get<ActiveDatasetManager>().IsComplete() && !Get<ActiveDatasetManager>().IsPartiallyComplete();
bool isNeedUp = !Get<ThreadNetif>().IsUp();
bool isNeedThreadStart = Get<Mle::MleRouter>().IsDisabled();

LogDebg("StartCcmAll nAe=%d nNk=%d nUp=%d nTs=%d", isNeedAe, isNeedNkp, isNeedUp, isNeedThreadStart); // FIXME can be removed
if (isNeedUp){
Get<ThreadNetif>().Up();
}

if (isNeedAe){
error = StartCcm(Joiner::Operation::kOperationCcmAeCbrski, HandleCcmAllOperationDone, this);
}
else if (isNeedNkp)
{
error = StartCcm(Joiner::Operation::kOperationCcmNkp, HandleCcmAllOperationDone, this);
}
else if (isNeedThreadStart)
{
error = Get<Mle::MleRouter>().Start();
}
else
{
mCallbackCcmAll.InvokeIfSet(kErrorNone);
done = true;
error = kErrorAbort; // set abort error to signal HandleCcmAllOperationDone() to stop recursion.
}

if (error != kErrorNone && !done)
{
mCallbackCcmAll.InvokeIfSet(error);
}
return error;
}

Error Joiner::PrepareCcmNkpJoinerFinalizeMessage(void)
{
Error error = kErrorNone;
Expand Down Expand Up @@ -168,12 +216,37 @@ Error Joiner::PrepareCcmNkpJoinerFinalizeMessage(void)
return error;
}

void Joiner::HandleCcmAllOperationDone(Error aErr, void *aContext)
{
static_cast<Joiner *>(aContext)->HandleCcmAllOperationDone(aErr);
}

void Joiner::HandleCcmAllOperationDone(Error aErr) {
Error error;

if (aErr == kErrorNone)
{
error = StartCcmAll(); // proceed with next required operation
}
else
{
error = aErr;
}

if (error != kErrorNone)
{
LogDebg("CCM 'all' operation finish (err=%s)", otThreadErrorToString(error));
mCallbackCcmAll.InvokeIfSet(error);
mCallbackCcmAll.Clear();
}
}

void Joiner::HandleCbrskiClientDone(Error aErr, void *aContext)
{
static_cast<Joiner *>(aContext)->HandleCbrskiClientDone(aErr);
}

void Joiner::HandleCbrskiClientDone(Error aErr) { Finish(aErr, true); }
void Joiner::HandleCbrskiClientDone(Error aErr) { Finish(aErr, /* aInvokeCallback */ true); }

} // namespace MeshCoP
} // namespace ot
Expand Down
10 changes: 5 additions & 5 deletions src/core/meshcop/joiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,11 @@ void Joiner::Finish(Error aError, bool aInvokeCallback)
SetState(kStateIdle);
FreeJoinerFinalizeMessage();

exit:
if (aInvokeCallback)
{
mCallback.InvokeIfSet(aError);
}

exit:
return;
}

uint8_t Joiner::CalculatePriority(int8_t aRssi, bool aSteeringDataAllowsAny)
Expand Down Expand Up @@ -348,7 +346,7 @@ void Joiner::TryNextJoinerRouter(Error aPrevError)
aPrevError = kErrorNotFound;
}

Finish(aPrevError, true);
Finish(aPrevError, /* aInvokeCallback */ true);

exit:
return;
Expand Down Expand Up @@ -615,7 +613,7 @@ void Joiner::HandleTimer(void)
OT_ASSERT(false);
}

Finish(error, true);
Finish(error, /* aInvokeCallback */ true);
}

// LCOV_EXCL_START
Expand Down Expand Up @@ -654,6 +652,8 @@ const char *Joiner::OperationToString(Joiner::Operation aOperation)
return "NKP";
case kOperationCcmEstCoaps:
return "EST-CoAPS/JR";
case kOperationCcmAll:
return "CCM(All)";
#endif
case kOperationMeshcop:
return "MeshCoP";
Expand Down
5 changes: 5 additions & 0 deletions src/core/meshcop/joiner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Joiner : public InstanceLocator, private NonCopyable
kOperationCcmEstCoaps = OT_JOIN_OPERATION_EST_COAPS,
kOperationCcmBrCbrski = OT_JOIN_OPERATION_BR_CBRSKI,
kOperationMeshcop = OT_JOIN_OPERATION_MESHCOP,
kOperationCcmAll = OT_JOIN_OPERATION_CCM_ALL,
};

/**
Expand Down Expand Up @@ -254,8 +255,11 @@ class Joiner : public InstanceLocator, private NonCopyable
void HandleJoinerFinalizeResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aResult);

#if OPENTHREAD_CONFIG_CCM_ENABLE
Error StartCcmAll(void);
static void HandleCbrskiClientDone(Error aErr, void *aContext);
void HandleCbrskiClientDone(Error aErr);
static void HandleCcmAllOperationDone(Error aErr, void *aContext);
void HandleCcmAllOperationDone(Error aErr);
#endif

template <Uri kUri> void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
Expand Down Expand Up @@ -290,6 +294,7 @@ class Joiner : public InstanceLocator, private NonCopyable

#if OPENTHREAD_CONFIG_CCM_ENABLE
CbrskiClient *mCbrskiClient;
Callback<otJoinerCallback> mCallbackCcmAll;
#endif

Operation mJoinerOperation;
Expand Down

0 comments on commit 3063384

Please sign in to comment.