diff --git a/src/comm/LinkInterface.cc b/src/comm/LinkInterface.cc index 05d9f8f..de15da9 100644 --- a/src/comm/LinkInterface.cc +++ b/src/comm/LinkInterface.cc @@ -22,8 +22,10 @@ LinkInterface::LinkInterface(SharedLinkConfigurationPtr& config, bool isPX4Flow) , _isPX4Flow(isPX4Flow) { QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); - qRegisterMetaType("LinkInterface*"); + + // This will cause the writeBytes calls to end up on the thread of the link + QObject::connect(this, &LinkInterface::_invokeWriteBytes, this, &LinkInterface::_writeBytes); } LinkInterface::~LinkInterface() @@ -80,10 +82,7 @@ void LinkInterface::_freeMavlinkChannel() void LinkInterface::writeBytesThreadSafe(const char *bytes, int length) { - QByteArray byteArray(bytes, length); - _writeBytesMutex.lock(); - _writeBytes(byteArray); - _writeBytesMutex.unlock(); + emit _invokeWriteBytes(QByteArray(bytes, length)); } void LinkInterface::addVehicleReference(void) diff --git a/src/comm/LinkInterface.h b/src/comm/LinkInterface.h index f92f22e..5a380d2 100644 --- a/src/comm/LinkInterface.h +++ b/src/comm/LinkInterface.h @@ -76,6 +76,7 @@ signals: void connected (void); void disconnected (void); void communicationError (const QString& title, const QString& error); + void _invokeWriteBytes (QByteArray); protected: // Links are only created by LinkManager so constructor is not public @@ -96,19 +97,18 @@ protected: virtual bool _allocateMavlinkChannel(); virtual void _freeMavlinkChannel (); +private slots: + virtual void _writeBytes(const QByteArray) = 0; // Not thread safe if called directly, only writeBytesThreadSafe is thread safe + private: // connect is private since all links should be created through LinkManager::createConnectedLink calls virtual bool _connect(void) = 0; - virtual void _writeBytes(const QByteArray) = 0; // Not thread safe, only writeBytesThreadSafe is thread safe - uint8_t _mavlinkChannel = std::numeric_limits::max(); bool _decodedFirstMavlinkPacket = false; bool _isPX4Flow = false; int _vehicleReferenceCount = 0; - mutable QMutex _writeBytesMutex; - QMap _mavlinkMessagesTimers; };