]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/tossim/sim_tossim.c
Incorporate new modular tos-bsl.
[tinyos-2.x.git] / tos / lib / tossim / sim_tossim.c
index f56fa76127af8002b00771f2fa77064adb528128..d0d4d1f407e61a98246003c1464c70a985f17298 100644 (file)
@@ -38,6 +38,8 @@
 #include <stdlib.h>
 #include <sys/time.h>
 
+#include <sim_noise.h> //added by HyungJune Lee
+
 static sim_time_t sim_ticks;
 static unsigned long current_node;
 static int sim_seed;
@@ -48,11 +50,22 @@ void sim_init() __attribute__ ((C, spontaneous)) {
   sim_queue_init();
   sim_log_init();
   sim_log_commit_change();
+  sim_noise_init(); //added by HyungJune Lee
 
   {
     struct timeval tv;
     gettimeofday(&tv, NULL);
-    sim_seed = tv.tv_usec;
+    // Need to make sure we don't pass zero to seed simulation.
+    // But in case some weird timing factor causes usec to always
+    // be zero, default to tv_sec. Note that the explicit
+    // seeding call also has a check for zero. Thanks to Konrad
+    // Iwanicki for finding this. -pal
+    if (tv.tv_usec != 0) {
+      sim_random_seed(tv.tv_usec);
+    }
+    else {
+      sim_random_seed(tv.tv_sec);
+    }
   } 
 }
 
@@ -79,6 +92,10 @@ int sim_random() __attribute__ ((C, spontaneous)) {
 }
 
 void sim_random_seed(int seed) __attribute__ ((C, spontaneous)) {
+  // A seed of zero wedges on zero, so use 1 instead.
+  if (seed == 0) {
+    seed = 1;
+  }
   sim_seed = seed;
 }
 
@@ -108,8 +125,11 @@ bool sim_run_next_event() __attribute__ ((C, spontaneous)) {
     sim_set_time(event->time);
     sim_set_node(event->mote);
 
-    dbg("Tossim", "CORE: popping event for %i at %llu... ", sim_node(), sim_time());
-    if (sim_mote_is_on(event->mote) || event->force) {
+    // Need to test whether function pointers are for statically
+    // allocted events that are zeroed out on reboot
+    dbg("Tossim", "CORE: popping event 0x%p for %i at %llu with handler %p... ", event, sim_node(), sim_time(), event->handle);
+    if ((sim_mote_is_on(event->mote) || event->force) &&
+       event->handle != NULL) {
       result = TRUE;
       dbg_clear("Tossim", " mote is on (or forced event), run it.\n");
       event->handle(event);
@@ -117,7 +137,9 @@ bool sim_run_next_event() __attribute__ ((C, spontaneous)) {
     else {
       dbg_clear("Tossim", "\n");
     }
-    event->cleanup(event);
+    if (event->cleanup != NULL) {
+      event->cleanup(event);
+    }
   }
 
   return result;