minmea.h 7.7 KB

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