change key and add log for handle
[platform/core/pim/contacts-service.git] / client / ctsvc_client_handle.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <glib.h>
21 #include <unistd.h>
22
23 #include "ctsvc_internal.h"
24 #include "ctsvc_handle.h"
25 #include "ctsvc_mutex.h"
26 #include "ctsvc_client_utils.h"
27 #include "ctsvc_client_handle.h"
28
29 static GHashTable *_ctsvc_handle_table = NULL;
30
31 static int _ctsvc_client_handle_get_key(unsigned int id, char *key, int key_len)
32 {
33         RETV_IF(NULL == key, CONTACTS_ERROR_INVALID_PARAMETER);
34
35         snprintf(key, key_len, "%d:%u", getuid(), id);
36         CTS_DBG("key[%s]", key);
37         return CONTACTS_ERROR_NONE;
38 }
39
40 int ctsvc_client_handle_get_p_with_id(unsigned int id, contacts_h *p_contact)
41 {
42         int ret;
43         char key[CTSVC_STR_SHORT_LEN] = {0};
44         contacts_h contact = NULL;
45
46         RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NO_DATA, "_ctsvc_handle_table is NULL");
47         RETV_IF(NULL == p_contact, CONTACTS_ERROR_INVALID_PARAMETER);
48
49         ret = _ctsvc_client_handle_get_key(id, key, sizeof(key));
50         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
51
52         ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
53         contact = g_hash_table_lookup(_ctsvc_handle_table, key);
54         ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
55
56         if (NULL == contact) {
57                 CTS_ERR("g_hash_table_lookup() Fail. key[%s]", key);
58                 return CONTACTS_ERROR_NO_DATA;
59         }
60
61         *p_contact = contact;
62         return CONTACTS_ERROR_NONE;
63 }
64
65 int ctsvc_client_handle_get_p(contacts_h *p_contact)
66 {
67         int ret;
68         char key[CTSVC_STR_SHORT_LEN] = {0};
69         contacts_h contact = NULL;
70
71         RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NO_DATA, "_ctsvc_handle_table is NULL");
72         RETV_IF(NULL == p_contact, CONTACTS_ERROR_INVALID_PARAMETER);
73
74         ret = _ctsvc_client_handle_get_key(ctsvc_client_get_tid(), key, sizeof(key));
75         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
76
77         ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
78         contact = g_hash_table_lookup(_ctsvc_handle_table, key);
79         ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
80
81         if (NULL == contact) {
82                 ret = _ctsvc_client_handle_get_key(ctsvc_client_get_pid(), key, sizeof(key));
83                 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
84
85                 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
86                 contact = g_hash_table_lookup(_ctsvc_handle_table, key);
87                 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
88
89                 RETVM_IF(NULL == contact, CONTACTS_ERROR_NO_DATA, "g_hash_table_lookup() Fail. key[%s]", key);
90         }
91         *p_contact = contact;
92         return CONTACTS_ERROR_NONE;
93 }
94
95 int ctsvc_client_handle_create(unsigned int id, contacts_h *p_contact)
96 {
97         int ret;
98         char handle_key[CTSVC_STR_SHORT_LEN] = {0};
99         contacts_h contact = NULL;
100
101         RETV_IF(NULL == p_contact, CONTACTS_ERROR_INVALID_PARAMETER);
102         *p_contact = NULL;
103
104         ret = _ctsvc_client_handle_get_key(id, handle_key, sizeof(handle_key));
105         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
106
107         ret = ctsvc_handle_create(&contact);
108         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_handle_create() Fail(%d)", ret);
109
110         ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
111         if (NULL == _ctsvc_handle_table)
112                 _ctsvc_handle_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
113
114         g_hash_table_insert(_ctsvc_handle_table, strdup(handle_key), contact);
115         CTS_INFO("handle insert key[%s]", handle_key);
116         ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
117
118         *p_contact = contact;
119         return CONTACTS_ERROR_NONE;
120 }
121
122 int ctsvc_client_handle_remove(unsigned int id, contacts_h contact)
123 {
124         int ret;
125         char key[CTSVC_STR_SHORT_LEN] = {0};
126         RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NONE, "_ctsvc_handle_table is NULL");
127
128         ret = _ctsvc_client_handle_get_key(id, key, sizeof(key));
129         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
130
131         ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
132         if (false == g_hash_table_remove(_ctsvc_handle_table, key))
133                 CTS_ERR("g_hash_table_remove() Fail. key[%s]", key);
134
135         if (0 == g_hash_table_size(_ctsvc_handle_table)) {
136                 g_hash_table_destroy(_ctsvc_handle_table);
137                 _ctsvc_handle_table = NULL;
138         }
139         ctsvc_handle_destroy(contact);
140         ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
141
142         return CONTACTS_ERROR_NONE;
143 }
144
145