

#include "CanNw.h"




CAN_NW_STATE euNwState;

unsigned char ucNwSendPduType;          //网络管理控制发送选择 0：关闭发送  1：发送APP报文  2：发送网络管理APP报文
unsigned int uiNwTimCnt;
unsigned int uiNwToutCnt;
unsigned int uiNwRMSCnt;
unsigned int uiNwBsSlpCnt;
unsigned int uiSetNwPduCycVal;
unsigned int uiNwPduCycCnt;
unsigned int uiNwLocWkpOffCnt;

unsigned char ucActiveWakeupBit;
unsigned char ucRemoteWakupFlg;
unsigned char ucNwPowerUpFlg;
unsigned char ucNwNetIdelCheck;
unsigned char ucNwNetIdelFlg;
unsigned char ucPreLocWkpStu;

unsigned char ucNetActiveFlg;

unsigned char ImmediateNM_count;
unsigned int  DiagCnt;
unsigned char RepeatMessageReq;

unsigned char DiagMsgRcvFlag;
unsigned char RSSDiagReq;

unsigned char CanNwWakeupSourceCheck(unsigned char LocalSource,unsigned char *RemoteSource);

unsigned char CanNwNetIdelCheck(unsigned char *IdelFlg);

void CanNwInit(void)
{

	euNwState = CAN_NW_RESERVED;
	uiNwTimCnt = 0u;
	uiNwToutCnt = 0u;
	uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;
	uiNwBsSlpCnt = 0u;
	uiSetNwPduCycVal = REPEAT_NW_CYCLE_TIMER_THR;
	ucActiveWakeupBit = PASSIVE_WAKEUP;
	ucRemoteWakupFlg = 0u;
	ucNwPowerUpFlg = 0u;
	ucNwSendPduType = SEND_DISABLE;
	uiNwPduCycCnt = 0;
	ucNwNetIdelCheck = 0;
	ucNwNetIdelFlg = 0;
	ucPreLocWkpStu = PASSIVE_WAKEUP;
	uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
	ucNetActiveFlg = 0;//网络管理使能

	Can_Nw_Init();
}

/*
	函数名：void CanNwHandle(void)
	调度周期时间：10ms
	相关说明：网络管理程序，包含唤醒检测，网络管理状态机切换，网络空闲检测
*/



