minmea.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This program is free software. It comes without any warranty, to the extent
  4. * permitted by applicable law. You can redistribute it and/or modify it under
  5. * the terms of the Do What The Fuck You Want To Public License, Version 2, as
  6. * published by Sam Hocevar. See the COPYING file for more details.
  7. */
  8. #ifndef MINMEA_H
  9. #define MINMEA_H
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <math.h>
  19. #include <sys/time.h>
  20. #define MINMEA_MAX_LENGTH 80
  21. enum minmea_sentence_id {
  22. MINMEA_INVALID = -1,
  23. MINMEA_UNKNOWN = 0,
  24. MINMEA_SENTENCE_RMC,
  25. MINMEA_SENTENCE_GGA,
  26. MINMEA_SENTENCE_GSA,
  27. MINMEA_SENTENCE_GST,
  28. };
  29. struct minmea_date {
  30. int day;
  31. int month;
  32. int year;
  33. };
  34. struct minmea_time {
  35. int hours;
  36. int minutes;
  37. int seconds;
  38. int microseconds;
  39. };
  40. struct minmea_sentence_rmc {
  41. struct minmea_time time;
  42. bool valid;
  43. int latitude, latitude_scale;
  44. int longitude, longitude_scale;
  45. int speed, speed_scale;
  46. int course, course_scale;
  47. struct minmea_date date;
  48. int variation, variation_scale;
  49. };
  50. struct minmea_sentence_gga {
  51. struct minmea_time time;
  52. int latitude, latitude_scale;
  53. int longitude, longitude_scale;
  54. int fix_quality;
  55. int satellites_tracked;
  56. int hdop, hdop_scale;
  57. int altitude, altitude_scale; char altitude_units;
  58. int height, height_scale; char height_units;
  59. int dgps_age;
  60. };
  61. struct minmea_sentence_gst {
  62. struct minmea_time time;
  63. int rms_deviation, rms_deviation_scale;
  64. int semi_major_deviation, semi_major_deviation_scale;
  65. int semi_minor_deviation, semi_minor_deviation_scale;
  66. int semi_major_orientation, semi_major_orientation_scale;
  67. int latitude_error_deviation, latitude_error_deviation_scale;
  68. int longitude_error_deviation, longitude_error_deviation_scale;
  69. int altitude_error_deviation, altitude_error_deviation_scale;
  70. };
  71. enum minmea_gsa_mode {
  72. MINMEA_GPGSA_MODE_AUTO = 'A',
  73. MINMEA_GPGSA_MODE_FORCED = 'M',
  74. };
  75. enum minmea_gsa_fix_type {
  76. MINMEA_GPGSA_FIX_NONE = 1,
  77. MINMEA_GPGSA_FIX_2D = 2,
  78. MINMEA_GPGSA_FIX_3D = 3,
  79. };
  80. struct minmea_sentence_gsa {
  81. char mode;
  82. int fix_type;
  83. int sats[12];
  84. int pdop, pdop_scale;
  85. int hdop, hdop_scale;
  86. int vdop, vdop_scale;
  87. };
  88. /**
  89. * Check sentence validity and checksum. Returns true for valid sentences.
  90. */
  91. bool minmea_check(const char *sentence);
  92. /**
  93. * Determine talker identifier.
  94. */
  95. bool minmea_talker_id(char talker[3], const char *sentence);
  96. /**
  97. * Determine sentence identifier.
  98. */
  99. enum minmea_sentence_id minmea_sentence_id(const char *sentence);
  100. /**
  101. * Scanf-like processor for NMEA sentences. Supports the following formats:
  102. * c - single character (char *)
  103. * d - direction, returned as 1/-1, default 0 (int *)
  104. * f - fractional, returned as value + scale (int *, int *)
  105. * i - decimal, default zero (int *)
  106. * s - string (char *)
  107. * t - talker identifier and type (char *)
  108. * T - date/time stamp (int *, int *, int *)
  109. * Returns true on success. See library source code for details.
  110. */
  111. bool minmea_scan(const char *sentence, const char *format, ...);
  112. /*
  113. * Parse a specific type of sentence. Return true on success.
  114. */
  115. bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
  116. bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
  117. bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
  118. bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence);
  119. /**
  120. * Convert GPS UTC date/time representation to a UNIX timestamp.
  121. */
  122. int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time);
  123. /**
  124. * Rescale a fixed-point value to a different scale. Rounds towards zero.
  125. */
  126. static inline int minmea_rescale(int value, int from, int to)
  127. {
  128. if (from == 0)
  129. return 0;
  130. if (from == to)
  131. return value;
  132. if (from > to)
  133. return (value + ((value > 0) - (value < 0)) * from/to/2) / (from/to);
  134. else
  135. return value * (to/from);
  136. }
  137. /**
  138. * Convert a fixed-point value to a floating-point value.
  139. * Returns NaN for "unknown" values.
  140. */
  141. static inline float minmea_float(int value, int scale)
  142. {
  143. if (scale == 0)
  144. return NAN;
  145. return (float) value / (float) scale;
  146. }
  147. /**
  148. * Convert a raw coordinate to a floating point DD.DDD... value.
  149. * Returns NaN for "unknown" values.
  150. */
  151. static inline float minmea_coord(int value, int scale)
  152. {
  153. if (scale == 0)
  154. return NAN;
  155. int degrees = value / (scale * 100);
  156. int minutes = value % (scale * 100);
  157. return (float) degrees + (float) minutes / (60 * scale);
  158. }
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. #endif /* MINMEA_H */
  163. /* vim: set ts=4 sw=4 et: */