forked from contiki-os/contiki
/
example-program.c
89 lines (77 loc) · 2.45 KB
/
example-program.c
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
/*
* This file contains an example of how a Contiki program looks.
*
* The program opens a UDP broadcast connection and sends one packet
* every second.
*/
#include "contiki.h"
#include "contiki-net.h"
/*
* All Contiki programs must have a process, and we declare it here.
*/
PROCESS(example_program_process, "Example process");
/*
* To make the program send a packet once every second, we use an
* event timer (etimer).
*/
static struct etimer timer;
/*---------------------------------------------------------------------------*/
/*
* Here we implement the process. The process is run whenever an event
* occurs, and the parameters "ev" and "data" will we set to the event
* type and any data that may be passed along with the event.
*/
PROCESS_THREAD(example_program_process, ev, data)
{
/*
* Declare the UDP connection. Note that this *MUST* be declared
* static, or otherwise the contents may be destroyed. The reason
* for this is that the process runs as a protothread, and
* protothreads do not support stack variables.
*/
static struct uip_udp_conn *c;
/*
* A process thread starts with PROCESS_BEGIN() and ends with
* PROCESS_END().
*/
PROCESS_BEGIN();
/*
* We create the UDP connection to port 4321. We don't want to
* attach any special data to the connection, so we pass it a NULL
* parameter.
*/
c = udp_broadcast_new(UIP_HTONS(4321), NULL);
/*
* Loop for ever.
*/
while(1) {
/*
* We set a timer that wakes us up once every second.
*/
etimer_set(&timer, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
/*
* Now, this is a the tricky bit: in order for us to send a UDP
* packet, we must call upon the uIP TCP/IP stack process to call
* us. (uIP works under the Hollywood principle: "Don't call us,
* we'll call you".) We use the function tcpip_poll_udp() to tell
* uIP to call us, and then we wait for the uIP event to come.
*/
tcpip_poll_udp(c);
PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
/*
* We can now send our packet.
*/
uip_send("Hello", 5);
/*
* We're done now, so we'll just loop again.
*/
}
/*
* The process ends here. Even though our program sits is a while(1)
* loop, we must put the PROCESS_END() at the end of the process, or
* else the program won't compile.
*/
PROCESS_END();
}
/*---------------------------------------------------------------------------*/