/**
 * @file hv_mw_UsbTask.h
 * @brief Header file of USB task module.
 * 
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 * (+) Use Hv_Mw_UsbTaskHandle(...) s a task function handle. 
 * 
 * @endverbatim
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-10
 */
#ifndef __SDK_MIDDLEWARE_RUNTIME_USBTASK_INC_USBTASK_H__
#define __SDK_MIDDLEWARE_RUNTIME_USBTASK_INC_USBTASK_H__

#include "hv_comm_DataType.h"
#include "hv_comm_Event.h"
#include "hv_vos_Comm.h"
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"

/**
 * @brief Defines the state of USB, which can be used for state machine operation and also for event notification with other modules.
 */
typedef enum _UsbState
{
    USB_STATE_IDLE                      = 0x01,  /*!< 空闲态，无实际作用 */
    USB_STATE_NO_UDISK_IN               = 0x02,  /*!< 端口检查结果：无设备接入 */
    USB_STATE_PLUGGED_IN                = 0x03,  /*!< 端口检查结果：有设备接入 */
    USB_STATE_NO_OTA_FILE               = 0x04,  /*!< OTA目标文件查找结果：未找到文件 */
    USB_STATE_FOUND_OTA_FILE            = 0x05,  /*!< OTA目标文件查找结果：命中 */
    USB_STATE_OTA_IN_PROCESSING         = 0x08,  /*!< OTA运行中 */
    USB_STATE_OTA_SUCCESS               = 0x09,  /*!< OTA结果：成功 */
    USB_STATE_OTA_FAIL                  = 0x0A,  /*!< OTA结果：失败 */
    USB_STATE_ANY                       = 0xFF,  /*!< 任意态，用于不限制状态机触发的初始状态时 */
} UsbState;


typedef enum _UsbWorkMode
{
    USB_MODE_HOST,
    USB_MODE_DEVICE,
    USB_MODE_MAX,
} UsbWorkMode;


/**
 * @brief OTA State Machine 
 */
typedef struct _UsbStateMachine
{
    USHORT16         usAcceptStates;
    UsbEventId       enEventId;
    Status           (*pfActionHandle)(UsbEvent *pstEvent);
}UsbStateMachine;


#define USB_NORMAL_TASK_PRIORITY      2 
#define USB_URGENT_TASK_PRIORITY      8

/**
 * @brief Set USB state.
 * @param[in] enState enum value of UsbOtaState
 * @return none
 */
VOID Hv_Mw_USB_SetUsbState(UsbState enState);

/**
 * @brief Get USB state.
 * @return enum value of UsbState.
 */
UsbState Hv_Mw_USB_GetUsbState(VOID);

/**
 * @brief Send event to usb module.
 * @return VOID
 */
VOID Hv_Mw_SendEvtToUsb(UsbEventId enEvtID, USHORT16 usValue);

/**
 * @brief USB task function handle.
 * @param[in] arg unused.
 * @return VOID
 */
VOID Hv_Mw_UsbTaskHandle(  VOID *arg  );

VOID Hv_Mw_UsbSetWorkMode(UsbWorkMode enMode);
UsbWorkMode Hv_Mw_UsbGetWorkMode(VOID);

VOID Hv_Drv_USB_SetPriority(UCHAR8 ucPriority);
VOID Hv_Drv_USB_SetTaskHandler(TaskHandle_t stTaskHdl);


#ifdef CONFIG_USER_USB_OTA_ON
#include "hv_drv_UsbFatfs.h"
#include "hv_drv_UsbFatfsDiskIo.h"

typedef enum _UsbOtaMode
{
    OTA_MODE_NORMAL                     = 0x01,
    OTA_MODE_FORCE                      = 0x02,
} UsbOtaMode;

/**
 * @brief Usb Host task init.
 * @return VOID
 */
VOID Hv_Mw_UsbHost_TaskStartInit(VOID);

/**
 * @brief Stop Usb Host task.
 * @return VOID
 */
VOID Hv_Mw_UsbHost_TaskStop(VOID);

Status Hv_Mw_USB_StartOta(UCHAR8 ucArgC, UCHAR8* cArgV[], UsbOtaMode enMode);
#endif


#ifdef CONFIG_USER_USB_BILLBOARD_ON
VOID Hv_Mw_UsbBillboard_TaskStartInit(VOID);
VOID Hv_Mw_UsbBillboard_TaskStop(VOID);
#endif

#endif
