diff options
author | Alex Blasche <alexander.blasche@digia.com> | 2014-02-28 15:50:26 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-03-03 14:44:51 +0100 |
commit | 68717ee6988633cec49f36ec7bc3640e9c14e55d (patch) | |
tree | 2148b325b278c5611a26cc56680046ce58c0b76a | |
parent | 5ac9aebf83bc7cc4025004232c32dbd62cf25ba3 (diff) |
Create QBluetoothServer unit test from QRfcommServer/QL2CAPServer tests
Task-number: QTBUG-22017
Change-Id: I3e5613153d2496567f7afd87c8a0a44c1067a4e7
Reviewed-by: Nedim Hadzic <nedimhadzija@gmail.com>
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
-rw-r--r-- | tests/auto/auto.pro | 3 | ||||
-rw-r--r-- | tests/auto/qbluetoothserver/qbluetoothserver.pro (renamed from tests/auto/qrfcommserver/qrfcommserver.pro) | 4 | ||||
-rw-r--r-- | tests/auto/qbluetoothserver/tst_qbluetoothserver.cpp (renamed from tests/auto/qrfcommserver/tst_qrfcommserver.cpp) | 26 | ||||
-rw-r--r-- | tests/auto/ql2capserver/ql2capserver.pro | 8 | ||||
-rw-r--r-- | tests/auto/ql2capserver/tst_ql2capserver.cpp | 285 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/README.txt | 50 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/rfcommclient/main.cpp | 111 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp | 211 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/rfcommclient/rfcommclient.h | 116 | ||||
-rw-r--r-- | tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro | 6 |
10 files changed, 16 insertions, 804 deletions
diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 10798a1b..5a703944 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -16,8 +16,7 @@ qtHaveModule(bluetooth) { qbluetoothtransfermanager \ qbluetoothtransferrequest \ qbluetoothuuid \ - ql2capserver \ - qrfcommserver + qbluetoothserver } qtHaveModule(nfc) { diff --git a/tests/auto/qrfcommserver/qrfcommserver.pro b/tests/auto/qbluetoothserver/qbluetoothserver.pro index 546b9ccb..4b5e5d46 100644 --- a/tests/auto/qrfcommserver/qrfcommserver.pro +++ b/tests/auto/qbluetoothserver/qbluetoothserver.pro @@ -1,5 +1,5 @@ -SOURCES += tst_qrfcommserver.cpp -TARGET = tst_qrfcommserver +SOURCES += tst_qbluetoothserver.cpp +TARGET = tst_qbluetoothserver CONFIG += testcase QT = core concurrent bluetooth testlib diff --git a/tests/auto/qrfcommserver/tst_qrfcommserver.cpp b/tests/auto/qbluetoothserver/tst_qbluetoothserver.cpp index 068e8fdc..347cddce 100644 --- a/tests/auto/qrfcommserver/tst_qrfcommserver.cpp +++ b/tests/auto/qbluetoothserver/tst_qbluetoothserver.cpp @@ -58,13 +58,13 @@ Q_DECLARE_METATYPE(QBluetoothServer::Error) // Max time to wait for connection static const int MaxConnectTime = 60 * 1000; // 1 minute in ms -class tst_QRfcommServer : public QObject +class tst_QBluetoothServer : public QObject { Q_OBJECT public: - tst_QRfcommServer(); - ~tst_QRfcommServer(); + tst_QBluetoothServer(); + ~tst_QBluetoothServer(); private slots: void initTestCase(); @@ -82,15 +82,15 @@ private: QBluetoothLocalDevice::HostMode initialHostMode; }; -tst_QRfcommServer::tst_QRfcommServer() +tst_QBluetoothServer::tst_QBluetoothServer() { } -tst_QRfcommServer::~tst_QRfcommServer() +tst_QBluetoothServer::~tst_QBluetoothServer() { } -void tst_QRfcommServer::setHostMode(const QBluetoothAddress &localAdapter, +void tst_QBluetoothServer::setHostMode(const QBluetoothAddress &localAdapter, QBluetoothLocalDevice::HostMode newHostMode) { QBluetoothLocalDevice device(localAdapter); @@ -124,7 +124,7 @@ void tst_QRfcommServer::setHostMode(const QBluetoothAddress &localAdapter, } } -void tst_QRfcommServer::initTestCase() +void tst_QBluetoothServer::initTestCase() { qRegisterMetaType<QBluetooth::SecurityFlags>("QBluetooth::SecurityFlags"); qRegisterMetaType<QBluetoothServer::Error>("QBluetoothServer::Error"); @@ -142,13 +142,13 @@ void tst_QRfcommServer::initTestCase() QVERIFY(hostMode != QBluetoothLocalDevice::HostPoweredOff); } -void tst_QRfcommServer::cleanupTestCase() +void tst_QBluetoothServer::cleanupTestCase() { QBluetoothLocalDevice device; setHostMode(device.address(), initialHostMode); } -void tst_QRfcommServer::tst_construction() +void tst_QBluetoothServer::tst_construction() { { QBluetoothServer server(QBluetoothServiceInfo::RfcommProtocol); @@ -173,14 +173,14 @@ void tst_QRfcommServer::tst_construction() } } -void tst_QRfcommServer::tst_receive_data() +void tst_QBluetoothServer::tst_receive_data() { QTest::addColumn<QBluetoothLocalDevice::HostMode>("hostmode"); QTest::newRow("offline mode") << QBluetoothLocalDevice::HostPoweredOff; QTest::newRow("online mode") << QBluetoothLocalDevice::HostConnectable; } -void tst_QRfcommServer::tst_receive() +void tst_QBluetoothServer::tst_receive() { QFETCH(QBluetoothLocalDevice::HostMode, hostmode); @@ -235,6 +235,6 @@ void tst_QRfcommServer::tst_receive() } -QTEST_MAIN(tst_QRfcommServer) +QTEST_MAIN(tst_QBluetoothServer) -#include "tst_qrfcommserver.moc" +#include "tst_qbluetoothserver.moc" diff --git a/tests/auto/ql2capserver/ql2capserver.pro b/tests/auto/ql2capserver/ql2capserver.pro deleted file mode 100644 index c87a0503..00000000 --- a/tests/auto/ql2capserver/ql2capserver.pro +++ /dev/null @@ -1,8 +0,0 @@ -SOURCES += tst_ql2capserver.cpp -TARGET = tst_ql2capserver - -# Note, not really an autotest. Requires manually setting up and running btclient first. -#CONFIG += testcase - -QT = core concurrent bluetooth testlib -DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/tests/auto/ql2capserver/tst_ql2capserver.cpp b/tests/auto/ql2capserver/tst_ql2capserver.cpp deleted file mode 100644 index d8a5d0d4..00000000 --- a/tests/auto/ql2capserver/tst_ql2capserver.cpp +++ /dev/null @@ -1,285 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtBluetooth module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** 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, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtTest/QtTest> - -#include <QDebug> - -#include <qbluetoothserver.h> -#include <qbluetoothsocket.h> - -QT_USE_NAMESPACE - -Q_DECLARE_METATYPE(QBluetooth::SecurityFlags); - -// Max time to wait for connection -static const int MaxConnectTime = 60 * 1000; // 1 minute in ms - -class tst_QL2capServer : public QObject -{ - Q_OBJECT - -public: - tst_QL2capServer(); - ~tst_QL2capServer(); - -private slots: - void initTestCase(); - - void tst_construction(); - - void tst_listen_data(); - void tst_listen(); - - void tst_pendingConnections_data(); - void tst_pendingConnections(); - - void tst_receive_data(); - void tst_receive(); - - void tst_secureFlags(); -}; - -tst_QL2capServer::tst_QL2capServer() -{ -} - -tst_QL2capServer::~tst_QL2capServer() -{ -} - -void tst_QL2capServer::initTestCase() -{ - qRegisterMetaType<QBluetooth::SecurityFlags>("QBluetooth::SecurityFlags"); -} - -void tst_QL2capServer::tst_construction() -{ - { - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - - QVERIFY(!server.isListening()); - QCOMPARE(server.maxPendingConnections(), 1); - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - QVERIFY(server.serverAddress().isNull()); - QCOMPARE(server.serverPort(), quint16(24160)); - } -} - -void tst_QL2capServer::tst_listen_data() -{ - QTest::addColumn<QBluetoothAddress>("address"); - QTest::addColumn<quint16>("port"); - - QTest::newRow("default") << QBluetoothAddress() << quint16(0); - QTest::newRow("specified address") << QBluetoothAddress("00:11:B1:08:AD:B8") << quint16(0); - QTest::newRow("specified port") << QBluetoothAddress() << quint16(24160); - QTest::newRow("specified address/port") << QBluetoothAddress("00:11:B1:08:AD:B8") << quint16(10); -} - -void tst_QL2capServer::tst_listen() -{ - QFETCH(QBluetoothAddress, address); - QFETCH(quint16, port); - - { - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - - bool result = server.listen(address, port); - - QVERIFY(result); - QVERIFY(server.isListening()); - - if (!address.isNull()) - QCOMPARE(server.serverAddress(), address); - else - QVERIFY(!server.serverAddress().isNull()); - - if (port != 0) - QCOMPARE(server.serverPort(), port); - else - QVERIFY(server.serverPort() != 0); - - QCOMPARE(server.maxPendingConnections(), 1); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - server.close(); - - QVERIFY(!server.isListening()); - - QVERIFY(server.serverAddress().isNull()); - QVERIFY(server.serverPort() == 0); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - } -} - -void tst_QL2capServer::tst_pendingConnections_data() -{ - QTest::addColumn<int>("maxConnections"); - - QTest::newRow("1 connection") << 1; - //QTest::newRow("2 connections") << 2; -} - -void tst_QL2capServer::tst_pendingConnections() -{ - QFETCH(int, maxConnections); - - { - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - - server.setMaxPendingConnections(maxConnections); - - bool result = server.listen(); - - QVERIFY(result); - QVERIFY(server.isListening()); - - qDebug() << "Listening on L2CAP channel:" << server.serverPort(); - - QCOMPARE(server.maxPendingConnections(), maxConnections); - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - { - /* wait for maxConnections simultaneous connections */ - qDebug() << "Waiting for" << maxConnections << "simultaneous connections."; - - QSignalSpy connectionSpy(&server, SIGNAL(newConnection())); - - 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); - } - - QVERIFY(!server.hasPendingConnections()); - QVERIFY(server.nextPendingConnection() == 0); - - while (!sockets.isEmpty()) { - QBluetoothSocket *socket = sockets.takeFirst(); - socket->close(); - delete socket; - } - } - - server.close(); - } -} - -void tst_QL2capServer::tst_receive_data() -{ - QTest::addColumn<QByteArray>("expected"); - - QTest::newRow("test") << QByteArray("hello\r\n"); -} - -void tst_QL2capServer::tst_receive() -{ - QFETCH(QByteArray, expected); - - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - - bool result = server.listen(); - - QVERIFY(result); - - qDebug() << "Listening on L2CAP channel:" << server.serverPort(); - - int connectTime = MaxConnectTime; - while (!server.hasPendingConnections() && connectTime > 0) { - QTest::qWait(1000); - connectTime -= 1000; - } - - QVERIFY(server.hasPendingConnections()); - - QBluetoothSocket *socket = server.nextPendingConnection(); - - QVERIFY(socket->state() == QBluetoothSocket::ConnectedState); - QVERIFY(socket->openMode() == QIODevice::ReadWrite); - - QSignalSpy readyReadSpy(socket, SIGNAL(readyRead())); - - int readyReadTime = 60000; - while (readyReadSpy.isEmpty() && readyReadTime > 0) { - QTest::qWait(1000); - readyReadTime -= 1000; - } - - QVERIFY(!readyReadSpy.isEmpty()); - - const QByteArray data = socket->readAll(); - - QCOMPARE(data, expected); -} - -void tst_QL2capServer::tst_secureFlags() -{ - QBluetoothServer server(QBluetoothServiceInfo::L2capProtocol); - QCOMPARE(server.securityFlags(), QBluetooth::NoSecurity); - - server.setSecurityFlags(QBluetooth::Encryption); - QCOMPARE(server.securityFlags(), QBluetooth::Encryption); -} - -QTEST_MAIN(tst_QL2capServer) - -#include "tst_ql2capserver.moc" diff --git a/tests/auto/qrfcommserver/README.txt b/tests/auto/qrfcommserver/README.txt deleted file mode 100644 index 8f3d2ef8..00000000 --- a/tests/auto/qrfcommserver/README.txt +++ /dev/null @@ -1,50 +0,0 @@ -This test currently can not be run automatically. - -To make this test work, manual connections must be started from a client -machine. There are 2 options for doing this, either use the rfcommclient application in the subdirectory, -or run the test manually as described below. When using rfcommclient, start qrfcommserver first and then -start rfcommclient on the other device. There might be some timing issues, so beware. - -Steps to run this test manually: - -1. start tst_qrfcommserver - -2. Wait for test display: - -QDEBUG : tst_QRfcommServer::tst_pendingConnections(1 connection) Waiting for 1 simultaneous connections - -On a linux machine run: - -rfcomm connect hci0 00:1A:9F:92:9E:5A - -On the linux machine you will see: - -Connected /dev/rfcomm0 to 00:1A:9F:92:9E:5A on channel 1 -Press CTRL-C for hangup -Disconnected - -and the test will pass. - -3. Wait for the test to then display: - -QDEBUG : tst_QRfcommServer::tst_receive(test) Listening on RFCOMM channel: 1 - -On the linux machine run: - -rfcomm connect hci0 00:1A:9F:92:9E:5A - -You will see: - -Connected /dev/rfcomm0 to 00:1A:9F:92:9E:5A on channel 1 -Press CTRL-C for hangup - -now run: - -echo -en "hello\r\n" > /dev/rfcomm0 - -The test should then pass. - -A linux machine is not required, but simple used as an example for how to make -easy rfcomm connections. Any system that will connect to a device and rfcomm -port will work. The format of the string for the second test is very important -for the test to pass. diff --git a/tests/auto/qrfcommserver/rfcommclient/main.cpp b/tests/auto/qrfcommserver/rfcommclient/main.cpp deleted file mode 100644 index be751074..00000000 --- a/tests/auto/qrfcommserver/rfcommclient/main.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** 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, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $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 deleted file mode 100644 index 0aa83016..00000000 --- a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp +++ /dev/null @@ -1,211 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** 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, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $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(QBluetoothServiceInfo::RfcommProtocol); - 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 deleted file mode 100644 index 48f1e760..00000000 --- a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.h +++ /dev/null @@ -1,116 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** 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, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $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> -#include <QThread> - -static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c9"); - -class QBluetoothSocket; - -class QDataStream; - -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 deleted file mode 100644 index 8c28a661..00000000 --- a/tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro +++ /dev/null @@ -1,6 +0,0 @@ -TARGET = rfcommclient -SOURCES += main.cpp rfcommclient.cpp -HEADERS += rfcommclient.h - -QT = core bluetooth -DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 |