diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2019-07-04 14:09:45 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@qt.io> | 2019-07-05 09:17:42 +0000 |
commit | b582af63040620bf5b67ebfb2c89d17297a283e3 (patch) | |
tree | bf5ac4eac3cde568215e23f905431a546dee3949 | |
parent | 183327bc16b97276b39479e4257b112037175930 (diff) |
QBluetoothServer - cleanup and de-duplicate code
Some code can be re-used from the shared public implementation.
*_osx_p.h is not needed either.
Task-number: QTBUG-75348
Change-Id: If373b74edebe9a9db90e820016cf779a1726baed
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r-- | src/bluetooth/bluetooth.pro | 2 | ||||
-rw-r--r-- | src/bluetooth/osx/osxbtsocketlistener.mm | 21 | ||||
-rw-r--r-- | src/bluetooth/osx/osxbtsocketlistener_p.h | 17 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserver.cpp | 2 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserver_osx.mm | 223 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserver_osx_p.h | 127 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserver_p.h | 62 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserviceinfo_osx.mm | 2 |
8 files changed, 141 insertions, 315 deletions
diff --git a/src/bluetooth/bluetooth.pro b/src/bluetooth/bluetooth.pro index 974d8e8a..0b14357d 100644 --- a/src/bluetooth/bluetooth.pro +++ b/src/bluetooth/bluetooth.pro @@ -171,7 +171,6 @@ qtConfig(bluez) { qlowenergycontroller_darwin.mm PRIVATE_HEADERS += qbluetoothsocket_osx_p.h \ - qbluetoothserver_osx_p.h \ qbluetoothtransferreply_osx_p.h \ qlowenergycontroller_darwin_p.h @@ -179,7 +178,6 @@ qtConfig(bluez) { SOURCES -= qbluetoothservicediscoveryagent.cpp SOURCES -= qbluetoothsocket.cpp SOURCES -= qbluetoothsocketbase.cpp - SOURCES -= qbluetoothserver.cpp } else:ios|tvos { DEFINES += QT_IOS_BLUETOOTH LIBS_PRIVATE += -framework Foundation -framework CoreBluetooth diff --git a/src/bluetooth/osx/osxbtsocketlistener.mm b/src/bluetooth/osx/osxbtsocketlistener.mm index 517b7f2d..10526b0f 100644 --- a/src/bluetooth/osx/osxbtsocketlistener.mm +++ b/src/bluetooth/osx/osxbtsocketlistener.mm @@ -39,31 +39,20 @@ #include "osxbtsocketlistener_p.h" #include "osxbtutility_p.h" +#include "btdelegates_p.h" #include <QtCore/qdebug.h> -QT_BEGIN_NAMESPACE - -namespace OSXBluetooth { - -SocketListener::~SocketListener() -{ -} - -} - -QT_END_NAMESPACE - QT_USE_NAMESPACE @implementation QT_MANGLE_NAMESPACE(OSXBTSocketListener) { IOBluetoothUserNotification *connectionNotification; - QT_PREPEND_NAMESPACE(OSXBluetooth::SocketListener) *delegate; + QT_PREPEND_NAMESPACE(DarwinBluetooth::SocketListener) *delegate; quint16 port; } -- (id)initWithListener:(OSXBluetooth::SocketListener *)aDelegate +- (id)initWithListener:(DarwinBluetooth::SocketListener *)aDelegate { Q_ASSERT_X(aDelegate, Q_FUNC_INFO, "invalid delegate (null)"); if (self = [super init]) { @@ -119,7 +108,7 @@ QT_USE_NAMESPACE Q_UNUSED(notification) Q_ASSERT_X(delegate, Q_FUNC_INFO, "invalid delegate (null)"); - delegate->openNotify(newChannel); + delegate->openNotifyRFCOMM(newChannel); } - (void)l2capOpenNotification:(IOBluetoothUserNotification *)notification @@ -128,7 +117,7 @@ QT_USE_NAMESPACE Q_UNUSED(notification) Q_ASSERT_X(delegate, Q_FUNC_INFO, "invalid delegate (null)"); - delegate->openNotify(newChannel); + delegate->openNotifyL2CAP(newChannel); } - (quint16)port diff --git a/src/bluetooth/osx/osxbtsocketlistener_p.h b/src/bluetooth/osx/osxbtsocketlistener_p.h index cac0b7c4..3bbce24e 100644 --- a/src/bluetooth/osx/osxbtsocketlistener_p.h +++ b/src/bluetooth/osx/osxbtsocketlistener_p.h @@ -57,22 +57,15 @@ #include <Foundation/Foundation.h> +// TODO: use the special macros we have to create an +// alias for a mangled name. @class QT_MANGLE_NAMESPACE(OSXBTSocketListener); QT_BEGIN_NAMESPACE -namespace OSXBluetooth { +namespace DarwinBluetooth { -class SocketListener -{ -public: - typedef QT_MANGLE_NAMESPACE(OSXBTSocketListener) ObjCListener; - - virtual ~SocketListener(); - - virtual void openNotify(IOBluetoothRFCOMMChannel *channel) = 0; - virtual void openNotify(IOBluetoothL2CAPChannel *channel) = 0; -}; +class SocketListener; } @@ -83,7 +76,7 @@ QT_END_NAMESPACE @interface QT_MANGLE_NAMESPACE(OSXBTSocketListener) : NSObject -- (id)initWithListener:(QT_PREPEND_NAMESPACE(OSXBluetooth::SocketListener) *)aDelegate; +- (id)initWithListener:(QT_PREPEND_NAMESPACE(DarwinBluetooth::SocketListener) *)aDelegate; - (void)dealloc; - (bool)listenRFCOMMConnectionsWithChannelID:(BluetoothRFCOMMChannelID)channelID; diff --git a/src/bluetooth/qbluetoothserver.cpp b/src/bluetooth/qbluetoothserver.cpp index 6991518f..14b0796d 100644 --- a/src/bluetooth/qbluetoothserver.cpp +++ b/src/bluetooth/qbluetoothserver.cpp @@ -266,7 +266,7 @@ bool QBluetoothServer::isListening() const { Q_D(const QBluetoothServer); -#if defined(QT_ANDROID_BLUETOOTH) || defined(QT_WINRT_BLUETOOTH) +#if defined(QT_ANDROID_BLUETOOTH) || defined(QT_WINRT_BLUETOOTH) || defined(QT_OSX_BLUETOOTH) return d->isListening(); #endif diff --git a/src/bluetooth/qbluetoothserver_osx.mm b/src/bluetooth/qbluetoothserver_osx.mm index eefaf4da..325381c8 100644 --- a/src/bluetooth/qbluetoothserver_osx.mm +++ b/src/bluetooth/qbluetoothserver_osx.mm @@ -38,7 +38,7 @@ ****************************************************************************/ #include "osx/osxbtsocketlistener_p.h" -#include "qbluetoothserver_osx_p.h" +#include "qbluetoothserver_p.h" // The order is important: a workround for // a private header included by private header @@ -58,7 +58,6 @@ #include <QtCore/qglobal.h> #include <QtCore/qmutex.h> -// Import, since Obj-C headers do not have inclusion guards. #include <Foundation/Foundation.h> #include <limits> @@ -67,7 +66,9 @@ QT_BEGIN_NAMESPACE namespace { -typedef QBluetoothServiceInfo QSInfo; +using DarwinBluetooth::RetainPolicy; +using ServiceInfo = QBluetoothServiceInfo; +using ObjCListener = QT_MANGLE_NAMESPACE(OSXBTSocketListener); QMap<quint16, QBluetoothServerPrivate *> &busyPSMs() { @@ -86,79 +87,88 @@ typedef QMap<quint16, QBluetoothServerPrivate *>::iterator ServerMapIterator; } -QBluetoothServerPrivate::QBluetoothServerPrivate(QSInfo::Protocol type, QBluetoothServer *q) - : serverType(type), - q_ptr(q), - lastError(QBluetoothServer::NoError), - port(0), - maxPendingConnections(1) +QBluetoothServerPrivate::QBluetoothServerPrivate(ServiceInfo::Protocol type) + : socket(nullptr), + maxPendingConnections(1), + securityFlags(QBluetooth::NoSecurity), + serverType(type), + q_ptr(nullptr), + m_lastError(QBluetoothServer::NoError), + port(0) { - Q_ASSERT_X(q_ptr, Q_FUNC_INFO, "invalid q_ptr (null)"); - if (serverType == QSInfo::UnknownProtocol) + if (serverType == ServiceInfo::UnknownProtocol) qCWarning(QT_BT_OSX) << "unknown protocol"; } QBluetoothServerPrivate::~QBluetoothServerPrivate() { - // Actually, not good, but lock must be acquired. - // TODO: test this. const QMutexLocker lock(&channelMapMutex()); unregisterServer(this); } -void QBluetoothServerPrivate::_q_newConnection() -{ - // Noop, we have openNotify for this. -} - bool QBluetoothServerPrivate::startListener(quint16 realPort) { Q_ASSERT_X(realPort, Q_FUNC_INFO, "invalid port"); - if (serverType == QSInfo::UnknownProtocol) { + if (serverType == ServiceInfo::UnknownProtocol) { qCWarning(QT_BT_OSX) << "invalid protocol"; return false; } - if (!listener) - listener.reset([[ObjCListener alloc] initWithListener:this]); + if (!listener) { + listener.reset([[ObjCListener alloc] initWithListener:this], + RetainPolicy::noInitialRetain); + } bool result = false; - if (serverType == QSInfo::RfcommProtocol) - result = [listener listenRFCOMMConnectionsWithChannelID:realPort]; + if (serverType == ServiceInfo::RfcommProtocol) + result = [listener.getAs<ObjCListener>() listenRFCOMMConnectionsWithChannelID:realPort]; else - result = [listener listenL2CAPConnectionsWithPSM:realPort]; + result = [listener.getAs<ObjCListener>() listenL2CAPConnectionsWithPSM:realPort]; if (!result) - listener.reset(nil); + listener.reset(); return result; } +bool QBluetoothServerPrivate::isListening() const +{ + if (serverType == ServiceInfo::UnknownProtocol) + return false; + + const QMutexLocker lock(&QBluetoothServerPrivate::channelMapMutex()); + return QBluetoothServerPrivate::registeredServer(q_ptr->serverPort(), serverType); +} + void QBluetoothServerPrivate::stopListener() { - listener.reset(nil); + listener.reset(); } -void QBluetoothServerPrivate::openNotify(IOBluetoothRFCOMMChannel *channel) +void QBluetoothServerPrivate::openNotifyRFCOMM(void *generic) { + auto channel = static_cast<IOBluetoothRFCOMMChannel *>(generic); + Q_ASSERT_X(listener, Q_FUNC_INFO, "invalid listener (nil)"); Q_ASSERT_X(channel, Q_FUNC_INFO, "invalid channel (nil)"); Q_ASSERT_X(q_ptr, Q_FUNC_INFO, "invalid q_ptr (null)"); - PendingConnection newConnection(channel, true); + PendingConnection newConnection(channel, RetainPolicy::doInitialRetain); pendingConnections.append(newConnection); emit q_ptr->newConnection(); } -void QBluetoothServerPrivate::openNotify(IOBluetoothL2CAPChannel *channel) +void QBluetoothServerPrivate::openNotifyL2CAP(void *generic) { + auto channel = static_cast<IOBluetoothL2CAPChannel *>(generic); + Q_ASSERT_X(listener, Q_FUNC_INFO, "invalid listener (nil)"); Q_ASSERT_X(channel, Q_FUNC_INFO, "invalid channel (nil)"); Q_ASSERT_X(q_ptr, Q_FUNC_INFO, "invalid q_ptr (null)"); - PendingConnection newConnection(channel, true); + PendingConnection newConnection(channel, RetainPolicy::doInitialRetain); pendingConnections.append(newConnection); emit q_ptr->newConnection(); @@ -209,11 +219,11 @@ void QBluetoothServerPrivate::registerServer(QBluetoothServerPrivate *server, qu // External lock is required + port must be free. Q_ASSERT_X(server, Q_FUNC_INFO, "invalid server (null)"); - const QSInfo::Protocol type = server->serverType; - if (type == QSInfo::RfcommProtocol) { + const ServiceInfo::Protocol type = server->serverType; + if (type == ServiceInfo::RfcommProtocol) { Q_ASSERT_X(!channelIsBusy(port), Q_FUNC_INFO, "port is busy"); busyChannels()[port] = server; - } else if (type == QSInfo::L2capProtocol) { + } else if (type == ServiceInfo::L2capProtocol) { Q_ASSERT_X(!psmIsBusy(port), Q_FUNC_INFO, "port is busy"); busyPSMs()[port] = server; } else { @@ -225,11 +235,11 @@ void QBluetoothServerPrivate::registerServer(QBluetoothServerPrivate *server, qu QBluetoothServerPrivate *QBluetoothServerPrivate::registeredServer(quint16 port, QBluetoothServiceInfo::Protocol protocol) { // Eternal lock is required. - if (protocol == QSInfo::RfcommProtocol) { + if (protocol == ServiceInfo::RfcommProtocol) { ServerMapIterator it = busyChannels().find(port); if (it != busyChannels().end()) return it.value(); - } else if (protocol == QSInfo::L2capProtocol) { + } else if (protocol == ServiceInfo::L2capProtocol) { ServerMapIterator it = busyPSMs().find(port); if (it != busyPSMs().end()) return it.value(); @@ -243,17 +253,17 @@ QBluetoothServerPrivate *QBluetoothServerPrivate::registeredServer(quint16 port, void QBluetoothServerPrivate::unregisterServer(QBluetoothServerPrivate *server) { // External lock is required. - const QSInfo::Protocol type = server->serverType; + const ServiceInfo::Protocol type = server->serverType; const quint16 port = server->port; - if (type == QSInfo::RfcommProtocol) { + if (type == ServiceInfo::RfcommProtocol) { ServerMapIterator it = busyChannels().find(port); if (it != busyChannels().end()) { busyChannels().erase(it); } else { qCWarning(QT_BT_OSX) << "server is not registered"; } - } else if (type == QSInfo::L2capProtocol) { + } else if (type == ServiceInfo::L2capProtocol) { ServerMapIterator it = busyPSMs().find(port); if (it != busyPSMs().end()) { busyPSMs().erase(it); @@ -265,21 +275,9 @@ void QBluetoothServerPrivate::unregisterServer(QBluetoothServerPrivate *server) } } - -QBluetoothServer::QBluetoothServer(QSInfo::Protocol serverType, QObject *parent) - : QObject(parent), - d_ptr(new QBluetoothServerPrivate(serverType, this)) -{ -} - -QBluetoothServer::~QBluetoothServer() -{ - delete d_ptr; -} - void QBluetoothServer::close() { - d_ptr->listener.reset(nil); + d_ptr->listener.reset(); // Needs a lock :( const QMutexLocker lock(&d_ptr->channelMapMutex()); @@ -289,8 +287,6 @@ void QBluetoothServer::close() bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) { - typedef QBluetoothServerPrivate::ObjCListener ObjCListener; - OSXBluetooth::qt_test_iobluetooth_runloop(); if (d_ptr->listener) { @@ -303,7 +299,7 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) qCWarning(QT_BT_OSX) << "device does not support Bluetooth or" << address.toString() << "is not a valid local adapter"; - d_ptr->lastError = UnknownError; + d_ptr->m_lastError = UnknownError; emit error(UnknownError); return false; } @@ -311,53 +307,53 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) const QBluetoothLocalDevice::HostMode hostMode = device.hostMode(); if (hostMode == QBluetoothLocalDevice::HostPoweredOff) { qCWarning(QT_BT_OSX) << "Bluetooth device is powered off"; - d_ptr->lastError = PoweredOffError; + d_ptr->m_lastError = PoweredOffError; emit error(PoweredOffError); return false; } - const QSInfo::Protocol type = d_ptr->serverType; + const ServiceInfo::Protocol type = d_ptr->serverType; - if (type == QSInfo::UnknownProtocol) { + if (type == ServiceInfo::UnknownProtocol) { qCWarning(QT_BT_OSX) << "invalid protocol"; - d_ptr->lastError = UnsupportedProtocolError; - emit error(d_ptr->lastError); + d_ptr->m_lastError = UnsupportedProtocolError; + emit error(d_ptr->m_lastError); return false; } - d_ptr->lastError = QBluetoothServer::NoError; + d_ptr->m_lastError = QBluetoothServer::NoError; // Now we have to register a (fake) port, doing a proper (?) lock. const QMutexLocker lock(&d_ptr->channelMapMutex()); if (port) { - if (type == QSInfo::RfcommProtocol) { + if (type == ServiceInfo::RfcommProtocol) { if (d_ptr->channelIsBusy(port)) { qCWarning(QT_BT_OSX) << "server port:" << port << "already registered"; - d_ptr->lastError = ServiceAlreadyRegisteredError; + d_ptr->m_lastError = ServiceAlreadyRegisteredError; } } else { if (d_ptr->psmIsBusy(port)) { qCWarning(QT_BT_OSX) << "server port:" << port << "already registered"; - d_ptr->lastError = ServiceAlreadyRegisteredError; + d_ptr->m_lastError = ServiceAlreadyRegisteredError; } } } else { - type == QSInfo::RfcommProtocol ? port = d_ptr->findFreeChannel() + type == ServiceInfo::RfcommProtocol ? port = d_ptr->findFreeChannel() : port = d_ptr->findFreePSM(); } - if (d_ptr->lastError != QBluetoothServer::NoError) { - emit error(d_ptr->lastError); + if (d_ptr->m_lastError != QBluetoothServer::NoError) { + emit error(d_ptr->m_lastError); return false; } if (!port) { qCWarning(QT_BT_OSX) << "all ports are busy"; - d_ptr->lastError = ServiceAlreadyRegisteredError; - emit error(d_ptr->lastError); + d_ptr->m_lastError = ServiceAlreadyRegisteredError; + emit error(d_ptr->m_lastError); return false; } @@ -365,82 +361,17 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) // (provided after a service was registered). d_ptr->port = port; d_ptr->registerServer(d_ptr, port); - d_ptr->listener.reset([[ObjCListener alloc] initWithListener:d_ptr]); + d_ptr->listener.reset([[ObjCListener alloc] initWithListener:d_ptr], + RetainPolicy::noInitialRetain); return true; } -QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const QString &serviceName) -{ - if (!listen()) - return QBluetoothServiceInfo(); - - QBluetoothServiceInfo serviceInfo; - serviceInfo.setAttribute(QSInfo::ServiceName, serviceName); - QBluetoothServiceInfo::Sequence publicBrowse; - publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); - serviceInfo.setAttribute(QSInfo::BrowseGroupList, publicBrowse); - - QSInfo::Sequence profileSequence; - QSInfo::Sequence classId; - classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); - classId << QVariant::fromValue(quint16(0x100)); - profileSequence.append(QVariant::fromValue(classId)); - serviceInfo.setAttribute(QSInfo::BluetoothProfileDescriptorList, profileSequence); - - classId.clear(); - classId << QVariant::fromValue(uuid); - classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); - serviceInfo.setAttribute(QSInfo::ServiceClassIds, classId); - serviceInfo.setServiceUuid(uuid); - - QSInfo::Sequence protocolDescriptorList; - QSInfo::Sequence protocol; - protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); - if (d_ptr->serverType == QSInfo::L2capProtocol) - protocol << QVariant::fromValue(serverPort()); - protocolDescriptorList.append(QVariant::fromValue(protocol)); - protocol.clear(); - - if (d_ptr->serverType == QBluetoothServiceInfo::RfcommProtocol) { - protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) - << QVariant::fromValue(quint8(serverPort())); - protocolDescriptorList.append(QVariant::fromValue(protocol)); - } - - serviceInfo.setAttribute(QSInfo::ProtocolDescriptorList, - protocolDescriptorList); - - - // It's now up to a service info to acquire a real PSM/channel ID - // (provided by IOBluetooth) and start a listener. - if (!serviceInfo.registerService()) - return QBluetoothServiceInfo(); - - return serviceInfo; -} - -bool QBluetoothServer::isListening() const -{ - if (d_ptr->serverType == QSInfo::UnknownProtocol) - return false; - - const QMutexLocker lock(&QBluetoothServerPrivate::channelMapMutex()); - return QBluetoothServerPrivate::registeredServer(serverPort(), d_ptr->serverType); -} - void QBluetoothServer::setMaxPendingConnections(int numConnections) { - // That's a 'fake' limit, it affects nothing. d_ptr->maxPendingConnections = numConnections; } -int QBluetoothServer::maxPendingConnections() const -{ - // That's a 'fake' limit, it affects nothing. - return d_ptr->maxPendingConnections; -} - bool QBluetoothServer::hasPendingConnections() const { return d_ptr->pendingConnections.size(); @@ -457,11 +388,11 @@ QBluetoothSocket *QBluetoothServer::nextPendingConnection() // Remove it even if we have some errors below. d_ptr->pendingConnections.pop_front(); - if (d_ptr->serverType == QSInfo::RfcommProtocol) { - if (!newSocket->d_ptr->setChannel(static_cast<IOBluetoothRFCOMMChannel *>(channel))) + if (d_ptr->serverType == ServiceInfo::RfcommProtocol) { + if (!newSocket->d_ptr->setChannel(channel.getAs<IOBluetoothRFCOMMChannel>())) return nullptr; } else { - if (!newSocket->d_ptr->setChannel(static_cast<IOBluetoothL2CAPChannel *>(channel))) + if (!newSocket->d_ptr->setChannel(channel.getAs<IOBluetoothL2CAPChannel>())) return nullptr; } @@ -481,25 +412,13 @@ quint16 QBluetoothServer::serverPort() const void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security) { Q_UNUSED(security) - // Not implemented (yet?) + Q_UNIMPLEMENTED(); } QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const { - // Not implemented (yet?) + Q_UNIMPLEMENTED(); return QBluetooth::NoSecurity; } -QSInfo::Protocol QBluetoothServer::serverType() const -{ - return d_ptr->serverType; -} - -QBluetoothServer::Error QBluetoothServer::error() const -{ - return d_ptr->lastError; -} - -#include "moc_qbluetoothserver.cpp" - QT_END_NAMESPACE diff --git a/src/bluetooth/qbluetoothserver_osx_p.h b/src/bluetooth/qbluetoothserver_osx_p.h deleted file mode 100644 index 3116ca02..00000000 --- a/src/bluetooth/qbluetoothserver_osx_p.h +++ /dev/null @@ -1,127 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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 QBLUETOOTHSERVER_OSX_P_H -#define QBLUETOOTHSERVER_OSX_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. -// - -#ifdef QT_OSX_BLUETOOTH - -#include "osx/osxbtsocketlistener_p.h" -#include "qbluetoothserviceinfo.h" -#include "osx/osxbtutility_p.h" -#include "qbluetoothserver.h" - -#include <QtCore/qglobal.h> -#include <QtCore/qlist.h> - -QT_BEGIN_NAMESPACE - -class QMutex; - -class QBluetoothServerPrivate : public OSXBluetooth::SocketListener -{ - friend class QBluetoothServer; - friend class QBluetoothServiceInfoPrivate; - -public: - QBluetoothServerPrivate(QBluetoothServiceInfo::Protocol type, QBluetoothServer *q); - ~QBluetoothServerPrivate(); - - void _q_newConnection(); -private: - bool startListener(quint16 realPort); - void stopListener(); - - // SocketListener (delegate): - void openNotify(IOBluetoothRFCOMMChannel *channel) override; - void openNotify(IOBluetoothL2CAPChannel *channel) override; - - QBluetoothServiceInfo::Protocol serverType; - QBluetoothServer *q_ptr; - QBluetoothServer::Error lastError; - - // Either a "temporary" channelID/PSM assigned by QBluetoothServer::listen, - // or a real channelID/PSM returned by IOBluetooth after we've registered - // a service. - quint16 port; - - typedef OSXBluetooth::ObjCScopedPointer<ObjCListener> Listener; - Listener listener; - - int maxPendingConnections; - - // These static functions below - // deal with differences between bluetooth sockets - // (bluez and QtBluetooth's API) and IOBluetooth, where it's not possible - // to have a real PSM/channelID _before_ a service is registered, - // the solution - "fake" ports. - // These functions require external locking - using channelMapMutex. - static QMutex &channelMapMutex(); - - static bool channelIsBusy(quint16 channelID); - static quint16 findFreeChannel(); - - static bool psmIsBusy(quint16 psm); - static quint16 findFreePSM(); - - static void registerServer(QBluetoothServerPrivate *server, quint16 port); - static QBluetoothServerPrivate *registeredServer(quint16 port, QBluetoothServiceInfo::Protocol protocol); - static void unregisterServer(QBluetoothServerPrivate *server); - - typedef OSXBluetooth::ObjCStrongReference<NSObject> PendingConnection; - QList<PendingConnection> pendingConnections; - -}; - -QT_END_NAMESPACE - -#endif //QT_OSX_BLUETOOTH - -#endif diff --git a/src/bluetooth/qbluetoothserver_p.h b/src/bluetooth/qbluetoothserver_p.h index d78eee5f..76349de6 100644 --- a/src/bluetooth/qbluetoothserver_p.h +++ b/src/bluetooth/qbluetoothserver_p.h @@ -77,6 +77,15 @@ class ServerAcceptanceThread; #include <windows.networking.sockets.h> #endif +#ifdef QT_OSX_BLUETOOTH + +#include "osx/btdelegates_p.h" +#include "osx/btraii_p.h" + +#include <QtCore/qvector.h> + +#endif // QT_OSX_BLUETOOTH + QT_BEGIN_NAMESPACE class QBluetoothAddress; @@ -84,9 +93,10 @@ class QBluetoothSocket; class QBluetoothServer; -#ifndef QT_OSX_BLUETOOTH - class QBluetoothServerPrivate +#ifdef QT_OSX_BLUETOOTH + : public DarwinBluetooth::SocketListener +#endif { Q_DECLARE_PUBLIC(QBluetoothServer) @@ -140,9 +150,53 @@ public: bool initiateActiveListening(const QString &serviceName); bool deactivateActiveListening(); #endif -}; -#endif //QT_OSX_BLUETOOTH +#ifdef QT_OSX_BLUETOOTH + +public: + + friend class QBluetoothServer; + friend class QBluetoothServiceInfoPrivate; + +private: + bool startListener(quint16 realPort); + void stopListener(); + bool isListening() const; + + // SocketListener (delegate): + void openNotifyRFCOMM(void *channel) override; + void openNotifyL2CAP(void *channel) override; + + // Either a "temporary" channelID/PSM assigned by QBluetoothServer::listen, + // or a real channelID/PSM returned by IOBluetooth after we've registered + // a service. + quint16 port; + + DarwinBluetooth::StrongReference listener; + + // These static functions below + // deal with differences between bluetooth sockets + // (bluez and QtBluetooth's API) and IOBluetooth, where it's not possible + // to have a real PSM/channelID _before_ a service is registered, + // the solution - "fake" ports. + // These functions require external locking - using channelMapMutex. + static QMutex &channelMapMutex(); + + static bool channelIsBusy(quint16 channelID); + static quint16 findFreeChannel(); + + static bool psmIsBusy(quint16 psm); + static quint16 findFreePSM(); + + static void registerServer(QBluetoothServerPrivate *server, quint16 port); + static QBluetoothServerPrivate *registeredServer(quint16 port, QBluetoothServiceInfo::Protocol protocol); + static void unregisterServer(QBluetoothServerPrivate *server); + + using PendingConnection = DarwinBluetooth::StrongReference; + QVector<PendingConnection> pendingConnections; + +#endif // QT_OSX_BLUETOOTH +}; QT_END_NAMESPACE diff --git a/src/bluetooth/qbluetoothserviceinfo_osx.mm b/src/bluetooth/qbluetoothserviceinfo_osx.mm index 34de4695..c0f914f6 100644 --- a/src/bluetooth/qbluetoothserviceinfo_osx.mm +++ b/src/bluetooth/qbluetoothserviceinfo_osx.mm @@ -38,9 +38,9 @@ ****************************************************************************/ #include "osx/osxbtservicerecord_p.h" -#include "qbluetoothserver_osx_p.h" #include "qbluetoothserviceinfo.h" #include "qbluetoothdeviceinfo.h" +#include "qbluetoothserver_p.h" #include "osx/osxbtutility_p.h" #include "osx/osxbluetooth_p.h" |