#include "Check_Ctrl.h"
#include "TYW_stdint.h"
#include "RTE_GPIO.h"
#include "UDS_ISO14229_Server.h"
#include "string.h"
#include "Display_Info.h"
#include "Analog_Signals.h"
#include "CAN_Communication_Matrix.h"
#include "COM_CAN.h"
#include "GenDelay.h"
#include "TFT_LCD.h"
#include "app_lsdn.h"

uint32_t NO_IPK_EXIST_Status;
uint32_t NO_IPK_EXIST;
//uint32_t NO_IPK_EXIST_Back;
uint32_t NO_IPK_EXIST_TIMER;
const unsigned char gImage_cuo12[] = {24, 24,0X00,0X00,0X00,0X00,0X30,0X70,0XE0,0XC0,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0X70,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X83,0XC7,0X66,0X38,0X38,0X7C,0XE6,0XC3,0X81,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0C,0X0C,0X06,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X07,0X0E,0X0C,0X00,0X00,0X00,0X00,};
const unsigned char gImage_dui12[] = {24, 24,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0XE0,0X40,0X00,0X00,0X00,0X00,0X08,0X18,0X30,0X70,0XC0,0X80,0X00,0X00,0X80,0XC0,0XE0,0X70,0X38,0X1C,0X0E,0X07,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X07,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,};
uint8_t InterNal_Version [ 128 ];
uint8_t SW_Version [ 128 ];
uint8_t HW_Version [ 128 ];
uint8_t Part_Version [ 128 ];
uint8_t Risk_Evade [ 128 ];
uint8_t Evade_ODO  [ 128 ];
uint8_t Line_IN_St [ 128 ];
uint8_t Res_St [ 128 ];
uint8_t Made_Date [ 128 ];//制造日期
uint8_t SUPPLIER [ 128 ];//供应商代码
uint8_t Voltage_UDS [ 128 ];
uint8_t BCM_2014 [ 128 ];
//uint8_t InterNal_Version [ 128 ];
uint32_t LCD_CHECN_BEGIN;
uint32_t LCD_CHECN_Retry;

uint32_t LINE_IN_CHECK_BEGIN;
uint32_t LINE_IN_CHECK_Retry;

uint32_t TFT_CHECN_BEGIN;
uint32_t TFT_CHECN_Retry;

uint32_t VOICE_BACK_BEGIN;
uint32_t VOICE_BACK_Retry;

uint32_t VOICE_CHECK_BEGIN;
uint32_t VOICE_CHECK_Retry;

uint32_t VOICE_CHECN_BEGIN;
uint32_t VOICE_CHECN_Retry;

uint32_t LINE_ODO_CLEAR_BEGIN;
uint32_t VOICE_ODO_CLEAR_Retry;

uint32_t RES_CHECK_BEGIN;
uint32_t RES_CHECK_Retry;

uint32_t UnLock_Flag = 0;
uint32_t UnLock_Retry;

uint8_t  KEYBUFFER [ 4 ];

uint32_t CAN_Msg_Tx_Enable;


uint32_t MENU_CHECK_STEP;

uint32_t Global_Session = 0;
uint32_t loc_Step;


void Function_Check_Ctrl(uint32_t cmd);

void MENU_CHECK_STEP_ADD(void)
{
    if(step_check == 2)
    {
        step_check = 0;
        MENU_CHECK_STEP++;
        if((MENU_CHECK_STEP == 3) || (MENU_CHECK_STEP == 5))
        {
            step_check = 1;
        }
    }
    
    if(MENU_CHECK_STEP > 9)
    {
        MENU_CHECK_STEP = 1;
    }
}
void MENU_CHECK_STEP_SUB(void)
{
    if ( MENU_CHECK_STEP != 0 )
    {
        MENU_CHECK_STEP--;
    }
        
  
    
}

void MENU_CHECK_Init(void)
{
    MENU_CHECK_STEP = 0;
}



uint8_t Get_DID_High_Byte(uint16_t Input_DID)
{
    uint16_t Res = 0;

    Res = (Input_DID >> 8);

    return  (uint8_t)(Res);
}

uint8_t Get_DID_Low_Byte(uint16_t Input_DID)
{
    uint16_t Res = 0;

    Res = (Input_DID & 0xFF);

    return  (uint8_t)(Res);
}







