#include "sys_config.h"
#include "sys_processor.h"

#include "UWater_eeprom_driver.h"
#include "UWater_timer1uS_driver.h"
#include "UWater_gp22_driver.h"
#include "UWater_valve_driver.h"

#include "UWater_rtcc_app.h"
#include "UWater_power_app.h"
#include "UWater_lcd_app.h"	
#include "UWater_gp22_app.h"
#include "UWater_pt_app.h" 
#include "UWater_DataTools_App.h"
#include "UWater_self_test.h"
#include "UWater_Valve_app.h"
#include "eeprom_app.h"
#include "SOE.h"
#include "UWater_QZ_Store_App.h"
#include "UWater_frame_func_ReplyNB.h"
#include "nb_upload_monitor.h"


static u8 check_addr[7] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00};
const u8 super_key[] = {113, 100, 46, 105, 101, 115, 108, 97, 98, 46, 99, 111, 109, 46, 99, 110};//超级密钥，默认 qd.ieslab.com.cn
static u8 key[16] = {81, 73, 78, 71, 68, 65, 79, 74, 73, 67, 72, 45, 78, 71, 78, 66}; //默认普通密钥
static u8 s_nb_eep_complete=0;
static u8 task_complete_ies = PTL_PACK_BUSY;
static u8 g_frame_temp_send_buf[UP_BUF_SIZE];	//数据部分缓冲区
static u16 g_frame_temp_len=0;
static u32 s_frame_temp_timer_tick;
static mtype16_ write_eeprom_address;	//写EE数据地址
static u8 write_eeprom_length;			//写EE数据长度

//static STRUCT_FLAG s_frame_flag_IES; //规约标志
static STRUCT_FRAME_IES frame_struct_IES; //规约信息 
static u8 *new_message_data_rev_ies; 
static u8 up_deal_stat_ies = UP_DEAL_S0;
static u8 frame_buffer[128];//规约数据帧缓存,防止解密时更改原始数据
///*******主动上传数据相关变量*****************************************/
#if (MODULE_BLE_USED)
//读取表计时间
static void frame_func_reply_NB_TimeFrequency(void)
{		
	 
	DateTime date_time;
	task_complete_ies = PTL_PACK_FAIL;
	g_frame_temp_len =0;
	if(*new_message_data_rev_ies == 0x01)//读取时间
	{		
		g_frame_temp_send_buf[g_frame_temp_len++]=0x01;
		RtccGetDateTime(&date_time);
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.second;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.minute;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.hour;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.day;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.month;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.year;        
        g_frame_temp_len = 7;
        task_complete_ies = PTL_PACK_SUCCESS;
	}

}
#else //NB 表
//积成UDP第二通道上报数据
static STRU_PROC_MSG upload_udp_frame;
static u8 upload_frame_stat_ies_udp=UP_DEAL_S0;
static u8 upload_task_complete_ies_udp=PTL_PACK_BUSY;
//生产规约上报状态
static STRU_SHORT_UPLOAD_FRAME  short_upload_product_frame;
static u8 upload_frame_stat_product_ies=0; //主动上报组帧状态
static u8 upload_task_complete_product_ies = PTL_PACK_FAIL;  
static u8 upload_frame_retry_times=0;

//读取IP及端口号
static void frame_func_reply_NB_ReadIpPort(void)
{
	g_frame_temp_len = 0;
	if(*(new_message_data_rev_ies - 2) == 0x02)//sta=2,读取UDP服务器地址
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_ip[0];
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_ip[1];
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_ip[2];
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_ip[3];
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_port.datas_map.data0;
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_port.datas_map.data1;
	}
	else
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_ip[0];
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_ip[1];
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_ip[2];
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_ip[3];
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_port.datas_map.data0;
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_port.datas_map.data1;
	}		
	task_complete_ies = PTL_PACK_SUCCESS;
}

//主动上传
static void frame_func_reply_NB_Upload(void)
{
    FrameSetMsgApp(MsgNbUartSend);
    g_frame_temp_len = 0;
	task_complete_ies = PTL_PACK_SUCCESS;
}


