Compare commits

...

6 Commits

3 changed files with 164 additions and 0 deletions

20
Makefile Normal file
View File

@ -0,0 +1,20 @@
CC = clang
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
$(phonetrackd): $(SRCS)
$(CC) $(CFLAGS) $(SRCS) -o $(phonetrackd)
all: $(phonetrackd)
install: $(phonetrackd)
cp $(phonetrackd) /usr/local/bin/
clean:
rm $(phonetrackd)

View File

@ -1,2 +1,4 @@
# phonetrack-linux # phonetrack-linux
Basically a position logger/reporter but for Linux devices.
This is still a work in progress aiming to have feature parity with the PhoneTrack Android app.

142
src/main.c Normal file
View File

@ -0,0 +1,142 @@
#include <curl/curl.h>
#include <geoclue.h>
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;
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;
}
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&timestamp=%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);
curl_easy_setopt(upload_request, CURLOPT_USERAGENT, "phonetrack-linux");
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;
GVariant *timestamp;
// const char *description;
struct location_data *location_struct = calloc(1, sizeof(struct location_data));
location = gclue_simple_get_location(simple);
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);
timestamp = gclue_location_get_timestamp(location);
if (timestamp) {
unsigned long timestamp_sec, timestamp_usec;
g_variant_get(timestamp, "(tt)", &timestamp_sec, &timestamp_usec);
location_struct->timestamp = timestamp_sec;
}
if (location_struct->latitude == 0.0F && location_struct->longitude == 0.0F) {
return;
}
upload_location_data(location_struct);
free(location_struct);
}
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;
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);
g_main_loop_run(main_loop);
return 0;
}