LinkManager.cc 35.4 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
lm's avatar
lm committed
9

pixhawk's avatar
pixhawk committed
10 11
#include <QList>
#include <QApplication>
12
#include <QDebug>
13
#include <QSignalSpy>
dogmaphobic's avatar
dogmaphobic committed
14

Gus Grubba's avatar
Gus Grubba committed
15
#ifndef NO_SERIAL_LINK
Don Gagne's avatar
Don Gagne committed
16
#include "QGCSerialPortInfo.h"
dogmaphobic's avatar
dogmaphobic committed
17
#endif
18

19
#include "LinkManager.h"
20
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
21 22
#include "UDPLink.h"
#include "TCPLink.h"
23
#include "SettingsManager.h"
dogmaphobic's avatar
dogmaphobic committed
24
#ifdef QGC_ENABLE_BLUETOOTH
dogmaphobic's avatar
dogmaphobic committed
25 26
#include "BluetoothLink.h"
#endif
27

Don Gagne's avatar
Don Gagne committed
28
#ifndef __mobile__
DonLakeFlyer's avatar
DonLakeFlyer committed
29
#include "GPSManager.h"
30
#include "PositionManager.h"
Don Gagne's avatar
Don Gagne committed
31 32
#endif

Don Gagne's avatar
Don Gagne committed
33
QGC_LOGGING_CATEGORY(LinkManagerLog, "LinkManagerLog")
Don Gagne's avatar
Don Gagne committed
34 35
QGC_LOGGING_CATEGORY(LinkManagerVerboseLog, "LinkManagerVerboseLog")

Don Gagne's avatar
Don Gagne committed
36
const char* LinkManager::_defaultUPDLinkName =       "UDP Link (AutoConnect)";
37

38 39 40 41 42 43 44 45
const int LinkManager::_autoconnectUpdateTimerMSecs =   1000;
#ifdef Q_OS_WIN
// Have to manually let the bootloader go by on Windows to get a working connect
const int LinkManager::_autoconnectConnectDelayMSecs =  6000;
#else
const int LinkManager::_autoconnectConnectDelayMSecs =  1000;
#endif

46 47
LinkManager::LinkManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
48 49 50
    , _configUpdateSuspended(false)
    , _configurationsLoaded(false)
    , _connectionsSuspended(false)
51
    , _mavlinkChannelsUsedBitMask(1)    // We never use channel 0 to avoid sequence numbering problems
52
    , _autoConnectSettings(NULL)
53
    , _mavlinkProtocol(NULL)
54
#ifndef __mobile__
55
    , _nmeaPort(NULL)
56
#endif
pixhawk's avatar
pixhawk committed
57
{
Don Gagne's avatar
Don Gagne committed
58 59 60 61
    qmlRegisterUncreatableType<LinkManager>         ("QGroundControl", 1, 0, "LinkManager",         "Reference only");
    qmlRegisterUncreatableType<LinkConfiguration>   ("QGroundControl", 1, 0, "LinkConfiguration",   "Reference only");
    qmlRegisterUncreatableType<LinkInterface>       ("QGroundControl", 1, 0, "LinkInterface",       "Reference only");

Gus Grubba's avatar
Gus Grubba committed
62
#ifndef NO_SERIAL_LINK
Don Gagne's avatar
Don Gagne committed
63 64 65
    _activeLinkCheckTimer.setInterval(_activeLinkCheckTimeoutMSecs);
    _activeLinkCheckTimer.setSingleShot(false);
    connect(&_activeLinkCheckTimer, &QTimer::timeout, this, &LinkManager::_activeLinkCheck);
Don Gagne's avatar
Don Gagne committed
66
#endif
pixhawk's avatar
pixhawk committed
67 68 69 70
}

LinkManager::~LinkManager()
{
71
#ifndef __mobile__
72
    delete _nmeaPort;
73
#endif
pixhawk's avatar
pixhawk committed
74 75
}

76 77
void LinkManager::setToolbox(QGCToolbox *toolbox)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
78
    QGCTool::setToolbox(toolbox);
79

DonLakeFlyer's avatar
DonLakeFlyer committed
80 81
    _autoConnectSettings = toolbox->settingsManager()->autoConnectSettings();
    _mavlinkProtocol = _toolbox->mavlinkProtocol();
82

83 84
    connect(_mavlinkProtocol, &MAVLinkProtocol::vehicleHeartbeatInfo, this, &LinkManager::_heartbeatReceived);

Don Gagne's avatar
Don Gagne committed
85
    connect(&_portListTimer, &QTimer::timeout, this, &LinkManager::_updateAutoConnectLinks);
86
    _portListTimer.start(_autoconnectUpdateTimerMSecs); // timeout must be long enough to get past bootloader on second pass
87

88 89
}

Don Gagne's avatar
Don Gagne committed
90 91 92 93 94 95 96 97 98 99
// This should only be used by Qml code
void LinkManager::createConnectedLink(LinkConfiguration* config)
{
    for(int i = 0; i < _sharedConfigurations.count(); i++) {
        SharedLinkConfigurationPointer& sharedConf = _sharedConfigurations[i];
        if (sharedConf->name() == config->name())
            createConnectedLink(sharedConf);
    }
}

