MAVLinkXMLParserV10.cc 5.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of class MAVLinkXMLParserV10
 *   @author Lorenz Meier <mail@qgroundcontrol.org>
 */

#include <QFile>
#include <QDir>
#include <QPair>
#include <QList>
#include <QMap>
#include <QDateTime>
#include <QLocale>
37
#include <QApplication>
lm's avatar
lm committed
38

39 40 41 42 43
#include "MAVLinkXMLParserV10.h"

#include <QDebug>

MAVLinkXMLParserV10::MAVLinkXMLParserV10(QDomDocument* document, QString outputDirectory, QObject* parent) : QObject(parent),
44 45 46
doc(document),
outputDirName(outputDirectory),
fileName("")
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
{
}

MAVLinkXMLParserV10::MAVLinkXMLParserV10(QString document, QString outputDirectory, QObject* parent) : QObject(parent)
{
    doc = new QDomDocument();
    QFile file(document);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        const QString instanceText(QString::fromUtf8(file.readAll()));
        doc->setContent(instanceText);
    }
    fileName = document;
    outputDirName = outputDirectory;
}

MAVLinkXMLParserV10::~MAVLinkXMLParserV10()
{
}

67
void MAVLinkXMLParserV10::processError(QProcess::ProcessError err)
68
{
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    switch(err)
    {
    case QProcess::FailedToStart:
        emit parseState(tr("Generator failed to start. Please check if the path and command is correct."));
        break;
    case QProcess::Crashed:
        emit parseState("Generator crashed, This is a generator-related problem. Please upgrade MAVLink generator.");
        break;
    case QProcess::Timedout:
        emit parseState(tr("Generator start timed out, please check if the path and command are correct"));
        break;
    case QProcess::WriteError:
        emit parseState(tr("Could not communicate with generator. Please check if the path and command are correct"));
        break;
    case QProcess::ReadError:
        emit parseState(tr("Could not communicate with generator. Please check if the path and command are correct"));
        break;
    case QProcess::UnknownError:
    default:
        emit parseState(tr("Generator error. Please check if the path and command is correct."));
        break;
    }
91 92
}

93 94 95 96 97
/**
 * Generate C-code (C-89 compliant) out of the XML protocol specs.
 */
bool MAVLinkXMLParserV10::generate()
{
98
    emit parseState(tr("Generator ready."));
99
#ifdef Q_OS_WIN
100
    QString generatorCall("files/mavlink_generator/generator/mavgen.exe");
101 102
#endif
#if (defined Q_OS_MAC) || (defined Q_OS_LINUX)
103
    QString generatorCall("python");
104 105 106 107 108
#endif
    QString lang("C");
    QString version("1.0");

    QStringList arguments;
109 110 111 112
#if (defined Q_OS_MAC) || (defined Q_OS_LINUX)
    // Script is only needed as argument if Python is used, the Py2Exe implicitely knows the script
    arguments << QString("%1/files/mavlink_generator/generator/mavgen.py").arg(QApplication::applicationDirPath());
#endif
113 114 115 116 117 118
    arguments << QString("--lang=%1").arg(lang);
    arguments << QString("--output=%2").arg(outputDirName);
    arguments << QString("%3").arg(fileName);
    arguments << QString("--wire-protocol=%4").arg(version);

    qDebug() << "Attempted to start" << generatorCall << arguments;
119
    process = new QProcess(this);
120
    process->setProcessChannelMode(QProcess::SeparateChannels);
121
    connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
122 123 124 125 126
    connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()));
    connect(process, SIGNAL(readyReadStandardError()), this, SLOT(readStdErr()));
    process->start(generatorCall, arguments, QIODevice::ReadWrite);
    QString output = QString(process->readAll());
    emit parseState(output);
127 128 129
    // Print process status
    emit parseState(QString("<font color=\"red\">%1</font>").arg(QString(process->readAllStandardError())));
    emit parseState(QString(process->readAllStandardOutput()));
130

131
    process->waitForFinished(20000);
132

133 134 135 136
    process->terminate();
    process->kill();
    return true;//result;
}
137

138 139 140 141 142
void MAVLinkXMLParserV10::readStdOut()
{
    QString out(process->readAllStandardOutput());
    emit parseState(out);
}
143

144 145 146 147 148
void MAVLinkXMLParserV10::readStdErr()
{
    QString out = QString("<font color=\"red\">%1</font>").arg(QString(process->readAllStandardError()));
    emit parseState(out);
}