LinkManager.cc 5.37 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 79
    // 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);
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
}
pixhawk's avatar
pixhawk committed
88 89 90

bool LinkManager::connectAll()
{
91 92 93 94
    if (_connectionsSuspendedMsg()) {
        return false;
    }
    
95 96
    bool allConnected = true;

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

    return allConnected;
pixhawk's avatar
pixhawk committed
107 108 109 110
}

bool LinkManager::disconnectAll()
{
111 112
    bool allDisconnected = true;

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

    return allDisconnected;
pixhawk's avatar
pixhawk committed
124 125 126 127
}

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

    return link->_connect();
pixhawk's avatar
pixhawk committed
135 136 137 138
}

bool LinkManager::disconnectLink(LinkInterface* link)
{
139 140
    Q_ASSERT(link);
    return link->_disconnect();
pixhawk's avatar
pixhawk committed
141 142
}

143
void LinkManager::deleteLink(LinkInterface* link)
144
{
145
    Q_ASSERT(link);
Don Gagne's avatar
Don Gagne committed
146
    
147
    _linkListMutex.lock();
148 149 150 151 152
    
    Q_ASSERT(_links.contains(link));
    _links.removeOne(link);
    Q_ASSERT(!_links.contains(link));

153
    _linkListMutex.unlock();
154
             
Don Gagne's avatar
Don Gagne committed
155
    // Emit removal of link
156 157 158 159 160
    emit linkDeleted(link);
    
    Q_ASSERT(link->_ownedByLinkManager);
    link->_deletedByLinkManager = true;   // Signal that this is a valid delete
    delete link;
pixhawk's avatar
pixhawk committed
161 162 163 164 165 166 167
}

/**
 *
 */
const QList<LinkInterface*> LinkManager::getLinks()
{
168
    _linkListMutex.lock();
169
    QList<LinkInterface*> ret(_links);
170
    _linkListMutex.unlock();
171
    return ret;
pixhawk's avatar
pixhawk committed
172
}
173 174 175

const QList<SerialLink*> LinkManager::getSerialLinks()
{
176
    _linkListMutex.lock();
177 178
    QList<SerialLink*> s;

179
    foreach (LinkInterface* link, _links)
180
    {
181 182 183
        Q_ASSERT(link);
        
        SerialLink* serialLink = qobject_cast<SerialLink*>(link);
184

185 186
        if (serialLink)
            s.append(serialLink);
187
    }
188
    _linkListMutex.unlock();
189 190 191

    return s;
}
192 193 194 195 196 197

/// @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
198 199
        QGCMessageBox::information(tr("Connect not allowed"),
                                   tr("Connect not allowed: %1").arg(_connectionsSuspendedReason));
200 201 202 203 204 205 206 207 208 209 210 211
        return true;
    } else {
        return false;
    }
}

void LinkManager::setConnectionsSuspended(QString reason)
{
    _connectionsSuspended = true;
    _connectionsSuspendedReason = reason;
    Q_ASSERT(!reason.isEmpty());
}
212 213 214 215 216 217 218 219 220

void LinkManager::_shutdown(void)
{
    QList<LinkInterface*> links = _links;
    foreach(LinkInterface* link, links) {
        disconnectLink(link);
        deleteLink(link);
    }
}