Skip to content

Commit 585a74c

Browse files
authored
Fix failing to cleanup the synced device after it is removed (project-chip#36412)
1 parent e338404 commit 585a74c

File tree

7 files changed

+29
-47
lines changed

7 files changed

+29
-47
lines changed

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <commands/interactive/InteractiveCommands.h>
2424
#include <controller/ExampleOperationalCredentialsIssuer.h>
2525
#include <crypto/CHIPCryptoPAL.h>
26+
#include <device_manager/DeviceManager.h>
2627
#include <device_manager/DeviceSynchronization.h>
2728
#include <lib/core/CHIPSafeCasts.h>
2829
#include <lib/support/logging/CHIPLogging.h>
@@ -33,10 +34,6 @@
3334

3435
#include <string>
3536

36-
#if defined(PW_RPC_ENABLED)
37-
#include <rpc/RpcClient.h>
38-
#endif
39-
4037
using namespace ::chip;
4138
using namespace ::chip::Controller;
4239

@@ -534,16 +531,16 @@ void PairingCommand::OnCurrentFabricRemove(void * context, NodeId nodeId, CHIP_E
534531
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
535532
VerifyOrReturn(command != nullptr, ChipLogError(NotSpecified, "OnCurrentFabricRemove: context is null"));
536533

534+
ChipLogProgress(NotSpecified, "PairingCommand::OnCurrentFabricRemove");
535+
537536
if (err == CHIP_NO_ERROR)
538537
{
539538
// print to console
540539
fprintf(stderr, "Device with Node ID: 0x%lx has been successfully removed.\n", nodeId);
541540

542-
#if defined(PW_RPC_ENABLED)
543541
app::InteractionModelEngine::GetInstance()->ShutdownSubscriptions(command->CurrentCommissioner().GetFabricIndex(), nodeId);
544542
ScopedNodeId scopedNodeId(nodeId, command->CurrentCommissioner().GetFabricIndex());
545-
admin::RemoveSynchronizedDevice(scopedNodeId);
546-
#endif
543+
admin::DeviceMgr().RemoveSyncedDevice(scopedNodeId);
547544
}
548545
else
549546
{

examples/fabric-admin/device_manager/DeviceManager.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <cstdio>
2626
#include <string>
2727

28+
#if defined(PW_RPC_ENABLED)
29+
#include <rpc/RpcClient.h>
30+
#endif
31+
2832
using namespace chip;
2933

3034
namespace admin {
@@ -109,8 +113,13 @@ Device * DeviceManager::FindDeviceByNode(NodeId nodeId)
109113
return nullptr;
110114
}
111115

112-
void DeviceManager::RemoveSyncedDevice(NodeId nodeId)
116+
void DeviceManager::RemoveSyncedDevice(chip::ScopedNodeId scopedNodeId)
113117
{
118+
#if defined(PW_RPC_ENABLED)
119+
RemoveSynchronizedDevice(scopedNodeId);
120+
#endif
121+
122+
NodeId nodeId = scopedNodeId.GetNodeId();
114123
Device * device = FindDeviceByNode(nodeId);
115124
if (device == nullptr)
116125
{
@@ -259,6 +268,10 @@ void DeviceManager::HandleReadSupportedDeviceCategories(TLV::TLVReader & data)
259268
ChipLogProgress(NotSpecified, "Remote Fabric-Bridge supports Fabric Synchronization, start reverse commissioning.");
260269
RequestCommissioningApproval();
261270
}
271+
else
272+
{
273+
ChipLogProgress(NotSpecified, "Remote Fabric-Bridge does not support Fabric Synchronization.");
274+
}
262275
}
263276

264277
void DeviceManager::RequestCommissioningApproval()
@@ -406,6 +419,8 @@ void DeviceManager::SendCommissionNodeRequest(uint64_t requestId, uint16_t respo
406419

407420
void DeviceManager::HandleReverseOpenCommissioningWindow(TLV::TLVReader & data)
408421
{
422+
ChipLogProgress(NotSpecified, "Handle ReverseOpenCommissioningWindow command.");
423+
409424
app::Clusters::CommissionerControl::Commands::ReverseOpenCommissioningWindow::DecodableType value;
410425
CHIP_ERROR error = app::DataModel::Decode(data, value);
411426

@@ -459,17 +474,4 @@ void DeviceManager::HandleCommandResponse(const app::ConcreteCommandPath & path,
459474
}
460475
}
461476

462-
void DeviceManager::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err)
463-
{
464-
if (err != CHIP_NO_ERROR)
465-
{
466-
ChipLogError(NotSpecified, "Failed to remove synced device:(" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
467-
ChipLogValueX64(deviceId), err.Format());
468-
return;
469-
}
470-
471-
RemoveSyncedDevice(deviceId);
472-
ChipLogProgress(NotSpecified, "Synced device with NodeId:" ChipLogFormatX64 " has been removed.", ChipLogValueX64(deviceId));
473-
}
474-
475477
} // namespace admin

examples/fabric-admin/device_manager/DeviceManager.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Device
5050
chip::EndpointId mEndpointId;
5151
};
5252

53-
class DeviceManager : public PairingDelegate
53+
class DeviceManager
5454
{
5555
public:
5656
DeviceManager() = default;
@@ -77,7 +77,7 @@ class DeviceManager : public PairingDelegate
7777

7878
void AddSyncedDevice(const Device & device);
7979

80-
void RemoveSyncedDevice(chip::NodeId nodeId);
80+
void RemoveSyncedDevice(chip::ScopedNodeId scopedNodeId);
8181

8282
/**
8383
* @brief Determines whether a given nodeId corresponds to the "current bridge device," either local or remote.
@@ -177,8 +177,6 @@ class DeviceManager : public PairingDelegate
177177
private:
178178
friend DeviceManager & DeviceMgr();
179179

180-
void OnDeviceRemoved(chip::NodeId deviceId, CHIP_ERROR err) override;
181-
182180
void RequestCommissioningApproval();
183181

184182
void HandleReadSupportedDeviceCategories(chip::TLV::TLVReader & data);

examples/fabric-admin/device_manager/PairingManager.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323
#include <sys/types.h>
2424

2525
#include <commands/common/CHIPCommand.h>
26+
#include <device_manager/DeviceManager.h>
2627
#include <device_manager/DeviceSynchronization.h>
2728
#include <lib/support/logging/CHIPLogging.h>
2829
#include <setup_payload/ManualSetupPayloadParser.h>
2930
#include <setup_payload/QRCodeSetupPayloadParser.h>
3031

31-
#if defined(PW_RPC_ENABLED)
32-
#include <rpc/RpcClient.h>
33-
#endif
34-
3532
using namespace ::chip;
3633
using namespace ::chip::Controller;
3734

@@ -547,6 +544,8 @@ void PairingManager::OnCurrentFabricRemove(void * context, NodeId nodeId, CHIP_E
547544
PairingManager * self = reinterpret_cast<PairingManager *>(context);
548545
VerifyOrReturn(self != nullptr, ChipLogError(NotSpecified, "OnCurrentFabricRemove: context is null"));
549546

547+
ChipLogProgress(NotSpecified, "PairingManager::OnCurrentFabricRemove");
548+
550549
if (err == CHIP_NO_ERROR)
551550
{
552551
// print to console
@@ -558,12 +557,10 @@ void PairingManager::OnCurrentFabricRemove(void * context, NodeId nodeId, CHIP_E
558557
self->SetPairingDelegate(nullptr);
559558
}
560559

561-
#if defined(PW_RPC_ENABLED)
562560
FabricIndex fabricIndex = self->CurrentCommissioner().GetFabricIndex();
563561
app::InteractionModelEngine::GetInstance()->ShutdownSubscriptions(fabricIndex, nodeId);
564562
ScopedNodeId scopedNodeId(nodeId, fabricIndex);
565-
RemoveSynchronizedDevice(scopedNodeId);
566-
#endif
563+
DeviceMgr().RemoveSyncedDevice(scopedNodeId);
567564
}
568565
else
569566
{

examples/fabric-sync/admin/DeviceManager.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,4 @@ CHIP_ERROR DeviceManager::UnpairRemoteDevice(NodeId nodeId)
143143
return CHIP_NO_ERROR;
144144
}
145145

146-
void DeviceManager::OnDeviceRemoved(NodeId deviceId, CHIP_ERROR err)
147-
{
148-
if (err != CHIP_NO_ERROR)
149-
{
150-
ChipLogError(NotSpecified, "Failed to remove synced device:(" ChipLogFormatX64 ") with error: %" CHIP_ERROR_FORMAT,
151-
ChipLogValueX64(deviceId), err.Format());
152-
return;
153-
}
154-
155-
ChipLogProgress(NotSpecified, "Synced device with NodeId:" ChipLogFormatX64 " has been removed.", ChipLogValueX64(deviceId));
156-
}
157-
158146
} // namespace admin

examples/fabric-sync/admin/DeviceManager.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
namespace admin {
2727

28-
class DeviceManager : public PairingDelegate
28+
class DeviceManager
2929
{
3030
public:
3131
DeviceManager() = default;
@@ -119,8 +119,6 @@ class DeviceManager : public PairingDelegate
119119
private:
120120
friend DeviceManager & DeviceMgr();
121121

122-
void OnDeviceRemoved(chip::NodeId deviceId, CHIP_ERROR err) override;
123-
124122
static DeviceManager sInstance;
125123

126124
chip::NodeId mLastUsedNodeId = 0;

examples/fabric-sync/admin/PairingManager.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ void PairingManager::OnCurrentFabricRemove(void * context, NodeId nodeId, CHIP_E
450450
PairingManager * self = reinterpret_cast<PairingManager *>(context);
451451
VerifyOrReturn(self != nullptr, ChipLogError(NotSpecified, "OnCurrentFabricRemove: context is null"));
452452

453+
ChipLogProgress(NotSpecified, "PairingManager::OnCurrentFabricRemove");
454+
453455
if (err == CHIP_NO_ERROR)
454456
{
455457
// print to console

0 commit comments

Comments
 (0)