Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
394 changes: 373 additions & 21 deletions LICENSE

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
In this repo it will be implemented an Arduino library wrapper for RPClite to be run on Arduino UNO Q boards.

The desired API is shown in https://github.com/bcmi-labs/Arduino_RouterBridge/blob/main/desired.ino.

This is WIP. Expects changes soon.

## The Bridge object ##

Including Arduino_RouterBridge.h gives the user access to a Bridge object that can be used both as a RPC client and/or server to execute and serve RPCs to/from the CPU Host running a GOLANG router.
Expand All @@ -16,6 +12,8 @@ Including Arduino_RouterBridge.h gives the user access to a Bridge object that c


```cpp
#include <Arduino_RouterBridge.h>

bool set_led(bool state) {
digitalWrite(LED_BUILTIN, state);
return state;
Expand Down Expand Up @@ -50,7 +48,5 @@ void loop() {
};

Bridge.notify("signal", 200);

//Bridge.update(); // Thread-unsafe update execution is granted in its own thread. It can be called manually with caution
}
```
65 changes: 65 additions & 0 deletions examples/monitor/monitor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
This file is part of the Arduino_RouterBridge library.

Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

*/

#include <Arduino_RouterBridge.h>


bool set_led(bool state) {
digitalWrite(LED_BUILTIN, state);
return state;
}

int add(int a, int b) {
return a + b;
}

String greet() {
return String("Hello Friend");
}

void setup() {
Serial.begin(115200);

if (!Bridge.begin()) {
Serial.println("cannot setup Bridge");
}

if(!Monitor.begin()){
Serial.println("cannot setup Monitor");
}

pinMode(LED_BUILTIN, OUTPUT);

if (!Bridge.provide("set_led", set_led)) {
Serial.println("Error providing method: set_led");
} else {
Serial.println("Registered method: set_led");
}

Bridge.provide("add", add);
Bridge.provide("greet", greet);

}

void loop() {

Bridge.notify("signal", 200);

Monitor.println("DEBUG: a debug message");

if (Monitor.available()) {
String input = Monitor.readStringUntil('\n'); // Read until newline
Monitor.print("You entered: ");
Monitor.println(input);
}

delay(500);
}
14 changes: 11 additions & 3 deletions examples/simple_bridge/simple_bridge.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/*
This file is part of the Arduino_RouterBridge library.

Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

*/

#include <Arduino_RouterBridge.h>

//BridgeClass Bridge(Serial1);

bool set_led(bool state) {
digitalWrite(LED_BUILTIN, state);
Expand Down Expand Up @@ -42,6 +52,4 @@ void loop() {
};

Bridge.notify("signal", 200);

//Bridge.update(); // Thread-unsafe update execution is granted in its own thread. It can be called manually with caution
}
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"url": "https://github.com/bcmi-labs/Arduino_RouterBridge",
"maintainer": true
},
"version": "0.1.0",
"license": "MIT",
"version": "0.1.2",
"license": "MPL2.0",
"frameworks": "arduino",
"platforms": "*",
"dependencies":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arduino_RouterBridge
version=0.1.0
version=0.1.2
author=BCMI-labs
maintainer=BCMI-labs
sentence=A RPC bridge for Arduino UNO Q boards
Expand Down
12 changes: 12 additions & 0 deletions src/Arduino_RouterBridge.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
/*
This file is part of the Arduino_RouterBridge library.

Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

*/

#ifndef ARDUINO_ROUTER_BRIDGE_H
#define ARDUINO_ROUTER_BRIDGE_H

#include "Arduino.h"
#include "bridge.h"
#include "monitor.h"

#endif //ARDUINO_ROUTER_BRIDGE_H
97 changes: 74 additions & 23 deletions src/bridge.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/*
This file is part of the Arduino_RouterBridge library.

Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

*/

#pragma once

#ifndef ROUTER_BRIDGE_H
Expand Down Expand Up @@ -81,41 +92,67 @@ class BridgeClass {

void update() {

k_msleep(1);
// Lock read mutex
k_mutex_lock(&read_mutex, K_FOREVER);
if (!server->get_rpc()) {
if (k_mutex_lock(&read_mutex, K_MSEC(10)) != 0 ) return;

RPCRequest<> req;
if (!server->get_rpc(req)) {
k_mutex_unlock(&read_mutex);
k_msleep(1);
return;
}

k_mutex_unlock(&read_mutex);

server->process_request();
server->process_request(req);

// Lock write mutex
k_mutex_lock(&write_mutex, K_FOREVER);
server->send_response();
k_mutex_unlock(&write_mutex);
while (true) {

if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0){
server->send_response(req);
k_mutex_unlock(&write_mutex);
k_msleep(1);
break;
} else {
k_msleep(1);
}

}

}

template<typename RType, typename... Args>
bool call(const MsgPack::str_t method, RType& result, Args&&... args) {

uint32_t msg_id_wait;

// Lock write mutex
k_mutex_lock(&write_mutex, K_FOREVER);
client->send_rpc(method, std::forward<Args>(args)...);
k_mutex_unlock(&write_mutex);
while (true) {
if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0) {
client->send_rpc(method, msg_id_wait, std::forward<Args>(args)...);
k_mutex_unlock(&write_mutex);
k_msleep(1);
break;
} else {
k_msleep(1);
}
}

// Lock read mutex
while(1) {
k_mutex_lock(&read_mutex, K_FOREVER);
if (client->get_response(result)) {
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);
k_msleep(1);
break;
}
k_mutex_unlock(&read_mutex);
break;
k_msleep(1);
} else {
k_msleep(1);
}
k_mutex_unlock(&read_mutex);
k_msleep(1);

}

return (client->lastError.code == NO_ERR);
Expand All @@ -140,21 +177,33 @@ class BridgeClass {
void update_safe() {

// Lock read mutex
k_msleep(1);
k_mutex_lock(&read_mutex, K_FOREVER);
if (!server->get_rpc()) {
if (k_mutex_lock(&read_mutex, K_MSEC(10)) != 0 ) return;

RPCRequest<> req;
if (!server->get_rpc(req, "__safe__")) {
k_mutex_unlock(&read_mutex);
k_msleep(1);
return;
}

k_mutex_unlock(&read_mutex);

server->process_request("__safe__");
server->process_request(req);

// Lock write mutex
k_mutex_lock(&write_mutex, K_FOREVER);
server->send_response();
k_mutex_unlock(&write_mutex);
while (true) {

if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0){
server->send_response(req);
k_mutex_unlock(&write_mutex);
k_msleep(1);
break;
} else {
k_msleep(1);
}

}

}

friend class BridgeClassUpdater;
Expand All @@ -176,6 +225,7 @@ BridgeClass Bridge(Serial1);
void updateEntryPoint(void *, void *, void *){
while(1){
Bridge.update();
k_msleep(1);
}
}

Expand All @@ -184,6 +234,7 @@ static void safeUpdate(){
}

void __loopHook(){
k_msleep(1);
safeUpdate();
}

Expand Down
Loading