void HW_CHECK_IPK_EXIST(void)
{

    uint8_t DIAG_BUFER [ 2 ];

    DIAG_BUFER [ 0 ] = 0x3E;
    DIAG_BUFER [ 1 ] = 0x00;
    UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 2);
    
    SET_IPK_NONE( );
}

void SET_IPK_NONE(void)
{
    NO_IPK_EXIST_TIMER = 12;
    NO_IPK_EXIST       = 12345678;
}
void Clear_IPK_NONE(void)   //CAN在线
{
    NO_IPK_EXIST        = 0;
    NO_IPK_EXIST_Status = 0x55667788;
}
uint8_t ClearFlag = 0;
uint8_t ClearFlagBack = 0;

void CHECK_IPK_COUNT(void)
{
    if ( CAN_Msg_Tx_Enable == 1u )
    {
        if ( NO_IPK_EXIST_TIMER != 0 )
        {
            NO_IPK_EXIST_TIMER--;
        }
        else
        {
            if ( NO_IPK_EXIST == 12345678 )
            {
                NO_IPK_EXIST_Status = 0x11223344;
                memset(InterNal_Version, 0xFF, sizeof(InterNal_Version));
                memset(SW_Version, 0xFF, sizeof(SW_Version));
                memset(HW_Version, 0xFF, sizeof(HW_Version));
                memset(Part_Version, 0xFF, sizeof(Part_Version));
                memset(Made_Date, 0xFF, sizeof(Made_Date));
                memset(SUPPLIER, 0xFF, sizeof(SUPPLIER));
                memset(Voltage_UDS, 0xFF, sizeof(Voltage_UDS));
                
                Display_Menu_Type = 0;
                Display_TFT_Clear( );
                Send_Init();                              
                // MENU_CHECK_Init( );
                Global_Session = 0;//掉线时，认为会话模式归零，重连CAN线后，重新将会话模式切到03扩展会话。
            }
            else
            {
                ClearFlag = 0;
            }
        }


    }
}

// 0x11223344 不在线
// 0x55667788 在线

uint32_t Get_IPK_Status(void)
{
    return NO_IPK_EXIST_Status;
}
void Diag_Info_Init(void)
{
    NO_IPK_EXIST_Status = 0x11223344;
    CAN_Msg_Tx_Enable   = 1u;

    memset(InterNal_Version , 0xFF, sizeof(InterNal_Version));
    memset(SW_Version , 0xFF, sizeof(SW_Version));
    memset(HW_Version , 0xFF, sizeof(HW_Version));
    memset(Part_Version , 0xFF, sizeof(Part_Version));
    memset(Risk_Evade, 0xFF, sizeof(Risk_Evade));
    memset(Evade_ODO, 0xFF, sizeof(Evade_ODO));
    memset(Res_St, 0xFF, sizeof(Res_St));
    memset(Made_Date , 0xFF, sizeof(Made_Date));
    memset(SUPPLIER , 0xFF, sizeof(SUPPLIER));
    //memset(Voltage_UDS, 0xFF, sizeof(Voltage_UDS));
    
}
uint8_t Type_Clear = 0;
extern uint8_t BCM_FLAG_2014 = 0;

    uint32_t loc_Type = 0;
