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 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 if (0 < ctsvc_connection_on_thread)
159 CTS_DBG("System : Contacts service has been already connected");
161 ctsvc_connection_on_thread++;
163 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
165 return CONTACTS_ERROR_NONE;
168 API int contacts_disconnect_on_thread()
172 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
174 ret = ctsvc_ipc_disconnect_on_thread();
175 if (ret != CONTACTS_ERROR_NONE) {
176 CTS_ERR("ctsvc_ipc_disconnect_on_thread() Failed(%d)", ret);
177 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
181 if (1 == ctsvc_connection_on_thread) {
182 CTS_DBG("System : connection_on_thread was destroyed successfully");
184 else if (1 < ctsvc_connection_on_thread) {
185 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
188 CTS_DBG("System : please call contacts_connect_on_thread(), connection count is (%d)", ctsvc_connection_on_thread);
189 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
190 return CONTACTS_ERROR_INVALID_PARAMETER;
193 ctsvc_connection_on_thread--;
195 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
197 return CONTACTS_ERROR_NONE;