QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4AirframeLoader.cc
Go to the documentation of this file.
1#include "PX4AirframeLoader.h"
2#include "AppMessages.h"
5#include "AutoPilotPlugin.h"
6
7#include <QFile>
8#include <QFileInfo>
9#include <QDir>
10#include <QDebug>
11#include <QtCore/QXmlStreamReader>
12#include <QtCore/QSettings>
13
14QGC_LOGGING_CATEGORY(PX4AirframeLoaderLog, "AutoPilotPlugins.PX4AirframeLoader")
15
16bool PX4AirframeLoader::_airframeMetaDataLoaded = false;
17
19{
20 Q_UNUSED(autopilot);
21 Q_UNUSED(parent);
22}
23
25{
26 QSettings settings;
27 QDir parameterDir = QFileInfo(settings.fileName()).dir();
28 return parameterDir.filePath("PX4AirframeFactMetaData.xml");
29}
30
35{
36 if (_airframeMetaDataLoaded) {
37 return;
38 }
39
40 qCDebug(PX4AirframeLoaderLog) << "Loading PX4 airframe fact meta data";
41
42 if (AirframeComponentAirframes::get().count() != 0) {
43 qCWarning(PX4AirframeLoaderLog) << "Internal error";
44 return;
45 }
46
47 QString airframeFilename;
48
49 // We want unit test builds to always use the resource based meta data to provide repeatable results
50 if (!QGC::runningUnitTests()) {
51 // First look for meta data that comes from a firmware download. Fall back to resource if not there.
52 airframeFilename = aiframeMetaDataFile();
53 }
54 if (airframeFilename.isEmpty() || !QFile(airframeFilename).exists()) {
55 airframeFilename = ":/AutoPilotPlugins/PX4/AirframeFactMetaData.xml";
56 }
57
58 qCDebug(PX4AirframeLoaderLog) << "Loading meta data file:" << airframeFilename;
59
60 QFile xmlFile(airframeFilename);
61 if (!xmlFile.exists()) {
62 qCWarning(PX4AirframeLoaderLog) << "Internal error";
63 return;
64 }
65
66 bool success = xmlFile.open(QIODevice::ReadOnly);
67
68 if (!success) {
69 qCWarning(PX4AirframeLoaderLog) << "Failed opening airframe XML";
70 return;
71 }
72
73 QXmlStreamReader xml(xmlFile.readAll());
74 xmlFile.close();
75 if (xml.hasError()) {
76 qCWarning(PX4AirframeLoaderLog) << "Badly formed XML" << xml.errorString();
77 return;
78 }
79
80 QString airframeGroup;
81 QString image;
82 QString airframeName;
83 QString airframeClass;
84 int airframeId = 0;
85 int xmlState = XmlStateNone;
86
87 while (!xml.atEnd()) {
88 if (xml.isStartElement()) {
89 QString elementName = xml.name().toString();
90
91 if (elementName == "airframes") {
92 if (xmlState != XmlStateNone) {
93 qCWarning(PX4AirframeLoaderLog) << "Badly formed XML";
94 return;
95 }
96 xmlState = XmlStateFoundAirframes;
97
98 } else if (elementName == "version") {
99 if (xmlState != XmlStateFoundAirframes) {
100 qCWarning(PX4AirframeLoaderLog) << "Badly formed XML";
101 return;
102 }
103 xmlState = XmlStateFoundVersion;
104
105 bool convertOk;
106 QString strVersion = xml.readElementText();
107 int intVersion = strVersion.toInt(&convertOk);
108 if (!convertOk) {
109 qCWarning(PX4AirframeLoaderLog) << "Badly formed XML";
110 return;
111 }
112 if (intVersion < 1) {
113 // We can't read these old files
114 qDebug() << "Airframe version stamp too old, skipping load. Found:" << intVersion << "Want: 3 File:" << airframeFilename;
115 return;
116 }
117
118 } else if (elementName == "airframe_version_major") {
119 // Just skip over for now
120 } else if (elementName == "airframe_version_minor") {
121 // Just skip over for now
122
123 } else if (elementName == "airframe_group") {
124 if (xmlState != XmlStateFoundVersion) {
125 // We didn't get a version stamp, assume older version we can't read
126 qDebug() << "Parameter version stamp not found, skipping load" << airframeFilename;
127 return;
128 }
129 xmlState = XmlStateFoundGroup;
130
131 if (!xml.attributes().hasAttribute("name") || !xml.attributes().hasAttribute("image")) {
132 qCWarning(PX4AirframeLoaderLog) << "Badly formed XML";
133 return;
134 }
135 airframeGroup = xml.attributes().value("name").toString();
136 image = xml.attributes().value("image").toString();
137 qCDebug(PX4AirframeLoaderLog) << "Found group: " << airframeGroup << " image:" << image;
138
139 } else if (elementName == "airframe") {
140 if (xmlState != XmlStateFoundGroup) {
141 qCWarning(PX4AirframeLoaderLog) << "Badly formed XML";
142 return;
143 }
144 xmlState = XmlStateFoundAirframe;
145
146 if (!xml.attributes().hasAttribute("name") || !xml.attributes().hasAttribute("id")) {
147 qCWarning(PX4AirframeLoaderLog) << "Badly formed XML";
148 return;
149 }
150
151 QString name = xml.attributes().value("name").toString();
152 QString id = xml.attributes().value("id").toString();
153
154 qCDebug(PX4AirframeLoaderLog) << "Found airframe name:" << name << " type:" << airframeGroup << " id:" << id;
155
156 // The class element follows, so the airframe is added to the system once it ends
157 airframeName = name;
158 airframeId = id.toInt();
159 airframeClass.clear();
160
161 } else {
162 // We should be getting meta data now
163 if (xmlState != XmlStateFoundAirframe) {
164 qCWarning(PX4AirframeLoaderLog) << "Badly formed XML";
165 return;
166 }
167 if (elementName == "class") {
168 airframeClass = xml.readElementText();
169 }
170 }
171 } else if (xml.isEndElement()) {
172 QString elementName = xml.name().toString();
173
174 if (elementName == "airframe") {
175 AirframeComponentAirframes::insert(airframeGroup, image, airframeName, airframeId, airframeClass);
176
177 // Reset for next airframe
178 xmlState = XmlStateFoundGroup;
179 } else if (elementName == "airframe_group") {
180 xmlState = XmlStateFoundVersion;
181 } else if (elementName == "airframes") {
182 xmlState = XmlStateFoundAirframes;
183 }
184 }
185 xml.readNext();
186 }
187
188 _airframeMetaDataLoaded = true;
189}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static QMap< QString, AirframeComponentAirframes::AirframeType_t * > & get()
static void insert(QString &group, QString &image, QString &name, int id, const QString &vehicleClass)
The AutoPilotPlugin class is an abstract base class which represents the methods and objects which ar...
Collection of Parameter Facts for PX4 AutoPilot.
static void loadAirframeMetaData(void)
static QString aiframeMetaDataFile(void)
PX4AirframeLoader(AutoPilotPlugin *autpilot, QObject *parent=nullptr)
bool runningUnitTests()