LinkManager.h 3.79 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 31

#ifndef _LINKMANAGER_H_
#define _LINKMANAGER_H_

#include <QThread>
#include <QList>
32
#include <QMultiMap>
33
#include <QMutex>
34 35 36 37 38

#include "LinkInterface.h"
#include "SerialLink.h"
#include "ProtocolInterface.h"
#include "QGCSingleton.h"
39
#include "MAVLinkProtocol.h"
pixhawk's avatar
pixhawk committed
40

41 42
class LinkManagerTest;

43 44 45 46 47 48
/// 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.

49
class LinkManager : public QGCSingleton
50
{
pixhawk's avatar
pixhawk committed
51 52 53
    Q_OBJECT

public:
54 55 56 57 58
    /// @brief Returns the LinkManager singleton
    static LinkManager* instance(void);
    
    virtual void deleteInstance(void);

pixhawk's avatar
pixhawk committed
59 60
    ~LinkManager();

61
    /// Returns list of all links
pixhawk's avatar
pixhawk committed
62 63
    const QList<LinkInterface*> getLinks();

64
    // Returns list of all serial links
65 66
    const QList<SerialLink*> getSerialLinks();

67
    /// Sets the flag to suspend the all new connections
68 69 70
    ///     @param reason User visible reason to suspend connections
    void setConnectionsSuspended(QString reason);
    
71
    /// Sets the flag to allow new connections to be made
72
    void setConnectionsAllowed(void) { _connectionsSuspended = false; }
73
    
74 75 76 77 78
    /// Adds the link to the LinkManager. LinkManager takes ownership of this object. To delete
    /// it, call LinkManager::deleteLink.
    void addLink(LinkInterface* link);
    
    /// Deletes the specified link. Will disconnect if connected.
79 80
    void deleteLink(LinkInterface* link);
    
81
    /// Re-connects all existing links
pixhawk's avatar
pixhawk committed
82
    bool connectAll();
83 84
    
    /// Disconnects all existing links
pixhawk's avatar
pixhawk committed
85
    bool disconnectAll();
86 87 88 89 90
    
    /// Connect the specified link
    bool connectLink(LinkInterface* link);
    
    /// Disconnect the specified link
pixhawk's avatar
pixhawk committed
91
    bool disconnectLink(LinkInterface* link);
92 93 94 95
    
    /// Returns the one mavlink protocol object in the system
    MAVLinkProtocol* mavlink(void) { return _mavlink; }
    
pixhawk's avatar
pixhawk committed
96 97
signals:
    void newLink(LinkInterface* link);
98
    void linkDeleted(LinkInterface* link);
99 100
    
private:
101
    /// All access to LinkManager is through LinkManager::instance
102
    LinkManager(QObject* parent = NULL, bool registerSingleton = true);
103
    
104
    /// LinkManager unit test is allowed to new LinkManager objects
105
    friend class LinkManagerTest;
106
    
107
    bool _connectionsSuspendedMsg(void);
Don Gagne's avatar
Don Gagne committed
108
    
109
    static LinkManager* _instance;  /// LinkManager singleton
110
    
111 112 113 114 115 116
    QList<LinkInterface*>   _links;         ///< List of available links
    QMutex                  _linkListMutex; ///< Mutex for thread safe access to _links list
    
    
    bool    _connectionsSuspended;          ///< true: all new connections should not be allowed
    QString _connectionsSuspendedReason;    ///< User visible reason for suspension
117
    
118
    MAVLinkProtocol* _mavlink;  ///< The one and only mavlink protocol
pixhawk's avatar
pixhawk committed
119 120
};

121
#endif