test_gpsmm.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2010 Eric S. Raymond.
  3. *
  4. * This software is distributed under a BSD-style license. See the
  5. * file "COPYING" in the top-level directory of the distribution for details.
  6. *
  7. */
  8. /* This simple program shows the basic functionality of the C++ wrapper class */
  9. #include <iostream>
  10. #include <getopt.h>
  11. #include "../libgpsmm.h"
  12. #include "../timespec.h"
  13. #include "../timespec_str.c"
  14. #include "../gpsdclient.c"
  15. /* YES ---> ^^^^
  16. Using .c rather than the .h to embed gpsd_source_spec() source here
  17. so that it is compiled in C++ rather than C of the gps library
  18. (otherwise fails to link as the signatures are unavailable/different)
  19. */
  20. using namespace std;
  21. void printfinite(double val)
  22. {
  23. if (0 == isfinite(val))
  24. (void)fputs(" N/A ", stdout);
  25. else
  26. (void)fprintf(stdout, "%6.2f ", val);
  27. }
  28. /*
  29. * We should get libgps_dump_state() from the client library, but
  30. * scons has a bug; we can't get it to add -lgps to the link line,
  31. * apparently because it doesn't honor parse_flags on a Program()
  32. * build of a C++ file.
  33. */
  34. static void libgps_dump_state(struct gps_data_t *collect)
  35. {
  36. char ts_str[TIMESPEC_LEN];
  37. /* no need to dump the entire state, this is a sanity check */
  38. #ifndef USE_QT
  39. (void)fprintf(stdout, "flags: (0x%04x) %s\n",
  40. (unsigned int)collect->set, gps_maskdump(collect->set));
  41. #endif
  42. if (collect->set & ONLINE_SET)
  43. (void)fprintf(stdout, "ONLINE: %s\n",
  44. timespec_str(&collect->online, ts_str, sizeof(ts_str)));
  45. if (collect->set & TIME_SET)
  46. (void)fprintf(stdout, "TIME: %s\n",
  47. timespec_str(&collect->fix.time, ts_str, sizeof(ts_str)));
  48. if (collect->set & LATLON_SET)
  49. (void)fprintf(stdout, "LATLON: lat/lon: %lf %lf\n",
  50. collect->fix.latitude, collect->fix.longitude);
  51. if (collect->set & ALTITUDE_SET)
  52. (void)fprintf(stdout, "ALTITUDE: altHAE: %lf U: climb: %lf\n",
  53. collect->fix.altHAE, collect->fix.climb);
  54. if (collect->set & SPEED_SET)
  55. (void)fprintf(stdout, "SPEED: %lf\n", collect->fix.speed);
  56. if (collect->set & TRACK_SET)
  57. (void)fprintf(stdout, "TRACK: track: %lf\n", collect->fix.track);
  58. if (collect->set & CLIMB_SET)
  59. (void)fprintf(stdout, "CLIMB: climb: %lf\n", collect->fix.climb);
  60. if (collect->set & STATUS_SET)
  61. (void)fprintf(stdout, "STATUS: status: %d\n", collect->status);
  62. if (collect->set & MODE_SET)
  63. (void)fprintf(stdout, "MODE: mode: %d\n", collect->fix.mode);
  64. if (collect->set & DOP_SET)
  65. (void)fprintf(stdout,
  66. "DOP: satellites %d, pdop=%lf, hdop=%lf, vdop=%lf\n",
  67. collect->satellites_used, collect->dop.pdop,
  68. collect->dop.hdop, collect->dop.vdop);
  69. if (collect->set & VERSION_SET)
  70. (void)fprintf(stdout, "VERSION: release=%s rev=%s proto=%d.%d\n",
  71. collect->version.release,
  72. collect->version.rev,
  73. collect->version.proto_major,
  74. collect->version.proto_minor);
  75. if (collect->set & POLICY_SET)
  76. (void)fprintf(stdout,
  77. "POLICY: watcher=%s nmea=%s raw=%d scaled=%s timing=%s, devpath=%s\n",
  78. collect->policy.watcher ? "true" : "false",
  79. collect->policy.nmea ? "true" : "false",
  80. collect->policy.raw,
  81. collect->policy.scaled ? "true" : "false",
  82. collect->policy.timing ? "true" : "false",
  83. collect->policy.devpath);
  84. if (collect->set & SATELLITE_SET) {
  85. int i;
  86. (void)fprintf(stdout, "SKY: satellites in view: %d\n",
  87. collect->satellites_visible);
  88. for (i = 0; i < collect->satellites_visible; i++) {
  89. (void)fprintf(stdout, " %3d", collect->skyview[i].PRN);
  90. printfinite(collect->skyview[i].elevation);
  91. printfinite(collect->skyview[i].azimuth);
  92. printfinite(collect->skyview[i].ss);
  93. (void)fprintf(stdout, collect->skyview[i].used ? " Y\n" : " N\n");
  94. }
  95. }
  96. if (collect->set & DEVICE_SET)
  97. (void)fprintf(stdout, "DEVICE: Device is '%s', driver is '%s'\n",
  98. collect->dev.path, collect->dev.driver);
  99. #ifdef OLDSTYLE_ENABLE
  100. if (collect->set & DEVICEID_SET)
  101. (void)fprintf(stdout, "GPSD ID is %s\n", collect->dev.subtype);
  102. #endif /* OLDSTYLE_ENABLE */
  103. if (collect->set & DEVICELIST_SET) {
  104. int i;
  105. (void)fprintf(stdout, "DEVICELIST:%d devices:\n",
  106. collect->devices.ndevices);
  107. for (i = 0; i < collect->devices.ndevices; i++) {
  108. (void)fprintf(stdout, "%d: path='%s' driver='%s'\n",
  109. collect->devices.ndevices,
  110. collect->devices.list[i].path,
  111. collect->devices.list[i].driver);
  112. }
  113. }
  114. }
  115. int main(int argc, char *argv[])
  116. {
  117. uint looper = UINT_MAX;
  118. // A typical C++ program may look to use a more native option parsing method
  119. // such as boost::program_options
  120. // But for this test program we don't want extra dependencies
  121. // Hence use C style getopt for (build) simplicity
  122. int option;
  123. while ((option = getopt(argc, argv, "l:h?")) != -1) {
  124. switch (option) {
  125. case 'l':
  126. looper = atoi(optarg);
  127. break;
  128. case '?':
  129. case 'h':
  130. default:
  131. cout << "usage: " << argv[0] << " [-l n]\n";
  132. exit(EXIT_FAILURE);
  133. break;
  134. }
  135. }
  136. struct fixsource_t source;
  137. /* Grok the server, port, and device. */
  138. if (optind < argc) {
  139. gpsd_source_spec(argv[optind], &source);
  140. } else
  141. gpsd_source_spec(NULL, &source);
  142. //gpsmm gps_rec("localhost", DEFAULT_GPSD_PORT);
  143. gpsmm gps_rec(source.server, source.port);
  144. if ( !((std::string)source.server == (std::string)GPSD_SHARED_MEMORY ||
  145. (std::string)source.server == (std::string)GPSD_DBUS_EXPORT) ) {
  146. if (gps_rec.stream(WATCH_ENABLE|WATCH_JSON) == NULL) {
  147. cerr << "No GPSD running.\n";
  148. return 1;
  149. }
  150. }
  151. // Loop for the specified number of times
  152. // If not specified then by default it loops until ll simply goes out of bounds
  153. // So with the 5 second wait & a 4 byte uint - this equates to ~680 years
  154. // - long enough for a test program :)
  155. for (uint ll = 0; ll < looper; ll++) {
  156. struct gps_data_t* newdata;
  157. if (!gps_rec.waiting(5000000))
  158. continue;
  159. if ((newdata = gps_rec.read()) == NULL) {
  160. cerr << "Read error.\n";
  161. return 1;
  162. } else {
  163. libgps_dump_state(newdata);
  164. }
  165. }
  166. cout << "Exiting\n";
  167. return 0;
  168. }