/*****************************************************************************************************************************
  File name     : mw_ut_config.c
  Module        : 
  Author        : 
  Copyright     : 
  Version       : 0.1
  Created on    : 2024-09-07
  Creator       : amir.liang
  Description   :
        Config for unittest
*************************************************************************************************************************************/
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "mw_config.h"
#include "mw_ut_config.h"


static int32_t init(int32_t argc, char **argv)
{
    (void)argc;
    (void)argv;
    return Mw_Cfg_Init(mw_storage_write, mw_storage_read);;
}

static int32_t save(int32_t argc, char **argv)
{
    uint32_t part = 0;

    if(argc < 1)
    {
        hlogw("Input para not enough!");
        return NG;
    }
    part = atoi(argv[0]);
    return Mw_Cfg_Save(part);
}

static int32_t print(int32_t argc, char **argv)
{
    uint32_t id = 0;
    if(argc <= 0)
    {
        return Mw_Cfg_PrintAll();
    }
    id = atoi(argv[0]);

    return Mw_Cfg_PrintId(id);
}

static int32_t set(int32_t argc, char **argv)
{
    uint32_t id = 0;
    uint32_t value = 0;

    if(argc < 2)
    {
        hlogw("Input para not enough!");
        return NG;
    }
    id = atoi(argv[0]);
    value = atoi(argv[1]);
    return Mw_Cfg_SetConfig((uint32_t)id, (void *)&value, sizeof(int));
}

static int32_t get(int32_t argc, char **argv)
{
    uint32_t id = 0;
    uint32_t value = 0;
    uint32_t ret = 0;

    if(argc < 2)
    {
        hlogw("Input para not enough!");
        return NG;
    }
    id = atoi(argv[0]);
    value = atoi(argv[1]);
    ret = Mw_Cfg_GetConfig((uint32_t)id, (void *)&value, sizeof(value));
    if(OK == ret)
    {
        hlogi("Get id[%d] success, data:%d", id, value);
        Mw_Cfg_PrintId(id);
    }
    else
    {
        hloge("Get id[%d] failed", id);
    }
    return ret;
}

static int32_t reset(int32_t argc, char **argv)
{
    uint32_t part = 0;

    if(argc < 1)
    {
        hlogw("Input para not enough!");
        return NG;
    }
    part = atoi(argv[0]);
    return Mw_Cfg_LoadDefault(part);
}

unittest_cmd g_ut_cmd_config[UT_CMDCNT_CONFIG] =
{
    DECLAR_UT_CMD(init ,   NULL, "init",        "No Param: Init config"),
    DECLAR_UT_CMD(print,   NULL, "print",       "No Param: Print all; [ID]: Print sigle id"),
    DECLAR_UT_CMD(save,    NULL, "save",        "[partition]: Save sigle partition"),
    DECLAR_UT_CMD(reset,   NULL, "reset",       "[partition]: Reset sigle partition"),
    DECLAR_UT_CMD(set,     NULL, "set",         "[id] [value]: Set sigle id(only support int type)"),
    DECLAR_UT_CMD(get,     NULL, "get",         "[id] [value]: Get sigle id"),
};

