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