100
LinkInterface* LinkManager::createConnectedLink(SharedLinkConfigurationPointer& config)
101
{
102 103 104 105 106
    if (!config) {
        qWarning() << "LinkManager::createConnectedLink called with NULL config";
        return NULL;
    }

107 108
    LinkInterface* pLink = NULL;
    switch(config->type()) {
Gus Grubba's avatar
Gus Grubba committed
109
#ifndef NO_SERIAL_LINK
DonLakeFlyer's avatar
DonLakeFlyer committed
110 111 112 113 114 115 116 117 118
    case LinkConfiguration::TypeSerial:
    {
        SerialConfiguration* serialConfig = dynamic_cast<SerialConfiguration*>(config.data());
        if (serialConfig) {
            pLink = new SerialLink(config);
            if (serialConfig->usbDirect()) {
                _activeLinkCheckList.append((SerialLink*)pLink);
                if (!_activeLinkCheckTimer.isActive()) {
                    _activeLinkCheckTimer.start();
Don Gagne's avatar
Don Gagne committed
119 120 121
                }
            }
        }
DonLakeFlyer's avatar
DonLakeFlyer committed
122
    }
Don Gagne's avatar
Don Gagne committed
123
        break;
dogmaphobic's avatar
dogmaphobic committed
124
#endif
DonLakeFlyer's avatar
DonLakeFlyer committed
125 126 127 128 129 130
    case LinkConfiguration::TypeUdp:
        pLink = new UDPLink(config);
        break;
    case LinkConfiguration::TypeTcp:
        pLink = new TCPLink(config);
        break;
dogmaphobic's avatar
dogmaphobic committed
131
#ifdef QGC_ENABLE_BLUETOOTH
DonLakeFlyer's avatar
DonLakeFlyer committed
132 133 134
    case LinkConfiguration::TypeBluetooth:
        pLink = new BluetoothLink(config);
        break;
dogmaphobic's avatar
dogmaphobic committed
135
#endif
dogmaphobic's avatar
dogmaphobic committed
136
#ifndef __mobile__
DonLakeFlyer's avatar
DonLakeFlyer committed
137 138 139
    case LinkConfiguration::TypeLogReplay:
        pLink = new LogReplayLink(config);
        break;
dogmaphobic's avatar
dogmaphobic committed
140
#endif
141
#ifdef QT_DEBUG
DonLakeFlyer's avatar
DonLakeFlyer committed
142 143 144
    case LinkConfiguration::TypeMock:
        pLink = new MockLink(config);
        break;
145
#endif
DonLakeFlyer's avatar
DonLakeFlyer committed
146 147 148
    case LinkConfiguration::TypeLast:
    default:
        break;
149
    }
150 151

    if (pLink) {
152 153
        _addLink(pLink);
        connectLink(pLink);
154
    }
155

156 157 158
    return pLink;
}

Don Gagne's avatar
Don Gagne committed
159
LinkInterface* LinkManager::createConnectedLink(const QString& name)
160
{
DonLakeFlyer's avatar
DonLakeFlyer committed
161 162 163 164 165 166 167 168 169
    if (name.isEmpty()) {
        qWarning() << "Internal error";
    } else {
        for(int i = 0; i < _sharedConfigurations.count(); i++) {
            SharedLinkConfigurationPointer& conf = _sharedConfigurations[i];
            if (conf->name() == name) {
                return createConnectedLink(conf);
            }
        }
170 171 172 173
    }
    return NULL;
}

174
void LinkManager::_addLink(LinkInterface* link)
pixhawk's avatar
pixhawk committed
175
{
Don Gagne's avatar
Don Gagne committed
176 177 178 179
    if (thread() != QThread::currentThread()) {
        qWarning() << "_deleteLink called from incorrect thread";
        return;
    }
180

Don Gagne's avatar
Don Gagne committed
181 182 183
    if (!link) {
        return;
    }
184

185
    if (!containsLink(link)) {
186 187 188 189
        int mavlinkChannel = _reserveMavlinkChannel();
        if (mavlinkChannel != 0) {
            link->_setMavlinkChannel(mavlinkChannel);
        } else {
190
            qWarning() << "Ran out of mavlink channels";
191
            return;
192 193
        }

194
        _sharedLinks.append(SharedLinkInterfacePointer(link));
195 196
        emit newLink(link);
    }
197

Don Gagne's avatar
Don Gagne committed
198 199
    connect(link, &LinkInterface::communicationError,   _app,               &QGCApplication::criticalMessageBoxOnMainThread);
    connect(link, &LinkInterface::bytesReceived,        _mavlinkProtocol,   &MAVLinkProtocol::receiveBytes);
200

201
    _mavlinkProtocol->resetMetadataForLink(link);
202
    _mavlinkProtocol->setVersion(_mavlinkProtocol->getCurrentVersion());
203

204 205 206 207 208 209
    connect(link, &LinkInterface::connected,            this, &LinkManager::_linkConnected);
    connect(link, &LinkInterface::disconnected,         this, &LinkManager::_linkDisconnected);

    // This connection is queued since it will cloe the link. So we want the link emitter to return otherwise we would
    // close the link our from under itself.
    connect(link, &LinkInterface::connectionRemoved,    this, &LinkManager::_linkConnectionRemoved, Qt::QueuedConnection);
210
}
pixhawk's avatar
pixhawk committed
211

Don Gagne's avatar
Don Gagne committed
212
void LinkManager::disconnectAll(void)
pixhawk's avatar
pixhawk committed
213
{
Don Gagne's avatar
Don Gagne committed
214
    // Walk list in reverse order to preserve indices during delete
215 216
    for (int i=_sharedLinks.count()-1; i>=0; i--) {
        disconnectLink(_sharedLinks[i].data());
217
    }
pixhawk's avatar
pixhawk committed
218 219 220 221
}

bool LinkManager::connectLink(LinkInterface* link)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
222 223 224 225 226 227 228
    if (link) {
        if (_connectionsSuspendedMsg()) {
            return false;
        }
        return link->_connect();
    } else {
        qWarning() << "Internal error";
229 230
        return false;
    }
pixhawk's avatar
pixhawk committed
231 232
}

Don Gagne's avatar
Don Gagne committed
233
void LinkManager::disconnectLink(LinkInterface* link)
pixhawk's avatar
pixhawk committed
234
{
235
    if (!link || !containsLink(link)) {
236 237
        return;
    }
Don Gagne's avatar
Don Gagne committed
238

Don Gagne's avatar
Don Gagne committed
239
    link->_disconnect();
240

Don Gagne's avatar
Don Gagne committed
241
    LinkConfiguration* config = link->getLinkConfiguration();
242 243 244 245 246
    for (int i=0; i<_sharedAutoconnectConfigurations.count(); i++) {
        if (_sharedAutoconnectConfigurations[i].data() == config) {
            qCDebug(LinkManagerLog) << "Removing disconnected autoconnect config" << config->name();
            _sharedAutoconnectConfigurations.removeAt(i);
            break;
247
        }
248
    }
249

Don Gagne's avatar
Don Gagne committed
250
    _deleteLink(link);
pixhawk's avatar
pixhawk committed
251 252
}

