#ifndef _SYS_PROCESSOR_H_
#define	_SYS_PROCESSOR_H_

#include "sys_config.h"
#if UPLOAD_MODE_1
#include "type.h"
#include "UWater_rtcc_driver.h"
#pragma pack(1) //1字节对齐


typedef struct
{
    u8 retry_num;             //重发次数
    u8 Step_send_report;//add by wm 2017.07.19 上报步骤 1是周期上报 2是密集上报 3是补包
    u8 Flag_Send_DATA;//add by wm 2017.07.19  发送指令标志 1是发送开始
    u8 Supplemental_send_NO;//补包查询过程变量，表示当前正在补哪一条记录，在补包上送成功后，要清除该记录bit
    u8 Save_histroy_FLAG;
    u8 Save_histroy_step;
    u8 Read_histroy_FLAG;
}FRAME_SEND_CONTROL;

typedef enum
{
    NB_HistroyReadIndex = 0, //历史数据的读指针
	NB_UploadFailTimes = 1,//失败次数
	NB_SendData_Msg = 2,//NB需要发送数据消息
	NB_SS_FormatError = 3, //数据格式错误
} NB_SS_ControlParaTypeDef;

//日最高流量和持续时间
typedef union{
    struct{
        mtype32_ pulse_max_flow;			//持续流量
        mtype16_ pulse_continue_time;		//持续时间
        u8 pulse_flow_time[8];				//产生时间
        };
     u8 byte[14];   
}IES_PULSE_DAY_MAX_FLOW;	//日最高流量

///**********************************************************周期上报模式判断**********************/
#define UPLOAD_MODE_HOUR    1   //小时上报模式
#define UPLOAD_MODE_DAY     2   //天上报模式
#define UPLOAD_MODE_WEEK    3   //周上报模式
#define UPLOAD_MODE_MONTH   4   //月上报模式

///**********************************************************上报类型定义**************************/
#define UPLOAD_TYPE_FIX_TIME     		0    			//0：自动定时 
#define UPLOAD_TYPE_HAND_KEY   		 	1	  			//1：手动按键 
#define UPLOAD_TYPE_FRAME_ORDER        	2  				//2: 规约命令发起上传
#define UPLOAD_TYPE_RETRY_AGAIN    		3  				//3: 重新上传
#define UPLOAD_TYPE_VALVE_CLOSE    		4  				//4: 阀门关闭上传
#define UPLOAD_TYPE_VALVE_ACTIVE  		5  				//5: 阀门动作上传
#define UPLOAD_TYPE_SET_IP     			6  				//6: 设置IP上传
#define UPLOAD_TYPE_TOUCH_KEY      		7  				//7: 触摸按键上传
#define UPLOAD_TYPE_WARNING      		8  				//8: 及时报警上传
#define UPLOAD_TYPE_FACTORY             9               //9: 厂区上报
#define UPLOAD_TYPE_OTHER    	 		255  			//255：其他
#define UPLOAD_EVENT_NUM         		10   			//同时支持10个上报事件 	

#define VALVE_CLOSE_UPLOAD_MAX          250             //关阀后最大上报次数
#define VALVE_CLOSE_UPLOAD_MIN			48              //关阀后最小上报次数

typedef struct {
	u8 upload_type[UPLOAD_EVENT_NUM];
	u8 event_cnt;
	u8 upload_cnt;
} EVENT_UPLOAD_; //事件
///**********************************************************重试间隔定义**************************/
#define JIANGE_20_MIN		1	//采用间隔20min
///**********************************************************数据上报状态机************************/
typedef enum{	
	SS_STATE_S0 = 0,
	SS_STATE_S1,
	SS_STATE_S2,
	SS_STATE_S3,
	SS_STATE_S4,
	SS_STATE_S5,
	SS_STATE_S6,
	SS_STATE_S7,
	SS_STATE_S8,
	SS_STATE_S9,
	SS_STATE_S10,
	SS_STATE_S11,
	SS_STATE_S20,
	SS_STATE_S21,
	SS_STATE_S22,
	SS_STATE_S23,
	SS_STATE_S30,
	SS_STATE_S31,
	SS_STATE_S40,
	SS_STATE_WAIT
}TYP_STATE_SS;


