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/QMap>
4#include <QtCore/QObject>
5#include <QtCore/QString>
6#include <QtCore/QTimer>
7#include <QtQmlIntegration/QtQmlIntegration>
8
9#include "Fact.h"
10#include "MAVLinkEnums.h"
11#include "QGCMAVLinkTypes.h"
12
13class QTextStream;
14
16class Vehicle;
17
18class ParameterManager : public QObject
19{
20 Q_OBJECT
21 QML_ELEMENT
22 QML_UNCREATABLE("")
23 Q_PROPERTY(bool parametersReady READ parametersReady NOTIFY parametersReadyChanged)
25 Q_PROPERTY(double loadProgress READ loadProgress NOTIFY loadProgressChanged)
26 Q_PROPERTY(bool pendingWrites READ pendingWrites NOTIFY pendingWritesChanged)
29
30public:
33
34 bool parametersReady() const { return _parametersReady; }
35 bool missingParameters() const { return _missingParameters; }
36 double loadProgress() const { return _loadProgress; }
37 bool parameterDownloadSkipped() const { return _parameterDownloadSkipped; }
38 void setParameterDownloadSkipped(bool skipped);
39
41 static QDir parameterCacheDir();
42
44 static QString parameterCacheFile(int vehicleId, int componentId);
45
46 void mavlinkMessageReceived(const mavlink_message_t &message);
47
48 QList<int> componentIds() const;
49
51 void refreshAllParameters(uint8_t componentID);
52 Q_INVOKABLE void refreshAllParameters() { refreshAllParameters(MAV_COMP_ID_ALL); }
53
57
59 void refreshParameter(int componentId, const QString &paramName);
60
62 void refreshParametersPrefix(int componentId, const QString &namePrefix);
63
70 void bulkRefresh(int componentId, const QStringList &names, bool notifyFailure = true);
71
74
78 bool parameterExists(int componentId, const QString &paramName) const;
79
81 QStringList parameterNames(int componentId) const;
82
87 Fact *getParameter(int componentId, const QString &paramName);
88
89 void writeParametersToStream(QTextStream &stream) const;
90
91 bool pendingWrites() const;
92
93#ifdef QGC_UNITTEST_BUILD
98 void setPendingWritesForTest(bool pending);
99#endif
100
101 Vehicle *vehicle();
102
103 static MAV_PARAM_TYPE factTypeToMavType(FactMetaData::ValueType_t factType);
104 static FactMetaData::ValueType_t mavTypeToFactType(MAV_PARAM_TYPE mavType);
105
106 static constexpr int defaultComponentId = -1;
107
108 // These are public for creating unit tests
109 static constexpr int kParamSetRetryCount = 2;
110 static constexpr int kParamRequestReadRetryCount = 2;
111 static constexpr int kWaitForParamValueAckMs = 1000;
112 static constexpr int kMaxInitialRequestListRetry = 4;
113 static constexpr int kHashCheckTimeoutMs = 1000;
114 static constexpr int kTestHashCheckTimeoutMs = 200;
115 static constexpr int kParamRequestListTimeoutMs = 5000;
116 static constexpr int kTestInitialRequestIntervalMs = 500;
119
120signals:
123 void loadProgressChanged(float value);
127 void factAdded(int componentId, Fact *fact);
128
129 // Internal signals — emitted by PARAM_SET / PARAM_REQUEST_READ state machines.
130 // Also consumed by BulkRefreshJob and unit tests.
131 void _paramSetSuccess(int componentId, const QString &paramName);
132 void _paramSetFailure(int componentId, const QString &paramName);
133 void _paramRequestReadSuccess(int componentId, const QString &paramName, int paramIndex);
134 void _paramRequestReadFailure(int componentId, const QString &paramName, int paramIndex);
135
136private slots:
137 void _factRawValueUpdated(const QVariant &rawValue);
138
139private:
141 void _handleParamValue(int componentId, const QString &parameterName, int parameterCount, int parameterIndex, MAV_PARAM_TYPE mavParamType, const QVariant &parameterValue);
143 void _mavlinkParamSet(int componentId, const QString &name, FactMetaData::ValueType_t valueType, const QVariant &rawValue);
144 void _waitingParamTimeout();
145 void _tryCacheLookup();
146 void _resetHashCheck();
147 void _startParameterDownload(uint8_t componentId);
148 void _hashCheckTimeout();
149 void _paramRequestListTimeout();
151 int _actualComponentId(int componentId) const;
152 void _mavlinkParamRequestRead(int componentId, const QString &paramName, int paramIndex, bool notifyFailure);
153 void _requestHashCheck(uint8_t componentId);
154 void _writeLocalParamCache(int vehicleId, int componentId);
155 void _tryCacheHashLoad(int vehicleId, int componentId, const QVariant &hashValue);
156 void _loadMetaData();
157 void _clearMetaData();
166 QString _remapParamNameToVersion(const QString &paramName) const;
167 bool _fillMavlinkParamUnion(FactMetaData::ValueType_t valueType, const QVariant &rawValue, mavlink_param_union_t &paramUnion) const;
168 bool _mavlinkParamUnionToVariant(const mavlink_param_union_t &paramUnion, QVariant &outValue) const;
170 void _loadOfflineEditingParams();
171 QString _logVehiclePrefix(int componentId) const;
172 void _setLoadProgress(double loadProgress);
176 bool _fillIndexBatchQueue(bool waitingParamTimeout);
177 void _updateProgressBar();
178 void _checkInitialLoadComplete();
179 void _ftpDownloadComplete(const QString &fileName, const QString &errorMsg);
180 void _ftpDownloadProgress(float progress);
183 bool _parseParamFile(const QString &filename);
184 void _incrementPendingWriteCount();
185 void _decrementPendingWriteCount();
186 QString _vehicleAndComponentString(int componentId) const;
187
188 static QVariant _stringToTypedVariant(const QString &string, FactMetaData::ValueType_t type, bool failOk = false);
189
190 Vehicle *_vehicle = nullptr;
191
192 QMap<int /* comp id */, QMap<QString /* parameter name */, Fact*>> _mapCompId2FactMap;
193
194 double _loadProgress = 0;
195 bool _parametersReady = false;
196 bool _parameterDownloadSkipped = false;
197 bool _missingParameters = false;
198 bool _initialLoadComplete = false;
199 bool _waitingForDefaultComponent = false;
200 bool _metaDataAddedToFacts = false;
201 bool _logReplay = false;
202 bool _hashCheckDone = false;
203 bool _cacheOnlyHashCheck = false;
204
205 typedef QPair<int /* FactMetaData::ValueType_t */, QVariant /* Fact::rawValue */> ParamTypeVal;
206 typedef QMap<QString /* parameter name */, ParamTypeVal> CacheMapName2ParamTypeVal;
207
208 QMap<int /* component id */, bool> _debugCacheCRC;
209 QMap<int /* component id */, CacheMapName2ParamTypeVal> _debugCacheMap;
210 QMap<int /* component id */, QMap<QString /* param name */, bool /* seen */>> _debugCacheParamSeen;
211
212 // Wait counts from previous parameter update cycle
213 int _prevWaitingReadParamIndexCount = 0;
214
215 bool _readParamIndexProgressActive = false;
216
217 static constexpr int _maxInitialRequestListRetry = kMaxInitialRequestListRetry;
218 int _initialRequestRetryCount = 0;
219 static constexpr int _maxInitialLoadRetrySingleParam = 5;
220 bool _disableAllRetries = false;
221 const int _waitForParamValueAckMs;
222
223 bool _indexBatchQueueActive = false;
224 QList<int> _indexBatchQueue;
225
226 QMap<int, int> _paramCountMap;
227 QMap<int, QMap<int, int>> _waitingReadParamIndexMap;
228 QMap<int, QList<int>> _failedReadParamIndexMap;
229
230 int _totalParamCount = 0;
231 int _pendingWritesCount = 0;
232
233 QTimer _hashCheckTimer;
234 QTimer _paramRequestListTimer;
235 QTimer _waitingParamTimeoutTimer;
236
237 Fact _defaultFact;
238
239 bool _tryftp = false;
240};
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:17
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
void refreshParametersPrefix(int componentId, const QString &namePrefix)
Request a refresh on all parameters that begin with the specified prefix.
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 kParamSetRetryCount
Number of retries for PARAM_SET.
void _paramSetSuccess(int componentId, const QString &paramName)
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 kTestMaxInitialRequestTimeMs
Maximum time to wait for initial request retries to exhaust in tests.
void _paramSetFailure(int componentId, const QString &paramName)