253
void LinkManager::_deleteLink(LinkInterface* link)
254
{
Don Gagne's avatar
Don Gagne committed
255 256 257 258 259 260 261 262
    if (thread() != QThread::currentThread()) {
        qWarning() << "_deleteLink called from incorrect thread";
        return;
    }

    if (!link) {
        return;
    }
263

264
    // Free up the mavlink channel associated with this link
265
    _freeMavlinkChannel(link->mavlinkChannel());
266

267 268 269 270 271 272
    for (int i=0; i<_sharedLinks.count(); i++) {
        if (_sharedLinks[i].data() == link) {
            _sharedLinks.removeAt(i);
            break;
        }
    }
273

Don Gagne's avatar
Don Gagne committed
274
    // Emit removal of link
275
    emit linkDeleted(link);
pixhawk's avatar
pixhawk committed
276 277
}

278 279 280 281 282 283 284 285 286 287 288 289
SharedLinkInterfacePointer LinkManager::sharedLinkInterfacePointerForLink(LinkInterface* link)
{
    for (int i=0; i<_sharedLinks.count(); i++) {
        if (_sharedLinks[i].data() == link) {
            return _sharedLinks[i];
        }
    }

    qWarning() << "LinkManager::sharedLinkInterfaceForLink returning NULL";
    return SharedLinkInterfacePointer(NULL);
}

290 291 292 293 294
/// @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) {
295
        qgcApp()->showMessage(tr("Connect not allowed: %1").arg(_connectionsSuspendedReason));
296 297 298 299 300 301 302 303 304 305 306
        return true;
    } else {
        return false;
    }
}

void LinkManager::setConnectionsSuspended(QString reason)
{
    _connectionsSuspended = true;
    _connectionsSuspendedReason = reason;
}
307

308 309 310 311 312 313 314 315 316
void LinkManager::_linkConnected(void)
{
    emit linkConnected((LinkInterface*)sender());
}

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

318 319 320 321 322 323
void LinkManager::_linkConnectionRemoved(LinkInterface* link)
{
    // Link has been removed from system, disconnect it automatically
    disconnectLink(link);
}

324 325 326 327 328 329 330 331 332
void LinkManager::suspendConfigurationUpdates(bool suspend)
{
    _configUpdateSuspended = suspend;
}

void LinkManager::saveLinkConfigurationList()
{
    QSettings settings;
    settings.remove(LinkConfiguration::settingsRoot());
333
    int trueCount = 0;
334 335
    for (int i = 0; i < _sharedConfigurations.count(); i++) {
        SharedLinkConfigurationPointer linkConfig = _sharedConfigurations[i];
Don Gagne's avatar
Don Gagne committed
336
        if (linkConfig) {
337
            if (!linkConfig->isDynamic()) {
Don Gagne's avatar
Don Gagne committed
338
                QString root = LinkConfiguration::settingsRoot();
339
                root += QString("/Link%1").arg(trueCount++);
Don Gagne's avatar
Don Gagne committed
340 341
                settings.setValue(root + "/name", linkConfig->name());
                settings.setValue(root + "/type", linkConfig->type());
342
                settings.setValue(root + "/auto", linkConfig->isAutoConnect());
343
                settings.setValue(root + "/high_latency", linkConfig->isHighLatency());
Don Gagne's avatar
Don Gagne committed
344 345 346 347
                // Have the instance save its own values
                linkConfig->saveSettings(settings, root);
            }
        } else {
348
            qWarning() << "Internal error for link configuration in LinkManager";
dogmaphobic's avatar
dogmaphobic committed
349
        }
350
    }
dogmaphobic's avatar
dogmaphobic committed
351
    QString root(LinkConfiguration::settingsRoot());
352 353
    settings.setValue(root + "/count", trueCount);
    emit linkConfigurationsChanged();
354 355 356 357
}

void LinkManager::loadLinkConfigurationList()
{
358
    bool linksChanged = false;
359 360 361 362 363 364 365 366 367 368
    QSettings settings;
    // Is the group even there?
    if(settings.contains(LinkConfiguration::settingsRoot() + "/count")) {
        // Find out how many configurations we have
        int count = settings.value(LinkConfiguration::settingsRoot() + "/count").toInt();
        for(int i = 0; i < count; i++) {
            QString root(LinkConfiguration::settingsRoot());
            root += QString("/Link%1").arg(i);
            if(settings.contains(root + "/type")) {
                int type = settings.value(root + "/type").toInt();
369
                if((LinkConfiguration::LinkType)type < LinkConfiguration::TypeLast) {
370 371 372 373
                    if(settings.contains(root + "/name")) {
                        QString name = settings.value(root + "/name").toString();
                        if(!name.isEmpty()) {
                            LinkConfiguration* pLink = NULL;
374
                            bool autoConnect = settings.value(root + "/auto").toBool();
375
                            bool highLatency = settings.value(root + "/high_latency").toBool();
376
                            switch((LinkConfiguration::LinkType)type) {
Gus Grubba's avatar
Gus Grubba committed
377
#ifndef NO_SERIAL_LINK
DonLakeFlyer's avatar
DonLakeFlyer committed
378 379 380
                            case LinkConfiguration::TypeSerial:
                                pLink = (LinkConfiguration*)new SerialConfiguration(name);
                                break;
dogmaphobic's avatar
dogmaphobic committed
381
#endif
DonLakeFlyer's avatar
DonLakeFlyer committed
382 383 384 385 386 387
                            case LinkConfiguration::TypeUdp:
                                pLink = (LinkConfiguration*)new UDPConfiguration(name);
                                break;
                            case LinkConfiguration::TypeTcp:
                                pLink = (LinkConfiguration*)new TCPConfiguration(name);
                                break;
dogmaphobic's avatar
dogmaphobic committed
388
#ifdef QGC_ENABLE_BLUETOOTH
DonLakeFlyer's avatar
DonLakeFlyer committed
389 390 391
                            case LinkConfiguration::TypeBluetooth:
                                pLink = (LinkConfiguration*)new BluetoothConfiguration(name);
                                break;
dogmaphobic's avatar
dogmaphobic committed
392
#endif
dogmaphobic's avatar
dogmaphobic committed
393
#ifndef __mobile__
DonLakeFlyer's avatar
DonLakeFlyer committed
394 395 396
                            case LinkConfiguration::TypeLogReplay:
                                pLink = (LinkConfiguration*)new LogReplayLinkConfiguration(name);
                                break;
dogmaphobic's avatar
dogmaphobic committed
397
#endif
398
#ifdef QT_DEBUG
DonLakeFlyer's avatar
DonLakeFlyer committed
399 400 401
                            case LinkConfiguration::TypeMock:
                                pLink = (LinkConfiguration*)new MockConfiguration(name);
                                break;
402
#endif
DonLakeFlyer's avatar
DonLakeFlyer committed
403 404 405
                            default:
                            case LinkConfiguration::TypeLast:
                                break;
406 407
                            }
                            if(pLink) {
408 409
                                //-- Have the instance load its own values
                                pLink->setAutoConnect(autoConnect);
410
                                pLink->setHighLatency(highLatency);
411
                                pLink->loadSettings(settings, root);
412
                                addConfiguration(pLink);
413
                                linksChanged = true;
414 415
                            }
                        } else {
416
                            qWarning() << "Link Configuration" << root << "has an empty name." ;
417 418
                        }
                    } else {
419
                        qWarning() << "Link Configuration" << root << "has no name." ;
420 421
                    }
                } else {
422
                    qWarning() << "Link Configuration" << root << "an invalid type: " << type;
423 424
                }
            } else {
425
                qWarning() << "Link Configuration" << root << "has no type." ;
426 427 428
            }
        }
    }
429 430

    if(linksChanged) {
431
        emit linkConfigurationsChanged();
432 433
    }
    // Enable automatic Serial PX4/3DR Radio hunting
434 435 436
    _configurationsLoaded = true;
}

