From d58d134d25695dd044fc13d57a4d754b1dfb4e4a Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Wed, 11 May 2022 16:21:41 +0200 Subject: Introduce error codes for missing permissions Some operating systems might request special permissions while working with Bluetooth. Previously if the permissions were missing, we were returning an UnknownError, and the error description, when available, was not very good as well. [ChangeLog][QtBluetooth] Various error enums are extended with new error codes that represent missing permissions error. Error descriptions are also updated, when available. This patch also applies new error codes to Android implementation. Fixes: QTBUG-102373 Change-Id: I247371de6b1eb8d39f0f99c50269d2b1f3bf21c2 Reviewed-by: Juha Vuolle --- src/bluetooth/qbluetoothdevicediscoveryagent.cpp | 3 +++ src/bluetooth/qbluetoothdevicediscoveryagent.h | 1 + src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp | 7 ++++--- src/bluetooth/qbluetoothlocaldevice.cpp | 3 +++ src/bluetooth/qbluetoothlocaldevice.h | 1 + src/bluetooth/qbluetoothlocaldevice_android.cpp | 1 + src/bluetooth/qbluetoothserver.cpp | 3 +++ src/bluetooth/qbluetoothserver.h | 3 ++- src/bluetooth/qbluetoothserver_android.cpp | 2 +- src/bluetooth/qbluetoothservicediscoveryagent.cpp | 3 +++ src/bluetooth/qbluetoothservicediscoveryagent.h | 1 + src/bluetooth/qbluetoothservicediscoveryagent_android.cpp | 5 +++-- src/bluetooth/qbluetoothsocket.cpp | 3 +++ src/bluetooth/qbluetoothsocket.h | 3 ++- src/bluetooth/qbluetoothsocket_android.cpp | 5 +++-- src/bluetooth/qlowenergycontroller.cpp | 3 +++ src/bluetooth/qlowenergycontroller.h | 3 ++- src/bluetooth/qlowenergycontroller_android.cpp | 4 ++-- tests/bttestui/btlocaldevice.cpp | 2 ++ 19 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent.cpp index 727ea690..126bfac8 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent.cpp @@ -100,6 +100,9 @@ Q_DECLARE_LOGGING_CATEGORY(QT_BT) \value LocationServiceTurnedOffError The location service is turned off. Usage of Bluetooth APIs is not possible when location service is turned off. This value was introduced by Qt 6.2. + \value [since 6.4] MissingPermissionsError The operating system requests + permissions which were not + granted by the user. \value UnknownError An unknown error has occurred. */ diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent.h b/src/bluetooth/qbluetoothdevicediscoveryagent.h index 1e536c09..d2e3c92d 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent.h +++ b/src/bluetooth/qbluetoothdevicediscoveryagent.h @@ -66,6 +66,7 @@ public: UnsupportedPlatformError, UnsupportedDiscoveryMethod, LocationServiceTurnedOffError, + MissingPermissionsError, UnknownError = 100 // New errors must be added before Unknown error }; Q_ENUM(Error) diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp index 208228d7..1281b9ed 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp @@ -148,7 +148,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent "Search not possible due to missing permission (ACCESS_FINE_LOCATION)"; errorString = QBluetoothDeviceDiscoveryAgent::tr( "Missing Location permission. Search is not possible."); - lastError = QBluetoothDeviceDiscoveryAgent::UnknownError; + lastError = QBluetoothDeviceDiscoveryAgent::MissingPermissionsError; emit q->errorOccurred(lastError); return; } @@ -193,8 +193,9 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent if (!(ensureAndroidPermission(BluetoothPermission::Scan) && ensureAndroidPermission(BluetoothPermission::Connect))) { qCWarning(QT_BT_ANDROID) << "Device discovery start() failed due to missing permissions"; - errorString = QBluetoothDeviceDiscoveryAgent::tr("Bluetooth adapter error"); - lastError = QBluetoothDeviceDiscoveryAgent::UnknownError; + errorString = QBluetoothDeviceDiscoveryAgent::tr( + "Failed to start device discovery due to missing permissions."); + lastError = QBluetoothDeviceDiscoveryAgent::MissingPermissionsError; emit q->errorOccurred(lastError); return; } diff --git a/src/bluetooth/qbluetoothlocaldevice.cpp b/src/bluetooth/qbluetoothlocaldevice.cpp index c9ac472f..20bd8f9b 100644 --- a/src/bluetooth/qbluetoothlocaldevice.cpp +++ b/src/bluetooth/qbluetoothlocaldevice.cpp @@ -85,6 +85,9 @@ QT_IMPL_METATYPE_EXTERN_TAGGED(QBluetoothLocalDevice::Error, QBluetoothLocalDevi \value NoError No known error \value PairingError Error in pairing + \value [since 6.4] MissingPermissionsError The operating system requests + permissions which were not + granted by the user. \value UnknownError Unknown error */ diff --git a/src/bluetooth/qbluetoothlocaldevice.h b/src/bluetooth/qbluetoothlocaldevice.h index 0da6f54c..bd75d3fd 100644 --- a/src/bluetooth/qbluetoothlocaldevice.h +++ b/src/bluetooth/qbluetoothlocaldevice.h @@ -75,6 +75,7 @@ public: enum Error { NoError, PairingError, + MissingPermissionsError, UnknownError = 100 }; Q_ENUM(Error) diff --git a/src/bluetooth/qbluetoothlocaldevice_android.cpp b/src/bluetooth/qbluetoothlocaldevice_android.cpp index cba1cf85..e108a0a8 100644 --- a/src/bluetooth/qbluetoothlocaldevice_android.cpp +++ b/src/bluetooth/qbluetoothlocaldevice_android.cpp @@ -299,6 +299,7 @@ void QBluetoothLocalDevice::setHostMode(QBluetoothLocalDevice::HostMode requeste if (!ensureAndroidPermission(BluetoothPermission::Advertise)) { qCWarning(QT_BT_ANDROID) << "Local device setHostMode() failed due to" "missing permissions"; + emit errorOccurred(QBluetoothLocalDevice::MissingPermissionsError); return; } const bool success = (bool)QJniObject::callStaticMethod( diff --git a/src/bluetooth/qbluetoothserver.cpp b/src/bluetooth/qbluetoothserver.cpp index c7332220..d6114b83 100644 --- a/src/bluetooth/qbluetoothserver.cpp +++ b/src/bluetooth/qbluetoothserver.cpp @@ -111,6 +111,9 @@ QT_BEGIN_NAMESPACE \value ServiceAlreadyRegisteredError The service or port was already registered \value UnsupportedProtocolError The \l {QBluetoothServiceInfo::Protocol}{Protocol} is not supported on this platform. + \value [since 6.4] MissingPermissionsError The operating system requests + permissions which were not + granted by the user. */ /*! diff --git a/src/bluetooth/qbluetoothserver.h b/src/bluetooth/qbluetoothserver.h index 0a13812b..cc5dc52c 100644 --- a/src/bluetooth/qbluetoothserver.h +++ b/src/bluetooth/qbluetoothserver.h @@ -66,7 +66,8 @@ public: PoweredOffError, InputOutputError, ServiceAlreadyRegisteredError, - UnsupportedProtocolError + UnsupportedProtocolError, + MissingPermissionsError }; Q_ENUM(Error) diff --git a/src/bluetooth/qbluetoothserver_android.cpp b/src/bluetooth/qbluetoothserver_android.cpp index 5089d3ce..33daa161 100644 --- a/src/bluetooth/qbluetoothserver_android.cpp +++ b/src/bluetooth/qbluetoothserver_android.cpp @@ -135,7 +135,7 @@ bool QBluetoothServer::listen(const QBluetoothAddress &localAdapter, quint16 por if (!ensureAndroidPermission(BluetoothPermission::Connect)) { qCWarning(QT_BT_ANDROID) << "Bluetooth server listen() failed due to missing permissions"; - d->m_lastError = QBluetoothServer::UnknownError; + d->m_lastError = QBluetoothServer::MissingPermissionsError; emit errorOccurred(d->m_lastError); return false; } diff --git a/src/bluetooth/qbluetoothservicediscoveryagent.cpp b/src/bluetooth/qbluetoothservicediscoveryagent.cpp index 5b374094..c7827b48 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent.cpp @@ -102,6 +102,9 @@ QT_BEGIN_NAMESPACE \value InvalidBluetoothAdapterError The passed local adapter address does not match the physical adapter address of any local Bluetooth device. This value was introduced by Qt 5.3. + \value [since 6.4] MissingPermissionsError The operating system requests + permissions which were not + granted by the user. \value UnknownError An unknown error has occurred. */ diff --git a/src/bluetooth/qbluetoothservicediscoveryagent.h b/src/bluetooth/qbluetoothservicediscoveryagent.h index dd944a7e..68050dbd 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent.h +++ b/src/bluetooth/qbluetoothservicediscoveryagent.h @@ -69,6 +69,7 @@ public: InputOutputError = QBluetoothDeviceDiscoveryAgent::InputOutputError, PoweredOffError = QBluetoothDeviceDiscoveryAgent::PoweredOffError, InvalidBluetoothAdapterError = QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError, + MissingPermissionsError = QBluetoothDeviceDiscoveryAgent::MissingPermissionsError, UnknownError = QBluetoothDeviceDiscoveryAgent::UnknownError //=100 //New Errors must be added after Unknown Error the space before UnknownError is reserved //for future device discovery errors diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp index 5b9d1965..81b4e83f 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent_android.cpp @@ -119,8 +119,9 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr if (!ensureAndroidPermission(BluetoothPermission::Connect)) { qCWarning(QT_BT_ANDROID) << "Service discovery start() failed due to missing permissions"; - error = QBluetoothServiceDiscoveryAgent::UnknownError; - errorString = QBluetoothServiceDiscoveryAgent::tr("Unable to perform SDP scan"); + error = QBluetoothServiceDiscoveryAgent::MissingPermissionsError; + errorString = QBluetoothServiceDiscoveryAgent::tr( + "Failed to start service discovery due to missing permissions."); emit q->errorOccurred(error); _q_serviceDiscoveryFinished(); return; diff --git a/src/bluetooth/qbluetoothsocket.cpp b/src/bluetooth/qbluetoothsocket.cpp index cd075cec..b7e7348c 100644 --- a/src/bluetooth/qbluetoothsocket.cpp +++ b/src/bluetooth/qbluetoothsocket.cpp @@ -131,6 +131,9 @@ Q_DECLARE_LOGGING_CATEGORY(QT_BT) that did not permit it. \value RemoteHostClosedError The remote host closed the connection. This value was introduced by Qt 5.10. + \value [since 6.4] MissingPermissionsError The operating system requests + permissions which were not + granted by the user. */ /*! diff --git a/src/bluetooth/qbluetoothsocket.h b/src/bluetooth/qbluetoothsocket.h index 6a34fd7b..59613444 100644 --- a/src/bluetooth/qbluetoothsocket.h +++ b/src/bluetooth/qbluetoothsocket.h @@ -91,7 +91,8 @@ public: ServiceNotFoundError, NetworkError, UnsupportedProtocolError, - OperationError + OperationError, + MissingPermissionsError }; Q_ENUM(SocketError) diff --git a/src/bluetooth/qbluetoothsocket_android.cpp b/src/bluetooth/qbluetoothsocket_android.cpp index 4c533226..9d8359a1 100644 --- a/src/bluetooth/qbluetoothsocket_android.cpp +++ b/src/bluetooth/qbluetoothsocket_android.cpp @@ -278,8 +278,9 @@ void QBluetoothSocketPrivateAndroid::connectToServiceHelper(const QBluetoothAddr if (!ensureAndroidPermission(BluetoothPermission::Connect)) { qCWarning(QT_BT_ANDROID) << "Bluetooth socket connect failed due to missing permissions"; - errorString = QBluetoothSocket::tr("Unknown socket error"); - q->setSocketError(QBluetoothSocket::SocketError::UnknownSocketError); + errorString = QBluetoothSocket::tr( + "Bluetooth socket connect failed due to missing permissions."); + q->setSocketError(QBluetoothSocket::SocketError::MissingPermissionsError); q->setSocketState(QBluetoothSocket::SocketState::UnconnectedState); return; } diff --git a/src/bluetooth/qlowenergycontroller.cpp b/src/bluetooth/qlowenergycontroller.cpp index eeebe5fe..ca279aa3 100644 --- a/src/bluetooth/qlowenergycontroller.cpp +++ b/src/bluetooth/qlowenergycontroller.cpp @@ -169,6 +169,9 @@ Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID) \value AuthorizationError The local Bluetooth device closed the connection due to insufficient authorization. This value was introduced by Qt 5.14. + \value [since 6.4] MissingPermissionsError The operating system requests + permissions which were not + granted by the user. */ /*! diff --git a/src/bluetooth/qlowenergycontroller.h b/src/bluetooth/qlowenergycontroller.h index 4a9baa29..4bd2234c 100644 --- a/src/bluetooth/qlowenergycontroller.h +++ b/src/bluetooth/qlowenergycontroller.h @@ -67,7 +67,8 @@ public: ConnectionError, AdvertisingError, RemoteHostClosedError, - AuthorizationError + AuthorizationError, + MissingPermissionsError }; Q_ENUM(Error) diff --git a/src/bluetooth/qlowenergycontroller_android.cpp b/src/bluetooth/qlowenergycontroller_android.cpp index 61d3713b..472f1108 100644 --- a/src/bluetooth/qlowenergycontroller_android.cpp +++ b/src/bluetooth/qlowenergycontroller_android.cpp @@ -145,7 +145,7 @@ void QLowEnergyControllerPrivateAndroid::connectToDevice() if (!ensureAndroidPermission(BluetoothPermission::Connect)) { // This is unlikely to happen as a valid local adapter is a precondition - setError(QLowEnergyController::AuthorizationError); + setError(QLowEnergyController::MissingPermissionsError); qCWarning(QT_BT_ANDROID) << "connectToDevice() failed due to missing permissions"; return; } @@ -1004,7 +1004,7 @@ void QLowEnergyControllerPrivateAndroid::startAdvertising(const QLowEnergyAdvert if (!(ensureAndroidPermission(BluetoothPermission::Advertise) && ensureAndroidPermission(BluetoothPermission::Connect))) { qCWarning(QT_BT_ANDROID) << "startAdvertising() failed due to missing permissions"; - setError(QLowEnergyController::AdvertisingError); + setError(QLowEnergyController::MissingPermissionsError); setState(QLowEnergyController::UnconnectedState); return; } diff --git a/tests/bttestui/btlocaldevice.cpp b/tests/bttestui/btlocaldevice.cpp index 53634a78..30dde951 100644 --- a/tests/bttestui/btlocaldevice.cpp +++ b/tests/bttestui/btlocaldevice.cpp @@ -489,6 +489,7 @@ void BtLocalDevice::dumpSocketInformation() case QBluetoothSocket::SocketError::NetworkError: tmp += "NetworkError"; break; //case QBluetoothSocket::SocketError::OperationError: tmp+= "OperationError"; break; case QBluetoothSocket::SocketError::UnsupportedProtocolError: tmp += "UnsupportedProtocolError"; break; + case QBluetoothSocket::SocketError::MissingPermissionsError: tmp += "MissingPermissionsError"; break; default: tmp+= "Undefined"; break; } @@ -730,6 +731,7 @@ void BtLocalDevice::dumpServerInformation() case QBluetoothSocket::SocketError::NetworkError: tmp += "NetworkError"; break; case QBluetoothSocket::SocketError::UnsupportedProtocolError: tmp += "UnsupportedProtocolError"; break; //case QBluetoothSocket::SocketError::OperationError: tmp+= "OperationError"; break; + case QBluetoothSocket::SocketError::MissingPermissionsError: tmp += "MissingPermissionsError"; break; default: tmp += QString::number(static_cast(client->error())); break; } -- cgit v1.2.3