#include "oscar.h"
#include "elog.h"
#include "FreeRTOS.h"
#include "cmsis_os.h"
#include "usart.h"

/******           Port             ***************/
#if 1
My_Oscar_TypeDef myoscar =
    {
        Oscar_Init,
        Oscar_Port,
        Oscar_Set_State,
        // SEL_232_485_GPIO_Port,
        // HDPLX_GPIO_Port,
        // DE485_GPIO_Port,
        // SEL_232_485_Pin,
        // HDPLX_Pin,
        // DE485_Pin,
        NULL,
        NULL,
        NULL,
        0,
        0,
        0,
        Uart_Interface_Default, /* 接口类型 0: common, 1: 485 ,2:3160*/
        Uart_RSMODE_232,        /* mode_232_485 0 commome  1:485*/
        Uart_Trans_DMA,         /* trans_type0 :polling, 1: IT  2: DMA*/
        Uart_Trans_DMA,         /* rcv_type 0 :polling, 1: IT  2: DMA*/
        Uart_IDLE_IT_ENABLE,    /* idle_enable_disable 0 :不启用空闲 , 1: 启用空闲*/
       NULL, /* pointer huart*/
};

UART_HandleTypeDef *pOscarUart = &huart2;
#define Oscar_Uart_IRQHandler USART2_IRQHandler

extern volatile uint8_t measure_flag;
Oscar_TypeDef *oscar;

/*
    01 03 0B 18 00 05 07 EA    (SN  2840 5  demo)
    01 03 00 0A 00 05 A5 CB    (SN  10 5   实际)
*/
#if Oscar_DEMO
static uint8_t Oscar_SN_command[8] = {0x01, 0x03, 0x0B, 0x18, 0x00, 0x05, 0x07, 0xEA};
#else
uint8_t Oscar_SN_command[8] = {0x01, 0x03, 0x00, 0x0A, 0x00, 0x05, 0xA5, 0xCB};
#endif

static uint8_t Oscar_Wavelength_sendbuf[2][8] = {
    /*
    01 03 08 36 00 7A 26 47    (2102  122)
    01 03 08 B0 00 7A C7 AE    (2224  122)
    */
    {0x01, 0x03, 0x08, 0x36, 0x00, 0x7A, 0x26, 0x47},
    {0x01, 0x03, 0x08, 0xB0, 0x00, 0x7A, 0xC7, 0xAE}};
static uint8_t Oscar_Data_sendbuf[2][8] = {
    /*
    01 03 0A 36 00 7A 27 FF    (2614  122)
    01 03 0A B0 00 7A C6 16    (2736  122)
    */
    {0x01, 0x03, 0x0A, 0x36, 0x00, 0x7A, 0x27, 0xFF},
    {0x01, 0x03, 0x0A, 0xB0, 0x00, 0x7A, 0xC6, 0x16}};
uint8_t Oscar_sn_buf[15] = {0};
// uint8_t Oscar_rcvbuf[2][Oscar_Rcv_Buf_Size] = {0};

uint8_t Oscar_Rcv_Buf[2 * Oscar_Rcv_Buf_Size] = {0};   // 一次接收 数据量 Oscar_Rcv_Buf_Size，翻倍*2， 多次

osThreadId_t oscarHandle;
const osThreadAttr_t Oscar_attributes = {
    .name = "oscar",
    .stack_size = 1024,
    .priority = (osPriority_t)osPriorityBelowNormal,
};

