PX4ParameterMetaData.cc 19.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11 12 13

/// @file
///     @author Don Gagne <don@thegagnes.com>

14
#include "PX4ParameterMetaData.h"
15 16 17 18 19 20 21 22
#include "QGCApplication.h"
#include "QGCLoggingCategory.h"

#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QDebug>

23
QGC_LOGGING_CATEGORY(PX4ParameterMetaDataLog, "PX4ParameterMetaDataLog")
24

25 26
PX4ParameterMetaData::PX4ParameterMetaData(void)
    : _parameterMetaDataLoaded(false)
27
{
28

29 30 31 32 33 34 35
}

/// Converts a string to a typed QVariant
///     @param string String to convert
///     @param type Type for Fact which dictates the QVariant type as well
///     @param convertOk Returned: true: conversion success, false: conversion failure
/// @return Returns the correctly type QVariant
36
QVariant PX4ParameterMetaData::_stringToTypedVariant(const QString& string, FactMetaData::ValueType_t type, bool* convertOk)
37 38 39
{
    QVariant var(string);

Don Gagne's avatar
Don Gagne committed
40
    int convertTo = QVariant::Int; // keep compiler warning happy
41
    switch (type) {
Don Gagne's avatar
Don Gagne committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    case FactMetaData::valueTypeUint8:
    case FactMetaData::valueTypeUint16:
    case FactMetaData::valueTypeUint32:
        convertTo = QVariant::UInt;
        break;
    case FactMetaData::valueTypeInt8:
    case FactMetaData::valueTypeInt16:
    case FactMetaData::valueTypeInt32:
        convertTo = QVariant::Int;
        break;
    case FactMetaData::valueTypeFloat:
        convertTo = QMetaType::Float;
        break;
    case FactMetaData::valueTypeDouble:
        convertTo = QVariant::Double;
        break;
    case FactMetaData::valueTypeString:
        qWarning() << "Internal Error: No support for string parameters";
        convertTo = QVariant::String;
        break;
62 63 64 65
    case FactMetaData::valueTypeBool:
        qWarning() << "Internal Error: No support for string parameters";
        convertTo = QVariant::Bool;
        break;
66 67 68 69 70 71 72
    }
    
    *convertOk = var.convert(convertTo);
    
    return var;
}

