QtNFC.cc 4.62 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
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
/****************************************************************************
 *
 *   (c) 2019 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 "PairingManager.h"
#include "QtNFC.h"
#include "QGCApplication.h"
#include <QSoundEffect>

QGC_LOGGING_CATEGORY(PairingNFCLog, "PairingNFCLog")

#include <QNdefNfcTextRecord>

//-----------------------------------------------------------------------------
PairingNFC::PairingNFC()
{
}

//-----------------------------------------------------------------------------
void
PairingNFC::start()
{
    if (manager != nullptr) {
        return;
    }
    qgcApp()->toolbox()->pairingManager()->setStatusMessage(tr("Waiting for NFC connection"));
    qCDebug(PairingNFCLog) << "Waiting for NFC connection";

    manager = new QNearFieldManager(this);
    if (!manager->isAvailable()) {
        qWarning() << "NFC not available";
        delete manager;
        manager = nullptr;
        return;
    }

    QNdefFilter filter;
    filter.setOrderMatch(false);
    filter.appendRecord<QNdefNfcTextRecord>(1, UINT_MAX);
    // type parameter cannot specify substring so filter for "image/" below
    filter.appendRecord(QNdefRecord::Mime, QByteArray(), 0, 1);

    int result = manager->registerNdefMessageHandler(filter, this, SLOT(handleMessage(QNdefMessage, QNearFieldTarget*)));

    if (result < 0)
        qWarning() << "Platform does not support NDEF message handler registration";

    manager->startTargetDetection();
    connect(manager, &QNearFieldManager::targetDetected, this, &PairingNFC::targetDetected);
    connect(manager, &QNearFieldManager::targetLost, this, &PairingNFC::targetLost);
}

//-----------------------------------------------------------------------------
void
PairingNFC::stop()
{
    if (manager != nullptr) {
        qgcApp()->toolbox()->pairingManager()->setStatusMessage("");
        qCDebug(PairingNFCLog) << "NFC: Stop";
        manager->stopTargetDetection();
        delete manager;
        manager = nullptr;
    }
}

//-----------------------------------------------------------------------------
void
PairingNFC::targetDetected(QNearFieldTarget *target)
{
    if (!target) {
        return;
    }

    qgcApp()->toolbox()->pairingManager()->setStatusMessage(tr("Device detected"));
    qCDebug(PairingNFCLog) << "NFC: Device detected";
    connect(target, &QNearFieldTarget::ndefMessageRead, this, &PairingNFC::handlePolledNdefMessage);
    connect(target, SIGNAL(error(QNearFieldTarget::Error,QNearFieldTarget::RequestId)),
            this, SLOT(targetError(QNearFieldTarget::Error,QNearFieldTarget::RequestId)));
    connect(target, &QNearFieldTarget::requestCompleted, this, &PairingNFC::handleRequestCompleted);

    manager->setTargetAccessModes(QNearFieldManager::NdefReadTargetAccess);
    QNearFieldTarget::RequestId id = target->readNdefMessages();
    if (target->waitForRequestCompleted(id)) {
        qCDebug(PairingNFCLog) << "requestCompleted ";
        QVariant res = target->requestResponse(id);
        qCDebug(PairingNFCLog) << "Response:  " << res.toString();
    }
}

//-----------------------------------------------------------------------------
void
PairingNFC::handleRequestCompleted(const QNearFieldTarget::RequestId& id)
{
    Q_UNUSED(id);
    qCDebug(PairingNFCLog) << "handleRequestCompleted ";
}

//-----------------------------------------------------------------------------
void
PairingNFC::targetError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId& id)
{
    Q_UNUSED(id);
    qCDebug(PairingNFCLog) << "Error: " << error;
}

//-----------------------------------------------------------------------------
void
PairingNFC::targetLost(QNearFieldTarget *target)
{
    qgcApp()->toolbox()->pairingManager()->setStatusMessage(tr("Device removed"));
    qCDebug(PairingNFCLog) << "NFC: Device removed";
    if (target) {
        target->deleteLater();
    }
}

//-----------------------------------------------------------------------------
void
PairingNFC::handlePolledNdefMessage(QNdefMessage message)
{
    qCDebug(PairingNFCLog) << "NFC: Handle NDEF message";
//    QNearFieldTarget *target = qobject_cast<QNearFieldTarget *>(sender());
    for (const QNdefRecord &record : message) {
        if (record.isRecordType<QNdefNfcTextRecord>()) {
            QNdefNfcTextRecord textRecord(record);
            qgcApp()->toolbox()->pairingManager()->jsonReceived(textRecord.text());
        }
    }
}

//-----------------------------------------------------------------------------