LinkManager.h 7.87 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 <QList>
13
#include <QMultiMap>
14
#include <QMutex>
15

16
#include "LinkConfiguration.h"
17
#include "LinkInterface.h"
Don Gagne's avatar
Don Gagne committed
18
#include "QGCLoggingCategory.h"
19
#include "QGCToolbox.h"
Don Gagne's avatar
Don Gagne committed
20
#include "MAVLinkProtocol.h"
Gus Grubba's avatar
Gus Grubba committed
21
#if !defined(__mobile__)
Don Gagne's avatar
Don Gagne committed
22
#include "LogReplayLink.h"
23
#include "UdpIODevice.h"
dogmaphobic's avatar
dogmaphobic committed
24
#endif
Don Gagne's avatar
Don Gagne committed
25
#include "QmlObjectListModel.h"
26

Gus Grubba's avatar
Gus Grubba committed
27
#ifndef NO_SERIAL_LINK
Don Gagne's avatar
Don Gagne committed
28
    #include "SerialLink.h"
dogmaphobic's avatar
dogmaphobic committed
29
#endif
30

Don Gagne's avatar
Don Gagne committed
31
Q_DECLARE_LOGGING_CATEGORY(LinkManagerLog)
Don Gagne's avatar
Don Gagne committed
32
Q_DECLARE_LOGGING_CATEGORY(LinkManagerVerboseLog)
Don Gagne's avatar
Don Gagne committed
33

34
class QGCApplication;
35 36
class UDPConfiguration;
class AutoConnectSettings;
37
class LogReplayLink;
38

39
/// @brief Manage communication links
40 41 42 43 44
///
/// The Link Manager organizes the physical Links. It can manage arbitrary
/// links and takes care of connecting them as well assigning the correct
/// protocol instance to transport the link data into the application.

