Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / device-manager / mo_tnds_processor.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <wbxml/wbxml.h>
19
20 #include "utility/sync_util.h"
21
22 #include "protocol-binder/interface.h"
23 #include "device-manager/mo_accessor_internal.h"
24 #include "device-manager/mo_accessor.h"
25
26 #include "device-manager/mo_tnds_processor.h"
27
28 #ifndef SYNC_AGENT_LOG
29 #undef LOG_TAG
30 #define LOG_TAG "AF_MO"
31 #endif
32
33 typedef struct {
34         sync_agent_dm_tnds_action_e tnds_action;
35         sync_agent_dm_mo_type_e mo_type;
36         char *mo_full_path;
37 } dm_tnds_binder_data_s;
38
39 static WBXMLTreeNode *__construct_TNDS_NODE(sync_agent_dm_mo_node_s * mo_node, sync_agent_pb_protocol_binder_info_s * pBinder, WBXMLTreeNode * ppWbxml_parent_node);
40 static WBXMLTreeNode *__recursive_contruct_TNDS(sync_agent_dm_mo_node_s * mo_parent_node, sync_agent_pb_protocol_binder_info_s * pBinder, WBXMLTreeNode * ppWbxml_parent_node);
41 static void __recursive_contruct_MO_TABLE(sync_agent_dm_tnds_action_e tnds_action, sync_agent_dm_mo_type_e mo_type, char *mo_parent_path, WBXMLTreeNode * pWbxml_dom_node);
42
43 static sync_agent_pb_error_e _mgmttree_start_converter_function(sync_agent_pb_protocol_binder_info_s * pBinder, Content_Ptr pContent, WBXMLTreeNode ** ppWbxml_dom_node);
44 static sync_agent_pb_error_e _verdtd_converter_function(sync_agent_pb_protocol_binder_info_s * pBinder, Content_Ptr pContent, WBXMLTreeNode ** ppWbxml_dom_node);
45 static sync_agent_pb_error_e _node_start_converter_function(sync_agent_pb_protocol_binder_info_s * pBinder, Content_Ptr pContent, WBXMLTreeNode ** ppWbxml_dom_node);
46 static sync_agent_pb_error_e _node_start_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * pReverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * pContent);
47
48 const sync_agent_pb_function_info_s tnds_binder_function_info[] = {
49         {DM_TNDS_TAG_MGMTTREE_START, "MgmtTree", _mgmttree_start_converter_function, NULL, false},
50         {DM_TNDS_TAG_MGMTTREE_END, "/MgmtTree", NULL, NULL, false},
51         {DM_TNDS_TAG_VERDTD, "VerDTD", _verdtd_converter_function, NULL, true},
52         {DM_TNDS_TAG_NODE_START, "Node", _node_start_converter_function, _node_start_reverse_converter_function, false},
53         {DM_TNDS_TAG_NODE_END, "/Node", NULL, NULL, false}
54 };
55
56 sync_agent_dm_mo_error_e dm_get_tnds_from_db(sync_agent_dm_tnds_type_e tnds_type, sync_agent_dm_mo_node_s * root_node, char **output_tnds, unsigned int *byte_size)
57 {
58         _EXTERN_FUNC_ENTER;
59
60         retvm_if(root_node == NULL, SYNC_AGENT_DM_MO_FAIL, "sync_agent_dm_mo_node_s is NULL !!");
61
62         sync_agent_dm_mo_error_e mo_error = SYNC_AGENT_DM_MO_SUCCESS;
63
64         sync_agent_pb_protocol_binder_function_set_s *pBinder_function_set = NULL;
65         sync_agent_pb_error_e pb_error = sync_agent_init_binder_function_set(sizeof(tnds_binder_function_info) / sizeof(sync_agent_pb_function_info_s),
66                                                                              (sync_agent_pb_function_info_s *) tnds_binder_function_info, &pBinder_function_set);
67
68         if (pb_error != SYNC_AGENT_PB_RETURN_OK) {
69                 _DEBUG_ERROR("Failed to sync_agent_init_binder_function_set()");
70                 return SYNC_AGENT_DM_MO_FAIL;
71         }
72
73         /* protocol binder initialization */
74         sync_agent_pb_protocol_binder_info_s *pBinder = NULL;
75         switch (tnds_type) {
76         case SYNC_AGENT_DM_MO_TNDS_TYPE_XML:
77                 {
78                         pBinder = sync_agent_init_protocol_binder(NULL, SYNC_AGENT_PB_PROTOCOL_SYNCML_DMDDF12, NULL, SYNC_AGENT_PB_ENCODING_XML, false, false, false, false, false, NULL, NULL, pBinder_function_set);
79                 }
80                 break;
81         case SYNC_AGENT_DM_MO_TNDS_TYPE_WBXML:
82                 {
83                         pBinder = sync_agent_init_protocol_binder(NULL, SYNC_AGENT_PB_PROTOCOL_SYNCML_DMDDF12, NULL, SYNC_AGENT_PB_ENCODING_WBXML_13, false, false, false, false, false, NULL, NULL, pBinder_function_set);
84                 }
85                 break;
86         default:
87                 _DEBUG_ERROR("TNDS TYPE [%d], Framework could not support!!", tnds_type);
88                 break;
89         }
90
91         pb_error = sync_agent_append_element(pBinder, DM_TNDS_TAG_MGMTTREE_START, NULL);
92
93         pb_error = sync_agent_append_element(pBinder, DM_TNDS_TAG_VERDTD, NULL);
94
95         pb_error = sync_agent_append_element(pBinder, DM_TNDS_TAG_NODE_START, root_node);
96         pb_error = sync_agent_append_element(pBinder, DM_TNDS_TAG_NODE_END, NULL);
97
98         pb_error = sync_agent_append_element(pBinder, DM_TNDS_TAG_MGMTTREE_END, NULL);
99
100         pb_error = sync_agent_get_stream_from_protocol_binder(pBinder, output_tnds, byte_size);
101
102         char *xml = NULL;
103         unsigned int xml_size = 0;
104         pb_error = sync_agent_get_xml_from_protocol_binder(pBinder, &xml, &xml_size);
105
106         fprintf(stderr, "xmlsize = %d, xml =\n%s\n", xml_size, xml);
107
108         sync_agent_destroy_protocol_binder(pBinder);
109
110         _EXTERN_FUNC_EXIT;
111
112         return mo_error;
113 }
114
115 sync_agent_dm_mo_error_e dm_insert_tnds_to_db(sync_agent_dm_tnds_action_e tnds_action, sync_agent_dm_tnds_type_e tnds_type, sync_agent_dm_mo_type_e mo_type, const char *mo_full_path, char *input_stream, unsigned int byte_size)
116 {
117         _EXTERN_FUNC_ENTER;
118
119         retvm_if(mo_full_path == NULL, SYNC_AGENT_DM_MO_FAIL, "mo_full_path is NULL !!");
120
121         sync_agent_pb_protocol_binder_function_set_s *pBinder_function_set = NULL;
122         sync_agent_pb_error_e pb_error = sync_agent_init_binder_function_set(sizeof(tnds_binder_function_info) / sizeof(sync_agent_pb_function_info_s),
123                                                                              (sync_agent_pb_function_info_s *) tnds_binder_function_info, &pBinder_function_set);
124
125         if (pb_error != SYNC_AGENT_PB_RETURN_OK) {
126                 _DEBUG_ERROR("Failed to sync_agent_init_binder_function_set()");
127                 return SYNC_AGENT_DM_MO_FAIL;
128         }
129
130         /*
131          * reverse protocol binder initialization
132          * */
133         sync_agent_pb_protocol_e protocol = SYNC_AGENT_PB_PROTOCOL_UNKNOWN;
134         sync_agent_pb_protocol_binder_reverse_info_s *pBinder = NULL;
135         dm_tnds_binder_data_s binder_data;
136         binder_data.tnds_action = tnds_action;
137         binder_data.mo_full_path = (char *)mo_full_path;
138         binder_data.mo_type = mo_type;
139
140         switch (tnds_type) {
141         case SYNC_AGENT_DM_MO_TNDS_TYPE_XML:
142                 {
143                         pb_error = sync_agent_init_reverse_protocol_binder(input_stream, byte_size, SYNC_AGENT_PB_DECODING_XML, &protocol, pBinder_function_set, &binder_data, &pBinder);
144                 }
145                 break;
146         case SYNC_AGENT_DM_MO_TNDS_TYPE_WBXML:
147                 {
148                         pb_error = sync_agent_init_reverse_protocol_binder(input_stream, byte_size, SYNC_AGENT_PB_DECODING_WBXML, &protocol, pBinder_function_set, NULL, &pBinder);
149                 }
150                 break;
151         default:
152                 _DEBUG_ERROR("TNDS TYPE [%d], Framework could not support!!", tnds_type);
153                 break;
154         }
155
156         if (pb_error != SYNC_AGENT_PB_RETURN_OK) {
157                 _DEBUG_ERROR("Failed to sync_agent_init_reverse_protocol_binder()");
158                 sync_agent_destroy_reverse_protocol_binder(pBinder);
159                 return SYNC_AGENT_DM_MO_FAIL;
160         }
161
162         if (protocol != SYNC_AGENT_PB_PROTOCOL_SYNCML_DMDDF12) {
163                 _DEBUG_ERROR("Failed to sync_agent_init_reverse_protocol_binder()");
164                 sync_agent_destroy_reverse_protocol_binder(pBinder);
165                 return SYNC_AGENT_DM_MO_FAIL;
166         }
167
168         Protocol_Element protocol_element;
169         char *protocol_element_name = NULL;
170         Content_Ptr content = NULL;
171
172         pb_error = sync_agent_next_element(pBinder, &protocol_element, &protocol_element_name, &content);
173
174         sync_agent_destroy_reverse_protocol_binder(pBinder);
175
176         if (pb_error != SYNC_AGENT_PB_RETURN_OK) {
177                 _DEBUG_ERROR("Failed to sync_agent_next_element()");
178                 sync_agent_destroy_reverse_protocol_binder(pBinder);
179                 return SYNC_AGENT_DM_MO_FAIL;
180         }
181
182         _EXTERN_FUNC_EXIT;
183
184         return SYNC_AGENT_DM_MO_SUCCESS;
185 }
186
187 /**************************************** Impl static function *******************************************/
188
189 static WBXMLTreeNode *__construct_TNDS_NODE(sync_agent_dm_mo_node_s * mo_node, sync_agent_pb_protocol_binder_info_s * pBinder, WBXMLTreeNode * ppWbxml_parent_node)
190 {
191         _INNER_FUNC_ENTER;
192
193         retvm_if(mo_node == NULL, NULL, "sync_agent_dm_mo_node_s is NULL !!");
194         retvm_if(pBinder == NULL, NULL, "sync_agent_pb_protocol_binder_info_s is NULL !!");
195
196         WBXMLTreeNode *node_node = NULL;
197
198         if (ppWbxml_parent_node == NULL) {
199                 node_node = sync_agent_create_node(pBinder, DM_TNDS_ELEMENT_NODE);
200         } else {
201                 node_node = sync_agent_add_child_node(pBinder, ppWbxml_parent_node, DM_TNDS_ELEMENT_NODE);
202         }
203
204         if (node_node == NULL) {
205                 return NULL;
206         }
207
208         sync_agent_add_child_text_node(pBinder, node_node, DM_TNDS_ELEMENT_NODENAME, mo_node->name, strlen(mo_node->name));
209
210         WBXMLTreeNode *rtproperties_node = sync_agent_add_child_node(pBinder, node_node, DM_TNDS_ELEMENT_RTPROPERTIES);
211
212         sync_agent_dm_mo_runtime_property_s *runtime_property = mo_node->runtime_property;
213         if (runtime_property != NULL) {
214                 int acl_len = 0;
215                 if (runtime_property->acl != NULL) {
216                         acl_len = strlen(runtime_property->acl);
217                 }
218                 sync_agent_add_child_text_node(pBinder, rtproperties_node, DM_TNDS_ELEMENT_ACL, runtime_property->acl, acl_len);
219
220                 WBXMLTreeNode *format_node = sync_agent_add_child_node(pBinder, rtproperties_node, DM_TNDS_ELEMENT_FORMAT);
221                 switch (runtime_property->format) {
222                 case SYNC_AGENT_DM_MO_FORMAT_B64:
223                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_B64);
224                         break;
225                 case SYNC_AGENT_DM_MO_FORMAT_BIN:
226                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_BIN);
227                         break;
228                 case SYNC_AGENT_DM_MO_FORMAT_BOOL:
229                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_BOOL);
230                         break;
231                 case SYNC_AGENT_DM_MO_FORMAT_CHR:
232                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_CHR);
233                         break;
234                 case SYNC_AGENT_DM_MO_FORMAT_INT:
235                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_INT);
236                         break;
237                 case SYNC_AGENT_DM_MO_FORMAT_NODE:
238                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_NODE);
239                         break;
240                 case SYNC_AGENT_DM_MO_FORMAT_NULL:
241                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_NULL);
242                         break;
243                 case SYNC_AGENT_DM_MO_FORMAT_XML:
244                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_XML);
245                         break;
246                 case SYNC_AGENT_DM_MO_FORMAT_DATE:
247                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_DATE);
248                         break;
249                 case SYNC_AGENT_DM_MO_FORMAT_TIME:
250                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_TIME);
251                         break;
252                 case SYNC_AGENT_DM_MO_FORMAT_FLOAT:
253                         sync_agent_add_child_node(pBinder, format_node, DM_TNDS_ELEMENT_FORMAT_FLOAT);
254                         break;
255                 default:
256                         break;
257                 }
258
259                 int title_len = 0;
260                 if (runtime_property->title != NULL) {
261                         title_len = strlen(runtime_property->title);
262                 }
263                 sync_agent_add_child_text_node(pBinder, rtproperties_node, DM_TNDS_ELEMENT_TITLE, runtime_property->title, title_len);
264
265                 int tstamp_len = 0;
266                 if (runtime_property->tStamp != NULL) {
267                         tstamp_len = strlen(runtime_property->tStamp);
268                 }
269                 sync_agent_add_child_text_node(pBinder, rtproperties_node, DM_TNDS_ELEMENT_TSTAMP, runtime_property->tStamp, tstamp_len);
270
271                 WBXMLTreeNode *type_node = sync_agent_add_child_node(pBinder, rtproperties_node, DM_TNDS_ELEMENT_TYPE);
272                 int value_len = 0;
273                 if (runtime_property->type_value != NULL) {
274                         value_len = strlen(runtime_property->type_value);
275                 }
276                 switch (runtime_property->type) {
277                 case SYNC_AGENT_DM_MO_DF_TYPE_MIME:
278                         sync_agent_add_child_text_node(pBinder, type_node, DM_TNDS_ELEMENT_TYPE_MIME, runtime_property->type_value, value_len);
279                         break;
280                 case SYNC_AGENT_DM_MO_DF_TYPE_DDFNAME:
281                         sync_agent_add_child_text_node(pBinder, type_node, DM_TNDS_ELEMENT_TYPE_DDFNAME, runtime_property->type_value, value_len);
282                         break;
283                 default:
284                         break;
285                 }
286
287                 int verno_len = 0;
288                 if (runtime_property->verNo != NULL) {
289                         verno_len = strlen(runtime_property->verNo);
290                 }
291                 sync_agent_add_child_text_node(pBinder, rtproperties_node, DM_TNDS_ELEMENT_VERNO, runtime_property->verNo, verno_len);
292         }
293
294         if (mo_node->type == SYNC_AGENT_DM_MO_NODE_LEAF) {
295                 int value_len = 0;
296                 if (mo_node->value != NULL) {
297                         value_len = strlen(mo_node->value);
298                 }
299                 sync_agent_add_child_text_node(pBinder, node_node, DM_TNDS_ELEMENT_VALUE, mo_node->value, value_len);
300         }
301
302         _INNER_FUNC_EXIT;
303
304         return node_node;
305 }
306
307 static WBXMLTreeNode *__recursive_contruct_TNDS(sync_agent_dm_mo_node_s * mo_parent_node, sync_agent_pb_protocol_binder_info_s * pBinder, WBXMLTreeNode * ppWbxml_parent_node)
308 {
309         _INNER_FUNC_ENTER;
310
311         retvm_if(mo_parent_node == NULL, NULL, "sync_agent_dm_mo_node_s is NULL !!");
312         retvm_if(pBinder == NULL, NULL, "sync_agent_pb_protocol_binder_info_s is NULL !!");
313
314         int i = 0;
315         _DEBUG_VERBOSE("%d", mo_parent_node->child_node_cnt);
316         _DEBUG_VERBOSE("%s", mo_parent_node->name);
317
318         for (; i < mo_parent_node->child_node_cnt; i++) {
319                 sync_agent_dm_mo_node_s *mo_node = &(mo_parent_node->child_node_list[i]);
320                 _DEBUG_VERBOSE("[%d] mo_node->name : %s", i, mo_node->name);
321
322                 WBXMLTreeNode *node_node = __construct_TNDS_NODE(mo_node, pBinder, ppWbxml_parent_node);
323
324                 __recursive_contruct_TNDS(mo_node, pBinder, node_node);
325         }
326
327         _INNER_FUNC_EXIT;
328
329         return NULL;
330 }
331
332 static void __recursive_contruct_MO_TABLE(sync_agent_dm_tnds_action_e tnds_action, sync_agent_dm_mo_type_e mo_type, char *mo_parent_path, WBXMLTreeNode * wbxml_dom_node)
333 {
334         _INNER_FUNC_ENTER;
335
336         retm_if(mo_parent_path == NULL, "mo_parent_path is NULL !!");
337         retm_if(wbxml_dom_node == NULL, "WBXMLTreeNode is NULL !!");
338
339         WBXMLTreeNode *pCursor = wbxml_dom_node->children;
340         int hasChildNode = 0;
341         sync_agent_dm_mo_node_s mo_node;
342         sync_agent_dm_mo_runtime_property_s mo_runtime_property;
343         memset(&mo_node, 0x00, sizeof(sync_agent_dm_mo_node_s));
344         memset(&mo_runtime_property, 0x00, sizeof(sync_agent_dm_mo_runtime_property_s));
345         mo_node.runtime_property = &mo_runtime_property;
346
347         while (pCursor != NULL) {
348                 if (pCursor->type == WBXML_TREE_ELEMENT_NODE) {
349                         const char *tag_name = (const char *)wbxml_tag_get_xml_name(pCursor->name);
350                         if (!strcmp(tag_name, DM_TNDS_ELEMENT_NODENAME)) {
351                                 char *node_name = NULL;
352                                 sync_agent_pb_error_e err_code = sync_agent_get_text_from_node(pCursor, &node_name);
353                                 if (err_code != SYNC_AGENT_PB_RETURN_OK) {
354                                         _DEBUG_ERROR("Failed to call sync_agent_get_text_from_node()");
355                                 }
356
357                                 mo_node.name = node_name;
358                                 /*
359                                  * full_path = mo_parent_path + "/" + node_name
360                                  */
361                                 int full_path_length = strlen(mo_parent_path) + strlen("/") + strlen(node_name) + 1;
362                                 mo_node.full_path = (char *)calloc(full_path_length, sizeof(char));
363
364                                 if (mo_node.full_path != NULL) {
365                                         snprintf(mo_node.full_path, full_path_length, "%s/%s", mo_parent_path, node_name);
366                                         _DEBUG_VERBOSE("node_name : %s, full_path : %s", node_name, mo_node.full_path);
367                                 }
368                         } else if (!strcmp(tag_name, DM_TNDS_ELEMENT_VALUE)) {
369                                 char *node_value = NULL;
370                                 sync_agent_pb_error_e err_code = sync_agent_get_text_from_node(pCursor, &node_value);
371                                 if (err_code != SYNC_AGENT_PB_RETURN_OK) {
372                                         _DEBUG_ERROR("Failed to call sync_agent_get_text_from_node()");
373                                 }
374                                 _DEBUG_VERBOSE("node_value : %s", node_value);
375                                 mo_node.value = node_value;
376                         } else if (!strcmp(tag_name, DM_TNDS_ELEMENT_RTPROPERTIES)) {
377                                 sync_agent_pb_error_e err_code = SYNC_AGENT_PB_RETURN_OK;
378                                 WBXMLTreeNode *pCursor_rtproperties = pCursor->children;
379                                 while (pCursor_rtproperties != NULL) {
380                                         if (pCursor_rtproperties->type == WBXML_TREE_ELEMENT_NODE) {
381                                                 const char *rtproperties_tag_name = (const char *)wbxml_tag_get_xml_name(pCursor_rtproperties->name);
382                                                 if (!strcmp(rtproperties_tag_name, DM_TNDS_ELEMENT_ACL)) {
383                                                         err_code = sync_agent_get_text_from_node(pCursor_rtproperties, &(mo_node.runtime_property->acl));
384                                                         if (err_code != SYNC_AGENT_PB_RETURN_OK) {
385                                                                 _DEBUG_ERROR("Failed to call sync_agent_get_text_from_node()");
386                                                         }
387                                                 } else if (!strcmp(rtproperties_tag_name, DM_TNDS_ELEMENT_FORMAT)) {
388                                                         WBXMLTreeNode *pCursor_format = pCursor_rtproperties->children;
389                                                         while (pCursor_format != NULL) {
390                                                                 if (pCursor_format->type == WBXML_TREE_ELEMENT_NODE) {
391                                                                         const char *format_tag_name = (const char *)wbxml_tag_get_xml_name(pCursor_format->name);
392                                                                         if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_B64)) {
393                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_B64;
394                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_BIN)) {
395                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_BIN;
396                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_BOOL)) {
397                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_BOOL;
398                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_CHR)) {
399                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_CHR;
400                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_INT)) {
401                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_INT;
402                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_NODE)) {
403                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_NODE;
404                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_NULL)) {
405                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_NULL;
406                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_XML)) {
407                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_XML;
408                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_DATE)) {
409                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_DATE;
410                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_TIME)) {
411                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_TIME;
412                                                                         } else if (!strcmp(format_tag_name, DM_TNDS_ELEMENT_FORMAT_FLOAT)) {
413                                                                                 mo_node.runtime_property->format = SYNC_AGENT_DM_MO_FORMAT_FLOAT;
414                                                                         }
415                                                                         break;
416                                                                 }
417                                                                 pCursor_format = pCursor_format->next;
418                                                         }
419                                                 } else if (!strcmp(rtproperties_tag_name, DM_TNDS_ELEMENT_TITLE)) {
420                                                         err_code = sync_agent_get_text_from_node(pCursor_rtproperties, &(mo_node.runtime_property->title));
421                                                         if (err_code != SYNC_AGENT_PB_RETURN_OK) {
422                                                                 _DEBUG_ERROR("Failed to call sync_agent_get_text_from_node()");
423                                                         }
424                                                 } else if (!strcmp(rtproperties_tag_name, DM_TNDS_ELEMENT_TSTAMP)) {
425                                                         err_code = sync_agent_get_text_from_node(pCursor_rtproperties, &(mo_node.runtime_property->tStamp));
426                                                         if (err_code != SYNC_AGENT_PB_RETURN_OK) {
427                                                                 _DEBUG_ERROR("Failed to call sync_agent_get_text_from_node()");
428                                                         }
429                                                 } else if (!strcmp(rtproperties_tag_name, DM_TNDS_ELEMENT_TYPE)) {
430                                                         WBXMLTreeNode *pCursor_type = pCursor_rtproperties->children;
431                                                         while (pCursor_type != NULL) {
432                                                                 if (pCursor_type->type == WBXML_TREE_ELEMENT_NODE) {
433                                                                         const char *type_tag_name = (const char *)wbxml_tag_get_xml_name(pCursor_type->name);
434                                                                         if (!strcmp(type_tag_name, DM_TNDS_ELEMENT_TYPE_DDFNAME)) {
435                                                                                 mo_node.runtime_property->type = SYNC_AGENT_DM_MO_DF_TYPE_DDFNAME;
436                                                                         } else if (!strcmp(type_tag_name, DM_TNDS_ELEMENT_TYPE_MIME)) {
437                                                                                 mo_node.runtime_property->type = SYNC_AGENT_DM_MO_DF_TYPE_MIME;
438                                                                         }
439                                                                         err_code = sync_agent_get_text_from_node(pCursor_type, &(mo_node.runtime_property->type_value));
440                                                                         if (err_code != SYNC_AGENT_PB_RETURN_OK) {
441                                                                                 _DEBUG_ERROR("Failed to call sync_agent_get_text_from_node()");
442                                                                         }
443
444                                                                         break;
445                                                                 }
446                                                                 pCursor_type = pCursor_type->next;
447                                                         }
448                                                 } else if (!strcmp(rtproperties_tag_name, DM_TNDS_ELEMENT_VERNO)) {
449                                                         err_code = sync_agent_get_text_from_node(pCursor_rtproperties, &(mo_node.runtime_property->verNo));
450                                                         if (err_code != SYNC_AGENT_PB_RETURN_OK) {
451                                                                 _DEBUG_ERROR("Failed to call sync_agent_get_text_from_node()");
452                                                         }
453                                                 }
454                                         }
455
456                                         pCursor_rtproperties = pCursor_rtproperties->next;
457                                 }
458                         } else if (!strcmp(tag_name, DM_TNDS_ELEMENT_NODE)) {
459                                 hasChildNode = 1;
460                         }
461                 }
462                 pCursor = pCursor->next;
463         }
464
465         mo_node.framework_property = NULL;
466
467         /*
468          * Process sync_agent_dm_mo_node_s
469          */
470         sync_agent_begin_transaction_mo();
471
472         if (hasChildNode) {
473                 mo_node.type = SYNC_AGENT_DM_MO_NODE_INTERIOR;
474         } else {
475                 mo_node.type = SYNC_AGENT_DM_MO_NODE_LEAF;
476         }
477
478         switch (tnds_action) {
479         case SYNC_AGENT_DM_MO_TNDS_ACTION_ADD:
480                 {
481                         sync_agent_dm_mo_error_e mo_err = dm_add_mo(mo_type, mo_node.full_path, &mo_node);
482                         if (mo_err != SYNC_AGENT_DM_MO_SUCCESS) {
483                                 _DEBUG_ERROR("Failed to call sync_agent_add_mo(%d, %s)", mo_type, mo_node.full_path);
484                         }
485                 }
486                 break;
487         case SYNC_AGENT_DM_MO_TNDS_ACTION_REPLACE:
488                 {
489                         sync_agent_dm_mo_error_e mo_err = dm_update_mo(mo_type, &mo_node);
490                         if (mo_err != SYNC_AGENT_DM_MO_SUCCESS) {
491                                 _DEBUG_ERROR("Failed to call sync_agent_update_mo(%d, %s)", mo_type, mo_node.full_path);
492                         }
493                 }
494                 break;
495         }
496
497         sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_COMMIT);
498
499         /*
500          * Process Child Nodes recursively
501          */
502         if (hasChildNode != 0) {
503                 pCursor = wbxml_dom_node->children;
504                 while (pCursor != NULL) {
505                         if (pCursor->type == WBXML_TREE_ELEMENT_NODE) {
506                                 const char *tag_name = (const char *)wbxml_tag_get_xml_name(pCursor->name);
507                                 if (!strcmp(tag_name, DM_TNDS_ELEMENT_NODE)) {
508                                         __recursive_contruct_MO_TABLE(tnds_action, mo_type, mo_node.full_path, pCursor);
509                                 }
510                         }
511
512                         pCursor = pCursor->next;
513                 }
514         }
515
516         if (mo_node.full_path != NULL) {
517                 free(mo_node.full_path);
518         }
519
520         _INNER_FUNC_EXIT;
521 }
522
523 static sync_agent_pb_error_e _mgmttree_start_converter_function(sync_agent_pb_protocol_binder_info_s * pBinder, Content_Ptr pContent, WBXMLTreeNode ** ppWbxml_dom_node)
524 {
525         _INNER_FUNC_ENTER;
526
527         retvm_if(pBinder == NULL, SYNC_AGENT_PB_RETURN_UNKNOWN_ERROR, "sync_agent_pb_protocol_binder_info_s is NULL !!");
528
529         sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
530         WBXMLTreeNode *mgmttree_node = sync_agent_create_node(pBinder, DM_TNDS_ELEMENT_MGMTTREE);
531         if (mgmttree_node == NULL) {
532                 err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY;
533                 goto error;
534         }
535
536         *ppWbxml_dom_node = mgmttree_node;
537
538         _INNER_FUNC_EXIT;
539
540         return err;
541
542  error:
543         _DEBUG_ERROR("error :%d", err);
544         return err;
545 }
546
547 static sync_agent_pb_error_e _verdtd_converter_function(sync_agent_pb_protocol_binder_info_s * pBinder, Content_Ptr pContent, WBXMLTreeNode ** ppWbxml_dom_node)
548 {
549         _INNER_FUNC_ENTER;
550
551         retvm_if(pBinder == NULL, SYNC_AGENT_PB_RETURN_UNKNOWN_ERROR, "sync_agent_pb_protocol_binder_info_s is NULL !!");
552
553         sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
554         const char *verdtd = "1.2";     /* todo HardCoding */
555         WBXMLTreeNode *verdtd_node = sync_agent_create_text_node(pBinder, DM_TNDS_ELEMENT_VERDTD, verdtd, strlen(verdtd));
556         if (verdtd_node == NULL) {
557                 err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY;
558                 goto error;
559         }
560
561         *ppWbxml_dom_node = verdtd_node;
562
563         _INNER_FUNC_EXIT;
564
565         return err;
566
567  error:
568         _DEBUG_ERROR("error :%d", err);
569         return err;
570 }
571
572 static sync_agent_pb_error_e _node_start_converter_function(sync_agent_pb_protocol_binder_info_s * pBinder, Content_Ptr pContent, WBXMLTreeNode ** ppWbxml_dom_node)
573 {
574         _INNER_FUNC_ENTER;
575
576         retvm_if(pBinder == NULL, SYNC_AGENT_PB_RETURN_UNKNOWN_ERROR, "sync_agent_pb_protocol_binder_info_s is NULL !!");
577
578         sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
579
580         sync_agent_dm_mo_node_s *root_mo_node = (sync_agent_dm_mo_node_s *) pContent;
581
582         WBXMLTreeNode *root_node_node = __construct_TNDS_NODE(root_mo_node, pBinder, 0);
583         if (root_node_node == NULL) {
584                 err = SYNC_AGENT_PB_RETURN_OUT_OF_MEMORY;
585                 goto error;
586         }
587
588         __recursive_contruct_TNDS(root_mo_node, pBinder, root_node_node);
589
590         *ppWbxml_dom_node = root_node_node;
591
592         _INNER_FUNC_EXIT;
593
594         return err;
595
596  error:
597         _DEBUG_ERROR("error :%d", err);
598         return err;
599 }
600
601 static sync_agent_pb_error_e _node_start_reverse_converter_function(sync_agent_pb_protocol_binder_reverse_info_s * pReverse_binder, WBXMLTreeNode * wbxml_dom_node, Content_Ptr * pContent)
602 {
603         _INNER_FUNC_ENTER;
604
605         retvm_if(pReverse_binder == NULL, SYNC_AGENT_PB_RETURN_UNKNOWN_ERROR, "sync_agent_pb_protocol_binder_reverse_info_s is NULL !!");
606         retvm_if(wbxml_dom_node == NULL, SYNC_AGENT_PB_RETURN_UNKNOWN_ERROR, "WBXMLTreeNode is NULL !!");
607
608         dm_tnds_binder_data_s *binder_data = (dm_tnds_binder_data_s *) sync_agent_get_user_data_from_reverse_protocol_binder(pReverse_binder);
609
610         __recursive_contruct_MO_TABLE(binder_data->tnds_action, binder_data->mo_type, binder_data->mo_full_path, wbxml_dom_node);
611
612         _INNER_FUNC_EXIT;
613
614         return SYNC_AGENT_PB_RETURN_OK;
615 }