fix: 对不支持weak的bsp, luat_http_client_onevent报重复定义了
[LuatOS.git] / components / minmea / minmea.h
blob9e1dc8ed33aab4de856fbf7e28bc060ab0f916c8
1 /*
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.
7 */
9 #ifndef MINMEA_H
10 #define MINMEA_H
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
16 #include <ctype.h>
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <time.h>
20 #include <math.h>
21 #ifdef MINMEA_INCLUDE_COMPAT
22 #include <minmea_compat.h>
23 #endif
25 #ifndef MINMEA_MAX_SENTENCE_LENGTH
26 #define MINMEA_MAX_SENTENCE_LENGTH 80
27 #endif
29 #define FRAME_TXT_MAX_LEN (79)
31 enum minmea_sentence_id {
32 MINMEA_INVALID = -1,
33 MINMEA_UNKNOWN = 0,
34 MINMEA_SENTENCE_GBS,
35 MINMEA_SENTENCE_GGA,
36 MINMEA_SENTENCE_GLL,
37 MINMEA_SENTENCE_GSA,
38 MINMEA_SENTENCE_GST,
39 MINMEA_SENTENCE_GSV,
40 MINMEA_SENTENCE_RMC,
41 MINMEA_SENTENCE_VTG,
42 MINMEA_SENTENCE_ZDA,
43 MINMEA_SENTENCE_TXT,
45 MINMEA_SENTENCE_MAX_ID
48 struct minmea_float {
49 int_least32_t value;
50 int_least32_t scale;
53 struct minmea_date {
54 int day;
55 int month;
56 int year;
59 struct minmea_time {
60 int hours;
61 int minutes;
62 int seconds;
63 int microseconds;
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;
71 int svid;
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;
79 bool valid;
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;
92 int fix_quality;
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;
120 char status;
121 char mode;
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 {
147 char mode;
148 int fix_type;
149 int sats[12];
150 struct minmea_float pdop;
151 struct minmea_float hdop;
152 struct minmea_float vdop;
153 int sysid;
156 struct minmea_sat_info {
157 int nr;
158 int elevation;
159 int azimuth;
160 int snr;
163 struct minmea_sentence_gsv {
164 int total_msgs;
165 int msg_nr;
166 int total_sats;
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;
181 int hour_offset;
182 int minute_offset;
185 struct minmea_sentence_txt {
186 int count;
187 int index;
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)
256 if (f->scale == 0)
257 return 0;
258 if (f->scale == new_scale)
259 return f->value;
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);
262 else
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)
272 if (f->scale == 0)
273 return 0;
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)
283 if (f->scale == 0)
284 return 0;
285 if (f->scale > (INT_LEAST32_MAX / 100))
286 return 0;
287 if (f->scale < (INT_LEAST32_MIN / 100))
288 return 0;
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)
300 if (f->scale == 0)
301 return 0;
302 if (f->scale > (INT_LEAST32_MAX / 100))
303 return 0;
304 if (f->scale < (INT_LEAST32_MIN / 100))
305 return 0;
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);
309 return ret;
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 != '*';
321 // 扩展
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);
339 enum GNSS_STATE {
340 GNSS_STATE_INIT = 0,
341 GNSS_STATE_FIXED,
342 GNSS_STATE_LOSE,
343 GNSS_STATE_OPEN,
344 GNSS_STATE_CLOSE
347 // typedef union
348 // {
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;
358 // }minmea_data_t;
360 typedef struct minmea_data {
361 char data[LUAT_LIBGNSS_MAX_LINE];
362 uint64_t tm;
363 }minmea_data_t;
365 typedef struct luat_libgnss
367 uint32_t fix_at_ticks;
368 struct minmea_sentence_rmc frame_rmc;
369 minmea_data_t* rmc;
370 minmea_data_t *gga;
371 minmea_data_t *gll;
372 minmea_data_t *gst;
373 minmea_data_t *vtg;
374 minmea_data_t *zda;
375 minmea_data_t *gsv[FRAME_GSV_MAX];
376 minmea_data_t *gsa[FRAME_GSA_MAX];
377 minmea_data_t* txt;
378 uint8_t debug;
379 uint8_t rtc_auto;
380 uint8_t gsa_offset;
381 } luat_libgnss_t;
383 int luat_libgnss_data_check(minmea_data_t* data, uint32_t timeout, uint64_t tnow);
386 #ifdef __cplusplus
388 #endif
390 #endif /* MINMEA_H */
392 /* vim: set ts=4 sw=4 et: */