1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QBLUETOOTHSOCKET_H
5#define QBLUETOOTHSOCKET_H
6
7#include <QtBluetooth/qtbluetoothglobal.h>
8
9#include <QtBluetooth/qbluetooth.h>
10#include <QtBluetooth/qbluetoothaddress.h>
11#include <QtBluetooth/qbluetoothuuid.h>
12#include <QtBluetooth/qbluetoothserviceinfo.h>
13
14#include <QtCore/qiodevice.h>
15
16QT_BEGIN_NAMESPACE
17
18
19class QBluetoothSocketBasePrivate;
20
21class Q_BLUETOOTH_EXPORT QBluetoothSocket : public QIODevice
22{
23 Q_OBJECT
24
25 Q_DECLARE_PRIVATE(QBluetoothSocketBase)
26
27 friend class QBluetoothServer;
28 friend class QBluetoothServerPrivate;
29 friend class QBluetoothSocketPrivateDarwin;
30 friend class QBluetoothSocketPrivateAndroid;
31 friend class QBluetoothSocketPrivateBluez;
32 friend class QBluetoothSocketPrivateBluezDBus;
33 friend class QBluetoothSocketPrivateDummy;
34 friend class QBluetoothSocketPrivateWin;
35 friend class QBluetoothSocketPrivateWinRT;
36
37public:
38
39 enum class SocketState {
40 UnconnectedState,
41 ServiceLookupState,
42 ConnectingState,
43 ConnectedState,
44 BoundState,
45 ClosingState,
46 ListeningState
47 };
48 Q_ENUM(SocketState)
49
50 enum class SocketError {
51 NoSocketError,
52 UnknownSocketError,
53 RemoteHostClosedError,
54 HostNotFoundError,
55 ServiceNotFoundError,
56 NetworkError,
57 UnsupportedProtocolError,
58 OperationError,
59 MissingPermissionsError
60 };
61 Q_ENUM(SocketError)
62
63 explicit QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, QObject *parent = nullptr); // create socket of type socketType
64 explicit QBluetoothSocket(QObject *parent = nullptr); // create a blank socket
65 virtual ~QBluetoothSocket();
66
67 void abort();
68
69 void close() override;
70
71 bool isSequential() const override;
72
73 qint64 bytesAvailable() const override;
74 qint64 bytesToWrite() const override;
75
76 bool canReadLine() const override;
77
78 void connectToService(const QBluetoothServiceInfo &service, OpenMode openMode = ReadWrite);
79 void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode = ReadWrite);
80 void connectToService(const QBluetoothAddress &address, quint16 port, OpenMode openMode = ReadWrite);
81 inline void connectToService(const QBluetoothAddress &address, QBluetoothUuid::ServiceClassUuid uuid,
82 OpenMode mode = ReadWrite)
83 {
84 connectToService(address, uuid: QBluetoothUuid(uuid), openMode: mode);
85 }
86 void disconnectFromService();
87
88 //bool flush();
89 //bool isValid() const;
90
91 QString localName() const;
92 QBluetoothAddress localAddress() const;
93 quint16 localPort() const;
94
95 QString peerName() const;
96 QBluetoothAddress peerAddress() const;
97 quint16 peerPort() const;
98 //QBluetoothServiceInfo peerService() const;
99
100 //qint64 readBufferSize() const;
101 //void setReadBufferSize(qint64 size);
102
103 bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
104 SocketState socketState = SocketState::ConnectedState,
105 OpenMode openMode = ReadWrite);
106 int socketDescriptor() const;
107
108 QBluetoothServiceInfo::Protocol socketType() const;
109 SocketState state() const;
110 SocketError error() const;
111 QString errorString() const;
112
113 //bool waitForConnected(int msecs = 30000);
114 //bool waitForDisconnected(int msecs = 30000);
115 //virtual bool waitForReadyRead(int msecs = 30000);
116
117 void setPreferredSecurityFlags(QBluetooth::SecurityFlags flags);
118 QBluetooth::SecurityFlags preferredSecurityFlags() const;
119
120Q_SIGNALS:
121 void connected();
122 void disconnected();
123 void errorOccurred(QBluetoothSocket::SocketError error);
124 void stateChanged(QBluetoothSocket::SocketState state);
125
126protected:
127 qint64 readData(char *data, qint64 maxSize) override;
128 qint64 writeData(const char *data, qint64 maxSize) override;
129
130 void setSocketState(SocketState state);
131 void setSocketError(SocketError error);
132
133 void doDeviceDiscovery(const QBluetoothServiceInfo &service, OpenMode openMode);
134
135private Q_SLOTS:
136 void serviceDiscovered(const QBluetoothServiceInfo &service);
137 void discoveryFinished();
138
139
140protected:
141#if QT_CONFIG(bluez)
142 //evil hack to enable QBluetoothServer on Bluez to set the desired d_ptr
143 explicit QBluetoothSocket(QBluetoothSocketBasePrivate *d,
144 QBluetoothServiceInfo::Protocol socketType,
145 QObject *parent = nullptr);
146#endif
147
148 QBluetoothSocketBasePrivate *d_ptr;
149
150private:
151 friend class QLowEnergyControllerPrivateBluez;
152};
153
154
155QT_END_NAMESPACE
156
157#endif
158

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