pipe.c

00001 #include <sys/types.h>
00002 #include <fcntl.h>
00003 #include <string.h>
00004 #include <stdio.h>
00005 #include <native/pipe.h>
00006 
00007 #define PIPE_MINOR 0
00008 
00009 /* User-space side */
00010 
00011 int pipe_fd;
00012 
00013 int main(int argc, char *argv[])
00014 {
00015         char devname[32], buf[16];
00016 
00017         /* ... */
00018 
00019         sprintf(devname, "/dev/rtp%d", PIPE_MINOR);
00020         pipe_fd = open(devname, O_RDWR);
00021 
00022         if (pipe_fd < 0)
00023                 fail();
00024 
00025         /* Wait for the prompt string "Hello"... */
00026         read(pipe_fd, buf, sizeof(buf));
00027 
00028         /* Then send the reply string "World": */
00029         write(pipe_fd, "World", sizeof("World"));
00030 
00031         /* ... */
00032 }
00033 
00034 void cleanup(void)
00035 {
00036         close(pipe_fd);
00037 }
00038 
00039 /* Kernel-side */
00040 
00041 #define TASK_PRIO  0            /* Highest RT priority */
00042 #define TASK_MODE  T_FPU|T_CPU(0)       /* Uses FPU, bound to CPU #0 */
00043 #define TASK_STKSZ 4096         /* Stack size (in bytes) */
00044 
00045 RT_TASK task_desc;
00046 
00047 RT_PIPE pipe_desc;
00048 
00049 void task_body(void)
00050 {
00051         RT_PIPE_MSG *msgout, *msgin;
00052         int err, len, n;
00053 
00054         for (;;) {
00055                 /* ... */
00056 
00057                 len = sizeof("Hello");
00058                 /* Get a message block of the right size in order to
00059                    initiate the message-oriented dialog with the
00060                    user-space process. Sending a continuous stream of
00061                    bytes is also possible using rt_pipe_stream(), in
00062                    which case no message buffer needs to be
00063                    preallocated. */
00064                 msgout = rt_pipe_alloc(len);
00065 
00066                 if (!msgout)
00067                         fail();
00068 
00069                 /* Send prompt message "Hello" (the output buffer will be freed
00070                    automatically)... */
00071                 strcpy(RT_PIPE_MSGPTR(msgout), "Hello");
00072                 rt_pipe_send(&pipe_desc, msgout, len, P_NORMAL);
00073 
00074                 /* Then wait for the reply string "World": */
00075                 n = rt_pipe_receive(&pipe_desc, &msgin, TM_INFINITE);
00076 
00077                 if (n < 0) {
00078                         printf("receive error> errno=%d\n", n);
00079                         continue;
00080                 }
00081         
00082                 if (n == 0) {
00083                         if (msg == NULL) {
00084                                 printf("pipe closed by peer while reading\n");
00085                                 continue;
00086                         }
00087 
00088                         printf("empty message received\n");
00089                 } else
00090                         printf("received msg> %s, size=%d\n", P_MSGPTR(msg),
00091                                P_MSGSIZE(msg));
00092 
00093                 /* Free the received message buffer. */
00094                 rt_pipe_free(&pipe_desc, msgin);
00095 
00096                 /* ... */
00097         }
00098 }
00099 
00100 init init_module(void)
00101 {
00102         int err;
00103 
00104         err = rt_pipe_create(&pipe_desc, NULL, PIPE_MINOR);
00105 
00106         if (err)
00107                 fail();
00108 
00109         /* ... */
00110 
00111         err = rt_task_create(&task_desc,
00112                              "MyTaskName", TASK_STKSZ, TASK_PRIO, TASK_MODE);
00113         if (!err)
00114                 rt_task_start(&task_desc, &task_body, NULL);
00115 
00116         /* ... */
00117 }
00118 
00119 void cleanup_module(void)
00120 {
00121         rt_pipe_delete(&pipe_desc);
00122         rt_task_delete(&task_desc);
00123 }

Generated on Mon Mar 24 18:02:40 2008 for Xenomai API by  doxygen 1.5.3