2 * This is an example of how to write a network device driver ("packet
3 * driver") for Contiki. A packet driver is a regular Contiki process
4 * that does two things:
5 * # Checks for incoming packets and delivers those to the TCP/IP stack
6 * # Provides an output function that transmits packets
8 * The output function is registered with the Contiki TCP/IP stack,
9 * whereas incoming packets must be checked inside a Contiki process.
10 * We use the same process for checking for incoming packets and for
11 * registering the output function.
15 * We include the "contiki-net.h" file to get all the network functions.
17 #include "contiki-net.h"
19 /*---------------------------------------------------------------------------*/
21 * We declare the process that we use to register with the TCP/IP stack,
22 * and to check for incoming packets.
24 PROCESS(example_packet_driver_process
, "Example packet driver process");
25 /*---------------------------------------------------------------------------*/
27 * Next, we define the function that transmits packets. This function
28 * is called from the TCP/IP stack when a packet is to be transmitted.
29 * The packet is located in the uip_buf[] buffer, and the length of the
30 * packet is in the uip_len variable.
33 example_packet_driver_output(void)
35 let_the_hardware_send_the_packet(uip_buf
, uip_len
);
38 * A network device driver returns always zero.
42 /*---------------------------------------------------------------------------*/
44 * This is the poll handler function in the process below. This poll
45 * handler function checks for incoming packets and delivers them to
52 * We assume that we have some hardware device that notifies us when
53 * a new packet has arrived. We also assume that we have a function
54 * that pulls out the new packet (here called
55 * check_and_copy_packet()) and puts it in the uip_buf[] buffer. The
56 * function returns the length of the incoming packet, and we store
57 * it in the global uip_len variable. If the packet is longer than
58 * zero bytes, we hand it over to the TCP/IP stack.
60 uip_len
= check_and_copy_packet();
63 * The function tcpip_input() delivers the packet in the uip_buf[]
64 * buffer to the TCP/IP stack.
71 * Now we'll make sure that the poll handler is executed repeatedly.
72 * We do this by calling process_poll() with this process as its
75 * In many cases, the hardware will cause an interrupt to be executed
76 * when a new packet arrives. For such hardware devices, the interrupt
77 * handler calls process_poll() (which is safe to use in an interrupt
80 process_poll(&example_packet_driver_process
);
82 /*---------------------------------------------------------------------------*/
84 * Finally, we define the process that does the work.
86 PROCESS_THREAD(example_packet_driver_process
, ev
, data
)
89 * This process has a poll handler, so we declare it here. Note that
90 * the PROCESS_POLLHANDLER() macro must come before the PROCESS_BEGIN()
93 PROCESS_POLLHANDLER(pollhandler());
96 * This process has an exit handler, so we declare it here. Note that
97 * the PROCESS_EXITHANDLER() macro must come before the PROCESS_BEGIN()
100 PROCESS_EXITHANDLER(exithandler());
103 * The process begins here.
108 * We start with initializing the hardware.
110 initialize_the_hardware();
113 * Register the driver. This will cause any previously registered driver
114 * to be ignored by the TCP/IP stack.
116 tcpip_set_outputfunc(example_packet_driver_output
);
119 * Now we'll make sure that the poll handler is executed initially. We do
120 * this by calling process_poll() with this process as its argument.
122 process_poll(&example_packet_driver_process
);
125 * And we wait for the process to exit.
127 PROCESS_WAIT_EVENT_UNTIL(ev
== PROCESS_EVENT_EXIT
);
130 * Now we shutdown the hardware.
132 shutdown_the_hardware();
135 * Here ends the process.
139 /*---------------------------------------------------------------------------*/