/*
 * hy_protocol.c
 *
 *  Created on: 2024年10月11日
 *      Author: psx
 */

#include "hy_protocol.h"
#include <string.h>
#include "inflash.h"
#include "pdebug.h"
#include "mppt_control.h"
#include <stdlib.h>
#include "task.h"
#include "tim.h"
#include "sl_protocol.h"
#include "parameter.h"

/* 电池状态数据报 */
static void HY_MsgProcFunc_batteryStatus(device_handle device, void *pMsg, uint32_t MsgLen);
/* 电量统计数据报 */
static void HY_MsgProcFunc_electricityStatistics(device_handle device, void *pMsg, uint32_t MsgLen);
/* 传感器号码配置 */
static void HY_MsgProcFunc_sensorNumberConfiguration(device_handle device, void *pMsg, uint32_t MsgLen);
/* 传感器号码查询 */
static void HY_MsgProcFunc_sensorNumberInquiry(device_handle device, void *pMsg, uint32_t MsgLen);
/* 充电阈值电压配置 */
static void HY_MsgProcFunc_chargingThresholdVoltageConfiguration(device_handle device, void *pMsg, uint32_t MsgLen);
/* 充电域值电压查询 */
static void HY_MsgProcFunc_chargingRangeVoltageQuery(device_handle device, void *pMsg, uint32_t MsgLen);
/* 复位指令 */
static void HY_MsgProcFunc_resetInstruction(device_handle device, void *pMsg, uint32_t MsgLen);

/* 充电控制配置 */
static void HY_MsgProcFunc_chargingControlConfiguration(device_handle device, void *pMsg, uint32_t MsgLen);
/* 充电控制查询 */
static void HY_MsgProcFunc_chargingControlQuery(device_handle device, void *pMsg, uint32_t MsgLen);

/* 配置协议类型 */
static void HY_MsgProcFunc_configureProtocolType(device_handle device, void *pMsg, uint32_t MsgLen);
///* 响应配置协议类型 */
//static void HY_MsgProcFunc_batteryStatus(device_handle device, void *pMsg, uint32_t MsgLen);

/* 查询电池控制盒当前配置 */
static void HY_MsgProcFunc_queryControlBoxConfiguration(device_handle device, void *pMsg, uint32_t MsgLen);
/* 查询电池控制盒软件版本 */
static void HY_MsgProcFunc_querySoftwareVersion(device_handle device, void *pMsg, uint32_t MsgLen);
/* 进入配置模式 */
static void HY_MsgProcFunc_enterConfigurationMode(device_handle device, void *pMsg, uint32_t MsgLen);

/* 配置控制盒硬件ID号 */
static void HY_MsgProcFunc_configureHardwareID(device_handle device, void *pMsg, uint32_t MsgLen);
/* 控制盒硬件ID号及通信ID号(原传感器号)查询 */
static void HY_MsgProcFunc_hardwareID_communicationIDQuery(device_handle device, void *pMsg, uint32_t MsgLen);
/* 修改通信ID号(原传感器号) */
static void HY_MsgProcFunc_modifyCommunicationID(device_handle device, void *pMsg, uint32_t MsgLen);
/* 查询主板温度值 */
static void HY_MsgProcFunc_checkMotherboardTemperature(device_handle device, void *pMsg, uint32_t MsgLen);


