LinkManager.h 6.23 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 38

// Links
dogmaphobic's avatar
dogmaphobic committed
39
#ifndef __ios__
40
#include "SerialLink.h"
dogmaphobic's avatar
dogmaphobic committed
41
#endif
42
#include "UDPLink.h"
43
#include "TCPLink.h"
Don Gagne's avatar
Don Gagne committed
44
#include "LogReplayLink.h"
45

46
#ifdef QT_DEBUG
47
#include "MockLink.h"
48
#endif
49

50 51
#include "ProtocolInterface.h"
#include "QGCSingleton.h"
52
#include "MAVLinkProtocol.h"
pixhawk's avatar
pixhawk committed
53

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

56 57
class LinkManagerTest;

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 QGCSingleton
65
{
pixhawk's avatar
pixhawk committed
66
    Q_OBJECT
Don Gagne's avatar
Don Gagne committed
67
    DECLARE_QGC_SINGLETON(LinkManager, LinkManager)
68

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

Don Gagne's avatar
Don Gagne committed
72
public:
pixhawk's avatar
pixhawk committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    /*!
      Add a new link configuration setting to the list
      @param[in] link An instance of the link setting.
    */
    void addLinkConfiguration(LinkConfiguration* link);

    /*!
      Removes (and deletes) an existing link configuration setting from the list
      @param[in] link An instance of the link setting.
    */
    void removeLinkConfiguration(LinkConfiguration* link);

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

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

    /// Get a list of the configured links. This is the list of configured links that can be used by QGC.
    const QList<LinkConfiguration*> getLinkConfigurationList();

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

99
    /// Returns list of all links
pixhawk's avatar
pixhawk committed
100 101
    const QList<LinkInterface*> getLinks();

102
    // Returns list of all serial links
dogmaphobic's avatar
dogmaphobic committed
103
#ifndef __ios__
104
    const QList<SerialLink*> getSerialLinks();
dogmaphobic's avatar
dogmaphobic committed
105
#endif
106
    /// Sets the flag to suspend the all new connections
107 108
    ///     @param reason User visible reason to suspend connections
    void setConnectionsSuspended(QString reason);
109

110
    /// Sets the flag to allow new connections to be made
111
    void setConnectionsAllowed(void) { _connectionsSuspended = false; }
112

113
    /// Creates, connects (and adds) a link  based on the given configuration instance.
Don Gagne's avatar
Don Gagne committed
114
    /// Link takes ownership of config.
115
    LinkInterface* createConnectedLink(LinkConfiguration* config);
116

117 118
    /// Creates, connects (and adds) a link  based on the given configuration name.
    LinkInterface* createConnectedLink(const QString& name);
119

120 121 122 123 124 125
    /// Returns true if the link manager is holding this link
    bool containsLink(LinkInterface* link);
    
    /// Returns the QSharedPointer for this link. You must use SharedLinkInterface if you are going to
    /// keep references to a link in a thread other than the main ui thread.
    SharedLinkInterface& sharedPointerForLink(LinkInterface* link);
126

127
    /// Re-connects all existing links
pixhawk's avatar
pixhawk committed
128
    bool connectAll();
129

130
    /// Disconnects all existing links
pixhawk's avatar
pixhawk committed
131
    bool disconnectAll();
132

133 134
    /// Connect the specified link
    bool connectLink(LinkInterface* link);
135

136
    /// Disconnect the specified link
pixhawk's avatar
pixhawk committed
137
    bool disconnectLink(LinkInterface* link);
138 139 140 141 142 143 144 145
    
    /// Returns true if there are any connected links
    bool anyConnectedLinks(void);
    
    // 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);
146

pixhawk's avatar
pixhawk committed
147 148
signals:
    void newLink(LinkInterface* link);
149
    void linkDeleted(LinkInterface* link);
150 151
    void linkConnected(LinkInterface* link);
    void linkDisconnected(LinkInterface* link);
152 153
    void linkConfigurationChanged();

154 155 156
private slots:
    void _linkConnected(void);
    void _linkDisconnected(void);
157

158
private:
159
    /// All access to LinkManager is through LinkManager::instance
Don Gagne's avatar
Don Gagne committed
160 161
    LinkManager(QObject* parent = NULL);
    ~LinkManager();
162
    
163
    virtual void _shutdown(void);
Don Gagne's avatar
Don Gagne committed
164

165
    bool _connectionsSuspendedMsg(void);
166
    void _updateConfigurationList(void);
dogmaphobic's avatar
dogmaphobic committed
167
#ifndef __ios__
168
    SerialConfiguration* _findSerialConfiguration(const QString& portName);
dogmaphobic's avatar
dogmaphobic committed
169
#endif
170
    QList<LinkConfiguration*>   _linkConfigurations;    ///< List of configured links
171 172 173 174 175 176
    
    /// List of available links kept as QSharedPointers. We use QSharedPointer since
    /// there are other objects that maintain copies of these links in other threads.
    /// The reference counting allows for orderly deletion.
    QList<SharedLinkInterface>  _links;
    
177 178 179 180 181 182
    QMutex                      _linkListMutex;         ///< Mutex for thread safe access to _links list

    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
dogmaphobic's avatar
dogmaphobic committed
183
#ifndef __ios__
184
    QTimer  _portListTimer;
dogmaphobic's avatar
dogmaphobic committed
185
#endif
186 187
    uint32_t _mavlinkChannelsUsedBitMask;
    
188
    SharedLinkInterface _nullSharedLink;
pixhawk's avatar
pixhawk committed
189 190
};

191
#endif