gpspipe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * gpspipe
  3. *
  4. * a simple program to connect to a gpsd daemon and dump the received data
  5. * to stdout
  6. *
  7. * This will dump the raw NMEA from gpsd to stdout
  8. * gpspipe -r
  9. *
  10. * This will dump the super-raw data (gps binary) from gpsd to stdout
  11. * gpspipe -R
  12. *
  13. * This will dump the GPSD sentences from gpsd to stdout
  14. * gpspipe -w
  15. *
  16. * This will dump the GPSD and the NMEA sentences from gpsd to stdout
  17. * gpspipe -wr
  18. *
  19. * Original code by: Gary E. Miller <gem@rellim.com>. Cleanup by ESR.
  20. *
  21. * This file is Copyright (c) 2010-2018 by the GPSD project
  22. * SPDX-License-Identifier: BSD-2-clause
  23. *
  24. */
  25. #include "gpsd_config.h" /* must be before all includes */
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <stdbool.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <strings.h>
  33. #include <sys/select.h>
  34. #include <sys/stat.h>
  35. #include <sys/time.h>
  36. #include <sys/types.h>
  37. #include <time.h> /* for time_t */
  38. #include <unistd.h>
  39. #ifdef HAVE_SYS_SOCKET_H
  40. #include <sys/socket.h>
  41. #endif /* HAVE_SYS_SOCKET_H */
  42. #ifdef HAVE_TERMIOS_H
  43. #include <termios.h> /* for cfmakeraw() on some OS */
  44. #endif /* HAVE_TERMIOS_H */
  45. #ifdef HAVE_WINSOCK2_H
  46. #include <winsock2.h>
  47. #endif /* HAVE_WINSOCK2_H */
  48. #include "gpsd.h"
  49. #include "gpsdclient.h"
  50. #include "revision.h"
  51. static struct gps_data_t gpsdata;
  52. static void spinner(unsigned int, unsigned int);
  53. /* NMEA-0183 standard baud rate */
  54. #define BAUDRATE B4800
  55. /* Serial port variables */
  56. #ifdef HAVE_TERMIOS_H
  57. static struct termios oldtio, newtio;
  58. #endif /* HAVE_TERMIOS_H */
  59. static int fd_out = 1; /* output initially goes to standard output */
  60. static char serbuf[255];
  61. static int debug;
  62. static void open_serial(char *device)
  63. /* open the serial port and set it up */
  64. {
  65. /*
  66. * Open modem device for reading and writing and not as controlling
  67. * tty.
  68. */
  69. if ((fd_out = open(device, O_RDWR | O_NOCTTY)) == -1) {
  70. (void)fprintf(stderr, "gpspipe: error opening serial port\n");
  71. exit(EXIT_FAILURE);
  72. }
  73. #ifdef HAVE_TERMIOS_H
  74. /* Save current serial port settings for later */
  75. if (tcgetattr(fd_out, &oldtio) != 0) {
  76. (void)fprintf(stderr, "gpspipe: error reading serial port settings\n");
  77. exit(EXIT_FAILURE);
  78. }
  79. /* Clear struct for new port settings. */
  80. memset(&newtio, 0, sizeof(newtio));
  81. /* make it raw */
  82. (void)cfmakeraw(&newtio);
  83. /* set speed */
  84. (void)cfsetospeed(&newtio, BAUDRATE);
  85. /* Clear the modem line and activate the settings for the port. */
  86. (void)tcflush(fd_out, TCIFLUSH);
  87. if (tcsetattr(fd_out, TCSANOW, &newtio) != 0) {
  88. (void)fprintf(stderr, "gpspipe: error configuring serial port\n");
  89. exit(EXIT_FAILURE);
  90. }
  91. #endif /* HAVE_TERMIOS_H */
  92. }
  93. static void usage(void)
  94. {
  95. (void)fprintf(stderr,
  96. "Usage: gpspipe [OPTIONS] [server[:port[:device]]]\n\n"
  97. "-2 Set the split24 flag.\n"
  98. "-d Run as a daemon.\n"
  99. "-h Show this help.\n"
  100. "-l Sleep for ten seconds before connecting to gpsd.\n"
  101. "-n [count] exit after count packets.\n"
  102. "-o [file] Write output to file.\n"
  103. "-P Include PPS JSON in NMEA or raw mode.\n"
  104. "-p Include profiling info in the JSON.\n"
  105. "-r Dump raw NMEA.\n"
  106. "-R Dump super-raw mode (GPS binary).\n"
  107. "-s [serial dev] emulate a 4800bps NMEA GPS on serial port (use with '-r').\n"
  108. "-S Set scaled flag. For AIS and subframe data.\n"
  109. "-T [format] set the timestamp format (strftime(3)-like; implies '-t')\n"
  110. "-t Time stamp the data.\n"
  111. "-u usec time stamp, implies -t. Use -uu to output sec.usec\n"
  112. "-v Print a little spinner.\n"
  113. "-V Print version and exit.\n"
  114. "-w Dump gpsd native data.\n"
  115. "-x [seconds] Exit after given delay.\n"
  116. "-Z sets the timestamp format iso8601: implies '-t'\n"
  117. "You must specify one, or more, of -r, -R, or -w\n"
  118. "You must use -o if you use -d.\n");
  119. }
  120. int main(int argc, char **argv)
  121. {
  122. char buf[4096];
  123. bool timestamp = false;
  124. bool iso8601 = false;
  125. char *format = "%F %T";
  126. char *zulu_format = "%FT%T";
  127. char tmstr[200];
  128. bool daemonize = false;
  129. bool binary = false;
  130. bool sleepy = false;
  131. bool new_line = true;
  132. bool raw = false;
  133. bool watch = false;
  134. bool profile = false;
  135. int option_u = 0; // option to show uSeconds
  136. long count = -1;
  137. time_t exit_timer = 0;
  138. int option;
  139. unsigned int vflag = 0, l = 0;
  140. FILE *fp;
  141. unsigned int flags;
  142. fd_set fds;
  143. struct fixsource_t source;
  144. char *serialport = NULL;
  145. char *outfile = NULL;
  146. flags = WATCH_ENABLE;
  147. while ((option = getopt(argc, argv,
  148. "2?dD:hln:o:pPrRwSs:tT:uvVx:Z")) != -1) {
  149. switch (option) {
  150. case '2':
  151. flags |= WATCH_SPLIT24;
  152. break;
  153. case 'D':
  154. debug = atoi(optarg);
  155. #ifdef CLIENTDEBUG_ENABLE
  156. gps_enable_debug(debug, stderr);
  157. #endif /* CLIENTDEBUG_ENABLE */
  158. break;
  159. case 'd':
  160. daemonize = true;
  161. break;
  162. case 'l':
  163. sleepy = true;
  164. break;
  165. case 'n':
  166. count = strtol(optarg, 0, 0);
  167. break;
  168. case 'o':
  169. outfile = optarg;
  170. break;
  171. case 'P':
  172. flags |= WATCH_PPS;
  173. break;
  174. case 'p':
  175. profile = true;
  176. break;
  177. case 'R':
  178. flags |= WATCH_RAW;
  179. binary = true;
  180. break;
  181. case 'r':
  182. raw = true;
  183. /*
  184. * Yes, -r invokes NMEA mode rather than proper raw mode.
  185. * This emulates the behavior under the old protocol.
  186. */
  187. flags |= WATCH_NMEA;
  188. break;
  189. case 'S':
  190. flags |= WATCH_SCALED;
  191. break;
  192. case 's':
  193. serialport = optarg;
  194. break;
  195. case 'T':
  196. timestamp = true;
  197. format = optarg;
  198. break;
  199. case 't':
  200. timestamp = true;
  201. break;
  202. case 'u':
  203. timestamp = true;
  204. option_u++;
  205. break;
  206. case 'V':
  207. (void)fprintf(stderr, "%s: %s (revision %s)\n",
  208. argv[0], VERSION, REVISION);
  209. exit(EXIT_SUCCESS);
  210. case 'v':
  211. vflag++;
  212. break;
  213. case 'w':
  214. flags |= WATCH_JSON;
  215. watch = true;
  216. break;
  217. case 'x':
  218. exit_timer = time(NULL) + strtol(optarg, 0, 0);
  219. break;
  220. case 'Z':
  221. timestamp = true;
  222. format = zulu_format;
  223. iso8601 = true;
  224. break;
  225. case '?':
  226. case 'h':
  227. default:
  228. usage();
  229. exit(EXIT_FAILURE);
  230. }
  231. }
  232. /* Grok the server, port, and device. */
  233. if (optind < argc) {
  234. gpsd_source_spec(argv[optind], &source);
  235. } else
  236. gpsd_source_spec(NULL, &source);
  237. if (serialport != NULL && !raw) {
  238. (void)fprintf(stderr, "gpspipe: use of '-s' requires '-r'.\n");
  239. exit(EXIT_FAILURE);
  240. }
  241. if (outfile == NULL && daemonize) {
  242. (void)fprintf(stderr, "gpspipe: use of '-d' requires '-o'.\n");
  243. exit(EXIT_FAILURE);
  244. }
  245. if (!raw && !watch && !binary) {
  246. (void)fprintf(stderr,
  247. "gpspipe: one of '-R', '-r', or '-w' is required.\n");
  248. exit(EXIT_FAILURE);
  249. }
  250. /* Daemonize if the user requested it. */
  251. if (daemonize)
  252. if (os_daemon(0, 0) != 0)
  253. (void)fprintf(stderr,
  254. "gpspipe: daemonization failed: %s\n",
  255. strerror(errno));
  256. /* Sleep for ten seconds if the user requested it. */
  257. if (sleepy)
  258. (void)sleep(10);
  259. /* Open the output file if the user requested it. If the user
  260. * requested '-R', we use the 'b' flag in fopen() to "do the right
  261. * thing" in non-linux/unix OSes. */
  262. if (outfile == NULL) {
  263. fp = stdout;
  264. } else {
  265. if (binary)
  266. fp = fopen(outfile, "wb");
  267. else
  268. fp = fopen(outfile, "w");
  269. if (fp == NULL) {
  270. (void)fprintf(stderr,
  271. "gpspipe: unable to open output file: %s\n",
  272. outfile);
  273. exit(EXIT_FAILURE);
  274. }
  275. }
  276. /* Open the serial port and set it up. */
  277. if (serialport)
  278. open_serial(serialport);
  279. if (gps_open(source.server, source.port, &gpsdata) != 0) {
  280. (void)fprintf(stderr,
  281. "gpspipe: could not connect to gpsd %s:%s, %s(%d)\n",
  282. source.server, source.port, gps_errstr(errno), errno);
  283. exit(EXIT_FAILURE);
  284. }
  285. if (profile)
  286. flags |= WATCH_TIMING;
  287. if (source.device != NULL)
  288. flags |= WATCH_DEVICE;
  289. (void)gps_stream(&gpsdata, flags, source.device);
  290. if ((isatty(STDERR_FILENO) == 0) || daemonize)
  291. vflag = 0;
  292. for (;;) {
  293. int r = 0;
  294. struct timespec tv;
  295. tv.tv_sec = 0;
  296. tv.tv_nsec = 100000000;
  297. FD_ZERO(&fds);
  298. FD_SET(gpsdata.gps_fd, &fds);
  299. errno = 0;
  300. r = pselect(gpsdata.gps_fd+1, &fds, NULL, NULL, &tv, NULL);
  301. if (r >= 0 && exit_timer && time(NULL) >= exit_timer)
  302. break;
  303. if (r == -1 && errno != EINTR) {
  304. (void)fprintf(stderr, "gpspipe: select error %s(%d)\n",
  305. strerror(errno), errno);
  306. exit(EXIT_FAILURE);
  307. } else if (r == 0)
  308. continue;
  309. if (vflag)
  310. spinner(vflag, l++);
  311. /* reading directly from the socket avoids decode overhead */
  312. errno = 0;
  313. r = (int)recv(gpsdata.gps_fd, buf, sizeof(buf), 0);
  314. if (r > 0) {
  315. int i = 0;
  316. int j = 0;
  317. for (i = 0; i < r; i++) {
  318. char c = buf[i];
  319. if (j < (int)(sizeof(serbuf) - 1)) {
  320. serbuf[j++] = buf[i];
  321. }
  322. if (new_line && timestamp) {
  323. char tmstr_u[40]; // time with "usec" resolution
  324. struct timespec now;
  325. struct tm tmp_now;
  326. int written;
  327. (void)clock_gettime(CLOCK_REALTIME, &now);
  328. (void)gmtime_r((time_t *)&(now.tv_sec), &tmp_now);
  329. (void)strftime(tmstr, sizeof(tmstr), format, &tmp_now);
  330. new_line = 0;
  331. switch( option_u ) {
  332. case 2:
  333. if(iso8601){
  334. written = strlen(tmstr);
  335. tmstr[written] = 'Z';
  336. tmstr[written+1] = '\0';
  337. }
  338. (void)snprintf(tmstr_u, sizeof(tmstr_u),
  339. " %lld.%06ld",
  340. (long long)now.tv_sec,
  341. (long)now.tv_nsec/1000);
  342. break;
  343. case 1:
  344. written = snprintf(tmstr_u, sizeof(tmstr_u),
  345. ".%06ld", (long)now.tv_nsec/1000);
  346. if((0 < written) && (40 > written) && iso8601){
  347. tmstr_u[written-1] = 'Z';
  348. tmstr_u[written] = '\0';
  349. }
  350. break;
  351. default:
  352. *tmstr_u = '\0';
  353. break;
  354. }
  355. if (fprintf(fp, "%.24s%s: ", tmstr, tmstr_u) <= 0) {
  356. (void)fprintf(stderr,
  357. "gpspipe: write error, %s(%d)\n",
  358. strerror(errno), errno);
  359. exit(EXIT_FAILURE);
  360. }
  361. }
  362. if (fputc(c, fp) == EOF) {
  363. (void)fprintf(stderr, "gpspipe: write error, %s(%d)\n",
  364. strerror(errno), errno);
  365. exit(EXIT_FAILURE);
  366. }
  367. if (c == '\n') {
  368. if (serialport != NULL) {
  369. if (write(fd_out, serbuf, (size_t) j) == -1) {
  370. (void)fprintf(stderr,
  371. "gpspipe: serial port write error,"
  372. " %s(%d)\n",
  373. strerror(errno), errno);
  374. exit(EXIT_FAILURE);
  375. }
  376. j = 0;
  377. }
  378. new_line = true;
  379. /* flush after every good line */
  380. if (fflush(fp)) {
  381. (void)fprintf(stderr,
  382. "gpspipe: fflush error, %s(%d)\n",
  383. strerror(errno), errno);
  384. exit(EXIT_FAILURE);
  385. }
  386. if (count > 0) {
  387. if (0 >= --count) {
  388. /* completed count */
  389. exit(EXIT_SUCCESS);
  390. }
  391. }
  392. }
  393. }
  394. } else {
  395. if (r == -1) {
  396. if (errno == EAGAIN)
  397. continue;
  398. else
  399. (void)fprintf(stderr, "gpspipe: read error %s(%d)\n",
  400. strerror(errno), errno);
  401. exit(EXIT_FAILURE);
  402. } else {
  403. exit(EXIT_SUCCESS);
  404. }
  405. }
  406. }
  407. #ifdef __UNUSED__
  408. if (serialport != NULL) {
  409. /* Restore the old serial port settings. */
  410. if (tcsetattr(fd_out, TCSANOW, &oldtio) != 0) {
  411. (void)fprintf(stderr, "gpsipe: error restoring serial port settings\n");
  412. exit(EXIT_FAILURE);
  413. }
  414. }
  415. #endif /* __UNUSED__ */
  416. exit(EXIT_SUCCESS);
  417. }
  418. static void spinner(unsigned int v, unsigned int num)
  419. {
  420. char *spin = "|/-\\";
  421. (void)fprintf(stderr, "\010%c", spin[(num / (1 << (v - 1))) % 4]);
  422. (void)fflush(stderr);
  423. return;
  424. }