main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: main.c,v 1.13 2008/11/09 12:30:32 adamdunkels Exp $
00034  */
00035 
00036 /**
00037  * \file
00038  * This file implements the main function of the Contiki distributed
00039  * sensor network simulation environment.
00040  * \author Adam Dunkels <adam@sics.se>
00041  *
00042  * When starting, each sensor node is created as its own process. The
00043  * sensor node processes communicates with the starting process using
00044  * named pipes. These pipes carry messages such as data packets and
00045  * configuration and statistics information requests.
00046  */
00047 #include "contiki-net.h"
00048 #include "display.h"
00049 #include "contiki-main.h"
00050 #include "nodes.h"
00051 #include "ether.h"
00052 #include "node.h"
00053 
00054 #include "net/ethernode.h"
00055 
00056 #include <sys/types.h>
00057 #include <unistd.h>
00058 #include <signal.h>
00059 #include <stdlib.h>
00060 #include <stdio.h>
00061 #include <sys/wait.h>
00062 #include <arpa/inet.h>
00063 
00064 in_addr_t gwaddr, winifaddr;
00065 
00066 void netsim_init(void);
00067 
00068 static int main_process = 0;
00069 
00070 /*---------------------------------------------------------------------------*/
00071 static void
00072 sigchld_handler(int sig)
00073 {
00074   int status;
00075   pid_t pid;
00076   struct nodes_node *node;
00077   if(!main_process) {
00078     return;
00079   }
00080   
00081   pid = waitpid(-1, &status, WNOHANG);
00082   
00083   if(WIFSIGNALED(status) &&
00084      WTERMSIG(status) == SIGSEGV) {
00085     node = nodes_find_pid(pid);
00086     if(node == NULL) {
00087       printf("A Contiki node crashed, but it wasn't even started by the system. Something weird is going on!\n");
00088     } else {
00089       printf("Contiki node at (%d, %d) crashed - Segmentation fault\n",
00090              node->x, node->y);
00091     }
00092   }
00093 }
00094 /*---------------------------------------------------------------------------*/
00095 static void
00096 idle(void)
00097 {
00098   int events;
00099 
00100   do {
00101     ether_server_poll();
00102     display_tick();
00103     display_redraw();
00104     ether_tick();
00105     events = process_run();
00106     if(events > 0) {
00107       printf("events %d\n", events);
00108     }
00109   } while(events > 0);
00110 
00111 }
00112 /*---------------------------------------------------------------------------*/
00113 static int
00114 start_node(int x, int y, int b)
00115 {
00116   pid_t pid;
00117   struct timeval tv;
00118   static unsigned short port = NODES_PORTBASE;
00119   
00120   pid = fork();
00121   
00122   if(pid == 0) {
00123 
00124     /* This is the sensor process. */
00125     main_process = 0;
00126 
00127     /* Make standard output unbuffered. */
00128     setvbuf(stdout, (char *)NULL, _IONBF, 0);
00129   
00130     
00131     srand(getpid());
00132 
00133     tv.tv_sec = 0;
00134     tv.tv_usec = 1000 * (rand() % 1000);
00135     select(0, NULL, NULL, NULL, &tv);
00136     
00137     node_init(port - NODES_PORTBASE + 2, x, y, b);
00138     ethernode_init(port);
00139 
00140     contiki_main(b);
00141     
00142     /* NOTREACHED */
00143   }
00144 
00145   if(b) {
00146     nodes_base_node_port = port;
00147   }
00148   
00149   /*  printf("Adding sensor %d at (%d,%d)\n", pid, x, y);*/
00150   main_process = 1;
00151   nodes_add(pid, x, y, port, port - NODES_PORTBASE + 2);
00152   
00153 
00154   ++port;
00155   return port - NODES_PORTBASE + 1;
00156 }
00157 /*---------------------------------------------------------------------------*/
00158 int
00159 main_add_node(int x, int y)
00160 {
00161   return start_node(x, y, 0);
00162 }
00163 /*---------------------------------------------------------------------------*/
00164 void
00165 main_add_base(int x, int y)
00166 {
00167   start_node(x, y, 1);
00168 }
00169 /*---------------------------------------------------------------------------*/
00170 int
00171 main(int argc, char **argv)
00172 {
00173 #ifdef __CYGWIN__
00174   if(argc == 3 &&
00175      inet_addr(argv[1]) == INADDR_NONE &&
00176      inet_addr(argv[2]) == INADDR_NONE) {
00177     printf("usage: %s <ip addr of ethernet card to share> "
00178            "<ip addr of netsim gateway>\n", argv[0]);
00179     exit(1);
00180   } else if(argc >= 2) {
00181     gwaddr = inet_addr(argv[2]);
00182     winifaddr = inet_addr(argv[1]);
00183   }
00184 #endif /* __CYGWIN__ */
00185 
00186   /*  system("ifconfig tap0 inet 192.168.250.1");*/
00187   /*  system("route delete 172.16.0.0/16");
00188       system("route add 172.16.0.0/16 192.168.250.2");*/
00189 
00190   nodes_init();
00191   
00192   atexit(nodes_kill);
00193   atexit(ether_print_stats);
00194 
00195   netsim_init();
00196   
00197   ether_server_init();
00198 
00199 #if 0
00200   while(1) {
00201     ether_server_poll();
00202     ether_tick();
00203     process_run();
00204     usleep(100);
00205   }
00206 #endif /* 0 */
00207 
00208 #ifdef __CYGWIN__
00209   if(argc > 1 && (strcmp(argv[1], "-q") ||
00210                   strcmp(argv[2], "-q") ||
00211                   strcmp(argv[3], "-q")) == 0) {
00212 #else /* __CYGWIN__ */
00213   if(argc > 1 && strcmp(argv[1], "-q") == 0) {
00214 #endif /* __CYGWIN__ */
00215     display_init(idle, 50, 0);
00216   } else {
00217     display_init(idle, 50, 1);
00218   }
00219   display_redraw();
00220 
00221   signal(SIGCHLD, sigchld_handler);
00222   
00223   display_run();
00224   
00225   return 0;
00226 
00227   argv = argv;
00228   argc = argc;
00229 }
00230 /*-----------------------------------------------------------------------------------*/
00231 char *arg_alloc(char size) {return NULL;}
00232 void  arg_init(void) {}
00233 void  arg_free(char *arg) {}
00234 /*-----------------------------------------------------------------------------------*/
00235 
00236 char *shell_prompt_text = "sensor-router> ";
00237 
00238 /*-----------------------------------------------------------------------------------*/
00239 #include <sys/time.h>
00240 
00241 static signed long drift = 0;
00242 
00243 void
00244 clock_delay(unsigned int num)
00245 {
00246   struct timeval tv;
00247   tv.tv_sec = 0;
00248   tv.tv_usec = 100 * num;
00249   select(0, NULL, NULL, NULL, &tv);
00250 }
00251 
00252 void
00253 clock_set_time(clock_time_t time, clock_time_t ftime)
00254 {
00255   drift = time - node_time();
00256 }
00257 
00258 clock_time_t
00259 clock_time(void)
00260 {
00261   return drift + node_time();
00262 }
00263 /*-----------------------------------------------------------------------------------*/
00264 unsigned long
00265 clock_seconds(void)
00266 {
00267   return node_seconds();
00268 }
00269 /*-----------------------------------------------------------------------------------*/
00270 void
00271 uip_log(char *m)
00272 {
00273   uip_ipaddr_t addr;
00274 
00275   uip_gethostaddr(&addr);
00276 
00277   printf("uIP log at %d.%d.%d.%d: %s\n", uip_ipaddr_to_quad(&addr), m);
00278   fflush(NULL);
00279 }
00280 void
00281 configurator_send_config(uip_ipaddr_t *addr, unsigned long seconds)
00282 {
00283   printf("Configurator: address %d.%d.%d.%d, seconds %lu\n",
00284          uip_ipaddr_to_quad(addr), seconds);
00285 }
00286 
00287 
00288 void
00289 system_log(char *m)
00290 {
00291   printf("%s", m);
00292 }
00293 /*void tr1001_drv_set_slip_dump(int c)
00294 {
00295 
00296 }*/

Generated on Mon Apr 11 14:23:44 2011 for Contiki 2.5 by  doxygen 1.6.1