Skip to content

Commit c966151

Browse files
committed
Bridge.call: move result retrieval to separate API
New calls will be in the form: Bridge.call("test", 1, 2) -> returns true if the call succeeded, ignore the return Bridge.call("test", 1, 2).result(c) -> stores the result in variable c, returns true if succeeded Bridge.call("test", 1, 2).timeout(20) -> waits 20ms for the result, then returns fals if timeout is reached Bridge.call("test", 1, 2).timeout(20).result(c) -> same but storing the result to variable c Bridge.call("test", 1, 2).result(c, 20) -> same as previous call * Usages that do not held the expected behaviour: Bridge.call("test", 1, 2).result(c).timeout(20) -> doesn't apply the timeout, waits forever
1 parent fe524ff commit c966151

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/bridge.h

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,43 @@
2828

2929
void updateEntryPoint(void *, void *, void *);
3030

31+
class RpcResult {
32+
public:
33+
RpcResult(uint32_t id, RPCClient* c, struct k_mutex* m) : msg_id_wait(id), client(c), read_mutex(m) {}
34+
35+
template<typename RType> bool result(RType& result, int timeout_ms = -1) {
36+
// Lock read mutex
37+
if (_timeout < 0) _timeout = timeout_ms;
38+
int start = millis();
39+
while(true && (_timeout < 0 || (millis() - start) < _timeout)) {
40+
if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) {
41+
if (client->get_response(msg_id_wait, result)) {
42+
k_mutex_unlock(read_mutex);
43+
break;
44+
}
45+
k_mutex_unlock(read_mutex);
46+
k_msleep(1);
47+
} else {
48+
k_yield();
49+
}
50+
}
51+
return (client->lastError.code == NO_ERR) && (_timeout < 0 || (millis() - start) < _timeout);
52+
}
53+
operator bool() {
54+
char c;
55+
return result(c);
56+
}
57+
RpcResult& timeout(int ms) {
58+
_timeout = ms;
59+
return *this;
60+
}
61+
private:
62+
uint32_t msg_id_wait;
63+
RPCClient* client;
64+
struct k_mutex* read_mutex;
65+
int _timeout = -1;
66+
};
67+
3168
class BridgeClass {
3269

3370
RPCClient* client = nullptr;
@@ -131,8 +168,8 @@ class BridgeClass {
131168

132169
}
133170

134-
template<typename RType, typename... Args>
135-
bool call(const MsgPack::str_t& method, RType& result, Args&&... args) {
171+
template<typename... Args>
172+
RpcResult call(const MsgPack::str_t& method, Args&&... args) {
136173

137174
uint32_t msg_id_wait;
138175

@@ -146,25 +183,10 @@ class BridgeClass {
146183
k_yield();
147184
}
148185
}
186+
return RpcResult{msg_id_wait, client, &read_mutex};
187+
}
149188

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-
}
164-
165-
return (client->lastError.code == NO_ERR);
166189

167-
}
168190

169191
template<typename... Args>
170192
void notify(const MsgPack::str_t method, Args&&... args) {

0 commit comments

Comments
 (0)