2014-02-17 03:03:14 +08:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
|
2014-02-26 23:27:24 +08:00
|
|
|
* This program is free software. It comes without any warranty, to the extent
|
|
|
|
* permitted by applicable law. You can redistribute it and/or modify it under
|
|
|
|
* the terms of the Do What The Fuck You Want To Public License, Version 2, as
|
|
|
|
* published by Sam Hocevar. See the COPYING file for more details.
|
2014-02-17 03:03:14 +08:00
|
|
|
*/
|
|
|
|
|
2014-02-14 22:22:18 +08:00
|
|
|
#ifndef MINMEA_H
|
|
|
|
#define MINMEA_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#define MINMEA_MAX_LENGTH 80
|
|
|
|
|
2014-03-25 05:40:25 +08:00
|
|
|
enum minmea_sentence_id {
|
2014-02-14 22:22:18 +08:00
|
|
|
MINMEA_INVALID = -1,
|
|
|
|
MINMEA_UNKNOWN = 0,
|
2014-03-25 05:40:25 +08:00
|
|
|
MINMEA_SENTENCE_RMC,
|
|
|
|
MINMEA_SENTENCE_GGA,
|
|
|
|
MINMEA_SENTENCE_GSA,
|
2014-04-24 01:57:30 +08:00
|
|
|
MINMEA_SENTENCE_GST,
|
2014-02-14 22:22:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct minmea_date {
|
|
|
|
int day;
|
|
|
|
int month;
|
|
|
|
int year;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct minmea_time {
|
|
|
|
int hours;
|
|
|
|
int minutes;
|
|
|
|
int seconds;
|
|
|
|
int microseconds;
|
|
|
|
};
|
|
|
|
|
2014-03-25 05:40:25 +08:00
|
|
|
struct minmea_sentence_rmc {
|
2014-02-14 22:22:18 +08:00
|
|
|
struct minmea_time time;
|
|
|
|
bool valid;
|
|
|
|
int latitude, latitude_scale;
|
|
|
|
int longitude, longitude_scale;
|
|
|
|
int speed, speed_scale;
|
|
|
|
int course, course_scale;
|
|
|
|
struct minmea_date date;
|
|
|
|
int variation, variation_scale;
|
|
|
|
};
|
|
|
|
|
2014-03-25 05:40:25 +08:00
|
|
|
struct minmea_sentence_gga {
|
2014-02-14 22:22:18 +08:00
|
|
|
struct minmea_time time;
|
|
|
|
int latitude, latitude_scale;
|
|
|
|
int longitude, longitude_scale;
|
|
|
|
int fix_quality;
|
|
|
|
int satellites_tracked;
|
|
|
|
int hdop, hdop_scale;
|
|
|
|
int altitude, altitude_scale; char altitude_units;
|
|
|
|
int height, height_scale; char height_units;
|
|
|
|
int dgps_age;
|
|
|
|
};
|
|
|
|
|
2014-04-20 16:27:22 +08:00
|
|
|
struct minmea_sentence_gst {
|
2014-04-21 01:21:05 +08:00
|
|
|
struct minmea_time time;
|
2014-04-23 19:23:24 +08:00
|
|
|
int rms_deviation, rms_deviation_scale;
|
|
|
|
int semi_major_deviation, semi_major_deviation_scale;
|
|
|
|
int semi_minor_deviation, semi_minor_deviation_scale;
|
2014-04-21 01:21:05 +08:00
|
|
|
int semi_major_orientation, semi_major_orientation_scale;
|
2014-04-24 01:57:30 +08:00
|
|
|
int latitude_error_deviation, latitude_error_deviation_scale;
|
2014-04-21 01:21:05 +08:00
|
|
|
int longitude_error_deviation, longitude_error_deviation_scale;
|
|
|
|
int altitude_error_deviation, altitude_error_deviation_scale;
|
2014-04-20 16:27:22 +08:00
|
|
|
};
|
|
|
|
|
2014-03-25 05:40:25 +08:00
|
|
|
enum minmea_gsa_mode {
|
2014-02-26 22:07:42 +08:00
|
|
|
MINMEA_GPGSA_MODE_AUTO = 'A',
|
|
|
|
MINMEA_GPGSA_MODE_FORCED = 'M',
|
2014-02-26 00:32:56 +08:00
|
|
|
};
|
|
|
|
|
2014-03-25 05:40:25 +08:00
|
|
|
enum minmea_gsa_fix_type {
|
2014-02-26 22:07:42 +08:00
|
|
|
MINMEA_GPGSA_FIX_NONE = 1,
|
|
|
|
MINMEA_GPGSA_FIX_2D = 2,
|
2014-02-26 22:10:28 +08:00
|
|
|
MINMEA_GPGSA_FIX_3D = 3,
|
2014-02-26 00:32:56 +08:00
|
|
|
};
|
|
|
|
|
2014-03-25 05:40:25 +08:00
|
|
|
struct minmea_sentence_gsa {
|
2014-02-26 22:07:42 +08:00
|
|
|
char mode;
|
2014-02-26 22:03:20 +08:00
|
|
|
int fix_type;
|
2014-02-26 22:07:42 +08:00
|
|
|
int sats[12];
|
2014-02-26 00:32:56 +08:00
|
|
|
int pdop, pdop_scale;
|
|
|
|
int hdop, hdop_scale;
|
|
|
|
int vdop, vdop_scale;
|
|
|
|
};
|
|
|
|
|
2014-02-14 22:22:18 +08:00
|
|
|
/**
|
2014-03-25 05:40:25 +08:00
|
|
|
* Check sentence validity and checksum. Returns true for valid sentences.
|
2014-02-14 22:22:18 +08:00
|
|
|
*/
|
|
|
|
bool minmea_check(const char *sentence);
|
|
|
|
|
2014-03-25 06:13:05 +08:00
|
|
|
/**
|
|
|
|
* Determine talker identifier.
|
|
|
|
*/
|
|
|
|
bool minmea_talker_id(char talker[3], const char *sentence);
|
|
|
|
|
2014-02-14 22:22:18 +08:00
|
|
|
/**
|
2014-03-25 05:40:25 +08:00
|
|
|
* Determine sentence identifier.
|
2014-02-14 22:22:18 +08:00
|
|
|
*/
|
2014-03-25 05:40:25 +08:00
|
|
|
enum minmea_sentence_id minmea_sentence_id(const char *sentence);
|
2014-02-14 22:22:18 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scanf-like processor for NMEA sentences. Supports the following formats:
|
|
|
|
* c - single character (char *)
|
|
|
|
* d - direction, returned as 1/-1, default 0 (int *)
|
|
|
|
* f - fractional, returned as value + scale (int *, int *)
|
|
|
|
* i - decimal, default zero (int *)
|
|
|
|
* s - string (char *)
|
|
|
|
* t - talker identifier and type (char *)
|
|
|
|
* T - date/time stamp (int *, int *, int *)
|
|
|
|
* Returns true on success. See library source code for details.
|
|
|
|
*/
|
|
|
|
bool minmea_scan(const char *sentence, const char *format, ...);
|
|
|
|
|
|
|
|
/*
|
2014-03-25 05:40:25 +08:00
|
|
|
* Parse a specific type of sentence. Return true on success.
|
2014-02-14 22:22:18 +08:00
|
|
|
*/
|
2014-03-25 05:40:25 +08:00
|
|
|
bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
|
|
|
|
bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
|
|
|
|
bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
|
2014-04-20 16:27:22 +08:00
|
|
|
bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence);
|
2014-02-14 22:22:18 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert GPS UTC date/time representation to a UNIX timestamp.
|
|
|
|
*/
|
|
|
|
int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time);
|
|
|
|
|
|
|
|
/**
|
2014-03-03 22:04:37 +08:00
|
|
|
* Rescale a fixed-point value to a different scale. Rounds towards zero.
|
2014-02-14 22:22:18 +08:00
|
|
|
*/
|
|
|
|
static inline int minmea_rescale(int value, int from, int to)
|
|
|
|
{
|
|
|
|
if (from == 0)
|
|
|
|
return 0;
|
2014-02-15 00:16:06 +08:00
|
|
|
if (from == to)
|
|
|
|
return value;
|
2014-03-03 22:04:37 +08:00
|
|
|
if (from > to)
|
|
|
|
return (value + ((value > 0) - (value < 0)) * from/to/2) / (from/to);
|
|
|
|
else
|
|
|
|
return value * (to/from);
|
2014-02-14 22:22:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a fixed-point value to a floating-point value.
|
|
|
|
* Returns NaN for "unknown" values.
|
|
|
|
*/
|
|
|
|
static inline float minmea_float(int value, int scale)
|
|
|
|
{
|
|
|
|
if (scale == 0)
|
|
|
|
return NAN;
|
|
|
|
return (float) value / (float) scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a raw coordinate to a floating point DD.DDD... value.
|
|
|
|
* Returns NaN for "unknown" values.
|
|
|
|
*/
|
|
|
|
static inline float minmea_coord(int value, int scale)
|
|
|
|
{
|
|
|
|
if (scale == 0)
|
|
|
|
return NAN;
|
|
|
|
int degrees = value / (scale * 100);
|
|
|
|
int minutes = value % (scale * 100);
|
|
|
|
return (float) degrees + (float) minutes / (60 * scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* MINMEA_H */
|
|
|
|
|
|
|
|
/* vim: set ts=4 sw=4 et: */
|