Skip to content

Commit 1825fee

Browse files
committed
Merge remote-tracking branch 'origin/main' into 14-add-a-class-for-udp-client
# Conflicts: # src/Arduino_RouterBridge.h # src/tcp_client.h
2 parents 138de80 + 99b074e commit 1825fee

File tree

9 files changed

+420
-137
lines changed

9 files changed

+420
-137
lines changed

examples/hci/hci.ino

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
This file is part of the Arduino_RouterBridge library.
3+
4+
Copyright (c) 2025 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include <Arduino_RouterBridge.h>
13+
14+
void setup() {
15+
Serial.begin(115200);
16+
while (!Serial) {
17+
delay(10);
18+
}
19+
20+
Serial.println("Arduino HCI Example - Read Local Version");
21+
22+
if (!Bridge.begin()) {
23+
Serial.println("Failed to setup Bridge");
24+
return;
25+
}
26+
27+
if (!HCI.begin("hci0")) {
28+
Serial.println("Failed to open HCI device");
29+
return;
30+
}
31+
32+
Serial.println("HCI device opened successfully");
33+
34+
delay(1000);
35+
readLocalVersion();
36+
}
37+
38+
void loop() {
39+
// Nothing to do in loop
40+
delay(1000);
41+
}
42+
43+
void readLocalVersion() {
44+
uint8_t cmd[4];
45+
cmd[0] = 0x01; // HCI Command Packet Type
46+
cmd[1] = 0x01; // OCF = 0x0001 (lower byte)
47+
cmd[2] = 0x10; // OGF = 0x04 (0x04 << 2 = 0x10 in upper 6 bits)
48+
cmd[3] = 0x00; // Parameter length = 0
49+
50+
Serial.println("Sending HCI Read Local Version command...");
51+
52+
int sent = HCI.send(cmd, sizeof(cmd));
53+
if (sent < 0) {
54+
Serial.println("Failed to send HCI command");
55+
return;
56+
}
57+
58+
Serial.print("Sent ");
59+
Serial.print(sent);
60+
Serial.println(" bytes");
61+
62+
// Wait for response with timeout
63+
Serial.println("Waiting for response...");
64+
int avail = 0;
65+
unsigned long startTime = millis();
66+
while (avail == 0 && (millis() - startTime) < 1000) { // 1 second timeout
67+
avail = HCI.available();
68+
if (avail == 0) {
69+
delay(10); // Small delay between polls
70+
}
71+
}
72+
73+
Serial.print("Available bytes: ");
74+
Serial.println(avail);
75+
76+
if (avail == 0) {
77+
Serial.println("Timeout: No response received");
78+
return;
79+
}
80+
81+
// Read response
82+
uint8_t response[255];
83+
int received = HCI.recv(response, sizeof(response));
84+
85+
if (received > 0) {
86+
Serial.print("Received ");
87+
Serial.print(received);
88+
Serial.println(" bytes:");
89+
90+
// Print response in hex
91+
for (int i = 0; i < received; i++) {
92+
if (response[i] < 0x10) Serial.print("0");
93+
Serial.print(response[i], HEX);
94+
Serial.print(" ");
95+
}
96+
Serial.println();
97+
98+
// Parse Command Complete Event
99+
// Event format: Packet Type, Event Code, Param Length, Num_HCI_Command_Packets, Opcode, Status, ...
100+
if (received >= 6 && response[0] == 0x04 && response[1] == 0x0E) {
101+
Serial.println("Command Complete Event received");
102+
Serial.print("Status: 0x");
103+
Serial.println(response[6], HEX);
104+
}
105+
} else {
106+
Serial.println("No response received");
107+
}
108+
}

