monitor: request-handler: Improve readability by removing duplicate code sandbox/chanwoochoi/monitor-request-handler
authorChanwoo Choi <cw00.choi@samsung.com>
Thu, 21 Jul 2022 02:50:07 +0000 (11:50 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Tue, 13 Sep 2022 23:21:27 +0000 (08:21 +0900)
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 <cw00.choi@samsung.com>
src/monitor/request-handler.c

index 324d0fc63826ce84e73598afdfc02c367ffb0438..e70db2ef29d9c1a192c5a27f6169651054abd265 100644 (file)
@@ -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:
+        *  - <RESOURCE_ID:RESOURCE_ATTR_ID>
+        * Format of REQUEST_SET_RESOURCE_ATTR and REQUEST_UNSET_RESOURCE_ATTR args:
+        *  - <RESOURCE_ID:INTEREST_MASK>
+        */
+       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:
-        *  - <RESOURCE_ID:INTEREST_MASK>
-        */
-       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:
-        *  - <RESOURCE_ID:RESOURCE_ATTR_ID>
-        */
-       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:
-        *  - <RESOURCE_ID:ATTR_ID>
-        */
-       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:
-        *  - <RESOURCE_ID:ATTR_ID>
-        */
-       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:
-        *  - <RESOURCE_ID:ATTR_ID>
-        */
-       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:
-        *  - <RESOURCE_ID:ATTR_ID>
-        */
-       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:
-        *  - <RESOURCE_ID:ATTR_ID>
-        */
-       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:
-        *  - <RESOURCE_ID:ATTR_ID>
-        */
-       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:
-        *  - <RESOURCE_ID:ATTR_ID>
-        */
-       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);