Added support for the FTP standalone client to the c64 target.
[contiki-2.x.git] / doc / example-program.c
blobc4f8f6f0fac8625fd22b3c95b6526b28a57e0c5a
1 /*
2 * This file contains an example of how a Contiki program looks.
4 * The program opens a UDP broadcast connection and sends one packet
5 * every second.
6 */
8 #include "contiki.h"
9 #include "contiki-net.h"
12 * All Contiki programs must have a process, and we declare it here.
14 PROCESS(example_program_process, "Example process");
17 * To make the program send a packet once every second, we use an
18 * event timer (etimer).
20 static struct etimer timer;
22 /*---------------------------------------------------------------------------*/
24 * Here we implement the process. The process is run whenever an event
25 * occurs, and the parameters "ev" and "data" will we set to the event
26 * type and any data that may be passed along with the event.
28 PROCESS_THREAD(example_program_process, ev, data)
31 * Declare the UDP connection. Note that this *MUST* be declared
32 * static, or otherwise the contents may be destroyed. The reason
33 * for this is that the process runs as a protothread, and
34 * protothreads do not support stack variables.
36 static struct uip_udp_conn *c;
39 * A process thread starts with PROCESS_BEGIN() and ends with
40 * PROCESS_END().
41 */
42 PROCESS_BEGIN();
45 * We create the UDP connection to port 4321. We don't want to
46 * attach any special data to the connection, so we pass it a NULL
47 * parameter.
49 c = udp_broadcast_new(UIP_HTONS(4321), NULL);
52 * Loop for ever.
54 while(1) {
57 * We set a timer that wakes us up once every second.
59 etimer_set(&timer, CLOCK_SECOND);
60 PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
63 * Now, this is a the tricky bit: in order for us to send a UDP
64 * packet, we must call upon the uIP TCP/IP stack process to call
65 * us. (uIP works under the Hollywood principle: "Don't call us,
66 * we'll call you".) We use the function tcpip_poll_udp() to tell
67 * uIP to call us, and then we wait for the uIP event to come.
69 tcpip_poll_udp(c);
70 PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
73 * We can now send our packet.
75 uip_send("Hello", 5);
78 * We're done now, so we'll just loop again.
83 * The process ends here. Even though our program sits is a while(1)
84 * loop, we must put the PROCESS_END() at the end of the process, or
85 * else the program won't compile.
87 PROCESS_END();
89 /*---------------------------------------------------------------------------*/