Devuan fork of gpsd
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

154 lines
3.6 KiB

  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. #ifdef CLIENTDEBUG_ENABLE
  38. int debug = 0;
  39. #endif
  40. (void)signal(SIGSEGV, onsig);
  41. #ifdef SIGBUS
  42. (void)signal(SIGBUS, onsig);
  43. #endif
  44. while ((option = getopt(argc, argv, "bf:hsD:?")) != -1) {
  45. switch (option) {
  46. case 'b':
  47. batchmode = true;
  48. break;
  49. case 'f':
  50. forwardmode = true;
  51. fmsg = optarg;
  52. break;
  53. case 's':
  54. (void)
  55. printf("Sizes: fix=%zd gpsdata=%zd rtcm2=%zd rtcm3=%zd "
  56. "ais=%zd compass=%zd raw=%zd devices=%zd policy=%zd "
  57. "version=%zd, noise=%zd\n",
  58. sizeof(struct gps_fix_t),
  59. sizeof(struct gps_data_t), sizeof(struct rtcm2_t),
  60. sizeof(struct rtcm3_t), sizeof(struct ais_t),
  61. sizeof(struct attitude_t), sizeof(struct rawdata_t),
  62. sizeof(collect.devices), sizeof(struct gps_policy_t),
  63. sizeof(struct version_t), sizeof(struct gst_t));
  64. exit(EXIT_SUCCESS);
  65. #ifdef CLIENTDEBUG_ENABLE
  66. case 'D':
  67. debug = atoi(optarg);
  68. break;
  69. #endif
  70. case '?':
  71. case 'h':
  72. default:
  73. (void)fputs("usage: test_libgps [-b] [-f fwdmsg] [-D lvl] [-s] [server[:port:[device]]]\n", stderr);
  74. exit(EXIT_FAILURE);
  75. }
  76. }
  77. /* Grok the server, port, and device. */
  78. if (optind < argc) {
  79. gpsd_source_spec(argv[optind], &source);
  80. } else
  81. gpsd_source_spec(NULL, &source);
  82. #ifdef CLIENTDEBUG_ENABLE
  83. gps_enable_debug(debug, stdout);
  84. #endif
  85. if (batchmode) {
  86. #ifdef SOCKET_EXPORT_ENABLE
  87. while (fgets(buf, sizeof(buf), stdin) != NULL) {
  88. if (buf[0] == '{' || isalpha( (int) buf[0])) {
  89. gps_unpack(buf, &gpsdata);
  90. #ifdef LIBGPS_DEBUG
  91. libgps_dump_state(&gpsdata);
  92. #endif
  93. }
  94. }
  95. #endif
  96. } else if (gps_open(source.server, source.port, &collect) != 0) {
  97. (void)fprintf(stderr,
  98. "test_libgps: no gpsd running or network error: %d, %s\n",
  99. errno, gps_errstr(errno));
  100. exit(EXIT_FAILURE);
  101. } else if (forwardmode) {
  102. if (gps_send(&collect, fmsg) == -1) {
  103. (void)fprintf(stderr,
  104. "test_libgps: gps send error: %d, %s\n",
  105. errno, gps_errstr(errno));
  106. }
  107. if (gps_read(&collect, NULL, 0) == -1) {
  108. (void)fprintf(stderr,
  109. "test_libgps: gps read error: %d, %s\n",
  110. errno, gps_errstr(errno));
  111. }
  112. #ifdef SOCKET_EXPORT_ENABLE
  113. #ifdef LIBGPS_DEBUG
  114. libgps_dump_state(&collect);
  115. #endif
  116. #endif
  117. (void)gps_close(&collect);
  118. } else {
  119. int tty = isatty(0);
  120. if (tty)
  121. (void)fputs("This is the gpsd exerciser.\n", stdout);
  122. for (;;) {
  123. if (tty)
  124. (void)fputs("> ", stdout);
  125. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  126. if (tty)
  127. putchar('\n');
  128. break;
  129. }
  130. collect.set = 0;
  131. (void)gps_send(&collect, buf);
  132. (void)gps_read(&collect, NULL, 0);
  133. #ifdef SOCKET_EXPORT_ENABLE
  134. #ifdef LIBGPS_DEBUG
  135. libgps_dump_state(&collect);
  136. #endif
  137. #endif
  138. }
  139. (void)gps_close(&collect);
  140. }
  141. return 0;
  142. }