Last active
September 27, 2020 12:02
-
-
Save svagionitis/9491661 to your computer and use it in GitHub Desktop.
Read named pipes and print the decimal and hex value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Build for STB: /opt/CodeSourcery/mips-4.4/bin/mips-linux-gnu-gcc -EL read_fifo.c -o read_fifo */ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <sys/time.h> | |
#include <sys/types.h> | |
#include <sys/select.h> | |
#include <sys/stat.h> | |
#include <sys/ioctl.h> | |
#include <fcntl.h> | |
int main(int argc, char *argv[]) | |
{ | |
int readlen, i; | |
int fifo_fd = 0; | |
int retries = 60; | |
if (argc != 2) | |
{ | |
printf("USAGE: %s [pipe]\n", argv[0]); | |
return -1; | |
} | |
// Open named pipe for read | |
while( (fifo_fd = open(argv[1], O_RDONLY)) == -1 ) | |
{ | |
if (retries--) | |
{ | |
if (fifo_fd) | |
{ | |
printf("ERROR: Pipe %s could not be opened, retry in 1 second\n", argv[1]); | |
fifo_fd = 0; | |
} | |
else | |
{ | |
printf("ERROR: Pipe %s is locked, retry in 1 second\n", argv[1]); | |
} | |
sleep(1); | |
continue; | |
} | |
printf("ERROR: Could not open pipe %s, give up\n", argv[1]); | |
} | |
while(1) | |
{ | |
unsigned int code = 0; | |
struct timeval tv; | |
int res; | |
fd_set fds; | |
FD_ZERO(&fds); | |
FD_SET(fifo_fd, &fds); | |
tv.tv_sec = 0; | |
tv.tv_usec = 150; | |
// Check if pipe is ready for a timeout of 150 usec... | |
res = select(FD_SETSIZE, &fds, NULL, NULL, &tv); | |
if (res == 0) // The timeout expires, so continue... | |
continue; | |
// Read 4 bytes and put them on code... | |
readlen = read(fifo_fd, &code, 4); | |
if (readlen == 0) | |
continue; | |
if (readlen < 0) | |
{ | |
printf("ERROR: readlen < 0\n"); | |
break; | |
} | |
if (readlen > 0) | |
{ | |
time_t tm = 0; | |
tm = time (NULL); | |
printf("%ld - Data read(Read %d bytes): %ld (0x%x)\n", tm, readlen, code, code); | |
} | |
} | |
if (readlen < 0 && errno != EINTR) | |
printf("Fatal Error\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment