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_client_ipc.h"
36 static int ctsvc_connection = 0;
38 static __thread int ctsvc_connection_on_thread = 0;
40 API int contacts_connect_with_flags(unsigned int flags)
43 int ret = CONTACTS_ERROR_NONE;
45 ret = contacts_connect2();
46 if (ret == CONTACTS_ERROR_NONE)
49 if (flags & CONTACTS_CONNECT_FLAG_RETRY) {
51 int waiting_time = 500;
53 usleep(waiting_time * 1000);
54 CTS_DBG("retry cnt=%d, ret=%x, %d",(i+1), ret, waiting_time);
55 ret = contacts_connect2();
56 if (ret == CONTACTS_ERROR_NONE)
59 waiting_time += 30000;
68 API int contacts_connect2()
73 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
74 if (0 == ctsvc_connection) {
76 ret = ctsvc_ipc_connect();
77 if (ret != CONTACTS_ERROR_NONE) {
78 CTS_ERR("ctsvc_ipc_connect() Failed(%d)", ret);
79 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
83 ret = ctsvc_socket_init();
84 if (ret != CONTACTS_ERROR_NONE) {
85 CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
86 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
90 ret = ctsvc_inotify_init();
91 if (ret != CONTACTS_ERROR_NONE) {
92 CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
94 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
98 ctsvc_view_uri_init();
99 ctsvc_ipc_create_for_change_subscription();
102 CTS_DBG("System : Contacts service has been already connected(%d)", ctsvc_connection + 1);
105 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
107 return CONTACTS_ERROR_NONE;
110 API int contacts_disconnect2()
116 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
117 if (1 == ctsvc_connection) {
118 ctsvc_ipc_destroy_for_change_subscription();
120 ret = ctsvc_ipc_disconnect();
121 if (ret != CONTACTS_ERROR_NONE) {
122 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
123 CTS_ERR("ctsvc_ipc_disconnect() Failed(%d)", ret);
127 ctsvc_view_uri_deinit();
128 ctsvc_inotify_close();
129 ctsvc_socket_final();
131 else if (1 < ctsvc_connection)
132 CTS_DBG("System : connection count is %d", ctsvc_connection);
134 CTS_DBG("System : please call contacts_connect2(), connection count is (%d)", ctsvc_connection);
135 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
136 return CONTACTS_ERROR_INVALID_PARAMETER;
140 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
142 return CONTACTS_ERROR_NONE;
145 API int contacts_connect_on_thread()
149 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
151 if (0 == ctsvc_connection_on_thread) {
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 ret = ctsvc_socket_init();
160 if (ret != CONTACTS_ERROR_NONE) {
161 CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
162 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
166 ret = ctsvc_inotify_init();
167 if (ret != CONTACTS_ERROR_NONE) {
168 CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
169 ctsvc_socket_final();
170 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
174 ctsvc_view_uri_init();
175 ctsvc_ipc_create_for_change_subscription();
177 else if (0 < ctsvc_connection_on_thread)
178 CTS_DBG("System : Contacts service has been already connected");
180 ctsvc_connection_on_thread++;
182 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
184 return CONTACTS_ERROR_NONE;
187 API int contacts_disconnect_on_thread()
191 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
193 if (1 == ctsvc_connection_on_thread) {
194 ctsvc_ipc_destroy_for_change_subscription();
196 ret = ctsvc_ipc_disconnect_on_thread();
197 if (ret != CONTACTS_ERROR_NONE) {
198 CTS_ERR("ctsvc_ipc_disconnect_on_thread() Failed(%d)", ret);
199 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
203 ctsvc_view_uri_deinit();
204 ctsvc_inotify_close();
205 ctsvc_socket_final();
206 CTS_DBG("System : connection_on_thread was destroyed successfully");
208 else if (1 < ctsvc_connection_on_thread) {
209 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
212 CTS_DBG("System : please call contacts_connect_on_thread(), connection count is (%d)", ctsvc_connection_on_thread);
213 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
214 return CONTACTS_ERROR_INVALID_PARAMETER;
217 ctsvc_connection_on_thread--;
219 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
221 return CONTACTS_ERROR_NONE;