lib: add tc_readable_set_data()

This commit is contained in:
jussi 2020-01-17 16:52:02 +02:00
parent d719832f1c
commit 455ccf2610
3 changed files with 26 additions and 3 deletions

View File

@ -15,7 +15,10 @@ extern "C" {
#define TC_ASSIGNABLE (1)
#define TC_READABLE (1 << 1)
#define TC_INTERFACE (1 << 2)
#define TC_READABLE_DYNAMIC (1)
#define TC_READABLE_STATIC (1 << 1)
// Categories for modules.
enum tc_module_category {
TC_CATEGORY_ASSIGNABLE,
@ -41,6 +44,11 @@ enum tc_module_category {
// Maximum argument count for "overloaded" functions
#define TC_MAX_FUNCTION_ARGC 16
union module_data_callback_t {
tc_readable_module_data_t (*readable_data)();
tc_assignable_module_data_t (*assignable_data)();
};
// Tagged union for category specific data
typedef struct {
uint64_t category;
@ -84,6 +92,11 @@ tc_module_t *tc_module_find(enum tc_module_category category, const char *name);
// Try to return all module handles matching 'category'. The return value needs to be freed in addition to tc_module_close()
tc_module_t **tc_module_find_all_from_category(enum tc_module_category category, uint16_t *count);
// Convenience functions
tc_module_category_info_t tc_module_category_info_create(uint64_t mask, uint16_t num_categories, const tc_module_category_data_t *categories);
tc_module_category_data_t tc_module_category_data_create(uint64_t category, union module_data_callback_t u);
// Close the module after successful find
void tc_module_close(tc_module_t *module);

View File

@ -36,8 +36,10 @@ typedef struct tc_readable_node_t_ {
// Master data structure loaded by module loader
typedef struct {
tc_readable_node_t *root_node;
const char *(*sha256_hash)(const tc_readable_node_t*); // Callback to get a unique hash for a node
uint64_t category_mask; // Which types of readable (dynamic or static) are implemented
tc_readable_node_t *root_static_node;
tc_readable_node_t *root_node;
const char *(*sha256_hash)(const tc_readable_node_t*); // Callback to get a unique hash for a node
} tc_readable_module_data_t;
// Utility functions
@ -53,6 +55,9 @@ int8_t tc_readable_node_add_child(tc_readable_node_t *parent, tc_readable_node_t
// Convinience function for creating a new node and adding it to parent
tc_readable_node_t *tc_readable_node_add_new_child(tc_readable_node_t *parent);
// Set node data
void tc_readable_node_set_data(tc_readable_node_t *node, const char *name, const char *unit);
// Create a tc_readable_result from data, data type and validity. Avoids boilerplate in returning values from readable nodes.
tc_readable_result_t tc_readable_result_create(enum tc_data_types type, void *data, bool valid);

View File

@ -50,6 +50,11 @@ tc_readable_node_t *tc_readable_node_add_new_child(tc_readable_node_t *parent) {
return node;
}
void tc_readable_node_set_data(tc_readable_node_t *node, const char *name, const char *unit) {
node->name = name;
node->unit = unit;
}
tc_readable_result_t tc_readable_result_create(enum tc_data_types type, void *data, bool valid) {
tc_readable_result_t res;
res.valid = valid;