Contiki 2.5
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 "dev/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 "node-id.h"
55 
56 #if WITH_UIP6
57 #include "net/uip-ds6.h"
58 #endif /* WITH_UIP6 */
59 
60 #if WITH_UIP
61 #include "net/uip.h"
62 #include "net/uip-fw.h"
63 #include "net/uip-fw-drv.h"
64 #include "net/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  rimeaddr_t addr;
81  int i;
82 
83  memset(&addr, 0, sizeof(rimeaddr_t));
84 #if UIP_CONF_IPV6
85  memcpy(addr.u8, ds2401_id, sizeof(addr.u8));
86 #else
87  if(node_id == 0) {
88  for(i = 0; i < sizeof(rimeaddr_t); ++i) {
89  addr.u8[i] = ds2401_id[7 - i];
90  }
91  } else {
92  addr.u8[0] = node_id & 0xff;
93  addr.u8[1] = node_id >> 8;
94  }
95 #endif
97  printf_P(PSTR("Rime started with address "));
98  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
99  printf_P(PSTR("%d."), addr.u8[i]);
100  }
101  printf_P(PSTR("%d\n"), addr.u8[i]);
102 }
103 
104 /*--------------------------------------------------------------------------*/
105 #if WITH_UIP
106 static void
107 set_gateway(void)
108 {
109  if(!is_gateway) {
110  leds_on(LEDS_RED);
111  printf_P(PSTR("%d.%d: making myself the IP network gateway.\n\n"),
113  printf_P(PSTR("IPv4 address of the gateway: %d.%d.%d.%d\n\n"),
114  uip_ipaddr_to_quad(&uip_hostaddr));
115  uip_over_mesh_set_gateway(&rimeaddr_node_addr);
116  uip_over_mesh_make_announced_gateway();
117  is_gateway = 1;
118  }
119 }
120 #endif /* WITH_UIP */
121 /*---------------------------------------------------------------------------*/
122 void
123 init_net(void)
124 {
125 
126  set_rime_addr();
127  cc2420_init();
128  {
129  uint8_t longaddr[8];
130  uint16_t shortaddr;
131 
132  shortaddr = (rimeaddr_node_addr.u8[0] << 8) +
133  rimeaddr_node_addr.u8[1];
134  memset(longaddr, 0, sizeof(longaddr));
135  rimeaddr_copy((rimeaddr_t *)&longaddr, &rimeaddr_node_addr);
136  printf_P(PSTR("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"),
137  longaddr[0], longaddr[1], longaddr[2], longaddr[3],
138  longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
139 
140  cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
141  }
142  cc2420_set_channel(RF_CHANNEL);
143 
144 
145 #if WITH_UIP6
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, RF_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  RF_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) {
179  int i;
180  uip_ip6addr(&ipaddr, 0xaaaa, 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 /* WITH_UIP6 */
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  RF_CHANNEL);
203 #endif /* WITH_UIP6 */
204 
205 
206 #if WITH_UIP
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, rimeaddr_node_addr.u8[0],
220  rimeaddr_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 /* WITH_UIP */
241 
242 
243 
244 }
245 /*---------------------------------------------------------------------------*/