ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
nmea0183.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file nmea0183.h
6 * @brief NMEA 0183 sentence codec (PROTOCORE_ENABLE_NMEA0183) - the marine / GPS ASCII protocol.
7 *
8 * NMEA 0183 sentences look like `$GPGGA,123519,4807.038,N,...*47<CR><LF>`: a `$` (or `!` for
9 * AIS-encapsulated), a comma-separated field list whose first field is the 5-char address
10 * (2-char talker id + 3-char sentence type), a `*`, and a two-hex-digit XOR checksum. This
11 * codec builds a sentence (adding the `$`, checksum, and CR/LF) and parses one (validating the
12 * checksum and splitting the fields), with `cellul.to_float` / `cellul.to_long` field-value helpers.
13 *
14 * GPS / marine receivers are cheap UART breakouts, so on an ESP32 this is a plain
15 * `HardwareSerial` link (commonly 4800 or 9600 baud); the UART is the application's. Pure
16 * codec, host-tested. Bridge position / wind / depth data onto Wi-Fi.
17 *
18 * @author Douglas Quigg (dstroy0)
19 * @date 2026
20 */
21
22#ifndef PROTOCORE_NMEA0183_H
23#define PROTOCORE_NMEA0183_H
24
25#include "protocore_config.h"
26
27#if PROTOCORE_ENABLE_NMEA0183
28
29EMBED_BEGIN_DECLS
30
31/** @brief A parsed NMEA 0183 sentence. Field pointers reference the caller's buffer. */
32typedef struct Nmea0183
33{
34 char talker[3]; ///< 2-char talker id (e.g. "GP") + NUL
35 char type[4]; ///< 3-char sentence type (e.g. "GGA") + NUL
36 uint8_t field_count; ///< number of fields, including field 0 (the address)
37 const char *fields[PROTOCORE_NMEA0183_MAX_FIELDS]; ///< field 0 is the address; data is 1..n
38 uint8_t field_len[PROTOCORE_NMEA0183_MAX_FIELDS]; ///< each field's length (0 for an empty field)
39} Nmea0183;
40
41/** @brief XOR checksum over @p len octets (the sentence body between `$` and `*`). */
42uint8_t protocore_nmea0183_checksum(const char *s, size_t len);
43
44/**
45 * @brief Build a sentence from @p body (e.g. "GPGGA,123519,..."): writes `$<body>*HH\r\n` and
46 * NUL-terminates. Returns the length (excluding the NUL) or 0 on overflow.
47 */
48size_t protocore_nmea0183_build(char *buf, size_t cap, const char *body);
49
50/**
51 * @brief Parse a sentence: requires a leading `$`/`!`, validates the `*HH` XOR checksum, and
52 * splits the comma-separated fields. Fills @p out (field 0 is the address; talker / type are
53 * derived from it). Returns false on a bad frame or checksum.
54 */
55embed_bool protocore_nmea0183_parse(const char *s, size_t len, Nmea0183 *out);
56
57/** @brief Decode field @p idx as a float (false if absent / empty / non-numeric). */
58embed_bool protocore_nmea0183_field_float(const Nmea0183 *m, uint8_t idx, float *out);
59
60/** @brief Decode field @p idx as a long integer (false if absent / empty / non-numeric). */
61embed_bool protocore_nmea0183_field_int(const Nmea0183 *m, uint8_t idx, long *out);
62
63// -- typed decoders for the two common GPS position sentences --
64//
65// These lift a parsed sentence (protocore_nmea0183_parse) into a position struct: the ddmm.mmmm coordinates
66// become signed decimal degrees (hemisphere-adjusted), and the hhmmss.ss / ddmmyy fields split into their
67// components. Coordinates are read through the float field helper, so their precision is float's (~1 m).
68
69/** @brief Decoded GGA (fix data). */
70typedef struct
71{
72 uint8_t hour; ///< UTC time of the fix
73 uint8_t minute; ///< UTC time of the fix
74 float second;
75 double lat_deg; ///< latitude in signed decimal degrees (+ N, - S); 0 if absent
76 double lon_deg; ///< longitude in signed decimal degrees (+ E, - W); 0 if absent
77 uint8_t fix_quality; ///< 0 = no fix, 1 = GPS, 2 = DGPS, ... (field 6)
78 uint8_t num_sats; ///< satellites used in the fix
79 float hdop; ///< horizontal dilution of precision
80 float alt_m; ///< altitude above mean sea level (metres)
81} protocore_nmea_gga;
82
83/** @brief Decoded RMC (recommended minimum: position + velocity + date). */
84typedef struct
85{
86 embed_bool valid; ///< true when the status field is 'A' (data valid), false for 'V' (warning)
87 uint8_t hour; ///< UTC time
88 uint8_t minute; ///< UTC time
89 float second;
90 uint8_t day; ///< UTC date (year is the 2-digit field value, 0..99)
91 uint8_t month; ///< UTC date (year is the 2-digit field value, 0..99)
92 uint8_t year; ///< UTC date (year is the 2-digit field value, 0..99)
93 double lat_deg; ///< latitude in signed decimal degrees
94 double lon_deg; ///< longitude in signed decimal degrees
95 float speed_knots; ///< speed over ground (knots)
96 float course_deg; ///< course over ground (degrees true)
97} protocore_nmea_rmc;
98
99/**
100 * @brief Decode a parsed GGA sentence into @p out. @return true iff @p m is a GGA sentence with enough
101 * fields; false (and @p out untouched) otherwise. Empty optional fields read back as 0.
102 */
103embed_bool protocore_nmea0183_parse_gga(const Nmea0183 *m, protocore_nmea_gga *out);
104
105/**
106 * @brief Decode a parsed RMC sentence into @p out. @return true iff @p m is an RMC sentence with enough
107 * fields; false otherwise. @c valid reflects the A/V status field (a 'V' sentence still decodes).
108 */
109embed_bool protocore_nmea0183_parse_rmc(const Nmea0183 *m, protocore_nmea_rmc *out);
110
111/** @brief One satellite record from a GSV sentence. */
112typedef struct
113{
114 uint8_t prn; ///< satellite PRN / id
115 int16_t elev_deg; ///< elevation (degrees, 0..90)
116 int16_t azim_deg; ///< azimuth (degrees true, 0..359)
117 uint8_t snr_db; ///< signal-to-noise ratio (dB-Hz), valid only when @ref snr_valid
118 embed_bool snr_valid; ///< false when the SNR field is blank (the satellite is not being tracked)
119} protocore_nmea_gsv_sat;
120
121/** @brief Decoded GSV (satellites in view). One sentence carries up to four satellite records; a full sky
122 * view spans @ref total_msgs sentences. */
123typedef struct
124{
125 uint8_t total_msgs; ///< total GSV sentences in this cycle
126 uint8_t msg_num; ///< this sentence's number (1-based)
127 uint8_t sats_in_view; ///< total satellites in view across the cycle
128 uint8_t sat_count; ///< satellite records present in THIS sentence (0..4)
129 protocore_nmea_gsv_sat sats[4];
130} protocore_nmea_gsv;
131
132/**
133 * @brief Decode a parsed GSV sentence into @p out. @return true iff @p m is a GSV sentence with at least
134 * the 3-field header. The per-satellite records present in this sentence are filled (0..4).
135 */
136embed_bool protocore_nmea0183_parse_gsv(const Nmea0183 *m, protocore_nmea_gsv *out);
137
138/** @brief Decoded ZDA (UTC time + calendar date + local zone offset). Unlike RMC this carries the full
139 * 4-digit year, so it is the sentence to read for wall-clock time sync. */
140typedef struct
141{
142 uint8_t hour; ///< UTC time
143 uint8_t minute; ///< UTC time
144 float second;
145 uint8_t day; ///< UTC date
146 uint8_t month; ///< UTC date
147 uint16_t year; ///< UTC year (4-digit)
148 int8_t zone_hours; ///< local zone offset hours (-13..+13); 0 if the field is absent
149 uint8_t zone_minutes; ///< local zone offset minutes (0..59); 0 if the field is absent
150} protocore_nmea_zda;
151
152/**
153 * @brief Decode a parsed ZDA sentence into @p out. @return true iff @p m is a ZDA sentence with at least
154 * the time / day / month / year fields; false otherwise. The zone offset reads back 0 when absent.
155 */
156embed_bool protocore_nmea0183_parse_zda(const Nmea0183 *m, protocore_nmea_zda *out);
157
158/** @brief Decoded VTG (course over ground + ground speed). The course-over-ground vector complements the
159 * RMC/GGA position - it is the sentence to read for heading and speed. */
160typedef struct
161{
162 float course_true_deg; ///< course over ground, degrees true (0 if the field is absent)
163 float course_mag_deg; ///< course over ground, degrees magnetic (0 if absent)
164 float speed_knots; ///< speed over ground in knots (0 if absent)
165 float speed_kmh; ///< speed over ground in km/h (0 if absent)
166 char mode; ///< NMEA 2.3+ mode indicator ('A'/'D'/'E'/'N'), or '\0' when the field is absent
167} protocore_nmea_vtg;
168
169/**
170 * @brief Decode a parsed VTG sentence into @p out. @return true iff @p m is a VTG sentence with at least the
171 * course / speed fields (through the km/h unit); false otherwise. The mode reads back '\0' when absent.
172 */
173embed_bool protocore_nmea0183_parse_vtg(const Nmea0183 *m, protocore_nmea_vtg *out);
174
175/** @brief Decoded GSA (GPS DOP + the satellites active in the fix). Where GSV lists satellites in view and
176 * GGA gives the fix quality, GSA gives the 2D/3D fix mode, which satellites were used, and all three DOPs. */
177typedef struct
178{
179 char mode; ///< selection mode: 'M' manual, 'A' automatic 2D/3D
180 uint8_t fix_type; ///< 1 = no fix, 2 = 2D, 3 = 3D
181 uint8_t sat_count; ///< number of satellite PRNs present (0..12)
182 uint8_t sats[12]; ///< PRNs of the satellites used in the fix
183 float pdop; ///< position (3D) dilution of precision
184 float hdop; ///< horizontal dilution of precision
185 float vdop; ///< vertical dilution of precision
186} protocore_nmea_gsa;
187
188/**
189 * @brief Decode a parsed GSA sentence into @p out. @return true iff @p m is a GSA sentence with the full
190 * field set (through VDOP). False otherwise. Empty PRN slots are skipped (not counted).
191 */
192embed_bool protocore_nmea0183_parse_gsa(const Nmea0183 *m, protocore_nmea_gsa *out);
193
194/** @brief Decoded MWV (wind speed + angle): the standard wind-instrument sentence. */
195typedef struct
196{
197 float wind_angle_deg; ///< wind angle (0..359 degrees)
198 char reference; ///< 'R' relative (apparent) or 'T' true, or '\0' if the field is absent
199 float wind_speed; ///< wind speed, in the units given by @ref speed_units
200 char speed_units; ///< 'K' km/h, 'M' m/s, 'N' knots, or '\0' if absent
201 embed_bool valid; ///< status field: true for 'A' (valid), false for 'V'
202} protocore_nmea_mwv;
203
204/**
205 * @brief Decode a parsed MWV sentence into @p out. @return true iff @p m is an MWV sentence with the angle /
206 * reference / speed / units / status fields; false otherwise.
207 */
208embed_bool protocore_nmea0183_parse_mwv(const Nmea0183 *m, protocore_nmea_mwv *out);
209
210/** @brief Decoded DPT (depth of water): depth relative to the transducer plus the transducer offset. */
211typedef struct
212{
213 float depth_m; ///< water depth relative to the transducer (meters)
214 float offset_m; ///< transducer offset (meters): + = transducer to waterline, - = transducer to keel
215 embed_bool has_range; ///< true when the optional maximum-range-scale field is present
216 float range_m; ///< maximum range scale in use (meters), valid only when @ref has_range
217} protocore_nmea_dpt;
218
219/**
220 * @brief Decode a parsed DPT sentence into @p out. @return true iff @p m is a DPT sentence with the depth
221 * and offset fields; false otherwise. The optional range scale sets @ref protocore_nmea_dpt::has_range.
222 */
223embed_bool protocore_nmea0183_parse_dpt(const Nmea0183 *m, protocore_nmea_dpt *out);
224
225/** @brief Decoded HDG (magnetic heading + deviation + variation): the compass sentence. The deviation and
226 * variation are signed with East positive / West negative, so true heading = heading + deviation + variation. */
227typedef struct
228{
229 float heading_deg; ///< magnetic sensor heading (degrees)
230 float deviation_deg; ///< magnetic deviation (degrees), + East / - West; 0 if absent
231 float variation_deg; ///< magnetic variation (degrees), + East / - West; 0 if absent
232} protocore_nmea_hdg;
233
234/**
235 * @brief Decode a parsed HDG sentence into @p out. @return true iff @p m is an HDG sentence with the heading
236 * / deviation / variation fields. False otherwise. The E/W direction fields are folded into the sign.
237 */
238embed_bool protocore_nmea0183_parse_hdg(const Nmea0183 *m, protocore_nmea_hdg *out);
239
240/** @brief Decoded GLL (geographic position - latitude / longitude): position + UTC time + validity. Where
241 * GGA carries the full fix quality and RMC adds velocity, GLL is the minimal position report. */
242typedef struct
243{
244 double lat_deg; ///< latitude in signed decimal degrees (+ N, - S); 0 if absent
245 double lon_deg; ///< longitude in signed decimal degrees (+ E, - W); 0 if absent
246 uint8_t hour; ///< UTC time of the position
247 uint8_t minute; ///< UTC time of the position
248 float second;
249 embed_bool valid; ///< true when the status field is 'A' (data valid), false for 'V' (warning)
250 char mode; ///< FAA mode indicator (NMEA 2.3+), or '\0' if the field is absent
251} protocore_nmea_gll;
252
253/**
254 * @brief Decode a parsed GLL sentence into @p out. @return true iff @p m is a GLL sentence through the
255 * status field; false otherwise. @c valid reflects the A/V status (a 'V' sentence still decodes);
256 * the FAA mode indicator is set only when the (optional, NMEA 2.3+) field is present.
257 */
258embed_bool protocore_nmea0183_parse_gll(const Nmea0183 *m, protocore_nmea_gll *out);
259
260/** @brief Decoded VHW (water speed + heading): the vessel's heading and its speed through the water. Where VTG
261 * reports GPS course + speed over ground, VHW reports the heading the vessel points and its speed relative to
262 * the water (from a paddlewheel / pitot log). */
263typedef struct
264{
265 float heading_true_deg; ///< vessel heading, degrees true (0 if the field is absent)
266 float heading_mag_deg; ///< vessel heading, degrees magnetic (0 if absent)
267 float speed_knots; ///< speed through the water in knots (0 if absent)
268 float speed_kmh; ///< speed through the water in km/h (0 if absent)
269} protocore_nmea_vhw;
270
271/**
272 * @brief Decode a parsed VHW sentence into @p out. @return true iff @p m is a VHW sentence with at least the
273 * heading / speed fields (through the km/h value); false otherwise. Empty optional fields read back 0.
274 */
275embed_bool protocore_nmea0183_parse_vhw(const Nmea0183 *m, protocore_nmea_vhw *out);
276
277/** @brief Decoded VLW (distance traveled through the water): the cumulative and trip water-distance log from a
278 * paddlewheel / pitot log - the marine odometer, the through-water companion to VHW's speed. */
279typedef struct
280{
281 float total_water_nm; ///< total cumulative water distance (nautical miles; 0 if absent)
282 float trip_water_nm; ///< water distance since the last reset (nautical miles; 0 if absent)
283} protocore_nmea_vlw;
284
285/**
286 * @brief Decode a parsed VLW sentence into @p out. @return true iff @p m is a VLW sentence with at least the
287 * total + trip water-distance fields; false otherwise. Empty optional fields read back 0.
288 */
289embed_bool protocore_nmea0183_parse_vlw(const Nmea0183 *m, protocore_nmea_vlw *out);
290
291EMBED_END_DECLS
292
293#endif // PROTOCORE_ENABLE_NMEA0183
294
295#endif // PROTOCORE_NMEA0183_H