/*
* ico_nmt.h - contains internal defines for NMT
*
* Copyright (c) 2012-2023 emotas embedded communication GmbH
*
*-------------------------------------------------------------------
* $Id: ico_nmt.h 49616 2023-09-26 14:22:08Z boe $
*
*-------------------------------------------------------------------
*
*
*/

/**
* \file
* \brief dataty type header
*/

#ifndef ICO_NMT_H
#define ICO_NMT_H 1

#ifdef CO_EVENT_DYNAMIC_NMT
# ifdef CO_EVENT_PROFILE_NMT
#  define CO_EVENT_NMT_CNT	(CO_EVENT_DYNAMIC_NMT + CO_EVENT_PROFILE_NMT)
# else /* CO_EVENT_PROFILE_NMT */
#  define CO_EVENT_NMT_CNT	(CO_EVENT_DYNAMIC_NMT)
# endif /* CO_EVENT_PROFILE_NMT */
#else /* CO_EVENT_DYNAMIC_NMT */
# ifdef CO_EVENT_PROFILE_NMT
#  define CO_EVENT_NMT_CNT	(CO_EVENT_PROFILE_NMT)
# endif /* CO_EVENT_PROFILE_NMT */
#endif /* CO_EVENT_DYNAMIC_NMT */

#if defined(CO_EVENT_STATIC_NMT) || defined(CO_EVENT_NMT_CNT)
# define CO_EVENT_NMT   1u
#endif /* defined(CO_EVENT_STATIC_NMT) || defined(CO_EVENT_NMT_CNT) */

#ifdef CO_EVENT_DYNAMIC_ERRCTRL
# ifdef CO_EVENT_PROFILE_ERRCTRL
#  define CO_EVENT_ERRCTRL_CNT	(CO_EVENT_DYNAMIC_ERRCTRL + CO_EVENT_PROFILE_ERRCTRL)
# else /* CO_EVENT_PROFILE_ERRCTRL */
#  define CO_EVENT_ERRCTRL_CNT	(CO_EVENT_DYNAMIC_ERRCTRL)
# endif /* CO_EVENT_PROFILE_ERRCTRL */
#else /* CO_EVENT_DYNAMIC_ERRCTRL */
# ifdef CO_EVENT_PROFILE_ERRCTRL
#  define CO_EVENT_ERRCTRL_CNT	(CO_EVENT_PROFILE_ERRCTRL)
# endif /* CO_EVENT_PROFILE_ERRCTRL */
#endif /* CO_EVENT_DYNAMIC_ERRCTRL */


#ifdef CO_EVENT_DYNAMIC_MANAGER
# ifdef CO_EVENT_PROFILE_MANAGER
#  define CO_EVENT_MANAGER_CNT	(CO_EVENT_DYNAMIC_MANAGER + CO_EVENT_PROFILE_MANAGER)
# else /* CO_EVENT_PROFILE_MANAGER */
#  define CO_EVENT_MANAGER_CNT	(CO_EVENT_DYNAMIC_MANAGER)
# endif /* CO_EVENT_PROFILE_MANAGER */
#else /* CO_EVENT_DYNAMIC_MANAGER */
# ifdef CO_EVENT_PROFILE_MANAGER
#  define CO_EVENT_MANAGER_CNT	(CO_EVENT_PROFILE_MANAGER)
# endif /* CO_EVENT_PROFILE_MANAGER */
#endif /* CO_EVENT_DYNAMIC_MANAGER */


/* datatypes */

typedef enum {
	CO_NMT_MASTER_STATE_POWERON,
	CO_NMT_MASTER_STATE_DETECT,
	CO_NMT_MASTER_STATE_SLAVE,
	CO_NMT_MASTER_STATE_MASTER
} CO_NMT_MASTER_STATE_T;


/* object 0x1f80 */
#define CO_NMT_STARTUP_BIT_MASTER		0x00000001ul
#define CO_NMT_STARTUP_BIT_STARTNMT0	0x00000002ul
#define CO_NMT_STARTUP_BIT_STARTITSELF	0x00000004ul
#define CO_NMT_STARTUP_BIT_STARTNODE	0x00000008ul
#define CO_NMT_STARTUP_BIT_RESETNODES	0x00000010ul
#define CO_NMT_STARTUP_BIT_FLYMA		0x00000020ul
#define CO_NMT_STARTUP_BIT_STOPNODES	0x00000040ul