73
void PX4ParameterMetaData::loadParameterFactMetaDataFile(const QString& metaDataFile)
74
{
75
    qCDebug(ParameterManagerLog) << "PX4ParameterMetaData::loadParameterFactMetaDataFile" << metaDataFile;
76

77
    if (_parameterMetaDataLoaded) {
78
        qWarning() << "Internal error: parameter meta data loaded more than once";
79 80 81
        return;
    }
    _parameterMetaDataLoaded = true;
82 83
	
    qCDebug(PX4ParameterMetaDataLog) << "Loading parameter meta data:" << metaDataFile;
84

85
    QFile xmlFile(metaDataFile);
86

87 88 89
    if (!xmlFile.exists()) {
        qWarning() << "Internal error: metaDataFile mission" << metaDataFile;
        return;
90
    }
91
    
92 93 94 95
    if (!xmlFile.open(QIODevice::ReadOnly)) {
        qWarning() << "Internal error: Unable to open parameter file:" << metaDataFile << xmlFile.errorString();
        return;
    }
96 97 98 99 100 101 102 103
    
    QXmlStreamReader xml(xmlFile.readAll());
    xmlFile.close();
    if (xml.hasError()) {
        qWarning() << "Badly formed XML" << xml.errorString();
        return;
    }
    
104 105 106 107 108
    QString         factGroup;
    QString         errorString;
    FactMetaData*   metaData = NULL;
    int             xmlState = XmlStateNone;
    bool            badMetaData = true;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    
    while (!xml.atEnd()) {
        if (xml.isStartElement()) {
            QString elementName = xml.name().toString();
            
            if (elementName == "parameters") {
                if (xmlState != XmlStateNone) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                xmlState = XmlStateFoundParameters;
                
            } else if (elementName == "version") {
                if (xmlState != XmlStateFoundParameters) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                xmlState = XmlStateFoundVersion;
                
                bool convertOk;
                QString strVersion = xml.readElementText();
                int intVersion = strVersion.toInt(&convertOk);
                if (!convertOk) {
                    qWarning() << "Badly formed XML";
                    return;
                }
135
                if (intVersion <= 2) {
136
                    // We can't read these old files
137
                    qDebug() << "Parameter version stamp too old, skipping load. Found:" << intVersion << "Want: 3 File:" << metaDataFile;
138 139 140
                    return;
                }
                
141 142 143 144 145
            } else if (elementName == "parameter_version_major") {
                // Just skip over for now
            } else if (elementName == "parameter_version_minor") {
                // Just skip over for now

146 147 148
            } else if (elementName == "group") {
                if (xmlState != XmlStateFoundVersion) {
                    // We didn't get a version stamp, assume older version we can't read
149
                    qDebug() << "Parameter version stamp not found, skipping load" << metaDataFile;
150 151 152 153 154 155 156 157 158
                    return;
                }
                xmlState = XmlStateFoundGroup;
                
                if (!xml.attributes().hasAttribute("name")) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                factGroup = xml.attributes().value("name").toString();
159
                qCDebug(PX4ParameterMetaDataLog) << "Found group: " << factGroup;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                
            } else if (elementName == "parameter") {
                if (xmlState != XmlStateFoundGroup) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                xmlState = XmlStateFoundParameter;
                
                if (!xml.attributes().hasAttribute("name") || !xml.attributes().hasAttribute("type")) {
                    qWarning() << "Badly formed XML";
                    return;
                }
                
                QString name = xml.attributes().value("name").toString();
                QString type = xml.attributes().value("type").toString();
175
                QString strDefault =    xml.attributes().value("default").toString();
176
                
177
                qCDebug(PX4ParameterMetaDataLog) << "Found parameter name:" << name << " type:" << type << " default:" << strDefault;
178 179

                // Convert type from string to FactMetaData::ValueType_t
Don Gagne's avatar
Don Gagne committed
180 181 182
                bool unknownType;
                FactMetaData::ValueType_t foundType = FactMetaData::stringToType(type, unknownType);
                if (unknownType) {
183 184 185 186 187
                    qWarning() << "Parameter meta data with bad type:" << type << " name:" << name;
                    return;
                }
                
                // Now that we know type we can create meta data object and add it to the system
188
                
189 190
                metaData = new FactMetaData(foundType);
                Q_CHECK_PTR(metaData);
191
                if (_mapParameterName2FactMetaData.contains(name)) {
192
                    // We can't trust the meta dafa since we have dups
193
                    qCWarning(PX4ParameterMetaDataLog) << "Duplicate parameter found:" << name;
194 195 196
                    badMetaData = true;
                    // Reset to default meta data
                    _mapParameterName2FactMetaData[name] = metaData;
197
                } else {
198
                    _mapParameterName2FactMetaData[name] = metaData;
199
                    metaData->setName(name);
200
                    metaData->setGroup(factGroup);
201
                    
202 203 204
                    if (xml.attributes().hasAttribute("default") && !strDefault.isEmpty()) {
                        QVariant varDefault;
                        
205 206
                        if (metaData->convertAndValidateRaw(strDefault, false, varDefault, errorString)) {
                            metaData->setRawDefaultValue(varDefault);
207
                        } else {
208
                            qCWarning(PX4ParameterMetaDataLog) << "Invalid default value, name:" << name << " type:" << type << " default:" << strDefault << " error:" << errorString;
209
                        }
210 211 212 213 214 215 216 217 218 219
                    }
                }
                
            } else {
                // We should be getting meta data now
                if (xmlState != XmlStateFoundParameter) {
                    qWarning() << "Badly formed XML";
                    return;
                }

220 221 222 223
                if (!badMetaData) {
                    if (elementName == "short_desc") {
                        Q_ASSERT(metaData);
                        QString text = xml.readElementText();
224
                        text = text.replace("\n", " ");
225
                        qCDebug(PX4ParameterMetaDataLog) << "Short description:" << text;
226
                        metaData->setShortDescription(text);
227

228 229 230
                    } else if (elementName == "long_desc") {
                        Q_ASSERT(metaData);
                        QString text = xml.readElementText();
231
                        text = text.replace("\n", " ");
232
                        qCDebug(PX4ParameterMetaDataLog) << "Long description:" << text;
233 234 235 236 237
                        metaData->setLongDescription(text);
                        
                    } else if (elementName == "min") {
                        Q_ASSERT(metaData);
                        QString text = xml.readElementText();
238
                        qCDebug(PX4ParameterMetaDataLog) << "Min:" << text;
239 240
                        
                        QVariant varMin;
241 242
                        if (metaData->convertAndValidateRaw(text, true /* convertOnly */, varMin, errorString)) {
                            metaData->setRawMin(varMin);
243
                        } else {
244
                            qCWarning(PX4ParameterMetaDataLog) << "Invalid min value, name:" << metaData->name() << " type:" << metaData->type() << " min:" << text << " error:" << errorString;
245 246 247 248 249
                        }
                        
                    } else if (elementName == "max") {
                        Q_ASSERT(metaData);
                        QString text = xml.readElementText();
250
                        qCDebug(PX4ParameterMetaDataLog) << "Max:" << text;
251 252
                        
                        QVariant varMax;
253 254
                        if (metaData->convertAndValidateRaw(text, true /* convertOnly */, varMax, errorString)) {
                            metaData->setRawMax(varMax);
255
                        } else {
256
                            qCWarning(PX4ParameterMetaDataLog) << "Invalid max value, name:" << metaData->name() << " type:" << metaData->type() << " max:" << text << " error:" << errorString;
257 258 259 260 261
                        }
                        
                    } else if (elementName == "unit") {
                        Q_ASSERT(metaData);
                        QString text = xml.readElementText();
262
                        qCDebug(PX4ParameterMetaDataLog) << "Unit:" << text;
263
                        metaData->setRawUnits(text);
264
                        
265 266 267
                    } else if (elementName == "decimal") {
                        Q_ASSERT(metaData);
                        QString text = xml.readElementText();
268
                        qCDebug(PX4ParameterMetaDataLog) << "Decimal:" << text;
269 270 271 272 273 274

                        bool convertOk;
                        QVariant varDecimals = QVariant(text).toUInt(&convertOk);
                        if (convertOk) {
                            metaData->setDecimalPlaces(varDecimals.toInt());
                        } else {
275
                            qCWarning(PX4ParameterMetaDataLog) << "Invalid decimals value, name:" << metaData->name() << " type:" << metaData->type() << " decimals:" << text << " error: invalid number";
276 277
                        }

278 279 280 281 282 283 284 285
                    } else if (elementName == "reboot_required") {
                        Q_ASSERT(metaData);
                        QString text = xml.readElementText();
                        qCDebug(PX4ParameterMetaDataLog) << "RebootRequired:" << text;
                        if (text.compare("true", Qt::CaseInsensitive) == 0) {
                            metaData->setRebootRequired(true);
                        }

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
                    } else if (elementName == "values") {
                        // doing nothing individual value will follow anyway. May be used for sanity checking.

                    } else if (elementName == "value") {
                        QString enumValueStr = xml.attributes().value("code").toString();
                        QString enumString = xml.readElementText();
                        qCDebug(PX4ParameterMetaDataLog) << "parameter value:"
                                                         << "value desc:" << enumString << "code:" << enumValueStr;

                        QVariant    enumValue;
                        QString     errorString;
                        if (metaData->convertAndValidateRaw(enumValueStr, false /* validate */, enumValue, errorString)) {
                            metaData->addEnumInfo(enumString, enumValue);
                        } else {
                            qCDebug(PX4ParameterMetaDataLog) << "Invalid enum value, name:" << metaData->name()
                                                             << " type:" << metaData->type() << " value:" << enumValueStr
                                                             << " error:" << errorString;
                        }
Don Gagne's avatar
Don Gagne committed
304 305 306 307 308 309 310 311 312 313 314
                    } else if (elementName == "increment") {
                        Q_ASSERT(metaData);
                        double  increment;
                        bool    ok;
                        QString text = xml.readElementText();
                        increment = text.toDouble(&ok);
                        if (ok) {
                            metaData->setIncrement(increment);
                        } else {
                            qCWarning(PX4ParameterMetaDataLog) << "Invalid value for increment, name:" << metaData->name() << " increment:" << text;
                        }
Don Gagne's avatar
Don Gagne committed
315 316 317 318 319 320 321 322

                    } else if (elementName == "boolean") {
                        QVariant    enumValue;
                        metaData->convertAndValidateRaw(1, false /* validate */, enumValue, errorString);
                        metaData->addEnumInfo(tr("Enabled"), enumValue);
                        metaData->convertAndValidateRaw(0, false /* validate */, enumValue, errorString);
                        metaData->addEnumInfo(tr("Disabled"), enumValue);

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
                    } else if (elementName == "bitmask") {
                        // doing nothing individual bits will follow anyway. May be used for sanity checking.

                    } else if (elementName == "bit") {
                        bool ok = false;
                        unsigned char bit = xml.attributes().value("index").toString().toUInt(&ok);
                        if (ok) {
                            QString bitDescription = xml.readElementText();
                            qCDebug(PX4ParameterMetaDataLog) << "parameter value:"
                                                             << "index:" << bit << "description:" << bitDescription;

                            if (bit < 31) {
                                QVariant bitmaskRawValue = 1 << bit;
                                QVariant bitmaskValue;
                                QString errorString;
                                if (metaData->convertAndValidateRaw(bitmaskRawValue, true, bitmaskValue, errorString)) {
                                    metaData->addBitmaskInfo(bitDescription, bitmaskValue);
                                } else {
                                    qCDebug(PX4ParameterMetaDataLog) << "Invalid bitmask value, name:" << metaData->name()
                                                                     << " type:" << metaData->type() << " value:" << bitmaskValue
                                                                     << " error:" << errorString;
                                }
                            } else {
                                qCWarning(PX4ParameterMetaDataLog) << "Invalid value for bitmask, bit:" << bit;
                            }
                        }
349
                    } else {
Don Gagne's avatar
Don Gagne committed
350
                        qCDebug(PX4ParameterMetaDataLog) << "Unknown element in XML: " << elementName;
351 352 353 354 355 356 357
                    }
                }
            }
        } else if (xml.isEndElement()) {
            QString elementName = xml.name().toString();

            if (elementName == "parameter") {
358 359 360 361
                // Done loading this parameter, validate default value
                if (metaData->defaultValueAvailable()) {
                    QVariant var;
                    
362 363
                    if (!metaData->convertAndValidateRaw(metaData->rawDefaultValue(), false /* convertOnly */, var, errorString)) {
                        qCWarning(PX4ParameterMetaDataLog) << "Invalid default value, name:" << metaData->name() << " type:" << metaData->type() << " default:" << metaData->rawDefaultValue() << " error:" << errorString;
364 365 366 367
                    }
                }
                
                // Reset for next parameter
368
                metaData = NULL;
369
                badMetaData = false;
370 371 372 373 374 375 376 377 378 379 380
                xmlState = XmlStateFoundGroup;
            } else if (elementName == "group") {
                xmlState = XmlStateFoundVersion;
            } else if (elementName == "parameters") {
                xmlState = XmlStateFoundParameters;
            }
        }
        xml.readNext();
    }
}

