LinkManager.cc 5.42 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
/*=====================================================================
lm's avatar
lm committed
2 3 4 5 6 7 8 9

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
10 11 12
    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.
lm's avatar
lm committed
13 14

    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17
    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.
lm's avatar
lm committed
18

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

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

pixhawk's avatar
pixhawk committed
24 25 26 27 28 29 30 31 32 33
/**
 * @file
 *   @brief Brief Description
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QList>
#include <QApplication>
34 35
#include <QDebug>

36 37
#include "LinkManager.h"
#include "MainWindow.h"
Don Gagne's avatar
Don Gagne committed
38
#include "QGCMessageBox.h"
39
#include "QGCApplication.h"
40

Don Gagne's avatar
Don Gagne committed
41
IMPLEMENT_QGC_SINGLETON(LinkManager, LinkManager)
42

pixhawk's avatar
pixhawk committed
43 44
/**
 * @brief Private singleton constructor
45
 *
pixhawk's avatar
pixhawk committed
46 47
 * This class implements the singleton design pattern and has therefore only a private constructor.
 **/
Don Gagne's avatar
Don Gagne committed
48 49
LinkManager::LinkManager(QObject* parent) :
    QGCSingleton(parent),
50 51
    _connectionsSuspended(false),
    _mavlink(NULL)
pixhawk's avatar
pixhawk committed
52
{
53
    _mavlink = new MAVLinkProtocol(this);
54
    Q_CHECK_PTR(_mavlink);
pixhawk's avatar
pixhawk committed
55 56 57 58 59
}

LinkManager::~LinkManager()
{
    disconnectAll();
60 61 62
    
    foreach (LinkInterface* link, _links) {
        Q_ASSERT(link);
63
        deleteLink(link);
Lorenz Meier's avatar
Lorenz Meier committed
64
    }
65
    _links.clear();
66 67 68 69 70
    
    // Clear out the queue so disconnects make it all the way through threads
    qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
    
    delete _mavlink;
pixhawk's avatar
pixhawk committed
71 72
}

73
void LinkManager::addLink(LinkInterface* link)
pixhawk's avatar
pixhawk committed
74
{
75 76
    Q_ASSERT(link);
    
77 78 79
    // Take ownership for delete
    link->_ownedByLinkManager = true;
    
80
    _linkListMutex.lock();
81
    
82
    if (!_links.contains(link)) {
83
        _links.append(link);
84
        _linkListMutex.unlock();
85
        emit newLink(link);
86
    } else {
87
        _linkListMutex.unlock();
88
    }
89
    
90 91 92
    // MainWindow may be around when doing things like running unit tests
    if (MainWindow::instance()) {
        connect(link, SIGNAL(communicationError(QString,QString)), MainWindow::instance(), SLOT(showCriticalMessage(QString,QString)), Qt::QueuedConnection);
93
    }
94 95 96 97 98
    
    connect(link, &LinkInterface::bytesReceived, _mavlink, &MAVLinkProtocol::receiveBytes);
    connect(link, &LinkInterface::connected, _mavlink, &MAVLinkProtocol::linkConnected);
    connect(link, &LinkInterface::disconnected, _mavlink, &MAVLinkProtocol::linkDisconnected);
    _mavlink->resetMetadataForLink(link);
99
}
pixhawk's avatar
pixhawk committed
100 101 102

bool LinkManager::connectAll()
{
103 104 105 106
    if (_connectionsSuspendedMsg()) {
        return false;
    }
    
107 108
    bool allConnected = true;

109
    _linkListMutex.lock();
110 111 112 113 114
    foreach (LinkInterface* link, _links) {
        Q_ASSERT(link);
        if (!link->_connect()) {
            allConnected = false;
        }
115
    }
116
    _linkListMutex.unlock();
117 118

    return allConnected;
pixhawk's avatar
pixhawk committed
119 120 121 122
}

bool LinkManager::disconnectAll()
{
123 124
    bool allDisconnected = true;

125
    _linkListMutex.lock();
126
    foreach (LinkInterface* link, _links)
Lorenz Meier's avatar
Lorenz Meier committed
127
    {
128
        Q_ASSERT(link);
129
        if (!link->_disconnect()) {
130 131
            allDisconnected = false;
        }
132
    }
133
    _linkListMutex.unlock();
134 135

    return allDisconnected;
pixhawk's avatar
pixhawk committed
136 137 138 139
}

bool LinkManager::connectLink(LinkInterface* link)
{
140 141 142 143 144 145 146
    Q_ASSERT(link);
    
    if (_connectionsSuspendedMsg()) {
        return false;
    }

    return link->_connect();
pixhawk's avatar
pixhawk committed
147 148 149 150
}

bool LinkManager::disconnectLink(LinkInterface* link)
{
151 152
    Q_ASSERT(link);
    return link->_disconnect();
pixhawk's avatar
pixhawk committed
153 154
}

155
void LinkManager::deleteLink(LinkInterface* link)
156
{
157
    Q_ASSERT(link);
Don Gagne's avatar
Don Gagne committed
158
    
159
    _linkListMutex.lock();
160 161 162 163 164
    
    Q_ASSERT(_links.contains(link));
    _links.removeOne(link);
    Q_ASSERT(!_links.contains(link));

165
    _linkListMutex.unlock();
166
             
Don Gagne's avatar
Don Gagne committed
167
    // Emit removal of link
168 169 170 171 172
    emit linkDeleted(link);
    
    Q_ASSERT(link->_ownedByLinkManager);
    link->_deletedByLinkManager = true;   // Signal that this is a valid delete
    delete link;
pixhawk's avatar
pixhawk committed
173 174 175 176 177 178 179
}

/**
 *
 */
const QList<LinkInterface*> LinkManager::getLinks()
{
180
    _linkListMutex.lock();
181
    QList<LinkInterface*> ret(_links);
182
    _linkListMutex.unlock();
183
    return ret;
pixhawk's avatar
pixhawk committed
184
}
185 186 187

const QList<SerialLink*> LinkManager::getSerialLinks()
{
188
    _linkListMutex.lock();
189 190
    QList<SerialLink*> s;

191
    foreach (LinkInterface* link, _links)
192
    {
193 194 195
        Q_ASSERT(link);
        
        SerialLink* serialLink = qobject_cast<SerialLink*>(link);
196

197 198
        if (serialLink)
            s.append(serialLink);
199
    }
200
    _linkListMutex.unlock();
201 202 203

    return s;
}
204 205 206 207 208 209

/// @brief If all new connections should be suspended a message is displayed to the user and true
///         is returned;
bool LinkManager::_connectionsSuspendedMsg(void)
{
    if (_connectionsSuspended) {
Don Gagne's avatar
Don Gagne committed
210 211
        QGCMessageBox::information(tr("Connect not allowed"),
                                   tr("Connect not allowed: %1").arg(_connectionsSuspendedReason));
212 213 214 215 216 217 218 219 220 221 222 223
        return true;
    } else {
        return false;
    }
}

void LinkManager::setConnectionsSuspended(QString reason)
{
    _connectionsSuspended = true;
    _connectionsSuspendedReason = reason;
    Q_ASSERT(!reason.isEmpty());
}