2010-06-23 06:11:01 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
2010-06-28 02:28:30 +02:00
|
|
|
* @file IPconnectionplugin.cpp
|
2010-06-23 06:11:01 +02:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2010-07-16 12:33:27 +02:00
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
2010-06-23 06:11:01 +02:00
|
|
|
* @{
|
2010-07-16 12:33:27 +02:00
|
|
|
* @addtogroup IPConnPlugin IP Telemetry Plugin
|
|
|
|
* @{
|
|
|
|
* @brief IP Connection Plugin impliment telemetry over TCP/IP and UDP/IP
|
2010-06-23 06:11:01 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
//The core of this plugin has been directly copied from the serial plugin and converted to work over a TCP link instead of a direct serial link
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
#include "ipconnectionplugin.h"
|
2010-06-23 06:11:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
#include <coreplugin/icore.h>
|
2010-08-20 18:23:14 +02:00
|
|
|
#include "ipconnection_internal.h"
|
2010-06-23 06:11:01 +02:00
|
|
|
|
|
|
|
#include <QtCore/QtPlugin>
|
|
|
|
#include <QtGui/QMainWindow>
|
2010-06-24 06:58:19 +02:00
|
|
|
#include <QtGui/QMessageBox>
|
2010-06-27 13:04:24 +02:00
|
|
|
#include <QtNetwork/QAbstractSocket>
|
2010-06-23 06:11:01 +02:00
|
|
|
#include <QtNetwork/QTcpSocket>
|
2010-06-27 13:04:24 +02:00
|
|
|
#include <QtNetwork/QUdpSocket>
|
2010-08-20 18:23:14 +02:00
|
|
|
#include <QWaitCondition>
|
2010-10-17 00:29:19 +02:00
|
|
|
#include <QMutex>
|
2010-08-20 18:23:14 +02:00
|
|
|
#include <coreplugin/threadmanager.h>
|
2010-06-23 06:11:01 +02:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2010-08-20 18:23:14 +02:00
|
|
|
//Communication between IPconnectionConnection::OpenDevice() and IPConnection::onOpenDevice()
|
|
|
|
QString errorMsg;
|
|
|
|
QWaitCondition openDeviceWait;
|
2010-10-17 00:29:19 +02:00
|
|
|
QWaitCondition closeDeviceWait;
|
|
|
|
//QReadWriteLock dummyLock;
|
|
|
|
QMutex ipConMutex;
|
2010-08-20 18:23:14 +02:00
|
|
|
QAbstractSocket *ret;
|
|
|
|
|
|
|
|
IPConnection::IPConnection(IPconnectionConnection *connection) : QObject()
|
|
|
|
{
|
|
|
|
moveToThread(Core::ICore::instance()->threadManager()->getRealTimeThread());
|
|
|
|
|
|
|
|
QObject::connect(connection, SIGNAL(CreateSocket(QString,int,bool)),
|
|
|
|
this, SLOT(onOpenDevice(QString,int,bool)));
|
|
|
|
QObject::connect(connection, SIGNAL(CloseSocket(QAbstractSocket*)),
|
|
|
|
this, SLOT(onCloseDevice(QAbstractSocket*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*IPConnection::~IPConnection()
|
|
|
|
{
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
void IPConnection::onOpenDevice(QString HostName, int Port, bool UseTCP)
|
|
|
|
{
|
|
|
|
QAbstractSocket *ipSocket;
|
|
|
|
const int Timeout = 5 * 1000;
|
|
|
|
int state;
|
|
|
|
|
2010-10-17 00:29:19 +02:00
|
|
|
ipConMutex.lock();
|
2010-08-20 18:23:14 +02:00
|
|
|
if (UseTCP) {
|
|
|
|
ipSocket = new QTcpSocket(this);
|
|
|
|
} else {
|
|
|
|
ipSocket = new QUdpSocket(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//do sanity check on hostname and port...
|
|
|
|
if((HostName.length()==0)||(Port<1)){
|
|
|
|
errorMsg = "Please configure Host and Port options before opening the connection";
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//try to connect...
|
|
|
|
ipSocket->connectToHost(HostName, Port);
|
|
|
|
|
|
|
|
//in blocking mode so we wait for the connection to succeed
|
|
|
|
if (ipSocket->waitForConnected(Timeout)) {
|
|
|
|
ret = ipSocket;
|
|
|
|
openDeviceWait.wakeAll();
|
2010-10-17 00:29:19 +02:00
|
|
|
ipConMutex.unlock();
|
2010-08-20 18:23:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//tell user something went wrong
|
|
|
|
errorMsg = ipSocket->errorString ();
|
|
|
|
}
|
|
|
|
/* 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.*/
|
|
|
|
ret = NULL;
|
|
|
|
openDeviceWait.wakeAll();
|
2010-10-17 00:29:19 +02:00
|
|
|
ipConMutex.unlock();
|
2010-08-20 18:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IPConnection::onCloseDevice(QAbstractSocket *ipSocket)
|
|
|
|
{
|
2010-10-17 00:29:19 +02:00
|
|
|
ipConMutex.lock();
|
2010-08-20 18:23:14 +02:00
|
|
|
ipSocket->close ();
|
|
|
|
delete(ipSocket);
|
2010-10-17 00:29:19 +02:00
|
|
|
closeDeviceWait.wakeAll();
|
|
|
|
ipConMutex.unlock();
|
2010-08-20 18:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IPConnection * connection = 0;
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
IPconnectionConnection::IPconnectionConnection()
|
2010-06-24 06:58:19 +02:00
|
|
|
{
|
2010-07-10 03:24:32 +02:00
|
|
|
ipSocket = NULL;
|
2010-06-27 06:10:01 +02:00
|
|
|
//create all our objects
|
2010-06-28 02:28:30 +02:00
|
|
|
m_config = new IPconnectionConfiguration("IP Network Telemetry", NULL, this);
|
2010-06-24 06:58:19 +02:00
|
|
|
m_config->restoresettings();
|
2010-06-23 06:11:01 +02:00
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
m_optionspage = new IPconnectionOptionsPage(m_config,this);
|
2010-06-24 06:58:19 +02:00
|
|
|
|
2010-08-20 18:23:14 +02:00
|
|
|
if(!connection)
|
|
|
|
connection = new IPConnection(this);
|
|
|
|
|
2010-06-24 06:58:19 +02:00
|
|
|
//just signal whenever we have a device event...
|
2010-06-23 06:11:01 +02:00
|
|
|
QMainWindow *mw = Core::ICore::instance()->mainWindow();
|
|
|
|
QObject::connect(mw, SIGNAL(deviceChange()),
|
|
|
|
this, SLOT(onEnumerationChanged()));
|
2010-06-24 06:58:19 +02:00
|
|
|
QObject::connect(m_optionspage, SIGNAL(availableDevChanged()),
|
|
|
|
this, SLOT(onEnumerationChanged()));
|
2010-06-23 06:11:01 +02:00
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
IPconnectionConnection::~IPconnectionConnection()
|
2010-07-10 03:24:32 +02:00
|
|
|
{//clean up out resources...
|
|
|
|
if (ipSocket){
|
|
|
|
ipSocket->close ();
|
|
|
|
delete(ipSocket);
|
|
|
|
}
|
2010-08-20 18:23:14 +02:00
|
|
|
if(connection)
|
|
|
|
{
|
|
|
|
delete connection;
|
|
|
|
connection = NULL;
|
|
|
|
}
|
2010-06-23 06:11:01 +02:00
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
void IPconnectionConnection::onEnumerationChanged()
|
2010-06-23 06:11:01 +02:00
|
|
|
{//no change from serial plugin
|
|
|
|
emit availableDevChanged(this);
|
|
|
|
}
|
|
|
|
|
2010-06-24 06:58:19 +02:00
|
|
|
|
2010-06-23 06:11:01 +02:00
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
QStringList IPconnectionConnection::availableDevices()
|
2010-06-23 06:11:01 +02:00
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
|
2010-06-27 06:10:01 +02:00
|
|
|
//we only have one "device" as defined by the configuration m_config
|
2010-06-24 06:58:19 +02:00
|
|
|
list.append((const QString )m_config->HostName());
|
2010-06-23 06:11:01 +02:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
QIODevice *IPconnectionConnection::openDevice(const QString &deviceName)
|
2010-06-23 06:11:01 +02:00
|
|
|
{
|
2010-08-20 18:23:14 +02:00
|
|
|
QString HostName;
|
|
|
|
int Port;
|
|
|
|
bool UseTCP;
|
|
|
|
QMessageBox msgBox;
|
|
|
|
|
|
|
|
//get the configuration info
|
|
|
|
HostName = m_config->HostName();
|
|
|
|
Port = m_config->Port();
|
|
|
|
UseTCP = m_config->UseTCP();
|
|
|
|
|
|
|
|
if (ipSocket){
|
|
|
|
//Andrew: close any existing socket... this should never occur
|
2010-10-17 00:29:19 +02:00
|
|
|
ipConMutex.lock();
|
2010-08-20 18:23:14 +02:00
|
|
|
emit CloseSocket(ipSocket);
|
2010-10-17 00:29:19 +02:00
|
|
|
closeDeviceWait.wait(&ipConMutex);
|
|
|
|
ipConMutex.unlock();
|
2010-08-20 18:23:14 +02:00
|
|
|
ipSocket = NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-17 00:29:19 +02:00
|
|
|
ipConMutex.lock();
|
2010-08-20 18:23:14 +02:00
|
|
|
emit CreateSocket(HostName, Port, UseTCP);
|
2010-10-17 00:29:19 +02:00
|
|
|
openDeviceWait.wait(&ipConMutex);
|
|
|
|
ipConMutex.unlock();
|
2010-08-20 18:23:14 +02:00
|
|
|
ipSocket = ret;
|
|
|
|
if(ipSocket == NULL)
|
|
|
|
{
|
|
|
|
msgBox.setText((const QString )errorMsg);
|
|
|
|
msgBox.exec();
|
|
|
|
}
|
2010-07-10 03:24:32 +02:00
|
|
|
return ipSocket;
|
2010-06-23 06:11:01 +02:00
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
void IPconnectionConnection::closeDevice(const QString &deviceName)
|
2010-06-24 06:58:19 +02:00
|
|
|
{
|
2010-07-10 03:24:32 +02:00
|
|
|
if (ipSocket){
|
2010-10-17 00:29:19 +02:00
|
|
|
ipConMutex.lock();
|
2010-08-20 18:23:14 +02:00
|
|
|
emit CloseSocket(ipSocket);
|
2010-10-17 00:29:19 +02:00
|
|
|
closeDeviceWait.wait(&ipConMutex);
|
|
|
|
ipConMutex.unlock();
|
2010-07-10 03:24:32 +02:00
|
|
|
ipSocket = NULL;
|
|
|
|
}
|
2010-06-23 06:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
QString IPconnectionConnection::connectionName()
|
2010-06-23 06:11:01 +02:00
|
|
|
{//updated from serial plugin
|
2010-06-27 13:04:24 +02:00
|
|
|
return QString("Network telemetry port");
|
2010-06-23 06:11:01 +02:00
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
QString IPconnectionConnection::shortName()
|
2010-06-23 06:11:01 +02:00
|
|
|
{//updated from serial plugin
|
2010-06-27 13:04:24 +02:00
|
|
|
if (m_config->UseTCP()) {
|
|
|
|
return QString("TCP");
|
|
|
|
} else {
|
|
|
|
return QString("UDP");
|
|
|
|
}
|
2010-06-23 06:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
IPconnectionPlugin::IPconnectionPlugin()
|
2010-06-23 06:11:01 +02:00
|
|
|
{//no change from serial plugin
|
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
IPconnectionPlugin::~IPconnectionPlugin()
|
2010-06-27 06:10:01 +02:00
|
|
|
{//manually remove the options page object
|
|
|
|
removeObject(m_connection->Optionspage());
|
2010-06-23 06:11:01 +02:00
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
void IPconnectionPlugin::extensionsInitialized()
|
2010-06-27 06:10:01 +02:00
|
|
|
{
|
2010-06-23 06:11:01 +02:00
|
|
|
addAutoReleasedObject(m_connection);
|
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
bool IPconnectionPlugin::initialize(const QStringList &arguments, QString *errorString)
|
2010-06-27 06:10:01 +02:00
|
|
|
{
|
2010-06-23 06:11:01 +02:00
|
|
|
Q_UNUSED(arguments);
|
|
|
|
Q_UNUSED(errorString);
|
2010-06-28 02:28:30 +02:00
|
|
|
m_connection = new IPconnectionConnection();
|
2010-06-27 06:10:01 +02:00
|
|
|
//must manage this registration of child object ourselves
|
|
|
|
//if we use an autorelease here it causes the GCS to crash
|
|
|
|
//as it is deleting objects as the app closes...
|
|
|
|
addObject(m_connection->Optionspage());
|
2010-06-23 06:11:01 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-28 02:28:30 +02:00
|
|
|
Q_EXPORT_PLUGIN(IPconnectionPlugin)
|