/*
 * communication_protocol.c
 *
 *  Created on: 2024年5月18日
 *      Author: 34509
 */

#include <communication_protocol.h>
#include "inflash.h"
#include <string.h>
#include "thread_communication.h"
#include "UART.h"


/* 用于解析串口包时的长度 */
#define readSensorAnalyzeStartFlag 2                //长度为2时解析起始标志
#define readSensorAnalyzeFunctionCode 10            //长度为10时解析功能码

#define readIntelligentModuleAnalyzeHeader 6        //长度为4时解析包尾
//#define readIntelligentModuleAnalyzeHeader 4        //长度为4时解析包尾
//#define readIntelligentModuleAnalyzeStartFlag 6     //长度为6时解析起始标志
//#define readIntelligentMOduleAnalyzeFunctionCode 14 //长度为14时解析功能码


SL_Mppt_Scan_Broadcast_pack g_Scan_Broadcast_pack = {
        .start_Flag = "SL",
        .address = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        .function_Code = SL_Function_Code_Broadcast_Scan,
        .check_Bit_H = 0x36,
        .check_Bit_L = 0xE6,
        .end_Flag = 0x16,
};

/* 读取串口数据时用该数组解析 */
static uint8_t uart_buff[50]={0x00};


/* 静态函数申明 */
static void SL_MsgProcFuncReadSensor_Read_Register(device_handle device, void *pMsg, uint32_t MsgLen);
static void SL_MsgProcFuncReadSensor_Registration_request(device_handle device, void *pMsg, uint32_t MsgLen);
static void SL_MsgProcFuncReadSensor_Update_Profile(device_handle device, void *pMsg, uint32_t MsgLen);
static void SL_MsgProcFuncReadSensor_Remote_Upgrade(device_handle device, void *pMsg, uint32_t MsgLen);

static void SL_MsgProcFuncReadIntelligentModule_Read_Register(device_handle device, void *pMsg, uint32_t MsgLen);
static void SL_MsgProcFuncReadIntelligentModule_Write_Register(device_handle device, void *pMsg, uint32_t MsgLen);
static void SL_MsgProcFuncReadIntelligentModule_Broadcast_Scan(device_handle device, void *pMsg, uint32_t MsgLen);
static void SL_MsgProcFuncReadIntelligentModule_Registration_request(device_handle device, void *pMsg, uint32_t MsgLen);
static void SL_MsgProcFuncReadIntelligentModule_Update_Profile(device_handle device, void *pMsg, uint32_t MsgLen);
static void SL_MsgProcFuncReadIntelligentModule_Remote_Upgrade(device_handle device, void *pMsg, uint32_t MsgLen);

/* 传感器发送上来的数据功能码处理表 */
SL_FuncionMsgProcTable g_MsgReadSensorTbl[] =
{
    {SL_Function_Code_Read_Register,        SL_MsgProcFuncReadSensor_Read_Register},
    {SL_Function_Code_Registration_request, SL_MsgProcFuncReadSensor_Registration_request},
    {SL_Function_Code_Update_Profile,       SL_MsgProcFuncReadSensor_Update_Profile},
    {SL_Function_Code_Remote_Upgrade,       SL_MsgProcFuncReadSensor_Remote_Upgrade},
};

/* 智能模组发送下来的数据功能码处理表 */
SL_FuncionMsgProcTable g_MsgReadIntelligentModuleTbl[] =
{
    {SL_Function_Code_Read_Register,        SL_MsgProcFuncReadIntelligentModule_Read_Register},
    {SL_Function_Code_Write_Register,       SL_MsgProcFuncReadIntelligentModule_Write_Register},
    {SL_Function_Code_Broadcast_Scan,       SL_MsgProcFuncReadIntelligentModule_Broadcast_Scan},
    {SL_Function_Code_Registration_request, SL_MsgProcFuncReadIntelligentModule_Registration_request},
    {SL_Function_Code_Update_Profile,       SL_MsgProcFuncReadIntelligentModule_Update_Profile},
    {SL_Function_Code_Remote_Upgrade,       SL_MsgProcFuncReadIntelligentModule_Remote_Upgrade},
};

/**
  * @brief  校验算法
  * @param
  * @retval
  */
