PairingNFC.cc 6.64 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 136 137 138 139 140 141 142 143 144 145 146
/****************************************************************************
 *
 *   (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 "PairingNFC.h"
#include "QGCApplication.h"
#include <QSoundEffect>

QGC_LOGGING_CATEGORY(PairingNFCLog, "PairingNFCLog")

extern "C" {
#include <TML/inc/tool.h>
#include <NfcTask/inc/ndef_helper.h>
//-----------------------------------------------------------------------------
void logmsg(char *buf)
{
    QString s = buf;
    s.replace("\n", "");
    qCDebug(PairingNFCLog) << s;
}

//-----------------------------------------------------------------------------
void NdefPull_Cb(unsigned char *pNdefMessage, unsigned short NdefMessageSize)
{
    Q_UNUSED(NdefMessageSize);
    unsigned char *pNdefRecord = pNdefMessage;
    NdefRecord_t NdefRecord;
    unsigned char save;

    if (!pNdefMessage)
    {
        qCDebug(PairingNFCLog) << "Issue during NDEF message reception (check provisioned buffer size)";
        return;
    }

    QSoundEffect _nfcSound;
    _nfcSound.setSource(QUrl::fromUserInput("qrc:/auterion/wav/beep.wav"));
    _nfcSound.setVolume(0.9);
    _nfcSound.play();

    while (pNdefRecord)
    {
        qCDebug(PairingNFCLog) << "NDEF record received";

        NdefRecord = DetectNdefRecordType(pNdefRecord);

        switch(NdefRecord.recordType)
        {
        case MEDIA_VCARD:
            save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize];
            NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0';
            qCDebug(PairingNFCLog) << "   vCard: " << reinterpret_cast<char *>(NdefRecord.recordPayload);
            NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save;
            break;

        case WELL_KNOWN_SIMPLE_TEXT:
        {
            save = NdefRecord.recordPayload[NdefRecord.recordPayloadSize];
            NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = '\0';
            QString text = reinterpret_cast<char *>(&NdefRecord.recordPayload[NdefRecord.recordPayload[0]+1]);
            qCDebug(PairingNFCLog) << "   Text: " << text;
            qgcApp()->toolbox()->pairingManager()->jsonReceived(text);
            NdefRecord.recordPayload[NdefRecord.recordPayloadSize] = save;
            break;
        }

        default:
            qCDebug(PairingNFCLog) << "   Unsupported NDEF record, cannot parse";
            break;
        }
        pNdefRecord = GetNextRecord(pNdefRecord);
    }
}
//-----------------------------------------------------------------------------
}

// Discovery loop configuration according to the targeted modes of operation
static unsigned char discoveryTechnologies[] = {
    MODE_POLL | TECH_PASSIVE_NFCA,