Contiki 2.6

contiki-init-net.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, STMicroelectronics.
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
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * This file is part of the Contiki OS
00031  *
00032  * $Id: contiki-init-net.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $
00033  */
00034 /*---------------------------------------------------------------------------*/
00035 /**
00036 * \file
00037 *                       Functions for net initialization.
00038 * \author
00039 *                       Salvatore Pitrulli <salvopitru@users.sourceforge.net>
00040 */
00041 /*---------------------------------------------------------------------------*/
00042 
00043 #include "contiki-net.h"
00044 
00045 #if UIP_CONF_IPV6
00046 
00047 #define DEBUG 1
00048 #if DEBUG
00049 #include <stdio.h>
00050 #define PRINTF(...) printf(__VA_ARGS__)
00051 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
00052 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",lladdr.u8[0], lladdr.u8[1], lladdr.u8[2], lladdr.u8[3],lladdr.u8[4], lladdr.u8[5], lladdr.u8[6], lladdr.u8[7])
00053 #else
00054 #define PRINTF(...)
00055 #define PRINT6ADDR(addr)
00056 #define PRINTLLADDR(addr)
00057 #endif
00058 
00059 void print_address(uip_ds6_addr_t *lladdr)
00060 {
00061   int i;
00062   
00063   for(i = 0; i < 7; ++i) {
00064     printf("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]);
00065   }
00066   printf("%02x%02x", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
00067 }
00068 
00069 /*---------------------------------------------------------------------------*/
00070 void print_addresses(void)
00071 {
00072   uip_ds6_addr_t *lladdr;
00073 
00074 
00075   printf("link-local IPv6 address: ");
00076   
00077   lladdr = uip_ds6_get_link_local(-1);
00078   if(lladdr != NULL){
00079     print_address(lladdr);  
00080     printf("\r\n");
00081   }
00082   else
00083     printf("None\r\n");
00084   
00085   printf("global IPv6 address: ");
00086   
00087   lladdr = uip_ds6_get_global(-1);
00088   if(lladdr != NULL){
00089     print_address(lladdr);  
00090     printf("\r\n");
00091   }
00092   else
00093     printf("None\r\n");
00094 
00095 }
00096 
00097 #if FIXED_NET_ADDRESS
00098 
00099 #include "net/rpl/rpl.h"
00100 
00101 
00102 void set_net_address(void)
00103 {
00104   uip_ipaddr_t ipaddr;
00105 #if RPL_BORDER_ROUTER
00106   rpl_dag_t *dag;
00107 #endif
00108 
00109   uip_ip6addr(&ipaddr, NET_ADDR_A, NET_ADDR_B, NET_ADDR_C, NET_ADDR_D, 0, 0, 0, 0);
00110   uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
00111   uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);  
00112 
00113  
00114 //#if !UIP_CONF_ROUTER
00115 //  uip_ds6_prefix_add(&ipaddr, 64, 0); // For on-link determination.
00116 //#else
00117 //  uip_ds6_prefix_add(&ipaddr, 64, 0, 0, 600, 600);
00118 //#endif
00119   
00120   print_addresses();
00121   
00122 #if RPL_BORDER_ROUTER
00123   dag = rpl_set_root(RPL_DEFAULT_INSTANCE,&ipaddr);
00124   if(dag != NULL) {
00125     PRINTF("This node is setted as root of a DAG.\r\n");
00126   }
00127   else {
00128     PRINTF("Error while setting this node as root of a DAG.\r\n");
00129   }
00130 #endif
00131   
00132 }
00133 #endif /* FIXED_GLOBAL_ADDRESS */
00134 
00135 
00136 #endif /* UIP_CONF_IPV6 */