X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=blobdiff_plain;f=tos%2Flib%2Ftossim%2Ftossim.c;h=74283ccfe6c923ce03992b9e3f7a050eb03ff0a8;hb=e9bfab607e051bae6afb47b44892ce37541d1b44;hp=f8af15c733f2b7abca2b86477b83b0d61608159a;hpb=1ba974b83d19fc41bf80acd52726f36f7f1df297;p=tinyos-2.x.git diff --git a/tos/lib/tossim/tossim.c b/tos/lib/tossim/tossim.c index f8af15c7..74283ccf 100644 --- a/tos/lib/tossim/tossim.c +++ b/tos/lib/tossim/tossim.c @@ -39,10 +39,12 @@ #include #include #include +#include #include #include #include +#include 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 = ""; - str.type = ""; + str.ptr = (char*)""; + str.type = (char*)""; str.len = strlen(""); 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(); }