1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "bluetoothlistener.h"
34 #include "trkdevice.h"
36 #include <QtCore/QDebug>
45 // Process id helpers.
47 inline DWORD processId(const QProcess &p)
49 if (const Q_PID processInfoStruct = p.pid())
50 return processInfoStruct->dwProcessId;
54 inline Q_PID processId(const QProcess &p)
65 struct BluetoothListenerPrivate {
66 BluetoothListenerPrivate();
74 bool printConsoleMessages;
75 BluetoothListener::Mode mode;
78 BluetoothListenerPrivate::BluetoothListenerPrivate() :
80 printConsoleMessages(false),
81 mode(BluetoothListener::Listen)
85 BluetoothListener::BluetoothListener(QObject *parent) :
87 d(new BluetoothListenerPrivate)
89 d->process.setProcessChannelMode(QProcess::MergedChannels);
91 connect(&d->process, SIGNAL(readyReadStandardError()),
92 this, SLOT(slotStdError()));
93 connect(&d->process, SIGNAL(readyReadStandardOutput()),
94 this, SLOT(slotStdOutput()));
95 connect(&d->process, SIGNAL(finished(int, QProcess::ExitStatus)),
96 this, SLOT(slotProcessFinished(int,QProcess::ExitStatus)));
97 connect(&d->process, SIGNAL(error(QProcess::ProcessError)),
98 this, SLOT(slotProcessError(QProcess::ProcessError)));
101 BluetoothListener::~BluetoothListener()
103 const int trc = terminateProcess();
105 qDebug() << "~BluetoothListener: terminated" << trc;
109 BluetoothListener::Mode BluetoothListener::mode() const
114 void BluetoothListener::setMode(Mode m)
119 bool BluetoothListener::printConsoleMessages() const
121 return d->printConsoleMessages;
124 void BluetoothListener::setPrintConsoleMessages(bool p)
126 d->printConsoleMessages = p;
129 int BluetoothListener::terminateProcess()
131 enum { TimeOutMS = 200 };
133 qDebug() << "terminateProcess" << d->process.pid() << d->process.state();
134 if (d->process.state() == QProcess::NotRunning)
136 emitMessage(tr("%1: Stopping listener %2...").arg(d->device).arg(processId(d->process)));
137 // When listening, the process should terminate by itself after closing the connection
138 if (mode() == Listen && d->process.waitForFinished(TimeOutMS))
141 kill(d->process.pid(), SIGHUP); // Listens for SIGHUP
142 if (d->process.waitForFinished(TimeOutMS))
145 d->process.terminate();
146 if (d->process.waitForFinished(TimeOutMS))
152 bool BluetoothListener::start(const QString &device, QString *errorMessage)
154 if (d->process.state() != QProcess::NotRunning) {
155 *errorMessage = QLatin1String("Internal error: Still running.");
159 const QString binary = QLatin1String("rfcomm");
160 QStringList arguments;
161 arguments << QLatin1String("-r")
162 << (d->mode == Listen ? QLatin1String("listen") : QLatin1String("watch"))
163 << device << QString(QLatin1Char('1'));
165 qDebug() << binary << arguments;
166 emitMessage(tr("%1: Starting Bluetooth listener %2...").arg(device, binary));
168 d->process.start(binary, arguments);
169 if (!d->process.waitForStarted()) {
170 *errorMessage = tr("Unable to run '%1': %2").arg(binary, d->process.errorString());
173 d->pid = processId(d->process); // Forgets it after crash/termination
174 emitMessage(tr("%1: Bluetooth listener running (%2).").arg(device).arg(processId(d->process)));
178 void BluetoothListener::slotStdOutput()
180 emitMessage(QString::fromLocal8Bit(d->process.readAllStandardOutput()));
183 void BluetoothListener::emitMessage(const QString &m)
185 if (d->printConsoleMessages || debug)
186 qDebug("%s\n", qPrintable(m));
190 void BluetoothListener::slotStdError()
192 emitMessage(QString::fromLocal8Bit(d->process.readAllStandardError()));
195 void BluetoothListener::slotProcessFinished(int ex, QProcess::ExitStatus state)
198 case QProcess::NormalExit:
199 emitMessage(tr("%1: Process %2 terminated with exit code %3.")
200 .arg(d->device).arg(d->pid).arg(ex));
202 case QProcess::CrashExit:
203 emitMessage(tr("%1: Process %2 crashed.").arg(d->device).arg(d->pid));
209 void BluetoothListener::slotProcessError(QProcess::ProcessError error)
211 emitMessage(tr("%1: Process error %2: %3")
212 .arg(d->device).arg(error).arg(d->process.errorString()));