minmea.h 5.9 KB

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