LinkManager.h 10.9 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
/*=====================================================================
2

pixhawk's avatar
pixhawk committed
3
PIXHAWK Micro Air Vehicle Flying Robotics Toolkit
4

5
(c) 2009, 2015 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>
6

pixhawk's avatar
pixhawk committed
7
This file is part of the PIXHAWK project
8

pixhawk's avatar
pixhawk committed
9 10 11 12
    PIXHAWK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
13

pixhawk's avatar
pixhawk committed
14 15 16 17
    PIXHAWK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
18

pixhawk's avatar
pixhawk committed
19 20
    You should have received a copy of the GNU General Public License
    along with PIXHAWK. If not, see <http://www.gnu.org/licenses/>.
21

pixhawk's avatar
pixhawk committed
22
======================================================================*/
23

24 25
/// @file
///     @author Lorenz Meier <mavteam@student.ethz.ch>
pixhawk's avatar
pixhawk committed
26 27 28 29 30

#ifndef _LINKMANAGER_H_
#define _LINKMANAGER_H_

#include <QList>
31
#include <QMultiMap>
32
#include <QMutex>
33

34
#include "LinkConfiguration.h"
35
#include "LinkInterface.h"
Don Gagne's avatar
Don Gagne committed
36
#include "QGCLoggingCategory.h"
37
#include "QGCToolbox.h"
Don Gagne's avatar
Don Gagne committed
38 39
#include "ProtocolInterface.h"
#include "MAVLinkProtocol.h"
dogmaphobic's avatar
dogmaphobic committed
40
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
41
#include "LogReplayLink.h"
dogmaphobic's avatar
dogmaphobic committed
42
#endif
Don Gagne's avatar
Don Gagne committed
43
#include "QmlObjectListModel.h"
44

dogmaphobic's avatar
dogmaphobic committed
45
#ifndef __ios__
Don Gagne's avatar
Don Gagne committed
46
    #include "SerialLink.h"
dogmaphobic's avatar
dogmaphobic committed
47
#endif
48

49
#ifdef QT_DEBUG
Don Gagne's avatar
Don Gagne committed
50
    #include "MockLink.h"
51
#endif
52

Don Gagne's avatar
Don Gagne committed
53
class UDPConfiguration;
pixhawk's avatar
pixhawk committed
54

Don Gagne's avatar
Don Gagne committed
55
Q_DECLARE_LOGGING_CATEGORY(LinkManagerLog)
Don Gagne's avatar
Don Gagne committed
56
Q_DECLARE_LOGGING_CATEGORY(LinkManagerVerboseLog)
Don Gagne's avatar
Don Gagne committed
57

58
class QGCApplication;
59

60 61 62 63 64 65
/// 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.

