/*******************************************************************************
 * (c) Copyright 2015 Microsemi Corporation.  All rights reserved.
 *
 *
 *
 * SVN $Revision: $
 * SVN $Date: $
 */

#ifndef RMEP_TYPES_H_
#define RMEP_TYPES_H_

#include <api_oam_sdk/common/types/inc/mr_api_types.h>
#include <api_oam_sdk/mep/src/mep_types.h>
#include <api_oam_sdk/rmep_db/inc/rmep_db.h>
#include "egl_types.h"
#include "egl_log.h"

#define	NULL_MEPID			0

//typedef int8_t	rMepNum_t;

typedef struct
{
	mepId_t		mepId;					// The MIB index for this MEP
	rMepNum_t	rMepNum;				// Hardware index associated with this MEP
	lMepNum_t	parentLMepNum;			// Hardware index associated with this MEPs parent Local MEP
	bool		inUse;					// Used to indicate that an entry is in use
} rMepCtxt_t;

typedef struct
{
	bool			rMepIsActive;		// A Boolean value stating if the remote MEP is active.
	time_t			failedOkTime;		// The time (SysUpTime) at which the IFF Remote MEP state machine last entered either the RMEP_FAILED or RMEP_OK state
	bool			linkIsUp;			// Indicates if the link is receiving good CCMs(=true). Updated by FNG task
	bool			rxRdiIsSet;			// Indicates if the RDI bit was set in a received CCM packet from this remote MEP
	psValue_t		lastRxPSValue;		// The last received Port Status TLV value, if any.
	isValue_t		lastRxISValue;		// The last received Interface Status TLV value, if any.
} rMepDBint_t;

struct rMepEntry_s
{
	TAILQ_ENTRY(struct rMepEntry_s,) 	ll;
	rMepDBint_t							rMepDB;
	rMepCtxt_t							rMepCtxt;
	bool								macValid;
};

typedef struct rMepEntry_s rMepEntry_t;

void setRMepDbLogLevel(log_level_t logLevel);

void rMepDBInit(void);

void rMepDBAdd(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId);

void rMepDBRemove(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId);

void rMepGetMAC(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId, bool *macValid, macAddress_t macAddress);

void rMepEnable(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId);

void rMepDisable(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId);

bool rMepIsActive(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId);

void rMepSetActive(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId);

void rMepClrActive(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId);

uint16_t rMepDBGetRMepCount(void);

extern rMepNum_t _rMepIdToRMepNum(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId);

extern rMepState_t readRMepState(rMepNum_t rMepNum, bool *rdi);

extern void _rMepSetLinkStatus(rMepNum_t rMepNum, bool linkIsUp);
extern void _rMepSetRxRdiStatus(rMepNum_t rMepNum, bool rxRdiIsSet);
extern void _rMepSetIsValue(rMepNum_t rMepNum, isValue_t newVlaue);
extern void _rMepSetPsValue(rMepNum_t rMepNum, psValue_t newVlaue);
extern void _rMepSetFailedOkTime(rMepNum_t rMepNum, time_t theTime);
extern lMepNum_t _rMepGetParentLMepNum(rMepNum_t rMepNum);

void _rMepGetIntInfo(mdIndex_t mdIndex, maIndex_t maIndex, mepId_t lMepId, mepId_t rMepId, rMepDBint_t *rMepDBint);
void _rMepGetParentsIndices(rMepNum_t rMepNum, mepParentIndices_t *mepParentIndices);
void _rMepSetMacValid(rMepNum_t rMepNum, bool valid);
bool _rMepNumIsValid(rMepNum_t rMepNum);

#endif /* RMEP_TYPES_H_ */
