summaryrefslogtreecommitdiff
path: root/navit/vehicle/qt5/vehicle_qt5.cpp
blob: ac475bd610e692f19651ae37a05d389f47655496 (plain)
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
/*
 *
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2017 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * @Author Tim Niemeyer <reddog@mastersword.de>
 * @date 2008-2009
 */
// style with: clang-format -style=WebKit -i *

extern "C" {
#include "config.h"
#include "item.h" /* needs to be first as attr.h depends on it */

#include "attr.h"
#include "coord.h"
#include "debug.h"
#include "plugin.h"
#include "vehicle.h"
}
#include <glib.h>
#include <math.h>
#include <string.h>
#include <time.h>

#include "vehicle_qt5.h"
// #include "vehicle_qt5.moc"
#include <QDateTime>

/**
 * @defgroup vehicle-qt5 Vehicle QT5
 * @ingroup vehicle-plugins
 * @brief The Vehicle to gain position data from qt5
 *
 * @{
 */

QNavitGeoReceiver::QNavitGeoReceiver(QObject* parent, struct vehicle_priv* c)
    : QObject(parent) {
    priv = c;
    if (priv->source != NULL) {
        connect(priv->source, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
    }
    if (priv->satellites != NULL) {
        connect(priv->satellites, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo>&)), this,
                SLOT(satellitesInUseUpdated(const QList<QGeoSatelliteInfo>&)));
        connect(priv->satellites, SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo>&)), this,
                SLOT(satellitesInViewUpdated(const QList<QGeoSatelliteInfo>&)));
    }
}
void QNavitGeoReceiver::satellitesInUseUpdated(const QList<QGeoSatelliteInfo>& sats) {
    dbg(lvl_debug, "Sats in use: %d", sats.count());
    priv->sats_used = sats.count();
    callback_list_call_attr_0(priv->cbl, attr_position_sats_used);
}

void QNavitGeoReceiver::satellitesInViewUpdated(const QList<QGeoSatelliteInfo>& sats) {
    dbg(lvl_debug, "Sats in view: %d", sats.count());
    priv->sats = sats.count();
    callback_list_call_attr_0(priv->cbl, attr_position_qual);
}

void QNavitGeoReceiver::positionUpdated(const QGeoPositionInfo& info) {
    /* ignore stale view */
    if (info.coordinate().isValid()) {
        if (info.timestamp().toUTC().secsTo(QDateTime::currentDateTimeUtc()) > 20) {
            dbg(lvl_debug, "Ignoring old FIX");
            return;
        }
    }

    if (info.hasAttribute(QGeoPositionInfo::HorizontalAccuracy)) {
        dbg(lvl_debug, "Horizontal acc (%f)", info.attribute(QGeoPositionInfo::HorizontalAccuracy));
        priv->radius = info.attribute(QGeoPositionInfo::HorizontalAccuracy);
        callback_list_call_attr_0(priv->cbl, attr_position_radius);
    }
    if (info.hasAttribute(QGeoPositionInfo::GroundSpeed)) {
        dbg(lvl_debug, "Got ground speed (%f)", info.attribute(QGeoPositionInfo::GroundSpeed));
        priv->speed = info.attribute(QGeoPositionInfo::GroundSpeed) * 3.6;
        callback_list_call_attr_0(priv->cbl, attr_position_speed);
    }
    if (info.hasAttribute(QGeoPositionInfo::Direction)) {
        dbg(lvl_debug, "Direction (%f)", info.attribute(QGeoPositionInfo::Direction));
        priv->direction = info.attribute(QGeoPositionInfo::Direction);
        callback_list_call_attr_0(priv->cbl, attr_position_direction);
    }

    switch (info.coordinate().type()) {
    case QGeoCoordinate::Coordinate3D:
        priv->fix_type = 2;
        break;
    case QGeoCoordinate::Coordinate2D:
        priv->fix_type = 1;
        break;
    case QGeoCoordinate::InvalidCoordinate:
        priv->fix_type = 0;
        break;
    }

    if (info.coordinate().isValid()) {
        dbg(lvl_debug, "Got valid coordinate (lat %f, lon %f)", info.coordinate().latitude(), info.coordinate().longitude());
        priv->geo.lat = info.coordinate().latitude();
        priv->geo.lng = info.coordinate().longitude();
        if (info.coordinate().type() == QGeoCoordinate::Coordinate3D) {
            dbg(lvl_debug, "Got valid altitude (alt %f)", info.coordinate().altitude());
            priv->height = info.coordinate().altitude();
        }
        // dbg(lvl_debug, "Time %s", info.timestamp().toUTC().toString().toLatin1().data());
        priv->fix_time = info.timestamp().toUTC().toTime_t();
        callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
        if (priv->have_coords != attr_position_valid_valid) {
            priv->have_coords = attr_position_valid_valid;
            callback_list_call_attr_0(priv->cbl, attr_position_valid);
        }
    } else {
        dbg(lvl_debug, "Got invalid coordinate");
        callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
        if (priv->have_coords != attr_position_valid_invalid) {
            priv->have_coords = attr_position_valid_invalid;
            callback_list_call_attr_0(priv->cbl, attr_position_valid);
        }
    }
}

