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