summaryrefslogtreecommitdiffstats
path: root/src/nfc/qllcpsocket.cpp
blob: 79cb12846bac33cec92ad47e481768da90ab5ba2 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtNfc module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qllcpsocket_p.h"

#if defined(QT_SIMULATOR)
#include "qllcpsocket_simulator_p.h"
#elif defined(QNX_NFC)
#include "qllcpsocket_qnx_p.h"
#else
#include "qllcpsocket_p_p.h"
#endif

QT_BEGIN_NAMESPACE

/*!
    \class QLlcpSocket
    \brief The QLlcpSocket class provides an NFC LLCP socket.
    \internal

    \ingroup connectivity-nfc
    \inmodule QtNfc

    NFC LLCP protocol is a peer-to-peer communication protocol between two NFC compliant devices.
*/

/*!
    \enum QLlcpSocket::SocketError

    This enum describes the errors that can occur. The most recent error can be retrieved through a
    call to error().

    \value UnknownSocketError       An unidentified error has occurred.
    \value RemoteHostClosedError    The remote host closed the connection.
    \value SocketAccessError        The socket operation failed because the application lacked the
                                    required privileges.
    \value SocketResourceError      The local system ran out of resources (e.g., too many sockets).
*/

/*!
    \enum QLlcpSocket::SocketState

    This enum describes the different state in which a socket can be.

    \value UnconnectedState The socket is not connected.
    \value ConnectingState  The socket has started establishing a connection.
    \value ConnectedState   A connection is established.
    \value ClosingState     The socket is about to close.
    \value BoundState       The socket is bound to a local port (for servers).
    \value ListeningState   The socket is listening for incoming connections (for internal use).
*/

/*!
    \fn QLlcpSocket::connected()

    This signal is emitted after connectToService() has been called and a connection has been
    successfully established.

    \sa connectToService(), disconnected()
*/

/*!
    \fn QLlcpSocket::disconnected()

    This signal is emitted when the socket has been disconnected.

    \sa disconnectFromService(),
*/

/*!
    \fn QLlcpSocket::error(QLlcpSocket::SocketError socketError)

    This signal is emitted when an error occurs. The \a socketError parameter describes the error.
*/

/*!
    \fn QLlcpSocket::stateChanged(QLlcpSocket::SocketState socketState)

    This signal is emitted when the state of the socket changes. The \a socketState parameter
    describes the new state.
*/

/*!
    Construct a new unconnected LLCP socket with \a parent.
*/
QLlcpSocket::QLlcpSocket(QObject *parent)
:   QIODevice(parent), d_ptr(new QLlcpSocketPrivate(this))
{
    setOpenMode(QIODevice::NotOpen);
}

/*!
    \internal
*/
QLlcpSocket::QLlcpSocket(QLlcpSocketPrivate *d, QObject *parent)
:   QIODevice(parent), d_ptr(d)
{
    setOpenMode(QIODevice::ReadWrite);
    d_ptr->q_ptr = this;
}

/*!
    Destroys the LLCP socket.
*/
QLlcpSocket::~QLlcpSocket()
{
    delete d_ptr;
}

/*!
    Connects to the service identified by the URI \a serviceUri on \a target.
*/
void QLlcpSocket::connectToService(QNearFieldTarget *target, const QString &serviceUri)
{
    Q_D(QLlcpSocket);

    d->connectToService(target, serviceUri);
}

/*!
    Disconnects the socket.
*/
void QLlcpSocket::disconnectFromService()
{
    Q_D(QLlcpSocket);

    d->disconnectFromService();
}

/*!
    Disconnects the socket.
*/
void QLlcpSocket::close()
{
    Q_D(QLlcpSocket);

    QIODevice::close();

    d->disconnectFromService();
}

/*!
    Binds the LLCP socket to local \a port. Returns true on success; otherwise returns false.
*/
bool QLlcpSocket::bind(quint8 port)
{
    Q_D(QLlcpSocket);

    return d->bind(port);
}

/*!
    Returns true if at least one datagram (service data units) is waiting to be read; otherwise
    returns false.

    \sa pendingDatagramSize(), readDatagram()
*/
bool QLlcpSocket::hasPendingDatagrams() const
{
    Q_D(const QLlcpSocket);

    return d->hasPendingDatagrams();
}

/*!
    Returns the size of the first pending datagram (service data unit). If there is no datagram
    available, this function returns -1.

    \sa hasPendingDatagrams(), readDatagram()
*/
qint64 QLlcpSocket::pendingDatagramSize() const
{
    Q_D(const QLlcpSocket);

    return d->pendingDatagramSize();
}

