Remove async APIs same as 2.4
[platform/core/pim/contacts-service.git] / client / ctsvc_client_service.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Dohyung Jin <dh.jin@samsung.com>
7  *                 Jongwon Lee <gogosing.lee@samsung.com>
8  *                 Donghee Ye <donghee.ye@samsung.com>
9  *
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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.
21  *
22  */
23 #include <errno.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 #include <pims-ipc-data.h>
28
29 #include "contacts.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"
35
36 static int ctsvc_connection = 0;
37
38 static __thread int ctsvc_connection_on_thread = 0;
39
40 API int contacts_connect_with_flags(unsigned int flags)
41 {
42         CTS_FN_CALL;
43         int ret = CONTACTS_ERROR_NONE;
44
45         // If new flag is defined, errer check should be updated
46         RETVM_IF(flags & 0x11111110, CONTACTS_ERROR_INVALID_PARAMETER, "flags is invalid");
47
48         ret = contacts_connect();
49         if (ret == CONTACTS_ERROR_PERMISSION_DENIED)
50                 return ret;
51         else if (ret == CONTACTS_ERROR_NONE)
52                 return ret;
53
54         if (flags & CONTACTS_CONNECT_FLAG_RETRY) {
55                 int i;
56                 int waiting_time = 500;
57                 for (i=0;i<9;i++) {
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)
62                                 break;
63                         if (6 < i)
64                                 waiting_time += 30000;
65                         else
66                                 waiting_time *= 2;
67                 }
68         }
69
70         return ret;
71 }
72
73 API int contacts_connect(void)
74 {
75         CTS_FN_CALL;
76         int ret;
77
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);
84                         return ret;
85                 }
86
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);
91                         return ret;
92                 }
93
94                 ret = ctsvc_inotify_init();
95                 if (ret != CONTACTS_ERROR_NONE) {
96                         CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
97                         ctsvc_socket_final();
98                         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
99                         return ret;
100                 }
101
102                 ctsvc_view_uri_init();
103                 ctsvc_ipc_create_for_change_subscription();
104         }
105         else
106                 CTS_DBG("System : Contacts service has been already connected(%d)", ctsvc_connection + 1);
107
108         ctsvc_connection++;
109         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
110
111         return CONTACTS_ERROR_NONE;
112 }
113
114 API int contacts_disconnect(void)
115 {
116         int ret;
117
118         CTS_FN_CALL;
119
120         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
121         if (1 == ctsvc_connection) {
122                 ctsvc_ipc_destroy_for_change_subscription();
123
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);
128                         return ret;
129                 }
130
131                 ctsvc_view_uri_deinit();
132                 ctsvc_inotify_close();
133                 ctsvc_socket_final();
134         }
135         else if (1 < ctsvc_connection)
136                 CTS_DBG("System : connection count is %d", ctsvc_connection);
137         else {
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;
141         }
142
143         ctsvc_connection--;
144         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
145
146         return CONTACTS_ERROR_NONE;
147 }
148
149 API int contacts_connect_on_thread(void)
150 {
151         int ret;
152
153         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
154
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);
160                         return ret;
161                 }
162
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);
167                         return ret;
168                 }
169
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);
175                         return ret;
176                 }
177
178                 ctsvc_view_uri_init();
179                 ctsvc_ipc_create_for_change_subscription();
180         }
181         else if (0 < ctsvc_connection_on_thread)
182                 CTS_DBG("System : Contacts service has been already connected");
183
184         ctsvc_connection_on_thread++;
185
186         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
187
188         return CONTACTS_ERROR_NONE;
189 }
190
191 API int contacts_disconnect_on_thread(void)
192 {
193         int ret;
194
195         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
196
197         if (1 == ctsvc_connection_on_thread) {
198                 ctsvc_ipc_destroy_for_change_subscription();
199
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);
204                         return ret;
205                 }
206
207                 ctsvc_view_uri_deinit();
208                 ctsvc_inotify_close();
209                 ctsvc_socket_final();
210                 CTS_DBG("System : connection_on_thread was destroyed successfully");
211         }
212         else if (1 < ctsvc_connection_on_thread) {
213                 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
214         }
215         else {
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;
219         }
220
221         ctsvc_connection_on_thread--;
222
223         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
224
225         return CONTACTS_ERROR_NONE;
226 }
227