/**********************************************************************************************************
 * Copyright(C) 2017, HeiLongJiang TYW Co.,Ltd.
 *
 * @Filename:           NM.c
 *
 * @Description:        AutoSar Net Manage
 *
 * @Functions List:
 *
 *
 * @History:            Version:        V0.0.1
                    Date:
                            Author:         myliu
                            Modification:  creat
 **********************************************************************************************************/

/**********************************************************************************************************
 * Include
*/
#include "Include.h"
#include "NM_Protocol.h"

/**********************************************************************************************************
 * Constants
 */


/**********************************************************************************************************
 * Local variables
 */
static NM_Mode_t                  NMMode                = Bus_Sleep_Mode;
static NM_Event_List_t            NMEventReq            = {0};
static NetworkMode_State_t        NetworkModeState      = Repeat_Message_State;
static unsigned   char                    NM_MessageCycle_Count = 0u;
static NM_Wakeup_Reasons_t        NMWakeupReason        = NM_Default;
static Network_Request_Reasons_t  NMStayawakeupReason   = NM_NOT_AWAKE;

/*Timer State*/
static NM_Timer_State_t NMTimerState = {0};
/*Timer Counter*/
static unsigned  long  NM_Trms_Counter;
static unsigned  long  NM_Tnm_Counter;
static unsigned  long  NM_Twbs_Counter;
static unsigned  long  NM_Ticyc_Counter;
static unsigned  long  NM_Tcyc_Counter;
static unsigned  long  NM_Tdiag_Counter;

/**********************************************************************************************************
 * Local functions
 */
static void CanNm_CutNetworkMode ( void );
static void CanNm_NetworkMode_Service ( void );
static void CanNm_Repeat_Message_Sevice ( void );
static void CanNm_Normal_Operation_Sevice ( void );
static void CanNm_Ready_Sleep_Sevice ( void );;
static void CanNm_ClearEvent ( void );

/**********************************************************************************************************
 * Defines
 */
/*Condition*/
#define NM_CONDITION_1              NMEventReq.Bits.Network_Requested
#define NM_CONDITION_2              NMEventReq.Bits.Succ_Recv_NMPDU
#define NM_CONDITION_3              NMEventReq.Bits.Succ_Trans_NMPDU
#define NM_CONDITION_4              NMEventReq.Bits.NM_Trms_Timeout
#define NM_CONDITION_5              NMEventReq.Bits.Network_Released
#define NM_CONDITION_6              NMEventReq.Bits.NM_Tnm_Timeout
#define NM_CONDITION_7              NMEventReq.Bits.Repeat_Msg_Bit_Recv
#define NM_CONDITION_8              NMEventReq.Bits.NM_Twbs_Timeout
#define NM_CONDITION_9              NMEventReq.Bits.NM_Repeat_Msg_Req
#define NM_CONDITION_10             NMEventReq.Bits.NM_Ticyc_Timeout
#define NM_CONDITION_11             NMEventReq.Bits.NM_Tcyc_Timeout

/*Action*/
#define NM_ACTION_1                 NMReStartTimer(NM_Trms_Timer);

#define NM_ACTION_2                 NMReStartTimer(NM_Tnm_Timer);

#define NM_ACTION_3                 D_Offline();                      \
    NMReStartTimer(NM_Twbs_Timer);    \
    NMMode = Pre_Bus_Sleep_Mode;

#define NM_ACTION_4                 CanNm_Data_Request(NMWakeupReason, NMStayawakeupReason);

#define NM_ACTION_5                 NMStopTimer(NM_Ticyc_Timer);      \
    NMStopTimer(NM_Tcyc_Timer);

#define NM_ACTION_6                 CanNm_ClearEvent();               \
    D_Init(BusSleep);                 \
    D_Offline();                      \
    NMMode = Bus_Sleep_Mode;

#define NM_ACTION_7                 NMStopTimer(NM_Tcyc_Timer);       \
    NMReStartTimer(NM_Ticyc_Timer);

#define NM_ACTION_8                 NM_MessageCycle_Count = 1u;        \
    NMStopTimer(NM_Ticyc_Timer);
/*NMReStartTimer(NM_Tcyc_Timer);*/

/**********************************************************************************************************
 * Global variables
 */

