doc: regenerate
[xenomai-head.git] / doc / generated / html / api / rtcansend_8c-example.html
blob5741678b62988b51755147b96e8735cb8776a2ca
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3 <title>Xenomai API: rtcansend.c</title>
4 <link href="doxygen.css" rel="stylesheet" type="text/css">
5 <link href="tabs.css" rel="stylesheet" type="text/css">
6 </head><body>
7 <!-- Generated by Doxygen 1.5.6 -->
8 <div class="navigation" id="top">
9 <div class="tabs">
10 <ul>
11 <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
12 <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
13 <li><a href="modules.html"><span>Modules</span></a></li>
14 <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
15 <li><a href="files.html"><span>Files</span></a></li>
16 <li><a href="examples.html"><span>Examples</span></a></li>
17 <li>
18 <form action="search.php" method="get">
19 <table cellspacing="0" cellpadding="0" border="0">
20 <tr>
21 <td><label>&nbsp;<u>S</u>earch&nbsp;for&nbsp;</label></td>
22 <td><input type="text" name="query" value="" size="20" accesskey="s"/></td>
23 </tr>
24 </table>
25 </form>
26 </li>
27 </ul>
28 </div>
29 </div>
30 <div class="contents">
31 <h1>rtcansend.c</h1><div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;stdio.h&gt;</span>
32 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
33 <span class="preprocessor">#include &lt;signal.h&gt;</span>
34 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
35 <span class="preprocessor">#include &lt;time.h&gt;</span>
36 <span class="preprocessor">#include &lt;errno.h&gt;</span>
37 <span class="preprocessor">#include &lt;getopt.h&gt;</span>
38 <span class="preprocessor">#include &lt;sys/mman.h&gt;</span>
40 <span class="preprocessor">#include &lt;<a class="code" href="task_8h.html" title="This file is part of the Xenomai project.">native/task.h</a>&gt;</span>
41 <span class="preprocessor">#include &lt;<a class="code" href="include_2native_2timer_8h.html" title="This file is part of the Xenomai project.">native/timer.h</a>&gt;</span>
42 <span class="preprocessor">#include &lt;<a class="code" href="native_2pipe_8h.html" title="This file is part of the Xenomai project.">native/pipe.h</a>&gt;</span>
44 <span class="preprocessor">#include &lt;<a class="code" href="rtcan_8h.html" title="Real-Time Driver Model for RT-Socket-CAN, CAN device profile header.">rtdm/rtcan.h</a>&gt;</span>
46 <span class="keyword">extern</span> <span class="keywordtype">int</span> optind, opterr, optopt;
48 <span class="keyword">static</span> <span class="keywordtype">void</span> print_usage(<span class="keywordtype">char</span> *prg)
50 fprintf(stderr,
51 <span class="stringliteral">"Usage: %s &lt;can-interface&gt; [Options] &lt;can-msg&gt;\n"</span>
52 <span class="stringliteral">"&lt;can-msg&gt; can consist of up to 8 bytes given as a space separated list\n"</span>
53 <span class="stringliteral">"Options:\n"</span>
54 <span class="stringliteral">" -i, --identifier=ID CAN Identifier (default = 1)\n"</span>
55 <span class="stringliteral">" -r --rtr send remote request\n"</span>
56 <span class="stringliteral">" -e --extended send extended frame\n"</span>
57 <span class="stringliteral">" -l --loop=COUNT send message COUNT times\n"</span>
58 <span class="stringliteral">" -c, --count message count in data[0-3]\n"</span>
59 <span class="stringliteral">" -d, --delay=MS delay in ms (default = 1ms)\n"</span>
60 <span class="stringliteral">" -s, --send use send instead of sendto\n"</span>
61 <span class="stringliteral">" -t, --timeout=MS timeout in ms\n"</span>
62 <span class="stringliteral">" -L, --loopback=0|1 switch local loopback off or on\n"</span>
63 <span class="stringliteral">" -v, --verbose be verbose\n"</span>
64 <span class="stringliteral">" -p, --print=MODULO print every MODULO message\n"</span>
65 <span class="stringliteral">" -h, --help this help\n"</span>,
66 prg);
70 RT_TASK rt_task_desc;
72 <span class="keyword">static</span> <span class="keywordtype">int</span> s=-1, dlc=0, rtr=0, extended=0, verbose=0, loops=1;
73 <span class="keyword">static</span> SRTIME delay=1000000;
74 <span class="keyword">static</span> <span class="keywordtype">int</span> count=0, print=1, use_send=0, loopback=-1;
75 <span class="keyword">static</span> <a name="a0"></a><a class="code" href="group__rtdm.html#gededf91e760e3268235351dcc9d9dcda" title="RTDM type for representing relative intervals.">nanosecs_rel_t</a> timeout = 0;
76 <span class="keyword">static</span> <span class="keyword">struct </span><a name="_a1"></a><a class="code" href="structcan__frame.html" title="Raw CAN frame.">can_frame</a> frame;
77 <span class="keyword">static</span> <span class="keyword">struct </span><a name="_a2"></a><a class="code" href="structsockaddr__can.html" title="Socket address structure for the CAN address family.">sockaddr_can</a> to_addr;
80 <span class="keywordtype">void</span> cleanup(<span class="keywordtype">void</span>)
82 <span class="keywordtype">int</span> ret;
84 <span class="keywordflow">if</span> (verbose)
85 printf(<span class="stringliteral">"Cleaning up...\n"</span>);
87 usleep(100000);
89 <span class="keywordflow">if</span> (s &gt;= 0) {
90 ret = <a name="a3"></a><a class="code" href="group__userapi.html#ga34c1ddf4e2deb727a1ab3cd648ce5d9" title="Close a device or socket.">rt_dev_close</a>(s);
91 s = -1;
92 <span class="keywordflow">if</span> (ret) {
93 fprintf(stderr, <span class="stringliteral">"rt_dev_close: %s\n"</span>, strerror(-ret));
95 <a name="a4"></a><a class="code" href="group__task.html#gb6e0d411830710e8cc82d77b9df19510" title="Delete a real-time task.">rt_task_delete</a>(&amp;rt_task_desc);
99 <span class="keywordtype">void</span> cleanup_and_exit(<span class="keywordtype">int</span> sig)
101 <span class="keywordflow">if</span> (verbose)
102 printf(<span class="stringliteral">"Signal %d received\n"</span>, sig);
103 cleanup();
104 exit(0);
107 <span class="keywordtype">void</span> rt_task(<span class="keywordtype">void</span>)
109 <span class="keywordtype">int</span> i, j, ret;
111 <span class="keywordflow">for</span> (i = 0; i &lt; loops; i++) {
112 <a name="a5"></a><a class="code" href="group__task.html#gd5225e5fb8d583fbdfa5299f322b8366" title="Delay the calling task (relative).">rt_task_sleep</a>(<a name="a6"></a><a class="code" href="group__native__timer.html#gad812d18e51f398fbb0e5392b2acdf2f" title="Convert nanoseconds to internal clock ticks.">rt_timer_ns2ticks</a>(delay));
113 <span class="keywordflow">if</span> (count)
114 memcpy(&amp;frame.data[0], &amp;i, <span class="keyword">sizeof</span>(i));
115 <span class="comment">/* Note: sendto avoids the definiton of a receive filter list */</span>
116 <span class="keywordflow">if</span> (use_send)
117 ret = <a name="a7"></a><a class="code" href="group__userapi.html#g8e6eeb1cc9924ed11f3f457d4153697d" title="Transmit message to socket.">rt_dev_send</a>(s, (<span class="keywordtype">void</span> *)&amp;frame, <span class="keyword">sizeof</span>(<a class="code" href="structcan__frame.html" title="Raw CAN frame.">can_frame_t</a>), 0);
118 <span class="keywordflow">else</span>
119 ret = <a name="a8"></a><a class="code" href="group__userapi.html#g6a3a39ea1a35258935a32e1870955269" title="Transmit message to socket.">rt_dev_sendto</a>(s, (<span class="keywordtype">void</span> *)&amp;frame, <span class="keyword">sizeof</span>(<a class="code" href="structcan__frame.html" title="Raw CAN frame.">can_frame_t</a>), 0,
120 (<span class="keyword">struct</span> sockaddr *)&amp;to_addr, <span class="keyword">sizeof</span>(to_addr));
121 <span class="keywordflow">if</span> (ret &lt; 0) {
122 <span class="keywordflow">switch</span> (ret) {
123 <span class="keywordflow">case</span> -ETIMEDOUT:
124 <span class="keywordflow">if</span> (verbose)
125 printf(<span class="stringliteral">"rt_dev_send(to): timed out"</span>);
126 <span class="keywordflow">break</span>;
127 <span class="keywordflow">case</span> -EBADF:
128 <span class="keywordflow">if</span> (verbose)
129 printf(<span class="stringliteral">"rt_dev_send(to): aborted because socket was closed"</span>);
130 <span class="keywordflow">break</span>;
131 <span class="keywordflow">default</span>:
132 fprintf(stderr, <span class="stringliteral">"rt_dev_send: %s\n"</span>, strerror(-ret));
133 <span class="keywordflow">break</span>;
135 i = loops; <span class="comment">/* abort */</span>
136 <span class="keywordflow">break</span>;
138 <span class="keywordflow">if</span> (verbose &amp;&amp; (i % print) == 0) {
139 <span class="keywordflow">if</span> (frame.can_id &amp; <a name="a9"></a><a class="code" href="group__rtcan.html#g5ae55ab3741140024858bd7b8762ea42" title="Extended frame.">CAN_EFF_FLAG</a>)
140 printf(<span class="stringliteral">"&lt;0x%08x&gt;"</span>, frame.can_id &amp; <a name="a10"></a><a class="code" href="group__rtcan.html#g83b943dca61d0aad5b07ec01c3317572" title="Bit mask for extended CAN IDs.">CAN_EFF_MASK</a>);
141 <span class="keywordflow">else</span>
142 printf(<span class="stringliteral">"&lt;0x%03x&gt;"</span>, frame.can_id &amp; <a name="a11"></a><a class="code" href="group__rtcan.html#gfe4fc3f9cab58aeeb463861289e8ce93" title="Bit mask for standard CAN IDs.">CAN_SFF_MASK</a>);
143 printf(<span class="stringliteral">" [%d]"</span>, frame.can_dlc);
144 <span class="keywordflow">for</span> (j = 0; j &lt; frame.can_dlc; j++) {
145 printf(<span class="stringliteral">" %02x"</span>, frame.data[j]);
147 printf(<span class="stringliteral">"\n"</span>);
152 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
154 <span class="keywordtype">int</span> i, opt, ret;
155 <span class="keyword">struct </span>ifreq ifr;
156 <span class="keywordtype">char</span> name[32];
158 <span class="keyword">struct </span>option long_options[] = {
159 { <span class="stringliteral">"help"</span>, no_argument, 0, <span class="charliteral">'h'</span> },
160 { <span class="stringliteral">"identifier"</span>, required_argument, 0, <span class="charliteral">'i'</span>},
161 { <span class="stringliteral">"rtr"</span>, no_argument, 0, <span class="charliteral">'r'</span>},
162 { <span class="stringliteral">"extended"</span>, no_argument, 0, <span class="charliteral">'e'</span>},
163 { <span class="stringliteral">"verbose"</span>, no_argument, 0, <span class="charliteral">'v'</span>},
164 { <span class="stringliteral">"count"</span>, no_argument, 0, <span class="charliteral">'c'</span>},
165 { <span class="stringliteral">"print"</span>, required_argument, 0, <span class="charliteral">'p'</span>},
166 { <span class="stringliteral">"loop"</span>, required_argument, 0, <span class="charliteral">'l'</span>},
167 { <span class="stringliteral">"delay"</span>, required_argument, 0, <span class="charliteral">'d'</span>},
168 { <span class="stringliteral">"send"</span>, no_argument, 0, <span class="charliteral">'s'</span>},
169 { <span class="stringliteral">"timeout"</span>, required_argument, 0, <span class="charliteral">'t'</span>},
170 { <span class="stringliteral">"loopback"</span>, required_argument, 0, <span class="charliteral">'L'</span>},
171 { 0, 0, 0, 0},
174 mlockall(MCL_CURRENT | MCL_FUTURE);
176 signal(SIGTERM, cleanup_and_exit);
177 signal(SIGINT, cleanup_and_exit);
179 frame.can_id = 1;
181 <span class="keywordflow">while</span> ((opt = getopt_long(argc, argv, <span class="stringliteral">"hvi:l:red:t:cp:sL:"</span>,
182 long_options, NULL)) != -1) {
183 <span class="keywordflow">switch</span> (opt) {
184 <span class="keywordflow">case</span> <span class="charliteral">'h'</span>:
185 print_usage(argv[0]);
186 exit(0);
188 <span class="keywordflow">case</span> <span class="charliteral">'p'</span>:
189 print = strtoul(optarg, NULL, 0);
191 <span class="keywordflow">case</span> <span class="charliteral">'v'</span>:
192 verbose = 1;
193 <span class="keywordflow">break</span>;
195 <span class="keywordflow">case</span> <span class="charliteral">'c'</span>:
196 count = 1;
197 <span class="keywordflow">break</span>;
199 <span class="keywordflow">case</span> <span class="charliteral">'l'</span>:
200 loops = strtoul(optarg, NULL, 0);
201 <span class="keywordflow">break</span>;
203 <span class="keywordflow">case</span> <span class="charliteral">'i'</span>:
204 frame.can_id = strtoul(optarg, NULL, 0);
205 <span class="keywordflow">break</span>;
207 <span class="keywordflow">case</span> <span class="charliteral">'r'</span>:
208 rtr = 1;
209 <span class="keywordflow">break</span>;
211 <span class="keywordflow">case</span> <span class="charliteral">'e'</span>:
212 extended = 1;
213 <span class="keywordflow">break</span>;
215 <span class="keywordflow">case</span> <span class="charliteral">'d'</span>:
216 delay = strtoul(optarg, NULL, 0) * 1000000LL;
217 <span class="keywordflow">break</span>;
219 <span class="keywordflow">case</span> <span class="charliteral">'s'</span>:
220 use_send = 1;
221 <span class="keywordflow">break</span>;
223 <span class="keywordflow">case</span> <span class="charliteral">'t'</span>:
224 timeout = strtoul(optarg, NULL, 0) * 1000000LL;
225 <span class="keywordflow">break</span>;
227 <span class="keywordflow">case</span> <span class="charliteral">'L'</span>:
228 loopback = strtoul(optarg, NULL, 0);
229 <span class="keywordflow">break</span>;
231 <span class="keywordflow">default</span>:
232 fprintf(stderr, <span class="stringliteral">"Unknown option %c\n"</span>, opt);
233 <span class="keywordflow">break</span>;
237 <span class="keywordflow">if</span> (optind == argc) {
238 print_usage(argv[0]);
239 exit(0);
242 <span class="keywordflow">if</span> (argv[optind] == NULL) {
243 fprintf(stderr, <span class="stringliteral">"No Interface supplied\n"</span>);
244 exit(-1);
247 <span class="keywordflow">if</span> (verbose)
248 printf(<span class="stringliteral">"interface %s\n"</span>, argv[optind]);
250 ret = <a name="a12"></a><a class="code" href="group__userapi.html#g95cfb4d81e3faf578015cad5750b22a3" title="Create a socket.">rt_dev_socket</a>(<a name="a13"></a><a class="code" href="group__rtcan.html#geac0c3db7a1e021f17987bcc76893849" title="CAN protocol family.">PF_CAN</a>, SOCK_RAW, <a name="a14"></a><a class="code" href="group__rtcan.html#g57682d9a1e4f4d90943dbaa683582bf5" title="Raw protocol of PF_CAN, applicable to socket type SOCK_RAW.">CAN_RAW</a>);
251 <span class="keywordflow">if</span> (ret &lt; 0) {
252 fprintf(stderr, <span class="stringliteral">"rt_dev_socket: %s\n"</span>, strerror(-ret));
253 <span class="keywordflow">return</span> -1;
255 s = ret;
257 <span class="keywordflow">if</span> (loopback &gt;= 0) {
258 ret = <a name="a15"></a><a class="code" href="group__userapi.html#gada2561361357285840c5b054fe20681" title="Set socket option.">rt_dev_setsockopt</a>(s, <a name="a16"></a><a class="code" href="group__rtcan.html#gd981aa82a29d828882a2fb4c35c1cdd7" title="CAN socket levels.">SOL_CAN_RAW</a>, <a name="a17"></a><a class="code" href="group__rtcan.html#ga7eb7dc3aa940a0aca20d3a564a147d9" title="CAN TX loopback.">CAN_RAW_LOOPBACK</a>,
259 &amp;loopback, <span class="keyword">sizeof</span>(loopback));
260 <span class="keywordflow">if</span> (ret &lt; 0) {
261 fprintf(stderr, <span class="stringliteral">"rt_dev_setsockopt: %s\n"</span>, strerror(-ret));
262 <span class="keywordflow">goto</span> failure;
264 <span class="keywordflow">if</span> (verbose)
265 printf(<span class="stringliteral">"Using loopback=%d\n"</span>, loopback);
268 strncpy(ifr.ifr_name, argv[optind], IFNAMSIZ);
269 <span class="keywordflow">if</span> (verbose)
270 printf(<span class="stringliteral">"s=%d, ifr_name=%s\n"</span>, s, ifr.ifr_name);
272 ret = <a name="a18"></a><a class="code" href="group__userapi.html#g72b69315a44f235b05627a0c257c01f4" title="Issue an IOCTL.">rt_dev_ioctl</a>(s, <a name="a19"></a><a class="code" href="group__rtcan.html#gee74ae50c8ea0a6aee277c6f124a3866" title="Get CAN interface index by name.">SIOCGIFINDEX</a>, &amp;ifr);
273 <span class="keywordflow">if</span> (ret &lt; 0) {
274 fprintf(stderr, <span class="stringliteral">"rt_dev_ioctl: %s\n"</span>, strerror(-ret));
275 <span class="keywordflow">goto</span> failure;
278 memset(&amp;to_addr, 0, <span class="keyword">sizeof</span>(to_addr));
279 to_addr.can_ifindex = ifr.ifr_ifindex;
280 to_addr.can_family = <a name="a20"></a><a class="code" href="group__rtcan.html#g546620c7e758f003b24b7fdae4f97bd4" title="CAN address family.">AF_CAN</a>;
281 <span class="keywordflow">if</span> (use_send) {
282 <span class="comment">/* Suppress definiton of a default receive filter list */</span>
283 ret = <a class="code" href="group__userapi.html#gada2561361357285840c5b054fe20681" title="Set socket option.">rt_dev_setsockopt</a>(s, <a class="code" href="group__rtcan.html#gd981aa82a29d828882a2fb4c35c1cdd7" title="CAN socket levels.">SOL_CAN_RAW</a>, <a name="a21"></a><a class="code" href="group__rtcan.html#g87313c6e632294aa4582899a3bbc89e4" title="CAN filter definition.">CAN_RAW_FILTER</a>, NULL, 0);
284 <span class="keywordflow">if</span> (ret &lt; 0) {
285 fprintf(stderr, <span class="stringliteral">"rt_dev_setsockopt: %s\n"</span>, strerror(-ret));
286 <span class="keywordflow">goto</span> failure;
289 ret = <a name="a22"></a><a class="code" href="group__userapi.html#g5442e4f8737560e3294bd5bab61ad3fd" title="Bind to local address.">rt_dev_bind</a>(s, (<span class="keyword">struct</span> sockaddr *)&amp;to_addr, <span class="keyword">sizeof</span>(to_addr));
290 <span class="keywordflow">if</span> (ret &lt; 0) {
291 fprintf(stderr, <span class="stringliteral">"rt_dev_bind: %s\n"</span>, strerror(-ret));
292 <span class="keywordflow">goto</span> failure;
296 <span class="keywordflow">if</span> (count)
297 frame.can_dlc = <span class="keyword">sizeof</span>(int);
298 <span class="keywordflow">else</span> {
299 <span class="keywordflow">for</span> (i = optind + 1; i &lt; argc; i++) {
300 frame.data[dlc] = strtoul(argv[i], NULL, 0);
301 dlc++;
302 <span class="keywordflow">if</span>( dlc == 8 )
303 <span class="keywordflow">break</span>;
305 frame.can_dlc = dlc;
308 <span class="keywordflow">if</span> (rtr)
309 frame.can_id |= <a name="a23"></a><a class="code" href="group__rtcan.html#gf324d65226057755175038a15d9bbd75" title="Remote transmission frame.">CAN_RTR_FLAG</a>;
311 <span class="keywordflow">if</span> (extended)
312 frame.can_id |= <a class="code" href="group__rtcan.html#g5ae55ab3741140024858bd7b8762ea42" title="Extended frame.">CAN_EFF_FLAG</a>;
314 <span class="keywordflow">if</span> (timeout) {
315 <span class="keywordflow">if</span> (verbose)
316 printf(<span class="stringliteral">"Timeout: %lld ns\n"</span>, (<span class="keywordtype">long</span> <span class="keywordtype">long</span>)timeout);
317 ret = <a class="code" href="group__userapi.html#g72b69315a44f235b05627a0c257c01f4" title="Issue an IOCTL.">rt_dev_ioctl</a>(s, <a name="a24"></a><a class="code" href="group__rtcan.html#gd3758528585b2779e8949df671f1cf6c" title="Specify a transmission timeout for a socket.">RTCAN_RTIOC_SND_TIMEOUT</a>, &amp;timeout);
318 <span class="keywordflow">if</span> (ret) {
319 fprintf(stderr, <span class="stringliteral">"rt_dev_ioctl SND_TIMEOUT: %s\n"</span>, strerror(-ret));
320 <span class="keywordflow">goto</span> failure;
324 snprintf(name, <span class="keyword">sizeof</span>(name), <span class="stringliteral">"rtcansend-%d"</span>, getpid());
325 ret = <a name="a25"></a><a class="code" href="group__task.html#g72e0a9355d06c346a95bf88b857a9749" title="Turns the current Linux task into a native Xenomai task.">rt_task_shadow</a>(&amp;rt_task_desc, name, 1, 0);
326 <span class="keywordflow">if</span> (ret) {
327 fprintf(stderr, <span class="stringliteral">"rt_task_shadow: %s\n"</span>, strerror(-ret));
328 <span class="keywordflow">goto</span> failure;
331 rt_task();
333 cleanup();
334 <span class="keywordflow">return</span> 0;
336 failure:
337 cleanup();
338 <span class="keywordflow">return</span> -1;
340 </pre></div> </div>
341 <hr size="1"><address style="text-align: right;"><small>Generated on Tue Mar 8 13:02:04 2011 for Xenomai API by&nbsp;
342 <a href="http://www.doxygen.org/index.html">
343 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
344 </body>
345 </html>