LinkManager.h 9.33 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 40 41
#include "ProtocolInterface.h"
#include "MAVLinkProtocol.h"
#include "LogReplayLink.h"
#include "QmlObjectListModel.h"
42

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

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

Don Gagne's avatar
Don Gagne committed
51
class UDPConfiguration;
pixhawk's avatar
pixhawk committed
52

Don Gagne's avatar
Don Gagne committed
53
Q_DECLARE_LOGGING_CATEGORY(LinkManagerLog)
Don Gagne's avatar
Don Gagne committed
54
Q_DECLARE_LOGGING_CATEGORY(LinkManagerVerboseLog)
Don Gagne's avatar
Don Gagne committed
55

56
class QGCApplication;
57

58 59 60 61 62 63
/// 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.

64
class LinkManager : public QGCTool
65
{
pixhawk's avatar
pixhawk committed
66
    Q_OBJECT
67

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

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

Don Gagne's avatar
Don Gagne committed
75 76 77 78 79 80
    Q_PROPERTY(bool anyActiveLinks                      READ anyActiveLinks                                                     NOTIFY anyActiveLinksChanged)
    Q_PROPERTY(bool anyConnectedLinks                   READ anyConnectedLinks                                                  NOTIFY anyConnectedLinksChanged)
    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)
81 82 83 84 85 86 87 88 89 90 91

    /// LinkInterface Accessor
    Q_PROPERTY(QmlObjectListModel* links                READ links                              CONSTANT)
    /// LinkConfiguration Accessor
    Q_PROPERTY(QmlObjectListModel* linkConfigurations   READ linkConfigurations                 CONSTANT)
    /// List of comm type strings
    Q_PROPERTY(QStringList         linkTypeStrings      READ linkTypeStrings                    CONSTANT)
    /// List of supported baud rates for serial links
    Q_PROPERTY(QStringList         serialBaudRates      READ serialBaudRates                    CONSTANT)
    /// List of comm ports
    Q_PROPERTY(QStringList         serialPortStrings    READ serialPortStrings                                                    NOTIFY commPortStringsChanged)
Don Gagne's avatar
Don Gagne committed
92 93 94 95 96 97 98 99 100 101

    // Property accessors

    bool anyConnectedLinks(void);
    bool anyActiveLinks(void);
    bool autoconnectUDP(void)       { return _autoconnectUDP; }
    bool autoconnectPixhawk(void)   { return _autoconnectPixhawk; }
    bool autoconnect3DRRadio(void)  { return _autoconnect3DRRadio; }
    bool autoconnectPX4Flow(void)   { return _autoconnectPX4Flow; }

102 103 104 105 106
    QmlObjectListModel* links               (void) { return &_links; }
    QmlObjectListModel* linkConfigurations  (void) { return &_linkConfigurations; }
    QStringList         linkTypeStrings     (void) const;
    QStringList         serialBaudRates     (void);
    QStringList         serialPortStrings   (void);
107

108 109 110 111
    void setAutoconnectUDP      (bool autoconnect);
    void setAutoconnectPixhawk  (bool autoconnect);
    void setAutoconnect3DRRadio (bool autoconnect);
    void setAutoconnectPX4Flow  (bool autoconnect);
112 113 114 115 116 117 118 119 120 121

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

122
    /// Sets the flag to suspend the all new connections
123 124
    ///     @param reason User visible reason to suspend connections
    void setConnectionsSuspended(QString reason);
125

126
    /// Sets the flag to allow new connections to be made
127
    void setConnectionsAllowed(void) { _connectionsSuspended = false; }
128

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

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

Don Gagne's avatar
Don Gagne committed
136 137
    /// Disconnects all existing links
    void disconnectAll(void);
138

139 140
    /// Connect the specified link
    bool connectLink(LinkInterface* link);
141

Don Gagne's avatar
Don Gagne committed
142 143
    /// Disconnect the specified link
    Q_INVOKABLE void disconnectLink(LinkInterface* link);
144

Don Gagne's avatar
Don Gagne committed
145 146 147 148 149 150 151 152
    /// Called to notify that a heartbeat was received with the specified information. Will transition
    /// a link to active as needed.
    ///     @param link Heartbeat came through on this link
    ///     @param vehicleId Mavlink system id for vehicle
    ///     @param heartbeat Mavlink heartbeat message
    /// @return true: continue further processing of this message, false: disregard this message
    bool notifyHeartbeatInfo(LinkInterface* link, int vehicleId, mavlink_heartbeat_t& heartbeat);

153 154 155 156
    // 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);
157

Don Gagne's avatar
Don Gagne committed
158 159 160
    // Called to signal app shutdown. Disconnects all links while turning off auto-connect.
    void shutdown(void);

161 162 163
    // Override from QGCTool
    virtual void setToolbox(QGCToolbox *toolbox);

pixhawk's avatar
pixhawk committed
164
signals:
Don Gagne's avatar
Don Gagne committed
165 166 167 168 169 170 171
    void anyActiveLinksChanged(bool anyActiveLinks);
    void anyConnectedLinksChanged(bool anyConnectedLinks);
    void autoconnectUDPChanged(bool autoconnect);
    void autoconnectPixhawkChanged(bool autoconnect);
    void autoconnect3DRRadioChanged(bool autoconnect);
    void autoconnectPX4FlowChanged(bool autoconnect);

pixhawk's avatar
pixhawk committed
172
    void newLink(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
173 174

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

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

    // Link disconnected, all vehicles on link should be gone as well.
181
    void linkDisconnected(LinkInterface* link);
Don Gagne's avatar
Don Gagne committed
182 183 184 185 186 187 188

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

189
    void linkConfigurationChanged();
190
    void commPortStringsChanged();
191

192 193 194
private slots:
    void _linkConnected(void);
    void _linkDisconnected(void);
Don Gagne's avatar
Don Gagne committed
195
    void _vehicleHeartbeatInfo(LinkInterface* link, int vehicleId, int vehicleMavlinkVersion, int vehicleFirmwareType, int vehicleType);
196

197
private:
198
    bool _connectionsSuspendedMsg(void);
Don Gagne's avatar
Don Gagne committed
199 200
    void _updateAutoConnectLinks(void);

dogmaphobic's avatar
dogmaphobic committed
201
#ifndef __ios__
Don Gagne's avatar
Don Gagne committed
202
    SerialConfiguration* _autoconnectConfigurationsContainsPort(const QString& portName);
dogmaphobic's avatar
dogmaphobic committed
203
#endif
204 205 206 207 208 209

    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;
210
    uint32_t _mavlinkChannelsUsedBitMask;
211 212

    MAVLinkProtocol*    _mavlinkProtocol;
Don Gagne's avatar
Don Gagne committed
213 214 215 216 217 218
    QList<int>          _ignoreVehicleIds;  ///< List of vehicle id for which we ignore further communication

    QmlObjectListModel  _links;
    QmlObjectListModel  _linkConfigurations;
    QmlObjectListModel  _autoconnectConfigurations;

219
    QStringList _autoconnectWaitList;
220
    QStringList _commPortList;
221

Don Gagne's avatar
Don Gagne committed
222 223 224 225 226 227 228 229 230 231
    bool _autoconnectUDP;
    bool _autoconnectPixhawk;
    bool _autoconnect3DRRadio;
    bool _autoconnectPX4Flow;

    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
232
    static const char* _defaultUPDLinkName;
pixhawk's avatar
pixhawk committed
233 234
};

235
#endif