CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/AP_Periph/serial_tunnel.cpp
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
/*
16
handle tunnelling of serial data over DroneCAN
17
*/
18
19
#include <AP_HAL/AP_HAL_Boards.h>
20
#include "AP_Periph.h"
21
22
#if AP_UART_MONITOR_ENABLED
23
24
#include <dronecan_msgs.h>
25
26
extern const AP_HAL::HAL &hal;
27
28
#define TUNNEL_LOCK_KEY 0xf2e460e4U
29
30
#ifndef TUNNEL_DEBUG
31
#define TUNNEL_DEBUG 0
32
#endif
33
34
#if TUNNEL_DEBUG
35
# define debug(fmt, args...) can_printf(fmt "\n", ##args)
36
#else
37
# define debug(fmt, args...)
38
#endif
39
40
/*
41
get the default port to tunnel if the client requests port -1
42
*/
43
int8_t AP_Periph_FW::get_default_tunnel_serial_port(void) const
44
{
45
int8_t uart_num = -1;
46
#ifdef HAL_PERIPH_ENABLE_GPS
47
if (uart_num == -1) {
48
uart_num = g.gps_port;
49
}
50
#endif
51
#ifdef HAL_PERIPH_ENABLE_RANGEFINDER
52
if (uart_num == -1) {
53
uart_num = g.rangefinder_port[0];
54
}
55
#endif
56
#ifdef HAL_PERIPH_ENABLE_ADSB
57
if (uart_num == -1) {
58
uart_num = g.adsb_port;
59
}
60
#endif
61
#ifdef HAL_PERIPH_ENABLE_PROXIMITY
62
if (uart_num == -1) {
63
uart_num = g.proximity_port;
64
}
65
#endif
66
return uart_num;
67
}
68
69
/*
70
handle tunnel data
71
*/
72
void AP_Periph_FW::handle_tunnel_Targetted(CanardInstance* canard_ins, CanardRxTransfer* transfer)
73
{
74
uavcan_tunnel_Targetted pkt;
75
if (uavcan_tunnel_Targetted_decode(transfer, &pkt)) {
76
return;
77
}
78
if (pkt.target_node != canardGetLocalNodeID(canard_ins)) {
79
return;
80
}
81
if (uart_monitor.buffer == nullptr) {
82
uart_monitor.buffer = NEW_NOTHROW ByteBuffer(1024);
83
if (uart_monitor.buffer == nullptr) {
84
return;
85
}
86
}
87
int8_t uart_num = pkt.serial_id;
88
if (uart_num == -1) {
89
uart_num = get_default_tunnel_serial_port();
90
}
91
if (uart_num < 0) {
92
return;
93
}
94
auto *uart = hal.serial(uart_num);
95
if (uart == nullptr) {
96
return;
97
}
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;
107
uart_monitor.baudrate = 0;
108
}
109
if (uart_monitor.uart == nullptr) {
110
return;
111
}
112
/*
113
allow for locked state to change at any time, so users can
114
switch between locked and unlocked while connected
115
*/
116
const bool was_locked = uart_monitor.locked;
117
uart_monitor.locked = (pkt.options & UAVCAN_TUNNEL_TARGETTED_OPTION_LOCK_PORT) != 0;
118
if (uart_monitor.locked) {
119
uart_monitor.uart->lock_port(TUNNEL_LOCK_KEY, TUNNEL_LOCK_KEY);
120
} else {
121
uart_monitor.uart->lock_port(0,0);
122
}
123
uart_monitor.node_id = transfer->source_node_id;
124
uart_monitor.protocol = pkt.protocol.protocol;
125
if (pkt.baudrate != uart_monitor.baudrate || !was_locked) {
126
if (uart_monitor.locked && pkt.baudrate != 0) {
127
// ensure we have enough buffer space for a uBlox fw update and fast uCenter data
128
uart_monitor.uart->begin_locked(pkt.baudrate, 2048, 2048, TUNNEL_LOCK_KEY);
129
debug("begin_locked %u", unsigned(pkt.baudrate));
130
}
131
uart_monitor.baudrate = pkt.baudrate;
132
}
133
uart_monitor.uart->set_monitor_read_buffer(uart_monitor.buffer);
134
uart_monitor.last_request_ms = AP_HAL::millis();
135
136
// write to device
137
if (pkt.buffer.len > 0) {
138
if (uart_monitor.locked) {
139
debug("write_locked %u", unsigned(pkt.buffer.len));
140
uart_monitor.uart->write_locked(pkt.buffer.data, pkt.buffer.len, TUNNEL_LOCK_KEY);
141
} else {
142
uart_monitor.uart->write(pkt.buffer.data, pkt.buffer.len);
143
}
144
} else {
145
debug("locked keepalive");
146
}
147
}
148
149
/*
150
send tunnelled serial data
151
*/
152
void AP_Periph_FW::send_serial_monitor_data()
153
{
154
if (uart_monitor.uart == nullptr ||
155
uart_monitor.node_id == 0 ||
156
uart_monitor.buffer == nullptr) {
157
return;
158
}
159
const uint32_t last_req_ms = uart_monitor.last_request_ms;
160
const uint32_t now_ms = AP_HAL::millis();
161
if (now_ms - last_req_ms >= 3000) {
162
// stop sending and unlock, but don't release the buffer
163
if (uart_monitor.locked) {
164
debug("unlock");
165
uart_monitor.uart->lock_port(0, 0);
166
}
167
uart_monitor.uart = nullptr;
168
return;
169
}
170
if (uart_monitor.locked) {
171
/*
172
when the port is locked nobody is reading the uart so the
173
monitor doesn't fill. We read here to ensure it fills
174
*/
175
uint8_t buf[120];
176
for (uint8_t i=0; i<8; i++) {
177
if (uart_monitor.uart->read_locked(buf, sizeof(buf), TUNNEL_LOCK_KEY) <= 0) {
178
break;
179
}
180
}
181
}
182
uint8_t sends = 8;
183
while (uart_monitor.buffer->available() > 0 && sends-- > 0) {
184
uint32_t n;
185
const uint8_t *buf = uart_monitor.buffer->readptr(n);
186
if (n == 0) {
187
return;
188
}
189
// broadcast data as tunnel packets, can be used for uCenter debug and device fw update
190
uavcan_tunnel_Targetted pkt {};
191
n = MIN(n, sizeof(pkt.buffer.data));
192
pkt.target_node = uart_monitor.node_id;
193
pkt.protocol.protocol = uart_monitor.protocol;
194
pkt.buffer.len = n;
195
pkt.baudrate = uart_monitor.baudrate;
196
pkt.serial_id = uart_monitor.uart_num;
197
memcpy(pkt.buffer.data, buf, n);
198
199
uint8_t buffer[UAVCAN_TUNNEL_TARGETTED_MAX_SIZE];
200
const uint16_t total_size = uavcan_tunnel_Targetted_encode(&pkt, buffer, !canfdout());
201
202
debug("read %u", unsigned(n));
203
204
if (!canard_broadcast(UAVCAN_TUNNEL_TARGETTED_SIGNATURE,
205
UAVCAN_TUNNEL_TARGETTED_ID,
206
CANARD_TRANSFER_PRIORITY_MEDIUM,
207
&buffer[0],
208
total_size)) {
209
break;
210
}
211
uart_monitor.buffer->advance(n);
212
}
213
}
214
#endif // AP_UART_MONITOR_ENABLED
215
216