1// Copyright (C) 2018 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 QBLUETOOTHSOCKETBASEPRIVATE_P_H
5#define QBLUETOOTHSOCKETBASEPRIVATE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <qglobal.h>
19#include <QObject>
20#include <QtBluetooth/qbluetoothsocket.h>
21
22#if defined(QT_ANDROID_BLUETOOTH)
23#include <QtCore/QJniObject>
24#endif
25
26#if defined(QT_WINRT_BLUETOOTH)
27#include <QtCore/QMutex>
28
29#include <wrl.h>
30
31namespace ABI {
32 namespace Windows {
33 namespace Networking {
34 namespace Sockets {
35 struct IStreamSocket;
36 }
37 }
38 namespace Foundation {
39 struct IAsyncAction;
40 enum class AsyncStatus;
41 }
42 }
43}
44#endif // QT_WINRT_BLUETOOTH
45
46#ifndef QPRIVATELINEARBUFFER_BUFFERSIZE
47#define QPRIVATELINEARBUFFER_BUFFERSIZE Q_INT64_C(16384)
48#endif
49#include "qprivatelinearbuffer_p.h"
50
51QT_FORWARD_DECLARE_CLASS(QSocketNotifier)
52QT_FORWARD_DECLARE_CLASS(QBluetoothServiceDiscoveryAgent)
53
54QT_BEGIN_NAMESPACE
55
56class QBluetoothSocketBasePrivate : public QObject
57{
58 Q_OBJECT
59
60public:
61 explicit QBluetoothSocketBasePrivate(QObject *parent = nullptr);
62 virtual ~QBluetoothSocketBasePrivate();
63
64 virtual bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type) = 0;
65
66 virtual QString localName() const = 0;
67 virtual QBluetoothAddress localAddress() const = 0;
68 virtual quint16 localPort() const = 0;
69
70 virtual QString peerName() const = 0;
71 virtual QBluetoothAddress peerAddress() const = 0;
72 virtual quint16 peerPort() const = 0;
73
74 virtual void abort() = 0;
75 virtual void close() = 0;
76
77 virtual qint64 writeData(const char *data, qint64 maxSize) = 0;
78 virtual qint64 readData(char *data, qint64 maxSize) = 0;
79
80 virtual qint64 bytesAvailable() const = 0;
81 virtual bool canReadLine() const = 0;
82 virtual qint64 bytesToWrite() const = 0;
83
84 virtual bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
85 QBluetoothSocket::SocketState socketState = QBluetoothSocket::SocketState::ConnectedState,
86 QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) = 0;
87
88
89#if defined(QT_ANDROID_BLUETOOTH)
90 virtual void connectToServiceHelper(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
91 QIODevice::OpenMode openMode) = 0;
92#else
93 virtual void connectToServiceHelper(const QBluetoothAddress &address, quint16 port,
94 QIODevice::OpenMode openMode) = 0;
95#endif
96 virtual void connectToService(const QBluetoothServiceInfo &service,
97 QIODevice::OpenMode openMode) = 0;
98 virtual void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
99 QIODevice::OpenMode openMode) = 0;
100 virtual void connectToService(const QBluetoothAddress &address, quint16 port,
101 QIODevice::OpenMode openMode) = 0;
102
103#ifdef QT_ANDROID_BLUETOOTH
104 virtual bool setSocketDescriptor(const QJniObject &socket, QBluetoothServiceInfo::Protocol socketType,
105 QBluetoothSocket::SocketState socketState = QBluetoothSocket::SocketState::ConnectedState,
106 QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) = 0;
107#elif defined(QT_WINRT_BLUETOOTH)
108 virtual bool setSocketDescriptor(Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocket> socket,
109 QBluetoothServiceInfo::Protocol socketType,
110 QBluetoothSocket::SocketState socketState = QBluetoothSocket::SocketState::ConnectedState,
111 QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) = 0;
112#endif
113
114public:
115 QPrivateLinearBuffer rxBuffer;
116 QPrivateLinearBuffer txBuffer;
117 int socket = -1;
118 QBluetoothServiceInfo::Protocol socketType = QBluetoothServiceInfo::UnknownProtocol;
119 QBluetoothSocket::SocketState state = QBluetoothSocket::SocketState::UnconnectedState;
120 QBluetoothSocket::SocketError socketError = QBluetoothSocket::SocketError::NoSocketError;
121 QSocketNotifier *readNotifier = nullptr;
122 QSocketNotifier *connectWriteNotifier = nullptr;
123 bool connecting = false;
124
125 QBluetoothServiceDiscoveryAgent *discoveryAgent = nullptr;
126 QBluetoothSocket::OpenMode openMode;
127 QBluetooth::SecurityFlags secFlags;
128
129 QString errorString;
130
131protected:
132 Q_DECLARE_PUBLIC(QBluetoothSocket)
133 QBluetoothSocket *q_ptr;
134
135#if QT_CONFIG(bluez)
136public:
137 quint8 lowEnergySocketType = 0;
138#endif
139};
140
141static inline void convertAddress(const quint64 from, quint8 (&to)[6])
142{
143 to[0] = (from >> 0) & 0xff;
144 to[1] = (from >> 8) & 0xff;
145 to[2] = (from >> 16) & 0xff;
146 to[3] = (from >> 24) & 0xff;
147 to[4] = (from >> 32) & 0xff;
148 to[5] = (from >> 40) & 0xff;
149}
150
151static inline quint64 convertAddress(const quint8 (&from)[6], quint64 *to = nullptr)
152{
153 const quint64 result = (quint64(from[0]) << 0) |
154 (quint64(from[1]) << 8) |
155 (quint64(from[2]) << 16) |
156 (quint64(from[3]) << 24) |
157 (quint64(from[4]) << 32) |
158 (quint64(from[5]) << 40);
159 if (to)
160 *to = result;
161 return result;
162}
163
164QT_END_NAMESPACE
165
166#endif // QBLUETOOTHSOCKETBASE_P_H
167

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