Skip to content

Commit

Permalink
Use fluent builder pattern for OCW params, and some fixes
Browse files Browse the repository at this point in the history
Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
samadDotDev and andy31415 committed Jul 8, 2024
1 parent d29e4fc commit 16bf72c
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ CHIP_ERROR OpenCommissioningWindowCommand::RunCommand()
if (mCommissioningWindowOption == Controller::CommissioningWindowOpener::CommissioningWindowOption::kTokenWithRandomPIN)
{
SetupPayload ignored;
Controller::CommissioningWindowOpener::CommissioningWindowPasscodeParams params = {
.common = { .deviceId = mNodeId,
.timeout = System::Clock::Seconds16(mCommissioningWindowTimeout),
.iteration = mIteration,
.discriminator = mDiscriminator },
.setupPIN = NullOptional,
.salt = NullOptional,
.readVIDPIDAttributes = true,
.callback = &mOnOpenCommissioningWindowCallback,
};
return mWindowOpener->OpenCommissioningWindow(params, ignored);
return mWindowOpener->OpenCommissioningWindow(Controller::CommissioningWindowPasscodeParams()
.SetNodeId(mNodeId)
.SetTimeout(mCommissioningWindowTimeout)
.SetIteration(mIteration)
.SetDiscriminator(mDiscriminator)
.SetReadVIDPIDAttributes(true),
&mOnOpenCommissioningWindowCallback, ignored);
}

ChipLogError(chipTool, "Unknown commissioning window option: %d", to_underlying(mCommissioningWindowOption));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,29 @@ CHIP_ERROR OpenCommissioningWindowCommand::RunCommand()

if (mCommissioningWindowOption == Controller::CommissioningWindowOpener::CommissioningWindowOption::kTokenWithRandomPIN)
{
Controller::CommissioningWindowOpener::CommissioningWindowCommonParams common = { .deviceId = mNodeId,
.timeout = System::Clock::Seconds16(
mCommissioningWindowTimeout),
.iteration = mIteration,
.discriminator = mDiscriminator };

if (mVerifier.HasValue())
{
VerifyOrReturnError(mSalt.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);
Controller::CommissioningWindowOpener::CommissioningWindowVerifierParams params = {
.common = common,
.verifier = mVerifier.Value(),
.salt = mSalt.Value(),
.callback = &mOnOpenCommissioningWindowVerifierCallback,
};
return mWindowOpener->OpenCommissioningWindow(params);
return mWindowOpener->OpenCommissioningWindow(Controller::CommissioningWindowVerifierParams()
.SetNodeId(mNodeId)
.SetTimeout(mCommissioningWindowTimeout)
.SetIteration(mIteration)
.SetDiscriminator(mDiscriminator)
.SetVerifier(mVerifier.Value())
.SetSalt(mSalt.Value()),
&mOnOpenCommissioningWindowVerifierCallback);
}
else
{
SetupPayload ignored;
Controller::CommissioningWindowOpener::CommissioningWindowPasscodeParams params = {
.common = common,
.setupPIN = NullOptional,
.salt = mSalt,
.readVIDPIDAttributes = true,
.callback = &mOnOpenCommissioningWindowCallback,
};
return mWindowOpener->OpenCommissioningWindow(params, ignored);
return mWindowOpener->OpenCommissioningWindow(Controller::CommissioningWindowPasscodeParams()
.SetNodeId(mNodeId)
.SetTimeout(mCommissioningWindowTimeout)
.SetIteration(mIteration)
.SetDiscriminator(mDiscriminator)
.SetSalt(mSalt)
.SetReadVIDPIDAttributes(true),
&mOnOpenCommissioningWindowCallback, ignored);
}
}

Expand Down
56 changes: 34 additions & 22 deletions examples/fabric-bridge-app/linux/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,16 @@ CHIP_ERROR InitRpcClient(uint16_t rpcServerPort)
return rpc::client::StartPacketProcessing();
}

