00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00046 #include "net/tapdev-drv.h"
00047 #endif
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
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
00077 static struct uip_fw_netif extif =
00078 {UIP_FW_NETIF(0,0,0,0, 0,0,0,0, tapdev_output)};
00079 #endif
00080 static struct uip_fw_netif meshif =
00081 {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
00082
00083
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
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
00149 process_start(&tapdev_process, NULL);
00150 #endif
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
00174
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;