From: Chanwoo Choi Date: Thu, 21 Jul 2022 02:50:07 +0000 (+0900) Subject: monitor: request-handler: Improve readability by removing duplicate code X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Fsandbox%2Fchanwoochoi%2Fmonitor-request-handler;p=platform%2Fcore%2Fsystem%2Fpass.git monitor: request-handler: Improve readability by removing duplicate code TOOD Some funcitons get both resource_id and attribute data by parsing the received data of socket. By extracting the common function, it makes that improve the readability and remove the duplicate code. By using the get_resource_id_and_attr_data functin, clien can easily understand the meaning of that parse the received data of socket. Change-Id: I64b8431eda06218497e39ec18239e4ee8931339c Signed-off-by: Chanwoo Choi --- diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c index 324d0fc..e70db2e 100644 --- a/src/monitor/request-handler.c +++ b/src/monitor/request-handler.c @@ -85,6 +85,32 @@ get_resource_by_id(struct request_client *client, int resource_id) return g_hash_table_lookup(client->resource_table, GINT_TO_POINTER(resource_id)); } +static int get_resource_id_and_attr_data(struct request_client *client, char *args, + int *id, u_int64_t *attr_data) +{ + if (!client || !args || !id || !attr_data) + return -ENOENT; + + /** + * Format of REQUEST_GET_VALUE_UINT args: + * Format of REQUEST_IS_RESOURCE_ATTR_SUPPORTED args: + * Format of REQUEST_GET_VALUE_INT args: + * Format of REQUEST_GET_VALUE_INT64 args: + * Format of REQUEST_GET_VALUE_UINT args: + * Format of REQUEST_GET_VALUE_UINT64 args: + * Format of REQUEST_GET_VALUE_DOUBLE args: + * Format of REQUEST_GET_VALUE_STRING args: + * Format of REQUEST_GET_VALUE_ARRAY args: + * - + * Format of REQUEST_SET_RESOURCE_ATTR and REQUEST_UNSET_RESOURCE_ATTR args: + * - + */ + if (sscanf(args, "%d$%"PRIu64, id, attr_data) < 2) + return -EINVAL; + + return 0; +} + static int handle_request_create_resource(struct request_client *client, char *args) { struct resource *res; @@ -219,14 +245,11 @@ static int handle_request_set_resource_attr(struct request_client *client, char return -ENOENT; } - /** - * Format of REQUEST_SET_RESOURCE_ATTR and REQUEST_UNSET_RESOURCE_ATTR args: - * - - */ - if (sscanf(args, "%d$%"PRIu64, &resource_id, &interest_masks) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &interest_masks); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", client->socket_fd); - return -EINVAL; + return ret; } res = get_resource_by_id(client, resource_id); @@ -305,7 +328,7 @@ static int handle_request_set_resource_flag(struct request_client *client, char static int handle_request_is_resource_attr_supported(struct request_client *client, char *args, bool *supported) { struct resource *res; - int resource_id; + int resource_id, ret; u_int64_t attr_id; if (!client || !args) { @@ -313,14 +336,11 @@ static int handle_request_is_resource_attr_supported(struct request_client *clie return -ENOENT; } - /** - * Format of REQUEST_IS_RESOURCE_ATTR_SUPPORTED args: - * - - */ - if (sscanf(args, "%d$%"PRIu64"", &resource_id, &attr_id) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", client->socket_fd); - return -EINVAL; + return ret; } res = get_resource_by_id(client, resource_id); @@ -438,14 +458,11 @@ static int handle_request_get_value_int(struct request_client *client, char *arg return -ENOENT; } - /** - * Format of REQUEST_GET_VALUE_INT args: - * - - */ - if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", - client->socket_fd); - return -EINVAL; + client->socket_fd); + return ret; } res = get_resource_by_id(client, resource_id); @@ -478,14 +495,11 @@ static int handle_request_get_value_int64(struct request_client *client, char *a return -ENOENT; } - /** - * Format of REQUEST_GET_VALUE_INT64 args: - * - - */ - if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", client->socket_fd); - return -EINVAL; + return ret; } res = get_resource_by_id(client, resource_id); @@ -519,14 +533,11 @@ handle_request_get_value_uint(struct request_client *client, char *args, u_int32 return -ENOENT; } - /** - * Format of REQUEST_GET_VALUE_UINT args: - * - - */ - if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", client->socket_fd); - return -EINVAL; + return ret; } res = get_resource_by_id(client, resource_id); @@ -560,14 +571,11 @@ handle_request_get_value_uint64(struct request_client *client, char *args, u_int return -ENOENT; } - /** - * Format of REQUEST_GET_VALUE_UINT64 args: - * - - */ - if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", client->socket_fd); - return -EINVAL; + return ret; } res = get_resource_by_id(client, resource_id); @@ -600,14 +608,11 @@ static int handle_request_get_value_double(struct request_client *client, char * return -ENOENT; } - /** - * Format of REQUEST_GET_VALUE_DOUBLE args: - * - - */ - if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", client->socket_fd); - return -EINVAL; + return ret; } res = get_resource_by_id(client, resource_id); @@ -640,14 +645,11 @@ static int handle_request_get_value_string(struct request_client *client, char * return -ENOENT; } - /** - * Format of REQUEST_GET_VALUE_INT args: - * - - */ - if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", client->socket_fd); - return -EINVAL; + return ret; } res = get_resource_by_id(client, resource_id); @@ -681,14 +683,11 @@ handle_request_get_value_array(struct request_client *client, char *args, struct return -ENOENT; } - /** - * Format of REQUEST_GET_VALUE_ARRAY args: - * - - */ - if (sscanf(args, "%d$%"PRIu64, &resource_id, &attr_id) < 2) { + ret = get_resource_id_and_attr_data(client, args, &resource_id, &attr_id); + if (ret < 0) { _E("failed to get resource and attribute id, client(%d)\n", client->socket_fd); - return -EINVAL; + return ret; } res = get_resource_by_id(client, resource_id);