uint16_t CheckFunc(uint8_t *arr_buff, uint8_t len)
{
    uint16_t crc = 0xFFFF;
    uint16_t i, j;
    for (j = 0; j < len; ++j) {
        crc = crc ^ (*arr_buff++);
        for (i = 0; i < 8; ++i) {
            if ((crc&0x0001) > 0) {
                crc = crc >> 1;
                crc = crc ^ 0xa001;
            }
            else {
                crc = crc >> 1;
            }
        }
    }
    return crc;
}

/**
  * @brief  检测485总线是否繁忙
  * @param
  * @retval 1 繁忙
  *         0 空闲
  */
uint8_t Check_485_bus_busy(device_handle device)
{
    uart_device_info *device_info = (uart_device_info *)device;
    if((!device) || (!device_info->init))
       return 0;

    USART_ITConfig(device_info->uart_index, USART_IT_RXNE, ENABLE);

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

    USART_ITConfig(device_info->uart_index, USART_IT_RXNE, DISABLE);

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

void SL_MsgProcFuncReadSensor_Read_Register(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.device = g_Upward_UART5_handle;
//    MqUartSend_pack.Channel = ((uart_device_info *)device)->uart_num;
    MqUartSend_pack.len = MsgLen + 5;

    /* 包头 */
    MqUartSend_pack.recv_Data[0] = g_slConfigInfo.headPack.start_Flag[0];
    MqUartSend_pack.recv_Data[1] = g_slConfigInfo.headPack.start_Flag[1];
    if (device == g_J1RS485_UART6_handle) {
        MqUartSend_pack.recv_Data[2] = 6;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J2RS485_UART7_handle) {
        MqUartSend_pack.recv_Data[2] = 7;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J3RS485_USART2_handle) {
        MqUartSend_pack.recv_Data[2] = 2;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J4RS485_UART8_handle) {
        MqUartSend_pack.recv_Data[2] = 8;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J50RS485_USART3_handle) {
        MqUartSend_pack.recv_Data[2] = 3;
        MqUartSend_pack.recv_Data[3] = ((uart_device_info *)device)->uart_num;
    } else if (device == g_LORA_UART4_handle) {
        MqUartSend_pack.recv_Data[2] = 4;
        MqUartSend_pack.recv_Data[3] = 1;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen; ++var) {
        MqUartSend_pack.recv_Data[var + 4] = buf[var];
    }

    MqUartSend_pack.recv_Data[MsgLen + 4] = 0x17;

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Sensor_Read_Register \n ");
}

void SL_MsgProcFuncReadSensor_Registration_request(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.device = g_Upward_UART5_handle;
//    MqUartSend_pack.Channel = ((uart_device_info *)device)->uart_num;
    MqUartSend_pack.len = MsgLen + 5;

    /* 包头 */
    MqUartSend_pack.recv_Data[0] = g_slConfigInfo.headPack.start_Flag[0];
    MqUartSend_pack.recv_Data[1] = g_slConfigInfo.headPack.start_Flag[1];
    if (device == g_J1RS485_UART6_handle) {
        MqUartSend_pack.recv_Data[2] = 6;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J2RS485_UART7_handle) {
        MqUartSend_pack.recv_Data[2] = 7;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J3RS485_USART2_handle) {
        MqUartSend_pack.recv_Data[2] = 2;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J4RS485_UART8_handle) {
        MqUartSend_pack.recv_Data[2] = 8;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J50RS485_USART3_handle) {
        MqUartSend_pack.recv_Data[2] = 3;
        MqUartSend_pack.recv_Data[3] = ((uart_device_info *)device)->uart_num;
    } else if (device == g_LORA_UART4_handle) {
        MqUartSend_pack.recv_Data[2] = 4;
        MqUartSend_pack.recv_Data[3] = 1;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen; ++var) {
        MqUartSend_pack.recv_Data[var + 4] = buf[var];
    }

    MqUartSend_pack.recv_Data[MsgLen + 4] = 0x17;

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

//    rt_kprintf(" in Sensor_Registration_request \n ");
}


void SL_MsgProcFuncReadSensor_Update_Profile(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.device = g_Upward_UART5_handle;
//    MqUartSend_pack.Channel = ((uart_device_info *)device)->uart_num;
    MqUartSend_pack.len = MsgLen + 5;

    /* 包头 */
    MqUartSend_pack.recv_Data[0] = g_slConfigInfo.headPack.start_Flag[0];
    MqUartSend_pack.recv_Data[1] = g_slConfigInfo.headPack.start_Flag[1];
    if (device == g_J1RS485_UART6_handle) {
        MqUartSend_pack.recv_Data[2] = 6;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J2RS485_UART7_handle) {
        MqUartSend_pack.recv_Data[2] = 7;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J3RS485_USART2_handle) {
        MqUartSend_pack.recv_Data[2] = 2;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J4RS485_UART8_handle) {
        MqUartSend_pack.recv_Data[2] = 8;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J50RS485_USART3_handle) {
        MqUartSend_pack.recv_Data[2] = 3;
        MqUartSend_pack.recv_Data[3] = ((uart_device_info *)device)->uart_num;
    } else if (device == g_LORA_UART4_handle) {
        MqUartSend_pack.recv_Data[2] = 4;
        MqUartSend_pack.recv_Data[3] = 1;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen; ++var) {
        MqUartSend_pack.recv_Data[var + 4] = buf[var];
    }

    MqUartSend_pack.recv_Data[MsgLen + 4] = 0x17;

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Sensor_Update_Profile \n ");
}

void SL_MsgProcFuncReadSensor_Remote_Upgrade(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.device = g_Upward_UART5_handle;
//    MqUartSend_pack.Channel = ((uart_device_info *)device)->uart_num;
    MqUartSend_pack.len = MsgLen + 5;

    /* 包头 */
    MqUartSend_pack.recv_Data[0] = g_slConfigInfo.headPack.start_Flag[0];
    MqUartSend_pack.recv_Data[1] = g_slConfigInfo.headPack.start_Flag[1];
    if (device == g_J1RS485_UART6_handle) {
        MqUartSend_pack.recv_Data[2] = 6;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J2RS485_UART7_handle) {
        MqUartSend_pack.recv_Data[2] = 7;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J3RS485_USART2_handle) {
        MqUartSend_pack.recv_Data[2] = 2;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J4RS485_UART8_handle) {
        MqUartSend_pack.recv_Data[2] = 8;
        MqUartSend_pack.recv_Data[3] = 1;
    } else if (device == g_J50RS485_USART3_handle) {
        MqUartSend_pack.recv_Data[2] = 3;
        MqUartSend_pack.recv_Data[3] = ((uart_device_info *)device)->uart_num;
    } else if (device == g_LORA_UART4_handle) {
        MqUartSend_pack.recv_Data[2] = 4;
        MqUartSend_pack.recv_Data[3] = 1;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen; ++var) {
        MqUartSend_pack.recv_Data[var + 4] = buf[var];
    }

    MqUartSend_pack.recv_Data[MsgLen + 4] = 0x17;

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Sensor_Remote_Upgrade \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Read_Register(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.len = MsgLen - 6;

    SL_Mppt_Recv_pack1 *rpack = (SL_Mppt_Recv_pack1 *)pMsg;
    switch (rpack->head.uart) {
    case 2:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 3:
        MqUartSend_pack.device = g_J50RS485_USART3_handle;
        MqUartSend_pack.Channel = rpack->head.Channel;
        break;

    case 4:
        MqUartSend_pack.device = g_LORA_UART4_handle;
        break;

    case 6:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 7:
        MqUartSend_pack.device = g_J2RS485_UART7_handle;
        break;

    case 8:
        MqUartSend_pack.device = g_J4RS485_UART8_handle;
        break;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen - 1; ++var) {
        MqUartSend_pack.recv_Data[var] = buf[var + 4];
    }

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Read_Register \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Write_Register(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.len = MsgLen - 4;

    SL_Mppt_Recv_pack1 *rpack = (SL_Mppt_Recv_pack1 *)pMsg;
    switch (rpack->head.uart) {
    case 2:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 3:
        MqUartSend_pack.device = g_J50RS485_USART3_handle;
        MqUartSend_pack.Channel = rpack->head.Channel;
        break;

    case 4:
        MqUartSend_pack.device = g_LORA_UART4_handle;
        break;

    case 6:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 7:
        MqUartSend_pack.device = g_J2RS485_UART7_handle;
        break;

    case 8:
        MqUartSend_pack.device = g_J4RS485_UART8_handle;
        break;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen - 1; ++var) {
        MqUartSend_pack.recv_Data[var] = buf[var + 4];
    }

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Write_Register \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Broadcast_Scan(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.len = MsgLen - 4;

    SL_Mppt_Recv_pack1 *rpack = (SL_Mppt_Recv_pack1 *)pMsg;
    switch (rpack->head.uart) {
    case 2:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 3:
        MqUartSend_pack.device = g_J50RS485_USART3_handle;
        MqUartSend_pack.Channel = rpack->head.Channel;
        break;

    case 4:
        MqUartSend_pack.device = g_LORA_UART4_handle;
        break;

    case 6:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 7:
        MqUartSend_pack.device = g_J2RS485_UART7_handle;
        break;

    case 8:
        MqUartSend_pack.device = g_J4RS485_UART8_handle;
        break;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen - 1; var++) {
        MqUartSend_pack.recv_Data[var] = buf[var + 4];
    }

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Broadcast_Scan \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Registration_request(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.len = MsgLen - 4;

    SL_Mppt_Recv_pack1 *rpack = (SL_Mppt_Recv_pack1 *)pMsg;
    switch (rpack->head.uart) {
    case 2:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 3:
        MqUartSend_pack.device = g_J50RS485_USART3_handle;
        MqUartSend_pack.Channel = rpack->head.Channel;
        break;

    case 4:
        MqUartSend_pack.device = g_LORA_UART4_handle;
        break;

    case 6:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 7:
        MqUartSend_pack.device = g_J2RS485_UART7_handle;
        break;

    case 8:
        MqUartSend_pack.device = g_J4RS485_UART8_handle;
        break;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen - 1; ++var) {
        MqUartSend_pack.recv_Data[var] = buf[var + 4];
    }

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Registration_request \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Update_Profile(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.len = MsgLen - 4;

    SL_Mppt_Recv_pack1 *rpack = (SL_Mppt_Recv_pack1 *)pMsg;
    switch (rpack->head.uart) {
    case 2:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 3:
        MqUartSend_pack.device = g_J50RS485_USART3_handle;
        MqUartSend_pack.Channel = rpack->head.Channel;
        break;

    case 4:
        MqUartSend_pack.device = g_LORA_UART4_handle;
        break;

    case 6:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 7:
        MqUartSend_pack.device = g_J2RS485_UART7_handle;
        break;

    case 8:
        MqUartSend_pack.device = g_J4RS485_UART8_handle;
        break;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen - 1; ++var) {
        MqUartSend_pack.recv_Data[var] = buf[var + 4];
    }

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Update_Profile \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Remote_Upgrade(device_handle device, void *pMsg, uint32_t MsgLen)
{
    memset(&MqUartSend_pack, 0, sizeof(MqUartSend_pack));
    MqUartSend_pack.len = MsgLen - 4;

    SL_Mppt_Recv_pack1 *rpack = (SL_Mppt_Recv_pack1 *)pMsg;
    switch (rpack->head.uart) {
    case 2:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 3:
        MqUartSend_pack.device = g_J50RS485_USART3_handle;
        MqUartSend_pack.Channel = rpack->head.Channel;
        break;

    case 4:
        MqUartSend_pack.device = g_LORA_UART4_handle;
        break;

    case 6:
        MqUartSend_pack.device = g_J3RS485_USART2_handle;
        break;

    case 7:
        MqUartSend_pack.device = g_J2RS485_UART7_handle;
        break;

    case 8:
        MqUartSend_pack.device = g_J4RS485_UART8_handle;
        break;
    }

    uint8_t *buf = (uint8_t *)pMsg;
    for (uint8_t var = 0; var < MsgLen - 1; ++var) {
        MqUartSend_pack.recv_Data[var] = buf[var + 4];
    }

    /* 发 送 消 息 到 消 息 队 列 中 */
    if (rt_mq_send(mqSend, &MqUartSend_pack, sizeof(MqUartSend_pack)) != RT_EOK) {
        rt_kprintf("rt_mq_send ERR\n");
    }

    rt_kprintf(" in Remote_Upgrade \n ");
}


/**
  * @brief  匹配起始标志"SL"
  * @param  start_buff  起始标志
  * @retval 1 匹配成功
  *         0 匹配失败
  */
static int Match_Startflag(uint8_t start_buff[2])
{
    if ((start_buff[0] == g_slConfigInfo.start_Flag[0]) && \
            (start_buff[1] == g_slConfigInfo.start_Flag[1])) {
        return 1;
    }
    return 0;
}

/**
  * @brief  读取传感器串口数据
  * @param  uart_handle 串口句柄
  * @param  buff        缓冲区
  * @param  buff_size   缓冲区长度
  * @retval
  */
static int uart_read_sensor_pack(device_handle uart_handle,uint8_t *buff, uint32_t buff_size)
{
    SL_Mppt_Recv_pack *pack = (SL_Mppt_Recv_pack *)buff;

    uint32_t offset = 0;
    uint32_t len = 0;
    uint8_t flag_run = 0;

    char c = 0;

    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 == readSensorAnalyzeStartFlag || (flag_run > 0)) {
            if (!Match_Startflag(pack->start_Flag)) {
                memcpy(buff, buff+1, offset-1);
                offset--;
                continue;
            }
//            rt_kprintf("start_Flag success \n");
        }

        /* 匹配功能码  */
        if ((offset == readSensorAnalyzeFunctionCode) || (flag_run > 2)) {
            /* 读寄存器数据 */
            if (pack->function_Code == SL_Function_Code_Read_Register) {
            }

            /* 注册请求 */
            else if ((pack->function_Code == SL_Function_Code_Registration_request)) {
                len = SL_MPPT_REGISTRATIONREQUEST_PACK_SIZE;
            }

            /* 远程升级或配置文件更改 */
            else if ((pack->function_Code == SL_Function_Code_Update_Profile)
                    || (pack->function_Code == SL_Function_Code_Remote_Upgrade)) {
                len = SL_MPPT_ROTHER_PACK_SIZE;
            }

            else {
                if (flag_run < 2) {
                    flag_run = 2;
                }
//                rt_kprintf("function_Code error : %x \n", pack->function_Code);
                memcpy(buff, buff+1, offset-1);
                offset--;
                continue;
            }
        }

        if ((offset == 12) && (pack->function_Code == SL_Function_Code_Read_Register)) {
            SL_Mppt_ROReply_pack *rpack = (SL_Mppt_ROReply_pack *)buff;
            uint8_t Register_Number = (rpack->reply_Register_Number_H << 8) | rpack->reply_Register_Number_L;
//            rt_kprintf("Register_Number : %d \n", Register_Number);
            len = Register_Number * 2 + SL_MPPT_ROReply_PACK_SIZE - 4;
            continue;
        }

        if (offset == len) {

             uint16_t crc_16 = buff[offset - 2] | (buff[offset - 3] << 8);

             if ((CheckFunc(buff, offset - 3) != crc_16)  || (buff[offset - 1] != 0x16)) {
                 if (flag_run < 3) {
                     flag_run = 3;
                 }
//                 rt_kprintf("crc: %x, end_Flag: %x \n", CheckFunc(buff, offset - 3), buff[offset - 1]);
                 memcpy(buff, buff+1, offset-1);
                 offset--;
             } else {
//                 rt_kprintf("success return offset : %d \n", offset);
                 return offset;
             }

        }
    }
    return 0;
}

///**
//  * @brief  读取智能模块串口数据
//  * @param  uart_handle 串口句柄
//  * @param  buff        缓冲区
//  * @param  buff_size   缓冲区长度
//  * @retval
//  */
//static int uart_read_intelligentModule_pack(device_handle uart_handle,uint8_t *buff, uint32_t buff_size)
//{
//    SL_Mppt_Recv_pack1 *pack = (SL_Mppt_Recv_pack1 *)buff;
//
//    uint32_t offset = 0;
//    uint32_t len = 0;
//    uint8_t flag_run = 0;
//
//    char c = 0;
//
//    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 == sizeof(SL_Head_pack)) {
////
////            }
//
//            /* 匹配包头 */
//            if ((offset == readIntelligentModuleAnalyzeHeader) || (flag_run > 0)) {
//                if (!Match_Startflag(pack->head.start_Flag)) {
////                    rt_kprintf("header error1 : %s \n", pack->head.start_Flag);
//                    memcpy(buff, buff+1, offset-1);
//                    offset--;
//                    continue;
//                }
//
//                if (!((pack->head.uart > 0) && (pack->head.uart < 9))) {
////                    rt_kprintf("header error2 : %d \n", pack->head.uart);
//                    memcpy(buff, buff+1, offset-1);
//                    offset--;
//                    continue;
//                }
//
//                if (!(((pack->head.uart == 3) && ((pack->head.Channel == 0) ||
//                        (pack->head.Channel < 10 && pack->head.Channel > 4)))
//                        || (pack->head.uart != 3))) {
////                    rt_kprintf("header error3 : %d \n", pack->head.Channel);
//                    memcpy(buff, buff+1, offset-1);
//                    offset--;
//                    continue;
//                }
//
////                rt_kprintf("header success \n");
//            }
//
//            /* 匹配起始标志位 */
//            if (offset == readIntelligentModuleAnalyzeStartFlag || (flag_run > 1)) {
//                if (!Match_Startflag(pack->rpack.start_Flag)) {
//                    if (flag_run < 1) {
//                        flag_run = 1;
//                    }
//                    memcpy(buff, buff+1, offset-1);
//                    offset--;
//                    continue;
//                }
//
////                rt_kprintf("start_Flag success \n");
//            }
//
//            /* 匹配功能码  */
//            if ((offset == readIntelligentMOduleAnalyzeFunctionCode) || (flag_run > 2)) {
//                /* 读寄存器数据 */
//                if (pack->rpack.function_Code == SL_Function_Code_Read_Register) {
//                    len = SL_MPPT_RORECV_PACK_SIZE1;
//                }
//
//                /* 写寄存器数据 */
//                else if (pack->rpack.function_Code == SL_Function_Code_Write_Register) {
////                    len = SL_MPPT_RORECV_PACK_SIZE1;
//                }
//
//                /* 广播请求 */
//                else if (pack->rpack.function_Code == SL_Function_Code_Broadcast_Scan) {
//                    len = SL_MPPT_SCAN_BROADCAST_PACK_SIZE1;
//                }
//
//                /* 注册回复 */
//                else if ((pack->rpack.function_Code == SL_Function_Code_Registration_request)) {
//                    len = SL_MPPT_REGISTRATIONREPLY_PACK_SIZE1;
//                }
//
//                /* 远程升级或配置文件更改 */
//                else if ((pack->rpack.function_Code == SL_Function_Code_Update_Profile)
//                        || (pack->rpack.function_Code == SL_Function_Code_Remote_Upgrade)) {
//                    len = SL_MPPT_SOTHER_PACK_SIZE1;
//                }
//
//                else {
//                    if (flag_run < 2) {
//                        flag_run = 2;
//                    }
//
////                    rt_kprintf("function_Code error : %x \n", pack->rpack.function_Code);
//                    memcpy(buff, buff+1, offset-1);
//                    offset--;
//                    continue;
//                }
//            }
//
//            if ((offset == 18) && (pack->rpack.function_Code == SL_Function_Code_Write_Register)) {
//                SL_Mppt_WOReply_pack1 *rpack = (SL_Mppt_WOReply_pack1 *)buff;
//                uint16_t Register_Number = (rpack->write_Register_Number_H << 8) | rpack->write_Register_Number_L;
////                rt_kprintf("Register_Number : %d \n", Register_Number);
//                len = Register_Number * 2 + 22;
//                continue;
//            }
//
//            if (offset == len) {
//                uint16_t crc_16 = buff[offset - 3] | (buff[offset - 4] << 8);
//
//                if ((CheckFunc(buff + 4, offset - 8) != crc_16)  || (buff[offset - 2] != 0x16) || (buff[offset - 1] != 0x17)) {
//                    if (flag_run < 3) {
//                        flag_run = 3;
//                    }
////                    rt_kprintf("crc: %x, end_Flag: %x, tail.end_Flag: %x \n", CheckFunc(buff + 4, offset - 8), buff[offset - 2], buff[offset - 1]);
//                    memcpy(buff, buff+1, offset-1);
//                    offset--;
//                } else {
////                    rt_kprintf("success return offset : %d \n", offset);
//                    return offset;
//                }
//            }
//    }
//    return 0;
//}

/**
  * @brief  读取智能模块串口数据
  * @param  uart_handle 串口句柄
  * @param  buff        缓冲区
  * @param  buff_size   缓冲区长度
  * @retval
  */
static int uart_read_intelligentModule_pack(device_handle uart_handle,uint8_t *buff, uint32_t buff_size)
{
    SL_Mppt_Recv_pack1 *pack = (SL_Mppt_Recv_pack1 *)buff;

    uint32_t offset = 0;
    uint32_t len = 0;
    uint8_t flag_run = 0;

    char c = 0;

    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 == HEAD_PACK_LEN) || (flag_run > 0)) {
            len = (pack->head.lengH << 8 | pack->head.lengL) + HEAD_PACK_LEN + 1;
        }

        if (offset == len) {
            if (buff[len - 1] == g_slConfigInfo.end_Flag) {
                return offset;
            } else {
                flag_run = 1;
                memcpy(buff, buff+1, offset-1);
                offset--;
            }
        }
    }
    return 0;
}

/**
  * @brief  处理一条传感器消息
  * @param
  * @retval
  *
  */
void FRT_SensorMsgHandler(device_handle device, uint8_t *pMsg, uint32_t MsgLen)
{
    SL_Mppt_Recv_pack *pack = (SL_Mppt_Recv_pack *)pMsg;

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

/**
  * @brief  处理智能模块一条消息
  * @param
  * @retval
  */
void FRT_IntelligentModuleMsgHandler(device_handle device, uint8_t *pMsg, uint32_t MsgLen)
{
    SL_Mppt_Recv_pack1 *pack = (SL_Mppt_Recv_pack1 *)pMsg;

    for (u_int16_t i = 0; i < sizeof(g_MsgReadIntelligentModuleTbl) / sizeof(SL_FuncionMsgProcTable); i++) {
        if (pack->rpack.function_Code == g_MsgReadIntelligentModuleTbl[i].msgId) {
//            rt_kprintf("function_Code error : %x \n", pack->rpack.function_Code);
            g_MsgReadIntelligentModuleTbl[i].pMsgProc(device, pMsg, MsgLen);
        }
    }
}


/**
  * @brief  读取并解析传感器串口数据
  * @param
  * @retval
  */
void read_and_process_sensorUart_data(device_handle device)
{
    if (uart_dev_char_present(device)) {
        rt_thread_mdelay(30);
//        rt_kprintf("ring_queue_length = %d \n", ring_queue_length(device));
        memset(uart_buff, 0, sizeof(uart_buff));
        int ret = uart_read_sensor_pack(device, uart_buff, sizeof(uart_buff));
        if(ret > 0){
            FRT_SensorMsgHandler(device, uart_buff, ret);
        }
    }
}

/**
  * @brief  读取并解析智能模组串口数据
  * @param
  * @retval
  */
void read_and_process_intelligentModuleUart_data(device_handle device)
{
    if (uart_dev_char_present(device)) {
        rt_thread_mdelay(10);
//        rt_kprintf("ring_queue_length = %d \n", ring_queue_length(device));
        memset(uart_buff, 0, sizeof(uart_buff));
        int ret = uart_read_intelligentModule_pack(device, uart_buff, sizeof(uart_buff));
        if(ret > 0){
            FRT_IntelligentModuleMsgHandler(device, uart_buff, ret);
        }
    }
}

/**
  * @brief  发送串口数据
  * @param
  * @retval
  */
void send_uart_data(uint8_t *buff)
{
    SL_UartSend_pack *upack = (SL_UartSend_pack *)buff;

    if (upack->device == g_J50RS485_USART3_handle) {
        if (upack->Channel == 5) {
            USART_CONNET_J5();
        } else if (upack->Channel == 6) {
            USART_CONNET_J6();
        } else if (upack->Channel == 7) {
            USART_CONNET_J7();
        } else if (upack->Channel == 8) {
            USART_CONNET_J8();
        } else if (upack->Channel == 9) {
            USART_CONNET_J9();
        } else {
            USART_CONNET_J0();
        }
    }

    for (int var = 0; var < upack->len; ++var) {
        rt_kprintf("%x ", upack->recv_Data[var]);
    }

    rt_kprintf("\n");
    uart_dev_write(upack->device, upack->recv_Data, upack->len);
}



