/**
 *  @file ins_ut_imu.c
 *  @details config unit test
 *
 */

#include <stdlib.h>
#include "unittest.h"
#include "unittest/device/imu/ins_ut_imu.h"
#include "ins_mw_config.h"
#include "ins_pal_error_code.h"
#include "ins_comm_log.h"

#include "ins_dev_gyro.h"
#include "AmbaUtility.h"

extern void InsDev_Gyro_SetUnittestLogFlag(uint8_t log_flag);
static int32_t InsUT_Device_ImuOpen(int32_t argc, char **argv)
{
    int32_t rval = 0;
    InsDev_Gyro_t *imu_dev = InsDev_Gyro_Init(0);

	argc = argc;
	argv = argv;
//    if(argc > 0)
//	{
//		INSLOG_E("argc = %d\n", argc);
//	    for(int i = 0; i < argc; i++)
//	    {
//	        INSLOG_E("argv[%d] = %s\n", i, argv[i]);
//	    }
//	}
    // if(argc < 1)
    // {
    //     INSLOG_W("Input para not enough!\n  imu open\n");
    //     return INSERR_NG;
    // }
    //if (AmbaUtility_StringCompare(argv[0], "open", 4U) == 0)
    {
        rval = imu_dev->open();
    }

    return (int32_t)rval;
}

static int32_t InsUT_Device_ImuClose(int32_t argc, char **argv)
{
    int32_t rval = 0;
    InsDev_Gyro_t *imu_dev = InsDev_Gyro_Init(0);

	argc = argc;
	argv = argv;
//    if(argc > 0)
//	{
//		INSLOG_E("argc = %d\n", argc);
//	    for(int i = 0; i < argc; i++)
//	    {
//	        INSLOG_E("argv[%d] = %s\n", i, argv[i]);
//	    }
//	}
    // if(argc < 1)
    // {
    //     INSLOG_W("Input para not enough!\n  imu close\n");
    //     return INSERR_NG;
    // }
    //if (AmbaUtility_StringCompare(argv[0], "close", 5U) == 0)
    {
        InsDev_Gyro_SetUnittestLogFlag(0);
        rval = imu_dev->close();
    }

    return (int32_t)rval;
}

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

	argc = argc;
	argv = argv;
//	if(argc > 0)
//	{
//		INSLOG_E("argc = %d\n", argc);
//	    for(int i = 0; i < argc; i++)
//	    {
//	        INSLOG_E("argv[%d] = %s\n", i, argv[i]);
//	    }
//	}

    InsDev_Gyro_SetUnittestLogFlag(1);

    return (int32_t)rval;
}

static int32_t InsUT_Device_ImuCalib(int32_t argc, char **argv)
{
    int32_t rval = 0;
    InsDev_Gyro_t *imu_dev = InsDev_Gyro_Init(0);
	InsDev_Gyro_Data_t cal_data;
	uint16_t cal_time;

#if 0
	if(argc > 0)
	{
		INSLOG_E("argc = %d\n", argc);
	    for(int i = 0; i < argc; i++)
	    {
	        INSLOG_E("argv[%d] = %s\n", i, argv[i]);
	    }
	}
#endif
    
	if(argc < 1)
	{
		INSLOG_W("Input para not enough!\n");
		INSLOG_W("you can input calib_data 200!\n");
		return INSERR_NG;
	}
    //if (AmbaUtility_StringCompare(argv[0], "open", 4U) == 0)
    {
    	//cal_time = 200;
    	cal_time = atoi(argv[0]);
        rval = imu_dev->calib(&cal_data, cal_time);
		INSLOG_W("tick = 0x%x, cal_time = %d\n", cal_data.tick, cal_time);
		INSLOG_W("acc_x = %f, acc_y = %f, acc_z = %f\n", cal_data.acc_x, cal_data.acc_y, cal_data.acc_z);
		INSLOG_W("gyro_x = %f, gyro_y = %f, gyro_z = %f\n", cal_data.gyro_x, cal_data.gyro_y, cal_data.gyro_z);
    }

    return (int32_t)rval;
}

InsUT_Unittest_CmdInfo_t g_ut_cmd_imu[INSUT_CMDCNT_IMU] = 
{
    {InsUT_Device_ImuOpen,      			0, NULL, "open",       		"imu gyro open"},
    {InsUT_Device_ImuClose,     			0, NULL, "close",       	"imu gyro close"},
    {InsUT_Device_ImuOpenInterruptData,     0, NULL, "inter_data",		"whether there is interrupt data"},
    {InsUT_Device_ImuCalib,     			0, NULL, "calib_data",   	"whether there is calib data"},
};
	