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

API modifiée du cache

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/C/branches/version-0.7@2293 b32b6428-25c9-4566-ad07-03861ab6144f
parent ad66fa94
No related branches found
No related tags found
No related merge requests found
...@@ -132,48 +132,13 @@ void manage_attributesInfo(device_t *device, cbor_item_t *cbody) { ...@@ -132,48 +132,13 @@ void manage_attributesInfo(device_t *device, cbor_item_t *cbody) {
} }
/* Reply to getDeviceAttribute */ /* Serialize an attribute into cbor */
bool reply_getDeviceAttribute(const xAAL_businfo_t *bus, cbor_item_t *get_cbor_attribute(attr_t *attribute) {
const xAAL_devinfo_t *me, cbor_item_t *cattribute = cbor_new_definite_map(3);
const uuid_t *target, cbor_map_add(cattribute, (struct cbor_pair){ cbor_move(cbor_build_string("name")), cbor_move(cbor_build_string(attribute->name)) });
cbor_item_t *cqueryBody, cbor_map_add(cattribute, (struct cbor_pair){ cbor_move(cbor_build_string("value")), attribute->val });
devices_t *devices) { cbor_map_add(cattribute, (struct cbor_pair){ cbor_move(cbor_build_string("date")), cbor_move(xAAL_cbor_build_int(attribute->date)) });
cbor_item_t *cqueryDevice, *cqueryAttribute, *cbody; return cattribute;
uuid_t queryDevice;
const char *queryAttribute;
size_t queryAttribute_sz;
device_t *device = NULL;
attr_t *attribute = NULL;
cqueryDevice = xAAL_cbor_map_get(cqueryBody, "device");
if ( !xAAL_cbor_is_uuid(cqueryDevice, &queryDevice) )
return false;
LIST_FOREACH(device, devices, entries)
if ( uuid_compare(device->uuid, queryDevice) == 0 )
break;
if (device == NULL)
return false;
cqueryAttribute = xAAL_cbor_map_get(cqueryBody, "attribute");
if ( !cqueryAttribute || !cbor_isa_string(cqueryAttribute) || !cbor_string_is_definite(cqueryAttribute) )
return false;
queryAttribute = (const char *)cbor_string_handle(cqueryAttribute);
queryAttribute_sz = cbor_string_length(cqueryAttribute);
LIST_FOREACH(attribute, &(device->attributes), entries)
if ( strncmp(attribute->name, queryAttribute, queryAttribute_sz) == 0 )
break;
if (attribute == NULL)
return false;
cbody = cbor_new_definite_map(4);
cbor_map_add(cbody, (struct cbor_pair){ cbor_move(cbor_build_string("device")), cbor_move(cqueryDevice) });
cbor_map_add(cbody, (struct cbor_pair){ cbor_move(cbor_build_string("attribute")), cbor_move(cqueryAttribute) });
cbor_map_add(cbody, (struct cbor_pair){ cbor_move(cbor_build_string("value")), attribute->val });
cbor_map_add(cbody, (struct cbor_pair){ cbor_move(cbor_build_string("date")), cbor_move(xAAL_cbor_build_int(attribute->date)) });
return xAAL_write_busl(bus, me, xAAL_REPLY, "getDeviceAttribute", cbody, target, NULL);
} }
...@@ -183,10 +148,12 @@ bool reply_getDeviceAttributes(const xAAL_businfo_t *bus, ...@@ -183,10 +148,12 @@ bool reply_getDeviceAttributes(const xAAL_businfo_t *bus,
const uuid_t *target, const uuid_t *target,
cbor_item_t *cqueryBody, cbor_item_t *cqueryBody,
devices_t *devices) { devices_t *devices) {
cbor_item_t *cqueryDevice, *cbody, *cattributes, *cattribute; cbor_item_t *cqueryDevice, *cbody, *cattributes, *cache;
uuid_t queryDevice; uuid_t queryDevice;
device_t *device = NULL; device_t *device = NULL;
attr_t *attribute = NULL; attr_t *attribute = NULL;
size_t i, sz, len;
char *name;
cqueryDevice = xAAL_cbor_map_get(cqueryBody, "device"); cqueryDevice = xAAL_cbor_map_get(cqueryBody, "device");
if ( !xAAL_cbor_is_uuid(cqueryDevice, &queryDevice) ) if ( !xAAL_cbor_is_uuid(cqueryDevice, &queryDevice) )
...@@ -198,18 +165,29 @@ bool reply_getDeviceAttributes(const xAAL_businfo_t *bus, ...@@ -198,18 +165,29 @@ bool reply_getDeviceAttributes(const xAAL_businfo_t *bus,
if (device == NULL) if (device == NULL)
return false; return false;
cattributes = cbor_new_indefinite_array(); cache = cbor_new_indefinite_array();
LIST_FOREACH(attribute, &(device->attributes), entries) {
cattribute = cbor_new_definite_map(3); cattributes = xAAL_cbor_map_get(cqueryBody, "attributes");
cbor_map_add(cattribute, (struct cbor_pair){ cbor_move(cbor_build_string("name")), cbor_move(cbor_build_string(attribute->name)) }); if (!cattributes || !cbor_isa_array(cattributes) || ((sz=cbor_array_size(cattributes)) == 0) ) {
cbor_map_add(cattribute, (struct cbor_pair){ cbor_move(cbor_build_string("value")), attribute->val }); LIST_FOREACH(attribute, &(device->attributes), entries) {
cbor_map_add(cattribute, (struct cbor_pair){ cbor_move(cbor_build_string("date")), cbor_move(xAAL_cbor_build_int(attribute->date)) }); cbor_array_push(cache, cbor_move(get_cbor_attribute(attribute)));
cbor_array_push(cattributes, cattribute); }
} else {
for (i=0; i<sz; i++) {
name = xAAL_cbor_string_dup(cbor_move(cbor_array_get(cattributes, i)), &len);
LIST_FOREACH(attribute, &(device->attributes), entries) {
if ( !name || (strcmp(attribute->name, name) == 0) ) {
cbor_array_push(cache, cbor_move(get_cbor_attribute(attribute)));
break;
}
}
free(name);
}
} }
cbody = cbor_new_definite_map(2); cbody = cbor_new_definite_map(2);
cbor_map_add(cbody, (struct cbor_pair){ cbor_move(cbor_build_string("device")), cbor_move(cqueryDevice) }); cbor_map_add(cbody, (struct cbor_pair){ cbor_move(cbor_build_string("device")), cbor_move(cqueryDevice) });
cbor_map_add(cbody, (struct cbor_pair){ cbor_move(cbor_build_string("attributes")), cbor_move(cattributes) }); cbor_map_add(cbody, (struct cbor_pair){ cbor_move(cbor_build_string("attributes")), cbor_move(cache) });
return xAAL_write_busl(bus, me, xAAL_REPLY, "getDeviceAttributes", cbody, target, NULL); return xAAL_write_busl(bus, me, xAAL_REPLY, "getDeviceAttributes", cbody, target, NULL);
} }
...@@ -263,10 +241,6 @@ void manage_msg(const xAAL_businfo_t *bus, const xAAL_devinfo_t *me, ...@@ -263,10 +241,6 @@ void manage_msg(const xAAL_businfo_t *bus, const xAAL_devinfo_t *me,
if ( !reply_getAttributes(bus, me, source) ) if ( !reply_getAttributes(bus, me, source) )
fprintf(xAAL_error_log, "Could not reply to getAttributes\n"); fprintf(xAAL_error_log, "Could not reply to getAttributes\n");
} else if ( strcmp(action, "getDeviceAttribute") == 0 ) {
if ( !reply_getDeviceAttribute(bus, me, source, cbody, devices) )
fprintf(xAAL_error_log, "Could not reply to getDeviceAttribute\n");
} else if ( strcmp(action, "getDeviceAttributes") == 0 ) { } else if ( strcmp(action, "getDeviceAttributes") == 0 ) {
if ( !reply_getDeviceAttributes(bus, me, source, cbody, devices) ) if ( !reply_getDeviceAttributes(bus, me, source, cbody, devices) )
fprintf(xAAL_error_log, "Could not reply to getDeviceAttributes\n"); fprintf(xAAL_error_log, "Could not reply to getDeviceAttributes\n");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment