1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtNfc module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QNEARFIELDTARGET_H |
41 | #define QNEARFIELDTARGET_H |
42 | |
43 | #include <QtCore/QByteArray> |
44 | #include <QtCore/QList> |
45 | #include <QtCore/QMetaType> |
46 | #include <QtCore/QObject> |
47 | #include <QtCore/QSharedDataPointer> |
48 | #include <QtNfc/qtnfcglobal.h> |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | class QString; |
52 | class QUrl; |
53 | QT_END_NAMESPACE |
54 | |
55 | QT_BEGIN_NAMESPACE |
56 | |
57 | class QNdefMessage; |
58 | class QNearFieldTargetPrivate; |
59 | |
60 | class Q_NFC_EXPORT QNearFieldTarget : public QObject |
61 | { |
62 | Q_OBJECT |
63 | |
64 | Q_DECLARE_PRIVATE(QNearFieldTarget) |
65 | |
66 | public: |
67 | enum Type { |
68 | ProprietaryTag, |
69 | NfcTagType1, |
70 | NfcTagType2, |
71 | NfcTagType3, |
72 | NfcTagType4, |
73 | MifareTag |
74 | }; |
75 | Q_ENUM(Type) |
76 | |
77 | enum AccessMethod { |
78 | UnknownAccess = 0x00, |
79 | NdefAccess = 0x01, |
80 | TagTypeSpecificAccess = 0x02, |
81 | LlcpAccess = 0x04 |
82 | }; |
83 | Q_ENUM(AccessMethod) |
84 | Q_DECLARE_FLAGS(AccessMethods, AccessMethod) |
85 | |
86 | enum Error { |
87 | NoError, |
88 | UnknownError, |
89 | UnsupportedError, |
90 | TargetOutOfRangeError, |
91 | NoResponseError, |
92 | ChecksumMismatchError, |
93 | InvalidParametersError, |
94 | NdefReadError, |
95 | NdefWriteError, |
96 | CommandError |
97 | }; |
98 | Q_ENUM(Error) |
99 | |
100 | class RequestIdPrivate; |
101 | class Q_NFC_EXPORT RequestId |
102 | { |
103 | public: |
104 | RequestId(); |
105 | RequestId(const RequestId &other); |
106 | RequestId(RequestIdPrivate *p); |
107 | ~RequestId(); |
108 | |
109 | bool isValid() const; |
110 | |
111 | int refCount() const; |
112 | |
113 | bool operator<(const RequestId &other) const; |
114 | bool operator==(const RequestId &other) const; |
115 | bool operator!=(const RequestId &other) const; |
116 | RequestId &operator=(const RequestId &other); |
117 | |
118 | QSharedDataPointer<RequestIdPrivate> d; |
119 | }; |
120 | |
121 | explicit QNearFieldTarget(QObject *parent = nullptr); |
122 | virtual ~QNearFieldTarget(); |
123 | |
124 | virtual QByteArray uid() const = 0; |
125 | virtual QUrl url() const; |
126 | |
127 | virtual Type type() const = 0; |
128 | virtual AccessMethods accessMethods() const = 0; |
129 | |
130 | bool keepConnection() const; |
131 | bool setKeepConnection(bool isPersistent); |
132 | bool disconnect(); |
133 | |
134 | bool isProcessingCommand() const; |
135 | |
136 | // NdefAccess |
137 | virtual bool hasNdefMessage(); |
138 | virtual RequestId readNdefMessages(); |
139 | virtual RequestId writeNdefMessages(const QList<QNdefMessage> &messages); |
140 | |
141 | // TagTypeSpecificAccess |
142 | int maxCommandLength() const; |
143 | virtual RequestId sendCommand(const QByteArray &command); |
144 | virtual RequestId sendCommands(const QList<QByteArray> &commands); |
145 | |
146 | virtual bool waitForRequestCompleted(const RequestId &id, int msecs = 5000); |
147 | |
148 | QVariant requestResponse(const RequestId &id); |
149 | void setResponseForRequest(const QNearFieldTarget::RequestId &id, const QVariant &response, |
150 | bool emitRequestCompleted = true); |
151 | |
152 | protected: |
153 | Q_INVOKABLE virtual bool handleResponse(const QNearFieldTarget::RequestId &id, |
154 | const QByteArray &response); |
155 | |
156 | void reportError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id); |
157 | |
158 | Q_SIGNALS: |
159 | void disconnected(); |
160 | |
161 | void ndefMessageRead(const QNdefMessage &message); |
162 | void ndefMessagesWritten(); |
163 | |
164 | void requestCompleted(const QNearFieldTarget::RequestId &id); |
165 | |
166 | void error(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id); |
167 | |
168 | private: |
169 | QNearFieldTargetPrivate *d_ptr; |
170 | }; |
171 | |
172 | #if QT_DEPRECATED_SINCE(5, 9) |
173 | Q_NFC_EXPORT quint16 qNfcChecksum(const char * data, uint len); |
174 | #endif |
175 | |
176 | Q_DECLARE_OPERATORS_FOR_FLAGS(QNearFieldTarget::AccessMethods) |
177 | |
178 | QT_END_NAMESPACE |
179 | |
180 | Q_DECLARE_METATYPE(QNearFieldTarget::RequestId) |
181 | |
182 | #endif // QNEARFIELDTARGET_H |
183 | |