void Total_Check(void)
{
uint8_t erweima[64] ;
uint8_t cmperweima[34];
uint8_t yibiaoshuju[34];
    
    loc_Type          = Get_Display_Type( );
    if ( Type_Clear != loc_Type )
    {
        Type_Clear = loc_Type;
        Display_TFT_Clear();
    }

    // POWER_CTRL_KL30_Current = 0u;//静态电流电阻的远仪表端，永远接通总电源。B+~

    BCM_FLAG_2014 = BCM_2014[1] & 0X40 ;
    // loc_Type = 2;
    switch ( loc_Type )
    {
        case 0:
         
            // Buzzer_Init();            
            // Function_Check_Ctrl(1);    //Excel表格发报文。硬线信号  
            //Buzzer_Warning();      
            // TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_dui12 ); 

            // for(int i = 0;i<34;i++)
            // {
            //     erweima[i] = barcode_Msg[i];
            // }
            memcpy(erweima,barcode_Msg,34);
            erweima[34] = 0xFF;
            General_Number_Disp(erweima, 3, 90);  
            
            break;

        case 1: 
            // Buzzer_Warning();    
            memcpy(cmperweima,barcode_Msg1,34);
            
            
            //  for(int i =0;i<34;i++)
            // {
            //     cmperweima[i] = barcode_Msg1[i];
            // }
            cmperweima[34] = 0xFF;
            General_Number_Disp(cmperweima, 3, 90);  
            memcpy(yibiaoshuju,get_num_buf,34);
            // for(int i =0;i<34;i++)
            // {
            //     yibiaoshuju[i] = get_num_buf[i];
            // }
            yibiaoshuju[34] = 0xFF;
            General_Number_Disp(yibiaoshuju, 3, 165);  
            // zhenduanflag = 3;
            // comparestart = 1;
            break; 
        case 2: 
            CAN_Msg_Tx_Enable = 1u;  
            if(MENU_CHECK_STEP == 0)
            {
                // Display_Version_Info(1);  
            }
            else
            {
                loc_Type_back = 0;
                
            }
            Function_Check_Ctrl(1);    //Excel表格发报文。硬线信号  
            Diag_Info_Init( );       
            Display_Init( );            
            // Send_Init();
            // Send_ODO_Init();             
            // Display_Send_ODO(0); 
            //FUEL_UDS__Display(3);
            Buzzer_Warning();
                  
            break;              
            
        case 3:   
            blename[4] = 0xFF;
            if(recvflag111 == 1)
            {
                General_Number_Disp(blename, 3, 165); 
                recvflag111 = 0;
            }
            
            // CAN_Msg_Tx_Enable = 1u;  
            // Diag_Info_Init( );       
            // Display_Init( );  
            // Display_Version_Info(0); 
                        
            // Display_Set_Buff(); 
            // // Function_Check_Ctrl(1);    //Excel表格发报文。硬线信号  
            // //FUEL_UDS__Display(3);
            // Buzzer_Warning();
            
            
            
                 
        //     break;                   
            
        // case 4:
            
                
        //         break;
            
            
                  
            

        default:            
            CAN_Msg_Tx_Enable = 1u;
            Display_Menu_Type = 0;
            Diag_Info_Init( );       
            Display_Init( );  
            Display_Version_Info(0);             
            Send_Init();
            Send_ODO_Init();
            // Buzzer_Init(); 
              
            break;
    }
}

