QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
ParameterManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QList>
4#include <QtCore/QMap>
5#include <QtCore/QObject>
6#include <QtCore/QPair>
7#include <QtCore/QSet>
8#include <QtCore/QString>
9#include <QtCore/QTimer>
10#include <QtQmlIntegration/QtQmlIntegration>
11
12#include "Fact.h"
13#include "MAVLinkEnums.h"
14#include "QGCMAVLinkTypes.h"
15
16class QTextStream;
17
19class Vehicle;
20
21class ParameterManager : public QObject
22{
23 Q_OBJECT
24 QML_ELEMENT
25 QML_UNCREATABLE("")
26 Q_PROPERTY(bool parametersReady READ parametersReady NOTIFY parametersReadyChanged)
28 Q_PROPERTY(double loadProgress READ loadProgress NOTIFY loadProgressChanged)
29 Q_PROPERTY(bool pendingWrites READ pendingWrites NOTIFY pendingWritesChanged)
30 Q_PROPERTY(
34
35public:
38
39 bool parametersReady() const { return _parametersReady; }
40 bool missingParameters() const { return _missingParameters; }
41 double loadProgress() const { return _loadProgress; }
42 bool parameterDownloadSkipped() const { return _parameterDownloadSkipped; }
43 void setParameterDownloadSkipped(bool skipped);
44
46 static QDir parameterCacheDir();
47
49 static QString parameterCacheFile(quint32 vehicleId, int componentId);
50
51 void mavlinkMessageReceived(const mavlink_message_t &message);
52
53 QList<int> componentIds() const;
54
56 void refreshAllParameters(uint8_t componentID);
57 Q_INVOKABLE void refreshAllParameters() { refreshAllParameters(MAV_COMP_ID_ALL); }
58
62
66 void refreshParameter(int componentId, const QString& paramName, bool notifyFailure = true);
67
69 void refreshParametersPrefix(int componentId, const QString &namePrefix);
70
77 void bulkRefresh(int componentId, const QStringList &names, bool notifyFailure = true);
78
81
85 bool parameterExists(int componentId, const QString &paramName) const;
86
88 QStringList parameterNames(int componentId) const;
89
94 Fact *getParameter(int componentId, const QString &paramName);
95
96 void writeParametersToStream(QTextStream &stream) const;
97
98 bool pendingWrites() const;
99
104
105#ifdef QGC_UNITTEST_BUILD
110 void setPendingWritesForTest(bool pending);
111#endif
112
113 Vehicle *vehicle();
114
115 static MAV_PARAM_TYPE factTypeToMavType(FactMetaData::ValueType_t factType);
116 static FactMetaData::ValueType_t mavTypeToFactType(MAV_PARAM_TYPE mavType);
117
118 static constexpr int defaultComponentId = -1;
119
120 // These are public for creating unit tests
121 static constexpr int kParamSetRetryCount = 2;
122 static constexpr int kParamRequestReadRetryCount = 2;
123 static constexpr int kWaitForParamValueAckMs = 1000;
124 static constexpr int kMaxInitialRequestListRetry = 4;
125 static constexpr int kMaxInitialLoadRetrySingleParam = 5;
126 static constexpr int kIndexBatchMaxOutstanding = 10;
127 static constexpr int kUnresponsiveMinOutstanding = 5;
128 static constexpr int kUnresponsiveSilentCycles = 2;
129 static constexpr int kHashCheckTimeoutMs = 1000;
130 static constexpr int kTestHashCheckTimeoutMs = 200;
131 static constexpr int kParamRequestListTimeoutMs = 5000;
132 static constexpr int kTestInitialRequestIntervalMs = 500;
135
136signals:
139 void loadProgressChanged(float value);
144 void factAdded(int componentId, Fact *fact);
145
146 // Internal signals — emitted by PARAM_SET / PARAM_REQUEST_READ state machines.
147 // Also consumed by BulkRefreshJob, PX4SerialPortsController (to end loading for an unanswered read) and unit tests.
148 void _paramSetSuccess(int componentId, const QString &paramName);
149 void _paramSetFailure(int componentId, const QString &paramName);
150 void _paramRequestReadSuccess(int componentId, const QString &paramName, int paramIndex);
151 void _paramRequestReadFailure(int componentId, const QString &paramName, int paramIndex);
152
153private slots:
154 void _factRawValueUpdated(const QVariant &rawValue);
155
156private:
158 void _handleParamValue(int componentId, const QString &parameterName, int parameterCount, int parameterIndex, MAV_PARAM_TYPE mavParamType, const QVariant &parameterValue);
160 void _mavlinkParamSet(int componentId, const QString &name, FactMetaData::ValueType_t valueType, const QVariant &rawValue);
161 void _waitingParamTimeout();
162 void _tryCacheLookup();
163 void _resetHashCheck();
164 void _startParameterDownload(uint8_t componentId);
165 void _hashCheckTimeout();
166 void _paramRequestListTimeout();
168 int _actualComponentId(int componentId) const;
169 void _mavlinkParamRequestRead(int componentId, const QString &paramName, int paramIndex, bool notifyFailure);
171 void _sendParamRequestReadIndex(int componentId, int paramIndex);
172 void _requestHashCheck(uint8_t componentId);
173 void _writeLocalParamCache(quint32 vehicleId, int componentId);
174 void _tryCacheHashLoad(quint32 vehicleId, int componentId, const QVariant& hashValue);
175 void _loadMetaData();
176 void _clearMetaData();
185 QString _remapParamNameToVersion(const QString &paramName) const;
186 bool _fillMavlinkParamUnion(FactMetaData::ValueType_t valueType, const QVariant &rawValue, mavlink_param_union_t &paramUnion) const;
187 bool _mavlinkParamUnionToVariant(const mavlink_param_union_t &paramUnion, QVariant &outValue) const;
189 void _loadOfflineEditingParams();
190 QString _logVehiclePrefix(int componentId) const;
191 void _setLoadProgress(double loadProgress);
195 bool _fillIndexBatchQueue(bool waitingParamTimeout);
198 void _giveUpOnUnresponsiveComponents();
199 void _updateProgressBar();
200 void _checkInitialLoadComplete();
202 void _checkOtherComponentsLoadComplete();
203 bool _anyComponentWaiting() const;
204 void _ftpDownloadComplete(const QString &fileName, const QString &errorMsg);
206 void _handleHeartbeat(int componentId);
207 void _sendParamRequestList(uint8_t componentId);
208 void _ftpDownloadProgress(float progress);
211 bool _parseParamFile(const QString &filename);
212 void _incrementPendingWriteCount();
213 void _decrementPendingWriteCount();
214 QString _vehicleAndComponentString(int componentId) const;
215
216 static QVariant _stringToTypedVariant(const QString &string, FactMetaData::ValueType_t type, bool failOk = false);
217
218 Vehicle *_vehicle = nullptr;
219
220 QMap<int /* comp id */, QMap<QString /* parameter name */, Fact*>> _mapCompId2FactMap;
221
222 double _loadProgress = 0;
223 bool _parametersReady = false;
224 bool _parameterDownloadSkipped = false;
225 bool _missingParameters = false;
226 bool _initialLoadComplete = false;
227 bool _otherComponentsReported = false;
228 bool _waitingForDefaultComponent = false;
229 bool _metaDataAddedToFacts = false;
230 bool _logReplay = false;
231 bool _hashCheckDone = false;
232 bool _cacheOnlyHashCheck = false;
233 int _rebootMessageSuppressCount = 0;
234
235 typedef QPair<int /* FactMetaData::ValueType_t */, QVariant /* Fact::rawValue */> ParamTypeVal;
236 typedef QMap<QString /* parameter name */, ParamTypeVal> CacheMapName2ParamTypeVal;
237
238 QMap<int /* component id */, bool> _debugCacheCRC;
239 QMap<int /* component id */, CacheMapName2ParamTypeVal> _debugCacheMap;
240 QMap<int /* component id */, QMap<QString /* param name */, bool /* seen */>> _debugCacheParamSeen;
241
242 // Wait counts from previous parameter update cycle
243 int _prevWaitingReadParamIndexCount = 0;
244
245 bool _readParamIndexProgressActive = false;
246 bool _refreshAllProgressActive = false;
247
248 static constexpr int _maxInitialRequestListRetry = kMaxInitialRequestListRetry;
249 int _initialRequestRetryCount = 0;
250 static constexpr int _maxInitialLoadRetrySingleParam = kMaxInitialLoadRetrySingleParam;
251 bool _disableAllRetries = false;
252 const int _waitForParamValueAckMs;
253
254 bool _indexBatchQueueActive = false;
255 QList<QPair<int, int>> _indexBatchQueue;
256
257 QMap<int, int> _paramCountMap;
258 QMap<int, QMap<int, int>> _waitingReadParamIndexMap;
259 QMap<int, QList<int>> _failedReadParamIndexMap;
260 QMap<int, int> _paramValuesReceivedThisCycle;
261 QMap<int, int> _silentCycleCount;
262
263 int _totalParamCount = 0;
264 int _pendingWritesCount = 0;
265
266 QTimer _hashCheckTimer;
267 QTimer _paramRequestListTimer;
268 QTimer _waitingParamTimeoutTimer;
269
270 Fact _defaultFact;
271
272 bool _tryftp = false;
273 bool _ftpDownloadInProgress = false;
274 bool _ftpParamsLoaded = false;
275 QSet<int> _heartbeatComponentIds;
276};
struct __mavlink_message mavlink_message_t
struct param_union mavlink_param_union_t
A Fact is used to hold a single value within the system.
Definition Fact.h:15
bool parameterExists(int componentId, const QString &paramName) const
void parameterDownloadSkippedChanged()
static constexpr int kWaitForParamValueAckMs
Time to wait for param value ack after set param.
void mavlinkMessageReceived(const mavlink_message_t &message)
static constexpr int kParamRequestListTimeoutMs
Timeout for PARAM_REQUEST_LIST response.
Fact * getParameter(int componentId, const QString &paramName)
static QString parameterCacheFile(quint32 vehicleId, int componentId)
static constexpr int kTestInitialRequestIntervalMs
void factAdded(int componentId, Fact *fact)
static FactMetaData::ValueType_t mavTypeToFactType(MAV_PARAM_TYPE mavType)
void refreshParameter(int componentId, const QString &paramName, bool notifyFailure=true)
void setParameterDownloadSkipped(bool skipped)
QList< int > componentIds() const
static constexpr int kMaxInitialLoadRetrySingleParam
Maximum index re-read attempts for a single param during initial load.
void refreshParametersPrefix(int componentId, const QString &namePrefix)
Request a refresh on all parameters that begin with the specified prefix.
void initialParametersRequestFailed()
Vehicle never responded to PARAM_REQUEST_LIST, all retries exhausted.
void parametersReadyChanged(bool parametersReady)
ParameterManager(Vehicle *vehicle)
double loadProgress() const
void bulkRefresh(int componentId, const QStringList &names, bool notifyFailure=true)
bool pendingWrites() const
static QDir parameterCacheDir()
static constexpr int kHashCheckTimeoutMs
Timeout for standalone _HASH_CHECK request.
void missingParametersChanged(bool missingParameters)
bool parametersReady() const
static constexpr int defaultComponentId
static constexpr int kTestHashCheckTimeoutMs
Shortened _HASH_CHECK timeout in unit tests (MockLink responds instantly).
QStringList parameterNames(int componentId) const
Returns all parameter names.
void _paramRequestReadFailure(int componentId, const QString &paramName, int paramIndex)
void _paramRequestReadSuccess(int componentId, const QString &paramName, int paramIndex)
static constexpr int kIndexBatchMaxOutstanding
Maximum index re-reads in flight at once.
static constexpr int kParamSetRetryCount
Number of retries for PARAM_SET.
void _paramSetSuccess(int componentId, const QString &paramName)
static constexpr int kUnresponsiveMinOutstanding
Minimum in-flight re-reads to a component before a silent cycle counts against it.
static constexpr int kMaxInitialRequestListRetry
Maximum retries for initial parameter request list.
void loadProgressChanged(float value)
void cacheCheckOnlyFailed()
void pendingWritesChanged(bool pendingWrites)
static MAV_PARAM_TYPE factTypeToMavType(FactMetaData::ValueType_t factType)
static constexpr int kParamRequestReadRetryCount
Number of retries for PARAM_REQUEST_READ.
friend class ParameterEditorController
< true: There are still pending write updates against the vehicle
bool missingParameters() const
void resetAllToVehicleConfiguration()
bool parameterDownloadSkipped() const
Q_INVOKABLE void refreshAllParameters()
void writeParametersToStream(QTextStream &stream) const
static constexpr int kUnresponsiveSilentCycles
Consecutive silent cycles before a non-default component is given up on.
static constexpr int kTestMaxInitialRequestTimeMs
Maximum time to wait for initial request retries to exhaust in tests.
void _paramSetFailure(int componentId, const QString &paramName)