forked from wertarbyte/tiny-gps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmea.c
498 lines (465 loc) · 10.2 KB
/
nmea.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "config.h"
#if USE_GPS
/* NMEA parser */
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "nmea.h"
#if __AVR__
#include <util/atomic.h>
#define ATOMIC(t) ATOMIC_BLOCK(t)
#else
#define ATOMIC(t)
#endif
/* these structs are used as a construction site
* during the parsing process; once a sentence has
* been received completely, we transfer the useful
* data to the output struct
*/
static union {
#if PARSE_GPS_NMEA_RMC
struct nmea_rmc_t rmc;
#endif
#if PARSE_GPS_NMEA_GGA
struct nmea_gga_t gga;
#endif
} nmea_wip;
/* this is the data we will be offering */
static struct nmea_data_t *nmea_data = NULL;
static enum {
GP_UNKNOWN,
GP_RMC,
GP_GGA,
} sentence = GP_UNKNOWN;
static enum {
CS_UNKNOWN, /* no checksum is available */
CS_CALC, /* calculating checksum */
CS_READ, /* reading transmitted checksum */
CS_VALID, /* transmitted checksum did not match the calculated one */
CS_INVALID, /* the transmitted checksum did not match the calculated one */
} checksum_state = CS_UNKNOWN;
static uint8_t checksum = 0;
static uint8_t token_nr = 0;
#define TOKEN_BUFFER_SIZE 11
static char token_buffer[TOKEN_BUFFER_SIZE] = "";
static void parse_to_bcd(char *s, uint8_t *b, uint8_t max) {
uint8_t i = 0;
while (i<max && s[i]) {
uint8_t n = (s[i]-'0');
if (i%2 == 1) {
b[i/2] |= (n << 4);
} else {
b[i/2] |= (0x0F & n);
}
i++;
}
}
static void parse_coord(struct coord *co) {
/* find the decimal point */
char *ptr = token_buffer;
while (*ptr && *ptr != '.') ptr++;
if (*ptr == '.') {
/* we found the point */
*ptr = '\0';
ptr++;
} else {
/* something weird happened,
* there is no decimal point?!
*/
return;
}
/* two digits before the decimal point
* are the minutes
*/
co->min = atoi(ptr-3);
*(ptr-3) = '\0';
co->deg = atoi(token_buffer);
/* now we BCD encode as many fractions
* of a minute as we can
*/
parse_to_bcd(ptr, co->frac, NMEA_MINUTE_FRACTS);
}
#if PARSE_GPS_ALTITUDE
static void parse_altitude(struct altitude_t *a) {
/* find the decimal point */
char *p = token_buffer;
while (*p && *p != '.') p++;
if (*p == '.') {
/* we found the point */
*p = '\0';
/* now we BCD encode as many fractions
* as we can
*/
parse_to_bcd(p+1, a->frac, NMEA_ALTITUDE_FRACTS);
}
/* parse the integer part */
a->m = atoi(token_buffer);
}
#endif
#if PARSE_GPS_TIME
static void parse_clock(struct clock_t *cl) {
token_buffer[6] = '\0';
cl->second = atoi(&token_buffer[4]);
token_buffer[4] = '\0';
cl->minute = atoi(&token_buffer[2]);
token_buffer[2] = '\0';
cl->hour = atoi(&token_buffer[0]);
}
static void parse_date(struct date_t *d) {
token_buffer[6] = '\0';
d->year = atoi(&token_buffer[4]);
token_buffer[4] = '\0';
d->month = atoi(&token_buffer[2]);
token_buffer[2] = '\0';
d->day = atoi(&token_buffer[0]);
}
#endif
#if PARSE_GPS_NMEA_RMC
static void process_gprmc_token(void) {
switch (token_nr) {
#if !PARSE_GPS_NMEA_GGA /* avoid duplicate parsing code */
#if PARSE_GPS_TIME
case 1:
/* time
* HHMMSS(.sssss)
*/
parse_clock(&nmea_wip.rmc.clock);
break;
#endif
case 2:
/* status
* A OK
* V Warning
*/
if (token_buffer[0] == 'A') {
nmea_wip.rmc.flags |= (1<<NMEA_RMC_FLAGS_STATUS_OK);
} else {
nmea_wip.rmc.flags &= ~(1<<NMEA_RMC_FLAGS_STATUS_OK);
}
break;
case 3:
/* latitude
* BBBB.BBBB
*/
parse_coord(&nmea_wip.rmc.lat);
break;
case 4:
/* orientation
* N north
* S south
*/
if (token_buffer[0] == 'N') {
nmea_wip.rmc.flags |= (1<<NMEA_RMC_FLAGS_LAT_NORTH);
} else {
nmea_wip.rmc.flags &= ~(1<<NMEA_RMC_FLAGS_LAT_NORTH);
}
break;
case 5:
/* longitude
* LLLLL.LLLL
*/
parse_coord(&nmea_wip.rmc.lon);
break;
case 6:
/* orientation
* E east
* W west
*/
if (token_buffer[0] == 'E') {
nmea_wip.rmc.flags |= (1<<NMEA_RMC_FLAGS_LON_EAST);
} else {
nmea_wip.rmc.flags &= ~(1<<NMEA_RMC_FLAGS_LON_EAST);
}
break;
#endif
case 7:
/* speed
* GG.G
*/
break;
case 8:
/* course
* RR.R
*/
break;
#if PARSE_GPS_TIME
case 9:
/* date
* DDMMYY
*/
parse_date(&nmea_wip.rmc.date);
break;
#endif
case 10:
/* magnetic declination
* M.M
*/
break;
case 11:
/* sign of declination
* E east
* W west
*/
break;
case 12:
/* signal integrity
* A autonomous mode
* D differential mode
* E estimated mode
* M manual input mode
* S simulated mode
* N data not valid
*/
break;
}
}
#endif
#if PARSE_GPS_NMEA_GGA
static void process_gpgga_token(void) {
switch (token_nr) {
#if PARSE_GPS_TIME
case 1:
/* time
* HHMMSS(.sssss)
*/
parse_clock(&nmea_wip.gga.clock);
break;
#endif
case 2:
/* latitude
* BBBB.BBBB
*/
parse_coord(&nmea_wip.gga.lat);
break;
case 3:
/* orientation
* N north
* S south
*/
if (token_buffer[0] == 'N') {
nmea_wip.gga.flags |= (1<<NMEA_RMC_FLAGS_LAT_NORTH);
} else {
nmea_wip.gga.flags &= ~(1<<NMEA_RMC_FLAGS_LAT_NORTH);
}
break;
case 4:
/* longitude
* LLLLL.LLLL
*/
parse_coord(&nmea_wip.gga.lon);
break;
case 5:
/* orientation
* E east
* W west
*/
if (token_buffer[0] == 'E') {
nmea_wip.gga.flags |= (1<<NMEA_RMC_FLAGS_LON_EAST);
} else {
nmea_wip.gga.flags &= ~(1<<NMEA_RMC_FLAGS_LON_EAST);
}
break;
case 6:
/* signal quality */
nmea_wip.gga.quality = atoi(token_buffer);
if (nmea_wip.gga.quality) {
nmea_wip.gga.flags |= (1<<NMEA_RMC_FLAGS_STATUS_OK);
} else {
nmea_wip.gga.flags &= ~(1<<NMEA_RMC_FLAGS_STATUS_OK);
}
break;
case 7:
/* number of used satellites */
nmea_wip.gga.sats = atoi(token_buffer);
break;
#if PARSE_GPS_ALTITUDE
case 9:
/* altitude */
parse_altitude(&nmea_wip.gga.alt);
break;
#endif
default:
/* nothing to do */
break;
}
}
#endif
static void sentence_started(void) {
/* a new sentence has started, we do not know which yet */
sentence = GP_UNKNOWN;
/* clear token buffer */
token_buffer[0] = '\0';
token_nr = 0;
}
static void sentence_finished(void) {
/* the entire sentence has been read;
* now copy the constructed data to the ouput struct
* if the checksum matches.
*/
if (checksum_state == CS_INVALID) {
return;
}
switch (sentence) {
#if PARSE_GPS_NMEA_RMC
case GP_RMC:
/* copy date, time and location */
#if PARSE_GPS_TIME
memcpy(&nmea_data->date, &nmea_wip.rmc.date, sizeof(nmea_wip.rmc.date));
memcpy(&nmea_data->clock, &nmea_wip.rmc.clock, sizeof(nmea_wip.rmc.clock));
#endif
#if !PARSE_GPS_NMEA_GGA /* avoid duplicate parsing code */
nmea_data->flags = nmea_wip.rmc.flags;
memcpy(&nmea_data->lon, &nmea_wip.rmc.lon, sizeof(nmea_wip.rmc.lon));
memcpy(&nmea_data->lat, &nmea_wip.rmc.lat, sizeof(nmea_wip.rmc.lat));
#endif
break;
#endif
#if PARSE_GPS_NMEA_GGA
case GP_GGA:
#if PARSE_GPS_TIME
memcpy(&nmea_data->clock, &nmea_wip.gga.clock, sizeof(nmea_wip.gga.clock));
#endif
nmea_data->flags = nmea_wip.gga.flags;
memcpy(&nmea_data->lon, &nmea_wip.gga.lon, sizeof(nmea_wip.gga.lon));
memcpy(&nmea_data->lat, &nmea_wip.gga.lat, sizeof(nmea_wip.gga.lat));
/* copy quality and number of satellites */
nmea_data->quality = nmea_wip.gga.quality;
nmea_data->sats = nmea_wip.gga.sats;
#if PARSE_GPS_ALTITUDE
/* copy altitude */
memcpy(&nmea_data->alt, &nmea_wip.gga.alt, sizeof(nmea_wip.gga.alt));
#endif
break;
#endif
default:
break;
}
}
static void gp_token_finished(void) {
/* a token of the nmea sentence has been completed */
switch (sentence) {
case GP_UNKNOWN:
if (token_nr == 0) {
/* clear the building site */
memset(&nmea_wip, 0, sizeof(nmea_wip));
/* the first token defines the sentence type */
#if PARSE_GPS_NMEA_RMC
if (strcmp(token_buffer, "GPRMC") == 0) {
sentence = GP_RMC;
} else
#endif
#if PARSE_GPS_NMEA_GGA
if (strcmp(token_buffer, "GPGGA") == 0) {
sentence = GP_GGA;
}
#else
{}
#endif
}
break;
#if PARSE_GPS_NMEA_RMC
case GP_RMC:
/* process data of the minimal data set */
process_gprmc_token();
break;
#endif
#if PARSE_GPS_NMEA_GGA
case GP_GGA:
process_gpgga_token();
break;
#endif
default:
/* don't know what to do with it */
break;
}
token_buffer[0] = '\0';
token_nr++;
}
static uint8_t hex_digit(char c) {
if (c >= '0' && c <= '9') {
return (c - '0');
} else if (c >= 'A' && c <= 'F') {
return (10+(c - 'A'));
} else if (c >= 'a' && c <= 'f') {
return (10+(c - 'a'));
} else {
return 0;
}
}
static uint8_t hex_to_short(char *s) {
uint8_t r = 0;
uint8_t i = 1;
char *w = s;
while (*w) w++; /* now *w == '\0' */
while (w > s) {
w--;
r += i*hex_digit(*w);
i *= 16;
}
return r;
}
static void token_finished(void) {
/* a token has been completed, process the content in the buffer */
if (checksum_state != CS_READ) {
/* it was a normal token and not the checksum */
gp_token_finished();
} else {
/* did we receive a checksum? */
if (token_buffer[0] == '\0') {
/* there is no checksum */
checksum_state = CS_UNKNOWN;
} else if ( hex_to_short(token_buffer) == checksum ) {
/* the received checksum does match our calculated one */
checksum_state = CS_VALID;
} else {
/* something strange happened */
checksum_state = CS_INVALID;
}
}
}
static void append_to_token(const char c) {
uint8_t l = strlen(token_buffer);
if (l < (TOKEN_BUFFER_SIZE-1)) {
token_buffer[l] = c;
token_buffer[l+1] = '\0';
}
}
static void add_to_checksum(const char c) {
checksum ^= c;
}
void nmea_init(struct nmea_data_t *output) {
nmea_data = output;
}
void nmea_process_character(char c) {
switch (c) {
case '$': /* a new sentence is starting */
sentence_started();
/* reset and enable checksum calculation */
checksum = 0;
checksum_state = CS_CALC;
break;
case ',':
token_finished();
break;
case '*': /* checksum is following */
token_finished();
checksum_state = CS_READ;
break;
case '\r':
/* \n is following soon, we ignore this */
break;
case '\n':
token_finished();
ATOMIC(ATOMIC_FORCEON) {
sentence_finished();
}
checksum_state = CS_UNKNOWN;
break;
default:
append_to_token(c);
}
if (checksum_state == CS_CALC && c != '$') {
add_to_checksum(c);
}
}
#endif