Skip to content

Commit

Permalink
[Fabric-Admin] Fix compile errors when RPC is disabled (#35639)
Browse files Browse the repository at this point in the history
* Fix compile error when RPC is disabled

* Restyled by whitespace

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
yufengwangca and restyled-commits authored Sep 18, 2024
1 parent 1d6b0a5 commit 2ac9bed
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
25 changes: 19 additions & 6 deletions examples/fabric-admin/device_manager/DeviceSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/

#include "DeviceSubscription.h"

#if defined(PW_RPC_ENABLED)
#include "rpc/RpcClient.h"
#endif

#include <app/InteractionModelEngine.h>
#include <app/server/Server.h>
Expand Down Expand Up @@ -61,29 +64,35 @@ void DeviceSubscription::OnAttributeData(const ConcreteDataAttributePath & path,
CHIP_ERROR err = data->Get(windowStatus);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(NotSpecified, "Failed to read WindowStatus"));
VerifyOrReturn(windowStatus != Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum::kUnknownEnumValue);
#if defined(PW_RPC_ENABLED)
mCurrentAdministratorCommissioningAttributes.window_status = static_cast<uint32_t>(windowStatus);
mChangeDetected = true;
#endif
mChangeDetected = true;
break;
}
case Clusters::AdministratorCommissioning::Attributes::AdminFabricIndex::Id: {
#if defined(PW_RPC_ENABLED)
FabricIndex fabricIndex;
CHIP_ERROR err = data->Get(fabricIndex);
mCurrentAdministratorCommissioningAttributes.has_opener_fabric_index = err == CHIP_NO_ERROR;
if (mCurrentAdministratorCommissioningAttributes.has_opener_fabric_index)
{
mCurrentAdministratorCommissioningAttributes.opener_fabric_index = static_cast<uint32_t>(fabricIndex);
}
#endif
mChangeDetected = true;
break;
}
case Clusters::AdministratorCommissioning::Attributes::AdminVendorId::Id: {
#if defined(PW_RPC_ENABLED)
chip::VendorId vendorId;
CHIP_ERROR err = data->Get(vendorId);
mCurrentAdministratorCommissioningAttributes.has_opener_vendor_id = err == CHIP_NO_ERROR;
if (mCurrentAdministratorCommissioningAttributes.has_opener_vendor_id)
{
mCurrentAdministratorCommissioningAttributes.opener_vendor_id = static_cast<uint32_t>(vendorId);
}
#endif
mChangeDetected = true;
break;
}
Expand Down Expand Up @@ -111,7 +120,7 @@ void DeviceSubscription::OnDone(ReadClient * apReadClient)
// After calling mOnDoneCallback we are indicating that `this` is deleted and we shouldn't do anything else with
// DeviceSubscription.
MoveToState(State::AwaitingDestruction);
mOnDoneCallback(mCurrentAdministratorCommissioningAttributes.node_id);
mOnDoneCallback(mNodeId);
}

void DeviceSubscription::OnError(CHIP_ERROR error)
Expand All @@ -126,7 +135,7 @@ void DeviceSubscription::OnDeviceConnected(Messaging::ExchangeManager & exchange
// After calling mOnDoneCallback we are indicating that `this` is deleted and we shouldn't do anything else with
// DeviceSubscription.
MoveToState(State::AwaitingDestruction);
mOnDoneCallback(mCurrentAdministratorCommissioningAttributes.node_id);
mOnDoneCallback(mNodeId);
return;
}
VerifyOrDie(mState == State::Connecting);
Expand All @@ -151,7 +160,7 @@ void DeviceSubscription::OnDeviceConnected(Messaging::ExchangeManager & exchange
// After calling mOnDoneCallback we are indicating that `this` is deleted and we shouldn't do anything else with
// DeviceSubscription.
MoveToState(State::AwaitingDestruction);
mOnDoneCallback(mCurrentAdministratorCommissioningAttributes.node_id);
mOnDoneCallback(mNodeId);
return;
}
MoveToState(State::SubscriptionStarted);
Expand Down Expand Up @@ -194,7 +203,7 @@ void DeviceSubscription::OnDeviceConnectionFailure(const ScopedNodeId & peerId,
// After calling mOnDoneCallback we are indicating that `this` is deleted and we shouldn't do anything else with
// DeviceSubscription.
MoveToState(State::AwaitingDestruction);
mOnDoneCallback(mCurrentAdministratorCommissioningAttributes.node_id);
mOnDoneCallback(mNodeId);
}

