gpxlogger.c 11 KB

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