void Oscar_Uart_IRQHandler(void)
{
    uint16_t count = 0;
    uint16_t data_length = 0;
    if (__HAL_UART_GET_FLAG(pOscarUart, UART_FLAG_TC) != RESET)
    {
        log_i("Oscar_.... TC IT ...");
        // oscar->uarthelper ->status = Uart_Status_Send_Cplt ;
        // Oscar_Set_UartHelper_Status( oscar, Uart_Status_Send_Cplt);
        Oscar_Set_GPIO_For_TC_Cplt(oscar);

        oscar->uarthelper->huart->gState = HAL_UART_STATE_READY;
        // pOscarUart->gState = HAL_UART_STATE_READY;
        // UartHelper_Trans_Cplt_Callback(  oscar->uarthelper  );
        //  log_i("Send Oscar_Uart_IRQHandler->  interface_type %d,  232-485  %d ,  sel: %d ,de485  %d  , duplx %d "
        //     ,oscar->uarthelper->interface->interface_type
        //     ,oscar->uarthelper->interface->mode_232_485
        //     ,HAL_GPIO_ReadPin(oscar->uarthelper->interface->sel_gpio,oscar->uarthelper->interface->sel_pin)
        //     ,HAL_GPIO_ReadPin(oscar->uarthelper->interface->de485_gpio,oscar->uarthelper->interface->de485_pin)
        //     ,HAL_GPIO_ReadPin(oscar->uarthelper->interface->dplx_gpio,oscar->uarthelper->interface->dplx_pin)
        //     );
        // oscar->status = Uart_Status_Send_Cplt;
        // huart3.gState = HAL_UART_STATE_READY;
        __HAL_UART_CLEAR_FLAG(oscar->uarthelper->huart, UART_FLAG_TC);
    }

    if (__HAL_UART_GET_FLAG(pOscarUart, UART_FLAG_IDLE) != RESET )
    {
        log_i("Oscar_....  IDLE  IT ....  ");
        // TODO ...处理空闲 ，最后一位  datalength-1

        count = __HAL_DMA_GET_COUNTER(oscar->uarthelper->huart->hdmarx);
        data_length = 2 * Oscar_Rcv_Buf_Size - count;
        if (data_length > 0)
        {
            log_i("Oscar_. IDLE..data_length  %d",   data_length);
            Oscar_copy(oscar, data_length);
            // log_i("Oscar_. IDLE..res  %02X %d %02X",  oscar->uarthelper ->receive_buf[0],  oscar->uarthelper ->receive_buf[2],  oscar->uarthelper ->receive_buf[data_length-1]);
        }
 
        __HAL_UART_CLEAR_IDLEFLAG(pOscarUart);
    }

    if (__HAL_UART_GET_FLAG(pOscarUart, UART_FLAG_ORE) != RESET)
    {
        log_i("Oscar_....ORE");

        __HAL_UART_CLEAR_FLAG(pOscarUart, UART_FLAG_ORE);
    }
    if (__HAL_UART_GET_FLAG(pOscarUart, UART_FLAG_FE) != RESET)
    {
        log_i("Oscar_....FE");

        __HAL_UART_CLEAR_FLAG(pOscarUart, UART_FLAG_FE);
    }
    if (__HAL_UART_GET_FLAG(pOscarUart, UART_FLAG_PE) != RESET)
    {
        log_i("Oscar_....PE");

        __HAL_UART_CLEAR_FLAG(pOscarUart, UART_FLAG_PE);
    }
    HAL_UART_IRQHandler(pOscarUart);
}

int Oscar_copy(Oscar_TypeDef *oscar ,uint16_t size)
{
    log_i( "Oscar_ idle copy  state %d   size :%d   ", oscar->state,size);
    if ( oscar->state == MODULE_Oscar_STATE_CHECK_SN  )
    {
        memcpy(oscar->sn_buf, oscar->uarthelper->receive_buf, size);
        oscar->size_received = oscar->size_received + size;
        return 0;
    }
    if ( oscar->state == MODULE_Oscar_STATE_GET_WL_0  )
    {
        memcpy(oscar->Oscar_Wavelength_Buf[0], oscar->uarthelper->receive_buf, size);
        oscar->size_received = oscar->size_received + size;
        return 0;
    }
    if (  oscar->state == MODULE_Oscar_STATE_GET_WL_1)
    {
        memcpy(oscar->Oscar_Wavelength_Buf[1], oscar->uarthelper->receive_buf, size);
        oscar->size_received = oscar->size_received + size;
        return 0;
    }
    if ( oscar->state == MODULE_Oscar_STATE_GET_DATA_0   )
    {
        memcpy(oscar->Oscar_Data_Buf[0], oscar->uarthelper->receive_buf, size);
        oscar->size_received = oscar->size_received + size;
        return 0;
    }
    if (  oscar->state == MODULE_Oscar_STATE_GET_DATA_1)
    {
        memcpy(oscar->Oscar_Data_Buf[1], oscar->uarthelper->receive_buf, size);
        oscar->size_received = oscar->size_received + size;
        return 0;
    }
 
    return 0;
}

