Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove global RPC call handler for each RPC call #34338

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions examples/fabric-admin/rpc/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ constexpr uint32_t kDefaultChannelId = 1;

// Fabric Bridge Client
rpc::pw_rpc::nanopb::FabricBridge::Client fabricBridgeClient(rpc::client::GetDefaultRpcClient(), kDefaultChannelId);
pw::rpc::NanopbUnaryReceiver<::pw_protobuf_Empty> addSynchronizedDeviceCall;
pw::rpc::NanopbUnaryReceiver<::pw_protobuf_Empty> removeSynchronizedDeviceCall;

std::mutex responseMutex;
std::condition_variable responseCv;
bool responseReceived = false;
CHIP_ERROR responseError = CHIP_NO_ERROR;

// By passing the `call` parameter into WaitForResponse we are explicitly trying to insure the caller takes into consideration that
// the lifetime of the `call` object when calling WaitForResponse
template <typename CallType>
CHIP_ERROR WaitForResponse(CallType & call)
{
Expand Down Expand Up @@ -117,52 +117,38 @@ CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId)
{
ChipLogProgress(NotSpecified, "AddSynchronizedDevice");

if (addSynchronizedDeviceCall.active())
{
ChipLogError(NotSpecified, "Add Synchronized Device operation is in progress\n");
return CHIP_ERROR_BUSY;
}

chip_rpc_SynchronizedDevice device;
device.node_id = nodeId;

// By assigning the returned call to the global 'addSynchronizedDeviceCall', the RPC
// call is kept alive until it completes. When a response is received, it
// will be logged by the handler function and the call will complete.
addSynchronizedDeviceCall = fabricBridgeClient.AddSynchronizedDevice(device, OnAddDeviceResponseCompleted);
// The RPC call is kept alive until it completes. When a response is received, it will be logged by the handler
// function and the call will complete.
auto call = fabricBridgeClient.AddSynchronizedDevice(device, OnAddDeviceResponseCompleted);

if (!addSynchronizedDeviceCall.active())
if (!call.active())
{
// The RPC call was not sent. This could occur due to, for example, an invalid channel ID. Handle if necessary.
return CHIP_ERROR_INTERNAL;
}

return WaitForResponse(addSynchronizedDeviceCall);
return WaitForResponse(call);
}

CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId)
{
ChipLogProgress(NotSpecified, "RemoveSynchronizedDevice");

if (removeSynchronizedDeviceCall.active())
{
ChipLogError(NotSpecified, "Remove Synchronized Device operation is in progress\n");
return CHIP_ERROR_BUSY;
}

chip_rpc_SynchronizedDevice device;
device.node_id = nodeId;

// By assigning the returned call to the global 'removeSynchronizedDeviceCall', the RPC
// call is kept alive until it completes. When a response is received, it
// will be logged by the handler function and the call will complete.
removeSynchronizedDeviceCall = fabricBridgeClient.RemoveSynchronizedDevice(device, OnRemoveDeviceResponseCompleted);
// The RPC call is kept alive until it completes. When a response is received, it will be logged by the handler
// function and the call will complete.
auto call = fabricBridgeClient.RemoveSynchronizedDevice(device, OnRemoveDeviceResponseCompleted);

if (!removeSynchronizedDeviceCall.active())
if (!call.active())
{
// The RPC call was not sent. This could occur due to, for example, an invalid channel ID. Handle if necessary.
return CHIP_ERROR_INTERNAL;
}

return WaitForResponse(removeSynchronizedDeviceCall);
return WaitForResponse(call);
}
18 changes: 7 additions & 11 deletions examples/fabric-bridge-app/linux/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ constexpr uint32_t kDefaultChannelId = 1;

// Fabric Admin Client
rpc::pw_rpc::nanopb::FabricAdmin::Client fabricAdminClient(rpc::client::GetDefaultRpcClient(), kDefaultChannelId);
pw::rpc::NanopbUnaryReceiver<::chip_rpc_OperationStatus> openCommissioningWindowCall;

std::mutex responseMutex;
std::condition_variable responseCv;
bool responseReceived = false;
CHIP_ERROR responseError = CHIP_NO_ERROR;

// By passing the `call` parameter into WaitForResponse we are explicitly trying to insure the caller takes into consideration that
// the lifetime of the `call` object when calling WaitForResponse
template <typename CallType>
CHIP_ERROR WaitForResponse(CallType & call)
{
Expand Down Expand Up @@ -98,23 +99,18 @@ CHIP_ERROR OpenCommissioningWindow(NodeId nodeId)
{
ChipLogProgress(NotSpecified, "OpenCommissioningWindow with Node Id 0x:" ChipLogFormatX64, ChipLogValueX64(nodeId));

if (openCommissioningWindowCall.active())
{
ChipLogError(NotSpecified, "OpenCommissioningWindow is in progress\n");
return CHIP_ERROR_BUSY;
}

chip_rpc_DeviceInfo device;
device.node_id = nodeId;

// The RPC will remain active as long as `openCommissioningWindowCall` is alive.
openCommissioningWindowCall = fabricAdminClient.OpenCommissioningWindow(device, OnOpenCommissioningWindowCompleted);
// The RPC call is kept alive until it completes. When a response is received, it will be logged by the handler
// function and the call will complete.
auto call = fabricAdminClient.OpenCommissioningWindow(device, OnOpenCommissioningWindowCompleted);

if (!openCommissioningWindowCall.active())
if (!call.active())
{
// The RPC call was not sent. This could occur due to, for example, an invalid channel ID. Handle if necessary.
return CHIP_ERROR_INTERNAL;
}

return WaitForResponse(openCommissioningWindowCall);
return WaitForResponse(call);
}
Loading