From 3538f3fd07e9402b30eb575af344f8fd3dfa8e0e Mon Sep 17 00:00:00 2001 From: Jinkun Jang Date: Wed, 13 Mar 2013 01:46:18 +0900 Subject: [PATCH] Tizen 2.1 base --- AUTHORS | 2 + CMakeLists.txt | 102 ++ LICENSE.APLv2 | 202 +++ NOTICE | 3 + capi-web-url-download.manifest | 6 + capi-web-url-download.pc.in | 15 + include/download.h | 771 +++++++++ packaging/capi-web-url-download.spec | 212 +++ src/download.c | 2945 ++++++++++++++++++++++++++++++++++ 9 files changed, 4258 insertions(+) create mode 100644 AUTHORS create mode 100755 CMakeLists.txt create mode 100644 LICENSE.APLv2 create mode 100644 NOTICE create mode 100755 capi-web-url-download.manifest create mode 100644 capi-web-url-download.pc.in create mode 100755 include/download.h create mode 100755 packaging/capi-web-url-download.spec create mode 100755 src/download.c diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..707ec6f --- /dev/null +++ b/AUTHORS @@ -0,0 +1,2 @@ +Kwangmin Bang +Jungki Kwak diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..727bbad --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,102 @@ + +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(fw_name "capi-web-url-download") + +PROJECT(${fw_name}) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(VERSION_MAJOR 0) +SET(VERSION "${VERSION_MAJOR}.1.0") + +SET(INC_DIR include) +INCLUDE_DIRECTORIES(${INC_DIR}) + +SET(requires "dlog capi-base-common capi-appfw-app-manager capi-appfw-application download-provider dbus-1") +MESSAGE(STATUS "PACKAGES : ${requires}") +SET(pc_requires "capi-base-common capi-appfw-application") + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_name} REQUIRED ${requires}) +FOREACH(flag ${${fw_name}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -Wall -Werror") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") + +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-DTARGET") +ENDIF("${ARCH}" STREQUAL "arm") + +ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") +ADD_DEFINITIONS("-DSLP_DEBUG") +ADD_DEFINITIONS(-DDP_SUPPORT_DBUS_ACTIVATION) + +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=/usr/lib") + +SET(SOURCES src/download.c) +MESSAGE(STATUS "SOURCES : ${SOURCES}") +ADD_LIBRARY(${fw_name} SHARED ${SOURCES}) + +TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS}) +SET_TARGET_PROPERTIES(${fw_name} PROPERTIES VERSION ${VERSION}) +SET_TARGET_PROPERTIES(${fw_name} PROPERTIES SOVERSION ${VERSION_MAJOR}) + +INSTALL(TARGETS ${fw_name} DESTINATION lib) +INSTALL( + DIRECTORY ${INC_DIR}/ DESTINATION include/web + FILES_MATCHING + PATTERN "*_private.h" EXCLUDE + PATTERN "${INC_DIR}/*.h" + ) + +SET(PC_NAME ${fw_name}) +SET(PC_REQUIRED ${pc_requires}) +SET(PC_LDFLAGS -l${fw_name}) +SET(PC_CFLAGS -I\${includedir}/system) + +CONFIGURE_FILE( + capi-web-url-download.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc + @ONLY +) +CONFIGURE_FILE( + LICENSE.APLv2 + ${fw_name} + @ONLY +) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION lib/pkgconfig) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name} DESTINATION share/license) + +IF(UNIX) + +ADD_CUSTOM_TARGET (distclean @echo cleaning for source distribution) +ADD_CUSTOM_COMMAND( + DEPENDS clean + COMMENT "distribution clean" + COMMAND find + ARGS . + -not -name config.cmake -and \( + -name tester.c -or + -name Testing -or + -name CMakeFiles -or + -name cmake.depends -or + -name cmake.check_depends -or + -name CMakeCache.txt -or + -name cmake.check_cache -or + -name *.cmake -or + -name Makefile -or + -name core -or + -name core.* -or + -name gmon.out -or + -name install_manifest.txt -or + -name *.pc -or + -name *~ \) + | grep -v TC | xargs rm -rf + TARGET distclean + VERBATIM +) + +ENDIF(UNIX) + diff --git a/LICENSE.APLv2 b/LICENSE.APLv2 new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE.APLv2 @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..6220b80 --- /dev/null +++ b/NOTICE @@ -0,0 +1,3 @@ +Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved. +Except as noted, this software is licensed under Apache License, Version 2. +Please, see the LICENSE file for Apache License terms and conditions. diff --git a/capi-web-url-download.manifest b/capi-web-url-download.manifest new file mode 100755 index 0000000..ca37499 --- /dev/null +++ b/capi-web-url-download.manifest @@ -0,0 +1,6 @@ + + + + + + diff --git a/capi-web-url-download.pc.in b/capi-web-url-download.pc.in new file mode 100644 index 0000000..2ecfef5 --- /dev/null +++ b/capi-web-url-download.pc.in @@ -0,0 +1,15 @@ + +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=/usr +libdir=/usr/lib +includedir=/usr/include/web + +Name: @PC_NAME@ +Description: @PACKAGE_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_LDFLAGS@ +Cflags: -I${includedir} + diff --git a/include/download.h b/include/download.h new file mode 100755 index 0000000..ca8af1f --- /dev/null +++ b/include/download.h @@ -0,0 +1,771 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef __TIZEN_WEB_DOWNLOAD_H__ +#define __TIZEN_WEB_DOWNLOAD_H__ + +#include +#include + + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * @addtogroup CAPI_WEB_DOWNLOAD_MODULE + * @{ + */ + +/** + * @brief Name for download service operation. Download Manager application is launched. + */ +#define SERVICE_OPERATION_DOWNLOAD "http://tizen.org/appcontrol/operation/download" + +/** + * @brief Enumeration of error code for URL download + */ +typedef enum +{ + DOWNLOAD_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ + DOWNLOAD_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ + DOWNLOAD_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ + DOWNLOAD_ERROR_NETWORK_UNREACHABLE = TIZEN_ERROR_NETWORK_UNREACHABLE, /**< Network is unreachable */ + DOWNLOAD_ERROR_CONNECTION_TIMED_OUT = TIZEN_ERROR_CONNECTION_TIME_OUT, /**< Http session time-out */ + DOWNLOAD_ERROR_NO_SPACE = TIZEN_ERROR_FILE_NO_SPACE_ON_DEVICE, /**< No space left on device */ + DOWNLOAD_ERROR_FIELD_NOT_FOUND = TIZEN_ERROR_KEY_NOT_AVAILABLE, /**< Specified field not found */ + DOWNLOAD_ERROR_INVALID_STATE = TIZEN_ERROR_WEB_CLASS | 0x21, /**< Invalid state */ + DOWNLOAD_ERROR_CONNECTION_FAILED = TIZEN_ERROR_WEB_CLASS | 0x22, /**< Connection failed */ + DOWNLOAD_ERROR_INVALID_URL = TIZEN_ERROR_WEB_CLASS | 0x24, /**< Invalid URL */ + DOWNLOAD_ERROR_INVALID_DESTINATION = TIZEN_ERROR_WEB_CLASS | 0x25, /**< Invalid destination */ + DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS = TIZEN_ERROR_WEB_CLASS | 0x26, /**< Full of available simultaneous downloads */ + DOWNLOAD_ERROR_QUEUE_FULL = TIZEN_ERROR_WEB_CLASS | 0x27, /**< Full of available downloading items from server*/ + DOWNLOAD_ERROR_ALREADY_COMPLETED = TIZEN_ERROR_WEB_CLASS | 0x28, /**< The download is already completed */ + DOWNLOAD_ERROR_FILE_ALREADY_EXISTS = TIZEN_ERROR_WEB_CLASS | 0x29, /**< It is failed to rename the downloaded file */ + DOWNLOAD_ERROR_CANNOT_RESUME = TIZEN_ERROR_WEB_CLASS | 0x2a, /**< It cannot resume */ + DOWNLOAD_ERROR_TOO_MANY_REDIRECTS = TIZEN_ERROR_WEB_CLASS | 0x30, /**< In case of too may redirects from http response header*/ + DOWNLOAD_ERROR_UNHANDLED_HTTP_CODE = TIZEN_ERROR_WEB_CLASS | 0x31, /**< The download cannot handle the http status value */ + DOWNLOAD_ERROR_REQUEST_TIMEOUT = TIZEN_ERROR_WEB_CLASS | 0x32, /**< There are no action after client create a download id*/ + DOWNLOAD_ERROR_RESPONSE_TIMEOUT = TIZEN_ERROR_WEB_CLASS | 0x33, /**< It does not call start API in some time although the download is created*/ + DOWNLOAD_ERROR_SYSTEM_DOWN = TIZEN_ERROR_WEB_CLASS | 0x34, /**< There are no response from client after rebooting download daemon*/ + DOWNLOAD_ERROR_ID_NOT_FOUND = TIZEN_ERROR_WEB_CLASS | 0x35, /**< The download id is not existed in download service module*/ + DOWNLOAD_ERROR_NO_DATA = TIZEN_ERROR_NO_DATA, /**< No data because the set API is not called */ + DOWNLOAD_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR , /**< Internal I/O error */ +} download_error_e; + + +/** + * @brief Enumerations of state of download + */ +typedef enum +{ + DOWNLOAD_STATE_NONE, /**< It is ready to download */ + DOWNLOAD_STATE_READY, /**< It is ready to download */ + DOWNLOAD_STATE_QUEUED, /**< It is queued to start downloading */ + DOWNLOAD_STATE_DOWNLOADING, /**< The download is currently running */ + DOWNLOAD_STATE_PAUSED, /**< The download is waiting to resume */ + DOWNLOAD_STATE_COMPLETED, /**< The download is completed. */ + DOWNLOAD_STATE_FAILED, /**< The download failed. */ + DOWNLOAD_STATE_CANCELED, /**< User cancel the download item. */ +} download_state_e; + +/** + * @brief Enumerations of network type for download + */ +typedef enum +{ + DOWNLOAD_NETWORK_DATA_NETWORK, /**< Download is available through data network */ + DOWNLOAD_NETWORK_WIFI, /**< Download is available through Wi-Fi */ + DOWNLOAD_NETWORK_ALL /**< Download is available through either data network or Wi-Fi */ +} download_network_type_e ; + + +/** + * @brief Called when the download status is changed. + * + * @param [in] download The download id + * @param [in] state The state of download + * @param [in] user_data The user data passed from download_set_state_changed_cb() + * @pre download_start() will cause this callback if you register this callback using download_set_state_changed_cb() + * @see download_set_state_changed_cb() + * @see download_unset_state_changed_cb() + */ +typedef void (*download_state_changed_cb) (int download_id, + download_state_e state, void *user_data); + +/** + * @brief Called when the progress of download changes. + * + * @remarks This callback function is only invoked in the downloading state. + * @param [in] download The download id + * @param [in] received The size of the data received in bytes + * @param [in] user_data The user data passed from download_set_progress_cb() + * @pre This callback function is invoked if you register this callback using download_set_progress_cb(). + * @see download_cancel() + * @see download_set_progress_cb() + * @see download_unset_progress_cb() + */ +typedef void (*download_progress_cb) (int download_id, unsigned long long received, void *user_data); + +/** + * @brief Creates a download id. + * + * @remarks The @a download must be released with download_destroy() by you.\n + * The g_type_init() should be called when creating a main loop by user side. \n + * Because the libsoup, which is http stack library of download module, use gobject internally. + * @param [out] download A download id to be newly created on success + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @post The download state will be #DOWNLOAD_STATE_READY + * @see download_destroy() + */ +int download_create(int *download_id); + + +/** + * @brief Unload all data concerning a download id from memory. + * + * @detail After calling this API, a download ID is existed at DB in certain time.\n + * Within that time, it is able to use the other API with the download ID. + * @remark If #DOWNLOAD_ERROR_ID_NOT_FOUND is returned, it means that the download ID is completely removed from DB. + * @param [in] download The download id + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_create() + */ +int download_destroy(int download_id); + + +/** + * @brief Sets the URL to download. + * + * @remarks This function should be called before downloading (See download_start()) + * @param [in] download The download id + * @param [in] url The URL to download \n + * If the @a url is NULL, it clears the previous value. + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_FAILED, #DOWNLOAD_STATE_CANCELED + * @see download_get_url() + */ +int download_set_url(int download_id, const char *url); + + +/** + * @brief Gets the URL to download. + * + * @remarks The @a url must be released with free() by you. + * @param [in] download The download id + * @param [out] url The URL to download + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_url() + */ +int download_get_url(int download_id, char **url); + + +/** + * @brief Sets the allowed network type for the downloaded file + * + * @details The file can be downloaded only under the allowed network. + * + * @remarks This function should be called before downloading (see download_start()) + * @param [in] download The download id + * @param [in] net_type The network type which the client prefer + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_FAILED, #DOWNLOAD_STATE_CANCELED + * @see download_get_network_type() + * @see #download_network_type_e + */ +int download_set_network_type(int download_id, download_network_type_e net_type); + + +/** + * @brief Gets the netowork type for the downloaded file + * + * @param [in] download The download id + * @param [out] net_type The network type which is defined by client. + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_network_type() + * @see #download_network_type_e + */ +int download_get_network_type(int download_id, download_network_type_e *net_type); + + +/** + * @brief Sets the destination for the downloaded file. + * + * @details The file will be downloaded to the specified destination. + * The downloaded file is saved to an auto-generated file name in the destination. + * If the destination is not specified, the file will be downloaded to default storage. (See the @ref CAPI_STORAGE_MODULE API) + * + * @remarks This function should be called before downloading (see download_start()) + * @param [in] download The download id + * @param [in] path The absolute path to the downloaded file + * If the @a path is NULL, it clears the previous value. + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_FAILED, #DOWNLOAD_STATE_CANCELED + * @see download_get_destination() + */ +int download_set_destination(int download_id, const char *path); + + +/** + * @brief Gets the destination for the downloaded file. + * + * @remarks The @a path must be released with free() by you. + * @param [in] download The download id + * @param [out] path The absolute path to the downloaded file + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_destination() + */ +int download_get_destination(int download_id, char **path); + + +/** + * @brief Sets the name for the downloaded file. + * + * @details The file will be downloaded to the specified destination as the given file name. + * If the file name is not specified, the downloaded file is saved to an auto-generated file name in the destination. + * + * @remarks This function should be called before downloading (see download_start()) + * @param [in] download The download id + * @param [in] file_name The file name for the downloaded file + * If the @a name is NULL, it clears the previous value. + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_FAILED, #DOWNLOAD_STATE_CANCELED + * @see download_get_file_name() + */ +int download_set_file_name(int download_id, const char *file_name); + + +/** + * @brief Gets the name which is set by user. + * + * @details If user do not set any name, it retruns NULL. + * + * @remarks The @a file_name must be released with free() by you. + * @param [in] download The download id + * @param [out] file_name The file name which is set by user. + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_file_name() + */ +int download_get_file_name(int download_id, char **file_name); + + +/** + * @brief Sets the option value to register notification messages by download service module. + * @details The tree types of notification message can be posted. Those are completion, failed and ongoing type. + * When the notification message of failed and ongoing types from the notification tray, \n + * the client application which call this API will be launched. \n + * + * @remarks The extra param should be set together (See download_set_notification_extra_param()). \n + * The downloading and failed notification can be registerd only if the extra param for noticiation message is set. \n + * If it is not, the client application can not know who request to launch itself. \n + * It should be necessary to understand the action operation of notification click event. + * @remarks If the competition notification message is selected from the notification tray,\n + * the proper player application is launched automatically. + * @remarks The default value is false. So if the client don't enable it, any notification messages are not registered. + * @remarks This function should be called before downloading (See download_start()) + * @param[in] download The download id + * @param[in] enable The boolean type. The true or false value is available. + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_FAILED, #DOWNLOAD_STATE_CANCELED + * @see download_get_notification() + * @see service_get_operation() + */ +int download_set_notification(int download_id, bool enable); + +/** + * @brief Gets the option value to register notification messages by download service module. + * @param[in] download The download id + * @param[out] enable The boolean type. The true or false value is retruned + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_notification() + */ +int download_get_notification(int download_id, bool *enable); + +/** + * @brief Sets the extra param data which pass by application service data when notification message is clicked + * @details When client set the extra param data for ongoing notificaiton action, \n + * it can get the data through service_get_extra_data() when client application is launched by notification action. + * + * @remarks This function should be called before downloading (See download_start()) + * + * @param[in] download The download id + * @param[in] key The character pointer type. The extra param has a pair of key and value + * @param[in] value The character pointer type. The extra param has a pair of key and value + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_get_notification_extra_param() + * @see download_service_get_extra_data() + * @see service_get_operation() + */ +int download_set_notification_extra_param(int download_id, char *key, char *value); + + +/** + * @brief Gets the extra param value to set by download_set_notification_extra_param + * @param[in] download The download id + * @param[out] key The character pointer type. The extra param has a pair of key and value + * @param[out] value param The character pointer type. The extra param has a pair of key and value + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_notification_extra_param() + */ +int download_get_notification_extra_param(int download_id, char **key, char **value); + + +/** + * @brief Gets the absolute path to save the downloaded file + * + * @remarks This function returns #DOWNLOAD_ERROR_INVALID_STATE if the download is not completed. \n + * The @a path must be released with free() by you. + * @param [in] download The download id + * @param [out] path The absolute path to the downloaded file + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be #DOWNLOAD_STATE_COMPLETED. + * @see download_set_file_name() + * @see download_set_destination() + */ +int download_get_downloaded_file_path(int download_id, char **path); + + +/** + * @brief Gets the mime type for downloading a content. + * + * @remarks This function returns #DOWNLOAD_ERROR_INVALID_STATE if the download has not been started. \n + * The @a mime_type must be released with free() by you. + * @param [in] download The download id + * @param [out] mime_type The MIME type of the downloaded file + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_file_name() + * @see download_set_destination() + * @see download_get_downloaded_file_path() + */ +int download_get_mime_type(int download_id, char **mime_type); + + +/** + * @brief Sets the option for auto download. + * @details If this option is enabled, \n + * the previous downloading item is restarted automatically as soon as the download daemon is restarted. \n + * And the download progress keep going after the client process is terminated. \n + * @remarks The client should call download_set_notification() and download_set_notification_extra_param() after call this API. \n + * If it is not, user do not receive the download result in case the client process is not alive. + * @remarks The default value is false. + * @param[in] download The download id + * @param[in] enable The boolean value for auto download which is defined by clinet. + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_FAILED, #DOWNLOAD_STATE_CANCELED + * @see download_get_auto_download() + * @see download_set_ongoing_notification() + * @see download_set_notification_extra_param() + * + */ +int download_set_auto_download(int download_id, bool enable); + + +/** + * @brief Gets the value of option for auto download. + * + * @param [in] download The download id + * @param [out] enable The boolean value for auto download which is defined by clinet. + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_auto_download() + */ +int download_get_auto_download(int download_id, bool *enable); + + +/** + * @brief Adds an HTTP header field to the download request + * + * @details The given HTTP header field will be included with the HTTP request of the download request. \n + * Refer to the HTTP/1.1: HTTP Message Headers + * @remarks This function should be called before downloading (see download_start()) \n + * This function replaces any existing value for the given key. \n + * This function returns #DOWNLOAD_ERROR_INVALID_PARAMETER if field or value is zero-length string. + * @param [in] download The download id + * @param [in] field The name of the HTTP header field + * @param [in] value The value associated with given field + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_FAILED, #DOWNLOAD_STATE_CANCELED + * @see download_get_http_header_field() + * @see download_remove_http_header_field() + */ +int download_add_http_header_field(int download_id, const char *field, const char *value); + + +/** + * @brief Gets the value associated with given HTTP header field from the download + * + * @remarks This function returns #DOWNLOAD_ERROR_INVALID_PARAMETER if field is zero-length string. \n + * The @a value must be released with free() by you. + * @param [in] download The download id + * @param [in] field The name of the HTTP header field + * @param [out] value The value associated with given field + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_FIELD_NOT_FOUND Specified field not found + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_add_http_header_field() + * @see download_remove_http_header_field() + */ +int download_get_http_header_field(int download_id, const char *field, char **value); + + +/** + * @brief Removes the given HTTP header field from the download + * + * @remarks This function should be called before downloading (see download_start()) \n + * This function returns #DOWNLOAD_ERROR_INVALID_PARAMETER if field is zero-length string. + * @param [in] download The download id + * @param [in] field The name of the HTTP header field + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_FIELD_NOT_FOUND Specified field not found + * @retval #DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_FAILED, #DOWNLOAD_STATE_CANCELED + * @see download_add_http_header_field() + * @see download_get_http_header_field() + */ +int download_remove_http_header_field(int download_id, const char *field); + + +/** + * @brief Registers a callback function to be invoked when the download state is changed. + * + * @remarks This function should be called before downloading (see download_start()) + * @param [in] download The download id + * @param [in] callback The callback function to register + * @param [in] user_data The user data to be passed to the callback function + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @post download_state_changed_cb() will be invoked. + * @see download_unset_state_changed_cb() + * @see download_state_changed_cb() +*/ +int download_set_state_changed_cb(int download_id, download_state_changed_cb callback, void* user_data); + + +/** + * @brief Unregisters the callback function. + * + * @remarks This function should be called before downloading (see download_start()) + * @param [in] download The download id + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_state_changed_cb() + * @see download_state_changed_cb() +*/ +int download_unset_state_changed_cb(int download_id); + + +/** + * @brief Registers a callback function to be invoked when progress of the download changes + * + * @remarks This function should be called before downloading (see download_start()) + * @param [in] download The download id + * @param [in] callback The callback function to register + * @param [in] user_data The user data to be passed to the callback function + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @post download_progress_cb() will be invoked. + * @see download_unset_progress_cb() + * @see download_progress_cb() +*/ +int download_set_progress_cb(int download_id, download_progress_cb callback, void *user_data); + + +/** + * @brief Unregisters the callback function. + * + * @remarks This function should be called before downloading (see download_start()) + * @param [in] download The download id + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see download_set_progress_cb() + * @see download_progress_cb() +*/ +int download_unset_progress_cb(int download_id); + + +/** + * @brief Starts or resumes the download, asynchronously. + * + * @details This function starts to download the current URL, or resumes the download if paused. + * + * @remarks The URL is the mandatory information to start the download. + * @remarks It should call download_set_progress_cb() and download_set_state_changed_cb() again \n + * after the client process is restarted or download_destry() is called + * @param [in] download The download id + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @retval #DOWNLOAD_ERROR_URL Invalid URL + * @retval #DOWNLOAD_ERROR_DESTINATION Invalid destination + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be #DOWNLOAD_STATE_READY, #DOWNLOAD_STATE_PAUSED, #DOWNLOAD_STATE_CANCELED, #DOWNLOAD_STATE_FAILED. + * @post The download state will be #DOWNLOAD_STATE_QUEUED or #DOWNLOAD_STATE_DOWNLOADING + * @see download_set_url() + * @see download_pause() + * @see download_cancel() + */ +int download_start(int download_id); + + +/** + * @brief Pauses the download, asynchronously. + * + * @remarks The paused download can be restarted with download_start() or canceled with download_cancel() + * @param [in] download The download id + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be #DOWNLOAD_STATE_DOWNLOADING. + * @post The download state will be #DOWNLOAD_STATE_PAUSED. + * @see download_start() + * @see download_cancel() + */ +int download_pause(int download_id); + + +/** + * @brief Cancel the download, asynchronously. + * + * @details This function cancels the running download and its state will be #DOWNLOAD_STATE_READY + * @remarks The cancelled download can be restarted with download_start(). + * @param [in] download The download id + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_OUT_OF_MEMORY Out of memory + * @retval #DOWNLOAD_ERROR_INVALID_STATE Invalid state + * @retval #DOWNLOAD_ERROR_IO_ERROR Internal I/O error + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be #DOWNLOAD_STATE_QUEUED, #DOWNLOAD_STATE_DOWNLOADING, #DOWNLOAD_STATE_PAUSED. + * @post The download state will be #DOWNLOAD_STATE_CANCELED. + * @see download_start() + */ +int download_cancel(int download_id); + + +/** + * @brief Gets the download's current state. + * + * @param [in] download The download id + * @param [out] state The current state of the download + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @see #download_state_e + */ +int download_get_state(int download_id, download_state_e *state); + + +/** + * @brief Gets the full path of temporary file for downloading a content. + * + * @param [in] download The download id + * @param [out] temp_path The full path of temporary file + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be one of state after #DOWNLOAD_STATE_DOWNLOADING. + * @see #download_set_state_changed_cb() + * @see #download_unset_state_changed_cb() + * @see download_start() + */ +int download_get_temp_path(int download_id, char **temp_path); + + +/** + * @brief Gets the content name for downloading a file. + * + * @details This can be defined with referense of HTTP response header data. + * The content name can be received when HTTP response header is received. + * + * @param [in] download The download id + * @param [out] content_name The content name for displaying to user + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be one of state after #DOWNLOAD_STATE_DOWNLOADING. + * @see #download_set_state_changed_cb() + * @see #download_unset_state_changed_cb() + * @see download_start() + */ +int download_get_content_name(int download_id, char **content_name); + + +/** + * @brief Gets the total size for downloading a content. + * + * @details This data receive from content server. If the content sever don't send total size of the content, the value set as zero. + * + * @param [in] download The download id + * @param [out] content_size The content size for displaying to user + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be one of state after #DOWNLOAD_STATE_DOWNLOADING. + * @see #download_set_state_changed_cb() + * @see #download_unset_state_changed_cb() + * @see download_start() + */ +int download_get_content_size(int download_id, unsigned long long *content_size); + + +/** + * @brief Gets the error value when the download is failed. + * + * @param [in] download The download id + * @param [out] error The error value + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be #DOWNLOAD_STATE_FAILED. + * @pre The download state must be #DOWNLOAD_STATE_CANCELED. + * @see #download_set_state_changed_cb() + * @see #download_unset_state_changed_cb() + * @see download_start() + * @see download_error_e + */ +int download_get_error(int download_id, download_error_e *error); + + +/** + * @brief Gets the http status code when the download error is happened. + * + * @param [in] download The download id + * @param [out] http_status The http status code which is defined in RFC 2616 + * @return 0 on success, otherwise a negative error value. + * @retval #DOWNLOAD_ERROR_NONE Successful + * @retval #DOWNLOAD_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DOWNLOAD_ERROR_ID_NOT_FOUND No Download ID + * @pre The download state must be #DOWNLOAD_STATE_FAILED. + * @see #download_set_download_status_cb() + * @see #download_unset_download_status_cb() + * @see download_start() + */ +int download_get_http_status(int download_id, int *http_status); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_WEB_DOWNLOAD_H__ */ diff --git a/packaging/capi-web-url-download.spec b/packaging/capi-web-url-download.spec new file mode 100755 index 0000000..c9f5268 --- /dev/null +++ b/packaging/capi-web-url-download.spec @@ -0,0 +1,212 @@ + +Name: capi-web-url-download +Summary: CAPI for content download with web url +Version: 1.0.1 +Release: 6 +Group: Development/Libraries +License: Apache License, Version 2.0 +URL: N/A +Source0: %{name}-%{version}.tar.gz +BuildRequires: pkgconfig(capi-base-common) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(capi-appfw-app-manager) +BuildRequires: pkgconfig(capi-appfw-application) +BuildRequires: pkgconfig(download-provider) +BuildRequires: pkgconfig(dbus-1) +BuildRequires: cmake + +%description +CAPI for the content download + +%package devel +Summary: url download +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +CAPI for content downloading with web url (developement files) + +%prep +%setup -q + +%build +cmake . -DCMAKE_INSTALL_PREFIX="/" + +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install +mkdir -p %{buildroot}/usr/share/license + +%post -p /sbin/ldconfig + +%postun -p /sbin/ldconfig + +%files +%defattr(-,root,root,-) +%{_libdir}/libcapi-web-url-download.so.* +/usr/share/license/%{name} + +%files devel +%defattr(-,root,root,-) +%manifest capi-web-url-download.manifest +%{_libdir}/libcapi-web-url-download.so +%{_libdir}/pkgconfig/capi-web-url-download.pc +%{_includedir}/web/download.h + +%changelog +* Thu Jan 31 2013 Kwangmin Bang +- support new state of provider + +* Tue Jan 29 2013 Jungki Kwak +- Check invalid download id when set the callback +- Create new slot when set callback for the request exist only in provider + +* Mon Jan 28 2013 Kwangmin Bang +- simple launch provider without dbus interface +- Modify the name of license and add notice file +- refer wrong address after free + +* Tue Jan 08 2013 Jungki Kwak +- Update doxygen comments +- Add soversion to shared library file +- Remove unnecessary dependency and configuration +- Removed deprecated APIs + +* Fri Dec 21 2012 Kwangmin Bang +- Remove duplicated dlog message +- Modify license information at spec file +- Support ECHO command + +* Fri Dec 14 2012 Kwangmin Bang +- Resolve prevent defect +- Remove old source codes +- Add missed boiler plate +- the limitation of the number of download ID which can be created at same time +- Update comments about state +- Update comments + +* Fri Nov 30 2012 Kwangmin Bang +- support DBUS-activation +- [Prevent Defect] The same expression occurs on both sides of an operator + +* Tue Nov 27 2012 Jungki Kwak +- Remove deprecated APIs +- Add an error code about download id +- Change the name of notification API +- Add APIs for notification extra param +- Update comments and add some error code + +* Fri Nov 16 2012 Jungki Kwak +- Three APIs will be deprecated +- Operands don't affect result +- Add new CAPIs to register notification message +- Add to add, get and remove http header values +- Modify comments for getting file name +- send pid from client when no support SO_PEERCRED + +* Thu Nov 01 2012 Kwangmin Bang +- prevent duplicated event thread +- print dlog message with thread id +- enhance mutex for event thread + +* Wed Oct 31 2012 Jungki Kwak +- enhance mutex for event thread + +* Mon Oct 29 2012 Kwangmin Bang +- fix the defects found by prevent tool +- Thread Safe APIs +- increase the timeout of socket +- do not disconnect socket in case of timeout + +* Thu Oct 24 2012 Kwangmin Bang +- fix prevent defects +- Remove TCs about removed API +- arrange the usage of mutex for IPC + +* Tue Oct 23 2012 Kwangmin Bang +- check IPC connection at the head of All APIs +- use write API instead of send API +- apply sending timeout for socket + +* Mon Oct 22 2012 Jungki Kwak +- Resolve the lockup when client app is crashed +- Prevent the crash by calling url_download_destory twice +- Remove memory leak code by fixing of clients + +* Fri Oct 19 2012 Kwangmin Bang +- wrapping Handle based CAPI to ID based CAPI +- Call the progress callback when total size of the content is zero. + +* Fri Oct 12 2012 Jungki Kwak +- Fix the crash when clear invalid socket from FD_SET +- Install LICENSE file. + +* Fri Sep 21 2012 Jungki Kwak +- Apply a default manifest file. + +* Mon Sep 18 2012 Kwangmin Bang +- fix the crash by referring invalid handle +- Add function to release headers +- Add exception code when fail to reconnect socket +- add license +- fix the error and warning of spec file + +* Fri Sep 07 2012 Kwangmin Bang +- fix timeout thread can run exactly + +* Thu Sep 06 2012 Kwangmin Bang +- check the state before clear socket +- add limitation in already completed state +- add checking INVALID_STATE error + +* Tue Sep 04 2012 Jungki Kwak +- Remove unused code which is base on libdownload-agent +- Change the name of application operation +- Add exception handling for non requestid +- Call clear_provider after getting the state +- url_download_get_state return IO error +- Resolve prevent defects + +* Mon Sep 03 2012 Kwangmin Bang +- stop the download even if no socket or callback +- pause/resume the download even if no socket or callback +- calling stopped callback in error case +- request STOP for free after finished download + +* Thu Aug 30 2012 Kwangmin Bang +- maxfd should be updated before created event thread + +* Thu Aug 30 2012 Kwangmin Bang +- remove duplicated call for creating socket +- fix the crash regarding pthread_kill +- allow to call url_download_stop in case of PAUSED state + +* Mon Aug 27 2012 Kwangmin Bang +- one thread model for event +- get state info from download-provider even if no connection +- fix the bug take a long time to receive first event + +* Tue Aug 22 2012 Jungki Kwak +- Enhance the exception handling in event thread +- Resolve a bug about state +- Add to pass service handle data to download daemon + +* Mon Aug 17 2012 Jungki Kwak +- Resolve a bug when getting a state +- Add error case for invalid id + +* Mon Aug 16 2012 Jungki Kwak +- Add new APIs for notification function +- The TC is changed due to change of url_download_start +- When unseting the callback function, the callback should be initialized although the error is happened. +- It remove the stop function when is called twice when destroying handle +- Add pc requries for app.h + +* Mon Aug 08 2012 Jungki Kwak +- Change requestid to INTEGER from String + +* Mon Aug 06 2012 Jungki Kwak +- The base model is changed to download provider daemon + diff --git a/src/download.c b/src/download.c new file mode 100755 index 0000000..5990018 --- /dev/null +++ b/src/download.c @@ -0,0 +1,2945 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#ifdef DP_ECHO_TEST +#define DOWNLOAD_ECHO_SUPPORT +#endif +#ifdef DOWNLOAD_ECHO_SUPPORT +#include +#endif + +#ifdef DP_DBUS_ACTIVATION +#include +#endif + +#define MAX_DOWNLOAD_HANDLE 5 + +#define DEBUG_MSG +//#define DEBUG_PRINTF + +#ifdef DEBUG_MSG +#ifdef DEBUG_PRINTF +#include +#define TRACE_ERROR(format, ARG...) \ +{ \ +fprintf(stderr,"[URL_DOWNLOAD][%s:%d] "format"\n", __FUNCTION__, __LINE__, ##ARG); \ +} +#define TRACE_STRERROR(format, ARG...) \ +{ \ +fprintf(stderr,"[URL_DOWNLOAD][%s:%d] "format" [%s]\n", __FUNCTION__, __LINE__, ##ARG, strerror(errno)); \ +} +#define TRACE_INFO(format, ARG...) \ +{ \ +fprintf(stderr,"[URL_DOWNLOAD][%s:%d] "format"\n", __FUNCTION__, __LINE__, ##ARG); \ +} +#else +#include +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "TIZEN_N_URL_DOWNLOAD" + +#define TRACE_ERROR(format, ARG...) \ +{ \ +LOGE(format, ##ARG); \ +} +#define TRACE_STRERROR(format, ARG...) \ +{ \ +LOGE(format" [%s]", ##ARG, strerror(errno)); \ +} +#define TRACE_INFO(format, ARG...) \ +{ \ +LOGI(format, ##ARG); \ +} +#endif +#else +#define TRACE_DEBUG_MSG(format, ARG...) ; +#endif + +// define type +typedef struct { + // send command * get return value. + int cmd_socket; + // getting event from download-provider + int event_socket; + pthread_mutex_t mutex; // lock before using, unlock after using +} download_ipc_info; + +typedef struct { + download_state_changed_cb state; + void *state_user_data; + download_progress_cb progress; + void *progress_user_data; +} download_cb_info; + +typedef struct { + int id; + download_cb_info callback; +} download_slots; + +typedef struct { + int index; + dp_event_info *event; +} download_event_info; + +// declare the variables +download_ipc_info *g_download_ipc = NULL; +download_slots g_download_slots[MAX_DOWNLOAD_HANDLE]; +static pthread_mutex_t g_download_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_t g_download_event_thread_pid = 0; + +#ifdef DP_DBUS_ACTIVATION +/* DBUS Activation */ +static int _launch_download_provider_service(void) +{ + DBusConnection *connection = NULL; + DBusError dbus_error; + + dbus_error_init(&dbus_error); + + connection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error); + if (connection == NULL) { + TRACE_ERROR("[ERROR] dbus_bus_get"); + if (dbus_error_is_set(&dbus_error)) { + TRACE_ERROR("[DBUS] dbus_bus_get: %s", dbus_error.message); + dbus_error_free(&dbus_error); + } + return -1; + } + + dbus_uint32_t result = 0; + if (dbus_bus_start_service_by_name + (connection, + DP_DBUS_SERVICE_DBUS, 0, &result, &dbus_error) == FALSE) { + if (dbus_error_is_set(&dbus_error)) { + TRACE_ERROR("[DBUS] dbus_bus_start_service_by_name: %s", + dbus_error.message); + dbus_error_free(&dbus_error); + } + dbus_connection_unref(connection); + return -1; + } + if (result == DBUS_START_REPLY_ALREADY_RUNNING) { + TRACE_INFO("DBUS_START_REPLY_ALREADY_RUNNING [%d]", result); + } else if (result == DBUS_START_REPLY_SUCCESS) { + TRACE_INFO("DBUS_START_REPLY_SUCCESS [%d]", result); + } + dbus_connection_unref(connection); + return 0; +} +#endif + +//////////// defines functions ///////////////// + +int _download_change_dp_errorcode(int errorcode) +{ + switch (errorcode) + { + case DP_ERROR_NONE : + return DOWNLOAD_ERROR_NONE; + case DP_ERROR_INVALID_PARAMETER : + return DOWNLOAD_ERROR_INVALID_PARAMETER; + case DP_ERROR_OUT_OF_MEMORY : + return DOWNLOAD_ERROR_OUT_OF_MEMORY; + case DP_ERROR_IO_ERROR : + return DOWNLOAD_ERROR_IO_ERROR; + case DP_ERROR_NETWORK_UNREACHABLE : + return DOWNLOAD_ERROR_NETWORK_UNREACHABLE; + case DP_ERROR_NO_SPACE : + return DOWNLOAD_ERROR_NO_SPACE; + case DP_ERROR_FIELD_NOT_FOUND : + return DOWNLOAD_ERROR_FIELD_NOT_FOUND; + case DP_ERROR_INVALID_STATE : + return DOWNLOAD_ERROR_INVALID_STATE; + case DP_ERROR_CONNECTION_FAILED : + return DOWNLOAD_ERROR_CONNECTION_TIMED_OUT; + case DP_ERROR_INVALID_URL : + return DOWNLOAD_ERROR_INVALID_URL; + case DP_ERROR_INVALID_DESTINATION : + return DOWNLOAD_ERROR_INVALID_DESTINATION; + case DP_ERROR_QUEUE_FULL : + return DOWNLOAD_ERROR_QUEUE_FULL; + case DP_ERROR_ALREADY_COMPLETED : + return DOWNLOAD_ERROR_ALREADY_COMPLETED; + case DP_ERROR_FILE_ALREADY_EXISTS : + return DOWNLOAD_ERROR_FILE_ALREADY_EXISTS; + case DP_ERROR_TOO_MANY_DOWNLOADS : + return DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS; + case DP_ERROR_NO_DATA : + return DOWNLOAD_ERROR_NO_DATA; + case DP_ERROR_UNHANDLED_HTTP_CODE : + return DOWNLOAD_ERROR_UNHANDLED_HTTP_CODE; + case DP_ERROR_CANNOT_RESUME : + return DOWNLOAD_ERROR_CANNOT_RESUME; + case DP_ERROR_ID_NOT_FOUND : + return DOWNLOAD_ERROR_ID_NOT_FOUND; + case DP_ERROR_UNKNOWN : + return DOWNLOAD_ERROR_INVALID_STATE; + default: + break; + } + return DOWNLOAD_ERROR_NONE; +} + +int _download_change_dp_state(int state) +{ + switch (state) + { + case DP_STATE_READY: + return DOWNLOAD_STATE_READY; + case DP_STATE_CONNECTING: + return DOWNLOAD_STATE_QUEUED; + case DP_STATE_QUEUED : + return DOWNLOAD_STATE_QUEUED; + case DP_STATE_DOWNLOADING: + return DOWNLOAD_STATE_DOWNLOADING; + case DP_STATE_PAUSE_REQUESTED : + return DOWNLOAD_STATE_DOWNLOADING; + case DP_STATE_PAUSED : + return DOWNLOAD_STATE_PAUSED; + case DP_STATE_COMPLETED : + return DOWNLOAD_STATE_COMPLETED; + case DP_STATE_CANCELED : + return DOWNLOAD_STATE_CANCELED; + case DP_STATE_FAILED : + return DOWNLOAD_STATE_FAILED; + default: + break; + } + return DOWNLOAD_ERROR_NONE; +} + +void _download_print_str_state(int state) +{ + switch (state) + { + case DOWNLOAD_STATE_READY: + TRACE_INFO("DOWNLOAD_STATE_READY"); + break; + case DOWNLOAD_STATE_DOWNLOADING: + TRACE_INFO("DOWNLOAD_STATE_DOWNLOADING"); + break; + case DOWNLOAD_STATE_QUEUED : + TRACE_INFO("DOWNLOAD_STATE_QUEUED"); + break; + case DOWNLOAD_STATE_PAUSED : + TRACE_INFO("DOWNLOAD_STATE_PAUSED"); + break; + case DOWNLOAD_STATE_COMPLETED : + TRACE_INFO("DOWNLOAD_STATE_COMPLETED"); + break; + case DOWNLOAD_STATE_CANCELED : + TRACE_INFO("DOWNLOAD_STATE_CANCELED"); + break; + case DOWNLOAD_STATE_FAILED : + TRACE_INFO("DOWNLOAD_STATE_FAILED"); + break; + default: + TRACE_INFO("Unknown state (%d)", state); + break; + } +} + +int _get_my_slot_index(int download_id) +{ + int i = 0; + // search same info in array. + for (; i < MAX_DOWNLOAD_HANDLE; i++) + if (g_download_slots[i].id == download_id) + return i; + return -1; +} + +int _get_empty_slot_index() +{ + int i = 0; + for (; i < MAX_DOWNLOAD_HANDLE; i++) + if (g_download_slots[i].id <= 0) + return i; + return -1; +} + +int _ipc_read_custom_type(int fd, void *value, size_t type_size) +{ + if (fd < 0) { + TRACE_ERROR("[CHECK SOCKET]"); + return -1; + } + + if (read(fd, value, type_size) < 0) { + TRACE_STRERROR("[CRITICAL] read"); + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return -1; + } + return 0; +} + +int _download_ipc_read_int(int fd) +{ + if (fd < 0) { + TRACE_ERROR("[CHECK SOCKET]"); + return -1; + } + + int value = -1; + if (read(fd, &value, sizeof(int)) < 0) { + TRACE_STRERROR("[CRITICAL] read"); + // for debugging. + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return -1; + } + + TRACE_INFO(" %d", value); + return value; +} + +// keep the order/ unsigned , str +char* _ipc_read_string(int fd) +{ + unsigned length = 0; + char *str = NULL; + + if (fd < 0) { + TRACE_ERROR("[CHECK FD]"); + return NULL; + } + + // read flexible URL from client. + if (read(fd, &length, sizeof(unsigned)) < 0) { + TRACE_STRERROR("failed to read length [%d]", length); + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return NULL; + } + if (length < 1 || length > DP_MAX_STR_LEN) { + TRACE_ERROR("[STRING LEGNTH] [%d]", length); + return NULL; + } + str = (char *)calloc((length + 1), sizeof(char)); + if (!str) { + TRACE_STRERROR("[CRITICAL] allocation"); + return NULL; + } + if (read(fd, str, length * sizeof(char)) < 0) { + TRACE_STRERROR("failed to read string"); + free(str); + str = NULL; + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return NULL; + } + str[length] = '\0'; + return str; +} + +download_error_e _download_ipc_read_return(int fd) +{ + download_error_e errorcode = DOWNLOAD_ERROR_NONE; + dp_error_type error_info = DP_ERROR_NONE; + + if (fd < 0) { + TRACE_ERROR("[CHECK SOCKET]"); + return DOWNLOAD_ERROR_IO_ERROR; + } + + if (read(fd, &error_info, sizeof(dp_error_type)) < 0) { + TRACE_STRERROR("[CRITICAL] read"); + // maybe, download-provider was crashed. + // for debugging. + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return DOWNLOAD_ERROR_IO_ERROR; + } + + errorcode = _download_change_dp_errorcode(error_info); + TRACE_INFO("return : %d", errorcode); + return errorcode; +} + +dp_event_info* _download_ipc_read_event(int fd) +{ + if (fd < 0) { + TRACE_ERROR("[CHECK SOCKET]"); + return NULL; + } + + dp_event_info *event = + (dp_event_info *) calloc(1, sizeof(dp_event_info)); + if (event == NULL) { + TRACE_ERROR("[CHECK ALLOCATION]"); + return NULL; + } + if (read(fd, event, sizeof(dp_event_info)) < 0) { + TRACE_STRERROR("[CRITICAL] read"); + free(event); + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return NULL; + } + + TRACE_INFO("EVENT INFO (ID : %d state : %d error : %d)", + event->id, event->state, event->err); + return event; +} + +int _ipc_send_int(int fd, int value) +{ + if (fd < 0) { + TRACE_ERROR("[CHECK FD] [%d]", fd); + return -1; + } + + if (fd >= 0 && write(fd, &value, sizeof(int)) < 0) { + TRACE_STRERROR("send"); + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return -1; + } + return 0; +} + +// keep the order/ unsigned , str +int _ipc_send_string(int fd, const char *str) +{ + unsigned length = 0; + + if (fd < 0) { + TRACE_ERROR("[CHECK FD]"); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (!str) { + TRACE_ERROR("[CHECK STRING]"); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + length = strlen(str); + if (length < 1) { + TRACE_ERROR("[CHECK LENGTH]"); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (fd >= 0 && write(fd, &length, sizeof(unsigned)) < 0) { + TRACE_STRERROR("send"); + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return DOWNLOAD_ERROR_IO_ERROR; + } + if (fd >= 0 && write(fd, str, length * sizeof(char)) < 0) { + TRACE_STRERROR("send"); + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return DOWNLOAD_ERROR_IO_ERROR; + } + return DOWNLOAD_ERROR_NONE; +} + +int _download_ipc_send_command(int fd, int id, dp_command_type cmd) +{ + if (fd < 0) { + TRACE_ERROR("[CHECK SOCKET]"); + return DOWNLOAD_ERROR_IO_ERROR; + } + + dp_command command; + command.id = id; + command.cmd = cmd; + if (fd >= 0 && write(fd, &command, sizeof(dp_command)) < 0) { + TRACE_STRERROR("[CRITICAL] send"); + if (errno == EPIPE) { + TRACE_INFO("[EPIPE] Broken Pipe errno [%d]", errno); + } else if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] Resource temporarily unavailable errno [%d]", errno); + } else { + TRACE_INFO("errno [%d]", errno); + } + return DOWNLOAD_ERROR_IO_ERROR; + } + return DOWNLOAD_ERROR_NONE; +} + +download_error_e _send_simple_cmd(int id, dp_command_type cmd ) +{ + TRACE_INFO(""); + + if (cmd <= DP_CMD_NONE) { + TRACE_ERROR("[CHECK COMMAND] (%d)", cmd); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + // send commnad with ID + if (_download_ipc_send_command + (g_download_ipc->cmd_socket, id, cmd) != DOWNLOAD_ERROR_NONE) { + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + return errorcode; +} + +int __create_socket() +{ + TRACE_INFO(""); + int sockfd = -1; + struct timeval tv_timeo = { 2, 500000 }; //2.5 second + + struct sockaddr_un clientaddr; + if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { + TRACE_STRERROR("[CRITICAL] socket system error"); + return -1; + } + + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeo, + sizeof( tv_timeo ) ) < 0) { + TRACE_STRERROR("[CRITICAL] setsockopt SO_SNDTIMEO"); + close(sockfd); + return -1; + } + if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv_timeo, + sizeof( tv_timeo ) ) < 0) { + TRACE_STRERROR("[CRITICAL] setsockopt SO_SNDTIMEO"); + close(sockfd); + return -1; + } + + bzero(&clientaddr, sizeof clientaddr); + clientaddr.sun_family = AF_UNIX; + memset(clientaddr.sun_path, 0x00, sizeof(clientaddr.sun_path)); + strncpy(clientaddr.sun_path, DP_IPC, strlen(DP_IPC)); + clientaddr.sun_path[strlen(DP_IPC)] = '\0'; + if (connect(sockfd, + (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0) { + close(sockfd); + return -1; + } + TRACE_INFO("sockfd [%d]", sockfd); + return sockfd; +} + +int _disconnect_from_provider() +{ + TRACE_INFO(""); + if (g_download_ipc != NULL) { + shutdown(g_download_ipc->cmd_socket, 0); + close(g_download_ipc->cmd_socket); + g_download_ipc->cmd_socket= -1; + shutdown(g_download_ipc->event_socket, 0); + close(g_download_ipc->event_socket); + g_download_ipc->event_socket = -1; + pthread_mutex_destroy((&g_download_ipc->mutex)); + free(g_download_ipc); + g_download_ipc = NULL; + + if (g_download_event_thread_pid > 0) { + TRACE_INFO("STOP event thread"); + pthread_cancel(g_download_event_thread_pid); + g_download_event_thread_pid = 0; + TRACE_INFO("OK terminate event thread"); + } + } + return DOWNLOAD_ERROR_NONE; +} + +#ifdef DOWNLOAD_ECHO_SUPPORT +// clear read buffer. call in head of API before calling IPC_SEND +void __clear_read_buffer(int fd) +{ + long i; + long unread_count; + char tmp_char; + + // FIONREAD : Returns the number of bytes immediately readable + if (ioctl(fd, FIONREAD, &unread_count) >= 0) { + if (unread_count > 0) { + TRACE_INFO("[CLEAN] garbage packet[%ld]", unread_count); + for ( i = 0; i < unread_count; i++) { + if (read(fd, &tmp_char, sizeof(char)) < 0) { + TRACE_STRERROR("[CHECK] read"); + break; + } + } + } + } +} + +// ask to provider before sending a command. +// if provider wait in some commnad, can not response immediately +// capi will wait in read block. +// after asking, call clear_read_buffer. + + +int _check_ipc_status(int fd) +{ + // echo from provider + download_error_e errorcode = _send_simple_cmd(-1, DP_CMD_ECHO); + if (errorcode != DOWNLOAD_ERROR_NONE) { + TRACE_INFO("[ECHO] [%d]", errorcode); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + if (errno == EAGAIN) { + TRACE_INFO("[EAGAIN] provider is busy"); + } else { + TRACE_INFO("[CRITICAL] Broken Socket"); + _disconnect_from_provider(); + } + return -1; + } + } + __clear_read_buffer(fd); + return 0; +} +#endif + +// listen ASYNC state event, no timeout +void *_download_event_manager(void *arg) +{ + int maxfd, index; + fd_set rset, read_fdset; + dp_event_info *eventinfo = NULL; + + if (g_download_ipc == NULL || g_download_ipc->event_socket < 0) { + TRACE_STRERROR("[CRITICAL] IPC NOT ESTABILISH"); + return 0; + } + + // deferred wait to cencal until next function called. + // ex) function : select, read in this thread + pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); + + TRACE_INFO("FD [%d]", g_download_ipc->event_socket); + + maxfd = g_download_ipc->event_socket; + FD_ZERO(&read_fdset); + FD_SET(g_download_ipc->event_socket, &read_fdset); + + while(g_download_ipc != NULL + && g_download_ipc->event_socket >= 0) { + rset = read_fdset; + if (select((maxfd + 1), &rset, 0, 0, 0) < 0) { + TRACE_STRERROR("[CRITICAL] select"); + break; + } + + if (g_download_event_thread_pid <=0 + || pthread_self() != g_download_event_thread_pid) { + TRACE_ERROR + ("[CRITICAL] [CHECK TID] SELF ID [%d] Global ID (%d)", + pthread_self(), g_download_event_thread_pid); + // another thread may work. just terminate + return 0; + } + + pthread_mutex_lock(&g_download_mutex); + if (g_download_ipc == NULL + || g_download_ipc->event_socket < 0) { + TRACE_ERROR("[CRITICAL] IPC BROKEN Ending Event Thread"); + pthread_mutex_unlock(&g_download_mutex); + // disconnected by main thread. just terminate + return 0; + } + + if (FD_ISSET(g_download_ipc->event_socket, &rset) > 0) { + // read state info from socket + eventinfo = + _download_ipc_read_event(g_download_ipc->event_socket); + if (!eventinfo || eventinfo->id <= 0) { + // failed to read from socket // ignore this status + if (eventinfo) + free(eventinfo); + TRACE_STRERROR("[CRITICAL] Can not read Event packet"); + pthread_mutex_unlock(&g_download_mutex); + if (errno != EAGAIN) // if not timeout. end thread + break; + continue; + } + index = _get_my_slot_index(eventinfo->id); + if (index < 0) { + TRACE_ERROR + ("[CRITICAL] not found slot for [%d]", + eventinfo->id); + free(eventinfo); + pthread_mutex_unlock(&g_download_mutex); + continue; + } + + pthread_mutex_unlock(&g_download_mutex); + + // begin protect callback sections + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + + if (eventinfo->state == DP_STATE_DOWNLOADING + && eventinfo->received_size > 0) { + if (g_download_slots[index].callback.progress) { + // progress event + TRACE_INFO("ID %d progress callback %p", + eventinfo->id, + g_download_slots[index].callback.progress ); + g_download_slots[index].callback.progress + (eventinfo->id, eventinfo->received_size, + g_download_slots[index].callback.progress_user_data); + } + } else { + if (g_download_slots[index].callback.state) { + // state event + TRACE_INFO("ID %d state callback %p", + eventinfo->id, + g_download_slots[index].callback.state ); + g_download_slots[index].callback.state + (eventinfo->id, + _download_change_dp_state(eventinfo->state), + g_download_slots[index].callback.state_user_data); + } + } + free(eventinfo); + + // end protect callback sections + pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL); + continue; + } + pthread_mutex_unlock(&g_download_mutex); + } // while + + FD_ZERO(&read_fdset); + + TRACE_INFO("Terminate Event Thread"); + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO("Disconnect All Connection"); + g_download_event_thread_pid = 0; // set 0 to not call pthread_cancel + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return 0; +} + + +int _connect_to_provider() +{ + TRACE_INFO(""); + if (g_download_ipc == NULL) { + #ifdef DP_DBUS_ACTIVATION + if (_launch_download_provider_service() < 0) { + TRACE_ERROR("[DBUS IO] _launch_download_provider_service"); + return DOWNLOAD_ERROR_IO_ERROR; + } + #endif + g_download_ipc = + (download_ipc_info *) calloc(1, sizeof(download_ipc_info)); + } + + if (g_download_ipc != NULL) { + + int connect_retry = 3; + g_download_ipc->cmd_socket = -1; + while(g_download_ipc->cmd_socket < 0 && connect_retry-- > 0) { + g_download_ipc->cmd_socket = __create_socket(); + if (g_download_ipc->cmd_socket < 0) + usleep(50000); + } + if (g_download_ipc->cmd_socket < 0) { + TRACE_STRERROR("[CRITICAL] connect system error"); + free(g_download_ipc); + g_download_ipc = NULL; + return DOWNLOAD_ERROR_IO_ERROR; + } + // send a command + if (_ipc_send_int(g_download_ipc->cmd_socket, + DP_CMD_SET_COMMAND_SOCKET) < 0) { + close(g_download_ipc->cmd_socket); + free(g_download_ipc); + g_download_ipc = NULL; + return DOWNLOAD_ERROR_IO_ERROR; + } + #ifndef SO_PEERCRED + // send PID. Not support SO_PEERCRED + if (_ipc_send_int(g_download_ipc->cmd_socket, get_pid()) < 0) { + close(g_download_ipc->cmd_socket); + free(g_download_ipc); + g_download_ipc = NULL; + return DOWNLOAD_ERROR_IO_ERROR; + } + #endif + g_download_ipc->event_socket = __create_socket(); + if (g_download_ipc->event_socket < 0) { + TRACE_STRERROR("[CRITICAL] connect system error"); + close(g_download_ipc->cmd_socket); + free(g_download_ipc); + g_download_ipc = NULL; + return DOWNLOAD_ERROR_IO_ERROR; + } + // send a command + if (_ipc_send_int(g_download_ipc->event_socket, + DP_CMD_SET_EVENT_SOCKET) < 0) { + close(g_download_ipc->cmd_socket); + close(g_download_ipc->event_socket); + free(g_download_ipc); + g_download_ipc = NULL; + return DOWNLOAD_ERROR_IO_ERROR; + } + #ifndef SO_PEERCRED + // send PID. Not support SO_PEERCRED + if (_ipc_send_int + (g_download_ipc->event_socket, get_pid()) < 0) { + close(g_download_ipc->cmd_socket); + close(g_download_ipc->event_socket); + free(g_download_ipc); + g_download_ipc = NULL; + return DOWNLOAD_ERROR_IO_ERROR; + } + #endif + + int ret = pthread_mutex_init((&g_download_ipc->mutex), NULL); + if (ret != 0) { + TRACE_STRERROR("ERR:pthread_mutex_init FAIL with %d.", ret); + _disconnect_from_provider(); + return DOWNLOAD_ERROR_IO_ERROR; + } + + if (g_download_event_thread_pid <= 0) { + // create thread here ( getting event_socket ) + pthread_attr_t thread_attr; + if (pthread_attr_init(&thread_attr) != 0) { + TRACE_STRERROR("[CRITICAL] pthread_attr_init"); + _disconnect_from_provider(); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (pthread_attr_setdetachstate(&thread_attr, + PTHREAD_CREATE_DETACHED) != 0) { + TRACE_STRERROR("[CRITICAL] pthread_attr_setdetachstate"); + _disconnect_from_provider(); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (pthread_create(&g_download_event_thread_pid, + &thread_attr, + _download_event_manager, + g_download_ipc) != 0) { + TRACE_STRERROR("[CRITICAL] pthread_create"); + _disconnect_from_provider(); + return DOWNLOAD_ERROR_IO_ERROR; + } + } + } + return DOWNLOAD_ERROR_NONE; +} + +download_error_e _check_connections() +{ + int ret = 0; + if (g_download_ipc == NULL) + if ((ret = _connect_to_provider()) != DOWNLOAD_ERROR_NONE) + return ret; + if (g_download_ipc == NULL || g_download_ipc->cmd_socket < 0) { + TRACE_ERROR("[CHECK IPC]"); + return DOWNLOAD_ERROR_IO_ERROR; + } + return DOWNLOAD_ERROR_NONE; +} + + + +/////////////////////// APIs ///////////////////////////////// + +int download_create(int *download_id) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + int t_download_id = 0; + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + int index = _get_empty_slot_index(); + if (index < 0) { + TRACE_ERROR + ("[ERROR] TOO_MANY_DOWNLOADS[%d]", MAX_DOWNLOAD_HANDLE); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS; + } + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(-1, DP_CMD_CREATE); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + + // getting state with ID from provider. + t_download_id = _download_ipc_read_int(g_download_ipc->cmd_socket); + if (t_download_id < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + TRACE_INFO("ID : %d", t_download_id); + + *download_id = t_download_id; + g_download_slots[index].id = t_download_id; + pthread_mutex_unlock((&g_download_ipc->mutex)); + + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_destroy(int download_id) +{ + int index = -1; + int errorcode = DOWNLOAD_ERROR_NONE; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + index = _get_my_slot_index(download_id); + if (index >= 0) { + g_download_slots[index].id = 0; + g_download_slots[index].callback.state = NULL; + g_download_slots[index].callback.state_user_data = NULL; + g_download_slots[index].callback.progress = NULL; + g_download_slots[index].callback.progress_user_data = NULL; + } + errorcode = _send_simple_cmd(download_id, DP_CMD_DESTROY); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + + // after getting errorcode, send FREE to provider. + TRACE_INFO("Request to Free the memory for ID : %d", download_id); + // send again DP_CMD_FREE with ID. + errorcode = _download_ipc_send_command + (g_download_ipc->cmd_socket, download_id, DP_CMD_FREE); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_start(int download_id) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_START); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_pause(int download_id) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_PAUSE); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_cancel(int download_id) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_CANCEL); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + + +int download_set_url(int download_id, const char *url) +{ + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + if (!url) { + TRACE_ERROR("[CHECK url]"); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + TRACE_INFO(""); + pthread_mutex_lock(&g_download_mutex); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + // send commnad with ID + if (_download_ipc_send_command + (g_download_ipc->cmd_socket, download_id, DP_CMD_SET_URL) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_string(g_download_ipc->cmd_socket, url) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + + +int download_get_url(int download_id, char **url) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + char *value = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_URL); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting state with ID from provider. + value = _ipc_read_string(g_download_ipc->cmd_socket); + if (value == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *url = value; + TRACE_INFO("ID : %d url : %s", download_id, *url); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_set_network_type(int download_id, + download_network_type_e net_type) +{ + int network_type = DP_NETWORK_TYPE_ALL; + + if (net_type == DOWNLOAD_NETWORK_WIFI) + network_type = DP_NETWORK_TYPE_WIFI; + else if (net_type == DOWNLOAD_NETWORK_DATA_NETWORK) + network_type = DP_NETWORK_TYPE_DATA_NETWORK; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + if (_download_ipc_send_command(g_download_ipc->cmd_socket, + download_id, DP_CMD_SET_NETWORK_TYPE) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_int(g_download_ipc->cmd_socket, network_type) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_get_network_type(int download_id, + download_network_type_e *net_type) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + int network_type = DP_NETWORK_TYPE_ALL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_NETWORK_TYPE); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + + network_type = _download_ipc_read_int(g_download_ipc->cmd_socket); + if (network_type < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + if (network_type == DP_NETWORK_TYPE_WIFI) + *net_type = DOWNLOAD_NETWORK_WIFI; + else if (network_type == DP_NETWORK_TYPE_DATA_NETWORK) + *net_type = DOWNLOAD_NETWORK_DATA_NETWORK; + else + *net_type = DOWNLOAD_NETWORK_ALL; + + TRACE_INFO("ID : %d network_type : %d", download_id, *net_type); + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_set_destination(int download_id, const char *path) +{ + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + if (!path) { + TRACE_ERROR("[CHECK PATH]"); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + // send commnad with ID + if (_download_ipc_send_command + (g_download_ipc->cmd_socket, download_id, DP_CMD_SET_DESTINATION) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_string(g_download_ipc->cmd_socket, path) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + + +int download_get_destination(int download_id, char **path) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + char *value = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = + _send_simple_cmd(download_id, DP_CMD_GET_DESTINATION); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting string with ID from provider. + value = _ipc_read_string(g_download_ipc->cmd_socket); + if (value == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *path = value; + TRACE_INFO("ID : %d path : %s", download_id, *path); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_set_file_name(int download_id, const char *file_name) +{ + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + if (!file_name) { + TRACE_ERROR("[CHECK file_name]"); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + // send commnad with ID + if (_download_ipc_send_command + (g_download_ipc->cmd_socket, download_id, DP_CMD_SET_FILENAME) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_string(g_download_ipc->cmd_socket, file_name) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_get_file_name(int download_id, char **file_name) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + char *value = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = + _send_simple_cmd(download_id, DP_CMD_GET_FILENAME); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting string with ID from provider. + value = _ipc_read_string(g_download_ipc->cmd_socket); + if (value == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *file_name = value; + TRACE_INFO("ID : %d file_name : %s", download_id, *file_name); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_set_ongoing_notification(int download_id, bool enable) +{ + return download_set_notification(download_id, enable); +} + +int download_set_notification(int download_id, bool enable) +{ + int value = enable; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + if (_download_ipc_send_command(g_download_ipc->cmd_socket, + download_id, DP_CMD_SET_NOTIFICATION) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_int(g_download_ipc->cmd_socket, value) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + +} + +int download_get_ongoing_notification(int download_id, bool *enable) +{ + return download_get_notification(download_id, enable); +} + +int download_get_notification(int download_id, bool *enable) +{ + int value = 0; + int errorcode = DOWNLOAD_ERROR_NONE; + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_NOTIFICATION); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + value = _download_ipc_read_int(g_download_ipc->cmd_socket); + if (value < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *enable = value; + TRACE_INFO("ID : %d auto download : %d", download_id, *enable); + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; + +} + +int download_get_downloaded_file_path(int download_id, char **path) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + char *value = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = + _send_simple_cmd(download_id, DP_CMD_GET_SAVED_PATH); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting string with ID from provider. + value = _ipc_read_string(g_download_ipc->cmd_socket); + if (value == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *path = value; + TRACE_INFO("ID : %d path : %s", download_id, *path); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_set_notification_extra_param(int download_id, char *key, char *value) +{ + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + if (!key || !value) { + TRACE_ERROR("[CHECK param]"); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + TRACE_INFO(""); + pthread_mutex_lock(&g_download_mutex); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + // send commnad with ID + if (_download_ipc_send_command + (g_download_ipc->cmd_socket, download_id, DP_CMD_SET_EXTRA_PARAM) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_string(g_download_ipc->cmd_socket, key) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + if (_ipc_send_string(g_download_ipc->cmd_socket, value) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_get_notification_extra_param(int download_id, char **key, char **value) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + char *key_str = NULL; + char *value_str = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_EXTRA_PARAM); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting state with ID from provider. + key_str = _ipc_read_string(g_download_ipc->cmd_socket); + if (key_str == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + value_str = _ipc_read_string(g_download_ipc->cmd_socket); + if (value_str == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + free(key_str); + return DOWNLOAD_ERROR_IO_ERROR; + } + + *key = key_str; + *value = value_str; + TRACE_INFO("ID : %d key : %s value : %s", download_id, *key, *value); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_add_http_header_field(int download_id, const char *field, + const char *value) +{ + download_error_e errorcode = DOWNLOAD_ERROR_NONE; + TRACE_INFO(""); + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + if (!field || !value) { + TRACE_ERROR("[CHECK field or value]"); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + // send commnad with ID + if (_download_ipc_send_command + (g_download_ipc->cmd_socket, download_id, DP_CMD_SET_HTTP_HEADER) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + if (_ipc_send_string(g_download_ipc->cmd_socket, field) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + if (_ipc_send_string(g_download_ipc->cmd_socket, value) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_get_http_header_field(int download_id, + const char *field, char **value) +{ + download_error_e errorcode = DOWNLOAD_ERROR_NONE; + char *str = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + if (!field || !value) { + TRACE_ERROR("[CHECK field or value]"); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + if (_download_ipc_send_command + (g_download_ipc->cmd_socket, download_id, DP_CMD_GET_HTTP_HEADER) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + + if (_ipc_send_string(g_download_ipc->cmd_socket, field) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode != DOWNLOAD_ERROR_NONE) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + + // getting string with ID from provider. + str = _ipc_read_string(g_download_ipc->cmd_socket); + if (str == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *value = str; + TRACE_INFO("ID : %d field:%s value: %s", download_id, field, *value); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_remove_http_header_field(int download_id, + const char *field) +{ + download_error_e errorcode = DOWNLOAD_ERROR_NONE; + + TRACE_INFO(""); + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + if (!field) { + TRACE_ERROR("[CHECK field]"); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + // send commnad with ID + if (_download_ipc_send_command + (g_download_ipc->cmd_socket, download_id, DP_CMD_DEL_HTTP_HEADER) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_string(g_download_ipc->cmd_socket, field) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + + return errorcode; +} + +int download_set_state_changed_cb(int download_id, + download_state_changed_cb callback, void* user_data) +{ + int index = -1; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + if (callback == NULL) { + download_unset_state_changed_cb(download_id); + return DOWNLOAD_ERROR_NONE; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + // turn on state_cb flag of provider + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + // send create command. + if (_download_ipc_send_command(g_download_ipc->cmd_socket, + download_id, DP_CMD_SET_STATE_CALLBACK) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + download_unset_state_changed_cb(download_id); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_int(g_download_ipc->cmd_socket, 1) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + download_unset_state_changed_cb(download_id); + return DOWNLOAD_ERROR_IO_ERROR; + } + + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + download_unset_state_changed_cb(download_id); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_NONE) { + // search same info in array. + index = _get_my_slot_index(download_id); + if (index < 0) { + index = _get_empty_slot_index(); + if (index < 0) { + TRACE_ERROR + ("[ERROR] TOO_MANY_DOWNLOADS[%d]", MAX_DOWNLOAD_HANDLE); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS; + } + g_download_slots[index].id = download_id; + } + g_download_slots[index].callback.state = callback; + g_download_slots[index].callback.state_user_data = user_data; + } + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_unset_state_changed_cb(int download_id) +{ + int index = -1; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + index = _get_my_slot_index(download_id); + if (index >= 0) { + g_download_slots[index].callback.state = NULL; + g_download_slots[index].callback.state_user_data = NULL; + } + // turn off state_cb flag of provider + // send create command. + if (_download_ipc_send_command(g_download_ipc->cmd_socket, + download_id, DP_CMD_SET_STATE_CALLBACK) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_int(g_download_ipc->cmd_socket, 0) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_set_progress_cb(int download_id, + download_progress_cb callback, void *user_data) +{ + int index = -1; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + if (callback == NULL) { + download_unset_progress_cb(download_id); + return DOWNLOAD_ERROR_NONE; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + // turn on progress_cb flag of provider + // send create command. + if (_download_ipc_send_command(g_download_ipc->cmd_socket, + download_id, DP_CMD_SET_PROGRESS_CALLBACK) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + download_unset_progress_cb(download_id); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_int(g_download_ipc->cmd_socket, 1) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + download_unset_progress_cb(download_id); + return DOWNLOAD_ERROR_IO_ERROR; + } + + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + download_unset_progress_cb(download_id); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_NONE) { + // search same info in array. + index = _get_my_slot_index(download_id); + if (index < 0) { + index = _get_empty_slot_index(); + if (index < 0) { + TRACE_ERROR + ("[ERROR] TOO_MANY_DOWNLOADS[%d]", MAX_DOWNLOAD_HANDLE); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS; + } + g_download_slots[index].id = download_id; + } + + g_download_slots[index].callback.progress = callback; + g_download_slots[index].callback.progress_user_data = user_data; + } + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_unset_progress_cb(int download_id) +{ + int index = -1; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + index = _get_my_slot_index(download_id); + if (index >= 0) { + g_download_slots[index].callback.progress = NULL; + g_download_slots[index].callback.progress_user_data = NULL; + } + // turn off progress_cb flag of provider + // send create command. + if (_download_ipc_send_command(g_download_ipc->cmd_socket, + download_id, DP_CMD_SET_PROGRESS_CALLBACK) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_int(g_download_ipc->cmd_socket, 0) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_get_state(int download_id, download_state_e *state) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + dp_state_type dp_state = DP_STATE_NONE; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_STATE); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting state with ID from provider. + if (_ipc_read_custom_type(g_download_ipc->cmd_socket, + &dp_state, sizeof(dp_state_type)) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *state = _download_change_dp_state(dp_state); + TRACE_INFO("ID : %d state : %d", download_id, *state); + _download_print_str_state(*state); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_get_temp_path(int download_id, char **temp_path) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + char *value = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = + _send_simple_cmd(download_id, DP_CMD_GET_TEMP_SAVED_PATH); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting string with ID from provider. + value = _ipc_read_string(g_download_ipc->cmd_socket); + if (value == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *temp_path = value; + TRACE_INFO("ID : %d temp_path : %s", download_id, *temp_path); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_get_content_name(int download_id, char **content_name) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + char *value = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_CONTENT_NAME); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting string with ID from provider. + value = _ipc_read_string(g_download_ipc->cmd_socket); + if (value == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *content_name = value; + TRACE_INFO("ID : %d content_name : %s", download_id, *content_name); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_get_content_size(int download_id, + unsigned long long *content_size) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_TOTAL_FILE_SIZE); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + + // getting content_size from provider. + errorcode = _ipc_read_custom_type(g_download_ipc->cmd_socket, + content_size, sizeof(unsigned long long)); + if (errorcode < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + TRACE_INFO("ID : %d content_size %lld", download_id, *content_size); + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_get_mime_type(int download_id, char **mime_type) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + char *value = NULL; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_MIME_TYPE); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting state with ID from provider. + value = _ipc_read_string(g_download_ipc->cmd_socket); + if (value == NULL) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *mime_type = value; + TRACE_INFO("ID : %d mime_type : %s", download_id, *mime_type); + // it need to free + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_set_auto_download(int download_id, bool enable) +{ + int value = enable; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + if (_download_ipc_send_command(g_download_ipc->cmd_socket, + download_id, DP_CMD_SET_AUTO_DOWNLOAD) + != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + if (_ipc_send_int(g_download_ipc->cmd_socket, value) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + // return from provider. + download_error_e errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; +} + +int download_get_auto_download(int download_id, bool *enable) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + int value = 0; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_AUTO_DOWNLOAD); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + + value = _download_ipc_read_int(g_download_ipc->cmd_socket); + if (value < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *enable = (bool)value; + TRACE_INFO("ID : %d auto download : %d", download_id, *enable); + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_get_error(int download_id, download_error_e *error) +{ + download_error_e errorcode = DOWNLOAD_ERROR_NONE; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_ERROR); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting errorcode from provider. + errorcode = + _download_ipc_read_return(g_download_ipc->cmd_socket); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR) { + TRACE_ERROR("[CHECK IO] (%d)", download_id); + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + *error = errorcode; + TRACE_INFO("ID : %d error : %d", download_id, *error); + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + +int download_get_http_status(int download_id, int *http_status) +{ + int errorcode = DOWNLOAD_ERROR_NONE; + int status = 0; + + if (download_id <= 0) { + TRACE_ERROR("[CHECK ID] (%d)", download_id); + return DOWNLOAD_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&g_download_mutex); + TRACE_INFO(""); + + if (_check_connections() != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + + pthread_mutex_lock((&g_download_ipc->mutex)); + +#ifdef DOWNLOAD_ECHO_SUPPORT + if (_check_ipc_status(g_download_ipc->cmd_socket) < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } +#endif + + errorcode = _send_simple_cmd(download_id, DP_CMD_GET_HTTP_STATUS); + if (errorcode != DOWNLOAD_ERROR_NONE) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errorcode == DOWNLOAD_ERROR_IO_ERROR && errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return errorcode; + } + // getting int from provider. + status = _download_ipc_read_int(g_download_ipc->cmd_socket); + if (status < 0) { + pthread_mutex_unlock((&g_download_ipc->mutex)); + if (errno != EAGAIN) + _disconnect_from_provider(); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_IO_ERROR; + } + *http_status = status; + TRACE_INFO("ID : %d http_status : %d", download_id, *http_status); + pthread_mutex_unlock((&g_download_ipc->mutex)); + pthread_mutex_unlock(&g_download_mutex); + return DOWNLOAD_ERROR_NONE; +} + -- 2.7.4