]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/tossim/CpmModelC.nc
Acks should still work if a node sets its AM address.
[tinyos-2.x.git] / tos / lib / tossim / CpmModelC.nc
index 5e6b61e1cceeab1f11a4a5e6e5cb0385addf7c9a..f28895efb64df5e406e8aabaef6d9fb3eea03aaf 100644 (file)
 /**
  *
  * CPM (closest-pattern matching) is a wireless noise simulation model
- * based on statistical extraction from empirical noise data.
- * This model provides far more precise
- * software simulation environment by exploiting time-correlated noise
- * characteristic and shadowing effect as well as path-loss model. For
- * details, please refer to the paper
+ * based on statistical extraction from empirical noise data.  This
+ * model provides far more precise software simulation environment by
+ * exploiting time-correlated noise characteristics. For details,
+ * please refer to the paper
  *
  * "Improving Wireless Simulation through Noise Modeling." HyungJune
  * Lee and Philip Levis, IPSN 2007. You can find a copy at
@@ -60,6 +59,7 @@ implementation {
     sim_time_t end;
     double power;
     double reversePower;
+    int8_t strength;
     bool lost;
     bool ack;
     message_t* msg;
@@ -127,7 +127,7 @@ implementation {
     else {
       noise_val = (double)sim_noise_generate(node_id, quotient);
     }
-    dbg("CpmModelC", "OUT: noise_hash_generation()\n");
+    dbg("CpmModelC,Tal", "%s: OUT: noise_hash_generation(): %lf\n", sim_time_string(), noise_val);
 
     return noise_val;
   }
@@ -200,8 +200,8 @@ implementation {
   }
   
   command bool Model.clearChannel() {
-    dbg("CpmModelC", "Checking clear channel @ %s: %f <= %f \n", sim_time_string(), (double)noise_hash_generation(), clearThreshold);
-    return noise_hash_generation() < clearThreshold;
+    dbg("CpmModelC", "Checking clear channel @ %s: %f <= %f \n", sim_time_string(), (double)packetNoise(NULL), clearThreshold);
+    return packetNoise(NULL) < clearThreshold;
   }
 
   void sim_gain_schedule_ack(int source, sim_time_t t, receive_message_t* r) {
@@ -309,6 +309,12 @@ implementation {
       mine->lost = 1;
     }
     if (!mine->lost) {
+      // Copy this receiver's packet signal strength to the metadata region
+      // of the packet. Note that this packet is actually shared across all
+      // receivers: a higher layer performs the copy.
+      tossim_metadata_t* meta = (tossim_metadata_t*)(&mine->msg->metadata);
+      meta->strength = mine->strength;
+      
       dbg_clear("CpmModelC,SNRLoss", "  -signaling reception\n");
       signal Model.receive(mine->msg);
       if (mine->ack) {
@@ -329,6 +335,19 @@ implementation {
       receiving = 0;
     } // If the packet was lost, then we're searching for new packets again
     else {
+      if (RandomUniform() < 0.001) {
+       dbg("CpmModelC,SNRLoss", "Packet was technically lost, but TOSSIM introduces an ack false positive rate.\n");
+       if (mine->ack && signal Model.shouldAck(mine->msg)) {
+         dbg_clear("CpmModelC", " scheduling ack.\n");
+         sim_gain_schedule_ack(mine->source, sim_time() + 1, mine);
+       }
+       else { // Otherwise free the receive_message_t*
+         free_receive_message(mine);
+       }
+      }
+      else {
+       free_receive_message(mine);
+      }
       receiving = 0;
       dbg_clear("CpmModelC,SNRLoss", "  -packet was lost.\n");
     }
@@ -346,10 +365,14 @@ implementation {
     rcv->end = endTime;
     rcv->power = power;
     rcv->reversePower = reversePower;
+    // The strength of a packet is the sum of the signal and noise. In most cases, this means
+    // the signal. By sampling this here, it assumes that the packet RSSI is sampled at
+    // the beginning of the packet. This is true for the CC2420, but is not true for all
+    // radios. But generalizing seems like complexity for minimal gain at this point.
+    rcv->strength = (int8_t)(floor(10.0 * log(pow(10.0, power/10.0) + pow(10.0, noiseStr/10.0)) / log(10.0)));
     rcv->msg = msg;
     rcv->lost = 0;
     rcv->ack = receive;
-    
     // If I'm off, I never receive the packet, but I need to keep track of
     // it in case I turn on and someone else starts sending me a weaker
     // packet. So I don't set receiving to 1, but I keep track of
@@ -403,7 +426,7 @@ implementation {
 
     while (neighborEntry != NULL) {
       int other = neighborEntry->mote;
-      sim_gain_put(other, msg, endTime, ack && (other == dest), power + sim_gain_value(sim_node(), other), reversePower + sim_gain_value(other, sim_node()));
+      sim_gain_put(other, msg, endTime, ack, power + sim_gain_value(sim_node(), other), reversePower + sim_gain_value(other, sim_node()));
       neighborEntry = sim_gain_next(neighborEntry);
     }
   }