Contiki 2.5
contiki-main.c
1 /*
2  * Copyright (c) 2004, Swedish Institute of Computer Science.
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  * Author: Adam Dunkels <adam@sics.se>
32  *
33  * $Id: contiki-main.c,v 1.37 2010/04/21 20:27:28 oliverschmidt Exp $
34  */
35 
36 #include "contiki.h"
37 #include "contiki-net.h"
38 #include "contiki-lib.h"
39 
40 #include "dev/serial-line.h"
41 #include "net/rime.h"
42 
43 #ifdef __CYGWIN__
44 #include "net/wpcap-drv.h"
45 #else /* __CYGWIN__ */
46 #include "net/tapdev-drv.h"
47 #endif /* __CYGWIN__ */
48 #include "net/ethernode-uip.h"
49 #include "net/ethernode-rime.h"
50 #include "net/ethernode.h"
51 #include "net/uip-over-mesh.h"
52 
53 #include "net/mac/nullmac.h"
54 #include "net/mac/lpp.h"
55 
56 #include "ether.h"
57 
58 #include <stdio.h>
59 #ifndef HAVE_SNPRINTF
60 int snprintf(char *str, size_t size, const char *format, ...);
61 #endif /* HAVE_SNPRINTF */
62 
63 #include <stdlib.h>
64 #include <sys/select.h>
65 #include <unistd.h>
66 
67 #include "dev/button-sensor.h"
68 #include "dev/pir-sensor.h"
69 #include "dev/vib-sensor.h"
70 #include "dev/radio-sensor.h"
71 #include "dev/leds.h"
72 
73 #ifdef __CYGWIN__
74 static struct uip_fw_netif extif =
75  {UIP_FW_NETIF(0,0,0,0, 0,0,0,0, wpcap_output)};
76 #else /* __CYGWIN__ */
77 static struct uip_fw_netif extif =
78  {UIP_FW_NETIF(0,0,0,0, 0,0,0,0, tapdev_output)};
79 #endif /* __CYGWIN__ */
80 static struct uip_fw_netif meshif =
81  {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
82 /*static struct uip_fw_netif ethernodeif =
83  {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, ethernode_drv_send)};*/
84 
85 static const struct uip_eth_addr ethaddr = {{0x00,0x06,0x98,0x01,0x02,0x12}};
86 
87 SENSORS(&button_sensor, &pir_sensor, &vib_sensor, &radio_sensor);
88 
89 PROCINIT(&sensors_process, &etimer_process, &tcpip_process);
90 
91 /*---------------------------------------------------------------------------*/
92 #ifdef __CYGWIN__
93 static void
94 remove_route(int s)
95 {
96  char buf[1024];
97 
98  snprintf(buf, sizeof(buf), "route delete %d.%d.%d.%d",
99  uip_ipaddr_to_quad(&meshif.ipaddr));
100  printf("%s\n", buf);
101  system(buf);
102  _exit(0);
103 }
104 #endif /* __CYGWIN__ */
105 /*---------------------------------------------------------------------------*/
106 void
107 contiki_main(int flag)
108 {
109  random_init(getpid());
110  srand(getpid());
111 
112  leds_init();
113 
114  process_init();
115 
116  procinit_init();
117 
118  serial_line_init();
119 
120  uip_init();
121 
122  ctimer_init();
123 
124  NETSTACK_MAC.init();
125  NETSTACK_RDC.init();
126 
127  uip_over_mesh_init(2);
128  uip_over_mesh_set_net(&meshif.ipaddr, &meshif.netmask);
129 
130  if(flag == 1) {
131 #ifdef __CYGWIN__
132  process_start(&wpcap_process, NULL);
133  {
134  char buf[1024];
135  uip_ipaddr_t ifaddr;
136  extern uip_ipaddr_t winifaddr;
137 
138  uip_ipaddr_copy(&ifaddr, &winifaddr);
139 
140  snprintf(buf, sizeof(buf), "route add %d.%d.%d.%d mask %d.%d.%d.%d %d.%d.%d.%d",
141  uip_ipaddr_to_quad(&meshif.ipaddr),
142  uip_ipaddr_to_quad(&meshif.netmask),
143  uip_ipaddr_to_quad(&ifaddr));
144  printf("%s\n", buf);
145  system(buf);
146  signal(SIGTERM, remove_route);
147  }
148 #else /* __CYGWIN__ */
149  process_start(&tapdev_process, NULL);
150 #endif /* __CYGWIN__ */
151  process_start(&uip_fw_process, NULL);
152 
153  uip_fw_register(&meshif);
154  uip_fw_default(&extif);
155  printf("uip_hostaddr %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr));
156  uip_over_mesh_make_announced_gateway();
157  } else {
158  process_start(&uip_fw_process, NULL);
159  uip_fw_default(&meshif);
160  }
161 
162  leds_on(LEDS_GREEN);
163 
164  rtimer_init();
165 
166  autostart_start(autostart_processes);
167 
168  while(1) {
169  int n;
170  struct timeval tv;
171 
172  n = process_run();
173  /* if(n > 0) {
174  printf("%d processes in queue\n", n);
175  }*/
176  tv.tv_sec = 0;
177  tv.tv_usec = 1;
178  select(0, NULL, NULL, NULL, &tv);
180  }
181 }
182 /*---------------------------------------------------------------------------*/
183 process_event_t codeprop_event_quit;