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 // If new flag is defined, errer check should be updated
46 RETVM_IF(flags & 0x11111110, CONTACTS_ERROR_INVALID_PARAMETER, "flags is invalid");
48 ret = contacts_connect();
49 if (ret == CONTACTS_ERROR_PERMISSION_DENIED)
51 else if (ret == CONTACTS_ERROR_NONE)
54 if (flags & CONTACTS_CONNECT_FLAG_RETRY) {
56 int waiting_time = 500;
58 usleep(waiting_time * 1000);
59 CTS_DBG("retry cnt=%d, ret=%x, %d",(i+1), ret, waiting_time);
60 ret = contacts_connect();
61 if (ret == CONTACTS_ERROR_NONE)
64 waiting_time += 30000;
73 API int contacts_connect(void)
78 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
79 if (0 == ctsvc_connection) {
80 ret = ctsvc_ipc_connect();
81 if (ret != CONTACTS_ERROR_NONE) {
82 CTS_ERR("ctsvc_ipc_connect() Failed(%d)", ret);
83 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
87 ret = ctsvc_socket_init();
88 if (ret != CONTACTS_ERROR_NONE) {
89 CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
90 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
94 ret = ctsvc_inotify_init();
95 if (ret != CONTACTS_ERROR_NONE) {
96 CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
98 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
102 ctsvc_view_uri_init();
103 ctsvc_ipc_create_for_change_subscription();
106 CTS_DBG("System : Contacts service has been already connected(%d)", ctsvc_connection + 1);
109 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
111 return CONTACTS_ERROR_NONE;
114 API int contacts_disconnect(void)
120 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
121 if (1 == ctsvc_connection) {
122 ctsvc_ipc_destroy_for_change_subscription();
124 ret = ctsvc_ipc_disconnect();
125 if (ret != CONTACTS_ERROR_NONE) {
126 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
127 CTS_ERR("ctsvc_ipc_disconnect() Failed(%d)", ret);
131 ctsvc_view_uri_deinit();
132 ctsvc_inotify_close();
133 ctsvc_socket_final();
135 else if (1 < ctsvc_connection)
136 CTS_DBG("System : connection count is %d", ctsvc_connection);
138 CTS_DBG("System : please call contacts_connect(), connection count is (%d)", ctsvc_connection);
139 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
140 return CONTACTS_ERROR_DB;
144 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
146 return CONTACTS_ERROR_NONE;
149 API int contacts_connect_on_thread(void)
153 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
155 if (0 == ctsvc_connection_on_thread) {
156 ret = ctsvc_ipc_connect_on_thread();
157 if (ret != CONTACTS_ERROR_NONE) {
158 CTS_ERR("ctsvc_ipc_connect_on_thread() Failed(%d)", ret);
159 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
163 ret = ctsvc_socket_init();
164 if (ret != CONTACTS_ERROR_NONE) {
165 CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
166 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
170 ret = ctsvc_inotify_init();
171 if (ret != CONTACTS_ERROR_NONE) {
172 CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
173 ctsvc_socket_final();
174 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
178 ctsvc_view_uri_init();
179 ctsvc_ipc_create_for_change_subscription();
181 else if (0 < ctsvc_connection_on_thread)
182 CTS_DBG("System : Contacts service has been already connected");
184 ctsvc_connection_on_thread++;
186 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
188 return CONTACTS_ERROR_NONE;
191 API int contacts_disconnect_on_thread(void)
195 ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
197 if (1 == ctsvc_connection_on_thread) {
198 ctsvc_ipc_destroy_for_change_subscription();
200 ret = ctsvc_ipc_disconnect_on_thread();
201 if (ret != CONTACTS_ERROR_NONE) {
202 CTS_ERR("ctsvc_ipc_disconnect_on_thread() Failed(%d)", ret);
203 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
207 ctsvc_view_uri_deinit();
208 ctsvc_inotify_close();
209 ctsvc_socket_final();
210 CTS_DBG("System : connection_on_thread was destroyed successfully");
212 else if (1 < ctsvc_connection_on_thread) {
213 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
216 CTS_DBG("System : please call contacts_connect_on_thread(), connection count is (%d)", ctsvc_connection_on_thread);
217 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
218 return CONTACTS_ERROR_DB;
221 ctsvc_connection_on_thread--;
223 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
225 return CONTACTS_ERROR_NONE;