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 QtBluetooth 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 QBLUETOOTHSOCKET_H
41#define QBLUETOOTHSOCKET_H
42
43#include <QtBluetooth/qtbluetoothglobal.h>
44
45#include <QtBluetooth/qbluetooth.h>
46#include <QtBluetooth/qbluetoothaddress.h>
47#include <QtBluetooth/qbluetoothuuid.h>
48#include <QtBluetooth/qbluetoothserviceinfo.h>
49
50#include <QtCore/qiodevice.h>
51#include <QtNetwork/qabstractsocket.h>
52
53QT_BEGIN_NAMESPACE
54
55
56class QBluetoothSocketBasePrivate;
57
58class Q_BLUETOOTH_EXPORT QBluetoothSocket : public QIODevice
59{
60 Q_OBJECT
61
62 Q_DECLARE_PRIVATE(QBluetoothSocketBase)
63
64 friend class QBluetoothServer;
65 friend class QBluetoothServerPrivate;
66 friend class QBluetoothSocketPrivate;
67 friend class QBluetoothSocketPrivateAndroid;
68 friend class QBluetoothSocketPrivateBluez;
69 friend class QBluetoothSocketPrivateBluezDBus;
70 friend class QBluetoothSocketPrivateDummy;
71 friend class QBluetoothSocketPrivateWinRT;
72
73public:
74
75 // TODO Decouple SocketState and SocketError enum values from QAbstractSocket in Qt 6
76 enum SocketState {
77 UnconnectedState = QAbstractSocket::UnconnectedState,
78 ServiceLookupState = QAbstractSocket::HostLookupState,
79 ConnectingState = QAbstractSocket::ConnectingState,
80 ConnectedState = QAbstractSocket::ConnectedState,
81 BoundState = QAbstractSocket::BoundState,
82 ClosingState = QAbstractSocket::ClosingState,
83 ListeningState = QAbstractSocket::ListeningState
84 };
85 Q_ENUM(SocketState)
86
87 enum SocketError {
88 NoSocketError = -2,
89 UnknownSocketError = QAbstractSocket::UnknownSocketError, //-1
90 RemoteHostClosedError = QAbstractSocket::RemoteHostClosedError, //1
91 HostNotFoundError = QAbstractSocket::HostNotFoundError, //2
92 ServiceNotFoundError = QAbstractSocket::SocketAddressNotAvailableError, //9
93 NetworkError = QAbstractSocket::NetworkError, //7
94 UnsupportedProtocolError = 8,
95 OperationError = QAbstractSocket::OperationError //19
96 //New enums (independent of QAbstractSocket) should be added from 100 onwards
97 };
98 Q_ENUM(SocketError)
99
100 explicit QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, QObject *parent = nullptr); // create socket of type socketType
101 explicit QBluetoothSocket(QObject *parent = nullptr); // create a blank socket
102 virtual ~QBluetoothSocket();
103
104 void abort();
105 virtual void close();
106
107 bool isSequential() const;
108
109 virtual qint64 bytesAvailable() const;
110 virtual qint64 bytesToWrite() const;
111
112 virtual bool canReadLine() const;
113
114 void connectToService(const QBluetoothServiceInfo &service, OpenMode openMode = ReadWrite);
115 void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode = ReadWrite);
116 void connectToService(const QBluetoothAddress &address, quint16 port, OpenMode openMode = ReadWrite);
117 inline void connectToService(const QBluetoothAddress &address, QBluetoothUuid::ServiceClassUuid uuid,
118 OpenMode mode = ReadWrite)
119 {
120 connectToService(address, QBluetoothUuid(uuid), mode);
121 }
122 void disconnectFromService();
123
124 //bool flush();
125 //bool isValid() const;
126
127 QString localName() const;
128 QBluetoothAddress localAddress() const;
129 quint16 localPort() const;
130
131 QString peerName() const;
132 QBluetoothAddress peerAddress() const;
133 quint16 peerPort() const;
134 //QBluetoothServiceInfo peerService() const;
135
136 //qint64 readBufferSize() const;
137 //void setReadBufferSize(qint64 size);
138
139 bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
140 SocketState socketState = ConnectedState,
141 OpenMode openMode = ReadWrite);
142 int socketDescriptor() const;
143
144 QBluetoothServiceInfo::Protocol socketType() const;
145 SocketState state() const;
146 SocketError error() const;
147 QString errorString() const;
148
149 //bool waitForConnected(int msecs = 30000);
150 //bool waitForDisconnected(int msecs = 30000);
151 //virtual bool waitForReadyRead(int msecs = 30000);
152
153 void setPreferredSecurityFlags(QBluetooth::SecurityFlags flags);
154 QBluetooth::SecurityFlags preferredSecurityFlags() const;
155
156Q_SIGNALS:
157 void connected();
158 void disconnected();
159 void error(QBluetoothSocket::SocketError error);
160 void stateChanged(QBluetoothSocket::SocketState state);
161
162protected:
163 virtual qint64 readData(char *data, qint64 maxSize);
164 virtual qint64 writeData(const char *data, qint64 maxSize);
165
166 void setSocketState(SocketState state);
167 void setSocketError(SocketError error);
168
169 void doDeviceDiscovery(const QBluetoothServiceInfo &service, OpenMode openMode);
170
171private Q_SLOTS:
172 void serviceDiscovered(const QBluetoothServiceInfo &service);
173 void discoveryFinished();
174
175
176protected:
177#if QT_CONFIG(bluez)
178 //evil hack to enable QBluetoothServer on Bluez to set the desired d_ptr
179 explicit QBluetoothSocket(QBluetoothSocketBasePrivate *d,
180 QBluetoothServiceInfo::Protocol socketType,
181 QObject *parent = nullptr);
182#endif
183
184 QBluetoothSocketBasePrivate *d_ptr;
185
186private:
187 friend class QLowEnergyControllerPrivateBluez;
188};
189
190#ifndef QT_NO_DEBUG_STREAM
191Q_BLUETOOTH_EXPORT QDebug operator<<(QDebug, QBluetoothSocket::SocketError);
192Q_BLUETOOTH_EXPORT QDebug operator<<(QDebug, QBluetoothSocket::SocketState);
193#endif
194
195QT_END_NAMESPACE
196
197#endif
198