/* 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.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 * $RCSfile: x_dbg.h,v $
 * $Revision: #1 $
 * $Date: 2015/11/10 $
 * $Author: dtvbm11 $
 *
 * Description:
 *         This header file contains debug related definitions, which are
 *         exported.
 *---------------------------------------------------------------------------*/


 
 
/*----------------------------------------------------------------------------*/
/*! @addtogroup groupMW_DBG
  *  @{  
  */
/*----------------------------------------------------------------------------*/
#ifndef _X_DBG_H_
#define _X_DBG_H_


/*-----------------------------------------------------------------------------
                    include files
-----------------------------------------------------------------------------*/

#include "u_dbg.h"
#include "u_common.h"


/*-----------------------------------------------------------------------------
                    macros, defines, typedefs, enums
 ----------------------------------------------------------------------------*/

extern INT32 dbg_ctrl_print_fl(const CHAR    *ps_file,UINT16  ui2_line);
//extern INT32 dbg_ctrl_stmt(const CHAR*  ps_format, ...);

#ifndef ANDROID
extern INT32 dbg_ctrl_stmt (const CHAR*  ps_format, ...);
#else
	#ifdef ANDROID_TOOLCHAIN
	    #ifdef __KERNEL__
    	extern INT32 dbg_ctrl_stmt (const CHAR*  ps_format, ...);
	    #else
    	    #ifdef CC_MTK_LOADER
        	extern INT32 dbg_ctrl_stmt (const CHAR*  ps_format, ...);
	        #else
		#ifndef CC_MEM_TOOL
    	        #ifndef __android_log_print
        	    extern int __android_log_print(int prio, const char *tag, const char *fmt, ...);
            	#endif
	            #include <android/log.h>
    	        #define dbg_ctrl_stmt(fmt...)\
        	    __android_log_print(ANDROID_LOG_DEBUG, "MTK_MW", fmt)
	        
		#else
			#define dbg_ctrl_stmt(fmt...)
		#endif
		#endif
    	#endif
	#else
		extern INT32 dbg_ctrl_stmt (const CHAR*  ps_format, ...);
	#endif
#endif

/* Macro to be used for level control debug statements. Note that */
/* the extern API declaration in the macro DBG_LEVEL_STMT was     */
/* placed there so that that API's external declaration is not    */
/* visible outside the macro's namespace.                         */
#undef DBG_LEVEL_STMT


#define DBG_LEVEL_STMT(_module_name,_ctrl_level,_dbg_level, _stmt_level, _stmt)         \
{                                                              \
    if(x_dbg_check_lvl(_ctrl_level,_dbg_level,_stmt_level,__FILE__,__FUNCTION__,__LINE__,__TIME__,__DATE__))\
    {                                                            \
        dbg_ctrl_stmt("{%s} ",_module_name);                       \
        dbg_ctrl_stmt _stmt;                                   \
    }                                                           \
}

#undef DBG_LOG_STMT

#ifndef __KERNEL__
#define DBG_LOG_STMT(_stmt)                               \
{                                                         \
    dbg_ctrl_print_fl(__FILE__,__LINE__);    \
    dbg_ctrl_stmt _stmt;                                 \
}                                                         
#else
#define DBG_LOG_STMT(_stmt)
#endif

/**<Macro to be used for level control debug statements.  Note that  
 the extern API declaration in the macro DBG_LEVEL_STMT was
 placed there so that that API's external declaration is not
 visible outside the macro's namespace. */


/* Debug setup types. */

/*------------------------------------------------------------------*/
/*! @brief     Indicates the output type of debug log information.
  *  @code 
  *  typedef enum 
  *  { 
  *      DBG_OUTPUT_TYPE_IGNORE = 0, 
  *      DBG_OUTPUT_TYPE_SERIAL_PORT
  *  } DBG_OUTPUT_TYPE_T; 
  *  @endcode 
  *  @li@c  DBG_OUTPUT_TYPE_IGNORE      -Ingore the debug log
  *  @li@c  DBG_OUTPUT_TYPE_SERIAL_PORT -Output the debug log to serial port.
  */
