3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sync_agent.h>
26 #include "common/dm_common.h"
27 #include "common/util/util.h"
28 #include "dm-engine/dl-manager/fw_downloader.h"
29 #include "dm-engine/dl-manager/dd_parser.h"
30 #include "dm-engine/dl-manager/sa_fw_downloader.h"
31 #include "ipc_agent.h"
33 #ifndef OMADM_AGENT_LOG
35 #define LOG_TAG "OMA_DM_DL"
38 static Data_Resume_Infomation *_alloc_data_resume_infomation();
39 static void _free_data_resume_information(Data_Resume_Infomation * data_resume_info);
40 static int _arrange_file(const char *download_folder, const char *file_name);
41 static char *_get_msg_for_status(DM_ERROR download_status);
43 DM_ERROR get_object_information(char *dd_server_uri, int *file_size, Download_Descriptor ** download_descriptor, DM_ERROR * download_status)
49 retvm_if((dd_server_uri) == NULL, COMMON_ERR_IS_NULL, "dd_server_uri is NULL!!");
51 /* 1. construct HTTP msg */
52 /* 2. send msg to server (about dd_server_uri) */
53 /* 3. receive dd information from server */
54 GList *send_header = 0;
56 unsigned int recvMsg_size = 0;
57 GList *recv_header = 0;
58 ret = send_download_msg(DD_DOWNLOAD, dd_server_uri, "application/vnd.oma.dd+xml", send_header, 0, 0, &recv_header, &recvMsg, &recvMsg_size);
60 *download_status = DM_ERR_LOSS_OF_SERVICE;
61 _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
65 /* 4. realloc or alloc download descriptor object */
67 /* 5. parse the dd information xml data (convert dd xml to dd object) */
68 ret = convert_xml_to_DD_object(recvMsg, recvMsg_size, download_descriptor);
70 _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
71 *download_status = DM_ERR_INVALID_DESCRIPTOR;
75 if (*download_descriptor != NULL && (*download_descriptor)->object_uri != NULL) {
76 *file_size = (*download_descriptor)->object_size;
78 _DEBUG_INFO(" dd->object_uri = %s \n", (*download_descriptor)->object_uri);
79 _DEBUG_INFO(" dd->size = %d \n", (*download_descriptor)->object_size);
80 _DEBUG_INFO(" dd->object_description = %s \n", (*download_descriptor)->object_description);
82 _DEBUG_INFO("dd obejct uri null");
83 ret = DM_ERR_INVALID_DESCRIPTOR;
84 *download_status = DM_ERR_INVALID_DESCRIPTOR;
93 if (recv_header != NULL)
94 free_header_info(recv_header);
100 DM_ERROR check_file_resume(const char *checked_download_folder, DOWNLOAD_FILE_STATUS * file_status, Download_Descriptor * download_descriptor, Data_Resume_Infomation ** data_resume_info)
103 DM_ERROR ret = DM_OK;
105 /* 1. validate download_folder variable */
106 retvm_if((checked_download_folder) == NULL, COMMON_ERR_IS_NULL, "checked_download_folder is NULL!!");
108 if (file_status != NULL) {
109 _DEBUG_INFO("checked_download_folder == 0 && file_status != 0 ");
110 ret = COMMON_ERR_IS_NULL;
115 char *file_name = NULL;
116 char *file_path = NULL;
118 /* send_msg to server */
119 GList *send_header = 0;
121 unsigned int recvMsg_size = 0;
122 GList *recv_header = 0;
123 ret = send_download_msg(GET_FILE_NAME, download_descriptor->object_uri, download_descriptor->object_type, send_header, 0, 0, &recv_header, &recvMsg, &recvMsg_size);
125 _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
129 /* get file_name for header info */
130 file_name = get_file_name(recv_header, download_descriptor->object_uri);
131 if (file_name == NULL) {
132 _DEBUG_INFO("file_name is null");
133 ret = COMMON_ERR_IS_NULL;
138 file_path = g_strdup_printf("%s/%s", checked_download_folder, file_name);
140 _DEBUG_INFO("Download_Folder = %s\n", checked_download_folder);
141 _DEBUG_INFO("Download_Folder_Path = %s\n", file_path);
143 unsigned long file_size = 0;
144 int err = sync_agent_get_file_size(file_path, &file_size);
146 _DEBUG_INFO("NOT_EXIST_FILE\n");
147 *file_status = NOT_EXIST_FILE;
151 _DEBUG_INFO("Exist File Size = %d", file_size);
152 _DEBUG_INFO(" download_descriptor->object_size = %d", download_descriptor->object_size);
153 if (file_size >= download_descriptor->object_size) {
154 _DEBUG_INFO("EXIST_COMPLETED_FILE\n");
155 *file_status = EXIST_COMPLETED_FILE;
161 (*data_resume_info) = _alloc_data_resume_infomation();
162 if ((*data_resume_info) == NULL) {
163 _DEBUG_INFO("calloc failed !!");
164 ret = COMMON_ERR_ALLOC;
165 *file_status = NOT_EXIST_FILE;
168 (*data_resume_info)->file_path = g_strdup_printf("%s", file_path);
169 (*data_resume_info)->current_data_size = file_size;
170 (*data_resume_info)->total_data_size = download_descriptor->object_size;
171 *file_status = EXIST_PARTIAL_FILE;
172 _DEBUG_INFO("EXIST_PARTIAL_FILE\n");
175 err = _arrange_file(checked_download_folder, file_name);
177 ret = COMMON_ERR_GENERIC;
184 if (recv_header != NULL)
185 free_header_info(recv_header);
187 str_free(&file_name);
188 str_free(&file_path);
190 _DEBUG_INFO("END error : %d !!", ret);
195 DM_ERROR download_object(const char *download_folder, char **object_downloaded_path, Data_Resume_Infomation * data_resume_info, int config, Download_Descriptor * download_descriptor, DM_ERROR * download_status)
199 DM_ERROR ret = DM_OK;
201 retvm_if((download_folder) == NULL, COMMON_ERR_IS_NULL, "download_folder is NULL!!");
202 retvm_if((data_resume_info) == NULL, COMMON_ERR_IS_NULL, "data_resume_info is NULL!!");
203 retvm_if((download_descriptor) == NULL, COMMON_ERR_IS_NULL, "download_descriptor is NULL!!");
205 if (data_resume_info->file_path == NULL) {
206 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
207 *download_status = DM_ERR_INVALID_DESCRIPTOR;
212 _DEBUG_INFO(" data_resume_info = %p", data_resume_info);
213 _DEBUG_INFO("file path : %s ", data_resume_info->file_path);
214 _DEBUG_INFO("file size : %d ", data_resume_info->total_data_size);
216 if (!sync_agent_is_existing_fs(data_resume_info->file_path)) {
217 _DEBUG_INFO("FILE IS NOT EXIST");
218 FILE *fp = fopen(data_resume_info->file_path, "w");
224 _DEBUG_INFO("############################################################################");
226 int noti_err = noti_send_download_info(data_resume_info->total_data_size, data_resume_info->file_path);
228 _DEBUG_INFO("download info noti : %d", noti_err);
229 _DEBUG_INFO("############################################################################");
231 ret = COMMON_ERR_IPC;
232 *download_status = DM_ERR_USER_CANDELLED;
235 char *download_path = NULL;
236 int current_file_download_size = 0;
237 if (data_resume_info != NULL) {
238 current_file_download_size = data_resume_info->current_data_size;
241 GList *send_header = 0;
242 ret = download_data(download_descriptor->object_uri, download_descriptor->object_type, send_header, current_file_download_size, (char *)download_folder, config, &download_path);
244 _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
245 *download_status = DM_ERR_USER_CANDELLED;
249 *object_downloaded_path = download_path;
250 *download_status = DM_DD_SUCCESS;
254 if (data_resume_info != NULL) {
255 _free_data_resume_information(data_resume_info);
256 _DEBUG_INFO("__free_Data_Resume_Information() Success\n");
259 _DEBUG_INFO("END : %d !!", ret);
264 DM_ERROR send_donwload_status(DM_ERROR download_status, Download_Descriptor * download_descriptor)
267 DM_ERROR ret = DM_OK;
269 retvm_if((download_descriptor) == NULL, COMMON_ERR_IS_NULL, "download_descriptor is NULL!!");
271 if ((download_descriptor == NULL) || (download_descriptor->install_notify_uri == NULL)) {
273 return COMMON_ERR_IS_NULL;
276 GList *send_header = 0;
279 unsigned int recvMsg_size = 0;
280 GList *recv_header = 0;
282 send_msg = _get_msg_for_status(download_status);
283 if (send_msg != NULL) {
284 ret = send_download_msg(SEND_DOWNLOAD_STATUS, download_descriptor->install_notify_uri, 0, send_header, send_msg, strlen(send_msg), &recv_header, &recvMsg, &recvMsg_size);
286 _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
295 if (recv_header != NULL)
296 free_header_info(recv_header);
302 /* static function implementation*/
304 void free_Download_Descriptor(Download_Descriptor ** download_descriptor)
308 if (*download_descriptor != NULL) {
310 if ((*download_descriptor)->object_name != NULL)
311 free((*download_descriptor)->object_name);
313 if ((*download_descriptor)->object_description != NULL)
314 free((*download_descriptor)->object_description);
316 if ((*download_descriptor)->object_uri != NULL)
317 free((*download_descriptor)->object_uri);
319 if ((*download_descriptor)->next_uri != NULL)
320 free((*download_descriptor)->next_uri);
322 if ((*download_descriptor)->install_notify_uri != NULL)
323 free((*download_descriptor)->install_notify_uri);
325 if ((*download_descriptor)->info_uri != NULL)
326 free((*download_descriptor)->info_uri);
328 if ((*download_descriptor)->icon_uri != NULL)
329 free((*download_descriptor)->icon_uri);
331 if ((*download_descriptor)->object_vender != NULL)
332 free((*download_descriptor)->object_vender);
334 if ((*download_descriptor)->DD_version != NULL)
335 free((*download_descriptor)->DD_version);
337 if ((*download_descriptor)->install_param != NULL)
338 free((*download_descriptor)->install_param);
340 free((*download_descriptor));
343 *download_descriptor = 0;
348 static Data_Resume_Infomation *_alloc_data_resume_infomation()
353 return (Data_Resume_Infomation *) calloc(1, sizeof(Data_Resume_Infomation));
356 static void _free_data_resume_information(Data_Resume_Infomation * data_resume_info)
359 if (data_resume_info != NULL) {
361 if (data_resume_info->file_path != NULL)
362 free(data_resume_info->file_path);
364 free(data_resume_info);
367 data_resume_info = NULL;
371 static int _arrange_file(const char *download_folder, const char *file_name)
375 retvm_if((download_folder) == NULL, 0, "download_folder is NULL!!");
376 retvm_if((file_name) == NULL, 0, "file_name is NULL!!");
379 GSList *fileList = 0;
380 err = sync_agent_get_file_list(download_folder, &fileList);
385 char *temp_file_info = 0;
386 char *temp_file_name = 0;
387 char *temp_file_path = 0;
388 for (iter = fileList; iter != 0; iter = g_slist_next(iter)) {
389 temp_file_info = (char *)(iter->data);
390 _DEBUG_TRACE("temp_file_name = %s\n", temp_file_name);
393 temp_file_name = strtok(temp_file_info, ",");
396 if (strcmp(temp_file_name, file_name) != 0) {
397 temp_file_path = g_strdup_printf("%s/%s", download_folder, temp_file_name);
398 _DEBUG_TRACE("temp_file_path = %s\n", temp_file_path);
399 err = sync_agent_delete_file(temp_file_path);
400 _DEBUG_TRACE("delete file = %d\n", err);
401 str_free(&temp_file_path);
411 static char *_get_msg_for_status(DM_ERROR download_status)
417 switch (download_status) {
419 send_msg = g_strdup_printf("%d Success", download_status);
421 case DM_ERR_INSUFFICIENT_MEMORY:
422 send_msg = g_strdup_printf("%d Insufficient memory", download_status);
424 case DM_ERR_USER_CANDELLED:
425 send_msg = g_strdup_printf("%d User Cancelled", download_status);
427 case DM_ERR_LOSS_OF_SERVICE:
428 send_msg = g_strdup_printf("%d Loss of Service", download_status);
430 case DM_ERR_ATTR_MISMATCH:
431 send_msg = g_strdup_printf("%d Attribute mismatch", download_status);
433 case DM_ERR_INVALID_DESCRIPTOR:
434 send_msg = g_strdup_printf("%d Invalid descriptor", download_status);
436 case DM_ERR_INVALID_DDVERSION:
437 send_msg = g_strdup_printf("%d Invalid DDVersion", download_status);
439 case DM_ERR_DEVICE_ABORTED:
440 send_msg = g_strdup_printf("%d Device Aborted", download_status);
442 case DM_ERR_NON_ACCEPTABLE_CONTENT:
443 send_msg = g_strdup_printf("%d Non-Acceptable Content", download_status);
445 case DM_ERR_LOADER_ERROR:
446 send_msg = g_strdup_printf("%d Loader Error", download_status);