FirmwareUpgradeController.cc 47.1 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/
9

10 11 12 13 14 15

/// @file
///     @brief PX4 Firmware Upgrade UI
///     @author Don Gagne <don@thegagnes.com>

#include "FirmwareUpgradeController.h"
16
#include "Bootloader.h"
Gus Grubba's avatar
Gus Grubba committed
17
//-- TODO: #include "QGCQFileDialog.h"
18
#include "QGCApplication.h"
19
#include "QGCFileDownload.h"
20 21
#include "QGCOptions.h"
#include "QGCCorePlugin.h"
Pritam Ghanghas's avatar
Pritam Ghanghas committed
22

Don Gagne's avatar
Don Gagne committed
23 24
#include <QStandardPaths>
#include <QRegularExpression>
Don Gagne's avatar
Don Gagne committed
25 26
#include <QJsonDocument>
#include <QJsonObject>
27
#include <QJsonArray>
28
#include <QNetworkProxy>
Don Gagne's avatar
Don Gagne committed
29

Pritam Ghanghas's avatar
Pritam Ghanghas committed
30 31 32 33 34 35 36
struct FirmwareToUrlElement_t {
    FirmwareUpgradeController::AutoPilotStackType_t    stackType;
    FirmwareUpgradeController::FirmwareType_t          firmwareType;
    FirmwareUpgradeController::FirmwareVehicleType_t   vehicleType;
    QString                                            url;
};

37
uint qHash(const FirmwareUpgradeController::FirmwareIdentifier& firmwareId)
38
{
39
    return static_cast<uint>(( firmwareId.autopilotStackType |
40
             (firmwareId.firmwareType << 8) |
41
             (firmwareId.firmwareVehicleType << 16) ));
42 43
}

44
/// @Brief Constructs a new FirmwareUpgradeController Widget. This widget is used within the PX4VehicleConfig set of screens.
45
FirmwareUpgradeController::FirmwareUpgradeController(void)
46 47 48 49 50 51 52 53
    : _singleFirmwareURL                (qgcApp()->toolbox()->corePlugin()->options()->firmwareUpgradeSingleURL())
    , _singleFirmwareMode               (!_singleFirmwareURL.isEmpty())
    , _downloadManager                  (nullptr)
    , _downloadNetworkReply             (nullptr)
    , _statusLog                        (nullptr)
    , _selectedFirmwareType             (StableFirmware)
    , _image                            (nullptr)
    , _apmBoardDescriptionReplaceText   ("<APMBoardDescription>")
54 55 56 57
{
    _threadController = new PX4FirmwareUpgradeThreadController(this);
    Q_CHECK_PTR(_threadController);

58 59 60 61 62 63 64 65 66 67 68 69 70
    connect(_threadController, &PX4FirmwareUpgradeThreadController::foundBoard,             this, &FirmwareUpgradeController::_foundBoard);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::noBoardFound,           this, &FirmwareUpgradeController::_noBoardFound);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::boardGone,              this, &FirmwareUpgradeController::_boardGone);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::foundBootloader,        this, &FirmwareUpgradeController::_foundBootloader);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::bootloaderSyncFailed,   this, &FirmwareUpgradeController::_bootloaderSyncFailed);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::error,                  this, &FirmwareUpgradeController::_error);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::updateProgress,         this, &FirmwareUpgradeController::_updateProgress);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::status,                 this, &FirmwareUpgradeController::_status);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::eraseStarted,           this, &FirmwareUpgradeController::_eraseStarted);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::eraseComplete,          this, &FirmwareUpgradeController::_eraseComplete);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::flashComplete,          this, &FirmwareUpgradeController::_flashComplete);
    connect(_threadController, &PX4FirmwareUpgradeThreadController::updateProgress,         this, &FirmwareUpgradeController::_updateProgress);
    
71
    connect(&_eraseTimer, &QTimer::timeout, this, &FirmwareUpgradeController::_eraseProgressTick);
72 73

    _initFirmwareHash();
Don Gagne's avatar
Don Gagne committed
74
    _determinePX4StableVersion();
75 76
}

Don Gagne's avatar
Don Gagne committed
77 78 79 80 81
FirmwareUpgradeController::~FirmwareUpgradeController()
{
    qgcApp()->toolbox()->linkManager()->setConnectionsAllowed();
}

82
void FirmwareUpgradeController::startBoardSearch(void)
83
{
Don Gagne's avatar
Don Gagne committed
84 85 86 87
    LinkManager* linkMgr = qgcApp()->toolbox()->linkManager();

    linkMgr->setConnectionsSuspended(tr("Connect not allowed during Firmware Upgrade."));

88 89
    // FIXME: Why did we get here with active vehicle?
    if (!qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()) {
Don Gagne's avatar
Don Gagne committed
90 91 92
        // We have to disconnect any inactive links
        linkMgr->disconnectAll();
    }
93

94 95 96
    _bootloaderFound = false;
    _startFlashWhenBootloaderFound = false;
    _threadController->startFindBoardLoop();
97 98
}

99 100 101
void FirmwareUpgradeController::flash(AutoPilotStackType_t stackType,
                                      FirmwareType_t firmwareType,
                                      FirmwareVehicleType_t vehicleType)
102
{
103
    qCDebug(FirmwareUpgradeLog) << "_flash stackType:firmwareType:vehicleType" << stackType << firmwareType << vehicleType;
104
    FirmwareIdentifier firmwareId = FirmwareIdentifier(stackType, firmwareType, vehicleType);
105
    if (_bootloaderFound) {
106
        _getFirmwareFile(firmwareId);
107 108 109
    } else {
        // We haven't found the bootloader yet. Need to wait until then to flash
        _startFlashWhenBootloaderFound = true;
110
        _startFlashWhenBootloaderFoundFirmwareIdentity = firmwareId;
111
    }
112 113
}

114 115 116 117 118
void FirmwareUpgradeController::flash(const FirmwareIdentifier& firmwareId)
{
    flash(firmwareId.autopilotStackType, firmwareId.firmwareType, firmwareId.firmwareVehicleType);
}

Gus Grubba's avatar
Gus Grubba committed
119
void FirmwareUpgradeController::flashSingleFirmwareMode(FirmwareType_t firmwareType)
120
{
Gus Grubba's avatar
Gus Grubba committed
121
    flash(SingleFirmwareMode, firmwareType, DefaultVehicleFirmware);
122 123
}

124
void FirmwareUpgradeController::cancel(void)
125
{
126 127 128
    _eraseTimer.stop();
    _threadController->cancel();
}
129

130
void FirmwareUpgradeController::_foundBoard(bool firstAttempt, const QSerialPortInfo& info, int boardType, QString boardName)
131 132
{
    _foundBoardInfo = info;
133
    _foundBoardType = static_cast<QGCSerialPortInfo::BoardType_t>(boardType);
134 135
    _foundBoardTypeName = boardName;
    _startFlashWhenBootloaderFound = false;
136

137
    if (_foundBoardType == QGCSerialPortInfo::BoardTypeSiKRadio) {
138 139 140 141 142 143 144 145
        if (!firstAttempt) {
            // Radio always flashes latest firmware, so we can start right away without
            // any further user input.
            _startFlashWhenBootloaderFound = true;
            _startFlashWhenBootloaderFoundFirmwareIdentity = FirmwareIdentifier(ThreeDRRadio,
                                                                                StableFirmware,
                                                                                DefaultVehicleFirmware);
        }
146
    }
147
    
148
    qCDebug(FirmwareUpgradeLog) << _foundBoardType << _foundBoardTypeName;
149
    emit boardFound();
150 151
}

