summaryrefslogtreecommitdiff
path: root/src/agent/dm-engine/dl-manager/fw_downloader.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent/dm-engine/dl-manager/fw_downloader.c')
-rw-r--r--src/agent/dm-engine/dl-manager/fw_downloader.c455
1 files changed, 455 insertions, 0 deletions
diff --git a/src/agent/dm-engine/dl-manager/fw_downloader.c b/src/agent/dm-engine/dl-manager/fw_downloader.c
new file mode 100644
index 0000000..b228d7c
--- /dev/null
+++ b/src/agent/dm-engine/dl-manager/fw_downloader.c
@@ -0,0 +1,455 @@
+/*
+ * 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 <glib.h>
+#include <unistd.h>
+
+/*sync-agent*/
+#include <sync_agent.h>
+
+/*dm-agent*/
+#include "common/dm_common.h"
+#include "common/util/util.h"
+#include "dm-engine/dl-manager/fw_downloader.h"
+#include "dm-engine/dl-manager/dd_parser.h"
+#include "dm-engine/dl-manager/sa_fw_downloader.h"
+#include "ipc_agent.h"
+
+#ifndef OMADM_AGENT_LOG
+#undef LOG_TAG
+#define LOG_TAG "OMA_DM_DL"
+#endif
+
+static Data_Resume_Infomation *_alloc_data_resume_infomation();
+static void _free_data_resume_information(Data_Resume_Infomation * data_resume_info);
+static int _arrange_file(const char *download_folder, const char *file_name);
+static char *_get_msg_for_status(DM_ERROR download_status);
+
+DM_ERROR get_object_information(char *dd_server_uri, int *file_size, Download_Descriptor ** download_descriptor, DM_ERROR * download_status)
+{
+ _EXTERN_FUNC_ENTER;
+
+ DM_ERROR ret = DM_OK;
+
+ retvm_if((dd_server_uri) == NULL, COMMON_ERR_IS_NULL, "dd_server_uri is NULL!!");
+
+ /* 1. construct HTTP msg */
+ /* 2. send msg to server (about dd_server_uri) */
+ /* 3. receive dd information from server */
+ GList *send_header = 0;
+ char *recvMsg = 0;
+ unsigned int recvMsg_size = 0;
+ GList *recv_header = 0;
+ ret = send_download_msg(DD_DOWNLOAD, dd_server_uri, "application/vnd.oma.dd+xml", send_header, 0, 0, &recv_header, &recvMsg, &recvMsg_size);
+ if (ret != DM_OK) {
+ *download_status = DM_ERR_LOSS_OF_SERVICE;
+ _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
+ goto error;
+ }
+
+ /* 4. realloc or alloc download descriptor object */
+
+ /* 5. parse the dd information xml data (convert dd xml to dd object) */
+ ret = convert_xml_to_DD_object(recvMsg, recvMsg_size, download_descriptor);
+ if (ret != DM_OK) {
+ _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
+ *download_status = DM_ERR_INVALID_DESCRIPTOR;
+ goto error;
+ }
+
+ if (*download_descriptor != NULL && (*download_descriptor)->object_uri != NULL) {
+ *file_size = (*download_descriptor)->object_size;
+
+ _DEBUG_INFO(" dd->object_uri = %s \n", (*download_descriptor)->object_uri);
+ _DEBUG_INFO(" dd->size = %d \n", (*download_descriptor)->object_size);
+ _DEBUG_INFO(" dd->object_description = %s \n", (*download_descriptor)->object_description);
+ } else {
+ _DEBUG_INFO("dd obejct uri null");
+ ret = DM_ERR_INVALID_DESCRIPTOR;
+ *download_status = DM_ERR_INVALID_DESCRIPTOR;
+ goto error;
+ }
+
+ error:
+
+ if (recvMsg != NULL)
+ free(recvMsg);
+
+ if (recv_header != NULL)
+ free_header_info(recv_header);
+
+ _EXTERN_FUNC_EXIT;
+ return ret;
+}
+
+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)
+{
+ _EXTERN_FUNC_ENTER;
+ DM_ERROR ret = DM_OK;
+
+ /* 1. validate download_folder variable */
+ retvm_if((checked_download_folder) == NULL, COMMON_ERR_IS_NULL, "checked_download_folder is NULL!!");
+
+ if (file_status != NULL) {
+ _DEBUG_INFO("checked_download_folder == 0 && file_status != 0 ");
+ ret = COMMON_ERR_IS_NULL;
+ _EXTERN_FUNC_EXIT;
+ return ret;
+ }
+
+ char *file_name = NULL;
+ char *file_path = NULL;
+
+ /* send_msg to server */
+ GList *send_header = 0;
+ char *recvMsg = 0;
+ unsigned int recvMsg_size = 0;
+ GList *recv_header = 0;
+ ret = send_download_msg(GET_FILE_NAME, download_descriptor->object_uri, download_descriptor->object_type, send_header, 0, 0, &recv_header, &recvMsg, &recvMsg_size);
+ if (ret != DM_OK) {
+ _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
+ goto error;
+ }
+
+ /* get file_name for header info */
+ file_name = get_file_name(recv_header, download_descriptor->object_uri);
+ if (file_name == NULL) {
+ _DEBUG_INFO("file_name is null");
+ ret = COMMON_ERR_IS_NULL;
+ goto error;
+ }
+
+ /* check file */
+ file_path = g_strdup_printf("%s/%s", checked_download_folder, file_name);
+
+ _DEBUG_INFO("Download_Folder = %s\n", checked_download_folder);
+ _DEBUG_INFO("Download_Folder_Path = %s\n", file_path);
+
+ unsigned long file_size = 0;
+ int err = sync_agent_get_file_size(file_path, &file_size);
+ if (err == 0) {
+ _DEBUG_INFO("NOT_EXIST_FILE\n");
+ *file_status = NOT_EXIST_FILE;
+ goto return_part;
+ }
+
+ _DEBUG_INFO("Exist File Size = %d", file_size);
+ _DEBUG_INFO(" download_descriptor->object_size = %d", download_descriptor->object_size);
+ if (file_size >= download_descriptor->object_size) {
+ _DEBUG_INFO("EXIST_COMPLETED_FILE\n");
+ *file_status = EXIST_COMPLETED_FILE;
+ goto arrange;
+ }
+
+ return_part:
+
+ (*data_resume_info) = _alloc_data_resume_infomation();
+ if ((*data_resume_info) == NULL) {
+ _DEBUG_INFO("calloc failed !!");
+ ret = COMMON_ERR_ALLOC;
+ *file_status = NOT_EXIST_FILE;
+ goto error;
+ }
+ (*data_resume_info)->file_path = g_strdup_printf("%s", file_path);
+ (*data_resume_info)->current_data_size = file_size;
+ (*data_resume_info)->total_data_size = download_descriptor->object_size;
+ *file_status = EXIST_PARTIAL_FILE;
+ _DEBUG_INFO("EXIST_PARTIAL_FILE\n");
+
+ arrange:
+ err = _arrange_file(checked_download_folder, file_name);
+ if (err != 1) {
+ ret = COMMON_ERR_GENERIC;
+ }
+
+ error:
+ if (recvMsg != NULL)
+ free(recvMsg);
+
+ if (recv_header != NULL)
+ free_header_info(recv_header);
+
+ str_free(&file_name);
+ str_free(&file_path);
+
+ _DEBUG_INFO("END error : %d !!", ret);
+ _EXTERN_FUNC_EXIT;
+ return ret;
+}
+
+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)
+{
+ _EXTERN_FUNC_ENTER;
+
+ DM_ERROR ret = DM_OK;
+
+ retvm_if((download_folder) == NULL, COMMON_ERR_IS_NULL, "download_folder is NULL!!");
+ retvm_if((data_resume_info) == NULL, COMMON_ERR_IS_NULL, "data_resume_info is NULL!!");
+ retvm_if((download_descriptor) == NULL, COMMON_ERR_IS_NULL, "download_descriptor is NULL!!");
+
+ if (data_resume_info->file_path == NULL) {
+ ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
+ *download_status = DM_ERR_INVALID_DESCRIPTOR;
+ _EXTERN_FUNC_EXIT;
+ return ret;
+ }
+
+ _DEBUG_INFO(" data_resume_info = %p", data_resume_info);
+ _DEBUG_INFO("file path : %s ", data_resume_info->file_path);
+ _DEBUG_INFO("file size : %d ", data_resume_info->total_data_size);
+
+ if (!sync_agent_is_existing_fs(data_resume_info->file_path)) {
+ _DEBUG_INFO("FILE IS NOT EXIST");
+ FILE *fp = fopen(data_resume_info->file_path, "w");
+ fclose(fp);
+
+ } else {
+ //
+ }
+ _DEBUG_INFO("############################################################################");
+ /*noti to ui */
+ int noti_err = noti_send_download_info(data_resume_info->total_data_size, data_resume_info->file_path);
+ sleep(1);
+ _DEBUG_INFO("download info noti : %d", noti_err);
+ _DEBUG_INFO("############################################################################");
+ if (noti_err != 1) {
+ ret = COMMON_ERR_IPC;
+ *download_status = DM_ERR_USER_CANDELLED;
+ goto error;
+ }
+ char *download_path = NULL;
+ int current_file_download_size = 0;
+ if (data_resume_info != NULL) {
+ current_file_download_size = data_resume_info->current_data_size;
+ }
+
+ GList *send_header = 0;
+ ret = download_data(download_descriptor->object_uri, download_descriptor->object_type, send_header, current_file_download_size, (char *)download_folder, config, &download_path);
+ if (ret != DM_OK) {
+ _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
+ *download_status = DM_ERR_USER_CANDELLED;
+ goto error;
+ }
+
+ *object_downloaded_path = download_path;
+ *download_status = DM_DD_SUCCESS;
+
+ error:
+
+ if (data_resume_info != NULL) {
+ _free_data_resume_information(data_resume_info);
+ _DEBUG_INFO("__free_Data_Resume_Information() Success\n");
+ }
+
+ _DEBUG_INFO("END : %d !!", ret);
+ _EXTERN_FUNC_EXIT;
+ return ret;
+}
+
+DM_ERROR send_donwload_status(DM_ERROR download_status, Download_Descriptor * download_descriptor)
+{
+ _EXTERN_FUNC_ENTER;
+ DM_ERROR ret = DM_OK;
+
+ retvm_if((download_descriptor) == NULL, COMMON_ERR_IS_NULL, "download_descriptor is NULL!!");
+
+ if ((download_descriptor == NULL) || (download_descriptor->install_notify_uri == NULL)) {
+ _EXTERN_FUNC_EXIT;
+ return COMMON_ERR_IS_NULL;
+ }
+
+ GList *send_header = 0;
+ char *send_msg = 0;
+ char *recvMsg = 0;
+ unsigned int recvMsg_size = 0;
+ GList *recv_header = 0;
+
+ send_msg = _get_msg_for_status(download_status);
+ if (send_msg != NULL) {
+ 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);
+ if (ret != DM_OK) {
+ _DEBUG_INFO("send_DL_Msg() err [%d]\n", ret);
+ _EXTERN_FUNC_EXIT;
+ return 1;
+ }
+ }
+
+ if (recvMsg != NULL)
+ free(recvMsg);
+
+ if (recv_header != NULL)
+ free_header_info(recv_header);
+
+ _EXTERN_FUNC_EXIT;
+ return ret;
+}
+
+/* static function implementation*/
+
+void free_Download_Descriptor(Download_Descriptor ** download_descriptor)
+{
+ _EXTERN_FUNC_ENTER;
+
+ if (*download_descriptor != NULL) {
+
+ if ((*download_descriptor)->object_name != NULL)
+ free((*download_descriptor)->object_name);
+
+ if ((*download_descriptor)->object_description != NULL)
+ free((*download_descriptor)->object_description);
+
+ if ((*download_descriptor)->object_uri != NULL)
+ free((*download_descriptor)->object_uri);
+
+ if ((*download_descriptor)->next_uri != NULL)
+ free((*download_descriptor)->next_uri);
+
+ if ((*download_descriptor)->install_notify_uri != NULL)
+ free((*download_descriptor)->install_notify_uri);
+
+ if ((*download_descriptor)->info_uri != NULL)
+ free((*download_descriptor)->info_uri);
+
+ if ((*download_descriptor)->icon_uri != NULL)
+ free((*download_descriptor)->icon_uri);
+
+ if ((*download_descriptor)->object_vender != NULL)
+ free((*download_descriptor)->object_vender);
+
+ if ((*download_descriptor)->DD_version != NULL)
+ free((*download_descriptor)->DD_version);
+
+ if ((*download_descriptor)->install_param != NULL)
+ free((*download_descriptor)->install_param);
+
+ free((*download_descriptor));
+ }
+
+ *download_descriptor = 0;
+
+ _EXTERN_FUNC_EXIT;
+}
+
+static Data_Resume_Infomation *_alloc_data_resume_infomation()
+{
+ _INNER_FUNC_ENTER;
+
+ _INNER_FUNC_EXIT;
+ return (Data_Resume_Infomation *) calloc(1, sizeof(Data_Resume_Infomation));
+}
+
+static void _free_data_resume_information(Data_Resume_Infomation * data_resume_info)
+{
+ _INNER_FUNC_ENTER;
+ if (data_resume_info != NULL) {
+
+ if (data_resume_info->file_path != NULL)
+ free(data_resume_info->file_path);
+
+ free(data_resume_info);
+ }
+
+ data_resume_info = NULL;
+ _INNER_FUNC_EXIT;
+}
+
+static int _arrange_file(const char *download_folder, const char *file_name)
+{
+ _INNER_FUNC_ENTER;
+
+ retvm_if((download_folder) == NULL, 0, "download_folder is NULL!!");
+ retvm_if((file_name) == NULL, 0, "file_name is NULL!!");
+
+ int err = 1;
+ GSList *fileList = 0;
+ err = sync_agent_get_file_list(download_folder, &fileList);
+ if (err != 1)
+ goto error;
+
+ GSList *iter = 0;
+ char *temp_file_info = 0;
+ char *temp_file_name = 0;
+ char *temp_file_path = 0;
+ for (iter = fileList; iter != 0; iter = g_slist_next(iter)) {
+ temp_file_info = (char *)(iter->data);
+ _DEBUG_TRACE("temp_file_name = %s\n", temp_file_name);
+
+ //
+ temp_file_name = strtok(temp_file_info, ",");
+ //
+
+ if (strcmp(temp_file_name, file_name) != 0) {
+ temp_file_path = g_strdup_printf("%s/%s", download_folder, temp_file_name);
+ _DEBUG_TRACE("temp_file_path = %s\n", temp_file_path);
+ err = sync_agent_delete_file(temp_file_path);
+ _DEBUG_TRACE("delete file = %d\n", err);
+ str_free(&temp_file_path);
+ }
+ }
+
+ error:
+
+ _INNER_FUNC_EXIT;
+ return err;
+}
+
+static char *_get_msg_for_status(DM_ERROR download_status)
+{
+ _INNER_FUNC_ENTER;
+
+ char *send_msg = 0;
+
+ switch (download_status) {
+ case DM_OK:
+ send_msg = g_strdup_printf("%d Success", download_status);
+ break;
+ case DM_ERR_INSUFFICIENT_MEMORY:
+ send_msg = g_strdup_printf("%d Insufficient memory", download_status);
+ break;
+ case DM_ERR_USER_CANDELLED:
+ send_msg = g_strdup_printf("%d User Cancelled", download_status);
+ break;
+ case DM_ERR_LOSS_OF_SERVICE:
+ send_msg = g_strdup_printf("%d Loss of Service", download_status);
+ break;
+ case DM_ERR_ATTR_MISMATCH:
+ send_msg = g_strdup_printf("%d Attribute mismatch", download_status);
+ break;
+ case DM_ERR_INVALID_DESCRIPTOR:
+ send_msg = g_strdup_printf("%d Invalid descriptor", download_status);
+ break;
+ case DM_ERR_INVALID_DDVERSION:
+ send_msg = g_strdup_printf("%d Invalid DDVersion", download_status);
+ break;
+ case DM_ERR_DEVICE_ABORTED:
+ send_msg = g_strdup_printf("%d Device Aborted", download_status);
+ break;
+ case DM_ERR_NON_ACCEPTABLE_CONTENT:
+ send_msg = g_strdup_printf("%d Non-Acceptable Content", download_status);
+ break;
+ case DM_ERR_LOADER_ERROR:
+ send_msg = g_strdup_printf("%d Loader Error", download_status);
+ break;
+ default:
+ break;
+ }
+
+ _INNER_FUNC_EXIT;
+
+ return send_msg;
+}