void Oscar_Set_State( MODULE_Oscar_WORK_STATE state  )
{
    // log_i( "Set_State...oscar->state .%d ", oscar->state);
      oscar->state = state;
}

uint8_t Oscar_Init()
{

    oscar = (Oscar_TypeDef *)malloc(sizeof(Oscar_TypeDef));

    UartInterface_TypeDef *interface    = UartInterface_Init( );
    UartHelper_TypeDef *uarthelper      = UartHelper_Init();

    Oscar_Set_Uarthelper( oscar,  uarthelper );
    Oscar_Set_Interface( oscar,  interface );
    Oscar_Set_Huart( oscar, pOscarUart );
 
    /* set_GPIO  UartInterface */
    UartInterface_Set_Sel_GPIO_Pin( oscar->uarthelper->interface, myoscar.sel_gpio, myoscar.sel_pin );
    UartInterface_Set_Dplx_GPIO_Pin( oscar->uarthelper->interface, myoscar.dplx_gpio, myoscar.dplx_pin );
    UartInterface_Set_DE485_GPIO_Pin( oscar->uarthelper->interface, myoscar.de_gpio, myoscar.de_pin );

    /* interface type */
    Oscar_Set_Interface_Type( oscar, myoscar.interface_type );
    Oscar_Set_RsMode_232_485 ( oscar, myoscar.mode_232_485 ); //  max3160 的232  485选择
    Oscar_Set_Trans_Type( oscar, myoscar.trans_type ) ;
    Oscar_Set_Rcv_Type ( oscar, myoscar.rcv_type ) ;
    Oscar_Set_Idle_Enable (oscar, myoscar.idle_enable_disable ) ;
    // UartHelper_Setup_Interface_type( oscar ,myoscar.interface_type); 
 
    //  check  
    if ( oscar->uarthelper->huart == pOscarUart ){
        log_i ( "oscar set huart ok. trans_type -> %d  rcv_type -> %d  ( 0 :polling, 1: IT  2: DMA) ",oscar->trans_type ,oscar->rcv_type ) ;
        log_i( " interface->   %d  %d (0:default, 1:485, 2:3160) " ,oscar->uarthelper->interface_type  ,oscar->uarthelper->interface->interface_type );  
        log_i( "oscar 232_485 ->   %d  %d (0:232 ,1: 485)   " ,oscar->uarthelper->mode_232_485, oscar->uarthelper->interface->mode_232_485  );  
    }else{
        log_e ( "oscar set huart failure "  ) ;
    }
        
    // oscar send rcv buf 是动态的
    Oscar_Set_Timeout( oscar, 1000 );
    // Oscar_Set_Sendbuf( oscar,  Oscar_SND_Buf, 8 );
    // Oscar_Set_Rcvbuf(  oscar,  Oscar_RCV_Buf, sizeof(Oscar_RCV_Buf) );

    // set paras;
    oscar->timebase        = 0;
    // oscar->timeout_ms    = 1000;
    oscar->send_status     = Uart_Status_Ready;
    oscar->send_flag       = 0;
    oscar->command_seq     = 4;
    // oscar->mode            = 2;
    // oscar->data_begin_flag = 0;
    oscar->state           = MODULE_Oscar_STATE_WAIT;
    return 0;
}

void Oscar_Port()
{
    oscarHandle = osThreadNew(Oscar_Task, (void*)(&oscar->state), &Oscar_attributes);
}

