[PATCH] ugps: add baud rate command line option

Wojciech Jowsa wojciech.jowsa at gmail.com
Sun Jan 16 11:17:17 PST 2022


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 at gmail.com>
---
 main.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/main.c b/main.c
index 2ab0f8c..bb1592b 100644
--- a/main.c
+++ b/main.c
@@ -105,10 +105,31 @@ 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:
+			return B0;
+    }
+}
+
 int
 main(int argc, char ** argv)
 {
@@ -116,6 +137,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 +146,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,11 +160,19 @@ 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]);
 		}
 	}
 
+	if (!baudrate) {
+		fprintf(stderr, "ERROR: incorrect baud rate \n");
+		return usage(argv[0]);
+	}
+
 	if (argc - optind < 1) {
 		fprintf(stderr, "ERROR: missing device parameter\n");
 		return usage(argv[0]);
@@ -156,7 +186,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();
-- 
2.25.1




More information about the openwrt-devel mailing list