summaryrefslogtreecommitdiff
path: root/contrib/binreplay.c
diff options
context:
space:
mode:
authorChris Kuethe <chris.kuethe@gmail.com>2007-01-15 21:36:41 +0000
committerChris Kuethe <chris.kuethe@gmail.com>2007-01-15 21:36:41 +0000
commita6346f7b5bf6d4ccda029d881d9f62dc54e9e9bb (patch)
tree91eadea788f51f4f8a8b8bb12016cf9b3e6fbfbe /contrib/binreplay.c
parent0d8793d744b4c64529efbb24e65a1eb3e77b5b2a (diff)
downloadgpsd-a6346f7b5bf6d4ccda029d881d9f62dc54e9e9bb.tar.gz
Add a pair of simpleminded utilities for capturing and replaying data on a pty.
This is probably useful for developing new protocols or when gpsfake may be suspect.
Diffstat (limited to 'contrib/binreplay.c')
-rw-r--r--contrib/binreplay.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/contrib/binreplay.c b/contrib/binreplay.c
new file mode 100644
index 00000000..d1838e4e
--- /dev/null
+++ b/contrib/binreplay.c
@@ -0,0 +1,93 @@
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <termios.h>
+#include <unistd.h>
+#include <util.h>
+
+#define WRLEN 256
+void spinner(int);
+
+int main( int argc, char **argv){
+ struct stat sb;
+ struct termios term;
+ char *buf, tn[32];
+ int ifd, ofd, sfd, t, l, speed;
+
+ if (argc != 3 ){
+ fprintf(stderr, "usage: binreplay <speed> <file>\n");
+ return 1;
+ }
+
+ speed = atoi(argv[1]);
+ switch (speed) {
+ case 230400:
+ case 115200:
+ case 57600:
+ case 38400:
+ case 28800:
+ case 14400:
+ case 9600:
+ case 4800:
+ break;
+ default:
+ fprintf(stderr, "invalid speed\n");
+ return 1;
+ }
+
+ if ((ifd = open(argv[2], O_RDONLY, 0444)) == -1)
+ err(1, "open");
+
+ if (fstat(ifd, &sb) == -1)
+ err(1, "fstat");
+
+ if ((buf = mmap(0, sb.st_size, PROT_READ, MAP_FILE, ifd, 0)) == MAP_FAILED)
+ err(1, "mmap");
+
+ cfmakeraw(&term);
+ cfsetospeed(&term, speed);
+ cfsetispeed(&term, speed);
+ if (openpty(&ofd, &sfd, tn, &term, NULL) == -1)
+ err(1, "openpty");
+
+ tcsetattr(ofd, TCSANOW, &term);
+ tcsetattr(sfd, TCSANOW, &term);
+
+ chmod(tn, 0444);
+ printf("configured %s for %dbps\n", tn, speed);
+ t = 1000000 / (speed / 8);
+
+ for(l = 0; l < sb.st_size; l += WRLEN ){
+ write(ofd, buf+l, WRLEN );
+ tcdrain(ofd);
+// tcdrain(sfd);
+ tcflush(ofd, TCIFLUSH);
+ tcflush(sfd, TCIFLUSH);
+ spinner( l );
+ usleep(t);
+ }
+
+ munmap(buf, sb.st_size);
+ close(ifd);
+ close(ofd);
+ fprintf(stderr, "\010\010\010\010\010\010\010\010\010\010\010\010\n");
+ return 0;
+}
+
+void spinner(int n){
+ char *s = "|/-\\";
+
+ if (n % (WRLEN * 4))
+ return;
+
+ n /= (WRLEN * 4);
+
+ fprintf(stderr, "\010\010\010\010\010\010\010\010\010\010\010\010\010");
+ fprintf(stderr, "%c %d", s[n%4], n);
+ fflush(stderr);
+}
+