Skip to content

Commit bfd45b9

Browse files
committed
feat: MsgPack RpcError type (cannot deserialize)
1 parent fa2456c commit bfd45b9

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

examples/rpc_lite_client/rpc_lite_client.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ void blink_before(){
2828
void loop() {
2929
float result;
3030
blink_before();
31-
bool ok = client.call("mult", result, 2.0, 3.0);
31+
//bool ok = client.call("mult", result, 2.0, 3.0);
32+
bool ok = client.call("divi", result, 2.0, 0.0);
3233

3334
if (ok) {
3435
Serial.print("Result: ");

extras/examples/serial_server_example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ def greet(name):
99
def multiply(a, b):
1010
return a*b
1111

12+
def divide(a, b):
13+
return a/b
14+
1215
PORT = '/dev/ttySTM0'
1316

1417
server = SerialServer(port=PORT, baudrate=115200)
1518
server.register_callback('add', add)
1619
server.register_callback('mult', multiply)
20+
server.register_callback('divi', divide)
1721
server.register_callback('greet', greet)
1822
server.start()
1923

extras/serial_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
RESPONSE = 1
88
NOTIFY = 2
99

10+
GENERIC_EXCEPTION = 0xff
1011

1112
class SerialServer:
1213
def __init__(self, port, baudrate=115200):
@@ -24,7 +25,7 @@ def on_request(self, msg_id, command, args):
2425
result = self.callbacks[command](*args)
2526
return [RESPONSE, msg_id, None, result]
2627
except Exception as e:
27-
print("Not handling exceptions yet")
28+
return [RESPONSE, msg_id, [GENERIC_EXCEPTION, str(e)] ,None]
2829

2930
def handle_message(self, message) -> bytes:
3031
"""Process incoming messages"""

src/client.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef RPCLITE_CLIENT_H
66
#define RPCLITE_CLIENT_H
77
#include "rpc.h"
8+
#include "error.h"
89

910
class RPCClient {
1011
ITransport& transport;
@@ -37,19 +38,33 @@ class RPCClient {
3738

3839
int r_msg_type;
3940
int r_msg_id;
40-
MsgPack::object::nil_t error;
41+
MsgPack::object::nil_t nil;
42+
RpcError rpc_error;
4143

4244
MsgPack::arr_size_t resp_size(4);
4345

44-
bool ok = unpacker.deserialize(resp_size, r_msg_type, r_msg_id, error, result);
46+
if (!unpacker.deserialize(resp_size, r_msg_type, r_msg_id, nil, result)){
47+
//Try to deserialize for a RpcError
48+
if (!unpacker.deserialize(resp_size, r_msg_type, r_msg_id, rpc_error, nil)){
49+
Serial.println("Unable to deserialize");
50+
Serial.print(raw_buffer_fill);
51+
for (size_t i = 0; i<raw_buffer_fill; i++){
52+
Serial.print(raw_buffer[i], HEX);
53+
Serial.print("-");
54+
}
55+
flush_buffer();
56+
return false;
57+
} else {
58+
Serial.print("RPC produced an error: ");
59+
Serial.println(rpc_error.code);
60+
Serial.println(rpc_error.traceback);
61+
}
4562

46-
if (!ok){
47-
//Serial.println("could not serialize resp");
48-
return false;
4963
}
5064

5165
if (r_msg_id != msg_id){
5266
//Serial.println("msg_id mismatch");
67+
flush_buffer();
5368
return false;
5469
}
5570

@@ -58,8 +73,6 @@ class RPCClient {
5873
flush_buffer();
5974
return true;
6075

61-
62-
return false;
6376
}
6477
};
6578

src/error.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Created by lucio on 4/25/25.
2+
//
3+
4+
#ifndef ERROR_RPC_H
5+
#define ERROR_RPC_H
6+
7+
#include "MsgPack.h"
8+
9+
struct RpcError {
10+
int code;
11+
MsgPack::str_t traceback;
12+
MSGPACK_DEFINE(code, traceback); // -> [code, traceback]
13+
};
14+
15+
#endif

0 commit comments

Comments
 (0)