Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-ipc / email-proxy / email-proxy-main.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22
23 #include <unistd.h>
24 #include <pthread.h>
25 #include <malloc.h>
26
27 #include "email-ipc.h"
28 #include "email-ipc-build.h"
29 #include "email-proxy-main.h"
30 #include "email-proxy-socket.h"
31 #include "email-proxy-callback-info.h"
32
33 #include "email-debug-log.h"
34
35
36 EXPORT_API int emipc_initialize_proxy_main()
37 {
38         EM_DEBUG_FUNC_BEGIN();
39         int sock_fd = 0;
40
41         sock_fd = emipc_get_proxy_socket_id();
42         
43         if (sock_fd) {
44                 EM_DEBUG_LOG("Socket already initialized");
45                 return EMAIL_ERROR_IPC_ALREADY_INITIALIZED;
46         }
47         
48         if (!emipc_start_proxy_socket()) {
49                 EM_DEBUG_EXCEPTION("Socket start failed");
50                 return EMAIL_ERROR_IPC_CONNECTION_FAILURE;
51         }
52         
53         EM_DEBUG_LOG("Socket ID : %d", emipc_get_proxy_socket_id());
54         EM_DEBUG_FUNC_END();
55         return EMAIL_ERROR_NONE; 
56 }
57
58 EXPORT_API int emipc_finalize_proxy_main()
59 {
60         EM_DEBUG_FUNC_BEGIN();
61         if (!emipc_end_proxy_socket()) {
62                 EM_DEBUG_EXCEPTION("emipc_finalize_proxy_main failed");
63                 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
64         }
65
66         EM_DEBUG_FUNC_END();
67         return EMAIL_ERROR_NONE;
68 }
69
70 EXPORT_API bool emipc_execute_api_of_proxy_main(emipc_email_api_info *api_info)
71 {
72         EM_DEBUG_FUNC_BEGIN();
73         
74         int ret;
75         unsigned char *in_stream = NULL;
76         int length = 0;
77         bool result = false;
78         int sending_bytes;
79
80         if (!api_info) {
81                 EM_DEBUG_EXCEPTION("Invalid Parameter");
82                 return false;
83         }
84
85         in_stream = emipc_serialize_api_info(api_info, ePARAMETER_IN, &length);
86         if( !in_stream ) {
87                 EM_DEBUG_EXCEPTION("NULL stream");
88                 return false;
89         }
90
91         sending_bytes = emipc_send_proxy_socket(in_stream, length);
92
93         EM_DEBUG_LOG("Proxy=>stub sending %d byte.", sending_bytes);
94
95         if (sending_bytes > 0) {
96 #ifdef IPCLIB_STREAM_TRACE_ON
97                 int index = 0;
98                 for (index=0;index<length;index++) 
99                         EM_DEBUG_LOG("in_stream[index] : [%x]", in_stream[index]);
100 #endif
101                 char *ipc_buf = NULL;
102
103                 ret = emipc_recv_proxy_socket(&ipc_buf);
104         
105                 EM_DEBUG_LOG("Recv length : %d", ret);
106
107                 if (ret > 0)
108                         result = emipc_deserialize_api_info(api_info, ePARAMETER_OUT, ipc_buf);
109                 else
110                         result = false; 
111
112                 EM_SAFE_FREE(ipc_buf);
113         }
114         
115         EM_DEBUG_FUNC_END();
116         return result;          
117 }