LinkManager.cc 6.43 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 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>

39 40 41 42 43 44 45 46 47 48 49
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;
pixhawk's avatar
pixhawk committed
50 51 52 53
}

/**
 * @brief Private singleton constructor
54
 *
pixhawk's avatar
pixhawk committed
55 56 57 58
 * This class implements the singleton design pattern and has therefore only a private constructor.
 **/
LinkManager::LinkManager()
{
59 60
    links = QList<LinkInterface*>();
    protocolLinks = QMap<ProtocolInterface*, LinkInterface*>();
pixhawk's avatar
pixhawk committed
61 62 63 64 65
}

LinkManager::~LinkManager()
{
    disconnectAll();
66
    dataMutex.lock();
Lorenz Meier's avatar
Lorenz Meier committed
67 68 69 70
    foreach (LinkInterface* link, links)
    {
        if(link) link->deleteLater();
    }
71
    dataMutex.unlock();
pixhawk's avatar
pixhawk committed
72 73 74 75
}

void LinkManager::add(LinkInterface* link)
{
76
    dataMutex.lock();
Lorenz Meier's avatar
Lorenz Meier committed
77 78
    if (!links.contains(link))
    {
79
        if(!link) return;
80
        connect(link, SIGNAL(destroyed(QObject*)), this, SLOT(removeObj(QObject*)));
81
        links.append(link);
82
        dataMutex.unlock();
83
        emit newLink(link);
84 85
    } else {
        dataMutex.unlock();
86
    }
pixhawk's avatar
pixhawk committed
87 88 89 90 91 92
}

void LinkManager::addProtocol(LinkInterface* link, ProtocolInterface* protocol)
{
    // Connect link to protocol
    // the protocol will receive new bytes from the link
Lorenz Meier's avatar
Lorenz Meier committed
93
    if (!link || !protocol) return;
94

95
    dataMutex.lock();
96 97 98 99
    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
100
    if (!linkList.contains(link))
Lorenz Meier's avatar
Lorenz Meier committed
101
    {
102 103
        // Protocol is new, add
        connect(link, SIGNAL(bytesReceived(LinkInterface*, QByteArray)), protocol, SLOT(receiveBytes(LinkInterface*, QByteArray)));
104 105
        // Add status
        connect(link, SIGNAL(connected(bool)), protocol, SLOT(linkStatusChanged(bool)));
106 107
        // Store the connection information in the protocol links map
        protocolLinks.insertMulti(protocol, link);
108
        dataMutex.unlock();
109 110
        // Make sure the protocol clears its metadata for this link.
        protocol->resetMetadataForLink(link);
111 112
    } else {
        dataMutex.unlock();
113
    }
114
    //qDebug() << __FILE__ << __LINE__ << "ADDED LINK TO PROTOCOL" << link->getName() << protocol->getName() << "NEW SIZE OF LINK LIST:" << protocolLinks.size();
pixhawk's avatar
pixhawk committed
115 116 117 118
}

QList<LinkInterface*> LinkManager::getLinksForProtocol(ProtocolInterface* protocol)
{
119 120 121 122
    dataMutex.lock();
    QList<LinkInterface*> links = protocolLinks.values(protocol);
    dataMutex.unlock();
    return links;
pixhawk's avatar
pixhawk committed
123 124
}

125 126
ProtocolInterface* LinkManager::getProtocolForLink(LinkInterface* link)
{
127 128 129 130
    dataMutex.lock();
    ProtocolInterface* interface = protocolLinks.key(link);
    dataMutex.unlock();
    return interface;
131
}
pixhawk's avatar
pixhawk committed
132 133 134

bool LinkManager::connectAll()
{
135 136
    bool allConnected = true;

137
    dataMutex.lock();
Lorenz Meier's avatar
Lorenz Meier committed
138 139
    foreach (LinkInterface* link, links)
    {
140 141 142
        if(!link) {}
        else if(!link->connect()) allConnected = false;
    }
143
    dataMutex.unlock();
144 145

    return allConnected;
pixhawk's avatar
pixhawk committed
146 147 148 149
}

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

152
    dataMutex.lock();
Lorenz Meier's avatar
Lorenz Meier committed
153 154
    foreach (LinkInterface* link, links)
    {
155 156 157 158
        //static int i=0;
        if(!link) {}
        else if(!link->disconnect()) allDisconnected = false;
    }
159
    dataMutex.unlock();
160 161

    return allDisconnected;
pixhawk's avatar
pixhawk committed
162 163 164 165
}

bool LinkManager::connectLink(LinkInterface* link)
{
166 167
    if(!link) return false;
    return link->connect();
pixhawk's avatar
pixhawk committed
168 169 170 171
}

bool LinkManager::disconnectLink(LinkInterface* link)
{
172 173
    if(!link) return false;
    return link->disconnect();
pixhawk's avatar
pixhawk committed
174 175
}

176
void LinkManager::removeObj(QObject* link)
177 178
{
    LinkInterface* linkInterface = dynamic_cast<LinkInterface*>(link);
Lorenz Meier's avatar
Lorenz Meier committed
179 180
    if (linkInterface)
    {
181
        removeLink(linkInterface);
182 183 184
    }
}

185 186
bool LinkManager::removeLink(LinkInterface* link)
{
Lorenz Meier's avatar
Lorenz Meier committed
187 188
    if(link)
    {
189
        dataMutex.lock();
Lorenz Meier's avatar
Lorenz Meier committed
190 191 192 193
        for (int i=0; i < QList<LinkInterface*>(links).size(); i++)
        {
            if(link==links.at(i))
            {
194
                links.removeAt(i); //remove from link list
195 196
            }
        }
197 198
        // Remove link from protocol map
        QList<ProtocolInterface* > protocols = protocolLinks.keys(link);
Lorenz Meier's avatar
Lorenz Meier committed
199 200
        foreach (ProtocolInterface* proto, protocols)
        {
201 202
            protocolLinks.remove(proto, link);
        }
203
        dataMutex.unlock();
204 205 206 207

        // Emit removal of link
        emit linkRemoved(link);

208 209 210
        return true;
    }
    return false;
211 212
}

pixhawk's avatar
pixhawk committed
213 214 215 216 217 218 219 220
/**
 * 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)
{
221 222
    dataMutex.lock();
    LinkInterface* linkret = NULL;
Lorenz Meier's avatar
Lorenz Meier committed
223 224
    foreach (LinkInterface* link, links)
    {
225 226 227 228
        if (link->getId() == id)
        {
            linkret = link;
        }
pixhawk's avatar
pixhawk committed
229
    }
230 231
    dataMutex.unlock();
    return linkret;
pixhawk's avatar
pixhawk committed
232 233 234 235 236 237 238
}

/**
 *
 */
const QList<LinkInterface*> LinkManager::getLinks()
{
239 240 241 242
    dataMutex.lock();
    QList<LinkInterface*> ret(links);
    dataMutex.unlock();
    return ret;
pixhawk's avatar
pixhawk committed
243
}
244 245 246

const QList<SerialLink*> LinkManager::getSerialLinks()
{
247
    dataMutex.lock();
248 249 250 251 252 253 254 255 256
    QList<SerialLink*> s;

    foreach (LinkInterface* i, links)
    {
        SerialLink* link = qobject_cast<SerialLink*>(i);

        if (link)
            s.append(link);
    }
257
    dataMutex.unlock();
258 259 260

    return s;
}