-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
382 lines (314 loc) · 12.1 KB
/
main.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
/* File: Main.c
* Author: Elliot Robinson
* Date modified: 3 Sept, 2011
* Changes:
* 3 Sept, 2011: Inititial creation
* 5 Sept, 2011: Adds verifySentence and validateChecksum
*/
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "time.h"
#include "main.h"
#include "parse.h"
/* Author: Elliot Robinson
* Contains the main event loop for the application
*/
#define MAX_MESSAGE_LENGTH 82 // 80 char max + "\r\n"
int main(int argc, char **argv)
{
int parseStatus;
char *inFile = 0, *outFile = 0, *outFile2 = 0;
parseCommandLine(argc, argv, &inFile, &outFile, &outFile2);
// Open files
FILE *fin = fopen(inFile, "r"); // Open input file for reading
FILE *fout = fopen(outFile, "w"); // Open output file for writing, truncating current file
FILE *fout2 = fopen(outFile2, "w"); // Open output file for writing, truncating current file
if ((fout == 0) || (fout2 == 0) || (fin == 0))
{
puts("Invalid filename");
return 1;
}
struct NMEAData persistentData = EMPTY_NMEADATA;
persistentData.date.tm_isdst = -1; // Necessary to keep hour records from being mangled by automatic DST.
time_t rawtime = time(NULL);
persistentData.localOffset = gmtime(&rawtime)->tm_hour - localtime(&rawtime)->tm_hour;
struct NMEAMessage *message = 0;
char *lineIn = 0; // Will be automatically alloc'ed by getline
size_t messageLen = 0;
char *outMessage = malloc(1024); // Way more memory than I'll ever need because I'm too lazy to figure out the maximum length of our output strings. No sizeof because the standard says sizeof(char) always == 1.
// Elliot: strlen() might be of use. ^^^^
// Doubtful. We'd have to do it in makeNMEADataString and resize the array on the fly. It's easier to just malloc a shit ton of memory.
// Add a check in here to only output the string when
// allDataSet == 133 (1+2+4+8+16+32+64)
// ^^^^^^^^^^ == Math fail. 0xFF is definitely 127.
// Put header
fputs("Month, Day, Year, UTC Time (HHMMSS), Epoch Time, TAI Time, Decimal Longitude, Decimal Latitude, DMS Longitude, DMS Latitude, Altitude (m), Satellite 1 PRN, Satellite 1 Elevation, Satellite 1 SNR, Satellite 1 Azimuth, Satellite 2 PRN, Satellite 2 Elevation, Satellite 2 SNR, Satellite 2 Azimuth, Satellite 3 PRN, Satellite 3 Elevation, Satellite 3 SNR, Satellite 3 Azimuth, Satellite 4 PRN, Satellite 4 Elevation, Satellite 4 SNR, Satellite 4 Azimuth, Satellite 5 PRN, Satellite 5 Elevation, Satellite 5 SNR, Satellite 5 Azimuth, Satellite 6 PRN, Satellite 6 Elevation, Satellite 6 SNR, Satellite 6 Azimuth, Satellite 7 PRN, Satellite 7 Elevation, Satellite 7 SNR, Satellite 7 Azimuth, Satellite 8 PRN, Satellite 8 Elevation, Satellite 8 SNR, Satellite 8 Azimuth, Satellite 9 PRN, Satellite 9 Elevation, Satellite 9 SNR, Satellite 9 Azimuth, Satellite 10 PRN, Satellite 10 Elevation, Satellite 10 SNR, Satellite 10 Azimuth, Satellite 11 PRN, Satellite 11 Elevation, Satellite 11 SNR, Satellite 11 Azimuth, Satellite 12 PRN, Satellite 12 Elevation, Satellite 12 SNR, Satellite 12 Azimuth\r\n", fout);
fflush(fout);
while (getline(&lineIn, &messageLen, fin) != EOF)
{
fputs(lineIn, fout2);
fflush(fout);
if((verifySentence(lineIn) == 0) && (validateChecksum(lineIn) == 0))
{
// If we're in here, the lines were good
message = messagify(lineIn);
parseStatus = parse(&persistentData, message);
if ((parseStatus == 0) && (persistentData.isDelta == 1) && (persistentData.allDataSet == 0x7F))
{
makeNMEADataString(outMessage, &persistentData);
fputs(outMessage, fout);
fflush(fout);
persistentData.isDelta = 0;
printf("Time (UTC): %02u:%02u.%02u\nCurrent Satellites: %u\nCurrent Lat: %f\nCurrentLon: %f\nCurrentAltitude (m): %f\n\n", persistentData.date.tm_hour + persistentData.localOffset, persistentData.date.tm_min, persistentData.date.tm_sec, persistentData.numSatellites, persistentData.lat/100, persistentData.lon/100, persistentData.altitude);
}
else
{
// printf("Validated sentence not parsed: %s", lineIn);
// printf("%u - %u - %x\n", parseStatus, persistentData.isDelta, persistentData.allDataSet);
persistentData.isDelta = 0;
}
}
else
{
// printf("Invalid sentence: %s", lineIn);
}
free(message); // Have to free this, since it gets malloc'ed in a function
message = 0; // Avoiding re-free errors is a Good Thing™
}
free(outMessage); // Yep, more freeing.
outMessage = 0; // Now I'm just being pedantic.
puts("");
return 0;
}
/* Author: Elliot Robinson
* Parses command line into char arrays inFile and outFile. Prints usage message and exits if argc != 3
*/
void parseCommandLine(int argc, char **argv, char **inFile, char **outFile, char **outFile2)
{
if (argc == 4)
{
*inFile = argv[1];
*outFile = argv[2];
*outFile2 = argv[3];
return;
}
else
{
printf("Usage: %s infile parsed_outfile raw_outfile\n", argv[0]);
exit(1);
}
}
/* Author: Elliot Robinson
* Parses a raw string into a NMEAMessage struct
*/
struct NMEAMessage * messagify(char *message)
{
struct NMEAMessage *str = malloc(sizeof(struct NMEAMessage));
char *intMsg = message + 1; // Skip the initial '$'
int n = 0;
while(message[n] != ',')
++n;
strncpy(str->type, intMsg, n-1); // Everything up to but not including the comma is the type
intMsg += n; // Move the pointer up to ignore the header
n = 0; // Reset n;
while(intMsg[n] != '*')
++n;
strncpy(str->data, intMsg, n); // Everything up to but not including the * is the data
if (str->data[n-1] == ',')
{
str->data[n] = ',';
++n;
}
str->data[n] = '\0';
return str;
}
/* Author: Elliot Robinson
* Takes a NMEAData struct and turns it into an output string for file storage.
*/
int makeNMEADataString(char *toFill, struct NMEAData *data)
{
char t[1024];
int offset = 0;
offset += sprintf(t, "%u,%u,%u,%02u%02u%02u,%u,%u,%.4f,%.4f,%s,%s,%.4f,%u", data->date.tm_mon + 1, data->date.tm_mday, data->date.tm_year + 1900, data->date.tm_hour + data->localOffset, data->date.tm_min, data->date.tm_sec, (unsigned int)data->epochTime, (unsigned int)data->taiTime, data->lat, data->lon, data->dmsLat, data->dmsLon, data->altitude, data->numSatellites);
// We can pull off this next part because the satellites all have PRNs between 01 and 32
if (data->satellites[0].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[0].prn, data->satellites[0].elevation, data->satellites[0].snr, data->satellites[0].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[1].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[1].prn, data->satellites[1].elevation, data->satellites[1].snr, data->satellites[1].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[2].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[2].prn, data->satellites[2].elevation, data->satellites[2].snr, data->satellites[2].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[3].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[3].prn, data->satellites[3].elevation, data->satellites[3].snr, data->satellites[3].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[4].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[4].prn, data->satellites[4].elevation, data->satellites[4].snr, data->satellites[4].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[5].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[5].prn, data->satellites[5].elevation, data->satellites[5].snr, data->satellites[5].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[6].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[6].prn, data->satellites[6].elevation, data->satellites[6].snr, data->satellites[6].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[7].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[7].prn, data->satellites[7].elevation, data->satellites[7].snr, data->satellites[7].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[8].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[8].prn, data->satellites[8].elevation, data->satellites[8].snr, data->satellites[8].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[9].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[9].prn, data->satellites[9].elevation, data->satellites[9].snr, data->satellites[9].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[10].prn != 0)
{
offset += sprintf(t + offset, ",%u,%u,%u,%u", data->satellites[10].prn, data->satellites[10].elevation, data->satellites[10].snr, data->satellites[10].azimuth);
}
else
{
offset += sprintf(t + offset, ",,,,");
}
if (data->satellites[11].prn != 0)
{
sprintf(t + offset, ",%u,%u,%u,%u\r\n", data->satellites[11].prn, data->satellites[11].elevation, data->satellites[11].snr, data->satellites[11].azimuth);
}
else
{
sprintf(t + offset, "\r\n");
}
strcpy(toFill, t);
return 0;
}
/* Author: Bryce Dodds
* Checks to see if the NMEA string provided is a valid NMEA string. If string the string is valid return 0 and 2 on on invalid string
* 9//2011
*/
int validateChecksum(char *sentence)
{
int i=0;
int sum=0, sump=0;
int a = strlen(sentence) - 4;
char sumProvided[3];
strncpy(sumProvided, sentence + a, 2);
sump = strtol(sumProvided, NULL, 16);
for(i=1; i<(a - 1); i++)
{
sum=sum ^ sentence[i];
} //calculate the checksum
if(sump != sum) //check if the checksum is the same with the prodived one.
{
return 1;
}
else
{
return 0;
}
}
/* Author: Bryce Dodds
* Checks to see if the NMEA string provided is a valid NMEA string. If string the string is valid return 0 and 1 on invalid string
* 9/4/2011
*/
int verifySentence(char *sentence)
{
//check to see if string is longer than 82 characters
if(strlen(sentence) > MAX_MESSAGE_LENGTH)
return 1;
//check to see if the length is less than 12
if(strlen(sentence) < 12)
return 1;
//check to see if there is a dollar sign at the beginning of the string
if(sentence[0] != '$')
return 1;
//check to see if there is a G
if(sentence[1] != 'G')
return 1;
//check to see if there is a P
if(sentence[2] != 'P')
return 1;
//check to see if the checksum is a valid checksum
if(sentence[strlen(sentence) - 5] != '*')
return 1;
//checks the frist number in the checksum is correct
if(! (((sentence[strlen(sentence) - 4] <= 57) && (sentence[strlen(sentence) - 4] >= 48)) || ((sentence[strlen(sentence) - 4] <= 70) && (sentence[strlen(sentence) - 4] >= 65))))
return 1;
if(! (((sentence[strlen(sentence) - 3] <= 57) && (sentence[strlen(sentence) - 3] >= 48)) || ((sentence[strlen(sentence) - 3] <= 70) && (sentence[strlen(sentence) - 3] >= 65))))
return 1;
//checks to see if formate at the end of the NMEA sequence is correct
if(sentence[strlen(sentence) - 2] != '\r')
return 1;
if(sentence[strlen(sentence) - 1] != '\n')
return 1;
//checks to see if the NMEA strings are between the proper ASCII values
int dotCount = 0;
int comCount = 0;
int i;
for( i = 1; i < strlen(sentence) - 5; i++)
{
char w = sentence[i];
if (!(((w <= 57) && (w >= 48)) || ((w <= 90) && (w >= 65)) || (w == '.') || (w == ',') || (w == '-')))
return 1;
//if a dot is found increment the dotcounter
if(w == '.')
dotCount++;
//if a , is found reset the dotcount
if(w == ',') {
dotCount = 0;
comCount++;
}
}
if(comCount == 0)
return 1;
return 0;
}