diff options
author | Alex Blasche <alexander.blasche@qt.io> | 2018-07-23 13:13:49 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@qt.io> | 2018-07-25 11:16:30 +0000 |
commit | e84d9f24cbb7c686535cbe7e13f28a1c0baaa48d (patch) | |
tree | aba4b661532d1843706b15b17f92e328a9ab70bb /src/bluetooth/qbluetoothsocket_bluez.cpp | |
parent | 81de083e4f9fdee7f1ef7d2fffc7d2147ddbc1b0 (diff) |
Move QBluetoothSocket::connectToService() to private implementations
This permits each platform to customize the implementations without
the need for ifdefs. Upcoming changes such as the BLuez DBuS addition
will increase the platform differences.
Task-number: QTBUG-68550
Change-Id: I8fc9a74d3ce704466f0bf2c16287e32f222c4376
Reviewed-by: Lubomir I. Ivanov <neolit123@gmail.com>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Diffstat (limited to 'src/bluetooth/qbluetoothsocket_bluez.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothsocket_bluez.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/bluetooth/qbluetoothsocket_bluez.cpp b/src/bluetooth/qbluetoothsocket_bluez.cpp index 0e5181f2..ccf34e7b 100644 --- a/src/bluetooth/qbluetoothsocket_bluez.cpp +++ b/src/bluetooth/qbluetoothsocket_bluez.cpp @@ -39,6 +39,7 @@ #include "qbluetoothsocket.h" #include "qbluetoothsocket_bluez_p.h" +#include "qbluetoothdeviceinfo.h" #include "bluez/manager_p.h" #include "bluez/adapter_p.h" @@ -206,6 +207,110 @@ void QBluetoothSocketPrivateBluez::connectToServiceHelper(const QBluetoothAddres } } +void QBluetoothSocketPrivateBluez::connectToService( + const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode) +{ + Q_Q(QBluetoothSocket); + + if (q->state() != QBluetoothSocket::UnconnectedState + && q->state() != QBluetoothSocket::ServiceLookupState) { + qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket"; + errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress"); + q->setSocketError(QBluetoothSocket::OperationError); + return; + } + + // we are checking the service protocol and not socketType() + // socketType will change in ensureNativeSocket() + if (service.socketProtocol() == QBluetoothServiceInfo::UnknownProtocol) { + qCWarning(QT_BT_BLUEZ) << "QBluetoothSocket::connectToService cannot " + "connect with 'UnknownProtocol' (type provided by given service)"; + errorString = QBluetoothSocket::tr("Socket type not supported"); + q->setSocketError(QBluetoothSocket::UnsupportedProtocolError); + return; + } + + if (service.protocolServiceMultiplexer() > 0) { + Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::L2capProtocol); + + if (!ensureNativeSocket(QBluetoothServiceInfo::L2capProtocol)) { + errorString = QBluetoothSocket::tr("Unknown socket error"); + q->setSocketError(QBluetoothSocket::UnknownSocketError); + return; + } + connectToServiceHelper(service.device().address(), service.protocolServiceMultiplexer(), + openMode); + } else if (service.serverChannel() > 0) { + Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::RfcommProtocol); + + if (!ensureNativeSocket(QBluetoothServiceInfo::RfcommProtocol)) { + errorString = QBluetoothSocket::tr("Unknown socket error"); + q->setSocketError(QBluetoothSocket::UnknownSocketError); + return; + } + connectToServiceHelper(service.device().address(), service.serverChannel(), openMode); + } else { + // try doing service discovery to see if we can find the socket + if (service.serviceUuid().isNull() + && !service.serviceClassUuids().contains(QBluetoothUuid::SerialPort)) { + qCWarning(QT_BT_BLUEZ) << "No port, no PSM, and no UUID provided. Unable to connect"; + return; + } + qCDebug(QT_BT_BLUEZ) << "Need a port/psm, doing discovery"; + q->doDeviceDiscovery(service, openMode); + } +} + +void QBluetoothSocketPrivateBluez::connectToService( + const QBluetoothAddress &address, const QBluetoothUuid &uuid, + QIODevice::OpenMode openMode) +{ + Q_Q(QBluetoothSocket); + + if (q->state() != QBluetoothSocket::UnconnectedState) { + qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket"; + errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress"); + q->setSocketError(QBluetoothSocket::OperationError); + return; + } + + if (q->socketType() == QBluetoothServiceInfo::UnknownProtocol) { + qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService cannot " + "connect with 'UnknownProtocol' (type provided by given service)"; + errorString = QBluetoothSocket::tr("Socket type not supported"); + q->setSocketError(QBluetoothSocket::UnsupportedProtocolError); + return; + } + + QBluetoothServiceInfo service; + QBluetoothDeviceInfo device(address, QString(), QBluetoothDeviceInfo::MiscellaneousDevice); + service.setDevice(device); + service.setServiceUuid(uuid); + q->doDeviceDiscovery(service, openMode); +} + +void QBluetoothSocketPrivateBluez::connectToService( + const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode) +{ + Q_Q(QBluetoothSocket); + + if (q->socketType() == QBluetoothServiceInfo::UnknownProtocol) { + qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService cannot " + "connect with 'UnknownProtocol' (type provided by given service)"; + errorString = QBluetoothSocket::tr("Socket type not supported"); + q->setSocketError(QBluetoothSocket::UnsupportedProtocolError); + return; + } + + if (q->state() != QBluetoothSocket::UnconnectedState) { + qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket"; + errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress"); + q->setSocketError(QBluetoothSocket::OperationError); + return; + } + connectToServiceHelper(address, port, openMode); +} + void QBluetoothSocketPrivateBluez::_q_writeNotify() { Q_Q(QBluetoothSocket); |