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