minmea.h 6.9 KB

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