/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*----------------------------------------------------------------------------*/
/*! @file c_httpc_api.h 
 *  $RCSfile: $
 *  $Revision: #1 $
 *  $Date: 2015/05/29 $
 *  $Author: brianpc.huang $
 *  
 *  @par Description:
 *         This header file contains http client specific
 *         definitions, which are exported.
 */
/*----------------------------------------------------------------------------*/

/*!
* @addtogroup  groupMW_INET_HTTP
* @brief       Provide an external API about HTTP.
* %HTTP API would be used by user who wants to download by http protocol.  
* 
*
* @{
* */

#ifndef _C_HTTPC_API_H_
#define _C_HTTPC_API_H_

#include "u_httpc_api.h"

/*------------------------------------------------------------------*/
/*! @brief Init http module
 *  @note use in system start
 */
/*------------------------------------------------------------------*/
extern VOID             c_httpc_init_module(VOID);

/*------------------------------------------------------------------*/
/*! @brief Allocate data pointer and init it's content
 *  @param [out] pt_data - the storage save data pointer
 */
/*------------------------------------------------------------------*/
extern VOID    c_httpc_data_init(HTTPC_DATA_T *pt_data);

/*------------------------------------------------------------------*/
/*! @brief Free allocated data pointer
 *  @param [in] pt_data - data pointer to free
 */
/*------------------------------------------------------------------*/
extern VOID    c_httpc_data_free(HTTPC_DATA_T *pt_data);