152 153

void FirmwareUpgradeController::_noBoardFound(void)
154
{
155 156 157 158 159 160
    emit noBoardFound();
}

void FirmwareUpgradeController::_boardGone(void)
{
    emit boardGone();
161 162 163 164 165 166
}

/// @brief Called when the bootloader is connected to by the findBootloader process. Moves the state machine
///         to the next step.
void FirmwareUpgradeController::_foundBootloader(int bootloaderVersion, int boardID, int flashSize)
{
167
    _bootloaderFound = true;
168 169 170
    _bootloaderVersion = static_cast<uint32_t>(bootloaderVersion);
    _bootloaderBoardID = static_cast<uint32_t>(boardID);
    _bootloaderBoardFlashSize = static_cast<uint32_t>(flashSize);
171
    
172 173 174 175
    _appendStatusLog(tr("Connected to bootloader:"));
    _appendStatusLog(tr("  Version: %1").arg(_bootloaderVersion));
    _appendStatusLog(tr("  Board ID: %1").arg(_bootloaderBoardID));
    _appendStatusLog(tr("  Flash size: %1").arg(_bootloaderBoardFlashSize));
176
    
177
    if (_startFlashWhenBootloaderFound) {
178
        flash(_startFlashWhenBootloaderFoundFirmwareIdentity);
179
    }
180 181

    _loadAPMVersions(_bootloaderBoardID);
182 183 184
}


