minmea.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #define MINMEA_MAX_LENGTH 80
  20. enum minmea_sentence_id {
  21. MINMEA_INVALID = -1,
  22. MINMEA_UNKNOWN = 0,
  23. MINMEA_SENTENCE_RMC,
  24. MINMEA_SENTENCE_GGA,
  25. MINMEA_SENTENCE_GSA,
  26. MINMEA_SENTENCE_GLL,
  27. MINMEA_SENTENCE_GST,
  28. MINMEA_SENTENCE_GSV,
  29. MINMEA_SENTENCE_VTG,
  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. enum minmea_gll_status {
  68. MINMEA_GLL_STATUS_DATA_VALID = 'A',
  69. MINMEA_GLL_STATUS_DATA_NOT_VALID = 'V',
  70. };
  71. // FAA mode added to some fields in NMEA 2.3.
  72. enum minmea_faa_mode {
  73. MINMEA_FAA_MODE_AUTONOMOUS = 'A',
  74. MINMEA_FAA_MODE_DIFFERENTIAL = 'D',
  75. MINMEA_FAA_MODE_ESTIMATED = 'E',
  76. MINMEA_FAA_MODE_MANUAL = 'M',
  77. MINMEA_FAA_MODE_SIMULATED = 'S',
  78. MINMEA_FAA_MODE_NOT_VALID = 'N',
  79. MINMEA_FAA_MODE_PRECISE = 'P',
  80. };
  81. struct minmea_sentence_gll {
  82. struct minmea_float latitude;
  83. struct minmea_float longitude;
  84. struct minmea_time time;
  85. char status;
  86. char mode;
  87. };
  88. struct minmea_sentence_gst {
  89. struct minmea_time time;
  90. struct minmea_float rms_deviation;
  91. struct minmea_float semi_major_deviation;
  92. struct minmea_float semi_minor_deviation;
  93. struct minmea_float semi_major_orientation;
  94. struct minmea_float latitude_error_deviation;
  95. struct minmea_float longitude_error_deviation;
  96. struct minmea_float altitude_error_deviation;
  97. };
  98. enum minmea_gsa_mode {
  99. MINMEA_GPGSA_MODE_AUTO = 'A',
  100. MINMEA_GPGSA_MODE_FORCED = 'M',
  101. };
  102. enum minmea_gsa_fix_type {
  103. MINMEA_GPGSA_FIX_NONE = 1,
  104. MINMEA_GPGSA_FIX_2D = 2,
  105. MINMEA_GPGSA_FIX_3D = 3,
  106. };
  107. struct minmea_sentence_gsa {
  108. char mode;
  109. int fix_type;
  110. int sats[12];
  111. struct minmea_float pdop;
  112. struct minmea_float hdop;
  113. struct minmea_float vdop;
  114. };
  115. struct minmea_sat_info {
  116. int nr;
  117. int elevation;
  118. int azimuth;
  119. int snr;
  120. };
  121. struct minmea_sentence_gsv {
  122. int total_msgs;
  123. int msg_nr;
  124. int total_sats;
  125. struct minmea_sat_info sats[4];
  126. };
  127. struct minmea_sentence_vtg {
  128. struct minmea_float true_track_degrees;
  129. struct minmea_float magnetic_track_degrees;
  130. struct minmea_float speed_knots;
  131. struct minmea_float speed_kph;
  132. enum minmea_faa_mode faa_mode;
  133. };
  134. /**
  135. * Calculate raw sentence checksum. Does not check sentence integrity.
  136. */
  137. uint8_t minmea_checksum(const char *sentence);
  138. /**
  139. * Check sentence validity and checksum. Returns true for valid sentences.
  140. */
  141. bool minmea_check(const char *sentence, bool strict);
  142. /**
  143. * Determine talker identifier.
  144. */
  145. bool minmea_talker_id(char talker[3], const char *sentence);
  146. /**
  147. * Determine sentence identifier.
  148. */
  149. enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict);
  150. /**
  151. * Scanf-like processor for NMEA sentences. Supports the following formats:
  152. * c - single character (char *)
  153. * d - direction, returned as 1/-1, default 0 (int *)
  154. * f - fractional, returned as value + scale (int *, int *)
  155. * i - decimal, default zero (int *)
  156. * s - string (char *)
  157. * t - talker identifier and type (char *)
  158. * T - date/time stamp (int *, int *, int *)
  159. * Returns true on success. See library source code for details.
  160. */
  161. bool minmea_scan(const char *sentence, const char *format, ...);
  162. /*
  163. * Parse a specific type of sentence. Return true on success.
  164. */
  165. bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
  166. bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
  167. bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
  168. bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence);
  169. bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence);
  170. bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence);
  171. bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence);
  172. /**
  173. * Convert GPS UTC date/time representation to a UNIX timestamp.
  174. */
  175. int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_);
  176. /**
  177. * Rescale a fixed-point value to a different scale. Rounds towards zero.
  178. */
  179. static inline int_least32_t minmea_rescale(struct minmea_float *f, int_least32_t new_scale)
  180. {
  181. if (f->scale == 0)
  182. return 0;
  183. if (f->scale == new_scale)
  184. return f->value;
  185. if (f->scale > new_scale)
  186. return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale/new_scale/2) / (f->scale/new_scale);
  187. else
  188. return f->value * (new_scale/f->scale);
  189. }
  190. /**
  191. * Convert a fixed-point value to a floating-point value.
  192. * Returns NaN for "unknown" values.
  193. */
  194. static inline float minmea_tofloat(struct minmea_float *f)
  195. {
  196. if (f->scale == 0)
  197. return NAN;
  198. return (float) f->value / (float) f->scale;
  199. }
  200. /**
  201. * Convert a raw coordinate to a floating point DD.DDD... value.
  202. * Returns NaN for "unknown" values.
  203. */
  204. static inline float minmea_tocoord(struct minmea_float *f)
  205. {
  206. if (f->scale == 0)
  207. return NAN;
  208. int_least32_t degrees = f->value / (f->scale * 100);
  209. int_least32_t minutes = f->value % (f->scale * 100);
  210. return (float) degrees + (float) minutes / (60 * f->scale);
  211. }
  212. #ifdef __cplusplus
  213. }
  214. #endif
  215. #endif /* MINMEA_H */
  216. /* vim: set ts=4 sw=4 et: */