diff --git a/libs/sutsim/c/sutsim.c b/libs/sutsim/c/sutsim.c index b9e8020..b0f7037 100644 --- a/libs/sutsim/c/sutsim.c +++ b/libs/sutsim/c/sutsim.c @@ -3,13 +3,20 @@ #include #include +/* Forward declarations */ extern void sut_init_hook(void); extern void sut_tick_hook(void); +/* PRIVATE DATA */ static sutsim_tagList_S tag_list = {0}; +/* PRIVATE FUNCTION DECLARATIONS */ +static sutsim_tagEntry_S* sutsim_find_tag(const char* tag); + +/* PUBLIC FUNCTIONS */ void sutsim_init(void) { tag_list.head = NULL; + tag_list.tail = NULL; tag_list.count = 0; tag_list.initialized = true; @@ -21,85 +28,80 @@ void sutsim_tick(void) { sut_tick_hook(); } -int sutsim_add_tag(const char* tag, void* data, uint32_t size, sutsim_dataType_E type) { +bool sutsim_add_tag(const char* tag, void* data, uint32_t size, sutsim_dataType_E type) { + if (sutsim_find_tag(tag)) { + return false; // Tag already exists + } + sutsim_tagEntry_S* new_entry = (sutsim_tagEntry_S*)malloc(sizeof(sutsim_tagEntry_S)); if (!new_entry) { - return -1; // Memory allocation failure + return false; // Memory allocation failure } - new_entry->id = tag_list.count; - new_entry->tag = tag; - new_entry->data = data; // Directly use the user-provided buffer - new_entry->size = size; - new_entry->type = type; + new_entry->id = tag_list.count; + new_entry->tag = tag; + new_entry->data = data; // Directly use the user-provided buffer + new_entry->size = size; + new_entry->type = type; new_entry->subscriber_cb = NULL; - new_entry->next = NULL; + new_entry->next = NULL; if (tag_list.head == NULL) { tag_list.head = new_entry; + tag_list.tail = new_entry; } else { - sutsim_tagEntry_S* current = tag_list.head; - while (current->next != NULL) { - current = current->next; - } - current->next = new_entry; + tag_list.tail->next = new_entry; + tag_list.tail = new_entry; } tag_list.count++; - return new_entry->id; + + return true; } -void sutsim_subscribe_to_tag(const char* tag, sutsim_TagCallback subscriber_cb) { - sutsim_tagEntry_S* current = tag_list.head; - while (current != NULL) { - if (strcmp(tag, current->tag) == 0) { - current->subscriber_cb = subscriber_cb; - return; - } - current = current->next; +bool sutsim_subscribe_to_tag(const char* tag, sutsim_TagCallback subscriber_cb) { + sutsim_tagEntry_S* tagEntry = sutsim_find_tag(tag); + + if (tagEntry) { + tagEntry->subscriber_cb = subscriber_cb; + return true; } + + return false; // Tag not found } bool sutsim_write(const char* tag, const void* data, uint32_t size) { - sutsim_tagEntry_S* current = tag_list.head; - while (current != NULL) { - if (strcmp(tag, current->tag) == 0) { - if (size != current->size) { - return false; // Size mismatch - } + sutsim_tagEntry_S* tagEntry = sutsim_find_tag(tag); - // Update the user-provided tag data buffer - if (current->data) { - memcpy(current->data, data, size); - } + if (tagEntry) { + if (size != tagEntry->size) { + return false; // Size mismatch + } - if (current->subscriber_cb) { - current->subscriber_cb(tag, data, size); - } + memcpy(tagEntry->data, data, size); - return true; + if (tagEntry->subscriber_cb) { + tagEntry->subscriber_cb(tag, data, size); } - current = current->next; + return true; } return false; // Tag not found } bool sutsim_read(const char* tag, void* buffer, uint32_t size) { - sutsim_tagEntry_S* current = tag_list.head; - while (current != NULL) { - if (strcmp(tag, current->tag) == 0) { - if (size != current->size) { - return false; // Size mismatch - } + sutsim_tagEntry_S* tagEntry = sutsim_find_tag(tag); - // Copy the data from the tag's buffer (user-provided) - memcpy(buffer, current->data, size); - return true; + if (tagEntry) { + if (size != tagEntry->size) { + return false; // Size mismatch } - current = current->next; + + memcpy(buffer, tagEntry->data, size); + return true; } + return false; // Tag not found } @@ -112,5 +114,18 @@ void sutsim_cleanup(void) { current = next; } tag_list.head = NULL; + tag_list.tail = NULL; tag_list.count = 0; } + +static sutsim_tagEntry_S* sutsim_find_tag(const char* tag) { + sutsim_tagEntry_S* current = tag_list.head; + while (current != NULL) { + if (strcmp(tag, current->tag) == 0) { + return current; + } + current = current->next; + } + + return NULL; +} diff --git a/libs/sutsim/c/sutsim.h b/libs/sutsim/c/sutsim.h index 483b3b9..3f9ad9c 100644 --- a/libs/sutsim/c/sutsim.h +++ b/libs/sutsim/c/sutsim.h @@ -31,6 +31,7 @@ typedef struct sutsim_tagEntry { typedef struct { bool initialized; sutsim_tagEntry_S* head; + sutsim_tagEntry_S* tail; uint32_t count; } sutsim_tagList_S; @@ -40,10 +41,10 @@ void sutsim_init(void); void sutsim_tick(void); // Add a tag entry (user provides a pointer to the data buffer) -int sutsim_add_tag(const char* tag, void* data, uint32_t size, sutsim_dataType_E type); +bool sutsim_add_tag(const char* tag, void* data, uint32_t size, sutsim_dataType_E type); // Subscribe to a tag (register a callback for tag updates) -void sutsim_subscribe_to_tag(const char* tag, sutsim_TagCallback subscriber_cb); +bool sutsim_subscribe_to_tag(const char* tag, sutsim_TagCallback subscriber_cb); // Write data to a tag bool sutsim_write(const char* tag, const void* data, uint32_t size); diff --git a/sim/core/Simulator.cpp b/sim/core/Simulator.cpp index 24fa773..5aed0e4 100644 --- a/sim/core/Simulator.cpp +++ b/sim/core/Simulator.cpp @@ -32,7 +32,7 @@ void Simulator::init_sut(const std::string& sut_name, const std::string& lib_pat context.sutsim_tick = reinterpret_cast(dlsym(context.lib_handle, "sutsim_tick")); context.sutsim_read = reinterpret_cast(dlsym(context.lib_handle, "sutsim_read")); context.sutsim_write = reinterpret_cast(dlsym(context.lib_handle, "sutsim_write")); - context.sutsim_subscribe_to_tag = reinterpret_cast(dlsym(context.lib_handle, "sutsim_subscribe_to_tag")); + context.sutsim_subscribe_to_tag = reinterpret_cast(dlsym(context.lib_handle, "sutsim_subscribe_to_tag")); if (!context.sutsim_init || !context.sutsim_tick || !context.sutsim_read || !context.sutsim_write || !context.sutsim_subscribe_to_tag) { throw std::runtime_error("Failed to load required symbols from SUT library."); diff --git a/sim/core/SutContext.hpp b/sim/core/SutContext.hpp index 6d7fb3d..46bf49f 100644 --- a/sim/core/SutContext.hpp +++ b/sim/core/SutContext.hpp @@ -14,7 +14,7 @@ class SutContext { void* lib_handle; void (*sutsim_init)(void); void (*sutsim_tick)(void); - void (*sutsim_subscribe_to_tag)(const char*, void*); + bool (*sutsim_subscribe_to_tag)(const char*, void*); bool (*sutsim_write)(const char*, const void*, uint32_t); bool (*sutsim_read)(const char*, void*, uint32_t);