#define UPLOAD_BUF_SIZE   				UP_BUF_SIZE		//与上行缓冲区大小一致
//等待时间设置
#define NB_LAUNCH_TIME    				3000			    //NB登录时间-----300秒
#define NB_TOTAL_WORK_TIME 				3600			    //NB总运行时间---360秒
#define NB_WAIT_5S                      50
#define NB_WAIT_10S                     100
//上报状态机状态
typedef enum{
	TIME_UPLOAD_S0 = 0,
	TIME_UPLOAD_S1,
	TIME_UPLOAD_S10,
	TIME_UPLOAD_S11,
	TIME_UPLOAD_S2,
	TIME_UPLOAD_S20,
	
	TIME_UPLOAD_S3,
	TIME_UPLOAD_S4,
	TIME_UPLOAD_S5,
	TIME_UPLOAD_S6,
	TIME_UPLOAD_S7,
	TIME_UPLOAD_S8,
	TIME_UPLOAD_S9,
	
	TIME_UPLOAD_UDP_S1,
	TIME_UPLOAD_UDP_S2,
	TIME_UPLOAD_UDP_S3,
	TIME_UPLOAD_UDP_S4,
	TIME_UPLOAD_UDP_S5,
	TIME_UPLOAD_UDP_S6,
	TIME_UPLOAD_UDP_S7,
	
//	TIME_UPLOAD_PRO_S4,
//	TIME_UPLOAD_PRO_S5,
//	TIME_UPLOAD_PRO_S6,
//	TIME_UPLOAD_PRO_S7,

	TIME_UPLOAD_PsmSleep,
	TIME_UPLOAD_PsmSleepWait,
	
	TIME_UPLOAD_PowerDown,
	TIME_UPLOAD_PowerDownWait,
	TIME_UPLOAD_PowerDownEnd,
	
	TIME_UPLOAD_END
}TYP_STATE_UPLOAD;

typedef enum{
	UPLOAD_S0 = 0,
	UPLOAD_S1,
	UPLOAD_S2
}TYP_MAIN_STATE_UPLOAD;
//存冻结数据状态机状态
enum{
	SAVE_FREEZE_S0 = 0,
	SAVE_FREEZE_S1,
	SAVE_FREEZE_S2,
	SAVE_FREEZE_S3,
	SAVE_FREEZE_S4,
	SAVE_FREEZE_S5,
	SAVE_FREEZE_S6,
	SAVE_FREEZE_S7
};

/**************************************冻结数据存储状态机****************************/
#define UPLOAD_DATA_IDLE            0
#define UPLOAD_DATA_READ_HIS        1
#define UPLOAD_DATA_STEP_FLOW       2
#define UPLOAD_DATA_RENEW_CURRENT   3
#define UPLOAD_DATA_SAVE_DATA       4
#define UPLOAD_DATA_SAVE_INDEX      5
#define UPLOAD_DATA_SAVE_BUBAOFLAG  6
#define UPLOAD_DATA_CLOSE_DEBUG     7
#define UPLOAD_DATA_EEP_WAIT        8
/**************************************根据不同规约组上报帧**************************/
#if PRODUCT_SS
//主上报流程
#define GetUploadDataMachine         GetUploadDataMachine_SS
#define GetUploadDataResetState      GetUploadDataResetState_SS
#define GetUploadDataCheckIdle       GetUploadDataCheckIdle_SS
#define GetUploadDataCheckTask       GetUploadDataCheckTask_SS
//副上报流程
#define GetUploadDataMachine_UDP     GetUploadDataMachine_IesUDP
#define GetUploadDataResetState_UDP  GetUploadDataResetState_IesUDP
#define GetUploadDataCheckIdle_UDP   GetUploadDataCheckIdle_IesUDP
#define GetUploadDataCheckTask_UDP   GetUploadDataCheckTask_IesUDP

#elif PRODUCT_JS
#define GetUploadDataMachine         GetUploadDataMachine_JX
#define GetUploadDataResetState      GetUploadDataResetState_JX
#define GetUploadDataCheckIdle       GetUploadDataCheckIdle_JX
#define GetUploadDataCheckTask       GetUploadDataCheckTask_JX

#define GetUploadDataMachine_UDP     GetUploadDataMachine_IesUDP
#define GetUploadDataResetState_UDP  GetUploadDataResetState_IesUDP
#define GetUploadDataCheckIdle_UDP   GetUploadDataCheckIdle_IesUDP
#define GetUploadDataCheckTask_UDP   GetUploadDataCheckTask_IesUDP

