Skip to content

Commit 655b08f

Browse files
committed
Add sync-device command in fabricsync command sets
1 parent 1d4ac45 commit 655b08f

10 files changed

+139
-5
lines changed

examples/fabric-admin/commands/common/Commands.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ static void DetectAndLogMismatchedDoubleQuotes(int argc, char ** argv)
150150

151151
} // namespace
152152

153+
// Define the static member
154+
Commands Commands::sInstance;
155+
153156
void Commands::Register(const char * commandSetName, commands_list commandsList, const char * helpText, bool isCluster)
154157
{
155158
VerifyOrDieWithMsg(isCluster || helpText != nullptr, NotSpecified, "Non-cluster command sets must have help text");
@@ -337,6 +340,7 @@ Commands::CommandSetMap::iterator Commands::GetCommandSet(std::string commandSet
337340
{
338341
std::string key(commandSet.first);
339342
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
343+
340344
if (key.compare(commandSetName) == 0)
341345
{
342346
return mCommandSets.find(commandSet.first);
@@ -346,6 +350,23 @@ Commands::CommandSetMap::iterator Commands::GetCommandSet(std::string commandSet
346350
return mCommandSets.end();
347351
}
348352

353+
Command * Commands::GetCommandByName(std::string commandSetName, std::string commandName)
354+
{
355+
auto commandSetIter = GetCommandSet(commandSetName);
356+
if (commandSetIter != mCommandSets.end())
357+
{
358+
auto & commandList = commandSetIter->second.commands;
359+
for (auto & command : commandList)
360+
{
361+
if (command->GetName() == commandName)
362+
{
363+
return command.get();
364+
}
365+
}
366+
}
367+
return nullptr;
368+
}
369+
349370
Command * Commands::GetCommand(CommandsVector & commands, std::string commandName)
350371
{
351372
for (auto & command : commands)

examples/fabric-admin/commands/common/Commands.h

+16
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Commands
4545
int Run(int argc, char ** argv);
4646
int RunInteractive(const char * command, const chip::Optional<char *> & storageDirectory, bool advertiseOperational);
4747

48+
Command * GetCommandByName(std::string commandSetName, std::string commandName);
49+
4850
private:
4951
struct CommandSet
5052
{
@@ -87,4 +89,18 @@ class Commands
8789
#ifdef CONFIG_USE_LOCAL_STORAGE
8890
PersistentStorage mStorage;
8991
#endif // CONFIG_USE_LOCAL_STORAGE
92+
93+
friend Commands & CommandMgr();
94+
static Commands sInstance;
9095
};
96+
97+
/**
98+
* Returns the public interface of the CommandManager singleton object.
99+
*
100+
* Applications should use this to access features of the CommandManager
101+
* object.
102+
*/
103+
inline Commands & CommandMgr()
104+
{
105+
return Commands::sInstance;
106+
}

examples/fabric-admin/commands/fabric-sync/Commands.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void registerCommandsFabricSync(Commands & commands, CredentialIssuerCommands *
2727

2828
commands_list clusterCommands = {
2929
make_unique<FabricSyncAddDeviceCommand>(credsIssuerConfig),
30+
make_unique<FabricSyncDeviceCommand>(credsIssuerConfig),
3031
};
3132

3233
commands.RegisterCommandSet(clusterName, clusterCommands, "Commands for fabric synchronization.");

examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "FabricSyncCommand.h"
2020
#include <commands/common/RemoteDataModelLogger.h>
21+
#include <commands/interactive/InteractiveCommands.h>
22+
#include <setup_payload/ManualSetupPayloadGenerator.h>
2123
#include <thread>
2224
#include <unistd.h>
2325

@@ -27,6 +29,13 @@
2729

2830
using namespace ::chip;
2931

32+
namespace {
33+
34+
constexpr uint16_t kRetryIntervalS = 3;
35+
constexpr uint16_t kMaxManaulCodeLength = 11;
36+
37+
} // namespace
38+
3039
CHIP_ERROR FabricSyncAddDeviceCommand::RunCommand(NodeId remoteId)
3140
{
3241
#if defined(PW_RPC_ENABLED)
@@ -36,3 +45,43 @@ CHIP_ERROR FabricSyncAddDeviceCommand::RunCommand(NodeId remoteId)
3645
return CHIP_ERROR_NOT_IMPLEMENTED;
3746
#endif
3847
}
48+
49+
void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_ERROR err, chip::SetupPayload payload)
50+
{
51+
if (err == CHIP_NO_ERROR)
52+
{
53+
char payloadBuffer[kMaxManaulCodeLength + 1];
54+
MutableCharSpan manualCode(payloadBuffer);
55+
CHIP_ERROR error = ManualSetupPayloadGenerator(payload).payloadDecimalStringRepresentation(manualCode);
56+
if (error == CHIP_NO_ERROR)
57+
{
58+
char command[64];
59+
snprintf(command, sizeof(command), "pairing code 3 %s", payloadBuffer);
60+
PushCommand(command);
61+
}
62+
else
63+
{
64+
ChipLogError(NotSpecified, "Unable to generate manual code for setup payload: %" CHIP_ERROR_FORMAT, error.Format());
65+
}
66+
}
67+
}
68+
69+
CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)
70+
{
71+
char command[64];
72+
snprintf(command, sizeof(command), "pairing open-commissioning-window 1 %d 1 300 1000 3840", remoteId);
73+
74+
OpenCommissioningWindowCommand * openCommand =
75+
static_cast<OpenCommissioningWindowCommand *>(CommandMgr().GetCommandByName("pairing", "open-commissioning-window"));
76+
77+
if (openCommand == nullptr)
78+
{
79+
return CHIP_ERROR_UNINITIALIZED;
80+
}
81+
82+
openCommand->RegisterDelegate(this);
83+
84+
PushCommand(command);
85+
86+
return CHIP_NO_ERROR;
87+
}

examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.h

+22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#pragma once
2020

2121
#include <commands/common/CHIPCommand.h>
22+
#include <commands/pairing/OpenCommissioningWindowCommand.h>
2223

2324
class FabricSyncAddDeviceCommand : public CHIPCommand
2425
{
@@ -38,3 +39,24 @@ class FabricSyncAddDeviceCommand : public CHIPCommand
3839

3940
CHIP_ERROR RunCommand(NodeId remoteId);
4041
};
42+
43+
class FabricSyncDeviceCommand : public CHIPCommand, CommissioningWindowDelegate
44+
{
45+
public:
46+
FabricSyncDeviceCommand(CredentialIssuerCommands * credIssuerCommands) : CHIPCommand("sync-device", credIssuerCommands)
47+
{
48+
AddArgument("endpointid", 0, UINT16_MAX, &mEndpointId);
49+
}
50+
51+
void OnCommissioningWindowOpened(NodeId deviceId, CHIP_ERROR status, chip::SetupPayload payload) override;
52+
53+
/////////// CHIPCommand Interface /////////
54+
CHIP_ERROR RunCommand() override { return RunCommand(mEndpointId); }
55+
56+
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(1); }
57+
58+
private:
59+
chip::EndpointId mEndpointId;
60+
61+
CHIP_ERROR RunCommand(chip::EndpointId remoteId);
62+
};

examples/fabric-admin/commands/interactive/InteractiveCommands.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ char * InteractiveStartCommand::GetCommand(char * command)
149149
command = new char[cmd.length() + 1];
150150
strcpy(command, cmd.c_str());
151151

152-
ChipLogProgress(NotSpecified, "GetCommand: %s", command);
153-
154152
// Do not save empty lines
155153
if (command != nullptr && *command)
156154
{

examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ CHIP_ERROR OpenCommissioningWindowCommand::RunCommand()
4747
void OpenCommissioningWindowCommand::OnOpenCommissioningWindowResponse(void * context, NodeId remoteId, CHIP_ERROR err,
4848
chip::SetupPayload payload)
4949
{
50-
LogErrorOnFailure(err);
50+
OpenCommissioningWindowCommand * self = static_cast<OpenCommissioningWindowCommand *>(context);
51+
if (self->mDelegate)
52+
{
53+
self->mDelegate->OnCommissioningWindowOpened(remoteId, err, payload);
54+
self->UnregisterDelegate();
55+
}
5156

57+
LogErrorOnFailure(err);
5258
OnOpenBasicCommissioningWindowResponse(context, remoteId, err);
5359
}
5460

examples/fabric-admin/commands/pairing/OpenCommissioningWindowCommand.h

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
#include <controller/CommissioningWindowOpener.h>
2323
#include <lib/support/CHIPMem.h>
2424

25+
class CommissioningWindowDelegate
26+
{
27+
public:
28+
virtual void OnCommissioningWindowOpened(chip::NodeId deviceId, CHIP_ERROR err, chip::SetupPayload payload) = 0;
29+
virtual ~CommissioningWindowDelegate() = default;
30+
};
31+
2532
class OpenCommissioningWindowCommand : public CHIPCommand
2633
{
2734
public:
@@ -31,6 +38,7 @@ class OpenCommissioningWindowCommand : public CHIPCommand
3138
mOnOpenBasicCommissioningWindowCallback(OnOpenBasicCommissioningWindowResponse, this)
3239
{
3340
AddArgument("node-id", 0, UINT64_MAX, &mNodeId, "Node to send command to.");
41+
AddArgument("endpoint-id", 0, UINT16_MAX, &mEndpointId, "Endpoint to send command to.");
3442
AddArgument("option", 0, 2, &mCommissioningWindowOption,
3543
"1 to use Enhanced Commissioning Method.\n 0 to use Basic Commissioning Method.");
3644
AddArgument("window-timeout", 0, UINT16_MAX, &mCommissioningWindowTimeout,
@@ -41,6 +49,9 @@ class OpenCommissioningWindowCommand : public CHIPCommand
4149
AddArgument("timeout", 0, UINT16_MAX, &mTimeout, "Time, in seconds, before this command is considered to have timed out.");
4250
}
4351

52+
void RegisterDelegate(CommissioningWindowDelegate * delegate) { mDelegate = delegate; }
53+
void UnregisterDelegate() { mDelegate = nullptr; }
54+
4455
/////////// CHIPCommand Interface /////////
4556
CHIP_ERROR RunCommand() override;
4657

@@ -50,7 +61,9 @@ class OpenCommissioningWindowCommand : public CHIPCommand
5061

5162
private:
5263
NodeId mNodeId;
64+
chip::EndpointId mEndpointId;
5365
chip::Controller::CommissioningWindowOpener::CommissioningWindowOption mCommissioningWindowOption;
66+
CommissioningWindowDelegate * mDelegate = nullptr;
5467
uint16_t mCommissioningWindowTimeout;
5568
uint32_t mIteration;
5669
uint16_t mDiscriminator;

examples/fabric-admin/commands/pairing/PairingCommand.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
#include <string>
3232

33+
#if defined(PW_RPC_ENABLED)
34+
#include <rpc/RpcClient.h>
35+
#endif
36+
3337
using namespace ::chip;
3438
using namespace ::chip::Controller;
3539

@@ -389,7 +393,11 @@ void PairingCommand::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
389393
{
390394
if (err == CHIP_NO_ERROR)
391395
{
392-
ChipLogProgress(NotSpecified, "Device commissioning completed with success");
396+
fprintf(stderr, "New device with Node ID: 0x%lx has been successfully added.\n", nodeId);
397+
398+
#if defined(PW_RPC_ENABLED)
399+
AddSynchronizedDevice(nodeId);
400+
#endif
393401
}
394402
else
395403
{

examples/fabric-admin/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int main(int argc, char * argv[])
5858
}
5959

6060
ExampleCredentialIssuerCommands credIssuerCommands;
61-
Commands commands;
61+
Commands & commands = CommandMgr();
6262

6363
registerCommandsFabricSync(commands, &credIssuerCommands);
6464
registerCommandsInteractive(commands, &credIssuerCommands);

0 commit comments

Comments
 (0)