void Read_IPK_Session_Type(void)//请求仪表发送 软硬件版本号 等数据。
{
    uint8_t         DIAG_BUFER [ 10 ];

    switch ( loc_Step )
    {
        case 0:
            loc_Step = 5;    
            break;

        case 1:
            if ( Global_Session != 3 )
            {
                DIAG_BUFER [ 0 ] = 0x10;
                DIAG_BUFER [ 1 ] = 0x03;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 2);//让仪表进入 扩展会话 中。
            }
            break;
        case 2:
            //*******************************
            //UnLock_Flag == 0x00   //初始    阶段
            //UnLock_Flag == 0x01   //请求种子阶段
            //UnLock_Flag == 0x02   //得到种子阶段
            //UnLock_Flag == 0x03   //发送密钥阶段
            //UnLock_Flag == 0x55   //解锁成功阶段
            //*******************************
            if ( UnLock_Flag == 0 )
            {
                UnLock_Retry     = 10;
                UnLock_Flag      = 1;//请求种子阶段
                DIAG_BUFER [ 0 ] = 0x27;
                DIAG_BUFER [ 1 ] = 0x03;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 2);    //
            }
            if ( UnLock_Flag == 0x55 )//解锁成功阶段
            {
                loc_Step = 3;
            }
            break;
        case 3:
            if ( UnLock_Flag == 2 )//得到种子阶段
            {
                UnLock_Flag      = 3;//发送密钥阶段
                UnLock_Retry     = 10;
                DIAG_BUFER [ 0 ] = 0x27;
                DIAG_BUFER [ 1 ] = 0x04;
                DIAG_BUFER [ 2 ] = KEYBUFFER [ 0 ];
                DIAG_BUFER [ 3 ] = KEYBUFFER [ 1 ];
                DIAG_BUFER [ 4 ] = KEYBUFFER [ 2 ];
                DIAG_BUFER [ 5 ] = KEYBUFFER [ 3 ];
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 4);    //
            }
            if ( UnLock_Flag == 0x55 )//解锁成功阶段
            {
                loc_Step = 4;
            }
            break;
        case 4:

            if ( InterNal_Version [ 0 ] == 0xff )
            {

                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = 0xf0;
                DIAG_BUFER [ 2 ] = 0x86;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //（工厂版本号）
            }
            loc_Step = 5;
            break;

        case 5:
            if ( SW_Version [ 0 ] == 0xff )
            {

                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(SV_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(SV_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 软件版本号
                //Execute_Specify_Step(2);
            }
            loc_Step = 6;
            break;

        case 6:
            if ( HW_Version [ 0 ] == 0xff )
            {
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(HV_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(HV_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 硬件版本号
            }
            loc_Step = 7;
            break;

        case 7:
            if ( Part_Version [ 0 ] == 0xff )
            {
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(PV_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(PV_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 零件号
            }
            loc_Step = 9;
            break;

        case 8:
            if ( Made_Date [ 0 ] == 0xff )
            {
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(DATE_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(DATE_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 生产日期
            }
            loc_Step = 9;
            break;
        case 9:
            if ( SUPPLIER [ 0 ] == 0xff )
            {
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = Get_DID_High_Byte(CODE_DID);
                DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(CODE_DID);
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 制造商代码
            }
            loc_Step = 10;
            break;
        case 10:
            //if ( Voltage_UDS [ 0 ] == 0xff )
            //{
                DIAG_BUFER [ 0 ] = 0x22;
                DIAG_BUFER [ 1 ] = 0x20;
                DIAG_BUFER [ 2 ] = 0x14;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    
            //}
            loc_Step = 11;
            break;
        case 11:
            //if ( Voltage_UDS [ 0 ] == 0xff )
            //{
                DIAG_BUFER [ 0 ] = 0x19;
                DIAG_BUFER [ 1 ] = 0x0A;
                
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 2);    
            //}
            loc_Step = 0;
            break;
        
        

        /*case 8:  //22 5008  读取燃油阻值
            if (RES_CHECK_BEGIN == 0) //发燃油信号时置0，下一步的时候若是0则请求读取燃油阻值，回复62，则置2，为2则刷图
            {
                if ( Res_St [ 0 ] == 0xff )
                {
                    RES_CHECK_BEGIN  = 1U;
                    DIAG_BUFER [ 0 ] = 0x22;
                    DIAG_BUFER [ 1 ] = Get_DID_High_Byte(FUEL_DID);
                    DIAG_BUFER [ 2 ] = Get_DID_Low_Byte(FUEL_DID);
                    UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 3);    //请求 燃油阻值
                }
            }*/
            
            
            #if 0
                DIAG_BUFER [ 0 ] = 0x2e;
                DIAG_BUFER [ 1 ] = 0x20;
                DIAG_BUFER [ 2 ] = 0x00;//07 07 F7 0B 

                DIAG_BUFER [ 3 ] = 0x4b;
                DIAG_BUFER [ 4 ] = 0x47;
                DIAG_BUFER [ 5 ] = 0xf7;
                DIAG_BUFER [ 6 ] = 0x6b;
                UDS_S_Data_Request(DIAG_ID_Tx, DIAG_BUFER, 7);    //
            #endif
            
            //loc_Step = 0;
            break;


    }

    if ( (UnLock_Flag == 1) || (UnLock_Flag == 3) )
    {
        if ( UnLock_Retry != 0 )
        {
            UnLock_Retry--;
        }
        else
        {
            if ( UnLock_Flag == 1 )
            {
                UnLock_Flag = 0u;
            }
            else if ( UnLock_Flag == 3 )
            {
                UnLock_Flag = 2;
            }
        }
    }
}
//重上大电
uint8_t resettime30;
void KL30_reset(void)
{
    if(MENU_CHECK_STEP == 2)
    {
        if((resettime30 <= 10) && (delayflash == 0))
        {
            resettime30++;
            RELAY_VBAT_MCU = 0;
        }
        else
        {
            resettime30 = 0;
            delayflash = 1;
            RELAY_VBAT_MCU = 1;
        }
    }
    else
    {
        resettime30 = 0;
        delayflash = 0;
    }
}

uint8_t settingtime;
void DefenseModeSettings(void)      //设防模式设置
{
    if(MENU_CHECK_STEP == 3)
    {
        if(settingtime <= 10)
        {
            settingtime++;
            RELAY_KEY_MCU = 1;
        }
        else if(settingtime <= 20)
        {
            settingtime++;
            RELAY_KEY_MCU = 0;
        }
        else
        {
            RELAY_KEY_MCU = 1;
        }
    }
    else
    {
        settingtime = 0;
    }
}

uint8_t RotationSettime;
uint8_t delayflash;
void RotationSet(void)
{
    if((MENU_CHECK_STEP == 2) && (delayflash == 1))
    {
        RotationAlarmMode = 1;
        if(RotationSettime <= 10)
        {
            RotationSettime++;
            ABC_OUT = 1;
        }
        else
        {
            ABC_OUT = 0;
        }
    }
    else
    {
        ABC_OUT = 0;
        RotationSettime = 0;
        RotationAlarmMode = 0;
    }
}

uint8_t LED_Waring = 0;
uint8_t ABStime = 0;
void Function_Check_Ctrl(uint32_t cmd)
{
    uint8_t         p [ 20 ];
    uint8_t         DIAG_BUFER [ 40 ];
    uint32_t        i;
    uint32_t        j;
    static uint32_t status    = 0;
    static uint32_t MENU_BACK = 0xff;

    if ( cmd == 0 )
    {
        ;
    }
    else if(cmd == 1)
    {
        //Voltage_UDS__Display();
        //FUEL_UDS__Display(1);

        if ( MENU_CHECK_STEP >= Get_Total_CheckStep( ) )
        {
            // MENU_CHECK_STEP = 0;
        }
        Display_Send_Vspead(MENU_CHECK_STEP);
        if ( MENU_BACK != MENU_CHECK_STEP )
        {
            MENU_BACK = MENU_CHECK_STEP;
            
            // Execute_Specify_Step(MENU_CHECK_STEP);//Excel表格发报文。

            // if (0)//数字需要3位还是2位
            // {
            //     p [ 0 ] = (MENU_CHECK_STEP + 1) / 100;    // 65535
            //     p [ 1 ] = ((MENU_CHECK_STEP + 1) % 100) / 10;
            //     p [ 2 ] = (MENU_CHECK_STEP + 1) % 10;    // 65535
            //     p [ 3 ] = 0xff;
            // }
            // else
            // {
            //     p [ 0 ] = ((MENU_CHECK_STEP + 1) % 100) / 10;
            //     p [ 1 ] = (MENU_CHECK_STEP + 1) % 10;    // 65535
            //     p [ 2 ] = 0xff;
            // }
            uint8_t MENUCHECKSTEP[2] = {MENU_CHECK_STEP, 0xFF};
            General_Number_Disp(MENUCHECKSTEP, 190, 10);//刷图：报文检测步数
        }
        Display_Send_Vspead(MENU_CHECK_STEP);
        switch (MENU_CHECK_STEP)
        {
            case 0:     //仪表下电设防
                RELAY_KEY_MCU = 0;
                break;

            case 1:     //检测震动报警
                RELAY_KEY_MCU = 0;
                break;

            case 2:   //检测轮动报警
                RELAY_KEY_MCU = 0;
                break;

            case 3:     //上电指示灯自检
                RELAY_KEY_MCU = 1;
                break;

            case 4:     //一键启动上下电
                RELAY_KEY_MCU = 0;
                break;

            case 5:     //左转右转远光
                RELAY_KEY_MCU = 0;
                break;

            case 6:     //光感检测
                RELAY_KEY_MCU = 0;
                break;

            case 7:     //坐桶锁
                RELAY_KEY_MCU = 0;
                ONEKEYFlag = 2;     //双击一键启动
                break;

            case 8:     //边撑下电
                RELAY_KEY_MCU = 0;
                BCDD_MCU_Flag = 1;
                break;

            case 9:
                RELAY_KEY_MCU = 1;
                break;

            default:
                break;
        }
    }
}


void Get_ODO_Call(void)
{
}
