-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay183.c
328 lines (295 loc) · 9.68 KB
/
replay183.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
/* replay183: Program to replay data in nmea-0183 format with sentences
* preceded by TAG timestamps in either second or millisecond resolution.
* such logs may be recorded by kplex (add "timestamp=ms" to a file interface
* declaration).
* Copyright Keith Young 2015
* This is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License or (at your option) any later
* version.
* See the file COPYING for full details
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#define MAXBUF 92
#define PROGNAME "replay183"
#define VERSION "1.1"
long long saywhen ( long long, struct itimerval *);
void inittime (long long);
/* no-op handler for SIGALRM */
void alrm(int s)
{}
/* convert string time TAG to millisecond representation */
long long strtot(char* ptr)
{
long long retval;
int len;
errno = 0;
if ((retval = strtoll(ptr,NULL,10))) {
if (errno == ERANGE)
return(0);
if ((len=strlen(ptr)) >= 10 && len < 13)
/* The only way you can tell it's seconds... */
retval *=1000;
else if (len < 10 || len > 15)
retval=0;
/* implicit else it's milliseconds already */
}
return retval;
}
/* extract next TAG timestamped sentence */
/* Returns millisecond timestamp and populates sentence buffer with associated
* sentence. Returns -1 on error or EOF */
long long nextsen(FILE *f, char *sen)
{
char c, *ptr;
int count;
long long sentime = 0;
enum { SEN_NODATA,
SEN_TAGFIRST,
SEN_TAG,
SEN_TAGSEEN,
SEN_PROC,
SEN_TIME} senstate;
/* initialise state machine */
senstate=SEN_NODATA;
/* Loop until we get a timestamped sentence, error or EOF */
while ((c = getc(f)) != EOF) {
switch(c) {
case '$':
case '!':
/* sentence start delimiter */
if (senstate != SEN_TAGSEEN) {
senstate=SEN_NODATA;
continue;
}
senstate=SEN_PROC;
count=1;
ptr=sen;
*ptr++=c;
continue;
case '\\':
/* TAG delimiter */
if (senstate==SEN_TIME) {
*ptr='\0';
if ((sentime=strtot(sen)))
senstate=SEN_TAGSEEN;
else
senstate=SEN_NODATA;
} else if (senstate==SEN_TAG) {
if (sentime)
senstate=SEN_TAGSEEN;
else
senstate=SEN_NODATA;
} else {
senstate=SEN_TAGFIRST;
}
continue;
case '\n':
case '\0':
/* End of line (the NULL shouldn't happen...) */
if (senstate == SEN_PROC) {
/* if termination was \r\n, back up */
if (*(ptr-1) == '\r')
--ptr;
/* we send back sentence without terminator to let
* the user decide on their termination */
*ptr = '\0';
return(sentime);
}
senstate = SEN_NODATA;
continue;
case ',':
/* separator for distinct TAG elements */
if (senstate==SEN_TAG)
senstate=SEN_TAGFIRST;
else if (senstate==SEN_TIME) {
*ptr++='\0';
sentime=strtot(sen);
senstate=SEN_TAG;
} else if (senstate==SEN_PROC)
break;
continue;
case 'c':
/* TAG code for timestamp */
if (senstate == SEN_PROC)
break;
else if (senstate==SEN_TAGFIRST) {
if (sentime != 0) {
senstate = SEN_NODATA;
sentime = 0;
} else if ((c = getc(f)) == ':') {
senstate = SEN_TIME;
ptr=sen;
count=0;
} else
senstate = SEN_NODATA;
}
continue;
case '*':
/* checksum */
if (senstate == SEN_TIME) {
*ptr++='\0';
sentime=strtot(sen);
senstate=SEN_TAG;
continue;
}
default:
if ((senstate==SEN_PROC) || (senstate==SEN_TIME))
break;
continue;
}
if (++count < MAXBUF) {
*ptr++ = c;
continue;
}
senstate=SEN_NODATA;
}
return(-1);
}
int main(int argc, char **argv)
{
FILE *f;
int c,loop=0,err=0;
long long t=0, n=1;
sigset_t sm,om;
char sentence[MAXBUF];
char *terminator="\n";
struct itimerval when;
struct sigaction sa;
struct timeval delay = {1,0};
while ((c = getopt(argc,argv,"Vrcd:n:")) != -1) {
switch(c) {
case 'r':
terminator="\r\n";
break;
case 'c':
loop=1;
break;
case 'd':
if ((delay.tv_usec=strtol(optarg,NULL,10)) == 0 && (errno)) {
fprintf(stderr,"Invalid delay: %s\n",optarg);
err++;
} else {
if ((delay.tv_sec = delay.tv_usec / 1000)) {
delay.tv_usec %= 1000;
}
delay.tv_usec *= 1000;
}
break;
case 'n':
if ((n=strtol(optarg,NULL,10)) == 0 && (errno)) {
fprintf(stderr,"Invalid repetitions: %s\n",optarg);
err++;
}
break;
case 'V':
printf("%s Version %s\n",PROGNAME,VERSION);
return 0;
break;
case '?':
default:
err++;
break;
}
}
if (err || (argc == optind)) {
fprintf(stderr,"Usage: %s [-r] [-c | -n <repetitions> ] [-d <delay> ] filename ...\n %s -V\n",PROGNAME,PROGNAME);
return(1);
}
argc-=optind;
argv+=optind;
/* set line buffering in case we want to pipe this to something */
setlinebuf(stdout);
/* We don't want to automatically re-set the interval timer so make
* it_interval 0 */
when.it_interval.tv_sec=0;
when.it_interval.tv_usec=0;
/* Block SIGARLM */
sigemptyset(&sm);
sigaddset(&sm,SIGALRM);
sigprocmask(SIG_BLOCK,&sm,&om);
/* Set up (no-op) handler for SIGALRM */
sa.sa_handler = alrm;
sigemptyset(&sa.sa_mask);
sa.sa_flags=0;
sigaction(SIGALRM,&sa,NULL);
while (n) {
for (c=0;c<argc;c++) {
if ((delay.tv_sec || delay.tv_usec) && t) {
/* inter-file time set and not first file */
when.it_value.tv_sec = delay.tv_sec;
when.it_value.tv_usec = delay.tv_usec;
setitimer(ITIMER_REAL, &when,NULL);
sigsuspend(&om);
}
if ((f=fopen(argv[c],"r")) == NULL) {
fprintf(stderr,"%s: Could not open %s: %s\n",PROGNAME,argv[c],
strerror(errno));
return(1);
}
/* Get first timestamped sentence */
if ((t = nextsen(f,sentence)) < 0) {
fprintf(stderr,"%s does not contain timestamped data\n",
argv[c]);
fclose(f);
return(1);
}
printf("%s%s",sentence,terminator);
/* initialise the base time to "now". differences between future
* timestamps and the first one will reflect when, relative to now,
* each sentence is sent
*/
inittime(t);
/* Main loop: until the end of file, loop getting timestamped
* sentences. Transmit those at points relative at the same time
* relative to the first one as when they were "recorded"
*/
while ((t = nextsen(f,sentence)) > 0) {
if (saywhen(t,&when) > 0) {
setitimer(ITIMER_REAL, &when,NULL);
/* This changes the signal mask to unblock SIGALRM. When the
* timer fires (even if it already has) the no-op handler
* is called and processing continues
*/
sigsuspend(&om);
}
printf("%s%s",sentence,terminator);
}
fclose(f);
}
if (!loop)
--n;
}
/* Restore original signal mask before exit */
sigprocmask(SIG_SETMASK,&om,NULL);
return(0);
}
long long basetime; /* Time we started the replay (ms since the epoch) */
/* Record time we started replay */
void inittime(long long t)
{
struct timeval tv;
gettimeofday(&tv,NULL);
basetime=(tv.tv_sec*1000 + tv.tv_usec/1000) - t;
}
/* Work out how long until the next sentence needs playing
* Returns the number of ms until the next sentence */
long long saywhen(long long t, struct itimerval *when)
{
long long s,now;
struct timeval tv;
gettimeofday(&tv, NULL);
now=tv.tv_sec*1000+tv.tv_usec/1000;
now-=basetime;
if ((t-=now)<0)
return 0;
when->it_value.tv_sec=t/1000;
when->it_value.tv_usec=(t-(when->it_value.tv_sec)*1000)*1000;
return t;
}