PX4FirmwareUpgradeThread.cc 10.9 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009, 2014 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 PX4 Firmware Upgrade operations which occur on a seperate thread.
///     @author Don Gagne <don@thegagnes.com>

#include "PX4FirmwareUpgradeThread.h"
#include "PX4Bootloader.h"

#include <QTimer>
#include <QSerialPortInfo>
#include <QDebug>

PX4FirmwareUpgradeThreadWorker::PX4FirmwareUpgradeThreadWorker(QObject* parent) :
    QObject(parent),
    _bootloader(NULL),
    _bootloaderPort(NULL),
    _timerTimeout(NULL),
    _timerRetry(NULL)   
{
    
}

PX4FirmwareUpgradeThreadWorker::~PX4FirmwareUpgradeThreadWorker()
{
    if (_bootloaderPort) {
        // deleteLater so delete happens on correct thread
        _bootloaderPort->deleteLater();
    }
}

/// @brief Initializes the PX4FirmwareUpgradeThreadWorker with the various child objects which must be created
///         on the worker thread.
void PX4FirmwareUpgradeThreadWorker::init(void)
{
    // We create the timers here so that they are on the right thread
    
    Q_ASSERT(_timerTimeout == NULL);
    _timerTimeout = new QTimer(this);
    Q_CHECK_PTR(_timerTimeout);
    connect(_timerTimeout, &QTimer::timeout, this, &PX4FirmwareUpgradeThreadWorker::timeout);
    _timerTimeout->setSingleShot(true);
    
    Q_ASSERT(_timerRetry == NULL);
    _timerRetry = new QTimer(this);
    Q_CHECK_PTR(_timerRetry);
    _timerRetry->setSingleShot(true);
    _timerRetry->setInterval(_retryTimeout);
    
    Q_ASSERT(_bootloader == NULL);
    _bootloader = new PX4Bootloader(this);
    connect(_bootloader, &PX4Bootloader::updateProgramProgress, this, &PX4FirmwareUpgradeThreadWorker::_updateProgramProgress);
}

void PX4FirmwareUpgradeThreadWorker::findBoard(int msecTimeout)
{
Don Gagne's avatar
Don Gagne committed
78
    _findBoardFirstAttempt = true;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    connect(_timerRetry, &QTimer::timeout, this, &PX4FirmwareUpgradeThreadWorker::_findBoardOnce);
    _timerTimeout->start(msecTimeout);
    _elapsed.start();
    _findBoardOnce();
}

