Skip to content

Commit e25647f

Browse files
authored
[Fabric-Bridge] Use Meyers' Singleton pattern for BridgedDeviceManager (project-chip#36440)
1 parent 8a3fce1 commit e25647f

File tree

6 files changed

+18
-28
lines changed

6 files changed

+18
-28
lines changed

examples/fabric-bridge-app/fabric-bridge-common/include/BridgedDeviceManager.h

+6-13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class BridgedDeviceManager
3131
public:
3232
BridgedDeviceManager() = default;
3333

34+
static BridgedDeviceManager & Instance()
35+
{
36+
static BridgedDeviceManager instance;
37+
return instance;
38+
}
39+
3440
/**
3541
* @brief Initializes the BridgedDeviceManager.
3642
*
@@ -112,8 +118,6 @@ class BridgedDeviceManager
112118
BridgedDevice * GetDeviceByUniqueId(const std::string & id);
113119

114120
private:
115-
friend BridgedDeviceManager & BridgeDeviceMgr();
116-
117121
/**
118122
* Creates a new unique ID that is not used by any other mDevice
119123
*/
@@ -126,15 +130,4 @@ class BridgedDeviceManager
126130
std::unique_ptr<BridgedDevice> mDevices[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT + 1];
127131
};
128132

129-
/**
130-
* Returns the public interface of the BridgedDeviceManager singleton object.
131-
*
132-
* Applications should use this to access features of the BridgedDeviceManager
133-
* object.
134-
*/
135-
inline BridgedDeviceManager & BridgeDeviceMgr()
136-
{
137-
return BridgedDeviceManager::sInstance;
138-
}
139-
140133
} // namespace bridge

