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/QString>
8#include <QtCore/QTimer>
9#include <QtQmlIntegration/QtQmlIntegration>
10
11#include "Fact.h"
12#include "MAVLinkEnums.h"
13#include "QGCMAVLinkTypes.h"
14
15class QTextStream;
16
18class Vehicle;
19
20class ParameterManager : public QObject
21{
22 Q_OBJECT
23 QML_ELEMENT
24 QML_UNCREATABLE("")
25 Q_PROPERTY(bool parametersReady READ parametersReady NOTIFY parametersReadyChanged)
27 Q_PROPERTY(double loadProgress READ loadProgress NOTIFY loadProgressChanged)
28 Q_PROPERTY(bool pendingWrites READ pendingWrites NOTIFY pendingWritesChanged)
29 Q_PROPERTY(
33
34public:
37
38 bool parametersReady() const { return _parametersReady; }
39 bool missingParameters() const { return _missingParameters; }
40 double loadProgress() const { return _loadProgress; }
41 bool parameterDownloadSkipped() const { return _parameterDownloadSkipped; }
42 void setParameterDownloadSkipped(bool skipped);
43
45 static QDir parameterCacheDir();
46
48 static QString parameterCacheFile(int vehicleId, int componentId);
49
50 void mavlinkMessageReceived(const mavlink_message_t &message);
51
52 QList<int> componentIds() const;
53
55 void refreshAllParameters(uint8_t componentID);
56 Q_INVOKABLE void refreshAllParameters() { refreshAllParameters(MAV_COMP_ID_ALL); }
57
61
63 void refreshParameter(int componentId, const QString &paramName);
64
66 void refreshParametersPrefix(int componentId, const QString &namePrefix);
67
74 void bulkRefresh(int componentId, const QStringList &names, bool notifyFailure = true);
75
78
82 bool parameterExists(int componentId, const QString &paramName) const;
83
85 QStringList parameterNames(int componentId) const;
86
91 Fact *getParameter(int componentId, const QString &paramName);
92
93 void writeParametersToStream(QTextStream &stream) const;
94
95 bool pendingWrites() const;
96
97#ifdef QGC_UNITTEST_BUILD
102 void setPendingWritesForTest(bool pending);
103#endif
104
105 Vehicle *vehicle();
106
107 static MAV_PARAM_TYPE factTypeToMavType(FactMetaData::ValueType_t factType);
108 static FactMetaData::ValueType_t mavTypeToFactType(MAV_PARAM_TYPE mavType);
109
110 static constexpr int defaultComponentId = -1;
111
112 // These are public for creating unit tests
113 static constexpr int kParamSetRetryCount = 2;
114 static constexpr int kParamRequestReadRetryCount = 2;
115 static constexpr int kWaitForParamValueAckMs = 1000;
116 static constexpr int kMaxInitialRequestListRetry = 4;
117 static constexpr int kMaxInitialLoadRetrySingleParam = 5;
118 static constexpr int kIndexBatchMaxOutstanding = 10;
119 static constexpr int kUnresponsiveMinOutstanding = 5;
120 static constexpr int kUnresponsiveSilentCycles = 2;
121 static constexpr int kHashCheckTimeoutMs = 1000;
122 static constexpr int kTestHashCheckTimeoutMs = 200;
123 static constexpr int kParamRequestListTimeoutMs = 5000;
124 static constexpr int kTestInitialRequestIntervalMs = 500;
127
128signals:
131 void loadProgressChanged(float value);
136 void factAdded(int componentId, Fact *fact);
137
138 // Internal signals — emitted by PARAM_SET / PARAM_REQUEST_READ state machines.
139 // Also consumed by BulkRefreshJob and unit tests.
140 void _paramSetSuccess(int componentId, const QString &paramName);
141 void _paramSetFailure(int componentId, const QString &paramName);
142 void _paramRequestReadSuccess(int componentId, const QString &paramName, int paramIndex);
143 void _paramRequestReadFailure(int componentId, const QString &paramName, int paramIndex);
144
145private slots:
146 void _factRawValueUpdated(const QVariant &rawValue);
147
148private:
150 void _handleParamValue(int componentId, const QString &parameterName, int parameterCount, int parameterIndex, MAV_PARAM_TYPE mavParamType, const QVariant &parameterValue);
152 void _mavlinkParamSet(int componentId, const QString &name, FactMetaData::ValueType_t valueType, const QVariant &rawValue);
153 void _waitingParamTimeout();
154 void _tryCacheLookup();
155 void _resetHashCheck();
156 void _startParameterDownload(uint8_t componentId);
157 void _hashCheckTimeout();
158 void _paramRequestListTimeout();
160 int _actualComponentId(int componentId) const;
161 void _mavlinkParamRequestRead(int componentId, const QString &paramName, int paramIndex, bool notifyFailure);
163 void _sendParamRequestReadIndex(int componentId, int paramIndex);
164 void _requestHashCheck(uint8_t componentId);
165 void _writeLocalParamCache(int vehicleId, int componentId);
166 void _tryCacheHashLoad(int vehicleId, int componentId, const QVariant &hashValue);
167 void _loadMetaData();
168 void _clearMetaData();
177 QString _remapParamNameToVersion(const QString &paramName) const;
178 bool _fillMavlinkParamUnion(FactMetaData::ValueType_t valueType, const QVariant &rawValue, mavlink_param_union_t &paramUnion) const;
179 bool _mavlinkParamUnionToVariant(const mavlink_param_union_t &paramUnion, QVariant &outValue) const;
181 void _loadOfflineEditingParams();
182 QString _logVehiclePrefix(int componentId) const;
183 void _setLoadProgress(double loadProgress);
187 bool _fillIndexBatchQueue(bool waitingParamTimeout);
190 void _giveUpOnUnresponsiveComponents();
191 void _updateProgressBar();
192 void _checkInitialLoadComplete();
194 void _checkOtherComponentsLoadComplete();
195 bool _anyComponentWaiting() const;
196 void _ftpDownloadComplete(const QString &fileName, const QString &errorMsg);
197 void _ftpDownloadProgress(float progress);
200 bool _parseParamFile(const QString &filename);
201 void _incrementPendingWriteCount();
202 void _decrementPendingWriteCount();
203 QString _vehicleAndComponentString(int componentId) const;
204
205 static QVariant _stringToTypedVariant(const QString &string, FactMetaData::ValueType_t type, bool failOk = false);
206
207 Vehicle *_vehicle = nullptr;
208
209 QMap<int /* comp id */, QMap<QString /* parameter name */, Fact*>> _mapCompId2FactMap;
210
211 double _loadProgress = 0;
212 bool _parametersReady = false;
213 bool _parameterDownloadSkipped = false;
214 bool _missingParameters = false;
215 bool _initialLoadComplete = false;
216 bool _otherComponentsReported = false;
217 bool _waitingForDefaultComponent = false;
218 bool _metaDataAddedToFacts = false;
219 bool _logReplay = false;
220 bool _hashCheckDone = false;
221 bool _cacheOnlyHashCheck = false;
222
223 typedef QPair<int /* FactMetaData::ValueType_t */, QVariant /* Fact::rawValue */> ParamTypeVal;
224 typedef QMap<QString /* parameter name */, ParamTypeVal> CacheMapName2ParamTypeVal;
225
226 QMap<int /* component id */, bool> _debugCacheCRC;
227 QMap<int /* component id */, CacheMapName2ParamTypeVal> _debugCacheMap;
228 QMap<int /* component id */, QMap<QString /* param name */, bool /* seen */>> _debugCacheParamSeen;
229
230 // Wait counts from previous parameter update cycle
231 int _prevWaitingReadParamIndexCount = 0;
232
233 bool _readParamIndexProgressActive = false;
234 bool _refreshAllProgressActive = false;
235
236 static constexpr int _maxInitialRequestListRetry = kMaxInitialRequestListRetry;
237 int _initialRequestRetryCount = 0;
238 static constexpr int _maxInitialLoadRetrySingleParam = kMaxInitialLoadRetrySingleParam;
239 bool _disableAllRetries = false;
240 const int _waitForParamValueAckMs;
241
242 bool _indexBatchQueueActive = false;
243 QList<QPair<int, int>> _indexBatchQueue;
244
245 QMap<int, int> _paramCountMap;
246 QMap<int, QMap<int, int>> _waitingReadParamIndexMap;
247 QMap<int, QList<int>> _failedReadParamIndexMap;
248 QMap<int, int> _paramValuesReceivedThisCycle;
249 QMap<int, int> _silentCycleCount;
250
251 int _totalParamCount = 0;
252 int _pendingWritesCount = 0;
253
254 QTimer _hashCheckTimer;
255 QTimer _paramRequestListTimer;
256 QTimer _waitingParamTimeoutTimer;
257
258 Fact _defaultFact;
259
260 bool _tryftp = false;
261 bool _ftpDownloadInProgress = false;
262};
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 constexpr int kTestInitialRequestIntervalMs
void factAdded(int componentId, Fact *fact)
static FactMetaData::ValueType_t mavTypeToFactType(MAV_PARAM_TYPE mavType)
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)
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 refreshParameter(int componentId, const QString &paramName)
Request a refresh on the specific parameter.
void pendingWritesChanged(bool pendingWrites)
static MAV_PARAM_TYPE factTypeToMavType(FactMetaData::ValueType_t factType)
static constexpr int kParamRequestReadRetryCount
Number of retries for PARAM_REQUEST_READ.
bool missingParameters() const
void resetAllToVehicleConfiguration()
bool parameterDownloadSkipped() const
Q_INVOKABLE void refreshAllParameters()
static QString parameterCacheFile(int vehicleId, int componentId)
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)