diff options
author | Alex Blasche <alexander.blasche@qt.io> | 2018-08-16 09:16:15 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@qt.io> | 2018-08-16 12:54:02 +0000 |
commit | f602d7fef2e2f067e123e5740d4b0bf16c4ec0e2 (patch) | |
tree | 4fa53bcee779c93304572dd3b46ee7fee8b57098 | |
parent | 58a0fe8f9ab9ea3e080148f95359a355517d5cc5 (diff) |
Ensure that QBluetoothServer works with new DBus based sockets
or better ensure that the DBus based socket is not used for QBluetoothServer.
The server implementation will continue to use the raw socket implementation
for the foreseeable future. The reason being the DBus version not yet working.
convertAddress() in the server cpp was removed because it was a duplicate
implementation. Including qbluetoothsocket_bluez_p.h pulls in the
qbluetoothsocketbase_p.h which provides the official convertAddress()
implementations.
Unfortunately this requires a new ctor for QBluetoothSocket which is only
specified for BlueZ builds. It is used to permit QBluetoothServer to set
the correct dptr for QBluetoothSocket.
Task-number: QTBUG-68550
Change-Id: I19298f75e9bc2ab93322d9f34e1816ad733ac6d9
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
-rw-r--r-- | src/bluetooth/qbluetoothserver_bluez.cpp | 28 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserver_p.h | 2 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket.cpp | 21 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket.h | 7 |
4 files changed, 46 insertions, 12 deletions
diff --git a/src/bluetooth/qbluetoothserver_bluez.cpp b/src/bluetooth/qbluetoothserver_bluez.cpp index fe36a712..f67dffde 100644 --- a/src/bluetooth/qbluetoothserver_bluez.cpp +++ b/src/bluetooth/qbluetoothserver_bluez.cpp @@ -40,6 +40,7 @@ #include "qbluetoothserver.h" #include "qbluetoothserver_p.h" #include "qbluetoothsocket.h" +#include "qbluetoothsocket_bluez_p.h" #include "qbluetoothlocaldevice.h" #include "bluez/bluez_data_p.h" @@ -52,14 +53,17 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(QT_BT_BLUEZ) -static inline void convertAddress(quint64 from, quint8 (&to)[6]) +QBluetoothSocket *QBluetoothServerPrivate::createSocketForServer( + QBluetoothServiceInfo::Protocol socketType) { - to[0] = (from >> 0) & 0xff; - to[1] = (from >> 8) & 0xff; - to[2] = (from >> 16) & 0xff; - to[3] = (from >> 24) & 0xff; - to[4] = (from >> 32) & 0xff; - to[5] = (from >> 40) & 0xff; + // QBluetoothServer does not work with the BluetoothSocket implementation for DBus. + // Fall back to the raw socket implementation. + // Usually the private implementation is picked based on detected BlueZ version. + + // ownership of these objects is taken care of inside QBluetoothSocket and QBluetoothServer + QBluetoothSocketPrivateBluez *rawSocketPrivate = new QBluetoothSocketPrivateBluez(); + QBluetoothSocket *socket = new QBluetoothSocket(rawSocketPrivate, socketType); + return socket; } QBluetoothServerPrivate::QBluetoothServerPrivate(QBluetoothServiceInfo::Protocol sType) @@ -67,9 +71,9 @@ QBluetoothServerPrivate::QBluetoothServerPrivate(QBluetoothServiceInfo::Protocol m_lastError(QBluetoothServer::NoError), socketNotifier(0) { if (sType == QBluetoothServiceInfo::RfcommProtocol) - socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); + socket = createSocketForServer(QBluetoothServiceInfo::RfcommProtocol); else - socket = new QBluetoothSocket(QBluetoothServiceInfo::L2capProtocol); + socket = createSocketForServer(QBluetoothServiceInfo::L2capProtocol); } QBluetoothServerPrivate::~QBluetoothServerPrivate() @@ -185,9 +189,9 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) delete d->socket; if (serverType() == QBluetoothServiceInfo::RfcommProtocol) - d->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); + d->socket = QBluetoothServerPrivate::createSocketForServer(QBluetoothServiceInfo::RfcommProtocol); else - d->socket = new QBluetoothSocket(QBluetoothServiceInfo::L2capProtocol); + d->socket = QBluetoothServerPrivate::createSocketForServer(QBluetoothServiceInfo::L2capProtocol); sock = d->socket->socketDescriptor(); if (sock < 0) { @@ -294,7 +298,7 @@ QBluetoothSocket *QBluetoothServer::nextPendingConnection() } if (pending >= 0) { - QBluetoothSocket *newSocket = new QBluetoothSocket; + QBluetoothSocket *newSocket = QBluetoothServerPrivate::createSocketForServer(); if (d->serverType == QBluetoothServiceInfo::RfcommProtocol) newSocket->setSocketDescriptor(pending, QBluetoothServiceInfo::RfcommProtocol); else diff --git a/src/bluetooth/qbluetoothserver_p.h b/src/bluetooth/qbluetoothserver_p.h index 9c414cdb..3fa14a10 100644 --- a/src/bluetooth/qbluetoothserver_p.h +++ b/src/bluetooth/qbluetoothserver_p.h @@ -98,6 +98,8 @@ public: void _q_newConnection(); void setSocketSecurityLevel(QBluetooth::SecurityFlags requestedSecLevel, int *errnoCode); QBluetooth::SecurityFlags socketSecurityLevel() const; + static QBluetoothSocket *createSocketForServer( + QBluetoothServiceInfo::Protocol socketType = QBluetoothServiceInfo::RfcommProtocol); #endif public: diff --git a/src/bluetooth/qbluetoothsocket.cpp b/src/bluetooth/qbluetoothsocket.cpp index 62633bb1..92983de8 100644 --- a/src/bluetooth/qbluetoothsocket.cpp +++ b/src/bluetooth/qbluetoothsocket.cpp @@ -296,6 +296,27 @@ QBluetoothSocket::QBluetoothSocket(QObject *parent) setOpenMode(QIODevice::NotOpen); } +#if QT_CONFIG(bluez) + +/*! + \internal +*/ +QBluetoothSocket::QBluetoothSocket(QBluetoothSocketBasePrivate *dPrivate, + QBluetoothServiceInfo::Protocol socketType, + QObject *parent) + : QIODevice(parent) +{ + d_ptr = dPrivate; + d_ptr->q_ptr = this; + + Q_D(QBluetoothSocketBase); + d->ensureNativeSocket(socketType); + + setOpenMode(QIODevice::NotOpen); +} + +#endif + /*! Destroys the Bluetooth socket. */ diff --git a/src/bluetooth/qbluetoothsocket.h b/src/bluetooth/qbluetoothsocket.h index cda64dff..cf76e8b8 100644 --- a/src/bluetooth/qbluetoothsocket.h +++ b/src/bluetooth/qbluetoothsocket.h @@ -181,6 +181,13 @@ private Q_SLOTS: protected: +#if QT_CONFIG(bluez) + //evil hack to enable QBluetoothServer on Bluez to set the desired d_ptr + explicit QBluetoothSocket(QBluetoothSocketBasePrivate *d, + QBluetoothServiceInfo::Protocol socketType, + QObject *parent = nullptr); + friend class QBluetoothServerPrivate; +#endif #ifndef QT_OSX_BLUETOOTH QBluetoothSocketBasePrivate *d_ptr; #else |