gpxlogger.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * This file is Copyright 2010 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. */
  5. #include "../include/gpsd_config.h" /* must be before all includes */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <libgen.h>
  9. #include <math.h>
  10. #ifdef HAVE_GETOPT_LONG
  11. #include <getopt.h> // for getopt_long()
  12. #endif
  13. #include <signal.h>
  14. #include <stdbool.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include "../include/gps.h"
  21. #include "../include/gpsdclient.h"
  22. #include "../include/os_compat.h"
  23. #include "../include/timespec.h"
  24. static char *progname;
  25. static struct fixsource_t source;
  26. /**************************************************************************
  27. *
  28. * Transport-layer-independent functions
  29. *
  30. **************************************************************************/
  31. static struct gps_data_t gpsdata;
  32. static FILE *logfile;
  33. static bool intrack = false;
  34. static time_t timeout = 5; /* seconds */
  35. static double minmove = 0; /* meters */
  36. static int debug;
  37. static void print_gpx_header(void)
  38. {
  39. char tbuf[CLIENT_DATE_MAX+1];
  40. (void)fprintf(logfile, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  41. (void)fprintf(logfile, "<gpx version=\"1.1\" creator=\"GPSD %s - %s\"\n",
  42. VERSION, GPSD_URL);
  43. (void)fprintf(logfile,
  44. " xmlns:xsi=\"https://www.w3.org/2001/XMLSchema-instance\"\n");
  45. (void)fprintf(logfile,
  46. " xmlns=\"http://www.topografix.com/GPX/1/1\"\n");
  47. (void)fprintf(logfile
  48. ," xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1\n");
  49. (void)fprintf(logfile
  50. ," http://www.topografix.com/GPX/1/1/gpx.xsd\">\n");
  51. (void)fprintf(logfile, " <metadata>\n");
  52. (void)fprintf(logfile, " <time>%s</time>\n",
  53. now_to_iso8601(tbuf, sizeof(tbuf)));
  54. (void)fprintf(logfile," </metadata>\n");
  55. (void)fflush(logfile);
  56. }
  57. static void print_gpx_trk_end(void)
  58. {
  59. (void)fprintf(logfile," </trkseg>\n");
  60. (void)fprintf(logfile," </trk>\n");
  61. (void)fflush(logfile);
  62. }
  63. static void print_gpx_footer(void)
  64. {
  65. if (intrack)
  66. print_gpx_trk_end();
  67. (void)fprintf(logfile,"</gpx>\n");
  68. (void)fclose(logfile);
  69. }
  70. static void print_gpx_trk_start(void)
  71. {
  72. (void)fprintf(logfile," <trk>\n");
  73. (void)fprintf(logfile," <src>GPSD %s</src>\n", VERSION);
  74. (void)fprintf(logfile," <trkseg>\n");
  75. (void)fflush(logfile);
  76. }
  77. static void print_fix(struct gps_data_t *gpsdata, timespec_t ts_time)
  78. {
  79. char tbuf[CLIENT_DATE_MAX+1];
  80. (void)fprintf(logfile," <trkpt lat=\"%.9f\" lon=\"%.9f\">\n",
  81. gpsdata->fix.latitude, gpsdata->fix.longitude);
  82. /*
  83. * From the specification at https://www.topografix.com/GPX/1/1/gpx.xsd
  84. * the <ele> tag is defined as "Elevation (in meters) of the point."
  85. * This is ambiguous between HAE and orthometric height (above geoid,
  86. * aka MSL).
  87. * gpsd has historically used HAE and MSL randomly for altitude.
  88. * gpsd now explicitly supports distinct HAE and MSL.
  89. */
  90. if ((isfinite(gpsdata->fix.altHAE) != 0))
  91. (void)fprintf(logfile," <ele>%.4f</ele>\n", gpsdata->fix.altHAE);
  92. (void)fprintf(logfile," <time>%s</time>\n",
  93. timespec_to_iso8601(ts_time, tbuf, sizeof(tbuf)));
  94. if (gpsdata->fix.status == STATUS_DGPS_FIX)
  95. (void)fprintf(logfile," <fix>dgps</fix>\n");
  96. else
  97. switch (gpsdata->fix.mode) {
  98. case MODE_3D:
  99. (void)fprintf(logfile," <fix>3d</fix>\n");
  100. break;
  101. case MODE_2D:
  102. (void)fprintf(logfile," <fix>2d</fix>\n");
  103. break;
  104. case MODE_NO_FIX:
  105. (void)fprintf(logfile," <fix>none</fix>\n");
  106. break;
  107. default:
  108. /* don't print anything if no fix indicator */
  109. break;
  110. }
  111. if ((gpsdata->fix.mode > MODE_NO_FIX) && (gpsdata->satellites_used > 0))
  112. (void)fprintf(logfile," <sat>%d</sat>\n", gpsdata->satellites_used);
  113. if (isfinite(gpsdata->dop.hdop) != 0)
  114. (void)fprintf(logfile," <hdop>%.1f</hdop>\n", gpsdata->dop.hdop);
  115. if (isfinite(gpsdata->dop.vdop) != 0)
  116. (void)fprintf(logfile," <vdop>%.1f</vdop>\n", gpsdata->dop.vdop);
  117. if (isfinite(gpsdata->dop.pdop) != 0)
  118. (void)fprintf(logfile," <pdop>%.1f</pdop>\n", gpsdata->dop.pdop);
  119. (void)fprintf(logfile," </trkpt>\n");
  120. (void)fflush(logfile);
  121. }
  122. static void conditionally_log_fix(struct gps_data_t *gpsdata)
  123. {
  124. static timespec_t ts_time, old_ts_time, ts_diff;
  125. static double old_lat, old_lon;
  126. static bool first = true;
  127. ts_time = gpsdata->fix.time;
  128. if (TS_EQ(&ts_time, &old_ts_time) || gpsdata->fix.mode < MODE_2D)
  129. return;
  130. /* may not be worth logging if we've moved only a very short distance */
  131. if (0 < minmove && !first && earth_distance(
  132. gpsdata->fix.latitude,
  133. gpsdata->fix.longitude,
  134. old_lat, old_lon) < minmove)
  135. return;
  136. /*
  137. * Make new track if the jump in time is above
  138. * timeout. Handle jumps both forward and
  139. * backwards in time. The clock sometimes jumps
  140. * backward when gpsd is submitting junk on the
  141. * dbus.
  142. */
  143. TS_SUB(&ts_diff, &ts_time, &old_ts_time);
  144. if (labs((long)ts_diff.tv_sec) > timeout && !first) {
  145. print_gpx_trk_end();
  146. intrack = false;
  147. }
  148. if (!intrack) {
  149. print_gpx_trk_start();
  150. intrack = true;
  151. if (first)
  152. first = false;
  153. }
  154. old_ts_time = ts_time;
  155. if (0 < minmove) {
  156. old_lat = gpsdata->fix.latitude;
  157. old_lon = gpsdata->fix.longitude;
  158. }
  159. print_fix(gpsdata, ts_time);
  160. }
  161. static void quit_handler(int signum)
  162. {
  163. /* don't clutter the logs on Ctrl-C */
  164. if (signum != SIGINT)
  165. syslog(LOG_INFO, "exiting, signal %d received", signum);
  166. print_gpx_footer();
  167. (void)gps_close(&gpsdata);
  168. exit(EXIT_SUCCESS);
  169. }
  170. /**************************************************************************
  171. *
  172. * Main sequence
  173. *
  174. **************************************************************************/
  175. static void usage(void)
  176. {
  177. (void)fprintf(stderr,
  178. "Usage: %s [OPTIONS] [server[:port:[device]]]\n\n"
  179. #ifdef HAVE_GETOPT_LONG
  180. " --daemonize Daemonize\n"
  181. " --debug DEBUGLEVEL\n"
  182. " --export EXPORTMETHOD Default %s\n"
  183. " --exports List available exports, then exit\n"
  184. " --help Show this help, then exit\n"
  185. " --interval TIMEOUT\n"
  186. " --minmove MINMOVE\n"
  187. " --output FILNAME\n"
  188. " --reconnect\n"
  189. " --version Show version, then exit\n"
  190. #endif
  191. " -D DEBUGLEVEL\n"
  192. " -d Daemonize\n"
  193. " -e EXPORTMETHOD Default %s \n"
  194. " -f FILENAME\n"
  195. " -h\n"
  196. " -i TIMEOUT Default 5\n"
  197. " -l \n"
  198. " -m MINMOVE\n"
  199. " -r\n"
  200. " -V Show version and exit\n",
  201. progname,
  202. #ifdef HAVE_GETOPT_LONG
  203. export_default()->name,
  204. #endif
  205. export_default()->name);
  206. exit(EXIT_FAILURE);
  207. }
  208. int main(int argc, char **argv)
  209. {
  210. int ch;
  211. bool daemonize = false;
  212. bool reconnect = false;
  213. unsigned int flags = WATCH_ENABLE;
  214. struct exportmethod_t *method = NULL;
  215. const char *optstring = "dD:e:f:hi:lm:rV";
  216. #ifdef HAVE_GETOPT_LONG
  217. int option_index = 0;
  218. static struct option long_options[] = {
  219. {"daemonize", no_argument, NULL, 'd'},
  220. {"debug", required_argument, NULL, 'D'},
  221. {"export", required_argument, NULL, 'e'},
  222. {"exports", required_argument, NULL, 'l'},
  223. {"help", no_argument, NULL, 'h'},
  224. {"interval", required_argument, NULL, 'i'},
  225. {"minmove", required_argument, NULL, 'm'},
  226. {"output", required_argument, NULL, 'f'},
  227. {"reconnect", no_argument, NULL, 'r' },
  228. {"version", no_argument, NULL, 'V' },
  229. {NULL, 0, NULL, 0},
  230. };
  231. #endif
  232. progname = argv[0];
  233. method = export_default();
  234. if (method == NULL) {
  235. (void)fprintf(stderr, "%s: no export methods.\n", progname);
  236. exit(EXIT_FAILURE);
  237. }
  238. logfile = stdout;
  239. while (1) {
  240. #ifdef HAVE_GETOPT_LONG
  241. ch = getopt_long(argc, argv, optstring, long_options, &option_index);
  242. #else
  243. ch = getopt(argc, argv, optstring);
  244. #endif
  245. if (ch == -1) {
  246. break;
  247. }
  248. switch (ch) {
  249. case 'd':
  250. openlog(basename(progname), LOG_PID | LOG_PERROR, LOG_DAEMON);
  251. daemonize = true;
  252. break;
  253. case 'D':
  254. debug = atoi(optarg);
  255. gps_enable_debug(debug, logfile);
  256. break;
  257. case 'e':
  258. method = export_lookup(optarg);
  259. if (method == NULL) {
  260. (void)fprintf(stderr,
  261. "%s: %s is not a known export method.\n",
  262. progname, optarg);
  263. exit(EXIT_FAILURE);
  264. }
  265. break;
  266. case 'f': /* Output file name. */
  267. {
  268. char *fname = NULL;
  269. time_t t;
  270. size_t s = 0;
  271. size_t fnamesize = strlen(optarg) + 1;
  272. t = time(NULL);
  273. while (s == 0) {
  274. char *newfname = realloc(fname, fnamesize);
  275. if (newfname == NULL) {
  276. syslog(LOG_ERR, "realloc failed.");
  277. goto bailout;
  278. } else {
  279. fname = newfname;
  280. }
  281. s = strftime(fname, fnamesize-1, optarg, localtime(&t));
  282. if (!s) {
  283. /* expanded filename did not fit in string, try
  284. * a bigger string */
  285. fnamesize += 1024;
  286. }
  287. }
  288. fname[s] = '\0';;
  289. logfile = fopen(fname, "w");
  290. if (logfile == NULL) {
  291. syslog(LOG_ERR,
  292. "Failed to open %s: %s, logging to stdout.",
  293. fname, strerror(errno));
  294. logfile = stdout;
  295. }
  296. bailout:
  297. free(fname);
  298. break;
  299. }
  300. case 'i': /* set polling interval */
  301. timeout = (time_t) atoi(optarg);
  302. if (timeout < 1)
  303. timeout = 1;
  304. if (timeout >= 3600)
  305. (void)fprintf(stderr,
  306. "WARNING: track timeout is an hour or more!\n");
  307. break;
  308. case 'l':
  309. export_list(stderr);
  310. exit(EXIT_SUCCESS);
  311. case 'm':
  312. minmove = (double )atoi(optarg);
  313. break;
  314. case 'r':
  315. reconnect = true;
  316. break;
  317. case 'V':
  318. (void)fprintf(stderr, "%s: version %s (revision %s)\n",
  319. progname, VERSION, REVISION);
  320. exit(EXIT_SUCCESS);
  321. default:
  322. usage();
  323. /* NOTREACHED */
  324. }
  325. }
  326. if (daemonize && logfile == stdout) {
  327. syslog(LOG_ERR, "Daemon mode with no valid logfile name - exiting.");
  328. exit(EXIT_FAILURE);
  329. }
  330. if (method->magic != NULL) {
  331. source.server = (char *)method->magic;
  332. source.port = NULL;
  333. source.device = NULL;
  334. } else {
  335. source.server = (char *)"localhost";
  336. source.port = (char *)DEFAULT_GPSD_PORT;
  337. source.device = NULL;
  338. }
  339. if (optind < argc) {
  340. /* in this case, switch to the method "socket" always */
  341. gpsd_source_spec(argv[optind], &source);
  342. }
  343. #if 0
  344. (void)fprintf(logfile,"<!-- server: %s port: %s device: %s -->\n",
  345. source.server, source.port, source.device);
  346. #endif
  347. /* catch all interesting signals */
  348. (void)signal(SIGTERM, quit_handler);
  349. (void)signal(SIGQUIT, quit_handler);
  350. (void)signal(SIGINT, quit_handler);
  351. /* might be time to daemonize */
  352. if (daemonize) {
  353. /* not SuS/POSIX portable, but we have our own fallback version */
  354. if (os_daemon(0, 0) != 0)
  355. (void) fprintf(stderr,"daemonization failed: %s\n", strerror(errno));
  356. }
  357. //syslog (LOG_INFO, "---------- STARTED ----------");
  358. if (gps_open(source.server, source.port, &gpsdata) != 0) {
  359. (void)fprintf(stderr,
  360. "%s: no gpsd running or network error: %d, %s\n",
  361. progname, errno, gps_errstr(errno));
  362. exit(EXIT_FAILURE);
  363. }
  364. if (source.device != NULL)
  365. flags |= WATCH_DEVICE;
  366. (void)gps_stream(&gpsdata, flags, source.device);
  367. print_gpx_header();
  368. while (gps_mainloop(&gpsdata, timeout * 1000000, conditionally_log_fix) < 0 &&
  369. reconnect) {
  370. /* avoid busy-calling gps_mainloop() */
  371. (void)sleep(timeout);
  372. syslog(LOG_INFO, "timeout; about to reconnect");
  373. }
  374. print_gpx_footer();
  375. (void)gps_close(&gpsdata);
  376. exit(EXIT_SUCCESS);
  377. }
  378. // vim: set expandtab shiftwidth=4