From: Dongwoo Lee Date: Fri, 8 Apr 2022 07:59:30 +0000 (+0900) Subject: monitor: resource: Add function for getting resource and its attributes list X-Git-Tag: accepted/tizen/unified/20220413.133040^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bb23b347050aa995adaffb8831ffbc9eea0955a1;p=platform%2Fcore%2Fsystem%2Fpass.git monitor: resource: Add function for getting resource and its attributes list To provide way to manage monitoring module without static list, now json type information for resource and its attributes list can be retrieved with following function: - pass_resource_monitor_get_resource_list_json() Change-Id: Ib9c8787a081f674fc1d6bdf12d9d027cca4b0352 Signed-off-by: Dongwoo Lee --- diff --git a/include/util/request.h b/include/util/request.h index 44d2047..c640830 100644 --- a/include/util/request.h +++ b/include/util/request.h @@ -45,6 +45,7 @@ enum { REQUEST_GET_VALUE_STRING, REQUEST_GET_VALUE_ARRAY, REQUEST_GET_RESOURCE_TS, + REQUEST_GET_RESOURCE_LIST_JSON, REQUEST_MAX, }; diff --git a/lib/resource-monitor/resource-monitor.c b/lib/resource-monitor/resource-monitor.c index 02361f9..fc70700 100644 --- a/lib/resource-monitor/resource-monitor.c +++ b/lib/resource-monitor/resource-monitor.c @@ -461,6 +461,9 @@ static int pass_resource_monitor_get_json(int id, char *json_string, int request buffer_len = snprintf(buffer, HUGE_BUFF_MAX, "%d$%d$%"PRIu64, request_type, resource_id, attr_id); break; + case REQUEST_GET_RESOURCE_LIST_JSON: + buffer_len = snprintf(buffer, HUGE_BUFF_MAX, "%d", request_type); + break; default: va_end(args); free(buffer); @@ -512,6 +515,12 @@ int pass_resource_monitor_get_value_json(int id, int resource_id, u_int64_t attr } EXPORT +int pass_resource_monitor_get_resource_list_json(int id, char *json_string) +{ + return pass_resource_monitor_get_json(id, json_string, REQUEST_GET_RESOURCE_LIST_JSON); +} + +EXPORT int pass_resource_monitor_get_value_int(int id, int resource_id, u_int64_t attr_id, int32_t *value) { char buffer[GENERIC_BUFF_MAX + 1]; diff --git a/lib/resource-monitor/resource-monitor.h b/lib/resource-monitor/resource-monitor.h index c943dc4..4644d96 100644 --- a/lib/resource-monitor/resource-monitor.h +++ b/lib/resource-monitor/resource-monitor.h @@ -277,6 +277,14 @@ int pass_resource_monitor_get_array_string(int id, int res_id, u_int64_t attr, c int pass_resource_monitor_get_resource_timestamp(int id, int res_id, int64_t *start, int64_t *end); +/** + * @brief Get all available resource list which monitoring module supported + * @param[in] Resource monitor id + * @param[out] JSON type string for resource + * @return @c positive integer as resource count on success, otherwise a negative error value + */ +int pass_resource_monitor_get_resource_list_json(int id, char *json_string); + #ifdef __cplusplus } #endif diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c index 2bad112..184631d 100644 --- a/src/monitor/request-handler.c +++ b/src/monitor/request-handler.c @@ -255,7 +255,7 @@ handle_request_get_json(struct request_client *client, char *args, u_int64_t attr_id; int resource_id; - if (!client || !args) + if (!client) return -ENOENT; /** @@ -263,6 +263,8 @@ handle_request_get_json(struct request_client *client, char *args, * - * Format of REQUEST_GET_VALUE_JSON args: * - : + * Format of REQUEST_GET_RESOURCE_LIST_JSON args: + * - NO ARGUMENTS */ switch (request_type) { case REQUEST_GET_RESOURCE_JSON: @@ -272,6 +274,8 @@ handle_request_get_json(struct request_client *client, char *args, if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) return -EINVAL; break; + case REQUEST_GET_RESOURCE_LIST_JSON: + goto skip_res; default: return -EINVAL; } @@ -280,11 +284,14 @@ handle_request_get_json(struct request_client *client, char *args, if (!res) return -EINVAL; +skip_res: switch (request_type) { case REQUEST_GET_RESOURCE_JSON: return get_resource_attrs_json(res, json_string); case REQUEST_GET_VALUE_JSON: return get_resource_attr_json(res, attr_id, json_string); + case REQUEST_GET_RESOURCE_LIST_JSON: + return get_resource_list_json(json_string); default: return -EINVAL; } @@ -511,6 +518,7 @@ static int handle_request(struct request_client *client, char *request) case REQUEST_GET_RESOURCE_JSON: case REQUEST_GET_VALUE_JSON: case REQUEST_GET_VALUE_ARRAY: + case REQUEST_GET_RESOURCE_LIST_JSON: buffer_len = HUGE_BUFF_MAX + 1; break; default: @@ -573,6 +581,7 @@ static int handle_request(struct request_client *client, char *request) break; case REQUEST_GET_RESOURCE_JSON: case REQUEST_GET_VALUE_JSON: + case REQUEST_GET_RESOURCE_LIST_JSON: { char *json_string; diff --git a/src/util/resource.c b/src/util/resource.c index abe7b49..9c2233d 100644 --- a/src/util/resource.c +++ b/src/util/resource.c @@ -551,6 +551,128 @@ int get_resource_attr_json(struct resource *resource, u_int64_t attr_id, char ** return 0; } +static json_object *get_resource_driver_json(const struct resource_driver *driver) +{ + json_object *jobj_drv, *jobj_drv_name, *jobj_drv_type; + json_object *jobj_drv_attrs_array, *jobj_drv_attr, *jobj_drv_ctrls_array, *jobj_drv_ctrl; + json_object *jobj_drv_attr_name, *jobj_drv_attr_type, *jobj_drv_attr_id; + json_object *jobj_drv_ctrl_name, *jobj_drv_ctrl_id; + const struct resource_attribute *attr; + const struct resource_control *ctrl; + int i; + + jobj_drv = json_object_new_object(); + + jobj_drv_name = json_object_new_string(driver->name); + jobj_drv_type = json_object_new_int(driver->type); + + jobj_drv_attrs_array = json_object_new_array(); + + for (i = 0; i < driver->num_attrs; i++) { + attr = &driver->attrs[i]; + + jobj_drv_attr = json_object_new_object(); + + jobj_drv_attr_name = json_object_new_string(attr->name); + jobj_drv_attr_type = json_object_new_int(attr->type); + jobj_drv_attr_id = json_object_new_uint64(attr->id); + + json_object_object_add(jobj_drv_attr, "name", jobj_drv_attr_name); + json_object_object_add(jobj_drv_attr, "type", jobj_drv_attr_type); + json_object_object_add(jobj_drv_attr, "id", jobj_drv_attr_id); + + json_object_array_add(jobj_drv_attrs_array, jobj_drv_attr); + } + + jobj_drv_ctrls_array = json_object_new_array(); + + for (i = 0; i < driver->num_ctrls; i++) { + ctrl = &driver->ctrls[i]; + + jobj_drv_ctrl = json_object_new_object(); + + jobj_drv_ctrl_name = json_object_new_string(ctrl->name); + jobj_drv_ctrl_id = json_object_new_uint64(ctrl->id); + + json_object_object_add(jobj_drv_ctrl, "name", jobj_drv_ctrl_name); + json_object_object_add(jobj_drv_ctrl, "id", jobj_drv_ctrl_id); + + json_object_array_add(jobj_drv_ctrls_array, jobj_drv_ctrl); + } + + json_object_object_add(jobj_drv, "name", jobj_drv_name); + json_object_object_add(jobj_drv, "type", jobj_drv_type); + json_object_object_add(jobj_drv, "attrs", jobj_drv_attrs_array); + json_object_object_add(jobj_drv, "ctrls", jobj_drv_ctrls_array); + + return jobj_drv; +} + +void put_resource_driver_json(json_object *jobj_drv) +{ + json_object *jobj_array, *jobj_obj; + int i; + + if (json_object_object_get_ex(jobj_drv, "ctrls", &jobj_array)) { + for (i = 0; i < json_object_array_length(jobj_array); i++) { + jobj_obj = json_object_array_get_idx(jobj_array, i); + + json_object_object_del(jobj_obj, "id"); + json_object_object_del(jobj_obj, "name"); + } + json_object_array_del_idx(jobj_array, 0, json_object_array_length(jobj_array)); + } + + if (json_object_object_get_ex(jobj_drv, "attrs", &jobj_array)) { + for (i = 0; i < json_object_array_length(jobj_array); i++) { + jobj_obj = json_object_array_get_idx(jobj_array, i); + + json_object_object_del(jobj_obj, "id"); + json_object_object_del(jobj_obj, "type"); + json_object_object_del(jobj_obj, "name"); + } + json_object_array_del_idx(jobj_array, 0, json_object_array_length(jobj_array)); + } + + json_object_object_del(jobj_drv, "ctrls"); + json_object_object_del(jobj_drv, "attrs"); + json_object_object_del(jobj_drv, "type"); + json_object_object_del(jobj_drv, "name"); +} + +int get_resource_list_json(char **json_string) +{ + const struct resource_driver *driver; + json_object *jobj_root, *jobj_drvs_array, *jobj_drv; + int i; + + jobj_root = json_object_new_object(); + jobj_drvs_array = json_object_new_array(); + + for (i = 0; i < g_list_length(g_resource_driver_head); i++) { + driver = g_list_nth(g_list_first(g_resource_driver_head), i)->data; + + jobj_drv = get_resource_driver_json(driver); + json_object_array_add(jobj_drvs_array, jobj_drv); + } + + json_object_object_add(jobj_root, "resources", jobj_drvs_array); + + *json_string = strdup(json_object_to_json_string(jobj_root)); + + for (i = 0; i < json_object_array_length(jobj_drvs_array); i++) { + jobj_drv = json_object_array_get_idx(jobj_drvs_array, i); + + put_resource_driver_json(jobj_drv); + } + json_object_array_del_idx(jobj_drvs_array, 0, json_object_array_length(jobj_drvs_array)); + + json_object_object_del(jobj_root, "resources"); + json_object_put(jobj_root); + + return 0; +} + int get_resource_attr_int(struct resource *resource, u_int64_t attr_id, int32_t *data) { struct resource_attribute_value *attr_value = NULL;