/*------------------------------------------------------------------*/
typedef enum
{
    DBG_OUTPUT_TYPE_IGNORE = 0,
    DBG_OUTPUT_TYPE_SERIAL_PORT
}   DBG_OUTPUT_TYPE_T;


/* Callback function definitions. */

/*------------------------------------------------------------------*/
/*! @brief  This type defines the debug log output callback function prototype.
  *  @param [in] ps_stmt           Contain the content to be output.
  */
/*------------------------------------------------------------------*/
typedef VOID (*x_dbg_output_fct) (const CHAR*  ps_stmt);


/*------------------------------------------------------------------*/
/*! @brief  This type defines the debug trace record callback function prototype.
  *  @param [in] b_trace_ena        Contain the switch of enable/disable trace.
  *  @param [in] ui4_code           Contain the code of trace info.
  *  @param [in] pv_data            Contain the data.
  *  @param [in] z_len              Contain the length of data. 
  */
/*------------------------------------------------------------------*/
typedef VOID (*x_dbg_trace_rec_fct) (BOOL         b_trace_ena,
                                     UINT32       ui4_code,
                                     const VOID*  pv_data,
                                     SIZE_T       z_len);
                                     
                                     
/*------------------------------------------------------------------*/
/*! @brief  This type defines the debug data input callback function prototype.
  *  @param [in] ui1_inp_data           Contain the input data.
  *  @param [in] pv_tag                 Contain the tag information.
  */
/*------------------------------------------------------------------*/
typedef VOID (*x_dbg_data_inp_fct) (UINT8  ui1_inp_data,
                                    VOID*  pv_tag);


/*-----------------------------------------------------------------------------
                    functions declarations
 ----------------------------------------------------------------------------*/

/* The following API's are exported by the Middleware. */
/*------------------------------------------------------------------*/
/*! @brief  This API dumps the content of the trace buffer. Note that the
*           trace buffer will be disabled duriung the dumping and
*           re-enabled (if required) afterwards.
*  @param [in]   pf_output     References a rotine which will handle the outputing of the trace record.
*  @retval      -DBGR_OK                    Routine successful.
*  @retval      -DBGR_NOT_INIT              Debug library not initialized.
*  @retval      -DBGR_NO_TRACE_BUFFER       No trace buffer allocated.
*  @retval      -DBGR_NO_OUTPUT_DEVICE      No output device available.
*  @retval      -DBGR_DUMP_IN_PROGRESS      Trace buffer dump is in progress.
*  @retval      -DBGR_NOT_ENABLED           Debug library not compiled with define DEBUG.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_dump_trace_buff (x_dbg_output_fct  pf_output);

/*------------------------------------------------------------------*/
/*! @brief  This API clears the trace buffer. Essentially, it simply resets the read and write indices.
*  @retval      -DBGR_OK               Routine successful.
*  @retval      -DBGR_NOT_INIT         Debug library not initialized.
*  @retval      -DBGR_NO_TRACE_BUFFER  No trace buffer allocated.
*  @retval      -DBGR_NOT_ENABLED      Debug library not compiled with define DEBUG.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_flush_trace_buff (VOID);

/*------------------------------------------------------------------*/
/*! @brief  This API returns the trace buffer state.
*  @retval      -TRUE             The control flag of trace buffer is enable.
*  @retval      -FALSE            The control flag of trace buffer is disable.
*/
/*------------------------------------------------------------------*/
extern BOOL x_dbg_get_trace_buff_ctrl (VOID);

