#ifndef UWATER_FRAME_FUNC_REPLAY_GUANGSHUI_H
#define	UWATER_FRAME_FUNC_REPLAY_GUANGSHUI_H

#include "sys_config.h"
#ifdef PRODUCT_GS


#define FRAME_HEAD_JM         0x69       	//帧头
#define FRAME_END_JM          0x17       	//帧尾本号
#define FRAME_MARK_JM         0xb1       	//帧标识
#define FRAME_TYPE_JM         0x14         //版本

#define FRAME_LEN_MAX       UP_BUF_SIZE
#define FRAME_LEN_MIN       10

#define  GS_HIS_STORE_MAX   540 //历史数据存储条数,默认540/5=108天

#define EEP_ADDR_CIRCLE_DATA  FRAME_NBIot_HISTORY_READ_ADD  //周期上报历史数据存储地址
#define EEP_ADDR_HIS_DATA     EEP_RTCC_APP_HISTORY_ADDR     //数据查询历史数据存储地址
#define EEP_ADDR_HIS_INDEX    (EEP_ADDR_HIS_DATA+GS_HIS_STORE_MAX*5)	//数据查询历史数据指针存储地址 0x440+0xA8C=0xECC~ED2(6字节)
#define EEP_ADDR_TEMP_STORE_UPLOAD   (EEP_ADDR_HIS_INDEX+6) //临时存储和上报参数地址(8字节)
//周期实时数据组帧
typedef union  //强制转化为浮点变量
{
    u32   gp22_u32;
    float gp22_float;
    u8 buf[4];
}U32_To_Float;

typedef struct //存放表计收到的比较重要的数据
{
    u8 locn;
    mtype16_ len;		
    mtype16_ serial_num;  
    u8 frame_ctrl;
    u8 STA;					
    mtype16_ data_len;   	
    u8 crc;
} STRUCT_FRAME_GS;

//数据存储指针
typedef union 
{
	struct
	{
		UNION_4U8TOU32 Index_w;
		UNION_2U8TOU16 Period;
	}Data;
	u8 data_buf[6];
}UNION_GS_STORE_INDEX;

#define STRU_DATA_LEN_GS           157 //广水历史数据区上报长度
#define STRU_DATA_LEN_GS_CURRENT   16  //广水当前数据区上报长度

typedef union {
	struct 
	{
		u8 Datetime[6];
		u8 StepCnt;
		u8 Step[2];
		u8 DataCode;
		u8 StepFlow1[6];
		u8 StepFlowN[47][3];
	}data;
	u8 buf[STRU_DATA_LEN_GS];
} STRU_UP_FRAME_GS; 

//设置主动上报参数
typedef union 
{
	struct
	{
		u8 start_time_h;
		u8 start_time_m;
		UNION_2U8TOU16 up_interval;//上发间隔分钟
		UNION_2U8TOU16 data_interval;//数据间隔分钟
		UNION_2U8TOU16 effective_time;
	}Data;
	u8 data_buf[8];
}UNION_GS_UPLOAD_PARA;
//////////////////////////////////////////规约控制字/////////////////////////////////////////////////////////
/*NB新增通信规约*/
//深水读取历史数据
#define SS_READ_HIS_CTRL                        0x27     
#define SS_ACK_READ_HIS_CTRL                    0xa7
#define SS_READ_HIS_CTRL_ID0                    0x35
#define SS_READ_HIS_CTRL_ID1                    0xA0

#define QZ_READ_HIS_MONTH_CTRL_ID1              0x30 //月冻结数据
#define QZ_READ_HIS_DAILY_CTRL_ID1              0x31 //日冻结信息

//深水上传数据报文
#define SS_UPLOAD_CTRL                          0x67            
#define SS_ACK_UPLOAD_CTRL                      0xD6            
#define SS_UPLOAD_CTRL_ID0                      0x43          
#define SS_UPLOAD_CTRL_ID1                      0xA1           
#define SS_UPLOAD_CTRL_UDP_ID0                  0x31  //UDP上报数据数据标识
//短帧上报数据
#define SHORT_UPLOAD_CTRL                       0x9A           
#define SHORT_ACK_UPLOAD_CTRL                   0xEF            
#define SHORT_UPLOAD_CTRL_ID0                   0x02      
#define SHORT_UPLOAD_CTRL_ID1                   0x10     

//新补报规约上报数据
#define SUPP_UPLOAD_CTRL                       0x9B           
#define SUPP_ACK_UPLOAD_CTRL                   0xF1            
#define SUPP_UPLOAD_CTRL_ID0                   0x01      
#define SUPP_UPLOAD_CTRL_ID1                   0x10   

