The parsing of the Simple_udp_register () function in simple-udp.c, with emphasis on the back function, is analyzed with the example of BROADCAST-UDP.C.
BROADCAST-UDP.C is an example of a broadcast datagram based on SIMPLE-UDP.
int Simple_udp_register (Structsimple_udp_connection *c,
uint16_t Local_port,
uip_ipaddr_t *remote_addr,
uint16_t Remote_port,
Simple_udp_callback receive_callback)
The declaration of the function is declared in the Simple-udp.h header file, registering a UDP connection
Receive_callback A pointer to a function that will be called when the packet is received
Return value: 0, unable to allocate UDP connection; 1, connection successfully allocated
The function registers a UDP connection and appends a callback function to it. The Destroy function is called when a packet arrives. The local UDP port can be set to zero to indicate that a temporary UDP port should be assigned. The remote IP address can be empty, indicating that packets from any IP address should be accepted.
At the same time, in the header file, also declared the Simple_udp_callback back function, it should be noted that this is just a declaration, the real implementation is in the caller BROADCAST-UDP.C This example program.
callback function: typedef void (*simple_udp_callback) (struct simple_udp_connection *c,
constuip_ipaddr_t *source_addr,
Uint16_tsource_port,
constuip_ipaddr_t *dest_addr,
Uint16_tdest_port,
constuint8_t *data, uint16_t datalen);
declares a callback function pointer and converts it to the Simple_udp_callback type . ( declared in Simple-udp.h )
However, the implementation of this callback function is implemented by a function named Recerver in the example program BROADCAST-UDP.C.
This is a typical application example of a callback function.
The Simple_udp_register function assigns a value to the back function by following this line of code:
C->receive_callback = Receive_callback
This sentence looks like an assignment, in fact receive_callback are pointers, so that two pointers to the same memory space, that is, the callback function, in this case, that is, the receiver function in broadcast-udp.c, so that the c-> The operation of the receive_callback is equivalent to the operation of receiver, so that it is understood that receiver is actually assigned by C->receive_callback (this usage is common in Contiki), The assignment process occurs in the process handler function of SIMPLE-UDP.C, as follows:
if (c->receive_callback! = NULL) {
Process_context_begin (c->client_process);
C->receive_callback (c,
& (uip_ip_buf->srcipaddr),
Uip_htons (Uip_ip_buf->srcport),
& (uip_ip_buf->destipaddr),
Uip_htons (Uip_ip_buf->destport),
Databuffer,uip_datalen ());
Process_context_end ();
which
#define UIP_IP_BUF (struct uip_udpip_hdr*) &uip_buf[uip_llh_len])
and
/* The UDP and IP headers. */
struct UIP_UDPIP_HDR {
#if Uip_conf_ipv6
/* IPV6 header. */
uint8_t VTC,
Tcf
uint16_t flow;
uint8_t len[2];
uint8_t Proto, TTL;
uip_ip6addr_t srcipaddr, destipaddr;
#else/* Uip_conf_ipv6 */
/* IP header. */
uint8_t VHL,
Tos
LEN[2],
IPID[2],
IPOFFSET[2],
Ttl
Proto
uint16_t ipchksum;
uip_ipaddr_t srcipaddr, destipaddr;
#endif/* Uip_conf_ipv6 */
/* UDP header. */
uint16_t Srcport,
Destport;
uint16_t Udplen;
uint16_t udpchksum;
};
This will understand that & (uip_ip_buf->srcipaddr) means point to the source address; Uip_ip_buf->srcport points to the port number.
return function parsing in Simple_udp_register