Skip to content

Commit

Permalink
[YAML] Add a simple pairing API for YAML (#15075)
Browse files Browse the repository at this point in the history
* [YAML] Add a simple pairing API for YAML

* Update src/app/zap-templates/common/simulated-clusters/clusters/CommissionerCommands.js
  • Loading branch information
vivien-apple authored and pull[bot] committed Oct 20, 2023
1 parent f625d9f commit 9007363
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 16 deletions.
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/tests/TestCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void TestCommand::OnDeviceConnectedFn(void * context, chip::OperationalDevicePro
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "Device connected, but cannot run the test, as the context is null"));
command->mDevices[command->GetIdentity()] = device;

command->ContinueOnChipMainThread();
LogErrorOnFailure(command->ContinueOnChipMainThread(CHIP_NO_ERROR));
}

void TestCommand::OnDeviceConnectionFailureFn(void * context, PeerId peerId, CHIP_ERROR error)
Expand All @@ -53,7 +53,7 @@ void TestCommand::OnDeviceConnectionFailureFn(void * context, PeerId peerId, CHI
auto * command = static_cast<TestCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "Test command context is null"));

command->ContinueOnChipMainThread();
LogErrorOnFailure(command->ContinueOnChipMainThread(CHIP_NO_ERROR));
}

void TestCommand::Exit(std::string message)
Expand Down
10 changes: 9 additions & 1 deletion examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ class TestCommand : public CHIPCommand,
static void OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device);
static void OnDeviceConnectionFailureFn(void * context, PeerId peerId, CHIP_ERROR error);

CHIP_ERROR ContinueOnChipMainThread() override { return WaitForMs(0); };
CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) override
{
if (CHIP_NO_ERROR == err)
{
return WaitForMs(0);
}
Exit(chip::ErrorStr(err));
return CHIP_NO_ERROR;
}

void Exit(std::string message) override;
void ThrowFailureResponse();
Expand Down
11 changes: 9 additions & 2 deletions examples/placeholder/linux/include/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ class TestCommand : public PICSChecker, public LogCommands, public DiscoveryComm
return 0;
}

CHIP_ERROR ContinueOnChipMainThread() override
CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) override
{
NextTest();
if (CHIP_NO_ERROR == err)
{
NextTest();
}
else
{
Exit(chip::ErrorStr(err));
}
return CHIP_NO_ERROR;
}

Expand Down
32 changes: 32 additions & 0 deletions src/app/tests/suites/commands/commissioner/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

static_library("commissioner") {
output_name = "libCommissionerCommands"

sources = [
"CommissionerCommands.cpp",
"CommissionerCommands.h",
]

cflags = [ "-Wconversion" ]

public_deps = [
"${chip_root}/src/controller",
"${chip_root}/src/lib/support",
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "CommissionerCommands.h"

constexpr uint16_t kPayloadMaxSize = 64;

CHIP_ERROR CommissionerCommands::PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload)
{
VerifyOrReturnError(payload.size() > 0 && payload.size() < kPayloadMaxSize, CHIP_ERROR_INVALID_ARGUMENT);

GetCurrentCommissioner().RegisterPairingDelegate(this);

char qrCode[kPayloadMaxSize];
memcpy(qrCode, payload.data(), payload.size());
return GetCurrentCommissioner().PairDevice(nodeId, qrCode);
}

CHIP_ERROR CommissionerCommands::PairWithManualCode(chip::NodeId nodeId, const chip::CharSpan payload)
{
VerifyOrReturnError(payload.size() > 0 && payload.size() < kPayloadMaxSize, CHIP_ERROR_INVALID_ARGUMENT);

GetCurrentCommissioner().RegisterPairingDelegate(this);

char manualCode[kPayloadMaxSize];
memcpy(manualCode, payload.data(), payload.size());
return GetCurrentCommissioner().PairDevice(nodeId, manualCode);
}

CHIP_ERROR CommissionerCommands::Unpair(chip::NodeId nodeId)
{
return GetCurrentCommissioner().UnpairDevice(nodeId);
}

void CommissionerCommands::OnStatusUpdate(DevicePairingDelegate::Status status)
{
switch (status)
{
case DevicePairingDelegate::Status::SecurePairingSuccess:
ChipLogProgress(chipTool, "Secure Pairing Success");
break;
case DevicePairingDelegate::Status::SecurePairingFailed:
ChipLogError(chipTool, "Secure Pairing Failed");
break;
}
}

void CommissionerCommands::OnPairingComplete(CHIP_ERROR err)
{
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Pairing Complete Failure: %s", ErrorStr(err));
LogErrorOnFailure(ContinueOnChipMainThread(err));
}
}

void CommissionerCommands::OnPairingDeleted(CHIP_ERROR err)
{
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Pairing Delete Failure: %s", ErrorStr(err));
}

LogErrorOnFailure(ContinueOnChipMainThread(err));
}

void CommissionerCommands::OnCommissioningComplete(chip::NodeId nodeId, CHIP_ERROR err)
{
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Commissioning Complete Failure: %s", ErrorStr(err));
}

