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

source code of qtconnectivity/src/bluetooth/qbluetoothsocket.h