Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8951403

Browse files
authoredJul 12, 2024··
Merge branch 'master' into feature/app-install-flow-public
2 parents 94dbf31 + a5d8f62 commit 8951403

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed
 

‎examples/fabric-admin/rpc/RpcClient.cpp

+39-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
#include "RpcClient.h"
2020
#include "RpcClientProcessor.h"
2121

22+
#include <chrono>
23+
#include <condition_variable>
24+
#include <mutex>
2225
#include <string>
2326
#include <thread>
24-
#include <unistd.h>
2527

2628
#include "fabric_bridge_service/fabric_bridge_service.rpc.pb.h"
2729
#include "pw_assert/check.h"
@@ -36,16 +38,45 @@ using namespace chip;
3638
namespace {
3739

3840
// Constants
41+
constexpr uint32_t kRpcTimeoutMs = 1000;
3942
constexpr uint32_t kDefaultChannelId = 1;
4043

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

49+
std::mutex responseMutex;
50+
std::condition_variable responseCv;
51+
bool responseReceived = false;
52+
CHIP_ERROR responseError = CHIP_NO_ERROR;
53+
54+
template <typename CallType>
55+
CHIP_ERROR WaitForResponse(CallType & call)
56+
{
57+
std::unique_lock<std::mutex> lock(responseMutex);
58+
responseReceived = false;
59+
responseError = CHIP_NO_ERROR;
60+
61+
if (responseCv.wait_for(lock, std::chrono::milliseconds(kRpcTimeoutMs), [] { return responseReceived; }))
62+
{
63+
return responseError;
64+
}
65+
else
66+
{
67+
fprintf(stderr, "RPC Response timed out!");
68+
return CHIP_ERROR_TIMEOUT;
69+
}
70+
}
71+
4672
// Callback function to be called when the RPC response is received
4773
void OnAddDeviceResponseCompleted(const pw_protobuf_Empty & response, pw::Status status)
4874
{
75+
std::lock_guard<std::mutex> lock(responseMutex);
76+
responseReceived = true;
77+
responseError = status.ok() ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
78+
responseCv.notify_one();
79+
4980
if (status.ok())
5081
{
5182
ChipLogProgress(NotSpecified, "AddSynchronizedDevice RPC call succeeded!");
@@ -59,6 +90,11 @@ void OnAddDeviceResponseCompleted(const pw_protobuf_Empty & response, pw::Status
5990
// Callback function to be called when the RPC response is received
6091
void OnRemoveDeviceResponseCompleted(const pw_protobuf_Empty & response, pw::Status status)
6192
{
93+
std::lock_guard<std::mutex> lock(responseMutex);
94+
responseReceived = true;
95+
responseError = status.ok() ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
96+
responseCv.notify_one();
97+
6298
if (status.ok())
6399
{
64100
ChipLogProgress(NotSpecified, "RemoveSynchronizedDevice RPC call succeeded!");
@@ -101,7 +137,7 @@ CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId)
101137
return CHIP_ERROR_INTERNAL;
102138
}
103139

104-
return CHIP_NO_ERROR;
140+
return WaitForResponse(addSynchronizedDeviceCall);
105141
}
106142

107143
CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId)
@@ -128,5 +164,5 @@ CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId)
128164
return CHIP_ERROR_INTERNAL;
129165
}
130166

131-
return CHIP_NO_ERROR;
167+
return WaitForResponse(removeSynchronizedDeviceCall);
132168
}

‎examples/fabric-admin/rpc/RpcClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CHIP_ERROR InitRpcClient(uint16_t rpcServerPort);
4141
*
4242
* @param nodeId The Node ID of the device to be added.
4343
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
44-
* - CHIP_NO_ERROR: The RPC command was successfully sent.
44+
* - CHIP_NO_ERROR: The RPC command was successfully processed.
4545
* - CHIP_ERROR_BUSY: Another operation is currently in progress.
4646
* - CHIP_ERROR_INTERNAL: An internal error occurred while activating the RPC call.
4747
*/
@@ -56,7 +56,7 @@ CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId);
5656
*
5757
* @param nodeId The Node ID of the device to be removed.
5858
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
59-
* - CHIP_NO_ERROR: The RPC command was successfully sent.
59+
* - CHIP_NO_ERROR: The RPC command was successfully processed.
6060
* - CHIP_ERROR_BUSY: Another operation is currently in progress.
6161
* - CHIP_ERROR_INTERNAL: An internal error occurred while activating the RPC call.
6262
*/

‎examples/fabric-bridge-app/linux/RpcClient.cpp

+34-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
#include "RpcClient.h"
2020
#include "RpcClientProcessor.h"
2121

22+
#include <chrono>
23+
#include <condition_variable>
24+
#include <mutex>
2225
#include <string>
2326
#include <thread>
24-
#include <unistd.h>
2527

2628
#include "fabric_admin_service/fabric_admin_service.rpc.pb.h"
2729
#include "pw_assert/check.h"
@@ -36,15 +38,44 @@ using namespace chip;
3638
namespace {
3739

3840
// Constants
41+
constexpr uint32_t kRpcTimeoutMs = 1000;
3942
constexpr uint32_t kDefaultChannelId = 1;
4043

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

48+
std::mutex responseMutex;
49+
std::condition_variable responseCv;
50+
bool responseReceived = false;
51+
CHIP_ERROR responseError = CHIP_NO_ERROR;
52+
53+
template <typename CallType>
54+
CHIP_ERROR WaitForResponse(CallType & call)
55+
{
56+
std::unique_lock<std::mutex> lock(responseMutex);
57+
responseReceived = false;
58+
responseError = CHIP_NO_ERROR;
59+
60+
if (responseCv.wait_for(lock, std::chrono::milliseconds(kRpcTimeoutMs), [] { return responseReceived; }))
61+
{
62+
return responseError;
63+
}
64+
else
65+
{
66+
ChipLogError(NotSpecified, "RPC Response timed out!");
67+
return CHIP_ERROR_TIMEOUT;
68+
}
69+
}
70+
4571
// Callback function to be called when the RPC response is received
4672
void OnOpenCommissioningWindowCompleted(const chip_rpc_OperationStatus & response, pw::Status status)
4773
{
74+
std::lock_guard<std::mutex> lock(responseMutex);
75+
responseReceived = true;
76+
responseError = status.ok() ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
77+
responseCv.notify_one();
78+
4879
if (status.ok())
4980
{
5081
ChipLogProgress(NotSpecified, "OpenCommissioningWindow received operation status: %d", response.success);
@@ -81,8 +112,9 @@ CHIP_ERROR OpenCommissioningWindow(NodeId nodeId)
81112

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

87-
return CHIP_NO_ERROR;
119+
return WaitForResponse(openCommissioningWindowCall);
88120
}

‎examples/fabric-bridge-app/linux/include/RpcClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ CHIP_ERROR InitRpcClient(uint16_t rpcServerPort);
3737
*
3838
* @param nodeId The identifier of the node for which the commissioning window should be opened.
3939
* @return CHIP_ERROR An error code indicating the success or failure of the operation.
40-
* - CHIP_NO_ERROR: The RPC command was successfully sent.
40+
* - CHIP_NO_ERROR: The RPC command was successfully processed.
4141
* - CHIP_ERROR_BUSY: Another commissioning window is currently in progress.
4242
* - CHIP_ERROR_INTERNAL: An internal error occurred.
4343
*/

0 commit comments

Comments
 (0)
Please sign in to comment.