/* 功能码处理表 */
HY_FuncionMsgProcTable g_hyMsgTbl[] =
{
    {HY_batteryStatus,                          HY_MsgProcFunc_batteryStatus},
    {HY_electricityStatistics,                  HY_MsgProcFunc_electricityStatistics},
    {HY_sensorNumberConfiguration,              HY_MsgProcFunc_sensorNumberConfiguration},
    {HY_sensorNumberInquiry,                    HY_MsgProcFunc_sensorNumberInquiry},
    {HY_chargingThresholdVoltageConfiguration,  HY_MsgProcFunc_chargingThresholdVoltageConfiguration},
    {HY_chargingRangeVoltageQuery,              HY_MsgProcFunc_chargingRangeVoltageQuery},
    {HY_resetInstruction,                       HY_MsgProcFunc_resetInstruction},
    {HY_chargingControlConfiguration,           HY_MsgProcFunc_chargingControlConfiguration},
    {HY_chargingControlQuery,                   HY_MsgProcFunc_chargingControlQuery},
    {HY_configureProtocolType,                  HY_MsgProcFunc_configureProtocolType},
//    {HY_responseConfigureProtocolType,          HY_MsgProcFunc_batteryStatus},
    {HY_queryControlBoxConfiguration,           HY_MsgProcFunc_queryControlBoxConfiguration},
    {HY_querySoftwareVersion,                   HY_MsgProcFunc_querySoftwareVersion},
    {HY_enterConfigurationMode,                 HY_MsgProcFunc_enterConfigurationMode},
    {HY_configureHardwareID,                    HY_MsgProcFunc_configureHardwareID},
    {HY_hardwareID_communicationIDQuery,        HY_MsgProcFunc_hardwareID_communicationIDQuery},
    {HY_modifyCommunicationID,                  HY_MsgProcFunc_modifyCommunicationID},
    {HY_checkMotherboardTemperature,            HY_MsgProcFunc_checkMotherboardTemperature},
};

/**
  * @brief  检测485总线是否繁忙
  * @param
  * @retval 1 繁忙
  *         0 空闲
  */
uint8_t HY_Check_485_bus_busy(device_handle device)
{
    if (device == g_bat485_uart3_handle) {
        USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
    } else {
        USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
    }

    uint16_t num_ago = ring_queue_length(device);
    Delay_Ms(2);
    uint16_t num_now = ring_queue_length(device);

    if (device == g_bat485_uart3_handle) {
        USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
    } else {
        USART_ITConfig(USART4, USART_IT_RXNE, DISABLE);
    }

    if (num_now == num_ago) {
        return 0;
    }
    return 1;
}

/**
  * @brief  校验算法
  * @param  采用累加和取反的校验方式,将终端号码、传感器号码、控制字、数据长度和数据区的所有字节进行算术累加,抛弃高位,只保留最后单字节,将单字节取反;
  * @retval
  */
uint8_t HY_CheckFunc(uint8_t *arr_buff, uint8_t len)
{
    uint8_t temp = 0x00;
    uint32_t sum = 0x00;

    for(uint8_t i = 0; i < len; i++) {
        sum += *arr_buff++;
    }

    temp = (~sum) & 0xff;

    return temp;
}

/**
  * @brief  匹配设备硬件ID
  * @param  address     地址
  * @retval 1 匹配成功
  *         0 匹配失败
  */
static int HY_matchHardwareID(u_int8_t hardwareID[6])
{
    if ((hardwareID[0] == g_otherParameter.hardwareID[0]) && \
            (hardwareID[1] == g_otherParameter.hardwareID[1]) && \
            (hardwareID[2] == g_otherParameter.hardwareID[2]) && \
            (hardwareID[3] == g_otherParameter.hardwareID[3]) && \
            (hardwareID[4] == g_otherParameter.hardwareID[4]) && \
            (hardwareID[5] == g_otherParameter.hardwareID[5])) {
        log_info("Match_hardwareIDHY success \r\n");
        return 1;
    }
    return 0;
}

/**
  * @brief  匹配广播硬件ID
  * @param  address     地址
  * @retval 1 匹配成功
  *         0 匹配失败
  */
static int Match_BroadcastHardwareID(u_int8_t hardwareID[6])
{
    if (hardwareID[0] == 0xFF && \
        hardwareID[1] == 0xFF && \
        hardwareID[2] == 0xFF && \
        hardwareID[3] == 0xFF && \
        hardwareID[4] == 0xFF && \
        hardwareID[5] == 0xFF) {
        log_info("Match_BroadcastHardwareID success\r\n");
        return 1;
    }
    return 0;
}

/**
  * @brief  匹配设备通信ID
  * @param  address     地址
  * @retval 1 匹配成功
  *         0 匹配失败
  */
