lib: tmonitor: tmonitor dev
authorSung-hun Kim <sfoon.kim@samsung.com>
Thu, 10 Feb 2022 04:25:43 +0000 (13:25 +0900)
committerSung-hun Kim <sfoon.kim@samsung.com>
Mon, 21 Feb 2022 00:45:52 +0000 (09:45 +0900)
Change-Id: Ie1c51cfbc64db78747c0f3f19d8a72c1093dbe26
Signed-off-by: Sung-hun Kim <sfoon.kim@samsung.com>
lib/tmonitor/tmonitor.c

index f8eb79076c69d99975788c473193be3107422162..d3bea3d9f9905e9d4ba5f214497801696fdc5211 100644 (file)
@@ -45,7 +45,6 @@ extern char *program_invocation_name;
 struct tmonitor_interest {
        int resource_type;
        u_int64_t attrs;
-       struct tmonitor_interest *next;
 };
 
 struct tmonitor_client {
@@ -54,10 +53,10 @@ struct tmonitor_client {
        int (*func)(void *data, void *user_data);
        void *user_data;
        int socket_fd;
-       struct tmonitor_interest *interest;
+       //struct tmonitor_interest *interest;
+       GList *g_interest_head;
 };
 
-static struct tmonitor_client *client_head;
 static GList *g_tmonitor_client_head;
 
 void add_client_to_list(struct tmonitor_client *client)
@@ -117,7 +116,7 @@ int tmonitor_init(int period, int (*func)(void *data, void* user_data), void *us
        client->period = period;
        client->func = func;
        client->user_data = user_data;
-       client->interest = NULL;
+       client->g_interest_head = NULL;
 
        /* open socket to server */
        client->socket_fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -155,13 +154,14 @@ int tmonitor_init(int period, int (*func)(void *data, void* user_data), void *us
        }
 
        buffer[buffer_len] = '\0';
-
        tok = strtok(buffer, ":");
+
        response_type = atoi(tok);
        if (response_type != REQUEST_INIT) {
                _E("[libpass] invalid response");
                return -EINVAL;
        }
+
        tok = strtok(NULL, ":");
        client->id = atoi(tok);
        add_client_to_list(client);
@@ -169,14 +169,38 @@ int tmonitor_init(int period, int (*func)(void *data, void* user_data), void *us
        return client->id;
 }
 
-static void release_interest_list(struct tmonitor_interest *interest)
+static void add_interest_to_list(struct tmonitor_interest *interest, GList **list)
+{
+       if (!interest || !list)
+               return;
+
+       *list = g_list_append(*list, (gpointer)interest);
+}
+
+static void remove_interest_from_list(struct tmonitor_interest *interest, GList **list)
+{
+       if (!interest || !list)
+               return;
+
+       *list = g_list_remove(*list, (gpointer)interest);
+}
+
+static void release_interest_list(GList **interest_head)
 {
-       struct tmonitor_interest *next;
+       struct tmonitor_interest *interest;
+       GList *node;
+       GList *next;
 
-       while (interest) {
-               next = interest->next;
+       if (!interest_head || !*interest_head)
+               return;
+
+       node = *interest_head;
+       while (node != NULL) {
+               next = node->next;
+               interest = node->data;
+               remove_interest_from_list(interest, interest_head);
                free(interest);
-               interest = next;
+               node = next;
        }
 }
 
@@ -186,6 +210,9 @@ void tmonitor_exit(int id)
        char buffer[100];
        int buffer_len;
        struct tmonitor_client *client;
+       char *tok;
+       int response_id;
+       int response_req;
 
        client = find_client_by_id(id);
        if (!client) {
@@ -194,8 +221,8 @@ void tmonitor_exit(int id)
        }
 
        /* finalize interest list */
-       if (client->interest)
-               release_interest_list(client->interest);
+       if (client->g_interest_head)
+               release_interest_list(&client->g_interest_head);
 
        buffer_len = sprintf(buffer, "%d:%d", REQUEST_EXIT, id);
        if (send(client->socket_fd, buffer, buffer_len, 0) < 0) {
@@ -205,6 +232,30 @@ void tmonitor_exit(int id)
 
        _I("[libpass] tmonitor_exit called for client-%d", id);
 
+       /* wait for response */
+
+       buffer_len = recv(client->socket_fd, buffer, 100, 0);
+       if (buffer_len <= 0) {
+               _E("[libpass] socket disconnected");
+               return;
+       }
+
+       buffer[buffer_len] = '\0';
+       tok = strtok(buffer, ":");
+       response_req = atoi(tok);
+       tok = strtok(NULL, ":");
+       response_id = atoi(tok);
+
+       if (response_req != REQUEST_EXIT) {
+               _E("wrong response");
+               return -1;
+       }
+
+       if (response_id != id) {
+               _E("wrong id");
+               return -1;
+       }
+
        close(client->socket_fd);
        remove_client_from_list(client);
        free(client);
@@ -213,7 +264,58 @@ void tmonitor_exit(int id)
 EXPORT
 int tmonitor_set_attrs(int id, int resource_type, u_int64_t attr_mask)
 {
-       /* TODO */
+       char buffer[100];
+       int buffer_len;
+       struct tmonitor_client *client;
+       struct tmonitor_interest *interest;
+       char *tok;
+       int response_id;
+       int response_req;
+
+       client = find_client_by_id(id);
+       if (!client) {
+               _E("[libpass] cannot find client-%d", id);
+               return -EINVAL;
+       }
+
+       interest = malloc(sizeof(struct tmonitor_interest));
+       interest->resource_type = resource_type;
+       interest->attrs = attr_mask;
+       add_interest_to_list(interest, &client->g_interest_head);
+
+       buffer_len = sprintf(buffer, "%d:%d:%d:%lx", REQUEST_SET_ATTRS, id, resource_type, attr_mask);
+       if (send(client->socket_fd, buffer, buffer_len, 0) < 0) {
+               _E("[libpass] error occurred while sending buffer");
+               return -EIO;
+       }
+
+       _I("[libpass] send \"%s\" to server", buffer);
+
+       /* wait for response */
+
+       buffer_len = recv(client->socket_fd, buffer, 100, 0);
+       if (buffer_len <= 0) {
+               _E("[libpass] socket disconnected");
+               return -EIO;
+       }
+
+       buffer[buffer_len] = '\0';
+       tok = strtok(buffer, ":");
+       response_req = atoi(tok);
+       tok = strtok(NULL, ":");
+       response_id = atoi(tok);
+
+       if (response_req != REQUEST_SET_ATTRS) {
+               _E("wrong response");
+               return -1;
+       }
+
+       if (response_id != id) {
+               _E("wrong id");
+               return -1;
+       }
+
+       _I("[libpass] tmonitor_set_attrs called for client-%d", id);
        return 0;
 }
 
@@ -229,6 +331,23 @@ EXPORT
 int tmonitor_start(int id)
 {
        /* TODO */
+       char buffer[100];
+       int buffer_len;
+       struct tmonitor_client *client;
+       struct tmonitor_interest *interest;
+
+       client = find_client_by_id(id);
+       if (!client) {
+               _E("[libpass] cannot find client-%d", id);
+               return -EINVAL;
+       }
+
+       buffer_len = sprintf(buffer, "%d:%d", REQUEST_START, id);
+       if (send(client->socket_fd, buffer, buffer_len, 0) < 0) {
+               _E("[libpass] error occurred while sending buffer");
+               return -EIO;
+       }
+
        return 0;
 }