/**********************************************************************************************************
 * Global functions
 */


/**********************************************************************************************************
 *@Function:            NM_Timer_Service
 *@Description:       网络管理中定时器服务，判断使能的定时器是否超时.
 *@Input:                 none.
 *@Output:              none.
 *@Calls:                   none.
 *@Return:              none.
 *@Note:                    在中断API_ISR中调用.
 **********************************************************************************************************/
void CanNm_Timer_Service ( unsigned  long API_Cycle )
{
    /*T_REPEAT_MESSAGE*/
    if ( NMTimerState.NM_Trms_Enable )
    {
        NM_Trms_Counter += API_Cycle;
        if ( NM_Trms_Counter >= T_REPEAT_MESSAGE )
        {
            NM_Trms_Counter = 0u;
            NMTimerState.NM_Trms_Timeout = 1u;
            CanNm_TimeroutRequest ( NM_Trms_Timer );
        }
    }
    /*T_NM_TIMEOUT*/
    if ( NMTimerState.NM_Tnm_Enable )
    {
        NM_Tnm_Counter += API_Cycle;
        if ( NM_Tnm_Counter >= T_NM_TIMEOUT )
        {
            NM_Tnm_Counter = 0u;
            NMTimerState.NM_Tnm_Timeout = 1u;
            CanNm_TimeroutRequest ( NM_Tnm_Timer );
        }
    }
    /*T_WAIT_BUS_SLEEP*/
    if ( NMTimerState.NM_Twbs_Enable )
    {
        NM_Twbs_Counter += API_Cycle;
        if ( NM_Twbs_Counter >= T_WAIT_BUS_SLEEP )
        {
            NM_Twbs_Counter = 0u;
            NMTimerState.NM_Twbs_Timeout = 1u;
            CanNm_TimeroutRequest ( NM_Twbs_Timer );
        }
    }
    /*T_NM_ImmediateCycleTime*/
    if ( NMTimerState.NM_Ticyc_Enable )
    {
        NM_Ticyc_Counter += API_Cycle;
        if ( NM_Ticyc_Counter >= T_NM_ImmediateCycleTime )
        {
            NM_Ticyc_Counter = 0u;
            NMTimerState.NM_Ticyc_Timeout = 1u;
            CanNm_TimeroutRequest ( NM_Ticyc_Timer );
        }

    }
    /*T_NM_MessageCycle*/
    if ( NMTimerState.NM_Tcyc_Enable )
    {
        NM_Tcyc_Counter += API_Cycle;
        if ( NM_Tcyc_Counter >= T_NM_MessageCycle )
        {
            NM_Tcyc_Counter = 0u;
            NMTimerState.NM_Tcyc_Timeout = 1u;
            CanNm_TimeroutRequest ( NM_Tcyc_Timer );
        }
    }

    /*T_NM_MessageCycle*/
    if ( NMTimerState.NM_Tdiag_Enable )
    {
        NM_Tdiag_Counter += API_Cycle;
        if ( NM_Tdiag_Counter >= T_DIAG_TIMEOUT )
        {
            NM_Tdiag_Counter = 0u;
            NMTimerState.NM_Tdiag_Timeout = 1u;
        }
    }
}

/**********************************************************************************************************
 *@Function:          NMStartTimer
 *@Description:     启动网路管理的定时器.
 *@Input:               NMTimerName_t TimerName -- 启动定时器的名称.
 *@Output:            none.
 *@Calls:               none.
 *@Return:            none.
 *@Note:                none.
 **********************************************************************************************************/
void NMReStartTimer ( NM_Timer_Name_t NMTimerName )
{
    switch ( NMTimerName )
    {
    case NM_Trms_Timer:
        NMTimerState.NM_Trms_Enable = 1u;
        NM_Trms_Counter = 0u;
        break;

    case NM_Tnm_Timer:
        NMTimerState.NM_Tnm_Enable = 1u;
        NM_Tnm_Counter = 0u;
        break;

    case NM_Twbs_Timer:
        NMTimerState.NM_Twbs_Enable = 1u;
        NM_Twbs_Counter = 0u;
        break;

    case NM_Ticyc_Timer:
        NMTimerState.NM_Ticyc_Enable = 1u;
        NM_Ticyc_Counter = 0u;
        break;

    case NM_Tcyc_Timer:
        NMTimerState.NM_Tcyc_Enable = 1u;
        NM_Tcyc_Counter = 0u;
        break;

    case NM_Tdiag_Timer:
        NMTimerState.NM_Tdiag_Enable = 1u;
        NM_Tdiag_Counter = 0u;
        break;

    default:
        break;
    }

    return;
}

