minmea.h 7.5 KB

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