LinkInterface.h 3.27 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
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 56
    Q_INVOKABLE virtual QString getName     (void) const = 0;
    Q_INVOKABLE virtual void    disconnect  (void) = 0;
57

58 59
    virtual bool isConnected    (void) const = 0;
    virtual bool isLogReplay    (void) { return false; }
dogmaphobic's avatar
dogmaphobic committed
60

61 62 63 64 65 66
    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
67 68

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

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

80
    void _connectionRemoved(void);
81

82
    SharedLinkConfigurationPtr _config;
83

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

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

90
    void _setMavlinkChannel(uint8_t channel);
91

92 93 94 95 96
    bool    _mavlinkChannelSet          = false;
    uint8_t _mavlinkChannel;
    bool    _decodedFirstMavlinkPacket  = false;
    bool    _isPX4Flow                  = false;
    int     _vehicleReferenceCount      = 0;
97

98
    mutable QMutex _writeBytesMutex;
Don Gagne's avatar
Don Gagne committed
99

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

103 104
typedef std::shared_ptr<LinkInterface>  SharedLinkInterfacePtr;
typedef std::weak_ptr<LinkInterface>    WeakLinkInterfacePtr;
105