QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4SerialPortsController.cc
Go to the documentation of this file.
2
3#include <algorithm>
4
5#include "Fact.h"
7#include "ParameterManager.h"
9#include "Vehicle.h"
10
11namespace {
12
13QList<PX4SerialPortConfig::Function> makeFunctions(const QList<Fact*>& functionFacts)
14{
15 QList<PX4SerialPortConfig::Function> functions;
16
17 for (const Fact* fact : functionFacts) {
18 functions.append(PX4SerialPortConfig::makeFunction(fact->name(), fact->shortDescription(), fact->enumValues(),
19 fact->enumStrings(), fact->rawValue().toInt()));
20 }
21
22 return functions;
23}
24
25} // namespace
26
27PX4SerialPortRow::PX4SerialPortRow(int portId, const QString& label, const QStringList& functionParamNames,
28 const QStringList& functionLabels, QObject* parent)
29 : QObject(parent)
30 , _portId(portId)
31 , _label(label)
32 , _functionParamNames(functionParamNames)
33 , _functionLabels(functionLabels)
34 , _shared(PX4SerialPortConfig::isSharedPort(portId))
35{}
36
38{
39 return _functionParamNames.value(_noneAvailable ? functionIndex : (functionIndex + 1));
40}
41
43{
44 const int index = static_cast<int>(_functionParamNames.indexOf(functionParamName));
45 return (_noneAvailable || (index < 0)) ? index : (index - 1);
46}
47
48void PX4SerialPortRow::setNoneAvailable(bool noneAvailable)
49{
50 if (noneAvailable != _noneAvailable) {
51 _noneAvailable = noneAvailable;
54 }
55}
56
58{
59 if (functionIndex != _functionIndex) {
60 _functionIndex = functionIndex;
62 }
63}
64
66{
67 if (functionEnabled != _functionEnabled) {
68 _functionEnabled = functionEnabled;
70 }
71}
72
74{
75 if (conflictText != _conflictText) {
76 _conflictText = conflictText;
78 }
79}
80
82{
83 if (assignments != _assignments) {
84 _assignments = assignments;
85 emit assignmentsChanged();
86 }
87}
88
90{
91 if (baudFact != _baudFact) {
92 _baudFact = baudFact;
93 emit baudFactChanged();
94 }
95}
96
98{
99 if (baudLoading != _baudLoading) {
100 _baudLoading = baudLoading;
101 emit baudLoadingChanged();
102 }
103}
104
106{
107 if (optionFacts != _optionFacts) {
108 _optionFacts = optionFacts;
109 emit optionFactsChanged();
110 }
111}
112
114{
115 if (optionsLoading != _optionsLoading) {
116 _optionsLoading = optionsLoading;
118 }
119}
120
122{
123 if (rebootRequired != _rebootRequired) {
124 _rebootRequired = rebootRequired;
126 }
127}
128
130 : FactPanelController(parent)
131 , _ports(new QmlObjectListModel(this))
132{
133 if (!_vehicle) {
134 return;
135 }
136 _parameterManager = _vehicle->parameterManager();
137 if (!_parameterManager) {
138 return;
139 }
140
142 (void) connect(_parameterManager, &ParameterManager::factAdded, this, &PX4SerialPortsController::_factAdded);
143 (void) connect(_parameterManager, &ParameterManager::_paramRequestReadFailure, this,
144 &PX4SerialPortsController::_parameterReadFailed);
145
146 _functionFacts = PX4SerialPortsComponent::functionFacts(_parameterManager);
147 for (Fact* fact : _functionFacts) {
148 _watchFact(fact);
149 }
150
151 const QList<PX4SerialPortConfig::Function> functions = makeFunctions(_functionFacts);
152 for (const PX4SerialPortConfig::Port& port : PX4SerialPortConfig::ports(functions)) {
153 QStringList functionParamNames{QString()};
154 QStringList functionLabels{tr("None")};
155 for (const PX4SerialPortConfig::Function& function :
156 PX4SerialPortConfig::functionsForPort(functions, port.id)) {
157 functionParamNames.append(function.paramName);
158 functionLabels.append(function.label);
159 }
160 _ports->append(new PX4SerialPortRow(port.id, port.label, functionParamNames, functionLabels, this));
161 }
162
163 _update();
164}
165
166void PX4SerialPortsController::setPortFunction(int portId, int functionIndex)
167{
168 const PX4SerialPortRow* const row = _row(portId);
169 if (!_parameterManager || !row || row->shared()) {
170 return;
171 }
172
173 _writeParams(
174 PX4SerialPortConfig::assignPort(makeFunctions(_functionFacts), portId, row->functionParamName(functionIndex)));
175}
176
177void PX4SerialPortsController::setPortFunctionEnabled(int portId, int functionIndex, bool enabled)
178{
179 const PX4SerialPortRow* const row = _row(portId);
180 if (!_parameterManager || !row || !row->shared()) {
181 return;
182 }
183
184 const QList<PX4SerialPortConfig::Function> functions = makeFunctions(_functionFacts);
185 const QString functionParamName = row->functionParamName(functionIndex);
186 _writeParams(enabled ? PX4SerialPortConfig::assignPort(functions, portId, functionParamName)
187 : PX4SerialPortConfig::removeFromPort(functions, portId, functionParamName));
188}
189
191{
192 // An earlier write still retrying could land after the revert and undo it
193 if (!_parameterManager || _parameterManager->pendingWrites()) {
194 return;
195 }
196
197 // Each write updates the rows, which can add newly seen parameters to _initialValues
198 const QHash<QString, QVariant> initialValues = _initialValues;
199 for (auto it = initialValues.cbegin(); it != initialValues.cend(); ++it) {
200 if (_differsFromInitial(it.key())) {
201 _fact(it.key())->setRawValue(it.value());
202 }
203 }
204}
205
206void PX4SerialPortsController::_update()
207{
208 // The facts belong to the parameter manager, which may be gone if the vehicle disconnected
209 if (!_parameterManager) {
210 return;
211 }
212
213 const QList<PX4SerialPortConfig::Function> functions = makeFunctions(_functionFacts);
214 QList<QStringList> rowsParams; // Per row, parameters whose change marks it Reboot required
215
216 // Options can move to another row, so every row records the options it shows before any row is checked
217 for (int i = 0; i < _ports->count(); i++) {
218 PX4SerialPortRow* const row = _ports->value<PX4SerialPortRow*>(i);
219 const int portId = row->portId();
220 const QStringList onPort = PX4SerialPortConfig::functionsOnPort(functions, portId);
221 QStringList rowParams;
222
223 // Choosing "None" can't remove a function from its default port
224 const bool defaultFunctionHere =
225 std::any_of(functions.cbegin(), functions.cend(), [portId](const PX4SerialPortConfig::Function& f) {
226 return (f.defaultPortId == portId) && (f.currentPortId == portId);
227 });
228 row->setNoneAvailable(!defaultFunctionHere);
229 row->setFunctionIndex(onPort.isEmpty() ? 0 : row->functionIndexOf(onPort.first()));
230
231 QVariantList functionEnabled;
232 for (int functionIndex = 0; functionIndex < row->functionLabels().count(); functionIndex++) {
233 const QString paramName = row->functionParamName(functionIndex);
234 functionEnabled.append(paramName.isEmpty() ? onPort.isEmpty() : onPort.contains(paramName));
235 }
236 row->setFunctionEnabled(functionEnabled);
237
238 QStringList assignments;
239 for (const QString& paramName : onPort) {
240 const Fact* const fact = _fact(paramName);
241 const int value = fact ? fact->rawValue().toInt() : portId;
242 assignments.append((value == PX4SerialPortConfig::kDisabledPortId)
243 ? tr("%1 = %2 (default port)").arg(paramName).arg(value)
244 : QStringLiteral("%1 = %2").arg(paramName).arg(value));
245 }
246 row->setAssignments(assignments);
247
248 QString conflictText;
249 if ((onPort.count() > 1) && !row->shared()) {
250 const auto defaultsHere = std::find_if(
251 functions.cbegin(), functions.cend(), [&onPort, portId](const PX4SerialPortConfig::Function& f) {
252 return (f.defaultPortId == portId) && onPort.contains(f.paramName);
253 });
254 QStringList labels;
255 for (const QString& paramName : onPort) {
256 if ((defaultsHere == functions.cend()) || (paramName != defaultsHere->paramName)) {
257 labels.append(row->functionLabels().value(row->functionIndexOf(paramName)));
258 }
259 }
260 if (defaultsHere != functions.cend()) {
261 // Disabling the function doesn't stop it, so the usual "only one will start" doesn't apply
262 conflictText = tr("%1 always uses this port unless it is assigned to another port, so %2 will not work "
263 "here.")
264 .arg(defaultsHere->label, labels.join(", "));
265 } else {
266 conflictText =
267 (labels.count() == 2)
268 ? tr("%1 and %2 are both assigned to this port. Only one of them will start.")
269 .arg(labels[0], labels[1])
270 : tr("%1 are all assigned to this port. Only one of them will start.").arg(labels.join(", "));
271 }
272 }
273 row->setConflictText(conflictText);
274
275 Fact* baudFact = nullptr;
277 if (!baudParamName.isEmpty()) {
278 baudFact = _fact(baudParamName);
279 if (baudFact) {
280 rowParams.append(baudParamName);
281 } else {
282 _requestParameter(baudParamName);
283 }
284 }
285 row->setBaudFact(baudFact);
286 row->setBaudLoading(!baudFact && _pendingParams.contains(baudParamName));
287
288 QVariantList optionFacts;
289 bool optionsLoading = false;
290 const QStringList optionFunctions = (row->shared() || (onPort.count() == 1)) ? onPort : QStringList();
291 for (const QString& functionParamName : optionFunctions) {
292 for (const QString& paramName : PX4SerialPortConfig::optionParamNames(functionParamName, portId)) {
293 if (Fact* const fact = _fact(paramName)) {
294 optionFacts.append(QVariant::fromValue(fact));
295 _optionParamPorts.insert(paramName, portId);
296 } else {
297 _requestParameter(paramName);
298 optionsLoading = optionsLoading || _pendingParams.contains(paramName);
299 }
300 }
301 }
302 row->setOptionFacts(optionFacts);
303 row->setOptionsLoading(optionsLoading);
304
305 rowsParams.append(rowParams);
306 }
307
308 int changedPortCount = 0;
309 for (int i = 0; i < _ports->count(); i++) {
310 PX4SerialPortRow* const row = _ports->value<PX4SerialPortRow*>(i);
311 const int portId = row->portId();
312 QStringList rowParams = rowsParams[i];
313
314 for (auto it = _optionParamPorts.cbegin(); it != _optionParamPorts.cend(); ++it) {
315 if (it.value() == portId) {
316 rowParams.append(it.key());
317 }
318 }
319
320 for (const PX4SerialPortConfig::Function& function : functions) {
321 const int initialValue = _initialValues.value(function.paramName).toInt();
322 const int initialPortId =
323 (initialValue == PX4SerialPortConfig::kDisabledPortId) ? function.defaultPortId : initialValue;
324 if ((function.currentPortId == portId) || (initialPortId == portId)) {
325 rowParams.append(function.paramName);
326 }
327 }
328 const bool rebootRequired = std::any_of(rowParams.cbegin(), rowParams.cend(), [this](const QString& paramName) {
329 return _differsFromInitial(paramName);
330 });
331 row->setRebootRequired(rebootRequired);
332 if (rebootRequired) {
334 }
335 }
336
337 if (changedPortCount != _changedPortCount) {
338 _changedPortCount = changedPortCount;
340 }
341}
342
343void PX4SerialPortsController::_factAdded(int componentId, Fact* fact)
344{
345 if (!_vehicle || !fact || (componentId != _vehicle->defaultComponentId()) || !_pendingParams.remove(fact->name())) {
346 return;
347 }
348
349 // factAdded is emitted before the new fact has its value from the vehicle
350 (void) QMetaObject::invokeMethod(this, &PX4SerialPortsController::_update, Qt::QueuedConnection);
351}
352
353void PX4SerialPortsController::_parameterReadFailed(int componentId, const QString& paramName)
354{
355 if (!_vehicle || (componentId != _vehicle->defaultComponentId()) || !_pendingParams.remove(paramName)) {
356 return;
357 }
358
359 _update();
360}
361
362Fact* PX4SerialPortsController::_fact(const QString& paramName)
363{
364 if (!_parameterManager || !_parameterManager->parameterExists(ParameterManager::defaultComponentId, paramName)) {
365 return nullptr;
366 }
367
368 Fact* const fact = _parameterManager->getParameter(ParameterManager::defaultComponentId, paramName);
369 _watchFact(fact);
370 return fact;
371}
372
373void PX4SerialPortsController::_watchFact(Fact* fact)
374{
375 if (!fact || _watchedFacts.contains(fact)) {
376 return;
377 }
378
379 _watchedFacts.insert(fact);
380 _initialValues.insert(fact->name(), fact->rawValue());
381 (void) connect(fact, &Fact::rawValueChanged, this, &PX4SerialPortsController::_update);
382}
383
384void PX4SerialPortsController::_requestParameter(const QString& paramName)
385{
386 if (!_parameterManager || _requestedParams.contains(paramName)) {
387 return;
388 }
389
390 _requestedParams.insert(paramName);
391 _pendingParams.insert(paramName);
392 _parameterManager->refreshParameter(ParameterManager::defaultComponentId, paramName, false /* notifyFailure */);
393}
394
395bool PX4SerialPortsController::_differsFromInitial(const QString& paramName) const
396{
397 const auto initial = _initialValues.constFind(paramName);
398 if ((initial == _initialValues.cend()) || !_parameterManager ||
399 !_parameterManager->parameterExists(ParameterManager::defaultComponentId, paramName)) {
400 return false;
401 }
402
403 const Fact* const fact = _parameterManager->getParameter(ParameterManager::defaultComponentId, paramName);
404 return fact && (fact->rawValue() != *initial);
405}
406
407void PX4SerialPortsController::_writeParams(const QList<PX4SerialPortConfig::ParamWrite>& writes)
408{
409 for (const PX4SerialPortConfig::ParamWrite& write : writes) {
410 if (Fact* const fact = _fact(write.paramName)) {
411 fact->setRawValue(write.value);
412 }
413 }
414}
415
416PX4SerialPortRow* PX4SerialPortsController::_row(int portId) const
417{
418 for (int i = 0; i < _ports->count(); i++) {
419 PX4SerialPortRow* const row = _ports->value<PX4SerialPortRow*>(i);
420 if (row->portId() == portId) {
421 return row;
422 }
423 }
424 return nullptr;
425}
FactPanelController(QObject *parent=nullptr)
void setSuppressRebootMessage(bool suppress)
A Fact is used to hold a single value within the system.
Definition Fact.h:15
void rawValueChanged(const QVariant &value)
void setRawValue(const QVariant &value)
Definition Fact.cc:135
QString name() const
Definition Fact.h:124
QVariant rawValue() const
Definition Fact.h:87
One serial port row on the PX4 Ports page.
void setFunctionIndex(int functionIndex)
void assignmentsChanged()
void setRebootRequired(bool rebootRequired)
void optionsLoadingChanged()
QString functionParamName(int functionIndex) const
Function parameter for an index into functionLabels, empty for "None".
QVariantList optionFacts() const
void defaultFunctionOnPortChanged()
void baudLoadingChanged()
void optionFactsChanged()
void setOptionFacts(const QVariantList &optionFacts)
PX4SerialPortRow(int portId, const QString &label, const QStringList &functionParamNames, const QStringList &functionLabels, QObject *parent=nullptr)
true: several functions can run on the port at once, so each one is turned on or off separately
void rebootRequiredChanged()
QVariantList functionEnabled() const
int functionIndexOf(const QString &functionParamName) const
void setFunctionEnabled(const QVariantList &functionEnabled)
void functionLabelsChanged()
QStringList assignments() const
void setConflictText(const QString &conflictText)
QStringList functionLabels() const
void setNoneAvailable(bool noneAvailable)
void setBaudFact(Fact *baudFact)
void baudFactChanged()
void setBaudLoading(bool baudLoading)
void functionEnabledChanged()
void setOptionsLoading(bool optionsLoading)
void conflictTextChanged()
void setAssignments(const QStringList &assignments)
void functionIndexChanged()
static QList< Fact * > functionFacts(ParameterManager *parameterManager)
Parameters which select the serial port a function runs on. The page is only shown when there are som...
Q_INVOKABLE void setPortFunctionEnabled(int portId, int functionIndex, bool enabled)
Turns the function at functionIndex on or off on a shared port, leaving the port's other functions al...
PX4SerialPortsController(QObject *parent=nullptr)
Q_INVOKABLE void revertChanges()
Writes back the values each parameter had when the page opened. Ignored while parameter writes are pe...
Q_INVOKABLE void setPortFunction(int portId, int functionIndex)
void factAdded(int componentId, Fact *fact)
static constexpr int defaultComponentId
void _paramRequestReadFailure(int componentId, const QString &paramName, int paramIndex)
bool write(QByteArray &buffer, const GeoTagData &geotag)
Definition ExifParser.cc:32
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...
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.
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 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)