Contiki 3.x
init-net.c
Go to the documentation of this file.
1 /*
2  * Contiki SeedEye Platform project
3  *
4  * Copyright (c) 2012,
5  * Scuola Superiore Sant'Anna (http://www.sssup.it) and
6  * Consorzio Nazionale Interuniversitario per le Telecomunicazioni
7  * (http://www.cnit.it).
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /**
38  * \addtogroup SeedEye Contiki SEEDEYE Platform
39  *
40  * @{
41  */
42 
43 /**
44  * \file platform/seedeye/init-net.c
45  * \brief Network initialization for the SEEDEYE port.
46  * \author Giovanni Pellerano <giovanni.pellerano@evilaliv3.org>
47  * \date 2012-03-25
48  */
49 
50 #include <contiki.h>
51 #include <contiki-net.h>
52 #include <net/netstack.h>
53 #include <net/queuebuf.h>
54 #include <net/mac/frame802154.h>
55 
56 #include <lib/random.h>
57 #include <time.h>
58 
59 #include <mrf24j40.h>
60 
61 #define DEBUG 1
62 #if DEBUG
63 #include <stdio.h>
64 #define PRINTF(...) printf(__VA_ARGS__)
65 #else
66 #define PRINTF(...)
67 #endif
68 
69 /*---------------------------------------------------------------------------*/
70 void
71 init_net(uint8_t node_id)
72 {
73  uint16_t shortaddr;
74  uint64_t longaddr;
75  linkaddr_t addr;
76 #if WITH_UIP6
77  uip_ds6_addr_t *lladdr;
78  uip_ipaddr_t ipaddr;
79 #endif
80 
81  uint8_t i;
82 
83  memset(&shortaddr, 0, sizeof(shortaddr));
84  memset(&longaddr, 0, sizeof(longaddr));
85  *((uint8_t *)&shortaddr) = node_id >> 8;
86  *((uint8_t *)&shortaddr + 1) = node_id;
87  *((uint8_t *)&longaddr) = node_id >> 8;
88  *((uint8_t *)&longaddr + 1) = node_id;
89  for(i = 2; i < sizeof(longaddr); ++i) {
90  ((uint8_t *)&longaddr)[i] = random_rand();
91  }
92 
93  PRINTF("SHORT MAC ADDRESS %02x:%02x\n",
94  *((uint8_t *) & shortaddr), *((uint8_t *) & shortaddr + 1));
95 
96  PRINTF("EXTENDED MAC ADDRESS %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
97  *((uint8_t *)&longaddr),
98  *((uint8_t *)&longaddr + 1),
99  *((uint8_t *)&longaddr + 2),
100  *((uint8_t *)&longaddr + 3),
101  *((uint8_t *)&longaddr + 4),
102  *((uint8_t *)&longaddr + 5),
103  *((uint8_t *)&longaddr + 6),
104  *((uint8_t *)&longaddr + 7));
105 
106  memset(&addr, 0, sizeof(linkaddr_t));
107 
108  for(i = 0; i < sizeof(addr.u8); ++i) {
109  addr.u8[i] = ((uint8_t *)&longaddr)[i];
110  }
111 
112  linkaddr_set_node_addr(&addr);
113 
114  PRINTF("Rime started with address: ");
115  for(i = 0; i < sizeof(addr.u8) - 1; ++i) {
116  PRINTF("%d.", addr.u8[i]);
117  }
118  PRINTF("%d\n", addr.u8[i]);
119 
120  queuebuf_init();
121 
122  NETSTACK_RADIO.init();
123 
124  mrf24j40_set_channel(RF_CHANNEL);
125  mrf24j40_set_panid(IEEE802154_PANID);
126  mrf24j40_set_short_mac_addr(shortaddr);
128 
129  NETSTACK_RDC.init();
130  NETSTACK_MAC.init();
131  NETSTACK_NETWORK.init();
132 
133  PRINTF("%s %s, channel check rate %d Hz, radio channel %u\n",
134  NETSTACK_MAC.name, NETSTACK_RDC.name,
135  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 :
136  NETSTACK_RDC.channel_check_interval()), RF_CHANNEL);
137 
138 #if WITH_UIP6
139 
140 #if LINKADDR_CONF_SIZE == 2
141  memset(&uip_lladdr.addr, 0, sizeof(uip_lladdr.addr));
142  uip_lladdr.addr[3] = 0xff;
143  uip_lladdr.addr[4]= 0xfe;
144  memcpy(&uip_lladdr.addr[6], &shortaddr, sizeof(shortaddr));
145 #else
146  memcpy(&uip_lladdr.addr, &longaddr, sizeof(uip_lladdr.addr));
147 #endif
148 
149  process_start(&tcpip_process, NULL);
150 
151  lladdr = uip_ds6_get_link_local(-1);
152 
153  PRINTF("Tentative link-local IPv6 address ");
154 
155  for(i = 0; i < 7; ++i) {
156  PRINTF("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]);
157  }
158 
159  PRINTF("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
160 
161  if(!UIP_CONF_IPV6_RPL) {
162  uip_ip6addr(&ipaddr, 0x2001, 0x1418, 0x100, 0x823c, 0, 0, 0, 0);
163  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
164  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
165 
166  PRINTF("Tentative global IPv6 address ");
167 
168  for(i = 0; i < 7; ++i) {
169  PRINTF("%02x%02x:", ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
170  }
171 
172  PRINTF("%02x%02x\n", ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
173  }
174 #endif
175 }
176 /*---------------------------------------------------------------------------*/
177 
178 /** @} */
void mrf24j40_set_short_mac_addr(uint16_t addr)
Store short MAC address.
Definition: mrf24j40.c:228
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
802.15.4 frame creation and parsing functions
Unicast address structure.
Definition: uip-ds6.h:172
uip_ipaddr_t ipaddr
The IP address of this interface.
Definition: uip-fw.h:57
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
void mrf24j40_set_channel(uint16_t ch)
Set the channel.
Definition: mrf24j40.c:203
MRF24J40 Driver.
#define NULL
The null pointer.
void mrf24j40_set_panid(uint16_t id)
Store MAC PAN ID.
Definition: mrf24j40.c:216
void mrf24j40_set_extended_mac_addr(uint64_t addr)
Store extended MAC address.
Definition: mrf24j40.c:240
Header file for the Rime queue buffer management
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition: uip-ds6.h:123
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
#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
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
Definition: random.c:47
Include file for the Contiki low-layer network stack (NETSTACK)
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82