4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include <sys/select.h>
27 #include "email-ipc-build.h"
28 #include "email-proxy-socket.h"
29 #include "email-ipc-socket.h"
31 #include "email-debug-log.h"
34 static int proxy_socket_fd = 0;
36 EXPORT_API bool emipc_start_proxy_socket()
38 EM_DEBUG_FUNC_BEGIN();
41 ret = emipc_init_email_socket(&proxy_socket_fd);
46 ret = emipc_connect_email_socket(proxy_socket_fd);
51 EXPORT_API bool emipc_end_proxy_socket()
53 EM_DEBUG_FUNC_BEGIN();
54 EM_DEBUG_LOG("[IPCLib] emipc_end_proxy_socket_fd");
56 if (proxy_socket_fd) {
57 emipc_close_email_socket(&proxy_socket_fd);
63 /* return result of emipc_send_email_socket
64 * EMAIL_ERROR_IPC_SOCKET_FAILURE, when no IPC connection */
65 EXPORT_API int emipc_send_proxy_socket(unsigned char *data, int len)
67 EM_DEBUG_FUNC_BEGIN();
68 if (!proxy_socket_fd) {
69 EM_DEBUG_EXCEPTION("[IPCLib] emipc_send_proxy_socket_fd not connect");
70 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
72 int send_len = emipc_send_email_socket(proxy_socket_fd, data, len);
74 EM_DEBUG_EXCEPTION("[IPCLib] server closed connection %x", proxy_socket_fd);
75 emipc_close_email_socket(&proxy_socket_fd);
80 EXPORT_API int emipc_get_proxy_socket_id()
82 EM_DEBUG_FUNC_BEGIN();
83 return proxy_socket_fd;
86 /* return true, when event occurred
87 * false, when select error
89 static bool wait_for_reply (int fd)
94 EM_DEBUG_EXCEPTION("Invalid file description : [%d]", fd);
101 if (select(fd + 1, &fds, NULL, NULL, NULL) == -1) {
102 EM_DEBUG_EXCEPTION("[IPCLib] select: %s", strerror(errno) );
106 if (FD_ISSET(fd, &fds)) return true;
112 /* return result of emipc_recv_email_socket
113 * EMAIL_ERROR_IPC_SOCKET_FAILURE, when no IPC connection, or wrong fd */
114 EXPORT_API int emipc_recv_proxy_socket(char **data)
116 EM_DEBUG_FUNC_BEGIN();
118 if (!proxy_socket_fd) {
119 EM_DEBUG_EXCEPTION("[IPCLib] proxy_socket_fd[%p] is not available or disconnected", proxy_socket_fd);
120 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
123 if( !wait_for_reply(proxy_socket_fd) ) {
125 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
128 int recv_len = emipc_recv_email_socket(proxy_socket_fd, data);
130 EM_DEBUG_EXCEPTION("[IPCLib] server closed connection %x", proxy_socket_fd);
131 emipc_close_email_socket(&proxy_socket_fd);