6LoWPAN-api

Version 5 (6lowpan group from 18-748 -, 05/05/2010 11:41 pm) → Version 6/8 (6lowpan group from 18-748 -, 05/06/2010 12:17 am)

= Nano-RK 6LoWPAN API =

For more information on 6LoWPAN, refer to [wiki:6LoWPAN 6LoWPAN].

For 6LoWPAN installation instructions, please refer to [wiki:6LoWPAN-installation].

* '''void _6lp_task_config ()'''
* This function configures 6LoWPAN and starts up the necessary tasks. This should be called before Nano-RK starts up.

* '''int8_t _6lp_init(uint8_t chan)'''
* This function starts 6LoWPAN. ''chan'' refers to the radio channel that packets will be sent on. Returns 1 upon success, -1 otherwise.

* '''int8_t udp_socket()'''
* This function creates a new UDP socket and returns a descriptor for the socket.

* '''int8_t udp_bind(int8_t sock_num, int16_t port)'''
* This function binds a created socket with a given port and the current node's IPv6 address. Currently there is only support for a single IPv6 address per node.

* '''int8_t udp_send(int8_t sock_num, struct pkt_buff *pktb, int8_t len, struct ipv6_addr *dest_addr, int16_t dest_port)'''
* This function transmits a UDP packet over the internet. ''pktb'' refers to the packet structure to be sent. ''len'' is the length of the UDP payload (does NOT include headers). ''dest_addr'' and ''dest_port'' are the destination address and destination port respectively.

* '''int8_t udp_receive(int8_t sock_num, struct pkt_buff *pktb)'''
* This function blocks until a packet is sent to the port assigned to ''sock_num''. Once the function returns the received IPv6 packet would be stored in the ''pktb'' structure.

* '''int8_t udp_release(int8_t sock_num)'''
* This function unbinds a socket and releases all resources associated with it.

* '''_6lowpan_hdr *_6lp_hdr(struct pkt_buff *pktb)'''
* This function returns a pointer to the start of the 6LoWPAN compressed IPv6 header given a packet buffer

* '''_6lp_udphdr *get_udphdr(struct pkt_buff *pktb)'''
* This function returns a pointer to the start of the udp header given a packet buffer

* '''char *get_udppayload(struct pkt_buff *pktb)'''
* This function returns a pointer to the start of the udp payload given a packet buffer
---

The application is required to allocate static buffers. These should typically be globally defined at the top of your application.

{{{
struct pkt_buff tx_pktb;
struct pkt_buff rx_pktb;
}}}

Here is a sample of how to create a simple UDP echo server listening on port 18748.

{{{

void echo_task()
{
int8_t sock;
int8_t len;

_6lp_init(25);

sock = udp_socket();

udp_bind(sock, 18748);

while(1)
{
len = udp_receive(sock, &rx_pktb);

// Copy payload of received packet to transmission packet buffer
memcpy(get_udppayload(&tx_pktb), get_udppayload(&rx_pktb), len);

udp_send(sock, &tx_pktb, len, &_6lp_hdr(&rx_pktb)->src_addr, get_udphdr(&rx_pktb)->src_port);
}
}

}}}