static int HY_matchCommunicationID(u_int8_t communicationID[4])
{
    if ((communicationID[0] == g_otherParameter.communicationID[0]) && \
            (communicationID[1] == g_otherParameter.communicationID[1]) && \
            (communicationID[2] == g_otherParameter.communicationID[2]) && \
            (communicationID[3] == g_otherParameter.communicationID[3])) {
        log_info("Match_hardwareIDHY success \r\n");
        return 1;
    }
    return 0;
}

/**
  * @brief  匹配广播ID
  * @param  address     地址
  * @retval 1 匹配成功
  *         0 匹配失败
  */
static int Match_BroadcastCommunicationID(u_int8_t communicationID[4])
{
    if (communicationID[0] == 0xFF && \
        communicationID[1] == 0xFF && \
        communicationID[2] == 0xFF && \
        communicationID[3] == 0xFF) {
        log_info("Match_BroadcastHardwareID success\r\n");
        return 1;
    }
    return 0;
}

/**
  * @brief  读取串口数据
  * @param  uart_handle 串口句柄
  * @param  buff        缓冲区
  * @param  buff_size   缓冲区长度
  * @retval
  */
static int HY_uart_read_climate_pack(device_handle uart_handle,uint8_t *buff, uint32_t buff_size)
{
    uint32_t offset = 0;
    uint32_t len = 0;
    uint8_t flag_run = 0;

    /* 接收到的硬件ID种类
     * 0x01 广播ID
     * 0x02 硬件ID
     **/
    uint8_t hardwordIDType = 0;
    char c = 0;

    HY_Recv_pack *pack = (HY_Recv_pack *)buff;

    buff_size--; //预留一个'\0'位置
    for (; offset < buff_size;){
        if (ring_queue_length(uart_handle) == 0) {
            break;
        }

        c = uart_dev_in_char(uart_handle);

        buff[offset++] = c;

        /* 匹配起始标志位 */
        if (offset == HY_analyzeStartFlag || (flag_run > 0)) {
            if (pack->start_Flag != g_otherParameter.startFlagHY) {
                memcpy(buff, buff+1, offset-1);
                offset--;
                continue;
            }
        }

        /* 匹配硬件ID */
        if (offset == HY_analyzeHardwareID || (flag_run > 1)) {
            if (HY_matchHardwareID(pack->hardwareID)) {
                hardwordIDType = 0x02;
            }

            else if (Match_BroadcastHardwareID(pack->hardwareID)) {
                hardwordIDType = 0x01;
            }

            else {
                hardwordIDType = 0x00;
                if (flag_run < 1) {
                    flag_run = 1;
                }
                memcpy(buff, buff+1, offset-1);
                offset--;
                continue;
            }
        }

        /* 匹配通信ID */
        if (offset == HY_analyzeCommunicationID || (flag_run > 2)) {
            if (HY_matchCommunicationID(pack->hardwareID) || Match_BroadcastCommunicationID(pack->hardwareID)) {
                if (flag_run < 2) {
                    flag_run = 2;
                }
                memcpy(buff, buff+1, offset-1);
                offset--;
                continue;
            }
        }

        /* 匹配 */
        if (offset == HY_analyzeControlWord || (flag_run > 3)) {
            if (pack->controlWord == HY_batteryStatus && hardwordIDType == 0x02) {
                len = HY_batteryStatusQuery_PACK_SIZE;
            }

            else if (pack->controlWord == HY_electricityStatistics && hardwordIDType == 0x02) {
                len = HY_electricityStatisticsQuery_PACK_SIZE;
            }

            else if (pack->controlWord == HY_sensorNumberConfiguration && hardwordIDType == 0x02) {
                len = HY_sensorNumberConfiguration_PACK_SIZE;
            }

            else if (pack->controlWord == HY_sensorNumberInquiry && hardwordIDType == 0x01) {
                len = HY_sensorNumberInquiryQuery_PACK_SIZE;
            }

            else if (pack->controlWord == HY_chargingThresholdVoltageConfiguration && hardwordIDType == 0x02) {
                len = HY_chargingThresholdVoltageConfig_PACK_SIZE;
            }

            else if (pack->controlWord == HY_chargingRangeVoltageQuery && hardwordIDType == 0x02) {
                len = HY_chargRangeVoltageQuery_PACK_SIZE;
            }

            else if (pack->controlWord == HY_resetInstruction && hardwordIDType == 0x02) {
                len = HY_resetInstructionQuery_PACK_SIZE;
            }

            else if (pack->controlWord == HY_chargingControlConfiguration && hardwordIDType == 0x02) {
                len = HY_chargingControlConfig_PACK_SIZE;
            }

            else if (pack->controlWord == HY_chargingControlQuery && hardwordIDType == 0x02) {
                len = HY_QueryChargingControl_PACK_SIZE;
            }

            else if (pack->controlWord == HY_configureProtocolType && hardwordIDType == 0x02) {
                len = HY_configProtocolType_PACK_SIZE;
            }

            else if (pack->controlWord == HY_queryControlBoxConfiguration && hardwordIDType == 0x02) {
                len = HY_queryControlBoxConfigurationQuery_PACK_SIZE;
            }

            else if (pack->controlWord == HY_querySoftwareVersion && hardwordIDType == 0x02) {
                len = HY_SoftwareVersionQuery_PACK_SIZE;
            }

            else if (pack->controlWord == HY_enterConfigurationMode && hardwordIDType == 0x02) {
                len = HY_enterConfigMode_PACK_SIZE;
            }

            else if (pack->controlWord == HY_configureHardwareID && hardwordIDType == 0x02) {
                len = HY_configHardwareID_PACK_SIZE;
            }
//            else if (pack->controlWord == HY_hardwareID_communicationIDQuery) {
//                len = HY_modifyCommunicationIDChange_PACK_SIZE;
//            }
            else if (pack->controlWord == HY_modifyCommunicationID && hardwordIDType == 0x02) {
                len = HY_modifyCommunicationIDChange_PACK_SIZE;
            }

            else if (pack->controlWord == HY_checkMotherboardTemperature && hardwordIDType == 0x02) {
                len = HY_checkMotherboardTemperatureQuery_PACK_SIZE;
            }

            else {
                if (flag_run < 3) {
                    flag_run = 3;
                }
                memcpy(buff, buff+1, offset-1);
                offset--;
                continue;
            }
        }

        /* 匹配通信ID */
        if (offset == HY_analyzeDataLen || (flag_run > 4)) {
            if (len != ((pack->dataLen[0] << 8 + pack->dataLen[1]) + 16)) {
                if (flag_run < 4) {
                    flag_run = 4;
                }
                memcpy(buff, buff+1, offset-1);
                offset--;
                continue;
            }
        }

        if (offset == len) {
            if (buff[len - 2] != HY_CheckFunc(buff, len - 2)
                    && buff[len - 1] != g_otherParameter.endFlagHY) {
                if (flag_run < 5) {
                    flag_run = 5;
                }
                memcpy(buff, buff+1, offset-1);
                offset--;
                continue;
            }
            return len;
        }
    }
    return 0;
}

