Skip to content

Commit 822af35

Browse files
committed
Add HCI RPC transport.
Supports sending raw HCI commands over RPC. Signed-off-by: iabdalkader <[email protected]>
1 parent 23acde3 commit 822af35

File tree

4 files changed

+169
-1
lines changed

4 files changed

+169
-1
lines changed

src/local/BLELocalDevice.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#undef ARDUINO_GIGA
3232
#undef ARDUINO_NICLA_VISION
3333
#undef ARDUINO_PORTENTA_C33
34+
#undef ARDUINO_UNO_Q
3435
#endif
3536

3637
#if defined(PORTENTA_H7_PINS) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018-2025 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#if defined(ARDUINO_UNO_Q)
21+
22+
#include "HCIVirtualTransportRPC.h"
23+
24+
HCIVirtualTransportRPCClass::HCIVirtualTransportRPCClass() : initialized(false) {
25+
}
26+
27+
HCIVirtualTransportRPCClass::~HCIVirtualTransportRPCClass() {
28+
}
29+
30+
int HCIVirtualTransportRPCClass::begin() {
31+
// Initialize the Bridge if not already started
32+
if (!Bridge) {
33+
if (!Bridge.begin()) {
34+
return 0;
35+
}
36+
}
37+
38+
// Open HCI device through Bridge RPC
39+
if (!HCI.begin("hci0")) {
40+
return 0;
41+
}
42+
43+
rxbuf.clear();
44+
initialized = true;
45+
46+
return 1;
47+
}
48+
49+
void HCIVirtualTransportRPCClass::end() {
50+
if (initialized) {
51+
HCI.end();
52+
initialized = false;
53+
}
54+
}
55+
56+
void HCIVirtualTransportRPCClass::wait(unsigned long timeout) {
57+
delay(timeout);
58+
}
59+
60+
int HCIVirtualTransportRPCClass::available() {
61+
if (!initialized) {
62+
return 0;
63+
}
64+
65+
// Return buffered data if available
66+
if (rxbuf.available()) {
67+
return rxbuf.available();
68+
}
69+
70+
// Check if data is available from Bridge HCI and buffer it
71+
if (HCI.available()) {
72+
uint8_t packet[258];
73+
int received = HCI.recv(packet, sizeof(packet));
74+
75+
if (received > 0) {
76+
// Store received data in ring buffer
77+
for (int i = 0; i < received; i++) {
78+
rxbuf.store_char(packet[i]);
79+
}
80+
}
81+
}
82+
83+
return rxbuf.available();
84+
}
85+
86+
int HCIVirtualTransportRPCClass::peek() {
87+
return -1;
88+
}
89+
90+
int HCIVirtualTransportRPCClass::read() {
91+
if (rxbuf.available()) {
92+
return rxbuf.read_char();
93+
}
94+
95+
return -1;
96+
}
97+
98+
size_t HCIVirtualTransportRPCClass::write(const uint8_t* data, size_t length) {
99+
if (!initialized) {
100+
return 0;
101+
}
102+
103+
int sent = HCI.send(data, length);
104+
if (sent < 0) {
105+
return 0;
106+
}
107+
108+
return sent;
109+
}
110+
111+
HCIVirtualTransportRPCClass HCIVirtualTransportRPC;
112+
HCITransportInterface& HCITransport = HCIVirtualTransportRPC;
113+
114+
#endif
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018-2025 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _HCI_VIRTUAL_TRANSPORT_RPC_H_
21+
#define _HCI_VIRTUAL_TRANSPORT_RPC_H_
22+
23+
#include "HCITransport.h"
24+
#include "api/RingBuffer.h"
25+
#include <Arduino_RouterBridge.h>
26+
#include <stdint.h>
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
class HCIVirtualTransportRPCClass : public HCITransportInterface {
31+
public:
32+
HCIVirtualTransportRPCClass();
33+
virtual ~HCIVirtualTransportRPCClass();
34+
35+
virtual int begin();
36+
virtual void end();
37+
38+
virtual void wait(unsigned long timeout);
39+
40+
virtual int available();
41+
virtual int peek();
42+
virtual int read();
43+
44+
virtual size_t write(const uint8_t* data, size_t length);
45+
46+
private:
47+
RingBufferN<258> rxbuf;
48+
bool initialized;
49+
};
50+
51+
extern HCIVirtualTransportRPCClass HCIVirtualTransportRPC;
52+
53+
#endif

src/utility/HCIVirtualTransportZephyr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#if defined(__ZEPHYR__)
20+
#if defined(__ZEPHYR__) && !defined(ARDUINO_UNO_Q)
2121

2222
#include "HCIVirtualTransportZephyr.h"
2323
#include "HCI.h"

0 commit comments

Comments
 (0)