Skip to content

Commit

Permalink
Comms: Update Serial Threading
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Dec 1, 2024
1 parent fd1ecc9 commit f727f89
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 420 deletions.
42 changes: 31 additions & 11 deletions android/libs/qtandroidserialport/qserialport_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
return false;
}

if (mode & QIODevice::ReadOnly) {
if (mode & QIODevice::ReadWrite == QIODevice::ReadOnly) {
if (!startAsyncRead()) {
qCWarning(AndroidSerialPortLog) << "Failed to start async read for" << systemLocation.toLatin1().constData();
close();
return false;
}
} else if (mode & QIODevice::WriteOnly) {
} else if (mode & QIODevice::ReadWrite == QIODevice::WriteOnly) {
if (!_stopAsyncRead()) {
qCWarning(AndroidSerialPortLog) << "Failed to stop async read for" << systemLocation.toLatin1().constData();
}
}

clear(QSerialPort::AllDirections);
(void) clear(QSerialPort::AllDirections);

return true;
}
Expand Down Expand Up @@ -109,6 +109,9 @@ void QSerialPortPrivate::newDataArrived(const char *bytes, int length)

qCDebug(AndroidSerialPortLog) << "newDataArrived" << length;

// qint64 newBytes = buffer.size();
// qint64 bytesToRead = QSERIALPORT_BUFFERSIZE;

