ctsvc_client_handle_remove in CTS_MUTEX_CONNECTION during disconnect_on_thread
[platform/core/pim/contacts-service.git] / client / ctsvc_client_service.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 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 "contacts.h"
24 #include "ctsvc_internal.h"
25 #include "ctsvc_client_ipc.h"
26 #include "ctsvc_client_utils.h"
27 #include "ctsvc_client_handle.h"
28 #include "ctsvc_client_service_helper.h"
29
30 API int contacts_connect_with_flags(unsigned int flags)
31 {
32         CTS_FN_CALL;
33         int ret;
34         contacts_h contact = NULL;
35         unsigned int id = ctsvc_client_get_pid();
36
37         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
38         if (CONTACTS_ERROR_NO_DATA == ret) {
39                 ret = ctsvc_client_handle_create(id, &contact);
40                 if (CONTACTS_ERROR_NONE != ret)
41                 {
42                         CTS_ERR("ctsvc_client_handle_create() Fail(%d)", ret);
43                         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
44                                 return CONTACTS_ERROR_INTERNAL;
45                         return ret;
46                 }
47         }
48         else if (CONTACTS_ERROR_NONE != ret) {
49                 CTS_ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
50                 return ret;
51         }
52
53         ret = ctsvc_client_connect_with_flags(contact, flags);
54         if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
55                         || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
56                 return CONTACTS_ERROR_IPC;
57
58         return ret;
59 }
60
61 API int contacts_connect(void)
62 {
63         CTS_FN_CALL;
64         int ret;
65         contacts_h contact = NULL;
66         unsigned int id = ctsvc_client_get_pid();
67
68         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
69         if (CONTACTS_ERROR_NO_DATA == ret) {
70                 ret = ctsvc_client_handle_create(id, &contact);
71                 if (CONTACTS_ERROR_NONE != ret)
72                 {
73                         CTS_ERR("ctsvc_client_handle_create() Fail(%d)", ret);
74                         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
75                                 return CONTACTS_ERROR_INTERNAL;
76                         return ret;
77                 }
78         }
79         else if (CONTACTS_ERROR_NONE != ret) {
80                 CTS_ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
81                 return ret;
82         }
83         ret = ctsvc_client_connect(contact);
84         if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
85                         || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
86                 return CONTACTS_ERROR_IPC;
87
88         return ret;
89 }
90
91 API int contacts_disconnect(void)
92 {
93         CTS_FN_CALL;
94         int ret;
95         contacts_h contact = NULL;
96         unsigned int id = ctsvc_client_get_pid();
97
98         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
99         RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
100         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
101
102         ret = ctsvc_client_disconnect(contact);
103         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect() Fail(%d)", ret);
104         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
105                 ret = CONTACTS_ERROR_IPC;
106
107         return ret;
108 }
109
110 API int contacts_connect_on_thread(void)
111 {
112         CTS_FN_CALL;
113         int ret;
114         contacts_h contact = NULL;
115         unsigned int id = ctsvc_client_get_tid();
116
117         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
118         if (CONTACTS_ERROR_NO_DATA == ret) {
119                 ret = ctsvc_client_handle_create(id, &contact);
120                 if (CONTACTS_ERROR_NONE != ret)
121                 {
122                         CTS_ERR("ctsvc_client_handle_create() Fail(%d)", ret);
123                         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
124                                 return CONTACTS_ERROR_INTERNAL;
125                         return ret;
126                 }
127         }
128         else if (CONTACTS_ERROR_NONE != ret) {
129                 CTS_ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
130                 return ret;
131         }
132
133         ret = ctsvc_client_connect_on_thread(contact);
134         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_connect_on_thread() Fail(%d)", ret);
135         if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
136                         || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
137                 return CONTACTS_ERROR_IPC;
138
139         return ret;
140 }
141
142 API int contacts_disconnect_on_thread(void)
143 {
144         CTS_FN_CALL;
145         int ret;
146         contacts_h contact = NULL;
147         unsigned int id = ctsvc_client_get_tid();
148
149         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
150         RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
151         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
152
153         ret = ctsvc_client_disconnect_on_thread(contact);
154         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect_on_thread() Fail(%d)", ret);
155         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
156                 ret = CONTACTS_ERROR_IPC;
157
158         return ret;
159 }
160