minmea.h 5.3 KB

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