-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseRMC.c
203 lines (161 loc) · 4.8 KB
/
parseRMC.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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "parse.h"
// Carole Fountain //
int parseRMC(struct NMEAData *dataStore, char* sentence) {
char token[256], *cursor = 0;
char dmsLat[16]; // (dddommm’sss.ss”\0)
char dmsLon[16]; // Same as above.
char temp[3] = "\0\0\0";
float lat = 0;
float lon = 0;
struct tm t_time;
t_time.tm_isdst = -1;
//////////////////////////////
// //
// Extract UTC time //
// //
//////////////////////////////
tokenize(token, sentence, ",", &cursor); // "hhmmss.ss"
// turn time into a useful value
// get "hh" from "hhmmss.ss"
strncpy(temp, token, 2);
t_time.tm_hour = (int) strtol(temp, NULL, 10) - dataStore->localOffset;
// get mm
memcpy(temp, &token[2], 2);
t_time.tm_min = (int) strtol(temp, NULL, 10);
// get ss (ignoring .ss)
memcpy(temp, &token[4], 2);
t_time.tm_sec = (int) strtol(temp, NULL, 10);
//////////////////////
// //
// Extract Status //
// //
//////////////////////
// extract status
tokenize(token, sentence, ",", &cursor);
// Don't care move on...
////////////////////////
// //
// Extract Latitude //
// //
////////////////////////
// extract latitude
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
if (toupper(token[0]) == 'S')
{
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
if (toupper(token[0]) == 'W')
{
lon *= -1;
}
}
else
return 1;
////////////////////////////////
// //
// Extract Speed over ground //
// //
////////////////////////////////
// extract speed
tokenize(token, sentence, ",", &cursor);
// don't care move on...
///////////////////////////////////
// //
// Extract angle in degrees //
// //
///////////////////////////////////
// extract angle
tokenize(token, sentence, ",", &cursor);
// don't care move on...
///////////////////////////////////
// //
// Extract date //
// //
///////////////////////////////////
// extract day
tokenize(token, sentence, ",", &cursor); //ddmmyy
// assign day
if (strcmp(token, "") != 0)
{
strncpy(temp, token, 2);
t_time.tm_mday = (int) strtol(temp, NULL, 10);
}
else
return 1;
// assign month
if (strcmp(token, "") != 0)
{
memcpy(temp, &token[2], 2);
t_time.tm_mon = (int) strtol(temp, NULL, 10) - 1; // tm_mon is 0 indexed
}
else
return 1;
// assign year
if (strcmp(token, "") != 0)
{
memcpy(temp, &token[4], 2);
t_time.tm_year = (int) strtol(temp, NULL, 10) + 100;
}
else
return 1;
///////////////////////////////////
// //
// Extract magnetic variation //
// //
///////////////////////////////////
// extract magnetic variation
tokenize(token, sentence, ",", &cursor);
// don't care ending...
if (mktime(&t_time) < dataStore->epochTime)
{
puts("Aborting parse: Stale data");
return 2;
}
else
{
if (mktime(&t_time) > dataStore->epochTime)
dataStore->isDelta = 1;
dataStore->lat = lat;
dataStore->lon = lon;
dataStore->date.tm_hour = t_time.tm_hour;
dataStore->date.tm_min = t_time.tm_min;
dataStore->date.tm_sec = t_time.tm_sec;
dataStore->date.tm_mday = t_time.tm_mday;
dataStore->date.tm_mon = t_time.tm_mon;
dataStore->date.tm_year = t_time.tm_year;
// 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 | TIMEX | DATEX);
}
return 0;
}