-
Notifications
You must be signed in to change notification settings - Fork 0
/
a00010.html
156 lines (138 loc) · 12.2 KB
/
a00010.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Contiki 2.5: example-collect.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.1 -->
<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="dirs.html"><span>Directories</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul>
</div>
</div>
<div class="contents">
<h1>example-collect.c</h1><div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment"> * Copyright (c) 2007, Swedish Institute of Computer Science.</span>
<span class="comment"> * All rights reserved.</span>
<span class="comment"> *</span>
<span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<span class="comment"> * modification, are permitted provided that the following conditions</span>
<span class="comment"> * are met:</span>
<span class="comment"> * 1. Redistributions of source code must retain the above copyright</span>
<span class="comment"> * notice, this list of conditions and the following disclaimer.</span>
<span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
<span class="comment"> * notice, this list of conditions and the following disclaimer in the</span>
<span class="comment"> * documentation and/or other materials provided with the distribution.</span>
<span class="comment"> * 3. Neither the name of the Institute nor the names of its contributors</span>
<span class="comment"> * may be used to endorse or promote products derived from this software</span>
<span class="comment"> * without specific prior written permission.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND</span>
<span class="comment"> * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
<span class="comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
<span class="comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE</span>
<span class="comment"> * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span>
<span class="comment"> * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS</span>
<span class="comment"> * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span>
<span class="comment"> * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span>
<span class="comment"> * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY</span>
<span class="comment"> * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span>
<span class="comment"> * SUCH DAMAGE.</span>
<span class="comment"> *</span>
<span class="comment"> * This file is part of the Contiki operating system.</span>
<span class="comment"> *</span>
<span class="comment"> * $Id: example-collect.c,v 1.16 2011/01/10 15:11:44 adamdunkels Exp $</span>
<span class="comment"> */</span>
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * \file</span>
<span class="comment"> * Example of how the collect primitive works.</span>
<span class="comment"> * \author</span>
<span class="comment"> * Adam Dunkels <adam@sics.se></span>
<span class="comment"> */</span>
<span class="preprocessor">#include "contiki.h"</span>
<span class="preprocessor">#include "lib/random.h"</span>
<span class="preprocessor">#include "<a class="code" href="a00310.html" title="Header file for the Rime stack.">net/rime.h</a>"</span>
<span class="preprocessor">#include "<a class="code" href="a00332.html" title="Header file for hop-by-hop reliable data collection.">net/rime/collect.h</a>"</span>
<span class="preprocessor">#include "dev/leds.h"</span>
<span class="preprocessor">#include "dev/button-sensor.h"</span>
<span class="preprocessor">#include "<a class="code" href="a00296.html" title="Include file for the Contiki low-layer network stack (NETSTACK).">net/netstack.h</a>"</span>
<span class="preprocessor">#include <stdio.h></span>
<span class="keyword">static</span> <span class="keyword">struct </span>collect_conn tc;
<span class="comment">/*---------------------------------------------------------------------------*/</span>
<a name="a0"></a><a class="code" href="a01627.html#ga27e9b6fc13f0438e37a198f69b38b4cf" title="Declare a process.">PROCESS</a>(example_collect_process, <span class="stringliteral">"Test collect process"</span>);
AUTOSTART_PROCESSES(&example_collect_process);
<span class="comment">/*---------------------------------------------------------------------------*/</span>
<span class="keyword">static</span> <span class="keywordtype">void</span>
recv(<span class="keyword">const</span> rimeaddr_t *originator, uint8_t seqno, uint8_t hops)
{
printf(<span class="stringliteral">"Sink got message from %d.%d, seqno %d, hops %d: len %d '%s'\n"</span>,
originator->u8[0], originator->u8[1],
seqno, hops,
<a name="a1"></a><a class="code" href="a01563.html#ga4a1157b882c15f5ea05d64f81d37115d" title="Get the length of the data in the packetbuf.">packetbuf_datalen</a>(),
(<span class="keywordtype">char</span> *)<a name="a2"></a><a class="code" href="a01563.html#ga29a6f3f230b55e6bc2f017879c27113a" title="Get a pointer to the data in the packetbuf.">packetbuf_dataptr</a>());
}
<span class="comment">/*---------------------------------------------------------------------------*/</span>
<span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">struct </span>collect_callbacks callbacks = { recv };
<span class="comment">/*---------------------------------------------------------------------------*/</span>
<a name="a3"></a><a class="code" href="a01627.html#gaa4cff8e4f3abc50c74619a90ae347200" title="Define the body of a process.">PROCESS_THREAD</a>(example_collect_process, ev, data)
{
<span class="keyword">static</span> <span class="keyword">struct </span><a name="_a4"></a><a class="code" href="a00042.html" title="A timer.">etimer</a> periodic;
<span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="a00042.html" title="A timer.">etimer</a> et;
<a name="a5"></a><a class="code" href="a01627.html#ga8efc62947f2ca2c870f52896e0dc1a81" title="Define the beginning of a process.">PROCESS_BEGIN</a>();
collect_open(&tc, 130, COLLECT_ROUTER, &callbacks);
<span class="keywordflow">if</span>(<a name="a6"></a><a class="code" href="a01606.html#gaade5a80096fb3b4296cf89eeaca3087d" title="The Rime address of the node.">rimeaddr_node_addr</a>.u8[0] == 1 &&
<a class="code" href="a01606.html#gaade5a80096fb3b4296cf89eeaca3087d" title="The Rime address of the node.">rimeaddr_node_addr</a>.u8[1] == 0) {
printf(<span class="stringliteral">"I am sink\n"</span>);
collect_set_sink(&tc, 1);
}
<span class="comment">/* Allow some time for the network to settle. */</span>
<a name="a7"></a><a class="code" href="a01623.html#gadb6c83643bbca82b614c50b27c586302" title="Set an event timer.">etimer_set</a>(&et, 120 * <a name="a8"></a><a class="code" href="a01620.html#gae3ced0551b26c9b99cb45a86f34d100a" title="A second, measured in system clock time.">CLOCK_SECOND</a>);
<a name="a9"></a><a class="code" href="a01627.html#ga203e74a64e870015d3c2aa456c1ad5d9" title="Wait for a condition to occur.">PROCESS_WAIT_UNTIL</a>(<a name="a10"></a><a class="code" href="a01623.html#gada74975ee3bc980472df748b373d8636" title="Check if an event timer has expired.">etimer_expired</a>(&et));
<span class="keywordflow">while</span>(1) {
<span class="comment">/* Send a packet every 30 seconds. */</span>
<span class="keywordflow">if</span>(<a class="code" href="a01623.html#gada74975ee3bc980472df748b373d8636" title="Check if an event timer has expired.">etimer_expired</a>(&periodic)) {
<a class="code" href="a01623.html#gadb6c83643bbca82b614c50b27c586302" title="Set an event timer.">etimer_set</a>(&periodic, <a class="code" href="a01620.html#gae3ced0551b26c9b99cb45a86f34d100a" title="A second, measured in system clock time.">CLOCK_SECOND</a> * 30);
<a class="code" href="a01623.html#gadb6c83643bbca82b614c50b27c586302" title="Set an event timer.">etimer_set</a>(&et, random_rand() % (<a class="code" href="a01620.html#gae3ced0551b26c9b99cb45a86f34d100a" title="A second, measured in system clock time.">CLOCK_SECOND</a> * 30));
}
<a name="a11"></a><a class="code" href="a01627.html#ga4f9c1d291e210793e8c4444762f748ba" title="Wait for an event to be posted to the process.">PROCESS_WAIT_EVENT</a>();
<span class="keywordflow">if</span>(<a class="code" href="a01623.html#gada74975ee3bc980472df748b373d8636" title="Check if an event timer has expired.">etimer_expired</a>(&et)) {
<span class="keyword">static</span> rimeaddr_t oldparent;
<span class="keyword">const</span> rimeaddr_t *parent;
printf(<span class="stringliteral">"Sending\n"</span>);
<a name="a12"></a><a class="code" href="a01563.html#gaa540c9abf0db13d27881b616f7794824" title="Clear and reset the packetbuf.">packetbuf_clear</a>();
<a name="a13"></a><a class="code" href="a01563.html#ga236b0d557fe119c39796432484c94b26" title="Set the length of the data in the packetbuf.">packetbuf_set_datalen</a>(sprintf(<a class="code" href="a01563.html#ga29a6f3f230b55e6bc2f017879c27113a" title="Get a pointer to the data in the packetbuf.">packetbuf_dataptr</a>(),
<span class="stringliteral">"%s"</span>, <span class="stringliteral">"Hello"</span>) + 1);
collect_send(&tc, 15);
parent = collect_parent(&tc);
<span class="keywordflow">if</span>(!<a name="a14"></a><a class="code" href="a01606.html#gaed118d3d8fe7e7ac9d6d00988268357e" title="Compare two Rime addresses.">rimeaddr_cmp</a>(parent, &oldparent)) {
<span class="keywordflow">if</span>(!<a class="code" href="a01606.html#gaed118d3d8fe7e7ac9d6d00988268357e" title="Compare two Rime addresses.">rimeaddr_cmp</a>(&oldparent, &<a name="a15"></a><a class="code" href="a01606.html#gaa96ae7849482147440e77751444faa6d" title="The null Rime address.">rimeaddr_null</a>)) {
printf(<span class="stringliteral">"#L %d 0\n"</span>, oldparent.u8[0]);
}
<span class="keywordflow">if</span>(!<a class="code" href="a01606.html#gaed118d3d8fe7e7ac9d6d00988268357e" title="Compare two Rime addresses.">rimeaddr_cmp</a>(parent, &<a class="code" href="a01606.html#gaa96ae7849482147440e77751444faa6d" title="The null Rime address.">rimeaddr_null</a>)) {
printf(<span class="stringliteral">"#L %d 1\n"</span>, parent->u8[0]);
}
<a name="a16"></a><a class="code" href="a01606.html#gacd6a22e93c7a4eb02d901743c958c2b2" title="Copy a Rime address.">rimeaddr_copy</a>(&oldparent, parent);
}
}
}
<a name="a17"></a><a class="code" href="a01627.html#ga9c2681a0070eba8a7c9fdf4dbb6db05e" title="Define the end of a process.">PROCESS_END</a>();
}
<span class="comment">/*---------------------------------------------------------------------------*/</span>
</pre></div> </div>
<hr size="1"/><address style="text-align: right;"><small>Generated on Mon Apr 11 14:23:26 2011 for Contiki 2.5 by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>
</body>
</html>