/******************************************************************************
 * 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
 *
 * <h2><center>&copy; COPYRIGHT(c) 2021 LandPower</center></h2>
 *
 * 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, FALSE, "debug"},
    {DBG_M_CLI, FALSE, "cli"},
    {DBG_M_MTIMER, FALSE, "timer"},
    {DBG_M_PROCESS, FALSE, "process"},
    {DBG_M_GPIO, FALSE, "gpio"},
    {DBG_M_FIFO, FALSE, "fifo"},
    {DBG_M_FIFO_ERR, TRUE, "fifo_err"},
    {DBG_M_CA_COLL, FALSE, "ca_coll"},
    {DBG_M_CA_COLL_ERR, TRUE, "ca_coll_err"},
    {DBG_M_CA_DB, FALSE, "ca_db"},
    {DBG_M_CA_DB_ERR, TRUE, "ca_db_err"},
    {DBG_M_CA_DBG, FALSE, "ca_dbg"},
    {DBG_M_CA_DBG_ERR, TRUE, "ca_dbg_err"},
    {DBG_M_CA_MQTT, TRUE, "ca_mqtt"},
    {DBG_M_CA_MQTT_ERR, TRUE, "ca_mqtt_err"},
    {DBG_M_CA_LAND, FALSE, "ca_land"},
    {DBG_M_CA_LAND_ERR, TRUE, "ca_land_err"}
};

/* Private function prototypes -----------------------------------------------*/
CMD(debug_on,
    debug_on_cmd,
    "debug WORD",
    "Debug\n"
    "Debug module\n")
{
    int32_t i = 0;
    int32_t len_desc = 0;
    int32_t len_cmd = 0;
    
    for(i = 0; i < DBG_M_COUNT; i++)
    {
        len_desc = strlen(_dbg_module[i].desc);
        len_cmd = strlen(argv[0]);
        
        if (strncmp(argv[0], _dbg_module[i].desc, len_desc > len_cmd ? len_desc : len_cmd))
        {
            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;
    int32_t len_desc = 0;
    int32_t len_cmd = 0;
    
    for(i = 0; i < DBG_M_COUNT; i++)
    {
        len_desc = strlen(_dbg_module[i].desc);
        len_cmd = strlen(argv[0]);
        
        if (strncmp(argv[0], _dbg_module[i].desc, len_desc > len_cmd ? len_desc : len_cmd))
        {
            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 ****************/
