diff options
author | Gary E. Miller <gem@rellim.com> | 2005-04-13 04:06:41 +0000 |
---|---|---|
committer | Gary E. Miller <gem@rellim.com> | 2005-04-13 04:06:41 +0000 |
commit | bd63c987d9fee6783efa463e3ad00ae6190cf55d (patch) | |
tree | 7b44c08647149d988736b37d621b9d2f60c8a69b /contrib | |
parent | b87ae01e9e1f77cae70af511d483bb145c7e16f9 (diff) | |
download | gpsd-bd63c987d9fee6783efa463e3ad00ae6190cf55d.tar.gz |
Added -t for a timestamp and -n [count] to exit after count packets
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/gpspipe.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/contrib/gpspipe.c b/contrib/gpspipe.c new file mode 100644 index 00000000..faba6e09 --- /dev/null +++ b/contrib/gpspipe.c @@ -0,0 +1,166 @@ +/* + * gpspipe + * + * a simple program to connect to a gpsd daemon and dump the received data + * to stdout + * + * This will dump the raw NMEA from gpsd to stdout + * gpspipe -r + * + * This will dump the GPSD sentences from gpsd to stdout + * gpspipe -w + * + * This will dump the GPSD and the NMEA sentences from gpsd to stdout + * gpspipe -wr + * + * bad code by: Gary E. Miller <gem@rellim.com> + * all rights given to the gpsd project to release under whatever open source + * license they use. A thank would be nice if you use this code. + * + * just needs to be linked to netlib like this: + * gcc gpspipe.c ../netlib.c -o gpspipe + * + * TODO + * add "-p [device]" + * This would force gpspipe to create a bidirectional pipe device + * for output. Then programs that expect to connect to a raw GPS + * device could conenct to that. + * + * man pages + * use autoconf to create configure, Makefile, etc. + */ + +#include <errno.h> +#include <stdio.h> +#include <sys/socket.h> +#include <unistd.h> +#include "../gpsd.h" + +void usage(const char *prog) { + fprintf(stderr, "%s: connect to local gpsd and dump data to stdout\n\n" + "-h show this help\n" + "-r Dump raw NMEA\n" + "-w Dump gpsd native data\n\n" + "-t time stamp the data\n\n" + "-n [count] exit after count packets\n\n" + "You must specify one, or both, of -r/-w\n", + prog); +} + +int main( int argc, char **argv) { + + int s = 0; + char buf[4096]; + char *cstr = NULL; + int wrote = 0; + int dump_nmea = 0; + int dump_gpsd = 0; + int timestamp = 0; + int new_line = 1; + long count = -1; + char option; + extern char *optarg; + + + while ((option = getopt(argc, argv, "?hrwtn:")) != -1) { + switch (option) { + case 'n': + count = strtol(optarg, 0, 0); + break; + case 'r': + dump_nmea = 1; + break; + case 't': + timestamp = 1; + break; + case 'w': + dump_gpsd = 1; + break; + case '?': + case 'h': + default: + usage( argv[0] ); + exit(1); + } + } + if ( dump_nmea ) { + if ( dump_gpsd ) { + cstr = "rw\n"; + } else { + cstr = "r\n"; + } + } else if ( dump_gpsd ) { + cstr = "w\n"; + } else { + usage( argv[0] ); + exit(1); + } + s = netlib_connectsock( "127.0.0.1", "2947", "tcp"); + if ( s < 0 ) { + fprintf( stderr, "%s: could not connect to gpsd, %s(%d)\n" + , argv[0] , strerror(errno), errno); + exit (1); + } + + wrote = write( s, cstr, strlen(cstr) ); + if ( strlen(cstr) != wrote ) { + fprintf( stderr, "%s: write error, %s(%d)\n", argv[0] + , strerror(errno), errno); + exit (1); + } + + for(;;) { + int i = 0; + int readbytes = 0; + + readbytes = read( s, buf, sizeof(buf)); + if ( readbytes > 0 ) { + for ( i = 0 ; i < readbytes ; i++ ) { + char c = buf[i]; + if ( new_line && timestamp ) { + time_t now = time(NULL); + + new_line = 0; + if ( 0 > fprintf( stdout, "%.24s :", + ctime( &now )) ) { + fprintf( stderr + , "%s: Write Error, %s(%d)\n" + , argv[0] + , strerror(errno), errno); + exit (1); + } + } + if ( EOF == fputc( c, stdout) ) { + fprintf( stderr, "%s: Write Error, %s(%d)\n" + , argv[0] + , strerror(errno), errno); + exit (1); + } + + if ( '\n' == c ) { + new_line = 1; + /* flush after eveery good line */ + if ( fflush( stdout ) ) { + fprintf( stderr, "%s: fflush Error, %s(%d)\n" + , argv[0] + , strerror(errno), errno); + exit (1); + } + if ( 0 <= count ) { + if ( 0 >= --count ) { + /* completed count */ + exit(0); + } + } + + } + } + } else if ( readbytes < 0 ) { + fprintf( stderr, "%s: Read Error %s(%d)\n", argv[0] + , strerror(errno), errno); + + exit(1); + } + } + exit(0); +} |