/**********************************************************************************************************
 *@Function:          NMStopTimer
 *@Description:     取消网路管理的定时器.
 *@Input:               NMTimerName_t TimerName -- 启动定时器的名称.
 *@Output:            none.
 *@Calls:               none.
 *@Return:            none.
 *@Note:                none.
 **********************************************************************************************************/
void NMStopTimer ( NM_Timer_Name_t NMTimerName )
{
    switch ( NMTimerName )
    {
    case NM_Trms_Timer:
        NMTimerState.NM_Trms_Enable = 0u;
        break;

    case NM_Tnm_Timer:
        NMTimerState.NM_Tnm_Enable = 0u;
        break;

    case NM_Twbs_Timer:
        NMTimerState.NM_Twbs_Enable = 0u;
        break;

    case NM_Ticyc_Timer:
        NMTimerState.NM_Ticyc_Enable = 0u;
        break;

    case NM_Tcyc_Timer:
        NMTimerState.NM_Tcyc_Enable = 0u;
        break;

    case NM_Tdiag_Timer:
        NMTimerState.NM_Tdiag_Enable = 0u;
        break;

    default:
        break;
    }

    return;
}

/**********************************************************************************************************
 *@Function:          IsNMTimerOut
 *@Description:     查询NM定时器是否超时.
 *@Input:               NMTimerName_t TimerName -- 启动定时器的名称.
 *@Output:            none.
 *@Calls:               none.
 *@Return:            none.
 *@Note:                none.
 **********************************************************************************************************/
unsigned   char IsNMTimerOut ( NM_Timer_Name_t NMTimerName )
{
    unsigned   char TimeoutFlag = 0u;

    switch ( NMTimerName )
    {
    case NM_Trms_Timer:
        TimeoutFlag = NMTimerState.NM_Trms_Timeout;
        NMTimerState.NM_Trms_Timeout = 0u;
        break;

    case NM_Tnm_Timer:
        TimeoutFlag = NMTimerState.NM_Tnm_Timeout;
        NMTimerState.NM_Tnm_Timeout = 0u;
        break;

    case NM_Twbs_Timer:
        TimeoutFlag = NMTimerState.NM_Twbs_Timeout;
        NMTimerState.NM_Twbs_Timeout = 0u;
        break;

    case NM_Ticyc_Timer:
        TimeoutFlag = NMTimerState.NM_Ticyc_Timeout;
        NMTimerState.NM_Ticyc_Timeout = 0u;
        break;

    case NM_Tcyc_Timer:
        TimeoutFlag = NMTimerState.NM_Tcyc_Timeout;
        NMTimerState.NM_Tcyc_Timeout = 0u;
        break;

    case NM_Tdiag_Timer:
        TimeoutFlag = NMTimerState.NM_Tdiag_Timeout;
        NMTimerState.NM_Tdiag_Timeout = 0u;
        break;

    default:
        break;
    }

    return TimeoutFlag;
}
/**********************************************************************************************************
 *@Function:        CanNm_Init
 *@Description: 初始化链路层，关闭应用通信，设置初始状态为睡眠.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             D_Init / D_Offline.
 *@Return:        none..
 *@Note:              在中断中调用.
 **********************************************************************************************************/
void CanNm_Init ( void )
{
    D_Init ( BusInit );
    D_Offline();
    NMEventReq.Words = 0u;
    NMStopTimer ( NM_Trms_Timer );
    NMStopTimer ( NM_Tnm_Timer );
    NMStopTimer ( NM_Twbs_Timer );
    NMStopTimer ( NM_Ticyc_Timer );
    NMStopTimer ( NM_Tcyc_Timer );
    D_Init ( BusSleep );
    NMMode = Bus_Sleep_Mode;
    NM_Init_AppTimer();
}

