LinkManager.h 11.3 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10


11 12
/// @file
///     @author Lorenz Meier <mavteam@student.ethz.ch>
pixhawk's avatar
pixhawk committed
13 14 15 16 17

#ifndef _LINKMANAGER_H_
#define _LINKMANAGER_H_

#include <QList>
18
#include <QMultiMap>
19
#include <QMutex>
20

21
#include "LinkConfiguration.h"
22
#include "LinkInterface.h"
Don Gagne's avatar
Don Gagne committed
23
#include "QGCLoggingCategory.h"
24
#include "QGCToolbox.h"
Don Gagne's avatar
Don Gagne committed
25 26
#include "ProtocolInterface.h"
#include "MAVLinkProtocol.h"
Gus Grubba's avatar
Gus Grubba committed
27
#if !defined(__mobile__)
Don Gagne's avatar
Don Gagne committed
28
#include "LogReplayLink.h"
dogmaphobic's avatar
dogmaphobic committed
29
#endif
Don Gagne's avatar
Don Gagne committed
30
#include "QmlObjectListModel.h"
31

Gus Grubba's avatar
Gus Grubba committed
32
#ifndef NO_SERIAL_LINK
Don Gagne's avatar
Don Gagne committed
33
    #include "SerialLink.h"
dogmaphobic's avatar
dogmaphobic committed
34
#endif
35

36
#ifdef QT_DEBUG
Don Gagne's avatar
Don Gagne committed
37
    #include "MockLink.h"
38
#endif
39

Don Gagne's avatar
Don Gagne committed
40
class UDPConfiguration;
pixhawk's avatar
pixhawk committed
41

Don Gagne's avatar
Don Gagne committed
42
Q_DECLARE_LOGGING_CATEGORY(LinkManagerLog)
Don Gagne's avatar
Don Gagne committed
43
Q_DECLARE_LOGGING_CATEGORY(LinkManagerVerboseLog)
Don Gagne's avatar
Don Gagne committed
44

45
class QGCApplication;
46

47 48 49 50 51 52
/// Manage communication links
///
/// 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.