//读取信号值
static void frame_func_reply_NB_ReadSignal(void)
{
	int i = 0;
	static UNION_SIGNAL temp_signal_val;
	u8 cell_id_data_frame[CELL_ID_SIZE];
	s16 s_temp_data=0;
	u8 s_temp_data1 = 0;
	u16 s_temp_data2 = 0;
	NbiotGetSNR(&s_temp_data);
	g_frame_temp_len=0;
	if(s_temp_data<0)
	{
		temp_signal_val.SignalStruct.snr.datas = (-s_temp_data)|0x8000;
	}
	else
    {
	    temp_signal_val.SignalStruct.snr.datas = s_temp_data;
	}
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.snr.data_buf[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.snr.data_buf[1];
	//
	NbiotGetECL(&s_temp_data);
	if(s_temp_data<0)
	{
		temp_signal_val.SignalStruct.ecl = (-s_temp_data)|0x8000;
	}
	else
	{
	    temp_signal_val.SignalStruct.ecl = s_temp_data;
	}
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.ecl;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.ecl>>8;
	//
	NbiotGetRSRP(&s_temp_data);
	temp_signal_val.SignalStruct.rsrp = -s_temp_data;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.rsrp;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.rsrp>>8;
	//
    NbiotGetCSQ(&s_temp_data1); 
	temp_signal_val.SignalStruct.csq = s_temp_data1;
	temp_signal_val.SignalStruct.csq = (temp_signal_val.SignalStruct.csq / 10)*16 + (temp_signal_val.SignalStruct.csq%10);
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.csq;
	//
	temp_signal_val.SignalStruct.faile_times = NB_SS_Control_GetPara(NB_UploadFailTimes);
    g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.faile_times;
    //
	g_frame_temp_send_buf[g_frame_temp_len++] = 0;
	//
    NbiotGetCell_ID(cell_id_data_frame);
	temp_signal_val.SignalStruct.cell_id = 0;
	for(i=0;i<CELL_ID_SIZE;i++)
	{
	    if(cell_id_data_frame[i] != 0)
		{
			temp_signal_val.SignalStruct.cell_id <<= 4;
			if(cell_id_data_frame[i] <= '9')
			{
			    temp_signal_val.SignalStruct.cell_id |= cell_id_data_frame[i]-'0';
			}
			else if((cell_id_data_frame[i] <= 'f')&&(cell_id_data_frame[i] >= 'a'))
			{
			    temp_signal_val.SignalStruct.cell_id |= cell_id_data_frame[i]-'a'+10;
			}
			else if((cell_id_data_frame[i] <= 'F')&&(cell_id_data_frame[i] >= 'A'))
			{
			    temp_signal_val.SignalStruct.cell_id |= cell_id_data_frame[i]-'A'+10;
			}
		}
	}
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.cell_id>>0;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.cell_id>>8;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.cell_id>>16;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.cell_id>>24;
	//
	NbiotGetEARFCN(&s_temp_data2);//上报频率
	temp_signal_val.SignalStruct.report_fre.datas = s_temp_data2;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.report_fre.datas>>8;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_signal_val.SignalStruct.report_fre.datas;

	g_frame_temp_len = 15;
	task_complete_ies = PTL_PACK_SUCCESS;
}

//读取上报开始时间、结束时间
static void frame_func_reply_NB_ReadStartEnd(void)
{
	int i;
	g_frame_temp_len =0;
	for(i=0;i<6;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_period_start[i];
	}
	for(i=0;i<6;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_period_end[i];
	}
	for(i=0;i<2;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_step_len.data_buf[i];
	}
	g_frame_temp_len = 14;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读取最小估长
static void frame_func_reply_NB_ReadStepLength(void)
{
	g_frame_temp_len =0;
    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_step_len.data_buf[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_step_len.data_buf[1];
	g_frame_temp_len = 2;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读取表计时间频点
static void frame_func_reply_NB_TimeFrequency(void)
{		
	 
	DateTime date_time;
	task_complete_ies = PTL_PACK_FAIL;
	g_frame_temp_len =0;
	if(*new_message_data_rev_ies == 0x01)//读取时间
	{		
		g_frame_temp_send_buf[g_frame_temp_len++]=0x01;
		RtccGetDateTime(&date_time);
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.second;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.minute;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.hour;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.day;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.month;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.year;        
        g_frame_temp_len = 7;
        task_complete_ies = PTL_PACK_SUCCESS;
	}
	else if(*new_message_data_rev_ies == 0x02)
	{
		g_frame_temp_send_buf[g_frame_temp_len++]=0x02;
		u8 temp = NbiotGetNBAND();
		u16 band_fre=0;
		
		if(1 == temp)
		{
			band_fre = 1980;
		}
		else if(3 == temp)
		{
			band_fre = 1785;
		}
		else if(5 == temp)
		{
			band_fre = 850;
		}
		else if(8 == temp)
		{
			band_fre = 900;
		}
		else if(20 == temp)
		{
			band_fre = 800;
		}
		else if(34 == temp)
		{
			band_fre = 2025;
		}
		else if(38 == temp)
		{
			band_fre = 2620;
		}
		else if(39 == temp)
		{
			band_fre = 1920;
		}
		else if(40 == temp)
		{
			band_fre = 2400;
		}
		else if(41 == temp)
		{
			band_fre = 2655;
		}
		else
		{
			band_fre = 850;
		}	
		g_frame_temp_send_buf[g_frame_temp_len++] = band_fre&0xFF;
		g_frame_temp_send_buf[g_frame_temp_len++] =(band_fre>>8)&0xFF;//默认频点 800
		
    	g_frame_temp_len = 3;
        task_complete_ies = PTL_PACK_SUCCESS;
	}
	else if(*new_message_data_rev_ies == 0x10)//读取上报时间
	{		
		g_frame_temp_send_buf[g_frame_temp_len++]=0x01;
		SystemGetUploadTime(&date_time);
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.second;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.minute;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.hour;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.day;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.month;
		g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.year;        
        g_frame_temp_len = 7;
        task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//深水读取重试次数
static void frame_func_reply_NB_ReadRetryCount(void)
{
	g_frame_temp_len=0;
    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_retry_times;
    g_frame_temp_len = 1;
	task_complete_ies = PTL_PACK_SUCCESS;
}

//读取上报频率
static void frame_func_reply_NB_ReadReportingFrequency(void)
{
	g_frame_temp_len = 0; 
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.s.type;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.s.period;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.s.hour;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.s.minute;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.s.second;

	task_complete_ies = PTL_PACK_SUCCESS;
}
//读取密集上报时间控制码
static void frame_func_reply_NB_ReadReportStartTime(void)
{
	int i;
	g_frame_temp_len =0;
	for(i=0;i<9;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_start_time[i];
	}
	g_frame_temp_len = 9;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读取APN
static void frame_func_reply_NB_ReadApn(void)
{
   int i;
	g_frame_temp_len =0;
	for(i=0;i<20;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.apn[1+i];
	}
	g_frame_temp_len =(Sys_data_object.P1.apn[1]+1);
	task_complete_ies = PTL_PACK_SUCCESS;    
}


//读取参数控制码
static void frame_func_reply_NB_UnifiedRead(void)
{
	int i=0;
	DateTime date_time;
	u8 temp_band = NbiotGetNBAND();
	RtccGetDateTime(&date_time);
	g_frame_temp_len = 0;
	
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.buf[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.buf[1];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.buf[2];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.buf[3];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Time_upload_2006.buf[4];
	
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_ip[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_ip[1];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_ip[2];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_ip[3];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_port.data_buf[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.server_port.data_buf[1];
	
	for(i=0;i<6;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_period_start[i];
	}
	for(i=0;i<6;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_period_end[i];
	}
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_step_len.data_buf[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_step_len.data_buf[1];
	
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_retry_times;
	for(i=0;i<9;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_start_time[i];
	}
	for(i=0;i<4;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.high_flow_alarm.data_buf[i];
	}
	g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.second;
	g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.minute;
	g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.hour;
	g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.day;
	g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.month;
	g_frame_temp_send_buf[g_frame_temp_len++] = date_time.s.year; 
	
	if(1 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 1980;
	}
	else if(3 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 1785;
	}
	else if(5 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 850;
	}
	else if(8 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 900;
	}
	else if(20 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 800;
	}
	else if(34 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 2025;
	}
	else if(38 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 2620;
	}
	else if(39 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 1920;
	}
	else if(40 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 2400;
	}
	else if(41 == temp_band)
	{
		Sys_data_object.P1.report_fre.datas = 2655;
	}
	else
	{
		Sys_data_object.P1.report_fre.datas = 850;
	}	
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_fre.data_buf[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.report_fre.data_buf[1]; 					  

	for(i = 0; i < 19; i++)
	{
		 g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.apn[1 + i];
	}
	g_frame_temp_send_buf[g_frame_temp_len++] = 0;
	//g_frame_temp_len = 67;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//深水读取IMEI控制码增加读取ICCID2019年5月16日
static void frame_func_reply_NB_ReadImei(void)
{
	u8 temp_buf1[16];
	u8 temp_buf2[16];
	u8 temp_iccid_buf[20];
	int i;
	g_frame_temp_len=0;
	NbiotGetIMEI((char*)temp_buf1); 
	temp_buf2[0] = temp_buf1[0] - 0x30;
	for(i=1;i<8;i++)
	{
		temp_buf2[i]  =  (temp_buf1[2*i]-0x30)&0x0F;
		temp_buf2[i] |= ((temp_buf1[2*i-1]-0x30)<<4)&0xF0;          
	}
	for(i=0;i<8;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = temp_buf2[7-i];
	}
	NbiotGetIMSI((char*)temp_buf1); 
	temp_buf2[0] = temp_buf1[0] - 0x30;
	for(i=1;i<8;i++)
	{
		temp_buf2[i]  =  (temp_buf1[2*i]-0x30)&0x0F;
		temp_buf2[i] |= ((temp_buf1[2*i-1]-0x30)<<4)&0xF0;          
	}
	for(i=0;i<8;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = temp_buf2[7-i];
	}
	NbiotGetICCID((char*)temp_iccid_buf); 
	for(i=0;i<20;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = temp_iccid_buf[19-i];
	}
	//g_frame_temp_len = 36;
	task_complete_ies = PTL_PACK_SUCCESS;
}

//读取上报重试间隔和附网次数
static void frame_func_reply_NB_ReadRetryTime(void)
{
	g_frame_temp_len = 0;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Upload_again_interval_time.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.Upload_again_interval_time.datas_map.data1;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.nb_para_login_ask_times.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.nb_para_login_ask_times.datas_map.data1;
	task_complete_ies = PTL_PACK_SUCCESS;
}

//写IP和端口号
static void frame_func_s3_NB_WriteIpPort(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		int i =0;
		g_frame_temp_len = 0;
		
		if(*(new_message_data_rev_ies - 2) == 0x02)//sta=2,UDP服务器地址
		{
			for (i = 0; i < 4; i++)
			{
				Sys_data_object.P2.server_ip[i] = *(new_message_data_rev_ies +i);
			}
			Sys_data_object.P2.server_port.data_buf[0] = *(new_message_data_rev_ies +4);
			Sys_data_object.P2.server_port.data_buf[1] = *(new_message_data_rev_ies +5);
			
			EepWrite(&Sys_data_object.P2.server_ip[0], EEP_UPDATE_UDP_IP, 6, &s_nb_eep_complete);//保存到UDP IP
			FrameSetMsgApp(MsgShenYang_UdpIpPortChange);
		}
		else
		{
			for (i = 0; i < 4; i++)
			{
				Sys_data_object.P1.server_ip[i] = *(new_message_data_rev_ies +i);
			}
			Sys_data_object.P1.server_port.data_buf[0] = *(new_message_data_rev_ies +4);
			Sys_data_object.P1.server_port.data_buf[1] = *(new_message_data_rev_ies +5);

			EepWrite(&Sys_data_object.P1.server_ip[0], FRAME_NBIot_IP, 6, &s_nb_eep_complete);
			FrameSetMsgApp(MsgNbIpPortChange);
		}		
		task_complete_ies = PTL_PACK_SUCCESS;      
	}
}

//深水统一设置参数
static void frame_func_s3_NB_Unified_Setting(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		//DateTime temp_date_time;
		int i=0;
		u8* temp_data_p = (new_message_data_rev_ies + 1);
		for(i=0;i<5;i++)
		{
			Sys_data_object.P1.Time_upload_2006.buf[i] = *(temp_data_p++);
		}
		for(i=0;i<4;i++)
		{
			Sys_data_object.P1.server_ip[i] = *(temp_data_p++);
		}
		Sys_data_object.P1.server_port.data_buf[0] = *(temp_data_p++);
		Sys_data_object.P1.server_port.data_buf[1] = *(temp_data_p++);
		for(i=0;i<6;i++)
		{
			Sys_data_object.P1.report_period_start[i] = *(temp_data_p++);
		}
		for(i=0;i<6;i++)
		{
			Sys_data_object.P1.report_period_end[i] = *(temp_data_p++);
		}
		Sys_data_object.P1.report_step_len.data_buf[0] = *(temp_data_p++);
		Sys_data_object.P1.report_step_len.data_buf[1] = *(temp_data_p++);
		Sys_data_object.P1.report_retry_times = *(temp_data_p++);
		for(i=0;i<9;i++)
		{
			Sys_data_object.P1.report_start_time[i] = *(temp_data_p++);
		}
		for(i=0;i<4;i++)
		{
			Sys_data_object.P1.high_flow_alarm.data_buf[i] = *(temp_data_p++);
		}
		for(i=0;i<6;i++)//预留
		{
			temp_data_p++;
		}
		for(i=0;i<4;i++)
		{
			Sys_data_object.P1.report_fre.data_buf[i] = *(temp_data_p++);
		}
		for(i=0;i<20;i++)
		{
			Sys_data_object.P1.apn[i] = *(temp_data_p++);
		}
		EepWrite(&Sys_data_object.P1.Time_upload_2006.buf[0], FRAME_NBIot_FREQVAL, 67, &s_nb_eep_complete);
		task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//深水设置最大流量报警门限
static void frame_func_s3_NB_SetThreshold(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		UNION_4U8TOU32 alarm_value;
		memcpy(&alarm_value.datas_map.data0, new_message_data_rev_ies, 4);
		if(alarm_value.datas >10000)//2018年12月29日14:09:11增加大流量报警门限防护
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}
		memcpy(&Sys_data_object.P1.high_flow_alarm.datas_map.data0, new_message_data_rev_ies,4);
		EepWrite(&Sys_data_object.P1.high_flow_alarm.datas_map.data0, FRAME_NBIot_HIGHFLOW, 4, &s_nb_eep_complete);
		FrameSetMsgApp(MsgNbFlowAlarm);
		task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//深水设置上报起始结束时间
static void frame_func_s3_NB_SetStartEnd(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		UNION_2U8TOU16  temp_fram ;
		temp_fram.datas_map.data0 = *(new_message_data_rev_ies + 12);
		temp_fram.datas_map.data1 = *(new_message_data_rev_ies + 13);
		if(temp_fram.datas >10)
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;				 
		}
		memcpy(&Sys_data_object.P1.report_period_start[0],new_message_data_rev_ies,14);
		EepWrite(&Sys_data_object.P1.report_period_start[0], FRAME_NBIot_REPORTSTART, 14, &s_nb_eep_complete);
		FrameSetMsgApp(MsgNbStartEndChange);
		task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//深水设置最小估长
static void frame_func_s3_NB_SetSteplength(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		UNION_2U8TOU16  temp_fram ;
		temp_fram.datas_map.data0 = *(new_message_data_rev_ies + 0);
		temp_fram.datas_map.data1 = *(new_message_data_rev_ies + 1);
		if(temp_fram.datas >43)
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;				 
		}
		memcpy(&Sys_data_object.P1.report_step_len.data_buf[0] ,new_message_data_rev_ies, 2);
		EepWrite(&Sys_data_object.P1.report_step_len.data_buf[0], FRAME_NBIot_LENGTH, 2, &s_nb_eep_complete);
		FrameSetMsgApp(MsgNbStepLengthChange);
		task_complete_ies = PTL_PACK_SUCCESS;  
	}
}

//深水设置重试次数
static void frame_func_s3_NB_SetRetryCount(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		memcpy(&Sys_data_object.P1.report_retry_times ,new_message_data_rev_ies,1);
		EepWrite(&Sys_data_object.P1.report_retry_times, EEP_RETRY_TIMES_ADDR, 1, &s_nb_eep_complete);
		FrameSetMsgApp(MsgNbRetryTimesChange);
        task_complete_ies = PTL_PACK_SUCCESS;      
	}
}

//深水设置上报频率规约（小时模式、天模式、月模式）		
static void frame_func_s3_NB_SetReportingFrequency(void)
{   
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		memcpy(&Sys_data_object.P1.Time_upload_2006.buf[0] ,new_message_data_rev_ies,5);
		EepWrite(&Sys_data_object.P1.Time_upload_2006.buf[0], FRAME_NBIot_FREQVAL, 5, &s_nb_eep_complete);
		Frame_Para_Init();
		FrameSetMsgApp(MsgNbUploadFreqChange);
		task_complete_ies = PTL_PACK_SUCCESS;       
	}
}

//深水设置密集上报起始结束时间
static void frame_func_s3_NB_SetReportStartTime(void)
{   
	 if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	 {
	 	g_frame_temp_len = 0;
		memcpy(&Sys_data_object.P1.report_start_time [0],new_message_data_rev_ies,9);
		if((Sys_data_object.P1.report_start_time[2]>Sys_data_object.P1.report_start_time[5]) ||((Sys_data_object.P1.report_start_time[2]==Sys_data_object.P1.report_start_time[5])&& (Sys_data_object.P1.report_start_time[1]>Sys_data_object.P1.report_start_time[4]))
		|| ((Sys_data_object.P1.report_start_time[2]==Sys_data_object.P1.report_start_time[5])&& (Sys_data_object.P1.report_start_time[1]==Sys_data_object.P1.report_start_time[4]) &&(Sys_data_object.P1.report_start_time[0]>Sys_data_object.P1.report_start_time[3])))
		{				 
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}
		EepWrite(&Sys_data_object.P1.report_start_time [0], FRAME_NBIot_STARTTIME, 9, &s_nb_eep_complete);
		Frame_Para_Init();
		FrameSetMsgApp(MsgNbIntensiveTime);
		task_complete_ies = PTL_PACK_SUCCESS;
	 }
}

//设置APN	
static void frame_func_s3_NB_SetApn(void)
{   
	if(TASK_IDLE == EepIfIdle())
	{
		g_frame_temp_len = 0;
		if((0< *new_message_data_rev_ies) && (*new_message_data_rev_ies <19)   )//APN长度判断
		{
			Sys_data_object.P1.apn[0] = 0x01;
			Sys_data_object.P1.apn[1] = *(new_message_data_rev_ies);//apn长度
			u8 i_temp = 0;
			for(i_temp = 0; i_temp < Sys_data_object.P1.apn[1]; i_temp++)
			{
				Sys_data_object.P1.apn[2 + i_temp] = *(new_message_data_rev_ies + 1 + i_temp);
			}
		}
		else
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}
		EepWrite(&Sys_data_object.P1.apn[0], FRAME_NBIot_APN, (Sys_data_object.P1.apn[1]+2), &s_nb_eep_complete);
		FrameSetMsgApp(MsgNbApnChange);
		task_complete_ies = PTL_PACK_SUCCESS;      	
	}
}
//设置上报相关参数
static void frame_func_s3_NB_SetRetryTime(void)
{   
	if(TASK_IDLE == EepIfIdle())
	{
		g_frame_temp_len = 0;
		Sys_data_object.P1.Upload_again_interval_time.datas_map.data0 = *(new_message_data_rev_ies + 0);
		Sys_data_object.P1.Upload_again_interval_time.datas_map.data1 = *(new_message_data_rev_ies + 1);
		Sys_data_object.P1.nb_para_login_ask_times.datas_map.data0 = *(new_message_data_rev_ies + 2);
		Sys_data_object.P1.nb_para_login_ask_times.datas_map.data1 = *(new_message_data_rev_ies + 3);
		
		EepWrite(&Sys_data_object.P1.Upload_again_interval_time.datas_map.data0, EEP_FRAME_NB_PARA_ADDR, 4, &s_nb_eep_complete);
		Frame_Para_Init();	
		task_complete_ies = PTL_PACK_SUCCESS;                
	}
}
//短帧上报应答
static void frame_func_reply_NB_AckUploadShort(void)
{
	g_frame_temp_len =0;
	FrameSetMsgApp(MsgNbUplaod_AckOk);
	task_complete_ies = PTL_PACK_FAIL;//这是上报应答帧,返回错误,表计不应答
}
//读取最大流量阈值
static void frame_func_reply_NB_ReadThreshole(void)
{
    int i = 0;
	g_frame_temp_len =0;
	for(i=0;i<4;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.high_flow_alarm.data_buf[i];
	}
	g_frame_temp_len = 4;
	task_complete_ies = PTL_PACK_SUCCESS;
}
#endif
//新增规约:读取电池参数
static void frame_func_reply_NB_read_battery(void)
{   
	u16 temp_u32_data;
	g_frame_temp_len = 0;
	//待机时间
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.total_use_sum_times_sec.to_uint8[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.total_use_sum_times_sec.to_uint8[1];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.total_use_sum_times_sec.to_uint8[2];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.total_use_sum_times_sec.to_uint8[3];
	//电池剩余电量
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.bat_surplus.to_uint8[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.bat_surplus.to_uint8[1];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.bat_surplus.to_uint8[2];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.bat_surplus.to_uint8[3];
	//红外累计工作时间
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.use_sum_times_sec.to_uint8[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.use_sum_times_sec.to_uint8[1];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.use_sum_times_sec.to_uint8[2];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.use_sum_times_sec.to_uint8[3];
	//NB累计工作时间
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.nb_use_sum_times_sec.to_uint8[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.nb_use_sum_times_sec.to_uint8[1];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.nb_use_sum_times_sec.to_uint8[2];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.nb_use_sum_times_sec.to_uint8[3];
	//NB上报次数
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.nb_upload_happen_num&0xFF;
	g_frame_temp_send_buf[g_frame_temp_len++] = (Sys_data_object.P1.run_information_object.nb_upload_happen_num>>8)&0xFF;
	//阀门工作时间
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.run_information_object.valve_use_sum_times_sec&0xFF;
	g_frame_temp_send_buf[g_frame_temp_len++] = (Sys_data_object.P1.run_information_object.valve_use_sum_times_sec>>8)&0xFF;
	//剩余功耗和剩余时间比值
	temp_u32_data = Sys_data_object.P1.run_information_object.ee_remain_para_energy_life*1000;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_u32_data&0xFF;
	g_frame_temp_send_buf[g_frame_temp_len++] = (temp_u32_data>>8)&0xFF;
	//电池电压
	g_frame_temp_send_buf[g_frame_temp_len++] = PowerCheckValueSys()&0xFF;
	g_frame_temp_send_buf[g_frame_temp_len++] = ( PowerCheckValueSys()>>8)&0xFF;
			
	task_complete_ies = PTL_PACK_SUCCESS;
}
////新增规约:清除电池参数
static void frame_func_reply_NB_read_batteryinit(void)
{   
	g_frame_temp_len = 0;
	//电池电量相关参数
	Sys_data_object.P1.run_information_object.total_use_sum_times_sec.to_uint32 = 0;//表计工作总时间
	Sys_data_object.P1.run_information_object.bat_surplus.to_uint32 = ENERGY_OF_26500;//电池电量uAH
	Sys_data_object.P1.run_information_object.use_sum_times_sec.to_uint32 = 0;//红外累计工作时间
	Sys_data_object.P1.run_information_object.nb_use_sum_times_sec.to_uint32 = 0;//nb累计工作时间
	Sys_data_object.P1.run_information_object.nb_upload_happen_num = 0;//nb激活次数
	Sys_data_object.P1.run_information_object.valve_use_sum_times_sec = 0;//阀门工作时间
	Sys_data_object.P1.run_information_object.ee_remain_para_energy_life = 3;//比值
	
	task_complete_ies = PTL_PACK_SUCCESS;
}
//新规约共用控制字:写UDP IP和端口号
static void frame_func_s3_NB_WriteUdpIpPort(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;

		Sys_data_object.P2.server_ip[0] = *(new_message_data_rev_ies+0);
		Sys_data_object.P2.server_ip[1] = *(new_message_data_rev_ies+1);
		Sys_data_object.P2.server_ip[2] = *(new_message_data_rev_ies+2);
		Sys_data_object.P2.server_ip[3] = *(new_message_data_rev_ies+3);
		Sys_data_object.P2.server_port.data_buf[0] = *(new_message_data_rev_ies+4);
		Sys_data_object.P2.server_port.data_buf[1] = *(new_message_data_rev_ies+5);

		EepWrite(&Sys_data_object.P2.server_ip[0], EEP_UPDATE_UDP_IP, 6, &s_nb_eep_complete);//保存到UDP IP
		FrameSetMsgApp(MsgShenYang_UdpIpPortChange);
		task_complete_ies = PTL_PACK_SUCCESS;      
	}
}
//新规约共用控制字:读取UDP IP及端口号
static void frame_func_reply_NB_ReadUdpIpPort(void)
{
	g_frame_temp_len = 0;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_ip[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_ip[1];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_ip[2];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_ip[3];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_port.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.server_port.datas_map.data1;

	task_complete_ies = PTL_PACK_SUCCESS;
}
//终端重启
static void frame_func_reply_NB_TernalRestart(void)
{
	FrameSetMsgApp(BootloaderMsg);			
	if(01 == LcdCheckStateApp())      //检定状态
	{
		FrameSetMsgApp(OutVerificationMsg);  //出检定msg
	}
	NVIC_SystemReset();  //重启
	g_frame_temp_len = 0;
	task_complete_ies = PTL_PACK_SUCCESS;
}

//设置为全显状态
static void frame_func_reply_NB_SetLcdDisplay(void)
{
    FrameSetMsgApp(LcdDisplayAllMsg);//
    g_frame_temp_len = 0;
	task_complete_ies = PTL_PACK_SUCCESS;
}

//控阀
static void frame_func_reply_NB_ValveControl(void)
{
	g_frame_temp_len =0;
#if defined(VALVE)
	u8 fram_valve_sta = *new_message_data_rev_ies; 
	g_frame_temp_send_buf[g_frame_temp_len++] = 0;
	g_frame_temp_send_buf[g_frame_temp_len++] = 0;
    if(0x55 == fram_valve_sta)//开阀门
    {
        ValveDealAppStart(VALVE_STATE_OPEN);
        g_frame_temp_send_buf[0] = 0x10;
    }
    else if(0x99 == fram_valve_sta)	//应急关阀
    {
        ValveDealAppStart(VALVE_STATE_CLOSE_EMERGENCY);
        g_frame_temp_send_buf[0] = 0x20;
    }
    else if(0xBB == fram_valve_sta)	//普通关阀
    {
        ValveDealAppStart(VALVE_STATE_CLOSE);
        g_frame_temp_send_buf[0] = 0x20;
    }
    else if(0xCC== fram_valve_sta)		//权限关阀
    {
        ValveDealAppStart(VALVE_STATE_CLOSE_LOCK);
        g_frame_temp_send_buf[0] = 0x20;
    }
	else
	{
	    task_complete_ies = PTL_PACK_FAIL;
	}
	g_frame_temp_len = 2;
	task_complete_ies = PTL_PACK_SUCCESS;
# else
	task_complete_ies = PTL_PACK_FAIL;
#endif
}

//读取普通秘钥
static void frame_func_reply_NB_ReadCommonKey(void)
{
  int i;
	g_frame_temp_len =0;
	for(i=0;i<16;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.key[i];
	}
	g_frame_temp_len = 16;
	task_complete_ies = PTL_PACK_SUCCESS;
}
 //读取物理地址
static void frame_func_reply_NB_ReadMac(void)
{
	int i;
	g_frame_temp_len =0;
	for(i=0;i<7;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.mac_addr[i];
	}
	//g_frame_temp_len = 7;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//加密下初值
static void frame_func_reply_NB_WriteInitValue(void)
{
	 g_frame_temp_len=0;
    if (0x01 == Sys_data_object.P1.start_using_flg)  //出厂启用
    {
		task_complete_ies = PTL_PACK_FAIL;
        return;
    }	
	FrameSetMsgApp(InitialDataMsg); //累计流量防护2019年7月8日				
	UNION_4U8TOU32 tmp_datas_buf1,tmp_datas_buf2;
	fram_tools_writeU32ByBuf(tmp_datas_buf1.data_buf, new_message_data_rev_ies, 0);					
	fram_tools_writeU32ByBuf(tmp_datas_buf2.data_buf, (new_message_data_rev_ies + 5), 0);								
	FrameSetMsgApp(MsgBLE_AdvChange);//修改蓝牙广播信息 
	Gp30InitDataApp(tmp_datas_buf1.datas,tmp_datas_buf2.datas);		    
	task_complete_ies = PTL_PACK_SUCCESS;		
}
//读取软件版本号
static void frame_func_reply_NB_ReadSwVersion(void)
{
	g_frame_temp_len=0;

	g_frame_temp_send_buf[g_frame_temp_len++] = (PROGRAME_VERSION>>24)&0xFF;
	g_frame_temp_send_buf[g_frame_temp_len++] = (PROGRAME_VERSION>>16)&0xFF;
	g_frame_temp_send_buf[g_frame_temp_len++] = (PROGRAME_VERSION>>8)&0xFF;
	g_frame_temp_send_buf[g_frame_temp_len++] = (PROGRAME_VERSION>>0)&0xFF;
	
	task_complete_ies = PTL_PACK_SUCCESS;
}
//退出检定状态
static void frame_func_reply_NB_OutVerificationState(void)
{
	 g_frame_temp_len=0;
	if(01 == LcdCheckStateApp())      //检定状态
	{
		FrameSetMsgApp(OutVerificationMsg); //出检定msg
	}			
	task_complete_ies = PTL_PACK_SUCCESS;
}
//加密读表数据
static void frame_func_reply_NB_ReadData(void)
{
	int i=0;
	static MET_STATE temp_state; //状态字
	UNION_4U8TOU32 tmp_datas_buf;
	UNION_FLOAT_STORAGE tmp_float_datas_buf1,tmp_float_datas_buf2;
	DateTime tmp_date_buf;
	 g_frame_temp_len=0;
	/*组应答*/
	tmp_float_datas_buf1.float_storage_format.datas = Gp30CheckDataApp(GP30_APP_TOTAL_FLOW);
	tmp_datas_buf.datas = (u32) tmp_float_datas_buf1.float_datas;
	tmp_datas_buf.datas = HexToBcd32(tmp_datas_buf.datas);							
	for(i=0;i<4;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
	}	
	g_frame_temp_send_buf[g_frame_temp_len++] = 0x2C;

	tmp_float_datas_buf2.float_storage_format.datas = Gp30CheckDataApp(GP30_APP_TOTAL_FLOW_REVERSE);
	tmp_datas_buf.datas = (u32) tmp_float_datas_buf2.float_datas;
	tmp_datas_buf.datas = HexToBcd32(tmp_datas_buf.datas);			
	for(i=0;i<4;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
	}	
	g_frame_temp_send_buf[g_frame_temp_len++] = 0x2C;

	RtccGetDateTime(&tmp_date_buf);
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_date_buf.s.day; //日
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_date_buf.s.hour; //时
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_date_buf.s.minute; //分
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_date_buf.s.second; //秒
	FrameGetByte();
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat0;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat1;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat2;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat3;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat4;

	//g_frame_temp_len = 19;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读出厂序列号
static void frame_func_reply_NB_ReadSerialnum(void)
{
	g_frame_temp_len = 0;
	task_complete_ies = PTL_PACK_SUCCESS;;
}
 //校时
static void frame_func_reply_NB_CalibrateTime(void)
{
	DateTime tmp_data_time_buf;
	g_frame_temp_len = 0;
	tmp_data_time_buf.s.year =  *(new_message_data_rev_ies + 2);
	tmp_data_time_buf.s.month = *(new_message_data_rev_ies + 3);
	tmp_data_time_buf.s.day =   *(new_message_data_rev_ies + 4);
	tmp_data_time_buf.s.hour =  *(new_message_data_rev_ies + 5);
	tmp_data_time_buf.s.minute =*(new_message_data_rev_ies + 6);
	tmp_data_time_buf.s.second =*(new_message_data_rev_ies + 7);

	RtccSetDateTime(&tmp_data_time_buf);
	RtccClearError();
	PowerUnSleepClearSumSys(); //休眠管理，如果长时间不休眠的处理  2015-7-15 增加的接口。。。
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读通讯地址
static void frame_func_reply_NB_ReadAddr(void)
{
	g_frame_temp_len = 0;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//清表数据
static void frame_func_reply_NB_ClearMeterData(void)
{
	static u8 s_clear_history_index=0;
	switch(up_deal_stat_ies)
    {       
        case UP_DEAL_S0:
        {
			if (0x01 == Sys_data_object.P1.start_using_flg)  //出厂启用
			{
				task_complete_ies = PTL_PACK_FAIL;
				return;
			}
			g_frame_temp_len =0;
            up_deal_stat_ies  = UP_DEAL_S1;
        }
        break;
        case UP_DEAL_S1://清累计工作时间
        {
			if (EepIfIdle())
			{
				for(int i=0;i<256;i++)
				{
					g_frame_temp_send_buf[i]=0xFF;
				}
				s_clear_history_index = 0;
				Sys_data_object.ee_data.data.work_hour = 0; //清累计工作时间
				Sys_data_object.ee_data.data.day_data = 0xFFFFFFFF; //结算日累计值
				RtccGetDateTime(&Sys_data_object.ee_data.data.start_time_up);//重置出厂时间
				EepWrite(Sys_data_object.ee_data.byte, EE_FIRST_USE_ADDR, sizeof (RTCC_EE_DATA), &s_nb_eep_complete);
				up_deal_stat_ies  = UP_DEAL_S2;
			}
        }
        break;
		case UP_DEAL_S2://清历史数据
        {
			if (EepIfIdle())
			{
                EepWrite(g_frame_temp_send_buf, FRAME_NBIot_HISTORY_READ_ADD + s_clear_history_index*256, 256, &s_nb_eep_complete);
				up_deal_stat_ies  = UP_DEAL_S3;
			}
        }
        break;

        case UP_DEAL_S3:
        {
            if (EepIfIdle())
            {                
                s_clear_history_index++;
                if (s_clear_history_index >= 96 )//0x2000~0x7FFF
                {
					SaveNewSoe(SOE_SY_CURRENT_CLEAR,SOE_REMARK_NOR);//清表数据记录报警
					FrameSetMsgApp(ClearRealtimeDataMsg);//清实时表数据
					FrameSetMsgApp(Msg_ClearHisOther);//清表数据给其他规约消息
					up_deal_stat_ies  = UP_DEAL_S0;
                    task_complete_ies = PTL_PACK_SUCCESS;
                }
                else
                {
                    up_deal_stat_ies  = UP_DEAL_S2;
                }
            }
            break;
        }
        default:
        {
            up_deal_stat_ies  = UP_DEAL_S0;
            task_complete_ies = PTL_PACK_FAIL;
        }
        break;
    }
}
   
//读流量系数
static void frame_func_reply_NB_ReadFlowCoefficient(void)
{
	g_frame_temp_len = 0;
    if(LOW_TYPE==*new_message_data_rev_ies)
    {
      memcpy(&g_frame_temp_send_buf[0], &(cal_flow_coefficient[0].datas_map.data0),24);  
    }
    else if(MIDDLE_TYPE==*new_message_data_rev_ies)
    {
      memcpy(&g_frame_temp_send_buf[0], &(cal_flow_coefficient[6].datas_map.data0),24);  
    }
    else if(LARGE_TYPE==*new_message_data_rev_ies)
    {
      memcpy(&g_frame_temp_send_buf[0], &(cal_flow_coefficient[12].datas_map.data0),24);  
    }
	g_frame_temp_len = 24;
	task_complete_ies = PTL_PACK_SUCCESS;
}
   
//设置为检定状态
static void frame_func_reply_NB_SetVerification(void)
{
	 g_frame_temp_len=0;
	if (0 == Sys_data_object.P1.enable_verification_function)
	{
		task_complete_ies = PTL_PACK_FAIL;
		return;
	}
	Sys_data_object.P3.LCD_NoTestFlag = 0x0;
	FrameSetMsgApp(SetVerificationMsg);
    FrameClearMsgApp(MsgCaliberate);//清零 单程时间校准校准标志
	
	task_complete_ies = PTL_PACK_SUCCESS;
}
//工装进检定状态
static void frame_func_reply_TOOL_SetVerification(void)
{
	g_frame_temp_len=0;
	if (0 == Sys_data_object.P1.enable_verification_function)
	{
		task_complete_ies = PTL_PACK_FAIL;
		return;
	}
	Sys_data_object.P3.LCD_NoTestFlag = 0x55;
	FrameSetMsgApp(SetVerificationMsg);
    FrameClearMsgApp(MsgCaliberate);//清零 单程时间校准校准标志
	
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读温度系数
static void frame_func_reply_NB_ReadTemperatureCoefficient(void)
{
	int i;
	UNION_4U8TOU32  temp_4U8toU32_data;
	g_frame_temp_len=0;
	temp_4U8toU32_data.datas = FrameCheckParaApp(FramePara_Tc0);
	for(i=0;i<4;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = temp_4U8toU32_data.data_buf[i];
	}
	temp_4U8toU32_data.datas = FrameCheckParaApp(FramePara_Tc1);
	for(i=0;i<4;i++)
	{
	    g_frame_temp_send_buf[g_frame_temp_len++] = temp_4U8toU32_data.data_buf[i];
	}
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读检定数据
static void frame_func_reply_NB_ReadVerificationData(void)
{
	int i;
	static MET_STATE temp_state; //状态字
	static DateTime tmp_time_buf;
	UNION_4U8TOU32 tmp_datas_buf;
	UNION_FLOAT_STORAGE tmp_float_datas_buf;
	float tempTempratureFlag;
	 g_frame_temp_len=0;
	if(01 != LcdCheckStateApp())      //非检定状态
	{
		task_complete_ies = PTL_PACK_FAIL;
		return;
	}	
	//温度

	if(TRUE==Check_Start_STOP_Filter_Flag())//获取start-stop完成温度滤波标志
	{
		Clear_Start_STOP_Filter_Flag();//清零start-stop完成温度滤波标志
		tempTempratureFlag = Check_Temperature_Parameter();//传递滤波的反算温度数据
	}
	else
	{
		tempTempratureFlag = TempCheckValueApp();//获取实时温度
	}
	
	if(tempTempratureFlag < 0)  
	{
		tempTempratureFlag = -tempTempratureFlag;
		tmp_datas_buf.datas = HexToBcd32((u32)(tempTempratureFlag*100));
		tmp_datas_buf.datas|= 0x800000;
	}
	else
	{
	tmp_datas_buf.datas = HexToBcd32((u32)(tempTempratureFlag*100));
	tmp_datas_buf.datas &= 0x7FFFFF;
	}  
  for(i=0;i<3;i++)
  {
    g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
  }		
	//累计流量
	tmp_float_datas_buf.float_storage_format.datas = Gp30CheckTestDataApp(GP30_APP_TOTAL_FLOW); //累计流量//BCD
	tmp_datas_buf.datas = (u32) (tmp_float_datas_buf.float_datas);
	tmp_datas_buf.datas = HexToBcd32(tmp_datas_buf.datas);
  for(i=0;i<4;i++)
  {
    g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
  }	
	g_frame_temp_send_buf[g_frame_temp_len++] = 0x2C; //m?
	//瞬时流量
	tmp_datas_buf.datas = Gp30CheckTestDataApp(GP30_APP_INS_FLOW); //瞬时流量(四位小数，放大100000倍)//BCD
	tmp_datas_buf.datas = Gp30DataToBcd(tmp_datas_buf.datas, 100);
  for(i=0;i<4;i++)
  {
    g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
  }	
	g_frame_temp_send_buf[g_frame_temp_len++] = 0x35; //m?/h
	//up单程时间
  tmp_datas_buf.datas = Gp30CheckTestDataApp(GP30_APP_TOF);//up单程时间获取 
  for(i=0;i<4;i++)
  {
    g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
  }	
	//Magnify 100000 times（m3/h）：Real-time instantaneous flow
	tmp_datas_buf.datas = Gp30CheckTestDataApp(GP30_APP_INS_FLOW_LCD); 
	tmp_datas_buf.datas = Gp30DataToBcd(tmp_datas_buf.datas, 100);
  for(i=0;i<4;i++)
  {
    g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
  }	
	//tof
	tmp_datas_buf.datas = Gp30CheckTestDataApp(GP30_APP_DIFF); //时间差
  for(i=0;i<4;i++)
  {
    g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
  }	
	////累计工作时间//BCD
	tmp_datas_buf.datas = RtccGetWorkHourApp(); 
	tmp_datas_buf.datas = HexToBcd32(tmp_datas_buf.datas);
  for(i=0;i<3;i++)
  {
    g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
  }	
	//实时时间
	RtccGetDateTime(&tmp_time_buf); 
	g_frame_temp_send_buf[g_frame_temp_len++] = 0x20;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.year;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.month;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.day;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.hour;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.minute;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.second;
	//状态字
	temp_state = FrameGetByte();
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat0;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat1;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat2;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat3;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat4;
   //************************新增数据:15字节***********************/
  //反算温度T0-T2（T2）:3字节
    //温度T0-T2（T2）:3字节
   #if WENDU_FANSUAN_OPEN
	tempTempratureFlag = TempCheckValueApp();//传递实时的反算数据
					
	if(tempTempratureFlag < 0)  
	{
		tempTempratureFlag = -tempTempratureFlag;
		tmp_datas_buf.datas = HexToBcd32((u32)(tempTempratureFlag*100));
		tmp_datas_buf.datas|= 0x800000;
	}
	else
	{
        tmp_datas_buf.datas = HexToBcd32((u32)(tempTempratureFlag*100));
        tmp_datas_buf.datas &= 0x7FFFFF;
	}
	for(i=0;i<3;i++)
    {
        g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.data_buf[i];
    }
	tmp_datas_buf.datas = Gp30CheckDataApp(11);	//TOF_UP补偿系数
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data1;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data2;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data3;
	 
	tmp_datas_buf.datas = Gp30CheckDataApp(12);//TOF_DOWN补偿系数
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data1;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data2;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data3;
	#else//常规程序反算温度默认写0
	for(i=0;i<3;i++)
    {
        g_frame_temp_send_buf[g_frame_temp_len++] = 0;
    }	
    //up amplitude(V1):4字节
	tmp_datas_buf.datas = Gp30CheckTestDataApp(GP30_APP_PEAK_UP);
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data1;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data2;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data3;
	 //down amplitude(V2):4字节
	tmp_datas_buf.datas = Gp30CheckTestDataApp(GP30_APP_PEAK_DOWN);
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data1;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data2;
	g_frame_temp_send_buf[g_frame_temp_len++]  = tmp_datas_buf.datas_map.data3;
	#endif
	//预留:4字节
    tmp_datas_buf.datas = Gp30CheckTestDataApp(GP30_APP_DOWN);//down 的单程时间       
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.datas_map.data1;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.datas_map.data2;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.datas_map.data3;
	
	//g_frame_temp_len = 55;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读检定数据
static void frame_func_reply_TOOL_ReadVerificationData(void)
{
	int i;
	UNION_FLOAT_STORAGE tmp_float_datas_buf;

	g_frame_temp_len=0;
	//累计流量
	tmp_float_datas_buf.float_storage_format.datas = Gp30CheckTestDataApp(GP30_APP_TOTAL_FLOW); //累计流量//BCD
	for(i=0;i<4;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = tmp_float_datas_buf.float_storage_format.data_buf[i];
	}
    //负向也用这个值
	for(i=0;i<4;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++] = tmp_float_datas_buf.float_storage_format.data_buf[i];
	}	
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读瞬时流量和温度
static void frame_func_reply_NB_ReadInstantFlowAndTemperature(void)
{
	MET_STATE temp_state; //状态字
	static DateTime tmp_time_buf;
	UNION_4U8TOU32 tmp_datas_buf;
	UNION_FLOAT_STORAGE tmp_float_datas_buf;
	g_frame_temp_len=0;
	tmp_float_datas_buf.float_storage_format.datas = Gp30CheckDataApp(GP30_APP_TOTAL_FLOW); //当前累积流量
	tmp_datas_buf.datas = (u32) tmp_float_datas_buf.float_datas;
	tmp_datas_buf.datas = HexToBcd32(tmp_datas_buf.datas);
	fram_tools_writeU32ByBuf(&g_frame_temp_send_buf[g_frame_temp_len], tmp_datas_buf.data_buf, 0);
	g_frame_temp_len+=4;
	g_frame_temp_send_buf[g_frame_temp_len++] = 0x2C;//m3
	tmp_datas_buf.datas = Gp30CheckDataApp(GP30_APP_INS_FLOW); //瞬时流量(四位小数，放大100000倍)//BCD
	tmp_datas_buf.datas = Gp30DataToBcd(tmp_datas_buf.datas, 100);
	fram_tools_writeU32ByBuf(&g_frame_temp_send_buf[5], tmp_datas_buf.data_buf, 0);
	g_frame_temp_len+=4;
	g_frame_temp_send_buf[g_frame_temp_len++] = 0x35;//m3/h
	float tmep = TempCheckValueApp();      //							
	if(tmep <0)
	{				
		tmp_datas_buf.datas = (u32)(tmep*(-100));	
		tmp_datas_buf.datas &= 0x0FFFF;
		tmp_datas_buf.datas |= 0x80000;
	}
	else
	{
		tmp_datas_buf.datas = (u32)(tmep*(100));	
		tmp_datas_buf.datas &= 0x7FFFF;
	}
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.datas_map.data1;
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_datas_buf.datas_map.data2;
	RtccGetDateTime(&tmp_time_buf);
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.day; //日
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.hour; //时
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.minute; //分
	g_frame_temp_send_buf[g_frame_temp_len++] = tmp_time_buf.s.second; //秒
	temp_state = FrameGetByte();
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat0;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat1;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat2;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat3;
	g_frame_temp_send_buf[g_frame_temp_len++] = temp_state.stat4;
	//g_frame_temp_len = 22;
	task_complete_ies = PTL_PACK_SUCCESS;
}
//启动停止测试
static void frame_func_reply_NB_StartOrStopTest(void)
{
	g_frame_temp_len = 0;
	if(01 != LcdCheckStateApp())      
	{
		task_complete_ies = PTL_PACK_FAIL;
		return;
	}	
	#if WENDU_FANSUAN_OPEN
	FrameClearMsgApp(MsgCaliberate);//清零 单程时间校准校准标志
	GP30_tof_buchang_filter(NOT_ALLOW_CALIBREATE);//这里可以有效预防NB上位机导致表计校准
	#endif	
	if(0x01 == *new_message_data_rev_ies)      //start
	{
		Gp30StartMeasureApp(1);
	}
	else if(0x00 == *new_message_data_rev_ies) //stop 
	{
		Gp30StartMeasureApp(0);
	}   
	g_frame_temp_send_buf[g_frame_temp_len++] = *new_message_data_rev_ies;

	task_complete_ies = PTL_PACK_SUCCESS;
}

//RTC校准
static void frame_func_reply_NB_RtcCalibtation(void)
{
	g_frame_temp_len = 0;
	if(0x01 == *new_message_data_rev_ies)
	{
		FrameSetMsgApp(CalibrateRtcMsg);//抛RTC校准msg
	}
	g_frame_temp_send_buf[g_frame_temp_len++] = *new_message_data_rev_ies;   
	g_frame_temp_send_buf[g_frame_temp_len++] = 0;  //sign + || -：0减  1加 
	g_frame_temp_send_buf[g_frame_temp_len++] = 200;//data 校准值

	task_complete_ies = PTL_PACK_SUCCESS;
}
//表计低功耗（测试规约）
static void frame_func_reply_NB_EnterLowPower(void)
{
	 g_frame_temp_len =0;
	if(0x01 == ((*new_message_data_rev_ies) & 0x01))   //表计低功耗
	{
		if (0x01 == Sys_data_object.P1.start_using_flg)  //出厂启用
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}
		 FrameSetMsgApp(LowPowerMsg);//s_struct_msg.MsgStruct.low_power_msg = 1;     //抛表计低功耗msg
		if(01 == LcdCheckStateApp())      //检定状态
		{
			FrameSetMsgApp(OutVerificationMsg);//s_struct_msg.MsgStruct.out_verification_msg = 1;  //出检定msg
		}
	}    
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读功能配置字
static void frame_func_reply_NB_RdFunctionCfg(void)
{
	int i=0;
	g_frame_temp_len =0;
	for(i = 0; i<32 ;i++)
	{
		g_frame_temp_send_buf[g_frame_temp_len++]	= Sys_data_object.P1.func_cfg_bytes[i];
	}
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读取始动流量
static void frame_func_reply_NB_ReadStartFlow(void)
{
    UNION_2U8TOU16 temp_data;
	g_frame_temp_len =0;
    temp_data.datas = (Sys_data_object.P1.start_flow.datas *(float)250 /(float)65536/DIFTOF_COEFFICIENT*(float)100);//转化为L/h格式;
    g_frame_temp_send_buf[g_frame_temp_len++] = temp_data.datas_map.data0;
    g_frame_temp_send_buf[g_frame_temp_len++] = temp_data.datas_map.data1;

	task_complete_ies = PTL_PACK_SUCCESS;
}
//读取逆流计量开关
static void frame_func_reply_NB_ReadCounterCurrent(void)
{
	g_frame_temp_len =0;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.counter_current_flow.datas_map.data0;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.counter_current_flow.datas_map.data1;

	task_complete_ies = PTL_PACK_SUCCESS;
}

//硬件自检控制字
static void frame_func_reply_NB_TestSelf(void)
{
    switch(up_deal_stat_ies)
    {       
        case UP_DEAL_S0:
        {
			g_frame_temp_len =0;
            SelfTest_Start();
            s_frame_temp_timer_tick = Timer1usGetTick();
            up_deal_stat_ies  = UP_DEAL_S1;
        }
        break;
        case UP_DEAL_S1:
        {
            SelfTest_Proc();//自检流程
            u8 temp_len = SelfTest_GetResult(g_frame_temp_send_buf);
            if(0 != temp_len)
            {
                up_deal_stat_ies  = UP_DEAL_S0;
                task_complete_ies = PTL_PACK_SUCCESS;
                g_frame_temp_len = temp_len;
            }
            else if((Timer1usGetTick() - s_frame_temp_timer_tick)>6000000)//6s 
            {
                SelfTest_End();
                up_deal_stat_ies  = UP_DEAL_S0;
                task_complete_ies = PTL_PACK_FAIL;
            } 
        }
        break;
        default:
        {
            up_deal_stat_ies  = UP_DEAL_S0;
            task_complete_ies = PTL_PACK_FAIL;
        }
        break;
    }
}


//读取时间段SOE记录
static void frame_func_reply_NB_ReadSoe(void)
{
	static u8 num_get,num_to_get,num_to_read,totalnum;
    static mtype16_ temp_soe_type;
    static ONE_SOE soe_event_read;
    static DateTime_noweek start_time;
    static DateTime_noweek end_time;

    switch(up_deal_stat_ies)
    {
        case UP_DEAL_S0:
        {
            totalnum = 0;
            num_get = 0;
            num_to_read = Sys_data_object.P4.Point_SOE_write;
            temp_soe_type.to_uint8[0] = *(new_message_data_rev_ies); //获取事件代码类型
            temp_soe_type.to_uint8[1] = *(new_message_data_rev_ies+1);
			start_time.s.year   = *(new_message_data_rev_ies + 2);				//起始时间
            start_time.s.month  = *(new_message_data_rev_ies + 3);
            start_time.s.day    = *(new_message_data_rev_ies + 4);
            start_time.s.hour   = *(new_message_data_rev_ies + 5);
            start_time.s.minute = *(new_message_data_rev_ies + 6);
            start_time.s.second = *(new_message_data_rev_ies + 7);

            end_time.s.year   = *(new_message_data_rev_ies + 8);					//结束时间
            end_time.s.month  = *(new_message_data_rev_ies + 9);
            end_time.s.day    = *(new_message_data_rev_ies + 10);
            end_time.s.hour   = *(new_message_data_rev_ies + 11);
            end_time.s.minute = *(new_message_data_rev_ies + 12);
            end_time.s.second = *(new_message_data_rev_ies + 13);

            num_to_get = *(new_message_data_rev_ies + 14);							//读取数量
			if((0 == num_to_get) || (MAX_NUM_READ_SOE <= num_to_get))
			{
				up_deal_stat_ies = UP_DEAL_S3;
			}
			else
			{
				up_deal_stat_ies = UP_DEAL_S1;
			}
       }
       break;
	   
       case UP_DEAL_S1://读取一条SOE
       {
           if(totalnum >= TOTAL_NUM_SOE)//总共读TOTAL_NUM_SOE次  强制退出不能再读了
           {
                up_deal_stat_ies = UP_DEAL_S3;
                break;
           }
           if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
           {
                totalnum++;
                if(num_to_read > TOTAL_NUM_SOE) //防止指针跑飞了
                {
                    num_to_read = TOTAL_NUM_SOE;
                }
                if(num_to_read == 0)
                {
                    num_to_read = TOTAL_NUM_SOE;
                }
                num_to_read--;
                EepRead(EEP_ADDR_SOE_BASE + (u16)num_to_read*LENGTH_ONE_SOE,(u8*)&soe_event_read,LENGTH_ONE_SOE, &s_nb_eep_complete);
            	up_deal_stat_ies = UP_DEAL_S2;
		   }
          
        }
        break;
		
        case UP_DEAL_S2:
        {
             if(TASK_IDLE == EepIfIdle())//读完了
             {
                 if(TRUE == Check_One_Soe(temp_soe_type.to_uint16,&start_time,&end_time,&soe_event_read))//检测到一条有效的SOE
                 {
                     memcpy(g_frame_temp_send_buf + 1 + (u16)num_get * LENGTH_ONE_SOE,&soe_event_read,LENGTH_ONE_SOE);
                     num_get++;
                     if(num_get >= num_to_get)//读完了
                     {
                         up_deal_stat_ies = UP_DEAL_S3;
                         break;
                     }
                 }
                 up_deal_stat_ies = UP_DEAL_S1;
            }
        }
        break;
		
        case UP_DEAL_S3:
        {
			up_deal_stat_ies = UP_DEAL_S0;
            g_frame_temp_send_buf[0] = num_get;
			g_frame_temp_len = (1 + (u16) num_get * LENGTH_ONE_SOE);
			task_complete_ies = PTL_PACK_SUCCESS;  
        }
        break;
		
        default:
        {
            up_deal_stat_ies  = UP_DEAL_S0;
			task_complete_ies = PTL_PACK_FAIL;  
        }
        break;
    }
}
/*老升级规约不使用
//升级规约
static void frame_func_s3_NB_BootLoad(void)
{
    static u8 s_bootloader_version_flg = 0; //远程升级版本比较结果

    switch(up_deal_stat_ies)
    {
        case UP_DEAL_S0:
        {
			if(TASK_IDLE == EepIfIdle())
			{
				g_frame_temp_len = 0;
				Sys_data_object.P2.mcu = *(new_message_data_rev_ies); //平台类型
				Sys_data_object.P2.mcu_model = *(new_message_data_rev_ies + 1);//MCU型号
				Sys_data_object.P2.product_model = *(new_message_data_rev_ies + 2);//产品类型
				Sys_data_object.P2.hardware_version = *(new_message_data_rev_ies + 3);//硬件版本

				if ((Sys_data_object.P2.hardware_version == HARDWARE_VERSION_USED)&&//硬件版本2.0
					(0x22== Sys_data_object.P2.mcu) && //高:0-MIC, 1-ST, 2-HC32; 低:0-水表,1-气表,2-超水,3-超气
						(0x00 == Sys_data_object.P2.mcu_model)&&//0x00--HC32L196
			#if defined(VALVE)
						((BCD2HEX(GP30_APP_DNX)+1) == Sys_data_object.P2.product_model))//产品类型;有阀口径+1
			#else
						(BCD2HEX(GP30_APP_DNX) == Sys_data_object.P2.product_model))//产品类型;表计口径
			#endif	
				{
					//软件版本
					fram_tools_writeU32ByBuf(Sys_data_object.P2.soft_version, (new_message_data_rev_ies + 10), 0);
					//升级总字节数
					fram_tools_writeU32ByBuf(Sys_data_object.P2.update_num.data_buf, (new_message_data_rev_ies + 4), 0);
					//升级校验码
					Sys_data_object.P2.update_check.datas_map.data0 = *(new_message_data_rev_ies + 8);
					Sys_data_object.P2.update_check.datas_map.data1 = *(new_message_data_rev_ies + 9);	
					//升级标志
					Sys_data_object.P2.bootloader_update_flg = 0x55;
					//升级方式 
					if(frame_get_datasource() == UP_SOURCE_IR)//如果是串口
					{ 
						Sys_data_object.P2.bootloader_datas_sourse = 1;//串口升级
					}
					else if(frame_get_datasource() == UP_SOURCE_NB)//如果是NB通信
					{
						Sys_data_object.P2.bootloader_datas_sourse = 2;//NB升级
					}
					else if(frame_get_datasource() == UP_SOURCE_BLE)//如果是蓝牙
					{
						Sys_data_object.P2.bootloader_datas_sourse = 3;//蓝牙升级
					}
					//包号清0
					Sys_data_object.P2.bootloader_paknum.datas =0;
					//程序破坏标志清0
					Sys_data_object.P2.bootloader_RSSI = 0;
					s_bootloader_version_flg = 0x00;
					FrameSetMsgApp(MsgUpdateSoftVersionCheckOk);
	                EepWrite(&Sys_data_object.P2.soft_version[0], EEP_SOFT_VERSION, 17, &s_nb_eep_complete);
				}
				else
				{
					s_bootloader_version_flg = 0x10;
					if (0x22 != Sys_data_object.P2.mcu)
					{
						s_bootloader_version_flg = s_bootloader_version_flg|0x01;
					}
					if(0x00 != Sys_data_object.P2.mcu_model)
					{
						s_bootloader_version_flg = s_bootloader_version_flg|0x02;
					}
					if(Sys_data_object.P2.hardware_version != HARDWARE_VERSION_USED)
					{
						s_bootloader_version_flg = s_bootloader_version_flg|0x04;
					}
				#if defined(VALVE)
					if((BCD2HEX(GP30_APP_DNX)+1) != Sys_data_object.P2.product_model)//产品类型;有阀口径+1
				#else
					if(BCD2HEX(GP30_APP_DNX) != Sys_data_object.P2.product_model)//产品类型;表计口径
				#endif	
					{
						s_bootloader_version_flg = s_bootloader_version_flg|0x08;
					}
				}
				up_deal_stat_ies  = UP_DEAL_S1;
			}
		}
		break;
		case UP_DEAL_S1:
        {
			if(TASK_IDLE == EepIfIdle())//存储数据完成
			{
				g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.mcu;
				g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.mcu_model;
				g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.product_model;
				g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.hardware_version;
				fram_tools_writeU32ByBuf(&g_frame_temp_send_buf[4], Sys_data_object.P2.soft_version, 0);
				g_frame_temp_len +=4;
				g_frame_temp_send_buf[g_frame_temp_len++] = s_bootloader_version_flg;
				//g_frame_temp_len = 9;
				up_deal_stat_ies  = UP_DEAL_S0;
				task_complete_ies = PTL_PACK_SUCCESS;
			}
        }
        break;
        default:
        {
            up_deal_stat_ies  = UP_DEAL_S0;
            task_complete_ies = PTL_PACK_FAIL;
        }
        break;
    }
}*/
//新升级规约  
static void frame_func_s3_NB_diff_BootLoad(void)
{
	static u8 s_bootloader_version_flg = 0; //远程升级版本比较结果

	switch(up_deal_stat_ies)
	{
		case UP_DEAL_S0:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				u8 temp_update_access=0;
				g_frame_temp_len = 0;
				Sys_data_object.P2.mcu = *(new_message_data_rev_ies + 0);//平台类型
				Sys_data_object.P2.mcu_model = *(new_message_data_rev_ies + 1);//MCU型号
				Sys_data_object.P2.product_model = *(new_message_data_rev_ies + 2);//产品型号
				Sys_data_object.P2.hardware_version = *(new_message_data_rev_ies + 3);//硬件版本号
				u16 software_version = (AsciiToHex(*(new_message_data_rev_ies + 10))<<12)+
				                       (AsciiToHex(*(new_message_data_rev_ies + 11))<<8)+
				                       (AsciiToHex(*(new_message_data_rev_ies + 12))<<4)+
				                       (AsciiToHex(*(new_message_data_rev_ies + 13))<<0);
				if(Sys_data_object.P2.product_model&0x80)//同版本升级标志位
				{
					if(software_version == (PROGRAME_VERSION>>16))
					{
						Sys_data_object.P2.product_model &= 0x7F;//去掉标志位,兼容已定义产品类型 
						temp_update_access =1;
					}
				}
				else
				{
					if(software_version != (PROGRAME_VERSION>>16))
					{
						temp_update_access =1;
					}
				}
				if ((Sys_data_object.P2.hardware_version == HARDWARE_VERSION_USED)&&//硬件版本2.4
					(0x22== Sys_data_object.P2.mcu) && //高:0-MIC, 1-ST, 2-HC32; 低:0-水表,1-气表,2-超水,3-超气
					(0x00 == Sys_data_object.P2.mcu_model)&&//0x00--HC32L196
					(temp_update_access)&&//版本号防护
				#if defined(VALVE)
					((BCD2HEX(GP30_APP_DNX)+1) == Sys_data_object.P2.product_model))//产品类型;有阀口径+1
				#else
					(BCD2HEX(GP30_APP_DNX) == Sys_data_object.P2.product_model))//产品类型;表计口径
				#endif	
				{
					//软件版本
					fram_tools_writeU32ByBuf(Sys_data_object.P2.soft_version, (new_message_data_rev_ies + 10), 0);
					//升级总字节数
					fram_tools_writeU32ByBuf(Sys_data_object.P2.update_num.data_buf, (new_message_data_rev_ies + 4), 0);
					//升级校验码
					Sys_data_object.P2.update_check.datas_map.data0 = *(new_message_data_rev_ies + 8);
					Sys_data_object.P2.update_check.datas_map.data1 = *(new_message_data_rev_ies + 9);	
					//升级通道:1-串口;2-coap;3-udp
					Sys_data_object.P2.bootloader_datas_sourse = *(new_message_data_rev_ies + 14);
					//升级标志/方式:1-完整升级;2-差分升级
					if(*(new_message_data_rev_ies + 15) == 2)
					{
						Sys_data_object.P2.bootloader_update_flg = 0x77;//差分升级
					}
					else
					{
						Sys_data_object.P2.bootloader_update_flg = 0x55;//全包升级
					}
				#if UPLOAD_MODE_1
					if(Sys_data_object.P2.bootloader_datas_sourse != 1)//不是串口升级
					{
						setUploadControlPara(UP_BOOT_MSG, Sys_data_object.P2.bootloader_update_flg);//升级功耗计算
					}
				#endif
					
					//包号清0
					Sys_data_object.P2.bootloader_paknum.datas =0;
					//程序破坏标志清0
					Sys_data_object.P2.bootloader_RSSI = 0;
					s_bootloader_version_flg = 0x00;
					FrameSetMsgApp(MsgUpdateSoftVersionCheckOk);
					EepWrite(&Sys_data_object.P2.soft_version[0], EEP_SOFT_VERSION, 17, &s_nb_eep_complete);
				}
				else
				{
					s_bootloader_version_flg = 0x10;
					if(software_version == (PROGRAME_VERSION>>16))//版本相同
					{
						s_bootloader_version_flg |= 0x20;
					}
					if (0x22 != Sys_data_object.P2.mcu)
					{
						s_bootloader_version_flg = s_bootloader_version_flg|0x01;
					}
					if(0x00 != Sys_data_object.P2.mcu_model)
					{
						s_bootloader_version_flg = s_bootloader_version_flg|0x02;
					}
					if(Sys_data_object.P2.hardware_version != HARDWARE_VERSION_USED)
					{
						s_bootloader_version_flg = s_bootloader_version_flg|0x04;
					}
					#if defined(VALVE)
						if((BCD2HEX(GP30_APP_DNX)+1) != Sys_data_object.P2.product_model)//产品类型;有阀口径+1
					#else
						if(BCD2HEX(GP30_APP_DNX) != Sys_data_object.P2.product_model)//产品类型;表计口径
					#endif	
					{
						s_bootloader_version_flg = s_bootloader_version_flg|0x08;
					}
				}
				up_deal_stat_ies  = UP_DEAL_S1;
			}
		}
		break;
		case UP_DEAL_S1:
		{
			if(TASK_IDLE == EepIfIdle())//存储数据完成
			{
				g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.mcu;
				g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.mcu_model;
				g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.product_model;
				g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P2.hardware_version;
				fram_tools_writeU32ByBuf(&g_frame_temp_send_buf[4], Sys_data_object.P2.soft_version, 0);
				g_frame_temp_len +=4;
				//升级版本后加升级通道与升级方式
				g_frame_temp_send_buf[g_frame_temp_len++]=Sys_data_object.P2.bootloader_datas_sourse;//升级通道
				g_frame_temp_send_buf[g_frame_temp_len++] = *(new_message_data_rev_ies + 15);
				g_frame_temp_send_buf[g_frame_temp_len++] = s_bootloader_version_flg;
				//g_frame_temp_len = 11; 
				task_complete_ies = PTL_PACK_SUCCESS;
				if(s_bootloader_version_flg==0)
				{
					FrameSetMsgApp(BootloaderMsg);		
					if(01 == LcdCheckStateApp())      //检定状态
					{
						FrameSetMsgApp(OutVerificationMsg);//出检定msg
					}
				}
				up_deal_stat_ies  = UP_DEAL_S0;
			}
		}
		break;
		default:
		{
			up_deal_stat_ies  = UP_DEAL_S0;
			task_complete_ies = PTL_PACK_FAIL;
		}
		break;
	}
}			
//加密设置通讯地址和无磁兼容
static void frame_func_s3_NB_SetAddr(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0; 
		if(0x01 == Sys_data_object.P1.start_using_flg)
		{
			task_complete_ies = PTL_PACK_FAIL; 
			return;
		}
		for (int tmp_i = 0; tmp_i < 7; tmp_i++)
		{
			Sys_data_object.P1.met_addr[tmp_i] = *(new_message_data_rev_ies + tmp_i);
		}		
		EepWrite(&Sys_data_object.P1.met_addr[0], EEP_FRAME_MET_ADDR, 7, &s_nb_eep_complete);
		Frame_Para_Init();
		FrameSetMsgApp(MsgAddr_Change);//更新MAC地址
		task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//写温度系数
static void frame_func_s3_NB_SetTempratureConefficient(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0; 
		if(0x01 == Sys_data_object.P1.start_using_flg)
		{
			task_complete_ies = PTL_PACK_FAIL; 
			return;
		}
		fram_tools_writeU32ByBuf(Sys_data_object.P1.temperature_coefficient0.data_buf, (new_message_data_rev_ies + 0), 1);
		fram_tools_writeU32ByBuf(Sys_data_object.P1.temperature_coefficient1.data_buf, (new_message_data_rev_ies + 4), 1);
		EepWrite(&Sys_data_object.P1.temperature_coefficient0.datas_map.data0, EEP_FRAME_TEMPERATURE_COEFFICIENT_ADDR, 8, &s_nb_eep_complete);
		Frame_Para_Init();
		task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//写流量系数 **修改写流量系数限制（0.5-1.5）2019年7月5日
static void frame_func_s3_NB_SetFowcoefficient(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		if(0x01 == Sys_data_object.P1.start_using_flg)
		{
			task_complete_ies = PTL_PACK_FAIL;     
			return;
		}
		UNION_4U8TOU32 flow_small;
		UNION_4U8TOU32 flow_medium0;
		UNION_4U8TOU32 flow_medium1;
		UNION_4U8TOU32 flow_medium2;
		UNION_4U8TOU32 flow_medium3;
		UNION_4U8TOU32 flow_large;
		fram_tools_writeU32ByBuf(flow_small.data_buf, (new_message_data_rev_ies + 0), 0);
		fram_tools_writeU32ByBuf(flow_medium0.data_buf, (new_message_data_rev_ies + 4), 0);
		fram_tools_writeU32ByBuf(flow_medium1.data_buf, (new_message_data_rev_ies + 8), 0);
		fram_tools_writeU32ByBuf(flow_medium2.data_buf, (new_message_data_rev_ies + 12), 0);
		fram_tools_writeU32ByBuf(flow_medium3.data_buf, (new_message_data_rev_ies + 16), 0);
		fram_tools_writeU32ByBuf(flow_large.data_buf, (new_message_data_rev_ies + 20), 0);
		if(( (FLOW_COEFFICIENT_MIN < flow_small.datas) &&  (flow_small.datas < FLOW_COEFFICIENT_MAX) ) && ( (FLOW_COEFFICIENT_MIN < flow_medium0.datas) &&  (flow_medium0.datas < FLOW_COEFFICIENT_MAX) ) 
		&& ( (FLOW_COEFFICIENT_MIN < flow_medium1.datas) &&  (flow_medium1.datas < FLOW_COEFFICIENT_MAX) ) && ( (FLOW_COEFFICIENT_MIN < flow_medium2.datas) &&  (flow_medium2.datas < FLOW_COEFFICIENT_MAX) ) 
		&& ( (FLOW_COEFFICIENT_MIN < flow_medium3.datas) &&  (flow_medium3.datas < FLOW_COEFFICIENT_MAX) ) && ( (FLOW_COEFFICIENT_MIN < flow_large.datas) &&  (flow_large.datas < FLOW_COEFFICIENT_MAX) ))
		{
			if(LOW_TYPE==*(new_message_data_rev_ies + 24))
			{
				memcpy(&(cal_flow_coefficient[0].datas_map.data0), new_message_data_rev_ies, 24);
				EepWrite(&(cal_flow_coefficient[0].datas_map.data0), FRAME_FLOW_LOW_COEFFICIENT_ADDR, 24, &s_nb_eep_complete); 
			}
			else if(MIDDLE_TYPE==*(new_message_data_rev_ies + 24))
			{
				memcpy(&(cal_flow_coefficient[6].datas_map.data0), new_message_data_rev_ies, 24);
				EepWrite(&(cal_flow_coefficient[6].datas_map.data0), FRAME_FLOW_MIDDLE_COEFFICIENT_ADDR, 24, &s_nb_eep_complete); 
			}
			else if(LARGE_TYPE==*(new_message_data_rev_ies + 24))
			{
				memcpy(&(cal_flow_coefficient[12].datas_map.data0), new_message_data_rev_ies, 24);
				EepWrite(&(cal_flow_coefficient[12].datas_map.data0), FRAME_FLOW_HIGH_COEFFICIENT_ADDR, 24, &s_nb_eep_complete); 
			}
			else if(ALL_TYPE==*(new_message_data_rev_ies + 24))
			{
				memcpy(&(cal_flow_coefficient[0].datas_map.data0), default_flow_coefficient, sizeof (default_flow_coefficient));
				EepWrite(&(cal_flow_coefficient[0].datas_map.data0), FRAME_FLOW_LOW_COEFFICIENT_ADDR, 72, &s_nb_eep_complete); 
			}
			Frame_Para_Init();
			task_complete_ies = PTL_PACK_SUCCESS;        
		}
		else
		{
			task_complete_ies = PTL_PACK_FAIL; 
			return;
		}
	}
}


//启用关闭检定或红外功能
static void frame_func_s3_NB_SwitchVerificationOrInfrared(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		Sys_data_object.P1.enable_verification_function = *(new_message_data_rev_ies + 0);
		Sys_data_object.P1.enable_infrared_function = *(new_message_data_rev_ies + 1);
		EepWrite(&Sys_data_object.P1.enable_verification_function, EEP_FRAME_VERIFICATION_FUNCTION_ADDR, 2, &s_nb_eep_complete);
		Frame_Para_Init();
        task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//配置功能
static void frame_func_s3_NB_CfgFunctioncfg(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		UNION_4U8TOU32 temp_flag_bytes;
		fram_tools_writeU32ByBuf(temp_flag_bytes.data_buf, (new_message_data_rev_ies + 0), 0);
		if (temp_flag_bytes.datas > 0)
		{
			for(int temp_i = 0; temp_i < 32; temp_i++)
			{
				if (0x00000001 == ((temp_flag_bytes.datas >> temp_i) & 0x00000001))
				{
					Sys_data_object.P1.func_cfg_bytes[temp_i] = *(new_message_data_rev_ies + 4 + temp_i);
				}
			}
			EepWrite(&Sys_data_object.P1.func_cfg_bytes[0], EEP_FRAME_FUNCCFGBYTES_ADDR, 32, &s_nb_eep_complete);
		}		
		Frame_Para_Init();
		FrameSetMsgApp(MsgBLE_ConfigChange);
		task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//(清)出厂启用
static void frame_func_s3_NB_SetOrClearUsing_SAcrc(void)
{
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		if (0x01 == *(new_message_data_rev_ies + 0))
		{
			Sys_data_object.P1.start_using_flg = 0x01;
		}
		else if (0x00 == *(new_message_data_rev_ies + 0))
		{
			Sys_data_object.P1.start_using_flg = 0x00;
		}
		else
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}
		EepWrite(&Sys_data_object.P1.start_using_flg, EEP_FRAME_STARTUSING_ADDR, 1, &s_nb_eep_complete);
		Frame_Para_Init();
		task_complete_ies = PTL_PACK_SUCCESS;
	}
}


//设置公共秘钥
static void frame_func_s3_NB_SetCommonKey(void)
{   
	if(TASK_IDLE == EepIfIdle())
	{
		g_frame_temp_len = 0;
		for(int i = 0; i < 16; i++)
		{
			Sys_data_object.P1.key[i] = *(new_message_data_rev_ies + i);
		}
		EepWrite(&Sys_data_object.P1.key[0], FRAME_NBIot_KEY, 16, &s_nb_eep_complete);
		task_complete_ies = PTL_PACK_SUCCESS;
	}
}

//设置始动流量
static void frame_func_s3_NB_SetStartFlow(void)
{   
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		if (0x01 == Sys_data_object.P1.start_using_flg)  //出厂启用
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}
		UNION_2U8TOU16 temp_flow;
		u16  convert_to_ps;//转为ps
		temp_flow.datas_map.data0 = *(new_message_data_rev_ies + 0);
		temp_flow.datas_map.data1= *(new_message_data_rev_ies + 1);
		convert_to_ps= (temp_flow.datas * (float)10 *DIFTOF_COEFFICIENT);//转化为ps
		if(( convert_to_ps <MIN_FLOW_TO_PS)||( convert_to_ps >MAX_FLOW_TO_PS))
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}
		temp_flow.datas = ((float)temp_flow.datas/(float)100*DIFTOF_COEFFICIENT*(float)65536/(float)250);//转化为Gp30格式;在Frame_Para_Init做转换
		Sys_data_object.P1.start_flow.datas = temp_flow.datas;
		EepWrite(&Sys_data_object.P1.start_flow.datas_map.data0, EEP_FRAME_START_FLOW_ADDR, 2, &s_nb_eep_complete);
		Frame_Para_Init();
		task_complete_ies = PTL_PACK_SUCCESS;       
	}
}

//设置逆流计量方式
static void frame_func_s3_NB_SetCounterCurrent(void)
{   
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len = 0;
		UNION_2U8TOU16 frame_temp;
		frame_temp.datas_map.data0 = *(new_message_data_rev_ies + 0);
		frame_temp.datas_map.data1= *(new_message_data_rev_ies + 1);
		if((frame_temp.datas !=0x2B1A)&&(frame_temp.datas !=0x4D3C))
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}	
		else if(frame_temp.datas != Sys_data_object.P1.counter_current_flow.datas )
		{
			FrameSetMsgApp(ModelChangeMsg);
		}
		Sys_data_object.P1.counter_current_flow.datas_map.data0 = *(new_message_data_rev_ies + 0);
		Sys_data_object.P1.counter_current_flow.datas_map.data1 = *(new_message_data_rev_ies + 1);
		EepWrite(&Sys_data_object.P1.counter_current_flow.datas_map.data0, EEP_FRAME_COUNTER_CURRENT_FLOW_ADDR, 2, &s_nb_eep_complete);
		Frame_Para_Init();
		task_complete_ies = PTL_PACK_SUCCESS;       
	}
}

//设置零流量校准开关
static void frame_func_s3_NB_SetDiftofCalibration(void)
{	
	if(TASK_IDLE == EepIfIdle())
	{
		UNION_2U8TOU16 frame_temp;
		g_frame_temp_len = 0;
		frame_temp.datas_map.data0 = *(new_message_data_rev_ies + 0);
		frame_temp.datas_map.data1= *(new_message_data_rev_ies + 1);
		if((frame_temp.datas !=0x2B1A)&&(frame_temp.datas !=0x4D3C))//防止下发的数据因为数据异常而发生误判断
		{
			task_complete_ies = PTL_PACK_FAIL;
			return;
		}
		if(0x2B1A == frame_temp.datas)//启动校准
		{
			Gp30DiftofCalibration();
			g_diftof_cal_data.s.diftof_calibration_flag.data = DIFTOF_CALIBRATION_FLAG;
			EepWrite(&g_diftof_cal_data.data[0], DIFTOF_CALIBRATION_FLAG_ADDR, sizeof (GP30_DIFTOF_CAL_DATA), &s_nb_eep_complete);
		}
		else
		{
			g_diftof_cal_data.s.diftof_calibration_flag.data =0;//关闭校准
			EepWrite(&g_diftof_cal_data.data[0], DIFTOF_CALIBRATION_FLAG_ADDR, 8, &s_nb_eep_complete);
		}	
		task_complete_ies = PTL_PACK_SUCCESS;                
	}
}


//新增规约:读取应急用水量 
static void frame_func_reply_NB_read_emergency(void)
{   
	g_frame_temp_len = 0;
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.ies_pulse_emergency.data_buf[0];
	g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.ies_pulse_emergency.data_buf[1];
	task_complete_ies = PTL_PACK_SUCCESS;
}

//新增规约:设置应急用水量  
static void frame_func_reply_NB_set_emergency(void)
{   
#if (MODULE_BLE_USED)//蓝牙表用配置字设置
#else
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
        s_nb_eep_complete = 0;
        Sys_data_object.P1.ies_pulse_emergency.data_buf[0] = *(new_message_data_rev_ies);
        Sys_data_object.P1.ies_pulse_emergency.data_buf[1] = *(new_message_data_rev_ies + 1);
		EepWrite(&Sys_data_object.P1.ies_pulse_emergency.data_buf[0],ee_addr_compute((u8*)(&Sys_data_object.P1.ies_pulse_emergency.data_buf[0])),2,&s_nb_eep_complete);
		Frame_Para_Init();	
		task_complete_ies = PTL_PACK_SUCCESS;       
	}
#endif
}
//设置/读取阀控参数
void ies_set_valve_para2_frame()
{
    switch(up_deal_stat_ies)
    {
        case UP_DEAL_S0:
        {
            if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
            {
				g_frame_temp_len = 0;
                if(0xAA == *new_message_data_rev_ies)
                {
                    if((*(new_message_data_rev_ies + 1) <= 36) && (*(new_message_data_rev_ies + 1) != 0))
                    {
                        Sys_data_object.P1.ies_valve_up_times.valve_up_times_pre = *(new_message_data_rev_ies + 1);
                        Sys_data_object.P1.ies_valve_up_times.valve_up_times_total = *(new_message_data_rev_ies + 2);
                        Sys_data_object.P1.ies_valve_up_times.valve_up_times_realtime = 0;
                    }
                    Sys_data_object.P1.ies_valve_up_times.valve_auto_check = *(new_message_data_rev_ies + 3);
					EepWrite((u8*)(&Sys_data_object.P1.ies_valve_up_times.valve_up_times_pre),ee_addr_compute((u8*)(&Sys_data_object.P1.ies_valve_up_times.valve_up_times_pre)),4,&s_nb_eep_complete);
                }
                up_deal_stat_ies  = UP_DEAL_S1;
            }
        }
        break;

        case UP_DEAL_S1:
        {
            if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM完成
            {
                g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.ies_valve_up_times.valve_up_times_pre;
                g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.ies_valve_up_times.valve_up_times_total;
                g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.ies_valve_up_times.valve_auto_check;
                g_frame_temp_send_buf[g_frame_temp_len++] = Sys_data_object.P1.ies_valve_up_times.valve_up_times_realtime;
                g_frame_temp_send_buf[g_frame_temp_len++] = 0;
                g_frame_temp_send_buf[g_frame_temp_len++] = 0;
                g_frame_temp_send_buf[g_frame_temp_len++] = 0;
                g_frame_temp_send_buf[g_frame_temp_len++] = 0;
                up_deal_stat_ies  = UP_DEAL_S0;
                task_complete_ies = TRUE;
            }
        }
        break;

        default:
        {
            up_deal_stat_ies  = UP_DEAL_S0;
            task_complete_ies = TRUE;
        }
        break;
    }
}
void frame_factory_test_upload(void)//新增规约：设置厂区发起上传测试
{
	FrameSetMsgApp(Msg_FactoryUpload);
	task_complete_ies = PTL_PACK_SUCCESS;
}
//读历史数据---日冻结
static void frame_func_reply_NB_ReadHistoryData_Daily(void)
{
    u8 temp = 0;	
    u8 temp_read_index = 0;
    u8 temp_read_code = *new_message_data_rev_ies;   

    switch(up_deal_stat_ies)
    {
        case UP_DEAL_S0:
        {
			if(TASK_IDLE == EepIfIdle())//读取历史数据
			{
				g_frame_temp_len =0;
				temp_read_index = get_QZ_StorePara(QZ_StoreCurIndex_Daily);
				if (temp_read_index >= temp_read_code)
				{
					temp = temp_read_index - temp_read_code;
				}
				else
				{
					temp = 100 - (temp_read_code - temp_read_index);
				}
				EepRead((EEP_ADDR_QZ_STORE_DAY +(temp*128)), g_frame_temp_send_buf, 128, &s_nb_eep_complete);		
				up_deal_stat_ies  = UP_DEAL_S1;
			}  					           
        }
        break;
        case UP_DEAL_S1:
        {
            if(TASK_IDLE == EepIfIdle())//读取历史数据完成
            {
				g_frame_temp_len = 128; 
                task_complete_ies = PTL_PACK_SUCCESS;                
                up_deal_stat_ies  = UP_DEAL_S0;
            }
        }
        break;
        default:
        {
			task_complete_ies = PTL_PACK_FAIL;
            up_deal_stat_ies  = UP_DEAL_S0;
        }
        break;
    }   	
}
//读历史数据---月冻结
static void frame_func_reply_NB_ReadHistoryData_Month(void)
{
    u8 temp = 0;	
    u8 temp_read_index = 0;
    u8 temp_read_code = *new_message_data_rev_ies;   

    switch(up_deal_stat_ies)
    {
        case UP_DEAL_S0:
        {
			if(TASK_IDLE == EepIfIdle())//读取历史数据
			{
				g_frame_temp_len =0;
				temp_read_index = get_QZ_StorePara(QZ_StoreCurIndex_Month);
				if (temp_read_index >= temp_read_code)
				{
					temp = temp_read_index - temp_read_code;
				}
				else
				{
					temp = 24 - (temp_read_code - temp_read_index);
				}
				EepRead((EEP_ADDR_QZ_STORE_MONTH +(temp*6)), g_frame_temp_send_buf, 6, &s_nb_eep_complete);		
				up_deal_stat_ies  = UP_DEAL_S1;	
			}    				
        }
        break;

        case UP_DEAL_S1:
        {
            if(TASK_IDLE == EepIfIdle())//读取历史数据完成
            {
				g_frame_temp_len = 6; 
                task_complete_ies = PTL_PACK_SUCCESS;                
                up_deal_stat_ies  = UP_DEAL_S0;
            }
        }
        break;
      
        default:
        {
            task_complete_ies = PTL_PACK_FAIL;
			up_deal_stat_ies  = UP_DEAL_S0;
        }
        break;
    }
}
//读取任意EEPROM数据
static void ies_read_eeprom_data_frame(void)
{
    mtype16_ read_eeprom_address;
    static u8 read_eeprom_length;

    switch(up_deal_stat_ies)
    {
        case UP_DEAL_S0:
        {
            if(TASK_IDLE == EepIfIdle())
            {
				g_frame_temp_len =0;
                read_eeprom_address.to_uint8[BYTE_L] = *(new_message_data_rev_ies + 0);
				read_eeprom_address.to_uint8[BYTE_H] = *(new_message_data_rev_ies + 1);
                read_eeprom_length  = *(new_message_data_rev_ies + 2);

                EepRead(read_eeprom_address.to_uint16, g_frame_temp_send_buf, read_eeprom_length, &s_nb_eep_complete);
                up_deal_stat_ies  = UP_DEAL_S1;
            }
        }
        break;

        case UP_DEAL_S1:
        {
            if(TASK_IDLE == EepIfIdle())//读EEPROM完成
            {
                g_frame_temp_len = read_eeprom_length; 
                task_complete_ies = PTL_PACK_SUCCESS;                
                up_deal_stat_ies  = UP_DEAL_S0;
            }
        }
        break;
		
        default:
        {
            up_deal_stat_ies  = UP_DEAL_S0;
            task_complete_ies = TRUE;
        }
        break;
    }
}

//写任意EEPROM数据
static void ies_write_eeprom_data_frame(void)
{
    switch(up_deal_stat_ies)
    {
        case UP_DEAL_S0:
        {
            if( ('i' == *(new_message_data_rev_ies + 0)) && ('e' == *(new_message_data_rev_ies + 1))
                    && ('s' == *(new_message_data_rev_ies + 2)) )
            {
				g_frame_temp_len =0;
                write_eeprom_address.to_uint8[BYTE_L] = *(new_message_data_rev_ies + 3);
				write_eeprom_address.to_uint8[BYTE_H] = *(new_message_data_rev_ies + 4);
                write_eeprom_length  = *(new_message_data_rev_ies + 5);

                if((write_eeprom_address.to_uint16 > EE_ADDR_MAX) || (write_eeprom_length > 8))//最多一次写8个字节
                {
                    up_deal_stat_ies  = UP_DEAL_S3;
                }
                else
                {
                    for (u8 i = 0; i < write_eeprom_length; i++)
                    {
                        g_frame_temp_send_buf[i] = *(new_message_data_rev_ies + 6 + i);
                    }
                    up_deal_stat_ies  = UP_DEAL_S1;
                }
            }
            else
            {
                up_deal_stat_ies  = UP_DEAL_S3;
            }
        }
        break;

        case UP_DEAL_S1:
        {
            if(TASK_IDLE == EepIfIdle())//写EEPROM
            {
                EepWrite(g_frame_temp_send_buf, write_eeprom_address.to_uint16, write_eeprom_length, &s_nb_eep_complete);
                up_deal_stat_ies = UP_DEAL_S2;
            }
        }
        break;

        case UP_DEAL_S2:
        {
            if(TASK_IDLE == EeReadImportantDataIfIdle())	//写EEPROM完成
            {
                set_important_data_rd_state();		//写完后读取重要数据
                up_deal_stat_ies  = UP_DEAL_S3;
            }
        }
        break;

        case UP_DEAL_S3:
        {
			task_complete_ies = PTL_PACK_SUCCESS;                
			up_deal_stat_ies  = UP_DEAL_S0;
        }
        break;

        default:
        {
            up_deal_stat_ies  = UP_DEAL_S0;
            task_complete_ies = TRUE;
        }
        break;
    }
}

//读RAM数据
static void ies_read_ram_frame (void)
{
    mtype32_ read_RAM_address;
    u8 read_RAM_length;
    u8 *ram_data;
	
	read_RAM_address.to_uint8[WORD_L] = *(new_message_data_rev_ies + 0);
	read_RAM_address.to_uint8[WORD_M] = *(new_message_data_rev_ies + 1);
	read_RAM_address.to_uint8[WORD_H] = *(new_message_data_rev_ies + 2);
	read_RAM_address.to_uint8[WORD_P] = *(new_message_data_rev_ies + 3);

    read_RAM_length  = *(new_message_data_rev_ies + 4);
    ram_data = (u8*)read_RAM_address.to_uint32;

    for(u8 i = 0; i < read_RAM_length; i++)
    {
        g_frame_temp_send_buf[i] = *(ram_data + i);
    }
	g_frame_temp_len = read_RAM_length; 
	task_complete_ies = PTL_PACK_SUCCESS;                
	up_deal_stat_ies  = UP_DEAL_S0;
}
/************************************************************************************************************/
//积成规约表地址判断
u8 check_met_addr_IES(u8 *p_buf,u8 *des_buf)
{
    u8 tmp_i;
    for (tmp_i = 0; tmp_i < 7; tmp_i++)
    {
        if (*(p_buf + 2 +tmp_i) != *(des_buf + tmp_i))
        {
            break;
        }
    }
    if(tmp_i >= 6)	
	{
		return SUCCESS_IES;
	}
	if( (FM_READ_ADDR_ARGUMENT_CTRL == *(p_buf+9))
		||(FM_ENC_SET_ADDR_ARGUMENT_CTRL == *(p_buf+9)))//读写表地址
	{
		for(tmp_i=0;tmp_i < 7;tmp_i++)
		{
			if(0xAA != *(p_buf + 2 + tmp_i))//广播读地址
			break;
		}	
	}
	if((FM_BOOTLOADER_ARGUMENT_CTRL==*(p_buf+9))||(FM_BOOTLOADER_NEW_ARGUMENT_CTRL==*(p_buf+9)))
	{
		for(tmp_i=0;tmp_i < 7;tmp_i++)
		{
			if((0x00 != *(p_buf + 2 + tmp_i))&&(*(p_buf + 2 +tmp_i) != *(des_buf + tmp_i)))//升级指令下发0地址
			{
				break;
			}
		}	
	}
	if(tmp_i >= 7)	
	{
		return SUCCESS_IES;
	}
	return ERROR_IES;
}


//解积成规约
u8 crack_frame_IES_Base(u16 frame_lenth,u8 *frame_buffer, STRUCT_FRAME_IES* pFrameIES)
{
    u16 tmp_i = 0;
    u16 tmp_j = 0;
    u8 tmp_check = 0;

    for (tmp_i = 0; tmp_i < frame_lenth; tmp_i++)
    {
        if (FM_HEAD_MARK == *(frame_buffer + tmp_i))
        {
            if (MET_TYPE == *(frame_buffer + tmp_i + 1))
            {
                    pFrameIES->head_site = tmp_i; //记录帧头
                    pFrameIES->sta  = *(frame_buffer + tmp_i + 10);//增加一个字节的状态字，bit0为1时表示加密，即01H表示加密，bit1为1时表示主动上传，其他bit位预留
                    pFrameIES->len = *(frame_buffer + tmp_i + 11);//数据长度
                    pFrameIES->end_site = pFrameIES->head_site + pFrameIES->len + 13;
                    pFrameIES->check_len = pFrameIES->end_site - pFrameIES->head_site - 1;
                
                    //检测数据帧是否有效
                    if ((pFrameIES->end_site < frame_lenth) && (FM_END_MARK == *(frame_buffer + pFrameIES->end_site)))
                    {
                        //s_frame_flag_IES.enc_reply_flg = 0;
                        pFrameIES->ctrl = *(frame_buffer + pFrameIES->head_site + 9);
						for (tmp_j = 0; tmp_j < 7; tmp_j++)
						{
							check_addr[tmp_j] = Sys_data_object.P1.met_addr[tmp_j];
						}
                        //计算校验和
                        for (tmp_j = pFrameIES->head_site; tmp_j < (pFrameIES->end_site - 1); tmp_j++)
                        {
                            tmp_check += *(frame_buffer + tmp_j);
                        }
                        if (tmp_check == *(frame_buffer + pFrameIES->end_site - 1))
                        {			
							if((SS_SET_COMMON_KEY_CTRL == frame_struct_IES.ctrl) || (SS_RD_COMMON_KEY_CTRL == frame_struct_IES.ctrl))//需要超级密钥解码
							{
								for (int tmp_j = 0; tmp_j < 16; tmp_j++)
								{
									key[tmp_j] = super_key[tmp_j];
								}
							}
							else
							{
								for (int tmp_j = 0; tmp_j < 16; tmp_j++)
								{
									key[tmp_j] = Sys_data_object.P1.key[tmp_j];
								}
							} 	
							if (SUCCESS_IES == check_met_addr_IES(frame_buffer + pFrameIES->head_site, check_addr)) //检查指令包含的地址和本表地址是否相同，相同返回1，不同返回0
							{
								if((pFrameIES->sta  & 0x01) == 0x01)//判断是否为加密规约
								{
									//还原成明文规约:解密,修改加密标志
									AES_decrypt(frame_buffer+pFrameIES->head_site +12, *(frame_buffer+pFrameIES->head_site +11),key); 
									//非加密校验和
									tmp_check = 0;
									for (tmp_j = pFrameIES->head_site; tmp_j < (pFrameIES->end_site - 1); tmp_j++)
									{
										tmp_check += *(frame_buffer + tmp_j);
									}
									*(frame_buffer + pFrameIES->end_site - 1) = tmp_check;
								}
								pFrameIES->sta &= 0xFD; //表示非主动上传（bit1为1时表示主动上传，0表示应答）
								pFrameIES->DI0 = *(frame_buffer + pFrameIES->head_site + 12);
								pFrameIES->DI1 = *(frame_buffer + pFrameIES->head_site + 13);
								pFrameIES->serial_num0 = *(frame_buffer + pFrameIES->head_site + 14);	
								pFrameIES->serial_num1 = *(frame_buffer + pFrameIES->head_site + 15); 		
								return  SUCCESS_IES;
							}							
                        }
                    }
            }
        }
    }
    return ERROR_IES;
}

u8 crack_frame_IES(u16 frame_lenth,u8 *in_frame_buffer)
{
	memcpy(frame_buffer, in_frame_buffer, frame_lenth);
	u8 temp_res = crack_frame_IES_Base(frame_lenth, frame_buffer, &frame_struct_IES);
	if(temp_res == SUCCESS_IES) 
	{
		if(
		(frame_struct_IES.ctrl == SS_TERNAL_RESTART_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_LCD_DISPLAY_CTRL)||
		(frame_struct_IES.ctrl == READ_IP_PORT_CTRL)||
		(frame_struct_IES.ctrl == SMS_UPLOAD_CTRL)||
		(frame_struct_IES.ctrl == VALVE_CONTROL_CTRL)||
		(frame_struct_IES.ctrl == READ_SIGNAL_INTENSITY_CTRL)||
		(frame_struct_IES.ctrl == SS_READ_THRESHOLD_CTRL)||
		(frame_struct_IES.ctrl == SS_READ_START_END_CTRL)||
		(frame_struct_IES.ctrl == SS_READ_STEPLENGTH_CTRL)||
		(frame_struct_IES.ctrl == SS_READ_TIME_FREQUENCY_CTRL)||
		(frame_struct_IES.ctrl == SS_READ_RETRY_COUNT_CTRL)||
		(frame_struct_IES.ctrl == SS_READ_REPORTING_FREQUENCY_CTRL)||
		(frame_struct_IES.ctrl == SS_READ_REPORTSTARTTIME_CTRL)||
		(frame_struct_IES.ctrl == SS_RD_APN_CTRL)||
		(frame_struct_IES.ctrl == SS_RD_COMMON_KEY_CTRL)||
		(frame_struct_IES.ctrl == SS_UNIFIED_READ_CTRL)||
		(frame_struct_IES.ctrl == SS_READ_IMEI_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_MAC_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_ENC_WRITE_INITVALUE_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_SWVERSION_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_OUT_VERIFICATIONSTATE_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_ENC_READ_DATA_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_SERIALNUM_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_CALIBRATE_TIME_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_ADDR_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_CLEAR_METERDATA_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_FLOWCOEFFICIENT_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_SET_VERIFICATIONSTATE_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_TEMPERATURECOEFFICIENT_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_VERIFICATIONDATA_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_INSTANTANEOUSFLOW_AND_TEMPERATURE_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_START_OR_STOP_TEST_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_RTC_CALIBTATION_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_ENTER_LOWPOWER_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_RD_FUNCTIONCFG_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_START_FLOW_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_COUNTER_CURRENT_CTRL)||
		(frame_struct_IES.ctrl == FM_TEST_SLEF_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_READ_RETRY_TIME_CTRL)||
		(frame_struct_IES.ctrl == FM_BOOTLOADER_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_BOOTLOADER_NEW_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_ENC_SET_ADDR_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_SET_TEMPERATURECOEFFICIENT_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_SET_FLOWCOEFFICIENT_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_SWITCH_VERIFICATION_OR_INFRARED_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_CFG_FUNCTIONCFG_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == FM_START_OR_CLEAR_USING_ARGUMENT_CTRL)||
		(frame_struct_IES.ctrl == WRITE_IP_PORT_CTRL)||
		(frame_struct_IES.ctrl == SS_UNIFIED_SETTINGS_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_THRESHOLD_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_START_END_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_STEPLENGTH_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_RETRY_COUNT_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_REPORTING_FREQUENCY_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_REPORTSTARTTIME_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_APN_CTRL)||
		(frame_struct_IES.ctrl == SS_SET_COMMON_KEY_CTRL)||
		(frame_struct_IES.ctrl == FM_SET_START_FLOW_CTRL)||
		(frame_struct_IES.ctrl == FM_SET_COUNTER_CURRENT_CTRL)||
		(frame_struct_IES.ctrl == FM_SET_DIFTOF_CALIBRATION_CTRL)||
		(frame_struct_IES.ctrl == FM_SET_RETRY_TIME_CTRL)||
		(frame_struct_IES.ctrl == IES_NEW_ADD_FRAME_CTRL)||
		((frame_struct_IES.ctrl == SS_READ_HIS_CTRL)&&(frame_struct_IES.DI1==QZ_READ_HIS_DAILY_CTRL_ID1))||
		((frame_struct_IES.ctrl == SS_READ_HIS_CTRL)&&(frame_struct_IES.DI1==QZ_READ_HIS_MONTH_CTRL_ID1))||
		(frame_struct_IES.ctrl == RD_RAM_CTRL)
		)
		{		
			new_message_data_rev_ies = frame_buffer + frame_struct_IES.head_site + 16;								
			FrameSetMsgApp(MsgNb_FlagRcvNbFrame);  //收到的不是应答帧时置位
			return  SUCCESS_IES;
		}
		else if(frame_struct_IES.ctrl == SHORT_ACK_UPLOAD_CTRL)
		{
			if(UPLOAD_TYPE_FRAME_ORDER == GetEventUploadType())//生产上报
			{
				return  SUCCESS_IES;
			}
		}
	}
	return  ERROR_IES;
}

		
//积成规约处理
u8 up_deal_code_ies(void)
{
    task_complete_ies = PTL_PACK_BUSY;
    switch(frame_struct_IES.ctrl)
    {
#if (MODULE_BLE_USED)
#else
		case READ_IP_PORT_CTRL:
		{
			frame_func_reply_NB_ReadIpPort();
			frame_struct_IES.ack_ctrl = ACK_READ_IP_PORT_CTRL;
		}
		break;
		case SMS_UPLOAD_CTRL:
		{
			frame_func_reply_NB_Upload();
			frame_struct_IES.ack_ctrl = ACK_SMS_UPLOAD_CTRL;
		}
		break;
		case READ_SIGNAL_INTENSITY_CTRL:
		{
			frame_func_reply_NB_ReadSignal();
			frame_struct_IES.ack_ctrl = ACK_READ_SIGNAL_INTENSITY_CTRL;
		}
		break;
		case SS_READ_THRESHOLD_CTRL:
		{
			frame_func_reply_NB_ReadThreshole();
			frame_struct_IES.ack_ctrl = SS_ACK_READ_THRESHOLD_CTRL;
		}
		break;
		case SS_READ_START_END_CTRL:
		{
			frame_func_reply_NB_ReadStartEnd();
			frame_struct_IES.ack_ctrl = SS_ACK_READ_START_END_CTRL;
		}
		break;
		case SS_READ_STEPLENGTH_CTRL:
		{
			frame_func_reply_NB_ReadStepLength();
			frame_struct_IES.ack_ctrl = SS_ACK_READ_STEPLENGTH_CTRL;
		}
		break;
//		case SS_READ_TIME_FREQUENCY_CTRL:
//		{
//			frame_func_reply_NB_TimeFrequency();
//			frame_struct_IES.ack_ctrl = SS_ACK_READ_TIME_FREQUENCY_CTRL;
//		}
//		break;
		case SS_READ_RETRY_COUNT_CTRL:
		{
			frame_func_reply_NB_ReadRetryCount();
			frame_struct_IES.ack_ctrl = SS_ACK_READ_RETRY_COUNT_CTRL;
		}
		break;
		case SS_READ_REPORTING_FREQUENCY_CTRL:
		{
			frame_func_reply_NB_ReadReportingFrequency();
			frame_struct_IES.ack_ctrl = SS_ACK_READ_REPORTING_FREQUENCY_CTRL;
		}
		break;
		case SS_READ_REPORTSTARTTIME_CTRL:
		{
			frame_func_reply_NB_ReadReportStartTime();
			frame_struct_IES.ack_ctrl = SS_ACK_READ_REPORTSTARTTIME_CTRL;
		}
		break;
		case SS_RD_APN_CTRL:
		{
			frame_func_reply_NB_ReadApn();
			frame_struct_IES.ack_ctrl = SS_ACK_RDT_APN_CTRL;
		}
		break;
//		case SS_RD_COMMON_KEY_CTRL:
//		{
//			frame_func_reply_NB_ReadCommonKey();
//			frame_struct_IES.ack_ctrl = ACK_SS_RD_COMMON_KEY_CTRL;
//		}
//		break;
		case SS_UNIFIED_READ_CTRL:
		{
			frame_func_reply_NB_UnifiedRead();
			frame_struct_IES.ack_ctrl = SS_ACK_UNIFIED_READ_CTRL;
		}
		break;
		case SS_READ_IMEI_CTRL:
		{
			frame_func_reply_NB_ReadImei();
			frame_struct_IES.ack_ctrl = SS_ACK_READ_IMEI_CTRL;
		}
		break;
		case FM_READ_RETRY_TIME_CTRL:
		{
			frame_func_reply_NB_ReadRetryTime();
			frame_struct_IES.ack_ctrl = FM_READ_RETRY_TIME_REPLY_CTRL;
		}
		break;
		case WRITE_IP_PORT_CTRL:
		{
			frame_func_s3_NB_WriteIpPort();;
			frame_struct_IES.ack_ctrl = ACK_WRTIE_IP_PORT_CTRL;
		}
		break;
		case SS_UNIFIED_SETTINGS_CTRL:
		{
			frame_func_s3_NB_Unified_Setting();;
			frame_struct_IES.ack_ctrl = SS_ACK_UNIFIED_SETTINGS_CTRL;
		}
		break;
		case SS_SET_THRESHOLD_CTRL:
		{
			frame_func_s3_NB_SetThreshold();;
			frame_struct_IES.ack_ctrl = SS_ACK_SET_THRESHOLD_CTRL;
		}
		break;
		case SS_SET_START_END_CTRL:
		{
			frame_func_s3_NB_SetStartEnd();;
			frame_struct_IES.ack_ctrl = SS_ACK_SET_START_END_CTRL;
		}
		break;
		case SS_SET_STEPLENGTH_CTRL:
		{
			frame_func_s3_NB_SetSteplength();;
			frame_struct_IES.ack_ctrl = SS_ACK_SET_STEPLENGTH_CTRL;
		}
		break;
		case SS_SET_RETRY_COUNT_CTRL:
		{
			frame_func_s3_NB_SetRetryCount();;
			frame_struct_IES.ack_ctrl = SS_ACK_SET_RETRY_COUNT_CTRL;
		}
		break;
		case SS_SET_REPORTING_FREQUENCY_CTRL:
		{
			frame_func_s3_NB_SetReportingFrequency();;
			frame_struct_IES.ack_ctrl = SS_ACK_SET_REPORTING_FREQUENCY_CTRL;
		}
		break;
		case SS_SET_REPORTSTARTTIME_CTRL:
		{
			frame_func_s3_NB_SetReportStartTime();;
			frame_struct_IES.ack_ctrl = SS_ACK_SET_REPORTSTARTTIME_CTRL;
		}
		break;
		case SS_SET_APN_CTRL:
		{
			frame_func_s3_NB_SetApn();;
			frame_struct_IES.ack_ctrl = SS_ACK_SET_APN_CTRL;
		}
		break;
		case FM_SET_RETRY_TIME_CTRL:
		{
			frame_func_s3_NB_SetRetryTime();;
			frame_struct_IES.ack_ctrl = FM_SET_RETRY_TIME_REPLY_CTRL;
		}
		break;
		case SHORT_ACK_UPLOAD_CTRL:
		{
			frame_func_reply_NB_AckUploadShort();
		}
		break;
#endif
		case SS_TERNAL_RESTART_CTRL:
		{
			frame_func_reply_NB_TernalRestart();
			frame_struct_IES.ack_ctrl = SS_ACK_TERNAL_RESTART_CTRL;
		}
		break;
		case SS_SET_LCD_DISPLAY_CTRL:
		{
			frame_func_reply_NB_SetLcdDisplay();
			frame_struct_IES.ack_ctrl = SS_ACK_SET_LCD_DISPLAY_CTRL;
		}
		break;
		case VALVE_CONTROL_CTRL:
		{
			frame_func_reply_NB_ValveControl();
			frame_struct_IES.ack_ctrl = ACK_VALVE_CONTROL_CTRL;
		}
		break;
		case SS_READ_TIME_FREQUENCY_CTRL:
		{
			frame_func_reply_NB_TimeFrequency();
			frame_struct_IES.ack_ctrl = SS_ACK_READ_TIME_FREQUENCY_CTRL;
		}
		break;
		case SS_RD_COMMON_KEY_CTRL:
		{
			frame_func_reply_NB_ReadCommonKey();
			frame_struct_IES.ack_ctrl = ACK_SS_RD_COMMON_KEY_CTRL;
		}
		break;
		case FM_READ_MAC_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ReadMac();
			frame_struct_IES.ack_ctrl = FM_READ_MAC_REPLY_CTRL;
		}
		break;
		case FM_ENC_WRITE_INITVALUE_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_WriteInitValue();
			frame_struct_IES.ack_ctrl = FM_ENC_WRITE_INITVALUE_REPLY_CTRL;
		}
		break;
		case FM_READ_SWVERSION_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ReadSwVersion();
			frame_struct_IES.ack_ctrl = FM_READ_SWVERSION_REPLY_CTRL;
		}
		break;
		case FM_OUT_VERIFICATIONSTATE_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_OutVerificationState();
			frame_struct_IES.ack_ctrl = FM_OUT_VERIFICATIONSTATE_REPLY_CTRL;
		}
		break;
		case FM_ENC_READ_DATA_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ReadData();
			frame_struct_IES.ack_ctrl = FM_ENC_READ_DATA_REPLY_CTRL;
		}
		break;
		case FM_READ_SERIALNUM_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ReadSerialnum();
			frame_struct_IES.ack_ctrl = FM_READ_SERIALNUM_REPLY_CTRL;
		}
		break;
		case FM_CALIBRATE_TIME_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_CalibrateTime();
			frame_struct_IES.ack_ctrl = FM_CALIBRATE_TIME_REPLY_CTRL;
		}
		break;
		case FM_READ_ADDR_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ReadAddr();
			frame_struct_IES.ack_ctrl = FM_READ_ADDR_REPLY_CTRL;
		}
		break;
		case FM_CLEAR_METERDATA_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ClearMeterData();
			frame_struct_IES.ack_ctrl = FM_CLEAR_METERDATA_REPLY_CTRL;
		}
		break;
		case FM_READ_FLOWCOEFFICIENT_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ReadFlowCoefficient();
			frame_struct_IES.ack_ctrl = FM_READ_FLOWCOEFFICIENT_REPLY_CTRL;
		}
		break;
		case FM_SET_VERIFICATIONSTATE_ARGUMENT_CTRL:
		{
			if(FM_SET_VERIFICATIONSTATE_TOOL_DI1 == frame_struct_IES.DI1)
			{
				frame_func_reply_TOOL_SetVerification();
			}
			else
			{
				frame_func_reply_NB_SetVerification();
			}
			frame_struct_IES.ack_ctrl = FM_SET_VERIFICATIONSTATE_REPLY_CTRL;
		}
		break;
		case FM_READ_TEMPERATURECOEFFICIENT_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ReadTemperatureCoefficient();
			frame_struct_IES.ack_ctrl = FM_READ_TEMPERATURECOEFFICIENT_REPLY_CTRL;
		}
		break;
		case FM_READ_VERIFICATIONDATA_ARGUMENT_CTRL:
		{
			if(FM_READ_VERIFICATIONDATA_TOOL_DI1 == frame_struct_IES.DI1)
			{ 
				frame_func_reply_TOOL_ReadVerificationData();
			}
			else
			{
				frame_func_reply_NB_ReadVerificationData();
			}
			frame_struct_IES.ack_ctrl = FM_READ_VERIFICATIONDATA_REPLY_CTRL;
		}
		break;
		case FM_READ_INSTANTANEOUSFLOW_AND_TEMPERATURE_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_ReadInstantFlowAndTemperature();
			frame_struct_IES.ack_ctrl = FM_READ_INSTANTANEOUSFLOW_AND_TEMPERATURE_REPLY_CTRL;
		}
		break;
		case FM_START_OR_STOP_TEST_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_StartOrStopTest();
			frame_struct_IES.ack_ctrl = FM_START_OR_STOP_TEST_REPLY_CTRL;
		}
		break;
		case FM_RTC_CALIBTATION_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_RtcCalibtation();
			frame_struct_IES.ack_ctrl = FM_RTC_CALIBTATION_REPLY_CTRL;
		}
		break;
		case FM_ENTER_LOWPOWER_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_EnterLowPower();
			frame_struct_IES.ack_ctrl = FM_ENTER_LOWPOWER_REPLY_CTRL;
		}
		break;
		case FM_RD_FUNCTIONCFG_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_RdFunctionCfg();
			frame_struct_IES.ack_ctrl = FM_RD_FUNCTIONCFG_REPLY_CTRL;
		}
		break;
		case FM_READ_START_FLOW_CTRL:
		{
			frame_func_reply_NB_ReadStartFlow();
			frame_struct_IES.ack_ctrl = FM_READ_START_FLOW_REPLY_CTRL;
		}
		break;
		case FM_READ_COUNTER_CURRENT_CTRL:
		{
			frame_func_reply_NB_ReadCounterCurrent();
			frame_struct_IES.ack_ctrl = FM_READ_COUNTER_CURRENT_REPLY_CTRL;
		}
		break;
		case FM_TEST_SLEF_ARGUMENT_CTRL:
		{
			frame_func_reply_NB_TestSelf();
			frame_struct_IES.ack_ctrl = FM_TEST_SLEF_REPLY_CTRL;
		}
		break;
		
		/*case FM_BOOTLOADER_ARGUMENT_CTRL:
		{
			frame_func_s3_NB_BootLoad();
			frame_struct_IES.ack_ctrl = FM_BOOTLOADER_REPLY_CTRL;
		}
		break;*/
		case FM_BOOTLOADER_NEW_ARGUMENT_CTRL:
		{
			if((frame_struct_IES.DI0 == SET_RD_VALVE_PARA2_CTRL_DI0) && (frame_struct_IES.DI1 == SET_RD_VALVE_PARA2_CTRL_DI1))//检验数据ID
            {
                ies_set_valve_para2_frame();
				frame_struct_IES.ack_ctrl = ACK_SET_RD_VALVE_PARA2_CTRL;
            }
            else if((frame_struct_IES.DI0 == FM_BOOTLOADER_NEW_DI0) && (frame_struct_IES.DI1 == FM_BOOTLOADER_NEW_DI1))
            {
                frame_func_s3_NB_diff_BootLoad();
				frame_struct_IES.ack_ctrl = FM_BOOTLOADER_NEW_REPLY_CTRL;
            }
		}
		break;
		case FM_ENC_SET_ADDR_ARGUMENT_CTRL:
		{
			frame_func_s3_NB_SetAddr();;
			frame_struct_IES.ack_ctrl = FM_ENC_SET_ADDR_REPLY_CTRL;
		}
		break;
		case FM_SET_TEMPERATURECOEFFICIENT_ARGUMENT_CTRL:
		{
			frame_func_s3_NB_SetTempratureConefficient();;
			frame_struct_IES.ack_ctrl = FM_SET_TEMPERATURECOEFFICIENT_REPLY_CTRL;
		}
		break;
		case FM_SET_FLOWCOEFFICIENT_ARGUMENT_CTRL:
		{
			frame_func_s3_NB_SetFowcoefficient();;
			frame_struct_IES.ack_ctrl = FM_SET_FLOWCOEFFICIENT_REPLY_CTRL;
		}
		break;
		case FM_SWITCH_VERIFICATION_OR_INFRARED_ARGUMENT_CTRL:
		{
			frame_func_s3_NB_SwitchVerificationOrInfrared();;
			frame_struct_IES.ack_ctrl = FM_SWITCH_VERIFICATION_OR_INFRARED_REPLY_CTRL;
		}
		break;
		case FM_CFG_FUNCTIONCFG_ARGUMENT_CTRL:
		{
			frame_func_s3_NB_CfgFunctioncfg();;
			frame_struct_IES.ack_ctrl = FM_CFG_FUNCTIONCFG_REPLY_CTRL;
		}
		break;
		case FM_START_OR_CLEAR_USING_ARGUMENT_CTRL:
		{
			frame_func_s3_NB_SetOrClearUsing_SAcrc();;
			frame_struct_IES.ack_ctrl = FM_START_OR_CLEAR_USING_REPLY_CTRL;
		}
		break;
	
		case SS_SET_COMMON_KEY_CTRL:
		{
			frame_func_s3_NB_SetCommonKey();;
			frame_struct_IES.ack_ctrl = ACK_SS_SET_COMMON_KEY_CTRL;
		}
		break;
		case FM_SET_START_FLOW_CTRL:
		{
			frame_func_s3_NB_SetStartFlow();;
			frame_struct_IES.ack_ctrl = FM_SET_START_FLOW_REPLY_CTRL;
		}
		break;
		case FM_SET_COUNTER_CURRENT_CTRL:
		{
			frame_func_s3_NB_SetCounterCurrent();;
			frame_struct_IES.ack_ctrl = FM_SET_COUNTER_CURRENT_REPLY_CTRL;
		}
		break;
		case FM_SET_DIFTOF_CALIBRATION_CTRL:
		{
			frame_func_s3_NB_SetDiftofCalibration();;
			frame_struct_IES.ack_ctrl = FM_SET_DIFTOF_CALIBRATION_REPLY_CTRL;
		}
		break;
		case SS_READ_HIS_CTRL:
		{
			switch(frame_struct_IES.DI1)
			{
				case QZ_READ_HIS_DAILY_CTRL_ID1://日冻结
				{
					frame_func_reply_NB_ReadHistoryData_Daily();
				}
				break;
				case QZ_READ_HIS_MONTH_CTRL_ID1://月冻结
				{
					frame_func_reply_NB_ReadHistoryData_Month();
				}
				break;
				default:
				{
					up_deal_stat_ies  = UP_DEAL_S0;
					task_complete_ies = PTL_PACK_FAIL;  
				}
			}
			frame_struct_IES.ack_ctrl = SS_ACK_READ_HIS_CTRL;
		}
		break;
		case RD_RAM_CTRL:
		{
			ies_read_ram_frame();;
			frame_struct_IES.ack_ctrl = ACK_RD_RAM_CTRL;
		}
		break;
		case IES_NEW_ADD_FRAME_CTRL:
		{
			if(COM_WATER_ID1_0x10 == frame_struct_IES.DI1)	//数据标识1统一为x010
            {
                switch(frame_struct_IES.DI0)
                {
					case READ_EEPROM_DATA_ID0://读EEPROM
					{
						ies_read_eeprom_data_frame();
					}
					break;
					case WRITE_EEPROM_DATA_ID0://写EEPROM
					{
						ies_write_eeprom_data_frame();
					}
					break;
                    case SET_UDP_IP_PORT_ID0://设置udp
                    {
                        frame_func_s3_NB_WriteUdpIpPort();
                    }
                    break;
					case READ_UDP_IP_PORT_ID0:		// 读取UDP模式IP
                    {
						frame_func_reply_NB_ReadUdpIpPort();
                    }
                    break;
					case READ_TIMES_SOE_ID0:
					{
						frame_func_reply_NB_ReadSoe();
					}
					break;
                    case SET_EMERGENCY_NUM_ID0:
					{
						frame_func_reply_NB_set_emergency();
					}
					break;
                    case READ_EMERGENCY_NUM_ID0:
					{
						frame_func_reply_NB_read_emergency();
					}
					break;
					case FACTORY_TEST_UPLOAD_ID0:			//厂区测试上传
					{
						frame_factory_test_upload();
					}
					break;
					case READ_BATERY_PARA:
					{
						frame_func_reply_NB_read_battery();
					}
					break;
					case CLEAR_BATERY_PARA:
					{
						frame_func_reply_NB_read_batteryinit();
					}
					break;
					default:
					{
						up_deal_stat_ies  = UP_DEAL_S0;
						task_complete_ies = PTL_PACK_FAIL; 
					}
					break;
				}
			}
			frame_struct_IES.ack_ctrl = ACK_IES_NEW_ADD_FRAME_CTRL;;
		}
		break;
        default:
        {     
			up_deal_stat_ies  = UP_DEAL_S0;
			task_complete_ies = PTL_PACK_FAIL;           
        }
        break;
    }
    return task_complete_ies;
}
void Frame_ResetDealState_Ies(void)
{
	up_deal_stat_ies = UP_DEAL_S0;
}

