summaryrefslogtreecommitdiffstats
path: root/src/bluetooth
diff options
context:
space:
mode:
authorTimur Pocheptsov <timur.pocheptsov@theqtcompany.com>2016-08-02 10:36:56 +0200
committerTimur Pocheptsov <timur.pocheptsov@theqtcompany.com>2016-08-26 13:41:27 +0000
commit21e7cb1bafdfc06c263e10067d02f9b103ff660f (patch)
treef6f5df73ef23e5d1cebcb9347dbd40be91a673cc /src/bluetooth
parent95513185fd90cb9372a8cb8b541de7a3f41b12d4 (diff)
QBluetoothSocket::connectToService - check a socket type first
Attempt to connect to a service using a socket with UnknownProtocol socket type will fail, but can result in an incorrect UnknownSocketError reported and also can start a device discovery (then finally failing to connect after all). Check this condition early before trying to actually connect/do device discovery and report error properly as UnsupportedProtocolError. Task-number: QTBUG-55073 Change-Id: Ib39e1ca7ad401e07d6387201a4664a1185d38d39 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src/bluetooth')
-rw-r--r--src/bluetooth/qbluetoothsocket.cpp27
-rw-r--r--src/bluetooth/qbluetoothsocket_osx.mm28
2 files changed, 51 insertions, 4 deletions
diff --git a/src/bluetooth/qbluetoothsocket.cpp b/src/bluetooth/qbluetoothsocket.cpp
index 2b2264e8..065cf74d 100644
--- a/src/bluetooth/qbluetoothsocket.cpp
+++ b/src/bluetooth/qbluetoothsocket.cpp
@@ -332,6 +332,15 @@ void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, Op
}
d->connectToService(service.device().address(), service.serviceUuid(), openMode);
#else
+ // Report this problem early:
+ if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT) << "QBluetoothSocket::connectToService cannot "
+ "connect with 'UnknownProtocol' type";
+ d->errorString = tr("Socket type not supported");
+ setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
if (service.protocolServiceMultiplexer() > 0) {
if (!d->ensureNativeSocket(QBluetoothServiceInfo::L2capProtocol)) {
d->errorString = tr("Unknown socket error");
@@ -401,6 +410,15 @@ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const
}
d->connectToService(address, uuid, openMode);
#else
+ // Report this problem early, prevent device discovery:
+ if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT) << "QBluetoothSocket::connectToService cannot "
+ "connect with 'UnknownProtocol' type";
+ d->errorString = tr("Socket type not supported");
+ setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
QBluetoothServiceInfo service;
QBluetoothDeviceInfo device(address, QString(), QBluetoothDeviceInfo::MiscellaneousDevice);
service.setDevice(device);
@@ -438,6 +456,15 @@ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, quint1
setSocketError(QBluetoothSocket::ServiceNotFoundError);
qCWarning(QT_BT) << "Connecting to port is not supported";
#else
+ // Report this problem early:
+ if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT) << "QBluetoothSocket::connectToService cannot "
+ "connect with 'UnknownProtocol' type";
+ d->errorString = tr("Socket type not supported");
+ setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
if (state() != QBluetoothSocket::UnconnectedState) {
qCWarning(QT_BT) << "QBluetoothSocket::connectToService called on busy socket";
d->errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
diff --git a/src/bluetooth/qbluetoothsocket_osx.mm b/src/bluetooth/qbluetoothsocket_osx.mm
index 91393138..aa994550 100644
--- a/src/bluetooth/qbluetoothsocket_osx.mm
+++ b/src/bluetooth/qbluetoothsocket_osx.mm
@@ -73,9 +73,6 @@ QBluetoothSocketPrivate::~QBluetoothSocketPrivate()
void QBluetoothSocketPrivate::connectToService(const QBluetoothAddress &address, quint16 port,
QIODevice::OpenMode mode)
{
- // We have readwrite channels with IOBluetooth's channels.
- Q_UNUSED(openMode)
-
Q_ASSERT_X(state == QBluetoothSocket::ServiceLookupState
|| state == QBluetoothSocket::UnconnectedState,
Q_FUNC_INFO, "invalid state");
@@ -86,7 +83,7 @@ void QBluetoothSocketPrivate::connectToService(const QBluetoothAddress &address,
txBuffer.clear();
IOReturn status = kIOReturnError;
- // Set socket state on q_ptr will emit a signal,
+ // Setting socket state on q_ptr will emit a signal,
// we'd like to avoid any signal until this function completes.
const QBluetoothSocket::SocketState oldState = state;
// To prevent other connectToService calls (from QBluetoothSocket):
@@ -440,6 +437,14 @@ qint64 QBluetoothSocket::bytesToWrite() const
void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, OpenMode openMode)
{
+ // Report this problem early, potentially avoid device discovery:
+ if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "cannot connect with 'UnknownProtocol' type";
+ d_ptr->errorString = QCoreApplication::translate(SOCKET, SOC_NETWORK_ERROR);
+ setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
if (state() != UnconnectedState && state() != ServiceLookupState) {
qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "called on a busy socket";
d_ptr->errorString = QCoreApplication::translate(SOCKET, SOC_CONNECT_IN_PROGRESS);
@@ -469,6 +474,14 @@ void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, Op
void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
OpenMode openMode)
{
+ // Report this problem early, avoid device discovery:
+ if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "cannot connect with 'UnknownProtocol' type";
+ d_ptr->errorString = QCoreApplication::translate(SOCKET, SOC_NETWORK_ERROR);
+ setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
if (state() != QBluetoothSocket::UnconnectedState) {
qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "called on a busy socket";
d_ptr->errorString = QCoreApplication::translate(SOCKET, SOC_CONNECT_IN_PROGRESS);
@@ -486,6 +499,13 @@ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const
void QBluetoothSocket::connectToService(const QBluetoothAddress &address, quint16 port,
OpenMode openMode)
{
+ if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "cannot connect with 'UnknownProtocol' type";
+ d_ptr->errorString = QCoreApplication::translate(SOCKET, SOC_NETWORK_ERROR);
+ setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
if (state() != QBluetoothSocket::UnconnectedState) {
qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "called on a busy socket";
d_ptr->errorString = QCoreApplication::translate(SOCKET, SOC_CONNECT_IN_PROGRESS);