minmea.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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; char altitude_units;
  80. struct minmea_float height; char height_units;
  81. struct minmea_float dgps_age;
  82. };
  83. enum minmea_gll_status {
  84. MINMEA_GLL_STATUS_DATA_VALID = 'A',
  85. MINMEA_GLL_STATUS_DATA_NOT_VALID = 'V',
  86. };
  87. // FAA mode added to some fields in NMEA 2.3.
  88. enum minmea_faa_mode {
  89. MINMEA_FAA_MODE_AUTONOMOUS = 'A',
  90. MINMEA_FAA_MODE_DIFFERENTIAL = 'D',
  91. MINMEA_FAA_MODE_ESTIMATED = 'E',
  92. MINMEA_FAA_MODE_MANUAL = 'M',
  93. MINMEA_FAA_MODE_SIMULATED = 'S',
  94. MINMEA_FAA_MODE_NOT_VALID = 'N',
  95. MINMEA_FAA_MODE_PRECISE = 'P',
  96. };
  97. struct minmea_sentence_gll {
  98. struct minmea_float latitude;
  99. struct minmea_float longitude;
  100. struct minmea_time time;
  101. char status;
  102. char mode;
  103. };
  104. struct minmea_sentence_gst {
  105. struct minmea_time time;
  106. struct minmea_float rms_deviation;
  107. struct minmea_float semi_major_deviation;
  108. struct minmea_float semi_minor_deviation;
  109. struct minmea_float semi_major_orientation;
  110. struct minmea_float latitude_error_deviation;
  111. struct minmea_float longitude_error_deviation;
  112. struct minmea_float altitude_error_deviation;
  113. };
  114. enum minmea_gsa_mode {
  115. MINMEA_GPGSA_MODE_AUTO = 'A',
  116. MINMEA_GPGSA_MODE_FORCED = 'M',
  117. };
  118. enum minmea_gsa_fix_type {
  119. MINMEA_GPGSA_FIX_NONE = 1,
  120. MINMEA_GPGSA_FIX_2D = 2,
  121. MINMEA_GPGSA_FIX_3D = 3,
  122. };
  123. struct minmea_sentence_gsa {
  124. char mode;
  125. int fix_type;
  126. int sats[12];
  127. struct minmea_float pdop;
  128. struct minmea_float hdop;
  129. struct minmea_float vdop;
  130. };
  131. struct minmea_sat_info {
  132. int nr;
  133. int elevation;
  134. int azimuth;
  135. int snr;
  136. };
  137. struct minmea_sentence_gsv {
  138. int total_msgs;
  139. int msg_nr;
  140. int total_sats;
  141. struct minmea_sat_info sats[4];
  142. };
  143. struct minmea_sentence_vtg {
  144. struct minmea_float true_track_degrees;
  145. struct minmea_float magnetic_track_degrees;
  146. struct minmea_float speed_knots;
  147. struct minmea_float speed_kph;
  148. enum minmea_faa_mode faa_mode;
  149. };
  150. struct minmea_sentence_zda {
  151. struct minmea_time time;
  152. struct minmea_date date;
  153. int hour_offset;
  154. int minute_offset;
  155. };
  156. /**
  157. * Calculate raw sentence checksum. Does not check sentence integrity.
  158. */
  159. uint8_t minmea_checksum(const char *sentence);
  160. /**
  161. * Check sentence validity and checksum. Returns true for valid sentences.
  162. */
  163. bool minmea_check(const char *sentence, bool strict);
  164. /**
  165. * Determine talker identifier.
  166. */
  167. bool minmea_talker_id(char talker[3], const char *sentence);
  168. /**
  169. * Determine sentence identifier.
  170. */
  171. enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict);
  172. /**
  173. * Scanf-like processor for NMEA sentences. Supports the following formats:
  174. * c - single character (char *)
  175. * d - direction, returned as 1/-1, default 0 (int *)
  176. * f - fractional, returned as value + scale (struct minmea_float *)
  177. * i - decimal, default zero (int *)
  178. * s - string (char *)
  179. * t - talker identifier and type (char *)
  180. * D - date (struct minmea_date *)
  181. * T - time stamp (struct minmea_time *)
  182. * _ - ignore this field
  183. * ; - following fields are optional
  184. * Returns true on success. See library source code for details.
  185. */
  186. bool minmea_scan(const char *sentence, const char *format, ...);
  187. /*
  188. * Parse a specific type of sentence. Return true on success.
  189. */
  190. bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence);
  191. bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
  192. bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
  193. bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
  194. bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence);
  195. bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence);
  196. bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence);
  197. bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence);
  198. bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence);
  199. /**
  200. * Convert GPS UTC date/time representation to a UNIX calendar time.
  201. */
  202. int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_);
  203. /**
  204. * Convert GPS UTC date/time representation to a UNIX timestamp.
  205. */
  206. int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_);
  207. /**
  208. * Rescale a fixed-point value to a different scale. Rounds towards zero.
  209. */
  210. static inline int_least32_t minmea_rescale(const struct minmea_float *f, int_least32_t new_scale)
  211. {
  212. if (f->scale == 0)
  213. return 0;
  214. if (f->scale == new_scale)
  215. return f->value;
  216. if (f->scale > new_scale)
  217. return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale/new_scale/2) / (f->scale/new_scale);
  218. else
  219. return f->value * (new_scale/f->scale);
  220. }
  221. /**
  222. * Convert a fixed-point value to a floating-point value.
  223. * Returns NaN for "unknown" values.
  224. */
  225. static inline float minmea_tofloat(const struct minmea_float *f)
  226. {
  227. if (f->scale == 0)
  228. return 0;
  229. return (float) f->value / (float) f->scale;
  230. }
  231. /**
  232. * Convert a raw coordinate to a floating point DD.DDD... value.
  233. * Returns NaN for "unknown" values.
  234. */
  235. static inline float minmea_tocoord(const struct minmea_float *f)
  236. {
  237. if (f->scale == 0)
  238. return 0;
  239. // if (f->scale > (INT_LEAST32_MAX / 100))
  240. // return 0;
  241. // if (f->scale < (INT_LEAST32_MIN / 100))
  242. // return 0;
  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. int test_nmea(void);
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #endif /* MINMEA_H */
  259. /* vim: set ts=4 sw=4 et: */