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

This commit is contained in:
X3F200C 2024-02-22 21:43:10 -05:00
parent b779ee7a55
commit 8b5afd720e
2 changed files with 95 additions and 1 deletions

View File

@ -1,6 +1,6 @@
CC = clang 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_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 SRCS = src/main.c
phonetrackd := phonetrackd phonetrackd := phonetrackd

View File

@ -2,9 +2,103 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <geoclue.h> #include <geoclue.h>
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)", &timestamp_sec, &timestamp_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[]) { int main(int argc, const char *argv[]) {
(void) argc; (void) argc;
(void) argv; (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; return 0;
} }