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 
54 #include "dev/ds2401.h"
55 #include "sys/node-id.h"
56 
57 #if WITH_UIP6
58 #include "net/ipv6/uip-ds6.h"
59 #endif /* WITH_UIP6 */
60 
61 #if WITH_UIP
62 #include "net/ip/uip.h"
63 #include "net/ipv4/uip-fw.h"
64 #include "net/uip-fw-drv.h"
65 #include "net/ipv4/uip-over-mesh.h"
66 static struct uip_fw_netif slipif =
67  {UIP_FW_NETIF(192,168,1,2, 255,255,255,255, slip_send)};
68 static struct uip_fw_netif meshif =
69  {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
70 
71 static uint8_t is_gateway;
72 
73 #endif /* WITH_UIP */
74 
75 #define UIP_OVER_MESH_CHANNEL 8
76 
77 /*---------------------------------------------------------------------------*/
78 static void
79 set_rime_addr(void)
80 {
81  linkaddr_t addr;
82  int i;
83 
84  memset(&addr, 0, sizeof(linkaddr_t));
85 #if UIP_CONF_IPV6
86  memcpy(addr.u8, ds2401_id, sizeof(addr.u8));
87 #else
88  if(node_id == 0) {
89  for(i = 0; i < sizeof(linkaddr_t); ++i) {
90  addr.u8[i] = ds2401_id[7 - i];
91  }
92  } else {
93  addr.u8[0] = node_id & 0xff;
94  addr.u8[1] = node_id >> 8;
95  }
96 #endif
98  printf_P(PSTR("Rime started with address "));
99  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
100  printf_P(PSTR("%d."), addr.u8[i]);
101  }
102  printf_P(PSTR("%d\n"), addr.u8[i]);
103 }
104 
105 /*--------------------------------------------------------------------------*/
106 #if WITH_UIP
107 static void
108 set_gateway(void)
109 {
110  if(!is_gateway) {
111  leds_on(LEDS_RED);
112  printf_P(PSTR("%d.%d: making myself the IP network gateway.\n\n"),
114  printf_P(PSTR("IPv4 address of the gateway: %d.%d.%d.%d\n\n"),
115  uip_ipaddr_to_quad(&uip_hostaddr));
116  uip_over_mesh_set_gateway(&linkaddr_node_addr);
117  uip_over_mesh_make_announced_gateway();
118  is_gateway = 1;
119  }
120 }
121 #endif /* WITH_UIP */
122 /*---------------------------------------------------------------------------*/
123 void
124 init_net(void)
125 {
126 
127  set_rime_addr();
128  cc2420_init();
129  {
130  uint8_t longaddr[8];
131  uint16_t shortaddr;
132 
133  shortaddr = (linkaddr_node_addr.u8[0] << 8) +
134  linkaddr_node_addr.u8[1];
135  memset(longaddr, 0, sizeof(longaddr));
136  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
137  printf_P(PSTR("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"),
138  longaddr[0], longaddr[1], longaddr[2], longaddr[3],
139  longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
140 
141  cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
142  }
143 
144 #if WITH_UIP6
145  memcpy(&uip_lladdr.addr, ds2401_id, sizeof(uip_lladdr.addr));
146  /* Setup nullmac-like MAC for 802.15.4 */
147  /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */
148  /* printf(" %s channel %u\n", sicslowmac_driver.name, CC2420_CONF_CHANNEL); */
149 
150  /* Setup X-MAC for 802.15.4 */
151  queuebuf_init();
152  NETSTACK_RDC.init();
153  NETSTACK_MAC.init();
154  NETSTACK_NETWORK.init();
155 
156  printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"),
157  NETSTACK_MAC.name, NETSTACK_RDC.name,
158  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
159  NETSTACK_RDC.channel_check_interval()),
160  CC2420_CONF_CHANNEL);
161 
162  process_start(&tcpip_process, NULL);
163 
164  printf_P(PSTR("Tentative link-local IPv6 address "));
165  {
166  uip_ds6_addr_t *lladdr;
167  int i;
168  lladdr = uip_ds6_get_link_local(-1);
169  for(i = 0; i < 7; ++i) {
170  printf_P(PSTR("%02x%02x:"), lladdr->ipaddr.u8[i * 2],
171  lladdr->ipaddr.u8[i * 2 + 1]);
172  }
173  printf_P(PSTR("%02x%02x\n"), lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
174  }
175 
176  if(!UIP_CONF_IPV6_RPL) {
177  uip_ipaddr_t ipaddr;
178  int i;
179  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
180  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
181  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
182  printf_P(PSTR("Tentative global IPv6 address "));
183  for(i = 0; i < 7; ++i) {
184  printf_P(PSTR("%02x%02x:"),
185  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
186  }
187  printf_P(PSTR("%02x%02x\n"),
188  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
189  }
190 
191 #else /* WITH_UIP6 */
192 
193  NETSTACK_RDC.init();
194  NETSTACK_MAC.init();
195  NETSTACK_NETWORK.init();
196 
197  printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"),
198  NETSTACK_MAC.name, NETSTACK_RDC.name,
199  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1:
200  NETSTACK_RDC.channel_check_interval()),
201  CC2420_CONF_CHANNEL);
202 #endif /* WITH_UIP6 */
203 
204 
205 #if WITH_UIP
206  uip_ipaddr_t hostaddr, netmask;
207 
208  uip_init();
209  uip_fw_init();
210 
211  process_start(&tcpip_process, NULL);
212  process_start(&slip_process, NULL);
213  process_start(&uip_fw_process, NULL);
214 
215  slip_set_input_callback(set_gateway);
216 
217  /* Construct ip address from four bytes. */
218  uip_ipaddr(&hostaddr, 172, 16, linkaddr_node_addr.u8[0],
219  linkaddr_node_addr.u8[1]);
220  /* Construct netmask from four bytes. */
221  uip_ipaddr(&netmask, 255,255,0,0);
222 
223  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
224  /* Set the IP address for this host. */
225  uip_sethostaddr(&hostaddr);
226  /* Set the netmask for this host. */
227  uip_setnetmask(&netmask);
228 
229  uip_over_mesh_set_net(&hostaddr, &netmask);
230 
231  /* Register slip interface with forwarding module. */
232  //uip_fw_register(&slipif);
233  uip_over_mesh_set_gateway_netif(&slipif);
234  /* Set slip interface to be a default forwarding interface . */
235  uip_fw_default(&meshif);
236  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
237  printf_P(PSTR("uIP started with IP address %d.%d.%d.%d\n"),
238  uip_ipaddr_to_quad(&hostaddr));
239 #endif /* WITH_UIP */
240 
241 
242 
243 }
244 /*---------------------------------------------------------------------------*/
#define uip_sethostaddr(addr)
Set the IP address of this host.
Definition: uip.h:195
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
uIP packet forwarding header file.
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
802.15.4 frame creation and parsing functions
#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
Representation of a uIP network interface.
Definition: uip-fw.h:54
#define LEDS_RED
LED1 (Red) -&gt; PC0.
Definition: board.h:89
Unicast address structure.
Definition: uip-ds6.h:172
uip_ipaddr_t ipaddr
The IP address of this interface.
Definition: uip-fw.h:57
Header file for the uIP TCP/IP stack.
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:535
void uip_fw_init(void)
Initialize the uIP packet forwarding module.
Definition: uip-fw.c:185
Network interface and stateless autoconfiguration (RFC 4862)
#define NULL
The null pointer.
Header file for tunnelling uIP over Rime mesh
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
Definition: uip.h:927
void uip_init(void)
uIP initialization function.
Definition: uip6.c:411
void uip_fw_default(struct uip_fw_netif *netif)
Register a default network interface.
Definition: uip-fw.c:515
uip_ipaddr_t netmask
The netmask of the interface.
Definition: uip-fw.h:58
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition: uip-ds6.h:123
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
Definition: uip.h:955
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Definition: uip.h:969
#define uip_setnetmask(addr)
Set the netmask.
Definition: uip.h:239
Include file for the Contiki low-layer network stack (NETSTACK)
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
Header file for the Rime address representation