/****************************************************************以下主动上报数据的处理*********************************************/
//NB发送数据组帧
void Frame_ack_IesBase(STRUCT_FRAME_IES temp_struct, u8* p_temp_buf, u16 p_temp_len, u8* p_send_buf, u16* p_send_len)
{
    int i = 0;
	UNION_2U8TOU16 temp_pack_len;
	
    p_send_buf[0] = 0xFE;
    p_send_buf[1] = 0xFE;
    p_send_buf[2] = FM_HEAD_MARK;
    p_send_buf[3] = MET_TYPE;
	for (i = 0; i < 7; i++)
	{
		p_send_buf[4 + i] = Sys_data_object.P1.met_addr[i];
	}
    p_send_buf[11] = temp_struct.ack_ctrl;
    p_send_buf[12] = temp_struct.sta;//sta 0x40标识主动上传帧  0x80 位加密帧 0xc0为加密的主动上传帧  
    if(p_temp_len <= (0xFF-4))
    {
        p_send_buf[13] = p_temp_len+4;//l
        p_send_buf[14] = temp_struct.DI0; //DI0
        p_send_buf[15] = temp_struct.DI1; //DI1
        p_send_buf[16] = temp_struct.serial_num0; //SER
        p_send_buf[17] = temp_struct.serial_num1; //SER
        memcpy(&p_send_buf[18],p_temp_buf, p_temp_len);
        if((temp_struct.sta & 0x01) == 0x01)
        {
            AES_encrypt(0,&p_send_buf[14],p_send_buf[13], key);//加密
            p_send_buf[13]=(p_send_buf[13]/16+1)*16;
        }
        p_send_buf[14+p_send_buf[13]]=0;
        for (i = 2; i < 14+p_send_buf[13]; i++)
        {
            p_send_buf[14+p_send_buf[13]] += p_send_buf[i];//校验和
        }
        p_send_buf[15+p_send_buf[13]] = FM_END_MARK; //帧结束符
        *p_send_len = 16+p_send_buf[13];
    }
    else
    {
        temp_pack_len.datas  = p_temp_len+4;
        p_send_buf[13] = temp_pack_len.datas_map.data0;
        p_send_buf[14] = temp_pack_len.datas_map.data1;
        p_send_buf[15] = temp_struct.DI0; //DI0
        p_send_buf[16] = temp_struct.DI1; //DI1
        p_send_buf[17] = temp_struct.serial_num0; //SER
        p_send_buf[18] = temp_struct.serial_num1; //SER
        memcpy(&p_send_buf[19], p_temp_buf, p_temp_len);
        if((temp_struct.sta & 0x01) == 0x01)
        {
            AES_encrypt(1,&p_send_buf[15], temp_pack_len.datas, key);//加密
            temp_pack_len.datas = (temp_pack_len.datas/16+1)*16;
            p_send_buf[13]=temp_pack_len.datas_map.data1;//高位在前，方便插件判断
            p_send_buf[14]=temp_pack_len.datas_map.data0;
        }
        p_send_buf[15+temp_pack_len.datas]=0;
        for (i = 2; i < 15+temp_pack_len.datas; i++)
        {
            p_send_buf[15+temp_pack_len.datas] += p_send_buf[i];//校验和
        }           
        p_send_buf[16+temp_pack_len.datas] = FM_END_MARK; //帧结束符
        *p_send_len = 17+temp_pack_len.datas;
    }    
}
//应答数据组帧
void Frame_ack_IES(u8* p_send_buf, u16* p_send_len)
{
	STRUCT_FRAME_IES temp_struct;
	
	temp_struct.ack_ctrl = frame_struct_IES.ack_ctrl;
	temp_struct.DI0 = frame_struct_IES.DI0;
	temp_struct.DI1 = frame_struct_IES.DI1;
	temp_struct.sta = frame_struct_IES.sta;
	temp_struct.serial_num0 = frame_struct_IES.serial_num0;
	temp_struct.serial_num1 = frame_struct_IES.serial_num1;

	Frame_ack_IesBase(temp_struct,g_frame_temp_send_buf, g_frame_temp_len, p_send_buf, p_send_len);
}
///////////////////////////////////////////////////////////数据上报组帧///////////////////////////////
#if (MODULE_BLE_USED)
#else
//获取信号相关数据
MEASURE_UPLOAD_INFO UploadGetInfo_Base(void)
{
	s16 s_temp_data=0;
	MEASURE_UPLOAD_INFO  temp_UploadInfo;
	
	NbiotGetSNR(&s_temp_data);
	if(s_temp_data<0)
	{
	    temp_UploadInfo.metinfo.snr_val =(-s_temp_data);
		temp_UploadInfo.metinfo.snr_val |= 0x8000;
	}
	else
	{
	    temp_UploadInfo.metinfo.snr_val = s_temp_data;
	}
	//
	NbiotGetECL(&s_temp_data);
	if(s_temp_data<0)
	{
		temp_UploadInfo.metinfo.ecl_val |= (-s_temp_data)&0x8000;
	}
	else
	{
	    temp_UploadInfo.metinfo.ecl_val = s_temp_data;
	}
	//
	NbiotGetRSRP(&s_temp_data);
	temp_UploadInfo.metinfo.RSRP_val = -s_temp_data;
	NbiotGetCSQ(&temp_UploadInfo.metinfo.single_val); 
	temp_UploadInfo.metinfo.single_val = (temp_UploadInfo.metinfo.single_val / 10)*16 + (temp_UploadInfo.metinfo.single_val%10);
	//获取电压值
	temp_UploadInfo.metinfo.bat_val.u16_data =HexToBcd16(PowerCheckValueSys());															
	temp_UploadInfo.metinfo.bat_threshold_val = 0x03;//欠压阈值3v
	return temp_UploadInfo;
}