Gus Grubba's avatar
Gus Grubba committed
437
#ifndef NO_SERIAL_LINK
Don Gagne's avatar
Don Gagne committed
438
SerialConfiguration* LinkManager::_autoconnectConfigurationsContainsPort(const QString& portName)
439 440
{
    QString searchPort = portName.trimmed();
Don Gagne's avatar
Don Gagne committed
441

442 443
    for (int i=0; i<_sharedAutoconnectConfigurations.count(); i++) {
        SerialConfiguration* serialConfig = qobject_cast<SerialConfiguration*>(_sharedAutoconnectConfigurations[i].data());
Don Gagne's avatar
Don Gagne committed
444

445 446 447
        if (serialConfig) {
            if (serialConfig->portName() == searchPort) {
                return serialConfig;
448
            }
Don Gagne's avatar
Don Gagne committed
449 450
        } else {
            qWarning() << "Internal error";
451 452 453 454
        }
    }
    return NULL;
}
dogmaphobic's avatar
dogmaphobic committed
455
#endif
456

Don Gagne's avatar
Don Gagne committed
457
void LinkManager::_updateAutoConnectLinks(void)
458
{
Don Gagne's avatar
Don Gagne committed
459
    if (_connectionsSuspended || qgcApp()->runningUnitTests()) {
460 461
        return;
    }
Don Gagne's avatar
Don Gagne committed
462

Don Gagne's avatar
Don Gagne committed
463 464
    // Re-add UDP if we need to
    bool foundUDP = false;
465 466
    for (int i=0; i<_sharedLinks.count(); i++) {
        LinkConfiguration* linkConfig = _sharedLinks[i]->getLinkConfiguration();
Don Gagne's avatar
Don Gagne committed
467 468 469 470 471
        if (linkConfig->type() == LinkConfiguration::TypeUdp && linkConfig->name() == _defaultUPDLinkName) {
            foundUDP = true;
            break;
        }
    }
472
    if (!foundUDP && _autoConnectSettings->autoConnectUDP()->rawValue().toBool()) {
Don Gagne's avatar
Don Gagne committed
473
        qCDebug(LinkManagerLog) << "New auto-connect UDP port added";
474
        // Default UDPConfiguration is set up for autoconnect
Don Gagne's avatar
Don Gagne committed
475
        UDPConfiguration* udpConfig = new UDPConfiguration(_defaultUPDLinkName);
DonLakeFlyer's avatar
DonLakeFlyer committed
476
        udpConfig->setDynamic(true);
477 478
        SharedLinkConfigurationPointer config = addConfiguration(udpConfig);
        createConnectedLink(config);
479
        emit linkConfigurationsChanged();
Don Gagne's avatar
Don Gagne committed
480 481
    }

Gus Grubba's avatar
Gus Grubba committed
482
#ifndef NO_SERIAL_LINK
dogmaphobic's avatar
dogmaphobic committed
483
    QStringList currentPorts;
484 485 486 487 488 489
    QList<QGCSerialPortInfo> portList;

#ifdef __android__
    // Android builds only support a single serial connection. Repeatedly calling availablePorts after that one serial
    // port is connected leaks file handles due to a bug somewhere in android serial code. In order to work around that
    // bug after we connect the first serial port we stop probing for additional ports.
490
    if (!_sharedAutoconnectConfigurations.count()) {
491 492
        portList = QGCSerialPortInfo::availablePorts();
    }
493 494
#else
    portList = QGCSerialPortInfo::availablePorts();
495
#endif
Don Gagne's avatar
Don Gagne committed
496

497
    // Iterate Comm Ports
Don Gagne's avatar
Don Gagne committed
498
    foreach (QGCSerialPortInfo portInfo, portList) {
Don Gagne's avatar
Don Gagne committed
499 500 501 502 503 504 505 506 507
        qCDebug(LinkManagerVerboseLog) << "-----------------------------------------------------";
        qCDebug(LinkManagerVerboseLog) << "portName:          " << portInfo.portName();
        qCDebug(LinkManagerVerboseLog) << "systemLocation:    " << portInfo.systemLocation();
        qCDebug(LinkManagerVerboseLog) << "description:       " << portInfo.description();
        qCDebug(LinkManagerVerboseLog) << "manufacturer:      " << portInfo.manufacturer();
        qCDebug(LinkManagerVerboseLog) << "serialNumber:      " << portInfo.serialNumber();
        qCDebug(LinkManagerVerboseLog) << "vendorIdentifier:  " << portInfo.vendorIdentifier();
        qCDebug(LinkManagerVerboseLog) << "productIdentifier: " << portInfo.productIdentifier();

dogmaphobic's avatar
dogmaphobic committed
508 509
        // Save port name
        currentPorts << portInfo.systemLocation();
Don Gagne's avatar
Don Gagne committed
510

511 512
        QGCSerialPortInfo::BoardType_t boardType;
        QString boardName;
Don Gagne's avatar
Don Gagne committed
513

514
#ifndef __mobile__
515 516 517 518 519 520 521 522 523 524 525 526
        if (portInfo.systemLocation().trimmed() == _autoConnectSettings->autoConnectNmeaPort()->cookedValueString()) {
            if (portInfo.systemLocation().trimmed() != _nmeaDeviceName) {
                _nmeaDeviceName = portInfo.systemLocation().trimmed();
                qCDebug(LinkManagerLog) << "Configuring nmea port" << _nmeaDeviceName;
                QSerialPort* newPort = new QSerialPort(portInfo);

                _nmeaBaud = _autoConnectSettings->autoConnectNmeaBaud()->cookedValue().toUInt();
                newPort->setBaudRate(_nmeaBaud);
                qCDebug(LinkManagerLog) << "Configuring nmea baudrate" << _nmeaBaud;

                // This will stop polling old device if previously set
                _toolbox->qgcPositionManager()->setNmeaSourceDevice(newPort);
527

528 529 530 531 532 533 534 535 536 537
                if (_nmeaPort) {
                    delete _nmeaPort;
                }
                _nmeaPort = newPort;

            } else if (_autoConnectSettings->autoConnectNmeaBaud()->cookedValue().toUInt() != _nmeaBaud) {
                _nmeaBaud = _autoConnectSettings->autoConnectNmeaBaud()->cookedValue().toUInt();
                _nmeaPort->setBaudRate(_nmeaBaud);
                qCDebug(LinkManagerLog) << "Configuring nmea baudrate" << _nmeaBaud;
            }
538 539 540
        } else
#endif
        if (portInfo.getBoardInfo(boardType, boardName)) {
Don Gagne's avatar
Don Gagne committed
541 542
            if (portInfo.isBootloader()) {
                // Don't connect to bootloader
543
                qCDebug(LinkManagerLog) << "Waiting for bootloader to finish" << portInfo.systemLocation();
Don Gagne's avatar
Don Gagne committed
544 545
                continue;
            }
546

547
            if (_autoconnectConfigurationsContainsPort(portInfo.systemLocation()) || _autoConnectRTKPort == portInfo.systemLocation()) {
548 549 550 551 552 553
                qCDebug(LinkManagerVerboseLog) << "Skipping existing autoconnect" << portInfo.systemLocation();
            } else if (!_autoconnectWaitList.contains(portInfo.systemLocation())) {
                // We don't connect to the port the first time we see it. The ability to correctly detect whether we
                // are in the bootloader is flaky from a cross-platform standpoint. So by putting it on a wait list
                // and only connect on the second pass we leave enough time for the board to boot up.
                qCDebug(LinkManagerLog) << "Waiting for next autoconnect pass" << portInfo.systemLocation();
554 555
                _autoconnectWaitList[portInfo.systemLocation()] = 1;
            } else if (++_autoconnectWaitList[portInfo.systemLocation()] * _autoconnectUpdateTimerMSecs > _autoconnectConnectDelayMSecs) {
Don Gagne's avatar
Don Gagne committed
556 557
                SerialConfiguration* pSerialConfig = NULL;

558
                _autoconnectWaitList.remove(portInfo.systemLocation());
559

Don Gagne's avatar
Don Gagne committed
560
                switch (boardType) {
561
                case QGCSerialPortInfo::BoardTypePixhawk:
562
                    if (_autoConnectSettings->autoConnectPixhawk()->rawValue().toBool()) {
563
                        pSerialConfig = new SerialConfiguration(tr("%1 on %2 (AutoConnect)").arg(boardName).arg(portInfo.portName().trimmed()));
564 565 566
                        pSerialConfig->setUsbDirect(true);
                    }
                    break;
Don Gagne's avatar
Don Gagne committed
567
                case QGCSerialPortInfo::BoardTypePX4Flow:
568
                    if (_autoConnectSettings->autoConnectPX4Flow()->rawValue().toBool()) {
569
                        pSerialConfig = new SerialConfiguration(tr("%1 on %2 (AutoConnect)").arg(boardName).arg(portInfo.portName().trimmed()));
Don Gagne's avatar
Don Gagne committed
570
                    }
Don Gagne's avatar
Don Gagne committed
571
                    break;
572
                case QGCSerialPortInfo::BoardTypeSiKRadio:
573
                    if (_autoConnectSettings->autoConnectSiKRadio()->rawValue().toBool()) {
574
                        pSerialConfig = new SerialConfiguration(tr("%1 on %2 (AutoConnect)").arg(boardName).arg(portInfo.portName().trimmed()));
Don Gagne's avatar
Don Gagne committed
575 576
                    }
                    break;
577
                case QGCSerialPortInfo::BoardTypeOpenPilot:
578
                    if (_autoConnectSettings->autoConnectLibrePilot()->rawValue().toBool()) {
579
                        pSerialConfig = new SerialConfiguration(tr("%1 on %2 (AutoConnect)").arg(boardName).arg(portInfo.portName().trimmed()));
580 581
                    }
                    break;
Don Gagne's avatar
Don Gagne committed
582 583
#ifndef __mobile__
                case QGCSerialPortInfo::BoardTypeRTKGPS:
584
                    if (_autoConnectSettings->autoConnectRTKGPS()->rawValue().toBool() && !_toolbox->gpsManager()->connected()) {
585 586
                        qCDebug(LinkManagerLog) << "RTK GPS auto-connected" << portInfo.portName().trimmed();
                        _autoConnectRTKPort = portInfo.systemLocation();
Don Gagne's avatar
Don Gagne committed
587 588 589 590
                        _toolbox->gpsManager()->connectGPS(portInfo.systemLocation());
                    }
                    break;
#endif
Don Gagne's avatar
Don Gagne committed
591 592
                default:
                    qWarning() << "Internal error";
Don Gagne's avatar
Don Gagne committed
593
                    continue;
dogmaphobic's avatar
dogmaphobic committed
594
                }
Don Gagne's avatar
Don Gagne committed
595

Don Gagne's avatar
Don Gagne committed
596 597
                if (pSerialConfig) {
                    qCDebug(LinkManagerLog) << "New auto-connect port added: " << pSerialConfig->name() << portInfo.systemLocation();
598
                    pSerialConfig->setBaud(boardType == QGCSerialPortInfo::BoardTypeSiKRadio ? 57600 : 115200);
Don Gagne's avatar
Don Gagne committed
599 600
                    pSerialConfig->setDynamic(true);
                    pSerialConfig->setPortName(portInfo.systemLocation());
601 602
                    _sharedAutoconnectConfigurations.append(SharedLinkConfigurationPointer(pSerialConfig));
                    createConnectedLink(_sharedAutoconnectConfigurations.last());
Don Gagne's avatar
Don Gagne committed
603
                }
dogmaphobic's avatar
dogmaphobic committed
604 605 606
            }
        }
    }
Don Gagne's avatar
Don Gagne committed
607

608 609 610 611 612 613 614
#ifndef __android__
    // Android builds only support a single serial connection. Repeatedly calling availablePorts after that one serial
    // port is connected leaks file handles due to a bug somewhere in android serial code. In order to work around that
    // bug after we connect the first serial port we stop probing for additional ports. The means we must rely on
    // the port disconnecting itself when the radio is pulled to signal communication list as opposed to automatically
    // closing the Link.

dogmaphobic's avatar
dogmaphobic committed
615 616
    // Now we go through the current configuration list and make sure any dynamic config has gone away
    QList<LinkConfiguration*>  _confToDelete;
617 618 619 620 621 622 623
    for (int i=0; i<_sharedAutoconnectConfigurations.count(); i++) {
        SerialConfiguration* serialConfig = qobject_cast<SerialConfiguration*>(_sharedAutoconnectConfigurations[i].data());
        if (serialConfig) {
            if (!currentPorts.contains(serialConfig->portName())) {
                if (serialConfig->link()) {
                    if (serialConfig->link()->isConnected()) {
                        if (serialConfig->link()->active()) {
624 625 626 627 628 629
                            // We don't remove links which are still connected which have been active with a vehicle on them
                            // even though at this point the cable may have been pulled. Instead we wait for the user to
                            // Disconnect. Once the user disconnects, the link will be removed.
                            continue;
                        }
                    }
Don Gagne's avatar
Don Gagne committed
630
                }
631
                _confToDelete.append(serialConfig);
dogmaphobic's avatar
dogmaphobic committed
632
            }
Don Gagne's avatar
Don Gagne committed
633 634
        } else {
            qWarning() << "Internal error";
dogmaphobic's avatar
dogmaphobic committed
635 636
        }
    }
Don Gagne's avatar
Don Gagne committed
637

Don Gagne's avatar
Don Gagne committed
638
    // Now remove all configs that are gone
Don Gagne's avatar
Don Gagne committed
639
    foreach (LinkConfiguration* pDeleteConfig, _confToDelete) {
Don Gagne's avatar
Don Gagne committed
640
        qCDebug(LinkManagerLog) << "Removing unused autoconnect config" << pDeleteConfig->name();
Don Gagne's avatar
Don Gagne committed
641 642 643
        if (pDeleteConfig->link()) {
            disconnectLink(pDeleteConfig->link());
        }
644 645 646 647 648 649
        for (int i=0; i<_sharedAutoconnectConfigurations.count(); i++) {
            if (_sharedAutoconnectConfigurations[i].data() == pDeleteConfig) {
                _sharedAutoconnectConfigurations.removeAt(i);
                break;
            }
        }
650
    }
651 652

    // Check for RTK GPS connection gone
653
#if !defined(__mobile__)
654 655 656 657 658
    if (!_autoConnectRTKPort.isEmpty() && !currentPorts.contains(_autoConnectRTKPort)) {
        qCDebug(LinkManagerLog) << "RTK GPS disconnected" << _autoConnectRTKPort;
        _toolbox->gpsManager()->disconnectGPS();
        _autoConnectRTKPort.clear();
    }
659
#endif
660

661
#endif
Gus Grubba's avatar
Gus Grubba committed
662
#endif // NO_SERIAL_LINK
663 664
}

