Arduino_RouterBridge is a zephyr RTOS multithreading wrapper of RPClite, designed for Arduino UNO Q boards.
By including Arduino_RouterBridge.h the user gains access to a Bridge singleton object that can be used as an RPC
client/server to execute and serve RPCs to/from a CPU Host running an rpclib compatible Router.
- The
Bridgeobject is defined over an UART port routed by the zephyr core, falling back to Serial1 if the core does not provide it - The
Bridge.callmethod is non-blocking and returns an RpcCall async object RpcCallclass implements a blocking.resultmethod that waits for the RPC response and returns true if the RPC returned with no errorsRpcCall.resultwrites the return value of the remote call to the provided reference parameter. The result can be retrieved exactly once; subsequent calls to.resultreturn an error.- The
Bridgecan provide callbacks to incoming RPC requests both in a thread-unsafe and thread-safe fashion (by means ofBridgeClass::provideandBridgeClass::provide_safe) - Thread-safe methods execution is granted in the main loop thread where
update_safeis called. By design, users cannot access.update_safe()freely - Thread-unsafe methods are served in an update callback, whose execution is granted in a separate thread. Nonetheless, users can access
.update()freely with caution
#include <Arduino_RouterBridge.h>
bool set_led(bool state) {
digitalWrite(LED_BUILTIN, state);
return state;
}
String greet() {
return String("Hello Friend");
}
void setup() {
Bridge.begin();
Monitor.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
Bridge.provide("set_led", set_led);
Bridge.provide_safe("greet", greet);
}
void loop() {
float sum;
// CALL EXAMPLES
// Standard chained call: Bridge.call("method", params...).result(res)
if (!Bridge.call("add", 1.0, 2.0).result(sum)) {
Monitor.println("Error calling method: add");
};
// Async call
RpcCall async_rpc = Bridge.call("add", 3.0, 4.5);
if (!async_rpc.result(sum)) {
Monitor.println("Error calling method: add");
Monitor.print("Error code: ");
Monitor.println(async_rpc.getErrorCode());
Monitor.print("Error message: ");
Monitor.println(async_rpc.getErrorMessage());
}
// Implicit boolean cast. Use with caution as in this case the call is indeed
// executed expecting a fallback nil result (MsgPack::object::nil_t)
if (!Bridge.call("send_greeting", "Hello Friend")) {
Monitor.println("Error calling method: send_greeting");
};
// Please use notify when no reult (None, null, void, nil etc.) is expected from the opposite side
// the following is executed immediately
Bridge.notify("signal", 200);
}Calling
Bridge.callfrom within an RPC callback may cause an MCU–CPU IPC deadlock.