From d70b5accbee6a0397c5eb76c11a766e7f80522b1 Mon Sep 17 00:00:00 2001 From: X3F200C Date: Thu, 27 Jun 2024 19:39:44 -0400 Subject: [PATCH] Actually upload data --- Makefile | 7 ++++-- src/main.c | 69 +++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index f376487..62a1fe6 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ 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 -lglib-2.0 -lgobject-2.0 +CFLAGS_GLIB = $(shell pkgconf --cflags --libs glib-2.0) +CFLAGS_GEOCLUE = $(shell pkgconf --cflags --libs libgeoclue-2.0) +CFLAGS_JSONC = $(shell pkgconf --cflags --libs json-c) +CFLAGS_CURL = $(shell pkgconf --cflags --libs libcurl) +CFLAGS = -Wall -Wextra -g $(CFLAGS_GLIB) $(CFLAGS_GEOCLUE) $(CFLAGS_JSONC) $(CFLAGS_CURL) SRCS = src/main.c phonetrackd := phonetrackd diff --git a/src/main.c b/src/main.c index e17bc40..fddf50a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,16 @@ -#include #include #include +struct location_data { + double latitude; + double longitude; + double altitude; + double accuracy; + double heading; + double speed; + unsigned long timestamp; +}; + GClueSimple *geoclue_simple = NULL; GClueClient *geoclue_client = NULL; GMainLoop *main_loop; @@ -16,41 +25,61 @@ gboolean on_location_timeout(gpointer user_data) { return FALSE; } +int upload_location_data(struct location_data *location) { + char *location_url_base = getenv("PHONETRACK_URL"); + if (location_url_base == NULL) { + return 1; + } + size_t url_base_length = strlen(location_url_base); + + char *location_url_params = g_strdup_printf("&lat=%f&lon=%f&alt=%f&acc=%f&bearing=%f&speed=%f×tamp=%lu&sat=%i&bat=%f", + location->latitude, location->longitude, location->altitude, location->accuracy, location->heading, location->speed, location->timestamp, 0, 100.0F); + + char *location_url = calloc(url_base_length + strlen(location_url_params) + 1, sizeof(char)); + strcat(location_url, location_url_base); + strcat(location_url, location_url_params); + + g_free(location_url_params); + + CURL *upload_request; + upload_request = curl_easy_init(); + curl_easy_setopt(upload_request, CURLOPT_URL, location_url); + + CURLcode response = curl_easy_perform(upload_request); + + curl_easy_cleanup(upload_request); + + free(location_url); + + return response; +} + void get_location(GClueSimple *simple) { (void) simple; GClueLocation *location; - gdouble latitude, longitude, altitude, accuracy; GVariant *timestamp; // const char *description; + struct location_data *location_struct = calloc(1, sizeof(struct location_data)); 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); + location_struct->latitude = gclue_location_get_latitude(location); + location_struct->longitude = gclue_location_get_longitude(location); + location_struct->altitude = gclue_location_get_altitude(location); + location_struct->accuracy = gclue_location_get_accuracy(location); + location_struct->heading = gclue_location_get_heading(location); + location_struct->speed = gclue_location_get_speed(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); + location_struct->timestamp = timestamp_sec; } - const char *location_payload = json_object_to_json_string(location_json); - printf("%s\n", location_payload); + upload_location_data(location_struct); } void on_geoclue_client_active(GClueClient *geoclue_client, GParamSpec *parameter_spec, gpointer user_data) { @@ -95,6 +124,8 @@ int main(int argc, const char *argv[]) { (void) argc; (void) argv; + curl_global_init(CURL_GLOBAL_ALL); + gclue_simple_new("phonetrackd", GCLUE_ACCURACY_LEVEL_EXACT, NULL, on_geoclue_ready, NULL); main_loop = g_main_loop_new(NULL, FALSE);