Skip to content

Commit

Permalink
Comms: Fix USBBoardInfo Parsing Test
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Nov 23, 2024
1 parent 0c34a2e commit 76ee1c1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 51 deletions.
92 changes: 43 additions & 49 deletions src/Comms/QGCSerialPortInfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
QGC_LOGGING_CATEGORY(QGCSerialPortInfoLog, "qgc.comms.qgcserialportinfo")

bool QGCSerialPortInfo::_jsonLoaded = false;
bool QGCSerialPortInfo::_jsonDataValid = false;
QList<QGCSerialPortInfo::BoardInfo_t> QGCSerialPortInfo::_boardInfoList;
QList<QGCSerialPortInfo::BoardRegExpFallback_t> QGCSerialPortInfo::_boardDescriptionFallbackList;
QList<QGCSerialPortInfo::BoardRegExpFallback_t> QGCSerialPortInfo::_boardManufacturerFallbackList;
Expand All @@ -41,35 +42,20 @@ QGCSerialPortInfo::~QGCSerialPortInfo()
// qCDebug(QGCSerialPortInfoLog) << Q_FUNC_INFO << this;
}

void QGCSerialPortInfo::_loadJsonData()
bool QGCSerialPortInfo::_loadJsonData()
{
if (_jsonLoaded) {
return;
return _jsonDataValid;
}

_jsonLoaded = true;

QFile file(QStringLiteral(":/json/USBBoardInfo.json"));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCWarning(QGCSerialPortInfoLog) << "Unable to open board info json:" << file.errorString();
return;
}

const QByteArray data = file.readAll();
file.close();
QJsonParseError jsonParseError;
const QJsonDocument jsonDoc(QJsonDocument::fromJson(data, &jsonParseError));
if (jsonParseError.error != QJsonParseError::NoError) {
qCWarning(QGCSerialPortInfoLog) << "Unable to parse board info json:" << jsonParseError.errorString();
return;
}