void CanNwHandle(void)
{
	static unsigned char FlgTemp = 0,FlgTemp1 = 0;
		
	//if((euNwState == CAN_NW_RESERVED)||(euNwState == CAN_NW_GOTO_SLEEP))
	if(euNwState == CAN_NW_RESERVED)
	{
		if((ucRemoteWakupFlg == 1)||(I_IGN_SIGN == IGN_ON))
		{
			FlgTemp1 = 0;
			if(FlgTemp == 0)
			{	
				FlgTemp = 1;
				C0CTRH &= ~0x0300;		  // 设置正常模式
			}
		}
		else
		{
			FlgTemp = 0;
			if(FlgTemp1 == 0)
			{
				C0CTRH |= 0x0300;		  // 设置监听模式
				FlgTemp1 = 1;
			}
		}
	}
    if(DiagMsgRcvFlag == 1)
    {
        DiagCnt = 5000;
        DiagMsgRcvFlag = 0;
        if(euNwState==CAN_NW_READY_SLEEP)RSSDiagReq=1;
    }
	ucActiveWakeupBit = CanNwWakeupSourceCheck(ucIgnStus,&ucRemoteWakupFlg);
	ucNwNetIdelFlg = CanNwNetIdelCheck(&ucNwNetIdelCheck);
    //CAN_2F6.Msg_Bit.NWM_STATE = euNwState;
	switch(euNwState)
	{
		case CAN_NW_RESERVED:
		{
			if((ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)||(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP))
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				euNwState = CAN_NW_RMS_SLEEP;    				 //睡眠模式到重复消息模式
                //设置100ms周期发送NW_PDU
				uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
				uiNwTimCnt = 0;
				ucNwSendPduType = SEND_NWAPP_PDU;
				uiNwPduCycCnt = 0;
                if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)    //本地唤醒进入立即发送网络管理报文模式
                {
                    ImmediateNM_count = 0;
                    uiSetNwPduCycVal = IMMEDIATENM_CYCLE_TIMER_THR;
                }
                else
                {
                    ImmediateNM_count = 5;
                    uiSetNwPduCycVal = REPEAT_NW_CYCLE_TIMER_THR;
                }
			}
			else
			{
				euNwState = CAN_NW_RESERVED;
				if(ucNwNetIdelFlg == 1)
				{
					if(uiNwTimCnt > 5000)                       //5sec goto sleep
					{
						ucNwSendPduType = SEND_DISABLE;
						SleepDetect();
					}
				}

			}
		}
		break;
		case CAN_NW_RMS_SLEEP:
		{
            if(ImmediateNM_count >= 4 )uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
			if((ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)||(ucNetActiveFlg == NW_NET_ACTIVE))
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
				{
					if(uiNwRMSCnt == NM_TIMEOUT)                          	//1S超时进入正常模式
					{
						euNwState = CAN_NW_NOS_REPEAT_MESSAGE;              //重复状态模式进入正常模式
						uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
						uiNwTimCnt = 0;										//网络管理定时器重置为“0”
						uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;			//设置200ms周期发送NW_PDU
						ucNwSendPduType = SEND_NWAPP_PDU;
					}
				}
				else  /*if(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP)*/
				{
					if(uiNwRMSCnt == NM_TIMEOUT)                          	//1S超时进入正常模式
					{
						euNwState = CAN_NW_NOS_REPEAT_MESSAGE;              //重复状态模式进入正常模式
						uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
						uiNwTimCnt = 0;										//网络管理定时器重置为“0”
						uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;			//设置200ms周期发送NW_PDU
						ucNwSendPduType = SEND_NWAPP_PDU;
					}
				}
			}
			else
			{
				//if(uiNwTimCnt > REPEAT_MESSAGE_TIMER_THR)               //唤醒信号超时1S后
				//if(uiNwLocWkpOffCnt == NM_TIMEOUT)
				if(uiNwRMSCnt == NM_TIMEOUT)	                
				{
					euNwState = CAN_NW_READY_SLEEP;					//重复状态进入准备睡眠模式
					uiNwTimCnt = 500-uiNwPduCycCnt;
					ucNwSendPduType = SEND_APP_PDU;
				}
			}
		}
		break;

		case CAN_NW_RMS_PREPARE_SLEEP:
		{
            if(ImmediateNM_count >= 4 )uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;

            if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
				{
					if(uiNwRMSCnt == NM_TIMEOUT)                          	//1S超时进入正常模式
					{
						euNwState = CAN_NW_NOS_REPEAT_MESSAGE;              //重复状态模式进入正常模式
						uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
						uiNwTimCnt = 0;										//网络管理定时器重置为“0”
						uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;			//设置200ms周期发送NW_PDU
						ucNwSendPduType = SEND_NWAPP_PDU;
					}
				}
				else  /*if(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP)*/
				{
					if(uiNwRMSCnt == NM_TIMEOUT)                          	//1S超时进入正常模式
					{
						euNwState = CAN_NW_NOS_REPEAT_MESSAGE;              //重复状态模式进入正常模式
						uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
						uiNwTimCnt = 0;										//网络管理定时器重置为“0”
						uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;			//设置200ms周期发送NW_PDU
						ucNwSendPduType = SEND_NWAPP_PDU;
					}
				}
			}
			else
			{
				//if(uiNwTimCnt > REPEAT_MESSAGE_TIMER_THR)               //唤醒信号超时1S后
                /*
				if(uiNwLocWkpOffCnt == NM_TIMEOUT)
				{
					euNwState = CAN_NW_READY_SLEEP;					//重复状态进入准备睡眠模式
					uiNwTimCnt = 500-uiNwPduCycCnt;;
					ucNwSendPduType = SEND_APP_PDU;
				}
				*/
				//SW030BT03
				if(uiNwRMSCnt == NM_TIMEOUT)	                
				{
					euNwState = CAN_NW_READY_SLEEP;					//重复状态进入准备睡眠模式
					uiNwTimCnt = 500-uiNwPduCycCnt;
					ucNwSendPduType = SEND_APP_PDU;
				}
			}
		}
		break;

		case CAN_NW_RMS_NORMAL_OPERATION:
		{
            if(ImmediateNM_count >= 4 )uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
			if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
				{
					if(uiNwRMSCnt == NM_TIMEOUT)                          	//1S超时进入正常模式
					{
						euNwState = CAN_NW_NOS_REPEAT_MESSAGE;              //重复状态模式进入正常模式
						uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
						uiNwTimCnt = 0;										//网络管理定时器重置为“0”
						uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;			//设置200ms周期发送NW_PDU
						ucNwSendPduType = SEND_NWAPP_PDU;
						
					}
				}
				else  /*if(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP)*/
				{
					if(uiNwRMSCnt == NM_TIMEOUT)                          	//1S超时进入正常模式
					{
						euNwState = CAN_NW_NOS_REPEAT_MESSAGE;              //重复状态模式进入正常模式
						uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
						uiNwTimCnt = 0;										//网络管理定时器重置为“0”
						uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;			//设置200ms周期发送NW_PDU
						ucNwSendPduType = SEND_NWAPP_PDU;
					}
				}
			}
			else
			{
				//if(uiNwTimCnt > REPEAT_MESSAGE_TIMER_THR)               //唤醒信号超时1S后
				/*
				if(uiNwLocWkpOffCnt == NM_TIMEOUT)
				{
					euNwState = CAN_NW_READY_SLEEP;					//重复状态进入准备睡眠模式
					uiNwTimCnt = 500-uiNwPduCycCnt;;
					ucNwSendPduType = SEND_APP_PDU;
				}
				*/
				//SW030BT03
				if(uiNwRMSCnt == NM_TIMEOUT)	                
				{
					euNwState = CAN_NW_READY_SLEEP;					//重复状态进入准备睡眠模式
					uiNwTimCnt = 500-uiNwPduCycCnt;
					ucNwSendPduType = SEND_APP_PDU;
				}
			}
		}
		break;

		case CAN_NW_RMS_READY_SLEEP:
		{
            if(ImmediateNM_count >= 4 )uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
			if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
				{
					if(uiNwRMSCnt == NM_TIMEOUT)                          	//1S超时进入正常模式
					{
						euNwState = CAN_NW_NOS_REPEAT_MESSAGE;              //重复状态模式进入正常模式
						uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
						uiNwTimCnt = 0;										//网络管理定时器重置为“0”
						uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;			//设置200ms周期发送NW_PDU
						ucNwSendPduType = SEND_NWAPP_PDU;
					}
				}
				else  /*if(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP)*/
				{
					if(uiNwRMSCnt == NM_TIMEOUT)                          	//1S超时进入正常模式
					{
						euNwState = CAN_NW_NOS_REPEAT_MESSAGE;              //重复状态模式进入正常模式
						uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
						uiNwTimCnt = 0;										//网络管理定时器重置为“0”
						uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;			//设置200ms周期发送NW_PDU
						ucNwSendPduType = SEND_NWAPP_PDU;
					}
				}
			}
			else
			{
				//if(uiNwTimCnt > REPEAT_MESSAGE_TIMER_THR)               //唤醒信号超时1S后
				/*
				if(uiNwLocWkpOffCnt == NM_TIMEOUT)
				{
					euNwState = CAN_NW_READY_SLEEP;					//重复状态进入准备睡眠模式
					uiNwTimCnt = 0;
					ucNwSendPduType = SEND_APP_PDU;
				}
				*/
				//SW030BT03
				if(uiNwRMSCnt == NM_TIMEOUT)	                
				{
					euNwState = CAN_NW_READY_SLEEP;					//重复状态进入准备睡眠模式
					uiNwTimCnt = 500-uiNwPduCycCnt;
					ucNwSendPduType = SEND_APP_PDU;
				}
			}
		}
		break;

		case CAN_NW_NOS_REPEAT_MESSAGE:
		{
			//if((ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)||(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP))
			if(RepeatMessageReq == 1)
            {
                RepeatMessageReq = 0;
                euNwState = CAN_NW_RMS_NORMAL_OPERATION;
                uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
                uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;
                uiNwTimCnt = 0;
                ucNwSendPduType = SEND_NWAPP_PDU;
                uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
                ImmediateNM_count = 5;
                uiNwPduCycCnt = 0;
            }
			if((ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)||(ucNetActiveFlg == NW_NET_ACTIVE))
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
				{
					uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
					ucNwSendPduType = SEND_NWAPP_PDU;
				}
				else  /*if(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP)*/
				{
					uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
					ucNwSendPduType = SEND_NWAPP_PDU;
                    
				}
			}
			else
			{
				//if((uiNwTimCnt > REPEAT_MESSAGE_TIMER_THR)||(uiNwRMSCnt == NM_TIMEOUT))
				//if((uiNwLocWkpOffCnt == NM_TIMEOUT)&&(uiNwRMSCnt == NM_TIMEOUT)&&(DiagCnt==NM_TIMEOUT))
                if(DiagCnt==NM_TIMEOUT)
				{
					euNwState = CAN_NW_READY_SLEEP;
					uiNwTimCnt = 500-uiNwPduCycCnt;;
					ucNwSendPduType = SEND_APP_PDU;
				}
			}
		}
		break;

		case CAN_NW_NOS_READY_SLEEP:
		{
            if(RepeatMessageReq == 1)
            {
                RepeatMessageReq = 0;
                euNwState = CAN_NW_RMS_NORMAL_OPERATION;
                uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
                uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;
                uiNwTimCnt = 0;
                ucNwSendPduType = SEND_NWAPP_PDU;
                uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
                ImmediateNM_count = 5;
                uiNwPduCycCnt = 0;
            }

			//if((ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)||(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP))
			if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
				{
					uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
					ucNwSendPduType = SEND_NWAPP_PDU;
				}
				else  /*if(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP)*/
				{
					uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
					ucNwSendPduType = SEND_NWAPP_PDU;
                   
				}
			}
			else
			{
				//if((uiNwTimCnt > REPEAT_MESSAGE_TIMER_THR)||(uiNwRMSCnt == NM_TIMEOUT))
				//if((uiNwLocWkpOffCnt == NM_TIMEOUT)&&(uiNwRMSCnt == NM_TIMEOUT)&&(DiagCnt==NM_TIMEOUT))
				if(DiagCnt==NM_TIMEOUT)
				{
					euNwState = CAN_NW_READY_SLEEP;
					uiNwTimCnt = 500-uiNwPduCycCnt;;
					ucNwSendPduType = SEND_APP_PDU;
				}
			}
		}
		break;

		case CAN_NW_READY_SLEEP:
		{
			if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				euNwState = CAN_NW_NOS_READY_SLEEP;
				uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
				uiNwTimCnt = 0;
				uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
				ucNwSendPduType = SEND_NWAPP_PDU;
			}
			else if(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP)
			{
				/*Wait In CAN_NW_NOS_READY_SLEEP */
				ucNwSendPduType = SEND_APP_PDU;
                if(RepeatMessageReq == 1)
                {
                    RepeatMessageReq = 0;
                    euNwState = CAN_NW_RMS_READY_SLEEP;
                    uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
                    uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;
                    uiNwTimCnt = 0;
                    ucNwSendPduType = SEND_NWAPP_PDU;
                    uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
                    ImmediateNM_count = 5;
                    uiNwPduCycCnt = 0;
                }
			}
			else
			{
			    if(RSSDiagReq == 1)
			    {
			        RSSDiagReq = 0;
			        uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
    				euNwState = CAN_NW_NOS_READY_SLEEP;
    				uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
    				uiNwTimCnt = 0;
    				uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
    				ucNwSendPduType = SEND_NWAPP_PDU;
			    }
				if(uiNwTimCnt > NM_TIMEOUT_TIMER_THR)
				{
					euNwState = CAN_NW_GOTO_SLEEP;
					uiNwTimCnt = 0;
					ucNwSendPduType = SEND_DISABLE;
				}
				if(RepeatMessageReq == 1)
                {
                    RepeatMessageReq = 0;
                    euNwState = CAN_NW_RMS_READY_SLEEP;
                    uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
                    uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;
                    uiNwTimCnt = 0;
                    ucNwSendPduType = SEND_NWAPP_PDU;
                    uiSetNwPduCycVal = NM_NORMAL_CYCLE_TIMER_THR;
                    ImmediateNM_count = 5;
                    uiNwPduCycCnt = 0;
                }
			}
		}
		break;

		case CAN_NW_GOTO_SLEEP:
		{

			if((ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)||(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP))
			{
				uiNwLocWkpOffCnt = NM_TIMEOUT_TIMER_THR;
				euNwState = CAN_NW_RMS_PREPARE_SLEEP;
				uiNwRMSCnt = REPEAT_MESSAGE_TIMER_THR;			 //设置1S超时后进入正常模式定时器
				uiSetNwPduCycVal = REPEAT_NW_CYCLE_TIMER_THR;		 //设置100ms周期发送NW_PDU
				uiNwTimCnt = 0;
				ucNwSendPduType = SEND_NWAPP_PDU;
				uiNwPduCycCnt = 0;
				if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)    //本地唤醒进入立即发送网络管理报文模式
                {
                    ImmediateNM_count = 0;
                    uiSetNwPduCycVal = IMMEDIATENM_CYCLE_TIMER_THR;
                }
                else
                {
                    ImmediateNM_count = 5;
                    uiSetNwPduCycVal = REPEAT_NW_CYCLE_TIMER_THR;
                }
			}
			else
			{
				if(uiNwTimCnt > WAIT_BUS_SLEEP_TIMER_THR)
				{
				    
					//满足进入睡眠条件后 执行睡眠程序，注意唤醒后状态机必须切换到 "euNwState = CAN_NW_RESERVED"
					if(ucNwNetIdelFlg == 1)
					{
						uiNwTimCnt = 0;
						ucNwSendPduType = SEND_DISABLE;
						
						FlgTemp = 0;
					    FlgTemp1 = 0;
					    C0CTRH |= 0x0300;		  // 设置监听模式
						
						SleepDetect();
					}
					else
					{
						//waiting can bus idel
						uiNwTimCnt = WAIT_BUS_SLEEP_TIMER_THR;
						ucNwSendPduType = SEND_DISABLE;
					}

				}
			}
		}
		break;

		default:
		{
			euNwState = CAN_NW_GOTO_SLEEP;
			uiNwTimCnt = 0;
		}
		break;
	}

	TxCanNwMsg_6B7.Msg_Part.RCP3_Address=0x36;
    /*
    if(euNwState == CAN_NW_RMS_SLEEP || euNwState == CAN_NW_RMS_PREPARE_SLEEP)
        TxCanNwMsg_6B7.Msg_Part.RCP3_RMR=1;
    else
        TxCanNwMsg_6B7.Msg_Part.RCP3_RMR=0;
    */ //20190707一直是0
    TxCanNwMsg_6B7.Msg_Part.RCP3_RMR=0;
    
	if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
	{
		TxCanNwMsg_6B7.Msg_Part.RCP3_AWB = 1;
	}
	else
	{
		TxCanNwMsg_6B7.Msg_Part.RCP3_AWB = 0;
	}



	if((euNwState == CAN_NW_NOS_REPEAT_MESSAGE)||(euNwState == CAN_NW_NOS_READY_SLEEP))
	{
		TxCanNwMsg_6B7.Msg_Part.RCP3_NMSts = 1;
	}
	else
	{
		TxCanNwMsg_6B7.Msg_Part.RCP3_NMSts = 0;
	}

	if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
	{
		TxCanNwMsg_6B7.Msg_Part.RCP3_Stayawake_reasons_2=0;
	}
	else
	{
		TxCanNwMsg_6B7.Msg_Part.RCP3_Stayawake_reasons_2=0;
	}

	if(ucActiveWakeupBit == REMOTE_ACTIVE_WAKEUP)
	{
		TxCanNwMsg_6B7.Msg_Part.RCP3_Wakeup_reasons=2;
	}
	else if(ucActiveWakeupBit == LOCAL_ACTIVE_WAKEUP)
	{
		TxCanNwMsg_6B7.Msg_Part.RCP3_Wakeup_reasons=1;
	}
    else
    {
        TxCanNwMsg_6B7.Msg_Part.RCP3_Wakeup_reasons=0;
    }

	TxCanNwMsg_6B7.Msg_Part.RCP3_Stayawake_reasons_1=0;
	TxCanNwMsg_6B7.Msg_Part.RCP3_Stayawake_reasons_4=0;
	TxCanNwMsg_6B7.Msg_Part.RCP3_Stayawake_reasons_5=0;
	TxCanNwMsg_6B7.Msg_Part.RCP3_Stayawake_reasons_6=0;
	TxCanNwMsg_6B7.Msg_Part.RCP3_Stayawake_reasons_7=0;
	TxCanNwMsg_6B7.Msg_Part.RCP3_Stayawake_reasons_8=0;

	Copy_Array_to_Array(TxCanNwPdu_436.DB,TxCanNwMsg_6B7.Msg_Byte,8);


}