/**
  * @brief  处理一条消息
  * @param
  * @retval
  */
static void HY_FRT_MsgHandler(device_handle device, uint8_t *pMsg, uint32_t MsgLen)
{
    HY_Recv_pack *pack = (HY_Recv_pack *)pMsg;

    for (u_int16_t i = 0; i < sizeof(g_hyMsgTbl) / sizeof(HY_FuncionMsgProcTable); i++){
        if (pack->controlWord == g_hyMsgTbl[i].msgId){
            g_hyMsgTbl[i].pMsgProc(device, pMsg, MsgLen);
        }
    }
}


/**
  * @brief  读取并解析串口数据
  * @param
  * @retval
  */
void HY_read_and_process_uart_data(device_handle device)
{

    if (uart_dev_char_present(device)) {
        Delay_Ms(20);
        memset(rs485_buff, 0, sizeof(rs485_buff));
        int ret = HY_uart_read_climate_pack(device, rs485_buff, sizeof(rs485_buff));
        if(ret > 0){
            HY_FRT_MsgHandler(device, rs485_buff, ret);
        }
    }
}

/* 电池状态数据报 */
void HY_MsgProcFunc_batteryStatus(device_handle device, void *pMsg, uint32_t MsgLen)
{
    HY_batteryStatusResponse pack;
    pack.start_Flag = g_otherParameter.startFlagHY;
    pack.hardwareID[0] = g_otherParameter.hardwareID[0];
    pack.hardwareID[1] = g_otherParameter.hardwareID[1];
    pack.hardwareID[2] = g_otherParameter.hardwareID[2];
    pack.hardwareID[3] = g_otherParameter.hardwareID[3];
    pack.hardwareID[4] = g_otherParameter.hardwareID[4];
    pack.hardwareID[5] = g_otherParameter.hardwareID[5];

    pack.communicationID[0] = g_otherParameter.communicationID[0];
    pack.communicationID[1] = g_otherParameter.communicationID[1];
    pack.communicationID[2] = g_otherParameter.communicationID[2];
    pack.communicationID[3] = g_otherParameter.communicationID[3];

    pack.controlWord = HY_batteryStatus;
    pack.dataLen[0] = HY_batteryStatusResponse_dataLen >> 8;
    pack.dataLen[1] = HY_batteryStatusResponse_dataLen;

    pack.batteryVoltage        = g_otherParameter.Battery_Voltage;
    pack.dischargCurrent       = g_otherParameter.Discharg_Current;
    pack.chargCurrent1         = g_otherParameter.Charg_Current;
    pack.SOC                   = g_otherParameter.SOC;
    pack.openCircuitVoltage1   = g_otherParameter.Solar_In_Circuit_Voltage;

    if (g_controlParameter.dutyRatio) {
        pack.chargSwitchStatus1 = 1;
    } else {
        pack.chargSwitchStatus1 = 0;
    }

    pack.chargCurrent2         = g_otherParameter.Charg_Current;
    pack.openCircuitVoltage1   = g_otherParameter.Solar_In_Circuit_Voltage;

    if (g_controlParameter.dutyRatio) {
        pack.chargSwitchStatus2 = 1;
    } else {
        pack.chargSwitchStatus2 = 0;
    }

    pack.Mos_Temperature = g_otherParameter.HighSideMos_Temperature;

    HY_batteryStatusQuery *Tpack = (HY_batteryStatusQuery *)pMsg;
    pack.frameNumber = Tpack->frameNumber;

    pack.check_Bit = HY_CheckFunc((uint8_t *)(&pack), HY_batteryStatusResponse_PACK_SIZE - 2);

    pack.end_Flag = g_otherParameter.endFlagHY;

    while (1) {
        Delay_Ms(randomDelay());
        if (!Check_485_bus_busy(device)) {
            uart_dev_write(device, &pack, HY_batteryStatusResponse_PACK_SIZE);

            if (device == g_bat485_uart3_handle) {
                USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
            } else {
                USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
            }

            break;
        }
    }

}

