PX4FirmwareUpgradeThread.h 5.69 KB
Newer Older
1 2
/****************************************************************************
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * 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
#include <QObject>
#include <QThread>
#include <QTimer>
#include <QTime>
26
#include <QSerialPort>
27

28 29
#include <stdint.h>

30
class PX4FirmwareUpgradeThreadController;
31

Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
32
/// @brief Used to run bootloader commands on a separate thread. These routines are mainly meant to to be called
33 34 35 36 37 38 39
///         internally by the PX4FirmwareUpgradeThreadController. Clients should call the various public methods
///         exposed by PX4FirmwareUpgradeThreadController.
class PX4FirmwareUpgradeThreadWorker : public QObject
{
    Q_OBJECT
    
public:
40
    PX4FirmwareUpgradeThreadWorker(PX4FirmwareUpgradeThreadController* controller);
41 42 43
    ~PX4FirmwareUpgradeThreadWorker();
    
signals:
44 45 46 47 48 49 50 51 52 53
    void updateProgress         (int curr, int total);
    void foundBoard             (bool firstAttempt, const QGCSerialPortInfo& portInfo, int type, QString boardName);
    void noBoardFound           (void);
    void boardGone              (void);
    void foundBoardInfo         (int bootloaderVersion, int boardID, int flashSize);
    void error                  (const QString& errorString);
    void status                 (const QString& statusText);
    void eraseStarted           (void);
    void eraseComplete          (void);
    void flashComplete          (void);
54 55
    
private slots:
56
    void _init              (void);
57
    void _startFindBoardLoop(void);
58 59 60 61 62
    void _reboot            (void);
    void _flash             (void);
    void _findBoardOnce     (void);
    void _updateProgress    (int curr, int total) { emit updateProgress(curr, total); }
    void _cancel            (void);
63 64
    
private:
65
    bool _findBoardFromPorts(QGCSerialPortInfo& portInfo, QGCSerialPortInfo::BoardType_t& boardType, QString& boardName);
66
    bool _erase             (void);
67 68 69
    
    PX4FirmwareUpgradeThreadController* _controller;
    
70 71
    Bootloader*         _bootloader             = nullptr;
    QTimer*             _findBoardTimer         = nullptr;
72
    QTime               _elapsed;
73 74 75 76
    bool                _foundBoard             = false;
    bool                _boardIsSiKRadio        = false;
    bool                _findBoardFirstAttempt  = true;     ///< true: we found the board right away, it needs to be unplugged and plugged back in
    QGCSerialPortInfo   _foundBoardPortInfo;                ///< port info for found board
77 78 79
};

/// @brief Provides methods to interact with the bootloader. The commands themselves are signalled
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
80
///         across to PX4FirmwareUpgradeThreadWorker so that they run on the separate thread.
81 82 83 84 85
class PX4FirmwareUpgradeThreadController : public QObject
{
    Q_OBJECT
    
public:
86
    PX4FirmwareUpgradeThreadController(QObject* parent = nullptr);
87 88
    ~PX4FirmwareUpgradeThreadController(void);
    
89 90 91
    /// @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);
92
    
93
    void cancel(void);
94 95
    
    /// @brief Sends a reboot command to the bootloader
96
    void reboot(void) { emit _rebootOnThread(); }
97
    
98
    void flash(const FirmwareImage* image);
99
    
100
    const FirmwareImage* image(void) { return _image; }
101 102
    
signals:
103 104 105 106 107 108 109 110 111 112
    void foundBoard     (bool firstAttempt, const QGCSerialPortInfo &portInfo, int boardType, QString boardName);
    void noBoardFound   (void);
    void boardGone      (void);
    void foundBoardInfo (int bootloaderVersion, int boardID, int flashSize);
    void error          (const QString& errorString);
    void status         (const QString& status);
    void eraseStarted   (void);
    void eraseComplete  (void);
    void flashComplete  (void);
    void updateProgress (int curr, int total);
113
    
114
    // Internal signals to communicate with thread worker
115
    void _initThreadWorker          (void);
116
    void _startFindBoardLoopOnThread(void);
117 118 119
    void _rebootOnThread            (void);
    void _flashOnThread             (void);
    void _cancel                    (void);
120 121
    
private slots:
122 123 124 125 126 127 128 129 130
    void _foundBoard            (bool firstAttempt, const QGCSerialPortInfo& portInfo, int type, QString name) { emit foundBoard(firstAttempt, portInfo, type, name); }
    void _noBoardFound          (void) { emit noBoardFound(); }
    void _boardGone             (void) { emit boardGone(); }
    void _foundBoardInfo        (int bootloaderVersion, int boardID, int flashSize) { emit foundBoardInfo(bootloaderVersion, boardID, flashSize); }
    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(); }
131 132
    
private:
133 134
    void _updateProgress(int curr, int total) { emit updateProgress(curr, total); }
    
135 136
    PX4FirmwareUpgradeThreadWorker* _worker         = nullptr;
    QThread*                        _workerThread   = nullptr;  ///< Thread which PX4FirmwareUpgradeThreadWorker runs on
137 138
    
    const FirmwareImage* _image;
139 140 141
};

#endif