4 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
29 static GHashTable *_ctsvc_handle_table = NULL;
31 static int _client_handle_get_key(unsigned int id, char *key, int key_len)
33 RETV_IF(NULL == key, CONTACTS_ERROR_INVALID_PARAMETER);
35 snprintf(key, key_len, "%d:%u", getuid(), id);
37 return CONTACTS_ERROR_NONE;
40 int ctsvc_client_handle_get_p_with_id(unsigned int id, contacts_h *p_contact)
43 char key[CTSVC_STR_SHORT_LEN] = {0};
44 contacts_h contact = NULL;
46 RETV_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NO_DATA);
47 RETV_IF(NULL == p_contact, CONTACTS_ERROR_INVALID_PARAMETER);
49 ret = _client_handle_get_key(id, key, sizeof(key));
50 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_client_handle_get_key() Fail(%d)", ret);
52 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
53 contact = g_hash_table_lookup(_ctsvc_handle_table, key);
54 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
56 if (NULL == contact) {
58 ERR("g_hash_table_lookup() Fail. key[%s]", key);
59 return CONTACTS_ERROR_NO_DATA;
64 return CONTACTS_ERROR_NONE;
67 int ctsvc_client_handle_get_p(contacts_h *p_contact)
70 char key[CTSVC_STR_SHORT_LEN] = {0};
71 contacts_h contact = NULL;
73 RETV_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NO_DATA);
74 RETV_IF(NULL == p_contact, CONTACTS_ERROR_INVALID_PARAMETER);
76 ret = _client_handle_get_key(ctsvc_client_get_tid(), key, sizeof(key));
77 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_client_handle_get_key() Fail(%d)", ret);
79 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
80 contact = g_hash_table_lookup(_ctsvc_handle_table, key);
81 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
83 if (NULL == contact) {
84 ret = _client_handle_get_key(ctsvc_client_get_pid(), key, sizeof(key));
85 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_client_handle_get_key() Fail(%d)", ret);
87 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
88 contact = g_hash_table_lookup(_ctsvc_handle_table, key);
89 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
90 RETVM_IF(NULL == contact, CONTACTS_ERROR_NO_DATA, "g_hash_table_lookup(%s) Fail", key);
93 return CONTACTS_ERROR_NONE;
96 int ctsvc_client_handle_create(unsigned int id, contacts_h *p_contact)
99 char handle_key[CTSVC_STR_SHORT_LEN] = {0};
100 contacts_h contact = NULL;
102 RETV_IF(NULL == p_contact, CONTACTS_ERROR_INVALID_PARAMETER);
105 ret = _client_handle_get_key(id, handle_key, sizeof(handle_key));
106 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_client_handle_get_key() Fail(%d)", ret);
108 ret = ctsvc_handle_create(&contact);
109 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_handle_create() Fail(%d)", ret);
111 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
112 if (NULL == _ctsvc_handle_table)
113 _ctsvc_handle_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
115 g_hash_table_insert(_ctsvc_handle_table, strdup(handle_key), contact);
116 INFO("handle insert key[%s]", handle_key);
117 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
119 *p_contact = contact;
120 return CONTACTS_ERROR_NONE;
123 int ctsvc_client_handle_remove(unsigned int id, contacts_h contact)
126 char key[CTSVC_STR_SHORT_LEN] = {0};
128 RETV_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NONE);
130 ret = _client_handle_get_key(id, key, sizeof(key));
131 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_client_handle_get_key() Fail(%d)", ret);
133 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
134 if (false == g_hash_table_remove(_ctsvc_handle_table, key))
135 ERR("g_hash_table_remove() Fail. key[%s]", key);
137 if (0 == g_hash_table_size(_ctsvc_handle_table)) {
138 g_hash_table_destroy(_ctsvc_handle_table);
139 _ctsvc_handle_table = NULL;
141 ctsvc_handle_destroy(contact);
142 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
144 return CONTACTS_ERROR_NONE;