const QJsonObject json = jsonDoc.object();
int fileVersion;
QString errorString;
if (!JsonHelper::validateInternalQGCJsonFile(json, _jsonFileTypeValue, 1, 1, fileVersion, errorString)) {
qCWarning(QGCSerialPortInfoLog) << errorString;
return;
int version;
const QJsonObject json = JsonHelper::openInternalQGCJsonFile(":/json/USBBoardInfo.json", _jsonFileTypeValue, 1, 1, version, errorString);
if (!errorString.isEmpty()) {
qCWarning(QGCSerialPortInfoLog) << "Internal Error:" << errorString;
return false;
}

static const QList<JsonHelper::KeyValidateInfo> rootKeyInfoList = {
Expand All @@ -79,7 +65,7 @@ void QGCSerialPortInfo::_loadJsonData()
};
if (!JsonHelper::validateKeys(json, rootKeyInfoList, errorString)) {
qCWarning(QGCSerialPortInfoLog) << errorString;
return;
return false;
}

static const QList<JsonHelper::KeyValidateInfo> boardKeyInfoList = {
Expand All @@ -92,13 +78,13 @@ void QGCSerialPortInfo::_loadJsonData()
for (const QJsonValue &jsonValue : rgBoardInfo) {
if (!jsonValue.isObject()) {
qCWarning(QGCSerialPortInfoLog) << "Entry in boardInfo array is not object";
return;
return false;
}

const QJsonObject boardObject = jsonValue.toObject();
if (!JsonHelper::validateKeys(boardObject, boardKeyInfoList, errorString)) {
qCWarning(QGCSerialPortInfoLog) << errorString;
return;
return false;
}

const BoardInfo_t boardInfo = {
Expand All @@ -109,9 +95,10 @@ void QGCSerialPortInfo::_loadJsonData()
};
if (boardInfo.boardType == BoardTypeUnknown) {
qCWarning(QGCSerialPortInfoLog) << "Bad board class" << boardObject[_jsonBoardClassKey].toString();
return;
return false;
}
(void) _boardInfoList.append(boardInfo);

_boardInfoList.append(boardInfo);
}

static const QList<JsonHelper::KeyValidateInfo> fallbackKeyInfoList = {
Expand All @@ -123,13 +110,13 @@ void QGCSerialPortInfo::_loadJsonData()
for (const QJsonValue &jsonValue : rgBoardDescriptionFallback) {
if (!jsonValue.isObject()) {
qCWarning(QGCSerialPortInfoLog) << "Entry in boardFallback array is not object";
return;
return false;
}

const QJsonObject fallbackObject = jsonValue.toObject();
if (!JsonHelper::validateKeys(fallbackObject, fallbackKeyInfoList, errorString)) {
qCWarning(QGCSerialPortInfoLog) << errorString;
return;
return false;
}

const BoardRegExpFallback_t boardFallback = {
Expand All @@ -139,22 +126,23 @@ void QGCSerialPortInfo::_loadJsonData()
};
if (boardFallback.boardType == BoardTypeUnknown) {
qCWarning(QGCSerialPortInfoLog) << "Bad board class" << fallbackObject[_jsonBoardClassKey].toString();
return;
return false;
}
(void) _boardDescriptionFallbackList.append(boardFallback);

_boardDescriptionFallbackList.append(boardFallback);
}

const QJsonArray rgBoardManufacturerFallback = json[_jsonBoardManufacturerFallbackKey].toArray();
for (const QJsonValue &jsonValue : rgBoardManufacturerFallback) {
if (!jsonValue.isObject()) {
qCWarning(QGCSerialPortInfoLog) << "Entry in boardFallback array is not object";
return;
return false;
}

const QJsonObject fallbackObject = jsonValue.toObject();
if (!JsonHelper::validateKeys(fallbackObject, fallbackKeyInfoList, errorString)) {
qCWarning(QGCSerialPortInfoLog) << errorString;
return;
return false;
}

const BoardRegExpFallback_t boardFallback = {
Expand All @@ -164,10 +152,15 @@ void QGCSerialPortInfo::_loadJsonData()
};
if (boardFallback.boardType == BoardTypeUnknown) {
qCWarning(QGCSerialPortInfoLog) << "Bad board class" << fallbackObject[_jsonBoardClassKey].toString();
return;
return false;
}
(void) _boardManufacturerFallbackList.append(boardFallback);

_boardManufacturerFallbackList.append(boardFallback);
}

_jsonDataValid = true;

return true;
}

QGCSerialPortInfo::BoardType_t QGCSerialPortInfo::_boardClassStringToType(const QString &boardClass)
Expand All @@ -185,7 +178,9 @@ bool QGCSerialPortInfo::getBoardInfo(QGCSerialPortInfo::BoardType_t &boardType,
{
boardType = BoardTypeUnknown;

_loadJsonData();
if (!_loadJsonData()) {
return false;
}

if (isNull()) {
return false;
Expand Down Expand Up @@ -266,12 +261,11 @@ bool QGCSerialPortInfo::isBootloader() const
{
BoardType_t boardType;
QString name;
if (getBoardInfo(boardType, name)) {
// TODO: bool SerialLink::_isBootloader()
return ((boardType == BoardTypePixhawk) && description().contains(QStringLiteral("BL")));
if (!getBoardInfo(boardType, name)) {
return false;
}

return false;
return ((boardType == BoardTypePixhawk) && description().contains(QStringLiteral("BL")));
}

bool QGCSerialPortInfo::isSystemPort(const QSerialPortInfo &port)
Expand Down Expand Up @@ -300,15 +294,15 @@ bool QGCSerialPortInfo::canFlash() const
{
BoardType_t boardType;
QString name;
if (getBoardInfo(boardType, name)) {
switch(boardType) {
case QGCSerialPortInfo::BoardTypePixhawk:
case QGCSerialPortInfo::BoardTypeSiKRadio:
return true;
default:
return false;
}
if (!getBoardInfo(boardType, name)) {
return false;
}

return false;
switch(boardType) {
case QGCSerialPortInfo::BoardTypePixhawk:
case QGCSerialPortInfo::BoardTypeSiKRadio:
return true;
default:
return false;
}
}
3 changes: 2 additions & 1 deletion src/Comms/QGCSerialPortInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ class QGCSerialPortInfo : public QSerialPortInfo
static QList<QGCSerialPortInfo> availablePorts();

private:
static void _loadJsonData();
static bool _loadJsonData();
static BoardType_t _boardClassStringToType(const QString &boardClass);
static QString _boardTypeToString(BoardType_t boardType);

static bool _jsonLoaded;
static bool _jsonDataValid;

struct BoardClassString2BoardType_t {
const char *classString;
Expand Down
6 changes: 5 additions & 1 deletion test/Comms/QGCSerialPortInfoTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
void QGCSerialPortInfoTest::_testLoadJsonData()
{
QVERIFY(!QGCSerialPortInfo::_jsonLoaded);
QGCSerialPortInfo::_loadJsonData();
QVERIFY(QGCSerialPortInfo::_loadJsonData());
QVERIFY(QGCSerialPortInfo::_jsonLoaded);
QVERIFY(QGCSerialPortInfo::_jsonDataValid);
QVERIFY(!QGCSerialPortInfo::_boardInfoList.isEmpty());
QVERIFY(!QGCSerialPortInfo::_boardDescriptionFallbackList.isEmpty());
QVERIFY(!QGCSerialPortInfo::_boardManufacturerFallbackList.isEmpty());
}

0 comments on commit 76ee1c1

Please sign in to comment.