montage-tech/src/kware/network/http_file/download_api.cpp
/*
*
*
*   RETURN VALUE:
*         0 means fail and  1 means success -1 means aborted by user
*
*
*
*/
RETURN VALUE:
0 means fail and  1 means success -1 means aborted by user
RETURN VALUE : 0 means fail and 1 means success - 1 means aborted by user
int Nw_DownloadURLTimeout(const char * url,
                          const char * tempFile,
                          unsigned int timeoutSec,
                          void * response,
                          void * arg,
                          const char * extraHeaders,
                          const char * body,
                          unsigned int bodyLen)
{
    DOWNLOAD_API_DEBUG("[%s] start start ...\n", __func__);
    DOWNLOAD_API_DEBUG("[%s] timeout %d seconds\n", __func__, timeoutSec);
    if (url == NULL) {
        DOWNLOAD_API_ERROR("[%s] url is NULL, please check it\n", __func__);
        return 0;
    }
    DOWNLOAD_API_DEBUG("[%s] url [%s]...\n", __func__, url);
    char tmpurl[MAX_TMP_URL_LEN];
    char file[MAX_FILE_NAME_LEN];
    memset(tmpurl, 0, MAX_TMP_URL_LEN);
    sprintf(tmpurl, "%s", url);
    memset(file, 0, MAX_FILE_NAME_LEN);
    sprintf(file, "%s", tempFile);
    http_download_mini instance;
    http_download_mini * dl = &instance;
    if (dl == NULL) {
        DOWNLOAD_API_ERROR("[%s] [ERROR] invalid dl (NULL) ...!!!\n", __func__);
        return 0;
    }
#ifndef __LINUX__
    mtos_task_info_t info = {0};
    mtos_task_get_info(&info);
    mtos_printk("[%s]  info.prio = %d...!!!\n", __func__, info.prio);
    if(check_task_download_running((int)info.prio))
    {
        abort_download_task((int)info.prio, TRUE);
        remove_task_from_manager((int)info.prio);
    }
    add_task_to_manager((int)info.prio, TRUE, dl);
#endif	
    HttpDownloadMiniSpeed loadresult = dl->download(tmpurl, timeoutSec*1000, file, NULL, false, extraHeaders, body, bodyLen,NULL);
#ifndef __LINUX__
    remove_task_from_manager((int)info.prio);
#endif
    DOWNLOAD_API_DEBUG("[%s] download result state: [%d] (0:COMPLETE; 1:ERR; 2:TIMEOUT; 3:ABORT; 4:ABORT by user\n", __func__, loadresult.state);
    DOWNLOAD_API_DEBUG("[%s] download size: [%d]\n", __func__, loadresult.size);
    if(loadresult.state == HTTP_MINI_ABORT_BY_USER)
    {
    	 delete_file(file);
        return -1;
    }
    if(loadresult.state != HTTP_MINI_COMPLETE || loadresult.size< 0)
    {
    	 delete_file(file);
        return 0;
    }
    DOWNLOAD_API_DEBUG("[%s] end end...\n", __func__);
    return 1;
}
int Nw_DownloadURLTimeout ( const char * url , const char * tempFile , unsigned int timeoutSec , void * response , void * arg , const char * extraHeaders , const char * body , unsigned int bodyLen ) { DOWNLOAD_API_DEBUG ( " \n " , __func__ ) ; DOWNLOAD_API_DEBUG ( " \n " , __func__ , timeoutSec ) ; if ( url == NULL ) { DOWNLOAD_API_ERROR ( " \n " , __func__ ) ; return 0 ; } DOWNLOAD_API_DEBUG ( " \n " , __func__ , url ) ; char tmpurl [ MAX_TMP_URL_LEN ] ; char file [ MAX_FILE_NAME_LEN ] ; memset ( tmpurl , 0 , MAX_TMP_URL_LEN ) ; sprintf ( tmpurl , " " , url ) ; memset ( file , 0 , MAX_FILE_NAME_LEN ) ; sprintf ( file , " " , tempFile ) ; http_download_mini instance ; http_download_mini * dl = & instance ; if ( dl == NULL ) { DOWNLOAD_API_ERROR ( " \n " , __func__ ) ; return 0 ; } #ifndef __LINUX__ mtos_task_info_t info = { 0 } ; mtos_task_get_info ( & info ) ; mtos_printk ( " \n " , __func__ , info . prio ) ; if ( check_task_download_running ( ( int ) info . prio ) ) { abort_download_task ( ( int ) info . prio , TRUE ) ; remove_task_from_manager ( ( int ) info . prio ) ; } add_task_to_manager ( ( int ) info . prio , TRUE , dl ) ; #endif HttpDownloadMiniSpeed loadresult = dl -> download ( tmpurl , timeoutSec * 1000 , file , NULL , false , extraHeaders , body , bodyLen , NULL ) ; #ifndef __LINUX__ remove_task_from_manager ( ( int ) info . prio ) ; #endif DOWNLOAD_API_DEBUG ( " \n " , __func__ , loadresult . state ) ; DOWNLOAD_API_DEBUG ( " \n " , __func__ , loadresult . size ) ; if ( loadresult . state == HTTP_MINI_ABORT_BY_USER ) { delete_file ( file ) ; return -1 ; } if ( loadresult . state != HTTP_MINI_COMPLETE || loadresult . size < 0 ) { delete_file ( file ) ; return 0 ; } DOWNLOAD_API_DEBUG ( " \n " , __func__ ) ; return 1 ; }
RETURN VALUE:
0 means fail and  1 means success -1 means aborted by user
RETURN VALUE : 0 means fail and 1 means success - 1 means aborted by user