Don Gagne's avatar
Don Gagne committed
665 666
void LinkManager::shutdown(void)
{
667
    setConnectionsSuspended(tr("Shutdown"));
Don Gagne's avatar
Don Gagne committed
668
    disconnectAll();
Don Gagne's avatar
Don Gagne committed
669 670
}

671 672 673 674 675 676
QStringList LinkManager::linkTypeStrings(void) const
{
    //-- Must follow same order as enum LinkType in LinkConfiguration.h
    static QStringList list;
    if(!list.size())
    {
Gus Grubba's avatar
Gus Grubba committed
677
#ifndef NO_SERIAL_LINK
678 679 680 681
        list += "Serial";
#endif
        list += "UDP";
        list += "TCP";
dogmaphobic's avatar
dogmaphobic committed
682
#ifdef QGC_ENABLE_BLUETOOTH
dogmaphobic's avatar
dogmaphobic committed
683 684
        list += "Bluetooth";
#endif
dogmaphobic's avatar
dogmaphobic committed
685
#ifdef QT_DEBUG
686
        list += "Mock Link";
dogmaphobic's avatar
dogmaphobic committed
687 688
#endif
#ifndef __mobile__
689
        list += "Log Replay";
dogmaphobic's avatar
dogmaphobic committed
690
#endif
DonLakeFlyer's avatar
DonLakeFlyer committed
691 692 693
        if (list.size() != (int)LinkConfiguration::TypeLast) {
            qWarning() << "Internal error";
        }
694 695 696 697
    }
    return list;
}

