-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps_time.c
365 lines (346 loc) · 7.92 KB
/
gps_time.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
/*
* Copyright (c) 2022, Kalopa Robotics Limited.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of Kalopa Robotics nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY KALOPA ROBOTICS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL KALOPA ROBOTICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ABSTRACT
* Set the system time by reading from a serially-attached GPS device.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#define BUFFER_SIZE 512
#define ST_WAITNL 0
#define ST_WAITDL 1
#define ST_CAPTURE 2
/*
* Translate table to convert a baud rate into a B-number for the kernel.
*/
struct speed {
int value;
int code;
} speeds[] = {
{50, B50},
{75, B75},
{110, B110},
{134, B134},
{150, B150},
{200, B200},
{300, B300},
{600, B600},
{1200, B1200},
{1800, B1800},
{2400, B2400},
{4800, B4800},
{9600, B9600},
{19200, B19200},
{38400, B38400},
{57600, B57600},
{115200, B115200},
{230400, B230400},
{0, 0}
};
int verbose;
char rdata[BUFFER_SIZE];
char input[BUFFER_SIZE];
void process(int);
void gps_line();
int crack(char *, char *[], int);
int getvalue(char *, int);
void usage();
/*
* All life starts here...
*/
int
main(int argc, char *argv[])
{
int i, fd, baud = 9600;
char *cp, *device = "/dev/ttyu0";
struct termios tios;
/*
* Do the command-line arguments.
*/
verbose = 0;
while ((i = getopt(argc, argv, "s:l:v")) != EOF) {
switch (i) {
case 's':
baud = atoi(optarg);
break;
case 'l':
device = optarg;
break;
case 'v':
verbose = 1;
break;
default:
usage();
break;
}
}
if (verbose)
printf("GPS device: %s, speed: %d.\n", device, baud);
if ((fd = open(device, O_RDONLY|O_NOCTTY)) < 0) {
fprintf(stderr, "gps_time: ");
perror(device);
exit(1);
}
/*
* Get and set the tty parameters.
*/
if (verbose)
printf("Setting serial I/O parameters.\n");
if (tcgetattr(fd, &tios) < 0) {
perror("gps_time: tcgetattr");
exit(1);
}
for (i = 0; speeds[i].value > 0; i++)
if (speeds[i].value == baud)
break;
if (speeds[i].value == 0) {
fprintf(stderr, "gps_time: invalid baud rate: %d\n", baud);
exit(1);
}
tios.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
tios.c_oflag = 0;
tios.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
tios.c_cflag &= ~(CSIZE|PARENB);
tios.c_cflag |= CS8;
tios.c_cc[VMIN] = 1;
tios.c_cc[VTIME] = 0;
cfsetispeed(&tios, speeds[i].code);
cfsetospeed(&tios, speeds[i].code);
if (tcsetattr(fd, TCSANOW, &tios) < 0) {
perror("gps_time: tcsetattr");
exit(1);
}
/*
* Convert the fd into a FILE pointer - let someone else
* do the buffering...
*/
if (verbose)
printf("Opening file descriptor as a file.\n");
/*
* Read each character from the serial device, and process it.
*/
while ((i = read(fd, cp = rdata, BUFFER_SIZE)) > 0) {
while (i-- > 0)
process(*cp++);
}
if (verbose)
printf("Program terminated normally.\n");
exit(0);
}
/*
* Process a single character of serial data.
*/
void
process(int ch)
{
static int inpos = 0;
static int state = ST_WAITNL;
if (ch == '\n' || ch == '\r') {
/*
* Saw a CR/NL. Process a line, if we have one.
*/
if (state == ST_CAPTURE) {
input[inpos++] = '\0';
gps_line();
}
state = ST_WAITDL;
return;
}
/*
* If we're waiting for a dollar-sign, then if we get one
* then we're good. Otherwise, any other character will
* force us into waiting for a new CR/LF.
*/
if (state == ST_WAITDL) {
inpos = 0;
if (ch == '$')
state = ST_CAPTURE;
else
state = ST_WAITNL;
return;
}
/*
* Capture the character, if we're in the right state.
*/
if (state != ST_CAPTURE)
return;
if (inpos >= sizeof(input) - 2) {
/*
* Line is too long. Dump it.
*/
state = ST_WAITNL;
return;
}
input[inpos++] = ch;
}
/*
* Handle a single line of GPS data. Really we only care about GPRMC lines.
*/
void
gps_line()
{
int csum = 0;
char *cp, *args[20];
struct timeval tval;
struct tm tm;
time_t now;
if (verbose)
printf("GPS: [%s]\n", input);
if (strncmp(input, "GPRMC", 5) != 0) {
if (verbose)
printf("Waiting for an RMC message - ignoring this one...\n");
return;
}
/*
* Skip to the end of the sentence, just before the checksum.
*/
for (cp = input; *cp; cp++) {
if (*cp == '*')
break;
csum ^= *cp;
}
/*
* No checksum? Dunno what that was - ditch it.
*/
if (*cp != '*') {
if (verbose)
printf("?Badly formed NMEA sentence - ignoring...\n");
return;
}
/*
* Compare the checksum we computed versus the one at the
* end of the sentence.
*/
if (strtol(cp + 1, NULL, 16) != csum) {
if (verbose)
printf("?Invalid checksum - ignoring...\n");
return;
}
*cp = '\0';
if (verbose)
printf("Checksum is good.\n");
/*
* Split the sentence into its arguments (comma-based).
*/
if (crack(input, args, 20) != 13) {
if (verbose)
printf("Incorrect number of RMC paramaters in sentence...\n");
return;
}
if (verbose) {
/*
* Show the encoded time and date fields.
*/
printf("GPS Time: %s\n", args[1]);
printf("GPS Date: %s\n", args[9]);
}
/*
* Fill a TM struct based on the data in the sentence. Note
* that the year is a bit Y2K, but what can ya do.
*/
tm.tm_sec = getvalue(args[1] + 4, 2);
tm.tm_min = getvalue(args[1] + 2, 2);
tm.tm_hour = getvalue(args[1], 2);
tm.tm_mday = getvalue(args[9], 2);
tm.tm_mon = getvalue(args[9] + 2, 2) - 1;
tm.tm_year = getvalue(args[9] + 4, 2) + 100;
tm.tm_isdst = 0;
tm.tm_gmtoff = 0L;
tval.tv_sec = mktime(&tm);
tval.tv_usec = getvalue(args[1] + 7, 3) * 1000;
if (verbose)
printf("Setting time to %s", ctime(&tval.tv_sec));
/*
* Set the system time.
*/
if (settimeofday(&tval, NULL) == 0) {
time(&now);
printf("%s", ctime(&now));
if (verbose)
printf("Time set successfully. Operation complete.\n");
exit(0);
}
perror("gps_time: settimeofday");
}
/*
* Crack a comman-separated string into components.
*/
int
crack(char *strp, char *argv[], int maxargs)
{
int n;
char *cp;
if (strp == NULL || *strp == '\0')
return(0);
for (n = 0; n < maxargs; n++) {
while (isspace(*strp))
strp++;
if (*strp == '\0')
return(n);
argv[n] = strp;
if ((cp = strchr(strp, ',')) != NULL)
*cp++ = '\0';
strp = cp;
if (strp == NULL || *strp == '\0')
break;
}
return(n + 1);
}
/*
* Get a numeric value from a string.
*/
int
getvalue(char *strp, int ndigits)
{
int value = 0;
while (ndigits-- && isdigit(*strp))
value = value * 10 + *strp++ - '0';
return(value);
}
/*
* Usage message & exit.
*/
void
usage()
{
fprintf(stderr, "Usage: gps_time [-s 9600][-l /dev/ttyu0][-v]\n");
exit(2);
}