[ACR-249] Change retval
[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         if (0 == ((ctsvc_base_s *)contact)->connection_count) {
108                 ret = ctsvc_client_handle_remove(id, contact);
109                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_handle_remove() Fail(%d)", ret);
110         }
111
112         return ret;
113 }
114
115 API int contacts_connect_on_thread(void)
116 {
117         CTS_FN_CALL;
118         int ret;
119         contacts_h contact = NULL;
120         unsigned int id = ctsvc_client_get_tid();
121
122         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
123         if (CONTACTS_ERROR_NO_DATA == ret) {
124                 ret = ctsvc_client_handle_create(id, &contact);
125                 if (CONTACTS_ERROR_NONE != ret)
126                 {
127                         CTS_ERR("ctsvc_client_handle_create() Fail(%d)", ret);
128                         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
129                                 return CONTACTS_ERROR_INTERNAL;
130                         return ret;
131                 }
132         }
133         else if (CONTACTS_ERROR_NONE != ret) {
134                 CTS_ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
135                 return ret;
136         }
137
138         ret = ctsvc_client_connect_on_thread(contact);
139         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_connect_on_thread() Fail(%d)", ret);
140         if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
141                         || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
142                 return CONTACTS_ERROR_IPC;
143
144         return ret;
145 }
146
147 API int contacts_disconnect_on_thread(void)
148 {
149         CTS_FN_CALL;
150         int ret;
151         contacts_h contact = NULL;
152         unsigned int id = ctsvc_client_get_tid();
153
154         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
155         RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
156         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
157
158         ret = ctsvc_client_disconnect_on_thread(contact);
159         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect_on_thread() Fail(%d)", ret);
160         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
161                 ret = CONTACTS_ERROR_IPC;
162
163         if (0 == ((ctsvc_base_s *)contact)->connection_count) {
164                 ret = ctsvc_client_handle_remove(id, contact);
165                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_handle_remove() Fail(%d)", ret);
166         }
167
168         return ret;
169 }
170