Contiki 3.x
init-net.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, University of Colombo School of Computing
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 copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$$
32  */
33 
34 /**
35  * \file
36  * Network initialization for the MICAz port.
37  * \author
38  * Kasun Hewage <kasun.ch@gmail.com>
39  */
40 
41 #include <stdio.h>
42 #include <string.h>
43 #include "queuebuf.h"
44 #include "K60.h"
45 
46 #include "contiki.h"
47 #include "rf230bb.h"
48 #include "net/netstack.h"
49 #include "net/mac/frame802154.h"
50 
51 #define DEBUG 1
52 #if DEBUG
53 #define PRINTF(...) printf(__VA_ARGS__)
54 #else
55 #define PRINTF(...)
56 #endif
57 
58 #if WITH_UIP6
59 #include "net/ipv6/uip-ds6.h"
60 #endif /* WITH_UIP6 */
61 
62 #define UIP_OVER_MESH_CHANNEL 8
63 #ifndef NODE_ID
64 #define NODE_ID 1
65 #warning Node id = 1
66 #else
67 #warning Using user defined node id
68 #endif
69 
70 static unsigned char id[8] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NODE_ID };
71 
72 /*---------------------------------------------------------------------------*/
73 static void
74 set_rime_addr(void)
75 {
76  linkaddr_t addr;
77  unsigned int i;
78 
79  /* memset(&addr, 0x65, sizeof(linkaddr_t)); */
80  memcpy(addr.u8, id, sizeof(addr.u8));
81 
83  PRINTF("Rime started with address ");
84  PRINTF("%d", addr.u8[0]);
85  for(i = 1; i < sizeof(addr.u8); i++) {
86  PRINTF(".%d", addr.u8[i]);
87  }
88  PRINTF("\n");
89 }
90 /*---------------------------------------------------------------------------*/
91 void
92 init_net(void)
93 {
94 #ifndef WITH_SLIP
95  id[0] = (((SIM->UIDL) >> (8 * 0)) & 0xFF) | 0x02;
96  id[1] = ((SIM->UIDL) >> (8 * 1)) & 0xFF;
97  id[2] = ((SIM->UIDL) >> (8 * 2)) & 0xFF;
98  id[3] = ((SIM->UIDL) >> (8 * 3)) & 0xFF;
99  id[4] = ((SIM->UIDML) >> (8 * 0)) & 0xFF;
100  id[5] = ((SIM->UIDML) >> (8 * 1)) & 0xFF;
101  id[6] = ((SIM->UIDML) >> (8 * 2)) & 0xFF;
102  id[7] = ((SIM->UIDML) >> (8 * 3)) & 0xFF;
103 #else
104  /* Use fixt address for border router. */
105  id[0] = 0x02;
106  id[7] = 0x01;
107 #endif
108 #if WITH_UIP6
109  set_rime_addr();
110  NETSTACK_RADIO.init();
111  {
112  uint8_t longaddr[8];
113  uint16_t shortaddr;
114 
115  shortaddr = (linkaddr_node_addr.u8[0] << 8) +
116  linkaddr_node_addr.u8[1];
117  memset(longaddr, 0, sizeof(longaddr));
118  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
119  rf230_set_pan_addr(IEEE802154_CONF_PANID, shortaddr, longaddr);
120  }
121  rf230_set_channel(RF_CHANNEL);
122 
123  memcpy(&uip_lladdr.addr, id, sizeof(uip_lladdr.addr));
124 
125  queuebuf_init();
126  NETSTACK_RDC.init();
127  NETSTACK_MAC.init();
128  NETSTACK_NETWORK.init();
129 
130  PRINTF("%s %s, channel check rate %d Hz, radio channel %d\n",
131  NETSTACK_MAC.name, NETSTACK_RDC.name,
132  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 :
133  NETSTACK_RDC.channel_check_interval()),
134  RF_CHANNEL);
135 
136  process_start(&tcpip_process, NULL);
137 
138  PRINTF("Tentative link-local IPv6 address ");
139  {
140  uip_ds6_addr_t *lladdr;
141  int i;
142  lladdr = uip_ds6_get_link_local(-1);
143  for(i = 0; i < 7; ++i) {
144  PRINTF("%04x:", lladdr->ipaddr.u8[i * 2] * 256 +
145  lladdr->ipaddr.u8[i * 2 + 1]);
146  }
147  PRINTF("%04x\n", lladdr->ipaddr.u8[14] * 256 + lladdr->ipaddr.u8[15]);
148  }
149 
150  if(!UIP_CONF_IPV6_RPL) {
151  uip_ipaddr_t ipaddr;
152  int i;
153  uip_ip6addr(&ipaddr, 0xfdfd, 0, 0, 0, 0, 0, 0, 0);
154  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
155  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
156  PRINTF("Tentative global IPv6 address ");
157  for(i = 0; i < 7; ++i) {
158  PRINTF("%04x:",
159  ipaddr.u8[i * 2] * 256 + ipaddr.u8[i * 2 + 1]);
160  }
161  PRINTF("%04x\n",
162  ipaddr.u8[7 * 2] * 256 + ipaddr.u8[7 * 2 + 1]);
163  }
164 
165 #else /* If no radio stack should be used only turn on radio and set it to sleep for minimal power consumption */
166  rf230_init();
167  rf230_driver.off();
168 #endif /* WITH_UIP6 */
169 }
170 /*---------------------------------------------------------------------------*/
171 
172 unsigned char *
173 init_net_get_addr()
174 {
175  return id;
176 }
#define IEEE802154_CONF_PANID
Default PAN ID: TI.
Definition: contiki-conf.h:390
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
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
Network interface and stateless autoconfiguration (RFC 4862)
#define NULL
The null pointer.
#define SIM
Peripheral SIM base pointer.
Definition: MK60D10.h:7650
K60 hardware register header wrapper.
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
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
int(* off)(void)
Turn the radio off.
Definition: radio.h:255
Include file for the Contiki low-layer network stack (NETSTACK)
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82