TCPLink.h 3.35 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL 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.
 
 QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

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

#ifndef TCPLINK_H
#define TCPLINK_H

#include <QString>
#include <QList>
#include <QMap>
#include <QMutex>
#include <QHostAddress>
#include <QTcpSocket>
#include <LinkInterface.h>
#include <configuration.h>

Don Gagne's avatar
Don Gagne committed
43 44
//#define TCPLINK_READWRITE_DEBUG   // Use to debug data reads/writes

Don Gagne's avatar
Don Gagne committed
45 46 47 48 49
class TCPLink : public LinkInterface
{
    Q_OBJECT
    
public:
Don Gagne's avatar
Don Gagne committed
50
    TCPLink(QHostAddress hostAddress = QHostAddress::LocalHost, quint16 socketPort = 5760);
Don Gagne's avatar
Don Gagne committed
51 52 53 54 55 56 57 58 59
    ~TCPLink();
    
    void requestReset() { }
    
    bool isConnected() const;
    qint64 bytesAvailable();
    int getPort() const {
        return port;
    }
Don Gagne's avatar
Don Gagne committed
60 61 62
    QHostAddress getHostAddress() const {
        return host;
    }
Don Gagne's avatar
Don Gagne committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    
    QString getName() const;
    int getBaudRate() const;
    int getBaudRateType() const;
    int getFlowType() const;
    int getParityType() const;
    int getDataBitsType() const;
    int getStopBitsType() const;
    
    /* Extensive statistics for scientific purposes */
    qint64 getNominalDataRate() const;
    qint64 getTotalUpstream();
    qint64 getCurrentUpstream();
    qint64 getMaxUpstream();
    qint64 getTotalDownstream();
    qint64 getCurrentDownstream();
    qint64 getMaxDownstream();
    qint64 getBitsSent() const;
    qint64 getBitsReceived() const;
    
    void run();
    
    int getLinkQuality() const;
    bool isFullDuplex() const;
    int getId() const;
    
public slots:
    void setAddress(QHostAddress host);
    void setPort(int port);    
    void readBytes();
    void writeBytes(const char* data, qint64 length);
    bool connect();
    bool disconnect();
Don Gagne's avatar
Don Gagne committed
96 97 98
    void socketError(QAbstractSocket::SocketError socketError);
    void setAddress(const QString &text);

Don Gagne's avatar
Don Gagne committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    
protected:
    QString name;
    QHostAddress host;
    quint16 port;
    int id;
    QTcpSocket* socket;
    bool socketIsConnected;
    
    quint64 bitsSentTotal;
    quint64 bitsSentCurrent;
    quint64 bitsSentMax;
    quint64 bitsReceivedTotal;
    quint64 bitsReceivedCurrent;
    quint64 bitsReceivedMax;
    quint64 connectionStartTime;
    QMutex statisticsMutex;
    QMutex dataMutex;
    
    void setName(QString name);
    
private:
	bool hardwareConnect(void);
Don Gagne's avatar
Don Gagne committed
122 123 124
#ifdef TCPLINK_READWRITE_DEBUG
    void writeDebugBytes(const char *data, qint16 size);
#endif
Don Gagne's avatar
Don Gagne committed
125 126 127 128 129 130 131
    
signals:
    //Signals are defined by LinkInterface
    
};

#endif // TCPLINK_H