19
19
#include " RpcClient.h"
20
20
#include " RpcClientProcessor.h"
21
21
22
+ #include < chrono>
23
+ #include < condition_variable>
24
+ #include < mutex>
22
25
#include < string>
23
26
#include < thread>
24
- #include < unistd.h>
25
27
26
28
#include " fabric_bridge_service/fabric_bridge_service.rpc.pb.h"
27
29
#include " pw_assert/check.h"
@@ -36,16 +38,45 @@ using namespace chip;
36
38
namespace {
37
39
38
40
// Constants
41
+ constexpr uint32_t kRpcTimeoutMs = 1000 ;
39
42
constexpr uint32_t kDefaultChannelId = 1 ;
40
43
41
44
// Fabric Bridge Client
42
45
rpc::pw_rpc::nanopb::FabricBridge::Client fabricBridgeClient (rpc::client::GetDefaultRpcClient(), kDefaultChannelId);
43
46
pw::rpc::NanopbUnaryReceiver<::pw_protobuf_Empty> addSynchronizedDeviceCall;
44
47
pw::rpc::NanopbUnaryReceiver<::pw_protobuf_Empty> removeSynchronizedDeviceCall;
45
48
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
+
46
72
// Callback function to be called when the RPC response is received
47
73
void OnAddDeviceResponseCompleted (const pw_protobuf_Empty & response, pw::Status status)
48
74
{
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
+
49
80
if (status.ok ())
50
81
{
51
82
ChipLogProgress (NotSpecified, " AddSynchronizedDevice RPC call succeeded!" );
@@ -59,6 +90,11 @@ void OnAddDeviceResponseCompleted(const pw_protobuf_Empty & response, pw::Status
59
90
// Callback function to be called when the RPC response is received
60
91
void OnRemoveDeviceResponseCompleted (const pw_protobuf_Empty & response, pw::Status status)
61
92
{
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
+
62
98
if (status.ok ())
63
99
{
64
100
ChipLogProgress (NotSpecified, " RemoveSynchronizedDevice RPC call succeeded!" );
@@ -101,7 +137,7 @@ CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId)
101
137
return CHIP_ERROR_INTERNAL;
102
138
}
103
139
104
- return CHIP_NO_ERROR ;
140
+ return WaitForResponse (addSynchronizedDeviceCall) ;
105
141
}
106
142
107
143
CHIP_ERROR RemoveSynchronizedDevice (chip::NodeId nodeId)
@@ -128,5 +164,5 @@ CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId)
128
164
return CHIP_ERROR_INTERNAL;
129
165
}
130
166
131
- return CHIP_NO_ERROR ;
167
+ return WaitForResponse (removeSynchronizedDeviceCall) ;
132
168
}
0 commit comments