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_db_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data, void *user_data)
54 subscribe_info_s *info = user_data;
57 ret = ctsvc_ipc_unmarshal_string(data, &str);
58 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_ipc_unmarshal_string() Fail(%d)", ret);
62 CTS_ERR("There is no changed data");
68 for (l = info->callbacks;l;l=l->next) {
69 db_callback_info_s *cb_info = l->data;
71 cb_info->cb(info->view_uri, str, cb_info->user_data);
79 /* This API should be called in CTS_MUTEX_PIMS_IPC_PUBSUB mutex */
80 pims_ipc_h ctsvc_ipc_get_handle_for_change_subsciption()
85 int ctsvc_ipc_create_for_change_subscription()
87 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
89 if (0 < __ipc_pubsub_ref) {
91 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
92 return CONTACTS_ERROR_NONE;
96 char sock_file[CTSVC_PATH_MAX_LEN] = {0};
97 snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/.%s_for_subscribe", getuid(), CTSVC_IPC_SERVICE);
98 __ipc = pims_ipc_create_for_subscribe(sock_file);
100 CTS_ERR("pims_ipc_create_for_subscribe error\n");
101 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
102 return CONTACTS_ERROR_IPC;
106 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
107 return CONTACTS_ERROR_NONE;
110 int ctsvc_ipc_recover_for_change_subscription()
112 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
113 if (__ipc_pubsub_ref <= 0) {
114 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
115 return CONTACTS_ERROR_NONE;
118 ctsvc_ipc_unset_disconnected_cb(__ipc);
119 __ipc = pims_ipc_create_for_subscribe(CTSVC_IPC_SOCKET_PATH_FOR_CHANGE_SUBSCRIPTION);
121 CTS_ERR("pims_ipc_create_for_subscribe error\n");
122 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
123 return CONTACTS_ERROR_IPC;
125 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
126 return CONTACTS_ERROR_NONE;
129 int ctsvc_ipc_destroy_for_change_subscription()
131 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
133 if (1 == __ipc_pubsub_ref) {
134 ctsvc_ipc_unset_disconnected_cb(__ipc);
135 pims_ipc_destroy_for_subscribe(__ipc);
138 else if (1 < __ipc_pubsub_ref) {
139 CTS_DBG("ctsvc pubsub ipc ref count : %d", __ipc_pubsub_ref);
142 CTS_DBG("System : please call connection APIs, connection count is (%d)", __ipc_pubsub_ref);
143 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
144 return CONTACTS_ERROR_INVALID_PARAMETER;
149 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
150 return CONTACTS_ERROR_NONE;
153 API int contacts_db_add_changed_cb_with_info(const char* view_uri,
154 contacts_db_change_cb_with_info cb, void* user_data)
157 subscribe_info_s *info = NULL;
158 db_callback_info_s *cb_info;
162 RETVM_IF(view_uri == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "view_uri is NULL");
163 RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "cb is NULL");
165 if (STRING_EQUAL == strncmp(view_uri, CTSVC_VIEW_URI_PHONELOG, strlen(CTSVC_VIEW_URI_PHONELOG))) {
166 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_PHONELOG_READ, &result);
167 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
168 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (phonelog read)");
170 else if (STRING_EQUAL == strncmp(view_uri, CTSVC_VIEW_URI_PERSON, strlen(CTSVC_VIEW_URI_PERSON))) {
171 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_CONTACT_READ, &result);
172 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
173 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (contact read)");
176 CTS_ERR("We support this API for only %s and %s", CTSVC_VIEW_URI_PERSON, CTSVC_VIEW_URI_PHONELOG);
177 return CONTACTS_ERROR_INVALID_PARAMETER;
180 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
182 for (it=__db_change_subscribe_list;it;it=it->next) {
183 if (NULL == it->data) continue;
186 if (STRING_EQUAL == strcmp(info->view_uri, view_uri))
193 info = calloc(1, sizeof(subscribe_info_s));
195 CTS_ERR("calloc() Fail");
196 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
197 return CONTACTS_ERROR_OUT_OF_MEMORY;
200 if (pims_ipc_subscribe(__ipc, CTSVC_IPC_SUBSCRIBE_MODULE, (char*)view_uri,
201 __ctsvc_db_subscriber_callback, (void*)info) != 0) {
202 CTS_ERR("pims_ipc_subscribe error\n");
204 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
205 return CONTACTS_ERROR_IPC;
207 info->view_uri = strdup(view_uri);
208 __db_change_subscribe_list = g_slist_append(__db_change_subscribe_list, info);
212 for (l = info->callbacks;l;l=l->next) {
213 db_callback_info_s *cb_info = l->data;
214 if (cb_info->cb == cb && cb_info->user_data == user_data) {
215 CTS_ERR("The same callback(%s) is already exist", view_uri);
216 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
217 return CONTACTS_ERROR_INVALID_PARAMETER;
222 cb_info = calloc(1, sizeof(db_callback_info_s));
223 if (NULL == cb_info) {
224 CTS_ERR("calloc() Failed");
225 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
226 return CONTACTS_ERROR_OUT_OF_MEMORY;
228 cb_info->user_data = user_data;
230 info->callbacks = g_slist_append(info->callbacks, cb_info);
232 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
233 return CONTACTS_ERROR_NONE;
236 API int contacts_db_remove_changed_cb_with_info(const char* view_uri,
237 contacts_db_change_cb_with_info cb, void* user_data)
240 subscribe_info_s *info = NULL;
242 RETVM_IF(view_uri == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "view_uri is NULL");
243 RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "cb is NULL");
245 if (STRING_EQUAL != strcmp(view_uri, CTSVC_VIEW_URI_PHONELOG) &&
246 STRING_EQUAL != strcmp(view_uri, CTSVC_VIEW_URI_PERSON)) {
247 CTS_ERR("We support this API for only %s and %s", CTSVC_VIEW_URI_PERSON, CTSVC_VIEW_URI_PHONELOG);
248 return CONTACTS_ERROR_INVALID_PARAMETER;
251 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
253 for (it=__db_change_subscribe_list;it;it=it->next) {
254 if (NULL == it->data) continue;
257 if (STRING_EQUAL == strcmp(info->view_uri, view_uri))
265 for (l = info->callbacks;l;l=l->next) {
266 db_callback_info_s *cb_info = l->data;
267 if (cb == cb_info->cb && user_data == cb_info->user_data) {
268 info->callbacks = g_slist_remove(info->callbacks, cb_info);
273 if (g_slist_length(info->callbacks) == 0) {
274 pims_ipc_unsubscribe(__ipc, CTSVC_IPC_SUBSCRIBE_MODULE, info->view_uri);
275 __db_change_subscribe_list = g_slist_remove(__db_change_subscribe_list, info);
276 free(info->view_uri);
281 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
282 return CONTACTS_ERROR_NONE;