minmea.h 4.1 KB

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