/*!
    Sends the datagram at \a data of size \a size to the service that this socket is connected to.
    Returns the number of bytes sent on success; otherwise return -1;
*/
qint64 QLlcpSocket::writeDatagram(const char *data, qint64 size)
{
    Q_D(QLlcpSocket);

    return d->writeDatagram(data, size);
}

/*!
    \reimp

    Always returns true.
*/
bool QLlcpSocket::isSequential() const
{
    return true;
}

/*!
    \overload

    Sends the datagram \a datagram to the service that this socket is connected to.
*/
qint64 QLlcpSocket::writeDatagram(const QByteArray &datagram)
{
    Q_D(QLlcpSocket);

    return d->writeDatagram(datagram);
}

/*!
    Receives a datagram no larger than \a maxSize bytes and stores it in \a data. The sender's
    details are stored in \a target and \a port (unless the pointers are 0).

    Returns the size of the datagram on success; otherwise returns -1.

    If maxSize is too small, the rest of the datagram will be lost. To avoid loss of data, call
    pendingDatagramSize() to determine the size of the pending datagram before attempting to read
    it. If maxSize is 0, the datagram will be discarded.

    \sa writeDatagram(), hasPendingDatagrams(), pendingDatagramSize()
*/
qint64 QLlcpSocket::readDatagram(char *data, qint64 maxSize, QNearFieldTarget **target,
                                 quint8 *port)
{
    Q_D(QLlcpSocket);

    return d->readDatagram(data, maxSize, target, port);
}

/*!
    Sends the datagram at \a data of size \a size to the service identified by the URI
    \a port on \a target. Returns the number of bytes sent on success; otherwise returns -1.

    \sa readDatagram()
*/
qint64 QLlcpSocket::writeDatagram(const char *data, qint64 size, QNearFieldTarget *target,
                                  quint8 port)
{
    Q_D(QLlcpSocket);

    return d->writeDatagram(data, size, target, port);
}

/*!
    \overload

    Sends the datagram \a datagram to the service identified by the URI \a port on \a target.
*/
qint64 QLlcpSocket::writeDatagram(const QByteArray &datagram, QNearFieldTarget *target,
                                  quint8 port)
{
    Q_D(QLlcpSocket);

    return d->writeDatagram(datagram, target, port);
}

/*!
    Returns the type of error that last occurred.
*/
QLlcpSocket::SocketError QLlcpSocket::error() const
{
    Q_D(const QLlcpSocket);

    return d->error();
}

/*!
    Returns the state of the socket.
*/
QLlcpSocket::SocketState QLlcpSocket::state() const
{
    Q_D(const QLlcpSocket);

    return d->state();
}

/*!
    \reimp
*/
qint64 QLlcpSocket::bytesAvailable() const
{
    Q_D(const QLlcpSocket);

    return d->bytesAvailable() + QIODevice::bytesAvailable();
}

/*!
    \reimp
*/
bool QLlcpSocket::canReadLine() const
{
    Q_D(const QLlcpSocket);

    return d->canReadLine() || QIODevice::canReadLine();
}

/*!
    \reimp
*/
bool QLlcpSocket::waitForReadyRead(int msecs)
{
    Q_D(QLlcpSocket);

    return d->waitForReadyRead(msecs);
}

/*!
    \reimp
*/
bool QLlcpSocket::waitForBytesWritten(int msecs)
{
    Q_D(QLlcpSocket);

    return d->waitForBytesWritten(msecs);
}

/*!
    Waits until the socket is connected, up to \a msecs milliseconds. If the connection has been
    established, this function returns true; otherwise it returns false. In the case where it
    returns false, you can call error() to determine the cause of the error.

    If msecs is -1, this function will not time out.
*/
bool QLlcpSocket::waitForConnected(int msecs)
{
    Q_D(QLlcpSocket);

    return d->waitForConnected(msecs);
}

/*!
    Waits until the socket is disconnected, up to \a msecs milliseconds. If the connection has been
    disconnected, this function returns true; otherwise it returns false. In the case where it
    returns false, you can call error() to determine the cause of the error.

    If msecs is -1, this function will not time out.
*/
bool QLlcpSocket::waitForDisconnected(int msecs)
{
    Q_D(QLlcpSocket);

    return d->waitForDisconnected(msecs);
}

/*!
    \internal
*/
qint64 QLlcpSocket::readData(char *data, qint64 maxlen)
{
    Q_D(QLlcpSocket);

    return d->readData(data, maxlen);
}

/*!
    \internal
*/
qint64 QLlcpSocket::writeData(const char *data, qint64 len)
{
    Q_D(QLlcpSocket);

    return d->writeData(data, len);
}

QT_END_NAMESPACE