Skip to content

Commit a758c48

Browse files
authored
sutsim C module cleanup (#8)
* wip * cleanup * added comment * sutsim C module cleanup
1 parent 0016f49 commit a758c48

File tree

4 files changed

+67
-51
lines changed

4 files changed

+67
-51
lines changed

libs/sutsim/c/sutsim.c

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
#include <stdlib.h>
44
#include <stdio.h>
55

6+
/* Forward declarations */
67
extern void sut_init_hook(void);
78
extern void sut_tick_hook(void);
89

10+
/* PRIVATE DATA */
911
static sutsim_tagList_S tag_list = {0};
1012

13+
/* PRIVATE FUNCTION DECLARATIONS */
14+
static sutsim_tagEntry_S* sutsim_find_tag(const char* tag);
15+
16+
/* PUBLIC FUNCTIONS */
1117
void sutsim_init(void) {
1218
tag_list.head = NULL;
19+
tag_list.tail = NULL;
1320
tag_list.count = 0;
1421
tag_list.initialized = true;
1522

@@ -21,85 +28,80 @@ void sutsim_tick(void) {
2128
sut_tick_hook();
2229
}
2330

24-
int sutsim_add_tag(const char* tag, void* data, uint32_t size, sutsim_dataType_E type) {
31+
bool sutsim_add_tag(const char* tag, void* data, uint32_t size, sutsim_dataType_E type) {
32+
if (sutsim_find_tag(tag)) {
33+
return false; // Tag already exists
34+
}
35+
2536
sutsim_tagEntry_S* new_entry = (sutsim_tagEntry_S*)malloc(sizeof(sutsim_tagEntry_S));
2637
if (!new_entry) {
27-
return -1; // Memory allocation failure
38+
return false; // Memory allocation failure
2839
}
2940

30-
new_entry->id = tag_list.count;
31-
new_entry->tag = tag;
32-
new_entry->data = data; // Directly use the user-provided buffer
33-
new_entry->size = size;
34-
new_entry->type = type;
41+
new_entry->id = tag_list.count;
42+
new_entry->tag = tag;
43+
new_entry->data = data; // Directly use the user-provided buffer
44+
new_entry->size = size;
45+
new_entry->type = type;
3546
new_entry->subscriber_cb = NULL;
36-
new_entry->next = NULL;
47+
new_entry->next = NULL;
3748

3849
if (tag_list.head == NULL) {
3950
tag_list.head = new_entry;
51+
tag_list.tail = new_entry;
4052
} else {
41-
sutsim_tagEntry_S* current = tag_list.head;
42-
while (current->next != NULL) {
43-
current = current->next;
44-
}
45-
current->next = new_entry;
53+
tag_list.tail->next = new_entry;
54+
tag_list.tail = new_entry;
4655
}
4756

4857
tag_list.count++;
49-
return new_entry->id;
58+
59+
return true;
5060
}
5161

52-
void sutsim_subscribe_to_tag(const char* tag, sutsim_TagCallback subscriber_cb) {
53-
sutsim_tagEntry_S* current = tag_list.head;
54-
while (current != NULL) {
55-
if (strcmp(tag, current->tag) == 0) {
56-
current->subscriber_cb = subscriber_cb;
57-
return;
58-
}
59-
current = current->next;
62+
bool sutsim_subscribe_to_tag(const char* tag, sutsim_TagCallback subscriber_cb) {
63+
sutsim_tagEntry_S* tagEntry = sutsim_find_tag(tag);
64+
65+
if (tagEntry) {
66+
tagEntry->subscriber_cb = subscriber_cb;
67+
return true;
6068
}
69+
70+
return false; // Tag not found
6171
}
6272

6373
bool sutsim_write(const char* tag, const void* data, uint32_t size) {
64-
sutsim_tagEntry_S* current = tag_list.head;
65-
while (current != NULL) {
66-
if (strcmp(tag, current->tag) == 0) {
67-
if (size != current->size) {
68-
return false; // Size mismatch
69-
}
74+
sutsim_tagEntry_S* tagEntry = sutsim_find_tag(tag);
7075

71-
// Update the user-provided tag data buffer
72-
if (current->data) {
73-
memcpy(current->data, data, size);
74-
}
76+
if (tagEntry) {
77+
if (size != tagEntry->size) {
78+
return false; // Size mismatch
79+
}
7580

76-
if (current->subscriber_cb) {
77-
current->subscriber_cb(tag, data, size);
78-
}
81+
memcpy(tagEntry->data, data, size);
7982

80-
return true;
83+
if (tagEntry->subscriber_cb) {
84+
tagEntry->subscriber_cb(tag, data, size);
8185
}
8286

83-
current = current->next;
87+
return true;
8488
}
8589

8690
return false; // Tag not found
8791
}
8892

8993
bool sutsim_read(const char* tag, void* buffer, uint32_t size) {
90-
sutsim_tagEntry_S* current = tag_list.head;
91-
while (current != NULL) {
92-
if (strcmp(tag, current->tag) == 0) {
93-
if (size != current->size) {
94-
return false; // Size mismatch
95-
}
94+
sutsim_tagEntry_S* tagEntry = sutsim_find_tag(tag);
9695

97-
// Copy the data from the tag's buffer (user-provided)
98-
memcpy(buffer, current->data, size);
99-
return true;
96+
if (tagEntry) {
97+
if (size != tagEntry->size) {
98+
return false; // Size mismatch
10099
}
101-
current = current->next;
100+
101+
memcpy(buffer, tagEntry->data, size);
102+
return true;
102103
}
104+
103105
return false; // Tag not found
104106
}
105107

@@ -112,5 +114,18 @@ void sutsim_cleanup(void) {
112114
current = next;
113115
}
114116
tag_list.head = NULL;
117+
tag_list.tail = NULL;
115118
tag_list.count = 0;
116119
}
120+
121+
static sutsim_tagEntry_S* sutsim_find_tag(const char* tag) {
122+
sutsim_tagEntry_S* current = tag_list.head;
123+
while (current != NULL) {
124+
if (strcmp(tag, current->tag) == 0) {
125+
return current;
126+
}
127+
current = current->next;
128+
}
129+
130+
return NULL;
131+
}

libs/sutsim/c/sutsim.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef struct sutsim_tagEntry {
3131
typedef struct {
3232
bool initialized;
3333
sutsim_tagEntry_S* head;
34+
sutsim_tagEntry_S* tail;
3435
uint32_t count;
3536
} sutsim_tagList_S;
3637

@@ -40,10 +41,10 @@ void sutsim_init(void);
4041
void sutsim_tick(void);
4142

4243
// Add a tag entry (user provides a pointer to the data buffer)
43-
int sutsim_add_tag(const char* tag, void* data, uint32_t size, sutsim_dataType_E type);
44+
bool sutsim_add_tag(const char* tag, void* data, uint32_t size, sutsim_dataType_E type);
4445

4546
// Subscribe to a tag (register a callback for tag updates)
46-
void sutsim_subscribe_to_tag(const char* tag, sutsim_TagCallback subscriber_cb);
47+
bool sutsim_subscribe_to_tag(const char* tag, sutsim_TagCallback subscriber_cb);
4748

4849
// Write data to a tag
4950
bool sutsim_write(const char* tag, const void* data, uint32_t size);

sim/core/Simulator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void Simulator::init_sut(const std::string& sut_name, const std::string& lib_pat
3232
context.sutsim_tick = reinterpret_cast<void (*)()>(dlsym(context.lib_handle, "sutsim_tick"));
3333
context.sutsim_read = reinterpret_cast<bool (*)(const char*, void*, uint32_t)>(dlsym(context.lib_handle, "sutsim_read"));
3434
context.sutsim_write = reinterpret_cast<bool (*)(const char*, const void*, uint32_t)>(dlsym(context.lib_handle, "sutsim_write"));
35-
context.sutsim_subscribe_to_tag = reinterpret_cast<void (*)(const char*, void*)>(dlsym(context.lib_handle, "sutsim_subscribe_to_tag"));
35+
context.sutsim_subscribe_to_tag = reinterpret_cast<bool (*)(const char*, void*)>(dlsym(context.lib_handle, "sutsim_subscribe_to_tag"));
3636

3737
if (!context.sutsim_init || !context.sutsim_tick || !context.sutsim_read || !context.sutsim_write || !context.sutsim_subscribe_to_tag) {
3838
throw std::runtime_error("Failed to load required symbols from SUT library.");

sim/core/SutContext.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class SutContext {
1414
void* lib_handle;
1515
void (*sutsim_init)(void);
1616
void (*sutsim_tick)(void);
17-
void (*sutsim_subscribe_to_tag)(const char*, void*);
17+
bool (*sutsim_subscribe_to_tag)(const char*, void*);
1818
bool (*sutsim_write)(const char*, const void*, uint32_t);
1919
bool (*sutsim_read)(const char*, void*, uint32_t);
2020

0 commit comments

Comments
 (0)