Contiki 3.x
init-net.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, University of Colombo School of Computing
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$$
32  */
33 
34 /**
35  * \file
36  * Network initialization for the MICAz port.
37  * \author
38  * Kasun Hewage <kasun.ch@gmail.com>
39  */
40 
41 #include <stdio.h>
42 #include <string.h>
43 #include <avr/pgmspace.h>
44 
45 #include "contiki.h"
46 #include "cc2420.h"
47 #include "dev/rs232.h"
48 #include "dev/slip.h"
49 #include "dev/leds.h"
50 #include "net/netstack.h"
51 #include "net/mac/frame802154.h"
52 #include "net/linkaddr.h"
53 #include "net/queuebuf.h"
54 
55 #include "dev/ds2401.h"
56 #include "sys/node-id.h"
57 
58 #if NETSTACK_CONF_WITH_IPV6
59 #include "net/ipv6/uip-ds6.h"
60 #endif /* NETSTACK_CONF_WITH_IPV6 */
61 
62 #if NETSTACK_CONF_WITH_IPV4
63 #include "net/ip/uip.h"
64 #include "net/ipv4/uip-fw.h"
65 #include "net/uip-fw-drv.h"
66 #include "net/ipv4/uip-over-mesh.h"
67 static struct uip_fw_netif slipif =
68  {UIP_FW_NETIF(192,168,1,2, 255,255,255,255, slip_send)};
69 static struct uip_fw_netif meshif =
70  {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
71 
72 static uint8_t is_gateway;
73 
74 #endif /* NETSTACK_CONF_WITH_IPV4 */
75 
76 #define UIP_OVER_MESH_CHANNEL 8
77 
78 /*---------------------------------------------------------------------------*/
79 static void
80 set_rime_addr(void)
81 {
82  linkaddr_t addr;
83  int i;
84 
85  memset(&addr, 0, sizeof(linkaddr_t));
86 #if NETSTACK_CONF_WITH_IPV6
87  memcpy(addr.u8, ds2401_id, sizeof(addr.u8));
88 #else
89  if(node_id == 0) {
90  for(i = 0; i < sizeof(linkaddr_t); ++i) {
91  addr.u8[i] = ds2401_id[7 - i];
92  }
93  } else {
94  addr.u8[0] = node_id & 0xff;
95  addr.u8[1] = node_id >> 8;
96  }
97 #endif
99  printf_P(PSTR("Rime started with address "));
100  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
101  printf_P(PSTR("%d."), addr.u8[i]);
102  }
103  printf_P(PSTR("%d\n"), addr.u8[i]);
104 }
105 
106 /*--------------------------------------------------------------------------*/
107 #if NETSTACK_CONF_WITH_IPV4
108 static void
109 set_gateway(void)
110 {
111  if(!is_gateway) {
112  leds_on(LEDS_RED);
113  printf_P(PSTR("%d.%d: making myself the IP network gateway.\n\n"),
115  printf_P(PSTR("IPv4 address of the gateway: %d.%d.%d.%d\n\n"),
116  uip_ipaddr_to_quad(&uip_hostaddr));
117  uip_over_mesh_set_gateway(&linkaddr_node_addr);
118  uip_over_mesh_make_announced_gateway();
119  is_gateway = 1;
120  }
121 }
122 #endif /* NETSTACK_CONF_WITH_IPV4 */
123 /*---------------------------------------------------------------------------*/
124 void
125 init_net(void)
126 {
127 
128  set_rime_addr();
129  cc2420_init();
130  {
131  uint8_t longaddr[8];
132  uint16_t shortaddr;
133 
134  shortaddr = (linkaddr_node_addr.u8[0] << 8) +
135  linkaddr_node_addr.u8[1];
136  memset(longaddr, 0, sizeof(longaddr));
137  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
138  printf_P(PSTR("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"),
139  longaddr[0], longaddr[1], longaddr[2], longaddr[3],
140  longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
141 
142  cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
143  }
144 
145 #if NETSTACK_CONF_WITH_IPV6
146  memcpy(&uip_lladdr.addr, ds2401_id, sizeof(uip_lladdr.addr));
147  /* Setup nullmac-like MAC for 802.15.4 */
148  /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */
149  /* printf(" %s channel %u\n", sicslowmac_driver.name, CC2420_CONF_CHANNEL); */
150 
151  /* Setup X-MAC for 802.15.4 */
152  queuebuf_init();
153  NETSTACK_RDC.init();
154  NETSTACK_MAC.init();
155  NETSTACK_NETWORK.init();
156 
157  printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"),
158  NETSTACK_MAC.name, NETSTACK_RDC.name,
159  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
160  NETSTACK_RDC.channel_check_interval()),
161  CC2420_CONF_CHANNEL);
162 
163  process_start(&tcpip_process, NULL);
164 
165  printf_P(PSTR("Tentative link-local IPv6 address "));
166  {
167  uip_ds6_addr_t *lladdr;
168  int i;
169  lladdr = uip_ds6_get_link_local(-1);
170  for(i = 0; i < 7; ++i) {
171  printf_P(PSTR("%02x%02x:"), lladdr->ipaddr.u8[i * 2],
172  lladdr->ipaddr.u8[i * 2 + 1]);
173  }
174  printf_P(PSTR("%02x%02x\n"), lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
175  }
176 
177  if(!UIP_CONF_IPV6_RPL) {
178  uip_ipaddr_t ipaddr;
179  int i;
180  uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
181  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
182  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
183  printf_P(PSTR("Tentative global IPv6 address "));
184  for(i = 0; i < 7; ++i) {
185  printf_P(PSTR("%02x%02x:"),
186  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
187  }
188  printf_P(PSTR("%02x%02x\n"),
189  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
190  }
191 
192 #else /* NETSTACK_CONF_WITH_IPV6 */
193 
194  NETSTACK_RDC.init();
195  NETSTACK_MAC.init();
196  NETSTACK_NETWORK.init();
197 
198  printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"),
199  NETSTACK_MAC.name, NETSTACK_RDC.name,
200  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1:
201  NETSTACK_RDC.channel_check_interval()),
202  CC2420_CONF_CHANNEL);
203 #endif /* NETSTACK_CONF_WITH_IPV6 */
204 
205 
206 #if NETSTACK_CONF_WITH_IPV4
207  uip_ipaddr_t hostaddr, netmask;
208 
209  uip_init();
210  uip_fw_init();
211 
212  process_start(&tcpip_process, NULL);
213  process_start(&slip_process, NULL);
214  process_start(&uip_fw_process, NULL);
215 
216  slip_set_input_callback(set_gateway);
217 
218  /* Construct ip address from four bytes. */
219  uip_ipaddr(&hostaddr, 172, 16, linkaddr_node_addr.u8[0],
220  linkaddr_node_addr.u8[1]);
221  /* Construct netmask from four bytes. */
222  uip_ipaddr(&netmask, 255,255,0,0);
223 
224  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
225  /* Set the IP address for this host. */
226  uip_sethostaddr(&hostaddr);
227  /* Set the netmask for this host. */
228  uip_setnetmask(&netmask);
229 
230  uip_over_mesh_set_net(&hostaddr, &netmask);
231 
232  /* Register slip interface with forwarding module. */
233  //uip_fw_register(&slipif);
234  uip_over_mesh_set_gateway_netif(&slipif);
235  /* Set slip interface to be a default forwarding interface . */
236  uip_fw_default(&meshif);
237  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
238  printf_P(PSTR("uIP started with IP address %d.%d.%d.%d\n"),
239  uip_ipaddr_to_quad(&hostaddr));
240 #endif /* NETSTACK_CONF_WITH_IPV4 */
241 
242 
243 
244 }
245 /*---------------------------------------------------------------------------*/
#define uip_sethostaddr(addr)
Set the IP address of this host.
Definition: uip.h:192
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:129
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
Definition: uip.h:956
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
uint8_t slip_send(void)
Send an IP packet from the uIP buffer with SLIP.
Definition: slip.c:193
Header file for IPv6-related data structures.
uip_ipaddr_t ipaddr
The IP address of this interface.
Definition: uip-fw.h:57
Header file for tunnelling uIP over Rime mesh
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
Header file for the Rime address representation
Unicast address structure.
Definition: uip-ds6.h:202
void uip_fw_init(void)
Initialize the uIP packet forwarding module.
Definition: uip-fw.c:185
void slip_set_input_callback(void(*c)(void))
Set a function to be called when there is activity on the SLIP interface; used for detecting if a nod...
Definition: slip.c:143
uip_ipaddr_t netmask
The netmask of the interface.
Definition: uip-fw.h:58
#define uip_setnetmask(addr)
Set the netmask.
Definition: uip.h:236
#define NULL
The null pointer.
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
Definition: uip.h:928
802.15.4 frame creation and parsing functions
uIP packet forwarding header file.
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
#define UIP_FW_NETIF(ip1, ip2, ip3, ip4, nm1, nm2, nm3, nm4, outputfunc)
Instantiating macro for a uIP network interface.
Definition: uip-fw.h:80
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition: uip-ds6.h:153
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Definition: uip.h:970
Header file for the uIP TCP/IP stack.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
uip_ds6_addr_t * uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
Add a unicast address to the interface.
Definition: uip-ds6.c:326
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
void uip_init(void)
uIP initialization function.
Definition: uip.c:363
Header file for the Rime queue buffer management
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
Include file for the Contiki low-layer network stack (NETSTACK)
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Definition: uip-ds6.c:542
void uip_fw_default(struct uip_fw_netif *netif)
Register a default network interface.
Definition: uip-fw.c:515
Representation of a uIP network interface.
Definition: uip-fw.h:54