1// Copyright (C) 2017 Governikus GmbH & Co. K
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qnearfieldtarget_p.h"
5
6#include <QtCore/QCoreApplication>
7#include <QtCore/QElapsedTimer>
8#include <QtCore/QPointer>
9
10QT_BEGIN_NAMESPACE
11
12
13QNearFieldTargetPrivate::QNearFieldTargetPrivate(QObject *parent)
14: QObject(parent)
15, q_ptr(nullptr)
16{
17}
18
19QByteArray QNearFieldTargetPrivate::uid() const
20{
21 return QByteArray();
22}
23
24QNearFieldTarget::Type QNearFieldTargetPrivate::type() const
25{
26 return QNearFieldTarget::Type::ProprietaryTag;
27}
28
29QNearFieldTarget::AccessMethods QNearFieldTargetPrivate::accessMethods() const
30{
31 return QNearFieldTarget::AccessMethod::UnknownAccess;
32}
33
34bool QNearFieldTargetPrivate::disconnect()
35{
36 return false;
37}
38
39// NdefAccess
40bool QNearFieldTargetPrivate::hasNdefMessage()
41{
42 return false;
43}
44
45QNearFieldTarget::RequestId QNearFieldTargetPrivate::readNdefMessages()
46{
47 const QNearFieldTarget::RequestId id;
48 Q_EMIT error(error: QNearFieldTarget::UnsupportedError, id);
49 return id;
50}
51
52QNearFieldTarget::RequestId
53QNearFieldTargetPrivate::writeNdefMessages(const QList<QNdefMessage> &messages)
54{
55 Q_UNUSED(messages);
56
57 const QNearFieldTarget::RequestId id;
58 Q_EMIT error(error: QNearFieldTarget::UnsupportedError, id);
59 return id;
60}
61
62// TagTypeSpecificAccess
63int QNearFieldTargetPrivate::maxCommandLength() const
64{
65 return 0;
66}
67
68QNearFieldTarget::RequestId QNearFieldTargetPrivate::sendCommand(const QByteArray &command)
69{
70 Q_UNUSED(command);
71
72 const QNearFieldTarget::RequestId id;
73 Q_EMIT error(error: QNearFieldTarget::UnsupportedError, id);
74 return id;
75}
76
77bool QNearFieldTargetPrivate::waitForRequestCompleted(const QNearFieldTarget::RequestId &id,
78 int msecs)
79{
80 QElapsedTimer timer;
81 timer.start();
82
83 const QPointer<QNearFieldTargetPrivate> weakThis = this;
84
85 do {
86 if (!weakThis)
87 return false;
88
89 if (m_decodedResponses.contains(key: id))
90 return true;
91 else
92 QCoreApplication::processEvents(flags: QEventLoop::WaitForMoreEvents, maxtime: 1);
93 } while (timer.elapsed() <= msecs);
94
95 reportError(error: QNearFieldTarget::TimeoutError, id);
96
97 return false;
98}
99
100QVariant QNearFieldTargetPrivate::requestResponse(const QNearFieldTarget::RequestId &id) const
101{
102 return m_decodedResponses.value(key: id);
103}
104
105void QNearFieldTargetPrivate::setResponseForRequest(const QNearFieldTarget::RequestId &id,
106 const QVariant &response,
107 bool emitRequestCompleted)
108{
109 for (auto i = m_decodedResponses.begin(), end = m_decodedResponses.end(); i != end; /* erasing */) {
110 // no more external references
111 if (i.key().refCount() == 1)
112 i = m_decodedResponses.erase(it: i);
113 else
114 ++i;
115 }
116
117 m_decodedResponses.insert(key: id, value: response);
118
119 if (emitRequestCompleted)
120 Q_EMIT requestCompleted(id);
121}
122
123void QNearFieldTargetPrivate::reportError(QNearFieldTarget::Error error,
124 const QNearFieldTarget::RequestId &id)
125{
126 setResponseForRequest(id, response: QVariant(), emitRequestCompleted: false);
127 QMetaObject::invokeMethod(object: this, function: [this, error, id]() {
128 Q_EMIT this->error(error, id);
129 }, type: Qt::QueuedConnection);
130}
131
132QT_END_NAMESPACE
133
134#include "moc_qnearfieldtarget_p.cpp"
135

source code of qtconnectivity/src/nfc/qnearfieldtarget_p.cpp