45
class LinkManager : public QGCTool
46
{
pixhawk's avatar
pixhawk committed
47
    Q_OBJECT
48

Don Gagne's avatar
Don Gagne committed
49
public:
50
    LinkManager(QGCApplication* app, QGCToolbox* toolbox);
51
    ~LinkManager();
52

53 54 55 56 57 58
    Q_PROPERTY(bool                 isBluetoothAvailable    READ isBluetoothAvailable   CONSTANT)
    Q_PROPERTY(QmlObjectListModel*  linkConfigurations      READ _qmlLinkConfigurations CONSTANT)
    Q_PROPERTY(QStringList          linkTypeStrings         READ linkTypeStrings        CONSTANT)
    Q_PROPERTY(QStringList          serialBaudRates         READ serialBaudRates        CONSTANT)
    Q_PROPERTY(QStringList          serialPortStrings       READ serialPortStrings      NOTIFY commPortStringsChanged)
    Q_PROPERTY(QStringList          serialPorts             READ serialPorts            NOTIFY commPortsChanged)
59

60
    /// Create/Edit Link Configuration
61 62 63 64 65 66
    Q_INVOKABLE LinkConfiguration*  createConfiguration         (int type, const QString& name);
    Q_INVOKABLE LinkConfiguration*  startConfigurationEditing   (LinkConfiguration* config);
    Q_INVOKABLE void                cancelConfigurationEditing  (LinkConfiguration* config) { delete config; }
    Q_INVOKABLE bool                endConfigurationEditing     (LinkConfiguration* config, LinkConfiguration* editedConfig);
    Q_INVOKABLE bool                endCreateConfiguration      (LinkConfiguration* config);
    Q_INVOKABLE void                removeConfiguration         (LinkConfiguration* config);
Don Gagne's avatar
Don Gagne committed
67

68 69 70 71 72
    // Called to signal app shutdown. Disconnects all links while turning off auto-connect.
    Q_INVOKABLE void shutdown(void);

    Q_INVOKABLE LogReplayLink* startLogReplay(const QString& logFile);

Don Gagne's avatar
Don Gagne committed
73 74
    // Property accessors

dogmaphobic's avatar
dogmaphobic committed
75
    bool isBluetoothAvailable       (void);
Don Gagne's avatar
Don Gagne committed
76

77 78 79 80 81
    QList<SharedLinkInterfacePtr>   links               (void) { return _rgLinks; }
    QStringList                     linkTypeStrings     (void) const;
    QStringList                     serialBaudRates     (void);
    QStringList                     serialPortStrings   (void);
    QStringList                     serialPorts         (void);
82 83 84 85 86 87 88

    void loadLinkConfigurationList();
    void saveLinkConfigurationList();

    /// Suspend automatic confguration updates (during link maintenance for instance)
    void suspendConfigurationUpdates(bool suspend);

89
    /// Sets the flag to suspend the all new connections
90 91
    ///     @param reason User visible reason to suspend connections
    void setConnectionsSuspended(QString reason);
92

93
    /// Sets the flag to allow new connections to be made
94
    void setConnectionsAllowed(void) { _connectionsSuspended = false; }
95

96
    /// Creates, connects (and adds) a link  based on the given configuration instance.
97
    bool createConnectedLink(SharedLinkConfigurationPtr& config, bool isPX4Flow = false);
Don Gagne's avatar
Don Gagne committed
98 99 100

    // This should only be used by Qml code
    Q_INVOKABLE void createConnectedLink(LinkConfiguration* config);
101

102 103
    /// Returns pointer to the mavlink forwarding link, or nullptr if it does not exist
    SharedLinkInterfacePtr mavlinkForwardingLink();
104

Don Gagne's avatar
Don Gagne committed
105
    void disconnectAll(void);
106

Don Gagne's avatar
Don Gagne committed
107 108 109 110 111
#ifdef QT_DEBUG
    // Only used by unit test tp restart after a shutdown
    void restart(void) { setConnectionsAllowed(); }
#endif

112 113 114
    // Override from QGCTool
    virtual void setToolbox(QGCToolbox *toolbox);

115 116 117 118 119
    /// @return This mavlink channel is never assigned to a vehicle.
    uint8_t reservedMavlinkChannel(void) { return 0; }

    /// If you are going to hold a reference to a LinkInterface* in your object you must reference count it
    /// by using this method to get access to the shared pointer.
120
    SharedLinkInterfacePtr sharedLinkInterfacePointerForLink(LinkInterface* link);
121 122 123

    bool containsLink(LinkInterface* link);

124
    SharedLinkConfigurationPtr addConfiguration(LinkConfiguration* config);
125

126 127
    void startAutoConnectedLinks(void);

128 129 130 131
    /// Reserves a mavlink channel for use
    /// @return Mavlink channel index, 0 for no channels available
    int _reserveMavlinkChannel(void);

132
    static const char*  settingsGroup;
Don Gagne's avatar
Don Gagne committed
133

134
signals:
135
    void commPortStringsChanged();
136
    void commPortsChanged();
137

138
private slots:
139
    void _linkDisconnected  (void);
140

141
private:
142 143 144 145 146 147 148 149 150 151
    QmlObjectListModel* _qmlLinkConfigurations      (void) { return &_qmlConfigurations; }
    bool                _connectionsSuspendedMsg    (void);
    void                _updateAutoConnectLinks     (void);
    void                _updateSerialPorts          (void);
    void                _fixUnnamed                 (LinkConfiguration* config);
    void                _removeConfiguration        (LinkConfiguration* config);
    void                _addUDPAutoConnectLink      (void);
    void                _addMAVLinkForwardingLink   (void);
    void                _freeMavlinkChannel         (int channel);
    bool                _isSerialPortConnected      (void);
Don Gagne's avatar
Don Gagne committed
152

Gus Grubba's avatar
Gus Grubba committed
153
#ifndef NO_SERIAL_LINK
154
    bool                _portAlreadyConnected       (const QString& portName);
dogmaphobic's avatar
dogmaphobic committed
155
#endif
156

157 158 159 160 161 162
    bool                                _configUpdateSuspended;                     ///< true: stop updating configuration list
    bool                                _configurationsLoaded;                      ///< true: Link configurations have been loaded
    bool                                _connectionsSuspended;                      ///< true: all new connections should not be allowed
    QString                             _connectionsSuspendedReason;                ///< User visible reason for suspension
    QTimer                              _portListTimer;
    uint32_t                            _mavlinkChannelsUsedBitMask;
163

164 165
    AutoConnectSettings*                _autoConnectSettings;
    MAVLinkProtocol*                    _mavlinkProtocol;
166

167 168 169 170
    QList<SharedLinkInterfacePtr>       _rgLinks;
    QList<SharedLinkConfigurationPtr>   _rgLinkConfigs;
    QString                             _autoConnectRTKPort;
    QmlObjectListModel                  _qmlConfigurations;
171

172 173 174
    QMap<QString, int>                  _autoconnectPortWaitList;               ///< key: QGCSerialPortInfo::systemLocation, value: wait count
    QStringList                         _commPortList;
    QStringList                         _commPortDisplayList;
175

Gus Grubba's avatar
Gus Grubba committed
176
#ifndef NO_SERIAL_LINK
177
    QList<SerialLink*>                  _activeLinkCheckList;                   ///< List of links we are waiting for a vehicle to show up on
Don Gagne's avatar
Don Gagne committed
178
#endif
Don Gagne's avatar
Don Gagne committed
179

180
    // NMEA GPS device for GCS position
181
#ifndef __mobile__
182
#ifndef NO_SERIAL_LINK
183 184 185 186
    QString                             _nmeaDeviceName;
    QSerialPort*                        _nmeaPort;
    uint32_t                            _nmeaBaud;
    UdpIODevice                         _nmeaSocket;
187
#endif
188
#endif
189 190 191 192 193 194

    static const char*  _defaultUDPLinkName;
    static const char*  _mavlinkForwardingLinkName;
    static const int    _autoconnectUpdateTimerMSecs;
    static const int    _autoconnectConnectDelayMSecs;

pixhawk's avatar
pixhawk committed
195 196
};