Skip to content
Snippets Groups Projects
Commit 16f23703 authored by clohr's avatar clohr
Browse files

sépare attributes et parameters

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/C/branches/version-0.7@2298 b32b6428-25c9-4566-ad07-03861ab6144f
parent b309ffdc
No related branches found
No related tags found
No related merge requests found
......@@ -29,12 +29,8 @@
#include "db-schema.h"
/*
* Section Attributes
*/
/* validate the name of an attribute */
bool validate_attribute(const char *attribute) {
/* validate the name of an identifier */
bool validate_identifier(const char *identifier) {
static regex_t *preg = NULL;
int r;
......@@ -47,13 +43,20 @@ bool validate_attribute(const char *attribute) {
return false;
}
}
return regexec(preg, attribute, 0, NULL, 0) != REG_NOMATCH;
return regexec(preg, identifier, 0, NULL, 0) != REG_NOMATCH;
}
/* select an attribute by its name */
attribute_t *select_attribute(attributes_t *attributes, const char *name) {
attribute_t *np;
/*
* Section Parameters
* used for Attributes Methodes Notifications
*/
/* select a parameter by its name */
parameter_t *select_parameter(parameters_t *parameters, const char *name) {
parameter_t *np;
TAILQ_FOREACH(np, attributes, entries)
if (strcmp(name, np->name) == 0)
return np;
......@@ -61,13 +64,79 @@ attribute_t *select_attribute(attributes_t *attributes, const char *name) {
}
/* insert a parameter */
parameter_t *insert_parameter(parameters_t *parameters, const char *name, const char *type) {
parameter_t *parameter = (parameter_t *)malloc(sizeof(parameter_t));
parameter->name = strdup(name);
parameter->type = type ? strdup(type) : NULL;
TAILQ_INSERT_TAIL(parameters, parameter, entries);
return parameter;
}
/* update a parameter: select by name and update its type, or insert it */
parameter_t *update_parameter(parameters_t *parameters, const char *name, const char *type);
parameter_t *parameter;
if ((parameter = select_parameter(parameters, name))) {
if (type) {
if (parameter->type) free(parameter->type);
parameter->type = strdup(type);
}
return parameter;
} else {
return insert_parameter(parameter, name, type);
}
}
/* update a parameters list from a cbor map { *name:type } */
void update_cbor_parameters(parameters_t *parameters, cbor_item_t *cparameters) {
char *name, *type;
struct cbor_pair *pairs;
size_t sz, i, len;
pairs = cbor_map_handle(cparameters);
sz = cbor_map_size(cparameters);
for (i = 0; i < sz; i++) {
name = xAAL_cbor_string_dup(pairs[i].key, &len);
type = xAAL_cbor_string_dup(pairs[i].value, &len);
if (name && type && validate_identifier(name) )
update_parameter(parameters, name, type);
if (name) free(name);
if (type) free(type);
}
}
/* update a parameters list from a json array */
void update_json_parameters(parameters_t *parameters, json_object *jparameters) {
const char *name, *type;
json_object_object_foreach(jparameters, key, val) {
if ( json_object_is_type(key, json_type_string) && json_object_is_type(value, json_type_string) ) {
name = json_object_get_string(key);
type = json_object_get_string(val);
if (validate_identifier(name) )
update_parameter(parameters, name, type);
}
}
}
/*
* Section Attributes
*/
/* select an attribute by its name in a schema or in extended ones */
attribute_t *select_schema_attribute(schema_t *schema, const char *name) {
parameter_t *select_schema_attribute(schema_t *schema, const char *name) {
schema_t *p_schema = schema;
attribute_t *attribute;
parameter_t *attribute;
while (p_schema) {
attribute = select_attribute(&(p_schema->attributes), name);
attribute = select_parameter(&(p_schema->attributes), name);
if (attribute)
return attribute;
p_schema = p_schema->extends;
......@@ -76,24 +145,6 @@ attribute_t *select_schema_attribute(schema_t *schema, const char *name) {
}
/* insert an attribute (its name) */
attribute_t *insert_attribute(attributes_t *attributes, const char *name) {
attribute_t *attribute = (attribute_t *)malloc(sizeof(attribute_t));
attribute->name = strdup(name);
attribute->type = NULL;
TAILQ_INSERT_TAIL(attributes, attribute, entries);
return attribute;
}
/* select or insert an attribute (its name) */
attribute_t *add_attribute(attributes_t *attributes, const char *name) {
attribute_t *attribute;
if ((attribute = select_attribute(attributes, name)))
return attribute;
else
return insert_attribute(attributes, name);
}
/* serialize attributes of a schema into cbor, including extended schemas recursively */
......@@ -102,7 +153,7 @@ cbor_item_t *get_cbor_attributes(schema_t *schema) {
return cbor_new_indefinite_map();
else {
cbor_item_t *cattributes = get_cbor_attributes(schema->extends);
attribute_t *attribute;
parameter_t *attribute;
TAILQ_FOREACH(attribute, &(schema->attributes), entries)
xAAL_cbor_map_update(cattributes, attribute->name, cbor_build_string(attribute->type));
return cattributes;
......@@ -116,7 +167,7 @@ json_object *get_json_attributes(schema_t *schema) {
return json_object_new_object();
else {
json_object *jattributes = get_json_attributes(schema->extends);
attribute_t *attribute;
parameter_t *attribute;
TAILQ_FOREACH(attribute, &(schema->attributes), entries) {
if (json_object_object_get_ex(jattributes, attribute->name, NULL))
json_object_object_del(jattributes, attribute->name);
......@@ -127,11 +178,9 @@ json_object *get_json_attributes(schema_t *schema) {
}
//// ICICICI
/* update an attribute */
void update_cbor_attribute(attributes_t *attributes, const char *name, cbor_item_t *cattribute) {
attribute_t *attribute = add_attribute(attributes, name);
void update_cbor_attribute(parameters_t *attributes, const char *name, cbor_item_t *cattribute) {
parameter_t *attribute = add_attribute(attributes, name);
char *description, *unit, *type;
size_t len;
......@@ -166,9 +215,9 @@ void update_cbor_attribute(attributes_t *attributes, const char *name, cbor_item
/* (json) update an attribute */
void update_json_attribute(attributes_t *attributes, const char *name, json_object *jattribute) {
void update_json_attribute(parameters_t *attributes, const char *name, json_object *jattribute) {
json_object *jdescription, *junit, *jtype;
attribute_t *attribute = add_attribute(attributes, name);
parameter_t *attribute = add_attribute(attributes, name);
if ( json_object_object_get_ex(jattribute, "description", &jdescription)
&& json_object_is_type(jdescription, json_type_string) ) {
......@@ -193,7 +242,7 @@ void update_json_attribute(attributes_t *attributes, const char *name, json_obje
/* Check if an attribute is querable by getAttributes()
Attributes of basic.basic are used by getDescription() and not getAttributes() */
i.e. Attributes of basic.basic are used with getDescription() and not getAttributes() */
bool not_basic_attribute(const char *name) {
static char *basic[] = { "devType", "address", "vendorId", "productId", "hwId",
"version", "groupId", "url", "info",
......
......@@ -99,11 +99,11 @@ attribute_t *select_attribute(attributes_t *attributes, const char *name);
/* select an attribute by its name in a schema or in extended ones */
attribute_t *select_schema_attribute(schema_t *schema, const char *name);
/* insert an attribute (its name) */
attribute_t *insert_attribute(attributes_t *attributes, const char *name);
/* insert an attribute */
attribute_t *insert_attribute(attributes_t *attributes, const char *name, const char *type);
/* select or insert an attribute (its name) */
attribute_t *add_attribute(attributes_t *attributes, const char *name);
/* update an attribute: select by name and update its type, or insert it */
attribute_t *update_attribute(attributes_t *attributes, const char *name, const char *type);
/* serialize attributes of a schema into cbor, including extended schemas recursively */
cbor_item_t *get_cbor_attributes(schema_t *schema);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment