33#include <stdlib.h>
44#include <stdio.h>
55
6+ /* Forward declarations */
67extern void sut_init_hook (void );
78extern void sut_tick_hook (void );
89
10+ /* PRIVATE DATA */
911static 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 */
1117void 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
6373bool 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
8993bool 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+ }
0 commit comments