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