diff --git a/c/core/.gitignore b/c/core/.gitignore new file mode 100644 index 00000000..a65b4177 --- /dev/null +++ b/c/core/.gitignore @@ -0,0 +1 @@ +lib diff --git a/c/core/include/tahu.h b/c/core/include/tahu.h index e5991465..d301b115 100644 --- a/c/core/include/tahu.h +++ b/c/core/include/tahu.h @@ -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. @@ -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. + * + *
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 * diff --git a/c/core/src/tahu.c b/c/core/src/tahu.c index 470ace9c..70bc493b 100644 --- a/c/core/src/tahu.c +++ b/c/core/src/tahu.c @@ -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"); diff --git a/c/core/test/.gitignore b/c/core/test/.gitignore index 745ec46d..0c14a777 100644 --- a/c/core/test/.gitignore +++ b/c/core/test/.gitignore @@ -1,2 +1,4 @@ test_dynamic.dSYM test_static.dSYM +test_dynamic +test_static diff --git a/c/core/test/test.c b/c/core/test/test.c index cf8bad8a..bb48c3fb 100644 --- a/c/core/test/test.c +++ b/c/core/test/test.c @@ -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;