/* 电量统计数据报 */
void HY_MsgProcFunc_electricityStatistics(device_handle device, void *pMsg, uint32_t MsgLen)
{
    HY_electricityStatisticsResponse pack;
    pack.start_Flag = g_otherParameter.startFlagHY;

    pack.hardwareID[0] = g_otherParameter.hardwareID[0];
    pack.hardwareID[1] = g_otherParameter.hardwareID[1];
    pack.hardwareID[2] = g_otherParameter.hardwareID[2];
    pack.hardwareID[3] = g_otherParameter.hardwareID[3];
    pack.hardwareID[4] = g_otherParameter.hardwareID[4];
    pack.hardwareID[5] = g_otherParameter.hardwareID[5];

    pack.communicationID[0] = g_otherParameter.communicationID[0];
    pack.communicationID[1] = g_otherParameter.communicationID[1];
    pack.communicationID[2] = g_otherParameter.communicationID[2];
    pack.communicationID[3] = g_otherParameter.communicationID[3];

    pack.controlWord = HY_electricityStatistics;

    pack.dataLen[0] = HY_electricityStatisticsResponse_dataLen >> 8;
    pack.dataLen[1] = HY_electricityStatisticsResponse_dataLen;

    pack.statisticalDuration            = 0;
    pack.totalChargCapacity             = g_otherParameter.totalChargCapacity;
    pack.totalElectricityConsumption    = g_otherParameter.totalElectricityConsumption;
    HY_electricityStatisticsQuery *Tpack = (HY_electricityStatisticsQuery *)pMsg;
    pack.frameNumber = Tpack->frameNumber;

    pack.check_Bit = HY_CheckFunc((uint8_t *)(&pack), HY_electricityStatisticsResponse_PACK_SIZE - 2);
    pack.end_Flag = g_otherParameter.endFlagHY;

    while (1) {
        Delay_Ms(randomDelay());
        if (!Check_485_bus_busy(device)) {
            uart_dev_write(device, &pack, HY_batteryStatusResponse_PACK_SIZE);

            if (device == g_bat485_uart3_handle) {
                USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
            } else {
                USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
            }

            break;
        }
    }
}

