#ifndef __CA_CSG_H__
#define __CA_CSG_H__

#ifdef CFG_DEV_TYPE_LAND_CA
/* Includes ------------------------------------------------------------------*/
#include "ca_main.h"
#include <netinet/in.h>
#include <arpa/inet.h>

/* Define --------------------------------------------------------------------*/

#define CSG_PKT_LEN (2048)

#define GATEWAY_LEN         (4)
#define START_FLAG          (0X5555AAAA)
#define END_FLAG            (0X5AA55AA5)
#define LandPackHeadTailLen (28) //包头加包尾长度

#define Cmd_ForceReqID              (0xA401)//强制请求从机的信息（ID）：【不判断ID匹配，用于识别ID】
#define Res_ForceReqID              (0xC401)
#define Cmd_ReqRTD2                 (0xA081)//召唤业务数据
#define Res_ReqRTD2                 (0xC081)
#define Cmd_ReqUTC                  (0xA090)//查询下位机时间
#define Res_ReqUTC                  (0xC090)
#define Cmd_SetUTC                  (0xA091)//设置下位机时间
#define Res_SetUTC                  (0xC091)
#define Cmd_ReadRTD_Flash2          (0xA094)//读最近的N条RTD数据
#define Res_ReadRTD_Flash2          (0xC094)
#define Cmd_ReadRTD_FlashLoss2      (0xA095)//读最近的N条RTD数据
#define Res_ReadRTD_FlashLoss2      (0xC095)
#define Cmd_ReqEarthBoxRTD          (0xA083)    //接地箱实时数据
#define Res_ReqEarthBoxRTD          (0xC083)    //

#define Cmd_SwitchOutPut      (0xA084)  //控制继电器输出
#define Res_SwitchOutPut      (0xC084)

#define Cmd_LfManualWaveTrig        (0xA071)//手动触发工频录波
#define Res_LfManualWaveTrig        (0xC071)
#define Cmd_ReqLfRecWaveStatus      (0xA072)//召唤工频录波的全部数据（分包送所有包）     
#define Res_ReqLfRecWaveStatus      (0xC072)
#define Cmd_ReqLfRecWaveLoss        (0xA073)//工频录波的数据补包
#define Res_ReqLfRecWaveLoss        (0xC073)

#define Cmd_ReqSysCfg        (0xA074)//查询装置系统配置
#define Res_ReqSysCfg        (0xC074)
#define Cmd_SetSysCfg        (0xA075)//设置装置系统配置
#define Res_SetSysCfg        (0xC075)
#define Cmd_ReqDevCfg        (0xA076)//查询装置设备配置
#define Res_ReqDevCfg        (0xC076)
#define Cmd_SetDevCfg        (0xA077)//设置装置设备配置
#define Res_SetDevCfg        (0xC077)
#define Cmd_ReqCauCfg        (0xA078)//查询装置CAU配置
#define Res_ReqCauCfg        (0xC078)
#define Cmd_SetCauCfg        (0xA079)//设置装置CAU配置
#define Res_SetCauCfg        (0xC079)

#define Cmd_CameraTrig        (0xA07a)//手动触发摄像头拍照
#define Res_CameraTrig        (0xC07a)
#define Cmd_ReqPhotePack      (0xA07b)//召唤照片数据
#define Res_ReqPhotePack      (0xC07b)
#define Cmd_ReqPhotePackLoss  (0xA07c)//召唤照片数据
#define Res_ReqPhotePackLoss  (0xC07c)

#define Cmd_Reboot            (0xA07E)//重启
#define Res_Reboot            (0xC07E)
#define Cmd_AppUpdate         (0xA07F)//升级
#define Res_AppUpdate         (0xC07F)



#define Cmd_HfManualWaveTrig        (0xA098)//手动触发高频采集
#define Res_HfManualWaveTrig        (0xC098)
#define Cmd_ReqHfRecWaveStatus      (0xA099)//查询高频采集的完成状态  【朗德上送废弃】
#define Res_ReqHfRecWaveStatus      (0xC099)
#define Cmd_ReqFfRecWaveFull        (0xA09a)//召唤高频采集的全部数据（连续发送所有包）
#define Res_ReqFfRecWaveFull        (0xC09a)
#define Cmd_ReqFfRecWaveLoss        (0xA09b)//召唤高频采集的补报（连续发送所请求的N包）
#define Res_ReqFfRecWaveLoss        (0xC09b)

/* Exported types ------------------------------------------------------------*/

typedef struct{
    U32 Head;//StartFlag1 0X5555AAAA
    U16 Len;//报文长度：所有字节总长度
    U16 Type;//报文类型
    U32 ID;//从机ID: 【ex. 0x40111234 】
    U32 LifeCnt;//主机每次加1，从机应答收到的值
    U16 gNum;//报文分组数目:（1-65535）；
    U16 gNumIndex;//报文分组编号；0 ～ （gNum－1）
}LandPkHead;//朗德协议包头

typedef struct{
    U32 CheckSum;
    U32 EndFlag1;
}LandPkTail;//朗德协议包尾


typedef struct
{
    int fd;                                     // 后台通讯使用的 socket.
    uint32_t pkt_index;                         // 报文索引.
    int32_t data_fifo_id;                       // 数据处理线程发向后台发送线程的 fifo.
    char buf_send[CSG_PKT_LEN];
    //char buf_reply[CSG_PKT_LEN];
    //char buf_recv[CSG_PKT_LEN];
    char buf[CSG_PKT_LEN];
    struct sockaddr_in server;
    int32_t server_ip;                          // server ip.
    uint16_t server_port;                       // server port.
    uint8_t is_connect;                         // 是否连接上服务器.
    uint8_t heartbeat;                          // 心跳发送后没有收到回复的次数.
    int32_t communication_time;                 // 最后通讯时间.
    pthread_mutex_t mutex;
} csg_t;

typedef struct 
{
    unsigned int utc;
    unsigned int resverd[8];
} csg_utc_t;

typedef struct
{
    unsigned int len;
    unsigned char data[1020];
} csg_upgrade_t;

typedef struct
{
    unsigned int status;
    unsigned int resverd[8];
} csg_upgrade_response_t;

/* Exported macro ------------------------------------------------------------*/

/* Extern global variables ---------------------------------------------------*/
extern csg_t csg;

/* Extern functions ----------------------------------------------------------*/
extern int32_t csg_handle_init(void);
#endif
#endif
