-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseGGA.c
209 lines (167 loc) · 4.66 KB
/
parseGGA.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "parse.h"
/////////////
// //
// TO DO //
// //
/////////////
// handle different units of altitude
int parseGGA(struct NMEAData *dataStore, char* sentence) {
char token[256];
char* cursor = 0;
char temp[3] = "\0\0\0";
char dmsLat[16]; // (dddommm’sss.ss”\0)
char dmsLon[16]; // Same as above.
float lat = 0.0f;
float lon = 0.0f;
short numSatellites = 0;
float alt = 0.0f;
struct tm tt;
tt.tm_isdst = -1;
////////////////////
// //
// EXTRACT TIME //
// //
////////////////////
tokenize(token, sentence, ",", &cursor); // "hhmmss.ss"
// turn time into a useful value
// get "hh" from "hhmmss.ss"
strncpy(temp, token, 2);
tt.tm_hour = (int) strtol(temp, NULL, 10) - dataStore->localOffset;
// get mm
memcpy(temp, &token[2], 2);
tt.tm_min = (int) strtol(temp, NULL, 10);
// get ss (ignoring .ss)
memcpy(temp, &token[4], 2);
tt.tm_sec = (int) strtol(temp, NULL, 10);
tt.tm_mday = dataStore->date.tm_mday;
tt.tm_mon = dataStore->date.tm_mon;
tt.tm_year = dataStore->date.tm_year;
////////////////////////
// //
// EXTRACT LATITUDE //
// //
////////////////////////
// extract lat val
tokenize(token, sentence, ",", &cursor);
if (strcmp(token, "") != 0)
{
// convert latitude to float
lat = strtof(token, NULL);
convertLat(dmsLat, token);
// extract N/S val
tokenize(token, sentence, ",", &cursor);
// if South, make latitude negative
toupper(token[0]);
if (strcmp(token, "S") == 0)
{
lat *= -1;
}
}
else
return 1;
/////////////////////////
// //
// EXTRACT LONGITUDE //
// //
/////////////////////////
// extract lon val
tokenize(token, sentence, ",", &cursor);
if (strcmp(token, "") != 0)
{
// convert lon to float
lon = strtof(token, NULL);
convertLon(dmsLon, token);
// extract E/W val
tokenize(token, sentence, ",", &cursor);
// if West, make lon negative
toupper(token[0]);
if (strcmp(token, "W") == 0)
{
lon *= -1;
}
}
else
return 1;
///////////////////////
// //
// EXTRACT QUALITY //
// //
///////////////////////
// extract quality val
tokenize(token, sentence, ",", &cursor);
// we don't care about fix quality. Carry on...
//////////////////////////////
// //
// EXTRACT NUM SATELLITES //
// //
//////////////////////////////
// extract number
tokenize(token, sentence, ",", &cursor);
if (strcmp(token, "") != 0)
{
// convert to int
// numSatellites = (short) strtol(token, NULL, 10);
}
else
return 1;
////////////////////////
// //
// EXTRACT DILUTION //
// //
////////////////////////
// extract dilution
tokenize(token, sentence, ",", &cursor);
// we don't care about this. Carry on...
////////////////////////
// //
// EXTRACT ALTITUDE //
// //
////////////////////////
// extract altitude
tokenize(token, sentence, ",", &cursor);
if (strcmp(token, "") != 0)
{
// convert to float
alt = strtof(token, NULL);
tokenize(token, sentence, ",", &cursor);
toupper(token[0]);
if(token[0] == 'F')
{
alt *= 0.3048;
}
else if (token[0] == 'M')
{;}
else
return 1;
}
else
return 1;
if (mktime(&tt) < dataStore->epochTime)
{
puts("Aborting parse: Stale data");
return 2;
}
else
{
if (mktime(&tt) > dataStore->epochTime)
dataStore->isDelta = 1;
dataStore->lat = lat;
dataStore->lon = lon;
dataStore->altitude = alt;
// dataStore->numSatellites = numSatellites;
dataStore->date.tm_hour = tt.tm_hour;
dataStore->date.tm_min = tt.tm_min;
dataStore->date.tm_sec = tt.tm_sec;
// set UTC and TAI
dataStore->epochTime = mktime(&dataStore->date);
dataStore->taiTime = dataStore->epochTime + 34;
strcpy(dataStore->dmsLat, dmsLat);
strcpy(dataStore->dmsLon, dmsLon);
dataStore->allDataSet |= (LATX | LONGX | SATSX | ALTX | TIMEX);
}
return 0;
}