/*------------------------------------------------------------------*/
/*! @brief Allocate a new http session for future use
 *  @param [out] ph_handle -the handle for this session
 *  @return    result
 *  @retval    HTTPR_OK   -successful    
 *  @retval    Others         -failed    
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_new_session(HANDLE_T *ph_handle);

/*------------------------------------------------------------------*/
/*! @brief Close an opened file
 *  @param [in] h_file -the session to free
 *  @return    result
 *  @retval    HTTPR_OK                         -successful    
 *  @retval    HTTPR_INVALID_HANDLE    -error handle   
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_free_session(HANDLE_T h_file);

/*------------------------------------------------------------------*/
/*! @brief Abort async operation
 *  @param [in] h_req -the handle for abort async operation
 *  @return    result
 *  @retval    HTTPR_OK                        -successful    
 *  @retval    HTTPR_INVALID_HANDLE   -error handle    
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_abort_async(HANDLE_T h_req);

/*------------------------------------------------------------------*/
/*! @brief Sync http get method
 *  @param [in] h_file -http session handle
 *  @param [in] ps_url -http url
 *  @param [out] pt_data -the data structure which save the http get body
 *  @return    result
 *  @retval    HTTPR_OK   -successful    
 *  @retval    Others         -failed 
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_get(HANDLE_T h_file, 
                                   const CHAR *ps_url, 
                                   HTTPC_DATA_T *pt_data);

/*------------------------------------------------------------------*/
/*! @brief Sync http  post method
 *  @param [in] h_file -http session handle
 *  @param [in] ps_url -http url
 *  @param [in] pv_body -data will be post to server
 *  @param [in] z_body_size -size of http post data
 *  @param [out] pt_data -the data structure to save return http body
 *  @return    result
 *  @retval    HTTPR_OK   -successful    
 *  @retval    Others         -failed 
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_post(HANDLE_T      h_file,
                                    const CHAR   *ps_url,
                                    VOID         *pv_body,
                                    SIZE_T        z_body_size,
                                    HTTPC_DATA_T *pt_data);

/*------------------------------------------------------------------*/
/*! @brief Sync http  post method
 *  @param [in] h_file -http session handle
 *  @param [in] ps_url -http url
 *  @param [out] pt_data -the data structure which save the http get body
 *  @param [in] pf_nfy_fct -async  call back function
 *  @param [in] pv_tag -private data passed to callback
 *  @param [out] ph_req -async http request
 *  @return    result
 *  @retval    HTTPR_OK   -successful    
 *  @retval    HTTPR_INVALID_HANDLE    -invalid http handle
 *  @retval    HTTPR_OUT_OF_MEMORY     -out of memory
 *  @retval    HTTPR_SEND_MSG_FAILED   -send message to queue error
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_get_async(HANDLE_T                h_file,
                                         const CHAR             *ps_url,
                                         HTTPC_DATA_T           *pt_data, 
                                         x_httpc_async_fct    pf_nfy_fct,
                                         VOID                   *pv_tag,
                                         HANDLE_T               *ph_req);

extern HTTPCR_CODE_T c_httpc_head_async(HANDLE_T                h_file,
                                         const CHAR             *ps_url,
                                         HTTPC_DATA_T           *pt_data, 
                                         x_httpc_async_fct    pf_nfy_fct,
                                         VOID                   *pv_tag,
                                         HANDLE_T               *ph_req);
/*------------------------------------------------------------------*/
/*! @brief Async http post method
 *  @param [in] h_file -http session handle
 *  @param [in] ps_url -http url
 *  @param [in] pv_body -data will be post to server
 *  @param [in] z_body_size -size of http post data
 *  @param [out] pt_data -the data structure to save return http body
 *  @param [in] pf_nfy_fct -async  call back function
 *  @param [in] pv_tag -private data passed to callback
 *  @param [out] ph_req -async http request
 *  @return    result
 *  @retval    HTTPR_OK   -successful    
 *  @retval    HTTPR_INVALID_HANDLE    -invalid http handle
 *  @retval    HTTPR_OUT_OF_MEMORY     -out of memory
 *  @retval    HTTPR_SEND_MSG_FAILED   -send message to queue error
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_post_async(HANDLE_T             h_file,
                                          const CHAR          *ps_url,
                                          VOID                *pv_body,
                                          SIZE_T               z_body_size,
                                          HTTPC_DATA_T        *pt_data,
                                          x_httpc_async_fct pf_nfy_fct,
                                          VOID                *pv_tag,
                                          HANDLE_T            *ph_req);

/*------------------------------------------------------------------*/
/*! @brief Sync http  put method
 *  @param [in] h_file -http session handle
 *  @param [in] ps_url -http url
 *  @param [in] pv_body -upload data
 *  @param [in] z_body_size -upload data size
 *  @param [out] pt_data -the data structure to save return http body
 *  @return    result
 *  @retval    HTTPR_OK   -successful    
 *  @retval    Others        -failed 
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T    c_httpc_put(HANDLE_T      h_file,
                                    const CHAR   *ps_url,
                                    VOID         *pv_body,
                                    SIZE_T        z_body_size,
                                    HTTPC_DATA_T *pt_data);

/*------------------------------------------------------------------*/
/*! @brief Async http put method
 *  @param [in] h_file -http session handle
 *  @param [in] ps_url -http url
 *  @param [in] pv_body -upload data
 *  @param [in] z_body_size -upload data size
 *  @param [out] pt_data -the data structure to save return http body
 *  @param [in] pf_nfy_fct -async  call back function
 *  @param [in] pv_tag -private data passed to callback
 *  @param [out] ph_req -async http request
 *  @return    result
 *  @retval    HTTPR_OK                           -successful    
 *  @retval    HTTPR_INVALID_HANDLE     -invalid http handle
 *  @retval    HTTPR_OUT_OF_MEMORY     -out of memory
 *  @retval    HTTPR_SEND_MSG_FAILED   -send message to queue error
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T    c_httpc_put_async(HANDLE_T            h_file,
                                          const CHAR         *ps_url,
                                          VOID               *pv_body,
                                          SIZE_T              z_body_size,
                                          HTTPC_DATA_T       *pt_data,
                                          x_httpc_async_fct   pf_nfy_fct,
                                          VOID               *pv_tag,
                                          HANDLE_T           *ph_req);

/*------------------------------------------------------------------*/
/*! @brief Append user specify header to http command
 *  @param [in] h_file -Handle of an opened file
 *  @param [in] ps_hdr -header
 *  @return    result
 *  @retval    HTTPR_OK                        -successful    
 *  @retval    HTTPR_INVALID_HANDLE   -error handle    
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_append_header(HANDLE_T    h_file,
                                             const CHAR *ps_hdr);

/*------------------------------------------------------------------*/
/*! @brief Free user append header to http command
 *  @param [in] h_file -Handle of an opened file
 *  @return    result
 *  @retval    HTTPR_OK                        -successful    
 *  @retval    HTTPR_INVALID_HANDLE   -error handle    
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T   c_httpc_clear_append_header(HANDLE_T h_file);

/*------------------------------------------------------------------*/
/*! @brief Set session option
 *  @param [in] h_handle -http session handle
 *  @param [in] e_option -Option Kind of argument
 *  @param [in] pv_val -argument reference
 *  @return    result
 *  @retval    HTTPR_OK   -successful    
 *  @retval    Others         -failed 
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T    c_httpc_set_option(HANDLE_T h_handle, 
                                           HTTP_HANDLE_OPT_T e_option, 
                                           VOID* pv_val);

/*------------------------------------------------------------------*/
/*! @brief Get session infomation
 *  @param [in] h_handle -http session handle
 *  @param [in] e_option -Option Kind of argument
 *  @param [in] pv_val -infomation of argument address
 *  @return    result
 *  @retval    HTTPR_OK     -successful    
 *  @retval    Others          -failed 
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T    c_httpc_get_info(HANDLE_T h_handle, 
                                         HTTP_HANDLE_INFO_T e_option, 
                                         VOID* pv_val);

/*------------------------------------------------------------------*/
/*! @brief Set progress callback function
 *  @param [in] h_handle -http session handle
 *  @param [in] pf_progrs_cb -call back function
 *  @param [in] pv_tag -private data passed to callback
 *  @return    result
 *  @retval    HTTPR_OK                        -successful    
 *  @retval    HTTPR_INVALID_HANDLE   -error handle   
 */