void Oscar_Task(void* argument)
{
    // log_i( " viper tran mode : %d ", oscar->->trans_mode );
    memset( oscar->Oscar_Data_Buf, 0, Oscar_Rcv_Buf_Size * 2 ); /* viper*3 viper *2 否则内存泄漏 */
    static int err_con=0;
    MODULE_Oscar_WORK_STATE *state = argument;
    // FLAG   发送后进入 busy,  接收sn后就rcved_flag
    for (;;)
    {
        // log_i( " Oscar_ state   %d   " , *state );
        switch (*state)
        {
        case MODULE_Oscar_STATE_WAIT:
 
            break;
        case MODULE_Oscar_STATE_START:
            // log_i( " Oscar_Task  1 state   %d   " , *state );
                oscar->timebase = osKernelGetTickCount();
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                UartHelper_Set_rcv_buf(oscar->uarthelper , Oscar_Rcv_Buf, sizeof(Oscar_Rcv_Buf));
                oscar->flag |= Oscar_FLAG_MASK_BUSY;
                *state=MODULE_Oscar_STATE_GET_SN; 
            break; 
        case MODULE_Oscar_STATE_GET_SN:
            // log_i( " Oscar_Task  2 state   %d   " , *state );
                // oscar->flag |=Oscar_FLAG_MASK_RECVED_SN;
                oscar->timebase = osKernelGetTickCount();
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
                UartHelper_Set_Trans_Buf(oscar->uarthelper , Oscar_SN_command, sizeof(Oscar_SN_command));
                Oscar_Begin_Rcv(oscar, oscar->uarthelper->receive_buf, 2 * oscar->uarthelper ->receive_buf_half_len);
                Oscar_Transmit(oscar, oscar->uarthelper ->trans_buf, oscar->uarthelper ->trans_size);
                *state=MODULE_Oscar_STATE_CHECK_SN; 
            break; 
        case MODULE_Oscar_STATE_CHECK_SN:
            // log_i( " Oscar_Task  3 state   %d   sn %d " , *state, oscar->sn );
            if ( oscar->size_received == sizeof(oscar->sn_buf) && (oscar->flag &Oscar_FLAG_MASK_RECVED_SN !=0) )
            {
                oscar->flag |= Oscar_FLAG_MASK_RECVED_SN;  // 记录获得SN
                Oscar_clean_flag( oscar ,Oscar_FLAG_MASK_RECVED_WL|Oscar_FLAG_MASK_RECVED_DATA);
                oscar->error =0;
                err_con=0;
                oscar->sn = oscar->sn_buf[11]*256 + oscar->sn_buf[12];
                HAL_UART_DMAStop(oscar->uarthelper ->huart );
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
                UartHelper_Set_Trans_Buf(oscar->uarthelper , Oscar_Wavelength_sendbuf[0], sizeof(Oscar_SN_command));
                Oscar_Begin_Rcv(oscar, oscar->uarthelper ->receive_buf, 2 * oscar->uarthelper ->receive_buf_half_len);
                Oscar_Transmit(oscar, oscar->uarthelper->trans_buf, oscar->uarthelper ->trans_size);
                *state=MODULE_Oscar_STATE_GET_WL_0;
            } 
            if( (osKernelGetTickCount()- oscar->timebase) >=1000){
                log_w("Oscar_ Get SN Failed %d,Retrying...",err_con);
                err_con++;
                if(err_con>10){
                    log_e("Oscar_ Init Error");
                    // operating_state.plug3_state=0;   //TODO?
                    oscar->error = 1;
                    *state=MODULE_Oscar_STATE_ERROR;
                    break;
                }
                oscar->timebase = osKernelGetTickCount();
                // Oscar_clean_flag( oscar ,Oscar_FLAG_MASK_BUSY);
                Oscar_Transmit(oscar, Oscar_SN_command,8);
                *state=MODULE_Oscar_STATE_GET_SN;
            }
            break;    
        case MODULE_Oscar_STATE_GET_WL_0:
            // log_i( " Oscar_Task  4 state   %d  size_received  %d " , *state ,oscar->size_received);
            if ( oscar->size_received == Oscar_Rcv_Buf_Size  && (oscar->flag &Oscar_FLAG_MASK_RECVED_WL !=0))
            {
                // log_i( "Oscar_ WL0 soscar->size_received    %d      " ,oscar->size_received  );
                oscar->error =0;
                err_con=0;
                HAL_UART_DMAStop(oscar->uarthelper ->huart );
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
                UartHelper_Set_Trans_Buf(oscar->uarthelper , Oscar_Wavelength_sendbuf[1], sizeof(Oscar_SN_command));
                Oscar_Begin_Rcv(oscar, oscar->uarthelper ->receive_buf, 2 * oscar->uarthelper ->receive_buf_half_len);
                Oscar_Transmit(oscar, oscar->uarthelper->trans_buf, oscar->uarthelper ->trans_size);
                *state=MODULE_Oscar_STATE_GET_WL_1;               
            } 
            if( (osKernelGetTickCount()- oscar->timebase) >=2000){
                log_w("Oscar_ Get WL0 Failed %d,Retrying...",err_con);
                *state=MODULE_Oscar_STATE_ERROR;
                break;
            }            
            break;
        case MODULE_Oscar_STATE_GET_WL_1:
            // log_i( " Oscar_Task  5 state   %d  size_received  %d " , *state ,oscar->size_received);
            if ( oscar->size_received == Oscar_Rcv_Buf_Size  && (oscar->flag &Oscar_FLAG_MASK_RECVED_WL !=0) )
            {
                // log_i( "Oscar_ WL1 oscar->size_received    %d      " ,oscar->size_received  );
                oscar->error =0;
                err_con=0;
                HAL_UART_DMAStop(oscar->uarthelper ->huart );
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
                *state=MODULE_Oscar_STATE_CHECK_WL;               
            } 
            if( (osKernelGetTickCount()- oscar->timebase) >=2000){
                log_w("Oscar_ Get WL0 Failed %d,Retrying...",err_con);
                *state=MODULE_Oscar_STATE_ERROR;
                break;
            }  
        case MODULE_Oscar_STATE_CHECK_WL:
            //  TODO 对获得波长数据进行校验 ,不状态跳转了
            // log_i( " Oscar_  "  );
            if (1==1)
            {
                log_i( " Oscar_  GET WL OK"  );
                oscar->flag |= Oscar_FLAG_MASK_RECVED_WL;
                // Oscar_clean_flag( oscar, Oscar_FLAG_MASK_BUSY );
                oscar->size_received = 0;
 
                            *state=MODULE_Oscar_STATE_Ready_GET_DATA;
            }
            break;
        case MODULE_Oscar_STATE_Ready_GET_DATA:
            // 外部直接调用这个状态进行测量
            log_i( " Oscar_Task  SN  WL  OK, wait.... "  );
            osDelay(5);
 
            break;
        case MODULE_Oscar_STATE_START_DATA:
            // 外部直接调用这个状态进行测量
            log_w("Oscar_ Get MODULE_Oscar_STATE_START_DATA ...  " );
                oscar->flag |= Oscar_FLAG_MASK_BUSY;
                Oscar_clean_flag( oscar ,Oscar_FLAG_MASK_RECVED_DATA);
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
                UartHelper_Set_Trans_Buf(oscar->uarthelper , Oscar_Data_sendbuf[0], sizeof(Oscar_SN_command));
                Oscar_Begin_Rcv(oscar, oscar->uarthelper ->receive_buf, 2 * oscar->uarthelper ->receive_buf_half_len);
                Oscar_Transmit(oscar, oscar->uarthelper->trans_buf, oscar->uarthelper ->trans_size);
            *state=MODULE_Oscar_STATE_GET_DATA_0;
            break;
 
        case MODULE_Oscar_STATE_GET_DATA_0:
            if ( oscar->size_received == Oscar_Rcv_Buf_Size  && (oscar->flag &Oscar_FLAG_MASK_RECVED_DATA !=0))
            {
                // log_i( "Oscar_ DATA0  oscar->size_received    %d      " ,oscar->size_received  );
                oscar->error =0;
                err_con=0;
                HAL_UART_DMAStop(oscar->uarthelper ->huart );
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
                UartHelper_Set_Trans_Buf(oscar->uarthelper , Oscar_Data_sendbuf[1], sizeof(Oscar_SN_command));
                Oscar_Begin_Rcv(oscar, oscar->uarthelper ->receive_buf, 2 * oscar->uarthelper ->receive_buf_half_len);
                Oscar_Transmit(oscar, oscar->uarthelper->trans_buf, oscar->uarthelper ->trans_size);
                *state = MODULE_Oscar_STATE_GET_DATA_1;               
            } 
            if( (osKernelGetTickCount()- oscar->timebase) >=2000){
                log_w("Oscar_ Get DATA0 Failed %d,Retrying...",err_con);
                *state=MODULE_Oscar_STATE_ERROR;
                break;
            }            
            break;
        case MODULE_Oscar_STATE_GET_DATA_1:
            log_i( " Oscar_Task  5 state   %d  size_received  %d " , *state ,oscar->size_received);
            if ( oscar->size_received == Oscar_Rcv_Buf_Size && (oscar->flag &Oscar_FLAG_MASK_RECVED_DATA !=0) )
            {
                // log_i( "Oscar_ Get DATA OK    %d      " ,oscar->size_received  );
                oscar->error =0;
                err_con=0;
                oscar->sn = oscar->sn_buf[11]*256 + oscar->sn_buf[12];
                HAL_UART_DMAStop(oscar->uarthelper ->huart );
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
                 *state=MODULE_Oscar_STATE_CHECK_DATA;            
            } 
            if( (osKernelGetTickCount()- oscar->timebase) >=2000){
                log_w("Oscar_ Get DATA1 Failed %d,Retrying...",err_con);
                *state=MODULE_Oscar_STATE_ERROR;
                break;
            }  
        case MODULE_Oscar_STATE_CHECK_DATA:
 
            if (1==1)
            {
                log_i("Oscar_ Get DATA OK " );
                oscar->flag |= Oscar_FLAG_MASK_RECVED_DATA;
                *state=    MODULE_Oscar_STATE_OK;  
            }else{
                err_con++;
                log_w("Oscar_ Get DATA Failed %d,Retrying...",err_con);
                if( err_con>3){
                *state=    MODULE_Oscar_STATE_ERROR;                

                }
                *state=    MODULE_Oscar_STATE_START_DATA;  
            }
            break;
        case MODULE_Oscar_STATE_OK:
            log_w("Oscar_ MODULE_Oscar_STATE_OK " );
                oscar->size_received = 0;
            break;
        case MODULE_Oscar_STATE_ERROR:
                Oscar_clean_flag( oscar ,Oscar_FLAG_MASK_BUSY);
                HAL_UART_DMAStop(oscar->uarthelper ->huart );
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
            break;   
        case MODULE_Oscar_STATE_STOP:
                Oscar_clean_flag( oscar ,Oscar_FLAG_MASK_BUSY);
                HAL_UART_DMAStop(oscar->uarthelper ->huart );
                memset(Oscar_Rcv_Buf, 0, sizeof(Oscar_Rcv_Buf));
                oscar->size_received = 0;
            break;
        default:
            Oscar_clean_flag( oscar ,Oscar_FLAG_MASK_BUSY);
            *state=MODULE_Oscar_STATE_WAIT;
            break;
        }

        osDelay(300);
    }
}

