2011-01-30 15:46:25 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file connectionmanager.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup CorePlugin Core Plugin
|
|
|
|
* @{
|
|
|
|
* @brief The Core GCS 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "connectionmanager.h"
|
|
|
|
|
|
|
|
#include <aggregation/aggregate.h>
|
|
|
|
#include <coreplugin/iconnection.h>
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
|
|
#include "qextserialport/src/qextserialenumerator.h"
|
|
|
|
#include "qextserialport/src/qextserialport.h"
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QComboBox>
|
2011-08-03 12:58:39 +02:00
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
2011-08-03 12:58:39 +02:00
|
|
|
ConnectionManager::ConnectionManager(Internal::MainWindow *mainWindow, QTabWidget *modeStack) :
|
2011-02-26 04:19:24 +01:00
|
|
|
QWidget(mainWindow), // Pip
|
|
|
|
m_availableDevList(0),
|
2011-01-30 15:46:25 +01:00
|
|
|
m_connectBtn(0),
|
2011-08-03 12:58:39 +02:00
|
|
|
m_ioDev(NULL),
|
|
|
|
m_mainWindow(mainWindow)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2011-05-31 16:29:53 +02:00
|
|
|
// Q_UNUSED(mainWindow);
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2011-08-03 12:58:39 +02:00
|
|
|
/* QVBoxLayout *top = new QVBoxLayout;
|
2011-01-30 15:46:25 +01:00
|
|
|
top->setSpacing(0);
|
2011-08-03 12:58:39 +02:00
|
|
|
top->setMargin(0);*/
|
2011-01-30 15:46:25 +01:00
|
|
|
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout;
|
2011-08-08 13:50:46 +02:00
|
|
|
layout->setSpacing(5);
|
|
|
|
layout->setContentsMargins(5,5,5,5);
|
|
|
|
layout->addWidget(new QLabel(tr("Connections:")));
|
2011-01-30 15:46:25 +01:00
|
|
|
|
|
|
|
m_availableDevList = new QComboBox;
|
|
|
|
//m_availableDevList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
|
|
m_availableDevList->setMinimumWidth(100);
|
|
|
|
m_availableDevList->setMaximumWidth(150);
|
|
|
|
m_availableDevList->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
layout->addWidget(m_availableDevList);
|
|
|
|
|
2011-08-08 13:50:46 +02:00
|
|
|
m_connectBtn = new QPushButton(tr("Connect"));
|
2011-01-30 15:46:25 +01:00
|
|
|
m_connectBtn->setEnabled(false);
|
|
|
|
layout->addWidget(m_connectBtn);
|
|
|
|
|
2011-08-03 12:58:39 +02:00
|
|
|
/* Utils::StyledBar *bar = new Utils::StyledBar;
|
2011-01-30 15:46:25 +01:00
|
|
|
bar->setLayout(layout);
|
|
|
|
|
2011-08-03 12:58:39 +02:00
|
|
|
top->addWidget(bar);*/
|
|
|
|
setLayout(layout);
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2011-08-03 12:58:39 +02:00
|
|
|
// modeStack->insertCornerWidget(modeStack->cornerWidgetCount()-1, this);
|
2011-08-08 13:50:46 +02:00
|
|
|
modeStack->setCornerWidget(this, Qt::TopRightCorner);
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2011-02-26 04:19:24 +01:00
|
|
|
QObject::connect(m_connectBtn, SIGNAL(pressed()), this, SLOT(onConnectPressed()));
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionManager::~ConnectionManager()
|
|
|
|
{
|
2011-02-26 04:19:24 +01:00
|
|
|
disconnectDevice(); // Pip
|
|
|
|
suspendPolling(); // Pip
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionManager::init()
|
|
|
|
{
|
|
|
|
//register to the plugin manager so we can receive
|
|
|
|
//new connection object from plugins
|
2011-02-26 04:19:24 +01:00
|
|
|
QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(objectAdded(QObject*)), this, SLOT(objectAdded(QObject*)));
|
|
|
|
QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(aboutToRemoveObject(QObject*)), this, SLOT(aboutToRemoveObject(QObject*)));
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
2011-03-04 21:42:47 +01:00
|
|
|
/**
|
|
|
|
* Method called when the user clicks the "Connect" button
|
|
|
|
*/
|
|
|
|
bool ConnectionManager::connectDevice()
|
|
|
|
{
|
2011-03-29 20:25:24 +02:00
|
|
|
devListItem connection_device = findDevice(m_availableDevList->itemData(m_availableDevList->currentIndex(),Qt::ToolTipRole).toString());
|
2011-03-25 11:59:24 +01:00
|
|
|
if (!connection_device.connection)
|
|
|
|
return false;
|
|
|
|
|
2011-03-29 20:25:24 +02:00
|
|
|
QIODevice *io_dev = connection_device.connection->openDevice(connection_device.Name);
|
2011-03-25 11:59:24 +01:00
|
|
|
if (!io_dev)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
// we appear to have connected to the device OK
|
|
|
|
// remember the connection/device details
|
|
|
|
m_connectionDevice = connection_device;
|
|
|
|
m_ioDev = io_dev;
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
connect(m_connectionDevice.connection, SIGNAL(destroyed(QObject *)), this, SLOT(onConnectionDestroyed(QObject *)), Qt::QueuedConnection);
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
// signal interested plugins that we connected to the device
|
|
|
|
emit deviceConnected(m_ioDev);
|
|
|
|
m_connectBtn->setText("Disconnect");
|
|
|
|
m_availableDevList->setEnabled(false);
|
|
|
|
return true;
|
2011-03-04 21:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method called by plugins who want to force a disconnection.
|
|
|
|
* Used by Uploader gadget for instance.
|
|
|
|
*/
|
|
|
|
bool ConnectionManager::disconnectDevice()
|
|
|
|
{
|
2011-03-25 11:59:24 +01:00
|
|
|
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;
|
|
|
|
}
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
// We are connected - disconnect from the device
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
// signal interested plugins that user is disconnecting his device
|
|
|
|
emit deviceAboutToDisconnect();
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
try {
|
|
|
|
if (m_connectionDevice.connection)
|
|
|
|
m_connectionDevice.connection->closeDevice(m_connectionDevice.devName);
|
|
|
|
} catch (...) { // handle exception
|
|
|
|
qDebug() << "Exception: m_connectionDevice.connection->closeDevice(" << m_connectionDevice.devName << ")";
|
|
|
|
}
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
m_connectionDevice.connection = NULL;
|
|
|
|
m_ioDev = NULL;
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
m_connectBtn->setText("Connect");
|
|
|
|
m_availableDevList->setEnabled(true);
|
2011-03-05 11:55:38 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
return true;
|
2011-03-04 21:42:47 +01:00
|
|
|
}
|
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
/**
|
|
|
|
* Slot called when a plugin added an object to the core pool
|
|
|
|
*/
|
|
|
|
void ConnectionManager::objectAdded(QObject *obj)
|
|
|
|
{
|
|
|
|
//Check if a plugin added a connection object to the pool
|
|
|
|
IConnection *connection = Aggregation::query<IConnection>(obj);
|
2011-02-26 04:19:24 +01:00
|
|
|
if (!connection) return;
|
2011-01-30 15:46:25 +01:00
|
|
|
|
|
|
|
//qDebug() << "Connection object registered:" << connection->connectionName();
|
|
|
|
//qDebug() << connection->availableDevices();
|
|
|
|
|
|
|
|
//register devices and populate CB
|
|
|
|
devChanged(connection);
|
|
|
|
|
|
|
|
// Keep track of the registration to be able to tell plugins
|
|
|
|
// to do things
|
|
|
|
m_connectionsList.append(connection);
|
|
|
|
|
2011-02-26 04:19:24 +01:00
|
|
|
QObject::connect(connection, SIGNAL(availableDevChanged(IConnection *)), this, SLOT(devChanged(IConnection *)));
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionManager::aboutToRemoveObject(QObject *obj)
|
|
|
|
{
|
|
|
|
//Check if a plugin added a connection object to the pool
|
|
|
|
IConnection *connection = Aggregation::query<IConnection>(obj);
|
2011-02-26 04:19:24 +01:00
|
|
|
if (!connection) return;
|
|
|
|
|
2011-03-04 21:42:47 +01:00
|
|
|
if (m_connectionDevice.connection && m_connectionDevice.connection == connection) // Pip
|
|
|
|
{ // we are currently using the one that is about to be removed
|
2011-03-03 23:03:03 +01:00
|
|
|
m_connectionDevice.connection = NULL;
|
|
|
|
m_ioDev = NULL;
|
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2011-03-04 21:42:47 +01:00
|
|
|
if (m_connectionsList.contains(connection))
|
|
|
|
m_connectionsList.removeAt(m_connectionsList.indexOf(connection));
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:19:24 +01:00
|
|
|
|
|
|
|
void ConnectionManager::onConnectionDestroyed(QObject *obj) // Pip
|
|
|
|
{
|
2011-06-07 17:15:16 +02:00
|
|
|
Q_UNUSED(obj)
|
|
|
|
//onConnectionClosed(obj);
|
|
|
|
disconnectDevice();
|
2011-02-26 04:19:24 +01:00
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Slot called when the user pressed the connect/disconnect button
|
|
|
|
*/
|
|
|
|
void ConnectionManager::onConnectPressed()
|
|
|
|
{
|
2011-03-25 11:59:24 +01:00
|
|
|
// Check if we have a ioDev already created:
|
|
|
|
if (!m_ioDev)
|
|
|
|
{ // connecting
|
|
|
|
connectDevice();
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
else
|
2011-03-25 11:59:24 +01:00
|
|
|
{ // disconnecting
|
|
|
|
disconnectDevice();
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a device by its displayed (visible on screen) name
|
|
|
|
*/
|
2011-03-29 20:25:24 +02:00
|
|
|
devListItem ConnectionManager::findDevice(const QString &devName)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2011-02-26 04:19:24 +01:00
|
|
|
foreach (devListItem d, m_devList)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2011-03-29 20:25:24 +02:00
|
|
|
if (d.devName == devName)
|
2011-01-30 15:46:25 +01:00
|
|
|
return d;
|
|
|
|
}
|
2011-02-26 04:19:24 +01:00
|
|
|
|
2011-03-29 20:25:24 +02:00
|
|
|
qDebug() << "findDevice: cannot find " << devName << " in device list";
|
2011-02-26 04:19:24 +01:00
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
devListItem d;
|
|
|
|
d.connection = NULL;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells every connection plugin to stop polling for devices if they
|
|
|
|
* are doing that.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void ConnectionManager::suspendPolling()
|
|
|
|
{
|
2011-02-26 04:19:24 +01:00
|
|
|
foreach (IConnection *cnx, m_connectionsList)
|
|
|
|
{
|
2011-01-30 15:46:25 +01:00
|
|
|
cnx->suspendPolling();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_connectBtn->setEnabled(false);
|
|
|
|
m_availableDevList->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells every connection plugin to resume polling for devices if they
|
|
|
|
* are doing that.
|
|
|
|
*/
|
|
|
|
void ConnectionManager::resumePolling()
|
|
|
|
{
|
2011-02-26 04:19:24 +01:00
|
|
|
foreach (IConnection *cnx, m_connectionsList)
|
|
|
|
{
|
2011-01-30 15:46:25 +01:00
|
|
|
cnx->resumePolling();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_connectBtn->setEnabled(true);
|
|
|
|
m_availableDevList->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
2011-03-04 21:42:47 +01:00
|
|
|
/**
|
|
|
|
* Unregister all devices from one connection plugin
|
|
|
|
* \param[in] connection Connection type that you want to forget about :)
|
|
|
|
*/
|
|
|
|
void ConnectionManager::unregisterAll(IConnection *connection)
|
|
|
|
{
|
|
|
|
for (QLinkedList<devListItem>::iterator iter = m_devList.begin(); iter != m_devList.end(); )
|
|
|
|
{
|
|
|
|
if (iter->connection == connection)
|
|
|
|
{
|
|
|
|
if (m_connectionDevice.connection && m_connectionDevice.connection == connection)
|
|
|
|
{ // we are currently using the one we are about to erase
|
2011-03-25 11:59:24 +01:00
|
|
|
//onConnectionClosed(m_connectionDevice.connection);
|
|
|
|
disconnectDevice();
|
2011-03-04 21:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iter = m_devList.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a device from a specific connection plugin
|
2011-04-17 08:32:37 +02:00
|
|
|
* @param devN contains the connection shortname + device name which is diplayed in the tooltip
|
|
|
|
* @param disp is the name that is displayed in the dropdown menu
|
|
|
|
* @param name is the actual device name
|
2011-01-30 15:46:25 +01:00
|
|
|
*/
|
2011-03-29 20:25:24 +02:00
|
|
|
void ConnectionManager::registerDevice(IConnection *conn, const QString &devN, const QString &name, const QString &disp)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
|
|
|
devListItem d;
|
|
|
|
d.connection = conn;
|
|
|
|
d.devName = devN;
|
2011-03-29 20:25:24 +02:00
|
|
|
d.Name = name;
|
|
|
|
d.displayName=disp;
|
2011-01-30 15:46:25 +01:00
|
|
|
m_devList.append(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A device plugin notified us that its device list has changed
|
|
|
|
* (eg: user plug/unplug a usb device)
|
|
|
|
* \param[in] connection Connection type which signaled the update
|
|
|
|
*/
|
|
|
|
void ConnectionManager::devChanged(IConnection *connection)
|
|
|
|
{
|
|
|
|
//clear device list combobox
|
|
|
|
m_availableDevList->clear();
|
|
|
|
|
|
|
|
//remove registered devices of this IConnection from the list
|
|
|
|
unregisterAll(connection);
|
|
|
|
|
|
|
|
//and add them back in the list
|
2011-03-29 20:25:24 +02:00
|
|
|
QList <IConnection::device> availableDev = connection->availableDevices();
|
2011-05-31 16:29:53 +02:00
|
|
|
foreach (IConnection::device dev, availableDev)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2011-03-29 20:25:24 +02:00
|
|
|
QString cbName = connection->shortName() + ": " + dev.name;
|
2011-04-17 08:32:37 +02:00
|
|
|
QString disp = connection->shortName() + " : " + dev.displayName;
|
|
|
|
registerDevice(connection,cbName,dev.name,disp);
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//add all the list again to the combobox
|
2011-05-31 16:29:53 +02:00
|
|
|
foreach (devListItem d, m_devList)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2011-03-29 20:25:24 +02:00
|
|
|
m_availableDevList->addItem(d.displayName);
|
|
|
|
m_availableDevList->setItemData(m_availableDevList->count()-1,(const QString)d.devName,Qt::ToolTipRole);
|
2011-06-01 19:40:04 +02:00
|
|
|
if(!m_ioDev && d.displayName.startsWith("USB"))
|
2011-05-31 16:29:53 +02:00
|
|
|
{
|
2011-06-01 19:40:04 +02:00
|
|
|
if(m_mainWindow->generalSettings()->autoConnect() || m_mainWindow->generalSettings()->autoSelect())
|
|
|
|
m_availableDevList->setCurrentIndex(m_availableDevList->count()-1);
|
|
|
|
if(m_mainWindow->generalSettings()->autoConnect())
|
2011-11-26 01:04:36 +01:00
|
|
|
{
|
2011-06-01 19:40:04 +02:00
|
|
|
connectDevice();
|
2011-11-26 01:04:36 +01:00
|
|
|
qDebug()<<"ConnectionManager::devChanged autoconnected USB device";
|
|
|
|
}
|
2011-05-31 16:29:53 +02:00
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
2011-05-31 16:29:53 +02:00
|
|
|
if(m_ioDev)//if a device is connected make it the one selected on the dropbox
|
|
|
|
{
|
|
|
|
for(int x=0;x<m_availableDevList->count();++x)
|
|
|
|
{
|
|
|
|
if(m_connectionDevice.devName==m_availableDevList->itemData(x,Qt::ToolTipRole).toString())
|
|
|
|
m_availableDevList->setCurrentIndex(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2011-03-29 20:25:24 +02:00
|
|
|
//disable connection button if the liNameif (m_availableDevList->count() > 0)
|
|
|
|
if (m_availableDevList->count() > 0)
|
2011-01-30 15:46:25 +01:00
|
|
|
m_connectBtn->setEnabled(true);
|
|
|
|
else
|
|
|
|
m_connectBtn->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace Core
|