/**********************************************************************************************************
 *@Function:        CanNm_NetworkRequest
 *@Description: 请求网络通信，一般由应用层调用.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             none.
 *@Return:        none..
 *@Note:              none.
 **********************************************************************************************************/
void CanNm_NetworkRequest ( Network_Request_Reasons_t StayawakeReason )
{
    if ( StayawakeReason == NM_REQ_DIAG_RECVD )
    {
        if ( ( NMMode != Network_Mode ) || ( NetworkModeState != Ready_Sleep_State ) )
        {

        }
        else
        {
            NM_CONDITION_1 = 1u;
            NM_CONDITION_5 = 0u;

            NMStayawakeupReason = StayawakeReason;
        }
    }
    else
    {
        NM_CONDITION_1 = 1u;
        NM_CONDITION_5 = 0u;

        NMStayawakeupReason = StayawakeReason;
    }


}

/**********************************************************************************************************
 *@Function:        CanNm_NetworkRelease
 *@Description: 释放网络，同意网络进入睡眠，一般在IGN_OFF情况且无报警情况下调用.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             none.
 *@Return:        none..
 *@Note:              none.
 **********************************************************************************************************/
void CanNm_NetworkRelease ( Network_Release_Reasons_t ReleaseReason )
{
	if ( ReleaseReason == NM_REQ_DIAG_TIMEOUT )
	{
		if ( NMStayawakeupReason == NM_REQ_KL15_ON )
		{

		}
		else
		{
			NM_CONDITION_5 = 1u;
			NM_CONDITION_1 = 0u;
			NMStayawakeupReason = NM_NOT_AWAKE;
		}
	}
	else if (ReleaseReason == NM_REQ_KL15_OFF)
	{
		if (NMStayawakeupReason == NM_REQ_DIAG_RECVD)
		{

		}
		else
		{
			NM_CONDITION_5 = 1u;
			NM_CONDITION_1 = 0u;
			NMStayawakeupReason = NM_NOT_AWAKE;
		}
	}
	else
	{
		NM_CONDITION_5 = 1u;
		NM_CONDITION_1 = 0u;
		NMStayawakeupReason = NM_NOT_AWAKE;
	}
}

/**********************************************************************************************************
 *@Function:        CanNm_RecvNMPDUInd
 *@Description: 提示网络管理，收到了网路管理报文，一般由链路层调用.
 *@Input:           CANMsg402_t *pCanNmMsg -- 网络管理报文.
 *@Output:        none.
 *@Calls:             none.
 *@Return:        none..
 *@Note:              none.
 **********************************************************************************************************/
void CanNm_RecvNMPDUInd ( unsigned   char m_CanNmMsg[ ] )
{
	unsigned   char i;
	CANNmMsg_t pCanNmMsg;
	NM_CONDITION_2 = 1u;


	for (i = 0u; i < 8u; i++)
	{
		pCanNmMsg.Msg[i] = m_CanNmMsg[i];
	}


	if ( ( NMMode == Network_Mode ) && ( ( NetworkModeState == Ready_Sleep_State ) || ( NetworkModeState == Normal_Operation_State ) ) )
	{
		if ( pCanNmMsg.Sig.ICU_RMR0 )
		{
			NM_CONDITION_7 = 1u;
		}
	}
}

/**********************************************************************************************************
 *@Function:        CanNm_TransNMPDUInd
 *@Description: 提示网络管理，已经成功发送网路管理报文，一般由链路层调用.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             none.
 *@Return:        none..
 *@Note:              none.
 **********************************************************************************************************/
void CanNm_TransNMPDUInd ( void )
{
	NM_CONDITION_3 = 1u;
}

/**********************************************************************************************************
 *@Function:        CanNm_RepeatMessageRequest
 *@Description: 进入重复状态请求，一般主节点使用，仪表属于从节点，一般不适用.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             none.
 *@Return:        none..
 *@Note:              none.
 **********************************************************************************************************/
void CanNm_RepeatMessageRequest ( void )
{
	NM_CONDITION_9 = 1u;
}

/**********************************************************************************************************
 *@Function:        CanNm_TimeroutRequest
 *@Description: 网络管理定时器，请求网络超时事件.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             none.
 *@Return:        none..
 *@Note:              由网络管理定时器调用.
 **********************************************************************************************************/
