7#include <QtCore/QJsonArray>
8#include <QtCore/QJsonDocument>
9#include <QtCore/QJsonObject>
22 qCDebug(QGCSerialPortInfoLog) <<
this;
28 qCDebug(QGCSerialPortInfoLog) <<
this;
33 qCDebug(QGCSerialPortInfoLog) <<
this;
36bool QGCSerialPortInfo::_loadJsonData()
39 return _jsonDataValid;
48 qCWarning(QGCSerialPortInfoLog) <<
"Internal Error:" <<
errorString;
52 static const QList<JsonParsing::KeyValidateInfo> rootKeyInfoList = {
55 {
"comment", QJsonValue::String,
false },
56 { _jsonBoardInfoKey, QJsonValue::Array,
true },
57 { _jsonBoardDescriptionFallbackKey, QJsonValue::Array,
true },
58 { _jsonBoardManufacturerFallbackKey, QJsonValue::Array,
true },
65 static const QList<JsonParsing::KeyValidateInfo> boardKeyInfoList = {
66 {
"comment", QJsonValue::String,
false },
67 { _jsonVendorIDKey, QJsonValue::Double,
true },
68 { _jsonProductIDKey, QJsonValue::Double,
true },
69 { _jsonBoardClassKey, QJsonValue::String,
true },
70 { _jsonNameKey, QJsonValue::String,
true },
72 const QJsonArray rgBoardInfo = json[_jsonBoardInfoKey].toArray();
73 for (
const QJsonValue &jsonValue : rgBoardInfo) {
74 if (!jsonValue.isObject()) {
75 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardInfo array is not object";
79 const QJsonObject boardObject = jsonValue.toObject();
85 const BoardInfo_t boardInfo = {
86 boardObject[_jsonVendorIDKey].toInt(),
87 boardObject[_jsonProductIDKey].toInt(),
88 _boardClassStringToType(boardObject[_jsonBoardClassKey].toString()),
89 boardObject[_jsonNameKey].toString()
92 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << boardObject[_jsonBoardClassKey].toString();
96 _boardInfoList.append(boardInfo);
99 static const QList<JsonParsing::KeyValidateInfo> fallbackKeyInfoList = {
100 {
"comment", QJsonValue::String,
false },
101 { _jsonRegExpKey, QJsonValue::String,
true },
102 { _jsonBoardClassKey, QJsonValue::String,
true },
103 { _jsonAndroidOnlyKey, QJsonValue::Bool,
false },
105 const QJsonArray rgBoardDescriptionFallback = json[_jsonBoardDescriptionFallbackKey].toArray();
106 for (
const QJsonValue &jsonValue : rgBoardDescriptionFallback) {
107 if (!jsonValue.isObject()) {
108 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardFallback array is not object";
112 const QJsonObject fallbackObject = jsonValue.toObject();
118 const QRegularExpression regExp(fallbackObject[_jsonRegExpKey].toString(), QRegularExpression::CaseInsensitiveOption);
119 if (!regExp.isValid()) {
120 qCWarning(QGCSerialPortInfoLog) <<
"Invalid regular expression in board description fallback:"
121 << regExp.errorString()
122 <<
"pattern:" << fallbackObject[_jsonRegExpKey].toString();
125 const BoardRegExpFallback_t boardFallback = {
127 _boardClassStringToType(fallbackObject[_jsonBoardClassKey].toString()),
128 fallbackObject[_jsonAndroidOnlyKey].toBool(
false)
131 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << fallbackObject[_jsonBoardClassKey].toString();
135 _boardDescriptionFallbackList.append(boardFallback);
138 const QJsonArray rgBoardManufacturerFallback = json[_jsonBoardManufacturerFallbackKey].toArray();
139 for (
const QJsonValue &jsonValue : rgBoardManufacturerFallback) {
140 if (!jsonValue.isObject()) {
141 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardFallback array is not object";
145 const QJsonObject fallbackObject = jsonValue.toObject();
151 const QRegularExpression regExp(fallbackObject[_jsonRegExpKey].toString(), QRegularExpression::CaseInsensitiveOption);
152 if (!regExp.isValid()) {
153 qCWarning(QGCSerialPortInfoLog) <<
"Invalid regular expression in board manufacturer fallback:"
154 << regExp.errorString()
155 <<
"pattern:" << fallbackObject[_jsonRegExpKey].toString();
158 const BoardRegExpFallback_t boardFallback = {
160 _boardClassStringToType(fallbackObject[_jsonBoardClassKey].toString()),
161 fallbackObject[_jsonAndroidOnlyKey].toBool(
false)
164 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << fallbackObject[_jsonBoardClassKey].toString();
168 _boardManufacturerFallbackList.append(boardFallback);
171 _jsonDataValid =
true;
178 static const BoardClassString2BoardType_t rgBoardClass2BoardType[
BoardTypeUnknown] = {
185 for (
const BoardClassString2BoardType_t &board : rgBoardClass2BoardType) {
186 if (boardClass == board.classString) {
187 return board.boardType;
198 if (!_loadJsonData()) {
206 for (
const BoardInfo_t &boardInfo : _boardInfoList) {
208 boardType = boardInfo.boardType;
209 name = boardInfo.name;
216 for (
const BoardRegExpFallback_t &boardFallback : _boardDescriptionFallbackList) {
217 if (
description().contains(boardFallback.regExp)) {
219 if (boardFallback.androidOnly) {
223 boardType = boardFallback.boardType;
224 name = _boardTypeToString(boardType);
229 for (
const BoardRegExpFallback_t &boardFallback : _boardManufacturerFallbackList) {
232 if (boardFallback.androidOnly) {
236 boardType = boardFallback.boardType;
237 name = _boardTypeToString(boardType);
245QString QGCSerialPortInfo::_boardTypeToString(BoardType_t boardType)
249 return QStringLiteral(
"Pixhawk");
251 return QStringLiteral(
"SiK Radio");
253 return QStringLiteral(
"OpenPilot");
255 return QStringLiteral(
"RTK GPS");
258 return QStringLiteral(
"Unknown");
264 QList<QGCSerialPortInfo> list;
273 list << *qgcPortInfo;
292 return description().contains(QStringLiteral(
"CubeBlack"));
298 static const QList<QString> systemPortLocations = {
299 QStringLiteral(
"tty.MALS"),
300 QStringLiteral(
"tty.SOC"),
301 QStringLiteral(
"tty.Bluetooth-Incoming-Port"),
302 QStringLiteral(
"tty.usbserial"),
303 QStringLiteral(
"tty.usbmodem")
305 for (
const QString &systemPortLocation : systemPortLocations) {
327 static const QList<BoardType_t> flashable = {
332 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.
QString systemLocation() const
Returns the system location of the serial port.
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