TCPLink.h 3.89 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
/*=====================================================================
 
 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/>.
 
 ======================================================================*/

24 25 26 27
/// @file
///     @brief TCP link type for SITL support
///
///     @author Don Gagne <don@thegagnes.com>
Don Gagne's avatar
Don Gagne committed
28 29 30 31 32 33 34 35 36 37

#ifndef TCPLINK_H
#define TCPLINK_H

#include <QString>
#include <QList>
#include <QMap>
#include <QMutex>
#include <QHostAddress>
#include <LinkInterface.h>
38
#include "QGCConfig.h"
Don Gagne's avatar
Don Gagne committed
39

40 41 42 43 44 45
// Even though QAbstractSocket::SocketError is used in a signal by Qt, Qt doesn't declare it as a meta type.
// This in turn causes debug output to be kicked out about not being able to queue the signal. We declare it
// as a meta type to silence that.
#include <QMetaType>
#include <QTcpSocket>

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

48 49
class TCPLinkUnitTest;

Don Gagne's avatar
Don Gagne committed
50 51 52 53
class TCPLink : public LinkInterface
{
    Q_OBJECT
    
54 55
    friend class TCPLinkUnitTest;
    
Don Gagne's avatar
Don Gagne committed
56
public:
Don Gagne's avatar
Don Gagne committed
57
    TCPLink(QHostAddress hostAddress = QHostAddress::LocalHost, quint16 socketPort = 5760);
Don Gagne's avatar
Don Gagne committed
58 59
    ~TCPLink();
    
60
    void setHostAddress(QHostAddress hostAddress);
Don Gagne's avatar
Don Gagne committed
61
    
62 63 64
    QHostAddress getHostAddress(void) const { return _hostAddress; }
    quint16 getPort(void) const { return _port; }
    QTcpSocket* getSocket(void) { return _socket; }
Don Gagne's avatar
Don Gagne committed
65
    
66 67
    void signalBytesWritten(void);

68 69 70 71 72
    // LinkInterface methods
    virtual int     getId(void) const;
    virtual QString getName(void) const;
    virtual bool    isConnected(void) const;
    virtual void    requestReset(void) {};
73 74 75 76 77

    // Extensive statistics for scientific purposes
    qint64 getConnectionSpeed() const;
    qint64 getCurrentInDataRate() const;
    qint64 getCurrentOutDataRate() const;
Don Gagne's avatar
Don Gagne committed
78
    
79 80 81 82 83
    // These are left unimplemented in order to cause linker errors which indicate incorrect usage of
    // connect/disconnect on link directly. All connect/disconnect calls should be made through LinkManager.
    bool connect(void);
    bool disconnect(void);
    
Don Gagne's avatar
Don Gagne committed
84
public slots:
85 86
    void setHostAddress(const QString& hostAddress);
    void setPort(int port);
Don Gagne's avatar
Don Gagne committed
87
    
88 89
    // From LinkInterface
    virtual void writeBytes(const char* data, qint64 length);
90 91 92
    
    void waitForBytesWritten(int msecs);
    void waitForReadyRead(int msecs);
93 94 95 96 97 98 99

protected slots:
    void _socketError(QAbstractSocket::SocketError socketError);

    // From LinkInterface
    virtual void readBytes(void);

Don Gagne's avatar
Don Gagne committed
100
protected:
101 102
    // From LinkInterface->QThread
    virtual void run(void);
103
    
Don Gagne's avatar
Don Gagne committed
104
private:
105 106 107 108
    // From LinkInterface
    virtual bool _connect(void);
    virtual bool _disconnect(void);

109 110
    void _resetName(void);
	bool _hardwareConnect(void);
Don Gagne's avatar
Don Gagne committed
111
#ifdef TCPLINK_READWRITE_DEBUG
112
    void _writeDebugBytes(const char *data, qint16 size);
Don Gagne's avatar
Don Gagne committed
113
#endif
114 115 116 117 118 119 120

    QString         _name;
    QHostAddress    _hostAddress;
    quint16         _port;
    int             _linkId;
    QTcpSocket*     _socket;
    bool            _socketIsConnected;
Don Gagne's avatar
Don Gagne committed
121
    
122 123 124 125 126 127 128 129
    quint64 _bitsSentTotal;
    quint64 _bitsSentCurrent;
    quint64 _bitsSentMax;
    quint64 _bitsReceivedTotal;
    quint64 _bitsReceivedCurrent;
    quint64 _bitsReceivedMax;
    quint64 _connectionStartTime;
    QMutex  _statisticsMutex;
Don Gagne's avatar
Don Gagne committed
130 131 132
};

#endif // TCPLINK_H