minmea.h 5.1 KB

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