/*------------------------------------------------------------------*/
extern HTTPCR_CODE_T    c_httpc_set_progress_callback(HANDLE_T h_handle, 
                                            x_httpc_progress_fct pf_progrs_cb, 
                                            VOID* pv_tag);


extern INT32 c_httpc_fm_open(const CHAR *ps_url, 
                             UINT32 ui4_buffer_size, 
                             HANDLE_T *ph_handle);

extern INT32 c_httpc_fm_open_proxy(const CHAR *ps_url, 
                            const CHAR *ps_proxy, 
                            UINT32 ui4_buffer_size, 
                            HANDLE_T *ph_handle);

extern INT32 c_httpc_fm_close(HANDLE_T h_file);

extern INT32 c_httpc_fm_read(HANDLE_T h_file, 
                             VOID *pv_data, 
                             UINT32 ui4_count, 
                             UINT32 *pui4_read);

extern INT32 c_httpc_get_last_modify_info(HANDLE_T   h_file,
                                            VOID*   pv_val);

extern INT32 c_httpc_fm_read_async(HANDLE_T        h_file,
                                   VOID            *pv_data,
                                   UINT32          ui4_count,
                                   UINT8           ui1_pri,
                                   x_fm_async_fct  pf_nfy_fct,
                                   VOID            *pv_tag,
                                   HANDLE_T        *ph_req);

extern INT32 c_httpc_fm_abort_async(HANDLE_T h_file, HANDLE_T h_req);

extern INT32 c_httpc_fm_feof(HANDLE_T h_file, BOOL *pb_eof);

extern INT32 c_httpc_fm_lseek(HANDLE_T h_file, 
                              INT64 i8_offset, 
                              UINT8 ui1_whence, 
                              UINT64 *pui8_cur_pos);

extern INT32 c_httpc_fm_get_info_by_handle(HANDLE_T h_file, FM_FILE_INFO_T *pt_info);

extern INT32 c_httpc_fm_get_rsp_hdr(HANDLE_T h_file, CHAR *pc_hdr, UINT32 *pui4_rsp_hdr_len);

extern INT32 c_httpc_fm_set_progress_callback(HANDLE_T h_file, 
                                              x_httpc_fm_progress_fct pf_progrs_cb, VOID* pv_tag);
/*dont free the return value*/
extern CHAR* c_httpc_fm_get_content_type(HANDLE_T h_file);

extern INT32 c_httpc_fm_open_proxy_agent(const CHAR *ps_url, 
                            const CHAR *ps_proxy, 
                            const CHAR *ps_agent, 
                            UINT32 ui4_buffer_size, 
                            HANDLE_T *ph_handle);
                            
extern INT32 c_httpc_fm_open_proxy_agent_timeout(const CHAR *ps_url, 
                            const CHAR *ps_proxy, 
                            const CHAR *ps_agent, 
                            UINT32 ui4_time_out,
                            UINT32 ui4_buffer_size, 
                            HANDLE_T *ph_handle);

#endif /*_C_HTTPC_API_H_*/

/*! @} */