CHIP_ERROR DeviceSubscription::StartSubscription(OnDoneCallback onDoneCallback, Controller::DeviceController & controller,
Expand All @@ -203,10 +212,14 @@ CHIP_ERROR DeviceSubscription::StartSubscription(OnDoneCallback onDoneCallback,
assertChipStackLockedByCurrentThread();
VerifyOrDie(mState == State::Idle);

mNodeId = nodeId;

#if defined(PW_RPC_ENABLED)
mCurrentAdministratorCommissioningAttributes = chip_rpc_AdministratorCommissioningChanged_init_default;
mCurrentAdministratorCommissioningAttributes.node_id = nodeId;
mCurrentAdministratorCommissioningAttributes.window_status =
static_cast<uint32_t>(Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum::kWindowNotOpen);
#endif

mOnDoneCallback = onDoneCallback;
MoveToState(State::Connecting);
Expand Down Expand Up @@ -243,5 +256,5 @@ void DeviceSubscription::StopSubscription()
// After calling mOnDoneCallback we are indicating that `this` is deleted and we shouldn't do anything else with
// DeviceSubscription.
MoveToState(State::AwaitingDestruction);
mOnDoneCallback(mCurrentAdministratorCommissioningAttributes.node_id);
mOnDoneCallback(mNodeId);
}
9 changes: 8 additions & 1 deletion examples/fabric-admin/device_manager/DeviceSubscription.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@

#include <memory>

#if defined(PW_RPC_ENABLED)
#include "fabric_bridge_service/fabric_bridge_service.pb.h"
#include "fabric_bridge_service/fabric_bridge_service.rpc.pb.h"
#endif

class DeviceSubscriptionManager;

/// Attribute subscription to attributes that are important to keep track and send to fabric-bridge
/// via RPC when change has been identified.
///
/// An instance of DeviceSubscription is intended to be used only once. Once a DeviceSubscription is
/// terminal, either from an error or from subscriptions getting shut down, we expect the instance
/// terminated, either from an error or from subscriptions getting shut down, we expect the instance
/// to be deleted. Any new subscription should instantiate another instance of DeviceSubscription.
class DeviceSubscription : public chip::app::ReadClient::Callback
{
Expand Down Expand Up @@ -78,13 +80,18 @@ class DeviceSubscription : public chip::app::ReadClient::Callback
void MoveToState(const State aTargetState);
const char * GetStateStr() const;

chip::NodeId mNodeId = chip::kUndefinedNodeId;

OnDoneCallback mOnDoneCallback;
std::unique_ptr<chip::app::ReadClient> mClient;

chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;

#if defined(PW_RPC_ENABLED)
chip_rpc_AdministratorCommissioningChanged mCurrentAdministratorCommissioningAttributes;
#endif

bool mChangeDetected = false;
State mState = State::Idle;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/

#include "DeviceSubscriptionManager.h"

#if defined(PW_RPC_ENABLED)
#include "rpc/RpcClient.h"
#endif

#include <app/InteractionModelEngine.h>
#include <app/server/Server.h>
Expand Down
28 changes: 23 additions & 5 deletions examples/fabric-admin/device_manager/DeviceSynchronization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#include "DeviceSynchronization.h"

#include "DeviceSubscriptionManager.h"

#if defined(PW_RPC_ENABLED)
#include "rpc/RpcClient.h"
#endif

#include <app/InteractionModelEngine.h>
#include <app/server/Server.h>
Expand All @@ -44,6 +47,7 @@ void OnDeviceConnectionFailureWrapper(void * context, const ScopedNodeId & peerI
reinterpret_cast<DeviceSynchronizer *>(context)->OnDeviceConnectionFailure(peerId, error);
}

#if defined(PW_RPC_ENABLED)
bool SuccessOrLog(CHIP_ERROR err, const char * name)
{
if (err == CHIP_NO_ERROR)
Expand All @@ -55,6 +59,7 @@ bool SuccessOrLog(CHIP_ERROR err, const char * name)

return false;
}
#endif

} // namespace

Expand Down Expand Up @@ -82,6 +87,7 @@ void DeviceSynchronizer::OnAttributeData(const ConcreteDataAttributePath & path,

switch (path.mAttributeId)
{
#if defined(PW_RPC_ENABLED)
case Clusters::BasicInformation::Attributes::UniqueID::Id:
mCurrentDeviceData.has_unique_id =
SuccessOrLog(data->GetString(mCurrentDeviceData.unique_id, sizeof(mCurrentDeviceData.unique_id)), "UniqueId");
Expand Down Expand Up @@ -120,6 +126,7 @@ void DeviceSynchronizer::OnAttributeData(const ConcreteDataAttributePath & path,
data->GetString(mCurrentDeviceData.software_version_string, sizeof(mCurrentDeviceData.software_version_string)),
"SoftwareVersionString");
break;
#endif // #if defined(PW_RPC_ENABLED)
default:
break;
}
Expand All @@ -134,7 +141,7 @@ void DeviceSynchronizer::OnReportEnd()
void DeviceSynchronizer::OnDone(chip::app::ReadClient * apReadClient)
{
#if defined(PW_RPC_ENABLED)
if (mState == State::ReceivedResponse && !DeviceMgr().IsCurrentBridgeDevice(mCurrentDeviceData.node_id))
if (mState == State::ReceivedResponse && !DeviceMgr().IsCurrentBridgeDevice(mNodeId))
{
GetUniqueId();
if (mState == State::GettingUid)
Expand Down Expand Up @@ -197,10 +204,14 @@ void DeviceSynchronizer::StartDeviceSynchronization(chip::Controller::DeviceCont
return;
}

mNodeId = nodeId;

#if defined(PW_RPC_ENABLED)
mCurrentDeviceData = chip_rpc_SynchronizedDevice_init_default;
mCurrentDeviceData.node_id = nodeId;
mCurrentDeviceData.has_is_icd = true;
mCurrentDeviceData.is_icd = deviceIsIcd;
#endif

ReturnOnFailure(controller->GetConnectedDevice(nodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback));
mController = controller;
Expand All @@ -212,10 +223,12 @@ void DeviceSynchronizer::GetUniqueId()
VerifyOrDie(mState == State::ReceivedResponse);
VerifyOrDie(mController);

#if defined(PW_RPC_ENABLED)
// If we have a UniqueId we can return leaving state in ReceivedResponse.
VerifyOrReturn(!mCurrentDeviceData.has_unique_id, ChipLogDetail(NotSpecified, "We already have UniqueId"));
#endif

auto * device = DeviceMgr().FindDeviceByNode(mCurrentDeviceData.node_id);
auto * device = DeviceMgr().FindDeviceByNode(mNodeId);
// If there is no associated remote Fabric Sync Aggregator there is no other place for us to try
// getting the UniqueId from and can return leaving the state in ReceivedResponse.
VerifyOrReturn(device, ChipLogDetail(NotSpecified, "No remote Fabric Sync Aggregator to get UniqueId from"));
Expand All @@ -231,8 +244,10 @@ void DeviceSynchronizer::GetUniqueId()
[this](std::optional<CharSpan> aUniqueId) {
if (aUniqueId.has_value())
{
#if defined(PW_RPC_ENABLED)
this->mCurrentDeviceData.has_unique_id = true;
memcpy(this->mCurrentDeviceData.unique_id, aUniqueId.value().data(), aUniqueId.value().size());
#endif
}
else
{
Expand All @@ -255,19 +270,22 @@ void DeviceSynchronizer::GetUniqueId()
void DeviceSynchronizer::SynchronizationCompleteAddDevice()
{
VerifyOrDie(mState == State::ReceivedResponse || mState == State::GettingUid);

#if defined(PW_RPC_ENABLED)
AddSynchronizedDevice(mCurrentDeviceData);
// TODO(#35077) Figure out how we should reflect CADMIN values of ICD.
if (!mCurrentDeviceData.is_icd)
{
VerifyOrDie(mController);
// TODO(#35333) Figure out how we should recover in this circumstance.
CHIP_ERROR err = DeviceSubscriptionManager::Instance().StartSubscription(*mController, mCurrentDeviceData.node_id);
CHIP_ERROR err = DeviceSubscriptionManager::Instance().StartSubscription(*mController, mNodeId);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed start subscription to NodeId:" ChipLogFormatX64,
ChipLogValueX64(mCurrentDeviceData.node_id));
ChipLogError(NotSpecified, "Failed start subscription to NodeId:" ChipLogFormatX64, ChipLogValueX64(mNodeId));
}
}
#endif

MoveToState(State::Idle);
}

Expand Down
7 changes: 6 additions & 1 deletion examples/fabric-admin/device_manager/DeviceSynchronization.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

#include <memory>

#if defined(PW_RPC_ENABLED)
#include "fabric_bridge_service/fabric_bridge_service.pb.h"
#include "fabric_bridge_service/fabric_bridge_service.rpc.pb.h"
#endif

/// Ensures that device data is synchronized to the remote fabric bridge.
///
Expand Down Expand Up @@ -92,6 +94,9 @@ class DeviceSynchronizer : public chip::app::ReadClient::Callback
// mController is expected to remain valid throughout the entire device synchronization process (i.e. when
// mState != Idle).
chip::Controller::DeviceController * mController = nullptr;
chip_rpc_SynchronizedDevice mCurrentDeviceData = chip_rpc_SynchronizedDevice_init_default;
chip::NodeId mNodeId = chip::kUndefinedNodeId;
#if defined(PW_RPC_ENABLED)
chip_rpc_SynchronizedDevice mCurrentDeviceData = chip_rpc_SynchronizedDevice_init_default;
#endif
UniqueIdGetter mUniqueIdGetter;
};

0 comments on commit 2ac9bed

Please sign in to comment.