Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #8

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 62 additions & 47 deletions libs/sutsim/c/sutsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
#include <stdlib.h>
#include <stdio.h>

/* 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;

Expand All @@ -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
}

Expand All @@ -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;
}
5 changes: 3 additions & 2 deletions libs/sutsim/c/sutsim.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sim/core/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void Simulator::init_sut(const std::string& sut_name, const std::string& lib_pat
context.sutsim_tick = reinterpret_cast<void (*)()>(dlsym(context.lib_handle, "sutsim_tick"));
context.sutsim_read = reinterpret_cast<bool (*)(const char*, void*, uint32_t)>(dlsym(context.lib_handle, "sutsim_read"));
context.sutsim_write = reinterpret_cast<bool (*)(const char*, const void*, uint32_t)>(dlsym(context.lib_handle, "sutsim_write"));
context.sutsim_subscribe_to_tag = reinterpret_cast<void (*)(const char*, void*)>(dlsym(context.lib_handle, "sutsim_subscribe_to_tag"));
context.sutsim_subscribe_to_tag = reinterpret_cast<bool (*)(const char*, void*)>(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.");
Expand Down
2 changes: 1 addition & 1 deletion sim/core/SutContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading