4 * Copyright (c) 2012 - 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.
22 #include <sys/types.h>
26 #include "cal_internal.h"
27 #include "cal_typedef.h"
28 #include "cal_mutex.h"
29 #include "cal_client_handle.h"
30 #include "cal_client_utils.h"
32 static GHashTable *_cal_handle_table = NULL;
34 static void _foreach_cb(gpointer key, gpointer value, gpointer user_data)
36 DBG("[hash check]--------- key[%s] value[%p]", key, value);
38 static void _print_hash(GHashTable *table)
41 g_hash_table_foreach(table, _foreach_cb, NULL);
44 static int _cal_client_handle_get_key(unsigned int id, char *key, int key_len)
46 RETV_IF(NULL == key, CALENDAR_ERROR_INVALID_PARAMETER);
48 snprintf(key, key_len, "%d:%u", getuid(), id);
50 return CALENDAR_ERROR_NONE;
53 int cal_client_handle_get_p(calendar_h *out_handle)
57 RETVM_IF(NULL == _cal_handle_table, CALENDAR_ERROR_NO_DATA, "No handle table");
58 RETV_IF(NULL == out_handle, CALENDAR_ERROR_INVALID_PARAMETER);
60 unsigned int tid = cal_client_get_tid();
61 unsigned int pid = cal_client_get_pid();
63 char key[CAL_STR_MIDDLE_LEN] = {0};
64 ret = _cal_client_handle_get_key(tid, key, sizeof(key));
65 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_client_handle_get_key() Fail(%d)", ret);
67 calendar_h handle = NULL;
68 cal_mutex_lock(CAL_MUTEX_HANDLE);
69 handle = g_hash_table_lookup(_cal_handle_table, key);
70 cal_mutex_unlock(CAL_MUTEX_HANDLE);
72 if (NULL == handle && tid != pid) {
73 ret = _cal_client_handle_get_key(pid, key, sizeof(key));
74 if (CALENDAR_ERROR_NONE != ret) {
75 ERR("_cal_client_handle_get_key() Fail(%d):No handle(key[%s])", ret, key);
76 _print_hash(_cal_handle_table);
80 cal_mutex_lock(CAL_MUTEX_HANDLE);
81 handle = g_hash_table_lookup(_cal_handle_table, key);
82 cal_mutex_unlock(CAL_MUTEX_HANDLE);
84 ERR("g_hash_table_lookup() Fail:No handle(key[%s])", key);
85 _print_hash(_cal_handle_table);
86 return CALENDAR_ERROR_NO_DATA;
90 return CALENDAR_ERROR_NONE;
93 int cal_client_handle_get_p_with_id(unsigned int id, calendar_h *out_handle)
97 RETVM_IF(NULL == _cal_handle_table, CALENDAR_ERROR_NO_DATA, "No handle table");
98 RETV_IF(NULL == out_handle, CALENDAR_ERROR_INVALID_PARAMETER);
100 char key[CAL_STR_MIDDLE_LEN] = {0};
101 ret = _cal_client_handle_get_key(id, key, sizeof(key));
102 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_client_handle_get_key_with_id() Fail(%d)", ret);
104 calendar_h handle = NULL;
105 cal_mutex_lock(CAL_MUTEX_HANDLE);
106 handle = g_hash_table_lookup(_cal_handle_table, key);
107 cal_mutex_unlock(CAL_MUTEX_HANDLE);
108 if (NULL == handle) {
109 ERR("g_hash_table_lookup() Fail:No handle:key[%s]", key);
110 _print_hash(_cal_handle_table);
111 return CALENDAR_ERROR_NO_DATA;
113 *out_handle = handle;
114 return CALENDAR_ERROR_NONE;
117 static int _cal_client_handle_add(calendar_h handle, const char *key)
119 RETV_IF(NULL == key, CALENDAR_ERROR_INVALID_PARAMETER);
120 cal_mutex_lock(CAL_MUTEX_HANDLE);
122 if (NULL == _cal_handle_table)
123 _cal_handle_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
125 g_hash_table_insert(_cal_handle_table, strdup(key), handle);
126 cal_mutex_unlock(CAL_MUTEX_HANDLE);
127 DBG("[HASH:handle] insert key[%s] value[%p]", key, handle);
128 return CALENDAR_ERROR_NONE;
131 int cal_client_handle_create(unsigned int id, calendar_h *out_handle)
134 calendar_h handle = NULL;
136 RETV_IF(NULL == out_handle, CALENDAR_ERROR_NONE);
138 char key[CAL_STR_MIDDLE_LEN] = {0};
139 ret = _cal_client_handle_get_key(id, key, sizeof(key));
140 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_client_handle_get_key() Fail(%d)", ret);
142 ret = cal_handle_create(&handle);
143 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_handle_create() Fail(%d)", ret);
145 ret = _cal_client_handle_add(handle, key);
146 if (CALENDAR_ERROR_NONE != ret) {
147 ERR("_cal_client_handle_add() Fail(%d)", ret);
148 cal_handle_destroy(handle);
152 *out_handle = handle;
153 return CALENDAR_ERROR_NONE;
156 int cal_client_handle_remove(unsigned int id, calendar_h handle)
159 char key[CAL_STR_MIDDLE_LEN] = {0};
161 RETVM_IF(NULL == _cal_handle_table, CALENDAR_ERROR_NONE, "No handle table");
163 ret = _cal_client_handle_get_key(id, key, sizeof(key));
164 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_client_handle_get_key() Fail(%d)", ret);
166 cal_mutex_lock(CAL_MUTEX_HANDLE);
167 g_hash_table_remove(_cal_handle_table, key);
168 DBG("[HASH:handle] remove [%s]", key);
169 if (0 == g_hash_table_size(_cal_handle_table)) {
170 g_hash_table_destroy(_cal_handle_table);
171 _cal_handle_table = NULL;
173 cal_mutex_unlock(CAL_MUTEX_HANDLE);
174 cal_handle_destroy(handle);
175 return CALENDAR_ERROR_NONE;
178 void cal_client_handle_set_version(calendar_h handle, int version)
180 RET_IF(NULL == handle);
181 cal_s *h = (cal_s *)handle;
182 h->version = version;