53
class LinkManager : public QGCTool
54
{
pixhawk's avatar
pixhawk committed
55
    Q_OBJECT
56

Don Gagne's avatar
Don Gagne committed
57 58
    /// Unit Test has access to private constructor/destructor
    friend class LinkManagerTest;
59

Don Gagne's avatar
Don Gagne committed
60
public:
61 62
    LinkManager(QGCApplication* app);
    ~LinkManager();
63

Don Gagne's avatar
Don Gagne committed
64 65 66 67
    Q_PROPERTY(bool autoconnectUDP                      READ autoconnectUDP                     WRITE setAutoconnectUDP         NOTIFY autoconnectUDPChanged)
    Q_PROPERTY(bool autoconnectPixhawk                  READ autoconnectPixhawk                 WRITE setAutoconnectPixhawk     NOTIFY autoconnectPixhawkChanged)
    Q_PROPERTY(bool autoconnect3DRRadio                 READ autoconnect3DRRadio                WRITE setAutoconnect3DRRadio    NOTIFY autoconnect3DRRadioChanged)
    Q_PROPERTY(bool autoconnectPX4Flow                  READ autoconnectPX4Flow                 WRITE setAutoconnectPX4Flow     NOTIFY autoconnectPX4FlowChanged)
Don Gagne's avatar
Don Gagne committed
68
    Q_PROPERTY(bool autoconnectRTKGPS                   READ autoconnectRTKGPS                  WRITE setAutoconnectRTKGPS      NOTIFY autoconnectRTKGPSChanged)
69
    Q_PROPERTY(bool autoconnectLibrePilot               READ autoconnectLibrePilot              WRITE setAutoconnectLibrePilot  NOTIFY autoconnectLibrePilotChanged)
70 71 72 73 74
    Q_PROPERTY(bool isBluetoothAvailable                READ isBluetoothAvailable                                               CONSTANT)

    Q_PROPERTY(QmlObjectListModel*  linkConfigurations  READ _qmlLinkConfigurations                                             NOTIFY linkConfigurationsChanged)
    Q_PROPERTY(QStringList          linkTypeStrings     READ linkTypeStrings                                                    CONSTANT)
    Q_PROPERTY(QStringList          serialBaudRates     READ serialBaudRates                                                    CONSTANT)
75 76 77 78 79 80 81 82 83 84
    Q_PROPERTY(QStringList          serialPortStrings   READ serialPortStrings                                                  NOTIFY commPortStringsChanged)
    Q_PROPERTY(QStringList          serialPorts         READ serialPorts                                                        NOTIFY commPortsChanged)

    // Create/Edit Link Configuration
    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
85 86 87

    // Property accessors

dogmaphobic's avatar
dogmaphobic committed
88 89 90 91
    bool autoconnectUDP             (void)  { return _autoconnectUDP; }
    bool autoconnectPixhawk         (void)  { return _autoconnectPixhawk; }
    bool autoconnect3DRRadio        (void)  { return _autoconnect3DRRadio; }
    bool autoconnectPX4Flow         (void)  { return _autoconnectPX4Flow; }
Don Gagne's avatar
Don Gagne committed
92
    bool autoconnectRTKGPS          (void)  { return _autoconnectRTKGPS; }
93
    bool autoconnectLibrePilot      (void)  { return _autoconnectLibrePilot; }
dogmaphobic's avatar
dogmaphobic committed
94
    bool isBluetoothAvailable       (void);
Don Gagne's avatar
Don Gagne committed
95

96 97 98 99 100
    QList<LinkInterface*> links                 (void);
    QStringList         linkTypeStrings         (void) const;
    QStringList         serialBaudRates         (void);
    QStringList         serialPortStrings       (void);
    QStringList         serialPorts             (void);
101

102 103 104 105 106 107
    void setAutoconnectUDP        (bool autoconnect);
    void setAutoconnectPixhawk    (bool autoconnect);
    void setAutoconnect3DRRadio   (bool autoconnect);
    void setAutoconnectPX4Flow    (bool autoconnect);
    void setAutoconnectRTKGPS     (bool autoconnect);
    void setAutoconnectLibrePilot (bool autoconnect);
108 109 110 111 112 113 114 115 116 117

    /// Load list of link configurations from disk
    void loadLinkConfigurationList();

    /// Save list of link configurations from disk
    void saveLinkConfigurationList();

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

118
    /// Sets the flag to suspend the all new connections
119 120
    ///     @param reason User visible reason to suspend connections
    void setConnectionsSuspended(QString reason);
121

122
    /// Sets the flag to allow new connections to be made
123
    void setConnectionsAllowed(void) { _connectionsSuspended = false; }
124

125
    /// Creates, connects (and adds) a link  based on the given configuration instance.
Don Gagne's avatar
Don Gagne committed
126
    /// Link takes ownership of config.
127
    Q_INVOKABLE LinkInterface* createConnectedLink(SharedLinkConfigurationPointer& config);
128

129
    /// Creates, connects (and adds) a link  based on the given configuration name.
Don Gagne's avatar
Don Gagne committed
130
    LinkInterface* createConnectedLink(const QString& name);
131

Don Gagne's avatar
Don Gagne committed
132 133
    /// Disconnects all existing links
    void disconnectAll(void);
134

135 136
    /// Connect the specified link
    bool connectLink(LinkInterface* link);
137

Don Gagne's avatar
Don Gagne committed
138 139
    /// Disconnect the specified link
    Q_INVOKABLE void disconnectLink(LinkInterface* link);
140

141 142 143 144
    // The following APIs are public but should not be called in normal use. The are mainly exposed
    // here for unit test code.
    void _deleteLink(LinkInterface* link);
    void _addLink(LinkInterface* link);
145

Don Gagne's avatar
Don Gagne committed
146
    // Called to signal app shutdown. Disconnects all links while turning off auto-connect.
Don Gagne's avatar
Don Gagne committed
147
    Q_INVOKABLE void shutdown(void);
Don Gagne's avatar
Don Gagne committed
148

Don Gagne's avatar
Don Gagne committed
149 150 151 152 153
#ifdef QT_DEBUG
    // Only used by unit test tp restart after a shutdown
    void restart(void) { setConnectionsAllowed(); }
#endif

154 155 156
    /// @return true: specified link is an autoconnect link
    bool isAutoconnectLink(LinkInterface* link);

157 158 159
    // Override from QGCTool
    virtual void setToolbox(QGCToolbox *toolbox);

160 161 162 163 164 165 166 167 168 169 170
    /// @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.
    SharedLinkInterfacePointer sharedLinkInterfacePointerForLink(LinkInterface* link);

    bool containsLink(LinkInterface* link);

    SharedLinkConfigurationPointer addConfiguration(LinkConfiguration* config);

pixhawk's avatar
pixhawk committed
171
signals:
172 173 174 175 176 177 178
    void autoconnectUDPChanged        (bool autoconnect);
    void autoconnectPixhawkChanged    (bool autoconnect);
    void autoconnect3DRRadioChanged   (bool autoconnect);
    void autoconnectPX4FlowChanged    (bool autoconnect);
    void autoconnectRTKGPSChanged     (bool autoconnect);
    void autoconnectLibrePilotChanged (bool autoconnect);

Don Gagne's avatar
Don Gagne committed
179

pixhawk's avatar
pixhawk committed
180
    void newLink(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
181 182

    // Link has been deleted. You may not necessarily get a linkInactive before the link is deleted.
183
    void linkDeleted(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
184 185

    // Link has been connected, but no Vehicle seen on link yet.
186
    void linkConnected(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
187 188

    // Link disconnected, all vehicles on link should be gone as well.
189
    void linkDisconnected(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
190 191 192 193 194 195 196

    // New vehicle has been seen on the link.
    void linkActive(LinkInterface* link, int vehicleId, int vehicleFirmwareType, int vehicleType);

    // No longer hearing from any vehicles on this link.
    void linkInactive(LinkInterface* link);

197
    void commPortStringsChanged();
198 199
    void commPortsChanged();
    void linkConfigurationsChanged();
200

201 202 203
private slots:
    void _linkConnected(void);
    void _linkDisconnected(void);
204
    void _linkConnectionRemoved(LinkInterface* link);
Gus Grubba's avatar
Gus Grubba committed
205
#ifndef NO_SERIAL_LINK
Don Gagne's avatar
Don Gagne committed
206
    void _activeLinkCheck(void);
Don Gagne's avatar
Don Gagne committed
207
#endif
208

209
private:
210
    QmlObjectListModel* _qmlLinkConfigurations  (void) { return &_qmlConfigurations; }
211
    bool _connectionsSuspendedMsg(void);
Don Gagne's avatar
Don Gagne committed
212
    void _updateAutoConnectLinks(void);
213 214
    void _updateSerialPorts();
    void _fixUnnamed(LinkConfiguration* config);
Don Gagne's avatar
Don Gagne committed
215
    bool _setAutoconnectWorker(bool& currentAutoconnect, bool newAutoconnect, const char* autoconnectKey);
216
    void _removeConfiguration(LinkConfiguration* config);
Don Gagne's avatar
Don Gagne committed
217

Gus Grubba's avatar
Gus Grubba committed
218
#ifndef NO_SERIAL_LINK
Don Gagne's avatar
Don Gagne committed
219
    SerialConfiguration* _autoconnectConfigurationsContainsPort(const QString& portName);
dogmaphobic's avatar
dogmaphobic committed
220
#endif
221 222 223 224 225 226

    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;
227
    uint32_t _mavlinkChannelsUsedBitMask;
228 229

    MAVLinkProtocol*    _mavlinkProtocol;
Don Gagne's avatar
Don Gagne committed
230

231 232 233 234 235

    QList<SharedLinkInterfacePointer>       _sharedLinks;
    QList<SharedLinkConfigurationPointer>   _sharedConfigurations;
    QList<SharedLinkConfigurationPointer>   _sharedAutoconnectConfigurations;
    QmlObjectListModel                      _qmlConfigurations;
Don Gagne's avatar
Don Gagne committed
236

237
    QMap<QString, int>  _autoconnectWaitList;   ///< key: QGCSerialPortInfo.systemLocation, value: wait count
238
    QStringList _commPortList;
239
    QStringList _commPortDisplayList;
240

Don Gagne's avatar
Don Gagne committed
241 242 243 244
    bool _autoconnectUDP;
    bool _autoconnectPixhawk;
    bool _autoconnect3DRRadio;
    bool _autoconnectPX4Flow;
Don Gagne's avatar
Don Gagne committed
245
    bool _autoconnectRTKGPS;
246
    bool _autoconnectLibrePilot;
Gus Grubba's avatar
Gus Grubba committed
247
#ifndef NO_SERIAL_LINK
248 249 250
    QTimer              _activeLinkCheckTimer;                  ///< Timer which checks for a vehicle showing up on a usb direct link
    QList<SerialLink*>  _activeLinkCheckList;                   ///< List of links we are waiting for a vehicle to show up on
    static const int    _activeLinkCheckTimeoutMSecs = 15000;   ///< Amount of time to wait for a heatbeat. Keep in mind ArduPilot stack heartbeat is slow to come.
Don Gagne's avatar
Don Gagne committed
251
#endif
Don Gagne's avatar
Don Gagne committed
252

253 254 255 256 257
    static const char*  _settingsGroup;
    static const char*  _autoconnectUDPKey;
    static const char*  _autoconnectPixhawkKey;
    static const char*  _autoconnect3DRRadioKey;
    static const char*  _autoconnectPX4FlowKey;
Don Gagne's avatar
Don Gagne committed
258
    static const char*  _autoconnectRTKGPSKey;
259
    static const char*  _autoconnectLibrePilotKey;
260 261 262
    static const char*  _defaultUPDLinkName;
    static const int    _autoconnectUpdateTimerMSecs;
    static const int    _autoconnectConnectDelayMSecs;
pixhawk's avatar
pixhawk committed
263 264
};

265
#endif