31 #include <sys/select.h>
36 #include <sys/types.h>
41 #ifndef PIPE_THREAD_PRIO
42 #define PIPE_THREAD_PRIO 10
45 static void *
pipe_thread(
void *data __attribute__((unused)));
46 static pthread_mutex_t
pipe_mutex = PTHREAD_MUTEX_INITIALIZER;
52 #if defined(USE_PIPE0_WRITER) || defined(USE_PIPE0_READER)
55 #if defined(USE_PIPE1_WRITER) || defined(USE_PIPE1_READER)
58 #if defined(USE_PIPE2_WRITER) || defined(USE_PIPE2_READER)
63 if (pthread_create(&tid, NULL,
pipe_thread, NULL) != 0) {
64 fprintf(stderr,
"pipe_arch_init: Could not create PIPE reading thread.\n");
68 pthread_setname_np(tid,
"pipe");
80 if( access( read_name, F_OK ) == -1 ) {
81 mkfifo(read_name, 0666);
83 p->fd_read = open(read_name, O_RDWR | O_NONBLOCK);
88 if(write_name != NULL)
90 if( access( write_name, F_OK ) == -1 ) {
91 mkfifo(write_name, 0666);
93 p->fd_write = open(write_name, O_RDWR | O_NONBLOCK);
107 int available =
p->rx_insert_idx -
p->rx_extract_idx;
123 uint8_t ret =
p->rx_buf[
p->rx_extract_idx];
134 if (
p == NULL) {
return; }
135 if (
p->fd_read < 0) {
return; }
141 if (available <= 0) {
145 ssize_t bytes_read = read(
p->fd_read, buf, available);
148 if (bytes_read > 0) {
149 for (i = 0; i < bytes_read; i++) {
150 p->rx_buf[
p->rx_insert_idx] = buf[i];
162 if (
p == NULL) {
return; }
163 if (
p->fd_write < 0) {
return; }
165 if (
p->tx_insert_idx > 0) {
166 ssize_t bytes_sent = write(
p->fd_write,
p->tx_buf,
p->tx_insert_idx);
167 if (bytes_sent !=
p->tx_insert_idx) {
168 if (bytes_sent < 0) {
169 fprintf(stderr,
"pipe_send_message failed\n");
171 fprintf(stderr,
"pipe_send_message: only sent %d bytes instead of %d\n",
172 (
int)bytes_sent,
p->tx_insert_idx);
175 p->tx_insert_idx = 0;
184 if (
p == NULL) {
return; }
185 if (
p->fd_write < 0) {
return; }
187 ssize_t test __attribute__((unused)) = write(
p->fd_write, buffer, size);
203 FD_ZERO(&fds_master);
205 int fd __attribute__((unused));
206 #ifdef USE_PIPE0_READER
207 if (pipe0.fd_read >= 0){
208 FD_SET(pipe0.fd_read, &fds_master);
209 if (pipe0.fd_read > fdmax) {
210 fdmax = pipe0.fd_read;
214 #ifdef USE_PIPE1_READER
215 if(pipe1.fd_read >= 0){
216 FD_SET(pipe1.fd_read, &socks_master);
217 if (pipe1.fd_read > fdmax) {
218 fdmax = pipe1.fd_read;
222 #ifdef USE_PIPE2_READER
223 if(pipe2.fd_read >= 0){
224 FD_SET(pipe2.fd_read, &socks_master);
225 if (pipe2.fd_read > fdmax) {
226 fdmax = pipe2.fd_read;
238 if (select(fdmax + 1, &fds, NULL, NULL, NULL) < 0) {
239 fprintf(stderr,
"pipe_thread: select failed!");
241 #ifdef USE_PIPE0_READER
242 if (FD_ISSET(pipe0.fd_read, &fds)) {
246 #ifdef USE_PIPE1_READER
247 if (FD_ISSET(pipe1.fd_read, &fds)) {
251 #ifdef USE_PIPE2_READER
252 if (FD_ISSET(pipe2.fd_read, &fds)) {