Pritam Ghanghas's avatar
Pritam Ghanghas committed
185 186 187
/// @brief intializes the firmware hashes with proper urls.
/// This happens only once for a class instance first time when it is needed.
void FirmwareUpgradeController::_initFirmwareHash()
188
{
Pritam Ghanghas's avatar
Pritam Ghanghas committed
189 190 191 192 193
    // indirect check whether this function has been called before or not
    // may have to be modified if _rgPX4FMUV2Firmware disappears
    if (!_rgPX4FMUV2Firmware.isEmpty()) {
        return;
    }
194

Pritam Ghanghas's avatar
Pritam Ghanghas committed
195 196 197 198 199
    //////////////////////////////////// PX4FMU aerocore firmwares //////////////////////////////////////////////////
    FirmwareToUrlElement_t  rgAeroCoreFirmwareArray[] = {
        { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://gumstix-aerocore.s3.amazonaws.com/PX4/stable/aerocore_default.px4"},
        { AutoPilotStackPX4, BetaFirmware,      DefaultVehicleFirmware, "http://gumstix-aerocore.s3.amazonaws.com/PX4/beta/aerocore_default.px4"},
        { AutoPilotStackPX4, DeveloperFirmware, DefaultVehicleFirmware, "http://gumstix-aerocore.s3.amazonaws.com/PX4/master/aerocore_default.px4"},
200
    #if !defined(NO_ARDUPILOT_DIALECT)
Don Gagne's avatar
Don Gagne committed
201
        { AutoPilotStackAPM, BetaFirmware,      CopterFirmware,         "http://firmware.ardupilot.org/Copter/beta/PX4/ArduCopter-v2.px4"},
Pritam Ghanghas's avatar
Pritam Ghanghas committed
202 203 204
        { AutoPilotStackAPM, StableFirmware,    HeliFirmware,           "http://gumstix-aerocore.s3.amazonaws.com/Copter/stable/PX4-heli/ArduCopter-v2.px4"},
        { AutoPilotStackAPM, StableFirmware,    PlaneFirmware,          "http://gumstix-aerocore.s3.amazonaws.com/Plane/stable/PX4/ArduPlane-v2.px4"},
        { AutoPilotStackAPM, StableFirmware,    RoverFirmware,          "http://gumstix-aerocore.s3.amazonaws.com/Rover/stable/PX4/APMrover2-v2.px4"},
205 206 207
        { AutoPilotStackAPM, BetaFirmware,      HeliFirmware,           "http://firmware.ardupilot.org/Copter/beta/PX4-heli/ArduCopter-v2.px4"},
        { AutoPilotStackAPM, BetaFirmware,      PlaneFirmware,          "http://firmware.ardupilot.org/Plane/beta/PX4/ArduPlane-v2.px4"},
        { AutoPilotStackAPM, BetaFirmware,      RoverFirmware,          "http://firmware.ardupilot.org/Rover/beta/PX4/APMrover2-v2.px4"},
Don Gagne's avatar
Don Gagne committed
208
        { AutoPilotStackAPM, DeveloperFirmware, CopterFirmware,         "http://gumstix-aerocore.s3.amazonaws.com/Copter/latest/PX4/ArduCopter-v2.px4"},
Pritam Ghanghas's avatar
Pritam Ghanghas committed
209 210 211
        { AutoPilotStackAPM, DeveloperFirmware, HeliFirmware,           "http://gumstix-aerocore.s3.amazonaws.com/Copter/latest/PX4-heli/ArduCopter-v2.px4"},
        { AutoPilotStackAPM, DeveloperFirmware, PlaneFirmware,          "http://gumstix-aerocore.s3.amazonaws.com/Plane/latest/PX4/ArduPlane-v2.px4"},
        { AutoPilotStackAPM, DeveloperFirmware, RoverFirmware,          "http://gumstix-aerocore.s3.amazonaws.com/Rover/latest/PX4/APMrover2-v2.px4"}
212
    #endif
Pritam Ghanghas's avatar
Pritam Ghanghas committed
213
    };
214

215 216 217 218 219
    //////////////////////////////////// AUAVX2_1 firmwares //////////////////////////////////////////////////
    FirmwareToUrlElement_t rgAUAVX2_1FirmwareArray[] = {
        { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/stable/auav-x21_default.px4"},
        { AutoPilotStackPX4, BetaFirmware,      DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/beta/auav-x21_default.px4"},
        { AutoPilotStackPX4, DeveloperFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/master/auav-x21_default.px4"},
220
    #if !defined(NO_ARDUPILOT_DIALECT)
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        { AutoPilotStackAPM, StableFirmware,    CopterFirmware,         "http://firmware.ardupilot.org/Copter/stable/PX4/ArduCopter-v3.px4"},
        { AutoPilotStackAPM, StableFirmware,    HeliFirmware,           "http://firmware.ardupilot.org/Copter/stable/PX4-heli/ArduCopter-v3.px4"},
        { AutoPilotStackAPM, StableFirmware,    PlaneFirmware,          "http://firmware.ardupilot.org/Plane/stable/PX4/ArduPlane-v2.px4"},
        { AutoPilotStackAPM, StableFirmware,    RoverFirmware,          "http://firmware.ardupilot.org/Rover/stable/PX4/APMrover2-v2.px4"},
        { AutoPilotStackAPM, StableFirmware,    SubFirmware,            "http://firmware.ardupilot.org/Sub/stable/PX4/ArduSub-v2.px4"},
        { AutoPilotStackAPM, BetaFirmware,      CopterFirmware,         "http://firmware.ardupilot.org/Copter/beta/PX4/ArduCopter-v3.px4"},
        { AutoPilotStackAPM, BetaFirmware,      HeliFirmware,           "http://firmware.ardupilot.org/Copter/beta/PX4-heli/ArduCopter-v3.px4"},
        { AutoPilotStackAPM, BetaFirmware,      PlaneFirmware,          "http://firmware.ardupilot.org/Plane/beta/PX4/ArduPlane-v3.px4"},
        { AutoPilotStackAPM, BetaFirmware,      RoverFirmware,          "http://firmware.ardupilot.org/Rover/beta/PX4/APMrover2-v3.px4"},
        { AutoPilotStackAPM, BetaFirmware,      SubFirmware,            "http://firmware.ardupilot.org/Sub/beta/PX4/ArduSub-v3.px4"},
        { AutoPilotStackAPM, DeveloperFirmware, CopterFirmware,         "http://firmware.ardupilot.org/Copter/latest/PX4/ArduCopter-v3.px4"},
        { AutoPilotStackAPM, DeveloperFirmware, HeliFirmware,           "http://firmware.ardupilot.org/Copter/latest/PX4-heli/ArduCopter-v3.px4"},
        { AutoPilotStackAPM, DeveloperFirmware, PlaneFirmware,          "http://firmware.ardupilot.org/Plane/latest/PX4/ArduPlane-v3.px4"},
        { AutoPilotStackAPM, DeveloperFirmware, RoverFirmware,          "http://firmware.ardupilot.org/Rover/latest/PX4/APMrover2-v3.px4"},
        { AutoPilotStackAPM, DeveloperFirmware, SubFirmware,            "http://firmware.ardupilot.org/Sub/latest/PX4/ArduSub-v3.px4"}
236
    #endif
237
    };
Henry Zhang's avatar
Henry Zhang committed
238 239 240 241 242 243
    //////////////////////////////////// MindPXFMUV2 firmwares //////////////////////////////////////////////////
    FirmwareToUrlElement_t rgMindPXFMUV2FirmwareArray[] = {
        { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/stable/mindpx-v2_default.px4"},
        { AutoPilotStackPX4, BetaFirmware,      DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/beta/mindpx-v2_default.px4"},
        { AutoPilotStackPX4, DeveloperFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/master/mindpx-v2_default.px4"},
    };
244 245 246 247 248
    //////////////////////////////////// TAPV1 firmwares //////////////////////////////////////////////////
    FirmwareToUrlElement_t rgTAPV1FirmwareArray[] = {
        { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/stable/tap-v1_default.px4"},
        { AutoPilotStackPX4, BetaFirmware,      DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/beta/tap-v1_default.px4"},
        { AutoPilotStackPX4, DeveloperFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/master/tap-v1_default.px4"},
249
        { SingleFirmwareMode,StableFirmware,    DefaultVehicleFirmware, _singleFirmwareURL},
250 251 252 253 254 255 256
    };
    //////////////////////////////////// ASCV1 firmwares //////////////////////////////////////////////////
    FirmwareToUrlElement_t rgASCV1FirmwareArray[] = {
        { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/stable/asc-v1_default.px4"},
        { AutoPilotStackPX4, BetaFirmware,      DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/beta/asc-v1_default.px4"},
        { AutoPilotStackPX4, DeveloperFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/master/asc-v1_default.px4"},
    };
257 258 259 260 261 262 263 264

    //////////////////////////////////// Crazyflie 2.0 firmwares //////////////////////////////////////////////////
    FirmwareToUrlElement_t rgCrazyflie2FirmwareArray[] = {
        { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/stable/crazyflie_default.px4"},
        { AutoPilotStackPX4, BetaFirmware,      DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/beta/crazyflie_default.px4"},
        { AutoPilotStackPX4, DeveloperFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/master/crazyflie_default.px4"},
    };

265 266 267 268 269
    FirmwareToUrlElement_t rgOmnibusF4SDFirmwareArray[] = {
        { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/stable/omnibus_f4sd_default.px4"},
        { AutoPilotStackPX4, BetaFirmware,      DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/beta/omnibus_f4sd_default.px4"},
        { AutoPilotStackPX4, DeveloperFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/master/omnibus_f4sd_default.px4"},
    };
270 271 272 273 274 275 276
    
    //////////////////////////////////// FMUK66V3 firmwares //////////////////////////////////////////////////
    FirmwareToUrlElement_t rgFMUK66V3FirmwareArray[] = {
        { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/stable/nxp_fmuk66-v3_default.px4"},
        { AutoPilotStackPX4, BetaFirmware,      DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/beta/nxp_fmuk66-v3_default.px4"},
        { AutoPilotStackPX4, DeveloperFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/master/nxp_fmuk66-v3_default.px4"},
    };
277

Pritam Ghanghas's avatar
Pritam Ghanghas committed
278 279
    /////////////////////////////// px4flow firmwares ///////////////////////////////////////
    FirmwareToUrlElement_t rgPX4FLowFirmwareArray[] = {
280
        { PX4FlowPX4, StableFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Flow/master/px4flow.px4" },
281
    #if !defined(NO_ARDUPILOT_DIALECT)
DonLakeFlyer's avatar
DonLakeFlyer committed
282
        { PX4FlowAPM, StableFirmware, DefaultVehicleFirmware, "http://firmware.ardupilot.org/Tools/PX4Flow/px4flow-klt-latest.px4" },
283
    #endif
Pritam Ghanghas's avatar
Pritam Ghanghas committed
284 285 286 287
    };

    /////////////////////////////// 3dr radio firmwares ///////////////////////////////////////
    FirmwareToUrlElement_t rg3DRRadioFirmwareArray[] = {
288
        { ThreeDRRadio, StableFirmware, DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/SiK/stable/radio~hm_trp.ihx"}
Pritam Ghanghas's avatar
Pritam Ghanghas committed
289 290
    };

Don Gagne's avatar
Don Gagne committed
291 292 293 294 295 296 297 298 299 300
    // We build the maps for PX4 and ArduPilot firmwares dynamically using the data below

#if 0
    Example URLs for PX4 and ArduPilot
    { AutoPilotStackPX4, StableFirmware,    DefaultVehicleFirmware, "http://px4-travis.s3.amazonaws.com/Firmware/stable/px4fmu-v4_default.px4"},
    { AutoPilotStackAPM, StableFirmware,    CopterFirmware,         "http://firmware.ardupilot.org/Copter/stable/PX4/ArduCopter-v4.px4"},
    { AutoPilotStackAPM, DeveloperFirmware, CopterChibiosFirmware,  "http://firmware.ardupilot.org/Copter/latest/fmuv4/arducopter.apj"},
#endif

    QString px4Url          ("http://px4-travis.s3.amazonaws.com/Firmware/%1/px4fmu-%2_default.px4");
Don Gagne's avatar
Don Gagne committed
301
    QString apmUrl          ("http://firmware.ardupilot.org/%1/%2/%3/%4-v%5.px4");
Don Gagne's avatar
Don Gagne committed
302 303 304
    QString apmChibiOSUrl   ("http://firmware.ardupilot.org/%1/%2/fmuv%3%4/%5.apj");

    QMap<FirmwareType_t, QString> px4MapFirmwareTypeToDir;
Don Gagne's avatar
Don Gagne committed
305 306 307
    px4MapFirmwareTypeToDir[StableFirmware] =       QStringLiteral("stable");
    px4MapFirmwareTypeToDir[BetaFirmware] =         QStringLiteral("beta");
    px4MapFirmwareTypeToDir[DeveloperFirmware] =    QStringLiteral("master");
Don Gagne's avatar
Don Gagne committed
308

309
#if !defined(NO_ARDUPILOT_DIALECT)
Don Gagne's avatar
Don Gagne committed
310
    QMap<FirmwareVehicleType_t, QString> apmMapVehicleTypeToDir;
Don Gagne's avatar
Don Gagne committed
311 312 313 314 315
    apmMapVehicleTypeToDir[CopterFirmware] =    QStringLiteral("Copter");
    apmMapVehicleTypeToDir[HeliFirmware] =      QStringLiteral("Copter");
    apmMapVehicleTypeToDir[PlaneFirmware] =     QStringLiteral("Plane");
    apmMapVehicleTypeToDir[RoverFirmware] =     QStringLiteral("Rover");
    apmMapVehicleTypeToDir[SubFirmware] =       QStringLiteral("Sub");
316
#endif
Don Gagne's avatar
Don Gagne committed
317

318
#if !defined(NO_ARDUPILOT_DIALECT)
Don Gagne's avatar
Don Gagne committed
319 320 321 322 323 324
    QMap<FirmwareVehicleType_t, QString> apmChibiOSMapVehicleTypeToDir;
    apmChibiOSMapVehicleTypeToDir[CopterChibiOSFirmware] =  QStringLiteral("Copter");
    apmChibiOSMapVehicleTypeToDir[HeliChibiOSFirmware] =    QStringLiteral("Copter");
    apmChibiOSMapVehicleTypeToDir[PlaneChibiOSFirmware] =   QStringLiteral("Plane");
    apmChibiOSMapVehicleTypeToDir[RoverChibiOSFirmware] =   QStringLiteral("Rover");
    apmChibiOSMapVehicleTypeToDir[SubChibiOSFirmware] =     QStringLiteral("Sub");
325
#endif
Don Gagne's avatar
Don Gagne committed
326

327
#if !defined(NO_ARDUPILOT_DIALECT)
Don Gagne's avatar
Don Gagne committed
328
    QMap<FirmwareType_t, QString> apmMapFirmwareTypeToDir;
Don Gagne's avatar
Don Gagne committed
329 330 331
    apmMapFirmwareTypeToDir[StableFirmware] =       QStringLiteral("stable");
    apmMapFirmwareTypeToDir[BetaFirmware] =         QStringLiteral("beta");
    apmMapFirmwareTypeToDir[DeveloperFirmware] =    QStringLiteral("latest");
332
#endif
Don Gagne's avatar
Don Gagne committed
333

334
#if !defined(NO_ARDUPILOT_DIALECT)
Don Gagne's avatar
Don Gagne committed
335
    QMap<FirmwareVehicleType_t, QString> apmMapVehicleTypeToPX4Dir;
Don Gagne's avatar
Don Gagne committed
336 337 338 339 340
    apmMapVehicleTypeToPX4Dir[CopterFirmware] = QStringLiteral("PX4");
    apmMapVehicleTypeToPX4Dir[HeliFirmware] =   QStringLiteral("PX4-heli");
    apmMapVehicleTypeToPX4Dir[PlaneFirmware] =  QStringLiteral("PX4");
    apmMapVehicleTypeToPX4Dir[RoverFirmware] =  QStringLiteral("PX4");
    apmMapVehicleTypeToPX4Dir[SubFirmware] =    QStringLiteral("PX4");
341
#endif
Don Gagne's avatar
Don Gagne committed
342

343
#if !defined(NO_ARDUPILOT_DIALECT)
Don Gagne's avatar
Don Gagne committed
344
    QMap<FirmwareVehicleType_t, QString> apmMapVehicleTypeToFilename;
Don Gagne's avatar
Don Gagne committed
345
    apmMapVehicleTypeToFilename[CopterFirmware] =   QStringLiteral("ArduCopter");
Don Gagne's avatar
Don Gagne committed
346
    apmMapVehicleTypeToFilename[HeliFirmware] =     QStringLiteral("ArduCopter");
Don Gagne's avatar
Don Gagne committed
347 348 349
    apmMapVehicleTypeToFilename[PlaneFirmware] =    QStringLiteral("ArduPlane");
    apmMapVehicleTypeToFilename[RoverFirmware] =    QStringLiteral("APMrover2");
    apmMapVehicleTypeToFilename[SubFirmware] =      QStringLiteral("ArduSub");
350
#endif
Don Gagne's avatar
Don Gagne committed
351

352
#if !defined(NO_ARDUPILOT_DIALECT)
Don Gagne's avatar
Don Gagne committed
353
    QMap<FirmwareVehicleType_t, QString> apmChibiOSMapVehicleTypeToFmuDir;
Don Gagne's avatar
Don Gagne committed
354 355 356 357 358
    apmChibiOSMapVehicleTypeToFmuDir[CopterChibiOSFirmware] =   QString();
    apmChibiOSMapVehicleTypeToFmuDir[HeliChibiOSFirmware] =     QStringLiteral("-heli");
    apmChibiOSMapVehicleTypeToFmuDir[PlaneChibiOSFirmware] =    QString();
    apmChibiOSMapVehicleTypeToFmuDir[RoverChibiOSFirmware] =    QString();
    apmChibiOSMapVehicleTypeToFmuDir[SubChibiOSFirmware] =      QString();
359
#endif
Don Gagne's avatar
Don Gagne committed
360

361
#if !defined(NO_ARDUPILOT_DIALECT)
Don Gagne's avatar
Don Gagne committed
362
    QMap<FirmwareVehicleType_t, QString> apmChibiOSMapVehicleTypeToFilename;
Don Gagne's avatar
Don Gagne committed
363
    apmChibiOSMapVehicleTypeToFilename[CopterChibiOSFirmware] = QStringLiteral("arducopter");
Don Gagne's avatar
Don Gagne committed
364
    apmChibiOSMapVehicleTypeToFilename[HeliChibiOSFirmware] =   QStringLiteral("arducopter-heli");
Don Gagne's avatar
Don Gagne committed
365 366 367
    apmChibiOSMapVehicleTypeToFilename[PlaneChibiOSFirmware] =  QStringLiteral("arduplane");
    apmChibiOSMapVehicleTypeToFilename[RoverChibiOSFirmware] =  QStringLiteral("ardurover");
    apmChibiOSMapVehicleTypeToFilename[SubChibiOSFirmware] =    QStringLiteral("ardusub");
368
#endif
Don Gagne's avatar
Don Gagne committed
369 370

    // PX4 Firmwares
371
    for (const FirmwareType_t& firmwareType: px4MapFirmwareTypeToDir.keys()) {
Don Gagne's avatar
Don Gagne committed
372
        QString dir = px4MapFirmwareTypeToDir[firmwareType];
373 374 375 376 377
        _rgFMUV5Firmware.insert     (FirmwareIdentifier(AutoPilotStackPX4, firmwareType, DefaultVehicleFirmware), px4Url.arg(dir).arg("v5"));
        _rgFMUV4PROFirmware.insert  (FirmwareIdentifier(AutoPilotStackPX4, firmwareType, DefaultVehicleFirmware), px4Url.arg(dir).arg("v4pro"));
        _rgFMUV4Firmware.insert     (FirmwareIdentifier(AutoPilotStackPX4, firmwareType, DefaultVehicleFirmware), px4Url.arg(dir).arg("v4"));
        _rgFMUV3Firmware.insert     (FirmwareIdentifier(AutoPilotStackPX4, firmwareType, DefaultVehicleFirmware), px4Url.arg(dir).arg("v3"));
        _rgPX4FMUV2Firmware.insert  (FirmwareIdentifier(AutoPilotStackPX4, firmwareType, DefaultVehicleFirmware), px4Url.arg(dir).arg("v2"));
Don Gagne's avatar
Don Gagne committed
378 379
    }

380
#if !defined(NO_ARDUPILOT_DIALECT)
381
    // ArduPilot non-ChibiOS Firmwares for direct board id to fmu mappings
382
    for (const FirmwareType_t& firmwareType: apmMapFirmwareTypeToDir.keys()) {
Don Gagne's avatar
Don Gagne committed
383
        QString firmwareTypeDir = apmMapFirmwareTypeToDir[firmwareType];
384
        for (const FirmwareVehicleType_t& vehicleType: apmMapVehicleTypeToDir.keys()) {
Don Gagne's avatar
Don Gagne committed
385 386 387
            QString vehicleTypeDir = apmMapVehicleTypeToDir[vehicleType];
            QString px4Dir = apmMapVehicleTypeToPX4Dir[vehicleType];
            QString filename = apmMapVehicleTypeToFilename[vehicleType];
388 389 390
            _rgFMUV5Firmware.insert     (FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), apmUrl.arg(vehicleTypeDir).arg(firmwareTypeDir).arg(px4Dir).arg(filename).arg("5"));
            _rgFMUV4Firmware.insert     (FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), apmUrl.arg(vehicleTypeDir).arg(firmwareTypeDir).arg(px4Dir).arg(filename).arg("4"));
            _rgFMUV3Firmware.insert     (FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), apmUrl.arg(vehicleTypeDir).arg(firmwareTypeDir).arg(px4Dir).arg(filename).arg("3"));
Don Gagne's avatar
Don Gagne committed
391 392
            _rgPX4FMUV2Firmware.insert  (FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), apmUrl.arg(vehicleTypeDir).arg(firmwareTypeDir).arg(px4Dir).arg(filename).arg("2"));
        }
DonLakeFlyer's avatar
DonLakeFlyer committed
393 394
    }

395
    // ArduPilot ChibiOS Firmwares for direct board id to fmu mappings. Used when bootloader is not new ArduPilot bootloader.
396
    for (const FirmwareType_t& firmwareType: apmMapFirmwareTypeToDir.keys()) {
Don Gagne's avatar
Don Gagne committed
397
        QString firmwareTypeDir = apmMapFirmwareTypeToDir[firmwareType];
398
        for (const FirmwareVehicleType_t& vehicleType: apmChibiOSMapVehicleTypeToDir.keys()) {
Don Gagne's avatar
Don Gagne committed
399 400 401
            QString vehicleTypeDir = apmChibiOSMapVehicleTypeToDir[vehicleType];
            QString fmuDir = apmChibiOSMapVehicleTypeToFmuDir[vehicleType];
            QString filename = apmChibiOSMapVehicleTypeToFilename[vehicleType];
402 403 404 405
            _rgFMUV5Firmware.insert     (FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), apmChibiOSUrl.arg(vehicleTypeDir).arg(firmwareTypeDir).arg("5").arg(fmuDir).arg(filename));
            _rgFMUV4Firmware.insert     (FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), apmChibiOSUrl.arg(vehicleTypeDir).arg(firmwareTypeDir).arg("4").arg(fmuDir).arg(filename));
            _rgFMUV3Firmware.insert     (FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), apmChibiOSUrl.arg(vehicleTypeDir).arg(firmwareTypeDir).arg("3").arg(fmuDir).arg(filename));
            _rgPX4FMUV2Firmware.insert  (FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), apmChibiOSUrl.arg(vehicleTypeDir).arg(firmwareTypeDir).arg("2").arg(fmuDir).arg(filename));
Don Gagne's avatar
Don Gagne committed
406
        }
Pritam Ghanghas's avatar
Pritam Ghanghas committed
407
    }
408

409
    // ArduPilot ChibiOS Firmwares when board id is an unknown type but follows ArduPilot port info naming conventions.
410
    // This is only used if the board is using the new ArduPilot bootloader port naming scheme.
411 412 413 414 415 416 417 418 419 420 421 422
    for (const FirmwareType_t& firmwareType: apmMapFirmwareTypeToDir.keys()) {
        QString firmwareTypeDir = apmMapFirmwareTypeToDir[firmwareType];
        for (const FirmwareVehicleType_t& vehicleType: apmChibiOSMapVehicleTypeToDir.keys()) {
            QString namedURL("http://firmware.ardupilot.org/%1/%2/%3%4/%5.apj");
            QString vehicleTypeDir = apmChibiOSMapVehicleTypeToDir[vehicleType];
            QString fmuDir = apmChibiOSMapVehicleTypeToFmuDir[vehicleType];
            QString filename = apmChibiOSMapVehicleTypeToFilename[vehicleType];
            _rgAPMChibiosReplaceNamedBoardFirmware.insert(FirmwareIdentifier(AutoPilotStackAPM, firmwareType, vehicleType), namedURL.arg(vehicleTypeDir).arg(firmwareTypeDir).arg(_apmBoardDescriptionReplaceText).arg(fmuDir).arg(filename));
        }
    }
#endif

Don Gagne's avatar
Don Gagne committed
423
    int size = sizeof(rgAeroCoreFirmwareArray)/sizeof(rgAeroCoreFirmwareArray[0]);
Pritam Ghanghas's avatar
Pritam Ghanghas committed
424 425 426 427
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgAeroCoreFirmwareArray[i];
        _rgAeroCoreFirmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }
428

429 430 431 432 433 434
    size = sizeof(rgAUAVX2_1FirmwareArray)/sizeof(rgAUAVX2_1FirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgAUAVX2_1FirmwareArray[i];
        _rgAUAVX2_1Firmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }

Henry Zhang's avatar
Henry Zhang committed
435 436 437 438 439 440
    size = sizeof(rgMindPXFMUV2FirmwareArray)/sizeof(rgMindPXFMUV2FirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgMindPXFMUV2FirmwareArray[i];
        _rgMindPXFMUV2Firmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }

441 442 443 444 445 446 447 448 449 450 451 452
    size = sizeof(rgTAPV1FirmwareArray)/sizeof(rgTAPV1FirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgTAPV1FirmwareArray[i];
        _rgTAPV1Firmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }

    size = sizeof(rgASCV1FirmwareArray)/sizeof(rgASCV1FirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgASCV1FirmwareArray[i];
        _rgASCV1Firmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }

453 454 455 456 457 458
    size = sizeof(rgCrazyflie2FirmwareArray)/sizeof(rgCrazyflie2FirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgCrazyflie2FirmwareArray[i];
        _rgCrazyflie2Firmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }

459 460 461 462 463
    size = sizeof(rgOmnibusF4SDFirmwareArray)/sizeof(rgOmnibusF4SDFirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgOmnibusF4SDFirmwareArray[i];
        _rgOmnibusF4SDFirmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }
464 465 466 467 468 469
    
    size = sizeof(rgFMUK66V3FirmwareArray)/sizeof(rgFMUK66V3FirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgFMUK66V3FirmwareArray[i];
        _rgFMUK66V3Firmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }
470

Pritam Ghanghas's avatar
Pritam Ghanghas committed
471 472 473 474 475
    size = sizeof(rgPX4FLowFirmwareArray)/sizeof(rgPX4FLowFirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rgPX4FLowFirmwareArray[i];
        _rgPX4FLowFirmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }
476

Pritam Ghanghas's avatar
Pritam Ghanghas committed
477 478 479 480 481
    size = sizeof(rg3DRRadioFirmwareArray)/sizeof(rg3DRRadioFirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rg3DRRadioFirmwareArray[i];
        _rg3DRRadioFirmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }
482 483 484 485 486 487

    size = sizeof(rg3DRRadioFirmwareArray)/sizeof(rg3DRRadioFirmwareArray[0]);
    for (int i = 0; i < size; i++) {
        const FirmwareToUrlElement_t& element = rg3DRRadioFirmwareArray[i];
        _rg3DRRadioFirmware.insert(FirmwareIdentifier(element.stackType, element.firmwareType, element.vehicleType), element.url);
    }
Pritam Ghanghas's avatar
Pritam Ghanghas committed
488
}
489

Pritam Ghanghas's avatar
Pritam Ghanghas committed
490 491 492 493 494 495
/// @brief Called when the findBootloader process is unable to sync to the bootloader. Moves the state
///         machine to the appropriate error state.
void FirmwareUpgradeController::_bootloaderSyncFailed(void)
{
    _errorCancel("Unable to sync with bootloader.");
}
496

497
QHash<FirmwareUpgradeController::FirmwareIdentifier, QString>* FirmwareUpgradeController::_firmwareHashForBoardId(int boardId)
Pritam Ghanghas's avatar
Pritam Ghanghas committed
498
{
499 500

    _rgFirmwareDynamic.clear();
501

502 503
    switch (boardId) {
    case Bootloader::boardIDPX4Flow:
504
        _rgFirmwareDynamic = _rgPX4FLowFirmware;
505
        break;
506
    case Bootloader::boardIDPX4FMUV2:
507
        _rgFirmwareDynamic = _rgPX4FMUV2Firmware;
508
        break;
DonLakeFlyer's avatar
DonLakeFlyer committed
509
    case Bootloader::boardIDPX4FMUV3:
510
        _rgFirmwareDynamic = _rgFMUV3Firmware;
511
        break;
512
    case Bootloader::boardIDPX4FMUV4:
513
        _rgFirmwareDynamic = _rgFMUV4Firmware;
514
        break;
515
    case Bootloader::boardIDPX4FMUV4PRO:
516
        _rgFirmwareDynamic = _rgFMUV4PROFirmware;
517
        break;
518
    case Bootloader::boardIDPX4FMUV5:
519
        _rgFirmwareDynamic = _rgFMUV5Firmware;
520
        break;
521
    case Bootloader::boardIDAeroCore:
522
        _rgFirmwareDynamic = _rgAeroCoreFirmware;
523
        break;
524
    case Bootloader::boardIDAUAVX2_1:
525
        _rgFirmwareDynamic = _rgAUAVX2_1Firmware;
526
        break;
Henry Zhang's avatar
Henry Zhang committed
527
    case Bootloader::boardIDMINDPXFMUV2:
528
        _rgFirmwareDynamic = _rgMindPXFMUV2Firmware;
529
        break;
530
    case Bootloader::boardIDTAPV1:
531
        _rgFirmwareDynamic = _rgTAPV1Firmware;
532
        break;
533
    case Bootloader::boardIDASCV1:
534
        _rgFirmwareDynamic = _rgASCV1Firmware;
535
        break;
536
    case Bootloader::boardIDCrazyflie2:
537
        _rgFirmwareDynamic = _rgCrazyflie2Firmware;
538
        break;
539
    case Bootloader::boardIDOmnibusF4SD:
540
        _rgFirmwareDynamic = _rgOmnibusF4SDFirmware;
541
        break;
542 543
    case Bootloader::boardIDFMUK66V3:
        _rgFirmwareDynamic = _rgFMUK66V3Firmware;
544
        break;
545
    case Bootloader::boardID3DRRadio:
546
        _rgFirmwareDynamic = _rg3DRRadioFirmware;
547
        break;
548
    default:
549 550
        // Unknown board id
        break;
551 552
    }

553 554 555 556 557
    // Check for ArduPilot ChibiOS bootloader
    QStringList rgManufacturers = { QStringLiteral("ArduPilot"), QStringLiteral("Hex/ProfiCNC"), QStringLiteral("Holybro") };
    QString apmDescriptionSuffix("-BL");
    if (rgManufacturers.contains(_foundBoardInfo.manufacturer()) && _foundBoardInfo.description().endsWith(apmDescriptionSuffix)) {
        // Board ios using a ChibiOS bootloader. Prefer naming scheme from that over board ids for ArduPilot entries.
558

559 560 561
        // First remove the ChibiOS by board id entries from the list
        for (const FirmwareIdentifier& firmwareId: _rgAPMChibiosReplaceNamedBoardFirmware.keys()) {
            _rgFirmwareDynamic.remove(firmwareId);
562 563
        }

564
        // Now add the ChibiOS by board description entries to the list
565 566
        for (const FirmwareIdentifier& firmwareId: _rgAPMChibiosReplaceNamedBoardFirmware.keys()) {
            QString namedUrl = _rgAPMChibiosReplaceNamedBoardFirmware[firmwareId];
567
            _rgFirmwareDynamic.insert(firmwareId, namedUrl.replace(_apmBoardDescriptionReplaceText, _foundBoardInfo.description().left(_foundBoardInfo.description().length() - apmDescriptionSuffix.length())));
568 569 570 571
        }
    }

    return &_rgFirmwareDynamic;
572
}
Lorenz Meier's avatar
Lorenz Meier committed
573

574 575 576
/// @brief Prompts the user to select a firmware file if needed and moves the state machine to the next state.
void FirmwareUpgradeController::_getFirmwareFile(FirmwareIdentifier firmwareId)
{
577
    QHash<FirmwareIdentifier, QString>* prgFirmware = _firmwareHashForBoardId(static_cast<int>(_bootloaderBoardID));
578
    
579
    if (firmwareId.firmwareType == CustomFirmware) {
Gus Grubba's avatar
Gus Grubba committed
580 581 582 583
        _firmwareFilename = QString(); //-- TODO: QGCQFileDialog::getOpenFileName(nullptr,                                                                // Parent to main window
                                                  //          tr("Select Firmware File"),                                             // Dialog Caption
                                                  //          QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),    // Initial directory
                                                  //          tr("Firmware Files (*.px4 *.apj *.bin *.ihx)"));                              // File filter
584
    } else {
585 586
        if (prgFirmware->contains(firmwareId)) {
            _firmwareFilename = prgFirmware->value(firmwareId);
587
        } else {
588
            _errorCancel(tr("Unable to find specified firmware for board type"));
589 590
            return;
        }
591 592 593
    }
    
    if (_firmwareFilename.isEmpty()) {
594
        _errorCancel(tr("No firmware file selected"));
595 596 597 598 599 600 601 602 603 604
    } else {
        _downloadFirmware();
    }
}

/// @brief Begins the process of downloading the selected firmware file.
void FirmwareUpgradeController::_downloadFirmware(void)
{
    Q_ASSERT(!_firmwareFilename.isEmpty());
    
605 606
    _appendStatusLog(tr("Downloading firmware..."));
    _appendStatusLog(tr(" From: %1").arg(_firmwareFilename));
607
    
608 609 610 611 612
    QGCFileDownload* downloader = new QGCFileDownload(this);
    connect(downloader, &QGCFileDownload::downloadFinished, this, &FirmwareUpgradeController::_firmwareDownloadFinished);
    connect(downloader, &QGCFileDownload::downloadProgress, this, &FirmwareUpgradeController::_firmwareDownloadProgress);
    connect(downloader, &QGCFileDownload::error,            this, &FirmwareUpgradeController::_firmwareDownloadError);
    downloader->download(_firmwareFilename);
613 614 615
}

/// @brief Updates the progress indicator while downloading
616
void FirmwareUpgradeController::_firmwareDownloadProgress(qint64 curr, qint64 total)
617 618 619
{
    // Take care of cases where 0 / 0 is emitted as error return value
    if (total > 0) {
620
        _progressBar->setProperty("value", static_cast<float>(curr) / static_cast<float>(total));
621 622 623 624
    }
}

/// @brief Called when the firmware download completes.
625
void FirmwareUpgradeController::_firmwareDownloadFinished(QString remoteFile, QString localFile)
626
{
627 628
    Q_UNUSED(remoteFile);

629
    _appendStatusLog(tr("Download complete"));
630
    
631
    FirmwareImage* image = new FirmwareImage(this);
632
    
633 634
    connect(image, &FirmwareImage::statusMessage, this, &FirmwareUpgradeController::_status);
    connect(image, &FirmwareImage::errorMessage, this, &FirmwareUpgradeController::_error);
635
    
636
    if (!image->load(localFile, _bootloaderBoardID)) {
637
        _errorCancel(tr("Image load failed"));
638
        return;
639 640
    }
    
641 642
    // We can't proceed unless we have the bootloader
    if (!_bootloaderFound) {
643
        _errorCancel(tr("Bootloader not found"));
644 645
        return;
    }
646
    
647
    if (_bootloaderBoardFlashSize != 0 && image->imageSize() > _bootloaderBoardFlashSize) {
648
        _errorCancel(tr("Image size of %1 is too large for board flash size %2").arg(image->imageSize()).arg(_bootloaderBoardFlashSize));
649
        return;
650
    }
651 652

    _threadController->flash(image);
653 654
}

655
/// @brief Called when an error occurs during download
656
void FirmwareUpgradeController::_firmwareDownloadError(QString errorMsg)
657
{
658
    _errorCancel(errorMsg);
659 660
}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
/// @brief returns firmware type as a string
QString FirmwareUpgradeController::firmwareTypeAsString(FirmwareType_t type) const
{
    switch (type) {
    case StableFirmware:
        return "stable";
    case DeveloperFirmware:
        return "developer";
    case BetaFirmware:
        return "beta";
    default:
        return "custom";
    }
}

676 677 678
/// @brief Signals completion of one of the specified bootloader commands. Moves the state machine to the
///         appropriate next step.
void FirmwareUpgradeController::_flashComplete(void)
679
{
680
    delete _image;
681
    _image = nullptr;
682
    
683
    _appendStatusLog(tr("Upgrade complete"), true);
684 685
    _appendStatusLog("------------------------------------------", false);
    emit flashComplete();
686
    qgcApp()->toolbox()->linkManager()->setConnectionsAllowed();
687 688
}

689
void FirmwareUpgradeController::_error(const QString& errorString)
690
{
691
    delete _image;
692
    _image = nullptr;
693 694
    
    _errorCancel(QString("Error: %1").arg(errorString));
695 696
}

697
void FirmwareUpgradeController::_status(const QString& statusString)
698
{
699
    _appendStatusLog(statusString);
700 701 702 703 704
}

/// @brief Updates the progress bar from long running bootloader commands
void FirmwareUpgradeController::_updateProgress(int curr, int total)
{
705 706
    // Take care of cases where 0 / 0 is emitted as error return value
    if (total > 0) {
707
        _progressBar->setProperty("value", static_cast<float>(curr) / static_cast<float>(total));
708
    }
709 710 711 712 713 714
}

/// @brief Moves the progress bar ahead on tick while erasing the board
void FirmwareUpgradeController::_eraseProgressTick(void)
{
    _eraseTickCount++;
715
    _progressBar->setProperty("value", static_cast<float>(_eraseTickCount*_eraseTickMsec) / static_cast<float>(_eraseTotalMsec));
716 717 718
}

/// Appends the specified text to the status log area in the ui
719
void FirmwareUpgradeController::_appendStatusLog(const QString& text, bool critical)
720 721 722 723
{
    Q_ASSERT(_statusLog);
    
    QVariant returnedValue;
724 725 726 727 728 729 730 731
    QVariant varText;
    
    if (critical) {
        varText = QString("<font color=\"yellow\">%1</font>").arg(text);
    } else {
        varText = text;
    }
    
732 733 734 735
    QMetaObject::invokeMethod(_statusLog,
                              "append",
                              Q_RETURN_ARG(QVariant, returnedValue),
                              Q_ARG(QVariant, varText));
Don Gagne's avatar
Don Gagne committed
736
}
737

738 739 740
void FirmwareUpgradeController::_errorCancel(const QString& msg)
{
    _appendStatusLog(msg, false);
741
    _appendStatusLog(tr("Upgrade cancelled"), true);
742 743 744
    _appendStatusLog("------------------------------------------", false);
    emit error();
    cancel();
745
    qgcApp()->toolbox()->linkManager()->setConnectionsAllowed();
746 747 748 749 750 751 752 753 754 755
}

void FirmwareUpgradeController::_eraseStarted(void)
{
    // We set up our own progress bar for erase since the erase command does not provide one
    _eraseTickCount = 0;
    _eraseTimer.start(_eraseTickMsec);
}

void FirmwareUpgradeController::_eraseComplete(void)
756
{
757
    _eraseTimer.stop();
758
}
759

760
void FirmwareUpgradeController::_loadAPMVersions(uint32_t bootloaderBoardID)
761 762 763
{
    _apmVersionMap.clear();

764
    QHash<FirmwareIdentifier, QString>* prgFirmware = _firmwareHashForBoardId(static_cast<int>(bootloaderBoardID));
765

766
    for (FirmwareIdentifier firmwareId: prgFirmware->keys()) {
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
        if (firmwareId.autopilotStackType == AutoPilotStackAPM) {
            QString versionFile = QFileInfo(prgFirmware->value(firmwareId)).path() + "/git-version.txt";

            qCDebug(FirmwareUpgradeLog) << "Downloading" << versionFile;
            QGCFileDownload* downloader = new QGCFileDownload(this);
            connect(downloader, &QGCFileDownload::downloadFinished, this, &FirmwareUpgradeController::_apmVersionDownloadFinished);
            downloader->download(versionFile);
        }
    }
}

void FirmwareUpgradeController::_apmVersionDownloadFinished(QString remoteFile, QString localFile)
{
    qCDebug(FirmwareUpgradeLog) << "Download complete" << remoteFile << localFile;

    // Now read the version file and pull out the version string

    QFile versionFile(localFile);
    versionFile.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream stream(&versionFile);
    QString versionContents = stream.readAll();

    QString version;
    QRegularExpression re("APMVERSION: (.*)$");
    QRegularExpressionMatch match = re.match(versionContents);
    if (match.hasMatch()) {
        version = match.captured(1);
    }

    if (version.isEmpty()) {
        qWarning() << "Unable to parse version info from file" << remoteFile;
Don Gagne's avatar
Don Gagne committed
798
        sender()->deleteLater();
799 800 801 802 803
        return;
    }

    // In order to determine the firmware and vehicle type for this file we find the matching entry in the firmware list

804
    QHash<FirmwareIdentifier, QString>* prgFirmware = _firmwareHashForBoardId(static_cast<int>(_bootloaderBoardID));
805 806

    QString remotePath = QFileInfo(remoteFile).path();
807
    for (FirmwareIdentifier firmwareId: prgFirmware->keys()) {
808 809 810 811 812 813 814
        if (remotePath == QFileInfo((*prgFirmware)[firmwareId]).path()) {
            qCDebug(FirmwareUpgradeLog) << "Adding version to map, version:firwmareType:vehicleType" << version << firmwareId.firmwareType << firmwareId.firmwareVehicleType;
            _apmVersionMap[firmwareId.firmwareType][firmwareId.firmwareVehicleType] = version;
        }
    }

    emit apmAvailableVersionsChanged();
Don Gagne's avatar
Don Gagne committed
815
    sender()->deleteLater();
816 817 818 819 820 821 822 823 824 825 826
}

void FirmwareUpgradeController::setSelectedFirmwareType(FirmwareType_t firmwareType)
{
    _selectedFirmwareType = firmwareType;
    emit selectedFirmwareTypeChanged(_selectedFirmwareType);
    emit apmAvailableVersionsChanged();
}

QStringList FirmwareUpgradeController::apmAvailableVersions(void)
{
827 828 829
    QStringList                     list;
    QList<FirmwareVehicleType_t>    vehicleTypes;

830 831
    // This allows us to force the order of the combo box display
    vehicleTypes << CopterChibiOSFirmware << HeliChibiOSFirmware << PlaneChibiOSFirmware << RoverChibiOSFirmware << SubChibiOSFirmware << CopterFirmware << HeliFirmware << PlaneFirmware << RoverFirmware << SubFirmware;
832 833 834

    _apmVehicleTypeFromCurrentVersionList.clear();

835
    for (FirmwareVehicleType_t vehicleType: vehicleTypes) {
836 837 838 839
        if (_apmVersionMap[_selectedFirmwareType].contains(vehicleType)) {
            QString version;

            switch (vehicleType) {
Don Gagne's avatar
Don Gagne committed
840
            case CopterFirmware:
841
                version = tr("NuttX - MultiRotor:");
842 843
                break;
            case HeliFirmware:
844
                version = tr("NuttX - Heli:");
845
                break;
Don Gagne's avatar
Don Gagne committed
846
            case CopterChibiOSFirmware:
847
                version = tr("ChibiOS- MultiRotor:");
Don Gagne's avatar
Don Gagne committed
848 849
                break;
            case HeliChibiOSFirmware:
850
                version = tr("ChibiOS - Heli:");
851
                break;
Don Gagne's avatar
Don Gagne committed
852 853 854 855
            case PlaneChibiOSFirmware:
            case RoverChibiOSFirmware:
            case SubChibiOSFirmware:
                version = tr("ChibiOS - ");
856
                break;
Don Gagne's avatar
Don Gagne committed
857
            default:
858
                version = tr("NuttX - ");
859 860 861 862 863 864 865
                break;
            }

            version += _apmVersionMap[_selectedFirmwareType][vehicleType];
            _apmVehicleTypeFromCurrentVersionList.append(vehicleType);

            list << version;
866 867 868 869 870 871 872 873 874 875
        }
    }

    return list;
}

FirmwareUpgradeController::FirmwareVehicleType_t FirmwareUpgradeController::vehicleTypeFromVersionIndex(int index)
{
    if (index < 0 || index >= _apmVehicleTypeFromCurrentVersionList.count()) {
        qWarning() << "Invalid index, index:count" << index << _apmVehicleTypeFromCurrentVersionList.count();
Don Gagne's avatar
Don Gagne committed
876
        return CopterFirmware;
877 878 879 880
    }

    return _apmVehicleTypeFromCurrentVersionList[index];
}
Don Gagne's avatar
Don Gagne committed
881 882 883 884

void FirmwareUpgradeController::_determinePX4StableVersion(void)
{
    QGCFileDownload* downloader = new QGCFileDownload(this);
885 886 887
    connect(downloader, &QGCFileDownload::downloadFinished, this, &FirmwareUpgradeController::_px4ReleasesGithubDownloadFinished);
    connect(downloader, &QGCFileDownload::error, this, &FirmwareUpgradeController::_px4ReleasesGithubDownloadError);
    downloader->download(QStringLiteral("https://api.github.com/repos/PX4/Firmware/releases"));
Don Gagne's avatar
Don Gagne committed
888 889
}

890
void FirmwareUpgradeController::_px4ReleasesGithubDownloadFinished(QString remoteFile, QString localFile)
Don Gagne's avatar
Don Gagne committed
891 892 893 894 895
{
    Q_UNUSED(remoteFile);

    QFile jsonFile(localFile);
    if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
896
        qCWarning(FirmwareUpgradeLog) << "Unable to open github px4 releases json file" << localFile << jsonFile.errorString();
Don Gagne's avatar
Don Gagne committed
897 898 899 900 901 902 903 904
        return;
    }
    QByteArray bytes = jsonFile.readAll();
    jsonFile.close();

    QJsonParseError jsonParseError;
    QJsonDocument doc = QJsonDocument::fromJson(bytes, &jsonParseError);
    if (jsonParseError.error != QJsonParseError::NoError) {
905
        qCWarning(FirmwareUpgradeLog) <<  "Unable to open px4 releases json document" << localFile << jsonParseError.errorString();
Don Gagne's avatar
Don Gagne committed
906 907 908
        return;
    }

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
    // Json should be an array of release objects
    if (!doc.isArray()) {
        qCWarning(FirmwareUpgradeLog) <<  "px4 releases json document is not an array" << localFile;
        return;
    }
    QJsonArray releases = doc.array();

    // The first release marked prerelease=false is stable
    // The first release marked prerelease=true is beta
    bool foundStable = false;
    bool foundBeta = false;
    for (int i=0; i<releases.count() && (!foundStable || !foundBeta); i++) {
        QJsonObject release = releases[i].toObject();
        if (!foundStable && !release["prerelease"].toBool()) {
            _px4StableVersion = release["name"].toString();
            emit px4StableVersionChanged(_px4StableVersion);
            qCDebug(FirmwareUpgradeLog()) << "Found px4 stable version" << _px4StableVersion;
            foundStable = true;
        } else if (!foundBeta && release["prerelease"].toBool()) {
            _px4BetaVersion = release["name"].toString();
            emit px4StableVersionChanged(_px4BetaVersion);
            qCDebug(FirmwareUpgradeLog()) << "Found px4 beta version" << _px4BetaVersion;
            foundBeta = true;
        }
    }

    if (!foundStable) {
        qCDebug(FirmwareUpgradeLog()) << "Unable to find px4 stable version" << localFile;
    }
    if (!foundBeta) {
        qCDebug(FirmwareUpgradeLog()) << "Unable to find px4 beta version" << localFile;
    }
Don Gagne's avatar
Don Gagne committed
941 942
}

943
void FirmwareUpgradeController::_px4ReleasesGithubDownloadError(QString errorMsg)
Don Gagne's avatar
Don Gagne committed
944
{
945
    qCWarning(FirmwareUpgradeLog) << "PX4 releases github download failed" << errorMsg;
Don Gagne's avatar
Don Gagne committed
946
}