From a4147d5fb24b54081f7418f914fcedb99b0ea2c8 Mon Sep 17 00:00:00 2001 From: julien Date: Sun, 11 Apr 2010 21:08:46 +0000 Subject: [PATCH] GCS/connectionManager: some light cleanup and refactoring GCS/serialconnection: added a new connection plugin for serial port git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@483 ebee16cc-31ac-478f-84a7-5cbb03baadba --- .../plugins/coreplugin/connectionmanager.cpp | 44 +++---- .../plugins/coreplugin/connectionmanager.h | 6 +- ground/src/plugins/plugins.pro | 7 +- ground/src/plugins/rawhid/pjrc_rawhid_win.cpp | 2 +- .../serialconnection/Serial.pluginspec | 10 ++ .../src/plugins/serialconnection/serial.pri | 3 + .../serialconnection/serial_dependencies.pri | 1 + .../plugins/serialconnection/serial_global.h | 39 ++++++ .../serialconnection/serialconnection.pro | 12 ++ .../plugins/serialconnection/serialplugin.cpp | 122 ++++++++++++++++++ .../plugins/serialconnection/serialplugin.h | 78 +++++++++++ ground/src/plugins/uavtalk/uavtalkplugin.cpp | 4 +- 12 files changed, 297 insertions(+), 31 deletions(-) create mode 100644 ground/src/plugins/serialconnection/Serial.pluginspec create mode 100644 ground/src/plugins/serialconnection/serial.pri create mode 100644 ground/src/plugins/serialconnection/serial_dependencies.pri create mode 100644 ground/src/plugins/serialconnection/serial_global.h create mode 100644 ground/src/plugins/serialconnection/serialconnection.pro create mode 100644 ground/src/plugins/serialconnection/serialplugin.cpp create mode 100644 ground/src/plugins/serialconnection/serialplugin.h diff --git a/ground/src/plugins/coreplugin/connectionmanager.cpp b/ground/src/plugins/coreplugin/connectionmanager.cpp index 500b7acb2..f38eb36ba 100644 --- a/ground/src/plugins/coreplugin/connectionmanager.cpp +++ b/ground/src/plugins/coreplugin/connectionmanager.cpp @@ -51,7 +51,7 @@ namespace Core { ConnectionManager::ConnectionManager(Internal::MainWindow *mainWindow, Internal::FancyTabWidget *modeStack) : m_availableDevList(0), m_connectBtn(0), - m_deviceConnected(false) + m_ioDev(NULL) { QVBoxLayout *top = new QVBoxLayout; top->setSpacing(0); @@ -128,37 +128,24 @@ void ConnectionManager::aboutToRemoveObject(QObject *obj) */ void ConnectionManager::onConnectPressed() { - if(!m_deviceConnected) + //check if we are trying to connect a new device + //or if we are disconnecting it + if(!m_ioDev) { m_connectBtn->setText("Disconnect"); m_availableDevList->setEnabled(false); - m_deviceConnected = true; m_connectionDevice = findDevice(m_availableDevList->currentText()); if(m_connectionDevice.connection) { - QIODevice *ioDev = m_connectionDevice.connection->openDevice(m_connectionDevice.devName); - /*QextSerialPort *ioDev = new QextSerialPort("COM5"); - ioDev->setBaudRate(BAUD57600); - ioDev->setFlowControl(FLOW_OFF);*/ - ioDev->open(QIODevice::ReadWrite); - /*if(ioDev->isOpen()) - ioDev->putChar('M');*/ - QList ports = QextSerialEnumerator::getPorts(); - qDebug() << "List of ports:"; - for (int i = 0; i < ports.size(); i++) { - qDebug() << "port name:" << ports.at(i).portName; - qDebug() << "friendly name:" << ports.at(i).friendName; - qDebug() << "physical name:" << ports.at(i).physName; - qDebug() << "enumerator name:" << ports.at(i).enumName; - qDebug() << "vendor ID:" << QString::number(ports.at(i).vendorID, 16); - qDebug() << "product ID:" << QString::number(ports.at(i).productID, 16); - qDebug() << "==================================="; - } - if(ioDev) + m_ioDev = m_connectionDevice.connection->openDevice(m_connectionDevice.devName); + + if(m_ioDev) { - emit deviceConnected(ioDev); + m_ioDev->open(QIODevice::ReadWrite); + //signal interested plugins that the user wants to connect to the device + emit deviceConnected(m_ioDev); return; } else @@ -172,13 +159,20 @@ void ConnectionManager::onConnectPressed() m_connectBtn->setText("Connect"); m_availableDevList->setEnabled(true); - m_deviceConnected = false; + //signal interested plugins that user is disconnecting his device + emit deviceDisconnected(); + + //close the device + if(m_ioDev) + { + m_ioDev->close(); + m_ioDev = NULL; + } if(m_connectionDevice.connection) m_connectionDevice.connection->closeDevice(m_connectionDevice.devName); m_connectionDevice.connection = NULL; - emit deviceDisconnected(); } /** diff --git a/ground/src/plugins/coreplugin/connectionmanager.h b/ground/src/plugins/coreplugin/connectionmanager.h index cd0c40011..262518c0e 100644 --- a/ground/src/plugins/coreplugin/connectionmanager.h +++ b/ground/src/plugins/coreplugin/connectionmanager.h @@ -90,8 +90,12 @@ protected: QComboBox *m_availableDevList; QPushButton *m_connectBtn; QLinkedList m_devList; + + //currently connected connection plugin devListItem m_connectionDevice; - bool m_deviceConnected; + + //currently connected QIODevice + QIODevice *m_ioDev; }; } //namespace Core diff --git a/ground/src/plugins/plugins.pro b/ground/src/plugins/plugins.pro index 855395aa3..7846ef2cd 100644 --- a/ground/src/plugins/plugins.pro +++ b/ground/src/plugins/plugins.pro @@ -18,11 +18,16 @@ plugin_welcome.subdir = welcome plugin_welcome.depends = plugin_coreplugin SUBDIRS += plugin_welcome -# RawHID plug-in +# RawHID connection plugin SUBDIRS += plugin_rawhid plugin_rawhid.subdir = rawhid plugin_rawhid.depends = plugin_coreplugin +# Serial port connection plugin +SUBDIRS += plugin_serial +plugin_serial.subdir = serialconnection +plugin_serial.depends = plugin_coreplugin + # UAVObjects plug-in SUBDIRS += plugin_uavobjects plugin_uavobjects.subdir = uavobjects diff --git a/ground/src/plugins/rawhid/pjrc_rawhid_win.cpp b/ground/src/plugins/rawhid/pjrc_rawhid_win.cpp index bbebd9504..1da20094b 100644 --- a/ground/src/plugins/rawhid/pjrc_rawhid_win.cpp +++ b/ground/src/plugins/rawhid/pjrc_rawhid_win.cpp @@ -126,7 +126,7 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage) if (h == INVALID_HANDLE_VALUE) continue; attrib.Size = sizeof(HIDD_ATTRIBUTES); ret = HidD_GetAttributes(h, &attrib); - printf("vid: %4x\n", attrib.VendorID); + //printf("vid: %4x\n", attrib.VendorID); if (!ret || (vid > 0 && attrib.VendorID != vid) || (pid > 0 && attrib.ProductID != pid) || !HidD_GetPreparsedData(h, &hid_data)) { diff --git a/ground/src/plugins/serialconnection/Serial.pluginspec b/ground/src/plugins/serialconnection/Serial.pluginspec new file mode 100644 index 000000000..4ab648a4d --- /dev/null +++ b/ground/src/plugins/serialconnection/Serial.pluginspec @@ -0,0 +1,10 @@ + +The OpenPilot Project + (C) 2010 OpenPilot Project + GNU Public License (GPL) Version 3 + Connection to OpenPilot board using serial link interface + http://www.openpilot.org + + + + diff --git a/ground/src/plugins/serialconnection/serial.pri b/ground/src/plugins/serialconnection/serial.pri new file mode 100644 index 000000000..d14f80e74 --- /dev/null +++ b/ground/src/plugins/serialconnection/serial.pri @@ -0,0 +1,3 @@ +include(serial_dependencies.pri) + +LIBS *= -l$$qtLibraryTarget(Serial) diff --git a/ground/src/plugins/serialconnection/serial_dependencies.pri b/ground/src/plugins/serialconnection/serial_dependencies.pri new file mode 100644 index 000000000..0e063b82b --- /dev/null +++ b/ground/src/plugins/serialconnection/serial_dependencies.pri @@ -0,0 +1 @@ +include(../../plugins/coreplugin/coreplugin.pri) \ No newline at end of file diff --git a/ground/src/plugins/serialconnection/serial_global.h b/ground/src/plugins/serialconnection/serial_global.h new file mode 100644 index 000000000..7b566c585 --- /dev/null +++ b/ground/src/plugins/serialconnection/serial_global.h @@ -0,0 +1,39 @@ +/** + ****************************************************************************** + * + * @file serial_global.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup serial_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 SERIAL_GLOBAL_H +#define SERIAL_GLOBAL_H + +#include + +#if defined(SERIAL_LIBRARY) +# define SERIAL_EXPORT Q_DECL_EXPORT +#else +# define SERIAL_EXPORT Q_DECL_IMPORT +#endif + +#endif // SERIAL_GLOBAL_H diff --git a/ground/src/plugins/serialconnection/serialconnection.pro b/ground/src/plugins/serialconnection/serialconnection.pro new file mode 100644 index 000000000..7cfa0648f --- /dev/null +++ b/ground/src/plugins/serialconnection/serialconnection.pro @@ -0,0 +1,12 @@ +TEMPLATE = lib +TARGET = Serial +include(../../openpilotgcsplugin.pri) +include(serial_dependencies.pri) +INCLUDEPATH += ../../libs\qextserialport\src +HEADERS += serialplugin.h \ + serial_global.h +SOURCES += serialplugin.cpp +FORMS += +RESOURCES += +DEFINES += SERIAL_LIBRARY +OTHER_FILES += Serial.pluginspec diff --git a/ground/src/plugins/serialconnection/serialplugin.cpp b/ground/src/plugins/serialconnection/serialplugin.cpp new file mode 100644 index 000000000..2a0772c9f --- /dev/null +++ b/ground/src/plugins/serialconnection/serialplugin.cpp @@ -0,0 +1,122 @@ +/** + ****************************************************************************** + * + * @file serialplugin.cpp + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Register connection object for the core connection manager + * @see The GNU Public License (GPL) Version 3 + * @defgroup serial_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 "serialplugin.h" + +#include +#include + +#include +#include +#include +#include + +#include + +SerialConnection::SerialConnection() +{ + //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... + QMainWindow *mw = Core::ICore::instance()->mainWindow(); + QObject::connect(mw, SIGNAL(deviceChange()), + this, SLOT(onEnumerationChanged())); +} + +SerialConnection::~SerialConnection() +{ +} + +void SerialConnection::onEnumerationChanged() +{ + emit availableDevChanged(this); +} + +QStringList SerialConnection::availableDevices() +{ + QStringList list; + QList ports = QextSerialEnumerator::getPorts(); + foreach( QextPortInfo port, ports ) { + list.append(port.friendName); + } + + return list; +} + +QIODevice *SerialConnection::openDevice(const QString &deviceName) +{ + QList ports = QextSerialEnumerator::getPorts(); + foreach( QextPortInfo port, ports ) { + if(port.friendName == deviceName) + { + //we need to handle port settings here... + return new QextSerialPort(port.portName); + } + } + return NULL; +} + +void SerialConnection::closeDevice(const QString &deviceName) +{ + //nothing to do here +} + + +QString SerialConnection::connectionName() +{ + return QString("Serial port"); +} + +QString SerialConnection::shortName() +{ + return QString("Serial"); +} + + +SerialPlugin::SerialPlugin() +{ +} + +SerialPlugin::~SerialPlugin() +{ + +} + +void SerialPlugin::extensionsInitialized() +{ + addAutoReleasedObject(new SerialConnection); +} + +bool SerialPlugin::initialize(const QStringList &arguments, QString *errorString) +{ + Q_UNUSED(arguments); + Q_UNUSED(errorString); + + return true; +} + +Q_EXPORT_PLUGIN(SerialPlugin) diff --git a/ground/src/plugins/serialconnection/serialplugin.h b/ground/src/plugins/serialconnection/serialplugin.h new file mode 100644 index 000000000..c17aa1d23 --- /dev/null +++ b/ground/src/plugins/serialconnection/serialplugin.h @@ -0,0 +1,78 @@ +/** + ****************************************************************************** + * + * @file serialplugin.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup serial_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 SERIALPLUGIN_H +#define SERIALPLUGIN_H + +#include "serial_global.h" + +#include "coreplugin/iconnection.h" +#include + +class IConnection; +class QextSerialEnumerator; + +/** +* Define a connection via the IConnection interface +* Plugin will add a instance of this class to the pool, +* so the connection manager can use it. +*/ +class SERIAL_EXPORT SerialConnection + : public Core::IConnection +{ + Q_OBJECT +public: + SerialConnection(); + virtual ~SerialConnection(); + + virtual QStringList availableDevices(); + virtual QIODevice *openDevice(const QString &deviceName); + virtual void closeDevice(const QString &deviceName); + + virtual QString connectionName(); + virtual QString shortName(); + +protected slots: + void onEnumerationChanged(); +}; + + +class SERIAL_EXPORT SerialPlugin + : public ExtensionSystem::IPlugin +{ + Q_OBJECT + +public: + SerialPlugin(); + ~SerialPlugin(); + + virtual bool initialize(const QStringList &arguments, QString *error_message); + virtual void extensionsInitialized(); +}; + + +#endif // SERIALPLUGIN_H diff --git a/ground/src/plugins/uavtalk/uavtalkplugin.cpp b/ground/src/plugins/uavtalk/uavtalkplugin.cpp index 492954d8a..2c649215f 100644 --- a/ground/src/plugins/uavtalk/uavtalkplugin.cpp +++ b/ground/src/plugins/uavtalk/uavtalkplugin.cpp @@ -46,9 +46,7 @@ void UAVTalkPlugin::extensionsInitialized() ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance(); objMngr = pm->getObject(); - // TODO: Initialize serial port and USB libraries, get QIODevice from each - // ---Julien: Actually I changed a little bit from the initial plan, - // now you have to connect to signal emited by the connection manager + // 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 *)));