summaryrefslogtreecommitdiffstats
path: root/src/nfc/qnearfieldtarget_neard_p.cpp
blob: d69820307c7abe81b21d3faf7bbbfcdc3caf567a (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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (C) 2016 BlackBerry Limited, Copyright (C) 2016 BasysKom GmbH
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qnearfieldtarget_neard_p.h"

#include <qndefnfctextrecord.h>
#include <qndefnfcsmartposterrecord.h>
#include <qndefnfcurirecord.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(QT_NFC_NEARD)

QNearFieldTargetPrivateImpl::QNearFieldTargetPrivateImpl(QObject *parent, QDBusObjectPath interfacePath)
    : QNearFieldTargetPrivate(parent),
      m_tagPath(interfacePath),
      m_readRequested(false)
{
    m_readErrorTimer.setSingleShot(true);
    m_recordPathsCollectedTimer.setSingleShot(true);
    m_delayedWriteTimer.setSingleShot(true);

    qCDebug(QT_NFC_NEARD) << "tag found at path" << interfacePath.path();
    m_dbusProperties = new OrgFreedesktopDBusPropertiesInterface(QStringLiteral("org.neard"),
                                        interfacePath.path(), QDBusConnection::systemBus(), this);
    if (!m_dbusProperties->isValid()) {
        qCWarning(QT_NFC_NEARD) << "Could not connect to dbus property interface at path"
                                << interfacePath.path();
        return;
    }

    QDBusPendingReply<QVariantMap> reply = m_dbusProperties->GetAll(QStringLiteral("org.neard.Tag"));
    reply.waitForFinished();
    if (reply.isError()) {
        qCWarning(QT_NFC_NEARD) << "Could not get properties of org.neard.Tag dbus interface";
        return;
    }

    const QString &type = reply.value().value(QStringLiteral("Type")).toString();
    m_type = QNearFieldTarget::ProprietaryTag;

    if (type == QStringLiteral("Type 1"))
        m_type = QNearFieldTarget::NfcTagType1;
    else if (type == QStringLiteral("Type 2"))
        m_type = QNearFieldTarget::NfcTagType2;
    else if (type == QStringLiteral("Type 3"))
        m_type = QNearFieldTarget::NfcTagType3;
    else if (type == QStringLiteral("Type 4"))
        m_type = QNearFieldTarget::NfcTagType4;

    qCDebug(QT_NFC_NEARD) << "tag type" << type;

    connect(&m_recordPathsCollectedTimer, &QTimer::timeout,
            this, &QNearFieldTargetPrivateImpl::createNdefMessage);
    connect(&m_readErrorTimer, &QTimer::timeout,
            this, &QNearFieldTargetPrivateImpl::handleReadError);
    connect(&m_delayedWriteTimer, &QTimer::timeout,
            this, &QNearFieldTargetPrivateImpl::handleWriteRequest);
    connect(NeardHelper::instance(), &NeardHelper::recordFound,
            this, &QNearFieldTargetPrivateImpl::handleRecordFound);
}

QNearFieldTargetPrivateImpl::~QNearFieldTargetPrivateImpl()
{
}

bool QNearFieldTargetPrivateImpl::isValid()
{
    return m_dbusProperties->isValid() && NeardHelper::instance()->dbusObjectManager()->isValid();
}

QByteArray QNearFieldTargetPrivateImpl::uid() const
{
    return QByteArray(); // TODO figure out a workaround because neard does not offer
                         // this property
}

QNearFieldTarget::Type QNearFieldTargetPrivateImpl::type() const
{
    return m_type;
}

QNearFieldTarget::AccessMethods QNearFieldTargetPrivateImpl::accessMethods() const
{
    return QNearFieldTarget::NdefAccess;
}

bool QNearFieldTargetPrivateImpl::hasNdefMessage()
{
    return !m_recordPaths.isEmpty();
}

QNearFieldTarget::RequestId QNearFieldTargetPrivateImpl::readNdefMessages()
{
    if (isValid()) {
        // if the user calls readNdefMessages before the previous request has been completed
        // return the current request id.
        if (m_currentReadRequestId.isValid())
            return m_currentReadRequestId;

        QNearFieldTarget::RequestId requestId = QNearFieldTarget::RequestId(new QNearFieldTarget::RequestIdPrivate());
        // save the id so it can be passed along with requestCompleted
        m_currentReadRequestId = requestId;
        // since the triggering of interfaceAdded will ultimately lead to createNdefMessage being called
        // we need to make sure that ndefMessagesRead will only be triggered when readNdefMessages has
        // been called before. In case readNdefMessages is called again after that we can directly call
        // call createNdefMessage.
        m_readRequested = true;
        if (hasNdefMessage())
            createNdefMessage();
        else
            m_readErrorTimer.start(1000);

        return requestId;
    } else {
        return QNearFieldTarget::RequestId();
    }
}

QNearFieldTarget::RequestId QNearFieldTargetPrivateImpl::sendCommand(const QByteArray &command)
{
    Q_UNUSED(command);
    return QNearFieldTarget::RequestId();
}

QNearFieldTarget::RequestId QNearFieldTargetPrivateImpl::writeNdefMessages(const QList<QNdefMessage> &messages)
{
    // disabling write due to neard crash (see QTBUG-43802)
    qWarning("QNearFieldTarget::WriteNdefMessages() disabled. See QTBUG-43802\n");
    return QNearFieldTarget::RequestId();


    // return old request id when previous write request hasn't completed
    if (m_currentWriteRequestId.isValid())
        return m_currentWriteRequestId;

    qCDebug(QT_NFC_NEARD) << "writing messages";
    if (messages.isEmpty() || messages.first().isEmpty()) {
        qCWarning(QT_NFC_NEARD) << "No record specified";
        return QNearFieldTarget::RequestId();
    }
    if (messages.count() > 1 || messages.first().count() > 1) {
        // neard only supports one ndef record per tag
        qCWarning(QT_NFC_NEARD) << "Writing of only one NDEF record and message is supported";
        return QNearFieldTarget::RequestId();
    }
    QNdefRecord record = messages.first().first();

    if (record.typeNameFormat() == QNdefRecord::NfcRtd) {
        m_currentWriteRequestData.clear();
        if (record.isRecordType<QNdefNfcUriRecord>()) {
            m_currentWriteRequestData.insert(QStringLiteral("Type"), QStringLiteral("URI"));
            QNdefNfcUriRecord uriRecord = static_cast<QNdefNfcUriRecord>(record);
            m_currentWriteRequestData.insert(QStringLiteral("URI"), uriRecord.uri().toString());
        } else if (record.isRecordType<QNdefNfcSmartPosterRecord>()) {
            m_currentWriteRequestData.insert(QStringLiteral("Type"), QStringLiteral("SmartPoster"));
            QNdefNfcSmartPosterRecord spRecord = static_cast<QNdefNfcSmartPosterRecord>(record);
            m_currentWriteRequestData.insert(QStringLiteral("URI"), spRecord.uri().toString());
            // Currently neard only supports the uri property for writing
        } else if (record.isRecordType<QNdefNfcTextRecord>()) {
            m_currentWriteRequestData.insert(QStringLiteral("Type"), QStringLiteral("Text"));
            QNdefNfcTextRecord textRecord = static_cast<QNdefNfcTextRecord>(record);
            m_currentWriteRequestData.insert(QStringLiteral("Representation"), textRecord.text());
            m_currentWriteRequestData.insert(QStringLiteral("Encoding"),
                                    textRecord.encoding() == QNdefNfcTextRecord::Utf8 ?
                                            QStringLiteral("UTF-8") : QStringLiteral("UTF-16") );
            m_currentWriteRequestData.insert(QStringLiteral("Language"), textRecord.locale());
        } else {
            qCWarning(QT_NFC_NEARD) << "Record type not supported for writing";
            return QNearFieldTarget::RequestId();
        }

        m_currentWriteRequestId = QNearFieldTarget::RequestId(new QNearFieldTarget::RequestIdPrivate());
        // trigger delayed write
        m_delayedWriteTimer.start(100);

        return m_currentWriteRequestId;
    }

    return QNearFieldTarget::RequestId();
}

QNdefRecord QNearFieldTargetPrivateImpl::readRecord(const QDBusObjectPath &path)
{
    qCDebug(QT_NFC_NEARD) << "reading record for path" << path.path();
    OrgFreedesktopDBusPropertiesInterface recordInterface(QStringLiteral("org.neard"), path.path(),
                                                          QDBusConnection::systemBus());
    if (!recordInterface.isValid())
        return QNdefRecord();

    QDBusPendingReply<QVariantMap> reply = recordInterface.GetAll(QStringLiteral("org.neard.Record"));
    reply.waitForFinished();
    if (reply.isError())
        return QNdefRecord();

    const QString &value = reply.value().value(QStringLiteral("Representation")).toString();
    const QString &locale = reply.value().value(QStringLiteral("Language")).toString();
    const QString &encoding = reply.value().value(QStringLiteral("Encoding")).toString();
    const QString &uri = reply.value().value(QStringLiteral("URI")).toString();

    //const QString &mime = reply.value().value(QStringLiteral("MIME")).toString();
    //const QString &arr = reply.value().value(QStringLiteral("ARR")).toString();

    const QString type = reply.value().value(QStringLiteral("Type")).toString();
    if (type == QStringLiteral("Text")) {
        QNdefNfcTextRecord textRecord;
        textRecord.setText(value);
        textRecord.setLocale(locale);
        textRecord.setEncoding((encoding == QStringLiteral("UTF-8")) ? QNdefNfcTextRecord::Utf8
                                                                     : QNdefNfcTextRecord::Utf16);
        return textRecord;
    } else if (type == QStringLiteral("SmartPoster")) {
        QNdefNfcSmartPosterRecord spRecord;
        if (!value.isEmpty()) {
            spRecord.addTitle(value, locale, (encoding == QStringLiteral("UTF-8"))
                                                    ? QNdefNfcTextRecord::Utf8
                                                    : QNdefNfcTextRecord::Utf16);
        }

        if (!uri.isEmpty())
            spRecord.setUri(QUrl(uri));

        const QString &action = reply.value().value(QStringLiteral("Action")).toString();
        if (!action.isEmpty()) {
            if (action == QStringLiteral("Do"))
                spRecord.setAction(QNdefNfcSmartPosterRecord::DoAction);
            else if (action == QStringLiteral("Save"))
                spRecord.setAction(QNdefNfcSmartPosterRecord::SaveAction);
            else if (action == QStringLiteral("Edit"))
                spRecord.setAction(QNdefNfcSmartPosterRecord::EditAction);
        }

        if (reply.value().contains(QStringLiteral("Size"))) {
            uint size = reply.value().value(QStringLiteral("Size")).toUInt();
            spRecord.setSize(size);
        }

        const QString &mimeType = reply.value().value(QStringLiteral("MIMEType")).toString();
        if (!mimeType.isEmpty()) {
            spRecord.setTypeInfo(mimeType);
        }


        return spRecord;
    } else if (type == QStringLiteral("URI")) {
        QNdefNfcUriRecord uriRecord;
        uriRecord.setUri(QUrl(uri));
        return uriRecord;
    } else if (type == QStringLiteral("MIME")) {

    } else if (type == QStringLiteral("AAR")) {

    }

    return QNdefRecord();
}

void QNearFieldTargetPrivateImpl::handleRecordFound(const QDBusObjectPath &path)
{
    m_recordPaths.append(path);
    // FIXME: this timer only exists because neard doesn't currently supply enough
    // information to let us know when all record interfaces have been added or
    // how many records are actually contained on a tag. We assume that when no
    // signal has been received for 100ms all record interfaces have been added.
    m_recordPathsCollectedTimer.start(100);
    // as soon as record paths have been added we can handle errors without the timer.
    m_readErrorTimer.stop();
}

void QNearFieldTargetPrivateImpl::createNdefMessage()
{
    if (m_readRequested) {
        qCDebug(QT_NFC_NEARD) << "creating Ndef message, reading" << m_recordPaths.length() << "record paths";
        QNdefMessage newNdefMessage;
        for (const QDBusObjectPath &recordPath : std::as_const(m_recordPaths))
            newNdefMessage.append(readRecord(recordPath));

        if (!newNdefMessage.isEmpty()) {
            QMetaObject::invokeMethod(this, "ndefMessageRead", Qt::QueuedConnection,
                                      Q_ARG(QNdefMessage, newNdefMessage));
            // the request id in requestCompleted has to match the one created in readNdefMessages
            QMetaObject::invokeMethod(this, [this]() {
                Q_EMIT requestCompleted(m_currentReadRequestId);
            }, Qt::QueuedConnection);
        } else {
            reportError(QNearFieldTarget::UnknownError, m_currentReadRequestId);
        }

        m_readRequested = false;
        // invalidate the current request id
        m_currentReadRequestId = QNearFieldTarget::RequestId(0);
    }
}

void QNearFieldTargetPrivateImpl::handleReadError()
{
    reportError(QNearFieldTarget::UnknownError, m_currentReadRequestId);
    m_currentReadRequestId = QNearFieldTarget::RequestId(0);
}

void QNearFieldTargetPrivateImpl::handleWriteRequest()
{
    OrgNeardTagInterface tagInterface(QStringLiteral("org.neard"), m_tagPath.path(),
                                      QDBusConnection::systemBus());
    if (!tagInterface.isValid()) {
        qCWarning(QT_NFC_NEARD) << "tag interface invalid";
    } else {
        QDBusPendingReply<> reply;
        reply = tagInterface.Write(m_currentWriteRequestData);
        reply.waitForFinished();
        if (reply.isError()) {
            qCWarning(QT_NFC_NEARD) << "Error writing to NFC tag" << reply.error();
            reportError(QNearFieldTarget::UnknownError, m_currentWriteRequestId);
        }

        QMetaObject::invokeMethod(this, [this]() {
            Q_EMIT requestCompleted(m_currentWriteRequestId);
        }, Qt::QueuedConnection);
    }

    // invalidate current write request
    m_currentWriteRequestId = QNearFieldTarget::RequestId(0);
}

QT_END_NAMESPACE