1616
1717#define RESET_METHOD " $/reset"
1818#define BIND_METHOD " $/register"
19+ // #define BRIDGE_ERROR "$/bridgeLog"
1920
2021#define UPDATE_THREAD_STACK_SIZE 500
2122#define UPDATE_THREAD_PRIORITY 5
2829
2930void updateEntryPoint (void *, void *, void *);
3031
32+ class RpcResult {
33+
34+ public:
35+ RpcError error;
36+
37+ RpcResult (uint32_t id, RPCClient* c, struct k_mutex * rm, struct k_mutex * wm) : msg_id_wait(id), client(c), read_mutex(rm), write_mutex(wm) {}
38+
39+ template <typename RType> bool result (RType& result) {
40+ if (_executed) return error.code == NO_ERR;
41+
42+ while (true ) {
43+ if (k_mutex_lock (read_mutex, K_MSEC (10 )) == 0 ) {
44+ if (client->get_response (msg_id_wait, result, error)) {
45+ k_mutex_unlock (read_mutex);
46+ // if (error.code == PARSING_ERR) {
47+ // k_mutex_lock(write_mutex, K_FOREVER);
48+ // client->notify(BRIDGE_ERROR, error.traceback);
49+ // k_mutex_unlock(write_mutex);
50+ // }
51+ break ;
52+ }
53+ k_mutex_unlock (read_mutex);
54+ k_msleep (1 );
55+ } else {
56+ k_yield ();
57+ }
58+ }
59+ _executed = true ;
60+ return error.code == NO_ERR;
61+ }
62+
63+ bool result () {
64+ MsgPack::object::nil_t nil;
65+ return result (nil);
66+ }
67+
68+ ~RpcResult (){
69+ result ();
70+ }
71+
72+ operator bool () {
73+ return result ();
74+ }
75+
76+ private:
77+ uint32_t msg_id_wait;
78+ RPCClient* client;
79+ bool _executed = false ;
80+ struct k_mutex * read_mutex;
81+ struct k_mutex * write_mutex;
82+ };
83+
3184class BridgeClass {
3285
3386 RPCClient* client = nullptr ;
@@ -73,7 +126,7 @@ class BridgeClass {
73126 UPDATE_THREAD_PRIORITY, 0 , K_NO_WAIT);
74127
75128 bool res;
76- call (RESET_METHOD, res);
129+ call (RESET_METHOD). result ( res);
77130 if (res) {
78131 started = true ;
79132 }
@@ -83,7 +136,7 @@ class BridgeClass {
83136 template <typename F>
84137 bool provide (const MsgPack::str_t & name, F&& func) {
85138 bool res;
86- if (!call (BIND_METHOD, res, name)) {
139+ if (!call (BIND_METHOD, name). result (res )) {
87140 return false ;
88141 }
89142 return server->bind (name, func);
@@ -92,7 +145,7 @@ class BridgeClass {
92145 template <typename F>
93146 bool provide_safe (const MsgPack::str_t & name, F&& func) {
94147 bool res;
95- if (!call (BIND_METHOD, res, name)) {
148+ if (!call (BIND_METHOD, name). result (res )) {
96149 return false ;
97150 }
98151
@@ -131,8 +184,8 @@ class BridgeClass {
131184
132185 }
133186
134- template <typename RType, typename ... Args>
135- bool call (const MsgPack::str_t & method, RType& result , Args&&... args) {
187+ template <typename ... Args>
188+ RpcResult call (const MsgPack::str_t & method, Args&&... args) {
136189
137190 uint32_t msg_id_wait;
138191
@@ -145,26 +198,11 @@ class BridgeClass {
145198 } else {
146199 k_yield ();
147200 }
148- }
149-
150- // Lock read mutex
151- while (true ) {
152- if (k_mutex_lock (&read_mutex, K_MSEC (10 )) == 0 ) {
153- if (client->get_response (msg_id_wait, result)) {
154- k_mutex_unlock (&read_mutex);
155- break ;
156- }
157- k_mutex_unlock (&read_mutex);
158- k_msleep (1 );
159- } else {
160- k_yield ();
161- }
162-
163- }
201+ }
202+ return RpcResult{msg_id_wait, client, &read_mutex, &write_mutex};
203+ }
164204
165- return (client->lastError .code == NO_ERR);
166205
167- }
168206
169207 template <typename ... Args>
170208 void notify (const MsgPack::str_t method, Args&&... args) {
0 commit comments