summaryrefslogtreecommitdiffstats
path: root/src/bluetooth
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2014-02-04 13:43:57 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-07 12:59:57 +0100
commit85e9ce386d2d931617f3c0d76acd4ca4165cb81d (patch)
tree404fd9907a34f62801c918195b41c8816d23fdda /src/bluetooth
parent477d9f450a1e9767dbf10f4c040e994231674a1f (diff)
QBluetoothServer fixes
1.) If QBluetoothServer::listen(QBluetoothUuid,QString) fails during service registration we have to ensure that the Server doesn't remain in listening state. 2.) 29de876f55dc96748fdca8dd3fef0c873791796f sets the socket descriptor to -1 when closing the QBluetoothSocket. QBluetothServer treats a value of -1 as error and aborts any call to listen(). This implies that any call to listen() after the first close() would always fail. This patch adds some redundancy and first tries to recreate the socket and only if the re-creation failed exists with an error. 3.) Catch case when user calls listen() on an already listening server. Documentation has been updated to document the behavior. Change-Id: I2df13500e74a9741017f7404f0e0c477c96d5356 Reviewed-by: Aaron McCarthy <mccarthy.aaron@gmail.com>
Diffstat (limited to 'src/bluetooth')
-rw-r--r--src/bluetooth/qbluetoothserver.cpp18
-rw-r--r--src/bluetooth/qbluetoothserver_bluez.cpp27
-rw-r--r--src/bluetooth/qbluetoothserver_qnx.cpp2
3 files changed, 39 insertions, 8 deletions
diff --git a/src/bluetooth/qbluetoothserver.cpp b/src/bluetooth/qbluetoothserver.cpp
index 967d4e71..e70ec1db 100644
--- a/src/bluetooth/qbluetoothserver.cpp
+++ b/src/bluetooth/qbluetoothserver.cpp
@@ -112,8 +112,11 @@ QT_BEGIN_NAMESPACE
Start listening for incoming connections to \a address on \a port.
- Returns true if the operation succeeded and the server is listening for
- incoming connections, otherwise returns false.
+ Returns \c true if the operation succeeded and the server is listening for
+ incoming connections, otherwise returns \c false.
+
+ If the server object is already listening for incoming connections this function
+ always returns \c false. \l close() should be called before calling this function.
\sa isListening(), newConnection()
*/
@@ -172,12 +175,17 @@ QBluetoothServer::~QBluetoothServer()
Convenience function for registering an SPP service with \a uuid and \a serviceName.
Because this function already registers the service, the QBluetoothServiceInfo object
- which is returned can not be changed any more.
+ which is returned can not be changed any more. To shutdown the server later on it is
+ required to call \l QBluetoothServiceInfo::unregisterService() and \l close() on this
+ server object.
Returns a registered QBluetoothServiceInfo instance if sucessful otherwise an
invalid QBluetoothServiceInfo. This function always assumes that the default Bluetooth adapter
should be used.
+ If the server object is already listening for incoming connections this function
+ returns an invalid \l QBluetoothServiceInfo.
+
For an RFCOMM server this function is equivalent to following code snippet.
\snippet qbluetoothserver.cpp listen
@@ -223,8 +231,10 @@ QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const
protocolDescriptorList);
bool result = serviceInfo.registerService();
//! [listen3]
- if (!result)
+ if (!result) {
+ close(); //close the still listening socket
return QBluetoothServiceInfo();
+ }
return serviceInfo;
}
diff --git a/src/bluetooth/qbluetoothserver_bluez.cpp b/src/bluetooth/qbluetoothserver_bluez.cpp
index 0b504d9a..8acd8e20 100644
--- a/src/bluetooth/qbluetoothserver_bluez.cpp
+++ b/src/bluetooth/qbluetoothserver_bluez.cpp
@@ -105,11 +105,32 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port)
{
Q_D(QBluetoothServer);
+ if (d->socket->state() == QBluetoothSocket::ListeningState) {
+ qCWarning(QT_BT_BLUEZ) << "Socket already in listen mode, close server first";
+ return false; //already listening, nothing to do
+ }
+
int sock = d->socket->socketDescriptor();
if (sock < 0) {
- d->m_lastError = InputOutputError;
- emit error(d->m_lastError);
- return false;
+ /* Negative socket descriptor is not always an error case
+ * Another cause could be a call to close()/abort()
+ * Check whether we can recover by re-creating the socket
+ * we should really call Bluez::QBluetoothSocketPrivate::ensureNativeSocket
+ * but a re-creation of the socket will do as well.
+ */
+
+ delete d->socket;
+ if (serverType() == QBluetoothServiceInfo::RfcommProtocol)
+ d->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
+ else
+ d->socket = new QBluetoothSocket(QBluetoothServiceInfo::L2capProtocol);
+
+ sock = d->socket->socketDescriptor();
+ if (sock < 0) {
+ d->m_lastError = InputOutputError;
+ emit error(d->m_lastError);
+ return false;
+ }
}
if (d->serverType == QBluetoothServiceInfo::RfcommProtocol) {
diff --git a/src/bluetooth/qbluetoothserver_qnx.cpp b/src/bluetooth/qbluetoothserver_qnx.cpp
index 9a16694f..d4289368 100644
--- a/src/bluetooth/qbluetoothserver_qnx.cpp
+++ b/src/bluetooth/qbluetoothserver_qnx.cpp
@@ -156,7 +156,7 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port)
// listen has already been called before
if (d->socket && d->socket->state() == QBluetoothSocket::ListeningState)
- return true;
+ return false;
d->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);