Skip to content

Commit

Permalink
[FS Example] Update the FS Example apps to support fabric sync setup …
Browse files Browse the repository at this point in the history
…process part I (#34906)

* [FS Example] Update the FS Example apps to support fabric sync setup process

* Move all command handling to device manager using StringBuilder
  • Loading branch information
yufengwangca authored and pull[bot] committed Sep 23, 2024
1 parent 2a9380c commit 1482400
Show file tree
Hide file tree
Showing 11 changed files with 556 additions and 56 deletions.
10 changes: 2 additions & 8 deletions examples/fabric-admin/commands/clusters/ReportCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void ReportCommand::OnAttributeData(const app::ConcreteDataAttributePath & path,

LogErrorOnFailure(RemoteDataModelLogger::LogAttributeAsJSON(path, data));

DeviceMgr().HandleAttributeChange(path, data);
DeviceMgr().HandleAttributeData(path, data);
}

void ReportCommand::OnEventData(const app::EventHeader & eventHeader, TLV::TLVReader * data, const app::StatusIB * status)
Expand All @@ -73,11 +73,5 @@ void ReportCommand::OnEventData(const app::EventHeader & eventHeader, TLV::TLVRe

LogErrorOnFailure(RemoteDataModelLogger::LogEventAsJSON(eventHeader, data));

CHIP_ERROR error = DataModelLogger::LogEvent(eventHeader, data);
if (CHIP_NO_ERROR != error)
{
ChipLogError(NotSpecified, "Response Failure: Can not decode Data");
mError = error;
return;
}
DeviceMgr().HandleEventData(eventHeader, data);
}
2 changes: 2 additions & 0 deletions examples/fabric-admin/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ inline constexpr char kIdentityGamma[] = "gamma";
// (CASE) communcation.
inline constexpr char kIdentityNull[] = "null-fabric-commissioner";

constexpr uint16_t kMaxCommandSize = 128;

class CHIPCommand : public Command
{
public:
Expand Down
53 changes: 27 additions & 26 deletions examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,28 @@ namespace {
// Constants
constexpr uint32_t kCommissionPrepareTimeMs = 500;
constexpr uint16_t kMaxManaulCodeLength = 21;
constexpr uint16_t kSubscribeMinInterval = 0;
constexpr uint16_t kSubscribeMaxInterval = 60;
constexpr uint16_t kRemoteBridgePort = 5540;

void CheckFabricBridgeSynchronizationSupport(intptr_t ignored)
{
DeviceMgr().ReadSupportedDeviceCategories();
}

} // namespace

void FabricSyncAddBridgeCommand::OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR err)
{
if (mBridgeNodeId != deviceId)
{
ChipLogProgress(NotSpecified, "Commissioning complete for non-bridge device: NodeId: " ChipLogFormatX64,
ChipLogValueX64(deviceId));
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to pair non-bridge device (0x:" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
ChipLogValueX64(deviceId), err.Format());
}
else
{
ChipLogProgress(NotSpecified, "Commissioning complete for non-bridge device: NodeId: " ChipLogFormatX64,
ChipLogValueX64(deviceId));
}
return;
}

Expand All @@ -56,11 +66,15 @@ void FabricSyncAddBridgeCommand::OnCommissioningComplete(chip::NodeId deviceId,
ChipLogProgress(NotSpecified, "Successfully paired bridge device: NodeId: " ChipLogFormatX64,
ChipLogValueX64(mBridgeNodeId));

char command[kMaxCommandSize];
snprintf(command, sizeof(command), "descriptor subscribe parts-list %d %d %ld %d", kSubscribeMinInterval,
kSubscribeMaxInterval, mBridgeNodeId, kAggragatorEndpointId);
DeviceMgr().SubscribeRemoteFabricBridge();

PushCommand(command);
// After successful commissioning of the Commissionee, initiate Reverse Commissioning
// via the Commissioner Control Cluster. However, we must first verify that the
// remote Fabric-Bridge supports Fabric Synchronization.
//
// Note: The Fabric-Admin MUST NOT send the RequestCommissioningApproval command
// if the remote Fabric-Bridge lacks Fabric Synchronization support.
DeviceLayer::PlatformMgr().ScheduleWork(CheckFabricBridgeSynchronizationSupport, 0);
}
else
{
Expand All @@ -80,10 +94,6 @@ CHIP_ERROR FabricSyncAddBridgeCommand::RunCommand(NodeId remoteId)
return CHIP_NO_ERROR;
}

char command[kMaxCommandSize];
snprintf(command, sizeof(command), "pairing already-discovered %ld %d %s %d", remoteId, kSetupPinCode,
reinterpret_cast<const char *>(mRemoteAddr.data()), kRemoteBridgePort);

PairingCommand * pairingCommand = static_cast<PairingCommand *>(CommandMgr().GetCommandByName("pairing", "already-discovered"));

if (pairingCommand == nullptr)
Expand All @@ -95,7 +105,7 @@ CHIP_ERROR FabricSyncAddBridgeCommand::RunCommand(NodeId remoteId)
pairingCommand->RegisterCommissioningDelegate(this);
mBridgeNodeId = remoteId;

PushCommand(command);
DeviceMgr().PairRemoteFabricBridge(remoteId, reinterpret_cast<const char *>(mRemoteAddr.data()));

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -136,9 +146,6 @@ CHIP_ERROR FabricSyncRemoveBridgeCommand::RunCommand()

mBridgeNodeId = bridgeNodeId;

char command[kMaxCommandSize];
snprintf(command, sizeof(command), "pairing unpair %ld", mBridgeNodeId);

PairingCommand * pairingCommand = static_cast<PairingCommand *>(CommandMgr().GetCommandByName("pairing", "unpair"));

if (pairingCommand == nullptr)
Expand All @@ -149,7 +156,7 @@ CHIP_ERROR FabricSyncRemoveBridgeCommand::RunCommand()

pairingCommand->RegisterPairingDelegate(this);

PushCommand(command);
DeviceMgr().UnpairRemoteFabricBridge();

return CHIP_NO_ERROR;
}
Expand All @@ -163,9 +170,7 @@ void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_
CHIP_ERROR error = ManualSetupPayloadGenerator(payload).payloadDecimalStringRepresentation(manualCode);
if (error == CHIP_NO_ERROR)
{
char command[kMaxCommandSize];
NodeId nodeId = DeviceMgr().GetNextAvailableNodeId();
snprintf(command, sizeof(command), "pairing code %ld %s", nodeId, payloadBuffer);

PairingCommand * pairingCommand = static_cast<PairingCommand *>(CommandMgr().GetCommandByName("pairing", "code"));

Expand All @@ -180,7 +185,7 @@ void FabricSyncDeviceCommand::OnCommissioningWindowOpened(NodeId deviceId, CHIP_

usleep(kCommissionPrepareTimeMs * 1000);

PushCommand(command);
DeviceMgr().PairRemoteDevice(nodeId, payloadBuffer);
}
else
{
Expand Down Expand Up @@ -224,10 +229,6 @@ CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)
return CHIP_NO_ERROR;
}

