From f0de9494348b021f6a19ab8a534534390a8745b7 Mon Sep 17 00:00:00 2001 From: Ismo Puustinen Date: Thu, 29 Nov 2012 14:24:22 +0200 Subject: [PATCH] resource-c: implemented functions. --- .../resource-native/libmurphy-resource/api_test.c | 61 ++++-- .../libmurphy-resource/resource-api.h | 39 +++- .../resource-native/libmurphy-resource/resource.c | 215 +++------------------ 3 files changed, 106 insertions(+), 209 deletions(-) diff --git a/src/plugins/resource-native/libmurphy-resource/api_test.c b/src/plugins/resource-native/libmurphy-resource/api_test.c index 1b8eefe..56bd43f 100644 --- a/src/plugins/resource-native/libmurphy-resource/api_test.c +++ b/src/plugins/resource-native/libmurphy-resource/api_test.c @@ -82,7 +82,6 @@ static void state_callback(murphy_resource_context *context, int i = 0, j = 0; const murphy_string_array *app_classes = NULL; const murphy_resource_set *rs; - murphy_resource **resource; murphy_string_array *attributes = NULL; murphy_resource_attribute *attr; bool system_handles_audio = FALSE; @@ -109,36 +108,48 @@ static void state_callback(murphy_resource_context *context, } if ((rs = murphy_resource_set_list(context)) != NULL) { + murphy_string_array *resource_names; + printf("listing all resources available in the system\n"); - resource = rs->resources; - for (i = 0; i < rs->num_resources; i++) { + resource_names = murphy_resource_list_names(context, rs); + + for (i = 0; i < resource_names->num_strings; i++) { + + murphy_resource *resource; + + resource = murphy_resource_get_by_name(context, rs, + resource_names->strings[i]); - printf("resource %d is %s\n", i, rs->resources[i]->name); - if (strcmp(rs->resources[i]->name, "audio_playback") == 0) + printf("resource %d is %s\n", i, resource->name); + if (strcmp(resource->name, "audio_playback") == 0) system_handles_audio = TRUE; - if (strcmp(rs->resources[i]->name, "video_playback") == 0) + if (strcmp(resource->name, "video_playback") == 0) system_handles_video = TRUE; - murphy_attribute_list(context, resource[i], &attributes); + attributes = murphy_attribute_list_names(context, resource); for (j = 0; j < attributes->num_strings; j++) { - murphy_attribute_get_by_name(context, - resource[i], - attributes->strings[i], - &attr); + attr = murphy_attribute_get_by_name(context, + resource, + attributes->strings[i]); printf("attr %s has ", attr->name); switch(attr->type) { case murphy_string: printf("type string and value %s\n", attr->string); + break; case murphy_int32: printf("type string and value %d\n", attr->integer); + break; case murphy_uint32: - printf("type string and value %d\n", attr->unsignd); + printf("type string and value %u\n", attr->unsignd); + break; case murphy_double: printf("type string and value %f\n", attr->floating); + break; default: printf("type unknown\n"); + break; } } @@ -165,17 +176,33 @@ static void resource_callback(murphy_resource_context *cx, void *userdata) { my_app_data *my_data = (my_app_data *) userdata; - int i; + murphy_resource *res; if (!murphy_resource_set_equals(rs, my_data->rs)) return; - for (i = 0; i < rs->num_resources; i++) { - /* here compare the resource set difference */ - printf("resource %d name %s\n", i, rs->resources[i]->name); - printf("resource %d state %d\n", i, rs->resources[i]->state); + /* here compare the resource set difference */ + + res = murphy_resource_get_by_name(cx, rs, "audio_playback"); + + if (!res) { + printf("audio_playback not present in resource set"); + return; } + printf("resource 0 name %s\n", res->name); + printf("resource 0 state %d\n", res->state); + + res = murphy_resource_get_by_name(cx, rs, "video_playback"); + + if (!res) { + printf("video_playback not present in resource set"); + return; + } + + printf("resource 1 name %s\n", res->name); + printf("resource 1 state %d\n", res->state); + /* let's copy the changed set for ourselves */ /* Delete must not mean releasing the set! Otherwise this won't work. diff --git a/src/plugins/resource-native/libmurphy-resource/resource-api.h b/src/plugins/resource-native/libmurphy-resource/resource-api.h index 9d3c5c8..7a6fd60 100644 --- a/src/plugins/resource-native/libmurphy-resource/resource-api.h +++ b/src/plugins/resource-native/libmurphy-resource/resource-api.h @@ -166,7 +166,7 @@ void murphy_resource_set_delete(murphy_resource_set *set); * * @return pointer to a copy of the resource set. */ -murphy_resource_set *murphy_resource_set_copy(murphy_resource_set *orig); +murphy_resource_set *murphy_resource_set_copy(const murphy_resource_set *orig); /** * You might have assigned the same update callback for @@ -229,6 +229,33 @@ murphy_resource *murphy_resource_create(murphy_resource_context *cx, bool shared); /** + * Get the names of all resources in this resource set. + * + * @param cx murphy context. + * @param rs resource set where the resource are. + * @param names pointer where the name array with content will + * be allocated. + * + * @return murphy error code + */ +murphy_string_array * murphy_resource_list_names(murphy_resource_context *cx, + const murphy_resource_set *rs); + +/** + * Delete resource by name from resource set. + * + * @param cx connnection to Murphy resource engine. + * @param rs resource set where you want to get the resource. + * @param name name of the resource you want to get. + * @param pointer to resource pointer to be assigned. + * + * @return 0 if resource found. + */ +murphy_resource * murphy_resource_get_by_name(murphy_resource_context *cx, + const murphy_resource_set *rs, + const char *name); + +/** * Delete a resource from a resource set. * * @param set resource the resource will deleted from. @@ -259,9 +286,8 @@ bool murphy_resource_delete_by_name(murphy_resource_set *rs, * * @return murphy error code */ -int murphy_attribute_list_names(murphy_resource_context *cx, - murphy_resource *res, - murphy_string_array **names); +murphy_string_array * murphy_attribute_list_names(murphy_resource_context *cx, + const murphy_resource *res); /** * Get the particular resource attribute by name from the resource. @@ -273,10 +299,9 @@ int murphy_attribute_list_names(murphy_resource_context *cx, * * @return murphy error code. */ -int murphy_attribute_get_by_name(murphy_resource_context *cx, +murphy_resource_attribute * murphy_attribute_get_by_name(murphy_resource_context *cx, murphy_resource *res, - const char *name, - murphy_resource_attribute **attr); + const char *name); /** * Set new attribute value to resource. diff --git a/src/plugins/resource-native/libmurphy-resource/resource.c b/src/plugins/resource-native/libmurphy-resource/resource.c index 237dc78..e70d9ff 100644 --- a/src/plugins/resource-native/libmurphy-resource/resource.c +++ b/src/plugins/resource-native/libmurphy-resource/resource.c @@ -666,7 +666,7 @@ void priv_attr_to_murphy_attr(attribute_t *attr, murphy_resource_attribute *attr switch (attr->type) { case 's': attribute->type = murphy_string; - attribute->string = mrp_strdup(attr->name); + attribute->string = mrp_strdup(attr->string); break; case 'i': attribute->type = murphy_int32; @@ -1534,39 +1534,6 @@ const murphy_resource_set * murphy_resource_set_list(murphy_resource_context *cx return cx->priv->master_resource_set; } -#if 0 -int murphy_resource_set_list_to_context(murphy_resource_context *cx, - murphy_resource_set **set) -{ - int i = 0; - - murphy_resource_set *rs = create_resource_set("implicit"); - - rs->num_resources = cx->priv->available_resources->dim; - - if (rs->num_resources > MAX_LEN) - goto error; - - for (i = 0; i < rs->num_resources; i++) { - rs->resources[i] = create_resource(cx, cx->priv->available_resources->defs[i].name); - if (!rs->resources[i]) { - rs->num_resources = i; /* only allocated this much */ - goto error; - } - } - - *set = rs; - - return 0; - -error: - printf("murphy_list_resources error\n"); - delete_resource_set(rs); - - return -1; -} -#endif - int murphy_resource_set_acquire(murphy_resource_context *cx, murphy_resource_set *rset) { @@ -1700,17 +1667,6 @@ error: return -1; } -#if 0 -void murphy_resource_set_set_callback(murphy_resource_set *rset, - murphy_resource_callback cb, - void *userdata) -{ - if (!rset) - return; - -} -#endif - bool murphy_resource_set_equals(const murphy_resource_set *a, const murphy_resource_set *b) { @@ -1731,7 +1687,7 @@ murphy_resource_set *murphy_resource_set_create(murphy_resource_context *cx, } -murphy_resource_set *murphy_resource_set_copy(murphy_resource_set *original) +murphy_resource_set *murphy_resource_set_copy(const murphy_resource_set *original) { murphy_resource_set *copy = NULL; int i; @@ -1776,27 +1732,6 @@ void murphy_resource_set_delete(murphy_resource_set *set) delete_resource_set(set); } - -#if 0 -murphy_resource *murphy_resource_create(murphy_resource_context *cx, - const char *name, - bool mandatory, - bool shared) -{ - murphy_resource *rs = create_resource(cx, name); - - if (!rs) - goto error; - - rs->mandatory = mandatory; - rs->shared = shared; - return rs; - -error: - return NULL; -} -#endif - void murphy_resource_delete(murphy_resource_set *set, murphy_resource *res) { (void)set; @@ -1841,130 +1776,53 @@ bool murphy_resource_delete_by_name(murphy_resource_set *rs, const char *name) return true; } -#if 0 -char **get_attribute_names(murphy_resource_context *cx, murphy_resource - *res) +murphy_string_array * murphy_resource_list_names(murphy_resource_context *cx, + const murphy_resource_set *rs) { int i; - char **arr; + murphy_string_array *ret; - if (!cx || !res) + if (!cx || !rs) return NULL; - arr = mrp_alloc_array(char *, res->priv->attrs->dim + 1); - - for (i = 0; i < res->priv->attrs->dim; i++) { - arr[i] = mrp_strdup(res->priv->attrs->elems[i].name); - } - - arr[i] = NULL; - - return arr; -} - -enum murphy_attribute_type get_attribute_type(murphy_resource_context *cx, murphy_resource - *res, const char *attribute_name) -{ - int i; - char **arr; - - if (!cx || !res) - return murphy_invalid; + ret = mrp_allocz(sizeof(murphy_string_array)); - arr = mrp_alloc_array(char *, res->priv->attrs->dim + 1); + ret->num_strings = rs->priv->num_resources; + ret->strings = mrp_allocz_array(const char *, rs->priv->num_resources); - for (i = 0; i < res->priv->attrs->dim; i++) { - attribute_t *elem = &res->priv->attrs->elems[i]; - if (strcmp(elem->name, attribute_name) == 0) { - switch (elem->type) { - case 's': - return murphy_string; - case 'u': - return murphy_uint32; - case 'i': - return murphy_int32; - case 'f': - return murphy_double; - default: - return murphy_invalid; - } - } + for (i = 0; i < rs->priv->num_resources; i++) { + ret->strings[i] = mrp_strdup(rs->priv->resources[i]->name); } - return murphy_invalid; + return ret; } - -const void *murphy_get_attribute(murphy_resource_context *cx, murphy_resource - *res, const char *attribute_name) +murphy_resource * murphy_resource_get_by_name(murphy_resource_context *cx, + const murphy_resource_set *rs, + const char *name) { int i; - const void *ret = NULL; - if (!cx || !res) + if (!cx || !rs) return NULL; - for (i = 0; i < res->priv->attrs->dim; i++) { - attribute_t *elem = &res->priv->attrs->elems[i]; - if (strcmp(elem->name, attribute_name) == 0) { - switch (elem->type) { - case 's': - ret = elem->string; - case 'u': - ret = &elem->unsignd; - case 'i': - ret = &elem->integer; - case 'f': - ret = &elem->floating; - default: - break; - } - break; + for (i = 0; i < rs->priv->num_resources; i++) { + if (strcmp(name, rs->priv->resources[i]->name) == 0) { + return rs->priv->resources[i]; } } - return ret; -} - -bool murphy_set_attribute(murphy_resource_context *cx, murphy_resource - *res, const char *attribute_name, void *value) -{ - int i; - - if (!cx || !res || !value) - return false; - - for (i = 0; i < res->priv->attrs->dim; i++) { - attribute_t *elem = &res->priv->attrs->elems[i]; - if (strcmp(elem->name, attribute_name) == 0) { - switch (elem->type) { - case 's': - elem->string = mrp_strdup(value); - case 'u': - elem->unsignd = *(uint32_t *) value; - case 'i': - elem->integer = *(int32_t *) value; - case 'f': - elem->floating = *(double *) value; - default: - return false; - } - return true; - } - } - return false; + return NULL; } -#endif -int murphy_attribute_list(murphy_resource_context *cx, - murphy_resource *res, - murphy_string_array **names) +murphy_string_array * murphy_attribute_list_names(murphy_resource_context *cx, + const murphy_resource *res) { int i; murphy_string_array *ret; if (!cx || !res) - return -1; + return NULL; ret = mrp_allocz(sizeof(murphy_string_array)); @@ -1972,39 +1830,26 @@ int murphy_attribute_list(murphy_resource_context *cx, ret->strings = mrp_allocz_array(const char *, res->priv->num_attributes); for (i = 0; i < res->priv->num_attributes; i++) { - ret->strings[i] = res->priv->attrs[i].name; + ret->strings[i] = mrp_strdup(res->priv->attrs[i].name); } - *names = ret; - - return 0; + return ret; } -int murphy_attribute_get_by_name(murphy_resource_context *cx, +murphy_resource_attribute * murphy_attribute_get_by_name(murphy_resource_context *cx, murphy_resource *res, - const char *name, - murphy_resource_attribute **attribute) + const char *name) { int i; if (!cx || !res) - return -1; + return NULL; for (i = 0; i < res->priv->num_attributes; i++) { if (strcmp(name, res->priv->attrs[i].name) == 0) { - *attribute = &res->priv->attrs[i]; - return 0; + return &res->priv->attrs[i]; } } - return -1; -} - -#if 0 -int murphy_attribute_set(murphy_resource_context *cx, - murphy_resource *res, - const murphy_resource_attribute *attribute) -{ - return 0; + return NULL; } -#endif \ No newline at end of file -- 2.7.4