Skip to content
UDPLink.h 3.17 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/*=====================================================================

pixhawk's avatar
pixhawk committed
QGroundControl Open Source Ground Control Station
pixhawk's avatar
pixhawk committed

pixhawk's avatar
pixhawk committed
(c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed

pixhawk's avatar
pixhawk committed
This file is part of the QGROUNDCONTROL project
pixhawk's avatar
pixhawk committed

pixhawk's avatar
pixhawk committed
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
    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.

pixhawk's avatar
pixhawk committed
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
    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
pixhawk's avatar
pixhawk committed
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed

======================================================================*/

/**
 * @file
 *   @brief UDP connection (server) for unmanned vehicles
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#ifndef UDPLINK_H
#define UDPLINK_H

#include <QString>
#include <QList>
#include <QMap>
#include <QMutex>
#include <QUdpSocket>
#include <LinkInterface.h>
pixhawk's avatar
pixhawk committed

class UDPLink : public LinkInterface
{
    Q_OBJECT
    //Q_INTERFACES(UDPLinkInterface:LinkInterface)

public:
    UDPLink(QHostAddress host = QHostAddress::Any, quint16 port = 14550);
pixhawk's avatar
pixhawk committed
    //UDPLink(QHostAddress host = "239.255.76.67", quint16 port = 7667);
pixhawk's avatar
pixhawk committed
    ~UDPLink();

pixhawk's avatar
pixhawk committed
    qint64 bytesAvailable();
    int getPort() const {
        return port;
    }
pixhawk's avatar
pixhawk committed

    /**
     * @brief The human readable port name
     */
    QString getName() const;
    int getBaudRate() const;
    int getBaudRateType() const;
    int getFlowType() const;
    int getParityType() const;
    int getDataBitsType() const;
    int getStopBitsType() const;
    QList<QHostAddress> getHosts() const {
        return hosts;
    }
pixhawk's avatar
pixhawk committed

    // Extensive statistics for scientific purposes
    qint64 getConnectionSpeed() const;
    qint64 getCurrentInDataRate() const;
    qint64 getCurrentOutDataRate() const;
pixhawk's avatar
pixhawk committed

    void run();

pixhawk's avatar
pixhawk committed

public slots:
oberion's avatar
oberion committed
    void setAddress(QHostAddress host);
    void setPort(int port);
    /** @brief Add a new host to broadcast messages to */
    void addHost(const QString& host);
    /** @brief Remove a host from broadcasting messages to */
    void removeHost(const QString& host);
pixhawk's avatar
pixhawk committed
    //    void readPendingDatagrams();

pixhawk's avatar
pixhawk committed
    /**
     * @brief Write a number of bytes to the interface.
     *
     * @param data Pointer to the data byte array
     * @param size The size of the bytes array
     **/
    void writeBytes(const char* data, qint64 length);
    bool connect();
    bool disconnect();

protected:
    QString name;
    QHostAddress host;
    quint16 port;
    int id;
    QUdpSocket* socket;
    bool connectState;
    QList<QHostAddress> hosts;
    QList<quint16> ports;
pixhawk's avatar
pixhawk committed

    QMutex dataMutex;

    void setName(QString name);

oberion's avatar
oberion committed
private:
	bool hardwareConnect(void);

pixhawk's avatar
pixhawk committed
signals:
oberion's avatar
oberion committed
    //Signals are defined by LinkInterface
pixhawk's avatar
pixhawk committed

};

#endif // UDPLINK_H