From abc014e31d2babbec0bac80c8f815807812bef79 Mon Sep 17 00:00:00 2001 From: Jean Pierre TOSONI Date: Thu, 7 Dec 2017 11:43:18 +0000 Subject: gpspipe improvement: add option to exit after delay If the GPS receiver cease to supply data to gpsd, gpspipe hangs indefinitely waiting for gpsd. This can happen if the GPS is turned off by external means, e.g. by an AT command on the command port of a cellular card. In order to use gpspipe to get a fix within bounded time, this patch adds an option to specify a timout, so that I can do things like: $x=$(gpspipe -x 5 -w|sed -n '/TPV/{p;q}') And get either the first TPV or an empty string if no fix is available. Signed-off-by: Gary E. Miller --- gpspipe.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'gpspipe.c') diff --git a/gpspipe.c b/gpspipe.c index 9c2e2438..6a9c2237 100644 --- a/gpspipe.c +++ b/gpspipe.c @@ -128,6 +128,7 @@ static void usage(void) "-u usec time stamp, implies -t. Use -uu to output sec.usec\n" "-s [serial dev] emulate a 4800bps NMEA GPS on serial port (use with '-r').\n" "-n [count] exit after count packets.\n" + "-x [seconds] Exit after given delay.\n" "-v Print a little spinner.\n" "-p Include profiling info in the JSON.\n" "-P Include PPS JSON in NMEA or raw mode.\n" @@ -151,6 +152,7 @@ int main(int argc, char **argv) bool profile = false; int option_u = 0; // option to show uSeconds long count = -1; + time_t exit_timer = 0; int option; unsigned int vflag = 0, l = 0; FILE *fp; @@ -162,7 +164,7 @@ int main(int argc, char **argv) char *outfile = NULL; flags = WATCH_ENABLE; - while ((option = getopt(argc, argv, "?dD:lhrRwStT:vVn:s:o:pPu2")) != -1) { + while ((option = getopt(argc, argv, "?dD:lhrRwStT:vVx:n:s:o:pPu2")) != -1) { switch (option) { case 'D': debug = atoi(optarg); @@ -222,6 +224,9 @@ int main(int argc, char **argv) (void)fprintf(stderr, "%s: %s (revision %s)\n", argv[0], VERSION, REVISION); exit(EXIT_SUCCESS); + case 'x': + exit_timer = time(NULL) + strtol(optarg, 0, 0); + break; case 's': serialport = optarg; break; @@ -321,6 +326,8 @@ int main(int argc, char **argv) FD_SET(gpsdata.gps_fd, &fds); errno = 0; r = select(gpsdata.gps_fd+1, &fds, NULL, NULL, &tv); + if (r >= 0 && exit_timer && time(NULL) >= exit_timer) + break; if (r == -1 && errno != EINTR) { (void)fprintf(stderr, "gpspipe: select error %s(%d)\n", strerror(errno), errno); -- cgit v1.2.1