1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtPositioning module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qgeosatelliteinfosource_gypsy_p.h"
#ifdef Q_LOCATION_GYPSY_DEBUG
#include <QDebug>
#endif
#include <QFile>
QT_BEGIN_NAMESPACE
#define UPDATE_TIMEOUT_COLD_START 120000
// Callback function for 'satellites-changed' -signal
static void satellites_changed (GypsySatellite *satellite,
GPtrArray *satellites,
gpointer userdata)
{
#ifdef Q_LOCATION_GYPSY_DEBUG
qDebug() << "QGeoSatelliteInfoSourceGypsy Gypsy satellites-changed -signal received.";
#endif
((QGeoSatelliteInfoSourceGypsy *)userdata)->satellitesChanged(satellite, satellites);
}
SatelliteGypsyEngine::SatelliteGypsyEngine(QGeoSatelliteInfoSource *parent) :
m_owner(parent)
{
}
SatelliteGypsyEngine::~SatelliteGypsyEngine()
{
}
// Glib symbols
gulong SatelliteGypsyEngine::eng_g_signal_connect(gpointer instance,
const gchar *detailed_signal,
GCallback c_handler,
gpointer data)
{
return ::g_signal_connect(instance, detailed_signal, c_handler, data);
}
guint SatelliteGypsyEngine::eng_g_signal_handlers_disconnect_by_func (gpointer instance,
gpointer func,
gpointer data)
{
return ::g_signal_handlers_disconnect_by_func(instance, func, data);
}
void SatelliteGypsyEngine::eng_g_free(gpointer mem)
{
return ::g_free(mem);
}
// Gypsy symbols
GypsyControl *SatelliteGypsyEngine::eng_gypsy_control_get_default (void)
{
return ::gypsy_control_get_default();
}
char *SatelliteGypsyEngine::eng_gypsy_control_create (GypsyControl *control, const char *device_name, GError **error)
{
return ::gypsy_control_create(control, device_name, error);
}
GypsyDevice *SatelliteGypsyEngine::eng_gypsy_device_new (const char *object_path)
{
return ::gypsy_device_new(object_path);
}
GypsySatellite *SatelliteGypsyEngine::eng_gypsy_satellite_new (const char *object_path)
{
return ::gypsy_satellite_new (object_path);
}
gboolean SatelliteGypsyEngine::eng_gypsy_device_start (GypsyDevice *device, GError **error)
{
return ::gypsy_device_start(device, error);
}
gboolean SatelliteGypsyEngine::eng_gypsy_device_stop (GypsyDevice *device, GError **error)
{
// Unfortunately this cannot be done; calling this will stop the GPS device
// (basically makes gypsy-daemon unusable for anyone), regardless of applications
// using it (see bug http://bugs.meego.com/show_bug.cgi?id=11707).
Q_UNUSED(device);
Q_UNUSED(error);
return true;
//return ::gypsy_device_stop (device, error);
}
GypsyDeviceFixStatus SatelliteGypsyEngine::eng_gypsy_device_get_fix_status (GypsyDevice *device, GError **error)
{
return ::gypsy_device_get_fix_status (device, error);
}
GPtrArray *SatelliteGypsyEngine::eng_gypsy_satellite_get_satellites (GypsySatellite *satellite, GError **error)
{
return ::gypsy_satellite_get_satellites (satellite, error);
}
void SatelliteGypsyEngine::eng_gypsy_satellite_free_satellite_array (GPtrArray *satellites)
{
return ::gypsy_satellite_free_satellite_array(satellites);
}
// GConf symbols (mockability due to X11 requirement)
GConfClient *SatelliteGypsyEngine::eng_gconf_client_get_default(void)
{
return ::gconf_client_get_default();
}
gchar *SatelliteGypsyEngine::eng_gconf_client_get_string(GConfClient *client, const gchar *key, GError** err)
{
return ::gconf_client_get_string(client, key, err);
}
QGeoSatelliteInfoSourceGypsy::QGeoSatelliteInfoSourceGypsy(QObject *parent) : QGeoSatelliteInfoSource(parent),
m_engine(0), m_satellite(0), m_device(0), m_requestTimer(this), m_updatesOngoing(false), m_requestOngoing(false)
{
m_requestTimer.setSingleShot(true);
QObject::connect(&m_requestTimer, SIGNAL(timeout()), this, SLOT(requestUpdateTimeout()));
}
void QGeoSatelliteInfoSourceGypsy::createEngine()
{
delete m_engine;
m_engine = new SatelliteGypsyEngine(this);
}
QGeoSatelliteInfoSourceGypsy::~QGeoSatelliteInfoSourceGypsy()
{
GError *error = NULL;
if (m_device) {
m_engine->eng_gypsy_device_stop (m_device, &error);
g_object_unref(m_device);
}
if (m_satellite)
g_object_unref(m_satellite);
if (error)
g_error_free(error);
delete m_engine;
}
void QGeoSatelliteInfoSourceGypsy::satellitesChanged(GypsySatellite *satellite,
GPtrArray *satellites)
{
if (!satellite || !satellites)
return;
// We have satellite data and assume it is valid.
// If a single updateRequest was active, send signals right away.
// If a periodic timer was running (meaning that the client wishes
// to have updates at defined intervals), store the data for later sending.
QList<QGeoSatelliteInfo> lastSatellitesInView;
QList<QGeoSatelliteInfo> lastSatellitesInUse;
unsigned int i;
for (i = 0; i < satellites->len; i++) {
GypsySatelliteDetails *details = (GypsySatelliteDetails *)satellites->pdata[i];
QGeoSatelliteInfo info;
info.setAttribute(QGeoSatelliteInfo::Elevation, details->elevation);
info.setAttribute(QGeoSatelliteInfo::Azimuth, details->azimuth);
info.setSignalStrength(details->snr);
if (details->in_use)
lastSatellitesInUse.append(info);
lastSatellitesInView.append(info);
}
bool sendUpdates(false);
// If a single updateRequest() has been issued:
if (m_requestOngoing) {
sendUpdates = true;
m_requestTimer.stop();
m_requestOngoing = false;
// If there is no regular updates ongoing, disconnect now.
if (!m_updatesOngoing) {
m_engine->eng_g_signal_handlers_disconnect_by_func(G_OBJECT(m_satellite), (void *)satellites_changed, this);
}
}
// If regular updates are to be delivered as they come:
if (m_updatesOngoing)
sendUpdates = true;
if (sendUpdates) {
emit satellitesInUseUpdated(lastSatellitesInUse);
emit satellitesInViewUpdated(lastSatellitesInView);
}
}
int QGeoSatelliteInfoSourceGypsy::init()
{
GError *error = NULL;
char *path;
GConfClient *client;
gchar *device_name;
g_type_init ();
createEngine();
client = m_engine->eng_gconf_client_get_default();
if (!client) {
qWarning ("QGeoSatelliteInfoSourceGypsy client creation failed.");
return -1;
}
device_name = m_engine->eng_gconf_client_get_string(client, "/apps/geoclue/master/org.freedesktop.Geoclue.GPSDevice", NULL);
g_object_unref(client);
QString deviceName(QString::fromLatin1(device_name));
if (deviceName.isEmpty() ||
(deviceName.trimmed().at(0) == '/' && !QFile::exists(deviceName.trimmed()))) {
qWarning ("QGeoSatelliteInfoSourceGypsy Empty/nonexistent GPS device name detected.");
qWarning ("Use gconftool-2 to set it, e.g. on terminal: ");
qWarning ("gconftool-2 -t string -s /apps/geoclue/master/org.freedesktop.Geoclue.GPSDevice /dev/ttyUSB0");
m_engine->eng_g_free(device_name);
return -1;
}
GypsyControl *control = NULL;
control = m_engine->eng_gypsy_control_get_default();
if (!control) {
qWarning("QGeoSatelliteInfoSourceGypsy unable to create Gypsy control.");
m_engine->eng_g_free(device_name);
return -1;
}
// (path is the DBus path)
path = m_engine->eng_gypsy_control_create (control, device_name, &error);
m_engine->eng_g_free(device_name);
g_object_unref(control);
if (!path) {
qWarning ("QGeoSatelliteInfoSourceGypsy error creating client.");
if (error) {
qWarning ("error message: %s", error->message);
g_error_free (error);
}
return -1;
}
m_device = m_engine->eng_gypsy_device_new (path);
m_satellite = m_engine->eng_gypsy_satellite_new (path);
m_engine->eng_g_free(path);
if (!m_device || !m_satellite) {
qWarning ("QGeoSatelliteInfoSourceGypsy error creating satellite device.");
qWarning ("Is GPS device set correctly? If not, use gconftool-2 to set it, e.g.: ");
qWarning ("gconftool-2 -t string -s /apps/geoclue/master/org.freedesktop.Geoclue.GPSDevice /dev/ttyUSB0");
if (m_device)
g_object_unref(m_device);
if (m_satellite)
g_object_unref(m_satellite);
return -1;
}
m_engine->eng_gypsy_device_start (m_device, &error);
if (error) {
qWarning ("QGeoSatelliteInfoSourceGypsy error starting device: %s ",
error->message);
g_error_free(error);
g_object_unref(m_device);
g_object_unref(m_satellite);
return -1;
}
return 0;
}
int QGeoSatelliteInfoSourceGypsy::minimumUpdateInterval() const
{
return 1;
}
QGeoSatelliteInfoSource::Error QGeoSatelliteInfoSourceGypsy::error() const
{
return NoError;
}
void QGeoSatelliteInfoSourceGypsy::startUpdates()
{
if (m_updatesOngoing)
return;
// If there is a request timer ongoing, we've connected to the signal already
if (!m_requestTimer.isActive()) {
m_engine->eng_g_signal_connect (m_satellite, "satellites-changed",
G_CALLBACK (satellites_changed), this);
}
m_updatesOngoing = true;
}
void QGeoSatelliteInfoSourceGypsy::stopUpdates()
{
if (!m_updatesOngoing)
return;
m_updatesOngoing = false;
// Disconnect only if there is no single update request ongoing. Once single update request
// is completed and it notices that there is no active update ongoing, it will disconnect
// the signal.
if (!m_requestTimer.isActive())
m_engine->eng_g_signal_handlers_disconnect_by_func(G_OBJECT(m_satellite), (void *)satellites_changed, this);
}
void QGeoSatelliteInfoSourceGypsy::requestUpdate(int timeout)
{
if (m_requestOngoing)
return;
if (timeout < 0) {
emit requestTimeout();
return;
}
m_requestOngoing = true;
GError *error = 0;
// If GPS has a fix a already, request current data.
GypsyDeviceFixStatus fixStatus = m_engine->eng_gypsy_device_get_fix_status(m_device, &error);
if (!error && (fixStatus != GYPSY_DEVICE_FIX_STATUS_INVALID &&
fixStatus != GYPSY_DEVICE_FIX_STATUS_NONE)) {
#ifdef Q_LOCATION_GYPSY_DEBUG
qDebug() << "QGeoSatelliteInfoSourceGypsy fix available, requesting current satellite data";
#endif
GPtrArray *satelliteData = m_engine->eng_gypsy_satellite_get_satellites(m_satellite, &error);
if (!error) {
// The fix was available and we have satellite data to deliver right away.
satellitesChanged(m_satellite, satelliteData);
m_engine->eng_gypsy_satellite_free_satellite_array(satelliteData);
return;
}
}
// No fix is available. If updates are not ongoing already, start them.
m_requestTimer.setInterval(timeout == 0? UPDATE_TIMEOUT_COLD_START: timeout);
if (!m_updatesOngoing) {
m_engine->eng_g_signal_connect (m_satellite, "satellites-changed",
G_CALLBACK (satellites_changed), this);
}
m_requestTimer.start();
if (error) {
#ifdef Q_LOCATION_GYPSY_DEBUG
qDebug() << "QGeoSatelliteInfoSourceGypsy error asking fix status or satellite data: " << error->message;
#endif
g_error_free(error);
}
}
void QGeoSatelliteInfoSourceGypsy::requestUpdateTimeout()
{
#ifdef Q_LOCATION_GYPSY_DEBUG
qDebug("QGeoSatelliteInfoSourceGypsy request update timeout occurred.");
#endif
// If we end up here, there has not been valid satellite update.
// Emit timeout and disconnect from signal if regular updates are not
// ongoing (as we were listening just for one single requestUpdate).
if (!m_updatesOngoing) {
m_engine->eng_g_signal_handlers_disconnect_by_func(G_OBJECT(m_satellite), (void *)satellites_changed, this);
}
m_requestOngoing = false;
emit requestTimeout();
}
QT_END_NAMESPACE
|