unsigned char CanNwWakeupSourceCheck(unsigned char LocalSource,unsigned char *RemoteSource)
{
	static unsigned char ucRemWkpTemp = 0u;
	unsigned char ucRtnTemp;
	ucRtnTemp = PASSIVE_WAKEUP;
	if(ucNwPowerUpFlg == 0u)   //上电初始化后进入循环后网络管理定时器清"0"
	{
		ucNwPowerUpFlg = 1u;
		uiNwTimCnt = 0u;
		ucRemWkpTemp = 0;
	}
	else
	{
		if(uiNwTimCnt > (NM_NORMAL_CYCLE_TIMER_THR+REPEAT_NW_CYCLE_TIMER_THR)||(*RemoteSource == 1))           //300ms检测一次远程标志位
		{
			if(*RemoteSource == 1)
			{
				*RemoteSource = 0;
				uiNwTimCnt = 0;
				ucRemWkpTemp = 1;
			}
			else
			{
				ucRemWkpTemp = 0;
			}
		}
	}
	if(LocalSource == IGN_ON)
	{
	    ucPreLocWkpStu = LOCAL_ACTIVE_WAKEUP;
		ucRtnTemp = LOCAL_ACTIVE_WAKEUP;
	}
	else if(ucRemWkpTemp == 1)
	{
		if(ucPreLocWkpStu == LOCAL_ACTIVE_WAKEUP)
		{
			ucRtnTemp = PASSIVE_WAKEUP;
		}
		else
		{
			ucRtnTemp = REMOTE_ACTIVE_WAKEUP;
		}
	}
	else
	{
	    ucPreLocWkpStu = PASSIVE_WAKEUP;
		ucRtnTemp = PASSIVE_WAKEUP;
	}
	return ucRtnTemp;
}




unsigned char CanNwNetIdelCheck(unsigned char *IdelFlg)
{
	static unsigned int suiIdelTimCnt = 0;
	static unsigned char sucIdelCheckCnt = 0;

	if(++suiIdelTimCnt > 30)
	{
		if(*IdelFlg == 0)
		{
			if(++sucIdelCheckCnt > 3)
			{
				sucIdelCheckCnt = 3;
				return 1;
			}
			else
			{
				suiIdelTimCnt = 0;
			}
		}
		else
		{
			*IdelFlg = 0;
			suiIdelTimCnt = 0;
			sucIdelCheckCnt = 0;
		}

		if(suiIdelTimCnt > 40)
		{
			suiIdelTimCnt = 40;
		}
	}

	return 0;
}