char command[kMaxCommandSize];
snprintf(command, sizeof(command), "pairing open-commissioning-window %ld %d %d %d %d %d", DeviceMgr().GetRemoteBridgeNodeId(),
remoteId, kEnhancedCommissioningMethod, kWindowTimeout, kIteration, kDiscriminator);

OpenCommissioningWindowCommand * openCommand =
static_cast<OpenCommissioningWindowCommand *>(CommandMgr().GetCommandByName("pairing", "open-commissioning-window"));

Expand All @@ -238,7 +239,7 @@ CHIP_ERROR FabricSyncDeviceCommand::RunCommand(EndpointId remoteId)

openCommand->RegisterDelegate(this);

PushCommand(command);
DeviceMgr().OpenRemoteDeviceCommissioningWindow(remoteId);

return CHIP_NO_ERROR;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@
#include <commands/pairing/OpenCommissioningWindowCommand.h>
#include <commands/pairing/PairingCommand.h>

constexpr uint32_t kSetupPinCode = 20202021;
constexpr uint16_t kMaxCommandSize = 64;
constexpr uint16_t kDiscriminator = 3840;
constexpr uint16_t kWindowTimeout = 300;
constexpr uint16_t kIteration = 1000;
constexpr uint16_t kAggragatorEndpointId = 1;
constexpr uint8_t kEnhancedCommissioningMethod = 1;

class FabricSyncAddBridgeCommand : public CHIPCommand, public CommissioningDelegate
{
public:
Expand Down
Loading

0 comments on commit 1482400

Please sign in to comment.