#include "can_data_handle.h"
#include "common.h"
#include "system_init.h"
#include "soft_can.h"
#include "string.h"
#include "soft_terrain.h"
#include "soft_obstacle.h"
#include "soft_version.h"
#include "soft_update.h"
#include "soft_flow.h"
#include "soft_water.h"
#include "soft_bms.h"
#include "soft_seed_weight.h"
#include "main.h"

/**
 * @file    Can1_decode_data
 * @brief   CAN1解析 不过滤的ID
 * @param   none
 * @details
 * @author  Zhang Sir
 **/
canpack bufhandle;
uint8_t Can1RxData[8];

void Can1_decode_data_function(CAN_RxHeaderTypeDef Rxhead)
{
    if (Rxhead.IDE == CAN_ID_STD)
    {
        switch (Rxhead.StdId)
        {
        // 恩曌仿地雷达
        case CAN_MIMO_T_ID:
            can_recv_enzhao_terrain(Rxhead.StdId, RxData, Rxhead.DLC);
            //Can_Send_Msg_Func(CANID1, 0, 8, 0xAAAA, CAN_ID_EXT);
            break;

        // 恩曌前后避障雷达(单点)
        case CAN_MIMO_FOBS_SIG ... CAN_MIMO_BOBS_SIG:
            can_recv_mimo_signal_obstacle(Rxhead.StdId, RxData, Rxhead.DLC);
            break;

        // 恩曌360雷达 新协议
        case CAN_360MIMO_1ID ... CAN_360MIMO_2ID:
            can_recv_mocib_new360_obstacle(Rxhead.StdId, RxData, Rxhead.DLC);
            break;
        
        //恩曌前后避障雷达(多点)
        case CAN_MIMO_FOBS_ID1 ... CAN_MIMO_BOBS_ID3:
            can_recv_enzhao_obstacle(Rxhead.StdId, RxData, Rxhead.DLC);
            break;
        
        //恩曌分区防地
        case CAN_MIMO_T_ID1 ... CAN_MIMO_T_ID5:
            can_recv_enzhao_terrain(Rxhead.StdId, RxData, Rxhead.DLC);
            break;
        
        //恩曌流量计
        case CAN_MIMO_FLOW:
            can_recv_mimor_flow_function(Rxhead.StdId, RxData, Rxhead.DLC);
            break;
        case CAN_UAVRH_FOBS_ID1 ... CAN_UAVRH_BOBS_ID3: 
            can_recv_mocib_obstacle(RxHeader.StdId, RxData, RxHeader.DLC);
            break;
        //莫之比仿地雷达(分区)
        /*case CAN_MICOB_T_ID1 ... CAN_MICOB_T_ID5:
            can_recv_mocib_T_terrain(Rxhead.StdId, RxData, Rxhead.DLC);
            break;*/
        //恩曌雷达版本信息 和流量计系数
        case CAN_MIMO_VERSION:
            can_recv_mimo_dev_version(Rxhead.StdId, RxData, Rxhead.DLC);
            break;
        case CAN_EZ_R_UPDATE:
            EZ_Radar_UpdateCanRecvHookFunction(Rxhead.StdId, RxData, Rxhead.DLC);
            break;
        default:
            break;
        }
    }
    else if (Rxhead.IDE == CAN_ID_EXT)
    {
        switch (Rxhead.ExtId)
        {
        //莫之比雷达SN号
        case CAN_OBSTARCLE11_SN ... CAN_OBSTARCLE57_SN:
            can_recv_mocib_version_info(Rxhead.ExtId, RxData, Rxhead.DLC);
            break;
        // EFTZ20前板 称重 机臂传感器 LED灯
         //莫之比前后避障雷达(多点)
        case CAN_UAVRH_FOBS_ID1 ... CAN_UAVRH_BOBS_ID3: 
            can_recv_mocib_obstacle(RxHeader.StdId, RxData, RxHeader.DLC);
            break;
        case CAN_EFT_FPLATE ... CAN_EFT_FPLATE_ACK:
            can_recv_eft_front_plate(Rxhead.ExtId, RxData, Rxhead.DLC);
            break;
        case CAN_MICOB_T_ID1 ... CAN_MICOB_T_ID5:
            can_recv_mocib_T_terrain(Rxhead.ExtId, RxData, Rxhead.DLC);
            break;
        default:
            break;
        }       
    }
}

