contiki-main.c

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: contiki-main.c,v 1.37 2010/04/21 20:27:28 oliverschmidt Exp $
00034  */
00035 
00036 #include "contiki.h"
00037 #include "contiki-net.h"
00038 #include "contiki-lib.h"
00039 
00040 #include "dev/serial-line.h"
00041 #include "net/rime.h"
00042 
00043 #ifdef __CYGWIN__
00044 #include "net/wpcap-drv.h"
00045 #else /* __CYGWIN__ */
00046 #include "net/tapdev-drv.h"
00047 #endif /* __CYGWIN__ */
00048 #include "net/ethernode-uip.h"
00049 #include "net/ethernode-rime.h"
00050 #include "net/ethernode.h"
00051 #include "net/uip-over-mesh.h"
00052 
00053 #include "net/mac/nullmac.h"
00054 #include "net/mac/lpp.h"
00055 
00056 #include "ether.h"
00057 
00058 #include <stdio.h>
00059 #ifndef HAVE_SNPRINTF
00060 int snprintf(char *str, size_t size, const char *format, ...);
00061 #endif /* HAVE_SNPRINTF */
00062 
00063 #include <stdlib.h>
00064 #include <sys/select.h>
00065 #include <unistd.h>
00066 
00067 #include "dev/button-sensor.h"
00068 #include "dev/pir-sensor.h"
00069 #include "dev/vib-sensor.h"
00070 #include "dev/radio-sensor.h"
00071 #include "dev/leds.h"
00072 
00073 #ifdef __CYGWIN__
00074 static struct uip_fw_netif extif =
00075   {UIP_FW_NETIF(0,0,0,0, 0,0,0,0, wpcap_output)};
00076 #else /* __CYGWIN__ */
00077 static struct uip_fw_netif extif =
00078   {UIP_FW_NETIF(0,0,0,0, 0,0,0,0, tapdev_output)};
00079 #endif /* __CYGWIN__ */
00080 static struct uip_fw_netif meshif =
00081   {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
00082 /*static struct uip_fw_netif ethernodeif =
00083   {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, ethernode_drv_send)};*/
00084 
00085 static const struct uip_eth_addr ethaddr = {{0x00,0x06,0x98,0x01,0x02,0x12}};
00086 
00087 SENSORS(&button_sensor, &pir_sensor, &vib_sensor, &radio_sensor);
00088 
00089 PROCINIT(&sensors_process, &etimer_process, &tcpip_process);
00090 
00091 /*---------------------------------------------------------------------------*/
00092 #ifdef __CYGWIN__
00093 static void
00094 remove_route(int s)
00095 {
00096   char buf[1024];
00097 
00098   snprintf(buf, sizeof(buf), "route delete %d.%d.%d.%d",
00099                              uip_ipaddr_to_quad(&meshif.ipaddr));
00100   printf("%s\n", buf);
00101   system(buf);
00102   _exit(0);
00103 }
00104 #endif /* __CYGWIN__ */
00105 /*---------------------------------------------------------------------------*/
00106 void
00107 contiki_main(int flag)
00108 {
00109   random_init(getpid());
00110   srand(getpid());
00111 
00112   leds_init();
00113   
00114   process_init();
00115 
00116   procinit_init();
00117 
00118   serial_line_init();
00119   
00120   uip_init();
00121  
00122   ctimer_init();
00123 
00124   NETSTACK_MAC.init();
00125   NETSTACK_RDC.init();
00126 
00127   uip_over_mesh_init(2);
00128   uip_over_mesh_set_net(&meshif.ipaddr, &meshif.netmask);
00129       
00130   if(flag == 1) {
00131 #ifdef __CYGWIN__
00132     process_start(&wpcap_process, NULL);
00133     {
00134       char buf[1024];
00135       uip_ipaddr_t ifaddr;
00136       extern uip_ipaddr_t winifaddr;
00137       
00138       uip_ipaddr_copy(&ifaddr, &winifaddr);
00139       
00140       snprintf(buf, sizeof(buf), "route add %d.%d.%d.%d mask %d.%d.%d.%d %d.%d.%d.%d",
00141                uip_ipaddr_to_quad(&meshif.ipaddr),
00142                uip_ipaddr_to_quad(&meshif.netmask),
00143                uip_ipaddr_to_quad(&ifaddr));
00144       printf("%s\n", buf);
00145       system(buf);
00146       signal(SIGTERM, remove_route);
00147     }
00148 #else /* __CYGWIN__ */
00149     process_start(&tapdev_process, NULL);
00150 #endif /* __CYGWIN__ */
00151     process_start(&uip_fw_process, NULL);
00152   
00153     uip_fw_register(&meshif);
00154     uip_fw_default(&extif);
00155     printf("uip_hostaddr %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr));
00156     uip_over_mesh_make_announced_gateway();
00157   } else {
00158     process_start(&uip_fw_process, NULL);
00159     uip_fw_default(&meshif);
00160   }
00161   
00162   leds_on(LEDS_GREEN);
00163 
00164   rtimer_init();
00165   
00166   autostart_start(autostart_processes);
00167   
00168   while(1) {
00169     int n;
00170     struct timeval tv;
00171 
00172     n = process_run();
00173     /*    if(n > 0) {
00174       printf("%d processes in queue\n", n);
00175       }*/
00176     tv.tv_sec = 0;
00177     tv.tv_usec = 1;
00178     select(0, NULL, NULL, NULL, &tv);
00179     etimer_request_poll();
00180   }
00181 }
00182 /*---------------------------------------------------------------------------*/
00183 process_event_t codeprop_event_quit;

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