Contiki 2.6
|
00001 /* 00002 * Copyright (c) 2010, University of Colombo School of Computing 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * This file is part of the Contiki operating system. 00030 * 00031 * @(#)$$ 00032 */ 00033 00034 /** 00035 * \file 00036 * Network initialization for the MICAz port. 00037 * \author 00038 * Kasun Hewage <kasun.ch@gmail.com> 00039 */ 00040 00041 #include <stdio.h> 00042 #include <string.h> 00043 #include <avr/pgmspace.h> 00044 00045 #include "contiki.h" 00046 #include "rf230bb.h" 00047 #include "dev/rs232.h" 00048 #include "dev/slip.h" 00049 #include "dev/leds.h" 00050 #include "net/netstack.h" 00051 #include "net/mac/frame802154.h" 00052 00053 #include "dev/ds2401.h" 00054 #include "node-id.h" 00055 00056 #if WITH_UIP6 00057 #include "net/uip-ds6.h" 00058 #endif /* WITH_UIP6 */ 00059 00060 #if WITH_UIP 00061 #include "net/uip.h" 00062 #include "net/uip-fw.h" 00063 #include "net/uip-fw-drv.h" 00064 #include "net/uip-over-mesh.h" 00065 static struct uip_fw_netif slipif = 00066 {UIP_FW_NETIF(192,168,1,2, 255,255,255,255, slip_send)}; 00067 static struct uip_fw_netif meshif = 00068 {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)}; 00069 00070 static uint8_t is_gateway; 00071 00072 #endif /* WITH_UIP */ 00073 00074 #define UIP_OVER_MESH_CHANNEL 8 00075 00076 /*---------------------------------------------------------------------------*/ 00077 static void 00078 set_rime_addr(void) 00079 { 00080 rimeaddr_t addr; 00081 int i; 00082 00083 memset(&addr, 0, sizeof(rimeaddr_t)); 00084 #if UIP_CONF_IPV6 00085 memcpy(addr.u8, ds2401_id, sizeof(addr.u8)); 00086 #else 00087 if(node_id == 0) { 00088 for(i = 0; i < sizeof(rimeaddr_t); ++i) { 00089 addr.u8[i] = ds2401_id[7 - i]; 00090 } 00091 } else { 00092 addr.u8[0] = node_id & 0xff; 00093 addr.u8[1] = node_id >> 8; 00094 } 00095 #endif 00096 rimeaddr_set_node_addr(&addr); 00097 printf_P(PSTR("Rime started with address ")); 00098 for(i = 0; i < sizeof(addr.u8) - 1; i++) { 00099 printf_P(PSTR("%d."), addr.u8[i]); 00100 } 00101 printf_P(PSTR("%d\n"), addr.u8[i]); 00102 } 00103 00104 /*--------------------------------------------------------------------------*/ 00105 #if WITH_UIP 00106 static void 00107 set_gateway(void) 00108 { 00109 if(!is_gateway) { 00110 leds_on(LEDS_RED); 00111 printf_P(PSTR("%d.%d: making myself the IP network gateway.\n\n"), 00112 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]); 00113 printf_P(PSTR("IPv4 address of the gateway: %d.%d.%d.%d\n\n"), 00114 uip_ipaddr_to_quad(&uip_hostaddr)); 00115 uip_over_mesh_set_gateway(&rimeaddr_node_addr); 00116 uip_over_mesh_make_announced_gateway(); 00117 is_gateway = 1; 00118 } 00119 } 00120 #endif /* WITH_UIP */ 00121 /*---------------------------------------------------------------------------*/ 00122 void 00123 init_net(void) 00124 { 00125 00126 set_rime_addr(); 00127 NETSTACK_RADIO.init(); 00128 { 00129 uint8_t longaddr[8]; 00130 uint16_t shortaddr; 00131 00132 shortaddr = (rimeaddr_node_addr.u8[0] << 8) + 00133 rimeaddr_node_addr.u8[1]; 00134 memset(longaddr, 0, sizeof(longaddr)); 00135 rimeaddr_copy((rimeaddr_t *)&longaddr, &rimeaddr_node_addr); 00136 printf_P(PSTR("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"), 00137 longaddr[0], longaddr[1], longaddr[2], longaddr[3], 00138 longaddr[4], longaddr[5], longaddr[6], longaddr[7]); 00139 00140 rf230_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr); 00141 } 00142 rf230_set_channel(RF_CHANNEL); 00143 00144 00145 #if WITH_UIP6 00146 memcpy(&uip_lladdr.addr, ds2401_id, sizeof(uip_lladdr.addr)); 00147 /* Setup nullmac-like MAC for 802.15.4 */ 00148 /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */ 00149 /* printf(" %s channel %u\n", sicslowmac_driver.name, RF_CHANNEL); */ 00150 00151 /* Setup X-MAC for 802.15.4 */ 00152 queuebuf_init(); 00153 NETSTACK_RDC.init(); 00154 NETSTACK_MAC.init(); 00155 NETSTACK_NETWORK.init(); 00156 00157 printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"), 00158 NETSTACK_MAC.name, NETSTACK_RDC.name, 00159 CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1: 00160 NETSTACK_RDC.channel_check_interval()), 00161 RF_CHANNEL); 00162 00163 process_start(&tcpip_process, NULL); 00164 00165 printf_P(PSTR("Tentative link-local IPv6 address ")); 00166 { 00167 uip_ds6_addr_t *lladdr; 00168 int i; 00169 lladdr = uip_ds6_get_link_local(-1); 00170 for(i = 0; i < 7; ++i) { 00171 printf_P(PSTR("%02x%02x:"), lladdr->ipaddr.u8[i * 2], 00172 lladdr->ipaddr.u8[i * 2 + 1]); 00173 } 00174 printf_P(PSTR("%02x%02x\n"), lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); 00175 } 00176 00177 if(!UIP_CONF_IPV6_RPL) { 00178 uip_ipaddr_t ipaddr; 00179 int i; 00180 uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); 00181 uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); 00182 uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE); 00183 printf_P(PSTR("Tentative global IPv6 address ")); 00184 for(i = 0; i < 7; ++i) { 00185 printf_P(PSTR("%02x%02x:"), 00186 ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]); 00187 } 00188 printf_P(PSTR("%02x%02x\n"), 00189 ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]); 00190 } 00191 00192 #else /* WITH_UIP6 */ 00193 00194 NETSTACK_RDC.init(); 00195 NETSTACK_MAC.init(); 00196 NETSTACK_NETWORK.init(); 00197 00198 printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"), 00199 NETSTACK_MAC.name, NETSTACK_RDC.name, 00200 CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1: 00201 NETSTACK_RDC.channel_check_interval()), 00202 RF_CHANNEL); 00203 #endif /* WITH_UIP6 */ 00204 00205 00206 #if WITH_UIP 00207 uip_ipaddr_t hostaddr, netmask; 00208 00209 uip_init(); 00210 uip_fw_init(); 00211 00212 process_start(&tcpip_process, NULL); 00213 process_start(&slip_process, NULL); 00214 process_start(&uip_fw_process, NULL); 00215 00216 slip_set_input_callback(set_gateway); 00217 00218 /* Construct ip address from four bytes. */ 00219 uip_ipaddr(&hostaddr, 172, 16, rimeaddr_node_addr.u8[0], 00220 rimeaddr_node_addr.u8[1]); 00221 /* Construct netmask from four bytes. */ 00222 uip_ipaddr(&netmask, 255,255,0,0); 00223 00224 uip_ipaddr_copy(&meshif.ipaddr, &hostaddr); 00225 /* Set the IP address for this host. */ 00226 uip_sethostaddr(&hostaddr); 00227 /* Set the netmask for this host. */ 00228 uip_setnetmask(&netmask); 00229 00230 uip_over_mesh_set_net(&hostaddr, &netmask); 00231 00232 /* Register slip interface with forwarding module. */ 00233 //uip_fw_register(&slipif); 00234 uip_over_mesh_set_gateway_netif(&slipif); 00235 /* Set slip interface to be a default forwarding interface . */ 00236 uip_fw_default(&meshif); 00237 uip_over_mesh_init(UIP_OVER_MESH_CHANNEL); 00238 printf_P(PSTR("uIP started with IP address %d.%d.%d.%d\n"), 00239 uip_ipaddr_to_quad(&hostaddr)); 00240 #endif /* WITH_UIP */ 00241 00242 00243 00244 } 00245 /*---------------------------------------------------------------------------*/