test_libgps.c 3.5 KB

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