TCPLink.h 3.82 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9

10
#pragma once
Don Gagne's avatar
Don Gagne committed
11 12 13 14 15 16 17

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

20 21 22 23 24 25
// 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
26 27
//#define TCPLINK_READWRITE_DEBUG   // Use to debug data reads/writes

28
class TCPLinkTest;
29
class LinkManager;
30

31 32 33 34
#define QGC_TCP_PORT 5760

class TCPConfiguration : public LinkConfiguration
{
Don Gagne's avatar
Don Gagne committed
35 36
    Q_OBJECT

37 38
public:

39 40
    Q_PROPERTY(quint16 port READ port WRITE setPort NOTIFY portChanged)
    Q_PROPERTY(QString host READ host WRITE setHost NOTIFY hostChanged)
41

42 43 44
    TCPConfiguration(const QString& name);
    TCPConfiguration(TCPConfiguration* source);

45 46 47 48 49 50 51 52 53 54 55 56 57 58
    quint16             port        (void)                          { return _port; }
    const QHostAddress& address     (void)                          { return _address; }
    const QString       host        (void)                          { return _address.toString(); }
    void                setPort     (quint16 port);
    void                setAddress  (const QHostAddress& address);
    void                setHost     (const QString host);

    //LinkConfiguration overrides
    LinkType    type                (void) override                                         { return LinkConfiguration::TypeTcp; }
    void        copyFrom            (LinkConfiguration* source) override;
    void        loadSettings        (QSettings& settings, const QString& root) override;
    void        saveSettings        (QSettings& settings, const QString& root) override;
    QString     settingsURL         (void) override                                         { return "TcpSettings.qml"; }
    QString     settingsTitle       (void) override                                         { return tr("TCP Link Settings"); }
59

60
signals:
61 62
    void portChanged(void);
    void hostChanged(void);
63

64
private:
65 66
    QHostAddress    _address;
    quint16         _port;
67 68
};

Don Gagne's avatar
Don Gagne committed
69 70 71
class TCPLink : public LinkInterface
{
    Q_OBJECT
72

73
    friend class TCPLinkTest;
74
    friend class TCPConfiguration;
75
    friend class LinkManager;
76

77
public:
78
    ~TCPLink();
79

80 81
    QTcpSocket* getSocket           (void) { return _socket; }
    void        signalBytesWritten  (void);
82

83
    // LinkInterface overrides
Don Gagne's avatar
Don Gagne committed
84 85
    bool isConnected(void) const override;
    void disconnect (void) override;
86 87

public slots:
88
    void waitForBytesWritten(int msecs);
89
    void waitForReadyRead   (int msecs);
90 91

protected slots:
92 93
    void _socketError   (QAbstractSocket::SocketError socketError);
    void readBytes      (void);
94

Don Gagne's avatar
Don Gagne committed
95
protected:
96 97
    // QThread overrides
    void run(void) override;
98

99 100 101
private slots:
    // LinkInterface overrides
    void _writeBytes(const QByteArray data) override;
102

103 104
private:
    TCPLink(SharedLinkConfigurationPtr& config);
105

106 107
    // LinkInterface overrides
    bool _connect(void) override;
108

109
    bool _hardwareConnect   (void);
Don Gagne's avatar
Don Gagne committed
110
#ifdef TCPLINK_READWRITE_DEBUG
111
    void _writeDebugBytes   (const QByteArray data);
Don Gagne's avatar
Don Gagne committed
112
#endif
113

114
    TCPConfiguration* _tcpConfig;
115 116
    QTcpSocket*       _socket;
    bool              _socketIsConnected;
117

118 119 120 121 122 123 124 125
    quint64 _bitsSentTotal;
    quint64 _bitsSentCurrent;
    quint64 _bitsSentMax;
    quint64 _bitsReceivedTotal;
    quint64 _bitsReceivedCurrent;
    quint64 _bitsReceivedMax;
    quint64 _connectionStartTime;
    QMutex  _statisticsMutex;
Don Gagne's avatar
Don Gagne committed
126 127
};