698
void LinkManager::_updateSerialPorts()
699
{
700 701
    _commPortList.clear();
    _commPortDisplayList.clear();
Gus Grubba's avatar
Gus Grubba committed
702
#ifndef NO_SERIAL_LINK
703 704
    QList<QSerialPortInfo> portList = QSerialPortInfo::availablePorts();
    foreach (const QSerialPortInfo &info, portList)
705
    {
706 707 708
        QString port = info.systemLocation().trimmed();
        _commPortList += port;
        _commPortDisplayList += SerialConfiguration::cleanPortDisplayname(port);
709 710
    }
#endif
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
}

QStringList LinkManager::serialPortStrings(void)
{
    if(!_commPortDisplayList.size())
    {
        _updateSerialPorts();
    }
    return _commPortDisplayList;
}

QStringList LinkManager::serialPorts(void)
{
    if(!_commPortList.size())
    {
        _updateSerialPorts();
    }
728 729 730 731 732
    return _commPortList;
}

QStringList LinkManager::serialBaudRates(void)
{
Gus Grubba's avatar
Gus Grubba committed
733
#ifdef NO_SERIAL_LINK
734 735 736 737 738 739
    QStringList foo;
    return foo;
#else
    return SerialConfiguration::supportedBaudRates();
#endif
}
740 741 742

