Contiki 2.5
main.c
Go to the documentation of this file.
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: main.c,v 1.13 2008/11/09 12:30:32 adamdunkels Exp $
34  */
35 
36 /**
37  * \file
38  * This file implements the main function of the Contiki distributed
39  * sensor network simulation environment.
40  * \author Adam Dunkels <adam@sics.se>
41  *
42  * When starting, each sensor node is created as its own process. The
43  * sensor node processes communicates with the starting process using
44  * named pipes. These pipes carry messages such as data packets and
45  * configuration and statistics information requests.
46  */
47 #include "contiki-net.h"
48 #include "display.h"
49 #include "contiki-main.h"
50 #include "nodes.h"
51 #include "ether.h"
52 #include "node.h"
53 
54 #include "net/ethernode.h"
55 
56 #include <sys/types.h>
57 #include <unistd.h>
58 #include <signal.h>
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <sys/wait.h>
62 #include <arpa/inet.h>
63 
64 in_addr_t gwaddr, winifaddr;
65 
66 void netsim_init(void);
67 
68 static int main_process = 0;
69 
70 /*---------------------------------------------------------------------------*/
71 static void
72 sigchld_handler(int sig)
73 {
74  int status;
75  pid_t pid;
76  struct nodes_node *node;
77  if(!main_process) {
78  return;
79  }
80 
81  pid = waitpid(-1, &status, WNOHANG);
82 
83  if(WIFSIGNALED(status) &&
84  WTERMSIG(status) == SIGSEGV) {
85  node = nodes_find_pid(pid);
86  if(node == NULL) {
87  printf("A Contiki node crashed, but it wasn't even started by the system. Something weird is going on!\n");
88  } else {
89  printf("Contiki node at (%d, %d) crashed - Segmentation fault\n",
90  node->x, node->y);
91  }
92  }
93 }
94 /*---------------------------------------------------------------------------*/
95 static void
96 idle(void)
97 {
98  int events;
99 
100  do {
101  ether_server_poll();
102  display_tick();
103  display_redraw();
104  ether_tick();
105  events = process_run();
106  if(events > 0) {
107  printf("events %d\n", events);
108  }
109  } while(events > 0);
110 
111 }
112 /*---------------------------------------------------------------------------*/
113 static int
114 start_node(int x, int y, int b)
115 {
116  pid_t pid;
117  struct timeval tv;
118  static unsigned short port = NODES_PORTBASE;
119 
120  pid = fork();
121 
122  if(pid == 0) {
123 
124  /* This is the sensor process. */
125  main_process = 0;
126 
127  /* Make standard output unbuffered. */
128  setvbuf(stdout, (char *)NULL, _IONBF, 0);
129 
130 
131  srand(getpid());
132 
133  tv.tv_sec = 0;
134  tv.tv_usec = 1000 * (rand() % 1000);
135  select(0, NULL, NULL, NULL, &tv);
136 
137  node_init(port - NODES_PORTBASE + 2, x, y, b);
138  ethernode_init(port);
139 
140  contiki_main(b);
141 
142  /* NOTREACHED */
143  }
144 
145  if(b) {
146  nodes_base_node_port = port;
147  }
148 
149  /* printf("Adding sensor %d at (%d,%d)\n", pid, x, y);*/
150  main_process = 1;
151  nodes_add(pid, x, y, port, port - NODES_PORTBASE + 2);
152 
153 
154  ++port;
155  return port - NODES_PORTBASE + 1;
156 }
157 /*---------------------------------------------------------------------------*/
158 int
159 main_add_node(int x, int y)
160 {
161  return start_node(x, y, 0);
162 }
163 /*---------------------------------------------------------------------------*/
164 void
165 main_add_base(int x, int y)
166 {
167  start_node(x, y, 1);
168 }
169 /*---------------------------------------------------------------------------*/
170 int
171 main(int argc, char **argv)
172 {
173 #ifdef __CYGWIN__
174  if(argc == 3 &&
175  inet_addr(argv[1]) == INADDR_NONE &&
176  inet_addr(argv[2]) == INADDR_NONE) {
177  printf("usage: %s <ip addr of ethernet card to share> "
178  "<ip addr of netsim gateway>\n", argv[0]);
179  exit(1);
180  } else if(argc >= 2) {
181  gwaddr = inet_addr(argv[2]);
182  winifaddr = inet_addr(argv[1]);
183  }
184 #endif /* __CYGWIN__ */
185 
186  /* system("ifconfig tap0 inet 192.168.250.1");*/
187  /* system("route delete 172.16.0.0/16");
188  system("route add 172.16.0.0/16 192.168.250.2");*/
189 
190  nodes_init();
191 
192  atexit(nodes_kill);
193  atexit(ether_print_stats);
194 
195  netsim_init();
196 
197  ether_server_init();
198 
199 #if 0
200  while(1) {
201  ether_server_poll();
202  ether_tick();
203  process_run();
204  usleep(100);
205  }
206 #endif /* 0 */
207 
208 #ifdef __CYGWIN__
209  if(argc > 1 && (strcmp(argv[1], "-q") ||
210  strcmp(argv[2], "-q") ||
211  strcmp(argv[3], "-q")) == 0) {
212 #else /* __CYGWIN__ */
213  if(argc > 1 && strcmp(argv[1], "-q") == 0) {
214 #endif /* __CYGWIN__ */
215  display_init(idle, 50, 0);
216  } else {
217  display_init(idle, 50, 1);
218  }
219  display_redraw();
220 
221  signal(SIGCHLD, sigchld_handler);
222 
223  display_run();
224 
225  return 0;
226 
227  argv = argv;
228  argc = argc;
229 }
230 /*-----------------------------------------------------------------------------------*/
231 char *arg_alloc(char size) {return NULL;}
232 void arg_init(void) {}
233 void arg_free(char *arg) {}
234 /*-----------------------------------------------------------------------------------*/
235 
236 char *shell_prompt_text = "sensor-router> ";
237 
238 /*-----------------------------------------------------------------------------------*/
239 #include <sys/time.h>
240 
241 static signed long drift = 0;
242 
243 void
244 clock_delay(unsigned int num)
245 {
246  struct timeval tv;
247  tv.tv_sec = 0;
248  tv.tv_usec = 100 * num;
249  select(0, NULL, NULL, NULL, &tv);
250 }
251 
252 void
253 clock_set_time(clock_time_t time, clock_time_t ftime)
254 {
255  drift = time - node_time();
256 }
257 
258 clock_time_t
260 {
261  return drift + node_time();
262 }
263 /*-----------------------------------------------------------------------------------*/
264 unsigned long
265 clock_seconds(void)
266 {
267  return node_seconds();
268 }
269 /*-----------------------------------------------------------------------------------*/
270 void
271 uip_log(char *m)
272 {
273  uip_ipaddr_t addr;
274 
275  uip_gethostaddr(&addr);
276 
277  printf("uIP log at %d.%d.%d.%d: %s\n", uip_ipaddr_to_quad(&addr), m);
278  fflush(NULL);
279 }
280 void
281 configurator_send_config(uip_ipaddr_t *addr, unsigned long seconds)
282 {
283  printf("Configurator: address %d.%d.%d.%d, seconds %lu\n",
284  uip_ipaddr_to_quad(addr), seconds);
285 }
286 
287 
288 void
289 system_log(char *m)
290 {
291  printf("%s", m);
292 }
293 /*void tr1001_drv_set_slip_dump(int c)
294 {
295 
296 }*/