4 * Copyright (c) 2010 - 2012 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 <pims-ipc-svc.h>
24 #include <pims-ipc-data.h>
27 #include "ctsvc_internal.h"
28 #include "ctsvc_ipc_define.h"
29 #include "ctsvc_ipc_marshal.h"
30 #include "ctsvc_mutex.h"
31 #include "ctsvc_client_ipc.h"
32 #include "contacts_extension.h"
36 contacts_db_change_cb_with_info cb;
46 static int __ipc_pubsub_ref = 0;
47 static pims_ipc_h __ipc = NULL;
48 static GSList *__db_change_subscribe_list = NULL;
50 static void _ctsvc_ipc_disconnected_cb(void *user_data)
52 ctsvc_ipc_set_disconnected(true);
55 static void __ctsvc_db_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data, void *user_data)
59 subscribe_info_s *info = user_data;
62 ret = ctsvc_ipc_unmarshal_string(data, &str);
63 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_ipc_unmarshal_string() Fail(%d)", ret);
67 CTS_ERR("There is no changed data");
73 for (l = info->callbacks;l;l=l->next) {
74 db_callback_info_s *cb_info = l->data;
76 cb_info->cb(info->view_uri, str, cb_info->user_data);
84 /* This API should be called in CTS_MUTEX_PIMS_IPC_PUBSUB mutex */
85 pims_ipc_h ctsvc_ipc_get_handle_for_change_subsciption()
90 int ctsvc_ipc_create_for_change_subscription()
92 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
94 if (0 < __ipc_pubsub_ref) {
96 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
97 return CONTACTS_ERROR_NONE;
101 char sock_file[CTSVC_PATH_MAX_LEN] = {0};
102 snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/.%s_for_subscribe", getuid(), CTSVC_IPC_SERVICE);
103 __ipc = pims_ipc_create_for_subscribe(sock_file);
105 CTS_ERR("pims_ipc_create_for_subscribe error\n");
106 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
107 return CONTACTS_ERROR_IPC;
109 ctsvc_ipc_set_disconnected_cb(__ipc, _ctsvc_ipc_disconnected_cb, NULL);
112 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
113 return CONTACTS_ERROR_NONE;
116 int ctsvc_ipc_recover_for_change_subscription()
118 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
119 if (__ipc_pubsub_ref <= 0) {
120 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
121 return CONTACTS_ERROR_NONE;
124 ctsvc_ipc_unset_disconnected_cb(__ipc);
125 __ipc = pims_ipc_create_for_subscribe(CTSVC_IPC_SOCKET_PATH_FOR_CHANGE_SUBSCRIPTION);
127 CTS_ERR("pims_ipc_create_for_subscribe error\n");
128 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
129 return CONTACTS_ERROR_IPC;
131 ctsvc_ipc_set_disconnected_cb(__ipc, _ctsvc_ipc_disconnected_cb, NULL);
132 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
133 return CONTACTS_ERROR_NONE;
136 int ctsvc_ipc_destroy_for_change_subscription()
138 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
140 if (1 == __ipc_pubsub_ref) {
141 pims_ipc_destroy_for_subscribe(__ipc);
142 ctsvc_ipc_unset_disconnected_cb(__ipc);
145 else if (1 < __ipc_pubsub_ref) {
146 CTS_DBG("ctsvc pubsub ipc ref count : %d", __ipc_pubsub_ref);
149 CTS_DBG("System : please call connection APIs, connection count is (%d)", __ipc_pubsub_ref);
150 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
151 return CONTACTS_ERROR_INVALID_PARAMETER;
156 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
157 return CONTACTS_ERROR_NONE;
160 API int contacts_db_add_changed_cb_with_info(const char* view_uri,
161 contacts_db_change_cb_with_info cb, void* user_data)
164 subscribe_info_s *info = NULL;
165 db_callback_info_s *cb_info;
169 RETVM_IF(view_uri == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "view_uri is NULL");
170 RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "cb is NULL");
172 if (STRING_EQUAL == strncmp(view_uri, CTSVC_VIEW_URI_PHONELOG, strlen(CTSVC_VIEW_URI_PHONELOG))) {
173 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_PHONELOG_READ, &result);
174 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
175 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (phonelog read)");
177 else if (STRING_EQUAL == strncmp(view_uri, CTSVC_VIEW_URI_PERSON, strlen(CTSVC_VIEW_URI_PERSON))) {
178 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_CONTACT_READ, &result);
179 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
180 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (contact read)");
183 CTS_ERR("We support this API for only %s and %s", CTSVC_VIEW_URI_PERSON, CTSVC_VIEW_URI_PHONELOG);
184 return CONTACTS_ERROR_INVALID_PARAMETER;
187 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
189 for (it=__db_change_subscribe_list;it;it=it->next) {
190 if (NULL == it->data) continue;
193 if (STRING_EQUAL == strcmp(info->view_uri, view_uri))
200 info = calloc(1, sizeof(subscribe_info_s));
202 CTS_ERR("calloc() Fail");
203 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
204 return CONTACTS_ERROR_OUT_OF_MEMORY;
207 if (pims_ipc_subscribe(__ipc, CTSVC_IPC_SUBSCRIBE_MODULE, (char*)view_uri,
208 __ctsvc_db_subscriber_callback, (void*)info) != 0) {
209 CTS_ERR("pims_ipc_subscribe error\n");
211 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
212 return CONTACTS_ERROR_IPC;
214 info->view_uri = strdup(view_uri);
215 __db_change_subscribe_list = g_slist_append(__db_change_subscribe_list, info);
219 for (l = info->callbacks;l;l=l->next) {
220 db_callback_info_s *cb_info = l->data;
221 if (cb_info->cb == cb && cb_info->user_data == user_data) {
222 CTS_ERR("The same callback(%s) is already exist", view_uri);
223 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
224 return CONTACTS_ERROR_INVALID_PARAMETER;
229 cb_info = calloc(1, sizeof(db_callback_info_s));
230 if (NULL == cb_info) {
231 CTS_ERR("calloc() Failed");
232 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
233 return CONTACTS_ERROR_OUT_OF_MEMORY;
235 cb_info->user_data = user_data;
237 info->callbacks = g_slist_append(info->callbacks, cb_info);
239 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
240 return CONTACTS_ERROR_NONE;
243 API int contacts_db_remove_changed_cb_with_info(const char* view_uri,
244 contacts_db_change_cb_with_info cb, void* user_data)
247 subscribe_info_s *info = NULL;
249 RETVM_IF(view_uri == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "view_uri is NULL");
250 RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "cb is NULL");
252 if (STRING_EQUAL != strcmp(view_uri, CTSVC_VIEW_URI_PHONELOG) &&
253 STRING_EQUAL != strcmp(view_uri, CTSVC_VIEW_URI_PERSON)) {
254 CTS_ERR("We support this API for only %s and %s", CTSVC_VIEW_URI_PERSON, CTSVC_VIEW_URI_PHONELOG);
255 return CONTACTS_ERROR_INVALID_PARAMETER;
258 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
260 for (it=__db_change_subscribe_list;it;it=it->next) {
261 if (NULL == it->data) continue;
264 if (STRING_EQUAL == strcmp(info->view_uri, view_uri))
272 for (l = info->callbacks;l;l=l->next) {
273 db_callback_info_s *cb_info = l->data;
274 if (cb == cb_info->cb && user_data == cb_info->user_data) {
275 info->callbacks = g_slist_remove(info->callbacks, cb_info);
280 if (g_slist_length(info->callbacks) == 0) {
281 pims_ipc_unsubscribe(__ipc, CTSVC_IPC_SUBSCRIBE_MODULE, info->view_uri);
282 __db_change_subscribe_list = g_slist_remove(__db_change_subscribe_list, info);
283 free(info->view_uri);
288 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
289 return CONTACTS_ERROR_NONE;