/******************         接口           *******************/
void Oscar_Set_Uarthelper(Oscar_TypeDef *oscar, UartHelper_TypeDef *uarthelper)
{
    oscar->uarthelper  = uarthelper;
}

void Oscar_Set_Interface(Oscar_TypeDef *oscar, UartInterface_TypeDef *interface)
{
    oscar->uarthelper->interface = interface;
}
void Oscar_Set_Huart( Oscar_TypeDef * oscar,  UART_HandleTypeDef * huart  )
{
    oscar->uarthelper->huart = huart;
}

void Oscar_Set_Interface_Type( Oscar_TypeDef *oscar,    Uart_Interface_Type_Typedef interface_type  )
{
    oscar->interface_type = interface_type;  
    oscar->uarthelper->interface_type = interface_type; 
    UartInterface_Setup_Interface_type( oscar->uarthelper->interface ,oscar->interface_type); 
}

void Oscar_Set_RsMode_232_485(Oscar_TypeDef *oscar, Uart_RS_Mode_TypeDef rs_232_485)
{
    //  TODO理清逻辑
    oscar->mode_232_485 = rs_232_485; 
    oscar->uarthelper->mode_232_485 = rs_232_485; 
       UartInterface_Setup_Mode_232_485( oscar->uarthelper->interface, rs_232_485 );
}
void Oscar_Set_Trans_Type( Oscar_TypeDef *oscar,  Uart_Transmode_TypeDef trans_type  )
{
    oscar->trans_type = trans_type; 
}
void Oscar_Set_Rcv_Type( Oscar_TypeDef *oscar,  Uart_Transmode_TypeDef rcv_type  )
{
    oscar->rcv_type = rcv_type; 
    
}
void Oscar_Set_Idle_Enable( Oscar_TypeDef *oscar,  Uart_IDLE_Enable_TypeDef idle_enable_disable  )
{
    oscar->idle_enable_disable = idle_enable_disable; 

}
void Oscar_Set_TransMode( Oscar_TypeDef *oscar,  Uart_Transmode_TypeDef trans_mode  )
{
    oscar->trans_type = trans_mode; 
    oscar->rcv_type = trans_mode; 

}
 
