X-Git-Url: http://git.joshuawise.com/fpgaboy.git/blobdiff_plain/2bcaaabf0bbbb0232fe8ffe0a8943a14159852ab..1eb82e46a20fba718e0dd0e7ced354451189ea09:/binwire.c diff --git a/binwire.c b/binwire.c index 9322ef1..cef0924 100644 --- a/binwire.c +++ b/binwire.c @@ -1,28 +1,120 @@ #include +#include +#include +#include +#include +#include void dowrite(char *s, int len) +{ + int i = 0; + while (i < len) + { + int cs = ((len-i) > 1) ? 1 : (len-i); + + write(1, s+i, cs); + i += cs; + } +} + +int waitchar(int timeout) +{ + struct pollfd pfd; + + pfd.fd = 0; + pfd.events = POLLIN; + return poll(&pfd, 1, timeout) == 1; +} + +int expect(char *s, int len) { int i; - for (i=0; i 0) + expect("A", 1); + expect_no_chars(); + while ((sz = read(rfd, buf+3, 255)) > 0) { + int rv; + char abuf[5]; buf[0] = 0x1B; buf[1] = 'D'; - buf[2] = sz - 1; + buf[2] = sz+1; + abuf[0] = 0x1B; + abuf[1] = 'A'; + abuf[2] = (tc >> 16) & 0xFF; + abuf[3] = (tc >> 8) & 0xFF; + abuf[4] = tc & 0xFF; + tc += sz; + retry: + dowrite(abuf, 5); + rv = expect("A", 1); dowrite(buf, sz + 3); - dowrite(".", 1); - fprintf(stderr, "Data sent\n"); - usleep(100000); + fprintf(stderr, "Data sent (%d)\n", tc); + rv |= expect("D", 1); + expect_no_chars(); + if (rv) + { + printf("Failure to ack... retrying\n"); + dowrite("...", 3); + rv = expect("...", 3); + expect_no_chars(); + goto retry; + } } + exit(0); } \ No newline at end of file