]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/tossim/tossim.c
Merge TinyOS 2.1.1 into master.
[tinyos-2.x.git] / tos / lib / tossim / tossim.c
index f8af15c733f2b7abca2b86477b83b0d61608159a..74283ccfe6c923ce03992b9e3f7a050eb03ff0a8 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <hashtable.h>
 
 #include <mac.c>
 #include <radio.c>
 #include <packet.c>
+#include <sim_noise.h>
 
 uint16_t TOS_NODE_ID = 1;
 
@@ -74,12 +76,33 @@ Variable::Variable(char* str, char* formatStr, int array, int which) {
     data = NULL;
     ptr = NULL;
   }
+  printf("Allocated variable %s\n", realName);
 }
 
 Variable::~Variable() {
+  printf("Freeing variable %s\n", realName);
   free(data);
+  free(realName);
 }
 
+/* This is the sdbm algorithm, taken from
+   http://www.cs.yorku.ca/~oz/hash.html -pal */
+static unsigned int tossim_hash(void* key) {
+  char* str = (char*)key;
+  unsigned int hashVal = 0;
+  int c;
+  
+  while ((c = *str++))
+    hashVal = c + (hashVal << 6) + (hashVal << 16) - hashVal;
+  
+  return hashVal;
+}
+
+static int tossim_hash_eq(void* key1, void* key2) {
+  return strcmp((char*)key1, (char*)key2) == 0;
+}
+
+
 variable_string_t Variable::getData() {
   if (data != NULL && ptr != NULL) {
     str.ptr = data;
@@ -90,8 +113,8 @@ variable_string_t Variable::getData() {
     memcpy(data, ptr, len);
   }
   else {
-    str.ptr = "<no such variable>";
-    str.type = "<no such variable>";
+    str.ptr = (char*)"<no such variable>";
+    str.type = (char*)"<no such variable>";
     str.len = strlen("<no such variable>");
     str.isArray = 0;
   }
@@ -100,7 +123,9 @@ variable_string_t Variable::getData() {
 
 Mote::Mote(nesc_app_t* n) {
   app = n;
+  varTable = create_hashtable(128, tossim_hash, tossim_hash_eq);
 }
+
 Mote::~Mote(){}
 
 unsigned long Mote::id() {
@@ -141,22 +166,41 @@ void Mote::setID(unsigned long val) {
 }
 
 Variable* Mote::getVariable(char* name) {
-  char* typeStr = "";
+  char* typeStr = (char*)"";
   int isArray;
-  // Could hash this for greater efficiency,
-  // but that would either require transformation
-  // in Tossim class or a more complex typemap.
-  if (app != NULL) {
-    for (int i = 0; i < app->numVariables; i++) {
-      if(strcmp(name, app->variableNames[i]) == 0) {
-       typeStr = app->variableTypes[i];
-       isArray = app->variableArray[i];
-       break;
+  Variable* var;
+  
+  var = (Variable*)hashtable_search(varTable, name);
+  if (var == NULL) {
+    // Could hash this for greater efficiency,
+    // but that would either require transformation
+    // in Tossim class or a more complex typemap.
+    if (app != NULL) {
+      for (int i = 0; i < app->numVariables; i++) {
+       if(strcmp(name, app->variableNames[i]) == 0) {
+         typeStr = app->variableTypes[i];
+         isArray = app->variableArray[i];
+         break;
+       }
       }
     }
+    //  printf("Getting variable %s of type %s %s\n", name, typeStr, isArray? "[]" : "");
+    var = new Variable(name, typeStr, isArray, nodeID);
+    hashtable_insert(varTable, name, var);
   }
-  //  printf("Getting variable %s of type %s %s\n", name, typeStr, isArray? "[]" : "");
-  return new Variable(name, typeStr, isArray, nodeID);
+  return var;
+}
+
+void Mote::addNoiseTraceReading(int val) {
+  sim_noise_trace_add(id(), (char)val);
+}
+
+void Mote::createNoiseModel() {
+  sim_noise_create_model(id());
+}
+
+int Mote::generateNoise(int when) {
+  return (int)sim_noise_generate(id(), when);
 }
 
 Tossim::Tossim(nesc_app_t* n) {
@@ -226,6 +270,10 @@ bool Tossim::removeChannel(char* channel, FILE* file) {
   return sim_remove_channel(channel, file);
 }
 
+void Tossim::randomSeed(int seed) {
+  return sim_random_seed(seed);
+}
+
 bool Tossim::runNextEvent() {
   return sim_run_next_event();
 }