mmi-client: add basic client manager implementation 16/264016/1
authorSung-Jin Park <sj76.park@samsung.com>
Sat, 17 Jul 2021 16:31:09 +0000 (01:31 +0900)
committerSung-Jin Park <sj76.park@samsung.com>
Mon, 13 Sep 2021 11:23:50 +0000 (20:23 +0900)
Change-Id: I9404b583daa444417edb5414d518f0b45bdccb55
Signed-off-by: Sung-Jin Park <sj76.park@samsung.com>
src/mmi-client.c
src/mmi-client.h

index 15bbbf4..6edc310 100644 (file)
 */
 
 #include "mmi-client.h"
+#include "mmi-common.h"
+#include "mmi-manager-dbg.h"
+#include "interface/mmifw_stub.h"
+
+#include <Eina.h>
+#include <stdlib.h>
+
+struct _mmi_client
+{
+        char *sender;
+        uid_t uid;
+        pid_t pid;
+        mmi_state state;
+};
+
+Eina_Hash *_client_hash = NULL;
+static Eina_Bool _init_done = false;
+
+static Eina_Bool
+_client_hash_free_cb(const Eina_Hash *hash, const void *key, void *data, void *fdata)
+{
+       (void) hash;
+       (void) key;
+       (void) fdata;
+
+       mmi_client *mc = (mmi_client *)data;
+       if (mc && mc->sender)
+               free(mc->sender);
+
+       return 1;
+}
+
+mmi_client *
+client_manager_add_client(const char* sender)
+{
+       mmi_client *mc = NULL;
+
+       if (!sender)
+               return NULL;
+
+       mc = calloc(1, sizeof(mmi_client));
+        if (!mc)
+        {
+                LOGE("Failed to allocate memory for client !\n");
+                return NULL;
+        }
+
+       mc->sender = strdup(sender);
+
+       if (!eina_hash_add(_client_hash, sender, mc))
+        {
+                LOGE("Failed to add client info to client hash !\n");
+                goto err;
+        }
+
+       return mc;
+err:
+       if (mc)
+               free(mc);
+       return NULL;
+}
+
+mmi_client *
+client_manager_get_client(const char* sender)
+{
+       mmi_client *mc = NULL;
+
+       if (!sender)
+               return NULL;
+
+       mc = eina_hash_find(_client_hash, sender);
+
+       if (!mc)
+       {
+               LOGE("Failed to find client info from sender(=%s)\n", sender);
+               return NULL;
+       }
+
+       return mc;
+}
+
+int
+client_manager_remove_client(const char *sender)
+{
+       mmi_client *mc = NULL;
+
+       if (!sender)
+               return -1;
+
+       mc = eina_hash_find(_client_hash, sender);
+       if (!mc)
+       {
+               LOGE("Failed to find client info from sender(=%s)\n", sender);
+               goto err;
+       }
+
+
+       if (!eina_hash_del(_client_hash, sender, mc))
+       {
+               LOGE("Failed to remove client info using sender(=%s) !\n", sender);
+               goto err;
+       }
+
+       if (mc->sender)
+               free(mc->sender);
+       free(mc);
+       mc = NULL;
+
+       return 0;
+err:
+       if (mc && mc->sender)
+               free(mc->sender);
+       if (mc)
+               free(mc);
+       mc = NULL;
+       return -1;
+}
+
+int
+client_manager_set_client_uid(mmi_client *client, uid_t uid)
+{
+       if (!client)
+               return -1;
+
+       client->uid = uid;
+       return uid;
+}
+
+int
+client_manager_get_client_uid(mmi_client *client)
+{
+       if (!client)
+               return -1;
+
+       return client->uid;
+}
+
+int
+client_manager_set_client_pid(mmi_client *client, pid_t pid)
+{
+       if (!client)
+               return -1;
+
+       client->pid = pid;
+       return pid;
+}
+
+int
+client_manager_get_client_pid(mmi_client *client)
+{
+       if (!client)
+               return -1;
+
+       return client->pid;
+}
+
+mmi_state
+client_manager_set_client_state(mmi_client *client, mmi_state state)
+{
+       if (!client)
+               return MMI_STATE_NONE;
+
+       client->state = state;
+       return state;
+}
+
+mmi_state
+client_manager_get_client_state(mmi_client *client)
+{
+       if (!client)
+               return MMI_STATE_NONE;
+
+       return client->state;
+}
+
+static void
+_cleanup_client_hash(void)
+{
+       if (!_client_hash)
+               return;
+
+       eina_hash_foreach(_client_hash, _client_hash_free_cb, NULL);
+       eina_hash_free(_client_hash);
+       _client_hash = NULL;
+}
 
 void
 client_manager_init(void)
 {
+       if (_init_done)
+               return;
+
+       if (_client_hash)
+               _cleanup_client_hash();
+
+       _init_done = true;
 }
 
 void
 client_manager_shutdown(void)
 {
+       if (!_init_done)
+               return;
+
+       if (_client_hash)
+               _cleanup_client_hash();
+
+       _init_done = false;
 }
index 76e180a..ca0755e 100644 (file)
 #ifndef __MMI_CLIENT_H__
 #define __MMI_CLIENT_H__
 
+#include "mmi-common.h"
+#include <sys/types.h>
+
+typedef struct _mmi_client mmi_client;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+mmi_client *client_manager_add_client(const char* sender);
+mmi_client *client_manager_get_client(const char* sender);
+int client_manager_remove_client(const char *sender);
+int client_manager_set_client_uid(mmi_client *client, uid_t uid);
+int client_manager_get_client_uid(mmi_client *client);
+int client_manager_set_client_pid(mmi_client *client, pid_t pid);
+int client_manager_get_client_pid(mmi_client *client);
+mmi_state client_manager_set_client_state(mmi_client *client, mmi_state state);
+mmi_state client_manager_get_client_state(mmi_client *client);
+
 void client_manager_init(void);
 void client_manager_shutdown(void);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif //__MMI_CLIENT_H__