#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;
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;
Mote::Mote(nesc_app_t* n) {
app = n;
+ varTable = create_hashtable(128, tossim_hash, tossim_hash_eq);
}
+
Mote::~Mote(){}
unsigned long Mote::id() {
Variable* Mote::getVariable(char* name) {
char* typeStr = "";
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) {
return sim_remove_channel(channel, file);
}
+void Tossim::randomSeed(int seed) {
+ return sim_random_seed(seed);
+}
+
bool Tossim::runNextEvent() {
return sim_run_next_event();
}