diff --git a/ground/src/plugins/coreplugin/connectionmanager.cpp b/ground/src/plugins/coreplugin/connectionmanager.cpp index 730fc9197..435cbb027 100644 --- a/ground/src/plugins/coreplugin/connectionmanager.cpp +++ b/ground/src/plugins/coreplugin/connectionmanager.cpp @@ -151,18 +151,28 @@ void ConnectionManager::onConnectPressed() else { m_connectionDevice.connection = NULL; + m_ioDev = NULL; } } } + else { + //only do this if we are disconnecting + //signal interested plugins that user is disconnecting his device + emit deviceDisconnected(); + if(m_connectionDevice.connection){ + m_connectionDevice.connection->closeDevice(m_connectionDevice.devName); + m_ioDev = NULL; + m_connectionDevice.connection = NULL; + } + } + //both in case of error and disconnection, we fall back here m_connectBtn->setText("Connect"); m_availableDevList->setEnabled(true); - //signal interested plugins that user is disconnecting his device - emit deviceDisconnected(); - //close the device + /*//close the device if(m_ioDev) { m_ioDev->close(); @@ -171,11 +181,9 @@ void ConnectionManager::onConnectPressed() delete m_ioDev; m_ioDev = NULL; } + */ - if(m_connectionDevice.connection) - m_connectionDevice.connection->closeDevice(m_connectionDevice.devName); - m_connectionDevice.connection = NULL; } /** diff --git a/ground/src/plugins/ipconnection/ipconnectionplugin.cpp b/ground/src/plugins/ipconnection/ipconnectionplugin.cpp index e390e112f..ccd305794 100644 --- a/ground/src/plugins/ipconnection/ipconnectionplugin.cpp +++ b/ground/src/plugins/ipconnection/ipconnectionplugin.cpp @@ -44,6 +44,7 @@ IPconnectionConnection::IPconnectionConnection() { + ipSocket = NULL; //create all our objects m_config = new IPconnectionConfiguration("IP Network Telemetry", NULL, this); m_config->restoresettings(); @@ -59,9 +60,11 @@ IPconnectionConnection::IPconnectionConnection() } IPconnectionConnection::~IPconnectionConnection() -{ - //tcpSocket->close (); - //delete(tcpSocket); +{//clean up out resources... + if (ipSocket){ + ipSocket->close (); + delete(ipSocket); + } } void IPconnectionConnection::onEnumerationChanged() @@ -89,10 +92,17 @@ QIODevice *IPconnectionConnection::openDevice(const QString &deviceName) int Port; QMessageBox msgBox; + if (ipSocket){ + //Andrew: close any existing socket... this should never occur + ipSocket->close (); + delete(ipSocket); + ipSocket = NULL; + } + if (m_config->UseTCP()) { - tcpSocket = new QTcpSocket(this); + ipSocket = new QTcpSocket(this); } else { - tcpSocket = new QUdpSocket(this); + ipSocket = new QUdpSocket(this); } //get the configuration info @@ -107,30 +117,29 @@ QIODevice *IPconnectionConnection::openDevice(const QString &deviceName) } else { //try to connect... - tcpSocket->connectToHost((const QString )HostName, Port); + ipSocket->connectToHost((const QString )HostName, Port); //in blocking mode so we wait for the connection to succeed - if (tcpSocket->waitForConnected(Timeout)) { - return tcpSocket; + if (ipSocket->waitForConnected(Timeout)) { + return ipSocket; } //tell user something went wrong - msgBox.setText((const QString )tcpSocket->errorString ()); + msgBox.setText((const QString )ipSocket->errorString ()); msgBox.exec(); } /* BUGBUG TODO - returning null here leads to segfault because some caller still calls disconnect without checking our return value properly * someone needs to debug this, I got lost in the calling chain.*/ - //return NULL; - return tcpSocket; + ipSocket = NULL; + return ipSocket; } void IPconnectionConnection::closeDevice(const QString &deviceName) { - //still having problems with the app crashing when we reference the tcpsocket outside the openDevice function... - //tcpSocket->close (); - //delete(tcpSocket); - // Note: CorvusCorax thinks its because the socket got deleted by the caller before this close() is even called - // so we end up with a dangling reference! Is this a bug? - + if (ipSocket){ + ipSocket->close (); + delete(ipSocket); + ipSocket = NULL; + } } diff --git a/ground/src/plugins/ipconnection/ipconnectionplugin.h b/ground/src/plugins/ipconnection/ipconnectionplugin.h index 43ef81a29..b4157d7e5 100644 --- a/ground/src/plugins/ipconnection/ipconnectionplugin.h +++ b/ground/src/plugins/ipconnection/ipconnectionplugin.h @@ -69,7 +69,7 @@ public: protected slots: void onEnumerationChanged(); private: - QAbstractSocket *tcpSocket; + QAbstractSocket *ipSocket; IPconnectionConfiguration *m_config; IPconnectionOptionsPage *m_optionspage; //QSettings* settings; diff --git a/ground/src/plugins/rawhid/rawhidplugin.cpp b/ground/src/plugins/rawhid/rawhidplugin.cpp index de2af862a..d14422a0e 100644 --- a/ground/src/plugins/rawhid/rawhidplugin.cpp +++ b/ground/src/plugins/rawhid/rawhidplugin.cpp @@ -75,6 +75,10 @@ void RawHIDEnumerationThread::run() RawHIDConnection::RawHIDConnection() : m_enumerateThread(this) { + //added by andrew + RawHidHandle = NULL; + + QObject::connect(&m_enumerateThread, SIGNAL(enumerationChanged()), this, SLOT(onEnumerationChanged())); m_enumerateThread.start(); @@ -112,12 +116,27 @@ QStringList RawHIDConnection::availableDevices() QIODevice *RawHIDConnection::openDevice(const QString &deviceName) { + //added by andrew + if (RawHidHandle){ + closeDevice(deviceName); + } + //end added by andrew m_deviceOpened = true; - return new RawHID(deviceName); + //return new RawHID(deviceName); + RawHidHandle = new RawHID(deviceName); + return RawHidHandle; } void RawHIDConnection::closeDevice(const QString &deviceName) { + //added by andrew... + if (RawHidHandle){ + RawHidHandle->close(); + delete(RawHidHandle); + RawHidHandle=NULL; + } + //end added by andrew + m_deviceOpened = false; } diff --git a/ground/src/plugins/rawhid/rawhidplugin.h b/ground/src/plugins/rawhid/rawhidplugin.h index d3b326b70..c323aa64e 100644 --- a/ground/src/plugins/rawhid/rawhidplugin.h +++ b/ground/src/plugins/rawhid/rawhidplugin.h @@ -29,6 +29,7 @@ #define RAWHIDPLUGIN_H #include "rawhid_global.h" +#include "rawhid.h" #include "coreplugin/iconnection.h" #include @@ -88,6 +89,9 @@ public: protected slots: void onEnumerationChanged(); +private: + RawHID *RawHidHandle; + protected: QMutex m_enumMutex; RawHIDEnumerationThread m_enumerateThread; diff --git a/ground/src/plugins/serialconnection/serialplugin.cpp b/ground/src/plugins/serialconnection/serialplugin.cpp index 641b5529d..09bca9f5a 100644 --- a/ground/src/plugins/serialconnection/serialplugin.cpp +++ b/ground/src/plugins/serialconnection/serialplugin.cpp @@ -37,6 +37,7 @@ SerialConnection::SerialConnection() { + serialHandle = NULL; //I'm cheating a little bit here: //Knowing if the device enumeration really changed is a bit complicated //so I just signal it whenever we have a device event... @@ -74,6 +75,9 @@ QStringList SerialConnection::availableDevices() QIODevice *SerialConnection::openDevice(const QString &deviceName) { + if (serialHandle){ + closeDevice(deviceName); + } QList ports = QextSerialEnumerator::getPorts(); foreach( QextPortInfo port, ports ) { if(port.friendName == deviceName) @@ -87,10 +91,11 @@ QIODevice *SerialConnection::openDevice(const QString &deviceName) set.FlowControl = FLOW_OFF; set.Timeout_Millisec = 500; #ifdef Q_OS_WIN - return new QextSerialPort(port.portName, set); + serialHandle = new QextSerialPort(port.portName, set); #else - return new QextSerialPort(port.physName, set); + serialHandle = new QextSerialPort(port.physName, set); #endif + return serialHandle; } } return NULL; @@ -98,7 +103,12 @@ QIODevice *SerialConnection::openDevice(const QString &deviceName) void SerialConnection::closeDevice(const QString &deviceName) { - //nothing to do here + //we have to delete the serial connection we created + if (serialHandle){ + serialHandle->close (); + delete(serialHandle); + serialHandle = NULL; + } } diff --git a/ground/src/plugins/serialconnection/serialplugin.h b/ground/src/plugins/serialconnection/serialplugin.h index 87d2c6365..d61d8c729 100644 --- a/ground/src/plugins/serialconnection/serialplugin.h +++ b/ground/src/plugins/serialconnection/serialplugin.h @@ -57,6 +57,9 @@ public: virtual QString connectionName(); virtual QString shortName(); +private: + QextSerialPort* serialHandle; + protected slots: void onEnumerationChanged(); };