-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmea.c
225 lines (185 loc) · 4.26 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
/*
* nmea.c
*
* Created on: Feb 16, 2019
* Author: TritionCubed
*
* An NMEA parsing lib, found on github.
* http://aprs.gids.nl/nmea/#rmc for the formats
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nmea.h"
/*
* Parses a GPGGA sentence (GNPPA on the GPS) from the buffer
* to the struct that represents it.
*/
void nmea_parse_gpgga( char *nmea, gpgga_t *loc ) {
char *p = nmea;
p = strchr( p, ',' ) + 1; //skip time
//parse latitude
p = strchr( p, ',' ) + 1;
loc->latitude = atof( p );
//parse direciton
p = strchr( p, ',' ) + 1;
switch( p[0] ) {
case 'N':
loc->lat = 'N';
break;
case 'S':
loc->lat = 'S';
break;
case ',':
loc->lat = '\0';
break;
}
//parse longitude
p = strchr( p, ',' ) + 1;
loc->longitude = atof( p );
//parse direction
p = strchr( p, ',' ) + 1;
switch( p[0] ) {
case 'W':
loc->lon = 'W';
break;
case 'E':
loc->lon = 'E';
break;
case ',':
loc->lon = '\0';
break;
}
//prase signal quality
p = strchr( p, ',' ) + 1;
loc->quality = (uint8_t) atoi( p );
//parse satellite list
p = strchr( p, ',' ) + 1;
loc->satellites = (uint8_t) atoi( p );
//skip accuracy
p = strchr( p, ',' ) + 1;
//parse altitude
p = strchr( p, ',' ) + 1;
loc->altitude = atof( p );
//implicit skip rest
}
/*
* Parses a GPRMC sentence (GNRMC on the GPS) from the buffer
* to the struct that represents it.
*/
void nmea_parse_gprmc( char *nmea, gprmc_t *loc ) {
char *p = nmea;
//parse time (UTC)
p = strchr( p, ',' ) + 1;
char time[3] = { 0, 0, 0 };
char *res;
//hour
time[0] = p[0];
time[1] = p[1];
loc->hour = strtol( time, &res, 10 );
//minute
time[0] = p[2];
time[1] = p[3];
loc->minute = strtol( time, &res, 10 );
//second
time[0] = p[4];
time[1] = p[5];
loc->second = strtol( time, &res, 10 );
p = strchr( p, ',' ) + 1; //skip status
//parse latitude
p = strchr( p, ',' ) + 1;
loc->latitude = atof( p );
//parse direction
p = strchr( p, ',' ) + 1;
switch( p[0] ) {
case 'N':
loc->lat = 'N';
break;
case 'S':
loc->lat = 'S';
break;
case ',':
loc->lat = '\0';
break;
}
//parse longitude
p = strchr( p, ',' ) + 1;
loc->longitude = atof( p );
//parse direction
p = strchr( p, ',' ) + 1;
switch( p[0] ) {
case 'W':
loc->lon = 'W';
break;
case 'E':
loc->lon = 'E';
break;
case ',':
loc->lon = '\0';
break;
}
//parse speed
p = strchr( p, ',' ) + 1;
loc->speed = atof( p );
//parse course
p = strchr( p, ',' ) + 1;
loc->course = atof( p );
}
/**
* Get the message type (GPGGA, GPRMC, etc..)
*
* This function filters out also wrong packages ( bad sentence, invalid checksum)
*
* Returns an error if one occured, or the type.
*/
uint8_t nmea_get_message_type( const char *message ) {
uint8_t checksum = 0;
//check we're at the start of a sentence
if( *message != '$' ) {
return NMEA_UNKNOWN;
}
//check for validity
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;
}
/*
* Calculates the checksum of the NMEA sentence
* Assume we are at the beginning of a valid string
*
* Returns _EMPTY on success, errors otherwise.
*/
uint8_t nmea_valid_checksum( const char *message ) {
//find the checksum indicator
char *offset = strchr( message, '*' );
//assert it must be there
if( offset == 0 ) {
return NMEA_CHECKSUM_ERR;
}
//find the checksum value, after a * character
uint8_t checksum = (uint8_t) strtol( offset + 1, 0, 16 );
//at this point, we're somewhere before the null terminator
//calculate checksum based on the sentence
uint8_t sum = 0;
message++; //skip over $
//while we haven't reached the null terminator or the end
//of a sentence, keep iterating
while( *message != 0 && *message != '*' ) {
//checksum does not use the delimiters ','
if( *message != ',' ) {
sum ^= *message;
}
message++;
}
if( sum != checksum ) {
return NMEA_CHECKSUM_ERR;
}
return _EMPTY;
}