1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

Attempt to fix GCS crashes on USB disconnect: please confirm on other operating systems it is still working fine in all situations.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@3066 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
edouard 2011-03-25 10:59:24 +00:00 committed by edouard
parent 2e5863281c
commit 9d0ce95b37
8 changed files with 214 additions and 271 deletions

View File

@ -106,50 +106,44 @@ void ConnectionManager::init()
*/
bool ConnectionManager::connectDevice()
{
devListItem connection_device = findDevice(m_availableDevList->currentText());
if (!connection_device.connection)
return false;
devListItem connection_device = findDevice(m_availableDevList->currentText());
if (!connection_device.connection)
return false;
QIODevice *io_dev = connection_device.connection->openDevice(connection_device.devName);
if (!io_dev)
return false;
QIODevice *io_dev = connection_device.connection->openDevice(connection_device.devName);
if (!io_dev)
return false;
io_dev->open(QIODevice::ReadWrite);
io_dev->open(QIODevice::ReadWrite);
// check if opening the device worked
if (!io_dev->isOpen())
{
qDebug() << "Error: io_dev->isOpen() returned FALSE .. could not open connection to " << connection_device.devName
<< ": " << io_dev->errorString();
// check if opening the device worked
if (!io_dev->isOpen()) {
qDebug() << "Error: io_dev->isOpen() returned FALSE .. could not open connection to " << connection_device.devName
<< ": " << io_dev->errorString();
// close the device
try
{
connection_device.connection->closeDevice(connection_device.devName);
}
catch (...)
{ // handle exception
qDebug() << "Exception: connection_device.connection->closeDevice(" << connection_device.devName << ")";
}
// close the device
// EDOUARD: why do we close if we could not open ???
try {
connection_device.connection->closeDevice(connection_device.devName);
}
catch (...) { // handle exception
qDebug() << "Exception: connection_device.connection->closeDevice(" << connection_device.devName << ")";
}
return false;
}
return false;
}
// we appear to have connected to the device OK
// remember the connection/device details
m_connectionDevice = connection_device;
m_ioDev = io_dev;
// we appear to have connected to the device OK
connect(m_connectionDevice.connection, SIGNAL(destroyed(QObject *)), this, SLOT(onConnectionDestroyed(QObject *)), Qt::QueuedConnection);
// remember the connection/device details
m_connectionDevice = connection_device;
m_ioDev = io_dev;
connect(m_connectionDevice.connection, SIGNAL(destroyed(QObject *)), this, SLOT(onConnectionDestroyed(QObject *)), Qt::QueuedConnection);
m_connectBtn->setText("Disconnect");
m_availableDevList->setEnabled(false);
// signal interested plugins that we connected to the device
emit deviceConnected(m_ioDev);
return true;
// signal interested plugins that we connected to the device
emit deviceConnected(m_ioDev);
m_connectBtn->setText("Disconnect");
m_availableDevList->setEnabled(false);
return true;
}
/**
@ -158,30 +152,32 @@ bool ConnectionManager::connectDevice()
*/
bool ConnectionManager::disconnectDevice()
{
if (!m_ioDev)
{ // apparently we are already disconnected
if (!m_ioDev) {
// apparently we are already disconnected: this can
// happen if a plugin tries to force a disconnect whereas
// we are not connected. Just return.
return false;
}
m_connectionDevice.connection = NULL;
m_ioDev = NULL;
// We are connected - disconnect from the device
return false;
}
// signal interested plugins that user is disconnecting his device
emit deviceAboutToDisconnect();
// we appear to be connected - disconnect from the device
try {
if (m_connectionDevice.connection)
m_connectionDevice.connection->closeDevice(m_connectionDevice.devName);
} catch (...) { // handle exception
qDebug() << "Exception: m_connectionDevice.connection->closeDevice(" << m_connectionDevice.devName << ")";
}
try
{
if (m_connectionDevice.connection)
m_connectionDevice.connection->closeDevice(m_connectionDevice.devName);
}
catch (...)
{ // handle exception
qDebug() << "Exception: m_connectionDevice.connection->closeDevice(" << m_connectionDevice.devName << ")";
}
m_connectionDevice.connection = NULL;
m_ioDev = NULL;
onConnectionClosed(m_connectionDevice.connection);
m_connectBtn->setText("Connect");
m_availableDevList->setEnabled(true);
return true;
return true;
}
/**
@ -222,24 +218,11 @@ void ConnectionManager::aboutToRemoveObject(QObject *obj)
m_connectionsList.removeAt(m_connectionsList.indexOf(connection));
}
void ConnectionManager::onConnectionClosed(QObject *obj) // Pip
{
if (!m_connectionDevice.connection || m_connectionDevice.connection != obj)
return;
m_connectionDevice.connection = NULL;
m_ioDev = NULL;
m_connectBtn->setText("Connect");
m_availableDevList->setEnabled(true);
// signal interested plugins that user is disconnecting his device
emit deviceDisconnected();
}
void ConnectionManager::onConnectionDestroyed(QObject *obj) // Pip
{
onConnectionClosed(obj);
//onConnectionClosed(obj);
disconnectDevice();
}
/**
@ -247,13 +230,14 @@ void ConnectionManager::onConnectionDestroyed(QObject *obj) // Pip
*/
void ConnectionManager::onConnectPressed()
{
if (!m_ioDev || !m_connectionDevice.connection)
{ // connecting
connectDevice();
// Check if we have a ioDev already created:
if (!m_ioDev)
{ // connecting
connectDevice();
}
else
{ // disconnecting
disconnectDevice();
{ // disconnecting
disconnectDevice();
}
}
@ -318,7 +302,8 @@ void ConnectionManager::unregisterAll(IConnection *connection)
{
if (m_connectionDevice.connection && m_connectionDevice.connection == connection)
{ // we are currently using the one we are about to erase
onConnectionClosed(m_connectionDevice.connection);
//onConnectionClosed(m_connectionDevice.connection);
disconnectDevice();
}
iter = m_devList.erase(iter);

View File

@ -81,7 +81,7 @@ protected:
signals:
void deviceConnected(QIODevice *dev);
void deviceDisconnected();
void deviceAboutToDisconnect();
private slots:
void objectAdded(QObject *obj);
@ -90,7 +90,7 @@ private slots:
void onConnectPressed();
void devChanged(IConnection *connection);
void onConnectionClosed(QObject *obj);
// void onConnectionClosed(QObject *obj);
void onConnectionDestroyed(QObject *obj);
protected:

View File

@ -94,7 +94,7 @@ QStringList RawHIDConnection::availableDevices()
QIODevice *RawHIDConnection::openDevice(const QString &deviceName)
{
//added by andrew
if (RawHidHandle)
if (RawHidHandle)
closeDevice(deviceName);
//end added by andrew

View File

@ -74,7 +74,7 @@ ScopeGadgetWidget::ScopeGadgetWidget(QWidget *parent) : QwtPlot(parent)
// running the scopes if we are not connected and not replaying logs
// Also listen to disconnect actions from the user
Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager();
connect(cm, SIGNAL(deviceDisconnected()), this, SLOT(stopPlotting()));
connect(cm, SIGNAL(deviceAboutToDisconnect()), this, SLOT(stopPlotting()));
connect(cm, SIGNAL(deviceConnected(QIODevice*)), this, SLOT(startPlotting()));
m_csvLoggingStarted=0;
@ -89,7 +89,7 @@ ScopeGadgetWidget::ScopeGadgetWidget(QWidget *parent) : QwtPlot(parent)
m_csvLoggingStartTime = QDateTime::currentDateTime();
//Listen to autopilot connection events
connect(cm, SIGNAL(deviceDisconnected()), this, SLOT(csvLoggingDisconnect()));
connect(cm, SIGNAL(deviceAboutToDisconnect()), this, SLOT(csvLoggingDisconnect()));
connect(cm, SIGNAL(deviceConnected(QIODevice*)), this, SLOT(csvLoggingConnect()));
}

View File

@ -56,6 +56,7 @@ const quint8 UAVTalk::crc_table[256] = {
UAVTalk::UAVTalk(QIODevice* iodev, UAVObjectManager* objMngr)
{
io = iodev;
this->objMngr = objMngr;
rxState = STATE_SYNC;
@ -67,23 +68,17 @@ UAVTalk::UAVTalk(QIODevice* iodev, UAVObjectManager* objMngr)
memset(&stats, 0, sizeof(ComStats));
if (io) // Pip
connect(io, SIGNAL(readyRead()), this, SLOT(processInputStream()));
connect(io, SIGNAL(readyRead()), this, SLOT(processInputStream()));
}
UAVTalk::~UAVTalk()
{
// Pip
mutex->lock();
io = NULL;
objMngr = NULL;
mutex->unlock();
// According to Qt, it is not necessary to disconnect upon
// object deletion.
//disconnect(io, SIGNAL(readyRead()), this, SLOT(processInputStream()));
}
/**
* Reset the statistics counters
*/
@ -109,24 +104,9 @@ void UAVTalk::processInputStream()
{
quint8 tmp;
while (true)
while (io->bytesAvailable() > 0)
{
mutex->lock(); // Pip
if (!io)
{
mutex->unlock();
break;
}
if (!io->isOpen() || io->bytesAvailable() <= 0)
{
mutex->unlock();
break;
}
qint64 bytes_read = io->read((char*)&tmp, sizeof(tmp));
mutex->unlock();
if (bytes_read <= 0) break; // Pip
io->read((char*)&tmp, 1);
processInputByte(tmp);
}
}
@ -216,9 +196,6 @@ bool UAVTalk::objectTransaction(UAVObject* obj, quint8 type, bool allInstances)
*/
bool UAVTalk::processInputByte(quint8 rxbyte)
{
if (!objMngr || !io) // Pip
return false;
// Update stats
stats.rxBytes++;
@ -429,10 +406,7 @@ bool UAVTalk::receiveObject(quint8 type, quint32 objId, quint16 instId, quint8*
{
Q_UNUSED(length);
if (!objMngr || !io) // Pip
return false;
UAVObject* obj = NULL;
UAVObject* obj = NULL;
bool error = false;
bool allInstances = (instId == ALL_INSTANCES? true : false);
@ -531,10 +505,7 @@ bool UAVTalk::receiveObject(quint8 type, quint32 objId, quint16 instId, quint8*
*/
UAVObject* UAVTalk::updateObject(quint32 objId, quint16 instId, quint8* data)
{
if (!objMngr || !io) // Pip
return NULL;
// Get object
// Get object
UAVObject* obj = objMngr->getObject(objId, instId);
// If the instance does not exist create it
if (obj == NULL)
@ -589,10 +560,7 @@ void UAVTalk::updateAck(UAVObject* obj)
*/
bool UAVTalk::transmitObject(UAVObject* obj, quint8 type, bool allInstances)
{
if (!objMngr || !io) // Pip
return false;
// If all instances are requested on a single instance object it is an error
// If all instances are requested on a single instance object it is an error
if (allInstances && obj->isSingleInstance())
{
allInstances = false;
@ -654,10 +622,7 @@ bool UAVTalk::transmitSingleObject(UAVObject* obj, quint8 type, bool allInstance
quint16 instId;
quint16 allInstId = ALL_INSTANCES;
if (!objMngr || !io) // Pip
return false;
// Setup type and object id fields
// Setup type and object id fields
objId = obj->getObjID();
txBuffer[0] = SYNC_VAL;
txBuffer[1] = type;

View File

@ -1,127 +1,127 @@
/**
******************************************************************************
*
* @file uavtalk.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @addtogroup GCSPlugins GCS Plugins
* @{
* @addtogroup UAVTalkPlugin UAVTalk Plugin
* @{
* @brief The UAVTalk protocol plugin
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef UAVTALK_H
#define UAVTALK_H
#include <QIODevice>
#include <QMutex>
#include <QMutexLocker>
#include <QSemaphore>
#include "uavobjectmanager.h"
#include "uavtalk_global.h"
class UAVTALK_EXPORT UAVTalk: public QObject
{
Q_OBJECT
public:
typedef struct {
quint32 txBytes;
quint32 rxBytes;
quint32 txObjectBytes;
quint32 rxObjectBytes;
quint32 rxObjects;
quint32 txObjects;
quint32 txErrors;
quint32 rxErrors;
} ComStats;
UAVTalk(QIODevice* iodev, UAVObjectManager* objMngr);
~UAVTalk();
bool sendObject(UAVObject* obj, bool acked, bool allInstances);
bool sendObjectRequest(UAVObject* obj, bool allInstances);
void cancelTransaction();
ComStats getStats();
void resetStats();
signals:
void transactionCompleted(UAVObject* obj);
private slots:
void processInputStream(void);
private:
// Constants
static const int TYPE_MASK = 0xFC;
static const int TYPE_VER = 0x20;
static const int TYPE_OBJ = (TYPE_VER | 0x00);
static const int TYPE_OBJ_REQ = (TYPE_VER | 0x01);
static const int TYPE_OBJ_ACK = (TYPE_VER | 0x02);
static const int TYPE_ACK = (TYPE_VER | 0x03);
static const int MIN_HEADER_LENGTH = 8; // sync(1), type (1), size(2), object ID(4)
static const int MAX_HEADER_LENGTH = 10; // sync(1), type (1), size(2), object ID (4), instance ID(2, not used in single objects)
static const int CHECKSUM_LENGTH = 1;
static const int MAX_PAYLOAD_LENGTH = 256;
static const int MAX_PACKET_LENGTH = (MAX_HEADER_LENGTH + MAX_PAYLOAD_LENGTH + CHECKSUM_LENGTH);
static const quint16 ALL_INSTANCES = 0xFFFF;
static const int TX_BUFFER_SIZE = 2*1024;
static const quint8 crc_table[256];
// Types
typedef enum {STATE_SYNC, STATE_TYPE, STATE_SIZE, STATE_OBJID, STATE_INSTID, STATE_DATA, STATE_CS} RxStateType;
// Variables
QIODevice* io;
UAVObjectManager* objMngr;
QMutex* mutex;
UAVObject* respObj;
bool respAllInstances;
quint8 rxBuffer[MAX_PACKET_LENGTH];
quint8 txBuffer[MAX_PACKET_LENGTH];
// Variables used by the receive state machine
quint8 rxTmpBuffer[4];
quint8 rxType;
quint32 rxObjId;
quint16 rxInstId;
quint16 rxLength;
quint16 rxPacketLength;
quint8 rxCSPacket, rxCS;
qint32 rxCount;
qint32 packetSize;
RxStateType rxState;
ComStats stats;
// Methods
bool objectTransaction(UAVObject* obj, quint8 type, bool allInstances);
bool processInputByte(quint8 rxbyte);
bool receiveObject(quint8 type, quint32 objId, quint16 instId, quint8* data, qint32 length);
UAVObject* updateObject(quint32 objId, quint16 instId, quint8* data);
void updateAck(UAVObject* obj);
bool transmitObject(UAVObject* obj, quint8 type, bool allInstances);
bool transmitSingleObject(UAVObject* obj, quint8 type, bool allInstances);
quint8 updateCRC(quint8 crc, const quint8 data);
quint8 updateCRC(quint8 crc, const quint8* data, qint32 length);
};
#endif // UAVTALK_H
/**
******************************************************************************
*
* @file uavtalk.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @addtogroup GCSPlugins GCS Plugins
* @{
* @addtogroup UAVTalkPlugin UAVTalk Plugin
* @{
* @brief The UAVTalk protocol plugin
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef UAVTALK_H
#define UAVTALK_H
#include <QIODevice>
#include <QMutex>
#include <QMutexLocker>
#include <QSemaphore>
#include "uavobjectmanager.h"
#include "uavtalk_global.h"
class UAVTALK_EXPORT UAVTalk: public QObject
{
Q_OBJECT
public:
typedef struct {
quint32 txBytes;
quint32 rxBytes;
quint32 txObjectBytes;
quint32 rxObjectBytes;
quint32 rxObjects;
quint32 txObjects;
quint32 txErrors;
quint32 rxErrors;
} ComStats;
UAVTalk(QIODevice* iodev, UAVObjectManager* objMngr);
~UAVTalk();
bool sendObject(UAVObject* obj, bool acked, bool allInstances);
bool sendObjectRequest(UAVObject* obj, bool allInstances);
void cancelTransaction();
ComStats getStats();
void resetStats();
signals:
void transactionCompleted(UAVObject* obj);
private slots:
void processInputStream(void);
private:
// Constants
static const int TYPE_MASK = 0xFC;
static const int TYPE_VER = 0x20;
static const int TYPE_OBJ = (TYPE_VER | 0x00);
static const int TYPE_OBJ_REQ = (TYPE_VER | 0x01);
static const int TYPE_OBJ_ACK = (TYPE_VER | 0x02);
static const int TYPE_ACK = (TYPE_VER | 0x03);
static const int MIN_HEADER_LENGTH = 8; // sync(1), type (1), size(2), object ID(4)
static const int MAX_HEADER_LENGTH = 10; // sync(1), type (1), size(2), object ID (4), instance ID(2, not used in single objects)
static const int CHECKSUM_LENGTH = 1;
static const int MAX_PAYLOAD_LENGTH = 256;
static const int MAX_PACKET_LENGTH = (MAX_HEADER_LENGTH + MAX_PAYLOAD_LENGTH + CHECKSUM_LENGTH);
static const quint16 ALL_INSTANCES = 0xFFFF;
static const int TX_BUFFER_SIZE = 2*1024;
static const quint8 crc_table[256];
// Types
typedef enum {STATE_SYNC, STATE_TYPE, STATE_SIZE, STATE_OBJID, STATE_INSTID, STATE_DATA, STATE_CS} RxStateType;
// Variables
QIODevice* io;
UAVObjectManager* objMngr;
QMutex* mutex;
UAVObject* respObj;
bool respAllInstances;
quint8 rxBuffer[MAX_PACKET_LENGTH];
quint8 txBuffer[MAX_PACKET_LENGTH];
// Variables used by the receive state machine
quint8 rxTmpBuffer[4];
quint8 rxType;
quint32 rxObjId;
quint16 rxInstId;
quint16 rxLength;
quint16 rxPacketLength;
quint8 rxCSPacket, rxCS;
qint32 rxCount;
qint32 packetSize;
RxStateType rxState;
ComStats stats;
// Methods
bool objectTransaction(UAVObject* obj, quint8 type, bool allInstances);
bool processInputByte(quint8 rxbyte);
bool receiveObject(quint8 type, quint32 objId, quint16 instId, quint8* data, qint32 length);
UAVObject* updateObject(quint32 objId, quint16 instId, quint8* data);
void updateAck(UAVObject* obj);
bool transmitObject(UAVObject* obj, quint8 type, bool allInstances);
bool transmitSingleObject(UAVObject* obj, quint8 type, bool allInstances);
quint8 updateCRC(quint8 crc, const quint8 data);
quint8 updateCRC(quint8 crc, const quint8* data, qint32 length);
};
#endif // UAVTALK_H

View File

@ -31,27 +31,29 @@
UAVTalkPlugin::UAVTalkPlugin()
{
telMngr = NULL; // Pip
}
UAVTalkPlugin::~UAVTalkPlugin()
{
if (telMngr) // Pip
telMngr->stop();
}
void UAVTalkPlugin::extensionsInitialized()
{
// Get UAVObjectManager instance
ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
objMngr = pm->getObject<UAVObjectManager>();
// Create TelemetryManager
telMngr = new TelemetryManager();
if (!telMngr) return; // Pip
addAutoReleasedObject(telMngr);
// Connect to connection manager so we get notified when the user connect to his device
Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager();
QObject::connect(cm, SIGNAL(deviceConnected(QIODevice *)),
this, SLOT(onDeviceConnect(QIODevice *)));
QObject::connect(cm, SIGNAL(deviceDisconnected()),
QObject::connect(cm, SIGNAL(deviceConnected(QIODevice *)),
this, SLOT(onDeviceConnect(QIODevice *)));
QObject::connect(cm, SIGNAL(deviceAboutToDisconnect()),
this, SLOT(onDeviceDisconnect()));
}
@ -65,18 +67,17 @@ bool UAVTalkPlugin::initialize(const QStringList & arguments, QString * errorStr
void UAVTalkPlugin::shutdown()
{
}
void UAVTalkPlugin::onDeviceConnect(QIODevice *dev)
{
if (telMngr) // Pip
telMngr->start(dev);
telMngr->start(dev);
}
void UAVTalkPlugin::onDeviceDisconnect()
{
if (telMngr) // Pip
telMngr->stop();
telMngr->stop();
}
Q_EXPORT_PLUGIN(UAVTalkPlugin)

View File

@ -43,14 +43,6 @@ UploaderGadgetWidget::UploaderGadgetWidget(QWidget *parent) : QWidget(parent)
connect(telMngr, SIGNAL(connected()), this, SLOT(onAutopilotConnect()));
connect(telMngr, SIGNAL(disconnected()), this, SLOT(onAutopilotDisconnect()));
// Note: remove listening to the connection manager, it overlaps with
// listening to the telemetry manager, we should only listen to one, not both.
// Also listen to disconnect actions from the user:
// Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager();
// connect(cm, SIGNAL(deviceDisconnected()), this, SLOT(onAutopilotDisconnect()));
connect(m_config->haltButton, SIGNAL(clicked()), this, SLOT(goToBootloader()));
connect(m_config->resetButton, SIGNAL(clicked()), this, SLOT(systemReset()));
connect(m_config->bootButton, SIGNAL(clicked()), this, SLOT(systemBoot()));