1616
1717#define RESET_METHOD " $/reset"
1818#define BIND_METHOD " $/register"
19+ #define BRIDGE_TIMEOUT " $/bridgeTimeout"
1920
2021#define UPDATE_THREAD_STACK_SIZE 500
2122#define UPDATE_THREAD_PRIORITY 5
@@ -30,25 +31,28 @@ void updateEntryPoint(void *, void *, void *);
3031
3132class RpcResult {
3233
33- const int FALLBACK_TIMEOUT = 10 ;
34-
3534public:
36- RpcResult ( uint32_t id, RPCClient* c, struct k_mutex * m, int timeout) : msg_id_wait(id), client(c), read_mutex(m), _timeout(timeout) {}
35+ RpcError error;
3736
38- template <typename RType> bool result (RType& result, int timeout = -1 ) {
39- if (_executed) return client->lastError .code == NO_ERR;
37+ RpcResult (uint32_t id, RPCClient* c, struct k_mutex * rm, struct k_mutex * wm, unsigned long timeout) : msg_id_wait(id), client(c), read_mutex(rm), write_mutex(wm), _timeout(timeout) {}
4038
41- if (timeout > 0 ) _timeout = timeout;
39+ template <typename RType> bool result (RType& result) {
40+ if (_executed) return error.code == NO_ERR;
4241
43- int start = millis ();
42+ unsigned long start = millis ();
4443 while (true ) {
4544 if (_timeout > 0 && (millis () - start) > _timeout){
46- client->lastError .code = GENERIC_ERR;
47- client->lastError .traceback = " Timed out" ;
45+ error.code = GENERIC_ERR;
46+ error.traceback = " Timed out" ;
47+ k_mutex_lock (write_mutex, K_FOREVER);
48+ client->notify (BRIDGE_TIMEOUT);
49+ k_mutex_unlock (write_mutex);
4850 break ;
4951 }
5052 if (k_mutex_lock (read_mutex, K_MSEC (10 )) == 0 ) {
5153 if (client->get_response (msg_id_wait, result)) {
54+ error.code = client->lastError .code ;
55+ error.traceback = client->lastError .traceback ;
5256 k_mutex_unlock (read_mutex);
5357 break ;
5458 }
@@ -59,20 +63,21 @@ class RpcResult {
5963 }
6064 }
6165 _executed = true ;
62- return client-> lastError .code == NO_ERR;
66+ return error .code == NO_ERR;
6367 }
6468
6569 operator bool () {
6670 MsgPack::object::nil_t nil;
67- return result (nil, FALLBACK_TIMEOUT );
71+ return result (nil);
6872 }
6973
7074private:
7175 uint32_t msg_id_wait;
7276 RPCClient* client;
7377 bool _executed = false ;
7478 struct k_mutex * read_mutex;
75- int _timeout = -1 ;
79+ struct k_mutex * write_mutex;
80+ unsigned long _timeout = -1 ;
7681};
7782
7883class BridgeClass {
@@ -91,7 +96,7 @@ class BridgeClass {
9196
9297 bool started = false ;
9398
94- int _timeout = -1 ;
99+ unsigned long _timeout = -1 ;
95100
96101public:
97102
@@ -199,7 +204,7 @@ class BridgeClass {
199204 k_yield ();
200205 }
201206 }
202- return RpcResult{msg_id_wait, client, &read_mutex, _timeout};
207+ return RpcResult{msg_id_wait, client, &read_mutex, &write_mutex, _timeout};
203208 }
204209
205210
0 commit comments