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.
26 #include "email-ipc-build.h"
27 #include "email-ipc-param-list.h"
29 #include "email-debug-log.h"
30 #include "email-utilities.h"
33 /* +----------------------------------------------------------------------------------------------------------+ */
34 /* | API ID(4B) | Resp. ID (4B) | Param Count(4B) | Param1 Len | Param1 Data | ... | ParamN Len | ParamN data | */
35 /* +----------------------------------------------------------------------------------------------------------+ */
38 EXPORT_API emipc_param_list *emipc_create_param_list()
40 emipc_param_list *new_param_list = NULL;
42 new_param_list = (emipc_param_list *) em_malloc (sizeof(emipc_param_list));
43 if (new_param_list == NULL) {
44 EM_DEBUG_EXCEPTION("em_malloc failed.");
47 memset(new_param_list, 0x00, sizeof(emipc_param_list));
49 return new_param_list;
52 EXPORT_API bool emipc_destroy_param_list(emipc_param_list *param_list)
58 EM_DEBUG_EXCEPTION("Invalid parameter.");
62 for (index = 0; index < count; index++) {
63 emipc_free_param(param_list->params[index]);
65 EM_SAFE_FREE(param_list->byte_stream);
66 EM_SAFE_FREE(param_list);
70 /* making stream into param length and param data */
71 EXPORT_API bool emipc_parse_stream_of_param_list(emipc_param_list *param_list, void *stream)
73 EM_DEBUG_FUNC_BEGIN();
74 long parameter_count = *((long *)stream + eSTREAM_COUNT);
75 if(parameter_count <= 0) {
76 EM_DEBUG_EXCEPTION("INVALID_PARAM : count %d", parameter_count);
80 unsigned char* cur = ((unsigned char*)stream) + sizeof(int)*eSTREAM_DATA;
83 /* stream is composed of data type which is encoded into length and data field */
85 for(i = 0; i < parameter_count; i++) {
87 memcpy(&len, cur, sizeof(int));
89 /* moving from length field to data field */
91 emipc_add_param_to_param_list(param_list, (void*)cur, len);
93 EM_DEBUG_LOG("Parsing stream : element %d is %dbyte long ", i, len);
95 /* move to next parameter */
103 EXPORT_API unsigned char *emipc_serialize_param_list(emipc_param_list *param_list, int *stream_length)
105 EM_DEBUG_FUNC_BEGIN();
106 if(param_list->byte_stream)
107 return param_list->byte_stream;
109 int stream_len = emipc_sum_param_list_length (param_list);
111 if (stream_len <= 0) {
112 EM_DEBUG_EXCEPTION("stream_len error %d", stream_len);
113 EM_SAFE_FREE(param_list->byte_stream);
117 param_list->byte_stream = (unsigned char*)calloc(1, stream_len);
118 int pos = sizeof(long)*eSTREAM_COUNT;
120 if (pos + (int)sizeof(param_list->param_count) > stream_len ) {
121 EM_DEBUG_EXCEPTION("%d > stream_len", pos + sizeof(param_list->param_count));
122 EM_SAFE_FREE(param_list->byte_stream);
126 memcpy((param_list->byte_stream + pos), ¶m_list->param_count, sizeof(param_list->param_count));
129 int index = 0, length = 0;
132 /* | param1 length | (param1 data) | param2 length | (param2 data) | ... |*/
133 /* if param is 0 long, the param data is omitted */
134 for(index=0; index<param_list->param_count; index++) {
135 length = emipc_get_length(param_list->params[index]);
137 EM_DEBUG_EXCEPTION("index = %d, length = %d", index, length);
138 EM_SAFE_FREE(param_list->byte_stream);
142 if (pos + (int)sizeof(long) > stream_len) {
143 EM_DEBUG_EXCEPTION("%d > stream_len", pos + sizeof(long));
144 EM_SAFE_FREE(param_list->byte_stream);
147 /* write param i length */
148 memcpy((param_list->byte_stream+pos), &length, sizeof(long));
151 if (pos + length > stream_len) {
152 EM_DEBUG_EXCEPTION("%d > stream_len", pos + length);
153 EM_SAFE_FREE(param_list->byte_stream);
156 /* write param i data if length is greater than 0 */
158 memcpy((param_list->byte_stream+pos), emipc_get_data(param_list->params[index]), length);
162 *stream_length = stream_len;
165 return param_list->byte_stream;
168 EXPORT_API int emipc_sum_param_list_length(emipc_param_list *param_list)
170 int length = sizeof(long) * eSTREAM_DATA;
172 for (index = 0; index < param_list->param_count; index++) {
173 length += sizeof(long);
174 length += emipc_get_length(param_list->params[index]);
179 EXPORT_API bool emipc_add_param_to_param_list(emipc_param_list *param_list, void *data, int len)
181 EM_DEBUG_FUNC_BEGIN();
182 if (emipc_set_param(&(param_list->params[param_list->param_count]), data, len)) {
183 param_list->param_count++;
184 EM_SAFE_FREE(param_list->byte_stream);
190 EXPORT_API void emipc_add_dynamic_param_to_param_list(emipc_param_list *param_list, void *data, int len)
192 EM_DEBUG_FUNC_BEGIN();
193 emipc_set_dynamic_param(&(param_list->params[param_list->param_count]), data, len);
194 param_list->param_count++;
195 EM_SAFE_FREE(param_list->byte_stream);
198 EXPORT_API void *emipc_get_param_of_param_list(emipc_param_list *param_list, int index)
200 EM_DEBUG_FUNC_BEGIN("index [%d]", index);
201 if (index < 0 || index >= param_list->param_count) {
202 EM_DEBUG_EXCEPTION("Index value is not valid");
205 return emipc_get_data(param_list->params[index]);
208 EXPORT_API int emipc_get_param_len_of_param_list(emipc_param_list *param_list, int index)
210 EM_DEBUG_FUNC_BEGIN();
211 if (index < 0 || index >= param_list->param_count) {
212 EM_DEBUG_EXCEPTION("Index valud is not valid");
215 return emipc_get_length(param_list->params[index]);