/* 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_dbg.h
 * $Revision: #2 $
 * $Date: 2016/03/04 $
 * $Author: jason.chen $
 *
 * @par Description:
 *         This header file contains debug related definitions, which are
 *         exported to application space.
 *---------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------*/
/*! @addtogroup groupMW_DBG
  *  @brief Provides external function TV Application
  *  %MW Application API would be used by TV Application, lot of these APIs would
  *   be wrapped from Exported API
  *  @see groupMW
  *  @{  
  */
/*----------------------------------------------------------------------------*/
 

#ifndef _C_DBG_H_
#define _C_DBG_H_


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

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


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

/* 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.                         */
extern INT32 dbg_ctrl_print_fl(const CHAR    *ps_file,UINT16  ui2_line);
#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
    
#undef DBG_LEVEL_STMT


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

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

#define DBG_LOG_STMT(_stmt)                              \
{                                                        \
    dbg_ctrl_print_fl(__FILE__,__LINE__);    \
    dbg_ctrl_stmt _stmt;                                 \
}                                                        \
/*-----------------------------------------------------------------------------
                    functions declarations
 ----------------------------------------------------------------------------*/


/*------------------------------------------------------------------*/
/*! @brief  This API output the debug string as specified format by control methd.
*  @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 c_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 c_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 c_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 c_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 c_dbg_secure_log_disable(VOID);


#endif /* _C_DBG_H_ */


#if(defined(MW_DBG_LOG_TO_FILE) && (defined(DEBUG) || defined(CLI_SUPPORT)))

typedef UINT32 t_device_status;
#define DEVICE_MOUNTED      ((t_device_status) 0)
#define DEVICE_UNMOUNTED    ((t_device_status) 1)

extern VOID c_dbg_log_to_file_notify(VOID* pv_tag, t_device_status t_status, const CHAR* ps_mount_point);
extern BOOL c_dbg_log_to_file_is_running(VOID);

#endif

extern VOID c_dbg_dump_stack(VOID);

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




