Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-ipc / email-ipc-api.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 <malloc.h>
23
24 #include "email-ipc.h"
25 #include "email-ipc-build.h"
26 #include "email-ipc-api-info.h"
27 #include "email-ipc-param-list.h"
28 #include "email-ipc-socket.h"
29 #include "email-proxy-main.h"
30 #include "email-core-task-manager.h"
31
32 #include "email-debug-log.h"
33 #include "email-errors.h"
34
35 EXPORT_API emipc_param_list *emipc_get_api_parameters(HIPC_API api, EPARAMETER_DIRECTION direction)
36 {
37         EM_DEBUG_FUNC_BEGIN();
38         emipc_email_api_info *api_info = (emipc_email_api_info *)api;
39
40         return (emipc_param_list *)emipc_get_parameters_of_api_info(api_info, direction);
41 }
42
43 EXPORT_API HIPC_API emipc_create_email_api(long api_id)
44 {
45         EM_DEBUG_FUNC_BEGIN();
46
47         emipc_email_api_info *api_info = (emipc_email_api_info *)calloc(1, sizeof(emipc_email_api_info));
48         if(api_info == NULL) {
49                 EM_DEBUG_EXCEPTION("Malloc failed");
50                 return NULL;
51         }
52
53         api_info->api_id = api_id;
54
55         return (HIPC_API)api_info;
56 }
57
58 EXPORT_API void emipc_destroy_email_api(HIPC_API api)
59 {
60         EM_DEBUG_FUNC_BEGIN("API = %p", api);
61         emipc_email_api_info *api_info = (emipc_email_api_info *)api;
62         emipc_free_api_info(api_info);
63         EM_SAFE_FREE(api_info);
64 }
65
66 EXPORT_API long emipc_get_api_id(HIPC_API api)
67 {
68         EM_DEBUG_FUNC_BEGIN();
69         emipc_email_api_info *api_info = (emipc_email_api_info*)api;
70         return api_info->api_id;
71 }
72
73 EXPORT_API long emipc_get_app_id(HIPC_API api)
74 {
75         EM_DEBUG_FUNC_BEGIN();
76         emipc_email_api_info *api_info = (emipc_email_api_info *)api;
77         return api_info->app_id;
78 }
79
80 /* note: there incurs additional cost (malloc & memcpy). */
81 /* if data is a dynamic variable, please use emipc_dynamic_parameter instead */
82 EXPORT_API bool emipc_add_parameter(HIPC_API api, EPARAMETER_DIRECTION direction, void *data, int data_length)
83 {
84         EM_DEBUG_FUNC_BEGIN("data_length[%d]", data_length);
85
86         emipc_param_list *parameters = emipc_get_api_parameters(api, direction);
87         if (!parameters) {
88                 EM_DEBUG_EXCEPTION("emipc_get_api_parameters failed");
89                 return false;
90         }
91
92         return emipc_add_param_to_param_list(parameters, data, data_length);
93 }
94
95 /* caution : data should be a dynamic variable */
96 /*           please, do not use a static variable */
97 EXPORT_API bool emipc_add_dynamic_parameter(HIPC_API api, EPARAMETER_DIRECTION direction, void *data, int data_length)
98 {
99         EM_DEBUG_FUNC_BEGIN("data_length[%d]", data_length);
100         
101         emipc_param_list *parameters = emipc_get_api_parameters(api, direction);
102         if (!parameters) {
103                 EM_DEBUG_EXCEPTION("emipc_get_api_parameters failed");
104                 return false;
105         }
106
107         emipc_add_dynamic_param_to_param_list(parameters, data, data_length);
108         return true;
109 }
110
111
112
113 EXPORT_API int emipc_get_parameter(HIPC_API input_api_handle, EPARAMETER_DIRECTION input_parameter_direction,
114                         int input_parameter_index, int input_parameter_buffer_size, void *output_parameter_buffer)
115 {
116         EM_DEBUG_FUNC_BEGIN("parameter_index [%d], parameter_buffer_size [%d]", input_parameter_index, input_parameter_buffer_size);
117         emipc_param_list *parameters = NULL;
118         void *local_buffer = NULL;
119
120         if (input_parameter_buffer_size == 0 || output_parameter_buffer == NULL) {
121                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
122                 return EMAIL_ERROR_INVALID_PARAM;
123         }
124
125         parameters = emipc_get_api_parameters(input_api_handle, input_parameter_direction);
126
127         if (parameters == NULL) {
128                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
129                 return EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
130         }
131
132         local_buffer = emipc_get_param_of_param_list(parameters, input_parameter_index);
133
134         if (local_buffer == NULL) {
135                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
136                 return EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
137         }
138
139         if (emipc_get_param_len_of_param_list(parameters, input_parameter_index) != input_parameter_buffer_size) {
140                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
141                 return EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
142         }
143
144         memcpy(output_parameter_buffer, local_buffer, input_parameter_buffer_size);
145
146         EM_DEBUG_FUNC_END();
147         return EMAIL_ERROR_NONE;
148 }
149
150
151 EXPORT_API void* emipc_get_nth_parameter_data(HIPC_API api_handle, EPARAMETER_DIRECTION direction, int param_index)
152 {
153         EM_DEBUG_FUNC_BEGIN("nth_parameter_index [%d]", param_index);
154         emipc_param_list *parameters = NULL;
155         void *buf = NULL;
156
157         parameters = emipc_get_api_parameters(api_handle, direction);
158
159         if (parameters == NULL) {
160                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
161                 return NULL;
162         }
163
164         buf = emipc_get_param_of_param_list(parameters, param_index);
165
166         if (!buf) {
167                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_IPC_PROTOCOL_FAILURE");
168                 return NULL;
169         }
170
171         EM_DEBUG_FUNC_END();
172         return buf;
173 }
174
175
176 EXPORT_API int emipc_get_parameter_length(HIPC_API api, EPARAMETER_DIRECTION direction, int parameter_index)
177 {
178         EM_DEBUG_FUNC_BEGIN();
179         emipc_param_list *parameters = emipc_get_api_parameters(api, direction);
180         if (parameters) {
181                 EM_DEBUG_FUNC_END("Suceeded");
182                 return emipc_get_param_len_of_param_list(parameters, parameter_index);
183         }
184         EM_DEBUG_FUNC_END("Failed");
185         return -1;
186 }
187
188 EXPORT_API int emipc_get_nth_parameter_length(HIPC_API api, EPARAMETER_DIRECTION direction, int parameter_index)
189 {
190         EM_DEBUG_FUNC_BEGIN();
191         emipc_param_list *parameters = emipc_get_api_parameters(api, direction);
192         if (parameters) {
193                 EM_DEBUG_FUNC_END("Suceeded");
194                 return emipc_get_param_len_of_param_list(parameters, parameter_index);
195         }
196         EM_DEBUG_FUNC_END("Failed");
197         return -1;
198 }
199
200 EXPORT_API int emipc_execute_proxy_task(email_task_type_t input_task_type, void *input_task_parameter)
201 {
202         EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_parameter [%p]", input_task_type, input_task_parameter);
203
204         int err = EMAIL_ERROR_NONE;
205         int task_parameter_length = 0;
206         char *task_parameter_stream = NULL;
207         HIPC_API hAPI = NULL;
208
209         if(input_task_parameter == NULL) {
210                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
211                 err = EMAIL_ERROR_INVALID_PARAM;
212                 goto FINISH_OFF;
213         }
214
215         if((err = emcore_encode_task_parameter(input_task_type, input_task_parameter, &task_parameter_stream, &task_parameter_length)) != EMAIL_ERROR_NONE) {
216                 EM_DEBUG_EXCEPTION("emcore_encode_task_parameter failed [%d]", err);
217                 goto FINISH_OFF;
218         }
219
220         hAPI = emipc_create_email_api(input_task_type);
221
222         if(!hAPI) {
223                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
224                 err = EMAIL_ERROR_NULL_VALUE;
225                 goto FINISH_OFF;
226         }
227
228         if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)task_parameter_stream, task_parameter_length)) {
229                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
230                 err = EMAIL_ERROR_OUT_OF_MEMORY;
231                 goto FINISH_OFF;
232         }
233
234         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
235                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
236                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
237                 goto FINISH_OFF;
238         }
239
240         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
241
242         FINISH_OFF:
243         if(hAPI)
244                 emipc_destroy_email_api(hAPI);
245
246         EM_DEBUG_FUNC_END("err [%d]", err);
247         return err;
248 }
249