/////////////////////////////////////////////积成UDP上报////////////////////////////////
void GetUploadDataResetState_IesUDP(void)
{
	upload_frame_stat_ies_udp =UP_DEAL_S1;
	upload_task_complete_ies_udp = PTL_PACK_BUSY;
}
u8 GetUploadDataCheckIdle_IesUDP(void)
{
	if(UP_DEAL_S0 == upload_frame_stat_ies_udp)
    {
        return TASK_IDLE;
    }
    else
    {
        return TASK_BUSY;
    }
}
u8 GetUploadDataCheckTask_IesUDP(void)
{
	return upload_task_complete_ies_udp;
}
void GetUploadDataMachine_IesUDP(u8 data_step, u8* p_send_buf, u16* p_send_len)
{
	if(data_step == UPLOAD_FRAME_REQUEST_NEW)
	{	
		STRUCT_FRAME_IES upload_struct_IES;
		u8 cell_id_data_frame[CELL_ID_SIZE];
		MET_STATE temp_state; //状态字
		Fram_To_Float tmp_float;
		s16 s_temp_data=0;
		
		upload_udp_frame.ProcMsgData.GP30_PeakUp.datas =  Gp30CheckTestDataApp(GP30_APP_PEAK_UP);//换能器接收信号幅值up
		upload_udp_frame.ProcMsgData.GP30_PeakDown.datas = Gp30CheckTestDataApp(GP30_APP_PEAK_DOWN);//换能器接收信号幅值down   平台显示单位为V
		//温度
		float tempTempratureFlag;
		tempTempratureFlag= TempCheckValueApp();  
		if(tempTempratureFlag < 0) //CMR还不能显示负温度  
		{
			tempTempratureFlag = -tempTempratureFlag;
			upload_udp_frame.ProcMsgData.TempX100.datas = (u32)(tempTempratureFlag*100);//HexToBcd32((u32)(tempTempratureFlag*100));
			upload_udp_frame.ProcMsgData.TempX100.datas|= 0x8000;
		}
		else
		{
			upload_udp_frame.ProcMsgData.TempX100.datas = (u32)(tempTempratureFlag*100);//HexToBcd32((u32)(tempTempratureFlag*100));
			upload_udp_frame.ProcMsgData.TempX100.datas &= 0x7FFF;
		} 
		//反算温度
		#if WENDU_FANSUAN_OPEN
			upload_udp_frame.ProcMsgData.GP30_TempX100.datas = upload_udp_frame.ProcMsgData.TempX100.datas;
		#else	
			upload_udp_frame.ProcMsgData.GP30_TempX100.datas = 0;
		#endif
		upload_udp_frame.ProcMsgData.GP30_Diff.datas = Gp30CheckTestDataApp(GP30_APP_DIFF);
		upload_udp_frame.ProcMsgData.GP30_DiffBig[0].datas =PROGRAME_VERSION>>16;
		upload_udp_frame.ProcMsgData.GP30_DiffBig[1].datas =(Sys_data_object.P1.run_information_object.ee_remain_para_energy_life*10);//电量比值;
		upload_udp_frame.ProcMsgData.GP30_DiffBig[2].datas = Sys_data_object.P1.run_information_object.nb_upload_happen_num;//NB上报次数
		upload_udp_frame.ProcMsgData.GP30_DiffBig[3].datas =0;
		upload_udp_frame.ProcMsgData.ResetCount = (Meter_GetResetCount()>>8)&0xFF;
		upload_udp_frame.ProcMsgData.ResetReason = Meter_GetResetCount()&0xFF;
		upload_udp_frame.ProcMsgData.BateryPower =PowerCheckValueSys();
		
		NbiotGetRSRP(&s_temp_data);	
		upload_udp_frame.ProcMsgData.Rsrp = -s_temp_data;
		NbiotGetSNR(&s_temp_data);
		if(s_temp_data<0)
		{
			upload_udp_frame.ProcMsgData.Snr =(-s_temp_data);
			upload_udp_frame.ProcMsgData.Snr |= 0x8000;
		}
		else
		{
			upload_udp_frame.ProcMsgData.Snr = s_temp_data;
		}
		//upload_udp_frame.ProcMsgData.UploadLastFailCode =;
		//cellid
		 NbiotGetCell_ID(cell_id_data_frame);
		upload_udp_frame.ProcMsgData.CellId.datas = 0;
		for(int i=0;i<CELL_ID_SIZE;i++)
		{
			if(cell_id_data_frame[i] != 0)
			{
				upload_udp_frame.ProcMsgData.CellId.datas <<= 4;
				if(cell_id_data_frame[i] <= '9')
				{
					upload_udp_frame.ProcMsgData.CellId.datas |= cell_id_data_frame[i]-'0';
				}
				else if((cell_id_data_frame[i] <= 'f')&&(cell_id_data_frame[i] >= 'a'))
				{
					upload_udp_frame.ProcMsgData.CellId.datas |= cell_id_data_frame[i]-'a'+10;
				}
				else if((cell_id_data_frame[i] <= 'F')&&(cell_id_data_frame[i] >= 'A'))
				{
					upload_udp_frame.ProcMsgData.CellId.datas |= cell_id_data_frame[i]-'A'+10;
				}
			}
		}
		upload_udp_frame.ProcMsgData.Frequency =NbiotGetNBAND();
		//upload_udp_frame.ProcMsgData.Reverse =;
		//状态字
		temp_state = FrameGetByte();
		upload_udp_frame.ProcMsgData.Sta0 = temp_state.stat0;
		upload_udp_frame.ProcMsgData.Sta1 = temp_state.stat1;
		upload_udp_frame.ProcMsgData.Sta2 = temp_state.stat2;
		upload_udp_frame.ProcMsgData.Sta3 = 0x02;

		//流量
		tmp_float.gp22_u32  = Gp30CheckDataApp(2);
		upload_udp_frame.ProcMsgData.CurrentFlow_short.datas = (u32)(tmp_float.gp22_float*10);//当前累计流量;//当前累计流量
		upload_udp_frame.ProcMsgData.null_buf.datas = 0; //大口径使用这4字节
		
		upload_struct_IES.ack_ctrl = SS_UPLOAD_CTRL;
		upload_struct_IES.DI0 = SS_UPLOAD_CTRL_UDP_ID0;
		upload_struct_IES.DI1 = SS_UPLOAD_CTRL_ID1;
		upload_struct_IES.sta = 0x03;//加密主动上传
		//upload_serial_num++;
		upload_struct_IES.serial_num0 = 0x12;//upload_serial_num&0xFF;
		upload_struct_IES.serial_num1 = 0x34;//(upload_serial_num>>8)&0xFF; 
		
		Frame_ack_IesBase(upload_struct_IES, upload_udp_frame.buf, STRU_DATA_LEN_IES_UDP, p_send_buf, p_send_len);
		//////////////
		upload_task_complete_ies_udp = PTL_PACK_SUCCESS;// 成功
	}
	else
	{
		upload_task_complete_ies_udp = PTL_PACK_FAIL;// 失败
	}
	upload_frame_stat_ies_udp = UP_DEAL_S0;
}
/////////////////////////////////////////////产线生产上报////////////////////////////////
void GetUploadDataResetState_Product(void)
{
	upload_frame_stat_product_ies =UP_DEAL_S1;
	upload_task_complete_product_ies = PTL_PACK_BUSY;
}
u8 GetUploadDataCheckIdle_Product(void)
{
	if(UP_DEAL_S0 == upload_frame_stat_product_ies)
    {
        return TASK_IDLE;
    }
    else
    {
        return TASK_BUSY;
    }
}
u8 GetUploadDataCheckTask_Product(void)
{
	return upload_task_complete_product_ies;
}
void GetUploadDataMachine_Product(u8 data_step, u8* p_send_buf, u16* p_send_len)
{
	DateTime tempRtcc_time;
	
	MET_STATE temp_state; //状态字
	Fram_To_Float tmp_float;
	MEASURE_UPLOAD_INFO  temp_UploadInfo;
	STRUCT_FRAME_IES upload_struct_IES;
	if(data_step == UPLOAD_FRAME_REQUEST_NEW)
	{
		upload_frame_retry_times =0;
			//流量
		tmp_float.gp22_u32  = Gp30CheckDataApp(2);
		short_upload_product_frame.data.null_buf.datas = 0; 
		short_upload_product_frame.data.CurrentFlow_short.datas = (u32)(tmp_float.gp22_float*10);//当前累计流量
		//信号\电池相关
		temp_UploadInfo = UploadGetInfo_Base();
		short_upload_product_frame.data.RSRP_val = (u8)(temp_UploadInfo.metinfo.RSRP_val/10);
		short_upload_product_frame.data.bat_voltage = PowerCheckValueSys();
		short_upload_product_frame.data.mode_snr = temp_UploadInfo.metinfo.snr_val;
		short_upload_product_frame.data.mode_ecl = temp_UploadInfo.metinfo.ecl_val;
		//上报时间
		RtccGetDateTime(&tempRtcc_time);
		short_upload_product_frame.data.UploadTime[0] = tempRtcc_time.s.minute;
		short_upload_product_frame.data.UploadTime[1] = tempRtcc_time.s.hour;
		short_upload_product_frame.data.UploadTime[2] = tempRtcc_time.s.day;
		short_upload_product_frame.data.UploadTime[3] = tempRtcc_time.s.month;
		short_upload_product_frame.data.UploadTime[4] = tempRtcc_time.s.year;
		//状态字
		temp_state = FrameGetByte();
		short_upload_product_frame.data.StateBytes[0] = temp_state.stat0;
		short_upload_product_frame.data.StateBytes[1] = temp_state.stat1;
		short_upload_product_frame.data.StateBytes[2] = temp_state.stat2;
		short_upload_product_frame.data.StateBytes[3] = 0x02;
		short_upload_product_frame.data.StateBytes[3] |= getUploadControlPara(UP_BAT_STATE);//剩余电池电量
		//软件版本
		short_upload_product_frame.data.soft_ver.datas_map.data1 =(FrameCheckParaApp(FramePara_ProgVersion)>>16)&0xFF;
		short_upload_product_frame.data.soft_ver.datas_map.data0 =(FrameCheckParaApp(FramePara_ProgVersion)>>24)&0xFF;
		//温度
		short_upload_product_frame.data.temp_value.datas = (u16)TempCheckValueApp();
		//////////////
		upload_struct_IES.ack_ctrl = SHORT_UPLOAD_CTRL;
		upload_struct_IES.DI0 = SHORT_UPLOAD_CTRL_ID0;
		upload_struct_IES.DI1 = SHORT_UPLOAD_CTRL_ID1;
		upload_struct_IES.sta = 0x03;//加密主动上传
		upload_struct_IES.serial_num0 = 0x56;
		upload_struct_IES.serial_num1 = 0x78; 
		Frame_ack_IesBase(upload_struct_IES, short_upload_product_frame.buf, STRU_DATA_LEN_IES_SHORT, p_send_buf, p_send_len);
		//////////////
		upload_task_complete_product_ies = PTL_PACK_SUCCESS;// 成功
	}
	else
	{
		if(data_step == UPLOAD_FRAME_UPLOAD_RETRY)
		{
			if(upload_frame_retry_times < 1) 
			{
				upload_frame_retry_times++;
				upload_task_complete_product_ies = PTL_PACK_SUCCESS;// 成功
			} 
			else//重新发送失败3次的处理
			{
				upload_task_complete_product_ies = PTL_PACK_FAIL;
			}
		}
		else if(data_step == UPLOAD_FRAME_ONLINE_FAIL)//上线失败
		{
			if((Sys_data_object.P1.func_cfg_bytes[0]>0)&&NbiotIfOnline())//厂区上报时间,如果有信号值,则不报故障
			{
				NB_SS_Control_SetPara(NB_UploadFailTimes, 0);
			}
			else
			{
				Sys_data_object.P3.ProductUpoadFailFlag = 50;
			}
			upload_task_complete_product_ies = PTL_PACK_FAIL;// 失败
		}
		else//应答成功
		{
			NB_SS_Control_SetPara(NB_UploadFailTimes, 0);//生产上报成功,清除失败次数
			upload_task_complete_product_ies = PTL_PACK_FAIL;// 失败
		}
	}
	upload_frame_stat_product_ies = UP_DEAL_S0;
}
#endif


