LinkInterface.h 3.17 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.
 *
 ****************************************************************************/
9

10
#pragma once
pixhawk's avatar
pixhawk committed
11 12

#include <QThread>
13 14 15
#include <QDateTime>
#include <QMutex>
#include <QMutexLocker>
Lorenz Meier's avatar
Lorenz Meier committed
16
#include <QMetaType>
17
#include <QSharedPointer>
18
#include <QDebug>
19
#include <QTimer>
pixhawk's avatar
pixhawk committed
20

21 22
#include <memory>

Don Gagne's avatar
Don Gagne committed
23
#include "QGCMAVLink.h"
24
#include "LinkConfiguration.h"
25
#include "MavlinkMessagesTimer.h"
Don Gagne's avatar
Don Gagne committed
26

27 28
class LinkManager;

pixhawk's avatar
pixhawk committed
29
/**
30 31
* @brief The link interface defines the interface for all links used to communicate
* with the ground station application.
32
**/
33 34
class LinkInterface : public QThread
{
35
    Q_OBJECT
36

37
    friend class LinkManager;
38

39
public:    
40
    virtual ~LinkInterface();
41

42
    Q_PROPERTY(bool isPX4Flow   READ isPX4Flow  CONSTANT)
43
    Q_PROPERTY(bool isMockLink  READ isMockLink CONSTANT)
Don Gagne's avatar
Don Gagne committed
44

45 46
    // Property accessors
    bool isPX4Flow(void) const { return _isPX4Flow; }
47 48 49 50 51
#ifdef UNITTEST_BUILD
    bool isMockLink(void);
#else
    bool isMockLink(void) { return false; }
#endif
52

53
    SharedLinkConfigurationPtr linkConfiguration(void) { return _config; }
54

55
    Q_INVOKABLE virtual void    disconnect  (void) = 0;
56

57 58
    virtual bool isConnected    (void) const = 0;
    virtual bool isLogReplay    (void) { return false; }
59

60 61 62 63 64 65
    uint8_t mavlinkChannel              (void) const;
    bool    decodedFirstMavlinkPacket   (void) const { return _decodedFirstMavlinkPacket; }
    bool    setDecodedFirstMavlinkPacket(bool decodedFirstMavlinkPacket) { return _decodedFirstMavlinkPacket = decodedFirstMavlinkPacket; }
    void    writeBytesThreadSafe        (const char *bytes, int length);
    void    addVehicleReference         (void);
    void    removeVehicleReference      (void);
pixhawk's avatar
pixhawk committed
66 67

signals:
68 69 70 71 72
    void bytesReceived      (LinkInterface* link, QByteArray data);
    void bytesSent          (LinkInterface* link, QByteArray data);
    void connected          (void);
    void disconnected       (void);
    void communicationError (const QString& title, const QString& error);
73

pixhawk's avatar
pixhawk committed
74
protected:
75
    // Links are only created by LinkManager so constructor is not public
76
    LinkInterface(SharedLinkConfigurationPtr& config, bool isPX4Flow = false);
77

78
    void _connectionRemoved(void);
79

80
    SharedLinkConfigurationPtr _config;
81

82
private:
83
    // connect is private since all links should be created through LinkManager::createConnectedLink calls
84
    virtual bool _connect(void) = 0;
85

86
    virtual void _writeBytes(const QByteArray) = 0; // Not thread safe, only writeBytesThreadSafe is thread safe
87

88
    void _setMavlinkChannel(uint8_t channel);
89

90 91 92 93 94
    bool    _mavlinkChannelSet          = false;
    uint8_t _mavlinkChannel;
    bool    _decodedFirstMavlinkPacket  = false;
    bool    _isPX4Flow                  = false;
    int     _vehicleReferenceCount      = 0;
Don Gagne's avatar
Don Gagne committed
95

96
    mutable QMutex _writeBytesMutex;
97

98
    QMap<int /* vehicle id */, MavlinkMessagesTimer*> _mavlinkMessagesTimers;
pixhawk's avatar
pixhawk committed
99 100
};

101 102
typedef std::shared_ptr<LinkInterface>  SharedLinkInterfacePtr;
typedef std::weak_ptr<LinkInterface>    WeakLinkInterfacePtr;
103