ugps: add baud rate command line option
authorWojciech Jowsa <wojciech.jowsa@gmail.com>
Sun, 16 Jan 2022 20:28:36 +0000 (21:28 +0100)
committerPetr Štetiar <ynezz@true.cz>
Sat, 19 Feb 2022 10:28:37 +0000 (11:28 +0100)
currently, gps device baud rate is hardcoded so
ugps might not work with devices
that baud rate is configured to be different than 4800.

Signed-off-by: Wojciech Jowsa <wojciech.jowsa@gmail.com>
main.c

diff --git a/main.c b/main.c
index 2ab0f8cf388099cf8e033d234ee07a77daf3509c..76a5aab4d0bc83b501dd4d1044f24605b8e617d8 100644 (file)
--- a/main.c
+++ b/main.c
@@ -105,10 +105,32 @@ usage(const char *prog)
                "       -s <path>       Path to ubus socket\n"
                "       -d <level>      Enable debug messages\n"
                "       -S              Print messages to stdout\n"
+               "       -b              Set gps device baud rate\n"
                "\n", prog);
        return -1;
 }
 
+static speed_t get_baudrate(int baudrate)
+{
+    switch (baudrate) {
+               case 4800:
+                       return B4800;
+               case 9600:
+                       return B9600;
+               case 19200:
+                       return B19200;
+               case 38400:
+                       return B38400;
+               case 57600:
+                       return B57600;
+               case 115200:
+                       return B115200;
+               default:
+                       fprintf(stderr, "ERROR: incorrect baud rate. Default 4800 baud rate has been set\n");
+                       return B4800;
+    }
+}
+
 int
 main(int argc, char ** argv)
 {
@@ -116,6 +138,7 @@ main(int argc, char ** argv)
        char *device = NULL;
        char *dbglvl = getenv("DBGLVL");
        int ulog_channels = ULOG_KMSG;
+       speed_t baudrate = B4800;
 
        signal(SIGPIPE, SIG_IGN);
 
@@ -124,7 +147,7 @@ main(int argc, char ** argv)
                unsetenv("DBGLVL");
        }
 
-       while ((ch = getopt(argc, argv, "ad:s:S")) != -1) {
+       while ((ch = getopt(argc, argv, "ad:s:S:b:")) != -1) {
                switch (ch) {
                case 'a':
                        adjust_clock = -1;
@@ -138,6 +161,9 @@ main(int argc, char ** argv)
                case 'S':
                        ulog_channels = ULOG_STDIO;
                        break;
+               case 'b':
+                       baudrate = get_baudrate(atoi(optarg));
+                       break;
                default:
                        return usage(argv[0]);
                }
@@ -156,7 +182,7 @@ main(int argc, char ** argv)
        conn.cb = ubus_connect_handler;
        ubus_auto_connect(&conn);
 
-       if (nmea_open(device, &stream, B4800) < 0)
+       if (nmea_open(device, &stream, baudrate) < 0)
                return -1;
 
        uloop_run();