Contiki-Inga 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 
53 #include "dev/ds2401.h"
54 #include "sys/node-id.h"
55 
56 #if WITH_UIP6
57 #include "net/ipv6/uip-ds6.h"
58 #endif /* WITH_UIP6 */
59 
60 #if WITH_UIP
61 #include "net/ip/uip.h"
62 #include "net/ipv4/uip-fw.h"
63 #include "net/uip-fw-drv.h"
64 #include "net/ipv4/uip-over-mesh.h"
65 static struct uip_fw_netif slipif =
66  {UIP_FW_NETIF(192,168,1,2, 255,255,255,255, slip_send)};
67 static struct uip_fw_netif meshif =
68  {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
69 
70 static uint8_t is_gateway;
71 
72 #endif /* WITH_UIP */
73 
74 #define UIP_OVER_MESH_CHANNEL 8
75 
76 /*---------------------------------------------------------------------------*/
77 static void
78 set_rime_addr(void)
79 {
80  linkaddr_t addr;
81  int i;
82 
83  memset(&addr, 0, sizeof(linkaddr_t));
84 #if UIP_CONF_IPV6
85  if(node_id) {
86  //cooja-avrora-patch to get common addresses between platforms
87  addr.u8[3]=node_id&0xff;
88  addr.u8[4]=(node_id&0xff)>>8;
89  addr.u8[5]=node_id;
90  addr.u8[6]=node_id;
91  addr.u8[7]=node_id;
92  memcpy(ds2401_id, addr.u8, sizeof(addr.u8));
93  }
94  memcpy(addr.u8, ds2401_id, sizeof(addr.u8));
95 
96 #else
97  if(node_id == 0) {
98  for(i = 0; i < sizeof(linkaddr_t); ++i) {
99  addr.u8[i] = ds2401_id[7 - i];
100  }
101  } else {
102  addr.u8[0] = node_id & 0xff;
103  addr.u8[1] = node_id >> 8;
104  }
105 #endif
106  linkaddr_set_node_addr(&addr);
107  printf_P(PSTR("Rime started with address "));
108  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
109  printf_P(PSTR("%d."), addr.u8[i]);
110  }
111  printf_P(PSTR("%d\n"), addr.u8[i]);
112 }
113 
114 /*--------------------------------------------------------------------------*/
115 #if WITH_UIP
116 static void
117 set_gateway(void)
118 {
119  if(!is_gateway) {
120  leds_on(LEDS_RED);
121  printf_P(PSTR("%d.%d: making myself the IP network gateway.\n\n"),
123  printf_P(PSTR("IPv4 address of the gateway: %d.%d.%d.%d\n\n"),
124  uip_ipaddr_to_quad(&uip_hostaddr));
125  uip_over_mesh_set_gateway(&linkaddr_node_addr);
126  uip_over_mesh_make_announced_gateway();
127  is_gateway = 1;
128  }
129 }
130 #endif /* WITH_UIP */
131 /*---------------------------------------------------------------------------*/
132 void
133 init_net(void)
134 {
135 
136  set_rime_addr();
137  cc2420_init();
138  {
139  uint8_t longaddr[8];
140  uint16_t shortaddr;
141 
142  shortaddr = (linkaddr_node_addr.u8[0] << 8) +
143  linkaddr_node_addr.u8[1];
144  memset(longaddr, 0, sizeof(longaddr));
145  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
146  printf_P(PSTR("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"),
147  longaddr[0], longaddr[1], longaddr[2], longaddr[3],
148  longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
149 
150  cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
151  }
152 
153 #if WITH_UIP6
154  memcpy(&uip_lladdr.addr, ds2401_id, sizeof(uip_lladdr.addr));
155  /* Setup nullmac-like MAC for 802.15.4 */
156  /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */
157  /* printf(" %s channel %u\n", sicslowmac_driver.name, CC2420_CONF_CHANNEL); */
158 
159  /* Setup X-MAC for 802.15.4 */
160  queuebuf_init();
161  NETSTACK_RDC.init();
162  NETSTACK_MAC.init();
163  NETSTACK_NETWORK.init();
164 
165  printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"),
166  NETSTACK_MAC.name, NETSTACK_RDC.name,
167  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
168  NETSTACK_RDC.channel_check_interval()),
169  CC2420_CONF_CHANNEL);
170 
171  process_start(&tcpip_process, NULL);
172 
173  printf_P(PSTR("Tentative link-local IPv6 address "));
174  {
175  uip_ds6_addr_t *lladdr;
176  int i;
177  lladdr = uip_ds6_get_link_local(-1);
178  for(i = 0; i < 7; ++i) {
179  printf_P(PSTR("%02x%02x:"), lladdr->ipaddr.u8[i * 2],
180  lladdr->ipaddr.u8[i * 2 + 1]);
181  }
182  printf_P(PSTR("%02x%02x\n"), lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
183  }
184 
185  if(!UIP_CONF_IPV6_RPL) {
186  uip_ipaddr_t ipaddr;
187  int i;
188  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
189  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
190  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
191  printf_P(PSTR("Tentative global IPv6 address "));
192  for(i = 0; i < 7; ++i) {
193  printf_P(PSTR("%02x%02x:"),
194  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
195  }
196  printf_P(PSTR("%02x%02x\n"),
197  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
198  }
199 
200 #else /* WITH_UIP6 */
201 
202  NETSTACK_RDC.init();
203  NETSTACK_MAC.init();
204  NETSTACK_NETWORK.init();
205 
206  printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"),
207  NETSTACK_MAC.name, NETSTACK_RDC.name,
208  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1:
209  NETSTACK_RDC.channel_check_interval()),
210  CC2420_CONF_CHANNEL);
211 #endif /* WITH_UIP6 */
212 
213 
214 #if WITH_UIP
215  uip_ipaddr_t hostaddr, netmask;
216 
217  uip_init();
218  uip_fw_init();
219 
220  process_start(&tcpip_process, NULL);
221  process_start(&slip_process, NULL);
222  process_start(&uip_fw_process, NULL);
223 
224  slip_set_input_callback(set_gateway);
225 
226  /* Construct ip address from four bytes. */
227  uip_ipaddr(&hostaddr, 172, 16, linkaddr_node_addr.u8[0],
228  linkaddr_node_addr.u8[1]);
229  /* Construct netmask from four bytes. */
230  uip_ipaddr(&netmask, 255,255,0,0);
231 
232  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
233  /* Set the IP address for this host. */
234  uip_sethostaddr(&hostaddr);
235  /* Set the netmask for this host. */
236  uip_setnetmask(&netmask);
237 
238  uip_over_mesh_set_net(&hostaddr, &netmask);
239 
240  /* Register slip interface with forwarding module. */
241  //uip_fw_register(&slipif);
242  uip_over_mesh_set_gateway_netif(&slipif);
243  /* Set slip interface to be a default forwarding interface . */
244  uip_fw_default(&meshif);
245  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
246  printf_P(PSTR("uIP started with IP address %d.%d.%d.%d\n"),
247  uip_ipaddr_to_quad(&hostaddr));
248 #endif /* WITH_UIP */
249 
250 
251 
252 }
253 /*---------------------------------------------------------------------------*/