From: Thomas Hooge Date: Tue, 18 Oct 2016 07:06:38 +0000 (+0200) Subject: Switched from strptime to sscanf, added command line switch to set system clock and... X-Git-Url: http://git.openwrt.org//?p=project%2Fugps.git;a=commitdiff_plain;h=32a6b2b702c3b9f8c425f3d9dc9f4273e276029c Switched from strptime to sscanf, added command line switch to set system clock and fixed typo Signed-off-by: Thomas Hogge --- diff --git a/main.c b/main.c index 6ff4c17..7696cbc 100644 --- a/main.c +++ b/main.c @@ -33,6 +33,7 @@ static struct ubus_auto_conn conn; static struct blob_buf b; static char *ubus_socket; struct timespec stamp = { 0 }; +unsigned int adjust_clock = 0; void gps_timestamp(void) @@ -57,7 +58,7 @@ gps_info(struct ubus_context *ctx, struct ubus_object *obj, 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, "elivation", elivation); + blobmsg_add_string(&b, "elevation", elevation); blobmsg_add_string(&b, "course", course); blobmsg_add_string(&b, "speed", speed); } @@ -95,6 +96,7 @@ usage(const char *prog) { fprintf(stderr, "Usage: %s [options] \n" "Options:\n" + " -a Adjust system clock from gps\n" " -s Path to ubus socket\n" " -d Enable debug messages\n" " -S Print messages to stdout\n" @@ -117,8 +119,11 @@ main(int argc, char ** argv) unsetenv("DBGLVL"); } - while ((ch = getopt(argc, argv, "d:D:s:S")) != -1) { + while ((ch = getopt(argc, argv, "ad:s:S")) != -1) { switch (ch) { + case 'a': + adjust_clock = -1; + break; case 's': ubus_socket = optarg; break; diff --git a/nmea.c b/nmea.c index 1a7ac27..275f39f 100644 --- a/nmea.c +++ b/nmea.c @@ -42,7 +42,7 @@ #include "nmea.h" #define MAX_NMEA_PARAM 20 -#define MAX_TIME_OFFSET 2 +#define MAX_TIME_OFFSET 5 #define MAX_BAD_TIME 3 struct nmea_param { @@ -51,7 +51,7 @@ struct nmea_param { } nmea_params[MAX_NMEA_PARAM]; static int nmea_bad_time; -char longitude[32] = { 0 }, latitude[32] = { 0 }, course[16] = { 0 }, speed[16] = { 0 }, elivation[16] = { 0 }; +char longitude[32] = { 0 }, latitude[32] = { 0 }, course[16] = { 0 }, speed[16] = { 0 }, elevation[16] = { 0 }; int gps_valid = 0; static void @@ -81,31 +81,36 @@ nmea_rmc_cb(void) memset(&tm, 0, sizeof(tm)); tm.tm_isdst = 1; - if (!strptime(nmea_params[1].str, "%H%M%S", &tm)) - ERROR("failed to parse time\n"); - else if (!strptime(nmea_params[9].str, "%d%m%y", &tm)) - ERROR("failed to parse date\n"); + if (sscanf(nmea_params[1].str, "%02d%02d%02d", + &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 3) { + ERROR("failed to parse time '%s'\n", nmea_params[1].str); + } + else if (sscanf(nmea_params[9].str, "%02d%02d%02d", + &tm.tm_mday, &tm.tm_mon, &tm.tm_year) != 3) { + ERROR("failed to parse date '%s'\n", nmea_params[9].str); + } else { - /* is there a libc api for the tz adjustment ? */ - struct timeval tv = { mktime(&tm), 0 }; - struct timeval cur; + tm.tm_year += 100; /* year starts with 1900 */ + tm.tm_mon -= 1; /* month starts with 0 */ - strftime(tmp, 256, "%D %02H:%02M:%02S", &tm); + strftime(tmp, 256, "%Y-%m-%dT%H:%M:%S", &tm); DEBUG(3, "date: %s UTC\n", tmp); - tv.tv_sec -= timezone; - if (daylight) - tv.tv_sec += 3600; + if (adjust_clock) { + struct timeval tv = { timegm(&tm), 0 }; + struct timeval cur; - gettimeofday(&cur, NULL); + gettimeofday(&cur, NULL); - if (abs(cur.tv_sec - tv.tv_sec) > MAX_TIME_OFFSET) { - if (++nmea_bad_time > MAX_BAD_TIME) { - LOG("system time differs from GPS time by more than %d seconds. Using %s UTC as the new time\n", MAX_TIME_OFFSET, tmp); - settimeofday(&tv, NULL); + if (abs(cur.tv_sec - tv.tv_sec) > MAX_TIME_OFFSET) { + if (++nmea_bad_time > MAX_BAD_TIME) { + LOG("system time differs from GPS time by more than %d seconds. Using %s UTC as the new time\n", MAX_TIME_OFFSET, tmp); + /* only set datetime if specified by command line argument! */ + settimeofday(&tv, NULL); + } + } else { + nmea_bad_time = 0; } - } else { - nmea_bad_time = 0; } } @@ -144,8 +149,8 @@ nmea_gga_cb(void) { if (!gps_valid) return; - strncpy(elivation, nmea_params[9].str, sizeof(elivation)); - DEBUG(4, "height: %s\n", elivation); + strncpy(elevation, nmea_params[9].str, sizeof(elevation)); + DEBUG(4, "height: %s\n", elevation); } static void diff --git a/nmea.h b/nmea.h index 641f49e..c6f1896 100644 --- a/nmea.h +++ b/nmea.h @@ -23,8 +23,9 @@ #include -extern char longitude[32], latitude[32], course[16], speed[16], elivation[16]; +extern char longitude[32], latitude[32], course[16], speed[16], elevation[16]; extern int nmea_open(char *dev, struct ustream_fd *s, speed_t speed); extern void gps_timestamp(void); +extern unsigned int adjust_clock; #endif