From 8b5afd720e67ee31c1ff447794db24646d9cc619 Mon Sep 17 00:00:00 2001 From: X3F200C Date: Thu, 22 Feb 2024 21:43:10 -0500 Subject: [PATCH] Copy a lot of code from "Where am I" example at https://cgit.freedesktop.org/geoclue/plain/demo/where-am-i.c and log JSON once --- Makefile | 2 +- src/main.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a139f9f..f376487 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = clang CFLAGS_GEOCLUE = -I/usr/include/libgeoclue-2.0 -I/usr/include/gio-unix-2.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/sysprof-6 -pthread -CFLAGS = -Wall -Wextra -g $(CFLAGS_GEOCLUE) -ljson-c -lcurl -lgeoclue-2 +CFLAGS = -Wall -Wextra -g $(CFLAGS_GEOCLUE) -ljson-c -lcurl -lgeoclue-2 -lglib-2.0 -lgobject-2.0 SRCS = src/main.c phonetrackd := phonetrackd diff --git a/src/main.c b/src/main.c index e5fb9b2..e17bc40 100644 --- a/src/main.c +++ b/src/main.c @@ -2,9 +2,103 @@ #include #include +GClueSimple *geoclue_simple = NULL; +GClueClient *geoclue_client = NULL; +GMainLoop *main_loop; + +gboolean on_location_timeout(gpointer user_data) { + (void) user_data; + + g_clear_object(&geoclue_client); + g_clear_object(&geoclue_simple); + g_main_loop_quit(main_loop); + + return FALSE; +} + +void get_location(GClueSimple *simple) { + (void) simple; + + GClueLocation *location; + gdouble latitude, longitude, altitude, accuracy; + GVariant *timestamp; + // const char *description; + + location = gclue_simple_get_location(simple); + json_object *location_json = json_object_new_object(); + + latitude = gclue_location_get_latitude(location); + longitude = gclue_location_get_longitude(location); + altitude = gclue_location_get_altitude(location); + accuracy = gclue_location_get_accuracy(location); + + json_object *latitude_json = json_object_new_double(latitude); + json_object_object_add(location_json, "lat", latitude_json); + json_object *longitude_json = json_object_new_double(longitude); + json_object_object_add(location_json, "lon", longitude_json); + json_object *altitude_json = json_object_new_double(altitude); + json_object_object_add(location_json, "alt", altitude_json); + json_object *accuracy_json = json_object_new_double(accuracy); + json_object_object_add(location_json, "acc", accuracy_json); + timestamp = gclue_location_get_timestamp(location); + if (timestamp) { + unsigned long timestamp_sec, timestamp_usec; + g_variant_get(timestamp, "(tt)", ×tamp_sec, ×tamp_usec); + + json_object *timestamp_json = json_object_new_int(timestamp_sec); + json_object_object_add(location_json, "tst", timestamp_json); + } + + const char *location_payload = json_object_to_json_string(location_json); + printf("%s\n", location_payload); +} + +void on_geoclue_client_active(GClueClient *geoclue_client, GParamSpec *parameter_spec, gpointer user_data) { + (void) parameter_spec; + (void) user_data; + + if (gclue_client_get_active(geoclue_client)) + return; + + printf("Geolocation disabled, can't continue... \n"); + on_location_timeout(NULL); +} + +void on_geoclue_ready(GObject *source_object, GAsyncResult *result, gpointer user_data) { + (void) source_object; + (void) user_data; + + GError *error = NULL; + + geoclue_simple = gclue_simple_new_with_thresholds_finish(result, &error); + if (error != NULL) { + printf("Couldn't connect to the GeoClue service : %s\n", error->message); + + exit(-1); + } + + geoclue_client = gclue_simple_get_client(geoclue_simple); + if (geoclue_client) { + g_object_ref(geoclue_client); + printf("GeoClue client ready ! \n"); + + g_signal_connect(geoclue_client, "notify::active", G_CALLBACK(on_geoclue_client_active), NULL); + } + + get_location(geoclue_simple); + + g_signal_connect(geoclue_client, "notify::location", G_CALLBACK(get_location), NULL); + +} + int main(int argc, const char *argv[]) { (void) argc; (void) argv; + gclue_simple_new("phonetrackd", GCLUE_ACCURACY_LEVEL_EXACT, NULL, on_geoclue_ready, NULL); + + main_loop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(main_loop); + return 0; }