QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4SerialPortConfig.cc
Go to the documentation of this file.
2
3#include <algorithm>
4
5#include <QtCore/QHash>
6#include <QtCore/QRegularExpression>
7
9
10namespace {
11
12// Port index -> tag, from serial_ports in PX4's Tools/serial/generate_config.py. PX4 never reuses or changes an index.
13const QHash<int, QString>& portTags()
14{
15 static const QHash<int, QString> tags = {
16 {1, QStringLiteral("URT1")}, {2, QStringLiteral("URT2")}, {3, QStringLiteral("URT3")},
17 {4, QStringLiteral("URT4")}, {5, QStringLiteral("URT5")}, {6, QStringLiteral("URT6")},
18 {7, QStringLiteral("URT7")}, {8, QStringLiteral("URT8")}, {9, QStringLiteral("URT9")},
19 {101, QStringLiteral("TEL1")}, {102, QStringLiteral("TEL2")}, {103, QStringLiteral("TEL3")},
20 {104, QStringLiteral("TEL4")}, {201, QStringLiteral("GPS1")}, {202, QStringLiteral("GPS2")},
21 {203, QStringLiteral("GPS3")}, {300, QStringLiteral("RC")}, {301, QStringLiteral("WIFI")},
22 {401, QStringLiteral("EXT2")},
23 };
24 return tags;
25}
26
27constexpr int kFirstNonUartPortId = 101;
28constexpr int kRcPortId = 300;
29
30const Function* findFunction(const QList<Function>& functions, const QString& paramName)
31{
32 const auto it = std::find_if(functions.cbegin(), functions.cend(),
33 [&paramName](const Function& function) { return function.paramName == paramName; });
34 return (it == functions.cend()) ? nullptr : &(*it);
35}
36
37bool allowsPort(const Function& function, int portId)
38{
39 return std::any_of(function.ports.cbegin(), function.ports.cend(),
40 [portId](const Port& port) { return port.id == portId; });
41}
42
44const QRegularExpression* usualFunctionsRegex(int portId)
45{
46 static const QRegularExpression gpsRegex(QStringLiteral("^(GPS_\\d_CONFIG|SEP_PORT\\d_CFG)$"));
47 static const QRegularExpression telemetryRegex(QStringLiteral("^(MAV_\\d_CONFIG|UXRCE_DDS_CFG)$"));
48 static const QRegularExpression rcRegex(QStringLiteral("^RC_"));
49
50 const QString tag = portTags().value(portId);
51 if (tag.startsWith(QStringLiteral("TEL")) || (tag == QStringLiteral("WIFI")) || (portId == kEthernetPortId)) {
52 return &telemetryRegex;
53 }
54 if (tag.startsWith(QStringLiteral("GPS"))) {
55 return &gpsRegex;
56 }
57 if (tag == QStringLiteral("RC")) {
58 return &rcRegex;
59 }
60 return nullptr;
61}
62
63} // namespace
64
65bool isFunctionParam(const QVariantList& enumValues)
66{
67 bool hasDisabled = false;
68 bool hasNonUartPort = false;
69
70 for (const QVariant& enumValue : enumValues) {
71 bool ok = false;
72 const int value = enumValue.toInt(&ok);
73 if (!ok) {
74 return false;
75 }
76 if (value == kDisabledPortId) {
77 hasDisabled = true;
78 } else if (portTags().contains(value)) {
79 // UART indices 1-9 overlap ordinary small enums, so they don't identify a port selection on their own
80 hasNonUartPort = hasNonUartPort || (value >= kFirstNonUartPortId);
81 } else if (value != kEthernetPortId) {
82 return false;
83 }
84 }
85
86 return hasDisabled && hasNonUartPort;
87}
88
89QString baudParamName(int portId)
90{
91 const QString tag = portTags().value(portId);
92 return tag.isEmpty() ? QString() : QStringLiteral("SER_%1_BAUD").arg(tag);
93}
94
95QString functionLabel(const QString& paramName, const QString& shortDescription)
96{
97 static const QString prefix = QStringLiteral("Serial Configuration for ");
98
99 if (shortDescription.startsWith(prefix)) {
100 return shortDescription.mid(prefix.length());
101 }
102 return shortDescription.isEmpty() ? paramName : shortDescription;
103}
104
105Function makeFunction(const QString& paramName, const QString& shortDescription, const QVariantList& enumValues,
106 const QStringList& enumStrings, int value)
107{
108 Function function{
109 .paramName = paramName,
110 .label = functionLabel(paramName, shortDescription),
111 .ports = {},
112 };
113
114 for (qsizetype i = 0; i < enumValues.count(); i++) {
115 const int portId = enumValues[i].toInt();
116 if (portId != kDisabledPortId) {
117 function.ports.append({.id = portId, .label = enumStrings.value(i, QString::number(portId))});
118 }
119 }
120
121 if ((paramName == QStringLiteral("RC_PORT_CONFIG")) && allowsPort(function, kRcPortId)) {
122 function.defaultPortId = kRcPortId;
123 }
124 function.currentPortId = (value == kDisabledPortId) ? function.defaultPortId : value;
125
126 return function;
127}
128
129QList<Port> ports(const QList<Function>& functions)
130{
131 QList<Port> result;
132
133 for (const Function& function : functions) {
134 for (const Port& port : function.ports) {
135 const bool known = std::any_of(result.cbegin(), result.cend(),
136 [&port](const Port& existing) { return existing.id == port.id; });
137 if (!known) {
138 result.append(port);
139 }
140 }
141 }
142
143 std::sort(result.begin(), result.end(), [](const Port& a, const Port& b) { return a.id < b.id; });
144 return result;
145}
146
147QStringList functionsOnPort(const QList<Function>& functions, int portId)
148{
149 QStringList result;
150
151 if (portId == kDisabledPortId) {
152 return result;
153 }
154 for (const Function& function : functions) {
155 if (function.currentPortId == portId) {
156 result.append(function.paramName);
157 }
158 }
159
160 return result;
161}
162
163QList<Function> functionsForPort(const QList<Function>& functions, int portId)
164{
165 QList<Function> usual;
166 QList<Function> others;
167 const QRegularExpression* const usualRegex = usualFunctionsRegex(portId);
168
169 for (const Function& function : functions) {
170 if (!allowsPort(function, portId)) {
171 continue;
172 }
173 if (usualRegex && usualRegex->match(function.paramName).hasMatch()) {
174 usual.append(function);
175 } else {
176 others.append(function);
177 }
178 }
179
180 return usual + others;
181}
182
183QList<ParamWrite> assignPort(const QList<Function>& functions, int portId, const QString& functionParamName)
184{
185 QList<ParamWrite> writes;
186
187 if (!functionParamName.isEmpty()) {
188 const Function* const function = findFunction(functions, functionParamName);
189 if (!function || !allowsPort(*function, portId)) {
190 return writes;
191 }
192 }
193
194 if (!isSharedPort(portId)) {
195 for (const Function& function : functions) {
196 if ((function.currentPortId == portId) && (function.paramName != functionParamName) &&
197 (function.defaultPortId != portId)) {
198 writes.append({.paramName = function.paramName, .value = kDisabledPortId});
199 }
200 }
201 }
202
203 if (!functionParamName.isEmpty() && (findFunction(functions, functionParamName)->currentPortId != portId)) {
204 writes.append({.paramName = functionParamName, .value = portId});
205 }
206
207 return writes;
208}
209
210QList<ParamWrite> removeFromPort(const QList<Function>& functions, int portId, const QString& functionParamName)
211{
212 const Function* const function = findFunction(functions, functionParamName);
213 if (!function || (portId == kDisabledPortId) || (function->currentPortId != portId) ||
214 (function->defaultPortId == portId)) {
215 return {};
216 }
217 return {{.paramName = functionParamName, .value = kDisabledPortId}};
218}
219
220bool isSharedPort(int portId)
221{
222 return portId == kEthernetPortId;
223}
224
225QStringList optionParamNames(const QString& functionParamName, int portId)
226{
227 static const QRegularExpression mavlinkRegex(QStringLiteral("^MAV_(\\d)_CONFIG$"));
228 static const QRegularExpression gpsRegex(QStringLiteral("^GPS_(\\d)_CONFIG$"));
229
230 if (portId == kDisabledPortId) {
231 return {};
232 }
233
234 QStringList suffixes;
235 QString prefix;
236
237 if (const QRegularExpressionMatch mavlinkMatch = mavlinkRegex.match(functionParamName); mavlinkMatch.hasMatch()) {
238 prefix = QStringLiteral("MAV_%1_").arg(mavlinkMatch.captured(1));
239 suffixes = {QStringLiteral("MODE"), QStringLiteral("RATE"), QStringLiteral("FORWARD")};
240 if (portId == kEthernetPortId) {
241 suffixes += {QStringLiteral("UDP_PRT"), QStringLiteral("REMOTE_PRT"), QStringLiteral("BROADCAST")};
242 } else {
243 suffixes += QStringLiteral("FLOW_CTRL");
244 }
245 } else if (const QRegularExpressionMatch gpsMatch = gpsRegex.match(functionParamName); gpsMatch.hasMatch()) {
246 prefix = QStringLiteral("GPS_%1_").arg(gpsMatch.captured(1));
247 suffixes = {QStringLiteral("PROTOCOL")};
248 }
249
250 QStringList names;
251 for (const QString& suffix : suffixes) {
252 names.append(prefix + suffix);
253 }
254 return names;
255}
256
257} // namespace PX4SerialPortConfig
bool isFunctionParam(const QVariantList &enumValues)
true: the enum values are a PX4 serial port selection, which makes the parameter a port function
QList< ParamWrite > removeFromPort(const QList< Function > &functions, int portId, const QString &functionParamName)
Writes which take functionParamName off portId. Empty if it isn't on the port, or the port is its def...
bool isSharedPort(int portId)
constexpr int kDisabledPortId
QList< ParamWrite > assignPort(const QList< Function > &functions, int portId, const QString &functionParamName)
QStringList optionParamNames(const QString &functionParamName, int portId)
Extra settings shown for a function when it runs on portId.
constexpr int kEthernetPortId
Function makeFunction(const QString &paramName, const QString &shortDescription, const QVariantList &enumValues, const QStringList &enumStrings, int value)
value is the function parameter's value. currentPortId is defaultPortId when it is Disabled.
QStringList functionsOnPort(const QList< Function > &functions, int portId)
QString functionLabel(const QString &paramName, const QString &shortDescription)
Display label from a PX4 "Serial Configuration for <label>" short description.
QString baudParamName(int portId)
Baud rate parameter for a port, empty if the port has none (Ethernet, unknown ports).
QList< Port > ports(const QList< Function > &functions)
Every port any function can run on, in port id order.
QList< Function > functionsForPort(const QList< Function > &functions, int portId)
QList< Port > ports
Ports the function can run on, excluding Disabled.