105 lines
3.0 KiB
C

#include <json-c/json.h>
#include <curl/curl.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[]) {
(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;
}