/* 传感器号码配置 */
void HY_MsgProcFunc_sensorNumberConfiguration(device_handle device, void *pMsg, uint32_t MsgLen)
{
    HY_sensorNumberConfigurationResponse pack;

    HY_sensorNumberConfig *Tpack = (HY_sensorNumberConfig *)pMsg;
    if (HY_matchHardwareID(Tpack->newHardwareID)) {
        g_otherParameter.communicationID[0] = Tpack->newCommunicationID[0];
        g_otherParameter.communicationID[1] = Tpack->newCommunicationID[1];
        g_otherParameter.communicationID[2] = Tpack->newCommunicationID[2];
        g_otherParameter.communicationID[3] = Tpack->newCommunicationID[3];
        pack.state = HY_success;
    }
    else {
        pack.state = HY_fail;
    }

    pack.start_Flag = g_otherParameter.startFlagHY;

    pack.hardwareID[0] = g_otherParameter.hardwareID[0];
    pack.hardwareID[1] = g_otherParameter.hardwareID[1];
    pack.hardwareID[2] = g_otherParameter.hardwareID[2];
    pack.hardwareID[3] = g_otherParameter.hardwareID[3];
    pack.hardwareID[4] = g_otherParameter.hardwareID[4];
    pack.hardwareID[5] = g_otherParameter.hardwareID[5];

    pack.communicationID[0] = g_otherParameter.communicationID[0];
    pack.communicationID[1] = g_otherParameter.communicationID[1];
    pack.communicationID[2] = g_otherParameter.communicationID[2];
    pack.communicationID[3] = g_otherParameter.communicationID[3];

    pack.controlWord = Tpack->controlWord;

    pack.dataLen[0] = HY_sensorNumberConfigurationResponse_dataLen >> 8;
    pack.dataLen[1] = HY_sensorNumberConfigurationResponse_dataLen;

    pack.frameNumber = Tpack->frameNumber;

    pack.check_Bit = HY_CheckFunc((uint8_t *)(&pack), HY_sensorNumberConfiguration_PACK_SIZE - 2);
    pack.end_Flag = g_otherParameter.endFlagHY;

    while (1) {
        Delay_Ms(randomDelay());
        if (!Check_485_bus_busy(device)) {
            uart_dev_write(device, &pack, HY_batteryStatusResponse_PACK_SIZE);

            if (device == g_bat485_uart3_handle) {
                USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
            } else {
                USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
            }

            break;
        }
    }
}

