minmea.h 7.5 KB

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