LinkInterface.cc 2.39 KB
Newer Older
1 2
/****************************************************************************
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10 11 12
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "LinkInterface.h"
#include "QGCApplication.h"

13 14 15 16
LinkInterface::LinkInterface(SharedLinkConfigurationPtr& config, bool isPX4Flow)
    : QThread   (0)
    , _config   (config)
    , _isPX4Flow(isPX4Flow)
17
{
18
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
19

20
    qRegisterMetaType<LinkInterface*>("LinkInterface*");
21 22
}

23
LinkInterface::~LinkInterface()
24
{
25 26
    if (_vehicleReferenceCount != 0) {
        qWarning() << "~LinkInterface still have vehicle references:" << _vehicleReferenceCount;
27
    }
28
    _config.reset();
29 30
}

31 32 33 34 35 36 37 38 39 40
uint8_t LinkInterface::mavlinkChannel(void) const
{
    if (!_mavlinkChannelSet) {
        qWarning() << "Call to LinkInterface::mavlinkChannel with _mavlinkChannelSet == false";
    }
    return _mavlinkChannel;
}

void LinkInterface::_setMavlinkChannel(uint8_t channel)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
41 42 43
    if (_mavlinkChannelSet) {
        qWarning() << "Mavlink channel set multiple times";
    }
44 45 46
    _mavlinkChannelSet = true;
    _mavlinkChannel = channel;
}
47

48 49 50 51 52 53 54 55 56
void LinkInterface::writeBytesThreadSafe(const char *bytes, int length)
{
    QByteArray byteArray(bytes, length);
    _writeBytesMutex.lock();
    _writeBytes(byteArray);
    _writeBytesMutex.unlock();
}

void LinkInterface::addVehicleReference(void)
57
{
58
    _vehicleReferenceCount++;
59
}
60

61 62 63 64 65 66 67
void LinkInterface::removeVehicleReference(void)
{
    if (_vehicleReferenceCount != 0) {
        _vehicleReferenceCount--;
        if (_vehicleReferenceCount == 0) {
            disconnect();
        }
68
    } else {
69
        qWarning() << "LinkInterface::removeVehicleReference called with no vehicle references";
70 71 72
    }
}

73 74 75 76 77 78 79
void LinkInterface::_connectionRemoved(void)
{
    if (_vehicleReferenceCount == 0) {
        // Since there are no vehicles on the link we can disconnect it right now
        disconnect();
    } else {
        // If there are still vehicles on this link we allow communication lost to trigger and don't automatically disconect until all the vehicles go away
80
    }
81
}
82

83 84 85 86 87
#ifdef UNITTEST_BUILD
#include "MockLink.h"
bool LinkInterface::isMockLink(void)
{
    return dynamic_cast<MockLink*>(this);
88
}
89
#endif