void Oscar_Set_Timeout( Oscar_TypeDef *oscar,    uint16_t timeout_ms )
{
    oscar->timeout_ms = timeout_ms; 
    oscar->timeout_enable = 1;

}
void Oscar_Set_Sendbuf( Oscar_TypeDef *oscar,  uint8_t * buf, uint16_t size )
{
 
    oscar->send_buf = buf;
    oscar->size_send = size;
}
void Oscar_Set_Rcvbuf( Oscar_TypeDef *oscar,  uint8_t * buf, uint16_t size )
{
    oscar->rcv_buf = buf;
    oscar->size_rcv = size;

}

int Oscar_Get_Timeout(Oscar_TypeDef *oscar)
{
    if ((osKernelGetTickCount() - oscar->timebase) > oscar->timeout_ms)
        return 1;
    return 0;
}

int Oscar_Transmit(Oscar_TypeDef *oscar, uint8_t *buf, uint16_t size)
{

    if (oscar->timeout_enable == 1)
    {
        oscar->timebase = osKernelGetTickCount();
    }
    return UartHelper_Transmit(oscar->uarthelper , buf, size);
}

int Oscar_Begin_Rcv(Oscar_TypeDef *oscar, uint8_t *buf, uint16_t size)
{
    return UartHelper_Begin_Rcv(oscar->uarthelper , buf, size);
}

