Contiki 3.x
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  *
33  */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <signal.h>
38 #include <time.h>
39 #include <sys/select.h>
40 #include <unistd.h>
41 #include <memory.h>
42 
43 #include "contiki.h"
44 #include "contiki-net.h"
45 #include "lib/assert.h"
46 
47 #include "dev/serial-line.h"
48 
49 #include "net/ip/uip.h"
50 #ifdef __CYGWIN__
51 #include "net/wpcap-drv.h"
52 #else /* __CYGWIN__ */
53 #include "net/tapdev-drv.h"
54 #endif /* __CYGWIN__ */
55 
56 #ifdef __CYGWIN__
57 PROCINIT(&etimer_process, &tcpip_process, &wpcap_process, &serial_line_process);
58 #else /* __CYGWIN__ */
59 PROCINIT(&etimer_process, &tapdev_process, &tcpip_process, &serial_line_process);
60 #endif /* __CYGWIN__ */
61 
62 #if RPL_BORDER_ROUTER
63 #include "net/rpl/rpl.h"
64 
65 static uint16_t dag_id[] = {0x1111, 0x1100, 0, 0, 0, 0, 0, 0x0011};
66 
67 PROCESS(border_router_process, "RPL Border Router");
68 PROCESS_THREAD(border_router_process, ev, data)
69 {
70 
71  PROCESS_BEGIN();
72 
73  PROCESS_PAUSE();
74 
75  {
76  rpl_dag_t *dag;
77  char buf[sizeof(dag_id)];
78  memcpy(buf,dag_id,sizeof(dag_id));
79  dag = rpl_set_root((uip_ip6addr_t *)buf);
80 
81  /* Assign separate addresses to the uip stack and the host network
82  interface, but with the same prefix E.g. bbbb::ff:fe00:200 to
83  the stack and bbbb::1 to the host *fallback* network interface
84  Otherwise the host will trap packets intended for the stack,
85  just as the stack will trap packets intended for the host
86  $ifconfig usb0 -arp on Ubuntu to skip the neighbor
87  solicitations. Add explicit neighbors on other OSs */
88 
89  if(dag != NULL) {
90  printf("Created a new RPL dag\n");
91 
92 #if UIP_CONF_ROUTER_RECEIVE_RA
93  /* Contiki stack will shut down until assigned an address from the
94  interface RA Currently this requires changes in the core
95  rpl-icmp6.c to pass the link-local RA broadcast.
96  */
97 
98 #else
99  {
100  static void sprint_ip6(uip_ip6addr_t addr);
101  int i;
102  uip_ip6addr_t ipaddr;
103 #ifdef HARD_CODED_ADDRESS
104  uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
105 #else
106  uip_ip6addr(&ipaddr, 0xbbbb, 0, 0, 0, 0, 0, 0, 0x1);
107 #endif
108  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
109  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
110  rpl_set_prefix(dag, &ipaddr, 64);
111 
112  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
113  if(uip_ds6_if.addr_list[i].isused) {
114  printf("IPV6 Address: ");
115  sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);
116  printf("\n");
117  }
118  }
119  }
120 #endif
121  }
122  }
123  /* The border router runs with a 100% duty cycle in order to ensure high
124  packet reception rates. */
125  /* NETSTACK_MAC.off(1); */
126 
127  while(1) {
128  PROCESS_YIELD();
129  /* Local and global dag repair can be done from ? */
130  /* rpl_set_prefix(rpl_get_dag(RPL_ANY_INSTANCE), &ipaddr, 64);
131  rpl_repair_dag(rpl_get_dag(RPL_ANY_INSTANCE)); */
132  }
133 
134  PROCESS_END();
135 }
136 #endif /* RPL_BORDER_ROUTER */
137 
138 #if UIP_CONF_IPV6
139 /*---------------------------------------------------------------------------*/
140 static void
141 sprint_ip6(uip_ip6addr_t addr)
142 {
143  unsigned char i = 0;
144  unsigned char zerocnt = 0;
145  unsigned char numprinted = 0;
146  unsigned char notskipped = 0;
147  char thestring[40];
148  char *result = thestring;
149 
150  *result++ = '[';
151  while(numprinted < 8) {
152  if((addr.u16[i] == 0) && (zerocnt == 0)) {
153  while(addr.u16[zerocnt + i] == 0) {
154  zerocnt++;
155  }
156  if(zerocnt == 1 && notskipped) {
157  *result++ = '0';
158  numprinted++;
159  notskipped = 1;
160  continue;
161  }
162  i += zerocnt;
163  numprinted += zerocnt;
164  } else {
165  result += sprintf(result, "%x", (unsigned int)(uip_ntohs(addr.u16[i])));
166  i++;
167  numprinted++;
168  }
169  if(numprinted != 8) {
170  *result++ = ':';
171  }
172  }
173  *result++=']';
174  *result=0;
175  printf("%s", thestring);
176 }
177 #endif /* UIP_CONF_IPV6 */
178 /*---------------------------------------------------------------------------*/
179 int contiki_argc = 0;
180 char **contiki_argv;
181 
182 int
183 main(int argc, char **argv)
184 {
185  /* crappy way of remembering and accessing argc/v */
186  contiki_argc = argc;
187  contiki_argv = argv;
188 
189  /* minimal-net under windows is hardcoded to use the first one or two args */
190  /* for wpcap configuration so this needs to be "removed" from contiki_args */
191 #ifdef __CYGWIN__
192  contiki_argc--;
193  contiki_argv++;
194 #ifdef UIP_FALLBACK_INTERFACE
195  contiki_argc--;
196  contiki_argv++;
197 #endif
198 #endif
199 
200  clock_init();
201 #if UIP_CONF_IPV6
202 /* A hard coded address overrides the stack default MAC address to
203  allow multiple instances. uip6.c defines it as
204  {0x00,0x06,0x98,0x00,0x02,0x32} giving an ipv6 address of
205  [fe80::206:98ff:fe00:232] We make it simpler, {0x02,0x00,0x00 + the
206  last three bytes of the hard coded address (if any are nonzero).
207  HARD_CODED_ADDRESS can be defined in the contiki-conf.h file, or
208  here to allow quick builds using different addresses. If
209  HARD_CODED_ADDRESS has a prefix it also applied, unless built as a
210  RPL end node. E.g. bbbb::12:3456 becomes fe80::ff:fe12:3456 and
211  prefix bbbb::/64 if non-RPL ::10 becomes fe80::ff:fe00:10 and
212  prefix awaits RA or RPL formation bbbb:: gives an address of
213  bbbb::206:98ff:fe00:232 if non-RPL */
214 #ifdef HARD_CODED_ADDRESS
215  {
216  uip_ipaddr_t ipaddr;
217  uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
218  if((ipaddr.u8[13] != 0) ||
219  (ipaddr.u8[14] != 0) ||
220  (ipaddr.u8[15] != 0)) {
221  if(sizeof(uip_lladdr) == 6) { /* Minimal-net uses ethernet MAC */
222  uip_lladdr.addr[0] = 0x02;
223  uip_lladdr.addr[1] = 0;
224  uip_lladdr.addr[2] = 0;
225  uip_lladdr.addr[3] = ipaddr.u8[13];
226  uip_lladdr.addr[4] = ipaddr.u8[14];
227  uip_lladdr.addr[5] = ipaddr.u8[15];
228  }
229  }
230  }
231 #endif /* HARD_CODED_ADDRESS */
232 #endif /* UIP_CONF_IPV6 */
233 
234  process_init();
235 /* procinit_init initializes RPL which sets a ctimer for the first DIS */
236 /* We must start etimers and ctimers,before calling it */
237  process_start(&etimer_process, NULL);
238  ctimer_init();
239 
240 #if RPL_BORDER_ROUTER
241  process_start(&border_router_process, NULL);
242  printf("Border Router Process started\n");
243 #elif UIP_CONF_IPV6_RPL
244  printf("RPL enabled\n");
245 #endif
246 
247  procinit_init();
248  autostart_start(autostart_processes);
249 
250  /* Set default IP addresses if not specified */
251 #if !UIP_CONF_IPV6
252  {
253  uip_ipaddr_t addr;
254 
255  uip_gethostaddr(&addr);
256  if(addr.u8[0] == 0) {
257  uip_ipaddr(&addr, 172,18,0,2);
258  }
259  printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
260  uip_sethostaddr(&addr);
261 
262  uip_getnetmask(&addr);
263  if(addr.u8[0] == 0) {
264  uip_ipaddr(&addr, 255,255,0,0);
265  uip_setnetmask(&addr);
266  }
267  printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
268 
269  uip_getdraddr(&addr);
270  if(addr.u8[0] == 0) {
271  uip_ipaddr(&addr, 172,18,0,1);
272  uip_setdraddr(&addr);
273  }
274  printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
275  }
276 #else /* UIP_CONF_IPV6 */
277 
278 #if !UIP_CONF_IPV6_RPL
279  {
280  uip_ipaddr_t ipaddr;
281 #ifdef HARD_CODED_ADDRESS
282  uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
283 #else
284  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
285 #endif
286  if((ipaddr.u16[0] != 0) ||
287  (ipaddr.u16[1] != 0) ||
288  (ipaddr.u16[2] != 0) ||
289  (ipaddr.u16[3] != 0)) {
290 #if UIP_CONF_ROUTER
291  if(!uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0)) {
292  fprintf(stderr,"uip_ds6_prefix_add() failed.\n");
293  exit(EXIT_FAILURE);
294  }
295 #else /* UIP_CONF_ROUTER */
296  if(!uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0)) {
297  fprintf(stderr,"uip_ds6_prefix_add() failed.\n");
298  exit(EXIT_FAILURE);
299  }
300 #endif /* UIP_CONF_ROUTER */
301 
302  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
303  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
304  }
305  }
306 #endif /* !UIP_CONF_IPV6_RPL */
307 
308 #endif /* !UIP_CONF_IPV6 */
309 
310  // procinit_init();
311  // autostart_start(autostart_processes);
312 
313  /* Make standard output unbuffered. */
314  setvbuf(stdout, (char *)NULL, _IONBF, 0);
315 
316  printf("\n*******%s online*******\n",CONTIKI_VERSION_STRING);
317 
318 #if UIP_CONF_IPV6 && !RPL_BORDER_ROUTER /* Border router process prints addresses later */
319  {
320  int i = 0;
321  int interface_count = 0;
322  for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
323  if(uip_ds6_if.addr_list[i].isused) {
324  printf("IPV6 Addresss: ");
325  sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);
326  printf("\n");
327  interface_count++;
328  }
329  }
330  assert(0 < interface_count);
331  }
332 #endif
333 
334  while(1) {
335  fd_set fds;
336  int n;
337  struct timeval tv;
338  clock_time_t next_event;
339 
340  n = process_run();
341  next_event = etimer_next_expiration_time() - clock_time();
342 
343 #if DEBUG_SLEEP
344  if(n > 0)
345  printf("sleep: %d events pending\n",n);
346  else
347  printf("sleep: next event @ T-%.03f\n",(double)next_event / (double)CLOCK_SECOND);
348 #endif
349 
350 #ifdef __CYGWIN__
351  /* wpcap doesn't appear to support select, so
352  * we can't idle the process on windows. */
353  next_event = 0;
354 #endif
355 
356  if(next_event > (CLOCK_SECOND * 2))
357  next_event = CLOCK_SECOND * 2;
358  tv.tv_sec = n ? 0 : (next_event / CLOCK_SECOND);
359  tv.tv_usec = n ? 0 : ((next_event % 1000) * 1000);
360 
361  FD_ZERO(&fds);
362  FD_SET(STDIN_FILENO, &fds);
363 #ifdef __CYGWIN__
364  select(1, &fds, NULL, NULL, &tv);
365 #else
366  FD_SET(tapdev_fd(), &fds);
367  if(0 > select(tapdev_fd() + 1, &fds, NULL, NULL, &tv)) {
368  perror("Call to select() failed.");
369  exit(EXIT_FAILURE);
370  }
371 #endif
372 
373  if(FD_ISSET(STDIN_FILENO, &fds)) {
374  char c;
375  if(read(STDIN_FILENO, &c, 1) > 0) {
377  }
378  }
379 #ifdef __CYGWIN__
380  process_poll(&wpcap_process);
381 #else
382  process_poll(&tapdev_process);
383 #endif
385  }
386 
387  return 0;
388 }
389 /*---------------------------------------------------------------------------*/
390 void
391 log_message(char *m1, char *m2)
392 {
393  printf("%s%s\n", m1, m2);
394 }
395 /*---------------------------------------------------------------------------*/
396 void
397 uip_log(char *m)
398 {
399  printf("uIP: '%s'\n", m);
400 }
401 /*---------------------------------------------------------------------------*/
402 void
403 _xassert(const char *file, int line)
404 {
405  fprintf(stderr, "%s:%u: failed assertion\n", file, line);
406  abort();
407 }
408 
409 
410 unsigned short
411 sensors_light1(void)
412 {
413  static unsigned short count;
414  return count++;
415 }
416 /*---------------------------------------------------------------------------*/
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
#define uip_sethostaddr(addr)
Set the IP address of this host.
Definition: uip.h:195
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define uip_gethostaddr(addr)
Get the IP address of this host.
Definition: uip.h:215
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Header file for the uIP TCP/IP stack.
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.
void uip_log(char *msg)
Print out a uIP log message.
Definition: uip-log.c:3
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
int main(void)
This is main...
Definition: ethconfig.c:49
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition: uiplib.h:71
void process_init(void)
Initialize the process module.
Definition: process.c:208
#define uip_getdraddr(addr)
Get the default router&#39;s IP address.
Definition: uip.h:250
#define uip_setdraddr(addr)
Set the default router&#39;s IP address.
Definition: uip.h:227
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
Generic serial I/O process header filer.
#define uip_getnetmask(addr)
Get the netmask.
Definition: uip.h:260
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
Definition: etimer.c:229
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
Definition: uip.h:927
#define PROCESS_PAUSE()
Yield the process for a short while.
Definition: process.h:221
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
Definition: uip.h:955
#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
#define uip_setnetmask(addr)
Set the netmask.
Definition: uip.h:239
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82