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/networking_passthru.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
#include "AP_Periph.h"
17
18
#if defined(HAL_PERIPH_ENABLE_NETWORKING) && HAL_PERIPH_NETWORK_NUM_PASSTHRU > 0
19
20
#include <AP_SerialManager/AP_SerialManager.h>
21
22
const AP_Param::GroupInfo Networking_Periph::Passthru::var_info[] = {
23
// @Param: ENABLE
24
// @DisplayName: Enable Passthrough
25
// @Description: Enable Passthrough of any UART, Network, or CAN ports to any UART, Network, or CAN ports.
26
// @Values: 0:Disabled, 1:Enabled
27
// @RebootRequired: True
28
// @User: Advanced
29
AP_GROUPINFO_FLAGS("ENABLE", 1, Networking_Periph::Passthru, enabled, 0, AP_PARAM_FLAG_ENABLE),
30
31
// @Param: EP1
32
// @DisplayName: Endpoint 1
33
// @Description: Passthrough Endpoint 1. This can be a serial port UART, a Network port, or a CAN port. The selected port will route to Endport 2.
34
// @Values: -1:Disabled, 0:Serial0(usually USB), 1:Serial1, 2:Serial2, 3:Serial3, 4:Serial4, 5:Serial5, 6:Serial6, 7:Serial7, 8:Serial8, 9:Serial9, 21:Network Port1, 22:Network Port2, 23:Network Port3, 24:Network Port4, 25:Network Port5, 26:Network Port6, 27:Network Port7, 28:Network Port8, 29:Network Port9, 41:CAN1 Port1, 42:CAN1 Port2, 43:CAN1 Port3, 44:CAN1 Port4, 45:CAN1 Port5, 46:CAN1 Port6, 47:CAN1 Port7, 48:CAN1 Port8, 49:CAN1 Port9, 51:CAN2 Port1, 52:CAN2 Port2, 53:CAN2 Port3, 54:CAN2 Port4, 55:CAN2 Port5, 56:CAN2 Port6, 57:CAN2 Port7, 58:CAN2 Port8, 59:CAN2 Port9
35
// @RebootRequired: True
36
// @User: Advanced
37
AP_GROUPINFO("EP1", 2, Networking_Periph::Passthru, ep1, -1),
38
39
// @Param: EP2
40
// @DisplayName: Endpoint 2
41
// @Description: Passthrough Endpoint 2. This can be a serial port UART, a Network port, or a CAN port. The selected port will route to Endport 1.
42
// @CopyFieldsFrom: NET_PASS1_EP1
43
AP_GROUPINFO("EP2", 3, Networking_Periph::Passthru, ep2, -1),
44
45
// @Param: BAUD1
46
// @DisplayName: Endpoint 1 Baud Rate
47
// @Description: The baud rate used for Endpoint 1. Only applies to serial ports.
48
// @CopyFieldsFrom: SERIAL1_BAUD
49
AP_GROUPINFO("BAUD1", 4, Networking_Periph::Passthru, baud1, 115200),
50
51
// @Param: BAUD2
52
// @DisplayName: Endpoint 2 Baud Rate
53
// @Description: The baud rate used for Endpoint 2. Only applies to serial ports.
54
// @CopyFieldsFrom: SERIAL1_BAUD
55
AP_GROUPINFO("BAUD2", 5, Networking_Periph::Passthru, baud2, 115200),
56
57
// @Param: OPT1
58
// @DisplayName: Serial Port Options EP1
59
// @Description: Control over UART options for Endpoint 1. Only applies to serial ports.
60
// @CopyFieldsFrom: SERIAL1_OPTIONS
61
AP_GROUPINFO("OPT1", 6, Networking_Periph::Passthru, options1, 0),
62
63
// @Param: OPT2
64
// @DisplayName: Serial Port Options EP2
65
// @Description: Control over UART options for Endpoint 2. Only applies to serial ports.
66
// @CopyFieldsFrom: SERIAL1_OPTIONS
67
AP_GROUPINFO("OPT2", 7, Networking_Periph::Passthru, options2, 0),
68
69
AP_GROUPEND
70
};
71
72
void Networking_Periph::Passthru::init()
73
{
74
if (enabled == 0) {
75
// Feature is disabled
76
return;
77
}
78
79
if (port1 != nullptr || port2 != nullptr) {
80
// The ports have already been initialized, nothing to do.
81
return;
82
}
83
84
if (ep1 <= -1 || ep2 <= -1 || ep1 == ep2) {
85
// end points are not set or are the same. Can't route to self
86
return;
87
}
88
89
port1 = AP::serialmanager().get_serial_by_id(ep1);
90
port2 = AP::serialmanager().get_serial_by_id(ep2);
91
92
if (port1 != nullptr && port2 != nullptr) {
93
port1->set_options(options1);
94
port1->begin(baud1);
95
96
port2->set_options(options2);
97
port2->begin(baud2);
98
}
99
}
100
101
void Networking_Periph::Passthru::update()
102
{
103
if (enabled == 0 || port1 == nullptr || port2 == nullptr) {
104
return;
105
}
106
107
// Fastest possible connection is 3Mbps serial port, which is roughly 300KB/s payload and we service this at <= 1kHz
108
// Raising this any higher just causes excess stack usage which never gets used.
109
uint8_t buf[300];
110
111
// read from port1, and write to port2
112
auto avail = port1->available();
113
if (avail > 0) {
114
auto space = port2->txspace();
115
const uint32_t n = MIN(space, sizeof(buf));
116
const auto nbytes = port1->read(buf, n);
117
if (nbytes > 0) {
118
port2->write(buf, nbytes);
119
}
120
}
121
122
// read from port2, and write to port1
123
avail = port2->available();
124
if (avail > 0) {
125
auto space = port1->txspace();
126
const uint32_t n = MIN(space, sizeof(buf));
127
const auto nbytes = port2->read(buf, n);
128
if (nbytes > 0) {
129
port1->write(buf, nbytes);
130
}
131
}
132
}
133
134
#endif // defined(HAL_PERIPH_ENABLE_NETWORKING) && HAL_PERIPH_NETWORK_NUM_PASSTHRU > 0
135
136
137