#elif PRODUCT_SY
#define GetUploadDataMachine         GetUploadDataMachine_ShenYangUDP
#define GetUploadDataResetState      GetUploadDataResetState_ShenYangUDP
#define GetUploadDataCheckIdle       GetUploadDataCheckIdle_ShenYangUDP
#define GetUploadDataCheckTask       GetUploadDataCheckTask_ShenYangUDP

#define GetUploadDataMachine_UDP     GetUploadDataMachine_IesUDP
#define GetUploadDataResetState_UDP  GetUploadDataResetState_IesUDP
#define GetUploadDataCheckIdle_UDP   GetUploadDataCheckIdle_IesUDP
#define GetUploadDataCheckTask_UDP   GetUploadDataCheckTask_IesUDP

#elif PRODUCT_SHTCP
#define GetUploadDataMachine         GetUploadDataMachine_SH
#define GetUploadDataResetState      GetUploadDataResetState_SH
#define GetUploadDataCheckIdle       GetUploadDataCheckIdle_SH
#define GetUploadDataCheckTask       GetUploadDataCheckTask_SH

#define GetUploadDataMachine_UDP     GetUploadDataMachine_IesUDP
#define GetUploadDataResetState_UDP  GetUploadDataResetState_IesUDP
#define GetUploadDataCheckIdle_UDP   GetUploadDataCheckIdle_IesUDP
#define GetUploadDataCheckTask_UDP   GetUploadDataCheckTask_IesUDP

#elif PRODUCT_WHYW
#define GetUploadDataMachine         GetUploadDataMachine_WHYW
#define GetUploadDataResetState      GetUploadDataResetState_WHYW
#define GetUploadDataCheckIdle       GetUploadDataCheckIdle_WHYW
#define GetUploadDataCheckTask       GetUploadDataCheckTask_WHYW

#define GetUploadDataMachine_UDP         GetUploadDataMachine_IesUDP
#define GetUploadDataResetState_UDP      GetUploadDataResetState_IesUDP
#define GetUploadDataCheckIdle_UDP       GetUploadDataCheckIdle_IesUDP
#define GetUploadDataCheckTask_UDP       GetUploadDataCheckTask_IesUDP
#elif PRODUCT_GS
#define GetUploadDataMachine         GetUploadDataMachine_GS
#define GetUploadDataResetState      GetUploadDataResetState_GS
#define GetUploadDataCheckIdle       GetUploadDataCheckIdle_GS
#define GetUploadDataCheckTask       GetUploadDataCheckTask_GS

#define GetUploadDataMachine_UDP     GetUploadDataMachine_IesUDP
#define GetUploadDataResetState_UDP  GetUploadDataResetState_IesUDP
#define GetUploadDataCheckIdle_UDP   GetUploadDataCheckIdle_IesUDP
#define GetUploadDataCheckTask_UDP   GetUploadDataCheckTask_IesUDP
#endif

//生产规约组上报帧
#define GetUploadDataMachineProduction         GetUploadDataMachine_Product
#define GetUploadDataResetStateProduction      GetUploadDataResetState_Product
#define GetUploadDataCheckIdleProduction       GetUploadDataCheckIdle_Product
#define GetUploadDataCheckTaskProduction       GetUploadDataCheckTask_Product
/**************************************上报帧组帧标志*****************************/

#define UPLOAD_FRAME_REQUEST_NEW               0x01
#define UPLOAD_FRAME_UPLOAD_OK                 0x02
#define UPLOAD_FRAME_UPLOAD_RETRY              0x03
#define UPLOAD_FRAME_ONLINE_FAIL               0xF1

u8 NB_SS_Control_GetPara(NB_SS_ControlParaTypeDef paraType);
void NB_SS_Control_SetPara(NB_SS_ControlParaTypeDef paraType, u8 data);

u8 GetEventUploadType(void);
void SystemGetUploadTime(DateTime *pDT);
void SystemClearUploadAgain(void);
void SystemAddUploadAgain(void);
void SystemProcessor(void);
void SystemProcessorInit(void);
u8 SystemProcessorIfSleep(void);
u8 FixedTimeNbIoTUploadIfIdle(void);
void RtccCheck_Hight_Flow(u8 **addr);
void SysDealAppPreSleep(void);

#elif UPLOAD_MODE_2
#include "sys_processor_ble.h"
#endif

#endif