int bytesToRead = length;
if (readBufferMaxSize && (bytesToRead > (readBufferMaxSize - buffer.size()))) {
bytesToRead = static_cast<int>(readBufferMaxSize - buffer.size());
Expand All @@ -122,6 +125,17 @@ void QSerialPortPrivate::newDataArrived(const char *bytes, int length)
char* const ptr = buffer.reserve(bytesToRead);
(void) memcpy(ptr, bytes, static_cast<size_t>(bytesToRead));

/*buffer.chop(bytesToRead - qMax(length, qint64(0)));
if (length < 0) {
setError(QSerialPortErrorInfo(QSerialPort::ReadError, QSerialPort::tr("Failed to Read New Data")));
if (QSerialPort::ResourceError) {
(void) _stopAsyncRead();
}
return;
} else if (length == 0) {
return;
}*/

// TODO: Limit signals as one read can handle multiple signals
emit q->readyRead();
}
Expand Down Expand Up @@ -169,26 +183,30 @@ bool QSerialPortPrivate::_writeDataOneShot(int msecs)
{
Q_Q(QSerialPort);

qint64 pendingBytesWritten = -1;

qint64 pendingBytesWritten = 0;
while (!writeBuffer.isEmpty()) {
const char *dataPtr = writeBuffer.readPointer();
const qint64 dataSize = writeBuffer.nextDataBlockSize();

pendingBytesWritten = _writeToPort(dataPtr, dataSize, msecs);
const qint64 written = _writeToPort(dataPtr, dataSize, msecs);

if (pendingBytesWritten <= 0) {
if (written < 0) {
qCWarning(AndroidSerialPortLog) << "Failed to write data one shot on device ID" << m_deviceId;
setError(QSerialPortErrorInfo(QSerialPort::WriteError, QSerialPort::tr("Failed to write data one shot")));
return false;
}

writeBuffer.free(pendingBytesWritten);
writeBuffer.free(written);
pendingBytesWritten += written;

}

const bool result = (pendingBytesWritten > 0);
if (result) {
emit q->bytesWritten(pendingBytesWritten);
}

return (pendingBytesWritten >= 0);
return result;
}

qint64 QSerialPortPrivate::_writeToPort(const char *data, qint64 maxSize, int timeout, bool async)
Expand All @@ -204,12 +222,14 @@ qint64 QSerialPortPrivate::_writeToPort(const char *data, qint64 maxSize, int ti

qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize)
{
if (!data || maxSize <= 0) {
if (!data || (maxSize < 0)) {
qCWarning(AndroidSerialPortLog) << "Invalid data or size in writeData for device ID" << m_deviceId;
setError(QSerialPortErrorInfo(QSerialPort::WriteError, QSerialPort::tr("Invalid data or size")));
return -1;
}

// writeBuffer.append(data, maxSize);

const qint64 result = _writeToPort(data, maxSize);
if (result < 0) {
setError(QSerialPortErrorInfo(QSerialPort::WriteError, QSerialPort::tr("Failed to write data")));
Expand Down Expand Up @@ -249,7 +269,7 @@ bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
const bool result = AndroidSerial::purgeBuffers(m_deviceId, input, output);
if (!result) {
qCWarning(AndroidSerialPortLog) << "Failed to purge buffers for device ID" << m_deviceId;
setError(QSerialPortErrorInfo(QSerialPort::UnknownError, QSerialPort::tr("Failed to purge buffers")));
// setError(QSerialPortErrorInfo(QSerialPort::UnknownError, QSerialPort::tr("Failed to purge buffers")));
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void APMAutoPilotPlugin::_checkForBadCubeBlack(void)
// FIXME: Put back
for (const QVariant& varLink: _vehicle->links()) {
SerialLink* serialLink = varLink.value<SerialLink*>();
if (serialLink && QSerialPortInfo(*serialLink->_hackAccessToPort()).description().contains(QStringLiteral("CubeBlack"))) {
if (serialLink && QSerialPortInfo(*serialLink->port()).description().contains(QStringLiteral("CubeBlack"))) {
cubeBlackFound = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Comms/LinkManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ void LinkManager::_updateSerialPorts()
for (const QGCSerialPortInfo &info: portList) {
const QString port = info.systemLocation().trimmed();
_commPortList += port;
_commPortDisplayList += SerialConfiguration::cleanPortDisplayname(port);
_commPortDisplayList += SerialConfiguration::cleanPortDisplayName(port);
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/Comms/LinkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ class LinkManager : public QObject
void loadLinkConfigurationList();
void saveLinkConfigurationList();

/// Suspend automatic confguration updates (during link maintenance for instance)
void suspendConfigurationUpdates(bool suspend) { _configUpdateSuspended = suspend; }

/// Sets the flag to suspend the all new connections
/// @param reason User visible reason to suspend connections
void setConnectionsSuspended(const QString &reason) { _connectionsSuspended = true; _connectionsSuspendedReason = reason; }
Expand Down
30 changes: 19 additions & 11 deletions src/Comms/QGCSerialPortInfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool QGCSerialPortInfo::_loadJsonData()

QString errorString;
int version;
const QJsonObject json = JsonHelper::openInternalQGCJsonFile(":/json/USBBoardInfo.json", _jsonFileTypeValue, 1, 1, version, errorString);
const QJsonObject json = JsonHelper::openInternalQGCJsonFile(QStringLiteral(":/json/USBBoardInfo.json"), QString(_jsonFileTypeValue), 1, 1, version, errorString);
if (!errorString.isEmpty()) {
qCWarning(QGCSerialPortInfoLog) << "Internal Error:" << errorString;
return false;
Expand Down Expand Up @@ -165,7 +165,14 @@ bool QGCSerialPortInfo::_loadJsonData()

QGCSerialPortInfo::BoardType_t QGCSerialPortInfo::_boardClassStringToType(const QString &boardClass)
{
for (const BoardClassString2BoardType_t &board : _rgBoardClass2BoardType) {
static const BoardClassString2BoardType_t rgBoardClass2BoardType[BoardTypeUnknown] = {
{ _boardTypeToString(BoardTypePixhawk), BoardTypePixhawk },
{ _boardTypeToString(BoardTypeRTKGPS), BoardTypeRTKGPS },
{ _boardTypeToString(BoardTypeSiKRadio), BoardTypeSiKRadio },
{ _boardTypeToString(BoardTypeOpenPilot), BoardTypeOpenPilot },
};

for (const BoardClassString2BoardType_t &board : rgBoardClass2BoardType) {
if (boardClass == board.classString) {
return board.boardType;
}
Expand Down Expand Up @@ -245,12 +252,14 @@ QString QGCSerialPortInfo::_boardTypeToString(BoardType_t boardType)
QList<QGCSerialPortInfo> QGCSerialPortInfo::availablePorts()
{
QList<QGCSerialPortInfo> list;
for (const QSerialPortInfo &portInfo : QSerialPortInfo::availablePorts()) {

const QList<QSerialPortInfo> availablePorts = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &portInfo : availablePorts) {
if (isSystemPort(portInfo)) {
continue;
}

const QGCSerialPortInfo* const qgcPortInfo = reinterpret_cast<const QGCSerialPortInfo*>(&portInfo);
const QGCSerialPortInfo *const qgcPortInfo = reinterpret_cast<const QGCSerialPortInfo*>(&portInfo);
list << *qgcPortInfo;
}

Expand Down Expand Up @@ -298,11 +307,10 @@ bool QGCSerialPortInfo::canFlash() const
return false;
}

switch(boardType) {
case QGCSerialPortInfo::BoardTypePixhawk:
case QGCSerialPortInfo::BoardTypeSiKRadio:
return true;
default:
return false;
}
static const QList<BoardType_t> flashable = {
BoardTypePixhawk,
BoardTypeSiKRadio
};

return flashable.contains(boardType);
}
19 changes: 7 additions & 12 deletions src/Comms/QGCSerialPortInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ class QGCSerialPortInfo : public QSerialPortInfo
~QGCSerialPortInfo();

enum BoardType_t {
BoardTypePixhawk,
BoardTypePixhawk = 0,
BoardTypeSiKRadio,
BoardTypeOpenPilot,
BoardTypeRTKGPS,
BoardTypeUnknown
};

bool getBoardInfo(BoardType_t &boardType, QString &name) const;

/// @return true: we can flash this board type
Expand All @@ -55,24 +56,18 @@ class QGCSerialPortInfo : public QSerialPortInfo
static QList<QGCSerialPortInfo> availablePorts();

private:
struct BoardClassString2BoardType_t {
const QString classString;
const BoardType_t boardType = BoardTypeUnknown;
};

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;
BoardType_t boardType;
};
static constexpr const BoardClassString2BoardType_t _rgBoardClass2BoardType[BoardTypeUnknown] = {
{ "Pixhawk", QGCSerialPortInfo::BoardTypePixhawk },
{ "RTK GPS", QGCSerialPortInfo::BoardTypeRTKGPS },
{ "SiK Radio", QGCSerialPortInfo::BoardTypeSiKRadio },
{ "OpenPilot", QGCSerialPortInfo::BoardTypeOpenPilot },
};

struct BoardInfo_t {
int vendorId;
int productId;
Expand Down
Loading

0 comments on commit f727f89

Please sign in to comment.