QGCSerialPortInfo.cc 7.77 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 14

#include "QGCSerialPortInfo.h"

QGC_LOGGING_CATEGORY(QGCSerialPortInfoLog, "QGCSerialPortInfoLog")

Don Gagne's avatar
Don Gagne committed
15 16 17 18 19 20 21 22 23 24 25 26
static const struct VIDPIDMapInfo_s {
    int                             vendorId;
    int                             productId;
    QGCSerialPortInfo::BoardType_t  boardType;
    const char *                    boardString;
} s_rgVIDPIDMappings[] = {
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::pixhawkFMUV4ProductId,               QGCSerialPortInfo::BoardTypePX4FMUV4,   "Found PX4 FMU V4" },
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::pixhawkFMUV2ProductId,               QGCSerialPortInfo::BoardTypePX4FMUV2,   "Found PX4 FMU V2" },
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::pixhawkFMUV2OldBootloaderProductId,  QGCSerialPortInfo::BoardTypePX4FMUV2,   "Found PX4 FMU V2"},
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::pixhawkFMUV1ProductId,               QGCSerialPortInfo::BoardTypePX4FMUV1,   "Found PX4 FMU V1" },
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::px4FlowProductId,                    QGCSerialPortInfo::BoardTypePX4Flow,    "Found PX4 Flow" },
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::AeroCoreProductId,                   QGCSerialPortInfo::BoardTypeAeroCore,   "Found AeroCore" },
Henry Zhang's avatar
Henry Zhang committed
27
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::MindPXFMUV2ProductId,                QGCSerialPortInfo::BoardTypeMINDPXFMUV2,"Found MindPX FMU V2" },
28 29
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::TAPV1ProductId,                      QGCSerialPortInfo::BoardTypeTAPV1,      "Found TAP V1" },
    { QGCSerialPortInfo::px4VendorId,           QGCSerialPortInfo::ASCV1ProductId,                      QGCSerialPortInfo::BoardTypeASCV1,      "Found ASC V1" },
Don Gagne's avatar
Don Gagne committed
30 31
    { QGCSerialPortInfo::threeDRRadioVendorId,  QGCSerialPortInfo::threeDRRadioProductId,               QGCSerialPortInfo::BoardTypeSikRadio,   "Found SiK Radio" },
    { QGCSerialPortInfo::siLabsRadioVendorId,   QGCSerialPortInfo::siLabsRadioProductId,                QGCSerialPortInfo::BoardTypeSikRadio,   "Found SiK Radio" },
32
    { QGCSerialPortInfo::ubloxRTKVendorId,      QGCSerialPortInfo::ubloxRTKProductId,                   QGCSerialPortInfo::BoardTypeRTKGPS,     "Found RTK GPS" },
33 34 35 36
    { QGCSerialPortInfo::openpilotVendorId,     QGCSerialPortInfo::revolutionProductId,                 QGCSerialPortInfo::BoardTypeLibrePilot, "Found OP Revolution" },
    { QGCSerialPortInfo::openpilotVendorId,     QGCSerialPortInfo::oplinkProductId,                     QGCSerialPortInfo::BoardTypeLibrePilot, "Found OP OPLink" },
    { QGCSerialPortInfo::openpilotVendorId,     QGCSerialPortInfo::sparky2ProductId,                    QGCSerialPortInfo::BoardTypeLibrePilot, "Found TL Sparky2" },
    { QGCSerialPortInfo::openpilotVendorId,     QGCSerialPortInfo::CC3DProductId,                       QGCSerialPortInfo::BoardTypeLibrePilot, "Found OP CC3D" },
Don Gagne's avatar
Don Gagne committed
37 38
};

Don Gagne's avatar
Don Gagne committed
39 40 41 42 43 44
QGCSerialPortInfo::QGCSerialPortInfo(void) :
    QSerialPortInfo()
{

}

45 46 47 48 49 50 51 52 53 54 55 56 57 58
QGCSerialPortInfo::QGCSerialPortInfo(const QSerialPort & port) :
    QSerialPortInfo(port)
{

}

