Skip to content

Commit

Permalink
Make RPC call synchronous in Fabric Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Jul 4, 2024
1 parent 5e37260 commit 8b7f10d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
27 changes: 25 additions & 2 deletions examples/fabric-admin/rpc/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,36 @@ using namespace chip;
namespace {

// Constants
constexpr uint32_t kRpcTimeoutMs = 1000;
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;

template <typename CallType>
CHIP_ERROR WaitForResponse(CallType & call)
{
// Wait for the response or timeout
uint32_t elapsedTimeMs = 0;
const uint32_t sleepTimeMs = 100;

while (call.active() && elapsedTimeMs < kRpcTimeoutMs)
{
usleep(sleepTimeMs * 1000);
elapsedTimeMs += sleepTimeMs;
}

if (elapsedTimeMs >= kRpcTimeoutMs)
{
fprintf(stderr, "RPC Response timed out!");
return CHIP_ERROR_TIMEOUT;
}

return CHIP_NO_ERROR;
}

// Callback function to be called when the RPC response is received
void OnAddDeviceResponseCompleted(const pw_protobuf_Empty & response, pw::Status status)
{
Expand Down Expand Up @@ -101,7 +124,7 @@ CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId)
return CHIP_ERROR_INTERNAL;
}

return CHIP_NO_ERROR;
return WaitForResponse(addSynchronizedDeviceCall);
}

CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId)
Expand All @@ -128,5 +151,5 @@ CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId)
return CHIP_ERROR_INTERNAL;
}

return CHIP_NO_ERROR;
return WaitForResponse(removeSynchronizedDeviceCall);
}
4 changes: 2 additions & 2 deletions examples/fabric-admin/rpc/RpcClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CHIP_ERROR InitRpcClient(uint16_t rpcServerPort);
*
* @param nodeId The Node ID of the device to be added.
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
* - CHIP_NO_ERROR: The RPC command was successfully sent.
* - CHIP_NO_ERROR: The RPC command was successfully processed.
* - CHIP_ERROR_BUSY: Another operation is currently in progress.
* - CHIP_ERROR_INTERNAL: An internal error occurred while activating the RPC call.
*/
Expand All @@ -56,7 +56,7 @@ CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId);
*
* @param nodeId The Node ID of the device to be removed.
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
* - CHIP_NO_ERROR: The RPC command was successfully sent.
* - CHIP_NO_ERROR: The RPC command was successfully processed.
* - CHIP_ERROR_BUSY: Another operation is currently in progress.
* - CHIP_ERROR_INTERNAL: An internal error occurred while activating the RPC call.
*/
Expand Down
26 changes: 25 additions & 1 deletion examples/fabric-bridge-app/linux/RpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,35 @@ using namespace chip;
namespace {

// Constants
constexpr uint32_t kRpcTimeoutMs = 1000;
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;

template <typename CallType>
CHIP_ERROR WaitForResponse(CallType & call)
{
// Wait for the response or timeout
uint32_t elapsedTimeMs = 0;
const uint32_t sleepTimeMs = 100;

while (call.active() && elapsedTimeMs < kRpcTimeoutMs)
{
usleep(sleepTimeMs * 1000);
elapsedTimeMs += sleepTimeMs;
}

if (elapsedTimeMs >= kRpcTimeoutMs)
{
ChipLogError(NotSpecified, "RPC Response timed out!");
return CHIP_ERROR_TIMEOUT;
}

return CHIP_NO_ERROR;
}

// Callback function to be called when the RPC response is received
void OnOpenCommissioningWindowCompleted(const chip_rpc_OperationStatus & response, pw::Status status)
{
Expand Down Expand Up @@ -81,8 +104,9 @@ CHIP_ERROR OpenCommissioningWindow(NodeId nodeId)

if (!openCommissioningWindowCall.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 CHIP_NO_ERROR;
return WaitForResponse(openCommissioningWindowCall);
}
2 changes: 1 addition & 1 deletion examples/fabric-bridge-app/linux/include/RpcClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ CHIP_ERROR InitRpcClient(uint16_t rpcServerPort);
*
* @param nodeId The identifier of the node for which the commissioning window should be opened.
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
* - CHIP_NO_ERROR: The RPC command was successfully sent.
* - CHIP_NO_ERROR: The RPC command was successfully processed.
* - CHIP_ERROR_BUSY: Another commissioning window is currently in progress.
* - CHIP_ERROR_INTERNAL: An internal error occurred.
*/
Expand Down

0 comments on commit 8b7f10d

Please sign in to comment.