/*------------------------------------------------------------------*/
/*! @brief  This API registers a record trace callback function. To prevent
*           simmultanous access of the callback function pointer, a critical
*           section start / stop is used.
*  @param [in]   pf_trace_rec     References the record trace callback fucntion.
*  @retval      -DBGR_OK             Routine successful.
*  @retval      -DBGR_NOT_INIT       Debug library not initialized.
*  @retval      -DBGR_NOT_ENABLED    Debug library not compiled with define DEBUG
*  @retval      -DBGR_REG_CB_ACTIVE  A callback is still active.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_reg_trace_rec_cb (x_dbg_trace_rec_fct  pf_trace_rec);

/*------------------------------------------------------------------*/
/*! @brief  This API registers a data input callback function with the debug library.
*  @param [in]   pf_data_inp     References the data receive callback function.
*  @param [in]   pv_tag          Contains some tag value which is returned with the callback function.
*  @retval      -DBGR_OK             Routine successful.
*  @retval      -DBGR_NOT_INIT       Debug library not initialized.
*  @retval      -DBGR_NOT_ENABLED    Debug library not compiled with define DEBUG
*  @retval      -DBGR_REG_CB_ACTIVE  A callback is still active.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_reg_data_inp_cb (x_dbg_data_inp_fct  pf_data_inp,
									VOID*        	    pv_tag);
/*------------------------------------------------------------------*/
/*! @brief  This API enables or disables the trace buffer recording. This
*           API may be called from within an interrupt context.
*  @param [in]   b_trace_ena        Set to TRUE of the trace buffer recording shall be enabled else FALSE.
*  @retval      -DBGR_OK             Routine successful.
*  @retval      -DBGR_NOT_INIT       Debug library not initialized.
*  @retval      -DBGR_NOT_ENABLED    Debug library not compiled with define DEBUG
*  @retval      -DBGR_REG_CB_ACTIVE  A callback is still active.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_set_trace_buff_ctrl (BOOL  b_trace_ena);


/*------------------------------------------------------------------*/
/*! @brief  This API output the debug string as specified format by control method.
*  @param [in]   ps_format     The specified output format.
*  @param [in]   ...                References to parameters of output.
*  @retval      -DBGR_OK                      Routine successful.
*  @retval      -DBGR_INV_ARG            The Input arguments are invalid.
*  @retval      -DBGR_NOT_INIT           The DBG moudle hasn't be initilized.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_ctrl_stmt (const CHAR*  ps_format, ...);

/*------------------------------------------------------------------*/
/*! @brief  This API output  specia message (data ,time ,line)with the log level
*  @param [in]   ui4_dbg_lvl     The  module's current log level.
*  @param [in]   ui4_stmt_lvl     current dbg outout level
*  @param [in]   ps_file            file name
*  @param [in]   ps_func          function name
*  @param [in]   ui2_line           line number
*  @param [in]   ps_time          time
*  @param [in]   ps_date          date
*  @retval      TRUE                 need to printf dbg message.
*  @retval      FALSE               no need to printf dbg message.
*/
/*------------------------------------------------------------------*/
extern BOOL x_dbg_check_lvl(UINT8 ui1_ctrl_lvl,
                         UINT32 ui4_dbg_lvl, 
                        UINT32 ui4_stmt_lvl,
                        const CHAR    *ps_file,
                        const CHAR    *ps_func,
                        UINT16  ui2_line,
                        CHAR   *ps_time,
                        CHAR   *ps_date);



/*------------------------------------------------------------------*/
/*! @brief  This API output the secure log as specified format.
*  @param [in]   ps_format     The specified output format.
*  @param [in]   ...                References to parameters of output.
*  @retval      -DBGR_OK                     Routine successful.
*  @retval      -DBGR_INV_ARG            The Input arguments are invalid.
*  @retval      -DBGR_NOT_INIT           The DBG moudle hasn't be initilized.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_secure_stmt (const CHAR*  ps_format, ...);


/*------------------------------------------------------------------*/
/*! @brief  This API enable secure log, after that all log will encrypt and output to the specified file.
*  @param [in]   ps_path       The specified output file to store secure log.
*  @retval      -DBGR_OK                     Routine successful.
*  @retval      -DBGR_INV_ARG            The Input arguments are invalid.
*  @retval      -DBGR_NOT_INIT           The DBG moudle hasn't be initilized.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_secure_log_enable(char * ps_path);


/*------------------------------------------------------------------*/
/*! @brief  This API disable secure log
*  @retval      -DBGR_OK                     Routine successful.
*  @retval      -DBGR_NOT_INIT           The DBG moudle hasn't be initilized.
*/
/*------------------------------------------------------------------*/
extern INT32 x_dbg_secure_log_disable(VOID);


#endif /* _X_DBG_H_ */

/*----------------------------------------------------------------------------*/
/*! @} */                                                 /* end of groupMW_DBG*/
/*----------------------------------------------------------------------------*/




