MultiSignalSpyV2.cc 7.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/****************************************************************************
 *
 * (c) 2009-2020 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.
 *
 ****************************************************************************/

#include "MultiSignalSpyV2.h"
#include <QEventLoop>
#include <QCoreApplication>
#include <QDebug>
#include <QTest>

MultiSignalSpyV2::MultiSignalSpyV2(QObject* parent)
    : QObject(parent)
{

}

MultiSignalSpyV2::~MultiSignalSpyV2()
{
    for (QSignalSpy* spy: _rgSpys) {
        delete spy;
    }
}

/// Initializes the class. Must be called once before use.
/// @param signalEmitter[in] QObject which the signals are emitted from
///     @return true if success, false for failure
bool MultiSignalSpyV2::init(QObject* signalEmitter)
{
    bool error = false;

    if (!signalEmitter) {
        qDebug() << "No signalEmitter specified";
        return false;
    }
    
    _signalEmitter = signalEmitter;

    const QMetaObject* metaObject = signalEmitter->metaObject();
    for (int i = 0; i < metaObject->methodCount(); i++) {
        const QMetaMethod method = metaObject->method(i);
        if (method.methodType() == QMetaMethod::Signal) {
            QString signalName = method.name();

            _rgSignalNames.append(signalName);
            QSignalSpy* spy = new QSignalSpy(_signalEmitter, QStringLiteral("2%1").arg(method.methodSignature().data()).toLocal8Bit().data());
            if (spy->isValid()) {
                _rgSpys.append(spy);
            } else {
                qDebug() << "Invalid signal:" << signalName;
                error = true;
                break;
            }
        }
    }

    if (error) {
        for (QSignalSpy* spy: _rgSpys) {
            delete spy;
        }
        _rgSignalNames.clear();
        _rgSpys.clear();
        return false;
    }

    return true;
}

bool MultiSignalSpyV2::_checkSignalByMaskWorker(quint32 mask, bool multipleSignalsAllowed)
{
    for (int i=0; i<_rgSignalNames.count(); i++) {
        if ((1 << i) & mask) {
            QSignalSpy* spy = _rgSpys[i];
            Q_ASSERT(spy != nullptr);
            
            if ((multipleSignalsAllowed && spy->count() ==  0) || (!multipleSignalsAllowed && spy->count() != 1)) {
                qDebug() << "Failed index:" << i;
                _printSignalState(mask);
                return false;
            }
        }
    }
    
    return true;
}

bool MultiSignalSpyV2::_checkOnlySignalByMaskWorker(quint32 mask, bool multipleSignalsAllowed)
{
    for (int i=0; i<_rgSignalNames.count(); i++) {
        QSignalSpy* spy = _rgSpys[i];
        Q_ASSERT(spy != nullptr);

        if ((1 << i) & mask) {
            if ((multipleSignalsAllowed && spy->count() ==  0) || (!multipleSignalsAllowed && spy->count() != 1)) {
                _printSignalState(mask);
                return false;
            }
        } else {
            if (spy->count() != 0) {
                _printSignalState(mask);
                return false;
            }
        }
    }
    
    return true;
}

bool MultiSignalSpyV2::checkSignalByMask(quint32 mask)
{
    return _checkSignalByMaskWorker(mask, false /* multipleSignalsAllowed */);
}

bool MultiSignalSpyV2::checkOnlySignalByMask(quint32 mask)
{
    return _checkOnlySignalByMaskWorker(mask, false /* multipleSignalsAllowed */);
}

bool MultiSignalSpyV2::checkSignalsByMask(quint32 mask)
{
    return _checkSignalByMaskWorker(mask, true /* multipleSignalsAllowed */);
}

bool MultiSignalSpyV2::checkOnlySignalsByMask(quint32 mask)
{
    return _checkOnlySignalByMaskWorker(mask, true /* multipleSignalsAllowed */);
}

