Skip to content

Commit

Permalink
per cecille, use CommissioningParameters to consume config from chip-…
Browse files Browse the repository at this point in the history
…tool
  • Loading branch information
msandstedt committed Feb 4, 2022
1 parent a10482d commit 76bcc3c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 45 deletions.
8 changes: 4 additions & 4 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ CHIP_ERROR PairingCommand::PairWithQRCode(NodeId remoteId)
auto onFailure = [this, stateMachine](Commissionee & commissionee) { OnCommissioningFailure(*stateMachine.get()); };
stateMachine.get()->Init(chip::Controller::DeviceControllerFactory::GetInstance().GetSystemState(),
mCredIssuerCmds->GetCredentialIssuer(), CurrentController().GetFabricIndex(), mNodeId,
mOperationalDataset, mSSID, mPassword);
GetCommissioningParameters());
CHIP_ERROR err = stateMachine.get()->Commission(mOnboardingPayload, onSuccess, onFailure);
if (err != CHIP_NO_ERROR)
{
Expand All @@ -109,7 +109,7 @@ CHIP_ERROR PairingCommand::PairWithManualCode(NodeId remoteId)
auto onFailure = [this, stateMachine](Commissionee & commissionee) { OnCommissioningFailure(*stateMachine.get()); };
stateMachine.get()->Init(chip::Controller::DeviceControllerFactory::GetInstance().GetSystemState(),
mCredIssuerCmds->GetCredentialIssuer(), CurrentController().GetFabricIndex(), mNodeId,
mOperationalDataset, mSSID, mPassword);
GetCommissioningParameters());
CHIP_ERROR err = stateMachine.get()->Commission(mOnboardingPayload, onSuccess, onFailure);
if (err != CHIP_NO_ERROR)
{
Expand All @@ -126,7 +126,7 @@ CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address)
auto onFailure = [this, stateMachine](Commissionee & commissionee) { OnCommissioningFailure(*stateMachine.get()); };
stateMachine.get()->Init(chip::Controller::DeviceControllerFactory::GetInstance().GetSystemState(),
mCredIssuerCmds->GetCredentialIssuer(), CurrentController().GetFabricIndex(), mNodeId,
mOperationalDataset, mSSID, mPassword);
GetCommissioningParameters());
CHIP_ERROR err =
stateMachine.get()->Commission(chip::RendezvousInformationFlag::kNone, mDiscriminator, mSetupPINCode, onSuccess, onFailure);
if (err != CHIP_NO_ERROR)
Expand All @@ -151,7 +151,7 @@ CHIP_ERROR PairingCommand::PairWithMdns(NodeId remoteId)
auto onFailure = [this, stateMachine](Commissionee & commissionee) { OnCommissioningFailure(*stateMachine.get()); };
stateMachine.get()->Init(chip::Controller::DeviceControllerFactory::GetInstance().GetSystemState(),
mCredIssuerCmds->GetCredentialIssuer(), CurrentController().GetFabricIndex(), mNodeId,
mOperationalDataset, mSSID, mPassword);
GetCommissioningParameters());
CHIP_ERROR err =
stateMachine.get()->Commission(chip::RendezvousInformationFlag::kOnNetwork, static_cast<uint16_t>(mDiscoveryFilterCode),
mSetupPINCode, onSuccess, onFailure);
Expand Down
20 changes: 9 additions & 11 deletions src/commissioner/ExampleCommissioningStateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ namespace chip {
namespace Commissioner {
namespace ExampleCommissioningStateMachine {

void StateFactory::Init(OpCredsIssuer * issuer, FabricIndex fabricIndex, NodeId nodeId, ByteSpan operationalDataset, ByteSpan ssid,
ByteSpan wiFiCredentials)
void StateFactory::Init(OpCredsIssuer * issuer, FabricIndex fabricIndex, NodeId nodeId,
CommissioningParameters & commissioningParameters)
{
mIssuer = issuer;
mFabricIndex = fabricIndex;
mNodeId = nodeId;
mOperationalDataset = operationalDataset;
mSsid = ssid;
mWiFiCredentials = wiFiCredentials;
mIssuer = issuer;
mFabricIndex = fabricIndex;
mNodeId = nodeId;
mCommissioningParameters = commissioningParameters;
}

void StateFactory::SetCallbacks(OnSuccess onSuccess, OnFailure onFailure)
Expand Down Expand Up @@ -218,16 +216,16 @@ chip::StateMachine::Optional<State> Transitions::operator()(const State & state,
}

void ExampleCommissioningStateMachine::Init(SystemState * systemState, OpCredsIssuer * issuer, FabricIndex fabricIndex,
NodeId nodeId, ByteSpan operationalDataset, ByteSpan ssid, ByteSpan wiFiCredentials)
NodeId nodeId, CommissioningParameters commissioningParameeters)
{
mTransitions.mFactory.Init(issuer, fabricIndex, nodeId, operationalDataset, ssid, wiFiCredentials);
mTransitions.mFactory.Init(issuer, fabricIndex, nodeId, commissioningParameeters);
mCommissionee.Init(systemState);
}

void ExampleCommissioningStateMachine::Init(SystemState * systemState, OpCredsIssuer * issuer, FabricIndex fabricIndex,
NodeId nodeId)
{
Init(systemState, issuer, fabricIndex, nodeId, ByteSpan{}, ByteSpan{}, ByteSpan{});
Init(systemState, issuer, fabricIndex, nodeId, CommissioningParameters());
}

void ExampleCommissioningStateMachine::Shutdown()
Expand Down
93 changes: 63 additions & 30 deletions src/commissioner/ExampleCommissioningStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,42 @@ struct FinishingPase : SdkStates::InitiatingPase

struct PaseComplete : SdkStates::Base
{
PaseComplete(Context & ctx, Commissionee & commissionee) : Base(ctx, commissionee, "PaseComplete") {}
void Enter() { this->mCtx.Dispatch(Event::Create<SdkEvents::ArmFailSafe>(SdkEvents::ArmFailSafe{ 60 })); }
PaseComplete(Context & ctx, Commissionee & commissionee, CommissioningParameters & commissioningParameters) :
Base(ctx, commissionee, "PaseComplete"), mCommissioningParameters(commissioningParameters)
{}
void Enter()
{
this->mCtx.Dispatch(
Event::Create<SdkEvents::ArmFailSafe>(SdkEvents::ArmFailSafe{ mCommissioningParameters.GetFailsafeTimerSeconds() }));
}

private:
CommissioningParameters & mCommissioningParameters;
};

struct FailSafeArmed : SdkStates::Base
{
FailSafeArmed(Context & ctx, Commissionee & commissionee) : Base(ctx, commissionee, "FailSafeArmed") {}
FailSafeArmed(Context & ctx, Commissionee & commissionee, CommissioningParameters & commissioningParameters) :
Base(ctx, commissionee, "FailSafeArmed"), mCommissioningParameters(commissioningParameters)
{}
void Enter()
{
uint8_t attestationNonce[chip::kAttestationNonceLength];
chip::Crypto::DRBG_get_bytes(attestationNonce, sizeof(attestationNonce));
this->mCtx.Dispatch(Event::Create<SdkEvents::AttestationInformation>(chip::ByteSpan(attestationNonce)));
uint8_t nonceBuf[chip::kAttestationNonceLength];
ByteSpan nonce;
if (mCommissioningParameters.GetAttestationNonce().HasValue())
{
nonce = mCommissioningParameters.GetAttestationNonce().Value();
}
else
{
chip::Crypto::DRBG_get_bytes(nonceBuf, sizeof(nonceBuf));
nonce = chip::ByteSpan(nonceBuf);
}
this->mCtx.Dispatch(Event::Create<SdkEvents::AttestationInformation>(nonce));
}

private:
CommissioningParameters & mCommissioningParameters;
};

struct AttestationVerification : SdkStates::Base
Expand Down Expand Up @@ -208,18 +231,30 @@ struct AttestationVerification : SdkStates::Base

struct AttestationVerified : SdkStates::Base
{
AttestationVerified(Context & ctx, Commissionee & commissionee, SdkEvents::AttestationInformation & attestationInformation) :
Base(ctx, commissionee, "AttestationVerified"), mAttestationInformation(attestationInformation)
AttestationVerified(Context & ctx, Commissionee & commissionee, SdkEvents::AttestationInformation & attestationInformation,
CommissioningParameters & commissioningParameters) :
Base(ctx, commissionee, "AttestationVerified"),
mAttestationInformation(attestationInformation), mCommissioningParameters(commissioningParameters)
{}
void Enter()
{
uint8_t csrNonce[kOpCSRNonceLength];
chip::Crypto::DRBG_get_bytes(csrNonce, sizeof(csrNonce));
this->mCtx.Dispatch(Event::Create<SdkEvents::NocsrInformation>(mAttestationInformation, chip::ByteSpan(csrNonce)));
uint8_t nonceBuf[kOpCSRNonceLength];
ByteSpan nonce;
if (mCommissioningParameters.GetCSRNonce().HasValue())
{
nonce = mCommissioningParameters.GetCSRNonce().Value();
}
else
{
chip::Crypto::DRBG_get_bytes(nonceBuf, sizeof(nonceBuf));
nonce = chip::ByteSpan(nonceBuf);
}
this->mCtx.Dispatch(Event::Create<SdkEvents::NocsrInformation>(mAttestationInformation, nonce));
}

private:
SdkEvents::AttestationInformation mAttestationInformation;
CommissioningParameters & mCommissioningParameters;
};

struct OpCSRResponseReceived : SdkStates::Base
Expand Down Expand Up @@ -363,9 +398,9 @@ struct OpCredsWritten : SdkStates::Base
struct NetworkFeatureMapRead : SdkStates::Base
{
NetworkFeatureMapRead(Context & ctx, Commissionee & commissionee, SdkEvents::NetworkFeatureMap featureMap,
ByteSpan operationalDataset, ByteSpan ssid, ByteSpan wiFiCredentials) :
CommissioningParameters & commissioningParameters) :
Base(ctx, commissionee, "NetworkFeatureMapRead"),
mFeatureMap(featureMap), mOperationalDataset(operationalDataset), mSsid(ssid), mWiFiCredentials(wiFiCredentials)
mFeatureMap(featureMap), mCommissioningParameters(commissioningParameters)
{}
void Enter()
{
Expand All @@ -376,15 +411,18 @@ struct NetworkFeatureMapRead : SdkStates::Base
// until then, we will infer commissoinee network type from the
// credentials given to us and only log the feature map.
ChipLogDetail(Controller, "Network Feature Map = 0x%08" PRIX32, mFeatureMap);
if (mSsid.size())
if (mCommissioningParameters.GetWiFiCredentials().HasValue())
{
auto ssid = mCommissioningParameters.GetWiFiCredentials().Value().ssid;
auto credentials = mCommissioningParameters.GetWiFiCredentials().Value().credentials;
this->mCtx.Dispatch(
Event::Create<SdkEvents::AddOrUpdateWiFiNetwork>(SdkEvents::AddOrUpdateWiFiNetwork{ mSsid, mWiFiCredentials }));
Event::Create<SdkEvents::AddOrUpdateWiFiNetwork>(SdkEvents::AddOrUpdateWiFiNetwork{ ssid, credentials }));
}
else if (mOperationalDataset.size())
else if (mCommissioningParameters.GetThreadOperationalDataset().HasValue())
{
auto operationalDataset = mCommissioningParameters.GetThreadOperationalDataset().Value();
this->mCtx.Dispatch(
Event::Create<SdkEvents::AddOrUpdateThreadNetwork>(SdkEvents::AddOrUpdateThreadNetwork{ mOperationalDataset }));
Event::Create<SdkEvents::AddOrUpdateThreadNetwork>(SdkEvents::AddOrUpdateThreadNetwork{ operationalDataset }));
}
else
{
Expand All @@ -397,9 +435,7 @@ struct NetworkFeatureMapRead : SdkStates::Base

private:
SdkEvents::NetworkFeatureMap mFeatureMap;
ByteSpan mOperationalDataset;
ByteSpan mSsid;
ByteSpan mWiFiCredentials;
CommissioningParameters & mCommissioningParameters;
};

struct NetworkAdded : SdkStates::Base
Expand Down Expand Up @@ -483,8 +519,7 @@ class StateFactory
{
public:
StateFactory(Context & ctx, Commissionee & commissionee) : mCtx(ctx), mCommissionee(commissionee) {}
void Init(OpCredsIssuer * issuer, FabricIndex fabricIndex, NodeId nodeId, ByteSpan operationalDataset, ByteSpan ssid,
ByteSpan wiFiCredentials);
void Init(OpCredsIssuer * issuer, FabricIndex fabricIndex, NodeId nodeId, CommissioningParameters & commissioningParameters);
void SetCallbacks(OnSuccess onSuccess, OnFailure onFailure);

// clang-format off
Expand Down Expand Up @@ -578,19 +613,19 @@ class StateFactory
}
auto CreatePaseComplete()
{
return State::Create<AppStates::PaseComplete>(mCtx, mCommissionee);
return State::Create<AppStates::PaseComplete>(mCtx, mCommissionee, mCommissioningParameters);
}
auto CreateFailSafeArmed()
{
return State::Create<AppStates::FailSafeArmed>(mCtx, mCommissionee);
return State::Create<AppStates::FailSafeArmed>(mCtx, mCommissionee, mCommissioningParameters);
}
auto CreateAttestationVerification(SdkEvents::AttestationInformation attestationInformation)
{
return State::Create<AppStates::AttestationVerification>(mCtx, mCommissionee, attestationInformation);
}
auto CreateAttestationVerified(SdkEvents::AttestationInformation attestationInformation)
{
return State::Create<AppStates::AttestationVerified>(mCtx, mCommissionee, attestationInformation);
return State::Create<AppStates::AttestationVerified>(mCtx, mCommissionee, attestationInformation, mCommissioningParameters);
}
auto CreateOpCSRResponseReceived(SdkEvents::NocsrInformation nocsrInformation)
{
Expand All @@ -610,7 +645,7 @@ class StateFactory
}
auto CreateNetworkFeatureMapRead(SdkEvents::NetworkFeatureMap featureMap)
{
return State::Create<AppStates::NetworkFeatureMapRead>(mCtx, mCommissionee, featureMap, mOperationalDataset, mSsid, mWiFiCredentials);
return State::Create<AppStates::NetworkFeatureMapRead>(mCtx, mCommissionee, featureMap, mCommissioningParameters);
}
auto CreateNetworkAdded(SdkEvents::NetworkId networkId)
{
Expand Down Expand Up @@ -640,9 +675,7 @@ class StateFactory
OpCredsIssuer * mIssuer;
FabricIndex mFabricIndex = kUndefinedFabricIndex;
NodeId mNodeId = kUndefinedNodeId;
ByteSpan mOperationalDataset;
ByteSpan mSsid;
ByteSpan mWiFiCredentials;
CommissioningParameters mCommissioningParameters;
OnSuccess mOnSuccess;
OnFailure mOnFailure;
};
Expand All @@ -665,7 +698,7 @@ class ExampleCommissioningStateMachine : public chip::StateMachine::StateMachine
{}
void Init(SystemState * systemState, OpCredsIssuer * issuer, FabricIndex fabricIndex, NodeId nodeId);
void Init(SystemState * systemState, OpCredsIssuer * issuer, FabricIndex fabricIndex, NodeId nodeId,
ByteSpan operationalDataset, ByteSpan ssid, ByteSpan wiFiCredentials);
CommissioningParameters commissioningParameters);
void Shutdown();
CHIP_ERROR Commission(const char * onboardingPayload, OnSuccess onSuccess, OnFailure onFailure);
CHIP_ERROR Commission(chip::SetupPayload & onboardingPayload, OnSuccess onSuccess, OnFailure onFailure);
Expand Down

0 comments on commit 76bcc3c

Please sign in to comment.