void CanNm_TimeroutRequest ( NM_Timer_Name_t NMTimerName )
{
	switch ( NMTimerName )
	{
		case NM_Trms_Timer:
			NM_CONDITION_4 = 1u;
		break;

		case NM_Tnm_Timer:
			NM_CONDITION_6 = 1u;
		break;

		case NM_Twbs_Timer:
			NM_CONDITION_8 = 1u;
		break;

		case NM_Ticyc_Timer:
			NM_CONDITION_10 = 1u;
		break;

		case NM_Tcyc_Timer:
			NM_CONDITION_11 = 1u;
		break;

		default:
		break;
	}

	return;
}

/**********************************************************************************************************
 *@Function:        CanNm_GetStatus
 *@Description: 获取网络管理当前的主要模式，睡眠，唤醒，睡前准备
 *@Input:           none.
 *@Output:        none.
 *@Calls:             none.
 *@Return:        NM_Mode_t Mode -- 网络当前模式.
 *@Note:              一般由电源管理调用，当网络管理处于睡眠状态后，电源可以进入低功耗模式.
 **********************************************************************************************************/
NM_Mode_t CanNm_GetMode ( void )
{
    return NMMode;
}

/**********************************************************************************************************
 *@Function:        CanNm_GetState
 *@Description: 获取网络管理当前的状态，主要是休眠还是唤醒.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             none.
 *@Return:        NetworkMode_State_t Mode -- 网络当前状态.
 *@Note:              发送报文时，需要此状态.
 **********************************************************************************************************/
NetworkMode_State_t CanNm_GetState ( void )
{
    return NetworkModeState;
}

/**********************************************************************************************************
 *@Function:        CanNm_Service
 *@Description: 网络管理服务，进行状态切换.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             CanNm_NetworkMode_Service.
 *@Return:        none..
 *@Note:              一般1ms调用一次，由于网络管理均有时间要求，所以此函数要求在中断调用.
 **********************************************************************************************************/
void CanNm_Service ( void )
{
	switch ( NMMode )
	{
		case Bus_Sleep_Mode:
		/*NM_Clr_AliveCnt();*/
		if (( NM_CONDITION_1 ) || ( NM_CONDITION_2 ))
		{
			D_Init ( BusAwake );
			NM_ACTION_1
			NM_ACTION_2
			NM_Init_AppTimer();
			if ( NM_CONDITION_1 )
			{
				NMWakeupReason = NM_KL15;
				NM_ACTION_4
				NM_ACTION_7
			}
			else
			{
				NM_CONDITION_2 = 0u;
				NM_CONDITION_7 = 0u;
				NMWakeupReason = NM_PDU_Received;
				NM_ACTION_4
				NM_ACTION_8
				NMReStartTimer ( NM_Tcyc_Timer );
			}

			CanNm_CutNetworkMode();
		}
		break;

		case Pre_Bus_Sleep_Mode:
		if (( NM_CONDITION_1) || (NM_CONDITION_2 ) )
		{
			NM_ACTION_1
			NM_ACTION_2
			if ( NM_CONDITION_1 )
			{
				NMWakeupReason = NM_KL15;
				NM_ACTION_4
				NM_ACTION_7
			}
			else
			{
				NM_CONDITION_2 = 0u;
				NM_CONDITION_7 = 0u;
				NMWakeupReason = NM_PDU_Received;
				NM_ACTION_4
				NM_ACTION_8
				NMReStartTimer ( NM_Tcyc_Timer );
			}
			NM_CONDITION_8 = 0u;
			NMStopTimer ( NM_Twbs_Timer );
			CanNm_CutNetworkMode();
		}
		else if ( NM_CONDITION_8 )
		{
			NM_CONDITION_8 = 0u;
			NM_ACTION_6
		}
		else
		{

		}
		break;

		case Network_Mode:
		if (( NM_CONDITION_2 ) || ( NM_CONDITION_3 ))
		{
			NM_CONDITION_2 = 0u;
			NM_CONDITION_3 = 0u;
			NM_ACTION_2
			D_Online();
		}
		CanNm_NetworkMode_Service();
		break;

		default:
		break;
	}

	return;
}

