LinkManager.cc 5.81 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
    _connectionsSuspended(false)
pixhawk's avatar
pixhawk committed
51
{
52

pixhawk's avatar
pixhawk committed
53 54 55 56
}

LinkManager::~LinkManager()
{
57
    Q_ASSERT_X(_links.count() == 0, "LinkManager", "LinkManager::_shutdown should have been called previously");
pixhawk's avatar
pixhawk committed
58 59
}

60
void LinkManager::addLink(LinkInterface* link)
pixhawk's avatar
pixhawk committed
61
{
62 63
    Q_ASSERT(link);
    
64 65 66
    // Take ownership for delete
    link->_ownedByLinkManager = true;
    
67
    _linkListMutex.lock();
68
    
69
    if (!_links.contains(link)) {
70
        _links.append(link);
71
        _linkListMutex.unlock();
72
        emit newLink(link);
73
    } else {
74
        _linkListMutex.unlock();
75
    }
76
    
77 78
    // MainWindow may be around when doing things like running unit tests
    if (MainWindow::instance()) {
79
        connect(link, &LinkInterface::communicationError, qgcApp(), &QGCApplication::criticalMessageBoxOnMainThread);
80
    }
81
    
82 83 84 85 86
    MAVLinkProtocol* mavlink = MAVLinkProtocol::instance();
    connect(link, &LinkInterface::bytesReceived, mavlink, &MAVLinkProtocol::receiveBytes);
    connect(link, &LinkInterface::connected, mavlink, &MAVLinkProtocol::linkConnected);
    connect(link, &LinkInterface::disconnected, mavlink, &MAVLinkProtocol::linkDisconnected);
    mavlink->resetMetadataForLink(link);
87 88 89
    
    connect(link, &LinkInterface::connected, this, &LinkManager::_linkConnected);
    connect(link, &LinkInterface::disconnected, this, &LinkManager::_linkDisconnected);
90
}
pixhawk's avatar
pixhawk committed
91 92 93

bool LinkManager::connectAll()
{
94 95 96 97
    if (_connectionsSuspendedMsg()) {
        return false;
    }
    
98 99
    bool allConnected = true;

100
    _linkListMutex.lock();
101 102 103 104 105
    foreach (LinkInterface* link, _links) {
        Q_ASSERT(link);
        if (!link->_connect()) {
            allConnected = false;
        }
106
    }
107
    _linkListMutex.unlock();
108 109

    return allConnected;
pixhawk's avatar
pixhawk committed
110 111 112 113
}

bool LinkManager::disconnectAll()
{
114 115
    bool allDisconnected = true;

116
    _linkListMutex.lock();
117
    foreach (LinkInterface* link, _links)
Lorenz Meier's avatar
Lorenz Meier committed
118
    {
119
        Q_ASSERT(link);
120
        if (!link->_disconnect()) {
121 122
            allDisconnected = false;
        }
123
    }
124
    _linkListMutex.unlock();
125 126

    return allDisconnected;
pixhawk's avatar
pixhawk committed
127 128 129 130
}

bool LinkManager::connectLink(LinkInterface* link)
{
131 132 133 134 135 136
    Q_ASSERT(link);
    
    if (_connectionsSuspendedMsg()) {
        return false;
    }

137 138 139 140 141
    if (link->_connect()) {
        return true;
    } else {
        return false;
    }
pixhawk's avatar
pixhawk committed
142 143 144 145
}

bool LinkManager::disconnectLink(LinkInterface* link)
{
146
    Q_ASSERT(link);
147 148 149 150 151 152
    
    if (link->_disconnect()) {
        return true;
    } else {
        return false;
    }
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());
}
224 225 226 227 228 229 230 231 232

void LinkManager::_shutdown(void)
{
    QList<LinkInterface*> links = _links;
    foreach(LinkInterface* link, links) {
        disconnectLink(link);
        deleteLink(link);
    }
}
233 234 235 236 237 238 239 240 241 242

void LinkManager::_linkConnected(void)
{
    emit linkConnected((LinkInterface*)sender());
}

void LinkManager::_linkDisconnected(void)
{
    emit linkDisconnected((LinkInterface*)sender());
}