"Fossies" - the Fresh Open Source Software Archive

Member "navit-0.5.6/navit/vehicle/geoclue/vehicle_geoclue.c" (6 Mar 2021, 6416 Bytes) of package /linux/privat/navit-0.5.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "vehicle_geoclue.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Navit, a modular navigation system.
    3  * Copyright 2020 Navit Team
    4  *
    5  * This program is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU General Public License
    7  * version 2 as published by the Free Software Foundation.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the
   16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   17  * Boston, MA  02110-1301, USA.
   18  *
   19  */
   20 
   21 #include <stdlib.h>
   22 #include <locale.h>
   23 #include <glib/gi18n.h>
   24 #include <geoclue.h>
   25 #include "debug.h"
   26 #include "callback.h"
   27 #include "coord.h"
   28 #include "item.h"
   29 #include "vehicle.h"
   30 #include "plugin.h"
   31 
   32 /**
   33  * @defgroup vehicle-gypsy Vehicle GeoClue
   34  * @ingroup vehicle-plugins
   35  * @brief The Vehicle to gain position data from GeoClue.
   36  * @Author jkoan
   37  * @date 2020
   38  *
   39  * @{
   40  */
   41 
   42 
   43 struct vehicle_priv {
   44     GClueLocation *location;
   45     char *bla;
   46     struct callback_list *cbl;
   47     double speed;
   48     double direction;
   49     double height;
   50     struct coord_geo geo;
   51     int accuracy;
   52     char* time_str;
   53     GClueSimple *simple;
   54 };
   55 GClueClient *client = NULL;
   56 
   57 /**
   58  * @brief Free the geoclue_vehicle
   59  *
   60  * @param priv
   61  * @returns nothing
   62  */
   63 static void vehicle_geoclue_destroy(struct vehicle_priv *priv) {
   64     g_clear_object(&client);
   65     g_clear_object(&priv->simple);
   66 }
   67 
   68 static void print_location(GClueSimple *simple,
   69                            GParamSpec *pspec,
   70                            gpointer    user_data) {
   71     GClueLocation *location;
   72     gdouble altitude;
   73     gdouble speed;
   74     gdouble direction;
   75     GVariant *timestamp;
   76     struct vehicle_priv *priv = user_data;
   77 
   78     location = gclue_simple_get_location(simple);
   79 
   80     priv->geo.lat = gclue_location_get_latitude(location);
   81     priv->geo.lng = gclue_location_get_longitude(location);
   82     priv->accuracy = gclue_location_get_accuracy(location);
   83     altitude = gclue_location_get_altitude(location);
   84     if(altitude != -G_MAXDOUBLE) {
   85         priv->height=altitude;
   86     }
   87     speed = gclue_location_get_speed(location);
   88     if(speed != -G_MAXDOUBLE) {
   89         priv->speed=speed;
   90     }
   91     direction = gclue_location_get_heading(location);
   92     if(direction != -G_MAXDOUBLE) {
   93         priv->direction=direction;
   94     }
   95 
   96     timestamp= gclue_location_get_timestamp(location);
   97     if(timestamp) {
   98         GDateTime *date_time;
   99         glong second_since_epoch;
  100 
  101 
  102         g_variant_get (timestamp, "(tt)", &second_since_epoch, NULL);
  103 
  104         date_time = g_date_time_new_from_unix_local (second_since_epoch);
  105 
  106         priv->time_str = g_date_time_format_iso8601(g_date_time_to_utc(date_time));
  107 
  108     }
  109     callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
  110 }
  111 
  112 static void on_client_active_notify(GClueClient *client,
  113                                     GParamSpec *pspec,
  114                                     gpointer    user_data) {
  115     if(gclue_client_get_active(client))
  116         return;
  117 
  118     g_print("Geolocation disabled. Quitting..\n");
  119     //vehicle_geoclue_destroy(&user_data);
  120 }
  121 
  122 static void on_simple_ready(GObject *source_object,
  123                             GAsyncResult *res,
  124                             gpointer user_data) {
  125     GError *error = NULL;
  126 
  127     struct vehicle_priv* priv = user_data;
  128 
  129     priv->simple = gclue_simple_new_finish(res, &error);
  130     if(error != NULL) {
  131         dbg(lvl_error,"Failed to connect to GeoClue2 service: %s", error->message);
  132 
  133         exit(-1);
  134     }
  135     client = gclue_simple_get_client(priv->simple);
  136     if(client) {
  137         g_object_ref(client);
  138         dbg(lvl_debug,"Client object: %s\n",
  139             g_dbus_proxy_get_object_path(G_DBUS_PROXY(client)));
  140 
  141         g_signal_connect(client,
  142                          "notify::active",
  143                          G_CALLBACK(on_client_active_notify),
  144                          NULL);
  145     }
  146     print_location(priv->simple,NULL,priv);
  147 
  148     g_signal_connect(priv->simple,
  149                      "notify::location",
  150                      G_CALLBACK(print_location),
  151                      priv);
  152 }
  153 
  154 /**
  155  * @brief Provide the outside with information
  156  *
  157  * @param priv
  158  * @param type TODO: What can this be?
  159  * @param attr
  160  * @returns true/false
  161  */
  162 static int vehicle_geoclue_position_attr_get(struct vehicle_priv *priv,
  163         enum attr_type type, struct attr *attr) {
  164     switch(type) {
  165     case attr_position_height:
  166         attr->u.numd = &priv->height;
  167         break;
  168     case attr_position_speed:
  169         attr->u.numd = &priv->speed;
  170         break;
  171     case attr_position_direction:
  172         attr->u.numd = &priv->direction;
  173         break;
  174     case attr_position_qual:
  175         attr->u.num = priv->accuracy;
  176         break;
  177     case attr_position_coord_geo:
  178         attr->u.coord_geo = &priv->geo;
  179         break;
  180     case attr_position_time_iso8601:
  181         if(!priv->time_str) {
  182             return 0;
  183         }
  184         attr->u.str=priv->time_str;
  185         break;
  186     case attr_active:
  187         return 1;
  188     default:
  189         return 0;
  190     }
  191     attr->type = type;
  192     return 1;
  193 }
  194 
  195 struct vehicle_methods vehicle_geoclue_methods = {
  196     .destroy           = vehicle_geoclue_destroy,
  197     .position_attr_get = vehicle_geoclue_position_attr_get,
  198 };
  199 
  200 /**
  201  * @brief Create geoclue_vehicle
  202  *
  203  * @param meth[out] Methodes supported by geoclue Plugin
  204  * @param cbl[in] Callback List reference
  205  * @param attrs Configuration attributes
  206  * @returns vehicle_priv The newly created Vehicle priv
  207  */
  208 static struct vehicle_priv *vehicle_geoclue_new(struct vehicle_methods *meth,
  209         struct callback_list *cbl,
  210         struct attr **attrs) {
  211     struct vehicle_priv *ret;
  212     dbg(lvl_debug, "enter");
  213 
  214     *meth = vehicle_geoclue_methods;
  215 
  216     ret = (struct vehicle_priv*)g_new0(struct vehicle_priv, 1);
  217     ret->cbl = cbl;
  218     gclue_simple_new("navit",
  219                      GCLUE_ACCURACY_LEVEL_EXACT,
  220                      NULL,
  221                      on_simple_ready,
  222                      ret
  223                     );
  224     return ret;
  225 };
  226 
  227 /**
  228  * @brief register vehicle_geoclue
  229  *
  230  * @returns nothing
  231  */
  232 void plugin_init(void) {
  233     dbg(lvl_error, "enter");
  234     plugin_register_category_vehicle("geoclue", vehicle_geoclue_new);
  235 };
  236