diff options
author | Alex Blasche <alexander.blasche@qt.io> | 2018-07-18 11:03:53 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@qt.io> | 2018-07-25 11:16:09 +0000 |
commit | 28eeb7afc27e99deeb52eefe10cdd458c7621698 (patch) | |
tree | 99d69ac21efebe315149cc9501ab5ed6ec4bfca4 /src/bluetooth | |
parent | 98aa6ba02628eb29cc82361fad6d2d8b18ae0488 (diff) |
Add runtime polymorphism for QBluetoothSocketPrivate
This patch introduces a generic interface towards QBluetoothSocketPrivate.
Later on, the QBluetoothSocketPrivate class will be split into
platform specific overloads/interfaces.
Ultimately, this will be needed to support runtime selection of
the QBluetootSocket d-pointer on Linux. The Linux Bluez5 implementation is
significantly different from the Bluez4 (raw socket) implementation.
Since recent Bluez5 releases the raw socket implementation is no longer
functional and/or the user has to have root permission
and enable bluetooth --compat mode. Therefore a second QBluetoothSocket
for the dbus socket API is needed. QBLuetoothSocket has to choose at runtime
(during its instanciation) which implementation to use.
Task-number: QTBUG-68550
Change-Id: I5d0b8e24b8acd1b149b897f52f0d82eade7f3823
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Lubomir I. Ivanov <neolit123@gmail.com>
Diffstat (limited to 'src/bluetooth')
-rw-r--r-- | src/bluetooth/bluetooth.pro | 5 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserver_osx.mm | 2 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket.cpp | 66 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket.h | 14 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_android.cpp | 9 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_bluez.cpp | 12 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_osx.mm | 8 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_osx_p.h | 5 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_p.cpp | 7 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_p.h | 149 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_winrt.cpp | 9 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocketbase.cpp | 54 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocketbase_p.h | 193 |
13 files changed, 343 insertions, 190 deletions
diff --git a/src/bluetooth/bluetooth.pro b/src/bluetooth/bluetooth.pro index ee548208..28470835 100644 --- a/src/bluetooth/bluetooth.pro +++ b/src/bluetooth/bluetooth.pro @@ -41,6 +41,7 @@ PRIVATE_HEADERS += \ qbluetoothdevicediscoveryagent_p.h\ qbluetoothservicediscoveryagent_p.h\ qbluetoothsocket_p.h\ + qbluetoothsocketbase_p.h \ qbluetoothserver_p.h\ qbluetoothtransferreply_p.h \ qbluetoothtransferrequest_p.h \ @@ -60,6 +61,7 @@ SOURCES += \ qbluetoothdevicediscoveryagent.cpp\ qbluetoothservicediscoveryagent.cpp\ qbluetoothsocket.cpp\ + qbluetoothsocketbase.cpp \ qbluetoothserver.cpp \ qbluetoothlocaldevice.cpp \ qbluetooth.cpp \ @@ -173,10 +175,13 @@ qtConfig(bluez) { qbluetoothtransferreply_osx_p.h \ qlowenergycontroller_osx_p.h + PRIVATE_HEADERS -= qbluetoothsocket_p.h + SOURCES -= qbluetoothdevicediscoveryagent.cpp SOURCES -= qbluetoothserviceinfo.cpp SOURCES -= qbluetoothservicediscoveryagent.cpp SOURCES -= qbluetoothsocket.cpp + SOURCES -= qbluetoothsocketbase.cpp SOURCES -= qbluetoothserver.cpp SOURCES -= qlowenergyservice_p.cpp SOURCES -= qlowenergyservice.cpp diff --git a/src/bluetooth/qbluetoothserver_osx.mm b/src/bluetooth/qbluetoothserver_osx.mm index a1774d14..d7f29ed3 100644 --- a/src/bluetooth/qbluetoothserver_osx.mm +++ b/src/bluetooth/qbluetoothserver_osx.mm @@ -43,7 +43,7 @@ // The order is important: a workround for // a private header included by private header // (incorrectly handled dependencies). -#include "qbluetoothsocket_p.h" +#include "qbluetoothsocketbase_p.h" #include "qbluetoothsocket_osx_p.h" #include "qbluetoothlocaldevice.h" diff --git a/src/bluetooth/qbluetoothsocket.cpp b/src/bluetooth/qbluetoothsocket.cpp index 214201b9..6a5178db 100644 --- a/src/bluetooth/qbluetoothsocket.cpp +++ b/src/bluetooth/qbluetoothsocket.cpp @@ -250,11 +250,12 @@ Q_DECLARE_LOGGING_CATEGORY(QT_BT) Constructs a Bluetooth socket of \a socketType type, with \a parent. */ QBluetoothSocket::QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, QObject *parent) -: QIODevice(parent), d_ptr(new QBluetoothSocketPrivate) +: QIODevice(parent) { + d_ptr = new QBluetoothSocketPrivate(); d_ptr->q_ptr = this; - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); d->ensureNativeSocket(socketType); setOpenMode(QIODevice::NotOpen); @@ -264,8 +265,9 @@ QBluetoothSocket::QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, Q Constructs a Bluetooth socket with \a parent. */ QBluetoothSocket::QBluetoothSocket(QObject *parent) - : QIODevice(parent), d_ptr(new QBluetoothSocketPrivate) + : QIODevice(parent) { + d_ptr = new QBluetoothSocketPrivate(); d_ptr->q_ptr = this; setOpenMode(QIODevice::NotOpen); } @@ -294,7 +296,7 @@ bool QBluetoothSocket::isSequential() const */ qint64 QBluetoothSocket::bytesAvailable() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return QIODevice::bytesAvailable() + d->bytesAvailable(); } @@ -304,7 +306,7 @@ qint64 QBluetoothSocket::bytesAvailable() const */ qint64 QBluetoothSocket::bytesToWrite() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->bytesToWrite(); } @@ -327,7 +329,7 @@ qint64 QBluetoothSocket::bytesToWrite() const */ void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, OpenMode openMode) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); if (state() != QBluetoothSocket::UnconnectedState && state() != QBluetoothSocket::ServiceLookupState) { qCWarning(QT_BT) << "QBluetoothSocket::connectToService called on busy socket"; @@ -419,7 +421,7 @@ void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, Op */ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); if (state() != QBluetoothSocket::UnconnectedState) { qCWarning(QT_BT) << "QBluetoothSocket::connectToService called on busy socket"; @@ -480,7 +482,7 @@ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const */ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, quint16 port, OpenMode openMode) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); #if defined(QT_ANDROID_BLUETOOTH) Q_UNUSED(port); Q_UNUSED(openMode); @@ -526,7 +528,7 @@ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, quint1 */ QBluetoothServiceInfo::Protocol QBluetoothSocket::socketType() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->socketType; } @@ -535,7 +537,7 @@ QBluetoothServiceInfo::Protocol QBluetoothSocket::socketType() const */ QBluetoothSocket::SocketState QBluetoothSocket::state() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->state; } @@ -544,7 +546,7 @@ QBluetoothSocket::SocketState QBluetoothSocket::state() const */ QBluetoothSocket::SocketError QBluetoothSocket::error() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->socketError; } @@ -553,7 +555,7 @@ QBluetoothSocket::SocketError QBluetoothSocket::error() const */ QString QBluetoothSocket::errorString() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->errorString; } @@ -586,7 +588,7 @@ QString QBluetoothSocket::errorString() const */ void QBluetoothSocket::setPreferredSecurityFlags(QBluetooth::SecurityFlags flags) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); if (d->secFlags != flags) d->secFlags = flags; } @@ -607,7 +609,7 @@ void QBluetoothSocket::setPreferredSecurityFlags(QBluetooth::SecurityFlags flags */ QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->secFlags; } @@ -616,7 +618,7 @@ QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags() const */ void QBluetoothSocket::setSocketState(QBluetoothSocket::SocketState state) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); SocketState old = d->state; d->state = state; if(old != d->state) @@ -636,7 +638,7 @@ void QBluetoothSocket::setSocketState(QBluetoothSocket::SocketState state) bool QBluetoothSocket::canReadLine() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->canReadLine(); } @@ -645,7 +647,7 @@ bool QBluetoothSocket::canReadLine() const */ void QBluetoothSocket::setSocketError(QBluetoothSocket::SocketError error_) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); d->socketError = error_; emit error(error_); } @@ -658,7 +660,7 @@ void QBluetoothSocket::setSocketError(QBluetoothSocket::SocketError error_) void QBluetoothSocket::doDeviceDiscovery(const QBluetoothServiceInfo &service, OpenMode openMode) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); setSocketState(QBluetoothSocket::ServiceLookupState); qCDebug(QT_BT) << "Starting Bluetooth service discovery"; @@ -697,7 +699,7 @@ void QBluetoothSocket::doDeviceDiscovery(const QBluetoothServiceInfo &service, O void QBluetoothSocket::serviceDiscovered(const QBluetoothServiceInfo &service) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); qCDebug(QT_BT) << "FOUND SERVICE!" << service; if (service.protocolServiceMultiplexer() > 0 || service.serverChannel() > 0) { connectToService(service, d->openMode); @@ -711,7 +713,7 @@ void QBluetoothSocket::serviceDiscovered(const QBluetoothServiceInfo &service) void QBluetoothSocket::discoveryFinished() { qCDebug(QT_BT) << "Socket discovery finished"; - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); if (d->discoveryAgent){ qCDebug(QT_BT) << "Didn't find any"; d->errorString = tr("Service cannot be found"); @@ -727,7 +729,7 @@ void QBluetoothSocket::abort() if (state() == UnconnectedState) return; - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); setOpenMode(QIODevice::NotOpen); if (state() == ServiceLookupState && d->discoveryAgent) { @@ -754,43 +756,43 @@ void QBluetoothSocket::disconnectFromService() QString QBluetoothSocket::localName() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->localName(); } QBluetoothAddress QBluetoothSocket::localAddress() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->localAddress(); } quint16 QBluetoothSocket::localPort() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->localPort(); } QString QBluetoothSocket::peerName() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->peerName(); } QBluetoothAddress QBluetoothSocket::peerAddress() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->peerAddress(); } quint16 QBluetoothSocket::peerPort() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->peerPort(); } qint64 QBluetoothSocket::writeData(const char *data, qint64 maxSize) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); if (!data || maxSize <= 0) { d_ptr->errorString = tr("Invalid data/data size"); @@ -803,7 +805,7 @@ qint64 QBluetoothSocket::writeData(const char *data, qint64 maxSize) qint64 QBluetoothSocket::readData(char *data, qint64 maxSize) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); return d->readData(data, maxSize); } @@ -812,7 +814,7 @@ void QBluetoothSocket::close() if (state() == UnconnectedState) return; - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); setOpenMode(QIODevice::NotOpen); if (state() == ServiceLookupState && d->discoveryAgent) { @@ -844,7 +846,7 @@ void QBluetoothSocket::close() bool QBluetoothSocket::setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, SocketState socketState, OpenMode openMode) { - Q_D(QBluetoothSocket); + Q_D(QBluetoothSocketBase); return d->setSocketDescriptor(socketDescriptor, socketType, socketState, openMode); } @@ -855,7 +857,7 @@ bool QBluetoothSocket::setSocketDescriptor(int socketDescriptor, QBluetoothServi int QBluetoothSocket::socketDescriptor() const { - Q_D(const QBluetoothSocket); + Q_D(const QBluetoothSocketBase); return d->socket; } diff --git a/src/bluetooth/qbluetoothsocket.h b/src/bluetooth/qbluetoothsocket.h index a7f43170..e7e59abf 100644 --- a/src/bluetooth/qbluetoothsocket.h +++ b/src/bluetooth/qbluetoothsocket.h @@ -52,15 +52,25 @@ QT_BEGIN_NAMESPACE +#ifndef QT_OSX_BLUETOOTH +class QBluetoothSocketBasePrivate; +#else class QBluetoothSocketPrivate; +#endif class Q_BLUETOOTH_EXPORT QBluetoothSocket : public QIODevice { Q_OBJECT +#ifndef QT_OSX_BLUETOOTH + Q_DECLARE_PRIVATE(QBluetoothSocketBase) +#else Q_DECLARE_PRIVATE(QBluetoothSocket) +#endif + friend class QBluetoothServer; friend class QBluetoothServerPrivate; + friend class QBluetoothSocketPrivate; public: @@ -166,7 +176,11 @@ private Q_SLOTS: protected: +#ifndef QT_OSX_BLUETOOTH + QBluetoothSocketBasePrivate *d_ptr; +#else QBluetoothSocketPrivate *d_ptr; +#endif private: friend class QLowEnergyControllerPrivateBluez; diff --git a/src/bluetooth/qbluetoothsocket_android.cpp b/src/bluetooth/qbluetoothsocket_android.cpp index a70b95a2..9a81fc3a 100644 --- a/src/bluetooth/qbluetoothsocket_android.cpp +++ b/src/bluetooth/qbluetoothsocket_android.cpp @@ -205,15 +205,10 @@ static QBluetoothUuid reverseUuid(const QBluetoothUuid &serviceUuid) } QBluetoothSocketPrivate::QBluetoothSocketPrivate() - : socket(-1), - socketType(QBluetoothServiceInfo::UnknownProtocol), - state(QBluetoothSocket::UnconnectedState), - socketError(QBluetoothSocket::NoSocketError), - connecting(false), - discoveryAgent(0), - secFlags(QBluetooth::Secure), + : inputThread(0) { + secFlags = QBluetooth::Secure; adapter = QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter", "getDefaultAdapter", "()Landroid/bluetooth/BluetoothAdapter;"); diff --git a/src/bluetooth/qbluetoothsocket_bluez.cpp b/src/bluetooth/qbluetoothsocket_bluez.cpp index 17b8e738..4fb51567 100644 --- a/src/bluetooth/qbluetoothsocket_bluez.cpp +++ b/src/bluetooth/qbluetoothsocket_bluez.cpp @@ -63,17 +63,9 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(QT_BT_BLUEZ) QBluetoothSocketPrivate::QBluetoothSocketPrivate() - : socket(-1), - socketType(QBluetoothServiceInfo::UnknownProtocol), - state(QBluetoothSocket::UnconnectedState), - socketError(QBluetoothSocket::NoSocketError), - readNotifier(0), - connectWriteNotifier(0), - connecting(false), - discoveryAgent(0), - secFlags(QBluetooth::Authorization), - lowEnergySocketType(0) + : QBluetoothSocketBasePrivate() { + secFlags = QBluetooth::Authorization; } QBluetoothSocketPrivate::~QBluetoothSocketPrivate() diff --git a/src/bluetooth/qbluetoothsocket_osx.mm b/src/bluetooth/qbluetoothsocket_osx.mm index dec542dd..ea39bb39 100644 --- a/src/bluetooth/qbluetoothsocket_osx.mm +++ b/src/bluetooth/qbluetoothsocket_osx.mm @@ -41,9 +41,7 @@ // The order is important (the first header contains // the base class for a private socket) - workaround for // dependencies problem. -#include "qbluetoothsocket_p.h" -#include "qbluetoothsocket_osx_p.h" -// +#include "qbluetoothsocketbase_p.h" #include "qbluetoothsocket_osx_p.h" #include "qbluetoothlocaldevice.h" #include "qbluetoothdeviceinfo.h" @@ -60,14 +58,14 @@ QT_BEGIN_NAMESPACE QBluetoothSocketPrivate::QBluetoothSocketPrivate() - : q_ptr(nullptr), - writeChunk(std::numeric_limits<UInt16>::max()), + : writeChunk(std::numeric_limits<UInt16>::max()), openMode(QIODevice::NotOpen), // That's what is set in public class' ctors. state(QBluetoothSocket::UnconnectedState), socketType(QBluetoothServiceInfo::UnknownProtocol), socketError(QBluetoothSocket::NoSocketError), isConnecting(false) { + q_ptr = nullptr; } QBluetoothSocketPrivate::~QBluetoothSocketPrivate() diff --git a/src/bluetooth/qbluetoothsocket_osx_p.h b/src/bluetooth/qbluetoothsocket_osx_p.h index ce376702..dcc684b8 100644 --- a/src/bluetooth/qbluetoothsocket_osx_p.h +++ b/src/bluetooth/qbluetoothsocket_osx_p.h @@ -79,10 +79,9 @@ QT_BEGIN_NAMESPACE -class QBluetoothServiceDiscoveryAgent; class QBluetoothAddress; -class QBluetoothSocketPrivate : public QBluetoothSocketPrivateBase, public OSXBluetooth::ChannelDelegate +class QBluetoothSocketPrivate : public QBluetoothSocketBasePrivate, public OSXBluetooth::ChannelDelegate { friend class QBluetoothSocket; friend class QBluetoothServer; @@ -121,8 +120,6 @@ private: qint64 writeData(const char *data, qint64 maxSize); qint64 readData(char *data, qint64 maxSize); - QBluetoothSocket *q_ptr; - QScopedPointer<QBluetoothServiceDiscoveryAgent> discoveryAgent; QPrivateLinearBuffer buffer; diff --git a/src/bluetooth/qbluetoothsocket_p.cpp b/src/bluetooth/qbluetoothsocket_p.cpp index 4c716005..bd074763 100644 --- a/src/bluetooth/qbluetoothsocket_p.cpp +++ b/src/bluetooth/qbluetoothsocket_p.cpp @@ -46,13 +46,8 @@ QT_BEGIN_NAMESPACE QBluetoothSocketPrivate::QBluetoothSocketPrivate() - : socket(-1), - socketType(QBluetoothServiceInfo::UnknownProtocol), - state(QBluetoothSocket::UnconnectedState), - socketError(QBluetoothSocket::NoSocketError), - discoveryAgent(0), - secFlags(QBluetooth::NoSecurity) { + secFlags = QBluetooth::NoSecurity; #ifndef QT_IOS_BLUETOOTH printDummyWarning(); #endif diff --git a/src/bluetooth/qbluetoothsocket_p.h b/src/bluetooth/qbluetoothsocket_p.h index ae3c6182..6b002959 100644 --- a/src/bluetooth/qbluetoothsocket_p.h +++ b/src/bluetooth/qbluetoothsocket_p.h @@ -52,82 +52,41 @@ // #include "qbluetoothsocket.h" +#include "qbluetoothsocketbase_p.h" +#include <QtGlobal> -#ifdef QT_ANDROID_BLUETOOTH +#if defined(QT_ANDROID_BLUETOOTH) #include <QtAndroidExtras/QAndroidJniObject> #include <QtCore/QPointer> #include "android/inputstreamthread_p.h" #include <jni.h> -class WorkerThread; -#endif - -#ifdef QT_WINRT_BLUETOOTH -#include <QtCore/QMutex> - -#include <wrl.h> - -namespace ABI { - namespace Windows { - namespace Networking { - namespace Sockets { - struct IStreamSocket; - } - } - namespace Foundation { - struct IAsyncAction; - enum class AsyncStatus; - } - } -} -#endif // QT_WINRT_BLUETOOTH - -#ifndef QPRIVATELINEARBUFFER_BUFFERSIZE -#define QPRIVATELINEARBUFFER_BUFFERSIZE Q_INT64_C(16384) -#endif -#include "qprivatelinearbuffer_p.h" - -#include <QtGlobal> +#endif // QT_ANDROID_BLUETOOTH QT_FORWARD_DECLARE_CLASS(QSocketNotifier) -QT_BEGIN_NAMESPACE - #ifdef QT_WINRT_BLUETOOTH -class SocketWorker; +QT_FORWARD_DECLARE_CLASS(SocketWorker) #endif -class QBluetoothServiceDiscoveryAgent; - -class QSocketServerPrivate -{ -public: - QSocketServerPrivate(); - ~QSocketServerPrivate(); -}; - - - -class QBluetoothSocket; -class QBluetoothServiceDiscoveryAgent; +QT_BEGIN_NAMESPACE -#ifndef QT_OSX_BLUETOOTH -class QBluetoothSocketPrivate : public QObject +class QBluetoothSocketPrivate : public QBluetoothSocketBasePrivate { Q_OBJECT - Q_DECLARE_PUBLIC(QBluetoothSocket) friend class QBluetoothServerPrivate; public: - QBluetoothSocketPrivate(); - ~QBluetoothSocketPrivate(); + ~QBluetoothSocketPrivate() override; //On Android we connect using the uuid not the port #if defined(QT_ANDROID_BLUETOOTH) void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, - QIODevice::OpenMode openMode); + QIODevice::OpenMode openMode) override; #else - void connectToService(const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode); + void connectToService(const QBluetoothAddress &address, + quint16 port, + QIODevice::OpenMode openMode) override; #endif #ifdef QT_ANDROID_BLUETOOTH bool fallBackConnect(QAndroidJniObject uuid, int channel); @@ -135,63 +94,39 @@ public: #endif - bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type); + bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type) override; - QString localName() const; - QBluetoothAddress localAddress() const; - quint16 localPort() const; + QString localName() const override; + QBluetoothAddress localAddress() const override; + quint16 localPort() const override; - QString peerName() const; - QBluetoothAddress peerAddress() const; - quint16 peerPort() const; - //QBluetoothServiceInfo peerService() const; + QString peerName() const override; + QBluetoothAddress peerAddress() const override; + quint16 peerPort() const override; - void abort(); - void close(); + void abort() override; + void close() override; - //qint64 readBufferSize() const; - //void setReadBufferSize(qint64 size); - - qint64 writeData(const char *data, qint64 maxSize); - qint64 readData(char *data, qint64 maxSize); + qint64 writeData(const char *data, qint64 maxSize) override; + qint64 readData(char *data, qint64 maxSize) override; #ifdef QT_ANDROID_BLUETOOTH bool setSocketDescriptor(const QAndroidJniObject &socket, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState = QBluetoothSocket::ConnectedState, - QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite); + QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) override; #elif defined(QT_WINRT_BLUETOOTH) bool setSocketDescriptor(Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocket> socket, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState = QBluetoothSocket::ConnectedState, - QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite); + QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) override; #endif bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState = QBluetoothSocket::ConnectedState, - QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite); - - qint64 bytesAvailable() const; - bool canReadLine() const; - qint64 bytesToWrite() const; - -public: - QPrivateLinearBuffer buffer; - QPrivateLinearBuffer txBuffer; - int socket; - QBluetoothServiceInfo::Protocol socketType; - QBluetoothSocket::SocketState state; - QBluetoothSocket::SocketError socketError; - QSocketNotifier *readNotifier; - QSocketNotifier *connectWriteNotifier; - bool connecting; - - QBluetoothServiceDiscoveryAgent *discoveryAgent; - QBluetoothSocket::OpenMode openMode; - QBluetooth::SecurityFlags secFlags; + QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) override; - -// QByteArray rxBuffer; -// qint64 rxOffset; - QString errorString; + qint64 bytesAvailable() const override; + bool canReadLine() const override; + qint64 bytesToWrite() const override; #ifdef QT_ANDROID_BLUETOOTH QAndroidJniObject adapter; @@ -213,7 +148,6 @@ public slots: signals: void connectJavaSocket(); void closeJavaSocket(); - #endif #ifdef QT_WINRT_BLUETOOTH @@ -240,37 +174,16 @@ private slots: void _q_writeNotify(); #endif -protected: - QBluetoothSocket *q_ptr; - private: #ifdef QT_WINRT_BLUETOOTH - HRESULT handleConnectOpFinished(ABI::Windows::Foundation::IAsyncAction *action, ABI::Windows::Foundation::AsyncStatus status); -#endif - -#if QT_CONFIG(bluez) -public: - quint8 lowEnergySocketType; + HRESULT handleConnectOpFinished(ABI::Windows::Foundation::IAsyncAction *action, + ABI::Windows::Foundation::AsyncStatus status); #endif -}; -#else // QT_OSX_BLUETOOTH -// QBluetoothSocketPrivate on OS X can not contain -// Q_OBJECT (moc does not parse Objective-C syntax). -// But QBluetoothSocket still requires QMetaObject::invokeMethod -// to work. Here's the trick: -class QBluetoothSocketPrivateBase : public QObject -{ -// The most important part of it: - Q_OBJECT -public slots: - virtual void _q_writeNotify() = 0; }; -#endif // QT_OSX_BLUETOOTH - static inline void convertAddress(const quint64 from, quint8 (&to)[6]) { to[0] = (from >> 0) & 0xff; diff --git a/src/bluetooth/qbluetoothsocket_winrt.cpp b/src/bluetooth/qbluetoothsocket_winrt.cpp index 670235fa..22bdbc7e 100644 --- a/src/bluetooth/qbluetoothsocket_winrt.cpp +++ b/src/bluetooth/qbluetoothsocket_winrt.cpp @@ -320,14 +320,9 @@ private: }; QBluetoothSocketPrivate::QBluetoothSocketPrivate() - : socket(-1), - socketType(QBluetoothServiceInfo::UnknownProtocol), - state(QBluetoothSocket::UnconnectedState), - socketError(QBluetoothSocket::NoSocketError), - discoveryAgent(0), - secFlags(QBluetooth::NoSecurity), - m_worker(new SocketWorker()) + : m_worker(new SocketWorker()) { + secFlags = QBluetooth::NoSecurity; connect(m_worker, &SocketWorker::newDataReceived, this, &QBluetoothSocketPrivate::handleNewData, Qt::QueuedConnection); connect(m_worker, &SocketWorker::socketErrorOccured, diff --git a/src/bluetooth/qbluetoothsocketbase.cpp b/src/bluetooth/qbluetoothsocketbase.cpp new file mode 100644 index 00000000..8401a1c3 --- /dev/null +++ b/src/bluetooth/qbluetoothsocketbase.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qbluetoothsocketbase_p.h" + +QT_BEGIN_NAMESPACE + +QBluetoothSocketBasePrivate::QBluetoothSocketBasePrivate(QObject *parent) : QObject(parent) +{ + +} + +QBluetoothSocketBasePrivate::~QBluetoothSocketBasePrivate() +{ + +} + +QT_END_NAMESPACE diff --git a/src/bluetooth/qbluetoothsocketbase_p.h b/src/bluetooth/qbluetoothsocketbase_p.h new file mode 100644 index 00000000..c2c6a1f7 --- /dev/null +++ b/src/bluetooth/qbluetoothsocketbase_p.h @@ -0,0 +1,193 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QBLUETOOTHSOCKETBASEPRIVATE_P_H +#define QBLUETOOTHSOCKETBASEPRIVATE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <qglobal.h> +#include <QObject> +#include <QtBluetooth/qbluetoothsocket.h> + +#if defined(QT_ANDROID_BLUETOOTH) +#include <QtAndroidExtras/QAndroidJniObject> +#endif + +#if defined(QT_WINRT_BLUETOOTH) +#include <QtCore/QMutex> + +#include <wrl.h> + +namespace ABI { + namespace Windows { + namespace Networking { + namespace Sockets { + struct IStreamSocket; + } + } + namespace Foundation { + struct IAsyncAction; + enum class AsyncStatus; + } + } +} +#endif // QT_WINRT_BLUETOOTH + +#ifndef QPRIVATELINEARBUFFER_BUFFERSIZE +#define QPRIVATELINEARBUFFER_BUFFERSIZE Q_INT64_C(16384) +#endif +#include "qprivatelinearbuffer_p.h" + +QT_FORWARD_DECLARE_CLASS(QSocketNotifier) +QT_FORWARD_DECLARE_CLASS(QBluetoothServiceDiscoveryAgent) + +QT_BEGIN_NAMESPACE + +#ifndef QT_OSX_BLUETOOTH +class QBluetoothSocketBasePrivate : public QObject +{ + Q_OBJECT + +public: + explicit QBluetoothSocketBasePrivate(QObject *parent = nullptr); + virtual ~QBluetoothSocketBasePrivate(); + + virtual bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type) = 0; + + virtual QString localName() const = 0; + virtual QBluetoothAddress localAddress() const = 0; + virtual quint16 localPort() const = 0; + + virtual QString peerName() const = 0; + virtual QBluetoothAddress peerAddress() const = 0; + virtual quint16 peerPort() const = 0; + + virtual void abort() = 0; + virtual void close() = 0; + + virtual qint64 writeData(const char *data, qint64 maxSize) = 0; + virtual qint64 readData(char *data, qint64 maxSize) = 0; + + virtual qint64 bytesAvailable() const = 0; + virtual bool canReadLine() const = 0; + virtual qint64 bytesToWrite() const = 0; + + virtual bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, + QBluetoothSocket::SocketState socketState = QBluetoothSocket::ConnectedState, + QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) = 0; + +#if defined(QT_ANDROID_BLUETOOTH) + virtual void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, + QIODevice::OpenMode openMode) = 0; +#else + virtual void connectToService(const QBluetoothAddress &address, quint16 port, + QIODevice::OpenMode openMode) = 0; +#endif + +#ifdef QT_ANDROID_BLUETOOTH + virtual bool setSocketDescriptor(const QAndroidJniObject &socket, QBluetoothServiceInfo::Protocol socketType, + QBluetoothSocket::SocketState socketState = QBluetoothSocket::ConnectedState, + QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) = 0; +#elif defined(QT_WINRT_BLUETOOTH) + virtual bool setSocketDescriptor(Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocket> socket, + QBluetoothServiceInfo::Protocol socketType, + QBluetoothSocket::SocketState socketState = QBluetoothSocket::ConnectedState, + QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) = 0; +#endif + +public: + QPrivateLinearBuffer buffer; + QPrivateLinearBuffer txBuffer; + int socket = -1; + QBluetoothServiceInfo::Protocol socketType = QBluetoothServiceInfo::UnknownProtocol; + QBluetoothSocket::SocketState state = QBluetoothSocket::UnconnectedState; + QBluetoothSocket::SocketError socketError = QBluetoothSocket::NoSocketError; + QSocketNotifier *readNotifier = nullptr; + QSocketNotifier *connectWriteNotifier = nullptr; + bool connecting = false; + + QBluetoothServiceDiscoveryAgent *discoveryAgent = nullptr; + QBluetoothSocket::OpenMode openMode; + QBluetooth::SecurityFlags secFlags; + + QString errorString; + +protected: + Q_DECLARE_PUBLIC(QBluetoothSocket) + QBluetoothSocket *q_ptr; + +#if QT_CONFIG(bluez) +public: + quint8 lowEnergySocketType = 0; +#endif +}; + +#else // QT_OSX_BLUETOOTH + +// QBluetoothSocketPrivate on macOS can not contain +// Q_OBJECT (moc does not parse Objective-C syntax). +// But QBluetoothSocket still requires QMetaObject::invokeMethod +// to work. Here's the trick: +class QBluetoothSocketBasePrivate : public QObject +{ +// The most important part of it: + Q_OBJECT +public slots: + virtual void _q_writeNotify() = 0; + +protected: + Q_DECLARE_PUBLIC(QBluetoothSocket) + QBluetoothSocket *q_ptr; +}; + +#endif // QT_OSX_BLUETOOTH + +QT_END_NAMESPACE + +#endif // QBLUETOOTHSOCKETBASE_P_H |