summaryrefslogtreecommitdiff
path: root/navit/vehicle/geoclue/vehicle_geoclue.c
blob: 6384a1cc3f8f98c7358bd2136b928f852ff7770b (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
/*
 * Navit, a modular navigation system.
 * Copyright 2020 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.
 *
 */

#include <stdlib.h>
#include <locale.h>
#include <glib/gi18n.h>
#include <geoclue.h>
#include "debug.h"
#include "callback.h"
#include "coord.h"
#include "item.h"
#include "vehicle.h"
#include "plugin.h"

/**
 * @defgroup vehicle-gypsy Vehicle GeoClue
 * @ingroup vehicle-plugins
 * @brief The Vehicle to gain position data from GeoClue.
 * @Author jkoan
 * @date 2020
 *
 * @{
 */


struct vehicle_priv {
    GClueLocation *location;
    char *bla;
    struct callback_list *cbl;
    double speed;
    double direction;
    double height;
    struct coord_geo geo;
    int accuracy;
    char* time_str;
    GClueSimple *simple;
};
GClueClient *client = NULL;

/**
 * @brief Free the geoclue_vehicle
 *
 * @param priv
 * @returns nothing
 */
static void vehicle_geoclue_destroy(struct vehicle_priv *priv) {
    g_clear_object(&client);
    g_clear_object(&priv->simple);
}

static void print_location(GClueSimple *simple,
                           GParamSpec *pspec,
                           gpointer    user_data) {
    GClueLocation *location;
    gdouble altitude;
    gdouble speed;
    gdouble direction;
    GVariant *timestamp;
    struct vehicle_priv *priv = user_data;

    location = gclue_simple_get_location(simple);

    priv->geo.lat = gclue_location_get_latitude(location);
    priv->geo.lng = gclue_location_get_longitude(location);
    priv->accuracy = gclue_location_get_accuracy(location);
    altitude = gclue_location_get_altitude(location);
    if(altitude != -G_MAXDOUBLE) {
        priv->height=altitude;
    }
    speed = gclue_location_get_speed(location);
    if(speed != -G_MAXDOUBLE) {
        priv->speed=speed;
    }
    direction = gclue_location_get_heading(location);
    if(direction != -G_MAXDOUBLE) {
        priv->direction=direction;
    }

    timestamp= gclue_location_get_timestamp(location);
    if(timestamp) {
        GDateTime *date_time;
        glong second_since_epoch;


        g_variant_get (timestamp, "(tt)", &second_since_epoch, NULL);

        date_time = g_date_time_new_from_unix_local (second_since_epoch);

        priv->time_str = g_date_time_format_iso8601(g_date_time_to_utc(date_time));

    }
    callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
}

static void on_client_active_notify(GClueClient *client,
                                    GParamSpec *pspec,
                                    gpointer    user_data) {
    if(gclue_client_get_active(client))
        return;

    g_print("Geolocation disabled. Quitting..\n");
    //vehicle_geoclue_destroy(&user_data);
}

static void on_simple_ready(GObject *source_object,
                            GAsyncResult *res,
                            gpointer user_data) {
    GError *error = NULL;

    struct vehicle_priv* priv = user_data;

    priv->simple = gclue_simple_new_finish(res, &error);
    if(error != NULL) {
        dbg(lvl_error,"Failed to connect to GeoClue2 service: %s", error->message);

        exit(-1);
    }
    client = gclue_simple_get_client(priv->simple);
    if(client) {
        g_object_ref(client);
        dbg(lvl_debug,"Client object: %s\n",
            g_dbus_proxy_get_object_path(G_DBUS_PROXY(client)));

        g_signal_connect(client,
                         "notify::active",
                         G_CALLBACK(on_client_active_notify),
                         NULL);
    }
    print_location(priv->simple,NULL,priv);

    g_signal_connect(priv->simple,
                     "notify::location",
                     G_CALLBACK(print_location),
                     priv);
}

/**
 * @brief Provide the outside with information
 *
 * @param priv
 * @param type TODO: What can this be?
 * @param attr
 * @returns true/false
 */
static int vehicle_geoclue_position_attr_get(struct vehicle_priv *priv,
        enum attr_type type, struct attr *attr) {
    switch(type) {
    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_qual:
        attr->u.num = priv->accuracy;
        break;
    case attr_position_coord_geo:
        attr->u.coord_geo = &priv->geo;
        break;
    case attr_position_time_iso8601:
        if(!priv->time_str) {
            return 0;
        }
        attr->u.str=priv->time_str;
        break;
    case attr_active:
        return 1;
    default:
        return 0;
    }
    attr->type = type;
    return 1;
}

struct vehicle_methods vehicle_geoclue_methods = {
    .destroy           = vehicle_geoclue_destroy,
    .position_attr_get = vehicle_geoclue_position_attr_get,
};

/**
 * @brief Create geoclue_vehicle
 *
 * @param meth[out] Methodes supported by geoclue Plugin
 * @param cbl[in] Callback List reference
 * @param attrs Configuration attributes
 * @returns vehicle_priv The newly created Vehicle priv
 */
static struct vehicle_priv *vehicle_geoclue_new(struct vehicle_methods *meth,
        struct callback_list *cbl,
        struct attr **attrs) {
    struct vehicle_priv *ret;
    dbg(lvl_debug, "enter");

    *meth = vehicle_geoclue_methods;

    ret = (struct vehicle_priv*)g_new0(struct vehicle_priv, 1);
    ret->cbl = cbl;
    gclue_simple_new("navit",
                     GCLUE_ACCURACY_LEVEL_EXACT,
                     NULL,
                     on_simple_ready,
                     ret
                    );
    return ret;
};

/**
 * @brief register vehicle_geoclue
 *
 * @returns nothing
 */
void plugin_init(void) {
    dbg(lvl_error, "enter");
    plugin_register_category_vehicle("geoclue", vehicle_geoclue_new);
};