/**********************************************************************************************************
 *@Function:        CanNm_NetworkMode_Service
 *@Description: 网络模式下服务函数.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             CanNm_Repeat_Message_Sevice / CanNm_Normal_Operation_Sevice / CanNm_Ready_Sleep_Sevice.
 *@Return:        none..
 *@Note:              在中断中调用.
 **********************************************************************************************************/
static void CanNm_NetworkMode_Service ( void )
{
	switch ( NetworkModeState )
	{
		case Repeat_Message_State:
			CanNm_Repeat_Message_Sevice();
		break;

		case Normal_Operation_State:
			CanNm_Normal_Operation_Sevice();
		break;

		case Ready_Sleep_State:
			CanNm_Ready_Sleep_Sevice();
		break;

		default:
		break;
	}

	return;
}

/**********************************************************************************************************
 *@Function:        CanNm_Repeat_Message_Sevice
 *@Description: Repeat Message State服务函数.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             NM_ACTION_2 / NM_ACTION_4 / NM_ACTION_5 / NM_ACTION_7 / NM_ACTION_8.
 *@Return:        none.
 *@Note:              内部调用.
 **********************************************************************************************************/
static void CanNm_Repeat_Message_Sevice ( void )
{
	if ( NM_CONDITION_6 )
	{
		NM_CONDITION_6 = 0u;
		NM_ACTION_2
	}
	else if (( NM_CONDITION_1 ) &&  ( NM_CONDITION_4 ))
	{
		NM_CONDITION_4 = 0u;
		NMStopTimer ( NM_Trms_Timer );
		/*        NMWakeupReason = NM_KL15;
		NM_ACTION_4*/
		NM_ACTION_8
		/*NMReStartTimer ( NM_Tcyc_Timer );*/
		NetworkModeState = Normal_Operation_State;
	}
	else if (( NM_CONDITION_4 ) && ( NM_CONDITION_5 ))
	{
		NM_CONDITION_4 = 0u;
		NMStopTimer ( NM_Trms_Timer );
		NM_ACTION_5
		NetworkModeState = Ready_Sleep_State;
	}
	else if ( NM_CONDITION_10 )
	{
		NM_CONDITION_10 = 0u;
		NM_MessageCycle_Count++;
		NM_ACTION_4
		if ( NM_MessageCycle_Count < N_ImmediateNM_TIMES )
		{
			NM_ACTION_7
		}
		else
		{
			NM_ACTION_8
			NMReStartTimer ( NM_Tcyc_Timer );
		}
	}
	else if ( NM_CONDITION_11 )
	{
		NM_CONDITION_11 = 0u;
		NM_ACTION_4
		NM_ACTION_8
		NMReStartTimer ( NM_Tcyc_Timer );
	}
	else
	{

	}
}

/**********************************************************************************************************
 *@Function:        CanNm_Normal_Operation_Sevice
 *@Description: Normal Operation State服务函数.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             NM_ACTION_1 / NM_ACTION_2 / NM_ACTION_4 / NM_ACTION_5 / NM_ACTION_7 / NM_ACTION_8.
 *@Return:        none.
 *@Note:              内部调用.
 **********************************************************************************************************/
static void CanNm_Normal_Operation_Sevice ( void )
{
	if ( NM_CONDITION_6 )
	{
		NM_CONDITION_6 = 0u;
		NM_ACTION_2
	}
	else if (( NM_CONDITION_7 ) || (NM_CONDITION_9 ))
	{
		/*NMWakeupReason = NM_PDU_Received;*/
		NetworkModeState = Repeat_Message_State;
		NM_ACTION_1
		NM_ACTION_4
		if ( NM_CONDITION_7 )
		{
			NM_CONDITION_7 = 0u;
			NMReStartTimer ( NM_Tcyc_Timer );
			/*NM_ACTION_7*/
		}
		else
		{
			NM_CONDITION_9 = 0u;
			NM_ACTION_8
			NMReStartTimer ( NM_Tcyc_Timer );
		}
	}
	else if ( NM_CONDITION_5 )
	{
		NM_ACTION_5
		NM_ACTION_2
		NetworkModeState = Ready_Sleep_State;
	}
	else if ( NM_CONDITION_10 )
	{
		NM_CONDITION_10 = 0u;
		NM_MessageCycle_Count++;
		NM_ACTION_4
		if ( NM_MessageCycle_Count < N_ImmediateNM_TIMES )
		{
			NM_ACTION_7
		}
		else
		{
			NM_ACTION_8
			NMReStartTimer ( NM_Tcyc_Timer );
		}
	}
	else if ( NM_CONDITION_11 )
	{
		NM_CONDITION_11 = 0u;
		NM_ACTION_4
		NM_ACTION_8
		NMReStartTimer ( NM_Tcyc_Timer );
	}
	else
	{

	}
}

