-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmea.cpp
160 lines (136 loc) · 2.96 KB
/
nmea.cpp
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
/** @file nmea.cpp
* @brief NMEA Functions
*/
#include "nmea.h"
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
/** @brief Parses GPGGA Message
* @author Paul Moggach
* @date 25MAR2019
* @param char * nmea Pointer to message
* @param gpgg_t * loc Pointer to output data structure
*/
void nmea_parse_gpgga(char *nmea, gpgga_t *loc) {
char *p = nmea;
p = strchr(p, ',') + 1; // time
loc->utc = atof(p);
p = strchr(p, ',') + 1;
loc->latitude = atof(p);
p = strchr(p, ',') + 1;
switch (p[0]) {
case 'N':
loc->lat = 'N';
break;
case 'S':
loc->lat = 'S';
break;
case ',':
loc->lat = '\0';
break;
}
p = strchr(p, ',') + 1;
loc->longitude = atof(p);
p = strchr(p, ',') + 1;
switch (p[0]) {
case 'W':
loc->lon = 'W';
break;
case 'E':
loc->lon = 'E';
break;
case ',':
loc->lon = '\0';
break;
}
p = strchr(p, ',') + 1;
loc->quality = (uint8_t)atoi(p);
p = strchr(p, ',') + 1;
loc->satellites = (uint8_t)atoi(p);
p = strchr(p, ',') + 1;
p = strchr(p, ',') + 1;
loc->altitude = atof(p);
}
/** @brief Parses GPRMC Message
* @author Paul Moggach
* @date 25MAR2019
* @param char * nmea Pointer to message
* @param gpgg_t * loc Pointer to output data structure
*/
void nmea_parse_gprmc(char *nmea, gprmc_t *loc) {
char *p = nmea;
p = strchr(p, ',') + 1; // skip time
p = strchr(p, ',') + 1; // skip status
p = strchr(p, ',') + 1;
loc->latitude = atof(p);
p = strchr(p, ',') + 1;
switch (p[0]) {
case 'N':
loc->lat = 'N';
break;
case 'S':
loc->lat = 'S';
break;
case ',':
loc->lat = '\0';
break;
}
p = strchr(p, ',') + 1;
loc->longitude = atof(p);
p = strchr(p, ',') + 1;
switch (p[0]) {
case 'W':
loc->lon = 'W';
break;
case 'E':
loc->lon = 'E';
break;
case ',':
loc->lon = '\0';
break;
}
p = strchr(p, ',') + 1;
loc->speed = atof(p);
p = strchr(p, ',') + 1;
loc->course = atof(p);
p = strchr(p, ',') + 1;
loc->date = atof(p);
}
/** @brief Get the message type (GPGGA, GPRMC, etc..)
* @param message The NMEA message
* @return The type of message if it is valid
*/
uint8_t nmea_get_message_type(const char *message) {
uint8_t checksum = 0;
if ((checksum = nmea_valid_checksum(message)) != _EMPTY) {
return checksum;
}
if (strstr(message, NMEA_GPGGA_STR) != NULL) {
return NMEA_GPGGA;
}
if (strstr(message, NMEA_GPRMC_STR) != NULL) {
return NMEA_GPRMC;
}
return NMEA_UNKNOWN;
}
/** @brief Checks Message Checksum
* @author Paul Moggach
* @date 25MAR2019
* @param char * nmea Pointer to message
* @return uint8_t Checksum
*/
uint8_t nmea_valid_checksum(const char *message) {
uint8_t checksum = (uint8_t)strtol(strchr(message, '*') + 1, NULL, 16);
char p;
uint8_t sum = 0;
++message;
while ((p = *message++) != '*') {
sum ^= p;
}
if (sum != checksum) {
return NMEA_CHECKSUM_ERR;
}
return _EMPTY;
}