CHIP_ERROR OpenCommissioningWindow(CommissioningWindowParams params)
CHIP_ERROR OpenCommissioningWindow(chip_rpc_DeviceCommissioningWindowInfo device)
{
ChipLogProgress(NotSpecified, "OpenCommissioningWindow with Node Id 0x:" ChipLogFormatX64, ChipLogValueX64(params.nodeId));
ChipLogProgress(NotSpecified, "OpenCommissioningWindow with Node Id 0x" ChipLogFormatX64, ChipLogValueX64(device.node_id));

if (openCommissioningWindowCall.active())
{
ChipLogError(NotSpecified, "OpenCommissioningWindow is in progress\n");
return CHIP_ERROR_BUSY;
}

chip_rpc_DeviceCommissioningWindowInfo device;
device.node_id = params.nodeId;
device.commissioning_timeout = params.commissioningTimeout;
device.discriminator = params.discriminator;
device.iterations = params.iterations;

if (params.salt.HasValue())
{
VerifyOrReturnError(params.salt.Value().size() <= sizeof(device.salt.bytes), CHIP_ERROR_BUFFER_TOO_SMALL);
memcpy(device.salt.bytes, params.salt.Value().data(), params.salt.Value().size());
device.salt.size = static_cast<size_t>(params.salt.Value().size());
}

if (params.verifier.HasValue())
{
VerifyOrReturnError(params.verifier.Value().size() <= sizeof(device.verifier.bytes), CHIP_ERROR_BUFFER_TOO_SMALL);
memcpy(device.verifier.bytes, params.verifier.Value().data(), params.verifier.Value().size());
device.verifier.size = static_cast<size_t>(params.verifier.Value().size());
}

// The RPC will remain active as long as `openCommissioningWindowCall` is alive.
openCommissioningWindowCall = fabricAdminClient.OpenCommissioningWindow(device, OnOpenCommissioningWindowCompleted);

Expand All @@ -103,3 +83,35 @@ CHIP_ERROR OpenCommissioningWindow(CommissioningWindowParams params)

return CHIP_NO_ERROR;
}

CHIP_ERROR
OpenCommissioningWindow(chip::Controller::CommissioningWindowPasscodeParams params)
{
chip_rpc_DeviceCommissioningWindowInfo device;
device.node_id = params.GetNodeId();
device.commissioning_timeout = params.GetTimeout().count();
device.discriminator = params.GetDiscriminator();
device.iterations = params.GetIteration();

return OpenCommissioningWindow(device);
}

CHIP_ERROR
OpenCommissioningWindow(chip::Controller::CommissioningWindowVerifierParams params)
{
chip_rpc_DeviceCommissioningWindowInfo device;
device.node_id = params.GetNodeId();
device.commissioning_timeout = params.GetTimeout().count();
device.discriminator = params.GetDiscriminator();
device.iterations = params.GetIteration();

VerifyOrReturnError(params.GetSalt().size() <= sizeof(device.salt.bytes), CHIP_ERROR_BUFFER_TOO_SMALL);
memcpy(device.salt.bytes, params.GetSalt().data(), params.GetSalt().size());
device.salt.size = static_cast<size_t>(params.GetSalt().size());

VerifyOrReturnError(params.GetVerifier().size() <= sizeof(device.verifier.bytes), CHIP_ERROR_BUFFER_TOO_SMALL);
memcpy(device.verifier.bytes, params.GetVerifier().data(), params.GetVerifier().size());
device.verifier.size = static_cast<size_t>(params.GetVerifier().size());

return OpenCommissioningWindow(device);
}
27 changes: 15 additions & 12 deletions examples/fabric-bridge-app/linux/include/RpcClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include <controller/CommissioningWindowParams.h>
#include <platform/CHIPDeviceLayer.h>

constexpr uint16_t kFabricAdminServerPort = 33001;
Expand All @@ -32,24 +33,26 @@ constexpr uint16_t kFabricAdminServerPort = 33001;
*/
CHIP_ERROR InitRpcClient(uint16_t rpcServerPort);