381
void PX4ParameterMetaData::addMetaDataToFact(Fact* fact, MAV_TYPE vehicleType)
382
{
383 384
    Q_UNUSED(vehicleType)

385 386
    if (_mapParameterName2FactMetaData.contains(fact->name())) {
        fact->setMetaData(_mapParameterName2FactMetaData[fact->name()]);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    }
}

void PX4ParameterMetaData::getParameterMetaDataVersionInfo(const QString& metaDataFile, int& majorVersion, int& minorVersion)
{
    QFile xmlFile(metaDataFile);

    if (!xmlFile.exists()) {
        qWarning() << "Internal error: metaDataFile mission" << metaDataFile;
        return;
    }

    if (!xmlFile.open(QIODevice::ReadOnly)) {
        qWarning() << "Internal error: Unable to open parameter file:" << metaDataFile << xmlFile.errorString();
        return;
    }

    QXmlStreamReader xml(xmlFile.readAll());
    xmlFile.close();
    if (xml.hasError()) {
        qWarning() << "Badly formed XML" << xml.errorString();
        return;
    }

    majorVersion = -1;
    minorVersion = -1;

    while (!xml.atEnd() && (majorVersion == -1 || minorVersion == -1)) {
        if (xml.isStartElement()) {
            QString elementName = xml.name().toString();

            if (elementName == "parameter_version_major") {
                bool convertOk;
                QString strVersion = xml.readElementText();
                majorVersion = strVersion.toInt(&convertOk);
                if (!convertOk) {
                    qWarning() << "Badly formed XML";
                    return;
                }
            } else if (elementName == "parameter_version_minor") {
                bool convertOk;
                QString strVersion = xml.readElementText();
                minorVersion = strVersion.toInt(&convertOk);
                if (!convertOk) {
                    qWarning() << "Badly formed XML";
                    return;
                }
            }
        }
        xml.readNext();
    }

    // Assume defaults if not found
    if (majorVersion == -1) {
        majorVersion = 1;
    }
    if (minorVersion == -1) {
        minorVersion = 1;
445 446
    }
}