LinkManager.cc 4.53 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 24 25 26 27 28 29 30 31 32 33 34
======================================================================*/
 
/**
 * @file
 *   @brief Brief Description
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QList>
#include <QApplication>
#include "LinkManager.h"
35
#include <iostream>
pixhawk's avatar
pixhawk committed
36

37 38
#include <QDebug>

pixhawk's avatar
pixhawk committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
LinkManager* LinkManager::instance() {
	static LinkManager* _instance = 0;
	if(_instance == 0) {
		_instance = new LinkManager();
		
		/* Set the application as parent to ensure that this object
		 * will be destroyed when the main application exits */
		_instance->setParent(qApp);
	}
	return _instance;
}

/**
 * @brief Private singleton constructor
 * 
 * This class implements the singleton design pattern and has therefore only a private constructor.
 **/
LinkManager::LinkManager()
{
        links = QList<LinkInterface*>();
        protocolLinks = QMap<ProtocolInterface*, LinkInterface*>();
}

LinkManager::~LinkManager()
{
    disconnectAll();
}

void LinkManager::add(LinkInterface* link)
{
69
    if(!link) return;
70
    connect(link, SIGNAL(destroyed(QObject*)), this, SLOT(removeLink(QObject*)));
pixhawk's avatar
pixhawk committed
71 72 73 74 75 76 77 78
    links.append(link);
    emit newLink(link);
}

void LinkManager::addProtocol(LinkInterface* link, ProtocolInterface* protocol)
{
    // Connect link to protocol
    // the protocol will receive new bytes from the link
79
    if(!link || !protocol) return;
80
    connect(link, SIGNAL(bytesReceived(LinkInterface*, QByteArray)), protocol, SLOT(receiveBytes(LinkInterface*, QByteArray)));
pixhawk's avatar
pixhawk committed
81
    // Store the connection information in the protocol links map
82 83
    protocolLinks.insertMulti(protocol, link);
    //qDebug() << __FILE__ << __LINE__ << "ADDED LINK TO PROTOCOL" << link->getName() << protocol->getName() << "NEW SIZE OF LINK LIST:" << protocolLinks.size();
pixhawk's avatar
pixhawk committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
}

QList<LinkInterface*> LinkManager::getLinksForProtocol(ProtocolInterface* protocol)
{
    return protocolLinks.values(protocol);
}


bool LinkManager::connectAll()
{
	bool allConnected = true;
	
        foreach (LinkInterface* link, links)
        {
98 99
            if(!link) {}
            else if(!link->connect()) allConnected = false;
pixhawk's avatar
pixhawk committed
100 101 102 103 104 105 106 107 108 109 110
	}
	
	return allConnected;
}

bool LinkManager::disconnectAll()
{
	bool allDisconnected = true;
	
        foreach (LinkInterface* link, links)
        {
lm's avatar
lm committed
111
            //static int i=0;
112 113
            if(!link){}
            else if(!link->disconnect()) allDisconnected = false;
pixhawk's avatar
pixhawk committed
114 115 116 117 118 119 120
	}
	
	return allDisconnected;
}

bool LinkManager::connectLink(LinkInterface* link)
{
121
        if(!link) return false;
pixhawk's avatar
pixhawk committed
122 123 124 125 126
	return link->connect();
}

bool LinkManager::disconnectLink(LinkInterface* link)
{
127
        if(!link) return false;
pixhawk's avatar
pixhawk committed
128 129 130
	return link->disconnect();
}

131 132 133 134 135 136 137 138 139 140 141
void LinkManager::removeLink(QObject* link)
{
    LinkInterface* linkInterface = dynamic_cast<LinkInterface*>(link);
    // Add link to link list
    if (links.contains(linkInterface))
    {
        int linkIndex = links.indexOf(linkInterface);
        links.removeAt(linkIndex);
    }
}

142 143 144 145 146 147 148 149 150 151 152
bool LinkManager::removeLink(LinkInterface* link)
{
        if(link)
        {
            for (int i=0; i < QList<LinkInterface*>(links).size(); i++)
            {
                if(link==links.at(i))
                {
                    links.removeAt(i); //remove from link list
                }
            }
lm's avatar
lm committed
153
            return true;
154
        }
lm's avatar
lm committed
155
        return false;
156 157
}

pixhawk's avatar
pixhawk committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/**
 * The access time is linear in the number of links.
 *
 * @param id link identifier to search for
 * @return A pointer to the link or NULL if not found
 */
LinkInterface* LinkManager::getLinkForId(int id)
{
    foreach (LinkInterface* link, links)
    {
        if (link->getId() == id) return link;
    }
    return NULL;
}

/**
 *
 */
const QList<LinkInterface*> LinkManager::getLinks()
{
    return QList<LinkInterface*>(links);
}