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

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



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

#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 = device;
//    MqUartSend_pack.len = MsgLen;
//
//    uint8_t *buf = (uint8_t *)pMsg;
//    for (uint8_t var = 0; var < MsgLen; ++var) {
//        MqUartSend_pack.recv_Data[var] = buf[var];
//    }
//
//    /* 发 送 消 息 到 消 息 队 列 中 */
//    if (rt_mq_send(mqSend, &MqUartSend_pack, MsgLen) != 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 = device;
//    MqUartSend_pack.len = SL_MPPT_REGISTRATIONREPLY_PACK_SIZE;
//
////    uint8_t *buf = (uint8_t *)pMsg;
////    for (uint8_t var = 0; var < MsgLen; ++var) {
////        MqUartSend_pack.recv_Data[var] = buf[var];
////    }
//
//    SL_Mppt_RegistrationReply_pack *rrpack = (SL_Mppt_RegistrationReply_pack *)MqUartSend_pack.recv_Data;
//    SL_Mppt_RegistrationRequest_pack *rpack = (SL_Mppt_RegistrationRequest_pack *)pMsg;
//
//    rrpack->start_Flag[0] = rpack->start_Flag[0];
//    rrpack->start_Flag[1] = rpack->start_Flag[1];
//
//    rrpack->address[0] = rpack->access_Node_ID[0];
//    rrpack->address[1] = rpack->access_Node_ID[1];
//    rrpack->address[2] = rpack->access_Node_ID[2];
//    rrpack->address[3] = rpack->access_Node_ID[3];
//    rrpack->address[4] = rpack->access_Node_ID[4];
//    rrpack->address[5] = rpack->access_Node_ID[5];
//    rrpack->address[6] = rpack->access_Node_ID[6];
//    rrpack->address[7] = rpack->access_Node_ID[7];
//
//    rrpack->function_Code = rpack->function_Code;
//
//    rrpack->register_Length_H = rpack->register_Length_H;
//    rrpack->register_Length_L = rpack->register_Length_L;
//
//    rrpack->registration_Status_H = 0x00;
//    rrpack->registration_Status_L = 0x02;
//
//    uint16_t crc_16 = CheckFunc(MqUartSend_pack.recv_Data, SL_MPPT_REGISTRATIONREPLY_PACK_SIZE - 3);
//    rrpack->check_Bit_H = crc_16 >> 8;
//    rrpack->check_Bit_L = crc_16;
//
//    rrpack->end_Flag = 0x16;
//
//    /* 发 送 消 息 到 消 息 队 列 中 */
//    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)
{

    rt_kprintf(" in Sensor_Update_Profile \n ");

}

void SL_MsgProcFuncReadSensor_Remote_Upgrade(device_handle device, void *pMsg, uint32_t MsgLen)
{

    rt_kprintf(" in Sensor_Remote_Upgrade \n ");

}

void SL_MsgProcFuncReadIntelligentModule_Read_Register(device_handle device, void *pMsg, uint32_t MsgLen)
{
    rt_kprintf(" in Read_Register \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Write_Register(device_handle device, void *pMsg, uint32_t MsgLen)
{
    rt_kprintf(" in Write_Register \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Broadcast_Scan(device_handle device, void *pMsg, uint32_t MsgLen)
{
    rt_kprintf(" in Broadcast_Scan \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Registration_request(device_handle device, void *pMsg, uint32_t MsgLen)
{
    rt_kprintf(" in Registration_request \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Update_Profile(device_handle device, void *pMsg, uint32_t MsgLen)
{
    rt_kprintf(" in Update_Profile \n ");
}

void SL_MsgProcFuncReadIntelligentModule_Remote_Upgrade(device_handle device, void *pMsg, uint32_t MsgLen)
{
    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
  * @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;

    uart_dev_write(upack->device, upack->recv_Data, upack->len + 1);
}