diff options
author | Andrew Stanley-Jones <andrew.stanley-jones@nokia.com> | 2011-04-07 13:41:29 +1000 |
---|---|---|
committer | Andrew Stanley-Jones <andrew.stanley-jones@nokia.com> | 2011-04-07 15:19:24 +1000 |
commit | 16f205b9f415f5113dd17439f690e9b5ca732e5d (patch) | |
tree | ef7ca980978dea7ee7766062686875fa34e4dc6f | |
parent | d9735cc3d47cb653f73c4bea2b7f2d993f7d59ab (diff) |
Fix symbian backend end of QRFcommServer auto test
Bluetooth QRfcommServer QTest unit does not pass all testscases;
added rfcommclient application to aid with qrfcommserver unit tests
Task-number: QTMOBILITY-1351
-rw-r--r-- | src/connectivity/bluetooth/qbluetoothsocket_symbian.cpp | 11 | ||||
-rw-r--r-- | src/connectivity/bluetooth/qrfcommserver_symbian.cpp | 24 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/qrfcommserver.pro | 4 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/rfcommclient/main.cpp | 108 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp | 210 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/rfcommclient/rfcommclient.h | 121 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro | 14 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/tst_qrfcommserver.cpp | 127 |
8 files changed, 554 insertions, 65 deletions
diff --git a/src/connectivity/bluetooth/qbluetoothsocket_symbian.cpp b/src/connectivity/bluetooth/qbluetoothsocket_symbian.cpp index d37795ef44..411f91b224 100644 --- a/src/connectivity/bluetooth/qbluetoothsocket_symbian.cpp +++ b/src/connectivity/bluetooth/qbluetoothsocket_symbian.cpp @@ -205,7 +205,7 @@ void QBluetoothSocketPrivate::receive() void QBluetoothSocketPrivate::HandleAcceptCompleteL(TInt aErr) { - qDebug() << __PRETTY_FUNCTION__; + qDebug() << __PRETTY_FUNCTION__ << ">> aErr=" << aErr; Q_Q(QBluetoothSocket); emit q->connected(); } @@ -217,6 +217,7 @@ void QBluetoothSocketPrivate::HandleActivateBasebandEventNotifierCompleteL(TInt void QBluetoothSocketPrivate::HandleConnectCompleteL(TInt aErr) { + qDebug() << __PRETTY_FUNCTION__ << ">> aErr=" << aErr; Q_Q(QBluetoothSocket); if (aErr == KErrNone) { q->setSocketState(QBluetoothSocket::ConnectedState); @@ -296,6 +297,7 @@ void QBluetoothSocketPrivate::HandleSendCompleteL(TInt aErr) void QBluetoothSocketPrivate::HandleShutdownCompleteL(TInt aErr) { + qDebug() << __PRETTY_FUNCTION__ << ">> aErr=" << aErr; Q_Q(QBluetoothSocket); if (aErr == KErrNone) { q->setSocketState(QBluetoothSocket::UnconnectedState); @@ -353,14 +355,14 @@ QString QBluetoothSocketPrivate::peerName() const iBlankSocket->RemoteName(sockAddr); else iSocket->RemoteName(sockAddr); + TInquirySockAddr address(sockAddr); address.SetBTAddr(sockAddr.BTAddr()); address.SetAction(KHostResName|KHostResIgnoreCache); // ignore name stored in cache err = resolver.GetByAddress(address, nameEntry); if(err == KErrNone) { - TNameRecord name = nameEntry(); - QString qString((QChar*)name.iName.Ptr(),name.iName.Length()); - m_peerName = qString; + TNameRecord name = nameEntry(); + m_peerName = QString((QChar*)name.iName.Ptr(),name.iName.Length()); } } resolver.Close(); @@ -368,7 +370,6 @@ QString QBluetoothSocketPrivate::peerName() const if (err != KErrNone) { return QString(); } - return m_peerName; } diff --git a/src/connectivity/bluetooth/qrfcommserver_symbian.cpp b/src/connectivity/bluetooth/qrfcommserver_symbian.cpp index 97af6eb334..d020958fd5 100644 --- a/src/connectivity/bluetooth/qrfcommserver_symbian.cpp +++ b/src/connectivity/bluetooth/qrfcommserver_symbian.cpp @@ -79,7 +79,8 @@ void QRfcommServer::close() bool QRfcommServer::listen(const QBluetoothAddress &address, quint16 port) { Q_D(QRfcommServer); - + qDebug() << __PRETTY_FUNCTION__ << ">> " << this << address.toString() << port; + TRfcommSockAddr addr; if (!address.isNull()) addr.SetBTAddr(TBTDevAddr(address.toUInt64())); @@ -107,10 +108,21 @@ bool QRfcommServer::listen(const QBluetoothAddress &address, quint16 port) break; } addr.SetSecurity(security); - d->ds->iSocket->Bind(addr); - d->socket->setSocketState(QBluetoothSocket::BoundState); - d->ds->iSocket->Listen(d->maxPendingConnections); + int err; + if ((err=d->ds->iSocket->Bind(addr)) == KErrNone) { + d->socket->setSocketState(QBluetoothSocket::BoundState); + } else { + qDebug() << __PRETTY_FUNCTION__ << "Socket bind failed, err=" << err; + d->socket->close(); + return false; + } + + if ((err=d->ds->iSocket->Listen(d->maxPendingConnections)) != KErrNone) { + qDebug() << __PRETTY_FUNCTION__ << "Socket listen failed, err=" << err; + d->socket->close(); + return false; + } d->pendingSocket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket, this); @@ -184,7 +196,7 @@ void QRfcommServerPrivate::HandleAcceptCompleteL(TInt aErr) pendingSocket->setSocketState(QBluetoothSocket::ConnectedState); activeSockets.append(pendingSocket); - QBluetoothSocketPrivate *pd = pendingSocket->d_ptr; + QBluetoothSocketPrivate *pd = pendingSocket->d_ptr; // ToDo: probably need a new socket here pd->iSocket->Accept(*pd->iBlankSocket); emit q->newConnection(); @@ -194,6 +206,8 @@ void QRfcommServerPrivate::HandleAcceptCompleteL(TInt aErr) pendingSocket = 0; socket->setSocketState(QBluetoothSocket::BoundState); } else { + // accept failed for unknown reason + socket->setSocketState(QBluetoothSocket::BoundState); qDebug() << __PRETTY_FUNCTION__ << aErr; return; } diff --git a/tests/auto/qrfcommserver/qrfcommserver.pro b/tests/auto/qrfcommserver/qrfcommserver.pro index 5374378e27..142eca9c5c 100644 --- a/tests/auto/qrfcommserver/qrfcommserver.pro +++ b/tests/auto/qrfcommserver/qrfcommserver.pro @@ -11,8 +11,10 @@ include(../../../common.pri) CONFIG += mobility MOBILITY = connectivity -symbian: TARGET.CAPABILITY = LocalServices NetworkControl + +symbian: TARGET.CAPABILITY = LocalServices NetworkControl WriteDeviceData OTHER_FILES += \ README.txt + diff --git a/tests/auto/qrfcommserver/rfcommclient/main.cpp b/tests/auto/qrfcommserver/rfcommclient/main.cpp new file mode 100644 index 0000000000..c510dba524 --- /dev/null +++ b/tests/auto/qrfcommserver/rfcommclient/main.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QCoreApplication> +#include <QStringList> +#include "rfcommclient.h" +#include <qbluetoothdeviceinfo.h> +//#include <qbluetoothlocaldevice.h> +//#include <QtTest/QtTest> + + + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + RfCommClient client; + QBluetoothLocalDevice localDevice; + MyThread mythread; + + QObject::connect(&client, SIGNAL(done()), &app, SLOT(quit())); + + QString address; + QString port; + QStringList args = QCoreApplication::arguments(); + + if(args.length() >= 2){ + address = args.at(1); + if(args.length() >= 3){ + port = args.at(2); + } + } + + // use previous value for client, stored earlier +// if(address.isEmpty()){ +// QSettings settings("QtDF", "bttennis"); +// address = settings.value("lastclient").toString(); +// } + + // hard-code address and port number if not provided + if(address.isEmpty()){ + address = "6C:9B:02:0C:91:D3"; // "J C7-2" + port = QString("20"); + } + + if(!address.isEmpty()){ + qDebug() << "Connecting to" << address << port; + QBluetoothDeviceInfo device = QBluetoothDeviceInfo(QBluetoothAddress(address), "", + QBluetoothDeviceInfo::MiscellaneousDevice); + QBluetoothServiceInfo service; + if (!port.isEmpty()) { + QBluetoothServiceInfo::Sequence protocolDescriptorList; + QBluetoothServiceInfo::Sequence protocol; + protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) + << QVariant::fromValue(port.toUShort()); + protocolDescriptorList.append(QVariant::fromValue(protocol)); + service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, + protocolDescriptorList); + qDebug() << "port" << port.toUShort() << service.protocolServiceMultiplexer(); + } + else { + service.setServiceUuid(QBluetoothUuid(serviceUuid)); + } + service.setDevice(device); + // delay so that server is in waiting state + qDebug() << "Starting sleep"; + mythread.sleep(10); // seconds + qDebug() << "Finished sleeping"; + client.startClient(service); + } else { + qDebug() << "failed because address and/or port is missing " << address << port; + } + + + return app.exec(); +} + diff --git a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp new file mode 100644 index 0000000000..d7351767c6 --- /dev/null +++ b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp @@ -0,0 +1,210 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "rfcommclient.h" + +#include <qbluetoothsocket.h> +#include <qbluetoothlocaldevice.h> + +#include <QtCore/QDataStream> +#include <QtCore/QByteArray> + +#include <QtCore/QStringList> + +void MyThread::sleep(int seconds) +{ + QThread::sleep(seconds); +} + +RfCommClient::RfCommClient(QObject *parent) +: QObject(parent), socket(0), stream(0), elapsed(new QTime), lagTimeout(0), state(listening) +{ + lagTimer.setSingleShot(true); + lagTimer.setInterval(5000); +} + +RfCommClient::~RfCommClient() +{ + stopClient(); +} + +bool RfCommClient::powerOn() +{ + qDebug() << __PRETTY_FUNCTION__ << ">>"; + // turn on BT in case it is not on + if (localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff) { + connect(&localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), + this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); +// connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), +// this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); + qDebug() << __PRETTY_FUNCTION__ << "Turning power on"; + localDevice.powerOn(); + } else { + qDebug() << __PRETTY_FUNCTION__ << "<< Power already on! returning true"; + return true; + } + qDebug() << __PRETTY_FUNCTION__ << "<< returning false"; + return false; +} + +void RfCommClient::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode) +{ + qDebug() << __PRETTY_FUNCTION__ << mode; + if (mode != QBluetoothLocalDevice::HostPoweredOff) + startClient(serviceInfo); +} +//! [startClient] +void RfCommClient::startClient(const QBluetoothServiceInfo &remoteService) +{ + qDebug() << __PRETTY_FUNCTION__ << ">>"; + serviceInfo = remoteService; + + // make sure preconditions are met + if (!powerOn() || socket) { + qDebug() << __PRETTY_FUNCTION__ << "<< power not on or socket already exists!"; + return; + } + + // Connect to service + if (state == listening) + state = pendingConnections; + socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket); + qDebug() << "Create socket"; + socket->connectToService(remoteService); + qDebug() << "ConnecttoService done"; + + connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); + connect(socket, SIGNAL(connected()), this, SLOT(connected())); + connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); + connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(error(QBluetoothSocket::SocketError))); + + qDebug() << __PRETTY_FUNCTION__ << "<<"; +} +//! [startClient] + +//! [stopClient] +void RfCommClient::stopClient() +{ + qDebug() << __PRETTY_FUNCTION__ << "closing client!"; + + lagTimer.stop(); + + delete stream; + stream = 0; + + socket->deleteLater(); + socket = 0; +} +//! [stopClient] + +//! [socketDisconnected] +void RfCommClient::socketDisconnected() +{ + // Note: it seems that the "disconnected" signal is not emitted by the socket, so this never gets called + qDebug() << __PRETTY_FUNCTION__ << "Got socketDisconnected"; + emit disconnected(); + stopClient(); + + // now reconnect and send text string + startClient(serviceInfo); + connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendText())); + lagTimer.start(); +} +//! [socketDisconnected] + +//! [readSocket] +void RfCommClient::readSocket() +{ + if (!socket) + return; + QString str; + + while (socket->bytesAvailable()) { + *stream >> str; + } + qDebug() << __PRETTY_FUNCTION__ << "socket read=" << str; +} +//! [readSocket] + +//! [connected] +void RfCommClient::connected() +{ + qDebug() << __PRETTY_FUNCTION__ << "connected to " << socket->peerName(); + stream = new QDataStream(socket); + emit connected(socket->peerName()); +} +//! [connected] + +void RfCommClient::error(QBluetoothSocket::SocketError err) +{ + qDebug() << __PRETTY_FUNCTION__ << "Got socket error" << err; + // remote side has closed the socket, effectively disconnecting it + if (state == pendingConnections) { + state = dataTransfer; + emit disconnected(); + stopClient(); + + // now reconnect and send text string + MyThread mythread; + mythread.sleep(5); + startClient(serviceInfo); + connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendText())); + lagTimer.start(); + } else { + qDebug() << __PRETTY_FUNCTION__ << "emitting done"; + emit done(); + } +} + +void RfCommClient::sendText() +{ + qDebug() << __PRETTY_FUNCTION__ << ">>"; + lagTimer.stop(); + if(stream) { + buffer.clear(); + buffer.append(QString("hello\r\n")); // ideally we would use QDataStream here + socket->write(buffer); + } + // terminate client program + emit done(); + qDebug() << __PRETTY_FUNCTION__ << "<< Terminating program"; +} + + diff --git a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.h b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.h new file mode 100644 index 0000000000..600682e64b --- /dev/null +++ b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.h @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef RFCOMMCLIENT_H +#define RFCOMMCLIENT_H + +#include <qbluetoothserviceinfo.h> +#include <qbluetoothsocket.h> + +#include <QtCore/QObject> +#include <QtCore/QTime> +#include <QtCore/QTimer> +#include <QBluetoothLocalDevice.h> +#include <QThread> + +static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c9"); + +QTM_BEGIN_NAMESPACE +class QBluetoothSocket; + +QTM_END_NAMESPACE + +class QDataStream; + +QTM_USE_NAMESPACE + +class MyThread : public QThread +{ +public: + void sleep(int seconds); +}; + + + +//! [declaration] +class RfCommClient : public QObject +{ + Q_OBJECT + + enum States { + listening, + pendingConnections, + dataTransfer + }; +public: + explicit RfCommClient(QObject *parent = 0); + ~RfCommClient(); + + void startClient(const QBluetoothServiceInfo &remoteService); + void stopClient(); + +public slots: + void error(QBluetoothSocket::SocketError); + +signals: + void connected(const QString &name); + void disconnected(); + void done(); + void lag(int ms); + +private slots: + void readSocket(); + void connected(); + void sendText(); + void socketDisconnected(); + void hostModeStateChanged(QBluetoothLocalDevice::HostMode); + +private: + bool powerOn(); + +private: + QBluetoothSocket *socket; + QDataStream *stream; + QByteArray buffer; + QBluetoothServiceInfo serviceInfo; + QBluetoothLocalDevice localDevice; + QTime *elapsed; + QTimer lagTimer; + int lagTimeout; + RfCommClient::States state; +}; +//! [declaration] + +#endif // RFCOMMCLIENT_H diff --git a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro new file mode 100644 index 0000000000..4fa74a7b95 --- /dev/null +++ b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro @@ -0,0 +1,14 @@ +TARGET = rfcommclient +SOURCES += main.cpp rfcommclient.cpp +HEADERS += rfcommclient.h + +QT = core + +INCLUDEPATH += ../../../../src/connectivity/bluetooth +DEPENDPATH += ../../../../src/connectivity/bluetooth + +include(../../../../common.pri) + +CONFIG += mobility +MOBILITY = connectivity +symbian: TARGET.CAPABILITY = LocalServices NetworkControl WriteDeviceData diff --git a/tests/auto/qrfcommserver/tst_qrfcommserver.cpp b/tests/auto/qrfcommserver/tst_qrfcommserver.cpp index 9d52511be7..ea96694903 100644 --- a/tests/auto/qrfcommserver/tst_qrfcommserver.cpp +++ b/tests/auto/qrfcommserver/tst_qrfcommserver.cpp @@ -83,7 +83,6 @@ private: tst_QRfcommServer::tst_QRfcommServer() { - localDevice.powerOn(); } tst_QRfcommServer::~tst_QRfcommServer() @@ -93,6 +92,23 @@ tst_QRfcommServer::~tst_QRfcommServer() void tst_QRfcommServer::initTestCase() { qRegisterMetaType<QBluetooth::SecurityFlags>("QBluetooth::SecurityFlags"); + + // turn on BT in case it is not on + if (localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff) { + QSignalSpy hostModeSpy(&localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); + QVERIFY(hostModeSpy.isEmpty()); + localDevice.powerOn(); + int connectTime = 5000; // ms + while (hostModeSpy.count() < 1 && connectTime > 0) { + QTest::qWait(500); + connectTime -= 500; + } + QVERIFY(hostModeSpy.count() > 0); + } + QBluetoothLocalDevice::HostMode hostMode= localDevice.hostMode(); + QVERIFY(hostMode == QBluetoothLocalDevice::HostConnectable + || hostMode == QBluetoothLocalDevice::HostDiscoverable + || hostMode == QBluetoothLocalDevice::HostDiscoverableLimitedInquiry); } void tst_QRfcommServer::tst_construction() @@ -119,7 +135,7 @@ void tst_QRfcommServer::tst_listen_data() //use localdevice address for listen address. QTest::newRow("specified address") << localDevice.address() << quint16(0); QTest::newRow("specified port") << QBluetoothAddress() << quint16(20); - QTest::newRow("specified address/port") << localDevice.address() << quint16(21); + QTest::newRow("specified address/port") << localDevice.address() << quint16(27); // port 21 returns KErrInUse #else QTest::newRow("specified address") << QBluetoothAddress("00:11:B1:08:AD:B8") << quint16(0); QTest::newRow("specified port") << QBluetoothAddress() << quint16(10); @@ -134,7 +150,7 @@ void tst_QRfcommServer::tst_listen() { QRfcommServer server; - + qDebug() << "tst_listen() address=" << address.toString() << "port=" << port; bool result = server.listen(address, port); QTest::qWait(1000); @@ -156,7 +172,7 @@ void tst_QRfcommServer::tst_listen() QVERIFY(server.nextPendingConnection() == 0); server.close(); - QTest::qWait(500); + QTest::qWait(2000); QVERIFY(!server.isListening()); @@ -180,60 +196,58 @@ void tst_QRfcommServer::tst_pendingConnections() { QFETCH(int, maxConnections); - { - QRfcommServer server; - - server.setMaxPendingConnections(maxConnections); - - bool result = server.listen(); - - QVERIFY(result); - QVERIFY(server.isListening()); - - qDebug() << "Listening on RFCOMM channel:" << server.serverPort(); - - QCOMPARE(server.maxPendingConnections(), maxConnections); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); + QRfcommServer server; + QBluetoothLocalDevice localDev; + + QBluetoothAddress address = localDev.address(); + server.setMaxPendingConnections(maxConnections); + bool result = server.listen(address, 20); // port == 20 + QTest::qWait(1000); - { - /* wait for maxConnections simultaneous connections */ - qDebug() << "Waiting for" << maxConnections << "simultaneous connections."; + QVERIFY(result); + QVERIFY(server.isListening()); - QSignalSpy connectionSpy(&server, SIGNAL(newConnection())); + qDebug() << "tst_pendingConnections() Listening on address " << address.toString() << "RFCOMM channel:" << server.serverPort(); - int connectTime = MaxConnectTime; - while (connectionSpy.count() < maxConnections && connectTime > 0) { - QTest::qWait(1000); - connectTime -= 1000; - } + QCOMPARE(server.maxPendingConnections(), maxConnections); - QList<QBluetoothSocket *> sockets; - while (server.hasPendingConnections()) - sockets.append(server.nextPendingConnection()); + QVERIFY(!server.hasPendingConnections()); + QVERIFY(server.nextPendingConnection() == 0); - QCOMPARE(connectionSpy.count(), maxConnections); - QCOMPARE(sockets.count(), maxConnections); + /* wait for maxConnections simultaneous connections */ + qDebug() << "Waiting for" << maxConnections << "simultaneous connections."; - foreach (QBluetoothSocket *socket, sockets) { - qDebug() << socket->state(); - QVERIFY(socket->state() == QBluetoothSocket::ConnectedState); - QVERIFY(socket->openMode() == QIODevice::ReadWrite); - } + QSignalSpy connectionSpy(&server, SIGNAL(newConnection())); - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); + int connectTime = MaxConnectTime; + while (connectionSpy.count() < maxConnections && connectTime > 0) { + QTest::qWait(1000); + connectTime -= 1000; + } + + QList<QBluetoothSocket *> sockets; + while (server.hasPendingConnections()) + sockets.append(server.nextPendingConnection()); + + QCOMPARE(connectionSpy.count(), maxConnections); + QCOMPARE(sockets.count(), maxConnections); + + foreach (QBluetoothSocket *socket, sockets) { + qDebug() << socket->state(); + QVERIFY(socket->state() == QBluetoothSocket::ConnectedState); + QVERIFY(socket->openMode() == QIODevice::ReadWrite); + } - while (!sockets.isEmpty()) { - QBluetoothSocket *socket = sockets.takeFirst(); - socket->close(); - delete socket; - } - } + QVERIFY(!server.hasPendingConnections()); + QVERIFY(server.nextPendingConnection() == 0); - server.close(); + while (!sockets.isEmpty()) { + QBluetoothSocket *socket = sockets.takeFirst(); + socket->close(); + delete socket; } + + server.close(); } void tst_QRfcommServer::tst_receive_data() @@ -248,12 +262,16 @@ void tst_QRfcommServer::tst_receive() QFETCH(QByteArray, expected); QRfcommServer server; - - bool result = server.listen(); + QBluetoothLocalDevice localDev; + + QBluetoothAddress address = localDev.address(); + bool result = server.listen(address, 20); // port == 20 + QTest::qWait(1000); QVERIFY(result); + QVERIFY(server.isListening()); - qDebug() << "Listening on RFCOMM channel:" << server.serverPort(); + qDebug() << "Listening on address " << address.toString() << "RFCOMM channel:" << server.serverPort(); int connectTime = MaxConnectTime; while (!server.hasPendingConnections() && connectTime > 0) { @@ -288,11 +306,12 @@ void tst_QRfcommServer::tst_receive() void tst_QRfcommServer::tst_secureFlags() { QRfcommServer server; - qDebug() << server.securityFlags(); - QCOMPARE(server.securityFlags(), QBluetooth::Authorization); + + server.setSecurityFlags(QBluetooth::NoSecurity); + QCOMPARE(server.securityFlags(), QBluetooth::NoSecurity); server.setSecurityFlags(QBluetooth::Encryption); - QCOMPARE(server.securityFlags(), QBluetooth::Encryption|QBluetooth::Authorization); + QCOMPARE(server.securityFlags(), QBluetooth::Encryption); } QTEST_MAIN(tst_QRfcommServer) |