13static constexpr size_t MAX_FIELDS = 20;
14static constexpr int32_t E7 = 10000000;
21 explicit Fields(
char *
line) {
23 for (
char *p =
line; *p !=
'\0' && this->
count_ < MAX_FIELDS; p++) {
31 const char *operator[](
size_t i)
const {
return i < this->
count_ ? this->
fields_[i] :
""; }
38bool is_digit(
char c) {
return c >=
'0' && c <=
'9'; }
40bool all_digits(
const char *s) {
41 for (; *s !=
'\0'; s++) {
49bool parse_decimal(
const char *s, uint8_t decimals, int32_t &
out) {
50 const bool integer = decimals == 0;
51 const bool negative = *s ==
'-';
55 bool any_digit =
false;
56 bool fraction =
false;
57 for (; *s !=
'\0'; s++) {
58 if (*s ==
'.' && !fraction && !integer) {
69 if (
value > (INT32_MAX - 9) / 10)
76 for (; decimals > 0; decimals--) {
77 if (
value > INT32_MAX / 10)
81 out = negative ? -
static_cast<int32_t
>(
value) : static_cast<int32_t>(
value);
85template<
typename T>
bool parse_unsigned(
const char *s, uint8_t decimals, T &
out) {
87 if (!parse_decimal(s, decimals,
value) ||
value < 0 ||
static_cast<uint32_t>(
value) > std::numeric_limits<T>::max())
93bool parse_two_digits(
const char *s, uint8_t max, uint8_t &
out) {
94 if (!is_digit(s[0]) || !is_digit(s[1]))
96 out = (s[0] -
'0') * 10 + (s[1] -
'0');
101bool parse_coordinate(
const char *s,
const char *hemisphere,
char positive,
char negative, int32_t max_degrees,
103 const char *dot = strchr(s,
'.');
104 const size_t int_len = dot !=
nullptr ? dot - s : strlen(s);
105 if (int_len < 3 || int_len > 5)
108 for (
size_t i = 0; i != int_len; i++) {
115 if (degrees > max_degrees || !parse_decimal(s + int_len - 2, 7, minutes_e7) || minutes_e7 >= 60 * E7)
117 const int32_t
value =
degrees * E7 + (minutes_e7 + 30) / 60;
118 if (
value > max_degrees * E7 || strlen(hemisphere) != 1)
120 if (hemisphere[0] == positive) {
122 }
else if (hemisphere[0] == negative) {
131bool parse_time(
const char *s, GPSData &data) {
132 if (!parse_two_digits(s, 23,
data.hour) || !parse_two_digits(s + 2, 59,
data.minute) ||
133 !parse_two_digits(s + 4, 60,
data.second))
135#ifdef USE_GPS_MILLISECOND
136 data.millisecond = 0;
140 if (s[6] !=
'.' || s[7] ==
'\0' || !all_digits(s + 7))
142#ifdef USE_GPS_MILLISECOND
143 uint16_t scale = 100;
144 for (
const char *p = s + 7; *p !=
'\0' && scale != 0; p++, scale /= 10)
145 data.millisecond += (*p -
'0') * scale;
151bool parse_date(
const char *s, GPSData &data) {
153 if (strlen(s) != 6 || !parse_two_digits(s, 31,
data.day) || !parse_two_digits(s + 2, 12,
data.month) ||
154 !parse_two_digits(s + 4, 99,
year) ||
data.month == 0)
161bool parse_location(
const Fields &f,
size_t first, GPSData &data) {
162 if (*f[first] ==
'\0' && *f[first + 1] ==
'\0' && *f[first + 2] ==
'\0' && *f[first + 3] ==
'\0')
164 if (!parse_coordinate(f[first], f[first + 1],
'N',
'S', 90,
data.latitude_e7) ||
165 !parse_coordinate(f[first + 2], f[first + 3],
'E',
'W', 180,
data.longitude_e7))
172template<
typename P>
bool parse_optional(
const char *s,
GPSField field, GPSData &data, P &&parse) {
177 data.updated |= field;
182bool parse_gga(
const Fields &f, GPSData &data) {
184 if (*f[6] !=
'\0' && !parse_unsigned(f[6], 0, quality))
187 [&data](
const char *s) {
return parse_unsigned(s, 0,
data.satellites); }) ||
189 [&data](
const char *s) {
return parse_unsigned(s, 2,
data.hdop_e2); }))
194 if (!parse_optional(f[1],
GPS_FIELD_TIME, data, [&data](
const char *s) {
return parse_time(s, data); }) ||
195 !parse_location(f, 2, data) || !parse_optional(f[9],
GPS_FIELD_ALTITUDE, data, [&data](
const char *s) {
196 return parse_decimal(s, 3,
data.altitude_e3);
199#ifdef USE_GPS_GEOID_SEPARATION
201 [&data](
const char *s) {
return parse_decimal(s, 3,
data.geoid_separation_e3); });
208bool parse_rmc(
const Fields &f, GPSData &data) {
210 if (*f[2] ==
'\0' || strcmp(f[2],
"V") == 0)
212 if (strcmp(f[2],
"A") != 0)
214 return parse_optional(f[1],
GPS_FIELD_TIME, data, [&data](
const char *s) {
return parse_time(s, data); }) &&
215 parse_location(f, 3, data) &&
217 [&data](
const char *s) {
return parse_unsigned(s, 3,
data.speed_e3); }) &&
220 [&data](
const char *s) {
return parse_unsigned(s, 2,
data.course_e2) &&
data.course_e2 <= 36000; }) &&
221 parse_optional(f[9],
GPS_FIELD_DATE, data, [&data](
const char *s) {
return parse_date(s, data); });
234 char *star = strchr(start,
'*');
236 if (star ==
nullptr || strlen(star + 1) != 2 || !
parse_hex(star + 1, &
checksum, 1))
238 if (
xor_bytes(
reinterpret_cast<const uint8_t *
>(start + 1), star - start - 1) !=
checksum) {
239 ESP_LOGV(TAG,
"Bad checksum: %s", start);
243 const Fields fields(start + 1);
244 const char *
address = fields[0];
252 if (strcmp(
type,
"GGA") == 0) {
253 ok = parse_gga(fields,
next);
254 }
else if (strcmp(
type,
"RMC") == 0) {
255 ok = parse_rmc(fields,
next);
260 ESP_LOGV(TAG,
"Malformed %s sentence",
address);
std::vector< uint8_t > & out
bool parse_nmea_sentence(char *line, size_t len, GPSData &data)
Parse one NMEA line of len bytes, which is changed in place; the sentence starts at the last '$'.
ESPHOME_LOG_TAG(TAG, "gps")
GPSField
Bits of GPSData::updated and GPSData::valid.
@ GPS_FIELD_GEOID_SEPARATION
std::span< const uint8_t > data
constexpr uint8_t xor_bytes(const uint8_t *data, size_t len, uint8_t init=0)
XOR the bytes of data with size len, starting from init.
uint8_t days_in_month(uint8_t month, uint16_t year)
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
const char * fields_[MAX_FIELDS]
Values from GGA and RMC sentences as scaled integers; a name ending in eN is the value times 10^N.