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

#ifndef SOFTWARE_PROTOCOL_COMMUNICATION_PROTOCOL_H_
#define SOFTWARE_PROTOCOL_COMMUNICATION_PROTOCOL_H_

#include "inc/UART.h"
#include "uart_dev.h"


/* 功能码 */
typedef enum
{
   SL_Function_Code_Read_Register           =   0x30,  /* 读寄存器数据 */
   SL_Function_Code_Write_Register          =   0x10,  /* 写寄存器数据 */
   SL_Function_Code_Broadcast_Scan          =   0xA0,  /* 广播扫描 */
   SL_Function_Code_Registration_request    =   0xA1,  /* 注册请求 */
}SL_MsgFunctionCode;

/* 寄存器地址 */
typedef enum
{
   SL_Register_Registration_Status                      = 0x0000,   /* 注册状态  */
   SL_Register_address                                  = 0x0001,   /* 地址  */
   SL_Register_Access_Node_Type                         = 0x0002,   /* 接入节点类型   */
   SL_Register_Communication_Methods                    = 0x0003,   /* 通信方式   */

   SL_Register_Battery_Voltage                          = 0x0100,   /* 电池电压   */
   SL_Register_Battery_temperature                      = 0x0101,   /* 电池温度   */
   SL_Register_Remaining_Battery_Bower                  = 0x0102,   /* 电池剩余电量   */
   SL_Register_Solar_Open_Circuit_Voltage1              = 0x0103,   /* 太阳能开路电压1 */
   SL_Register_Solar_Open_Circuit_Voltage2              = 0x0104,   /* 太阳能开路电压2 */

   SL_Register_Ambient_Temperature                      = 0x0200,   /* 环境温度 */
   SL_Register_Ambient_Humidity                         = 0x0201,   /* 环境湿度 */
   SL_Register_Barometric_Pressure                      = 0x0202,   /* 大气压 */
   SL_Register_Wind_Speed                               = 0x0203,   /* 风速 */
   SL_Register_Wind_Direction                           = 0x0204,   /* 风向 */
   SL_Register_Rainfall                                 = 0x0205,   /* 雨量 */
   SL_Register_Optical_Radiation                        = 0x0206,   /* 光辐射 */

   SL_Register_Leakage_Current_Value                    = 0x0220,   /* 泄漏电流值 */
   SL_Register_Leakage_Current_MaxValue                 = 0x0221,   /* 最大泄漏电流值 */
   SL_Register_Pulse_Frequency                          = 0x0222,   /* 脉冲次数 */
   SL_Register_Zero_Leakage_Current_MaxValue_Statistics = 0x0223,   /* 清零最大泄漏电流值统计 */
   SL_Register_Zero_Pulse_Frequency_Statistics          = 0x0224,   /* 清零脉冲次数统计 */

   SL_Register_Inclination_AngleX                       = 0x0230,   /* X轴倾角 */
   SL_Register_Inclination_AngleY                       = 0x0231,   /* Y轴倾角 */
   SL_Register_Zero_Inclination_Angle                   = 0x0232,   /* 校零倾角 */
}SL_MsgRegister;

/* 注册状态 */
typedef enum
{
   UNREGISTER = 0,          /* 未注册 */
   REGISTER_FAIL = 1,       /* 注册失败 */
   REGISTER_SUCCESS = 2,    /* 注册成功 */
}SL_REGISTERSTATUS;

/* 接入节点类型 */
typedef enum
{
   POWERBOX = 1,            /* 电源箱子 */
   MICROMETEOROLOGY = 2,    /* 微气象 */
}SL_ACCESSNODETYPE;

/* 通信方式 */
typedef enum
{
    RS485 = 1,
    RJ45 = 2,
}SL_COMMUNICATIONMETHODS;

/* 指定对齐方式为1字节 */
#pragma pack(push,1)

/* 读时发送的帧格式(汇聚网关->接入节点) */
typedef struct _SL_Mppt_Rorecv_pack{
    uint8_t start_Flag[2];                  /* 起始标志  */
    uint8_t address[7];                     /* 地址 */
    uint8_t function_Code;                  /* 功能码 */
    uint8_t read_Register_Start_Address_H;  /* 读取寄存器起始地址高8位 */
    uint8_t read_Register_Start_Address_L;  /* 读取寄存器起始地址低八位 */
    uint8_t read_Register_Number_H;         /* 读取寄存器个数高八位 */
    uint8_t read_Register_Number_L;         /* 读取寄存器个数低八位 */
    uint8_t check_Bit_H;                    /* 校验位高八位 */
    uint8_t check_Bit_L;                    /* 校验位低八位 */
    uint8_t end_Flag;                       /* 结束标志 */
}SL_Mppt_Rorecv_pack;
#define SL_MPPT_RORECV_PACK_SIZE        (sizeof(SL_Mppt_Rorecv_pack))