/**
 * @file    Can2_decode_data
 * @brief   CAN2解析 不过滤的ID
 * @param   none
 * @details
 * @author  Zhang Sir
 **/
uint8_t Can2RxData[8];
void Can2_decode_data_function(CAN_RxHeaderTypeDef Rxhead)
{
    if (Rxhead.IDE == CAN_ID_STD)
    {
        switch (Rxhead.StdId)
        {
        //恩曌雷达版本信息 和流量计系数
        case CAN_MIMO_VERSION:
            can_recv_mimo_dev_version(Rxhead.StdId,RxData,Rxhead.DLC);
            break;
        default:
            break;
        }
    }
    else if (Rxhead.IDE == CAN_ID_EXT)
    {
        switch (Rxhead.ExtId)
        {
        // //格式电池
        // case CAN_NEWTATTU_MSG ... CAN_NEWTATTU_MSG2:
        //     TattuCanRecvHookFunction(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
        //     break;

        //EFT播撒器
        case CAN_EFT_SPREAD ... CAN_EFT_VERSION:
            Eft_Spread_And_Mimolack_recieved_hookfuction(Rxhead.ExtId,RxData,Rxhead.DLC);
            break;
        case 0x9527: //水泵
            Eft_Pump_Nozzle_recieved_hookfuction(Rxhead.ExtId,RxData,Rxhead.DLC);
            break;
        case Z70_DEVICE ... Z70_DEVICE_ACK:
            Z70_Pump_Nozzle_recieved_hookfuction(Rxhead.ExtId,RxData,Rxhead.DLC);
            break;
        case CAN_EFT70_WEIGHT:
            Z70_Pump_Nozzle_recieved_hookfuction(Rxhead.ExtId, RxData, Rxhead.DLC);
            break;
        case CAN_EFT70_WEIGHT_ACK:
            Z70_Pump_Nozzle_recieved_hookfuction(Rxhead.ExtId, RxData, Rxhead.DLC);
            break;
        //恩曌断料记
        case CAN_MIMO_LACKLOSS:
            Eft_Spread_And_Mimolack_recieved_hookfuction(Rxhead.ExtId,RxData,Rxhead.DLC);
            break;
        default:
            //水泵离心
            Eft_Pump_Nozzle_recieved_hookfuction(Rxhead.ExtId,RxData,Rxhead.DLC);
            //好盈ID基本没有固定位，全检测
            HobbywingCanRecvHookFunction(Rxhead.ExtId,RxData,Rxhead.DLC);
            break;
        }
    }
}

enum CAN_rkfifo_handle can_step;

void check_can_data()
{
    uint8_t c = 0;
    static uint32_t vk_recv_time = 0;
    static uint8_t i = 0;

    while (rkfifo_out(&can1_rkfifo, &c, 1) != 0)
    {
        if (can_step != CAN_HEAD && HAL_GetTick() - vk_recv_time > 200)
        {
            can_step = CAN_HEAD;
        }

        // 解析出一包完整的数据
        switch (can_step)
        {
        case CAN_HEAD:
            if (c == 'V')
            {
                vk_recv_time = HAL_GetTick();
                memset(&bufhandle, 0, sizeof(canpack));
                i = 0;
                bufhandle.Head[0] = c;
            }
            else if (c == 'K')
            {
                bufhandle.Head[1] = c;
            }
            else if (bufhandle.Head[0] == 'V' && bufhandle.Head[1] == 'K')
            {
                bufhandle.Head[2] = c;
                bufhandle.Head[0] = 0;
                bufhandle.Head[1] = 0;
                can_step = CAN_ID;
            }
            break;
        case CAN_ID:
            bufhandle.Id += c << i;
            i += 8;
            if (i == 32)
            {
                can_step = CAN_IDE;
                i = 0;
            }
            break;
        case CAN_IDE:
            bufhandle.Ide += c << i;
            i += 8;
            if (i == 32)
            {
                can_step = CAN_DLC;
                i = 0;
            }
            break;
        case CAN_DLC:
            bufhandle.Dlc += c << i;
            i += 8;
            if (i == 32)
            {
                can_step = CAN_FILTER;
                if(bufhandle.Dlc > 8)
                {
                    can_step = CAN_HEAD;
                    bufhandle.Dlc = 0;
                }
                    
                i = 0;
            }
            break;
        case CAN_FILTER:
            bufhandle.FilterIndex += c << i;
            i += 8;
            if (i == 32)
            {
                can_step = CAN_DATA;
                i = 0;
            }
            break;
        case CAN_DATA:
            bufhandle.Data[i] = c;
            i++;
            if (i == bufhandle.Dlc)
            {
                can_step = CAN_HEAD;
                bufhandle.complete_flag = true;
            }
            break;

        default:
            can_step = CAN_HEAD;
            break;
        }
        if (bufhandle.complete_flag == true)
        {
            break;
        }
    }
}

