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

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

pixhawk's avatar
pixhawk committed
5
(c) 2009, 2010 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"
36 37

// Links
38
#include "SerialLink.h"
39 40 41
#include "UDPLink.h"
#include "MockLink.h"

42 43
#include "ProtocolInterface.h"
#include "QGCSingleton.h"
44
#include "MAVLinkProtocol.h"
pixhawk's avatar
pixhawk committed
45

46 47
class LinkManagerTest;

48 49 50 51 52 53
/// 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.

54
class LinkManager : public QGCSingleton
55
{
pixhawk's avatar
pixhawk committed
56
    Q_OBJECT
Don Gagne's avatar
Don Gagne committed
57
    DECLARE_QGC_SINGLETON(LinkManager, LinkManager)
58

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

Don Gagne's avatar
Don Gagne committed
62
public:
pixhawk's avatar
pixhawk committed
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

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

89
    /// Returns list of all links
pixhawk's avatar
pixhawk committed
90 91
    const QList<LinkInterface*> getLinks();

92
    // Returns list of all serial links
93 94
    const QList<SerialLink*> getSerialLinks();

95
    /// Sets the flag to suspend the all new connections
96 97
    ///     @param reason User visible reason to suspend connections
    void setConnectionsSuspended(QString reason);
98

99
    /// Sets the flag to allow new connections to be made
100
    void setConnectionsAllowed(void) { _connectionsSuspended = false; }
101 102 103 104 105 106 107 108 109

    /// Creates (and adds) a link  based on the given configuration instance. LinkManager takes ownership of this object. To delete
    /// it, call LinkManager::deleteLink.
    LinkInterface* createLink(LinkConfiguration* config);

    /// Creates (and adds) a link  based on the given configuration name. LinkManager takes ownership of this object. To delete
    /// it, call LinkManager::deleteLink.
    LinkInterface* createLink(const QString& name);

110 111 112
    /// Adds the link to the LinkManager. LinkManager takes ownership of this object. To delete
    /// it, call LinkManager::deleteLink.
    void addLink(LinkInterface* link);
113

114
    /// Deletes the specified link. Will disconnect if connected.
115
    // TODO Will also crash if called. MAVLink protocol is not handling the disconnect properly.
116
    void deleteLink(LinkInterface* link);
117

118
    /// Re-connects all existing links
pixhawk's avatar
pixhawk committed
119
    bool connectAll();
120

121
    /// Disconnects all existing links
pixhawk's avatar
pixhawk committed
122
    bool disconnectAll();
123

124 125
    /// Connect the specified link
    bool connectLink(LinkInterface* link);
126

127
    /// Disconnect the specified link
pixhawk's avatar
pixhawk committed
128
    bool disconnectLink(LinkInterface* link);
129

pixhawk's avatar
pixhawk committed
130 131
signals:
    void newLink(LinkInterface* link);
132
    void linkDeleted(LinkInterface* link);
133 134
    void linkConnected(LinkInterface* link);
    void linkDisconnected(LinkInterface* link);
135 136
    void linkConfigurationChanged();

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

141
private:
142
    /// All access to LinkManager is through LinkManager::instance
Don Gagne's avatar
Don Gagne committed
143 144
    LinkManager(QObject* parent = NULL);
    ~LinkManager();
145

146
    virtual void _shutdown(void);
Don Gagne's avatar
Don Gagne committed
147

148
    bool _connectionsSuspendedMsg(void);
149 150 151 152 153 154 155 156 157 158 159 160
    void _updateConfigurationList(void);
    SerialConfiguration* _findSerialConfiguration(const QString& portName);

    QList<LinkConfiguration*>   _linkConfigurations;    ///< List of configured links
    QList<LinkInterface*>       _links;                 ///< List of available links
    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
    QTimer  _portListTimer;
pixhawk's avatar
pixhawk committed
161 162
};

163
#endif