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;
50 pthread_mutex_init(&pipe_mutex, NULL);
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);
106 pthread_mutex_lock(&pipe_mutex);
111 pthread_mutex_unlock(&pipe_mutex);
122 pthread_mutex_lock(&pipe_mutex);
125 pthread_mutex_unlock(&pipe_mutex);
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);
147 pthread_mutex_lock(&pipe_mutex);
148 if (bytes_read > 0) {
149 for (i = 0; i < bytes_read; i++) {
154 pthread_mutex_unlock(&pipe_mutex);
162 if (p == NULL) {
return; }
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",
184 if (p == NULL) {
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)) {
arch independent PIPE API
void pipe_receive(struct pipe_periph *p)
Read bytes from PIPE.
int pipe_char_available(struct pipe_periph *p)
Get number of bytes available in receive buffer.
uint8_t rx_buf[PIPE_RX_BUFFER_SIZE]
Receive buffer.
void pipe_arch_periph_init(struct pipe_periph *p, char *read_name, char *write_name)
Initialize the PIPE peripheral.
Functions to obtain rt priority or set the nice level.
void pipe_send_raw(struct pipe_periph *p, long fd, uint8_t *buffer, uint16_t size)
Send a packet from another buffer.
int fd_read
PIPE file descriptor.
uint8_t tx_buf[PIPE_TX_BUFFER_SIZE]
Transmit buffer.
static pthread_mutex_t pipe_mutex
#define PIPE_RX_BUFFER_SIZE
uint8_t pipe_getch(struct pipe_periph *p)
Get the last character from the receive buffer.
static int get_rt_prio(int prio)
void pipe_send_message(struct pipe_periph *p, long fd)
Send a message.
static void * pipe_thread(void *data)
check for new pipe packets to receive.
void pipe_arch_init(void)