|
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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #ifndef COMMUNICATIONSTARTER_H |
|
43 #define COMMUNICATIONSTARTER_H |
|
44 |
|
45 #include <QtCore/QSharedPointer> |
|
46 #include <QtCore/QObject> |
|
47 |
|
48 namespace trk { |
|
49 class TrkDevice; |
|
50 class BluetoothListener; |
|
51 struct BaseCommunicationStarterPrivate; |
|
52 |
|
53 /* BaseCommunicationStarter: A QObject that repeatedly tries to open a |
|
54 * trk device until a connection succeeds or a timeout occurs (emitting |
|
55 * signals), allowing to do something else in the foreground (local event loop |
|
56 * [say QMessageBox] or some asynchronous operation). If the initial |
|
57 * connection attempt in start() fails, the |
|
58 * virtual initializeStartupResources() is called to initialize resources |
|
59 * required to pull up the communication (namely Bluetooth listeners). |
|
60 * The base class can be used as is to prompt the user to launch App TRK for a |
|
61 * serial communication as this requires no further resource setup. */ |
|
62 |
|
63 class BaseCommunicationStarter : public QObject { |
|
64 Q_OBJECT |
|
65 Q_DISABLE_COPY(BaseCommunicationStarter) |
|
66 public: |
|
67 typedef QSharedPointer<TrkDevice> TrkDevicePtr; |
|
68 |
|
69 enum State { Running, Connected, TimedOut }; |
|
70 |
|
71 explicit BaseCommunicationStarter(const TrkDevicePtr& trkDevice, QObject *parent = 0); |
|
72 virtual ~BaseCommunicationStarter(); |
|
73 |
|
74 int intervalMS() const; |
|
75 void setIntervalMS(int i); |
|
76 |
|
77 int attempts() const; |
|
78 void setAttempts(int a); |
|
79 |
|
80 QString device() const; |
|
81 void setDevice(const QString &); |
|
82 |
|
83 State state() const; |
|
84 QString errorString() const; |
|
85 |
|
86 enum StartResult { |
|
87 Started, // Starter is now running. |
|
88 ConnectionSucceeded, /* Initial connection attempt succeeded, |
|
89 * no need to keep running. */ |
|
90 StartError // Error occurred during start. |
|
91 }; |
|
92 |
|
93 StartResult start(); |
|
94 |
|
95 signals: |
|
96 void connected(); |
|
97 void timeout(); |
|
98 void message(const QString &); |
|
99 |
|
100 private slots: |
|
101 void slotTimer(); |
|
102 |
|
103 protected: |
|
104 virtual bool initializeStartupResources(QString *errorMessage); |
|
105 |
|
106 private: |
|
107 inline void stopTimer(); |
|
108 |
|
109 BaseCommunicationStarterPrivate *d; |
|
110 }; |
|
111 |
|
112 /* AbstractBluetoothStarter: Repeatedly tries to open a trk Bluetooth |
|
113 * device. Note that in case a Listener is already running mode, the |
|
114 * connection will succeed immediately. |
|
115 * initializeStartupResources() is implemented to fire up the listener. |
|
116 * Introduces a new virtual createListener() that derived classes must |
|
117 * implement as a factory function that creates and sets up the |
|
118 * listener (mode, message connection, etc). */ |
|
119 |
|
120 class AbstractBluetoothStarter : public BaseCommunicationStarter { |
|
121 Q_OBJECT |
|
122 Q_DISABLE_COPY(AbstractBluetoothStarter) |
|
123 public: |
|
124 |
|
125 protected: |
|
126 explicit AbstractBluetoothStarter(const TrkDevicePtr& trkDevice, QObject *parent = 0); |
|
127 |
|
128 // Implemented to fire up the listener. |
|
129 virtual bool initializeStartupResources(QString *errorMessage); |
|
130 // New virtual: Overwrite to create and parametrize the listener. |
|
131 virtual BluetoothListener *createListener() = 0; |
|
132 }; |
|
133 |
|
134 /* ConsoleBluetoothStarter: Convenience class for console processes. Creates a |
|
135 * listener in "Listen" mode with the messages redirected to standard output. */ |
|
136 |
|
137 class ConsoleBluetoothStarter : public AbstractBluetoothStarter { |
|
138 Q_OBJECT |
|
139 Q_DISABLE_COPY(ConsoleBluetoothStarter) |
|
140 public: |
|
141 static bool startBluetooth(const TrkDevicePtr& trkDevice, |
|
142 QObject *listenerParent, |
|
143 const QString &device, |
|
144 int attempts, |
|
145 QString *errorMessage); |
|
146 |
|
147 protected: |
|
148 virtual BluetoothListener *createListener(); |
|
149 |
|
150 private: |
|
151 explicit ConsoleBluetoothStarter(const TrkDevicePtr& trkDevice, |
|
152 QObject *listenerParent, |
|
153 QObject *parent = 0); |
|
154 |
|
155 QObject *m_listenerParent; |
|
156 }; |
|
157 |
|
158 } // namespace trk |
|
159 |
|
160 #endif // COMMUNICATIONSTARTER_H |