Skip to content

Commit 054878a

Browse files
vivien-applepull[bot]
authored andcommitted
[YAML] Add a simple pairing API for YAML (#15075)
* [YAML] Add a simple pairing API for YAML * Update src/app/zap-templates/common/simulated-clusters/clusters/CommissionerCommands.js
1 parent 70a1cb7 commit 054878a

File tree

14 files changed

+249
-16
lines changed

14 files changed

+249
-16
lines changed

examples/chip-tool/commands/tests/TestCommand.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void TestCommand::OnDeviceConnectedFn(void * context, chip::OperationalDevicePro
4343
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "Device connected, but cannot run the test, as the context is null"));
4444
command->mDevices[command->GetIdentity()] = device;
4545

46-
command->ContinueOnChipMainThread();
46+
LogErrorOnFailure(command->ContinueOnChipMainThread(CHIP_NO_ERROR));
4747
}
4848

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

56-
command->ContinueOnChipMainThread();
56+
LogErrorOnFailure(command->ContinueOnChipMainThread(CHIP_NO_ERROR));
5757
}
5858

5959
void TestCommand::Exit(std::string message)

examples/chip-tool/commands/tests/TestCommand.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ class TestCommand : public CHIPCommand,
6767
static void OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device);
6868
static void OnDeviceConnectionFailureFn(void * context, PeerId peerId, CHIP_ERROR error);
6969

70-
CHIP_ERROR ContinueOnChipMainThread() override { return WaitForMs(0); };
70+
CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) override
71+
{
72+
if (CHIP_NO_ERROR == err)
73+
{
74+
return WaitForMs(0);
75+
}
76+
Exit(chip::ErrorStr(err));
77+
return CHIP_NO_ERROR;
78+
}
7179

7280
void Exit(std::string message) override;
7381
void ThrowFailureResponse();

examples/placeholder/linux/include/TestCommand.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ class TestCommand : public PICSChecker, public LogCommands, public DiscoveryComm
6262
return 0;
6363
}
6464

