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_common_setting.h"
35 #include "ctsvc_client_ipc.h"
37 static int ctsvc_connection = 0;
39 static __thread int 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)
60 waiting_time += 30000;
69 API int contacts_connect2()
74 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
75 if (0 == ctsvc_connection) {
77 ret = ctsvc_ipc_connect();
78 if (ret != CONTACTS_ERROR_NONE) {
79 CTS_ERR("ctsvc_ipc_connect() Failed(%d)", ret);
80 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
84 ret = ctsvc_socket_init();
85 if (ret != CONTACTS_ERROR_NONE) {
86 CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
87 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
91 ret = ctsvc_inotify_init();
92 if (ret != CONTACTS_ERROR_NONE) {
93 CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
95 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
99 ctsvc_view_uri_init();
100 ctsvc_register_common_vconf();
101 ctsvc_ipc_create_for_change_subscription();
104 CTS_DBG("System : Contacts service has been already connected(%d)", ctsvc_connection + 1);
107 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
109 return CONTACTS_ERROR_NONE;
112 API int contacts_disconnect2()
118 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
119 if (1 == ctsvc_connection) {
120 ctsvc_ipc_destroy_for_change_subscription();
122 ret = ctsvc_ipc_disconnect();
123 if (ret != CONTACTS_ERROR_NONE) {
124 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
125 CTS_ERR("ctsvc_ipc_disconnect() Failed(%d)", ret);
129 ctsvc_view_uri_deinit();
130 ctsvc_inotify_close();
131 ctsvc_socket_final();
132 ctsvc_deregister_common_vconf();
134 else if (1 < ctsvc_connection)
135 CTS_DBG("System : connection count is %d", ctsvc_connection);
137 CTS_DBG("System : please call contacts_connect2(), connection count is (%d)", ctsvc_connection);
138 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
139 return CONTACTS_ERROR_INVALID_PARAMETER;
143 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
145 return CONTACTS_ERROR_NONE;
148 API int contacts_connect_on_thread()
152 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
154 ret = ctsvc_ipc_connect_on_thread();
155 if (ret != CONTACTS_ERROR_NONE) {
156 CTS_ERR("ctsvc_ipc_connect_on_thread() Failed(%d)", ret);
157 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
161 if (0 < ctsvc_connection_on_thread)
162 CTS_DBG("System : Contacts service has been already connected");
164 ctsvc_connection_on_thread++;
166 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
168 return CONTACTS_ERROR_NONE;
171 API int contacts_disconnect_on_thread()
175 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
177 ret = ctsvc_ipc_disconnect_on_thread();
178 if (ret != CONTACTS_ERROR_NONE) {
179 CTS_ERR("ctsvc_ipc_disconnect_on_thread() Failed(%d)", ret);
180 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
184 if (1 == ctsvc_connection_on_thread) {
185 CTS_DBG("System : connection_on_thread was destroyed successfully");
187 else if (1 < ctsvc_connection_on_thread) {
188 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
191 CTS_DBG("System : please call contacts_connect_on_thread(), connection count is (%d)", ctsvc_connection_on_thread);
192 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
193 return CONTACTS_ERROR_INVALID_PARAMETER;
196 ctsvc_connection_on_thread--;
198 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
200 return CONTACTS_ERROR_NONE;