66
class LinkManager : public QGCTool
67
{
pixhawk's avatar
pixhawk committed
68
    Q_OBJECT
69

Don Gagne's avatar
Don Gagne committed
70 71
    /// Unit Test has access to private constructor/destructor
    friend class LinkManagerTest;
72

Don Gagne's avatar
Don Gagne committed
73
public:
74 75
    LinkManager(QGCApplication* app);
    ~LinkManager();
76

Don Gagne's avatar
Don Gagne committed
77 78 79 80
    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
81
    Q_PROPERTY(bool autoconnectRTKGPS                   READ autoconnectRTKGPS                  WRITE setAutoconnectRTKGPS      NOTIFY autoconnectRTKGPSChanged)
dogmaphobic's avatar
dogmaphobic committed
82
    Q_PROPERTY(bool isBluetoothAvailable                READ isBluetoothAvailable               CONSTANT)
83 84

    /// LinkInterface Accessor
85
    Q_PROPERTY(QmlObjectListModel*  links               READ links                              CONSTANT)
86
    /// LinkConfiguration Accessor
87
    Q_PROPERTY(QmlObjectListModel*  linkConfigurations  READ linkConfigurations                                                 NOTIFY linkConfigurationsChanged)
88
    /// List of comm type strings
89
    Q_PROPERTY(QStringList          linkTypeStrings     READ linkTypeStrings                    CONSTANT)
90
    /// List of supported baud rates for serial links
91 92 93
    Q_PROPERTY(QStringList          serialBaudRates     READ serialBaudRates                    CONSTANT)
    /// List of comm ports display names
    Q_PROPERTY(QStringList          serialPortStrings   READ serialPortStrings                                                  NOTIFY commPortStringsChanged)
94
    /// List of comm ports
95 96 97 98 99 100 101 102 103
    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
104 105 106

    // Property accessors

dogmaphobic's avatar
dogmaphobic committed
107 108 109 110
    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
111
    bool autoconnectRTKGPS          (void)  { return _autoconnectRTKGPS; }
dogmaphobic's avatar
dogmaphobic committed
112
    bool isBluetoothAvailable       (void);
Don Gagne's avatar
Don Gagne committed
113

114 115 116 117 118
    QmlObjectListModel* links               (void) { return &_links; }
    QmlObjectListModel* linkConfigurations  (void) { return &_linkConfigurations; }
    QStringList         linkTypeStrings     (void) const;
    QStringList         serialBaudRates     (void);
    QStringList         serialPortStrings   (void);
119
    QStringList         serialPorts         (void);
120

121 122 123 124
    void setAutoconnectUDP      (bool autoconnect);
    void setAutoconnectPixhawk  (bool autoconnect);
    void setAutoconnect3DRRadio (bool autoconnect);
    void setAutoconnectPX4Flow  (bool autoconnect);
Don Gagne's avatar
Don Gagne committed
125
    void setAutoconnectRTKGPS   (bool autoconnect);
126 127 128 129 130 131 132 133 134 135

    /// 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);

136
    /// Sets the flag to suspend the all new connections
137 138
    ///     @param reason User visible reason to suspend connections
    void setConnectionsSuspended(QString reason);
139

140
    /// Sets the flag to allow new connections to be made
141
    void setConnectionsAllowed(void) { _connectionsSuspended = false; }
142

143
    /// Creates, connects (and adds) a link  based on the given configuration instance.
Don Gagne's avatar
Don Gagne committed
144
    /// Link takes ownership of config.
Don Gagne's avatar
Don Gagne committed
145
    Q_INVOKABLE LinkInterface* createConnectedLink(LinkConfiguration* config);
146

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

Don Gagne's avatar
Don Gagne committed
150 151
    /// Disconnects all existing links
    void disconnectAll(void);
152

153 154
    /// Connect the specified link
    bool connectLink(LinkInterface* link);
155

Don Gagne's avatar
Don Gagne committed
156 157
    /// Disconnect the specified link
    Q_INVOKABLE void disconnectLink(LinkInterface* link);
158

159 160 161 162
    // 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);
163

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

Don Gagne's avatar
Don Gagne committed
167 168 169 170 171
#ifdef QT_DEBUG
    // Only used by unit test tp restart after a shutdown
    void restart(void) { setConnectionsAllowed(); }
#endif

172 173 174
    /// @return true: specified link is an autoconnect link
    bool isAutoconnectLink(LinkInterface* link);

175 176 177
    // Override from QGCTool
    virtual void setToolbox(QGCToolbox *toolbox);

pixhawk's avatar
pixhawk committed
178
signals:
Don Gagne's avatar
Don Gagne committed
179 180 181 182 183
    void autoconnectUDPChanged      (bool autoconnect);
    void autoconnectPixhawkChanged  (bool autoconnect);
    void autoconnect3DRRadioChanged (bool autoconnect);
    void autoconnectPX4FlowChanged  (bool autoconnect);
    void autoconnectRTKGPSChanged   (bool autoconnect);
Don Gagne's avatar
Don Gagne committed
184

pixhawk's avatar
pixhawk committed
185
    void newLink(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
186 187

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

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

    // Link disconnected, all vehicles on link should be gone as well.
194
    void linkDisconnected(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
195 196 197 198 199 200 201

    // 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);

202
    void commPortStringsChanged();
203 204
    void commPortsChanged();
    void linkConfigurationsChanged();
205

206 207 208
private slots:
    void _linkConnected(void);
    void _linkDisconnected(void);
209
    void _linkConnectionRemoved(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
210
#ifndef __ios__
Don Gagne's avatar
Don Gagne committed
211
    void _activeLinkCheck(void);
Don Gagne's avatar
Don Gagne committed
212
#endif
213

214
private:
215
    bool _connectionsSuspendedMsg(void);
Don Gagne's avatar
Don Gagne committed
216
    void _updateAutoConnectLinks(void);
217 218
    void _updateSerialPorts();
    void _fixUnnamed(LinkConfiguration* config);
Don Gagne's avatar
Don Gagne committed
219
    bool _setAutoconnectWorker(bool& currentAutoconnect, bool newAutoconnect, const char* autoconnectKey);
Don Gagne's avatar
Don Gagne committed
220

dogmaphobic's avatar
dogmaphobic committed
221
#ifndef __ios__
Don Gagne's avatar
Don Gagne committed
222
    SerialConfiguration* _autoconnectConfigurationsContainsPort(const QString& portName);
dogmaphobic's avatar
dogmaphobic committed
223
#endif
224 225 226 227 228 229

    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;
230
    uint32_t _mavlinkChannelsUsedBitMask;
231 232

    MAVLinkProtocol*    _mavlinkProtocol;
Don Gagne's avatar
Don Gagne committed
233 234 235 236 237

    QmlObjectListModel  _links;
    QmlObjectListModel  _linkConfigurations;
    QmlObjectListModel  _autoconnectConfigurations;

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

Don Gagne's avatar
Don Gagne committed
242 243 244 245
    bool _autoconnectUDP;
    bool _autoconnectPixhawk;
    bool _autoconnect3DRRadio;
    bool _autoconnectPX4Flow;
Don Gagne's avatar
Don Gagne committed
246
    bool _autoconnectRTKGPS;
Don Gagne's avatar
Don Gagne committed
247

Don Gagne's avatar
Don Gagne committed
248
#ifndef __ios__
249 250 251
    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
252
#endif
Don Gagne's avatar
Don Gagne committed
253

254 255 256 257 258
    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
259
    static const char*  _autoconnectRTKGPSKey;
260 261 262
    static const char*  _defaultUPDLinkName;
    static const int    _autoconnectUpdateTimerMSecs;
    static const int    _autoconnectConnectDelayMSecs;
pixhawk's avatar
pixhawk committed
263 264
};

265
#endif