65-
CHIP_ERROR ContinueOnChipMainThread() override
65+
CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) override
6666
{
67-
NextTest();
67+
if (CHIP_NO_ERROR == err)
68+
{
69+
NextTest();
70+
}
71+
else
72+
{
73+
Exit(chip::ErrorStr(err));
74+
}
6875
return CHIP_NO_ERROR;
6976
}
7077

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2022 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import("//build_overrides/build.gni")
16+
import("//build_overrides/chip.gni")
17+
18+
static_library("commissioner") {
19+
output_name = "libCommissionerCommands"
20+
21+
sources = [
22+
"CommissionerCommands.cpp",
23+
"CommissionerCommands.h",
24+
]
25+
26+
cflags = [ "-Wconversion" ]
27+
28+
public_deps = [
29+
"${chip_root}/src/controller",
30+
"${chip_root}/src/lib/support",
31+
]
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2022 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
#include "CommissionerCommands.h"
20+
21+
constexpr uint16_t kPayloadMaxSize = 64;
22+
23+
CHIP_ERROR CommissionerCommands::PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload)
24+
{
25+
VerifyOrReturnError(payload.size() > 0 && payload.size() < kPayloadMaxSize, CHIP_ERROR_INVALID_ARGUMENT);
26+
27+
GetCurrentCommissioner().RegisterPairingDelegate(this);
28+
29+
char qrCode[kPayloadMaxSize];
30+
memcpy(qrCode, payload.data(), payload.size());
31+
return GetCurrentCommissioner().PairDevice(nodeId, qrCode);
32+
}
33+
34+
CHIP_ERROR CommissionerCommands::PairWithManualCode(chip::NodeId nodeId, const chip::CharSpan payload)
35+
{
36+
VerifyOrReturnError(payload.size() > 0 && payload.size() < kPayloadMaxSize, CHIP_ERROR_INVALID_ARGUMENT);
37+
38+
GetCurrentCommissioner().RegisterPairingDelegate(this);
39+
40+
char manualCode[kPayloadMaxSize];
41+
memcpy(manualCode, payload.data(), payload.size());
42+
return GetCurrentCommissioner().PairDevice(nodeId, manualCode);
43+
}
44+
45+
CHIP_ERROR CommissionerCommands::Unpair(chip::NodeId nodeId)
46+
{
47+
return GetCurrentCommissioner().UnpairDevice(nodeId);
48+
}
49+
50+
void CommissionerCommands::OnStatusUpdate(DevicePairingDelegate::Status status)
51+
{
52+
switch (status)
53+
{
54+
case DevicePairingDelegate::Status::SecurePairingSuccess:
55+
ChipLogProgress(chipTool, "Secure Pairing Success");
56+
break;
57+
case DevicePairingDelegate::Status::SecurePairingFailed:
58+
ChipLogError(chipTool, "Secure Pairing Failed");
59+
break;
60+
}
61+
}
62+
63+
void CommissionerCommands::OnPairingComplete(CHIP_ERROR err)
64+
{
65+
if (CHIP_NO_ERROR != err)
66+
{
67+
ChipLogError(chipTool, "Pairing Complete Failure: %s", ErrorStr(err));
68+
LogErrorOnFailure(ContinueOnChipMainThread(err));
69+
}
70+
}
71+
72+
void CommissionerCommands::OnPairingDeleted(CHIP_ERROR err)
73+
{
74+
if (CHIP_NO_ERROR != err)
75+
{
76+
ChipLogError(chipTool, "Pairing Delete Failure: %s", ErrorStr(err));
77+
}
78+
79+
LogErrorOnFailure(ContinueOnChipMainThread(err));
80+
}
81+
82+
void CommissionerCommands::OnCommissioningComplete(chip::NodeId nodeId, CHIP_ERROR err)
83+
{
84+
if (CHIP_NO_ERROR != err)
85+
{
86+
ChipLogError(chipTool, "Commissioning Complete Failure: %s", ErrorStr(err));
87+
}
88+
89+
LogErrorOnFailure(ContinueOnChipMainThread(err));
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2022 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
#pragma once
20+
21+
#include <controller/CHIPDeviceController.h>
22+
#include <lib/support/CodeUtils.h>
23+
24+
class CommissionerCommands : public chip::Controller::DevicePairingDelegate
25+
{
26+
public:
27+
CommissionerCommands(){};
28+
virtual ~CommissionerCommands(){};
29+
30+
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
31+
virtual chip::Controller::DeviceCommissioner & GetCurrentCommissioner() = 0;
32+
33+
CHIP_ERROR PairWithQRCode(chip::NodeId nodeId, const chip::CharSpan payload);
34+
CHIP_ERROR PairWithManualCode(chip::NodeId nodeId, const chip::CharSpan payload);
35+
CHIP_ERROR Unpair(chip::NodeId nodeId);
36+
37+
/////////// DevicePairingDelegate Interface /////////
38+
void OnStatusUpdate(chip::Controller::DevicePairingDelegate::Status status) override;
39+
void OnPairingComplete(CHIP_ERROR error) override;
40+
void OnPairingDeleted(CHIP_ERROR error) override;
41+
void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR error) override;
42+
};

src/app/tests/suites/commands/delay/DelayCommands.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ CHIP_ERROR DelayCommands::WaitForOperationalAdvertisement()
6161
CHIP_ERROR DelayCommands::RunInternal(const char * command)
6262
{
6363
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
64-
return ContinueOnChipMainThread();
64+
return ContinueOnChipMainThread(CHIP_NO_ERROR);
6565
}

src/app/tests/suites/commands/delay/DelayCommands.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class DelayCommands
2828
DelayCommands(){};
2929
virtual ~DelayCommands(){};
3030

31-
virtual CHIP_ERROR ContinueOnChipMainThread() = 0;
32-
virtual void OnWaitForMs() = 0;
31+
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
32+
virtual void OnWaitForMs() = 0;
3333

