minmea.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #define _BSD_SOURCE
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #include <stdio.h>
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <errno.h>
  18. #include <time.h>
  19. #include <math.h>
  20. #include <sys/time.h>
  21. #define MINMEA_MAX_LENGTH 80
  22. enum minmea_sentence_id {
  23. MINMEA_INVALID = -1,
  24. MINMEA_UNKNOWN = 0,
  25. MINMEA_SENTENCE_RMC,
  26. MINMEA_SENTENCE_GGA,
  27. MINMEA_SENTENCE_GSA,
  28. };
  29. struct minmea_date {
  30. int day;
  31. int month;
  32. int year;
  33. };
  34. struct minmea_time {
  35. int hours;
  36. int minutes;
  37. int seconds;
  38. int microseconds;
  39. };
  40. struct minmea_sentence_rmc {
  41. struct minmea_time time;
  42. bool valid;
  43. int latitude, latitude_scale;
  44. int longitude, longitude_scale;
  45. int speed, speed_scale;
  46. int course, course_scale;
  47. struct minmea_date date;
  48. int variation, variation_scale;
  49. };
  50. struct minmea_sentence_gga {
  51. struct minmea_time time;
  52. int latitude, latitude_scale;
  53. int longitude, longitude_scale;
  54. int fix_quality;
  55. int satellites_tracked;
  56. int hdop, hdop_scale;
  57. int altitude, altitude_scale; char altitude_units;
  58. int height, height_scale; char height_units;
  59. int dgps_age;
  60. };
  61. enum minmea_gsa_mode {
  62. MINMEA_GPGSA_MODE_AUTO = 'A',
  63. MINMEA_GPGSA_MODE_FORCED = 'M',
  64. };
  65. enum minmea_gsa_fix_type {
  66. MINMEA_GPGSA_FIX_NONE = 1,
  67. MINMEA_GPGSA_FIX_2D = 2,
  68. MINMEA_GPGSA_FIX_3D = 3,
  69. };
  70. struct minmea_sentence_gsa {
  71. char mode;
  72. int fix_type;
  73. int sats[12];
  74. int pdop, pdop_scale;
  75. int hdop, hdop_scale;
  76. int vdop, vdop_scale;
  77. };
  78. /**
  79. * Check sentence validity and checksum. Returns true for valid sentences.
  80. */
  81. bool minmea_check(const char *sentence);
  82. /**
  83. * Determine talker identifier.
  84. */
  85. bool minmea_talker_id(char talker[3], const char *sentence);
  86. /**
  87. * Determine sentence identifier.
  88. */
  89. enum minmea_sentence_id minmea_sentence_id(const char *sentence);
  90. /**
  91. * Scanf-like processor for NMEA sentences. Supports the following formats:
  92. * c - single character (char *)
  93. * d - direction, returned as 1/-1, default 0 (int *)
  94. * f - fractional, returned as value + scale (int *, int *)
  95. * i - decimal, default zero (int *)
  96. * s - string (char *)
  97. * t - talker identifier and type (char *)
  98. * T - date/time stamp (int *, int *, int *)
  99. * Returns true on success. See library source code for details.
  100. */
  101. bool minmea_scan(const char *sentence, const char *format, ...);
  102. /*
  103. * Parse a specific type of sentence. Return true on success.
  104. */
  105. bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
  106. bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
  107. bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
  108. /**
  109. * Convert GPS UTC date/time representation to a UNIX timestamp.
  110. */
  111. int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time);
  112. /**
  113. * Rescale a fixed-point value to a different scale. Rounds towards zero.
  114. */
  115. static inline int minmea_rescale(int value, int from, int to)
  116. {
  117. if (from == 0)
  118. return 0;
  119. if (from == to)
  120. return value;
  121. if (from > to)
  122. return (value + ((value > 0) - (value < 0)) * from/to/2) / (from/to);
  123. else
  124. return value * (to/from);
  125. }
  126. /**
  127. * Convert a fixed-point value to a floating-point value.
  128. * Returns NaN for "unknown" values.
  129. */
  130. static inline float minmea_float(int value, int scale)
  131. {
  132. if (scale == 0)
  133. return NAN;
  134. return (float) value / (float) scale;
  135. }
  136. /**
  137. * Convert a raw coordinate to a floating point DD.DDD... value.
  138. * Returns NaN for "unknown" values.
  139. */
  140. static inline float minmea_coord(int value, int scale)
  141. {
  142. if (scale == 0)
  143. return NAN;
  144. int degrees = value / (scale * 100);
  145. int minutes = value % (scale * 100);
  146. return (float) degrees + (float) minutes / (60 * scale);
  147. }
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #endif /* MINMEA_H */
  152. /* vim: set ts=4 sw=4 et: */