1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-27 13:18:06 +11:00
collapseos/tools/pingpong.c

60 lines
1.6 KiB
C
Raw Normal View History

2020-01-12 14:45:22 +11:00
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "common.h"
/* Sends the contents of `fname` to `device`, expecting every sent byte to be
* echoed back verbatim. Compare every echoed byte with the one sent and bail
* out if a mismatch is detected. When the whole file is sent, push a null char
* to indicate EOF to the receiving end.
*
* It is recommended that you send contents that has gone through `ttysafe`.
2020-07-23 07:26:06 +10:00
*
* If "delayus" is specified, this will be the delay we wait between the moment
* we write the "ping" and the moment where we fetch the "pong".
2020-01-12 14:45:22 +11:00
*/
int main(int argc, char **argv)
{
2020-07-23 07:26:06 +10:00
int delayus = 1000;
if (argc == 4) {
delayus = atoi(argv[3]);
} else if (argc != 3) {
fprintf(stderr, "Usage: ./pingpong device fname [delayus] \n");
2020-01-12 14:45:22 +11:00
return 1;
}
FILE *fp = fopen(argv[2], "r");
if (!fp) {
fprintf(stderr, "Can't open %s.\n", argv[2]);
return 1;
}
2020-07-23 07:26:06 +10:00
int fd = ttyopen(argv[1]);
2020-01-12 14:45:22 +11:00
unsigned char c;
int returncode = 0;
while (fread(&c, 1, 1, fp)) {
putchar('.');
fflush(stdout);
write(fd, &c, 1);
2020-07-23 07:26:06 +10:00
usleep(delayus); // let it breathe
if (!mexpect(fd, c)) {
2020-01-12 14:45:22 +11:00
// mismatch!
unsigned int pos = ftell(fp);
2020-07-23 07:26:06 +10:00
fprintf(stderr, "Mismatch at byte %d!\n", pos);
2020-01-12 14:45:22 +11:00
returncode = 1;
break;
}
}
// To close the receiving loop on the other side, we send a straight null
c = 0;
write(fd, &c, 1);
printf("Done!\n");
fclose(fp);
2020-07-23 07:26:06 +10:00
if (fd > 0) {
close(fd);
}
2020-01-12 14:45:22 +11:00
return returncode;
}