examples/fabric-bridge-app/fabric-bridge-common/src/BridgedAdministratorCommissioning.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ CHIP_ERROR BridgedAdministratorCommissioning::Read(const ConcreteReadAttributePa
4545
{
4646
VerifyOrDie(aPath.mClusterId == Clusters::AdministratorCommissioning::Id);
4747
EndpointId endpointId = aPath.mEndpointId;
48-
BridgedDevice * device = BridgeDeviceMgr().GetDevice(endpointId);
48+
BridgedDevice * device = BridgedDeviceManager::Instance().GetDevice(endpointId);
4949

5050
if (!device)
5151
{

examples/fabric-bridge-app/fabric-bridge-common/src/BridgedDeviceBasicInformationImpl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ CHIP_ERROR BridgedDeviceBasicInformationImpl::Read(const ConcreteReadAttributePa
3636
// Registration is done for the bridged device basic information only
3737
VerifyOrDie(path.mClusterId == app::Clusters::BridgedDeviceBasicInformation::Id);
3838

39-
BridgedDevice * dev = BridgeDeviceMgr().GetDevice(path.mEndpointId);
39+
BridgedDevice * dev = BridgedDeviceManager::Instance().GetDevice(path.mEndpointId);
4040
VerifyOrReturnError(dev != nullptr, CHIP_ERROR_NOT_FOUND);
4141

4242
switch (path.mAttributeId)
@@ -93,7 +93,7 @@ CHIP_ERROR BridgedDeviceBasicInformationImpl::Write(const ConcreteDataAttributeP
9393
{
9494
VerifyOrDie(path.mClusterId == app::Clusters::BridgedDeviceBasicInformation::Id);
9595

96-
BridgedDevice * dev = BridgeDeviceMgr().GetDevice(path.mEndpointId);
96+
BridgedDevice * dev = BridgedDeviceManager::Instance().GetDevice(path.mEndpointId);
9797
VerifyOrReturnError(dev != nullptr, CHIP_ERROR_NOT_FOUND);
9898

9999
if (!dev->IsReachable())

examples/fabric-bridge-app/fabric-bridge-common/src/BridgedDeviceManager.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ const EmberAfDeviceType sBridgedDeviceTypes[] = { { DEVICE_TYPE_BRIDGED_NODE, DE
176176

177177
} // namespace
178178

179-
// Define the static member
180-
BridgedDeviceManager BridgedDeviceManager::sInstance;
181-
182179
void BridgedDeviceManager::Init()
183180
{
184181
mFirstDynamicEndpointId = static_cast<chip::EndpointId>(

examples/fabric-bridge-app/linux/RpcServer.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ pw::Status FabricBridge::AddSynchronizedDevice(const chip_rpc_SynchronizedDevice
118118
device->SetBridgedAttributes(attributes);
119119
device->SetIcd(request.has_is_icd && request.is_icd);
120120

121-
auto result = BridgeDeviceMgr().AddDeviceEndpoint(std::move(device), 1 /* parentEndpointId */);
121+
auto result = BridgedDeviceManager::Instance().AddDeviceEndpoint(std::move(device), 1 /* parentEndpointId */);
122122
if (!result.has_value())
123123
{
124124
ChipLogError(NotSpecified, "Failed to add device with Id=[%d:0x" ChipLogFormatX64 "]", scopedNodeId.GetFabricIndex(),
125125
ChipLogValueX64(scopedNodeId.GetNodeId()));
126126
return pw::Status::Unknown();
127127
}
128128

129-
BridgedDevice * addedDevice = BridgeDeviceMgr().GetDeviceByScopedNodeId(scopedNodeId);
129+
BridgedDevice * addedDevice = BridgedDeviceManager::Instance().GetDeviceByScopedNodeId(scopedNodeId);
130130
VerifyOrDie(addedDevice);
131131

132132
CHIP_ERROR err = EcosystemInformation::EcosystemInformationServer::Instance().AddEcosystemInformationClusterToEndpoint(
@@ -143,7 +143,7 @@ pw::Status FabricBridge::RemoveSynchronizedDevice(const chip_rpc_SynchronizedDev
143143
ChipLogProgress(NotSpecified, "Received RemoveSynchronizedDevice: Id=[%d:" ChipLogFormatX64 "]", scopedNodeId.GetFabricIndex(),
144144
ChipLogValueX64(scopedNodeId.GetNodeId()));
145145

146-
auto removed_idx = BridgeDeviceMgr().RemoveDeviceByScopedNodeId(scopedNodeId);
146+
auto removed_idx = BridgedDeviceManager::Instance().RemoveDeviceByScopedNodeId(scopedNodeId);
147147
if (!removed_idx.has_value())
148148
{
149149
ChipLogError(NotSpecified, "Failed to remove device with Id=[%d:0x" ChipLogFormatX64 "]", scopedNodeId.GetFabricIndex(),
@@ -161,7 +161,7 @@ pw::Status FabricBridge::ActiveChanged(const chip_rpc_KeepActiveChanged & reques
161161
ChipLogProgress(NotSpecified, "Received ActiveChanged: Id=[%d:" ChipLogFormatX64 "]", scopedNodeId.GetFabricIndex(),
162162
ChipLogValueX64(scopedNodeId.GetNodeId()));
163163

164-
auto * device = BridgeDeviceMgr().GetDeviceByScopedNodeId(scopedNodeId);
164+
auto * device = BridgedDeviceManager::Instance().GetDeviceByScopedNodeId(scopedNodeId);
165165
if (device == nullptr)
166166
{
167167
ChipLogError(NotSpecified, "Could not find bridged device associated with Id=[%d:0x" ChipLogFormatX64 "]",
@@ -181,7 +181,7 @@ pw::Status FabricBridge::AdminCommissioningAttributeChanged(const chip_rpc_Admin
181181
ChipLogProgress(NotSpecified, "Received CADMIN attribute change: Id=[%d:" ChipLogFormatX64 "]", scopedNodeId.GetFabricIndex(),
182182
ChipLogValueX64(scopedNodeId.GetNodeId()));
183183

184-
auto * device = BridgeDeviceMgr().GetDeviceByScopedNodeId(scopedNodeId);
184+
auto * device = BridgedDeviceManager::Instance().GetDeviceByScopedNodeId(scopedNodeId);
185185
if (device == nullptr)
186186
{
187187
ChipLogError(NotSpecified, "Could not find bridged device associated with Id=[%d:0x" ChipLogFormatX64 "]",
@@ -220,7 +220,7 @@ pw::Status FabricBridge::DeviceReachableChanged(const chip_rpc_ReachabilityChang
220220
ChipLogProgress(NotSpecified, "Received device reachable changed: Id=[%d:" ChipLogFormatX64 "]", scopedNodeId.GetFabricIndex(),
221221
ChipLogValueX64(scopedNodeId.GetNodeId()));
222222

223-
auto * device = BridgeDeviceMgr().GetDeviceByScopedNodeId(scopedNodeId);
223+
auto * device = BridgedDeviceManager::Instance().GetDeviceByScopedNodeId(scopedNodeId);
224224
if (device == nullptr)
225225
{
226226
ChipLogError(NotSpecified, "Could not find bridged device associated with Id=[%d:0x" ChipLogFormatX64 "]",

examples/fabric-bridge-app/linux/main.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void AdministratorCommissioningCommandHandler::InvokeCommand(HandlerContext & ha
151151
Status status = Status::Failure;
152152

153153
#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
154-
BridgedDevice * device = BridgeDeviceMgr().GetDevice(endpointId);
154+
BridgedDevice * device = BridgedDeviceManager::Instance().GetDevice(endpointId);
155155

156156
// TODO: issues:#33784, need to make OpenCommissioningWindow synchronous
157157
if (device != nullptr &&
@@ -214,7 +214,7 @@ void BridgedDeviceInformationCommandHandler::InvokeCommand(HandlerContext & hand
214214
return;
215215
}
216216

217-
BridgedDevice * device = BridgeDeviceMgr().GetDevice(endpointId);
217+
BridgedDevice * device = BridgedDeviceManager::Instance().GetDevice(endpointId);
218218
if (device == nullptr || !device->IsIcd())
219219
{
220220
handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Status::Failure);
@@ -262,7 +262,7 @@ void ApplicationInit()
262262
AttemptRpcClientConnect(&DeviceLayer::SystemLayer(), nullptr);
263263
#endif
264264

265-
bridge::BridgeDeviceMgr().Init();
265+
bridge::BridgedDeviceManager::Instance().Init();
266266
VerifyOrDie(bridge::gBridgedAdministratorCommissioning.Init() == CHIP_NO_ERROR);
267267

268268
VerifyOrDieWithMsg(bridge::CommissionerControlInit() == CHIP_NO_ERROR, NotSpecified,

0 commit comments

Comments
 (0)