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.
15 #define boolstr(s) ((s) ? "true" : "false")
17 static int hex2int(char c
)
19 if (c
>= '0' && c
<= '9')
21 if (c
>= 'A' && c
<= 'F')
23 if (c
>= 'a' && c
<= 'f')
28 uint8_t minmea_checksum(const char *sentence
)
30 // Support senteces with or without the starting dollar sign.
34 uint8_t checksum
= 0x00;
36 // The optional checksum is an XOR of all bytes between "$" and "*".
37 while (*sentence
&& *sentence
!= '*')
38 checksum
^= *sentence
++;
43 bool minmea_check(const char *sentence
, bool strict
)
45 uint8_t checksum
= 0x00;
47 // A valid sentence starts with "$".
48 if (*sentence
++ != '$')
51 // The optional checksum is an XOR of all bytes between "$" and "*".
52 while (*sentence
&& *sentence
!= '*' && isprint((unsigned char) *sentence
))
53 checksum
^= *sentence
++;
55 // If checksum is present...
56 if (*sentence
== '*') {
59 int upper
= hex2int(*sentence
++);
62 int lower
= hex2int(*sentence
++);
65 int expected
= upper
<< 4 | lower
;
67 // Check for checksum mismatch.
68 if (checksum
!= expected
)
71 // Discard non-checksummed frames in strict mode.
75 // The only stuff allowed at this point is a newline.
76 while (*sentence
== '\r' || *sentence
== '\n') {
87 bool minmea_scan(const char *sentence
, const char *format
, ...)
90 bool optional
= false;
98 const char *field
= sentence
;
99 #define next_field() \
101 /* Progress to the next field. */ \
102 while (minmea_isfield(*sentence)) \
104 /* Make sure there is a field there. */ \
105 if (*sentence == ',') { \
114 char type
= *format
++;
117 // All further fields are optional.
122 if (!field
&& !optional
) {
123 // Field requested but we ran out if input. Bail out.
128 case 'c': { // Single character field (char).
131 if (field
&& minmea_isfield(*field
))
134 *va_arg(ap
, char *) = value
;
137 case 'd': { // Single character direction field (int).
140 if (field
&& minmea_isfield(*field
)) {
155 *va_arg(ap
, int *) = value
;
158 case 'f': { // Fractional value with scale (struct minmea_float).
160 int_least32_t value
= -1;
161 int_least32_t scale
= 0;
164 while (minmea_isfield(*field
)) {
165 if (*field
== '+' && !sign
&& value
== -1) {
167 } else if (*field
== '-' && !sign
&& value
== -1) {
169 } else if (isdigit((unsigned char) *field
)) {
170 int digit
= *field
- '0';
173 if (value
> (INT_LEAST32_MAX
-digit
) / 10) {
174 /* we ran out of bits, what do we do? */
176 /* truncate extra precision */
179 /* integer overflow. bail out. */
183 value
= (10 * value
) + digit
;
186 } else if (*field
== '.' && scale
== 0) {
188 } else if (*field
== ' ') {
189 /* Allow spaces at the start of the field. Not NMEA
190 * conformant, but some modules do this. */
191 if (sign
!= 0 || value
!= -1 || scale
!= 0)
200 if ((sign
|| scale
) && value
== -1)
204 /* No digits were scanned. */
207 } else if (scale
== 0) {
208 /* No decimal point. */
214 *va_arg(ap
, struct minmea_float
*) = (struct minmea_float
) {value
, scale
};
217 case 'i': { // Integer value, default 0 (int).
222 value
= strtol(field
, &endptr
, 10);
223 if (minmea_isfield(*endptr
))
227 *va_arg(ap
, int *) = value
;
230 case 's': { // String value (char *).
231 char *buf
= va_arg(ap
, char *);
234 while (minmea_isfield(*field
))
241 case 't': { // NMEA talker+sentence identifier (char *).
242 // This field is always mandatory.
248 for (int f
=0; f
<5; f
++)
249 if (!minmea_isfield(field
[1+f
]))
252 char *buf
= va_arg(ap
, char *);
253 memcpy(buf
, field
+1, 5);
257 case 'D': { // Date (int, int, int), -1 if empty.
258 struct minmea_date
*date
= va_arg(ap
, struct minmea_date
*);
260 int d
= -1, m
= -1, y
= -1;
262 if (field
&& minmea_isfield(*field
)) {
263 // Always six digits.
264 for (int f
=0; f
<6; f
++)
265 if (!isdigit((unsigned char) field
[f
]))
268 char dArr
[] = {field
[0], field
[1], '\0'};
269 char mArr
[] = {field
[2], field
[3], '\0'};
270 char yArr
[] = {field
[4], field
[5], '\0'};
271 d
= strtol(dArr
, NULL
, 10);
272 m
= strtol(mArr
, NULL
, 10);
273 y
= strtol(yArr
, NULL
, 10);
281 case 'T': { // Time (int, int, int, int), -1 if empty.
282 struct minmea_time
*time_
= va_arg(ap
, struct minmea_time
*);
284 int h
= -1, i
= -1, s
= -1, u
= -1;
286 if (field
&& minmea_isfield(*field
)) {
287 // Minimum required: integer time.
288 for (int f
=0; f
<6; f
++)
289 if (!isdigit((unsigned char) field
[f
]))
292 char hArr
[] = {field
[0], field
[1], '\0'};
293 char iArr
[] = {field
[2], field
[3], '\0'};
294 char sArr
[] = {field
[4], field
[5], '\0'};
295 h
= strtol(hArr
, NULL
, 10);
296 i
= strtol(iArr
, NULL
, 10);
297 s
= strtol(sArr
, NULL
, 10);
300 // Extra: fractional time. Saved as microseconds.
301 if (*field
++ == '.') {
303 uint32_t scale
= 1000000LU;
304 while (isdigit((unsigned char) *field
) && scale
> 1) {
305 value
= (value
* 10) + (*field
++ - '0');
317 time_
->microseconds
= u
;
320 case '_': { // Ignore the field.
323 default: { // Unknown.
338 bool minmea_talker_id(char talker
[3], const char *sentence
)
341 if (!minmea_scan(sentence
, "t", type
))
351 enum minmea_sentence_id
minmea_sentence_id(const char *sentence
, bool strict
)
353 if (!minmea_check(sentence
, strict
))
354 return MINMEA_INVALID
;
357 if (!minmea_scan(sentence
, "t", type
))
358 return MINMEA_INVALID
;
360 if (!strcmp(type
+2, "GBS"))
361 return MINMEA_SENTENCE_GBS
;
362 if (!strcmp(type
+2, "GGA"))
363 return MINMEA_SENTENCE_GGA
;
364 if (!strcmp(type
+2, "GLL"))
365 return MINMEA_SENTENCE_GLL
;
366 if (!strcmp(type
+2, "GSA"))
367 return MINMEA_SENTENCE_GSA
;
368 if (!strcmp(type
+2, "GST"))
369 return MINMEA_SENTENCE_GST
;
370 if (!strcmp(type
+2, "GSV"))
371 return MINMEA_SENTENCE_GSV
;
372 if (!strcmp(type
+2, "RMC"))
373 return MINMEA_SENTENCE_RMC
;
374 if (!strcmp(type
+2, "VTG"))
375 return MINMEA_SENTENCE_VTG
;
376 if (!strcmp(type
+2, "ZDA"))
377 return MINMEA_SENTENCE_ZDA
;
378 if (!strcmp(type
+2, "TXT"))
379 return MINMEA_SENTENCE_TXT
;
381 return MINMEA_UNKNOWN
;
384 bool minmea_parse_gbs(struct minmea_sentence_gbs
*frame
, const char *sentence
)
386 // $GNGBS,170556.00,3.0,2.9,8.3,,,,*5C
388 if (!minmea_scan(sentence
, "tTfffifff",
391 &frame
->err_latitude
,
392 &frame
->err_longitude
,
393 &frame
->err_altitude
,
400 if (strcmp(type
+2, "GBS"))
406 bool minmea_parse_rmc(struct minmea_sentence_rmc
*frame
, const char *sentence
)
408 // $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
411 int latitude_direction
;
412 int longitude_direction
;
413 int variation_direction
;
414 if (!minmea_scan(sentence
, "tTcfdfdffDfd",
418 &frame
->latitude
, &latitude_direction
,
419 &frame
->longitude
, &longitude_direction
,
423 &frame
->variation
, &variation_direction
))
425 if (strcmp(type
+2, "RMC"))
428 frame
->valid
= (validity
== 'A');
429 frame
->latitude
.value
*= latitude_direction
;
430 frame
->longitude
.value
*= longitude_direction
;
431 frame
->variation
.value
*= variation_direction
;
436 bool minmea_parse_gga(struct minmea_sentence_gga
*frame
, const char *sentence
)
438 // $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
440 int latitude_direction
;
441 int longitude_direction
;
443 if (!minmea_scan(sentence
, "tTfdfdiiffcfcf_",
446 &frame
->latitude
, &latitude_direction
,
447 &frame
->longitude
, &longitude_direction
,
449 &frame
->satellites_tracked
,
451 &frame
->altitude
, &frame
->altitude_units
,
452 &frame
->height
, &frame
->height_units
,
455 if (strcmp(type
+2, "GGA"))
458 frame
->latitude
.value
*= latitude_direction
;
459 frame
->longitude
.value
*= longitude_direction
;
464 bool minmea_parse_gsa(struct minmea_sentence_gsa
*frame
, const char *sentence
)
466 // $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
469 if (!minmea_scan(sentence
, "tciiiiiiiiiiiiifff;i",
490 if (strcmp(type
+2, "GSA"))
496 bool minmea_parse_gll(struct minmea_sentence_gll
*frame
, const char *sentence
)
498 // $GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41$;
500 int latitude_direction
;
501 int longitude_direction
;
503 if (!minmea_scan(sentence
, "tfdfdTc;c",
505 &frame
->latitude
, &latitude_direction
,
506 &frame
->longitude
, &longitude_direction
,
511 if (strcmp(type
+2, "GLL"))
514 frame
->latitude
.value
*= latitude_direction
;
515 frame
->longitude
.value
*= longitude_direction
;
520 bool minmea_parse_gst(struct minmea_sentence_gst
*frame
, const char *sentence
)
522 // $GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58
525 if (!minmea_scan(sentence
, "tTfffffff",
528 &frame
->rms_deviation
,
529 &frame
->semi_major_deviation
,
530 &frame
->semi_minor_deviation
,
531 &frame
->semi_major_orientation
,
532 &frame
->latitude_error_deviation
,
533 &frame
->longitude_error_deviation
,
534 &frame
->altitude_error_deviation
))
536 if (strcmp(type
+2, "GST"))
542 bool minmea_parse_gsv(struct minmea_sentence_gsv
*frame
, const char *sentence
)
544 // $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74
545 // $GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D
546 // $GPGSV,4,2,11,08,51,203,30,09,45,215,28*75
547 // $GPGSV,4,4,13,39,31,170,27*40
551 if (!minmea_scan(sentence
, "tiii;iiiiiiiiiiiiiiii",
557 &frame
->sats
[0].elevation
,
558 &frame
->sats
[0].azimuth
,
561 &frame
->sats
[1].elevation
,
562 &frame
->sats
[1].azimuth
,
565 &frame
->sats
[2].elevation
,
566 &frame
->sats
[2].azimuth
,
569 &frame
->sats
[3].elevation
,
570 &frame
->sats
[3].azimuth
,
575 if (strcmp(type
+2, "GSV"))
581 bool minmea_parse_vtg(struct minmea_sentence_vtg
*frame
, const char *sentence
)
583 // $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48
584 // $GPVTG,156.1,T,140.9,M,0.0,N,0.0,K*41
585 // $GPVTG,096.5,T,083.5,M,0.0,N,0.0,K,D*22
586 // $GPVTG,188.36,T,,M,0.820,N,1.519,K,A*3F
588 char c_true
, c_magnetic
, c_knots
, c_kph
, c_faa_mode
;
590 if (!minmea_scan(sentence
, "t;fcfcfcfcc",
592 &frame
->true_track_degrees
,
594 &frame
->magnetic_track_degrees
,
602 if (strcmp(type
+2, "VTG"))
604 // values are only valid with the accompanying characters
606 frame
->true_track_degrees
.scale
= 0;
607 if (c_magnetic
!= 'M')
608 frame
->magnetic_track_degrees
.scale
= 0;
610 frame
->speed_knots
.scale
= 0;
612 frame
->speed_kph
.scale
= 0;
613 frame
->faa_mode
= (enum minmea_faa_mode
)c_faa_mode
;
618 bool minmea_parse_zda(struct minmea_sentence_zda
*frame
, const char *sentence
)
620 // $GPZDA,201530.00,04,07,2002,00,00*60
623 if(!minmea_scan(sentence
, "tTiiiii",
630 &frame
->minute_offset
))
632 if (strcmp(type
+2, "ZDA"))
636 if (abs(frame
->hour_offset
) > 13 ||
637 frame
->minute_offset
> 59 ||
638 frame
->minute_offset
< 0)
644 bool minmea_parse_txt(struct minmea_sentence_txt
*frame
, const char *sentence
) {
646 if(!minmea_scan(sentence
, "t___s",
653 int minmea_getdatetime(struct tm
*tm
, const struct minmea_date
*date
, const struct minmea_time
*time_
)
655 if (date
->year
== -1 || time_
->hours
== -1)
658 memset(tm
, 0, sizeof(*tm
));
659 if (date
->year
< 80) {
660 tm
->tm_year
= 2000 + date
->year
; // 2000-2079
661 } else if (date
->year
>= 1900) {
662 tm
->tm_year
= date
->year
; // 4 digit year, use directly
664 tm
->tm_year
= date
->year
+ 1900; // 1980-1999
666 tm
->tm_mon
= date
->month
- 1;
667 tm
->tm_mday
= date
->day
;
668 tm
->tm_hour
= time_
->hours
;
669 tm
->tm_min
= time_
->minutes
;
670 tm
->tm_sec
= time_
->seconds
;
675 // int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_)
678 // if (minmea_getdatetime(&tm, date, time_))
681 // // time_t timestamp = timegm(&tm); /* See README.md if your system lacks timegm(). */
682 // // if (timestamp != (time_t)-1) {
683 // // ts->tv_sec = timestamp;
684 // // ts->tv_nsec = time_->microseconds * 1000;
692 /* vim: set ts=4 sw=4 et: */