3434
virtual CHIP_ERROR WaitForCommissionee(chip::NodeId nodeId) { return CHIP_ERROR_NOT_IMPLEMENTED; };
3535
virtual CHIP_ERROR WaitForCommissioning() { return CHIP_ERROR_NOT_IMPLEMENTED; };

src/app/tests/suites/commands/discovery/DiscoveryCommands.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DiscoveryCommands : public chip::Dnssd::ResolverDelegate
4949
DiscoveryCommands(){};
5050
virtual ~DiscoveryCommands(){};
5151

52-
virtual CHIP_ERROR ContinueOnChipMainThread() = 0;
52+
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
5353

5454
CHIP_ERROR FindCommissionable();
5555
CHIP_ERROR FindCommissionableByShortDiscriminator(uint64_t value);

src/app/tests/suites/commands/log/LogCommands.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
CHIP_ERROR LogCommands::Log(const char * message)
2222
{
2323
ChipLogDetail(chipTool, "%s", message);
24-
ReturnErrorOnFailure(ContinueOnChipMainThread());
25-
return CHIP_NO_ERROR;
24+
return ContinueOnChipMainThread(CHIP_NO_ERROR);
2625
}
2726

2827
CHIP_ERROR LogCommands::UserPrompt(const char * message)
2928
{
3029
ChipLogDetail(chipTool, "USER_PROMPT: %s", message);
31-
ReturnErrorOnFailure(ContinueOnChipMainThread());
32-
return CHIP_NO_ERROR;
30+
return ContinueOnChipMainThread(CHIP_NO_ERROR);
3331
}

src/app/tests/suites/commands/log/LogCommands.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LogCommands
2626
LogCommands(){};
2727
virtual ~LogCommands(){};
2828

29-
virtual CHIP_ERROR ContinueOnChipMainThread() = 0;
29+
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
3030

3131
CHIP_ERROR Log(const char * message);
3232
CHIP_ERROR UserPrompt(const char * message);

src/app/tests/suites/commands/system/SystemCommands.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ CHIP_ERROR SystemCommands::FactoryReset()
7171
CHIP_ERROR SystemCommands::RunInternal(const char * command)
7272
{
7373
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
74-
return ContinueOnChipMainThread();
74+
return ContinueOnChipMainThread(CHIP_NO_ERROR);
7575
}

src/app/tests/suites/commands/system/SystemCommands.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SystemCommands
2626
SystemCommands(){};
2727
virtual ~SystemCommands(){};
2828

29-
virtual CHIP_ERROR ContinueOnChipMainThread() = 0;
29+
virtual CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) = 0;
3030

3131
CHIP_ERROR Start(uint16_t discriminator);
3232
CHIP_ERROR Stop();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* This file declare test suites utilities methods for commissioner commands.
20+
*
21+
* Each method declared in this file needs to be implemented on a per-language
22+
* basis and permits to exposes methods to the test suites that are not part
23+
* of the regular cluster set of APIs.
24+
*
25+
*/
26+
27+
const PairWithQRCode = {
28+
name : 'PairWithQRCode',
29+
arguments : [ { type : 'NODE_ID', name : 'nodeId' }, { type : 'CHAR_STRING', name : 'payload' } ],
30+
response : { arguments : [] }
31+
};
32+
33+
const PairWithManualCode = {
34+
name : 'PairWithManualCode',
35+
arguments : [ { type : 'NODE_ID', name : 'nodeId' }, { type : 'CHAR_STRING', name : 'payload' } ],
36+
response : { arguments : [] }
37+
};
38+
39+
const Unpair = {
40+
name : 'Unpair',
41+
arguments : [ { type : 'NODE_ID', name : 'nodeId' } ],
42+
response : { arguments : [] }
43+
};
44+
45+
const name = 'CommissionerCommands';
46+
const commands = [ PairWithQRCode, PairWithManualCode, Unpair ];
47+
48+
const CommissionerCommands = {
49+
name,
50+
commands
51+
};
52+
53+
//
54+
// Module exports
55+
//
56+
exports.cluster = CommissionerCommands;

0 commit comments

Comments
 (0)