void PX4FirmwareUpgradeThreadWorker::_findBoardOnce(void)
{
    qDebug() << "_findBoardOnce";
    
    QString portName;
    QString portDescription;
    
    foreach (QSerialPortInfo info, QSerialPortInfo::availablePorts()) {
        if (!info.portName().isEmpty() && (info.description().contains("PX4") || info.vendorIdentifier() == 9900 /* 3DR */)) {
            
            qDebug() << "Found Board:";
            qDebug() << "\tport name:" << info.portName();
            qDebug() << "\tdescription:" << info.description();
            qDebug() << "\tsystem location:" << info.systemLocation();
            qDebug() << "\tvendor ID:" << info.vendorIdentifier();
            qDebug() << "\tproduct ID:" << info.productIdentifier();
            
102
            portName = info.systemLocation();
103 104 105
            portDescription = info.description();
            
            _closeFind();
Don Gagne's avatar
Don Gagne committed
106
            emit foundBoard(_findBoardFirstAttempt, portName, portDescription);
107 108 109 110
            return;
        }
    }
    
Don Gagne's avatar
Don Gagne committed
111 112
    _findBoardFirstAttempt = false;
    
113 114 115 116
    emit updateProgress(_elapsed.elapsed(), _timerTimeout->interval());
    _timerRetry->start();
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
bool PX4FirmwareUpgradeThreadController::pluggedInBoard(void)
{
    qDebug() << "pluggedInBoard";
    
    QString portName;
    QString portDescription;
    
    foreach (QSerialPortInfo info, QSerialPortInfo::availablePorts()) {
        if (!info.portName().isEmpty() && (info.description().contains("PX4") || info.vendorIdentifier() == 9900 /* 3DR */)) {
            return true;
        }
    }
    
    return false;
}

133 134
void PX4FirmwareUpgradeThreadWorker::findBootloader(const QString portName, int msecTimeout)
{
Don Gagne's avatar
Don Gagne committed
135 136 137
    Q_UNUSED(msecTimeout);
    
    // Once the port shows up, we only try to connect to the bootloader a single time
138 139 140 141 142 143 144 145 146 147
    _portName = portName;
    _findBootloaderOnce();
}

void PX4FirmwareUpgradeThreadWorker::_findBootloaderOnce(void)
{
    qDebug() << "_findBootloaderOnce";
    
    uint32_t    bootloaderVersion, boardID, flashSize;

148
    _bootloaderPort = new QextSerialPort(QextSerialPort::Polling);
149 150 151 152 153 154 155 156 157 158 159 160 161
    Q_CHECK_PTR(_bootloaderPort);
    
    if (_bootloader->open(_bootloaderPort, _portName)) {
        if (_bootloader->sync(_bootloaderPort)) {
            if (_bootloader->getBoardInfo(_bootloaderPort, bootloaderVersion, boardID, flashSize)) {
                _closeFind();
                qDebug() << "Found bootloader";
                emit foundBootloader(bootloaderVersion, boardID, flashSize);
                return;
            }
        }
    }
    
Don Gagne's avatar
Don Gagne committed
162 163 164 165 166 167
    _closeFind();
    _bootloaderPort->close();
    _bootloaderPort->deleteLater();
    _bootloaderPort = NULL;
    qDebug() << "Bootloader error:" << _bootloader->errorString();
    emit error(commandBootloader, _bootloader->errorString());
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

void PX4FirmwareUpgradeThreadWorker::_closeFind(void)
{
    emit updateProgress(100, 100);
    disconnect(_timerRetry, SIGNAL(timeout()), 0, 0);
    _timerRetry->stop();
    _timerTimeout->stop();
}

void PX4FirmwareUpgradeThreadWorker::cancelFind(void)
{
    _closeFind();
    emit complete(commandCancel);
}

void PX4FirmwareUpgradeThreadWorker::timeout(void)
{
    qDebug() << "Find timeout";
    _closeFind();
    emit findTimeout();
}

void PX4FirmwareUpgradeThreadWorker::sendBootloaderReboot(void)
{
193 194 195 196 197 198 199
    if (_bootloaderPort) {
        if (_bootloaderPort->isOpen()) {
            _bootloader->sendBootloaderReboot(_bootloaderPort);
        }
        _bootloaderPort->deleteLater();
        _bootloaderPort = NULL;
    }
200 201 202 203 204 205
}

void PX4FirmwareUpgradeThreadWorker::program(const QString firmwareFilename)
{
    qDebug() << "Program";
    if (!_bootloader->program(_bootloaderPort, firmwareFilename)) {
206
        _bootloaderPort->deleteLater();
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        _bootloaderPort = NULL;
        qDebug() << "Program failed:" << _bootloader->errorString();
        emit error(commandProgram, _bootloader->errorString());
    } else {
        qDebug() << "Program complete";
        emit complete(commandProgram);
    }
}

void PX4FirmwareUpgradeThreadWorker::verify(const QString firmwareFilename)
{
    qDebug() << "Verify";
    if (!_bootloader->verify(_bootloaderPort, firmwareFilename)) {
        qDebug() << "Verify failed:" << _bootloader->errorString();
        emit error(commandVerify, _bootloader->errorString());
    } else {
        qDebug() << "Verify complete";
        emit complete(commandVerify);
    }
226
    _bootloaderPort->deleteLater();
227 228 229 230 231 232 233
    _bootloaderPort = NULL;
}

void PX4FirmwareUpgradeThreadWorker::erase(void)
{
    qDebug() << "Erase";
    if (!_bootloader->erase(_bootloaderPort)) {
234
        _bootloaderPort->deleteLater();
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
        _bootloaderPort = NULL;
        qDebug() << "Erase failed:" << _bootloader->errorString();
        emit error(commandErase, _bootloader->errorString());
    } else {
        qDebug() << "Erase complete";
        emit complete(commandErase);
    }
}

PX4FirmwareUpgradeThreadController::PX4FirmwareUpgradeThreadController(QObject* parent) :
    QObject(parent)
{
    _worker = new PX4FirmwareUpgradeThreadWorker();
    Q_CHECK_PTR(_worker);
    
    _workerThread = new QThread(this);
    Q_CHECK_PTR(_workerThread);
    _worker->moveToThread(_workerThread);
    
    connect(_worker, &PX4FirmwareUpgradeThreadWorker::foundBoard, this, &PX4FirmwareUpgradeThreadController::_foundBoard);
    connect(_worker, &PX4FirmwareUpgradeThreadWorker::foundBootloader, this, &PX4FirmwareUpgradeThreadController::_foundBootloader);
    connect(_worker, &PX4FirmwareUpgradeThreadWorker::bootloaderSyncFailed, this, &PX4FirmwareUpgradeThreadController::_bootloaderSyncFailed);
    connect(_worker, &PX4FirmwareUpgradeThreadWorker::error, this, &PX4FirmwareUpgradeThreadController::_error);
    connect(_worker, &PX4FirmwareUpgradeThreadWorker::complete, this, &PX4FirmwareUpgradeThreadController::_complete);
    connect(_worker, &PX4FirmwareUpgradeThreadWorker::findTimeout, this, &PX4FirmwareUpgradeThreadController::_findTimeout);
    connect(_worker, &PX4FirmwareUpgradeThreadWorker::updateProgress, this, &PX4FirmwareUpgradeThreadController::_updateProgress);

    connect(this, &PX4FirmwareUpgradeThreadController::_initThreadWorker, _worker, &PX4FirmwareUpgradeThreadWorker::init);
    connect(this, &PX4FirmwareUpgradeThreadController::_findBoardOnThread, _worker, &PX4FirmwareUpgradeThreadWorker::findBoard);
    connect(this, &PX4FirmwareUpgradeThreadController::_findBootloaderOnThread, _worker, &PX4FirmwareUpgradeThreadWorker::findBootloader);
    connect(this, &PX4FirmwareUpgradeThreadController::_sendBootloaderRebootOnThread, _worker, &PX4FirmwareUpgradeThreadWorker::sendBootloaderReboot);
    connect(this, &PX4FirmwareUpgradeThreadController::_programOnThread, _worker, &PX4FirmwareUpgradeThreadWorker::program);
    connect(this, &PX4FirmwareUpgradeThreadController::_verifyOnThread, _worker, &PX4FirmwareUpgradeThreadWorker::verify);
    connect(this, &PX4FirmwareUpgradeThreadController::_eraseOnThread, _worker, &PX4FirmwareUpgradeThreadWorker::erase);
    connect(this, &PX4FirmwareUpgradeThreadController::_cancelFindOnThread, _worker, &PX4FirmwareUpgradeThreadWorker::cancelFind);
    
    _workerThread->start();
    
    emit _initThreadWorker();
}

PX4FirmwareUpgradeThreadController::~PX4FirmwareUpgradeThreadController()
{
    _workerThread->quit();
    _workerThread->wait();
}

void PX4FirmwareUpgradeThreadController::findBoard(int msecTimeout)
{
    qDebug() << "PX4FirmwareUpgradeThreadController::findBoard";
    emit _findBoardOnThread(msecTimeout);
}

void PX4FirmwareUpgradeThreadController::findBootloader(const QString& portName, int msecTimeout)
{
    qDebug() << "PX4FirmwareUpgradeThreadController::findBootloader";
    emit _findBootloaderOnThread(portName, msecTimeout);
}

Don Gagne's avatar
Don Gagne committed
294
void PX4FirmwareUpgradeThreadController::_foundBoard(bool firstTry, const QString portName, QString portDescription)
295
{
Don Gagne's avatar
Don Gagne committed
296
    emit foundBoard(firstTry, portName, portDescription);
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
}

void PX4FirmwareUpgradeThreadController::_foundBootloader(int bootloaderVersion, int boardID, int flashSize)
{
    emit foundBootloader(bootloaderVersion, boardID, flashSize);
}

void PX4FirmwareUpgradeThreadController::_bootloaderSyncFailed(void)
{    
    emit bootloaderSyncFailed();
}

void PX4FirmwareUpgradeThreadController::_findTimeout(void)
{
    emit findTimeout();
}