/* object 0x1f81 */
#define CO_NMT_SLAVE_ASSIGN_BIT_SLAVE		0x00000001ul
#define CO_NMT_SLAVE_ASSIGN_BIT_BOOT		0x00000004ul
#define CO_NMT_SLAVE_ASSIGN_BIT_MANDATORY	0x00000008ul
#define CO_NMT_SLAVE_ASSIGN_BIT_RESETCOMM	0x00000010ul
#define CO_NMT_SLAVE_ASSIGN_BIT_SWVERSION	0x00000020ul
#define CO_NMT_SLAVE_ASSIGN_BIT_RESTORE		0x00000080ul


#ifndef CO_NODE_ID
# define CO_NODE_ID		icoNmtPersistantNodeIdGet(CO_LINE)
#endif /* CO_NODE_ID */


/* function prototypes */

void	icoNmtMsgHandler(CO_LINE_DECL, CO_CONST CO_REC_DATA_T *pNmtRec);
void	icoNmtErrorCtrlHandler(CO_LINE_DECL, CO_CONST CO_REC_DATA_T *pRecData);
void	icoErrorCtrlReset(CO_LINE_DECL);
void	icoErrorCtrlSetDefaultValue(CO_LINE_DECL);
RET_T	icoHbProdStart(CO_LINE_DECL, BOOL_T send);
void	*icoErrCtrlGetObjectAddr(CO_LINE_DECL, UNSIGNED16 index, UNSIGNED8	subIndex);
RET_T	icoErrCtrlObjChanged(CO_LINE_DECL, UNSIGNED16 index, UNSIGNED8	subIndex);
RET_T	icoErrCtrlCheckObjLimit(CO_LINE_DECL, UNSIGNED8	subIndex,
			UNSIGNED32 newValue);
COB_REFERENZ_T icoNmtGetCob(CO_LINE_DECL);
void	icoNmtStateChange(CO_LINE_DECL, CO_NMT_STATE_T reqState, BOOL_T	cmdFromMaster);
RET_T	icoNmtStateReq(CO_LINE_DECL, UNSIGNED8 node, CO_NMT_STATE_T	reqState,
			BOOL_T master, UNSIGNED8 flags);
void	icoErrorBehavior(CO_LINE_DECL);
void	icoNmtResetNodeId(CO_LINE_DECL);
void	icoNmtVarInit(const UNSIGNED8 *pNodeId, const CO_NODE_ID_T *nodeIdFct);
void	icoErrCtrlInd(CO_LINE_DECL, UNSIGNED8 node, CO_ERRCTRL_T state,
			CO_NMT_STATE_T nmtState);

UNSIGNED8	icoNmtPersistantNodeIdGet(CO_LINE_DECL);
void	icoErrorCtrlVarInit(UNSIGNED8 *pList);
void	icoNmtSetDefaultValue(CO_LINE_DECL);
void	icoNmtApplyObj(CO_LINE_DECL);

void	icoNmtMasterVarInit(UNSIGNED8 *pSlaveAssign);
void	icoGuardingVarInit(UNSIGNED8 *pSlaveAssign);
void	icoManagerVarInit(UNSIGNED8 *pSlaveAssign);

void	icoFlymaVarInit(void);
void	icoFlymaSetDefaultValue(CO_LINE_DECL);
void	icoNmtMasterReset(CO_LINE_DECL);
void	*icoNmtGetObjectAddr(CO_LINE_DECL, UNSIGNED16 index, UNSIGNED8 subIndex);
RET_T	icoNmtObjChanged(CO_LINE_DECL, UNSIGNED16 index, UNSIGNED8 subIndex);

void	icoNmtFlymaReset(CO_LINE_DECL);
UNSIGNED32	icoNmtMasterGetStartupObj(CO_LINE_DECL);
RET_T	icoNmtFlymaInit(CO_LINE_DECL);
void	icoNmtMasterSetState(CO_LINE_DECL, CO_NMT_MASTER_STATE_T mState);
CO_NMT_MASTER_STATE_T icoNmtMasterGetState(CO_LINE_DECL);
void	icoNmtFlymaHandler(CO_LINE_DECL, CO_CONST CO_REC_DATA_T *pRecData);
void	icoNmtFlymaErrCtrlFailure(CO_LINE_DECL, UNSIGNED8 node);
void	*icoFlymaGetObjectAddr(CO_LINE_DECL, UNSIGNED16 index, UNSIGNED8 subIndex);

CO_NMT_STATE_T icoGuardGetRemoteNodeState(CO_LINE_DECL, UNSIGNED8 nodeId);
void	icoGuardingHandler(CO_LINE_DECL, CO_CONST CO_REC_DATA_T	*pRecData);

UNSIGNED32 icoNmtMasterGetSlaveAssignment(CO_LINE_DECL, UNSIGNED8 idx);
#endif /* ICO_NMT_H */