examples/simple_bridge/simple_bridge.ino

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ void loop() {
4949
float res;
5050
if (!Bridge.call("multiply", 1.0, 2.0).result(res)) {
5151
Serial.println("Error calling method: multiply");
52-
Serial.println(Bridge.get_error_code());
53-
Serial.println(Bridge.get_error_message());
5452
};
5553

5654
// Call with deferred response check
@@ -61,8 +59,8 @@ void loop() {
6159
Serial.print("Result of the operation is: ");
6260
Serial.println(res);
6361
} else {
64-
Serial.println(Bridge.get_error_code());
65-
Serial.println(Bridge.get_error_message());
62+
Serial.println(outcome.error.code);
63+
Serial.println(outcome.error.traceback);
6664
}
6765

6866
Bridge.notify("signal", 200);

library.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
"description": "A RPC bridge for Arduino UNO Q boards",
55
"repository": {
66
"type": "git",
7-
"url": "https://github.com/bcmi-labs/Arduino_RouterBridge"
7+
"url": "https://github.com/arduino-libraries/Arduino_RouterBridge"
88
},
99
"authors": {
10-
"name": "BCMI-labs",
11-
"url": "https://github.com/bcmi-labs/Arduino_RouterBridge",
10+
"name": "Arduino",
11+
"url": "https://github.com/arduino-libraries/Arduino_RouterBridge",
1212
"maintainer": true
1313
},
14-
"version": "0.2.1",
14+
"version": "0.2.2",
1515
"license": "MPL2.0",
1616
"frameworks": "arduino",
1717
"platforms": "*",
1818
"dependencies":
1919
{
20-
"name": "bcmi-labs/Arduino_RPClite"
20+
"name": "arduino-libraries/Arduino_RPClite"
2121
}
2222
}

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name=Arduino_RouterBridge
2-
version=0.2.1
3-
author=BCMI-labs
4-
maintainer=BCMI-labs
2+
version=0.2.2
3+
author=Arduino
4+
maintainer=Arduino
55
sentence=A RPC bridge for Arduino UNO Q boards
66
paragraph=This library provides a simple RPC bridge for Arduino UNO Q boards, allowing communication between the board and other devices using MsgPack serialization.
77
category=Communication

src/bridge.h

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class BridgeClass {
106106

107107
struct k_mutex read_mutex{};
108108
struct k_mutex write_mutex{};
109+
struct k_mutex bridge_mutex{};
109110

110111
k_tid_t upd_tid{};
111112
k_thread_stack_t *upd_stack_area{};
@@ -119,17 +120,27 @@ class BridgeClass {
119120
serial_ptr = &serial;
120121
}
121122

122-
operator bool() const {
123-
return started;
123+
operator bool() {
124+
return is_started();
125+
}
126+
127+
bool is_started() {
128+
k_mutex_lock(&bridge_mutex, K_FOREVER);
129+
bool out = started;
130+
k_mutex_unlock(&bridge_mutex);
131+
return out;
124132
}
125133

126134
// Initialize the bridge
127135
bool begin(unsigned long baud=DEFAULT_SERIAL_BAUD) {
128-
serial_ptr->begin(baud);
129-
transport = new SerialTransport(*serial_ptr);
130-
131136
k_mutex_init(&read_mutex);
132137
k_mutex_init(&write_mutex);
138+
k_mutex_init(&bridge_mutex);
139+
140+
if (is_started()) return true;
141+
142+
serial_ptr->begin(baud);
143+
transport = new SerialTransport(*serial_ptr);
133144

134145
client = new RPCClient(*transport);
135146
server = new RPCServer(*transport);
@@ -140,33 +151,31 @@ class BridgeClass {
140151
updateEntryPoint,
141152
NULL, NULL, NULL,
142153
UPDATE_THREAD_PRIORITY, 0, K_NO_WAIT);
154+
k_thread_name_set(upd_tid, "bridge");
143155

144-
bool res;
145-
call(RESET_METHOD).result(res);
146-
if (res) {
147-
started = true;
148-
}
156+
k_mutex_lock(&bridge_mutex, K_FOREVER);
157+
bool res = false;
158+
started = call(RESET_METHOD).result(res) && res;
159+
k_mutex_unlock(&bridge_mutex);
149160
return res;
150161
}
151162

152163
template<typename F>
153164
bool provide(const MsgPack::str_t& name, F&& func) {
165+
k_mutex_lock(&bridge_mutex, K_FOREVER);
154166
bool res;
155-
if (!call(BIND_METHOD, name).result(res)) {
156-
return false;
157-
}
158-
return server->bind(name, func);
167+
bool out = call(BIND_METHOD, name).result(res) && res && server->bind(name, func);
168+
k_mutex_unlock(&bridge_mutex);
169+
return out;
159170
}
160171

161172
template<typename F>
162173
bool provide_safe(const MsgPack::str_t& name, F&& func) {
174+
k_mutex_lock(&bridge_mutex, K_FOREVER);
163175
bool res;
164-
if (!call(BIND_METHOD, name).result(res)) {
165-
return false;
166-
}
167-
168-
return server->bind(name, func, "__safe__");
169-
176+
bool out = call(BIND_METHOD, name).result(res) && res && server->bind(name, func, "__safe__");
177+
k_mutex_unlock(&bridge_mutex);
178+
return out;
170179
}
171180

172181
void update() {

0 commit comments

Comments
 (0)