/// @return true if signal count = 0 for specified signals
bool MultiSignalSpyV2::checkNoSignalByMask(quint32 mask)
{
    for (int i=0; i<_rgSignalNames.count(); i++) {
        if ((1 << i) & mask) {
            QSignalSpy* spy = _rgSpys[i];
            Q_ASSERT(spy != nullptr);

            if (spy->count() != 0) {
                _printSignalState(mask);
                return false;
            }
        }
    }
    
    return true;
}

/// @return true if signal count = 0 on all signals
bool MultiSignalSpyV2::checkNoSignals(void)
{
    return checkNoSignalByMask(~0);
}

/// @return QSignalSpy for the specified signal
QSignalSpy* MultiSignalSpyV2::getSpy(const char* signalName)
{
    for (int i=0; i<_rgSignalNames.count(); i++) {
        if (_rgSignalNames[i] == signalName) {
            return _rgSpys[i];
        }
    }

    qWarning() << "MultiSignalSpyV2::getSpy no such signal" << signalName;
    return nullptr;
}

/// Sets the signal count to 0 for the specified signal
void MultiSignalSpyV2::clearSignal(const char* signalName)
{
    getSpy(signalName)->clear();
}

/// Sets the signal count to 0 for all specified signals
void MultiSignalSpyV2::clearSignalsByMask(quint32 mask)
{
    for (int i=0; i<_rgSignalNames.count(); i++) {
        if ((1 << i) & mask) {
            QSignalSpy* spy = _rgSpys[i];
            Q_ASSERT(spy != nullptr);

            spy->clear();
        }
    }
}

/// Sets the signal count to 0 for all signals
void MultiSignalSpyV2::clearAllSignals(void)
{
    for (QSignalSpy* spy: _rgSpys) {
        spy->clear();
    }
}

void MultiSignalSpyV2::timerEvent(QTimerEvent * event)
{
    Q_UNUSED(event);
    _timeout = true;
}

/// Waits the specified signal
///     @return false for timeout
bool MultiSignalSpyV2::waitForSignal(const char* signalName, int msecs)
{
    // Check input parameters
    if (msecs < -1 || msecs == 0)
        return false;
    
    // activate the timeout
    _timeout = false;
    int timerId;
    if (msecs != -1) {
        timerId = startTimer(msecs);
        Q_ASSERT(timerId);
    } else {
        timerId = 0;
    }
    
    // Begin waiting
    QSignalSpy* spy = getSpy(signalName);
    Q_ASSERT(spy);
    
    while (spy->count() == 0 && !_timeout) {
        QTest::qWait(100);
    }
    
    // Clean up and return status
    if (timerId) {
        killTimer(timerId);
    }

    return spy->count() != 0;
}

void MultiSignalSpyV2::_printSignalState(quint32 mask)
{
    for (int i=0; i<_rgSignalNames.count(); i++) {
        bool expected = (1 << i) & mask;

        QSignalSpy* spy = _rgSpys[i];
        Q_ASSERT(spy != nullptr);
        qDebug() << "Signal index:" << i << "count:" << spy->count() << "expected:" << expected << _rgSignalNames[i];
    }
}

bool MultiSignalSpyV2::pullBoolFromSignal(const char* signalName)
{
    QSignalSpy* spy = getSpy(signalName);
    return spy->value(0).value(0).toBool();
}

int MultiSignalSpyV2::pullIntFromSignal(const char* signalName)
{
    QSignalSpy* spy = getSpy(signalName);
    return spy->value(0).value(0).toInt();
}

QGeoCoordinate MultiSignalSpyV2::pullQGeoCoordinateFromSignal(const char* signalName)
{
    QSignalSpy* spy = getSpy(signalName);
    return spy->value(0).value(0).value<QGeoCoordinate>();
}

quint32 MultiSignalSpyV2::signalNameToMask(const char* signalName)
{
    for (int i=0; i<_rgSignalNames.count(); i++) {
        if (_rgSignalNames[i] == signalName) {
            return 1 << i;
        }
    }

    qDebug() << "MultiSignalSpyV2::signalNameToMask signal not found" << signalName;

    return 0;
}