Remove unnecessary error check
[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         ret = contacts_connect2();
46         if (ret == CONTACTS_ERROR_NONE)
47                 return ret;
48
49         if (flags & CONTACTS_CONNECT_FLAG_RETRY) {
50                 int i;
51                 int waiting_time = 500;
52                 for (i=0;i<9;i++) {
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)
57                                 break;
58                         if (6 < i)
59                                 waiting_time += 30000;
60                         else
61                                 waiting_time *= 2;
62                 }
63         }
64
65         return ret;
66 }
67
68 API int contacts_connect2()
69 {
70         CTS_FN_CALL;
71         int ret;
72
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);
79                         return ret;
80                 }
81
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);
86                         return ret;
87                 }
88
89                 ret = ctsvc_inotify_init();
90                 if (ret != CONTACTS_ERROR_NONE) {
91                         CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
92                         ctsvc_socket_final();
93                         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
94                         return ret;
95                 }
96
97                 ctsvc_view_uri_init();
98                 ctsvc_ipc_create_for_change_subscription();
99         }
100         else
101                 CTS_DBG("System : Contacts service has been already connected(%d)", ctsvc_connection + 1);
102
103         ctsvc_connection++;
104         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
105
106         return CONTACTS_ERROR_NONE;
107 }
108
109 API int contacts_disconnect2()
110 {
111         int ret;
112
113         CTS_FN_CALL;
114
115         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
116         if (1 == ctsvc_connection) {
117                 ctsvc_ipc_destroy_for_change_subscription();
118
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);
123                         return ret;
124                 }
125
126                 ctsvc_view_uri_deinit();
127                 ctsvc_inotify_close();
128                 ctsvc_socket_final();
129         }
130         else if (1 < ctsvc_connection)
131                 CTS_DBG("System : connection count is %d", ctsvc_connection);
132         else {
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;
136         }
137
138         ctsvc_connection--;
139         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
140
141         return CONTACTS_ERROR_NONE;
142 }
143
144 API int contacts_connect_on_thread()
145 {
146         int ret;
147
148         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
149
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);
155                         return ret;
156                 }
157
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);
162                         return ret;
163                 }
164
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);
170                         return ret;
171                 }
172
173                 ctsvc_view_uri_init();
174                 ctsvc_ipc_create_for_change_subscription();
175         }
176         else if (0 < ctsvc_connection_on_thread)
177                 CTS_DBG("System : Contacts service has been already connected");
178
179         ctsvc_connection_on_thread++;
180
181         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
182
183         return CONTACTS_ERROR_NONE;
184 }
185
186 API int contacts_disconnect_on_thread()
187 {
188         int ret;
189
190         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
191
192         if (1 == ctsvc_connection_on_thread) {
193                 ctsvc_ipc_destroy_for_change_subscription();
194
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);
199                         return ret;
200                 }
201
202                 ctsvc_view_uri_deinit();
203                 ctsvc_inotify_close();
204                 ctsvc_socket_final();
205                 CTS_DBG("System : connection_on_thread was destroyed successfully");
206         }
207         else if (1 < ctsvc_connection_on_thread) {
208                 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
209         }
210         else {
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;
214         }
215
216         ctsvc_connection_on_thread--;
217
218         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
219
220         return CONTACTS_ERROR_NONE;
221 }