/**
 * @brief Free the null_vehicle
 *
 * @param priv
 * @returns nothing
 */
static void vehicle_qt5_destroy(struct vehicle_priv* priv) {
    dbg(lvl_debug, "enter");
    if (priv->receiver != NULL)
        delete priv->receiver;
    if (priv->source != NULL)
        delete priv->source;
    g_free(priv);
}

/**
 * @brief Provide the outside with information
 *
 * @param priv
 * @param type TODO: What can this be?
 * @param attr
 * @returns true/false
 */
static int vehicle_qt5_position_attr_get(struct vehicle_priv* priv,
        enum attr_type type, struct attr* attr) {
    struct attr* active = NULL;
    dbg(lvl_debug, "enter %s", attr_to_name(type));
    switch (type) {
    case attr_position_valid:
        attr->u.num = priv->have_coords;
        break;
    case attr_position_fix_type:
        attr->u.num = priv->fix_type;
        break;
    case attr_position_height:
        attr->u.numd = &priv->height;
        break;
    case attr_position_speed:
        attr->u.numd = &priv->speed;
        break;
    case attr_position_direction:
        attr->u.numd = &priv->direction;
        break;
    case attr_position_radius:
        attr->u.numd = &priv->radius;
        break;
    case attr_position_qual:
        attr->u.num = priv->sats;
        break;
    case attr_position_sats_used:
        attr->u.num = priv->sats_used;
        break;
    case attr_position_coord_geo:
        attr->u.coord_geo = &priv->geo;
        if (priv->have_coords != attr_position_valid_valid)
            return 0;
        break;
    case attr_position_time_iso8601:
        if (priv->fix_time) {
            struct tm tm;
            if (gmtime_r(&priv->fix_time, &tm)) {
                strftime(priv->fixiso8601, sizeof(priv->fixiso8601),
                         "%Y-%m-%dT%TZ", &tm);
                attr->u.str = priv->fixiso8601;
            } else {
                priv->fix_time = 0;
                return 0;
            }
            // dbg(lvl_debug,"Fix Time: %s", priv->fixiso8601);
        } else {
            // dbg(lvl_debug,"Fix Time: 0");
            return 0;
        }
        break;

    case attr_active:
        active = attr_search(priv->attrs, attr_active);
        if (active != NULL) {
            attr->u.num = active->u.num;
            return 1;
        } else
            return 0;
        break;

    default:
        return 0;
    }
    dbg(lvl_debug, "ok");
    attr->type = type;
    return 1;
}

static int vehicle_qt5_set_attr(struct vehicle_priv* priv, struct attr* attr) {
    switch (attr->type) {
    case attr_position_speed:
        priv->speed = *attr->u.numd;
        break;
    case attr_position_direction:
        priv->direction = *attr->u.numd;
        break;
    case attr_position_coord_geo:
        priv->geo = *attr->u.coord_geo;
        if (priv->have_coords != attr_position_valid_valid) {
            priv->have_coords = attr_position_valid_valid;
            callback_list_call_attr_0(priv->cbl, attr_position_valid);
        }
        break;
    default:
        break;
    }
    callback_list_call_attr_0(priv->cbl, attr->type);
    return 1;
}

struct vehicle_methods vehicle_null_methods = {
    vehicle_qt5_destroy,
    vehicle_qt5_position_attr_get,
    vehicle_qt5_set_attr,
};

/**
 * @brief Create null_vehicle
 *
 * @param meth
 * @param cbl
 * @param attrs
 * @returns vehicle_priv
 */
static struct vehicle_priv* vehicle_qt5_new_qt5(struct vehicle_methods* meth,
        struct callback_list* cbl,
        struct attr** attrs) {
    struct vehicle_priv* ret;

    dbg(lvl_debug, "enter");
    ret = g_new0(struct vehicle_priv, 1);
    ret->cbl = cbl;
    *meth = vehicle_null_methods;
    ret->attrs = attrs;
    ret->source = QGeoPositionInfoSource::createDefaultSource(NULL);
    ret->satellites = QGeoSatelliteInfoSource::createDefaultSource(NULL);
    if (ret->source == NULL) {
        dbg(lvl_error, "Got NO QGeoPositionInfoSource");
    } else {
        dbg(lvl_debug, "Using %s", ret->source->sourceName().toLatin1().data());
        ret->receiver = new QNavitGeoReceiver(NULL, ret);
        if (ret->satellites != NULL) {
            ret->satellites->setUpdateInterval(1000);
            ret->satellites->startUpdates();
        }
        ret->source->setUpdateInterval(500);
        ret->source->startUpdates();
    }
    dbg(lvl_debug, "return");
    return ret;
}

/**
 * @brief register vehicle_null
 *
 * @returns nothing
 */
void plugin_init(void) {
    dbg(lvl_debug, "enter");
    plugin_register_category_vehicle("qt5", vehicle_qt5_new_qt5);
}