Hello Elena,
the dynamic dimensions are only possible in UA meta data (array_dimensiosn attribute),
but not and a variant.
A variant value always needs concrete length information, because real memory is allocated for this.
So the array length of a variable may be dynamic, but the variant value is the state of the variable's value at a specific point in time,
where this must be defined.
The next version V1.2.0 will contain some enhancements regarding this topic:
- array dimensions attribute will be supported (this is missing in previous versions)
- new convenience functions for array and matrix handling have been added.
Code: Select all
int ua_variant_set_array(struct ua_variant *v, enum ua_variant_type type, const void *val, size_t num);
int ua_variant_set_matrix(struct ua_variant *v, enum ua_variant_type type, const void *val, size_t num, int32_t *dimensions, int32_t num_dimensions);
int ua_variant_get_array(const struct ua_variant *v, struct ua_variant_array const **array);
int ua_variant_array_get_data(const struct ua_variant_array *array, void const **data, int32_t *length);
int ua_variant_get_matrix(const struct ua_variant *v, struct ua_variant_matrix const **matrix);
int ua_variant_matrix_get_data(const struct ua_variant_matrix *matrix, void const **data, int32_t *length);
int ua_variant_matrix_get_dimensions(const struct ua_variant_matrix *matrix, int32_t const **dimensions, int32_t *num_dimensions);
Example code for filling a variant array/matrix:
Code: Select all
void demo_array(void)
{
uint32_t data[5] = { 1, 2, 3, 4, 5 };
struct ua_variant value;
/* Available already in V1.1.0 */
ua_variant_set_array(&value, UA_VT_UINT32, data, countof(data));
}
void demo_matrix(void)
{
uint32_t data[3][5] = {
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
};
struct ua_variant value;
int32_t num_dimensions = 2;
int32_t dimensions[2] = { 3, 5 };
size_t num_elements;
int i;
/* calculate number of elements in data */
for (num_elements = 1, i = 0; i < num_dimensions; ++i) {
num_elements *= dimensions[i];
}
/* Available already in V1.2.0 */
ua_variant_set_matrix(&value, UA_VT_UINT32, data, num_elements, dimensions, num_dimensions);
}