bool LinkManager::endConfigurationEditing(LinkConfiguration* config, LinkConfiguration* editedConfig)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
743 744 745 746 747 748 749 750 751 752 753
    if (config && editedConfig) {
        _fixUnnamed(editedConfig);
        config->copyFrom(editedConfig);
        saveLinkConfigurationList();
        // Tell link about changes (if any)
        config->updateSettings();
        // Discard temporary duplicate
        delete editedConfig;
    } else {
        qWarning() << "Internal error";
    }
754 755 756 757 758
    return true;
}

bool LinkManager::endCreateConfiguration(LinkConfiguration* config)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
759 760 761 762 763 764 765
    if (config) {
        _fixUnnamed(config);
        addConfiguration(config);
        saveLinkConfigurationList();
    } else {
        qWarning() << "Internal error";
    }
766 767 768 769 770
    return true;
}

LinkConfiguration* LinkManager::createConfiguration(int type, const QString& name)
{
Gus Grubba's avatar
Gus Grubba committed
771
#ifndef NO_SERIAL_LINK
772 773
    if((LinkConfiguration::LinkType)type == LinkConfiguration::TypeSerial)
        _updateSerialPorts();
dogmaphobic's avatar
dogmaphobic committed
774
#endif
775 776 777 778 779
    return LinkConfiguration::createSettings(type, name);
}

LinkConfiguration* LinkManager::startConfigurationEditing(LinkConfiguration* config)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
780
    if (config) {
Gus Grubba's avatar
Gus Grubba committed
781
#ifndef NO_SERIAL_LINK
DonLakeFlyer's avatar
DonLakeFlyer committed
782 783
        if(config->type() == LinkConfiguration::TypeSerial)
            _updateSerialPorts();
dogmaphobic's avatar
dogmaphobic committed
784
#endif
DonLakeFlyer's avatar
DonLakeFlyer committed
785 786 787 788 789
        return LinkConfiguration::duplicateSettings(config);
    } else {
        qWarning() << "Internal error";
        return NULL;
    }
790 791 792 793 794
}


void LinkManager::_fixUnnamed(LinkConfiguration* config)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
795 796 797 798
    if (config) {
        //-- Check for "Unnamed"
        if (config->name() == "Unnamed") {
            switch(config->type()) {
Gus Grubba's avatar
Gus Grubba committed
799
#ifndef NO_SERIAL_LINK
800 801
            case LinkConfiguration::TypeSerial: {
                QString tname = dynamic_cast<SerialConfiguration*>(config)->portName();
802
#ifdef Q_OS_WIN
803 804 805 806 807 808 809
                tname.replace("\\\\.\\", "");
#else
                tname.replace("/dev/cu.", "");
                tname.replace("/dev/", "");
#endif
                config->setName(QString("Serial Device on %1").arg(tname));
                break;
DonLakeFlyer's avatar
DonLakeFlyer committed
810
            }
811 812 813
#endif
            case LinkConfiguration::TypeUdp:
                config->setName(
DonLakeFlyer's avatar
DonLakeFlyer committed
814
                            QString("UDP Link on Port %1").arg(dynamic_cast<UDPConfiguration*>(config)->localPort()));
815 816
                break;
            case LinkConfiguration::TypeTcp: {
DonLakeFlyer's avatar
DonLakeFlyer committed
817 818 819 820
                TCPConfiguration* tconfig = dynamic_cast<TCPConfiguration*>(config);
                if(tconfig) {
                    config->setName(
                                QString("TCP Link %1:%2").arg(tconfig->address().toString()).arg((int)tconfig->port()));
821
                }
DonLakeFlyer's avatar
DonLakeFlyer committed
822
            }
823
                break;
dogmaphobic's avatar
dogmaphobic committed
824
#ifdef QGC_ENABLE_BLUETOOTH
dogmaphobic's avatar
dogmaphobic committed
825
            case LinkConfiguration::TypeBluetooth: {
DonLakeFlyer's avatar
DonLakeFlyer committed
826 827 828
                BluetoothConfiguration* tconfig = dynamic_cast<BluetoothConfiguration*>(config);
                if(tconfig) {
                    config->setName(QString("%1 (Bluetooth Device)").arg(tconfig->device().name));
dogmaphobic's avatar
dogmaphobic committed
829
                }
DonLakeFlyer's avatar
DonLakeFlyer committed
830
            }
dogmaphobic's avatar
dogmaphobic committed
831 832
                break;
#endif
dogmaphobic's avatar
dogmaphobic committed
833
#ifndef __mobile__
834
            case LinkConfiguration::TypeLogReplay: {
DonLakeFlyer's avatar
DonLakeFlyer committed
835 836 837
                LogReplayLinkConfiguration* tconfig = dynamic_cast<LogReplayLinkConfiguration*>(config);
                if(tconfig) {
                    config->setName(QString("Log Replay %1").arg(tconfig->logFilenameShort()));
838
                }
DonLakeFlyer's avatar
DonLakeFlyer committed
839
            }
840
                break;
dogmaphobic's avatar
dogmaphobic committed
841
#endif
842 843 844
#ifdef QT_DEBUG
            case LinkConfiguration::TypeMock:
                config->setName(
DonLakeFlyer's avatar
DonLakeFlyer committed
845
                            QString("Mock Link"));
846 847 848 849 850
                break;
#endif
            case LinkConfiguration::TypeLast:
            default:
                break;
DonLakeFlyer's avatar
DonLakeFlyer committed
851
            }
852
        }
DonLakeFlyer's avatar
DonLakeFlyer committed
853 854
    } else {
        qWarning() << "Internal error";
855 856 857 858 859
    }
}

