7#include <QtCore/QJsonArray>
8#include <QtCore/QJsonDocument>
9#include <QtCore/QJsonObject>
10#include <QtCore/QStringList>
23 qCDebug(QGCSerialPortInfoLog) <<
this;
29 qCDebug(QGCSerialPortInfoLog) <<
this;
34 qCDebug(QGCSerialPortInfoLog) <<
this;
37bool QGCSerialPortInfo::_loadJsonData()
40 return _jsonDataValid;
49 qCWarning(QGCSerialPortInfoLog) <<
"Internal Error:" <<
errorString;
53 static const QList<JsonParsing::KeyValidateInfo> rootKeyInfoList = {
56 {
"comment", QJsonValue::String,
false },
57 { _jsonBoardInfoKey, QJsonValue::Array,
true },
58 { _jsonBoardDescriptionFallbackKey, QJsonValue::Array,
true },
59 { _jsonBoardManufacturerFallbackKey, QJsonValue::Array,
true },
66 static const QList<JsonParsing::KeyValidateInfo> boardKeyInfoList = {
67 {
"comment", QJsonValue::String,
false },
68 { _jsonVendorIDKey, QJsonValue::Double,
true },
69 { _jsonProductIDKey, QJsonValue::Double,
true },
70 { _jsonBoardClassKey, QJsonValue::String,
true },
71 { _jsonNameKey, QJsonValue::String,
true },
73 const QJsonArray rgBoardInfo = json[_jsonBoardInfoKey].toArray();
74 for (
const QJsonValue &jsonValue : rgBoardInfo) {
75 if (!jsonValue.isObject()) {
76 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardInfo array is not object";
80 const QJsonObject boardObject = jsonValue.toObject();
86 const BoardInfo_t boardInfo = {
87 boardObject[_jsonVendorIDKey].toInt(),
88 boardObject[_jsonProductIDKey].toInt(),
89 _boardClassStringToType(boardObject[_jsonBoardClassKey].toString()),
90 boardObject[_jsonNameKey].toString()
93 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << boardObject[_jsonBoardClassKey].toString();
97 _boardInfoList.append(boardInfo);
100 static const QList<JsonParsing::KeyValidateInfo> fallbackKeyInfoList = {
101 {
"comment", QJsonValue::String,
false },
102 { _jsonRegExpKey, QJsonValue::String,
true },
103 { _jsonBoardClassKey, QJsonValue::String,
true },
104 { _jsonAndroidOnlyKey, QJsonValue::Bool,
false },
106 const QJsonArray rgBoardDescriptionFallback = json[_jsonBoardDescriptionFallbackKey].toArray();
107 for (
const QJsonValue &jsonValue : rgBoardDescriptionFallback) {
108 if (!jsonValue.isObject()) {
109 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardFallback array is not object";
113 const QJsonObject fallbackObject = jsonValue.toObject();
119 const QRegularExpression regExp(fallbackObject[_jsonRegExpKey].toString(), QRegularExpression::CaseInsensitiveOption);
120 if (!regExp.isValid()) {
121 qCWarning(QGCSerialPortInfoLog) <<
"Invalid regular expression in board description fallback:"
122 << regExp.errorString()
123 <<
"pattern:" << fallbackObject[_jsonRegExpKey].toString();
126 const BoardRegExpFallback_t boardFallback = {
128 _boardClassStringToType(fallbackObject[_jsonBoardClassKey].toString()),
129 fallbackObject[_jsonAndroidOnlyKey].toBool(
false)
132 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << fallbackObject[_jsonBoardClassKey].toString();
136 _boardDescriptionFallbackList.append(boardFallback);
139 const QJsonArray rgBoardManufacturerFallback = json[_jsonBoardManufacturerFallbackKey].toArray();
140 for (
const QJsonValue &jsonValue : rgBoardManufacturerFallback) {
141 if (!jsonValue.isObject()) {
142 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardFallback array is not object";
146 const QJsonObject fallbackObject = jsonValue.toObject();
152 const QRegularExpression regExp(fallbackObject[_jsonRegExpKey].toString(), QRegularExpression::CaseInsensitiveOption);
153 if (!regExp.isValid()) {
154 qCWarning(QGCSerialPortInfoLog) <<
"Invalid regular expression in board manufacturer fallback:"
155 << regExp.errorString()
156 <<
"pattern:" << fallbackObject[_jsonRegExpKey].toString();
159 const BoardRegExpFallback_t boardFallback = {
161 _boardClassStringToType(fallbackObject[_jsonBoardClassKey].toString()),
162 fallbackObject[_jsonAndroidOnlyKey].toBool(
false)
165 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << fallbackObject[_jsonBoardClassKey].toString();
169 _boardManufacturerFallbackList.append(boardFallback);
172 _jsonDataValid =
true;
179 static const BoardClassString2BoardType_t rgBoardClass2BoardType[
BoardTypeUnknown] = {
186 for (
const BoardClassString2BoardType_t &board : rgBoardClass2BoardType) {
187 if (boardClass == board.classString) {
188 return board.boardType;
199 if (!_loadJsonData()) {
207 for (
const BoardInfo_t &boardInfo : _boardInfoList) {
209 boardType = boardInfo.boardType;
210 name = boardInfo.name;
215 for (
const BoardRegExpFallback_t &boardFallback : _boardDescriptionFallbackList) {
216 if (
description().contains(boardFallback.regExp)) {
218 if (boardFallback.androidOnly) {
222 boardType = boardFallback.boardType;
223 name = _boardTypeToString(boardType);
228 for (
const BoardRegExpFallback_t &boardFallback : _boardManufacturerFallbackList) {
231 if (boardFallback.androidOnly) {
235 boardType = boardFallback.boardType;
236 name = _boardTypeToString(boardType);
244QString QGCSerialPortInfo::_boardTypeToString(BoardType_t boardType)
248 return QStringLiteral(
"Pixhawk");
250 return QStringLiteral(
"SiK Radio");
252 return QStringLiteral(
"OpenPilot");
254 return QStringLiteral(
"RTK GPS");
257 return QStringLiteral(
"Unknown");
263 QList<QGCSerialPortInfo> list;
272 list << *qgcPortInfo;
291 return description().contains(QStringLiteral(
"CubeBlack"));
297 static const QStringList systemPortLocations = {
298 QStringLiteral(
"tty.MALS"),
299 QStringLiteral(
"tty.SOC"),
300 QStringLiteral(
"tty.Bluetooth-Incoming-Port"),
301 QStringLiteral(
"tty.usbserial"),
302 QStringLiteral(
"tty.usbmodem")
304 for (
const QString &systemPortLocation : systemPortLocations) {
305 if (port.systemLocation().contains(systemPortLocation)) {
326 static const QList<BoardType_t> flashable = {
331 return flashable.contains(boardType);
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QGC's version of Qt QSerialPortInfo. It provides additional information about board types that QGC ca...
bool isBootloader() const
static bool isSystemPort(const QSerialPortInfo &port)
bool getBoardInfo(BoardType_t &boardType, QString &name) const
static QList< QGCSerialPortInfo > availablePorts()
Override of QSerialPortInfo::availablePorts.
Provides information about existing serial ports.
quint16 productIdentifier() const
Returns the 16-bit product number for the serial port, if available; otherwise returns zero.
QString manufacturer() const
Returns the manufacturer string of the serial port, if available; otherwise returns an empty string.
static QList< QSerialPortInfo > availablePorts()
Returns a list of available serial ports on the system.
bool isNull() const
Returns whether this QSerialPortInfo object holds a serial port definition.
QString description() const
Returns the description string of the serial port, if available; otherwise returns an empty string.
quint16 vendorIdentifier() const
Returns the 16-bit vendor number for the serial port, if available; otherwise returns zero.
Provides functions to access serial ports.
constexpr const char * jsonFileTypeKey
QJsonObject openInternalQGCJsonFile(const QString &jsonFilename, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString, const QStringList &defaultTranslateKeys, const QStringList &defaultArrayIDKeys)
bool validateKeysStrict(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates keys like validateKeys but also rejects any keys not listed in keyInfo.
constexpr const char * jsonVersionKey