1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Xenomai API: pipe.c</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.6 -->
<div class="navigation" id="top">
<div class="tabs">
<ul>
<li><a href="main.html"><span>Main Page</span></a></li>
<li><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Data Structures</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
<li>
<form action="search.php" method="get">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td><label> <u>S</u>earch for </label></td>
<td><input type="text" name="query" value="" size="20" accesskey="s"/></td>
</tr>
</table>
</form>
</li>
</ul>
</div>
</div>
<div class="contents">
<h1>pipe.c</h1><div class="fragment"><pre class="fragment"><span class="preprocessor">#include <sys/types.h></span>
<span class="preprocessor">#include <fcntl.h></span>
<span class="preprocessor">#include <string.h></span>
<span class="preprocessor">#include <stdio.h></span>
<span class="preprocessor">#include <<a class="code" href="native_2pipe_8h.html" title="This file is part of the Xenomai project.">native/pipe.h</a>></span>
<span class="preprocessor">#define PIPE_MINOR 0</span>
<span class="preprocessor"></span>
<span class="comment">/* User-space side */</span>
<span class="keywordtype">int</span> pipe_fd;
<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
{
<span class="keywordtype">char</span> devname[32], buf[16];
<span class="comment">/* ... */</span>
sprintf(devname, <span class="stringliteral">"/dev/rtp%d"</span>, PIPE_MINOR);
pipe_fd = open(devname, O_RDWR);
<span class="keywordflow">if</span> (pipe_fd < 0)
fail();
<span class="comment">/* Wait for the prompt string "Hello"... */</span>
read(pipe_fd, buf, <span class="keyword">sizeof</span>(buf));
<span class="comment">/* Then send the reply string "World": */</span>
write(pipe_fd, <span class="stringliteral">"World"</span>, <span class="keyword">sizeof</span>(<span class="stringliteral">"World"</span>));
<span class="comment">/* ... */</span>
}
<span class="keywordtype">void</span> cleanup(<span class="keywordtype">void</span>)
{
<a name="a0"></a><a class="code" href="group__posix__shm.html#gafbcde67669a1b96577e735ddebd8634" title="Close a file descriptor.">close</a>(pipe_fd);
}
<span class="comment">/* Kernel-side */</span>
<span class="preprocessor">#define TASK_PRIO 0 </span><span class="comment">/* Highest RT priority */</span>
<span class="preprocessor">#define TASK_MODE T_FPU|T_CPU(0) </span><span class="comment">/* Uses FPU, bound to CPU #0 */</span>
<span class="preprocessor">#define TASK_STKSZ 4096 </span><span class="comment">/* Stack size (in bytes) */</span>
RT_TASK task_desc;
RT_PIPE pipe_desc;
<span class="keywordtype">void</span> task_body(<span class="keywordtype">void</span>)
{
RT_PIPE_MSG *msgout, *msgin;
<span class="keywordtype">int</span> err, len, n;
<span class="keywordflow">for</span> (;;) {
<span class="comment">/* ... */</span>
len = <span class="keyword">sizeof</span>(<span class="stringliteral">"Hello"</span>);
<span class="comment">/* Get a message block of the right size in order to</span>
<span class="comment"> initiate the message-oriented dialog with the</span>
<span class="comment"> user-space process. Sending a continuous stream of</span>
<span class="comment"> bytes is also possible using rt_pipe_stream(), in</span>
<span class="comment"> which case no message buffer needs to be</span>
<span class="comment"> preallocated. */</span>
msgout = <a name="a1"></a><a class="code" href="group__pipe.html#g40f294bf6254d2e1b66b8b6b400fc2e5" title="Allocate a message pipe buffer.">rt_pipe_alloc</a>(len);
<span class="keywordflow">if</span> (!msgout)
fail();
<span class="comment">/* Send prompt message "Hello" (the output buffer will be freed</span>
<span class="comment"> automatically)... */</span>
strcpy(RT_PIPE_MSGPTR(msgout), <span class="stringliteral">"Hello"</span>);
<a name="a2"></a><a class="code" href="group__pipe.html#gf40b20cad1dcd7bedcda72beaa16c057" title="Send a message through a pipe.">rt_pipe_send</a>(&pipe_desc, msgout, len, P_NORMAL);
<span class="comment">/* Then wait for the reply string "World": */</span>
n = <a name="a3"></a><a class="code" href="group__pipe.html#g731e5ef18007dcd58a9346bea66abbc6" title="Receive a message from a pipe.">rt_pipe_receive</a>(&pipe_desc, &msgin, TM_INFINITE);
<span class="keywordflow">if</span> (n < 0) {
printf(<span class="stringliteral">"receive error> errno=%d\n"</span>, n);
<span class="keywordflow">continue</span>;
}
<span class="keywordflow">if</span> (n == 0) {
<span class="keywordflow">if</span> (msg == NULL) {
printf(<span class="stringliteral">"pipe closed by peer while reading\n"</span>);
<span class="keywordflow">continue</span>;
}
printf(<span class="stringliteral">"empty message received\n"</span>);
} <span class="keywordflow">else</span>
printf(<span class="stringliteral">"received msg> %s, size=%d\n"</span>, P_MSGPTR(msg),
P_MSGSIZE(msg));
<span class="comment">/* Free the received message buffer. */</span>
<a name="a4"></a><a class="code" href="group__pipe.html#g8face1c57df99cf43b029b3e3b6a05c6" title="Free a message pipe buffer.">rt_pipe_free</a>(&pipe_desc, msgin);
<span class="comment">/* ... */</span>
}
}
init init_module(<span class="keywordtype">void</span>)
{
<span class="keywordtype">int</span> err;
err = <a name="a5"></a><a class="code" href="group__pipe.html#g29521cc898afa0069963964955167aa5" title="Create a message pipe.">rt_pipe_create</a>(&pipe_desc, NULL, PIPE_MINOR);
<span class="keywordflow">if</span> (err)
fail();
<span class="comment">/* ... */</span>
err = <a name="a6"></a><a class="code" href="group__task.html#g03387550693c21d0223f739570ccd992" title="Create a new real-time task.">rt_task_create</a>(&task_desc,
<span class="stringliteral">"MyTaskName"</span>, TASK_STKSZ, TASK_PRIO, TASK_MODE);
<span class="keywordflow">if</span> (!err)
<a name="a7"></a><a class="code" href="group__task.html#gc9638918b8310a430088f5c9a04d2bb7" title="Start a real-time task.">rt_task_start</a>(&task_desc, &task_body, NULL);
<span class="comment">/* ... */</span>
}
<span class="keywordtype">void</span> cleanup_module(<span class="keywordtype">void</span>)
{
<a name="a8"></a><a class="code" href="group__pipe.html#g1d84976a1b9b668366261ca9b836b677" title="Delete a message pipe.">rt_pipe_delete</a>(&pipe_desc);
<a name="a9"></a><a class="code" href="group__task.html#gb6e0d411830710e8cc82d77b9df19510" title="Delete a real-time task.">rt_task_delete</a>(&task_desc);
}
</pre></div> </div>
<hr size="1"><address style="text-align: right;"><small>Generated on Mon Aug 2 12:48:36 2010 for Xenomai API by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
|