6LoWPAN-api

Version 7 (6lowpan group from 18-748 -, 05/06/2010 12:19 am)

1 1 6lowpan group from 18-748 -
= Nano-RK 6LoWPAN API =
2 1 6lowpan group from 18-748 -
3 5 6lowpan group from 18-748 -
For more information on 6LoWPAN, refer to [wiki:6LoWPAN 6LoWPAN].
4 5 6lowpan group from 18-748 -
5 5 6lowpan group from 18-748 -
For 6LoWPAN installation instructions, please refer to [wiki:6LoWPAN-installation].
6 5 6lowpan group from 18-748 -
7 1 6lowpan group from 18-748 -
 * '''void _6lp_task_config ()'''
8 1 6lowpan group from 18-748 -
    * This function configures 6LoWPAN and starts up the necessary tasks.  This should be called before Nano-RK starts up.
9 1 6lowpan group from 18-748 -
10 1 6lowpan group from 18-748 -
 * '''int8_t _6lp_init(uint8_t chan)'''
11 1 6lowpan group from 18-748 -
    * This function starts 6LoWPAN.  ''chan'' refers to the radio channel that packets will be sent on. Returns 1 upon success, -1 otherwise.
12 2 6lowpan group from 18-748 -
13 2 6lowpan group from 18-748 -
 * '''int8_t udp_socket()'''
14 3 6lowpan group from 18-748 -
    * This function creates a new UDP socket and returns a descriptor for the socket.
15 2 6lowpan group from 18-748 -
16 4 6lowpan group from 18-748 -
 * '''int8_t udp_bind(int8_t sock_num, int16_t port)'''
17 2 6lowpan group from 18-748 -
    * 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.
18 2 6lowpan group from 18-748 -
19 1 6lowpan group from 18-748 -
 * '''int8_t udp_send(int8_t sock_num, struct pkt_buff *pktb, int8_t len, struct ipv6_addr *dest_addr, int16_t dest_port)'''
20 5 6lowpan group from 18-748 -
    * 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.
21 1 6lowpan group from 18-748 -
22 2 6lowpan group from 18-748 -
 * '''int8_t udp_receive(int8_t sock_num, struct pkt_buff *pktb)'''
23 5 6lowpan group from 18-748 -
    * 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.
24 2 6lowpan group from 18-748 -
25 2 6lowpan group from 18-748 -
 * '''int8_t udp_release(int8_t sock_num)'''
26 5 6lowpan group from 18-748 -
    * This function unbinds a socket and releases all resources associated with it.
27 6 6lowpan group from 18-748 -
28 6 6lowpan group from 18-748 -
 * '''_6lowpan_hdr *_6lp_hdr(struct pkt_buff *pktb)'''
29 6 6lowpan group from 18-748 -
    * This function returns a pointer to the start of the 6LoWPAN compressed IPv6 header given a packet buffer
30 6 6lowpan group from 18-748 -
31 6 6lowpan group from 18-748 -
 * '''_6lp_udphdr *get_udphdr(struct pkt_buff *pktb)'''
32 6 6lowpan group from 18-748 -
    * This function returns a pointer to the start of the udp header given a packet buffer
33 6 6lowpan group from 18-748 -
34 6 6lowpan group from 18-748 -
 * '''char *get_udppayload(struct pkt_buff *pktb)'''
35 6 6lowpan group from 18-748 -
    * This function returns a pointer to the start of the udp payload given a packet buffer
36 7 6lowpan group from 18-748 -
----
37 6 6lowpan group from 18-748 -
38 6 6lowpan group from 18-748 -
The application is required to allocate static buffers.  These should typically be globally defined at the top of your application.
39 6 6lowpan group from 18-748 -
40 6 6lowpan group from 18-748 -
{{{
41 6 6lowpan group from 18-748 -
struct pkt_buff tx_pktb;
42 6 6lowpan group from 18-748 -
struct pkt_buff rx_pktb;
43 6 6lowpan group from 18-748 -
}}}
44 6 6lowpan group from 18-748 -
45 6 6lowpan group from 18-748 -
Here is a sample of how to create a simple UDP echo server listening on port 18748.
46 6 6lowpan group from 18-748 -
47 6 6lowpan group from 18-748 -
{{{
48 6 6lowpan group from 18-748 -
49 6 6lowpan group from 18-748 -
void echo_task()
50 6 6lowpan group from 18-748 -
{
51 6 6lowpan group from 18-748 -
    int8_t sock;
52 6 6lowpan group from 18-748 -
    int8_t len;
53 6 6lowpan group from 18-748 -
54 6 6lowpan group from 18-748 -
    _6lp_init(25);
55 6 6lowpan group from 18-748 -
56 6 6lowpan group from 18-748 -
    sock = udp_socket();
57 6 6lowpan group from 18-748 -
58 6 6lowpan group from 18-748 -
    udp_bind(sock, 18748);
59 6 6lowpan group from 18-748 -
60 6 6lowpan group from 18-748 -
    while(1)
61 6 6lowpan group from 18-748 -
    {
62 6 6lowpan group from 18-748 -
        len = udp_receive(sock, &rx_pktb);
63 6 6lowpan group from 18-748 -
64 6 6lowpan group from 18-748 -
        // Copy payload of received packet to transmission packet buffer
65 6 6lowpan group from 18-748 -
        memcpy(get_udppayload(&tx_pktb), get_udppayload(&rx_pktb), len);
66 6 6lowpan group from 18-748 -
67 6 6lowpan group from 18-748 -
        udp_send(sock, &tx_pktb, len, &_6lp_hdr(&rx_pktb)->src_addr, get_udphdr(&rx_pktb)->src_port);
68 6 6lowpan group from 18-748 -
    }
69 6 6lowpan group from 18-748 -
}
70 6 6lowpan group from 18-748 -
71 6 6lowpan group from 18-748 -
72 6 6lowpan group from 18-748 -
}}}