minmea.h 4.8 KB

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