QGCSerialPortInfo::BoardType_t QGCSerialPortInfo::boardType(void) const
{
    if (isNull()) {
        return BoardTypeUnknown;
    }

    BoardType_t boardType = BoardTypeUnknown;

Don Gagne's avatar
Don Gagne committed
59 60 61 62 63 64
    for (size_t i=0; i<sizeof(s_rgVIDPIDMappings)/sizeof(s_rgVIDPIDMappings[0]); i++) {
        const struct VIDPIDMapInfo_s* pIDMap = &s_rgVIDPIDMappings[i];

        if (vendorIdentifier() == pIDMap->vendorId && productIdentifier() == pIDMap->productId) {
            boardType = pIDMap->boardType;
            qCDebug(QGCSerialPortInfoLog) << pIDMap->boardString;
65
            break;
Don Gagne's avatar
Don Gagne committed
66
        }
67 68 69 70 71
    }

    if (boardType == BoardTypeUnknown) {
        // Fall back to port name matching which could lead to incorrect board mapping. But in some cases the
        // vendor and product id do not come through correctly so this is used as a last chance detection method.
72 73 74 75
        if (description() == "PX4 FMU v4.x" || description() == "PX4 BL FMU v4.x") {
            qCDebug(QGCSerialPortInfoLog) << "Found PX4 FMU V4 (by name matching fallback)";
            boardType = BoardTypePX4FMUV4;
        } else if (description() == "PX4 FMU v2.x" || description() == "PX4 BL FMU v2.x") {
76 77 78 79 80 81 82 83
            qCDebug(QGCSerialPortInfoLog) << "Found PX4 FMU V2 (by name matching fallback)";
            boardType = BoardTypePX4FMUV2;
        } else if (description() == "PX4 FMU v1.x" || description() == "PX4 BL FMU v1.x") {
            qCDebug(QGCSerialPortInfoLog) << "Found PX4 FMU V1 (by name matching fallback)";
            boardType = BoardTypePX4FMUV1;
        } else if (description().startsWith("PX4 FMU")) {
            qCDebug(QGCSerialPortInfoLog) << "Found PX4 FMU, assuming V2 (by name matching fallback)";
            boardType = BoardTypePX4FMUV2;
84 85 86
        } else if (description().contains(QRegExp("PX4.*Flow", Qt::CaseInsensitive))) {
            qCDebug(QGCSerialPortInfoLog) << "Found possible px4 flow camera (by name matching fallback)";
            boardType = BoardTypePX4Flow;
Henry Zhang's avatar
Henry Zhang committed
87 88 89
        } else if (description() == "MindPX FMU v2.x" || description() == "MindPX BL FMU v2.x") {
            qCDebug(QGCSerialPortInfoLog) << "Found MindPX FMU V2 (by name matching fallback)";
            boardType = BoardTypeMINDPXFMUV2;
90 91 92 93 94 95
        } else if (description() == "PX4 TAP v1.x" || description() == "PX4 BL TAP v1.x") {
            qCDebug(QGCSerialPortInfoLog) << "Found TAP V1 (by name matching fallback)";
            boardType = BoardTypeTAPV1;
        } else if (description() == "PX4 ASC v1.x" || description() == "PX4 BL ASC v1.x") {
            qCDebug(QGCSerialPortInfoLog) << "Found ASC V1 (by name matching fallback)";
            boardType = BoardTypeASCV1;
Don Gagne's avatar
Don Gagne committed
96 97
        } else if (description() == "FT231X USB UART") {
            qCDebug(QGCSerialPortInfoLog) << "Found possible Radio (by name matching fallback)";
Don Gagne's avatar
Don Gagne committed
98
            boardType = BoardTypeSikRadio;
99 100 101 102 103
#ifdef __android__
        } else if (description().endsWith("USB UART")) {
            // This is a fairly broad fallbacks for radios which will also catch most FTDI devices. That would
            // cause problems on desktop due to incorrect connections. Since mobile is more anal about connecting
            // it will work fine here.
Don Gagne's avatar
Don Gagne committed
104
            boardType = BoardTypeSikRadio;
105
#endif
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        }
    }

    return boardType;
}

QList<QGCSerialPortInfo> QGCSerialPortInfo::availablePorts(void)
{
    QList<QGCSerialPortInfo>    list;

    foreach(QSerialPortInfo portInfo, QSerialPortInfo::availablePorts()) {
        list << *((QGCSerialPortInfo*)&portInfo);
    }

    return list;
}

bool QGCSerialPortInfo::boardTypePixhawk(void) const
{
    BoardType_t boardType = this->boardType();

Henry Zhang's avatar
Henry Zhang committed
127 128
    return boardType == BoardTypePX4FMUV1 || boardType == BoardTypePX4FMUV2
            || boardType == BoardTypePX4FMUV4 || boardType == BoardTypeAeroCore
129 130
            || boardType == BoardTypeMINDPXFMUV2 || boardType == BoardTypeTAPV1
            || boardType == BoardTypeASCV1;
131 132 133 134 135 136 137
}

bool QGCSerialPortInfo::isBootloader(void) const
{
    // FIXME: Check SerialLink bootloade detect code which is different
    return boardTypePixhawk() && description().contains("BL");
}
138 139 140 141

bool QGCSerialPortInfo::canFlash(void)
{
    BoardType_t boardType = this->boardType();
142 143 144 145 146 147 148 149 150 151
    switch(boardType){
    case QGCSerialPortInfo::BoardTypeUnknown:
    case QGCSerialPortInfo::BoardTypeRTKGPS:
    case QGCSerialPortInfo::BoardTypeLibrePilot:
        return false;
    default:
        return true;

    }

152 153

}