Commit 5652f391 authored by oberion's avatar oberion

Changed UDP link

parent ef95a56b
...@@ -145,6 +145,7 @@ QGCCore::QGCCore(int &argc, char* argv[]) : QApplication(argc, argv) ...@@ -145,6 +145,7 @@ QGCCore::QGCCore(int &argc, char* argv[]) : QApplication(argc, argv)
// to make sure that all components are initialized when the // to make sure that all components are initialized when the
// first messages arrive // first messages arrive
UDPLink* udpLink = new UDPLink(QHostAddress::Any, 14550); UDPLink* udpLink = new UDPLink(QHostAddress::Any, 14550);
MainWindow::instance()->addLink(udpLink);
// Listen on Multicast-Address 239.255.77.77, Port 14550 // Listen on Multicast-Address 239.255.77.77, Port 14550
//QHostAddress * multicast_udp = new QHostAddress("239.255.77.77"); //QHostAddress * multicast_udp = new QHostAddress("239.255.77.77");
//UDPLink* udpLink = new UDPLink(*multicast_udp, 14550); //UDPLink* udpLink = new UDPLink(*multicast_udp, 14550);
......
...@@ -40,19 +40,22 @@ This file is part of the QGROUNDCONTROL project ...@@ -40,19 +40,22 @@ This file is part of the QGROUNDCONTROL project
//#include <netinet/in.h> //#include <netinet/in.h>
UDPLink::UDPLink(QHostAddress host, quint16 port) UDPLink::UDPLink(QHostAddress host, quint16 port)
: socket(NULL)
{ {
this->host = host; this->host = host;
this->port = port; this->port = port;
this->connectState = false; this->connectState = false;
// Set unique ID and add link to the list of links // Set unique ID and add link to the list of links
this->id = getNextLinkId(); this->id = getNextLinkId();
this->name = tr("UDP Link (port:%1)").arg(14550); this->name = tr("UDP Link (port:%1)").arg(this->port);
LinkManager::instance()->add(this); emit nameChanged(this->name);
// LinkManager::instance()->add(this);
} }
UDPLink::~UDPLink() UDPLink::~UDPLink()
{ {
disconnect(); disconnect();
this->deleteLater();
} }
/** /**
...@@ -61,23 +64,39 @@ UDPLink::~UDPLink() ...@@ -61,23 +64,39 @@ UDPLink::~UDPLink()
**/ **/
void UDPLink::run() void UDPLink::run()
{ {
// forever exec();
// {
// QGC::SLEEP::msleep(5000);
// }
exec();
} }
void UDPLink::setAddress(QString address) void UDPLink::setAddress(QHostAddress host)
{ {
Q_UNUSED(address); bool reconnect(false);
if(this->isConnected())
{
disconnect();
reconnect = true;
}
this->host = host;
if(reconnect)
{
connect();
}
} }
void UDPLink::setPort(int port) void UDPLink::setPort(int port)
{ {
bool reconnect(false);
if(this->isConnected())
{
disconnect();
reconnect = true;
}
this->port = port; this->port = port;
disconnect(); this->name = tr("UDP Link (port:%1)").arg(this->port);
connect(); emit nameChanged(this->name);
if(reconnect)
{
connect();
}
} }
/** /**
...@@ -104,9 +123,11 @@ void UDPLink::addHost(const QString& host) ...@@ -104,9 +123,11 @@ void UDPLink::addHost(const QString& host)
} }
} }
hosts.append(address); hosts.append(address);
this->setAddress(address);
//qDebug() << "Address:" << address.toString(); //qDebug() << "Address:" << address.toString();
// Set port according to user input // Set port according to user input
ports.append(host.split(":").last().toInt()); ports.append(host.split(":").last().toInt());
this->setPort(host.split(":").last().toInt());
} }
} }
else else
...@@ -245,8 +266,14 @@ qint64 UDPLink::bytesAvailable() ...@@ -245,8 +266,14 @@ qint64 UDPLink::bytesAvailable()
**/ **/
bool UDPLink::disconnect() bool UDPLink::disconnect()
{ {
delete socket; this->quit();
socket = NULL; this->wait();
if(socket)
{
delete socket;
socket = NULL;
}
connectState = false; connectState = false;
...@@ -262,7 +289,19 @@ bool UDPLink::disconnect() ...@@ -262,7 +289,19 @@ bool UDPLink::disconnect()
**/ **/
bool UDPLink::connect() bool UDPLink::connect()
{ {
socket = new QUdpSocket(this); if(this->isRunning())
{
this->quit();
this->wait();
}
this->hardwareConnect();
start(HighPriority);
return true;
}
bool UDPLink::hardwareConnect(void)
{
socket = new QUdpSocket();
//Check if we are using a multicast-address //Check if we are using a multicast-address
// bool multicast = false; // bool multicast = false;
...@@ -309,11 +348,10 @@ bool UDPLink::connect() ...@@ -309,11 +348,10 @@ bool UDPLink::connect()
emit connected(); emit connected();
connectionStartTime = QGC::groundTimeUsecs()/1000; connectionStartTime = QGC::groundTimeUsecs()/1000;
} }
return connectState;
start(HighPriority);
return connectState;
} }
/** /**
* @brief Check if connection is active. * @brief Check if connection is active.
* *
......
...@@ -87,7 +87,7 @@ public: ...@@ -87,7 +87,7 @@ public:
int getId(); int getId();
public slots: public slots:
void setAddress(QString address); void setAddress(QHostAddress host);
void setPort(int port); void setPort(int port);
/** @brief Add a new host to broadcast messages to */ /** @brief Add a new host to broadcast messages to */
void addHost(const QString& host); void addHost(const QString& host);
...@@ -128,8 +128,11 @@ protected: ...@@ -128,8 +128,11 @@ protected:
void setName(QString name); void setName(QString name);
private:
bool hardwareConnect(void);
signals: signals:
// Signals are defined by LinkInterface //Signals are defined by LinkInterface
}; };
......
...@@ -232,14 +232,14 @@ void CommConfigurationWindow::setLinkType(int linktype) ...@@ -232,14 +232,14 @@ void CommConfigurationWindow::setLinkType(int linktype)
break; break;
} }
#endif // XBEELINK #endif // XBEELINK
/* case 1: case 1:
{ {
UDPLink *udp = new UDPLink(); UDPLink *udp = new UDPLink();
tmpLink = udp; tmpLink = udp;
MainWindow::instance()->addLink(tmpLink); MainWindow::instance()->addLink(tmpLink);
break; break;
} }
*/
#ifdef OPAL_RT #ifdef OPAL_RT
case 3: case 3:
{ {
...@@ -261,11 +261,13 @@ void CommConfigurationWindow::setLinkType(int linktype) ...@@ -261,11 +261,13 @@ void CommConfigurationWindow::setLinkType(int linktype)
} }
} }
// trigger new window // trigger new window
const int32_t& linkIndex(LinkManager::instance()->getLinks().indexOf(tmpLink));
const int32_t& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId());
QList<QAction*> actions = MainWindow::instance()->listLinkMenuActions(); QList<QAction*> actions = MainWindow::instance()->listLinkMenuActions();
foreach (QAction* act, actions) foreach (QAction* act, actions)
{ {
const int& linkIndex(LinkManager::instance()->getLinks().indexOf(tmpLink));
const int& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId());
if (act->data().toInt() == linkID) if (act->data().toInt() == linkID)
{ {
act->trigger(); act->trigger();
......
...@@ -1492,8 +1492,8 @@ void MainWindow::addLink() ...@@ -1492,8 +1492,8 @@ void MainWindow::addLink()
// Go fishing for this link's configuration window // Go fishing for this link's configuration window
QList<QAction*> actions = ui.menuNetwork->actions(); QList<QAction*> actions = ui.menuNetwork->actions();
const int& linkIndex(LinkManager::instance()->getLinks().indexOf(link)); const int32_t& linkIndex(LinkManager::instance()->getLinks().indexOf(link));
const int& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId()); const int32_t& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId());
foreach (QAction* act, actions) { foreach (QAction* act, actions) {
if (act->data().toInt() == linkID) { // LinkManager::instance()->getLinks().indexOf(link) if (act->data().toInt() == linkID) { // LinkManager::instance()->getLinks().indexOf(link)
...@@ -1517,8 +1517,8 @@ void MainWindow::addLink(LinkInterface *link) ...@@ -1517,8 +1517,8 @@ void MainWindow::addLink(LinkInterface *link)
bool found(false); bool found(false);
const int& linkIndex(LinkManager::instance()->getLinks().indexOf(link)); const int32_t& linkIndex(LinkManager::instance()->getLinks().indexOf(link));
const int& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId()); const int32_t& linkID(LinkManager::instance()->getLinks()[linkIndex]->getId());
foreach (QAction* act, actions) { foreach (QAction* act, actions) {
if (act->data().toInt() == linkID) { // LinkManager::instance()->getLinks().indexOf(link) if (act->data().toInt() == linkID) { // LinkManager::instance()->getLinks().indexOf(link)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment