#include "ins_ut_ambalink.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "ins_mw_mmc_event.h"
#include "ins_mw_media.h"
#include "ins_mw_device_rx.h"
#include "ins_comm_log.h"
#include <app/record/record.h>
#include <app/live/live.h>
#include <app/applink/ambalink.h>
#include <proto_handler/ucd/rtos_message.h>
#include "ins_pal_memory.h"
#include "ins_pal_system.h"
#include "ins_dev_interface.h"
#include "ins_mw_ambalink.h"
#include "ins_mw_rpmsg.h"

static int32_t InsUT_Ambalink_LaunchModule(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);

    InsMw_MMC_Init();
    InsMw_Media_Init();
    //InsApp_Live_Init();
#if defined(CONFIG_ENABLE_AMBALINK)
    InsMw_Ambalink_Init();
    InsMw_Rpmsg_Init();
    InsApp_Handler_AppLinkInit();
#endif
    InsMw_DeviceRx_Init();
    return 0;
}

static int32_t InsUT_Ambalink_ExitModule(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    //test code dengyanbing 2021/12/07
    InsMw_DeviceRx_Deinit();    
    InsMw_Media_Deinit();
    //InsApp_Live_Deinit();
    InsMw_MMC_Deinit();
    return 0;
}

//extern int32_t InsProto_Rtos_Message_OpenWifi(InsMw_Rpmsg_Channel_Id_t msg_dev_id);
//extern int32_t InsProto_Rtos_Message_CloseWifi(InsMw_Rpmsg_Channel_Id_t msg_dev_id);
//extern int32_t InsProto_Rtos_Message_Rpmsg_Test(InsMw_Rpmsg_Channel_Id_t msg_dev_id, char *msg);
//extern int32_t InsProto_Rtos_Message_Start_Rpc_Server(InsMw_Rpmsg_Channel_Id_t msg_dev_id);
//extern int32_t InsProto_Rtos_Message_StartBT(InsMw_Rpmsg_Channel_Id_t msg_dev_id, uint32_t mode);
//extern int32_t InsProto_Rtos_Message_StopBT(InsMw_Rpmsg_Channel_Id_t msg_dev_id);

static int32_t InsUT_Ambalink_Open_Wifi(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsProto_Rtos_Message_OpenWifi(INSMW_RPMSG_CHANNEL_ID_COMM);
    return 0;
}

static int32_t InsUT_Ambalink_Close_Wifi(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsProto_Rtos_Message_CloseWifi(INSMW_RPMSG_CHANNEL_ID_COMM);
    return 0;
}

static int32_t InsUT_Ambalink_Wifi_SetFreq(int32_t argc, char **argv)
{
    uint32_t freq = 0;

    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    
    if ((argc != 1 || NULL == argv))
    {
        return INSUT_UNITTEST_USAGE;
    }
    
    freq = atoi(argv[0]);
    InsProto_Rtos_Message_Wifi_SetFreq(INSMW_RPMSG_CHANNEL_ID_COMM, freq);
    return 0;
}

static int32_t InsUT_Ambalink_Rpmsg_Test(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsProto_Rtos_Message_Rpmsg_Test(INSMW_RPMSG_CHANNEL_ID_COMM, "Rpmsg test");
    return 0;
}

static int32_t InsUT_Ambalink_Start_RpcServer(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsProto_Rtos_Message_Start_Rpc_Server(INSMW_RPMSG_CHANNEL_ID_COMM);
    return 0;
}

static int32_t InsUT_Ambalink_StartBt(int32_t argc, char **argv)
{
    uint32_t mode = 0;
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    if ((argc != 1 || NULL == argv))
    {
        return INSUT_UNITTEST_USAGE;
    }
    mode = atoi(argv[0]);
    InsProto_Rtos_Message_StartBT(INSMW_RPMSG_CHANNEL_ID_COMM, mode);
    return 0;
}

static int32_t InsUT_Ambalink_StopBt(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsProto_Rtos_Message_StopBT(INSMW_RPMSG_CHANNEL_ID_COMM);
    return 0;
}

static int32_t InsUT_Ambalink_BtWakeup(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsProto_Rtos_Message_ConfigureWakeup(INSMW_RPMSG_CHANNEL_ID_COMM);
    return 0;
}

#define INSUT_CMDCNT_AMBALINK_DEBUG      (10)
InsUT_Unittest_CmdInfo_t g_ut_cmd_ambalink_debug[INSUT_CMDCNT_AMBALINK_DEBUG] =
{
    {InsUT_Ambalink_LaunchModule,                0,  NULL,   "launch_module",    "No Param"},
    {InsUT_Ambalink_ExitModule,                  0,  NULL,   "exit_module",       "No Param"},
    {InsUT_Ambalink_Open_Wifi,                   0,  NULL,   "open_wifi",         "No Param"},
    {InsUT_Ambalink_Close_Wifi,                  0,  NULL,   "close_wifi",        "No Param"},
    {InsUT_Ambalink_Wifi_SetFreq,                0,  NULL,   "wifi_setfreq",      "[0|1]"},
    {InsUT_Ambalink_Rpmsg_Test,                  0,  NULL,   "rpmsg_test",        "No Param"},
    {InsUT_Ambalink_Start_RpcServer,             0,  NULL,   "start_rpc",         "No Param"},
    {InsUT_Ambalink_StartBt,                     0,  NULL,   "start_bt",          "No Param"},
    {InsUT_Ambalink_StopBt,                      0,  NULL,   "stop_bt",           "No Param"},
    {InsUT_Ambalink_BtWakeup,                    0,  NULL,   "bt_wakeup",         "No Param"},
    //{InsUT_GUI_DebugToast,            0,  NULL,   "toast",        "[text]"},
};

InsUT_Unittest_CmdInfo_t g_ut_cmd_ambalink[INSUT_CMDCNT_AMBALINK] =
{
    {NULL,  INSUT_CMDCNT_AMBALINK_DEBUG,     g_ut_cmd_ambalink_debug,     "debug",        "Ambalink Debug"},
};

