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
00037
00038
00039
00040
00041
00042
00043
00044
00045
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
00125 main_process = 0;
00126
00127
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
00143 }
00144
00145 if(b) {
00146 nodes_base_node_port = port;
00147 }
00148
00149
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
00185
00186
00187
00188
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
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
00213 if(argc > 1 && strcmp(argv[1], "-q") == 0) {
00214 #endif
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
00294
00295
00296