/* 读时接收到的回复帧格式(接入节点->汇聚网关) */
typedef struct _SL_Mppt_ROReply_pack{
    uint8_t start_Flag[2];                  /* 起始标志  */
    uint8_t address[7];                     /* 地址 */
    uint8_t function_Code;                  /* 功能码 */
    uint8_t reply_Register_Number_H;        /* 回复字节数长度高八位  */
    uint8_t reply_Register_Number_L;        /* 回复字节数长度低八位  */
    uint8_t *content;                       /* 回复数据内容 */
    uint8_t check_Bit_H;                    /* 校验位高八位 */
    uint8_t check_Bit_L;                    /* 校验位低八位 */
    uint8_t end_Flag;                       /* 结束标志 */
}SL_Mppt_ROReply_pack;
#define SL_MPPT_ROReply_PACK_SIZE        (sizeof(SL_Mppt_ROReply_pack))

/* 扫描广播帧F1(汇聚网关->接入节点) */
typedef struct _SL_Mppt_ScanBroadcast_pack{
    uint8_t start_Flag[2];                  /* 起始标志  */
    uint8_t address[7];                     /* 地址 */
    uint8_t function_Code;                  /* 功能码 */
    uint8_t check_Bit_H;                    /* 校验位高八位 */
    uint8_t check_Bit_L;                    /* 校验位低八位 */
    uint8_t end_Flag;                       /* 结束标志 */
}SL_Mppt_Scan_Broadcast_pack;
#define SL_MPPT_SCAN_BROADCAST_PACK_SIZE    (sizeof(SL_Mppt_Scan_Broadcast_pack))

/* 注册请求帧F2(接入节点->汇聚网关) */
typedef struct _SL_Mppt_RegistrationRequest_pack{
    uint8_t start_Flag[2];              /* 起始标志  */
    uint8_t address[7];                 /* 地址 */
    uint8_t function_Code;              /* 功能码 */
    uint8_t register_Length_H;          /* 寄存器长度高八位 */
    uint8_t register_Length_L;          /* 寄存器长度低八位 */
    uint8_t registration_Status_H;      /* 注册状态高八位  */
    uint8_t registration_Status_L;      /* 注册状态低八位 */
    uint8_t access_Node_ID[7];          /* 接入节点ID */
    uint8_t access_Node_Type_H;         /* 接入节点类型高八位 */
    uint8_t access_Node_Type_L;         /* 接入节点类型低八位 */
    uint8_t check_Bit_H;                /* 校验位高八位 */
    uint8_t check_Bit_L;                /* 校验位低八位 */
    uint8_t end_Flag;                   /* 结束标志 */
}SL_Mppt_RegistrationRequest_pack;
#define SL_MPPT_REGISTRATIONREQUEST_PACK_SIZE    (sizeof(SL_Mppt_RegistrationRequest_pack))

/* 注册回复帧F3(汇聚网关->接入节点) */
typedef struct _SL_Mppt_RegistrationReply_pack{
    uint8_t start_Flag[2];              /* 起始标志  */
    uint8_t address[7];                 /* 地址 */
    uint8_t function_Code;              /* 功能码 */
    uint8_t register_Length_H;          /* 寄存器长度高八位 */
    uint8_t register_Length_L;          /* 寄存器长度低八位 */
    uint8_t registration_Status_H;      /* 注册状态高八位  */
    uint8_t registration_Status_L;      /* 注册状态低八位 */
    uint8_t check_Bit_H;                /* 校验位高八位 */
    uint8_t check_Bit_L;                /* 校验位低八位 */
    uint8_t end_Flag;                   /* 结束标志 */
}SL_Mppt_RegistrationReply_pack;
#define SL_MPPT_REGISTRATIONREPLY_PACK_SIZE (sizeof(SL_Mppt_RegistrationReply_pack))

/*  */
#define RECV_LENGTH 20
typedef struct _SL_Mppt_Recv_pack{
    uint8_t  start_Flag[2];                 /* 起始标志  */
    uint8_t  address[7];                    /* 地址 */
    uint8_t  function_Code;                 /* 功能码 */
    uint8_t  recv_Data[RECV_LENGTH];        /* 其他部分 */
}SL_Mppt_Recv_pack;

/* 功能码处理函数 */
typedef void (*MsgProcFunc)(device_handle device, void*, uint32_t MsgLen);
typedef struct _SL_FunctionMsgProcTable{
    u_int32_t    msgId;
    MsgProcFunc  pMsgProc;
}SL_FuncionMsgProcTable;

/* 恢复默认的对齐设置 */
#pragma pack(pop)

extern SL_Mppt_Scan_Broadcast_pack g_Scan_Broadcast_pack;

void read_and_process_uart_data(device_handle device);
void send_data(uint8_t *buff);

#endif /* SOFTWARE_PROTOCOL_COMMUNICATION_PROTOCOL_H_ */