[767] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[767] | 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 |
|
---|
| 49 | namespace trk {
|
---|
| 50 |
|
---|
| 51 | // --------------- AbstractBluetoothStarter
|
---|
| 52 | struct 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 errorString;
|
---|
| 62 | BaseCommunicationStarter::State state;
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | BaseCommunicationStarterPrivate::BaseCommunicationStarterPrivate(const BaseCommunicationStarter::TrkDevicePtr &d) :
|
---|
| 66 | trkDevice(d),
|
---|
| 67 | listener(0),
|
---|
| 68 | timer(0),
|
---|
| 69 | intervalMS(1000),
|
---|
| 70 | attempts(-1),
|
---|
| 71 | n(0),
|
---|
| 72 | state(BaseCommunicationStarter::TimedOut)
|
---|
| 73 | {
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | BaseCommunicationStarter::BaseCommunicationStarter(const TrkDevicePtr &trkDevice, QObject *parent) :
|
---|
| 77 | QObject(parent),
|
---|
| 78 | d(new BaseCommunicationStarterPrivate(trkDevice))
|
---|
| 79 | {
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | BaseCommunicationStarter::~BaseCommunicationStarter()
|
---|
| 83 | {
|
---|
| 84 | stopTimer();
|
---|
| 85 | delete d;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void BaseCommunicationStarter::stopTimer()
|
---|
| 89 | {
|
---|
| 90 | if (d->timer && d->timer->isActive())
|
---|
| 91 | d->timer->stop();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | bool BaseCommunicationStarter::initializeStartupResources(QString *errorMessage)
|
---|
| 95 | {
|
---|
| 96 | errorMessage->clear();
|
---|
| 97 | return true;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | BaseCommunicationStarter::StartResult BaseCommunicationStarter::start()
|
---|
| 101 | {
|
---|
| 102 | if (state() == Running) {
|
---|
| 103 | d->errorString = QLatin1String("Internal error, attempt to re-start BaseCommunicationStarter.\n");
|
---|
| 104 | return StartError;
|
---|
| 105 | }
|
---|
| 106 | // Before we instantiate timers, and such, try to open the device,
|
---|
| 107 | // which should succeed if another listener is already running in
|
---|
| 108 | // 'Watch' mode
|
---|
| 109 | if (d->trkDevice->open(&(d->errorString)))
|
---|
| 110 | return ConnectionSucceeded;
|
---|
| 111 | // Pull up resources for next attempt
|
---|
| 112 | d->n = 0;
|
---|
| 113 | if (!initializeStartupResources(&(d->errorString)))
|
---|
| 114 | return StartError;
|
---|
| 115 | // Start timer
|
---|
| 116 | if (!d->timer) {
|
---|
| 117 | d->timer = new QTimer;
|
---|
| 118 | connect(d->timer, SIGNAL(timeout()), this, SLOT(slotTimer()));
|
---|
| 119 | }
|
---|
| 120 | d->timer->setInterval(d->intervalMS);
|
---|
| 121 | d->timer->setSingleShot(false);
|
---|
| 122 | d->timer->start();
|
---|
| 123 | d->state = Running;
|
---|
| 124 | return Started;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | BaseCommunicationStarter::State BaseCommunicationStarter::state() const
|
---|
| 128 | {
|
---|
| 129 | return d->state;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | int BaseCommunicationStarter::intervalMS() const
|
---|
| 133 | {
|
---|
| 134 | return d->intervalMS;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | void BaseCommunicationStarter::setIntervalMS(int i)
|
---|
| 138 | {
|
---|
| 139 | d->intervalMS = i;
|
---|
| 140 | if (d->timer)
|
---|
| 141 | d->timer->setInterval(i);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | int BaseCommunicationStarter::attempts() const
|
---|
| 145 | {
|
---|
| 146 | return d->attempts;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | void BaseCommunicationStarter::setAttempts(int a)
|
---|
| 150 | {
|
---|
| 151 | d->attempts = a;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | QString BaseCommunicationStarter::device() const
|
---|
| 155 | {
|
---|
| 156 | return d->trkDevice->port();
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | QString BaseCommunicationStarter::errorString() const
|
---|
| 160 | {
|
---|
| 161 | return d->errorString;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | void BaseCommunicationStarter::slotTimer()
|
---|
| 165 | {
|
---|
| 166 | ++d->n;
|
---|
| 167 | // Check for timeout
|
---|
| 168 | if (d->attempts >= 0 && d->n >= d->attempts) {
|
---|
| 169 | stopTimer();
|
---|
| 170 | d->errorString = tr("%1: timed out after %n attempts using an interval of %2ms.", 0, d->n)
|
---|
| 171 | .arg(d->trkDevice->port()).arg(d->intervalMS);
|
---|
| 172 | d->state = TimedOut;
|
---|
| 173 | emit timeout();
|
---|
| 174 | } else {
|
---|
| 175 | // Attempt n to connect?
|
---|
| 176 | if (d->trkDevice->open(&(d->errorString))) {
|
---|
| 177 | stopTimer();
|
---|
| 178 | const QString msg = tr("%1: Connection attempt %2 succeeded.").arg(d->trkDevice->port()).arg(d->n);
|
---|
| 179 | emit message(msg);
|
---|
| 180 | d->state = Connected;
|
---|
| 181 | emit connected();
|
---|
| 182 | } else {
|
---|
| 183 | const QString msg = tr("%1: Connection attempt %2 failed: %3 (retrying)...")
|
---|
| 184 | .arg(d->trkDevice->port()).arg(d->n).arg(d->errorString);
|
---|
| 185 | emit message(msg);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | // --------------- AbstractBluetoothStarter
|
---|
| 191 |
|
---|
| 192 | AbstractBluetoothStarter::AbstractBluetoothStarter(const TrkDevicePtr &trkDevice, QObject *parent) :
|
---|
| 193 | BaseCommunicationStarter(trkDevice, parent)
|
---|
| 194 | {
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | bool AbstractBluetoothStarter::initializeStartupResources(QString *errorMessage)
|
---|
| 198 | {
|
---|
| 199 | // Create the listener and forward messages to it.
|
---|
| 200 | BluetoothListener *listener = createListener();
|
---|
| 201 | connect(this, SIGNAL(message(QString)), listener, SLOT(emitMessage(QString)));
|
---|
| 202 | return listener->start(device(), errorMessage);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | // -------- ConsoleBluetoothStarter
|
---|
| 206 | ConsoleBluetoothStarter::ConsoleBluetoothStarter(const TrkDevicePtr &trkDevice,
|
---|
| 207 | QObject *listenerParent,
|
---|
| 208 | QObject *parent) :
|
---|
| 209 | AbstractBluetoothStarter(trkDevice, parent),
|
---|
| 210 | m_listenerParent(listenerParent)
|
---|
| 211 | {
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | BluetoothListener *ConsoleBluetoothStarter::createListener()
|
---|
| 215 | {
|
---|
| 216 | BluetoothListener *rc = new BluetoothListener(m_listenerParent);
|
---|
| 217 | rc->setMode(BluetoothListener::Listen);
|
---|
| 218 | rc->setPrintConsoleMessages(true);
|
---|
| 219 | return rc;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | bool ConsoleBluetoothStarter::startBluetooth(const TrkDevicePtr &trkDevice,
|
---|
| 223 | QObject *listenerParent,
|
---|
| 224 | int attempts,
|
---|
| 225 | QString *errorMessage)
|
---|
| 226 | {
|
---|
| 227 | // Set up a console starter to print to stdout.
|
---|
| 228 | ConsoleBluetoothStarter starter(trkDevice, listenerParent);
|
---|
| 229 | starter.setAttempts(attempts);
|
---|
| 230 | switch (starter.start()) {
|
---|
| 231 | case Started:
|
---|
| 232 | break;
|
---|
| 233 | case ConnectionSucceeded:
|
---|
| 234 | return true;
|
---|
| 235 | case StartError:
|
---|
| 236 | *errorMessage = starter.errorString();
|
---|
| 237 | return false;
|
---|
| 238 | }
|
---|
| 239 | // Run the starter with an event loop. @ToDo: Implement
|
---|
| 240 | // some asynchronous keypress read to cancel.
|
---|
| 241 | QEventLoop eventLoop;
|
---|
| 242 | connect(&starter, SIGNAL(connected()), &eventLoop, SLOT(quit()));
|
---|
| 243 | connect(&starter, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
|
---|
| 244 | eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
|
---|
| 245 | if (starter.state() != AbstractBluetoothStarter::Connected) {
|
---|
| 246 | *errorMessage = starter.errorString();
|
---|
| 247 | return false;
|
---|
| 248 | }
|
---|
| 249 | return true;
|
---|
| 250 | }
|
---|
| 251 | } // namespace trk
|
---|