Skip to content
LinkManager.cc 6.78 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/*=====================================================================
lm's avatar
lm committed

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
    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

    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
    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.
pixhawk's avatar
pixhawk committed
    You should have received a copy of the GNU General Public License
lm's avatar
lm committed
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

pixhawk's avatar
pixhawk committed
======================================================================*/
pixhawk's avatar
pixhawk committed
/**
 * @file
 *   @brief Brief Description
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QList>
#include <QApplication>
#include "LinkManager.h"
#include "MainWindow.h"
Don Gagne's avatar
Don Gagne committed
#include "QGCMessageBox.h"
#include "QGCApplication.h"
LinkManager* LinkManager::_instance = NULL;

LinkManager* LinkManager::instance(void)
{
    if(_instance == 0) {
        _instance = new LinkManager(qgcApp());
        Q_CHECK_PTR(_instance);
    
    Q_ASSERT(_instance);
    
    return _instance;
pixhawk's avatar
pixhawk committed
}

void LinkManager::deleteInstance(void)
{
    _instance = NULL;
    delete this;
}

pixhawk's avatar
pixhawk committed
/**
 * @brief Private singleton constructor
pixhawk's avatar
pixhawk committed
 * This class implements the singleton design pattern and has therefore only a private constructor.
 **/
LinkManager::LinkManager(QObject* parent, bool registerSingleton) :
    QGCSingleton(parent, registerSingleton),
    _connectionsSuspended(false)
pixhawk's avatar
pixhawk committed
{
    _links = QList<LinkInterface*>();
    _protocolLinks = QMap<ProtocolInterface*, LinkInterface*>();
pixhawk's avatar
pixhawk committed
}

LinkManager::~LinkManager()
{
    disconnectAll();
    
    foreach (LinkInterface* link, _links) {
        Q_ASSERT(link);
        deleteLink(link);
Lorenz Meier's avatar
Lorenz Meier committed
    }
    _links.clear();
pixhawk's avatar
pixhawk committed
}

void LinkManager::add(LinkInterface* link)
{
    // Take ownership for delete
    link->_ownedByLinkManager = true;
    
    if (!_links.contains(link)) {
        _links.append(link);
        _dataMutex.unlock();
        _dataMutex.unlock();
pixhawk's avatar
pixhawk committed
}

void LinkManager::addProtocol(LinkInterface* link, ProtocolInterface* protocol)
{
    Q_ASSERT(link);
    Q_ASSERT(protocol);
    
pixhawk's avatar
pixhawk committed
    // Connect link to protocol
    // the protocol will receive new bytes from the link
    _dataMutex.lock();
    QList<LinkInterface*> linkList = _protocolLinks.values(protocol);

    // If protocol has not been added before (list length == 0)
    // OR if link has not been added to protocol, add
Lorenz Meier's avatar
Lorenz Meier committed
    if (!linkList.contains(link))
Lorenz Meier's avatar
Lorenz Meier committed
    {
        // Protocol is new, add
        connect(link, SIGNAL(bytesReceived(LinkInterface*, QByteArray)), protocol, SLOT(receiveBytes(LinkInterface*, QByteArray)));
        // Add status
        connect(link, SIGNAL(connected(bool)), protocol, SLOT(linkStatusChanged(bool)));
        // Store the connection information in the protocol links map
        _protocolLinks.insertMulti(protocol, link);
        _dataMutex.unlock();
        // Make sure the protocol clears its metadata for this link.
        protocol->resetMetadataForLink(link);
        _dataMutex.unlock();
    //qDebug() << __FILE__ << __LINE__ << "ADDED LINK TO PROTOCOL" << link->getName() << protocol->getName() << "NEW SIZE OF LINK LIST:" << _protocolLinks.size();
pixhawk's avatar
pixhawk committed
}

QList<LinkInterface*> LinkManager::getLinksForProtocol(ProtocolInterface* protocol)
{
    _dataMutex.lock();
    QList<LinkInterface*> links = _protocolLinks.values(protocol);
    _dataMutex.unlock();
pixhawk's avatar
pixhawk committed
}

ProtocolInterface* LinkManager::getProtocolForLink(LinkInterface* link)
{
    _dataMutex.lock();
    ProtocolInterface* protocol = _protocolLinks.key(link);
    _dataMutex.unlock();
	return protocol;
pixhawk's avatar
pixhawk committed

bool LinkManager::connectAll()
{
    if (_connectionsSuspendedMsg()) {
        return false;
    }
    
    bool allConnected = true;

    _dataMutex.lock();
    foreach (LinkInterface* link, _links) {
        Q_ASSERT(link);
        if (!link->_connect()) {
            allConnected = false;
        }
    _dataMutex.unlock();

    return allConnected;
pixhawk's avatar
pixhawk committed
}

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

    _dataMutex.lock();
    foreach (LinkInterface* link, _links)
Lorenz Meier's avatar
Lorenz Meier committed
    {
        if (!link->_disconnect()) {
            allDisconnected = false;
        }
    _dataMutex.unlock();

    return allDisconnected;
pixhawk's avatar
pixhawk committed
}

bool LinkManager::connectLink(LinkInterface* link)
{
    Q_ASSERT(link);
    
    if (_connectionsSuspendedMsg()) {
        return false;
    }

    return link->_connect();
pixhawk's avatar
pixhawk committed
}

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

void LinkManager::deleteLink(LinkInterface* link)
    Q_ASSERT(link);
Don Gagne's avatar
Don Gagne committed
    
    _dataMutex.lock();
    
    Q_ASSERT(_links.contains(link));
    _links.removeOne(link);
    Q_ASSERT(!_links.contains(link));

Don Gagne's avatar
Don Gagne committed
    // Remove link from protocol map
    QList<ProtocolInterface* > protocols = _protocolLinks.keys(link);
    foreach (ProtocolInterface* proto, protocols) {
Don Gagne's avatar
Don Gagne committed
        _protocolLinks.remove(proto, link);
    }
Don Gagne's avatar
Don Gagne committed
    _dataMutex.unlock();
Don Gagne's avatar
Don Gagne committed
    // Emit removal of link
    emit linkDeleted(link);
    
    Q_ASSERT(link->_ownedByLinkManager);
    link->_deletedByLinkManager = true;   // Signal that this is a valid delete
    delete link;
pixhawk's avatar
pixhawk committed
}

/**
 *
 */
const QList<LinkInterface*> LinkManager::getLinks()
{
    _dataMutex.lock();
    QList<LinkInterface*> ret(_links);
    _dataMutex.unlock();
pixhawk's avatar
pixhawk committed
}

const QList<SerialLink*> LinkManager::getSerialLinks()
{
    _dataMutex.lock();
    QList<SerialLink*> s;

    foreach (LinkInterface* link, _links)
        Q_ASSERT(link);
        
        SerialLink* serialLink = qobject_cast<SerialLink*>(link);
        if (serialLink)
            s.append(serialLink);
    _dataMutex.unlock();

/// @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
        QGCMessageBox::information(tr("Connect not allowed"),
                                   tr("Connect not allowed: %1").arg(_connectionsSuspendedReason));
        return true;
    } else {
        return false;
    }
}

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