add APIs for getting SIM init status and importing SIM contacts by sim slot no
[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  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20 #include "contacts.h"
21 #include "ctsvc_internal.h"
22 #include "ctsvc_client_ipc.h"
23 #include "ctsvc_client_utils.h"
24 #include "ctsvc_client_handle.h"
25 #include "ctsvc_client_service_helper.h"
26
27 API int contacts_connect_with_flags(unsigned int flags)
28 {
29         CTS_FN_CALL;
30         int ret;
31         contacts_h contact = NULL;
32         unsigned int id = ctsvc_client_get_pid();
33
34         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
35         if (CONTACTS_ERROR_NO_DATA == ret) {
36                 ret = ctsvc_client_handle_create(id, &contact);
37                 if (CONTACTS_ERROR_NONE != ret) {
38                         ERR("ctsvc_client_handle_create() Fail(%d)", ret);
39                         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
40                                 return CONTACTS_ERROR_INTERNAL;
41                         return ret;
42                 }
43         } else if (CONTACTS_ERROR_NONE != ret) {
44                 ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
45                 return ret;
46         }
47
48         ret = ctsvc_client_connect_with_flags(contact, flags);
49         if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
50                         || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
51                 return CONTACTS_ERROR_IPC;
52
53         return ret;
54 }
55
56 API int contacts_connect(void)
57 {
58         CTS_FN_CALL;
59         int ret;
60         contacts_h contact = NULL;
61         unsigned int id = ctsvc_client_get_pid();
62
63         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
64         if (CONTACTS_ERROR_NO_DATA == ret) {
65                 ret = ctsvc_client_handle_create(id, &contact);
66                 if (CONTACTS_ERROR_NONE != ret) {
67                         ERR("ctsvc_client_handle_create() Fail(%d)", ret);
68                         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
69                                 return CONTACTS_ERROR_INTERNAL;
70                         return ret;
71                 }
72         } else if (CONTACTS_ERROR_NONE != ret) {
73                 ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
74                 return ret;
75         }
76         ret = ctsvc_client_connect(contact);
77         if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
78                         || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
79                 return CONTACTS_ERROR_IPC;
80
81         return ret;
82 }
83
84 API int contacts_disconnect(void)
85 {
86         CTS_FN_CALL;
87         int ret;
88         contacts_h contact = NULL;
89         unsigned int id = ctsvc_client_get_pid();
90
91         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
92         RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
93         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
94
95         ret = ctsvc_client_disconnect(contact);
96         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect() Fail(%d)", ret);
97         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
98                 ret = CONTACTS_ERROR_IPC;
99
100         return ret;
101 }
102
103 API int contacts_connect_on_thread(void)
104 {
105         CTS_FN_CALL;
106         int ret;
107         contacts_h contact = NULL;
108         unsigned int id = ctsvc_client_get_tid();
109
110         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
111         if (CONTACTS_ERROR_NO_DATA == ret) {
112                 ret = ctsvc_client_handle_create(id, &contact);
113                 if (CONTACTS_ERROR_NONE != ret) {
114                         ERR("ctsvc_client_handle_create() Fail(%d)", ret);
115                         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
116                                 return CONTACTS_ERROR_INTERNAL;
117                         return ret;
118                 }
119         } else if (CONTACTS_ERROR_NONE != ret) {
120                 ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
121                 return ret;
122         }
123
124         ret = ctsvc_client_connect_on_thread(contact);
125         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_connect_on_thread() Fail(%d)", ret);
126         if ((CONTACTS_ERROR_IPC_NOT_AVALIABLE == ret)
127                         || (CONTACTS_ERROR_PERMISSION_DENIED == ret))
128                 return CONTACTS_ERROR_IPC;
129
130         return ret;
131 }
132
133 API int contacts_disconnect_on_thread(void)
134 {
135         CTS_FN_CALL;
136         int ret;
137         contacts_h contact = NULL;
138         unsigned int id = ctsvc_client_get_tid();
139
140         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
141         RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
142         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
143
144         ret = ctsvc_client_disconnect_on_thread(contact);
145         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect_on_thread() Fail(%d)", ret);
146         if (CONTACTS_ERROR_INVALID_PARAMETER == ret)
147                 ret = CONTACTS_ERROR_IPC;
148
149         return ret;
150 }
151