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 = {
53 { _jsonBoardInfoKey, QJsonValue::Array,
true },
54 { _jsonBoardDescriptionFallbackKey, QJsonValue::Array,
true },
55 { _jsonBoardManufacturerFallbackKey, QJsonValue::Array,
true },
62 static const QList<JsonParsing::KeyValidateInfo> boardKeyInfoList = {
63 { _jsonVendorIDKey, QJsonValue::Double,
true },
64 { _jsonProductIDKey, QJsonValue::Double,
true },
65 { _jsonBoardClassKey, QJsonValue::String,
true },
66 { _jsonNameKey, QJsonValue::String,
true },
68 const QJsonArray rgBoardInfo = json[_jsonBoardInfoKey].toArray();
69 for (
const QJsonValue &jsonValue : rgBoardInfo) {
70 if (!jsonValue.isObject()) {
71 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardInfo array is not object";
75 const QJsonObject boardObject = jsonValue.toObject();
81 const BoardInfo_t boardInfo = {
82 boardObject[_jsonVendorIDKey].toInt(),
83 boardObject[_jsonProductIDKey].toInt(),
84 _boardClassStringToType(boardObject[_jsonBoardClassKey].toString()),
85 boardObject[_jsonNameKey].toString()
88 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << boardObject[_jsonBoardClassKey].toString();
92 _boardInfoList.append(boardInfo);
95 static const QList<JsonParsing::KeyValidateInfo> fallbackKeyInfoList = {
96 { _jsonRegExpKey, QJsonValue::String,
true },
97 { _jsonBoardClassKey, QJsonValue::String,
true },
98 { _jsonAndroidOnlyKey, QJsonValue::Bool,
false },
100 const QJsonArray rgBoardDescriptionFallback = json[_jsonBoardDescriptionFallbackKey].toArray();
101 for (
const QJsonValue &jsonValue : rgBoardDescriptionFallback) {
102 if (!jsonValue.isObject()) {
103 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardFallback array is not object";
107 const QJsonObject fallbackObject = jsonValue.toObject();
113 const QRegularExpression regExp(fallbackObject[_jsonRegExpKey].toString(), QRegularExpression::CaseInsensitiveOption);
114 if (!regExp.isValid()) {
115 qCWarning(QGCSerialPortInfoLog) <<
"Invalid regular expression in board description fallback:"
116 << regExp.errorString()
117 <<
"pattern:" << fallbackObject[_jsonRegExpKey].toString();
120 const BoardRegExpFallback_t boardFallback = {
122 _boardClassStringToType(fallbackObject[_jsonBoardClassKey].toString()),
123 fallbackObject[_jsonAndroidOnlyKey].toBool(
false)
126 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << fallbackObject[_jsonBoardClassKey].toString();
130 _boardDescriptionFallbackList.append(boardFallback);
133 const QJsonArray rgBoardManufacturerFallback = json[_jsonBoardManufacturerFallbackKey].toArray();
134 for (
const QJsonValue &jsonValue : rgBoardManufacturerFallback) {
135 if (!jsonValue.isObject()) {
136 qCWarning(QGCSerialPortInfoLog) <<
"Entry in boardFallback array is not object";
140 const QJsonObject fallbackObject = jsonValue.toObject();
146 const QRegularExpression regExp(fallbackObject[_jsonRegExpKey].toString(), QRegularExpression::CaseInsensitiveOption);
147 if (!regExp.isValid()) {
148 qCWarning(QGCSerialPortInfoLog) <<
"Invalid regular expression in board manufacturer fallback:"
149 << regExp.errorString()
150 <<
"pattern:" << fallbackObject[_jsonRegExpKey].toString();
153 const BoardRegExpFallback_t boardFallback = {
155 _boardClassStringToType(fallbackObject[_jsonBoardClassKey].toString()),
156 fallbackObject[_jsonAndroidOnlyKey].toBool(
false)
159 qCWarning(QGCSerialPortInfoLog) <<
"Bad board class" << fallbackObject[_jsonBoardClassKey].toString();
163 _boardManufacturerFallbackList.append(boardFallback);
166 _jsonDataValid =
true;
173 static const BoardClassString2BoardType_t rgBoardClass2BoardType[
BoardTypeUnknown] = {
180 for (
const BoardClassString2BoardType_t &board : rgBoardClass2BoardType) {
181 if (boardClass == board.classString) {
182 return board.boardType;
193 if (!_loadJsonData()) {
201 for (
const BoardInfo_t &boardInfo : _boardInfoList) {
203 boardType = boardInfo.boardType;
204 name = boardInfo.name;
211 for (
const BoardRegExpFallback_t &boardFallback : _boardDescriptionFallbackList) {
212 if (
description().contains(boardFallback.regExp)) {
214 if (boardFallback.androidOnly) {
218 boardType = boardFallback.boardType;
219 name = _boardTypeToString(boardType);
224 for (
const BoardRegExpFallback_t &boardFallback : _boardManufacturerFallbackList) {
227 if (boardFallback.androidOnly) {
231 boardType = boardFallback.boardType;
232 name = _boardTypeToString(boardType);
240QString QGCSerialPortInfo::_boardTypeToString(BoardType_t boardType)
244 return QStringLiteral(
"Pixhawk");
246 return QStringLiteral(
"SiK Radio");
248 return QStringLiteral(
"OpenPilot");
250 return QStringLiteral(
"RTK GPS");
253 return QStringLiteral(
"Unknown");
259 QList<QGCSerialPortInfo> list;
268 list << *qgcPortInfo;
287 return description().contains(QStringLiteral(
"CubeBlack"));
293 static const QList<QString> systemPortLocations = {
294 QStringLiteral(
"tty.MALS"),
295 QStringLiteral(
"tty.SOC"),
296 QStringLiteral(
"tty.Bluetooth-Incoming-Port"),
297 QStringLiteral(
"tty.usbserial"),
298 QStringLiteral(
"tty.usbmodem")
300 for (
const QString &systemPortLocation : systemPortLocations) {
322 static const QList<BoardType_t> flashable = {
327 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.
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.
QJsonObject openInternalQGCJsonFile(const QString &jsonFilename, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString, const QStringList &defaultTranslateKeys, const QStringList &defaultArrayIDKeys)