Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-ipc / email-stub / email-stub-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 #include <glib.h>
23
24 #include "email-stub-main.h"
25 #include "email-ipc.h"
26 #include "email-ipc-param-list.h"
27 #include "email-ipc-build.h"
28 #include "email-dbus-activation.h"
29 #include "email-stub-socket.h"
30
31 #include "email-api.h"
32 #include "email-debug-log.h"
33
34 static bool stub_socket = false;
35 static PFN_EXECUTE_API this_fn_api_mapper = NULL;
36
37 EXPORT_API bool emipc_initialize_stub_main(PFN_EXECUTE_API fn_api_mapper)
38 {
39         EM_DEBUG_FUNC_BEGIN();
40
41         if (!fn_api_mapper) {
42                 EM_DEBUG_EXCEPTION("Invalid Param");
43                 return false;
44         }
45
46         this_fn_api_mapper = fn_api_mapper;
47
48         /* Initialize the socket */
49         stub_socket = emipc_start_stub_socket();
50         if (!stub_socket) {
51                 EM_DEBUG_EXCEPTION("Socket did not create");
52                 return false;
53         }
54
55         emipc_init_dbus_connection();
56
57         EM_DEBUG_FUNC_END();
58         return true;
59 }
60
61 EXPORT_API bool emipc_finalize_stub_main()
62 {
63         EM_DEBUG_FUNC_BEGIN();
64
65         if (stub_socket) {
66                 emipc_end_stub_socket();
67                 stub_socket = false;
68         }
69         
70         if (this_fn_api_mapper)
71                 this_fn_api_mapper = NULL;
72                 
73         EM_DEBUG_FUNC_END();
74         return true;
75 }
76
77 EXPORT_API bool emipc_execute_api_proxy_to_stub(emipc_email_api_info *api_info)
78 {
79         EM_DEBUG_FUNC_BEGIN();
80         if (!api_info) {
81                 EM_DEBUG_EXCEPTION("Invalid Param");
82                 return false;
83         }
84         
85         if (this_fn_api_mapper) {
86                 this_fn_api_mapper(api_info);
87         }
88
89         EM_DEBUG_FUNC_END();
90         return true;
91 }
92
93 EXPORT_API bool emipc_execute_api_stub_to_proxy(emipc_email_api_info *api_info)
94 {
95         EM_DEBUG_FUNC_BEGIN("api_info [%p]", api_info);
96         EM_IF_NULL_RETURN_VALUE(api_info, false);
97         EM_DEBUG_LOG("APIID [%s], response Socket ID [%d], APPID [%d]",
98                                 EM_APIID_TO_STR(api_info->api_id), api_info->response_id, api_info->app_id);
99         
100         unsigned char *stream = NULL;
101         int stream_length = 0;
102         
103         stream = emipc_serialize_api_info(api_info, ePARAMETER_OUT, &stream_length);
104         EM_DEBUG_LOG("Stub => Proxy Sending %dB", stream_length);
105
106         emipc_send_stub_socket(api_info->response_id, stream, stream_length);
107         
108 #ifdef IPCLIB_STREAM_TRACE_ON
109         int index = 0;
110         for (index = 0; index < stream_length; index++)
111                 EM_DEBUG_LOG("Stream[index] : [%d]", stream[index]);
112 #endif
113
114         EM_DEBUG_FUNC_END();
115         return true;
116 }