Uart_Status_TypeDef Oscar_Get_UartHelper_Status(Oscar_TypeDef *oscar)
{
    return oscar->uarthelper ->status;
}
int Oscar_Set_UartHelper_Status(Oscar_TypeDef *oscar, Uart_Status_TypeDef status)
{
    oscar->uarthelper ->status = status;
    return 0;
}

int Oscar_Init_Ok()
{
    // uint64_t timebase = osKernelGetTickCount( );
    // while (1)
    // {
    //     log_i(  " flag :  %d  %d ",oscar->flag , (oscar->flag & Oscar_FLAG_MASK_RECVED_WL));
    //     if (count > 50)  break;
    //     if (  oscar->flag & Oscar_FLAG_MASK_RECVED_WL != 0   ) return 0;
    //     osDelay ( 20 );
    //     count++;
    // }
    // return -1;
    
}

int Oscar_Get_Data_OK(Oscar_TypeDef *oscar)
{
    return oscar->data_ok;
}
void Oscar_Set_Send_Flag(Oscar_TypeDef *oscar)
{
    // vPortEnterCritical();
    oscar->send_flag = 1;
    oscar->command_seq = 0;
    oscar->data_ok = 0;
    // vPortExitCritical( );
}


void Oscar_callback(Oscar_TypeDef *oscar, uint8_t *buf, uint16_t size)
{
}

void Oscar_Set_GPIO_For_TC_Cplt(Oscar_TypeDef *oscar)
{
    // oscar->status = 2;
    UartHelper_Set_GPIO_For_Trans_Cplt(oscar->uarthelper );
}


void Oscar_clean_flag(Oscar_TypeDef *oscar,uint8_t mask)
{
    oscar->flag&=~mask;
}
 
uint8_t Oscar_get_flag(Oscar_TypeDef *oscar,uint8_t mask)
{
    return oscar->flag&mask;
}

uint16_t Oscar_get_serial_number(Oscar_TypeDef * oscar )
{
    if((oscar->flag&Oscar_FLAG_MASK_RECVED_SN)==0){
        return 0xffff;
    }
    return oscar->sn;
}


void Oscar_Error(Oscar_TypeDef *oscar, char *str)
{
    log_e(str);
    while (1)
        ;
}


#endif