/* 传感器号码查询 */
void HY_MsgProcFunc_sensorNumberInquiry(device_handle device, void *pMsg, uint32_t MsgLen)
{
    HY_sensorNumberInquiryQuery *Tpack = (HY_sensorNumberInquiryQuery *)pMsg;

    HY_sensorNumberInquiryResponse pack;

    pack.start_Flag = g_otherParameter.startFlagHY;

    pack.hardwareID[0] = g_otherParameter.hardwareID[0];
    pack.hardwareID[1] = g_otherParameter.hardwareID[1];
    pack.hardwareID[2] = g_otherParameter.hardwareID[2];
    pack.hardwareID[3] = g_otherParameter.hardwareID[3];
    pack.hardwareID[4] = g_otherParameter.hardwareID[4];
    pack.hardwareID[5] = g_otherParameter.hardwareID[5];

    pack.communicationID[0] = g_otherParameter.communicationID[0];
    pack.communicationID[1] = g_otherParameter.communicationID[1];
    pack.communicationID[2] = g_otherParameter.communicationID[2];
    pack.communicationID[3] = g_otherParameter.communicationID[3];

    pack.controlWord = Tpack->controlWord;

    pack.dataLen[0] = HY_sensorNumberInquiryResponse_dataLen >> 8;
    pack.dataLen[1] = HY_sensorNumberInquiryResponse_dataLen;

    pack.hardwareIDR[0] = g_otherParameter.hardwareID[0];
    pack.hardwareIDR[1] = g_otherParameter.hardwareID[1];
    pack.hardwareIDR[2] = g_otherParameter.hardwareID[2];
    pack.hardwareIDR[3] = g_otherParameter.hardwareID[3];
    pack.hardwareIDR[4] = g_otherParameter.hardwareID[4];
    pack.hardwareIDR[5] = g_otherParameter.hardwareID[5];

    pack.communicationIDR[0] = g_otherParameter.communicationID[0];
    pack.communicationIDR[1] = g_otherParameter.communicationID[1];
    pack.communicationIDR[2] = g_otherParameter.communicationID[2];
    pack.communicationIDR[3] = g_otherParameter.communicationID[3];

    pack.frameNumber = Tpack->frameNumber;

    pack.check_Bit = HY_CheckFunc((uint8_t *)(&pack), HY_sensorNumberConfiguration_PACK_SIZE - 2);
    pack.end_Flag = g_otherParameter.endFlagHY;

    while (1) {
        Delay_Ms(randomDelay());
        if (!Check_485_bus_busy(device)) {
            uart_dev_write(device, &pack, HY_batteryStatusResponse_PACK_SIZE);

            if (device == g_bat485_uart3_handle) {
                USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
            } else {
                USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
            }

            break;
        }
    }
}

