summaryrefslogtreecommitdiffstats
path: root/src/nfc/qnx/qnxnfceventfilter.cpp
blob: 52a1d08e8bce0dd46ef12906d1a3462643e4b2ae (plain)
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
#include "qnxnfceventfilter_p.h"
#include <QDebug>
#include "nfc/nfc.h"
#include "qnxnfcmanager_p.h"

QT_BEGIN_NAMESPACE_NFC

QNXNFCEventFilter::QNXNFCEventFilter()
{
}

void QNXNFCEventFilter::installOnEventDispatcher(QAbstractEventDispatcher *dispatcher)
{
    dispatcher->installNativeEventFilter(this);
    // start flow of navigator events
    navigator_request_events(0);
}

void QNXNFCEventFilter::uninstallEventFilter()
{
    removeEventFilter(this);
}

bool QNXNFCEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(eventType);
    Q_UNUSED(result);
    bps_event_t *event = static_cast<bps_event_t *>(message);

    int code = bps_event_get_code(event);

    if (code == NAVIGATOR_INVOKE_TARGET) {
        // extract bps request from event
        const navigator_invoke_invocation_t *invoke = navigator_invoke_event_get_invocation(event);
        const char *uri = navigator_invoke_invocation_get_uri(invoke);
        const char *type = navigator_invoke_invocation_get_type(invoke);
        int dataLength = navigator_invoke_invocation_get_data_length(invoke);
        const char *raw_data = (const char*)navigator_invoke_invocation_get_data(invoke);
        QByteArray data(raw_data, dataLength);

        //message.fromByteArray(data);

        const char* metadata = navigator_invoke_invocation_get_metadata(invoke);

        nfc_ndef_message_t *ndefMessage;
        nfc_create_ndef_message_from_bytes(reinterpret_cast<const uchar_t *>(data.data()),
                                                  data.length(), &ndefMessage);

        QNdefMessage message = QNXNFCManager::instance()->decodeMessage(ndefMessage);

        unsigned int ndefRecordCount;
        nfc_get_ndef_record_count(ndefMessage, &ndefRecordCount);

        qQNXNFCDebug() << "Got Invoke event" << uri << "Type" << type;

        Q_EMIT ndefEvent(message);
    }

    return false;
}

QT_END_NAMESPACE_NFC