source: trunk/tools/runonphone/trk/communicationstarter.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "communicationstarter.h"
43#include "bluetoothlistener.h"
44#include "trkdevice.h"
45
46#include <QtCore/QTimer>
47#include <QtCore/QEventLoop>
48
49namespace trk {
50
51// --------------- AbstractBluetoothStarter
52struct BaseCommunicationStarterPrivate {
53 explicit BaseCommunicationStarterPrivate(const BaseCommunicationStarter::TrkDevicePtr &d);
54
55 const BaseCommunicationStarter::TrkDevicePtr trkDevice;
56 BluetoothListener *listener;
57 QTimer *timer;
58 int intervalMS;
59 int attempts;
60 int n;
61 QString device;
62 QString errorString;
63 BaseCommunicationStarter::State state;
64};
65
66BaseCommunicationStarterPrivate::BaseCommunicationStarterPrivate(const BaseCommunicationStarter::TrkDevicePtr &d) :
67 trkDevice(d),
68 listener(0),
69 timer(0),
70 intervalMS(1000),
71 attempts(-1),
72 n(0),
73 device(QLatin1String("/dev/rfcomm0")),
74 state(BaseCommunicationStarter::TimedOut)
75{
76}
77
78BaseCommunicationStarter::BaseCommunicationStarter(const TrkDevicePtr &trkDevice, QObject *parent) :
79 QObject(parent),
80 d(new BaseCommunicationStarterPrivate(trkDevice))
81{
82}
83
84BaseCommunicationStarter::~BaseCommunicationStarter()
85{
86 stopTimer();
87 delete d;
88}
89
90void BaseCommunicationStarter::stopTimer()
91{
92 if (d->timer && d->timer->isActive())
93 d->timer->stop();
94}
95
96bool BaseCommunicationStarter::initializeStartupResources(QString *errorMessage)
97{
98 errorMessage->clear();
99 return true;
100}
101
102BaseCommunicationStarter::StartResult BaseCommunicationStarter::start()
103{
104 if (state() == Running) {
105 d->errorString = QLatin1String("Internal error, attempt to re-start BaseCommunicationStarter.\n");
106 return StartError;
107 }
108 // Before we instantiate timers, and such, try to open the device,
109 // which should succeed if another listener is already running in
110 // 'Watch' mode
111 if (d->trkDevice->open(d->device , &(d->errorString)))
112 return ConnectionSucceeded;
113 // Pull up resources for next attempt
114 d->n = 0;
115 if (!initializeStartupResources(&(d->errorString)))
116 return StartError;
117 // Start timer
118 if (!d->timer) {
119 d->timer = new QTimer;
120 connect(d->timer, SIGNAL(timeout()), this, SLOT(slotTimer()));
121 }
122 d->timer->setInterval(d->intervalMS);
123 d->timer->setSingleShot(false);
124 d->timer->start();
125 d->state = Running;
126 return Started;
127}
128
129BaseCommunicationStarter::State BaseCommunicationStarter::state() const
130{
131 return d->state;
132}
133
134int BaseCommunicationStarter::intervalMS() const
135{
136 return d->intervalMS;
137}
138
139void BaseCommunicationStarter::setIntervalMS(int i)
140{
141 d->intervalMS = i;
142 if (d->timer)
143 d->timer->setInterval(i);
144}
145
146int BaseCommunicationStarter::attempts() const
147{
148 return d->attempts;
149}
150
151void BaseCommunicationStarter::setAttempts(int a)
152{
153 d->attempts = a;
154}
155
156QString BaseCommunicationStarter::device() const
157{
158 return d->device;
159}
160
161void BaseCommunicationStarter::setDevice(const QString &dv)
162{
163 d->device = dv;
164}
165
166QString BaseCommunicationStarter::errorString() const
167{
168 return d->errorString;
169}
170
171void BaseCommunicationStarter::slotTimer()
172{
173 ++d->n;
174 // Check for timeout
175 if (d->attempts >= 0 && d->n >= d->attempts) {
176 stopTimer();
177 d->errorString = tr("%1: timed out after %n attempts using an interval of %2ms.", 0, d->n)
178 .arg(d->device).arg(d->intervalMS);
179 d->state = TimedOut;
180 emit timeout();
181 } else {
182 // Attempt n to connect?
183 if (d->trkDevice->open(d->device , &(d->errorString))) {
184 stopTimer();
185 const QString msg = tr("%1: Connection attempt %2 succeeded.").arg(d->device).arg(d->n);
186 emit message(msg);
187 d->state = Connected;
188 emit connected();
189 } else {
190 const QString msg = tr("%1: Connection attempt %2 failed: %3 (retrying)...")
191 .arg(d->device).arg(d->n).arg(d->errorString);
192 emit message(msg);
193 }
194 }
195}
196
197// --------------- AbstractBluetoothStarter
198
199AbstractBluetoothStarter::AbstractBluetoothStarter(const TrkDevicePtr &trkDevice, QObject *parent) :
200 BaseCommunicationStarter(trkDevice, parent)
201{
202}
203
204bool AbstractBluetoothStarter::initializeStartupResources(QString *errorMessage)
205{
206 // Create the listener and forward messages to it.
207 BluetoothListener *listener = createListener();
208 connect(this, SIGNAL(message(QString)), listener, SLOT(emitMessage(QString)));
209 return listener->start(device(), errorMessage);
210}
211
212// -------- ConsoleBluetoothStarter
213ConsoleBluetoothStarter::ConsoleBluetoothStarter(const TrkDevicePtr &trkDevice,
214 QObject *listenerParent,
215 QObject *parent) :
216AbstractBluetoothStarter(trkDevice, parent),
217m_listenerParent(listenerParent)
218{
219}
220
221BluetoothListener *ConsoleBluetoothStarter::createListener()
222{
223 BluetoothListener *rc = new BluetoothListener(m_listenerParent);
224 rc->setMode(BluetoothListener::Listen);
225 rc->setPrintConsoleMessages(true);
226 return rc;
227}
228
229bool ConsoleBluetoothStarter::startBluetooth(const TrkDevicePtr &trkDevice,
230 QObject *listenerParent,
231 const QString &device,
232 int attempts,
233 QString *errorMessage)
234{
235 // Set up a console starter to print to stdout.
236 ConsoleBluetoothStarter starter(trkDevice, listenerParent);
237 starter.setDevice(device);
238 starter.setAttempts(attempts);
239 switch (starter.start()) {
240 case Started:
241 break;
242 case ConnectionSucceeded:
243 return true;
244 case StartError:
245 *errorMessage = starter.errorString();
246 return false;
247 }
248 // Run the starter with an event loop. @ToDo: Implement
249 // some asynchronous keypress read to cancel.
250 QEventLoop eventLoop;
251 connect(&starter, SIGNAL(connected()), &eventLoop, SLOT(quit()));
252 connect(&starter, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
253 eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
254 if (starter.state() != AbstractBluetoothStarter::Connected) {
255 *errorMessage = starter.errorString();
256 return false;
257 }
258 return true;
259}
260} // namespace trk
Note: See TracBrowser for help on using the repository browser.