minmea.h 6.6 KB

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