void thread_can_task_entry(void *param)
{
    while (1)
    {
        rt_sem_take(&can1_sem, RT_WAITING_FOREVER);

        check_can_data();

        if (bufhandle.complete_flag == true)
        {
            switch (bufhandle.Head[2]) // 过滤器编号
            {
            case 1:
                if (bufhandle.FilterIndex == 0)
                {
                    // 莫之比前、后避、仿地
                    switch (bufhandle.Id)
                    {
                    case CAN_OBSTACLE_1:
                        can_recv_mocib_F_obstacle(bufhandle.Data);
                        break;
                    case CAN_OBSTACLE_2:
                        can_recv_mocib_B_obstacle(bufhandle.Data);
                        break;
                    case CAN_UAVH30_MSG:
                        can_recv_mocib_terrain(bufhandle.Data);
                        break;
                    default:
                        can_recv_mocib_version_info(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
                        break;
                    }
                }
                // 莫之比升级 灵敏度
                else if (bufhandle.FilterIndex == 1)
                {
                    can_recv_mocib_updata_read_set_hookfunction(bufhandle.Id, bufhandle.Data);
                }
                // VK协议
                else if (bufhandle.FilterIndex == 2)
                {
                    switch (((bufhandle.Id) & SRC_ID_MASK) >> 5)
                    {
                    // 液位计
                    case CAN_NODEID_LIQUED:
                        // liquid_recieved_hookfuction(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
                        break;
                    case CAN_NODEID_FLOW:
                        Flow_recieved_hookfuction(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
                        break;
                    // 称重传感器、播撒器
                    case CAN_NODEID_WEIGHT:
                        // Weight_recieved_hookfuction(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
                        break;
                    // VK设备升级
                    case CAN_NODEID_RADAR_UPDATE:
                        switch (((bufhandle.Id) & MSG_ID_MASK) >> 19)
                        {
                        case CAN_MSGID_RADAR_START:
                            if ((bufhandle.Id & 0x3) == 1)
                            {
                                update_info.step_200_flag = true;
                            }
                            else if ((bufhandle.Id & 0x3) == 2)
                            {
                                Update_Dev_Bootversion_Function(bufhandle.Data);
                            }
                            break;
                        case CAN_MSGID_RADAR_ING:
                            if (update_info.vk_dev_pack_num == (bufhandle.Data[1] + bufhandle.Data[2] * 256))
                            {
                                update_info.vk_dev_pack_num++;
                                update_info.step_201_flag = true;
                            }
                            break;
                        case CAN_MSGID_RADAR_END:
                            update_info.step_202_flag = true;
                            break;
                        default:
                            break;
                        }
                        break;
                    case CAN_NODEID_DISTRIBUTOR:
                        // distributor_recieved_hookfuction(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
                        break;
                    default:
                        break;
                    }
                }
                else
                {
                    Can1_decode_data_function(RxHeader);
                }
                break;
            case 2:
                if (bufhandle.FilterIndex == 0)
                {
                    HerewinCanRecvHookFunction(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
                }
                else if (bufhandle.FilterIndex == 1)
                {
                    OkcellCanRecvHookFunction(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
                }
                // VK电池
                else if (bufhandle.FilterIndex == 3)
                {
                    BMSCanRecvHookFunction(bufhandle.Id, bufhandle.Data, bufhandle.Dlc);
                }
                else
                {
                    Can2_decode_data_function(RxHeader);
                }
                break;
            default:
                break;
            }
            bufhandle.complete_flag = false;
        }
    }
}

