FirmwareUpgradeController.h 10.8 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 15
#ifndef FirmwareUpgradeController_H
#define FirmwareUpgradeController_H
16

17
#include "PX4FirmwareUpgradeThread.h"
18 19
#include "LinkManager.h"
#include "FirmwareImage.h"
20 21

#include <QObject>
22 23 24 25
#include <QUrl>
#include <QTimer>
#include <QNetworkAccessManager>
#include <QNetworkReply>
26 27
#include <QPixmap>
#include <QQuickItem>
28

29 30
#include "qextserialport.h"

31 32
#include <stdint.h>

33 34
/// Supported firmware types. If you modify these you will need to update the qml file as well.

35 36
// Firmware Upgrade MVC Controller for FirmwareUpgrade.qml.
class FirmwareUpgradeController : public QObject
37 38
{
    Q_OBJECT
39
    
40
public:
41 42 43 44
        typedef enum {
            AutoPilotStackPX4,
            AutoPilotStackAPM,
            PX4Flow,
45
            ThreeDRRadio
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        } AutoPilotStackType_t;

        typedef enum {
            StableFirmware,
            BetaFirmware,
            DeveloperFirmware,
            CustomFirmware
        } FirmwareType_t;

        typedef enum {
            QuadFirmware,
            X8Firmware,
            HexaFirmware,
            OctoFirmware,
            YFirmware,
            Y6Firmware,
            HeliFirmware,
Don Gagne's avatar
Don Gagne committed
63
            CopterFirmware,
64 65
            PlaneFirmware,
            RoverFirmware,
66
            DefaultVehicleFirmware
67 68 69 70 71 72 73 74 75 76 77 78 79 80
        } FirmwareVehicleType_t;

        Q_ENUMS(AutoPilotStackType_t)
        Q_ENUMS(FirmwareType_t)
        Q_ENUMS(FirmwareVehicleType_t)

    class FirmwareIdentifier
    {
    public:
        FirmwareIdentifier(AutoPilotStackType_t stack = AutoPilotStackPX4,
                           FirmwareType_t firmware = StableFirmware,
                           FirmwareVehicleType_t vehicle = DefaultVehicleFirmware)
            : autopilotStackType(stack), firmwareType(firmware), firmwareVehicleType(vehicle) {}

81
        bool operator==(const FirmwareIdentifier& firmwareId) const
82
        {
83 84 85
            return (firmwareId.autopilotStackType == autopilotStackType &&
                    firmwareId.firmwareType == firmwareType &&
                    firmwareId.firmwareVehicleType == firmwareVehicleType);
86 87 88 89 90 91 92 93
        }

        // members
        AutoPilotStackType_t    autopilotStackType;
        FirmwareType_t          firmwareType;
        FirmwareVehicleType_t   firmwareVehicleType;
    };

94
    FirmwareUpgradeController(void);
Don Gagne's avatar
Don Gagne committed
95 96
    ~FirmwareUpgradeController();

97 98 99
    Q_PROPERTY(QString          boardPort                   READ boardPort                                              NOTIFY boardFound)
    Q_PROPERTY(QString          boardDescription            READ boardDescription                                       NOTIFY boardFound)
    Q_PROPERTY(QString          boardType                   MEMBER _foundBoardTypeName                                  NOTIFY boardFound)
100 101
    Q_PROPERTY(bool             pixhawkBoard                READ pixhawkBoard                                           NOTIFY boardFound)
    Q_PROPERTY(bool             px4FlowBoard                READ px4FlowBoard                                           NOTIFY boardFound)
102 103
    Q_PROPERTY(FirmwareType_t   selectedFirmwareType        READ selectedFirmwareType   WRITE setSelectedFirmwareType   NOTIFY selectedFirmwareTypeChanged)
    Q_PROPERTY(QStringList      apmAvailableVersions        READ apmAvailableVersions                                   NOTIFY apmAvailableVersionsChanged)
Don Gagne's avatar
Don Gagne committed
104
    Q_PROPERTY(QString          px4StableVersion            READ px4StableVersion                                       NOTIFY px4StableVersionChanged)
105
    Q_PROPERTY(QString          px4BetaVersion              READ px4BetaVersion                                         NOTIFY px4BetaVersionChanged)
106

107 108 109
    /// TextArea for log output
    Q_PROPERTY(QQuickItem* statusLog READ statusLog WRITE setStatusLog)
    
110 111
    /// Progress bar for you know what
    Q_PROPERTY(QQuickItem* progressBar READ progressBar WRITE setProgressBar)
112 113 114

    /// Starts searching for boards on the background thread
    Q_INVOKABLE void startBoardSearch(void);
115
    
116 117
    /// Cancels whatever state the upgrade worker thread is in
    Q_INVOKABLE void cancel(void);
118
    
119
    /// Called when the firmware type has been selected by the user to continue the flash process.
120 121 122
    Q_INVOKABLE void flash(AutoPilotStackType_t stackType,
                           FirmwareType_t firmwareType = StableFirmware,
                           FirmwareVehicleType_t vehicleType = DefaultVehicleFirmware );
123 124

    Q_INVOKABLE FirmwareVehicleType_t vehicleTypeFromVersionIndex(int index);
125
    
126 127 128
    // overload, not exposed to qml side
    void flash(const FirmwareIdentifier& firmwareId);

129
    // Property accessors
130
    
131 132 133
    QQuickItem* progressBar(void) { return _progressBar; }
    void setProgressBar(QQuickItem* progressBar) { _progressBar = progressBar; }
    
134 135 136
    QQuickItem* statusLog(void) { return _statusLog; }
    void setStatusLog(QQuickItem* statusLog) { _statusLog = statusLog; }
    
137 138
    QString boardPort(void) { return _foundBoardInfo.portName(); }
    QString boardDescription(void) { return _foundBoardInfo.description(); }
139 140 141

    FirmwareType_t selectedFirmwareType(void) { return _selectedFirmwareType; }
    void setSelectedFirmwareType(FirmwareType_t firmwareType);
142
    QString firmwareTypeAsString(FirmwareType_t type) const;
143 144

    QStringList apmAvailableVersions(void);
Don Gagne's avatar
Don Gagne committed
145
    QString px4StableVersion(void) { return _px4StableVersion; }
146
    QString px4BetaVersion(void) { return _px4BetaVersion; }
147

148 149 150
    bool pixhawkBoard(void) const { return _foundBoardType == QGCSerialPortInfo::BoardTypePixhawk; }
    bool px4FlowBoard(void) const { return _foundBoardType == QGCSerialPortInfo::BoardTypePX4Flow; }

151
signals:
152 153 154 155 156 157
    void boardFound(void);
    void noBoardFound(void);
    void boardGone(void);
    void flashComplete(void);
    void flashCancelled(void);
    void error(void);
158 159
    void selectedFirmwareTypeChanged(FirmwareType_t firmwareType);
    void apmAvailableVersionsChanged(void);
Don Gagne's avatar
Don Gagne committed
160
    void px4StableVersionChanged(const QString& px4StableVersion);
161 162
    void px4BetaVersionChanged(const QString& px4BetaVersion);

163
private slots:
164 165 166
    void _firmwareDownloadProgress(qint64 curr, qint64 total);
    void _firmwareDownloadFinished(QString remoteFile, QString localFile);
    void _firmwareDownloadError(QString errorMsg);
167
    void _foundBoard(bool firstAttempt, const QSerialPortInfo& portInfo, int boardType, QString boardName);
168 169
    void _noBoardFound(void);
    void _boardGone();
170
    void _foundBootloader(int bootloaderVersion, int boardID, int flashSize);
171 172
    void _error(const QString& errorString);
    void _status(const QString& statusString);
173
    void _bootloaderSyncFailed(void);
174
    void _flashComplete(void);
175
    void _updateProgress(int curr, int total);
176 177
    void _eraseStarted(void);
    void _eraseComplete(void);
178
    void _eraseProgressTick(void);
179
    void _apmVersionDownloadFinished(QString remoteFile, QString localFile);
180 181
    void _px4ReleasesGithubDownloadFinished(QString remoteFile, QString localFile);
    void _px4ReleasesGithubDownloadError(QString errorMsg);
182 183

private:
184
    void _getFirmwareFile(FirmwareIdentifier firmwareId);
Pritam Ghanghas's avatar
Pritam Ghanghas committed
185
    void _initFirmwareHash();
186
    void _downloadFirmware(void);
187 188
    void _appendStatusLog(const QString& text, bool critical = false);
    void _errorCancel(const QString& msg);
189
    void _loadAPMVersions(uint32_t bootloaderBoardID);
190
    QHash<FirmwareIdentifier, QString>* _firmwareHashForBoardId(int boardId);
Don Gagne's avatar
Don Gagne committed
191
    void _determinePX4StableVersion(void);
192

193 194 195
    QString _portName;
    QString _portDescription;

Pritam Ghanghas's avatar
Pritam Ghanghas committed
196
    // firmware hashes
Lorenz Meier's avatar
Lorenz Meier committed
197
    QHash<FirmwareIdentifier, QString> _rgPX4FMUV4Firmware;
Pritam Ghanghas's avatar
Pritam Ghanghas committed
198 199 200
    QHash<FirmwareIdentifier, QString> _rgPX4FMUV2Firmware;
    QHash<FirmwareIdentifier, QString> _rgAeroCoreFirmware;
    QHash<FirmwareIdentifier, QString> _rgPX4FMUV1Firmware;
201
    QHash<FirmwareIdentifier, QString> _rgAUAVX2_1Firmware;
Henry Zhang's avatar
Henry Zhang committed
202
    QHash<FirmwareIdentifier, QString> _rgMindPXFMUV2Firmware;
203 204
    QHash<FirmwareIdentifier, QString> _rgTAPV1Firmware;
    QHash<FirmwareIdentifier, QString> _rgASCV1Firmware;
Pritam Ghanghas's avatar
Pritam Ghanghas committed
205 206 207
    QHash<FirmwareIdentifier, QString> _rgPX4FLowFirmware;
    QHash<FirmwareIdentifier, QString> _rg3DRRadioFirmware;

208 209 210
    QMap<FirmwareType_t, QMap<FirmwareVehicleType_t, QString> > _apmVersionMap;
    QList<FirmwareVehicleType_t>                                _apmVehicleTypeFromCurrentVersionList;

211 212 213 214 215 216
    /// Information which comes back from the bootloader
    bool        _bootloaderFound;           ///< true: we have received the foundBootloader signals
    uint32_t    _bootloaderVersion;         ///< Bootloader version
    uint32_t    _bootloaderBoardID;         ///< Board ID
    uint32_t    _bootloaderBoardFlashSize;  ///< Flash size in bytes of board
    
217 218
    bool                 _startFlashWhenBootloaderFound;
    FirmwareIdentifier   _startFlashWhenBootloaderFoundFirmwareIdentity;
219 220 221 222 223 224 225 226

    QPixmap _boardIcon;             ///< Icon used to display image of board
    
    QString _firmwareFilename;      ///< Image which we are going to flash to the board
    
    QNetworkAccessManager*  _downloadManager;       ///< Used for firmware file downloading across the internet
    QNetworkReply*          _downloadNetworkReply;  ///< Used for firmware file downloading across the internet
    
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
227
    /// @brief Thread controller which is used to run bootloader commands on separate thread
228 229 230 231 232 233 234 235 236 237
    PX4FirmwareUpgradeThreadController* _threadController;
    
    static const int    _eraseTickMsec = 500;       ///< Progress bar update tick time for erase
    static const int    _eraseTotalMsec = 15000;    ///< Estimated amount of time erase takes
    int                 _eraseTickCount;            ///< Number of ticks for erase progress update
    QTimer              _eraseTimer;                ///< Timer used to update progress bar for erase

    static const int    _findBoardTimeoutMsec = 30000;      ///< Amount of time for user to plug in USB
    static const int    _findBootloaderTimeoutMsec = 5000;  ///< Amount time to look for bootloader
    
238
    QQuickItem*     _statusLog;         ///< Status log TextArea Qml control
239
    QQuickItem*     _progressBar;
240 241
    
    bool _searchingForBoard;    ///< true: searching for board, false: search for bootloader
242
    
243 244 245 246 247 248
    QSerialPortInfo                 _foundBoardInfo;
    QGCSerialPortInfo::BoardType_t  _foundBoardType;
    QString                         _foundBoardTypeName;

    FirmwareType_t                  _selectedFirmwareType;

249
    FirmwareImage*  _image;
Don Gagne's avatar
Don Gagne committed
250 251

    QString _px4StableVersion;  // Version strange for latest PX4 stable
252
    QString _px4BetaVersion;    // Version strange for latest PX4 beta
253 254
};

255
// global hashing function
256
uint qHash(const FirmwareUpgradeController::FirmwareIdentifier& firmwareId);
257

258
#endif