add progress callback to the API importing SIM contacts
[platform/core/pim/contacts-service.git] / common / ctsvc_socket.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <errno.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23
24 #include "contacts.h"
25 #include "ctsvc_internal.h"
26 #include "ctsvc_socket.h"
27 #include "ctsvc_mutex.h"
28 #include "ctsvc_inotify.h"
29
30 static int __ctsvc_conn_refcnt = 0;
31 static int __ctsvc_sockfd = -1;
32
33 int ctsvc_socket_init(void)
34 {
35         int ret;
36         struct sockaddr_un caddr;
37
38         if (0 < __ctsvc_conn_refcnt) {
39                 __ctsvc_conn_refcnt++;
40                 return  CONTACTS_ERROR_NONE;
41         }
42
43         char sock_file[CTSVC_PATH_MAX_LEN] = {0};
44         snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/%s", getuid(), CTSVC_SOCKET_FILE);
45
46         bzero(&caddr, sizeof(caddr));
47         caddr.sun_family = AF_UNIX;
48         snprintf(caddr.sun_path, sizeof(caddr.sun_path), "%s", sock_file);
49
50         __ctsvc_sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
51         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC,
52                         "socket() Fail(errno = %d)", errno);
53
54         ret = connect(__ctsvc_sockfd, (struct sockaddr *)&caddr, sizeof(caddr));
55         if (-1 == ret) {
56                 ERR("connect() Fail(errno = %d)", errno);
57                 close(__ctsvc_sockfd);
58                 __ctsvc_sockfd = -1;
59                 return CONTACTS_ERROR_IPC;
60         }
61
62         __ctsvc_conn_refcnt++;
63         return CONTACTS_ERROR_NONE;
64 }
65
66 void ctsvc_socket_final(void)
67 {
68         if (1 < __ctsvc_conn_refcnt) {
69                 DBG("socket ref count : %d", __ctsvc_conn_refcnt);
70                 __ctsvc_conn_refcnt--;
71                 return;
72         } else if (__ctsvc_conn_refcnt < 1) {
73                 DBG("Please call connection API. socket ref count : %d", __ctsvc_conn_refcnt);
74                 return;
75         }
76         __ctsvc_conn_refcnt--;
77
78         close(__ctsvc_sockfd);
79         __ctsvc_sockfd = -1;
80 }
81
82 static inline int __ctsvc_safe_write(int fd, const char *buf, int buf_size)
83 {
84         int ret, writed = 0;
85         while (buf_size) {
86                 ret = write(fd, buf+writed, buf_size);
87                 if (-1 == ret) {
88                         if (EINTR == errno)
89                                 continue;
90                         else
91                                 return ret;
92                 }
93                 writed += ret;
94                 buf_size -= ret;
95         }
96         return writed;
97 }
98
99 static inline int __ctsvc_safe_read(int fd, char *buf, int buf_size)
100 {
101         int ret, read_size = 0;
102         while (buf_size) {
103                 ret = read(fd, buf+read_size, buf_size);
104                 if (-1 == ret) {
105                         if (EINTR == errno)
106                                 continue;
107                         else
108                                 return ret;
109                 }
110                 read_size += ret;
111                 buf_size -= ret;
112         }
113         return read_size;
114 }
115
116 static int __ctsvc_socket_handle_return(int fd, ctsvc_socket_msg_s *msg)
117 {
118         CTS_FN_CALL;
119         int ret;
120
121         ret = __ctsvc_safe_read(fd, (char *)msg, sizeof(ctsvc_socket_msg_s));
122         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
123
124         WARN_IF(CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE != msg->type,
125                         "Unknown Type(%d), ret=%d, attach_num= %d,"
126                         "attach1 = %d, attach2 = %d, attach3 = %d, attach4 = %d",
127                         msg->type, msg->val, msg->attach_num,
128                         msg->attach_sizes[0], msg->attach_sizes[1], msg->attach_sizes[2],
129                         msg->attach_sizes[3]);
130
131         RETVM_IF(CTSVC_SOCKET_MSG_REQUEST_MAX_ATTACH < msg->attach_num, CONTACTS_ERROR_IPC,
132                         "Invalid msg(attach_num = %d)", msg->attach_num);
133
134         return CONTACTS_ERROR_NONE;
135 }
136
137 static int __ctsvc_client_socket_response_handler(int fd, ctsvc_socket_msg_s *msg, contacts_sim_import_progress_cb callback, void *user_data)
138 {
139         CTS_FN_CALL;
140         int ret;
141
142         do {
143                 ret = __ctsvc_safe_read(fd, (char *)msg, sizeof(ctsvc_socket_msg_s));
144                 RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
145                 RETVM_IF(CTSVC_SOCKET_MSG_REQUEST_MAX_ATTACH < msg->attach_num, CONTACTS_ERROR_IPC,
146                                         "Invalid msg(attach_num = %d)", msg->attach_num);
147
148                 switch (msg->type) {
149                 case CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE:
150                         break;
151
152                 case CTSVC_SOCKET_MSG_TYPE_REQUEST_INVOKE_CB:
153                         DBG("total[%d], index[%d]", msg->val, msg->data);
154                         if (callback)
155                                 callback(msg->val, msg->data, user_data);
156                         else
157                                 WARN("No callback");
158                         break;
159
160                 /* LCOV_EXCL_START */
161                 default:
162                         WARN("Unknown Type(%d), ret=%d, attach_num= %d,"
163                                         "attach1 = %d, attach2 = %d, attach3 = %d, attach4 = %d",
164                                         msg->type, msg->val, msg->attach_num,
165                                         msg->attach_sizes[0], msg->attach_sizes[1], msg->attach_sizes[2],
166                                         msg->attach_sizes[3]);
167                         break;
168                 /* LCOV_EXCL_STOP */
169                 }
170         } while (CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE != msg->type);
171
172         return CONTACTS_ERROR_NONE;
173 }
174
175 static void __ctsvc_remove_invalid_msg(int fd, int size)
176 {
177         int ret;
178         char dummy[CTSVC_SOCKET_MSG_SIZE] = {0};
179
180         while (size) {
181                 if (sizeof(dummy) < size) {
182                         ret = read(fd, dummy, sizeof(dummy));
183                         if (-1 == ret) {
184                                 if (EINTR == errno)
185                                         continue;
186                                 else
187                                         return;
188                         }
189                         size -= ret;
190                 } else {
191                         ret = read(fd, dummy, size);
192                         if (-1 == ret) {
193                                 if (EINTR == errno)
194                                         continue;
195                                 else
196                                         return;
197                         }
198                         size -= ret;
199                 }
200         }
201 }
202
203 int ctsvc_request_sim_import(int sim_slot_no, contacts_sim_import_progress_cb callback,
204                 void *user_data)
205 {
206         int i, ret;
207         ctsvc_socket_msg_s msg = {0};
208         char src[64] = {0};
209
210         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC, "socket is not connected");
211
212         msg.type = CTSVC_SOCKET_MSG_TYPE_REQUEST_IMPORT_SIM;
213
214         snprintf(src, sizeof(src), "%d", sim_slot_no);
215         msg.attach_num = 1;
216         msg.attach_sizes[0] = strlen(src);
217
218         ret = __ctsvc_safe_write(__ctsvc_sockfd, (char *)&msg, sizeof(msg));
219         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
220
221         ret = __ctsvc_safe_write(__ctsvc_sockfd, src, msg.attach_sizes[0]);
222         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
223
224         ret = __ctsvc_client_socket_response_handler(__ctsvc_sockfd, &msg, callback, user_data);
225         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Fail(%d)", ret);
226         DBG("attach_num = %d", msg.attach_num);
227
228         for (i = 0; i < msg.attach_num; i++)
229                 __ctsvc_remove_invalid_msg(__ctsvc_sockfd, msg.attach_sizes[i]);
230
231         return msg.val;
232 }
233
234 int ctsvc_request_sim_get_initialization_status(int sim_slot_no, bool *completed)
235 {
236         int ret = 0;
237         ctsvc_socket_msg_s msg = {0};
238         char dest[CTSVC_SOCKET_MSG_SIZE] = {0};
239         char src[64] = {0};
240
241         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC, "socket is not connected");
242
243         msg.type = CTSVC_SOCKET_MSG_TYPE_REQUEST_SIM_INIT_COMPLETE;
244
245         snprintf(src, sizeof(src), "%d", sim_slot_no);
246         msg.attach_num = 1;
247         msg.attach_sizes[0] = strlen(src);
248
249         ret = __ctsvc_safe_write(__ctsvc_sockfd, (char *)&msg, sizeof(msg));
250         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
251
252         ret = __ctsvc_safe_write(__ctsvc_sockfd, src, msg.attach_sizes[0]);
253         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
254
255         ret = __ctsvc_socket_handle_return(__ctsvc_sockfd, &msg);
256         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Fail(%d)", ret);
257         DBG("attach_num = %d", msg.attach_num);
258
259         ret = __ctsvc_safe_read(__ctsvc_sockfd, dest, msg.attach_sizes[0]);
260         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
261
262         if (atoi(dest) == 0)
263                 *completed = false;
264         else
265                 *completed = true;
266
267         INFO("sim init complete : %d", *completed);
268
269         return msg.val;
270 }