test_libgps.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * A simple command-line exerciser for the library.
  3. * Not really useful for anything but debugging.
  4. * SPDX-License-Identifier: BSD-2-clause
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <stdarg.h>
  12. #include <ctype.h>
  13. #include "../gps.h"
  14. #include "../libgps.h"
  15. #include "../gpsdclient.h"
  16. #include <unistd.h>
  17. #include <getopt.h>
  18. #include <signal.h>
  19. static void onsig(int sig)
  20. {
  21. (void)fprintf(stderr, "libgps: died with signal %d\n", sig);
  22. exit(EXIT_FAILURE);
  23. }
  24. #ifdef SOCKET_EXPORT_ENABLE
  25. /* must start zeroed, otherwise the unit test will try to chase garbage pointer fields. */
  26. static struct gps_data_t gpsdata;
  27. #endif
  28. int main(int argc, char *argv[])
  29. {
  30. struct gps_data_t collect;
  31. struct fixsource_t source;
  32. char buf[BUFSIZ];
  33. int option;
  34. bool batchmode = false;
  35. bool forwardmode = false;
  36. char *fmsg = NULL;
  37. int debug = 0;
  38. (void)signal(SIGSEGV, onsig);
  39. #ifdef SIGBUS
  40. (void)signal(SIGBUS, onsig);
  41. #endif
  42. while ((option = getopt(argc, argv, "bf:hsD:?")) != -1) {
  43. switch (option) {
  44. case 'b':
  45. batchmode = true;
  46. break;
  47. case 'f':
  48. forwardmode = true;
  49. fmsg = optarg;
  50. break;
  51. case 's':
  52. (void)
  53. printf("Sizes: fix=%zd gpsdata=%zd rtcm2=%zd rtcm3=%zd "
  54. "ais=%zd compass=%zd raw=%zd devices=%zd policy=%zd "
  55. "version=%zd, noise=%zd\n",
  56. sizeof(struct gps_fix_t),
  57. sizeof(struct gps_data_t), sizeof(struct rtcm2_t),
  58. sizeof(struct rtcm3_t), sizeof(struct ais_t),
  59. sizeof(struct attitude_t), sizeof(struct rawdata_t),
  60. sizeof(collect.devices), sizeof(struct gps_policy_t),
  61. sizeof(struct version_t), sizeof(struct gst_t));
  62. exit(EXIT_SUCCESS);
  63. case 'D':
  64. debug = atoi(optarg);
  65. break;
  66. case '?':
  67. case 'h':
  68. default:
  69. (void)fputs("usage: test_libgps [-b] [-f fwdmsg] [-D lvl] [-s] [server[:port:[device]]]\n", stderr);
  70. exit(EXIT_FAILURE);
  71. }
  72. }
  73. /* Grok the server, port, and device. */
  74. if (optind < argc) {
  75. gpsd_source_spec(argv[optind], &source);
  76. } else
  77. gpsd_source_spec(NULL, &source);
  78. gps_enable_debug(debug, stdout);
  79. if (batchmode) {
  80. #ifdef SOCKET_EXPORT_ENABLE
  81. while (fgets(buf, sizeof(buf), stdin) != NULL) {
  82. if (buf[0] == '{' || isalpha( (int) buf[0])) {
  83. gps_unpack(buf, &gpsdata);
  84. libgps_dump_state(&gpsdata);
  85. }
  86. }
  87. #endif
  88. } else if (gps_open(source.server, source.port, &collect) != 0) {
  89. (void)fprintf(stderr,
  90. "test_libgps: no gpsd running or network error: %d, %s\n",
  91. errno, gps_errstr(errno));
  92. exit(EXIT_FAILURE);
  93. } else if (forwardmode) {
  94. if (gps_send(&collect, fmsg) == -1) {
  95. (void)fprintf(stderr,
  96. "test_libgps: gps send error: %d, %s\n",
  97. errno, gps_errstr(errno));
  98. }
  99. if (gps_read(&collect, NULL, 0) == -1) {
  100. (void)fprintf(stderr,
  101. "test_libgps: gps read error: %d, %s\n",
  102. errno, gps_errstr(errno));
  103. }
  104. #ifdef SOCKET_EXPORT_ENABLE
  105. libgps_dump_state(&collect);
  106. #endif
  107. (void)gps_close(&collect);
  108. } else {
  109. int tty = isatty(0);
  110. if (tty)
  111. (void)fputs("This is the gpsd exerciser.\n", stdout);
  112. for (;;) {
  113. if (tty)
  114. (void)fputs("> ", stdout);
  115. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  116. if (tty)
  117. putchar('\n');
  118. break;
  119. }
  120. collect.set = 0;
  121. (void)gps_send(&collect, buf);
  122. (void)gps_read(&collect, NULL, 0);
  123. #ifdef SOCKET_EXPORT_ENABLE
  124. libgps_dump_state(&collect);
  125. #endif
  126. }
  127. (void)gps_close(&collect);
  128. }
  129. return 0;
  130. }