UDPLink.h 4.3 KB
Newer Older
1 2
/****************************************************************************
 *
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.
 *
 ****************************************************************************/
pixhawk's avatar
pixhawk committed
9

10
#pragma once
pixhawk's avatar
pixhawk committed
11 12 13 14 15 16

#include <QString>
#include <QList>
#include <QMap>
#include <QMutex>
#include <QUdpSocket>
17
#include <QMutex>
18 19
#include <QQueue>
#include <QByteArray>
20

21 22 23 24
#if defined(QGC_ZEROCONF_ENABLED)
#include <dns_sd.h>
#endif

25
#include "QGCConfig.h"
26 27 28 29
#include "LinkConfiguration.h"
#include "LinkInterface.h"

class LinkManager;
30

Gus Grubba's avatar
Gus Grubba committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
class UDPCLient {
public:
    UDPCLient(const QHostAddress& address_, quint16 port_)
        : address(address_)
        , port(port_)
    {}
    UDPCLient(const UDPCLient* other)
        : address(other->address)
        , port(other->port)
    {}
    QHostAddress    address;
    quint16         port;
};

45 46
class UDPConfiguration : public LinkConfiguration
{
Don Gagne's avatar
Don Gagne committed
47
    Q_OBJECT
48 49
public:

50 51 52
    Q_PROPERTY(quint16      localPort   READ localPort  WRITE setLocalPort  NOTIFY localPortChanged)
    Q_PROPERTY(QStringList  hostList    READ hostList                       NOTIFY  hostListChanged)

53 54
    UDPConfiguration(const QString& name);
    UDPConfiguration(UDPConfiguration* source);
Gus Grubba's avatar
Gus Grubba committed
55
    ~UDPConfiguration();
56 57 58

    quint16 localPort   () { return _localPort; }

59
    /// @param[in] host Host name in standard formatt, e.g. localhost:14551 or 192.168.1.1:14551
60
    Q_INVOKABLE void addHost (const QString host);
61

62 63
    /// @param[in] host Host name, e.g. localhost or 192.168.1.1
    /// @param[in] port Port number
Gus Grubba's avatar
Gus Grubba committed
64
    void addHost        (const QString& host, quint16 port);
65

66
    /// @param[in] host Host name, e.g. localhost or 192.168.1.1
67
    Q_INVOKABLE void removeHost  (const QString host);
68

69 70 71 72 73 74 75 76 77 78 79
    void                    setLocalPort(quint16 port);
    QStringList             hostList    (void)          { return _hostList; }
    const QList<UDPCLient*> targetHosts (void)          { return _targetHosts; }

    /// LinkConfiguration overrides
    LinkType    type                 (void) override                                        { return LinkConfiguration::TypeUdp; }
    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 "UdpSettings.qml"; }
    QString     settingsTitle        (void) override                                        { return tr("UDP Link Settings"); }
80

81
signals:
82 83
    void localPortChanged   (void);
    void hostListChanged    (void);
84 85

private:
86 87
    void _updateHostList    (void);
    void _clearTargetHosts  (void);
Gus Grubba's avatar
Gus Grubba committed
88
    void _copyFrom          (LinkConfiguration *source);
89

Gus Grubba's avatar
Gus Grubba committed
90
    QList<UDPCLient*>   _targetHosts;
91
    QStringList         _hostList;
Gus Grubba's avatar
Gus Grubba committed
92
    quint16             _localPort;
93
};
pixhawk's avatar
pixhawk committed
94 95 96 97

class UDPLink : public LinkInterface
{
    Q_OBJECT
98

99
    friend class UDPConfiguration;
100
    friend class LinkManager;
101

pixhawk's avatar
pixhawk committed
102
public:
103
    ~UDPLink();
pixhawk's avatar
pixhawk committed
104

105 106 107
    // LinkInterface overrides
    bool isConnected(void) const override;
    void disconnect (void) override;
108

109 110
    // QThread overrides
    void run(void) override;
pixhawk's avatar
pixhawk committed
111 112

public slots:
113
    void readBytes(void);
114

115
private slots:
116 117
    // LinkInterface overrides
    void _writeBytes(const QByteArray data) override;
pixhawk's avatar
pixhawk committed
118

oberion's avatar
oberion committed
119
private:
120
    // Links are only created/destroyed by LinkManager so constructor/destructor is not public
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    UDPLink(SharedLinkConfigurationPtr& config);

    // LinkInterface overrides
    bool _connect(void) override;

    bool _isIpLocal         (const QHostAddress& add);
    bool _hardwareConnect   (void);
    void _registerZeroconf  (uint16_t port, const std::string& regType);
    void _deregisterZeroconf(void);
    void _writeDataGram     (const QByteArray data, const UDPCLient* target);

    bool                _running;
    QUdpSocket*         _socket;
    UDPConfiguration*   _udpConfig;
    bool                _connectState;
    QList<UDPCLient*>   _sessionTargets;
    QMutex              _sessionTargetsMutex;
    QList<QHostAddress> _localAddresses;
Gus Grubba's avatar
Gus Grubba committed
139
#if defined(QGC_ZEROCONF_ENABLED)
140
    DNSServiceRef       _dnssServiceRef;
141
#endif
pixhawk's avatar
pixhawk committed
142
};