PX4FirmwareUpgradeThread.h 6.13 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

/// @file
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
12
///     @brief PX4 Firmware Upgrade operations which occur on a separate thread.
13 14 15 16 17
///     @author Don Gagne <don@thegagnes.com>

#ifndef PX4FirmwareUpgradeThread_H
#define PX4FirmwareUpgradeThread_H

18 19
#include "Bootloader.h"
#include "FirmwareImage.h"
Don Gagne's avatar
Don Gagne committed
20
#include "QGCSerialPortInfo.h"
21

22 23 24 25 26
#include <QObject>
#include <QThread>
#include <QTimer>
#include <QTime>

27 28
#include "qextserialport.h"

29 30
#include <stdint.h>

31
class PX4FirmwareUpgradeThreadController;
32

Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
33
/// @brief Used to run bootloader commands on a separate thread. These routines are mainly meant to to be called
34 35 36 37 38 39 40
///         internally by the PX4FirmwareUpgradeThreadController. Clients should call the various public methods
///         exposed by PX4FirmwareUpgradeThreadController.
class PX4FirmwareUpgradeThreadWorker : public QObject
{
    Q_OBJECT
    
public:
41
    PX4FirmwareUpgradeThreadWorker(PX4FirmwareUpgradeThreadController* controller);
42 43 44
    ~PX4FirmwareUpgradeThreadWorker();
    
signals:
45
    void updateProgress(int curr, int total);
46
    void foundBoard(bool firstAttempt, const QGCSerialPortInfo& portInfo, int type, QString boardName);
47 48
    void noBoardFound(void);
    void boardGone(void);
49 50
    void foundBootloader(int bootloaderVersion, int boardID, int flashSize);
    void bootloaderSyncFailed(void);
51 52 53 54 55
    void error(const QString& errorString);
    void status(const QString& statusText);
    void eraseStarted(void);
    void eraseComplete(void);
    void flashComplete(void);
56 57
    
private slots:
58 59 60 61
    void _init(void);
    void _startFindBoardLoop(void);
    void _reboot(void);
    void _flash(void);
62
    void _findBoardOnce(void);
63 64
    void _updateProgress(int curr, int total) { emit updateProgress(curr, total); }
    void _cancel(void);
65 66
    
private:
67
    bool _findBoardFromPorts(QGCSerialPortInfo& portInfo, QGCSerialPortInfo::BoardType_t& boardType, QString& boardName);
Don Gagne's avatar
Don Gagne committed
68 69
    bool _findBootloader(const QGCSerialPortInfo& portInfo, bool radioMode, bool errorOnNotFound);
    void _3drRadioForceBootloader(const QGCSerialPortInfo& portInfo);
70 71 72 73 74
    bool _erase(void);
    
    PX4FirmwareUpgradeThreadController* _controller;
    
    Bootloader*      _bootloader;
75
    QextSerialPort*     _bootloaderPort;
76 77
    QTimer*             _timerRetry;
    QTime               _elapsed;
78
    static const int    _retryTimeout = 100;
79 80 81
    
    bool                _foundBoard;            ///< true: board is currently connected
    bool                _findBoardFirstAttempt; ///< true: this is our first try looking for a board
Don Gagne's avatar
Don Gagne committed
82
    QGCSerialPortInfo   _foundBoardPortInfo;    ///< port info for found board
83 84 85
};

/// @brief Provides methods to interact with the bootloader. The commands themselves are signalled
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
86
///         across to PX4FirmwareUpgradeThreadWorker so that they run on the separate thread.
87 88 89 90 91 92 93 94
class PX4FirmwareUpgradeThreadController : public QObject
{
    Q_OBJECT
    
public:
    PX4FirmwareUpgradeThreadController(QObject* parent = NULL);
    ~PX4FirmwareUpgradeThreadController(void);
    
95 96 97
    /// @brief Begins the process of searching for a supported board connected to any serial port. This will
    /// continue until cancelFind is called. Signals foundBoard and boardGone as boards come and go.
    void startFindBoardLoop(void);
98
    
99
    void cancel(void);
100 101
    
    /// @brief Sends a reboot command to the bootloader
102
    void reboot(void) { emit _rebootOnThread(); }
103
    
104
    void flash(const FirmwareImage* image);
105
    
106
    const FirmwareImage* image(void) { return _image; }
107 108
    
signals:
109
    /// @brief Emitted by the find board process when it finds a board.
110
    void foundBoard(bool firstAttempt, const QGCSerialPortInfo &portInfo, int boardType, QString boardName);
111 112 113 114 115
    
    void noBoardFound(void);
    
    /// @brief Emitted by the find board process when a board it previously reported as found disappears.
    void boardGone(void);
116 117 118 119 120
    
    /// @brief Emitted by the findBootloader process when has a connection to the bootloader
    void foundBootloader(int bootloaderVersion, int boardID, int flashSize);
    
    /// @brief Emitted by the bootloader commands when an error occurs.
121 122 123
    void error(const QString& errorString);
    
    void status(const QString& status);
124 125 126 127 128
    
    /// @brief Signalled when the findBootloader process connects to the port, but cannot sync to the
    ///         bootloader.
    void bootloaderSyncFailed(void);
    
129 130
    void eraseStarted(void);
    void eraseComplete(void);
131
    
132
    void flashComplete(void);
133 134 135 136
    
    /// @brief Signalled to update progress for long running bootloader commands
    void updateProgress(int curr, int total);
    
137
    // Internal signals to communicate with thread worker
138
    void _initThreadWorker(void);
139 140 141 142
    void _startFindBoardLoopOnThread(void);
    void _rebootOnThread(void);
    void _flashOnThread(void);
    void _cancel(void);
143 144
    
private slots:
145
    void _foundBoard(bool firstAttempt, const QGCSerialPortInfo& portInfo, int type, QString name) { emit foundBoard(firstAttempt, portInfo, type, name); }
146 147 148 149 150 151 152 153 154
    void _noBoardFound(void) { emit noBoardFound(); }
    void _boardGone(void) { emit boardGone(); }
    void _foundBootloader(int bootloaderVersion, int boardID, int flashSize) { emit foundBootloader(bootloaderVersion, boardID, flashSize); }
    void _bootloaderSyncFailed(void) { emit bootloaderSyncFailed(); }
    void _error(const QString& errorString) { emit error(errorString); }
    void _status(const QString& statusText) { emit status(statusText); }
    void _eraseStarted(void) { emit eraseStarted(); }
    void _eraseComplete(void) { emit eraseComplete(); }
    void _flashComplete(void) { emit flashComplete(); }
155 156
    
private:
157 158
    void _updateProgress(int curr, int total) { emit updateProgress(curr, total); }
    
159 160
    PX4FirmwareUpgradeThreadWorker* _worker;
    QThread*                        _workerThread;  ///< Thread which PX4FirmwareUpgradeThreadWorker runs on
161 162
    
    const FirmwareImage* _image;
163 164 165
};

#endif