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.

f90g_track.cc 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Map file reader for F90G Automobile DVR.
  3. Copyright (C) 2014 Jim Keeler, James.L.Keeler@gmail.com
  4. Copyright (C) 2001-2013 Robert Lipe, robertlipe+source@gpsbabel.org
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  16. Read the map file contents picking out the defined record types.
  17. The map file contains a constant 30 byte header record followed by a variable number of
  18. TT records. The TT records start with the two characters "TT" and are 251 bytes long.
  19. The TT records conatain values for time, position and velocity.
  20. */
  21. #include "defs.h"
  22. #include "gbfile.h"
  23. #include <QtCore/QDebug>
  24. #define MYNAME "f90g_track"
  25. #define TTRECORDSIZE 249
  26. #define HEADERRECORDSIZE 30
  27. #define FLIPEDBITS 0xaa
  28. static gbfile* fin = NULL;
  29. static route_head* track = NULL;
  30. static
  31. arglist_t f90g_track_args[] = {
  32. ARG_TERMINATOR
  33. };
  34. /*******************************************************************************
  35. * %%% global callbacks called by gpsbabel main process %%% *
  36. *******************************************************************************/
  37. #define VALIDHEADER "MEDIA 1."
  38. static void
  39. f90g_track_rd_init(const QString& fname)
  40. {
  41. char header[HEADERRECORDSIZE];
  42. fin = gbfopen(fname, "r", MYNAME);
  43. gbfseek(fin, 0, SEEK_SET);
  44. if (gbfread(header, 1, HEADERRECORDSIZE, fin) != HEADERRECORDSIZE) {
  45. fatal(MYNAME ": read error");
  46. } else {
  47. // flip bits and check for valid header
  48. for (int i = 0; i<HEADERRECORDSIZE; i++) {
  49. header[i] ^= FLIPEDBITS;
  50. }
  51. if (memcmp(header, VALIDHEADER, sizeof(VALIDHEADER)-1)) {
  52. fatal(MYNAME ": bad header");
  53. }
  54. // start the track list
  55. track = route_head_alloc();
  56. is_fatal((track == NULL), MYNAME ": memory non-enough");
  57. track->rte_name = fname;
  58. track_add_head(track);
  59. }
  60. }
  61. static void
  62. f90g_track_rd_deinit(void)
  63. {
  64. gbfclose(fin);
  65. }
  66. // needed conversion factors
  67. static const double MIN_PER_DEGREE = 600000.0f;
  68. static const float SPEED_CONVERSION = (10.0f)/(36.0f); // convert KPH to meters per second
  69. static void
  70. f90g_track_read(void)
  71. {
  72. Waypoint* readWaypoint;
  73. char northSouth, eastWest, velocityMark, ttRec[TTRECORDSIZE], tempBuf[20];
  74. int year, mon, mday, hour, min, sec, latitudeDeg, latitudeMin, longitudeDeg, longitudeMin, velocity;
  75. QDateTime dt;
  76. is_fatal((track == NULL), MYNAME "Track setup error");
  77. for (;;) {
  78. if ((gbfread((void*)ttRec, 1, 2, fin) != 2)
  79. || (memcmp(ttRec,"TT",2))) {
  80. break;
  81. }
  82. if (gbfread((void*)ttRec, 1, TTRECORDSIZE, fin) != TTRECORDSIZE) {
  83. break;
  84. }
  85. for (int i = 0; i<TTRECORDSIZE; i++) {
  86. ttRec[i] ^= FLIPEDBITS;
  87. }
  88. // Pick the TT record apart and if it is good, fill in a new Waypoint
  89. year = mon = mday = hour = min = sec = latitudeDeg = latitudeMin = longitudeDeg = longitudeMin = velocity= 0;
  90. // Get the time stamp
  91. sscanf(&ttRec[15],"%4d%2d%2d%2d%2d%2d" ,&year, &mon, &mday, &hour, &min, &sec);
  92. // Get latitude and longitude
  93. sscanf(&ttRec[30],"%1c%2d%6d%1c%3d%6d", &northSouth, &latitudeDeg, &latitudeMin,
  94. &eastWest, &longitudeDeg, &longitudeMin);
  95. // Get velocity (KPH)
  96. sscanf(&ttRec[53],"%1c%3d", &velocityMark, &velocity);
  97. // sanity check the data before committing to the Waypoint
  98. if (year != 0 && (northSouth == 'N' || northSouth == 'S') && (eastWest == 'E' || eastWest == 'W')
  99. && velocityMark == 'M') {
  100. // create the Waypoint and fill it in
  101. readWaypoint = new Waypoint;
  102. dt = QDateTime(QDate(year, mon, mday), QTime(hour, min, sec), Qt::UTC);
  103. readWaypoint->SetCreationTime(dt);
  104. readWaypoint->latitude = (double(latitudeDeg) + double(latitudeMin)/MIN_PER_DEGREE)
  105. * ((northSouth == 'N')? 1.0f : -1.0f);
  106. readWaypoint->longitude = (double(longitudeDeg) + double(longitudeMin)/MIN_PER_DEGREE)
  107. * ((eastWest == 'E')? 1.0f : -1.0f);
  108. // qDebug() << dt.toString() << latitudeDeg << latitudeMin << readWaypoint->latitude;
  109. readWaypoint->speed = float(velocity) * SPEED_CONVERSION;
  110. // Name the Waypoint
  111. snprintf(tempBuf, sizeof(tempBuf), "%2.2dM%2.2dS-%3.3dKPH", min, sec, velocity);
  112. readWaypoint->shortname = QString(tempBuf);
  113. // Add the Waypoint to the current track
  114. track_add_wpt(track, readWaypoint);
  115. }
  116. }
  117. }
  118. // capabilities below means: we can only read trace file.
  119. ff_vecs_t f90g_track_vecs = {
  120. ff_type_file,
  121. { ff_cap_none, (ff_cap)(ff_cap_read), ff_cap_none },
  122. f90g_track_rd_init,
  123. NULL,
  124. f90g_track_rd_deinit,
  125. NULL,
  126. f90g_track_read,
  127. NULL,
  128. NULL,
  129. f90g_track_args,
  130. CET_CHARSET_UTF8, 0 /* ascii is the expected character set */
  131. /* not fixed, can be changed through command line parameter */
  132. };