Skip to content

Commit d8fba93

Browse files
committed
AP_Periph: make simultaneous tunneling/serial-passthrough serial ports work
this allows for more than one connection to be running through the peripheral at any one time
1 parent 8384b74 commit d8fba93

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

Tools/AP_Periph/AP_Periph.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ class AP_Periph_FW {
533533
void send_serial_monitor_data();
534534
int8_t get_default_tunnel_serial_port(void) const;
535535

536-
struct {
536+
struct UARTMonitor {
537537
ByteBuffer *buffer;
538538
uint32_t last_request_ms;
539539
AP_HAL::UARTDriver *uart;
@@ -542,7 +542,8 @@ class AP_Periph_FW {
542542
uint8_t protocol;
543543
uint32_t baudrate;
544544
bool locked;
545-
} uart_monitor;
545+
} uart_monitors[SERIALMANAGER_MAX_PORTS];
546+
void send_serial_monitor_data_instance(UARTMonitor &monitor);
546547
#endif
547548

548549
// handlers for incoming messages

Tools/AP_Periph/serial_tunnel.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,32 @@ void AP_Periph_FW::handle_tunnel_Targetted(CanardInstance* canard_ins, CanardRxT
7878
if (pkt.target_node != canardGetLocalNodeID(canard_ins)) {
7979
return;
8080
}
81-
if (uart_monitor.buffer == nullptr) {
82-
uart_monitor.buffer = NEW_NOTHROW ByteBuffer(1024);
83-
if (uart_monitor.buffer == nullptr) {
84-
return;
85-
}
86-
}
8781
int8_t uart_num = pkt.serial_id;
8882
if (uart_num == -1) {
8983
uart_num = get_default_tunnel_serial_port();
9084
}
9185
if (uart_num < 0) {
9286
return;
9387
}
94-
auto *uart = hal.serial(uart_num);
95-
if (uart == nullptr) {
88+
if (uint8_t(uart_num) > ARRAY_SIZE(uart_monitors)) {
9689
return;
9790
}
98-
if (uart_monitor.uart_num != uart_num && uart_monitor.uart != nullptr) {
99-
// remove monitor from previous uart
100-
hal.serial(uart_monitor.uart_num)->set_monitor_read_buffer(nullptr);
101-
}
102-
uart_monitor.uart_num = uart_num;
103-
if (uart != uart_monitor.uart) {
104-
// change of uart or expired, clear old data
105-
uart_monitor.buffer->clear();
106-
uart_monitor.uart = uart;
91+
auto &uart_monitor = uart_monitors[uart_num];
92+
if (uart_monitor.uart == nullptr) {
93+
auto *uart = hal.serial(uart_num);
94+
if (uart == nullptr) {
95+
return;
96+
}
97+
if (uart_monitor.buffer == nullptr) {
98+
uart_monitor.buffer = NEW_NOTHROW ByteBuffer(1024);
99+
if (uart_monitor.buffer == nullptr) {
100+
return;
101+
}
102+
uart_monitor.uart_num = uart_num;
103+
}
107104
uart_monitor.baudrate = 0;
105+
uart_monitor.uart = uart; // setting this indicates the monitor is set up
106+
uart_monitor.uart->set_monitor_read_buffer(uart_monitor.buffer);
108107
}
109108
if (uart_monitor.uart == nullptr) {
110109
return;
@@ -130,7 +129,6 @@ void AP_Periph_FW::handle_tunnel_Targetted(CanardInstance* canard_ins, CanardRxT
130129
}
131130
uart_monitor.baudrate = pkt.baudrate;
132131
}
133-
uart_monitor.uart->set_monitor_read_buffer(uart_monitor.buffer);
134132
uart_monitor.last_request_ms = AP_HAL::millis();
135133

136134
// write to device
@@ -150,6 +148,13 @@ void AP_Periph_FW::handle_tunnel_Targetted(CanardInstance* canard_ins, CanardRxT
150148
send tunnelled serial data
151149
*/
152150
void AP_Periph_FW::send_serial_monitor_data()
151+
{
152+
for (auto &m : uart_monitors) {
153+
send_serial_monitor_data_instance(m);
154+
}
155+
}
156+
157+
void AP_Periph_FW::send_serial_monitor_data_instance(AP_Periph_FW::UARTMonitor &uart_monitor)
153158
{
154159
if (uart_monitor.uart == nullptr ||
155160
uart_monitor.node_id == 0 ||

0 commit comments

Comments
 (0)