Skip to content
Open
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
1 change: 1 addition & 0 deletions c/core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
27 changes: 27 additions & 0 deletions c/core/include/tahu.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ extern "C" {
#define PROPERTY_DATA_TYPE_STRING 12
#define PROPERTY_DATA_TYPE_DATETIME 13
#define PROPERTY_DATA_TYPE_TEXT 14
#define PROPERTY_DATA_TYPE_PROPERTYSET 20

/**
* Attach Metadata to an existing Metric.
Expand Down Expand Up @@ -181,6 +182,32 @@ int add_property_to_set(org_eclipse_tahu_protobuf_Payload_PropertySet *propertys
const void *value,
size_t size_of_value);

/**
* Add a PropertySet as property to an existing PropertySet.
*
* <p>Caution: The propertyset structure is duplicated via shallow
* copy, and it is expected that any pointers within it are safe
* to pass to free(). This will happen if pb_release() is called
* on this structure or any structure referencing it, for
* example via a call to free_payload().
*
* @param existing_propertyset
* The propertyset into which to add the new property with has the value
* of the requested new propertyset
*
* @param key Pointer to null-terminated string giving name of new property
*
* @param new_propertyset
* The new propertyset to add
*
* @return Returns
* >= 0 on success, or negative on failure
*/
int add_propertyset_to_set(
org_eclipse_tahu_protobuf_Payload_PropertySet *existing_propertyset,
const char *key,
const org_eclipse_tahu_protobuf_Payload_PropertySet *new_propertyset);

/**
* Add a PropertySet to an existing Metric
*
Expand Down
32 changes: 32 additions & 0 deletions c/core/src/tahu.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,38 @@ int add_property_to_set(org_eclipse_tahu_protobuf_Payload_PropertySet *propertys
return 0;
}

int add_propertyset_to_set(org_eclipse_tahu_protobuf_Payload_PropertySet *existing_propertyset,
const char *key,
const org_eclipse_tahu_protobuf_Payload_PropertySet *new_propertyset) {
DEBUG_PRINT("Add propertyset to set...\n");
const int old_count = existing_propertyset->keys_count;
const int new_count = (old_count + 1);
const size_t key_allocation_size = sizeof(char *) * new_count;
const size_t value_allocation_size = sizeof(org_eclipse_tahu_protobuf_Payload_PropertyValue) * new_count;
void *key_allocation_result = realloc(existing_propertyset->keys, key_allocation_size);
void *value_allocation_result = realloc(existing_propertyset->values, value_allocation_size);
if ((key_allocation_result == NULL) || (value_allocation_result == NULL)) {
fprintf(stderr, "realloc failed in add_metric_to_payload\n");
return -1;
}
existing_propertyset->keys = (char**)key_allocation_result;
existing_propertyset->keys_count = new_count;
existing_propertyset->values = (org_eclipse_tahu_protobuf_Payload_PropertyValue *)value_allocation_result;
existing_propertyset->values_count = new_count;
existing_propertyset->keys[old_count] = strdup(key);
if (existing_propertyset->keys[old_count] == NULL) {
fprintf(stderr, "strdup failed in add_metric_to_payload\n");
return -1;
}
org_eclipse_tahu_protobuf_Payload_PropertyValue *p_new_property_value = &existing_propertyset->values[old_count];
memset(p_new_property_value, 0, sizeof(org_eclipse_tahu_protobuf_Payload_PropertyValue));
p_new_property_value->has_type = true;
p_new_property_value->type = PROPERTY_DATA_TYPE_PROPERTYSET;
p_new_property_value->which_value = org_eclipse_tahu_protobuf_Payload_PropertyValue_propertyset_value_tag;
memcpy(&p_new_property_value->value.propertyset_value, new_propertyset, sizeof(org_eclipse_tahu_protobuf_Payload_PropertySet));
return 0;
}

int add_propertyset_to_metric(org_eclipse_tahu_protobuf_Payload_Metric *metric,
org_eclipse_tahu_protobuf_Payload_PropertySet *properties) {
DEBUG_PRINT("Add propertyset to metric...\n");
Expand Down
2 changes: 2 additions & 0 deletions c/core/test/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
test_dynamic.dSYM
test_static.dSYM
test_dynamic
test_static
14 changes: 14 additions & 0 deletions c/core/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,20 @@ void publish_node_birth(struct mosquitto *mosq) {
add_propertyset_to_metric(&prop_metric, &properties);
add_metric_to_payload(&nbirth_payload, &prop_metric);


// Add a metric with a PropertySet where a property value is a PropertySet
fprintf(stdout, "Adding metric: 'Node Metric3'\n");
org_eclipse_tahu_protobuf_Payload_PropertySet inner_propertyset = org_eclipse_tahu_protobuf_Payload_PropertySet_init_default;
uint32_t nbirth_metric_three_inner_propset_value_one = 42;
uint32_t nbirth_metric_three_inner_propset_value_two = 4242;
add_property_to_set(&inner_propertyset, "key1", METRIC_DATA_TYPE_INT32, &nbirth_metric_three_inner_propset_value_one, sizeof(&nbirth_metric_three_inner_propset_value_one));
add_property_to_set(&inner_propertyset, "key2", METRIC_DATA_TYPE_INT32, &nbirth_metric_three_inner_propset_value_two, sizeof(&nbirth_metric_three_inner_propset_value_two));
org_eclipse_tahu_protobuf_Payload_PropertySet outer_propertyset = org_eclipse_tahu_protobuf_Payload_PropertySet_init_default;
add_propertyset_to_set(&outer_propertyset, "myset", &inner_propertyset);
org_eclipse_tahu_protobuf_Payload_Metric prop_metric3 = org_eclipse_tahu_protobuf_Payload_Metric_init_default;
add_propertyset_to_metric(&prop_metric3, &outer_propertyset);
add_metric_to_payload(&nbirth_payload, &prop_metric3);

// Create a metric called RPMs which is a member of the UDT definition - note aliases do not apply to UDT members
org_eclipse_tahu_protobuf_Payload_Metric rpms_metric = org_eclipse_tahu_protobuf_Payload_Metric_init_default;
uint32_t rpms_value = 0;
Expand Down