Contiki 2.5
contiki-main.c
1 /*
2  * Copyright (c) 2002, Adam Dunkels.
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-main.c,v 1.25 2010/10/19 18:29:05 adamdunkels Exp $
33  *
34  */
35 
36 #include <stdio.h>
37 #include <time.h>
38 #include <sys/select.h>
39 #include <unistd.h>
40 #include <memory.h>
41 
42 #include "contiki.h"
43 #include "contiki-net.h"
44 
45 #include "dev/serial-line.h"
46 
47 #include "net/uip.h"
48 #ifdef __CYGWIN__
49 #include "net/wpcap-drv.h"
50 #else /* __CYGWIN__ */
51 #include "net/tapdev-drv.h"
52 #endif /* __CYGWIN__ */
53 
54 #ifdef __CYGWIN__
55 PROCINIT(&etimer_process, &tcpip_process, &wpcap_process, &serial_line_process);
56 #else /* __CYGWIN__ */
57 PROCINIT(&etimer_process, &tapdev_process, &tcpip_process, &serial_line_process);
58 #endif /* __CYGWIN__ */
59 
60 #if RPL_BORDER_ROUTER
61 #include "net/rpl/rpl.h"
62 
63 uint16_t dag_id[] = {0x1111, 0x1100, 0, 0, 0, 0, 0, 0x0011};
64 
65 PROCESS(border_router_process, "RPL Border Router");
66 PROCESS_THREAD(border_router_process, ev, data)
67 {
68 
69  PROCESS_BEGIN();
70 
71  PROCESS_PAUSE();
72 
73 { rpl_dag_t *dag;
74  char buf[sizeof(dag_id)];
75  memcpy(buf,dag_id,sizeof(dag_id));
76  dag = rpl_set_root((uip_ip6addr_t *)buf);
77 
78 /* Assign separate addresses to the uip stack and the host network interface, but with the same prefix */
79 /* E.g. bbbb::ff:fe00:200 to the stack and bbbb::1 to the host *fallback* network interface */
80 /* Otherwise the host will trap packets intended for the stack, just as the stack will trap packets intended for the host */
81 /* $ifconfig usb0 -arp on Ubuntu to skip the neighbor solicitations. Add explicit neighbors on other OSs */
82  if(dag != NULL) {
83  printf("Created a new RPL dag\n");
84 
85 #if UIP_CONF_ROUTER_RECEIVE_RA
86 //Contiki stack will shut down until assigned an address from the interface RA
87 //Currently this requires changes in the core rpl-icmp6.c to pass the link-local RA broadcast
88 
89 #else
90 void sprint_ip6(uip_ip6addr_t addr);
91  int i;
92  uip_ip6addr_t ipaddr;
93 #ifdef HARD_CODED_ADDRESS
94  uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
95 #else
96  uip_ip6addr(&ipaddr, 0xbbbb, 0, 0, 0, 0, 0, 0, 0x1);
97 #endif
99  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
100  rpl_set_prefix(dag, &ipaddr, 64);
101 
102  for (i=0;i<UIP_DS6_ADDR_NB;i++) {
103  if (uip_ds6_if.addr_list[i].isused) {
104  printf("IPV6 Address: ");sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);printf("\n");
105  }
106  }
107 #endif
108  }
109 }
110  /* The border router runs with a 100% duty cycle in order to ensure high
111  packet reception rates. */
112  // NETSTACK_MAC.off(1);
113 
114  while(1) {
115  PROCESS_YIELD();
116  /* Local and global dag repair can be done from ? */
117  // rpl_set_prefix(rpl_get_dag(RPL_ANY_INSTANCE), &ipaddr, 64);
118  // rpl_repair_dag(rpl_get_dag(RPL_ANY_INSTANCE));
119 
120  }
121 
122  PROCESS_END();
123 }
124 #endif /* RPL_BORDER_ROUTER */
125 
126 #if UIP_CONF_IPV6
127 /*---------------------------------------------------------------------------*/
128 void
129 sprint_ip6(uip_ip6addr_t addr)
130 {
131  unsigned char i = 0;
132  unsigned char zerocnt = 0;
133  unsigned char numprinted = 0;
134  char thestring[40];
135  char * result = thestring;
136 
137  *result++='[';
138  while (numprinted < 8) {
139  if ((addr.u16[i] == 0) && (zerocnt == 0)) {
140  while(addr.u16[zerocnt + i] == 0) zerocnt++;
141  if (zerocnt == 1) {
142  *result++ = '0';
143  numprinted++;
144  break;
145  }
146  i += zerocnt;
147  numprinted += zerocnt;
148  } else {
149  result += sprintf(result, "%x", (unsigned int)(uip_ntohs(addr.u16[i])));
150  i++;
151  numprinted++;
152  }
153  if (numprinted != 8) *result++ = ':';
154  }
155  *result++=']';
156  *result=0;
157  printf("%s",thestring);
158 }
159 #endif /* UIP_CONF_IPV6 */
160 /*---------------------------------------------------------------------------*/
161 
162 /*---------------------------------------------------------------------------*/
163 int
164 main(void)
165 {
166  clock_init();
167 #if UIP_CONF_IPV6
168 /* A hard coded address overrides the stack default MAC address to allow multiple instances.
169  * uip6.c defines it as {0x00,0x06,0x98,0x00,0x02,0x32} giving an ipv6 address of [fe80::206:98ff:fe00:232]
170  * We make it simpler, {0x02,0x00,0x00 + the last three bytes of the hard coded address (if any are nonzero).
171  * HARD_CODED_ADDRESS can be defined in the contiki-conf.h file, or here to allow quick builds using different addresses.
172  * If HARD_CODED_ADDRESS has a prefix it also applied, unless built as a RPL end node.
173  * E.g. bbbb::12:3456 becomes fe80::ff:fe12:3456 and prefix bbbb::/64 if non-RPL
174  * ::10 becomes fe80::ff:fe00:10 and prefix awaits RA or RPL formation
175  * bbbb:: gives an address of bbbb::206:98ff:fe00:232 if non-RPL
176 */
177 //#define HARD_CODED_ADDRESS "bbbb::20"
178 #ifdef HARD_CODED_ADDRESS
179 {
180  uip_ipaddr_t ipaddr;
181  uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
182  if ((ipaddr.u8[13]!=0) || (ipaddr.u8[14]!=0) || (ipaddr.u8[15]!=0)) {
183  if (sizeof(uip_lladdr)==6) { //Minimal-net uses ethernet MAC
184  uip_lladdr.addr[0]=0x02;uip_lladdr.addr[1]=0;uip_lladdr.addr[2]=0;
185  uip_lladdr.addr[3]=ipaddr.u8[13];;
186  uip_lladdr.addr[4]=ipaddr.u8[14];
187  uip_lladdr.addr[5]=ipaddr.u8[15];
188  }
189  }
190 }
191 #endif
192 #endif
193 
194  process_init();
195 /* procinit_init initializes RPL which sets a ctimer for the first DIS */
196 /* We must start etimers and ctimers,before calling it */
197  process_start(&etimer_process, NULL);
198  ctimer_init();
199 
200  procinit_init();
201  autostart_start(autostart_processes);
202 
203 #if RPL_BORDER_ROUTER
204  process_start(&border_router_process, NULL);
205  printf("Border Router Process started\n");
206 #elif UIP_CONF_IPV6_RPL
207  printf("RPL enabled\n");
208 #endif
209 
210  /* Set default IP addresses if not specified */
211 #if !UIP_CONF_IPV6
212  uip_ipaddr_t addr;
213 
214  uip_gethostaddr(&addr);
215  if (addr.u8[0]==0) {
216  uip_ipaddr(&addr, 10,1,1,1);
217  }
218  printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
219  uip_sethostaddr(&addr);
220 
221  uip_getnetmask(&addr);
222  if (addr.u8[0]==0) {
223  uip_ipaddr(&addr, 255,0,0,0);
224  uip_setnetmask(&addr);
225  }
226  printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
227 
228  uip_getdraddr(&addr);
229  if (addr.u8[0]==0) {
230  uip_ipaddr(&addr, 10,1,1,100);
231  uip_setdraddr(&addr);
232  }
233  printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
234 
235 #else /* UIP_CONF_IPV6 */
236 
237 #if !UIP_CONF_IPV6_RPL
238  uip_ipaddr_t ipaddr;
239 #ifdef HARD_CODED_ADDRESS
240  uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
241 #else
242  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
243 #endif
244  if ((ipaddr.u16[0]!=0) || (ipaddr.u16[1]!=0) || (ipaddr.u16[2]!=0) || (ipaddr.u16[3]!=0)) {
245 #if UIP_CONF_ROUTER
246  uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0);
247 #else /* UIP_CONF_ROUTER */
248  uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
249 #endif /* UIP_CONF_ROUTER */
250 #if !UIP_CONF_IPV6_RPL
251  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
252  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
253 #endif
254  }
255 #endif
256 
257 #if !RPL_BORDER_ROUTER //Border router process prints addresses later
258 { uint8_t i;
259  for (i=0;i<UIP_DS6_ADDR_NB;i++) {
260  if (uip_ds6_if.addr_list[i].isused) {
261  printf("IPV6 Addresss: ");sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);printf("\n");
262  }
263  }
264 }
265 #endif
266 #endif /* !UIP_CONF_IPV6 */
267 
268  /* Make standard output unbuffered. */
269  setvbuf(stdout, (char *)NULL, _IONBF, 0);
270 
271  printf("\n*******%s online*******\n",CONTIKI_VERSION_STRING);
272 
273  while(1) {
274  fd_set fds;
275  int n;
276  struct timeval tv;
277 
278  n = process_run();
279  /* if(n > 0) {
280  printf("%d processes in queue\n");
281  }*/
282 
283  tv.tv_sec = 0;
284  tv.tv_usec = 1;
285  FD_ZERO(&fds);
286  FD_SET(STDIN_FILENO, &fds);
287  select(1, &fds, NULL, NULL, &tv);
288 
289  if(FD_ISSET(STDIN_FILENO, &fds)) {
290  char c;
291  if(read(STDIN_FILENO, &c, 1) > 0) {
292  serial_line_input_byte(c);
293  }
294  }
296  }
297 
298  return 0;
299 }
300 /*---------------------------------------------------------------------------*/
301 void log_message(char *m1, char *m2)
302 {
303  printf("%s%s\n", m1, m2);
304 }
305 /*---------------------------------------------------------------------------*/
306 void
307 uip_log(char *m)
308 {
309  printf("uIP: '%s'\n", m);
310 }
311 /*---------------------------------------------------------------------------*/
312 unsigned short
313 sensors_light1(void)
314 {
315  static unsigned short count;
316  return count++;
317 }
318 /*---------------------------------------------------------------------------*/