//深水登录帧
#define SS_SIGN_IN_CTRL                         0x8f          
#define SS_ACK_SIGN_IN_CTRL                     0xe8         
#define SS_SIGN_IN_CTRL_ID0                     0x55           
#define SS_SIGN_IN_CTRL_ID1                     0xa1              

//清除失败次数
#define SS_CLEAR_FAIL_TIMES_CTRL                0x94            
#define SS_ACK_CLEAR_FAIL_TIMES_CTRL            0xEC
#define SS_CLEAR_FAIL_TIMES_CTRL_DI0            0x59 
#define SS_CLEAR_FAIL_TIMES_CTRL_DI1            0xA1 

//广水控制码
#define READ_MET_CTRL_00					    0x00     		//
#define READ_MET_CTRL_01					    0x01     		//
#define READ_MET_CTRL_02					    0x02     		//
#define READ_MET_CTRL_03					    0x03     		//
#define READ_MET_CTRL_04					    0x04        //
#define READ_MET_CTRL_05					    0x05     		//
#define READ_MET_CTRL_06					    0x06        //
#define READ_MET_CTRL_07					    0x07     		//
#define READ_MET_CTRL_08					    0x08        //
#define READ_MET_CTRL_09					    0x09     		//
#define READ_MET_CTRL_010					    0x0a        //
#define READ_MET_CTRL_011					    0x0b    		//
#define READ_MET_CTRL_012					    0x0c        //
#define READ_MET_CTRL_013				      	0x0d        //

enum{
	SUPPLEMENT_DATA_FRAME_S0 = 0,
	SUPPLEMENT_DATA_FRAME_S1,
	SUPPLEMENT_DATA_FRAME_S2,
	SUPPLEMENT_DATA_FRAME_S3,
	SUPPLEMENT_DATA_FRAME_S4,
	SUPPLEMENT_DATA_FRAME_S5,
	SUPPLEMENT_DATA_FRAME_S6,
	SUPPLEMENT_DATA_FRAME_S7
};
#define SUPPLEMENT_DATA_MAX_NUM		19		//一帧补报数据里最多19条日冻结

typedef enum
{
	SS_StoreMsg0  = 0u,                
	SS_StoreMsg1  = 1u,    
	SS_StoreMsg2  = 2u,                
	SS_StoreMsg3  = 3u,
	SS_StoreMsg4  = 4u,                
	SS_StoreMsg5  = 5u,
	SS_StoreMsg6  = 6u,                
	SS_StoreMsg7  = 7u
}SS_StoreMsgType;

/***************************************长帧上报数据格式*********************/
#define PACK_NUM_GPRS                            155 //周期上报部分数据长度
#define PACK_NUM_GPRS_INTEN                      199 //24小时的30min间隔流量数据长度,48*4+7
#define PACK_NUM_GPRS_TOTAL       (PACK_NUM_GPRS+PACK_NUM_GPRS_INTEN)    //周期上报的数据总长度
#define CURRENT_DATA_INFO_LEN  36
#define STEP_FLOW_START_ADDR   (0+CURRENT_DATA_INFO_LEN)
/**************************规约应答相关函数**********************************/
u8 up_deal_code_GuangShui(void);
u8 crack_frame_GuangShui(u16 frame_lenth,u8 *frame_buffer);
void Frame_ResetDealState_GuangShui(void);
void Frame_ack_GuangShui(u8* p_send_buf, u16* p_send_len);
/**************************上报相关函数***************************************/
#define PERIOD_DATA_NUM  32 //周期数据记录总报数

void GetUploadDataResetState_GS(void);
u8 GetUploadDataCheckIdle_GS(void);
u8 GetUploadDataCheckTask_GS(void);
void GetUploadDataMachine_GS(u8 data_step, u8* p_send_buf, u16* p_send_len);
/**************************存储相关函数***************************************/
void DataStoreInit(void);
void DataStoreMachine(void);
u8 DataStoreIfIdle(void);
/*****************************************************************************/
#define MAIN_CONNECT_TYPE      NBIOT_CONNECT_COAP
#define MAIN_SEND_TYPE         SEND_TYPE_COAP
#define SLAVE_CONNECT_TYPE     NBIOT_CONNECT_UDP1
#define SLAVE_SEND_TYPE        SEND_TYPE_UDP1
//主上报10秒，副上报5秒
#define COAP_UPLOAD_WAIT_ACK_TIME  100 //10秒   
#define UDP_UPLOAD_WAIT_ACK_TIME   50 
#define NB_ONLINE_KEEP_TIME  50

#endif

#endif	


