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) {
75 ret = ctsvc_ipc_connect();
76 if (ret != CONTACTS_ERROR_NONE) {
77 CTS_ERR("ctsvc_ipc_connect() Failed(%d)", ret);
78 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
82 ret = ctsvc_socket_init();
83 if (ret != CONTACTS_ERROR_NONE) {
84 CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
85 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
89 ret = ctsvc_inotify_init();
90 if (ret != CONTACTS_ERROR_NONE) {
91 CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
93 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
97 ctsvc_view_uri_init();
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();
130 else if (1 < ctsvc_connection)
131 CTS_DBG("System : connection count is %d", ctsvc_connection);
133 CTS_DBG("System : please call contacts_connect2(), connection count is (%d)", ctsvc_connection);
134 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
135 return CONTACTS_ERROR_INVALID_PARAMETER;
139 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
141 return CONTACTS_ERROR_NONE;
144 API int contacts_connect_on_thread()
148 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
150 if (0 == ctsvc_connection_on_thread) {
151 ret = ctsvc_ipc_connect_on_thread();
152 if (ret != CONTACTS_ERROR_NONE) {
153 CTS_ERR("ctsvc_ipc_connect_on_thread() Failed(%d)", ret);
154 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
158 ret = ctsvc_socket_init();
159 if (ret != CONTACTS_ERROR_NONE) {
160 CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
161 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
165 ret = ctsvc_inotify_init();
166 if (ret != CONTACTS_ERROR_NONE) {
167 CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
168 ctsvc_socket_final();
169 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
173 ctsvc_view_uri_init();
174 ctsvc_ipc_create_for_change_subscription();
176 else if (0 < ctsvc_connection_on_thread)
177 CTS_DBG("System : Contacts service has been already connected");
179 ctsvc_connection_on_thread++;
181 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
183 return CONTACTS_ERROR_NONE;
186 API int contacts_disconnect_on_thread()
190 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
192 if (1 == ctsvc_connection_on_thread) {
193 ctsvc_ipc_destroy_for_change_subscription();
195 ret = ctsvc_ipc_disconnect_on_thread();
196 if (ret != CONTACTS_ERROR_NONE) {
197 CTS_ERR("ctsvc_ipc_disconnect_on_thread() Failed(%d)", ret);
198 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
202 ctsvc_view_uri_deinit();
203 ctsvc_inotify_close();
204 ctsvc_socket_final();
205 CTS_DBG("System : connection_on_thread was destroyed successfully");
207 else if (1 < ctsvc_connection_on_thread) {
208 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
211 CTS_DBG("System : please call contacts_connect_on_thread(), connection count is (%d)", ctsvc_connection_on_thread);
212 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
213 return CONTACTS_ERROR_INVALID_PARAMETER;
216 ctsvc_connection_on_thread--;
218 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
220 return CONTACTS_ERROR_NONE;