summaryrefslogtreecommitdiff
path: root/src/agent/dm-engine/dl-manager/dd_parser.c
blob: d17eeb5dd3cf181f84e258bb43edead8487314c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * oma-dm-agent
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*lib*/
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

/*sync-agent*/
#include <sync_agent.h>

/*dm-agent*/
#include "common/dm_common.h"
#include "dm-engine/dl-manager/dd_parser.h"

#ifndef OMADM_AGENT_LOG
#undef LOG_TAG
#define LOG_TAG	"OMA_DM_DL"
#endif

typedef enum {
	DD_NODE_NO_TYPE = 0,
	DD_NODE_OBJECT_TYPE,
	DD_NODE_DESCRIPTION,
	DD_NODE_OBJECT_URI,
	DD_NODE_SIZE,
	DD_NODE_INSTALL_NOTIFY_URI
} DD_NODE_TYPE;

static DD_NODE_TYPE ___get_node_type(unsigned char *node_name);
static DM_ERROR __set_value_to_dd_object(xmlNode * a_node, Download_Descriptor ** dd_object);
static DM_ERROR _convert_doc_to_dd_object(xmlDocPtr doc, Download_Descriptor ** dd_object);

DM_ERROR convert_xml_to_DD_object(const char *dd_xml, unsigned int dd_xml_length, Download_Descriptor ** dd_object)
{
	_EXTERN_FUNC_ENTER;

	DM_ERROR ret = DM_OK;
	/* parse the in memory document */
	xmlDocPtr doc = NULL;

	retvm_if((dd_xml) == NULL, COMMON_ERR_IS_NULL, "dd_xml is NULL!!");

	if (dd_xml == NULL || dd_xml_length == 0 || *dd_object == NULL) {
		_DEBUG_INFO("dd_xml == NULL || dd_xml_length == 0 || *dd_object == NULL");
		ret = COMMON_ERR_IS_NULL;
		goto error;
	}
	_DEBUG_INFO("xml: %s, size: %d", dd_xml, dd_xml_length);
	doc = xmlReadMemory(dd_xml, dd_xml_length, 0, 0, 0);
	if (doc == NULL) {
		_DEBUG_INFO("xmlReadMemory() Fail\n");
		ret = COMMON_ERR_IS_NULL;
		goto error;
	}
	/* convert doc to dd_object */
	ret = _convert_doc_to_dd_object(doc, dd_object);
	_DEBUG_INFO("convert doc to dd object : %d", ret);

 error:

	if (doc != NULL)
		xmlFreeDoc(doc);

	_EXTERN_FUNC_EXIT;
	return ret;
}

static DD_NODE_TYPE ___get_node_type(unsigned char *node_name)
{
	_INNER_FUNC_ENTER;

	_DEBUG_VERBOSE("node_name: %s\n", node_name);

	if (strcmp((const char *)node_name, "type") == 0) {
		_INNER_FUNC_EXIT;
		return DD_NODE_OBJECT_TYPE;
	} else if (strcmp((const char *)node_name, "description") == 0) {
		_INNER_FUNC_EXIT;
		return DD_NODE_DESCRIPTION;
	} else if (strcmp((const char *)node_name, "objectURI") == 0) {
		_INNER_FUNC_EXIT;
		return DD_NODE_OBJECT_URI;
	} else if (strcmp((const char *)node_name, "size") == 0) {
		_INNER_FUNC_EXIT;
		return DD_NODE_SIZE;
	} else if (strcmp((const char *)node_name, "installNotifyURI") == 0) {
		_INNER_FUNC_EXIT;
		return DD_NODE_INSTALL_NOTIFY_URI;
	}

	_INNER_FUNC_EXIT;
	return DD_NODE_NO_TYPE;
}

static DM_ERROR __set_value_to_dd_object(xmlNode * a_node, Download_Descriptor ** dd_object)
{
	_INNER_FUNC_ENTER;
	DM_ERROR ret = DM_OK;
	xmlNode *cur_node = 0;
	DD_NODE_TYPE node_type = DD_NODE_NO_TYPE;

	retvm_if((a_node) == NULL, COMMON_ERR_IS_NULL, "a_node is NULL!!");

	unsigned char *content = NULL;
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			content = xmlNodeGetContent(cur_node);

			_DEBUG_VERBOSE("node type: Element, name: %s\n", cur_node->name);
			_DEBUG_VERBOSE("node type: Element, content: %s\n", content);

			node_type = ___get_node_type((unsigned char *)(cur_node->name));
			switch (node_type) {
			case DD_NODE_OBJECT_TYPE:
				{
					if (content != NULL) {
						(*dd_object)->object_type = strdup((const char *)content);
					}
				}
				break;
			case DD_NODE_DESCRIPTION:
				{
					if (content != NULL) {
						(*dd_object)->object_description = strdup((const char *)content);
					}
				}
				break;
			case DD_NODE_OBJECT_URI:
				{
#ifdef _DM_BUNDANG_TEST
					if (content != NULL) {
						(*dd_object)->object_uri = get_new_uri((char *)content);
					}
#else
					if (content != NULL) {
						(*dd_object)->object_uri = strdup((const char *)content);
					}
#endif
					if ((*dd_object)->object_uri != NULL) {
						_DEBUG_VERBOSE("object_uri = %s\n", (*dd_object)->object_uri);
					} else {
						_DEBUG_VERBOSE("object uri null");
					}
				}
				break;
			case DD_NODE_SIZE:
				{
					if (content != NULL) {
						(*dd_object)->object_size = atoi((const char *)content);
					}
				}
				break;
			case DD_NODE_INSTALL_NOTIFY_URI:
				{
#ifdef _DM_BUNDANG_TEST
					if (content != NULL) {
						(*dd_object)->install_notify_uri = get_new_uri((char *)content);
					}
#else
					if (content != NULL) {
						(*dd_object)->install_notify_uri = strdup((const char *)content);
					}
#endif
					if ((*dd_object)->install_notify_uri != NULL) {
						_DEBUG_VERBOSE("install_notify_uri = %s\n", (*dd_object)->install_notify_uri);
					} else {
						_DEBUG_VERBOSE("install notify uri null");
					}
				}
				break;
			default:
				break;
			}
		}

		ret = __set_value_to_dd_object(cur_node->children, dd_object);
		_DEBUG_VERBOSE("set value to dd object : %d", ret);
	}

	_INNER_FUNC_EXIT;
	return DM_OK;
}

static DM_ERROR _convert_doc_to_dd_object(xmlDocPtr doc, Download_Descriptor ** dd_object)
{
	_INNER_FUNC_ENTER;

	DM_ERROR ret = DM_OK;

	xmlNode *root_element = 0;

	/* get the root element node */
	root_element = xmlDocGetRootElement(doc);
	if (root_element == NULL) {
		_INNER_FUNC_EXIT;
		return COMMON_ERR_IS_NULL;
	}

	/*DD_NODE_TYPE node_type = DD_NODE_NO_TYPE; */
	ret = __set_value_to_dd_object(root_element, dd_object);
	_DEBUG_TRACE("set value dd object : %d", ret);

	_INNER_FUNC_EXIT;
	return ret;
}