From c966151255e337952c03a9d9580fbc4519860233 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 24 Sep 2025 09:40:03 +0200 Subject: [PATCH 01/14] 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 --- src/bridge.h | 60 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/src/bridge.h b/src/bridge.h index dc2c2b6..cead08c 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -28,6 +28,43 @@ void updateEntryPoint(void *, void *, void *); +class RpcResult { +public: + RpcResult(uint32_t id, RPCClient* c, struct k_mutex* m) : msg_id_wait(id), client(c), read_mutex(m) {} + + template bool result(RType& result, int timeout_ms = -1) { + // Lock read mutex + if (_timeout < 0) _timeout = timeout_ms; + int start = millis(); + while(true && (_timeout < 0 || (millis() - start) < _timeout)) { + if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) { + if (client->get_response(msg_id_wait, result)) { + k_mutex_unlock(read_mutex); + break; + } + k_mutex_unlock(read_mutex); + k_msleep(1); + } else { + k_yield(); + } + } + return (client->lastError.code == NO_ERR) && (_timeout < 0 || (millis() - start) < _timeout); + } + operator bool() { + char c; + return result(c); + } + RpcResult& timeout(int ms) { + _timeout = ms; + return *this; + } +private: + uint32_t msg_id_wait; + RPCClient* client; + struct k_mutex* read_mutex; + int _timeout = -1; +}; + class BridgeClass { RPCClient* client = nullptr; @@ -131,8 +168,8 @@ class BridgeClass { } - template - bool call(const MsgPack::str_t& method, RType& result, Args&&... args) { + template + RpcResult call(const MsgPack::str_t& method, Args&&... args) { uint32_t msg_id_wait; @@ -146,25 +183,10 @@ class BridgeClass { k_yield(); } } + return RpcResult{msg_id_wait, client, &read_mutex}; + } - // Lock read mutex - while(true) { - if (k_mutex_lock(&read_mutex, K_MSEC(10)) == 0 ) { - if (client->get_response(msg_id_wait, result)) { - k_mutex_unlock(&read_mutex); - break; - } - k_mutex_unlock(&read_mutex); - k_msleep(1); - } else { - k_yield(); - } - - } - - return (client->lastError.code == NO_ERR); - } template void notify(const MsgPack::str_t method, Args&&... args) { From 7932a8181543445277c56d3eae6ae85b30576718 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 24 Sep 2025 10:56:36 +0200 Subject: [PATCH 02/14] Change all Bridge.call in library --- README.md | 2 +- examples/simple_bridge/simple_bridge.ino | 2 +- src/bridge.h | 6 +++--- src/monitor.h | 8 ++++---- src/tcp_client.h | 10 +++++----- src/tcp_server.h | 6 +++--- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8eed4d7..e877494 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ void setup() { void loop() { float res; - if (!Bridge.call("multiply", res, 1.0, 2.0)) { + if (!Bridge.call("multiply", 1.0, 2.0).result(res)) { Serial.println("Error calling method: multiply"); Serial.println(Bridge.get_error_code()); Serial.println(Bridge.get_error_message()); diff --git a/examples/simple_bridge/simple_bridge.ino b/examples/simple_bridge/simple_bridge.ino index 6a1eccb..18dee49 100644 --- a/examples/simple_bridge/simple_bridge.ino +++ b/examples/simple_bridge/simple_bridge.ino @@ -47,7 +47,7 @@ void setup() { void loop() { float res; - if (!Bridge.call("multiply", res, 1.0, 2.0)) { + if (!Bridge.call("multiply", 1.0, 2.0).result(res)) { Serial.println("Error calling method: multiply"); Serial.println(Bridge.get_error_code()); Serial.println(Bridge.get_error_message()); diff --git a/src/bridge.h b/src/bridge.h index cead08c..97d84a4 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -110,7 +110,7 @@ class BridgeClass { UPDATE_THREAD_PRIORITY, 0, K_NO_WAIT); bool res; - call(RESET_METHOD, res); + call(RESET_METHOD).result(res); if (res) { started = true; } @@ -120,7 +120,7 @@ class BridgeClass { template bool provide(const MsgPack::str_t& name, F&& func) { bool res; - if (!call(BIND_METHOD, res, name)) { + if (!call(BIND_METHOD, name).result(res)) { return false; } return server->bind(name, func); @@ -129,7 +129,7 @@ class BridgeClass { template bool provide_safe(const MsgPack::str_t& name, F&& func) { bool res; - if (!call(BIND_METHOD, res, name)) { + if (!call(BIND_METHOD, name).result(res)) { return false; } diff --git a/src/monitor.h b/src/monitor.h index 99154d0..d091b04 100644 --- a/src/monitor.h +++ b/src/monitor.h @@ -44,7 +44,7 @@ class BridgeMonitor: public Stream { if (!bridge_started) { bridge_started = bridge->begin(); } - return bridge_started && bridge->call(MON_CONNECTED_METHOD, is_connected); + return bridge_started && bridge->call(MON_CONNECTED_METHOD).result(is_connected); } explicit operator bool() const { @@ -99,7 +99,7 @@ class BridgeMonitor: public Stream { } size_t written; - const bool ret = bridge->call(MON_WRITE_METHOD, written, send_buffer); + const bool ret = bridge->call(MON_WRITE_METHOD, send_buffer).result(written); if (ret) { return written; } @@ -109,7 +109,7 @@ class BridgeMonitor: public Stream { bool reset() { bool res; - bool ok = bridge->call(MON_RESET_METHOD, res); + bool ok = bridge->call(MON_RESET_METHOD).result(res); if (ok && res) { is_connected = false; } @@ -124,7 +124,7 @@ class BridgeMonitor: public Stream { k_mutex_lock(&monitor_mutex, K_FOREVER); MsgPack::arr_t message; - bool ret = bridge->call(MON_READ_METHOD, message, size); + bool ret = bridge->call(MON_READ_METHOD, size).result(message); if (ret) { for (size_t i = 0; i < message.size(); ++i) { diff --git a/src/tcp_client.h b/src/tcp_client.h index 8874314..62da5e7 100644 --- a/src/tcp_client.h +++ b/src/tcp_client.h @@ -61,7 +61,7 @@ class BridgeTCPClient : public Client { k_mutex_lock(&client_mutex, K_FOREVER); - const bool resp = bridge->call(TCP_CONNECT_METHOD, connection_id, hostname, port); + const bool resp = bridge->call(TCP_CONNECT_METHOD, hostname, port).result(connection_id); if (!resp) { _connected = false; @@ -84,7 +84,7 @@ class BridgeTCPClient : public Client { k_mutex_lock(&client_mutex, K_FOREVER); - const bool resp = bridge->call(TCP_CONNECT_SSL_METHOD, connection_id, hostname, port, ca_cert_str); + const bool resp = bridge->call(TCP_CONNECT_SSL_METHOD, hostname, port, ca_cert_str).result(connection_id); if (!resp) { _connected = false; @@ -116,7 +116,7 @@ class BridgeTCPClient : public Client { } size_t written; - const bool ret = bridge->call(TCP_WRITE_METHOD, written, connection_id, payload); + const bool ret = bridge->call(TCP_WRITE_METHOD, connection_id, payload).result(written); if (ret) { return written; } @@ -170,7 +170,7 @@ class BridgeTCPClient : public Client { void stop() override { k_mutex_lock(&client_mutex, K_FOREVER); String msg; - const bool resp = bridge->call(TCP_CLOSE_METHOD, msg, connection_id); + const bool resp = bridge->call(TCP_CLOSE_METHOD, connection_id).result(msg); if (resp) { _connected = false; } @@ -198,7 +198,7 @@ class BridgeTCPClient : public Client { k_mutex_lock(&client_mutex, K_FOREVER); MsgPack::arr_t message; - const bool ret = bridge->call(TCP_READ_METHOD, message, connection_id, size); + const bool ret = bridge->call(TCP_READ_METHOD, connection_id, size).result(message); if (ret) { for (size_t i = 0; i < message.size(); ++i) { diff --git a/src/tcp_server.h b/src/tcp_server.h index ad4c0a5..f62e57d 100644 --- a/src/tcp_server.h +++ b/src/tcp_server.h @@ -52,7 +52,7 @@ class BridgeTCPServer final: public Server { k_mutex_lock(&server_mutex, K_FOREVER); String hostname = _addr.toString(); - _listening = bridge->call(TCP_LISTEN_METHOD, listener_id, hostname, _port); + _listening = bridge->call(TCP_LISTEN_METHOD, hostname, _port).result(listener_id); k_mutex_unlock(&server_mutex); @@ -70,7 +70,7 @@ class BridgeTCPServer final: public Server { k_mutex_lock(&server_mutex, K_FOREVER); - const bool ret = bridge->call(TCP_ACCEPT_METHOD, connection_id, listener_id); + const bool ret = bridge->call(TCP_ACCEPT_METHOD, listener_id).result(connection_id); k_mutex_unlock(&server_mutex); @@ -104,7 +104,7 @@ class BridgeTCPServer final: public Server { k_mutex_lock(&server_mutex, K_FOREVER); String msg; - const bool ret = bridge->call(TCP_CLOSE_LISTENER_METHOD, msg, listener_id); + const bool ret = bridge->call(TCP_CLOSE_LISTENER_METHOD, listener_id).result(msg); if (ret) { _listening = false; From 31ddb455d642e3d47c4247e0480851dde3438283 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 24 Sep 2025 15:05:37 +0200 Subject: [PATCH 03/14] feat: on timeout bridge.client has appropriate error code fix: RpcResult overloaded bool using a generic return type, not setting a timeout, can potentially execute result method multiple times --- src/bridge.h | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/bridge.h b/src/bridge.h index 97d84a4..84af31f 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -29,14 +29,21 @@ void updateEntryPoint(void *, void *, void *); class RpcResult { + + const int FALLBACK_TIMEOUT = 10; + public: - RpcResult(uint32_t id, RPCClient* c, struct k_mutex* m) : msg_id_wait(id), client(c), read_mutex(m) {} + RpcResult(uint32_t id, RPCClient* c, struct k_mutex* m, int timeout) : msg_id_wait(id), client(c), read_mutex(m), _timeout(timeout) {} + + template bool result(RType& result) { - template bool result(RType& result, int timeout_ms = -1) { - // Lock read mutex - if (_timeout < 0) _timeout = timeout_ms; int start = millis(); - while(true && (_timeout < 0 || (millis() - start) < _timeout)) { + while(true) { + if (_timeout > 0 && (millis() - start) > _timeout){ + client->lastError.code = GENERIC_ERR; + client->lastError.message = "Timed out"; + break; + } if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) { if (client->get_response(msg_id_wait, result)) { k_mutex_unlock(read_mutex); @@ -48,19 +55,20 @@ class RpcResult { k_yield(); } } - return (client->lastError.code == NO_ERR) && (_timeout < 0 || (millis() - start) < _timeout); + _executed = true; + return client->lastError.code == NO_ERR; } + operator bool() { - char c; - return result(c); - } - RpcResult& timeout(int ms) { - _timeout = ms; - return *this; + if (_executed) return client->lastError.code == NO_ERR; + MsgPack::object::nil_t nil; + return result(nil, FALLBACK_TIMEOUT); } + private: uint32_t msg_id_wait; RPCClient* client; + bool _executed = false; struct k_mutex* read_mutex; int _timeout = -1; }; @@ -81,6 +89,8 @@ class BridgeClass { bool started = false; + int _timeout = -1; + public: explicit BridgeClass(HardwareSerial& serial) { @@ -91,6 +101,10 @@ class BridgeClass { return started; } + void setTimeout(int t) { + _timeout = t; + } + // Initialize the bridge bool begin(unsigned long baud=DEFAULT_SERIAL_BAUD) { serial_ptr->begin(baud); @@ -183,7 +197,7 @@ class BridgeClass { k_yield(); } } - return RpcResult{msg_id_wait, client, &read_mutex}; + return RpcResult{msg_id_wait, client, &read_mutex, _timeout}; } From bcfb56ef99edb596d6b1c07506238243ca83cdc1 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 24 Sep 2025 15:18:34 +0200 Subject: [PATCH 04/14] examples: simple_bridge.ino showcases Bridge callback with async-like result retrieval --- examples/simple_bridge/simple_bridge.ino | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/simple_bridge/simple_bridge.ino b/examples/simple_bridge/simple_bridge.ino index 18dee49..face5cc 100644 --- a/examples/simple_bridge/simple_bridge.ino +++ b/examples/simple_bridge/simple_bridge.ino @@ -53,5 +53,17 @@ void loop() { Serial.println(Bridge.get_error_message()); }; + // Call with deferred response check + RpcResult outcome = Bridge.call("multiply", 5.0, 7.0); + Serial.println("RPC called"); + delay(10); + if (outcome.result(res)) { + Serial.print("Result of the operation is: "); + Serial.println(res); + } else { + Serial.println(Bridge.get_error_code()); + Serial.println(Bridge.get_error_message()); + } + Bridge.notify("signal", 200); } From eab384e4fbd5871aceebdf7623e0a944cc015a90 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 24 Sep 2025 15:47:05 +0200 Subject: [PATCH 05/14] fix: lastError has traceback field. RpcResult.result must accept a timeout param --- src/bridge.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bridge.h b/src/bridge.h index b827074..091a486 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -35,13 +35,14 @@ class RpcResult { public: RpcResult(uint32_t id, RPCClient* c, struct k_mutex* m, int timeout) : msg_id_wait(id), client(c), read_mutex(m), _timeout(timeout) {} - template bool result(RType& result) { + template bool result(RType& result, int timeout = -1) { + if (timeout > 0) _timeout = timeout; int start = millis(); while(true) { if (_timeout > 0 && (millis() - start) > _timeout){ client->lastError.code = GENERIC_ERR; - client->lastError.message = "Timed out"; + client->lastError.traceback = "Timed out"; break; } if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) { From d4cae3a7a32a4eeb5411ab6488f4af0f187c51d1 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 24 Sep 2025 15:56:49 +0200 Subject: [PATCH 06/14] mod: RpcResult _executed guard moved inside result method. result method must be executed exactly once --- src/bridge.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bridge.h b/src/bridge.h index 091a486..4c66637 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -36,6 +36,8 @@ class RpcResult { RpcResult(uint32_t id, RPCClient* c, struct k_mutex* m, int timeout) : msg_id_wait(id), client(c), read_mutex(m), _timeout(timeout) {} template bool result(RType& result, int timeout = -1) { + if (_executed) return client->lastError.code == NO_ERR; + if (timeout > 0) _timeout = timeout; int start = millis(); @@ -61,7 +63,6 @@ class RpcResult { } operator bool() { - if (_executed) return client->lastError.code == NO_ERR; MsgPack::object::nil_t nil; return result(nil, FALLBACK_TIMEOUT); } From 018b569929d59a466b96f1de43697b392e37b0ec Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Thu, 25 Sep 2025 09:44:50 +0200 Subject: [PATCH 07/14] mod: rem RpcResult.result timeout param. Setting timeout is only allowed at Bridge level feat: Bridge notifies Router with a timeout event feat: RpcResult.error field --- src/bridge.h | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/bridge.h b/src/bridge.h index 4c66637..fe9b35f 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -16,6 +16,7 @@ #define RESET_METHOD "$/reset" #define BIND_METHOD "$/register" +#define BRIDGE_TIMEOUT "$/bridgeTimeout" #define UPDATE_THREAD_STACK_SIZE 500 #define UPDATE_THREAD_PRIORITY 5 @@ -30,25 +31,28 @@ void updateEntryPoint(void *, void *, void *); class RpcResult { - const int FALLBACK_TIMEOUT = 10; - public: - RpcResult(uint32_t id, RPCClient* c, struct k_mutex* m, int timeout) : msg_id_wait(id), client(c), read_mutex(m), _timeout(timeout) {} + RpcError error; - template bool result(RType& result, int timeout = -1) { - if (_executed) return client->lastError.code == NO_ERR; + 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) {} - if (timeout > 0) _timeout = timeout; + template bool result(RType& result) { + if (_executed) return error.code == NO_ERR; - int start = millis(); + unsigned long start = millis(); while(true) { if (_timeout > 0 && (millis() - start) > _timeout){ - client->lastError.code = GENERIC_ERR; - client->lastError.traceback = "Timed out"; + error.code = GENERIC_ERR; + error.traceback = "Timed out"; + k_mutex_lock(write_mutex, K_FOREVER); + client->notify(BRIDGE_TIMEOUT); + k_mutex_unlock(write_mutex); break; } if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) { if (client->get_response(msg_id_wait, result)) { + error.code = client->lastError.code; + error.traceback = client->lastError.traceback; k_mutex_unlock(read_mutex); break; } @@ -59,12 +63,12 @@ class RpcResult { } } _executed = true; - return client->lastError.code == NO_ERR; + return error.code == NO_ERR; } operator bool() { MsgPack::object::nil_t nil; - return result(nil, FALLBACK_TIMEOUT); + return result(nil); } private: @@ -72,7 +76,8 @@ class RpcResult { RPCClient* client; bool _executed = false; struct k_mutex* read_mutex; - int _timeout = -1; + struct k_mutex* write_mutex; + unsigned long _timeout = -1; }; class BridgeClass { @@ -91,7 +96,7 @@ class BridgeClass { bool started = false; - int _timeout = -1; + unsigned long _timeout = -1; public: @@ -199,7 +204,7 @@ class BridgeClass { k_yield(); } } - return RpcResult{msg_id_wait, client, &read_mutex, _timeout}; + return RpcResult{msg_id_wait, client, &read_mutex, &write_mutex, _timeout}; } From cf6b26c0e853f20e225b0fd1ba24926e419b5f14 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Thu, 25 Sep 2025 18:32:30 +0200 Subject: [PATCH 08/14] mod: rem timeout logic. Catch PARSING_ERR and send $/bridgeError notification --- src/bridge.h | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/bridge.h b/src/bridge.h index fe9b35f..252bda6 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -16,7 +16,7 @@ #define RESET_METHOD "$/reset" #define BIND_METHOD "$/register" -#define BRIDGE_TIMEOUT "$/bridgeTimeout" +#define BRIDGE_ERROR "$/bridgeError" #define UPDATE_THREAD_STACK_SIZE 500 #define UPDATE_THREAD_PRIORITY 5 @@ -34,27 +34,24 @@ class RpcResult { public: RpcError error; - 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) {} + 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) {} template bool result(RType& result) { if (_executed) return error.code == NO_ERR; - unsigned long start = millis(); while(true) { - if (_timeout > 0 && (millis() - start) > _timeout){ - error.code = GENERIC_ERR; - error.traceback = "Timed out"; - k_mutex_lock(write_mutex, K_FOREVER); - client->notify(BRIDGE_TIMEOUT); - k_mutex_unlock(write_mutex); - break; - } if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) { if (client->get_response(msg_id_wait, result)) { error.code = client->lastError.code; error.traceback = client->lastError.traceback; k_mutex_unlock(read_mutex); break; + } else if (client->lastError.code == PARSING_ERR) { + error.code = client->lastError.code; + error.traceback = client->lastError.traceback; + k_mutex_lock(write_mutex, K_FOREVER); + client->notify(BRIDGE_ERROR); + k_mutex_unlock(write_mutex); } k_mutex_unlock(read_mutex); k_msleep(1); @@ -77,7 +74,6 @@ class RpcResult { bool _executed = false; struct k_mutex* read_mutex; struct k_mutex* write_mutex; - unsigned long _timeout = -1; }; class BridgeClass { @@ -96,8 +92,6 @@ class BridgeClass { bool started = false; - unsigned long _timeout = -1; - public: explicit BridgeClass(HardwareSerial& serial) { @@ -108,10 +102,6 @@ class BridgeClass { return started; } - void setTimeout(int t) { - _timeout = t; - } - // Initialize the bridge bool begin(unsigned long baud=DEFAULT_SERIAL_BAUD) { serial_ptr->begin(baud); @@ -204,7 +194,7 @@ class BridgeClass { k_yield(); } } - return RpcResult{msg_id_wait, client, &read_mutex, &write_mutex, _timeout}; + return RpcResult{msg_id_wait, client, &read_mutex, &write_mutex}; } From df1514cbb8132bad1d15d9e2f876dd19853b9882 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Thu, 25 Sep 2025 18:41:21 +0200 Subject: [PATCH 09/14] feat: on BRIDGE_ERROR the parsing error message is sent with notification --- src/bridge.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bridge.h b/src/bridge.h index 252bda6..049d217 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -50,7 +50,7 @@ class RpcResult { error.code = client->lastError.code; error.traceback = client->lastError.traceback; k_mutex_lock(write_mutex, K_FOREVER); - client->notify(BRIDGE_ERROR); + client->notify(BRIDGE_ERROR, error.traceback); k_mutex_unlock(write_mutex); } k_mutex_unlock(read_mutex); From 7cfc61f99ff839a24649aa026ab5bef60bcb1dab Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Thu, 25 Sep 2025 22:13:41 +0200 Subject: [PATCH 10/14] fix: RpcResult.result must check per-call error not client->lastError --- src/bridge.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/bridge.h b/src/bridge.h index 049d217..9f2a47e 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -41,17 +41,15 @@ class RpcResult { while(true) { if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) { - if (client->get_response(msg_id_wait, result)) { - error.code = client->lastError.code; - error.traceback = client->lastError.traceback; + if (client->get_response(msg_id_wait, result, error)) { k_mutex_unlock(read_mutex); break; - } else if (client->lastError.code == PARSING_ERR) { - error.code = client->lastError.code; - error.traceback = client->lastError.traceback; + } else if (error.code != NO_ERR) { + k_mutex_unlock(read_mutex); k_mutex_lock(write_mutex, K_FOREVER); client->notify(BRIDGE_ERROR, error.traceback); k_mutex_unlock(write_mutex); + break; } k_mutex_unlock(read_mutex); k_msleep(1); From db9a2883ee8b7048f614b99905100b2e216d6855 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Thu, 25 Sep 2025 22:39:22 +0200 Subject: [PATCH 11/14] minor: Monitor._read optimization --- src/monitor.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/monitor.h b/src/monitor.h index d091b04..bac7465 100644 --- a/src/monitor.h +++ b/src/monitor.h @@ -121,25 +121,22 @@ class BridgeMonitor: public Stream { if (size == 0) return; - k_mutex_lock(&monitor_mutex, K_FOREVER); - MsgPack::arr_t message; bool ret = bridge->call(MON_READ_METHOD, size).result(message); if (ret) { + k_mutex_lock(&monitor_mutex, K_FOREVER); for (size_t i = 0; i < message.size(); ++i) { temp_buffer.store_char(static_cast(message[i])); } + k_mutex_unlock(&monitor_mutex); } // if (bridge.lastError.code > NO_ERR) { // is_connected = false; // } - - k_mutex_unlock(&monitor_mutex); } - }; extern BridgeClass Bridge; From 744fb967e2dac96bbc4091a5a3752581520bad9e Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 26 Sep 2025 09:14:33 +0200 Subject: [PATCH 12/14] fix: RpcResult should break out of its while loop only if the packet was consumed but notify PARSING_ERR --- src/bridge.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/bridge.h b/src/bridge.h index 9f2a47e..68254f5 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -43,12 +43,11 @@ class RpcResult { if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) { if (client->get_response(msg_id_wait, result, error)) { k_mutex_unlock(read_mutex); - break; - } else if (error.code != NO_ERR) { - k_mutex_unlock(read_mutex); - k_mutex_lock(write_mutex, K_FOREVER); - client->notify(BRIDGE_ERROR, error.traceback); - k_mutex_unlock(write_mutex); + if (error.code == PARSING_ERR) { + k_mutex_lock(write_mutex, K_FOREVER); + client->notify(BRIDGE_ERROR, error.traceback); + k_mutex_unlock(write_mutex); + } break; } k_mutex_unlock(read_mutex); From 381fed79acd86fa1e4b14411126e50b097c5d8d4 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 26 Sep 2025 14:12:23 +0200 Subject: [PATCH 13/14] feat: overloaded RpcResult.result() waits a msgpack nil result feat: RpcResult destructor invokes .result() --- src/bridge.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/bridge.h b/src/bridge.h index 68254f5..03b2c59 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -60,11 +60,19 @@ class RpcResult { return error.code == NO_ERR; } - operator bool() { + bool result() { MsgPack::object::nil_t nil; return result(nil); } + ~RpcResult(){ + result(); + } + + operator bool() { + return result(); + } + private: uint32_t msg_id_wait; RPCClient* client; From 5dc55dda840775cf6889359be4a4c56022e8f86a Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 26 Sep 2025 17:07:20 +0200 Subject: [PATCH 14/14] mod: commented out the BRIDGE_ERROR notification --- src/bridge.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bridge.h b/src/bridge.h index 03b2c59..7a2858c 100644 --- a/src/bridge.h +++ b/src/bridge.h @@ -16,7 +16,7 @@ #define RESET_METHOD "$/reset" #define BIND_METHOD "$/register" -#define BRIDGE_ERROR "$/bridgeError" +//#define BRIDGE_ERROR "$/bridgeLog" #define UPDATE_THREAD_STACK_SIZE 500 #define UPDATE_THREAD_PRIORITY 5 @@ -43,11 +43,11 @@ class RpcResult { if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) { if (client->get_response(msg_id_wait, result, error)) { k_mutex_unlock(read_mutex); - if (error.code == PARSING_ERR) { - k_mutex_lock(write_mutex, K_FOREVER); - client->notify(BRIDGE_ERROR, error.traceback); - k_mutex_unlock(write_mutex); - } + // if (error.code == PARSING_ERR) { + // k_mutex_lock(write_mutex, K_FOREVER); + // client->notify(BRIDGE_ERROR, error.traceback); + // k_mutex_unlock(write_mutex); + // } break; } k_mutex_unlock(read_mutex);