4 * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Dohyung Jin <dh.jin@samsung.com>
7 * Jongwon Lee <gogosing.lee@samsung.com>
8 * Donghee Ye <donghee.ye@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 #include <sys/socket.h>
27 #include <pims-ipc-data.h>
30 #include "ctsvc_internal.h"
31 #include "ctsvc_socket.h"
32 #include "ctsvc_mutex.h"
33 #include "ctsvc_inotify.h"
34 #include "ctsvc_setting.h"
35 #include "ctsvc_client_ipc.h"
37 static int ctsvc_connection = 0;
39 static int __thread ctsvc_connection_on_thread = 0;
41 API int contacts_connect_with_flags(unsigned int flags)
44 int ret = CONTACTS_ERROR_NONE;
46 ret = contacts_connect2();
47 if (ret == CONTACTS_ERROR_NONE)
50 if (flags & CONTACTS_CONNECT_FLAG_RETRY) {
52 int waiting_time = 500;
54 usleep(waiting_time * 1000);
55 DBG("retry cnt=%d, ret=%x, %d",(i+1), ret, waiting_time);
56 ret = contacts_connect2();
57 if (ret == CONTACTS_ERROR_NONE)
66 API int contacts_connect2()
71 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
72 if (0 == ctsvc_connection) {
74 ret = ctsvc_ipc_connect();
75 if (ret != CONTACTS_ERROR_NONE) {
76 CTS_ERR("ctsvc_ipc_connect() Failed(%d)", ret);
77 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
81 ret = ctsvc_socket_init();
82 if (ret != CONTACTS_ERROR_NONE) {
83 CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
84 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
88 ret = ctsvc_inotify_init();
89 if (ret != CONTACTS_ERROR_NONE) {
90 CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
92 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
96 ctsvc_view_uri_init();
97 ctsvc_register_vconf();
98 ctsvc_ipc_create_for_change_subscription();
101 CTS_DBG("System : Contacts service has been already connected(%d)", ctsvc_connection + 1);
104 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
106 return CONTACTS_ERROR_NONE;
109 API int contacts_disconnect2()
115 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
116 if (1 == ctsvc_connection) {
117 ctsvc_ipc_destroy_for_change_subscription();
119 ret = ctsvc_ipc_disconnect();
120 if (ret != CONTACTS_ERROR_NONE) {
121 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
122 CTS_ERR("ctsvc_ipc_disconnect() Failed(%d)", ret);
126 ctsvc_view_uri_deinit();
127 ctsvc_inotify_close();
128 ctsvc_socket_final();
129 ctsvc_deregister_vconf();
132 else if (1 < ctsvc_connection)
133 CTS_DBG("System : connection count is %d", ctsvc_connection);
135 CTS_DBG("System : please call contacts_connect2(), connection count is (%d)", ctsvc_connection);
136 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
137 return CONTACTS_ERROR_INVALID_PARAMETER;
141 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
143 return CONTACTS_ERROR_NONE;
146 API int contacts_connect_on_thread()
150 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
152 ret = ctsvc_ipc_connect_on_thread();
153 if (ret != CONTACTS_ERROR_NONE) {
154 CTS_ERR("ctsvc_ipc_connect_on_thread() Failed(%d)", ret);
155 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
159 if (0 < ctsvc_connection_on_thread)
160 CTS_DBG("System : Contacts service has been already connected");
162 ctsvc_connection_on_thread++;
164 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
166 return CONTACTS_ERROR_NONE;
169 API int contacts_disconnect_on_thread()
173 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
175 ret = ctsvc_ipc_disconnect_on_thread();
176 if (ret != CONTACTS_ERROR_NONE) {
177 CTS_ERR("ctsvc_ipc_disconnect_on_thread() Failed(%d)", ret);
178 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
182 if (1 == ctsvc_connection_on_thread) {
183 CTS_DBG("System : connection_on_thread was destroyed successfully");
185 else if (1 < ctsvc_connection_on_thread) {
186 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
189 CTS_DBG("System : please call contacts_connect_on_thread(), connection count is (%d)", ctsvc_connection_on_thread);
190 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
191 return CONTACTS_ERROR_INVALID_PARAMETER;
194 ctsvc_connection_on_thread--;
196 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
198 return CONTACTS_ERROR_NONE;