Skip to content

Commit 2b98fa3

Browse files
committed
RPC: gracefully handle timeouts
1 parent aa154fc commit 2b98fa3

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

libraries/RPC/src/RPC.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ int RPCClass::begin(long unsigned int np, uint16_t nd) {
149149
// Ideally this should execute only once
150150
disableCM4Autoboot();
151151

152-
eventThread = new rtos::Thread(osPriorityHigh);
152+
eventThread = new rtos::Thread(osPriorityHigh, 4096);
153153
eventThread->start(&eventHandler);
154154

155-
dispatcherThread = new rtos::Thread(osPriorityNormal);
155+
dispatcherThread = new rtos::Thread(osPriorityNormal, 4096);
156156
dispatcherThread->start(mbed::callback(this, &RPCClass::dispatch));
157157

158-
responseThread = new rtos::Thread(osPriorityNormal);
158+
responseThread = new rtos::Thread(osPriorityNormal, 4096);
159159
responseThread->start(mbed::callback(this, &RPCClass::response));
160160

161161
/* Initialize OpenAmp and libmetal libraries */
@@ -196,13 +196,13 @@ int RPCClass::begin(long unsigned int np, uint16_t nd) {
196196

197197
int RPCClass::begin(long unsigned int np, uint16_t nd) {
198198

199-
eventThread = new rtos::Thread(osPriorityHigh);
199+
eventThread = new rtos::Thread(osPriorityHigh, 4096);
200200
eventThread->start(&eventHandler);
201201

202-
dispatcherThread = new rtos::Thread(osPriorityNormal);
202+
dispatcherThread = new rtos::Thread(osPriorityNormal, 4096);
203203
dispatcherThread->start(mbed::callback(this, &RPCClass::dispatch));
204204

205-
responseThread = new rtos::Thread(osPriorityNormal);
205+
responseThread = new rtos::Thread(osPriorityNormal, 4096);
206206
responseThread->start(mbed::callback(this, &RPCClass::response));
207207

208208
/* Initialize OpenAmp and libmetal libraries */

libraries/RPC/src/RPC.h

+18
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ class RPCClass : public Stream, public rpc::detail::dispatcher {
8181
delete client;
8282
}
8383

84+
void setTimeout(uint32_t milliseconds) {
85+
_timeout = milliseconds;
86+
}
87+
8488
template <typename... Args>
8589
RPCLIB_MSGPACK::object_handle call(std::string const &func_name,
8690
Args... args) {
@@ -98,8 +102,15 @@ class RPCClass : public Stream, public rpc::detail::dispatcher {
98102
}
99103
mtx.unlock();
100104

105+
clients[i]->setTimeout(_timeout);
106+
has_timed_out = false;
107+
101108
// thread start and client .call
102109
clients[i]->call(func_name, args...);
110+
111+
if (clients[i]->timedOut()) {
112+
has_timed_out = true;
113+
}
103114
RPCLIB_MSGPACK::object_handle ret = std::move(clients[i]->result);
104115

105116
mtx.lock();
@@ -111,6 +122,10 @@ class RPCClass : public Stream, public rpc::detail::dispatcher {
111122

112123
rpc::client* clients[10];
113124

125+
bool timedOut() {
126+
return has_timed_out;
127+
}
128+
114129
private:
115130
RingBufferN<256> rx_buffer;
116131
bool initialized = false;
@@ -133,6 +148,9 @@ class RPCClass : public Stream, public rpc::detail::dispatcher {
133148

134149
mbed::Callback<void()> _rx;
135150

151+
uint32_t _timeout = osWaitForever;
152+
bool has_timed_out = false;
153+
136154
//rpc::detail::response response;
137155
RPCLIB_MSGPACK::object_handle call_result;
138156

libraries/RPC/src/RPC_client.h

+16-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ class client {
2828

2929
send_msgpack(buffer);
3030

31-
osSignalWait(0, osWaitForever);
32-
33-
//getResult(result);
34-
31+
auto e = osSignalWait(0, timeout);
3532
delete buffer;
3633

34+
if (e.status != osEventSignal) {
35+
has_timed_out = true;
36+
}
37+
3738
RPCLIB_MSGPACK::object_handle q = std::move(result);
3839
return q;
3940
}
@@ -62,11 +63,22 @@ class client {
6263
delete buffer;
6364
}
6465

66+
void setTimeout(uint32_t milliseconds) {
67+
timeout = milliseconds;
68+
}
69+
70+
bool timedOut() {
71+
return has_timed_out;
72+
}
73+
6574
protected:
6675
osThreadId callThreadId;
6776
friend class arduino::RPCClass;
6877
RPCLIB_MSGPACK::object_handle result;
6978

79+
uint32_t timeout = osWaitForever;
80+
bool has_timed_out = false;
81+
7082
private:
7183
enum class request_type { call = 0, notification = 2 };;
7284

0 commit comments

Comments
 (0)