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.
21 #ifdef MINMEA_INCLUDE_COMPAT
22 #include <minmea_compat.h>
25 #ifndef MINMEA_MAX_SENTENCE_LENGTH
26 #define MINMEA_MAX_SENTENCE_LENGTH 80
29 #define FRAME_TXT_MAX_LEN (79)
31 enum minmea_sentence_id
{
45 MINMEA_SENTENCE_MAX_ID
66 struct minmea_sentence_gbs
{
67 struct minmea_time time
;
68 struct minmea_float err_latitude
;
69 struct minmea_float err_longitude
;
70 struct minmea_float err_altitude
;
72 struct minmea_float prob
;
73 struct minmea_float bias
;
74 struct minmea_float stddev
;
77 struct minmea_sentence_rmc
{
78 struct minmea_time time
;
80 struct minmea_float latitude
;
81 struct minmea_float longitude
;
82 struct minmea_float speed
;
83 struct minmea_float course
;
84 struct minmea_date date
;
85 struct minmea_float variation
;
88 struct minmea_sentence_gga
{
89 struct minmea_time time
;
90 struct minmea_float latitude
;
91 struct minmea_float longitude
;
93 int satellites_tracked
;
94 struct minmea_float hdop
;
95 struct minmea_float altitude
; char altitude_units
;
96 struct minmea_float height
; char height_units
;
97 struct minmea_float dgps_age
;
100 enum minmea_gll_status
{
101 MINMEA_GLL_STATUS_DATA_VALID
= 'A',
102 MINMEA_GLL_STATUS_DATA_NOT_VALID
= 'V',
105 // FAA mode added to some fields in NMEA 2.3.
106 enum minmea_faa_mode
{
107 MINMEA_FAA_MODE_AUTONOMOUS
= 'A',
108 MINMEA_FAA_MODE_DIFFERENTIAL
= 'D',
109 MINMEA_FAA_MODE_ESTIMATED
= 'E',
110 MINMEA_FAA_MODE_MANUAL
= 'M',
111 MINMEA_FAA_MODE_SIMULATED
= 'S',
112 MINMEA_FAA_MODE_NOT_VALID
= 'N',
113 MINMEA_FAA_MODE_PRECISE
= 'P',
116 struct minmea_sentence_gll
{
117 struct minmea_float latitude
;
118 struct minmea_float longitude
;
119 struct minmea_time time
;
124 struct minmea_sentence_gst
{
125 struct minmea_time time
;
126 struct minmea_float rms_deviation
;
127 struct minmea_float semi_major_deviation
;
128 struct minmea_float semi_minor_deviation
;
129 struct minmea_float semi_major_orientation
;
130 struct minmea_float latitude_error_deviation
;
131 struct minmea_float longitude_error_deviation
;
132 struct minmea_float altitude_error_deviation
;
135 enum minmea_gsa_mode
{
136 MINMEA_GPGSA_MODE_AUTO
= 'A',
137 MINMEA_GPGSA_MODE_FORCED
= 'M',
140 enum minmea_gsa_fix_type
{
141 MINMEA_GPGSA_FIX_NONE
= 1,
142 MINMEA_GPGSA_FIX_2D
= 2,
143 MINMEA_GPGSA_FIX_3D
= 3,
146 struct minmea_sentence_gsa
{
150 struct minmea_float pdop
;
151 struct minmea_float hdop
;
152 struct minmea_float vdop
;
156 struct minmea_sat_info
{
163 struct minmea_sentence_gsv
{
167 struct minmea_sat_info sats
[4];
170 struct minmea_sentence_vtg
{
171 struct minmea_float true_track_degrees
;
172 struct minmea_float magnetic_track_degrees
;
173 struct minmea_float speed_knots
;
174 struct minmea_float speed_kph
;
175 enum minmea_faa_mode faa_mode
;
178 struct minmea_sentence_zda
{
179 struct minmea_time time
;
180 struct minmea_date date
;
185 struct minmea_sentence_txt
{
188 char txt
[FRAME_TXT_MAX_LEN
+ 1];
192 * Calculate raw sentence checksum. Does not check sentence integrity.
194 uint8_t minmea_checksum(const char *sentence
);
197 * Check sentence validity and checksum. Returns true for valid sentences.
199 bool minmea_check(const char *sentence
, bool strict
);
202 * Determine talker identifier.
204 bool minmea_talker_id(char talker
[3], const char *sentence
);
207 * Determine sentence identifier.
209 enum minmea_sentence_id
minmea_sentence_id(const char *sentence
, bool strict
);
212 * Scanf-like processor for NMEA sentences. Supports the following formats:
213 * c - single character (char *)
214 * d - direction, returned as 1/-1, default 0 (int *)
215 * f - fractional, returned as value + scale (struct minmea_float *)
216 * i - decimal, default zero (int *)
217 * s - string (char *)
218 * t - talker identifier and type (char *)
219 * D - date (struct minmea_date *)
220 * T - time stamp (struct minmea_time *)
221 * _ - ignore this field
222 * ; - following fields are optional
223 * Returns true on success. See library source code for details.
225 bool minmea_scan(const char *sentence
, const char *format
, ...);
228 * Parse a specific type of sentence. Return true on success.
230 bool minmea_parse_gbs(struct minmea_sentence_gbs
*frame
, const char *sentence
);
231 bool minmea_parse_rmc(struct minmea_sentence_rmc
*frame
, const char *sentence
);
232 bool minmea_parse_gga(struct minmea_sentence_gga
*frame
, const char *sentence
);
233 bool minmea_parse_gsa(struct minmea_sentence_gsa
*frame
, const char *sentence
);
234 bool minmea_parse_gll(struct minmea_sentence_gll
*frame
, const char *sentence
);
235 bool minmea_parse_gst(struct minmea_sentence_gst
*frame
, const char *sentence
);
236 bool minmea_parse_gsv(struct minmea_sentence_gsv
*frame
, const char *sentence
);
237 bool minmea_parse_vtg(struct minmea_sentence_vtg
*frame
, const char *sentence
);
238 bool minmea_parse_zda(struct minmea_sentence_zda
*frame
, const char *sentence
);
239 bool minmea_parse_txt(struct minmea_sentence_txt
*frame
, const char *sentence
);
242 * Convert GPS UTC date/time representation to a UNIX calendar time.
244 int minmea_getdatetime(struct tm
*tm
, const struct minmea_date
*date
, const struct minmea_time
*time_
);
247 * Convert GPS UTC date/time representation to a UNIX timestamp.
249 int minmea_gettime(struct timespec
*ts
, const struct minmea_date
*date
, const struct minmea_time
*time_
);
252 * Rescale a fixed-point value to a different scale. Rounds towards zero.
254 static inline int_least32_t minmea_rescale(const struct minmea_float
*f
, int_least32_t new_scale
)
258 if (f
->scale
== new_scale
)
260 if (f
->scale
> new_scale
)
261 return (f
->value
+ ((f
->value
> 0) - (f
->value
< 0)) * f
->scale
/new_scale
/2) / (f
->scale
/new_scale
);
263 return f
->value
* (new_scale
/f
->scale
);
267 * Convert a fixed-point value to a floating-point value.
268 * Returns NaN for "unknown" values.
270 static inline float minmea_tofloat(const struct minmea_float
*f
)
274 return (float) f
->value
/ (float) f
->scale
;
278 * Convert a raw coordinate to a floating point DD.DDD... value.
279 * Returns NaN for "unknown" values.
281 static inline float minmea_tocoord(const struct minmea_float
*f
)
285 if (f
->scale
> (INT_LEAST32_MAX
/ 100))
287 if (f
->scale
< (INT_LEAST32_MIN
/ 100))
289 int_least32_t degrees
= f
->value
/ (f
->scale
* 100);
290 int_least32_t minutes
= f
->value
% (f
->scale
* 100);
291 return (float) degrees
+ (float) minutes
/ (60 * f
->scale
);
295 * Convert a raw coordinate to a floating point DD.DDD... value.
296 * Returns NaN for "unknown" values.
298 static inline uint32_t minmea_tocoord2(const struct minmea_float
*f
)
302 if (f
->scale
> (INT_LEAST32_MAX
/ 100))
304 if (f
->scale
< (INT_LEAST32_MIN
/ 100))
306 int_least32_t degrees
= f
->value
/ (f
->scale
* 100);
307 int_least32_t minutes
= f
->value
% (f
->scale
* 100);
308 uint32_t ret
= degrees
* 10000000 + (uint32_t)(((float) minutes
/ (60 * f
->scale
)) * 10000000);
313 * Check whether a character belongs to the set of characters allowed in a
314 * sentence data field.
316 static inline bool minmea_isfield(char c
) {
317 return isprint((unsigned char) c
) && c
!= ',' && c
!= '*';
323 #define RECV_BUFF_SIZE (2048)
324 #define FRAME_GSA_MAX (7)
325 #define FRAME_GSV_MAX (24)
326 #define FRAME_TXT_MAX (80)
329 #define LUAT_LIBGNSS_MAX_LINE (128)
331 int luat_libgnss_init(int clear
);
332 int luat_libgnss_parse_data(const char* data
, size_t len
);
333 int luat_libgnss_parse_nmea(const char* line
);
334 void luat_libgnss_uart_recv_cb(int uart_id
, uint32_t data_len
);
335 int luat_libgnss_state_onchanged(int state
);
337 int luat_libgnss_on_rawdata(const char* data
, size_t len
, int type
);
349 // struct minmea_sentence_rmc frame_rmc;
350 // struct minmea_sentence_gga frame_gga;
351 // struct minmea_sentence_gll frame_gll;
352 // struct minmea_sentence_gst frame_gst;
353 // struct minmea_sentence_gsv frame_gsv;
354 // struct minmea_sentence_vtg frame_vtg;
355 // struct minmea_sentence_gsa frame_gsa;
356 // struct minmea_sentence_zda frame_zda;
357 // struct minmea_sentence_txt txt;
360 typedef struct minmea_data
{
361 char data
[LUAT_LIBGNSS_MAX_LINE
];
365 typedef struct luat_libgnss
367 uint32_t fix_at_ticks
;
368 struct minmea_sentence_rmc frame_rmc
;
375 minmea_data_t
*gsv
[FRAME_GSV_MAX
];
376 minmea_data_t
*gsa
[FRAME_GSA_MAX
];
383 int luat_libgnss_data_check(minmea_data_t
* data
, uint32_t timeout
, uint64_t tnow
);
390 #endif /* MINMEA_H */
392 /* vim: set ts=4 sw=4 et: */