Contiki 2.5
contiki-init-net.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, STMicroelectronics.
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
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki OS
31  *
32  * $Id: contiki-init-net.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $
33  */
34 /*---------------------------------------------------------------------------*/
35 /**
36 * \file
37 * Functions for net initialization.
38 * \author
39 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
40 */
41 /*---------------------------------------------------------------------------*/
42 
43 #include "contiki-net.h"
44 
45 #if UIP_CONF_IPV6
46 
47 #define DEBUG 1
48 #if DEBUG
49 #include <stdio.h>
50 #define PRINTF(...) printf(__VA_ARGS__)
51 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
52 #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])
53 #else
54 #define PRINTF(...)
55 #define PRINT6ADDR(addr)
56 #define PRINTLLADDR(addr)
57 #endif
58 
59 void print_address(uip_ds6_addr_t *lladdr)
60 {
61  int i;
62 
63  for(i = 0; i < 7; ++i) {
64  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]);
65  }
66  printf("%02x%02x", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
67 }
68 
69 /*---------------------------------------------------------------------------*/
70 void print_addresses(void)
71 {
72  uip_ds6_addr_t *lladdr;
73 
74 
75  printf("link-local IPv6 address: ");
76 
77  lladdr = uip_ds6_get_link_local(-1);
78  if(lladdr != NULL){
79  print_address(lladdr);
80  printf("\r\n");
81  }
82  else
83  printf("None\r\n");
84 
85  printf("global IPv6 address: ");
86 
87  lladdr = uip_ds6_get_global(-1);
88  if(lladdr != NULL){
89  print_address(lladdr);
90  printf("\r\n");
91  }
92  else
93  printf("None\r\n");
94 
95 }
96 
97 #if FIXED_NET_ADDRESS
98 
99 #include "net/rpl/rpl.h"
100 
101 
102 void set_net_address(void)
103 {
104  uip_ipaddr_t ipaddr;
105 #if RPL_BORDER_ROUTER
106  rpl_dag_t *dag;
107 #endif
108 
109  uip_ip6addr(&ipaddr, NET_ADDR_A, NET_ADDR_B, NET_ADDR_C, NET_ADDR_D, 0, 0, 0, 0);
110  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
111  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
112 
113 
114 //#if !UIP_CONF_ROUTER
115 // uip_ds6_prefix_add(&ipaddr, 64, 0); // For on-link determination.
116 //#else
117 // uip_ds6_prefix_add(&ipaddr, 64, 0, 0, 600, 600);
118 //#endif
119 
120  print_addresses();
121 
122 #if RPL_BORDER_ROUTER
123  dag = rpl_set_root(&ipaddr);
124  if(dag != NULL) {
125  PRINTF("This node is setted as root of a DAG.\r\n");
126  }
127  else {
128  PRINTF("Error while setting this node as root of a DAG.\r\n");
129  }
130 #endif
131 
132 }
133 #endif /* FIXED_GLOBAL_ADDRESS */
134 
135 
136 #endif /* UIP_CONF_IPV6 */