/**********************************************************************************************************
 *@Function:        CanNm_Ready_Sleep_Sevice
 *@Description: Ready Sleep State服务函数.
 *@Input:           none.
 *@Output:        none.
 *@Calls:             NM_ACTION_2 / NM_ACTION_4 / NM_ACTION_5 / NM_ACTION_7 / NM_ACTION_8.
 *@Return:        none.
 *@Note:              内部调用.
 **********************************************************************************************************/
static void CanNm_Ready_Sleep_Sevice ( void )
{
	if ( NM_CONDITION_1 )
	{
		/*if(NMStayawakeupReason == NM_REQ_KL15_ON)
		NMWakeupReason = NM_KL15;
		else
		NMWakeupReason = NM_Default;*/

		NetworkModeState = Normal_Operation_State;
		NM_ACTION_4
		NM_ACTION_8
		NMReStartTimer ( NM_Tcyc_Timer );
	}
	else if (( NM_CONDITION_7) || (NM_CONDITION_9 ))
	{
		/*NMWakeupReason = NM_PDU_Received;*/
		NetworkModeState = Repeat_Message_State;
		NM_ACTION_4
		if ( NM_CONDITION_7 )
		{
			NM_CONDITION_7 = 0u;
			NMReStartTimer ( NM_Tcyc_Timer );
			/*NM_ACTION_7*/
		}
		else
		{
			NM_CONDITION_9 = 0u;
			NM_ACTION_8
			NMReStartTimer ( NM_Tcyc_Timer );
		}
		NM_ACTION_1
	}
	else if ( NM_CONDITION_6 )
	{
		NM_CONDITION_6 = 0u;
		NM_ACTION_3
	}
	else
	{
	}
}

/**********************************************************************************************************
 *@Function:            CanNm_CutNetworkMode
 *@Description:       网络管理中定时器服务，判断使能的定时器是否超时.
 *@Input:                 none.
 *@Output:              none.
 *@Calls:                   none.
 *@Return:              none..
 *@Note:                    内部调用.
 **********************************************************************************************************/
static void CanNm_CutNetworkMode ( void )
{
	NM_MessageCycle_Count = 1u;
	NetworkModeState = Repeat_Message_State;
	NMMode = Network_Mode;
}

/**********************************************************************************************************
 *@Function:            CanNm_ClearEvent
 *@Description:       进入睡眠前清空网络管理事件（申请和释放网络除外）.
 *@Input:                 none.
 *@Output:              none.
 *@Calls:                   none.
 *@Return:              none..
 *@Note:                    内部调用.
 **********************************************************************************************************/
static void CanNm_ClearEvent ( void )
{
	/*Clear Event Req*/
	NM_CONDITION_2 = 0u;
	NM_CONDITION_3 = 0u;
	NM_CONDITION_4 = 0u;
	NM_CONDITION_6 = 0u;
	NM_CONDITION_7 = 0u;
	NM_CONDITION_8 = 0u;
	NM_CONDITION_9 = 0u;

	/*Stop Timer*/
	NMStopTimer ( NM_Trms_Timer );
	NMStopTimer ( NM_Tnm_Timer );
	NMStopTimer ( NM_Twbs_Timer );
}
/*获取当前库版本版本号*/

/* *pCanNmMsg 修改为 pCanNmMsg 不使用指针   02版本*/
/* busoff下不再发送repeat msg信号 不使用指针   03版本*/
unsigned  long CanNM_Read_Verison(void)
{
	unsigned  long Verison;
	Verison =0x00000003; /* *pCanNmMsg 修改为 pCanNmMsg 不使用指针 */
	return Verison;
}
/**********************************************************************************************************
 **********************************************************************************************************/

