From 5e88403fc0d39ae8a270d2c6c6e9c8a4d5232cf3 Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Mon, 7 Jun 2021 22:44:52 +0100 Subject: [PATCH] ubus: display only available information GPS receivers may not provide all possible data, some take more time and some are irrelevant for some applications and are never sent by some GPS receivers (eg. equipment found in maritime context doesn't report elevation). Only populate attributes for which data is actually available. Signed-off-by: Daniel Golle --- main.c | 17 +++++++++++------ nmea.c | 5 +++++ nmea.h | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 7696cbc..2ab0f8c 100644 --- a/main.c +++ b/main.c @@ -52,15 +52,20 @@ gps_info(struct ubus_context *ctx, struct ubus_object *obj, blob_buf_init(&b, 0); - if (!stamp.tv_sec) { + if (!stamp.tv_sec || !gps_fields) { blobmsg_add_u8(&b, "signal", 0); } else { blobmsg_add_u32(&b, "age", now.tv_sec - stamp.tv_sec); - blobmsg_add_string(&b, "latitude", latitude); - blobmsg_add_string(&b, "longitude", longitude); - blobmsg_add_string(&b, "elevation", elevation); - blobmsg_add_string(&b, "course", course); - blobmsg_add_string(&b, "speed", speed); + if (gps_fields & GPS_FIELD_LAT) + blobmsg_add_string(&b, "latitude", latitude); + if (gps_fields & GPS_FIELD_LON) + blobmsg_add_string(&b, "longitude", longitude); + if (gps_fields & GPS_FIELD_ALT) + blobmsg_add_string(&b, "elevation", elevation); + if (gps_fields & GPS_FIELD_COG) + blobmsg_add_string(&b, "course", course); + if (gps_fields & GPS_FIELD_SPD) + blobmsg_add_string(&b, "speed", speed); } ubus_send_reply(ctx, req, b.head); diff --git a/nmea.c b/nmea.c index d51d936..8e61606 100644 --- a/nmea.c +++ b/nmea.c @@ -54,6 +54,7 @@ struct nmea_param { static int nmea_bad_time; char longitude[33] = { 0 }, latitude[33] = { 0 }, course[17] = { 0 }, speed[17] = { 0 }, elevation[17] = { 0 }; int gps_valid = 0; +char gps_fields = 0; static void nmea_txt_cb(void) @@ -119,6 +120,8 @@ parse_gps_coords(char *latstr, char *vhem, char *lonstr, char *hhem) snprintf(longitude, sizeof(longitude), "%f", lon); DEBUG(3, "position: %s %s\n", latitude, longitude); + gps_fields |= GPS_FIELD_LAT | GPS_FIELD_LON; + gps_timestamp(); } @@ -220,6 +223,7 @@ nmea_gga_cb(void) if (!gps_valid) return; strncpy(elevation, nmea_params[9].str, sizeof(elevation)); + gps_fields |= GPS_FIELD_ALT; DEBUG(4, "height: %s\n", elevation); } @@ -230,6 +234,7 @@ nmea_vtg_cb(void) return; strncpy(course, nmea_params[1].str, sizeof(course)); strncpy(speed, nmea_params[7].str, sizeof(speed)); + gps_fields |= GPS_FIELD_COG | GPS_FIELD_SPD; DEBUG(4, "course: %s\n", course); DEBUG(4, "speed: %s\n", speed); } diff --git a/nmea.h b/nmea.h index 9f46d29..2de06dc 100644 --- a/nmea.h +++ b/nmea.h @@ -27,5 +27,11 @@ extern char longitude[33], latitude[33], course[17], speed[17], elevation[17]; extern int nmea_open(char *dev, struct ustream_fd *s, speed_t speed); extern void gps_timestamp(void); extern unsigned int adjust_clock; +extern char gps_fields; +#define GPS_FIELD_LAT (1<<0) +#define GPS_FIELD_LON (1<<1) +#define GPS_FIELD_COG (1<<2) +#define GPS_FIELD_SPD (1<<3) +#define GPS_FIELD_ALT (1<<4) #endif -- 2.30.2