summaryrefslogtreecommitdiffstats
path: root/binding/afm-gps-binding.c
diff options
context:
space:
mode:
authorMatt Ranostay <matt.ranostay@konsulko.com>2017-11-13 18:41:33 -0800
committerMatt Ranostay <matt.ranostay@konsulko.com>2017-11-13 20:03:35 -0800
commit0cc78ea27bc2a67c47223d645ad9268d4f8d8598 (patch)
treedbc268d85ed50349ad239828bcee077102b66d29 /binding/afm-gps-binding.c
parent20d636d0224c445e4a60fee8283bb18a8c221908 (diff)
binding: gps: add recording functionality
Allow recording of gps position data from the binding to allow data for use in unit tests, and demonstrations. Bug-AGL: SPEC-1089 Change-Id: I58da8382835e5d3fc45193b6e552525835e034db Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
Diffstat (limited to 'binding/afm-gps-binding.c')
-rw-r--r--binding/afm-gps-binding.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/binding/afm-gps-binding.c b/binding/afm-gps-binding.c
index 7fbd907..91b82cd 100644
--- a/binding/afm-gps-binding.c
+++ b/binding/afm-gps-binding.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <gps.h>
+#include <time.h>
#include <pthread.h>
#include <json-c/json.h>
@@ -36,6 +37,12 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define MSECS_TO_USECS(x) (x * 1000)
+struct {
+ FILE *current_file;
+ char buffer[32];
+ int count;
+} recording;
+
// struct dop_t item order
static const char *dop_names[] = {
"xdop",
@@ -121,6 +128,90 @@ static void get_data(struct afb_req request)
pthread_mutex_unlock(&mutex);
}
+static json_object *gps_recording_state(json_object *jresp)
+{
+ json_object *value = NULL;
+
+ if (recording.current_file == NULL) {
+ value = json_object_new_boolean(true);
+ json_object_object_add(jresp, "recording", value);
+ } else {
+ value = json_object_new_boolean(true);
+ json_object_object_add(jresp, "recording", value);
+
+ value = json_object_new_string(recording.buffer);
+ json_object_object_add(jresp, "filename", value);
+
+ if (recording.count) {
+ value = json_object_new_int(recording.count);
+ json_object_object_add(jresp, "count", value);
+ }
+ }
+
+ return jresp;
+}
+
+static void record(struct afb_req request)
+{
+ json_object *jresp = NULL;
+ const char *value = afb_req_value(request, "state");
+ bool record = false;
+
+ if (!value) {
+ pthread_mutex_lock(&mutex);
+ jresp = gps_recording_state(json_object_new_object());
+ pthread_mutex_unlock(&mutex);
+
+ afb_req_success(request, jresp, "GPS Current Recording State");
+ return;
+ }
+
+ if (!strcasecmp(value, "on"))
+ record = true;
+ else if (!strcasecmp(value, "off"))
+ record = false;
+ else {
+ afb_req_fail(request, "failed", "Invalid state requested");
+ return;
+ }
+
+ pthread_mutex_lock(&mutex);
+
+ if (recording.current_file != NULL) {
+ recording.count = 0;
+ fclose(recording.current_file);
+ recording.current_file = NULL;
+
+ if (!record) {
+ pthread_mutex_unlock(&mutex);
+ afb_req_success(request, NULL, NULL);
+ return;
+ }
+ } else if (recording.current_file == NULL && !record) {
+ pthread_mutex_unlock(&mutex);
+ afb_req_fail(request, "failed", "Recording was already stopped");
+ return;
+ }
+
+ {
+ time_t cur_time;
+ struct tm *info;
+
+ time(&cur_time);
+ info = localtime(&cur_time);
+ strftime((char *) &recording.buffer, sizeof(recording.buffer),
+ "gps_%Y%m%d_%H%M.log", info);
+
+ recording.current_file = fopen(recording.buffer, "w+");
+
+ jresp = gps_recording_state(json_object_new_object());
+ afb_req_success(request, jresp, "GPS Recording Result");
+ }
+
+ pthread_mutex_unlock(&mutex);
+}
+
+
static void subscribe(struct afb_req request)
{
const char *value = afb_req_value(request, "value");
@@ -147,6 +238,14 @@ static void unsubscribe(struct afb_req request)
afb_req_fail(request, "failed", "Invalid event");
}
+static void add_record(json_object *jresp)
+{
+ fprintf(recording.current_file, "%s\n", json_object_to_json_string(jresp));
+ fflush(recording.current_file);
+
+ recording.count++;
+}
+
static void *data_poll(void *ptr)
{
/*
@@ -165,6 +264,10 @@ static void *data_poll(void *ptr)
if (!(data.set & (TRACKERR_SET | SPEEDERR_SET| CLIMBERR_SET))) {
jresp = populate_json_data(json_object_new_object());
+
+ if (recording.current_file != NULL)
+ add_record(jresp);
+
if (jresp != NULL)
afb_event_push(location_event, jresp);
}
@@ -206,6 +309,7 @@ static int init()
static const struct afb_verb_v2 binding_verbs[] = {
{ .verb = "location", .callback = get_data, .info = "Get GNSS data" },
+ { .verb = "record", .callback = record, .info = "Record GPS data" },
{ .verb = "subscribe", .callback = subscribe, .info = "Subscribe to GNSS events" },
{ .verb = "unsubscribe", .callback = unsubscribe, .info = "Unsubscribe to GNSS events" },
{ }