/******************************************************************************
 * file    lib/management/common.c 
 * author  YuLiang
 * version 1.0.0
 * date    14-Sep-2021
 * brief   This file provides all the debug operation functions.
 *
 ******************************************************************************
 * Attention
 *
 * 
© COPYRIGHT(c) 2021 LandPower
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. 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.
 *   3. Neither the name of LandPower 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.
 *
 ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
//#ifdef HAVE_CONFIG_H
#include "config.h"
//#endif
#include "cmd.h"
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#ifdef CFG_DBG_ON
dbg_module_t _dbg_module[DBG_M_COUNT] =
{
    {DBG_M_DBG, TRUE, "debug"},
    {DBG_M_CLI, FALSE, "cli"},
    {DBG_M_MTIMER, FALSE, "timer"},
    {DBG_M_PROCESS, FALSE, "process"},
    {DBG_M_GPIO, FALSE, "gpio"},
    {DBG_M_PD, FALSE, "pd"},
    {DBG_M_PD_ERR, FALSE, "pd_err"},
    {DBG_M_PD_DAU, FALSE, "pd_dau"},
    {DBG_M_PD_DAU_SEND, FALSE, "pd_dau_send"},
    {DBG_M_PD_DAU_RECV, FALSE, "pd_dau_recv"},
    {DBG_M_PD_DAU_ERR, TRUE, "pd_dau_err"},
    {DBG_M_FIFO, FALSE, "fifo"},
    {DBG_M_FIFO_ERR, FALSE, "fifo_err"},
    {DBG_M_PD_CSG, FALSE, "csg"},
    {DBG_M_PD_CSG_ERR, FALSE, "csg_err"},
    {DBG_M_PD_CSG_TREND, FALSE, "csg_trend"},
    {DBG_M_PD_CSG_REAL, FALSE, "csg_real"},
    {DBG_M_PD_CSG_EIGEN, FALSE, "csg_eigen"},
    {DBG_M_STORAGE_ERR, FALSE, "stroage"},
    {DBG_M_DEBUG, FALSE, "debug"},
    {DBG_M_PD_UPGRADE, TRUE, "upgrade"},
};
/* Private function prototypes -----------------------------------------------*/
CMD(debug_on,
    debug_on_cmd,
    "debug WORD",
    "Debug\n"
    "Debug module\n")
{
    int32_t i = 0;
    
    for(i = 0; i < DBG_M_COUNT; i++)
    {
        if (strncmp(argv[0], _dbg_module[i].desc, strlen(_dbg_module[i].desc)))
        {
            continue;
        }
        dbg_cmd_hander(DBG_CMD_ON, i);
    }
    return CMD_SUCCESS;
}
CMD(no_debug_on,
    no_debug_on_cmd,
    "no debug WORD",
    NO_STR
    "Debug\n"
    "Debug module\n")
{
    int32_t i = 0;
    
    for(i = 0; i < DBG_M_COUNT; i++)
    {
        if (strncmp(argv[0], _dbg_module[i].desc, strlen(argv[0])))
        {
            continue;
        }
        dbg_cmd_hander(DBG_CMD_OFF, i);
    }
    return CMD_SUCCESS;
}
CMD(no_debug_all,
    no_debug_all_cmd,
    "no debug",
    NO_STR
    "Debug\n")
{
    dbg_cmd_hander(DBG_CMD_ALL_OFF, 0);
    return CMD_SUCCESS;
}
CMD(show_debug_all,
    show_debug_all_cmd,
    "show debug",
    SHOW_STR
    "Debug state\n")
{
    int32_t i = 0;
    
    for(i = 0; i < DBG_M_COUNT; i++)
    {
        vty_out(vty, "%03d | %-16s %s%s", i, _dbg_module[i].desc, _dbg_module[i].stat ? "on" : "off", VTY_NEWLINE);
    }
    return CMD_SUCCESS;
}
/* Internal functions --------------------------------------------------------*/
/* 开指定模块的debug */
static int32_t _dbg_on(DBG_MODULE_E module)
{
    if (module >= DBG_M_COUNT)
    {
        return E_BAD_PARAM;
    }
    _dbg_module[module].stat = TRUE;
    return E_NONE;
}
/* 关指定模块的debug */
static int32_t _dbg_off(DBG_MODULE_E module)
{
    if (module >= DBG_M_COUNT)
    {
        return E_BAD_PARAM;
    }
    _dbg_module[module].stat = FALSE;
    return E_NONE;
}
/* 关所有模块的debug */
static int32_t _dbg_all_off(void)
{
    unsigned int i = 0;
    for(i = 0; i < DBG_M_COUNT; i++)
    {
        _dbg_module[i].stat = FALSE;
    }
    return E_NONE;
}
/* Interface functions -------------------------------------------------------*/
/* description: 获取当前模块debug状态
   param:   module  --  模块ID
   return:  (TRUE)开启 (FALSE)关闭 */
int32_t dbg_stat_get(DBG_MODULE_E module)
{
    if (module >= DBG_M_COUNT)
    {
        return FALSE;
    }
    return _dbg_module[module].stat;
}
/* description: debug模块命令函数分发.
   param:   module  --  模块ID
   return:  (E_NONE)成功,(其他)失败 */
int32_t dbg_cmd_hander(DBG_CMD_E cmd, int32_t module)
{
    switch(cmd)
    {
        case DBG_CMD_ON:
            LD_E_RETURN(DBG_M_DBG, _dbg_on(module));
            break;
        case DBG_CMD_OFF:
            LD_E_RETURN(DBG_M_DBG, _dbg_off(module));
            break;
        case DBG_CMD_ALL_OFF:
            LD_E_RETURN(DBG_M_DBG, _dbg_all_off());
            break;
        default:
            break;
    }
    return E_NONE;
}
/* description: debug模块初始化
   param:
   return: */
void dbg_init(void)
{
    cmd_install_element(COMMON_NODE, &show_debug_all_cmd);
    cmd_install_element(ENABLE_NODE, &debug_on_cmd);
    cmd_install_element(ENABLE_NODE, &no_debug_on_cmd);
    cmd_install_element(ENABLE_NODE, &no_debug_all_cmd);
}
#else
int32_t dbg_stat_get(DBG_MODULE_E module)
{
    return FALSE;
}
int dbg_cmd_hander(DBG_CMD_E cmd, int32_t module)
{
    return E_NONE;
}
void dbg_init(void)
{
}
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/