void LinkManager::removeConfiguration(LinkConfiguration* config)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
860 861 862 863 864
    if (config) {
        LinkInterface* iface = config->link();
        if(iface) {
            disconnectLink(iface);
        }
865

DonLakeFlyer's avatar
DonLakeFlyer committed
866 867 868 869 870
        _removeConfiguration(config);
        saveLinkConfigurationList();
    } else {
        qWarning() << "Internal error";
    }
871
}
872

873 874
bool LinkManager::isAutoconnectLink(LinkInterface* link)
{
875 876 877 878 879 880
    for (int i=0; i<_sharedAutoconnectConfigurations.count(); i++) {
        if (_sharedAutoconnectConfigurations[i].data() == link->getLinkConfiguration()) {
            return true;
        }
    }
    return false;
881
}
dogmaphobic's avatar
dogmaphobic committed
882 883 884 885 886

bool LinkManager::isBluetoothAvailable(void)
{
    return qgcApp()->isBluetoothAvailable();
}
Don Gagne's avatar
Don Gagne committed
887

Gus Grubba's avatar
Gus Grubba committed
888
#ifndef NO_SERIAL_LINK
Don Gagne's avatar
Don Gagne committed
889 890
void LinkManager::_activeLinkCheck(void)
{
891
    SerialLink* link = NULL;
Don Gagne's avatar
Don Gagne committed
892 893 894
    bool found = false;

    if (_activeLinkCheckList.count() != 0) {
895
        link = _activeLinkCheckList.takeFirst();
896
        if (containsLink(link) && link->isConnected()) {
Don Gagne's avatar
Don Gagne committed
897 898 899 900 901 902 903 904 905
            // Make sure there is a vehicle on the link
            QmlObjectListModel* vehicles = _toolbox->multiVehicleManager()->vehicles();
            for (int i=0; i<vehicles->count(); i++) {
                Vehicle* vehicle = qobject_cast<Vehicle*>(vehicles->get(i));
                if (vehicle->containsLink(link)) {
                    found = true;
                    break;
                }
            }
906 907
        } else {
            link = NULL;
Don Gagne's avatar
Don Gagne committed
908 909 910 911 912 913 914
        }
    }

    if (_activeLinkCheckList.count() == 0) {
        _activeLinkCheckTimer.stop();
    }

915 916 917
    if (!found && link) {
        // See if we can get an NSH prompt on this link
        bool foundNSHPrompt = false;
918
        link->writeBytesSafe("\r", 1);
919 920 921 922 923 924 925 926 927
        QSignalSpy spy(link, SIGNAL(bytesReceived(LinkInterface*, QByteArray)));
        if (spy.wait(100)) {
            QList<QVariant> arguments = spy.takeFirst();
            if (arguments[1].value<QByteArray>().contains("nsh>")) {
                foundNSHPrompt = true;
            }
        }

        qgcApp()->showMessage(foundNSHPrompt ?
928 929
                                  tr("Please check to make sure you have an SD Card inserted in your Vehicle and try again.") :
                                  tr("Your Vehicle is not responding. If this continues, shutdown %1, restart the Vehicle letting it boot completely, then start %1.").arg(qgcApp()->applicationName()));
Don Gagne's avatar
Don Gagne committed
930 931
    }
}
Don Gagne's avatar
Don Gagne committed
932
#endif
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

bool LinkManager::containsLink(LinkInterface* link)
{
    for (int i=0; i<_sharedLinks.count(); i++) {
        if (_sharedLinks[i].data() == link) {
            return true;
        }
    }
    return false;
}

SharedLinkConfigurationPointer LinkManager::addConfiguration(LinkConfiguration* config)
{
    _qmlConfigurations.append(config);
    _sharedConfigurations.append(SharedLinkConfigurationPointer(config));
    return _sharedConfigurations.last();
}

void LinkManager::_removeConfiguration(LinkConfiguration* config)
{
    _qmlConfigurations.removeOne(config);

    for (int i=0; i<_sharedConfigurations.count(); i++) {
        if (_sharedConfigurations[i].data() == config) {
            _sharedConfigurations.removeAt(i);
            return;
        }
    }

    qWarning() << "LinkManager::_removeConfiguration called with unknown config";
}

QList<LinkInterface*> LinkManager::links(void)
{
    QList<LinkInterface*> rawLinks;

    for (int i=0; i<_sharedLinks.count(); i++) {
        rawLinks.append(_sharedLinks[i].data());
    }

    return rawLinks;
}
975 976 977 978 979 980 981 982 983 984 985

void LinkManager::startAutoConnectedLinks(void)
{
    SharedLinkConfigurationPointer conf;

    for(int i = 0; i < _sharedConfigurations.count(); i++) {
        conf = _sharedConfigurations[i];
        if (conf->isAutoConnect())
            createConnectedLink(conf);
    }
}
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

int LinkManager::_reserveMavlinkChannel(void)
{
    // Find a mavlink channel to use for this link, Channel 0 is reserved for internal use.
    for (int mavlinkChannel=1; mavlinkChannel<32; mavlinkChannel++) {
        if (!(_mavlinkChannelsUsedBitMask & 1 << mavlinkChannel)) {
            mavlink_reset_channel_status(mavlinkChannel);
            // Start the channel on Mav 1 protocol
            mavlink_status_t* mavlinkStatus = mavlink_get_channel_status(mavlinkChannel);
            mavlinkStatus->flags |= MAVLINK_STATUS_FLAG_OUT_MAVLINK1;
            _mavlinkChannelsUsedBitMask |= 1 << mavlinkChannel;
            return mavlinkChannel;
        }
    }

    return 0;   // All channels reserved
}

void LinkManager::_freeMavlinkChannel(int channel)
{
    _mavlinkChannelsUsedBitMask &= ~(1 << channel);
}
1008

1009 1010 1011 1012 1013
void LinkManager::_heartbeatReceived(LinkInterface* link, int vehicleId, int componentId, int vehicleFirmwareType, int vehicleType) {
    Q_UNUSED(vehicleId);
    Q_UNUSED(componentId);
    Q_UNUSED(vehicleFirmwareType);
    Q_UNUSED(vehicleType);
1014 1015 1016 1017 1018 1019 1020

    link->timerStart();

    if (!link->active()) {
        link->setActive(true);
    }
}