Switched from strptime to sscanf, added command line switch to set system clock and...
authorThomas Hooge <thomas@hoogi.de>
Tue, 18 Oct 2016 07:06:38 +0000 (09:06 +0200)
committerJohn Crispin <john@phrozen.org>
Sat, 22 Oct 2016 20:08:53 +0000 (22:08 +0200)
Signed-off-by: Thomas Hogge <thomas@hoogi.de>
main.c
nmea.c
nmea.h

diff --git a/main.c b/main.c
index 6ff4c173dab1ca08bafdd6e179cd6d6d0a09bc84..7696cbc9c26282e1a55f68e2abdeebbb6517f862 100644 (file)
--- 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] <device>\n"
                "Options:\n"
+               "       -a              Adjust system clock from gps\n"
                "       -s <path>       Path to ubus socket\n"
                "       -d <level>      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 1a7ac275af8e15e0b4101429428a0a4908f2f0ac..275f39f690e7e96215eae90e5eecf3a311a62d1a 100644 (file)
--- 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 641f49ec9ec9ce219cbca8481e1e5526858cdcc9..c6f1896df986cefeb2dc1bbad5180f7cb65f14fe 100644 (file)
--- a/nmea.h
+++ b/nmea.h
@@ -23,8 +23,9 @@
 
 #include <libubox/ustream.h>
 
-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