#include "ins_ut_cmd.h"

#include <stdlib.h>
#include <string.h>

#include "ins_comm_log.h"


static int32_t InsUT_Cmd_String(int32_t argc, char **argv)
{
    int32_t i = 0;
    int32_t cmd_size = 0, tmp_size = 0;
    char cmd_string[256] = {0};

    if(argc < 1 || argv == NULL)
    {
        return INSUT_UNITTEST_USAGE;
    }

    for(i=0; i<argc; i++)
    {
        tmp_size = strlen(argv[i]);
        if((cmd_size + tmp_size) < 256)
        {
            strcat(cmd_string, argv[i]);
            strcat(cmd_string, " ");
            cmd_size += (tmp_size+1);
        }
        else
        {
            INSLOG_E("Cmd String size > 256");
            return INSUT_UNITTEST_USAGE;
        }
    }
    cmd_string[cmd_size-1] = '\0';

    return InsUT_Unittest_RunCmdString(cmd_string);
}

static int32_t InsUT_Cmd_File(int32_t argc, char **argv)
{
    int32_t rval = 0, i = 0;

    if(argc < 1 || argv == NULL)
    {
        return INSUT_UNITTEST_USAGE;
    }

    for(i=0; i<argc; i++)
    {
        rval |= InsUT_Unittest_RunCmdFile(1, &argv[i]);
    }
    return rval;
}


static int32_t g_cmd_rval[8] = {0};

static int32_t InsUT_Cmd_GetRval(int32_t argc, char **argv)
{
    if(argc != 1 || argv == NULL)
    {
        return INSUT_UNITTEST_USAGE;
    }
    if(atoi(argv[0]) >= 8)
    {
        return INSUT_UNITTEST_USAGE;
    }

    return g_cmd_rval[atoi(argv[0])];
}

static int32_t InsUT_Cmd_SetRval(int32_t argc, char **argv)
{
    if(argc != 2 || argv == NULL)
    {
        return INSUT_UNITTEST_USAGE;
    }
    if(atoi(argv[0]) >= 8)
    {
        return INSUT_UNITTEST_USAGE;
    }

    g_cmd_rval[atoi(argv[0])] = atoi(argv[1]);
    return 0;
}

static int32_t InsUT_Cmd_OrRval(int32_t argc, char **argv)
{
    if(argc != 2 || argv == NULL)
    {
        return INSUT_UNITTEST_USAGE;
    }
    if(atoi(argv[0]) >= 8)
    {
        return INSUT_UNITTEST_USAGE;
    }

    g_cmd_rval[atoi(argv[0])] |= atoi(argv[1]);
    return 0;
}

static int32_t InsUT_Cmd_ChkRval(int32_t argc, char **argv)
{
    if(argc != 2 || argv == NULL)
    {
        return INSUT_UNITTEST_USAGE;
    }
    if(atoi(argv[0]) >= 8)
    {
        return INSUT_UNITTEST_USAGE;
    }

    INSLOG_I("Cmd check rval %d %d", g_cmd_rval[atoi(argv[0])], atoi(argv[1]));
    if(g_cmd_rval[atoi(argv[0])] == atoi(argv[1]))
    {
        return 0;
    }
    else
    {
        return -1;
    }
}


InsUT_Unittest_CmdInfo_t g_ut_cmd_cmd[INSUT_CMDCNT_CMD] =
{
    {InsUT_Cmd_String,  0,  NULL,   "string",       "[string(256)]"},
    {InsUT_Cmd_File,    0,  NULL,   "file",         "[filename(64)]"},
    {InsUT_Cmd_GetRval, 0,  NULL,   "get_rval",     "[index(8)]"},
    {InsUT_Cmd_SetRval, 0,  NULL,   "set_rval",     "[index(8)][rval]"},
    {InsUT_Cmd_OrRval,  0,  NULL,   "or_rval",      "[index(8)][rval]"},
    {InsUT_Cmd_ChkRval, 0,  NULL,   "chk_rval",     "[index(8)][rval]"},
};

