6LoWPAN-api

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

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