/* 充电阈值电压配置 */
void HY_MsgProcFunc_chargingThresholdVoltageConfiguration(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

/* 充电域值电压查询 */
void HY_MsgProcFunc_chargingRangeVoltageQuery(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

/* 复位指令 */
void HY_MsgProcFunc_resetInstruction(device_handle device, void *pMsg, uint32_t MsgLen)
{
    HY_resetInstructionQuery *Tpack = (HY_resetInstructionQuery *)pMsg;

    HY_resetInstructionResponse pack;

    pack.start_Flag = g_otherParameter.startFlagHY;

    pack.hardwareID[0] = g_otherParameter.hardwareID[0];
    pack.hardwareID[1] = g_otherParameter.hardwareID[1];
    pack.hardwareID[2] = g_otherParameter.hardwareID[2];
    pack.hardwareID[3] = g_otherParameter.hardwareID[3];
    pack.hardwareID[4] = g_otherParameter.hardwareID[4];
    pack.hardwareID[5] = g_otherParameter.hardwareID[5];

    pack.communicationID[0] = g_otherParameter.communicationID[0];
    pack.communicationID[1] = g_otherParameter.communicationID[1];
    pack.communicationID[2] = g_otherParameter.communicationID[2];
    pack.communicationID[3] = g_otherParameter.communicationID[3];

    pack.controlWord = Tpack->controlWord;

    pack.dataLen[0] = HY_resetInstructionResponse_dataLen >> 8;
    pack.dataLen[1] = HY_resetInstructionResponse_dataLen;

    pack.state = HY_success;

    pack.frameNumber = Tpack->frameNumber;

    pack.check_Bit = HY_CheckFunc((uint8_t *)(&pack), HY_resetInstructionResponse_PACK_SIZE - 2);
    pack.end_Flag = g_otherParameter.endFlagHY;

    while (1) {
        Delay_Ms(randomDelay());
        if (!Check_485_bus_busy(device)) {
            uart_dev_write(device, &pack, HY_batteryStatusResponse_PACK_SIZE);

            if (device == g_bat485_uart3_handle) {
                USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
            } else {
                USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
            }
            break;
        }
    }
}


/* 充电控制配置 */
void HY_MsgProcFunc_chargingControlConfiguration(device_handle device, void *pMsg, uint32_t MsgLen)
{
//    HY_chargingControlConfig *Tpack = (HY_chargingControlConfig *)pMsg;
//
//    HY_chargingControlConfigResponse pack;
//
//    pack.start_Flag = g_otherParameter.startFlagHY;
//
//    pack.hardwareID[0] = g_otherParameter.hardwareID[0];
//    pack.hardwareID[1] = g_otherParameter.hardwareID[1];
//    pack.hardwareID[2] = g_otherParameter.hardwareID[2];
//    pack.hardwareID[3] = g_otherParameter.hardwareID[3];
//    pack.hardwareID[4] = g_otherParameter.hardwareID[4];
//    pack.hardwareID[5] = g_otherParameter.hardwareID[5];
//
//    pack.communicationID[0] = g_otherParameter.communicationID[0];
//    pack.communicationID[1] = g_otherParameter.communicationID[1];
//    pack.communicationID[2] = g_otherParameter.communicationID[2];
//    pack.communicationID[3] = g_otherParameter.communicationID[3];
//
//    pack.controlWord = Tpack->controlWord;
//
//    pack.dataLen[0] = HY_chargingControlConfigResponse_dataLen >> 8;
//    pack.dataLen[1] = HY_chargingControlConfigResponse_dataLen;
//
//    pack.state = HY_success;
//
//    pack.frameNumber = Tpack->frameNumber;
//
//    pack.check_Bit = HY_CheckFunc((uint8_t *)(&pack), HY_chargingControlConfigResponse_PACK_SIZE - 2);
//    pack.end_Flag = g_otherParameter.endFlagHY;
//
//    while (1) {
//        Delay_Ms(randomDelay());
//        if (!Check_485_bus_busy(device)) {
//            uart_dev_write(device, &pack, HY_batteryStatusResponse_PACK_SIZE);
//
//            if (device == g_bat485_uart3_handle) {
//                USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
//            } else {
//                USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
//            }
//            break;
//        }
//    }
}

/* 充电控制查询 */
static void HY_MsgProcFunc_chargingControlQuery(device_handle device, void *pMsg, uint32_t MsgLen)
{

}


/* 配置协议类型 */
void HY_MsgProcFunc_configureProtocolType(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

///* 响应配置协议类型 */
//static void HY_MsgProcFunc_batteryStatus(device_handle device, void *pMsg, uint32_t MsgLen);

/* 查询电池控制盒当前配置 */
void HY_MsgProcFunc_queryControlBoxConfiguration(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

/* 查询电池控制盒软件版本 */
void HY_MsgProcFunc_querySoftwareVersion(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

/* 进入配置模式 */
void HY_MsgProcFunc_enterConfigurationMode(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

/* 配置控制盒硬件ID号 */
void HY_MsgProcFunc_configureHardwareID(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

/* 控制盒硬件ID号及通信ID号(原传感器号)查询 */
void HY_MsgProcFunc_hardwareID_communicationIDQuery(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

/* 修改通信ID号(原传感器号) */
void HY_MsgProcFunc_modifyCommunicationID(device_handle device, void *pMsg, uint32_t MsgLen)
{

}

/* 查询主板温度值 */
void HY_MsgProcFunc_checkMotherboardTemperature(device_handle device, void *pMsg, uint32_t MsgLen)
{

}