gps_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This file is Copyright 2019 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. */
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include "gps.h"
  12. int main (int argc, char **argv) {
  13. struct gps_data_t gps_data;
  14. int gpsopen = -2;
  15. /* debug
  16. * FILE *fp = fopen("/data/bin/gpslog", "a+");
  17. * gps_enable_debug(3, fp);
  18. */
  19. printf("Usage: gpsdtest [host]\n\n");
  20. memset(&gps_data, 0, sizeof(gps_data));
  21. while (1) {
  22. if (gpsopen < 0) {
  23. gpsopen = gps_open((argc == 1 ? "localhost" : argv[1]),
  24. "2947", &gps_data);
  25. if (0 == gpsopen) {
  26. printf("gps_open returned 0 (success)\n");
  27. gps_stream(&gps_data, WATCH_ENABLE, NULL);
  28. } else {
  29. printf("gps_open failed, returned: %d\n", gpsopen);
  30. gpsopen = -1;
  31. sleep(5);
  32. continue;
  33. }
  34. }
  35. if (gps_waiting (&gps_data, 2000000)) {
  36. errno = 0;
  37. if (gps_read (&gps_data, NULL, 0) != -1) {
  38. if (gps_data.status >= 1 && gps_data.fix.mode >= 2){
  39. printf("\nHave a fix: ");
  40. if (gps_data.fix.mode == 2)
  41. printf("2D\n");
  42. else
  43. printf("3D\n");
  44. printf("Latitude: %f\n", gps_data.fix.latitude);
  45. printf("Longitude: %f\n", gps_data.fix.longitude);
  46. printf("Speed: %f\n", gps_data.fix.speed);
  47. printf("Bearing: %f\n", gps_data.fix.track);
  48. printf("H Accuracy: %f\n", gps_data.fix.eph);
  49. printf("S Accuracy: %f\n", gps_data.fix.eps);
  50. printf("B Accuracy: %f\n", gps_data.fix.epd);
  51. printf("Time: %ld\n", (long) gps_data.fix.time);
  52. printf("Altitude: %f\n", gps_data.fix.altitude);
  53. printf("V Accuracy: %f\n\n", gps_data.fix.epv);
  54. }
  55. printf("Satellites visible: %d\n",
  56. gps_data.satellites_visible);
  57. for (int i = 0; i < gps_data.satellites_visible; i++) {
  58. printf("SV type: ");
  59. switch (gps_data.skyview[i].gnssid) {
  60. case 0:
  61. printf("GPS, ");
  62. break;
  63. case 1:
  64. printf("SBAS, ");
  65. break;
  66. case 2:
  67. printf("Galileo, ");
  68. break;
  69. case 3:
  70. printf("Beidou, ");
  71. break;
  72. case 4:
  73. printf("Unknown, ");
  74. break;
  75. case 5:
  76. printf("QZSS, ");
  77. break;
  78. case 6:
  79. printf("Glonass, ");
  80. break;
  81. }
  82. printf("SVID: %d, SNR: %d, Elevation: %d, "
  83. "Azimuth: %d, Used: %d\n",
  84. gps_data.skyview[i].svid,
  85. (int)gps_data.skyview[i].ss,
  86. gps_data.skyview[i].elevation,
  87. gps_data.skyview[i].azimuth,
  88. gps_data.skyview[i].used);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. // vim: set expandtab shiftwidth=4