LogErrorOnFailure(ContinueOnChipMainThread(err));
}
42 changes: 42 additions & 0 deletions src/app/tests/suites/commands/commissioner/CommissionerCommands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include <controller/CHIPDeviceController.h>
#include <lib/support/CodeUtils.h>

class CommissionerCommands : public chip::Controller::DevicePairingDelegate
{
public:
CommissionerCommands(){};
virtual ~CommissionerCommands(){};

virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
virtual chip::Controller::DeviceCommissioner & GetCurrentCommissioner() = 0;

CHIP_ERROR PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload);
CHIP_ERROR PairWithManualCode(chip::NodeId nodeId, const chip::CharSpan payload);
CHIP_ERROR Unpair(chip::NodeId nodeId);

/////////// DevicePairingDelegate Interface /////////
void OnStatusUpdate(chip::Controller::DevicePairingDelegate::Status status) override;
void OnPairingComplete(CHIP_ERROR error) override;
void OnPairingDeleted(CHIP_ERROR error) override;
void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR error) override;
};
2 changes: 1 addition & 1 deletion src/app/tests/suites/commands/delay/DelayCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ CHIP_ERROR DelayCommands::WaitForOperationalAdvertisement()
CHIP_ERROR DelayCommands::RunInternal(const char * command)
{
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
return ContinueOnChipMainThread();
return ContinueOnChipMainThread(CHIP_NO_ERROR);
}
4 changes: 2 additions & 2 deletions src/app/tests/suites/commands/delay/DelayCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class DelayCommands
DelayCommands(){};
virtual ~DelayCommands(){};

virtual CHIP_ERROR ContinueOnChipMainThread() = 0;
virtual void OnWaitForMs() = 0;
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
virtual void OnWaitForMs() = 0;

virtual CHIP_ERROR WaitForCommissionee(chip::NodeId nodeId) { return CHIP_ERROR_NOT_IMPLEMENTED; };
virtual CHIP_ERROR WaitForCommissioning() { return CHIP_ERROR_NOT_IMPLEMENTED; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DiscoveryCommands : public chip::Dnssd::ResolverDelegate
DiscoveryCommands(){};
virtual ~DiscoveryCommands(){};

virtual CHIP_ERROR ContinueOnChipMainThread() = 0;
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;

CHIP_ERROR FindCommissionable();
CHIP_ERROR FindCommissionableByShortDiscriminator(uint64_t value);
Expand Down
6 changes: 2 additions & 4 deletions src/app/tests/suites/commands/log/LogCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
CHIP_ERROR LogCommands::Log(const char * message)
{
ChipLogDetail(chipTool, "%s", message);
ReturnErrorOnFailure(ContinueOnChipMainThread());
return CHIP_NO_ERROR;
return ContinueOnChipMainThread(CHIP_NO_ERROR);
}

CHIP_ERROR LogCommands::UserPrompt(const char * message)
{
ChipLogDetail(chipTool, "USER_PROMPT: %s", message);
ReturnErrorOnFailure(ContinueOnChipMainThread());
return CHIP_NO_ERROR;
return ContinueOnChipMainThread(CHIP_NO_ERROR);
}
2 changes: 1 addition & 1 deletion src/app/tests/suites/commands/log/LogCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LogCommands
LogCommands(){};
virtual ~LogCommands(){};

virtual CHIP_ERROR ContinueOnChipMainThread() = 0;
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;

CHIP_ERROR Log(const char * message);
CHIP_ERROR UserPrompt(const char * message);
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/suites/commands/system/SystemCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ CHIP_ERROR SystemCommands::FactoryReset()
CHIP_ERROR SystemCommands::RunInternal(const char * command)
{
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
return ContinueOnChipMainThread();
return ContinueOnChipMainThread(CHIP_NO_ERROR);
}
2 changes: 1 addition & 1 deletion src/app/tests/suites/commands/system/SystemCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SystemCommands
SystemCommands(){};
virtual ~SystemCommands(){};

virtual CHIP_ERROR ContinueOnChipMainThread() = 0;
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;

CHIP_ERROR Start(uint16_t discriminator);
CHIP_ERROR Stop();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* This file declare test suites utilities methods for commissioner commands.
*
* Each method declared in this file needs to be implemented on a per-language
* basis and permits to exposes methods to the test suites that are not part
* of the regular cluster set of APIs.
*
*/

const PairWithQRCode = {
name : 'PairWithQRCode',
arguments : [ { type : 'NODE_ID', name : 'nodeId' }, { type : 'CHAR_STRING', name : 'payload' } ],
response : { arguments : [] }
};

const PairWithManualCode = {
name : 'PairWithManualCode',
arguments : [ { type : 'NODE_ID', name : 'nodeId' }, { type : 'CHAR_STRING', name : 'payload' } ],
response : { arguments : [] }
};

const Unpair = {
name : 'Unpair',
arguments : [ { type : 'NODE_ID', name : 'nodeId' } ],
response : { arguments : [] }
};

const name = 'CommissionerCommands';
const commands = [ PairWithQRCode, PairWithManualCode, Unpair ];

const CommissionerCommands = {
name,
commands
};

//
// Module exports
//
exports.cluster = CommissionerCommands;

0 comments on commit 9007363

Please sign in to comment.