struct CommissioningWindowParams
{
chip::NodeId nodeId;
uint16_t commissioningTimeout;
uint16_t discriminator;
uint32_t iterations;
chip::Optional<chip::ByteSpan> salt = chip::NullOptional;
chip::Optional<chip::ByteSpan> verifier = chip::NullOptional;
};
/**
* Opens a commissioning window for a specified node using setup PIN (passcode).
*
* @param params Params for opening the commissioning window using passcode.
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
* - CHIP_NO_ERROR: The RPC command was successfully sent.
* - CHIP_ERROR_BUSY: Another commissioning window is currently in progress.
* - CHIP_ERROR_INTERNAL: An internal error occurred.
*/
CHIP_ERROR
OpenCommissioningWindow(chip::Controller::CommissioningWindowPasscodeParams params);

/**
* Opens a commissioning window for a specified node.
* Opens a commissioning window for a specified node using pre-computed PAKE passcode verifier.
*
* @param params Params for opening the commissioning window on given node.
* @param params Params for opening the commissioning window using verifier.
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
* - CHIP_NO_ERROR: The RPC command was successfully sent.
* - CHIP_ERROR_BUSY: Another commissioning window is currently in progress.
* - CHIP_ERROR_INTERNAL: An internal error occurred.
*/
CHIP_ERROR
OpenCommissioningWindow(CommissioningWindowParams params);
OpenCommissioningWindow(chip::Controller::CommissioningWindowVerifierParams params);
27 changes: 14 additions & 13 deletions examples/fabric-bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ void BridgePollingThread()
#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
else if (ch == 'o')
{
CommissioningWindowParams params = {
.nodeId = 0x1234, .commissioningTimeout = 300, .discriminator = 3840, .iterations = 1000
};
CHIP_ERROR err = OpenCommissioningWindow(params);
CHIP_ERROR err = OpenCommissioningWindow(chip::Controller::CommissioningWindowPasscodeParams()
.SetNodeId(0x1234)
.SetTimeout(300)
.SetDiscriminator(3840)
.SetIteration(1000));
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to call OpenCommissioningWindow RPC: %" CHIP_ERROR_FORMAT, err.Format());
Expand Down Expand Up @@ -123,7 +124,7 @@ void AdministratorCommissioningCommandHandler::InvokeCommand(HandlerContext & ha
using Protocols::InteractionModel::Status;

EndpointId endpointId = handlerContext.mRequestPath.mEndpointId;
ChipLogProgress(NotSpecified, "Received command to open commissioning window on Endpoind: %d", endpointId);
ChipLogProgress(NotSpecified, "Received command to open commissioning window on Endpoint: %d", endpointId);

if (handlerContext.mRequestPath.mCommandId != Commands::OpenCommissioningWindow::Id || endpointId == kRootEndpointId)
{
Expand All @@ -145,15 +146,15 @@ void AdministratorCommissioningCommandHandler::InvokeCommand(HandlerContext & ha
#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
Device * device = DeviceMgr().GetDevice(endpointId);

CommissioningWindowParams params = { .nodeId = device->GetNodeId(),
.commissioningTimeout = commandData.commissioningTimeout,
.discriminator = commandData.discriminator,
.iterations = commandData.iterations,
.salt = chip::Optional<chip::ByteSpan>(commandData.salt),
.verifier = chip::Optional<chip::ByteSpan>(commandData.PAKEPasscodeVerifier) };

// TODO: issues:#33784, need to make OpenCommissioningWindow synchronous
if (device != nullptr && OpenCommissioningWindow(params) == CHIP_NO_ERROR)
if (device != nullptr &&
OpenCommissioningWindow(chip::Controller::CommissioningWindowVerifierParams()
.SetNodeId(device->GetNodeId())
.SetTimeout(commandData.commissioningTimeout)
.SetDiscriminator(commandData.discriminator)
.SetIteration(commandData.iterations)
.SetSalt(commandData.salt)
.SetVerifier(commandData.PAKEPasscodeVerifier)) == CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Commissioning window is now open");
status = Status::Success;
Expand Down
Loading

0 comments on commit 16bf72c

Please sign in to comment.