summaryrefslogtreecommitdiffstats
path: root/tests/auto/qrfcommserver/rfcommclient
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/qrfcommserver/rfcommclient')
-rw-r--r--tests/auto/qrfcommserver/rfcommclient/main.cpp111
-rw-r--r--tests/auto/qrfcommserver/rfcommclient/rfcommclient.cpp210
-rw-r--r--tests/auto/qrfcommserver/rfcommclient/rfcommclient.h115
-rw-r--r--tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro7
4 files changed, 443 insertions, 0 deletions
diff --git a/tests/auto/qrfcommserver/rfcommclient/main.cpp b/tests/auto/qrfcommserver/rfcommclient/main.cpp
new file mode 100644
index 00000000..ee6b8584
--- /dev/null
+++ b/tests/auto/qrfcommserver/rfcommclient/main.cpp
@@ -0,0 +1,111 @@
+/****************************************************************************
+**
+** 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$
+** GNU Lesser General Public License Usage
+** 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.
+**
+** 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.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $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 00000000..f3e33f56
--- /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 00000000..6825576a
--- /dev/null
+++ b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.h
@@ -0,0 +1,115 @@
+/****************************************************************************
+**
+** 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");
+
+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
new file mode 100644
index 00000000..7a1834c6
--- /dev/null
+++ b/tests/auto/qrfcommserver/rfcommclient/rfcommclient.pro
@@ -0,0 +1,7 @@
+TARGET = rfcommclient
+SOURCES += main.cpp rfcommclient.cpp
+HEADERS += rfcommclient.h
+
+QT = core
+
+symbian: TARGET.CAPABILITY = LocalServices NetworkControl WriteDeviceData