QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
CompInfoParam.cc
Go to the documentation of this file.
1#include "CompInfoParam.h"
2#include "FirmwarePlugin.h"
3#include "JsonParsing.h"
5#include "ParameterMetaData.h"
7#include "Vehicle.h"
8
9#include <QtCore/QJsonArray>
10#include <QtCore/QJsonDocument>
11#include <QtCore/QRegularExpression>
12
13QGC_LOGGING_CATEGORY(CompInfoParamLog, "ComponentInformation.CompInfoParam")
14
15CompInfoParam::CompInfoParam(uint8_t compId_, Vehicle *vehicle_, QObject *parent)
16 : CompInfo(COMP_METADATA_TYPE_PARAMETER, compId_, vehicle_, parent)
17{
18}
19
20void CompInfoParam::setJson(const QString &metadataJsonFileName)
21{
22 if (metadataJsonFileName.isEmpty()) {
23 return;
24 }
25
26 QString errorString;
27 QJsonDocument jsonDoc;
28
29 if (!JsonParsing::isJsonFile(metadataJsonFileName, jsonDoc, errorString)) {
30 qCWarning(CompInfoParamLog) << "Metadata json file open failed: compid:" << compId << errorString;
31 return;
32 }
33
34 QString schemaError;
35 if (!JsonSchemaValidator::validate(jsonDoc, QStringLiteral(":/json/component_metadata/parameter.schema.json"), schemaError)) {
36 qCWarning(CompInfoParamLog) << "Metadata json schema validation failed: compid:" << compId << schemaError;
37 }
38
39 const QJsonObject jsonObj = jsonDoc.object();
40 const QList<JsonParsing::KeyValidateInfo> keyInfoList = {
41 {JsonParsing::jsonVersionKey, QJsonValue::Double, true},
42 {kJsonParametersKey, QJsonValue::Array, true},
43 };
44 if (!JsonParsing::validateKeys(jsonObj, keyInfoList, errorString)) {
45 qCWarning(CompInfoParamLog) << "Metadata json validation failed: compid:" << compId << errorString;
46 return;
47 }
48
49 if (jsonObj[JsonParsing::jsonVersionKey].toInt() != 1) {
50 qCWarning(CompInfoParamLog) << "Metadata json unsupported version" << jsonObj[JsonParsing::jsonVersionKey].toInt();
51 return;
52 }
53
54 _noJsonMetadata = false;
55
56 const QJsonArray parameters = jsonObj[kJsonParametersKey].toArray();
57 const QString escapedTag = QRegularExpression::escape(kIndexedNameTag);
58
59 for (const QJsonValue &parameterValue : parameters) {
60 if (!parameterValue.isObject()) {
61 qCWarning(CompInfoParamLog) << "Metadata json read failed: compid:" << compId << "parameters array contains non-object";
62 return;
63 }
64
65 const QJsonObject parameterJson = parameterValue.toObject();
66 const QString name = parameterJson.value(u"name").toString();
67 if (!name.contains(kIndexedNameTag)) {
68 // Replacement metadata must win over an entry resolved earlier; Facts keep the old object
69 (void) _nameToMetaDataMap.remove(name);
70 _paramJsonByName.insert(name, parameterJson);
71 continue;
72 }
73
74 FactMetaData* newMetaData =
76 QString regexPattern = QRegularExpression::escape(newMetaData->name());
77 regexPattern.replace(escapedTag, QStringLiteral("(\\d+)"));
78 _indexedNameMetaDataList.append({QRegularExpression(QStringLiteral("^%1$").arg(regexPattern)), newMetaData});
79 }
80}
81
83{
84 if (FactMetaData *cached = _nameToMetaDataMap.value(name)) {
85 return cached;
86 }
87
88 FactMetaData *factMetaData = _resolveMetaData(name, valueType);
89 _nameToMetaDataMap[name] = factMetaData;
90 return factMetaData;
91}
92
93FactMetaData *CompInfoParam::_resolveMetaData(const QString &name, FactMetaData::ValueType_t valueType)
94{
95 if (_noJsonMetadata) {
96 // No vehicle-provided metadata — use firmware-bundled metadata
97 if (ParameterMetaData *fwMeta = _getParameterMetaData()) {
98 return fwMeta->getMetaDataForFact(name, valueType);
99 }
100 } else {
101 // Vehicle provided JSON metadata — check exact match then indexed patterns
102 if (FactMetaData *found = _lookupJsonMetaData(name)) {
103 return found;
104 }
105 }
106
107 // Generic fallback
108 auto *factMetaData = new FactMetaData(valueType, this);
109 const int sep = name.indexOf('_');
110 if (sep > 0) {
111 factMetaData->setGroup(name.left(sep));
112 }
113 if (compId != MAV_COMP_ID_AUTOPILOT1) {
114 factMetaData->setCategory(tr("Component %1").arg(compId));
115 }
116 return factMetaData;
117}
118
119FactMetaData *CompInfoParam::_lookupJsonMetaData(const QString &name)
120{
121 const QJsonObject parameterJson = _paramJsonByName.take(name);
122 if (!parameterJson.isEmpty()) {
123 FactMetaData* factMetaData =
125 if (!factMetaData->name().isEmpty()) {
126 return factMetaData;
127 }
128 delete factMetaData;
129 }
130
131 // Try indexed name patterns (e.g. "CAL_GYRO{n}_ID" matches "CAL_GYRO0_ID")
132 for (const auto &[regex, templateMeta] : _indexedNameMetaDataList) {
133 const QRegularExpressionMatch match = regex.match(name);
134 if (match.hasMatch()) {
135 auto *factMetaData = new FactMetaData(*templateMeta, this);
136 factMetaData->setName(name);
137
138 const QString index = match.captured(1);
139 QString desc = factMetaData->shortDescription();
140 desc.replace(kIndexedNameTag, index);
141 factMetaData->setShortDescription(desc);
142
143 desc = factMetaData->longDescription();
144 desc.replace(kIndexedNameTag, index);
145 factMetaData->setLongDescription(desc);
146 return factMetaData;
147 }
148 }
149
150 return nullptr;
151}
152
153ParameterMetaData *CompInfoParam::_getParameterMetaData()
154{
155 if (!_parameterMetaData && compId == MAV_COMP_ID_AUTOPILOT1) {
156 _parameterMetaData = vehicle->firmwarePlugin()->loadParameterMetaData(vehicle);
157 if (_parameterMetaData) {
158 _parameterMetaData->setParent(this);
159 }
160 }
161
162 return _parameterMetaData;
163}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void setJson(const QString &metadataJsonFileName) override
CompInfoParam(uint8_t compId_, Vehicle *vehicle_, QObject *parent=nullptr)
FactMetaData * factMetaDataForName(const QString &name, FactMetaData::ValueType_t valueType)
Vehicle *const vehicle
Definition CompInfo.h:37
CompInfo(COMP_METADATA_TYPE type_, uint8_t compId_, Vehicle *vehicle_, QObject *parent=nullptr)
Definition CompInfo.cc:3
const uint8_t compId
Definition CompInfo.h:38
Holds the meta data associated with a Fact.
static FactMetaData * createFromJsonObject(const QJsonObject &json, const QMap< QString, QString > &defineMap, QObject *metaDataParent)
void setShortDescription(const QString &shortDescription)
void setName(const QString &name)
void setLongDescription(const QString &longDescription)
QString shortDescription() const
QString longDescription() const
QString name() const
static const FactMetaData::DefineMap_t kEmptyDefines
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.
bool isJsonFile(const QByteArray &bytes, QJsonDocument &jsonDoc, QString &errorString)
Determines whether an in-memory byte buffer contains parseable JSON content.
constexpr const char * jsonVersionKey
Definition JsonParsing.h:14
bool validate(const QJsonDocument &doc, const QString &schemaResourcePath, QString &errorString)