/********************************************************
*                                                                                                            
* 文 件 名： TransmitStateDataManage.c 
* 版权说明： 北京交控科技有限公司 
* 创建时间： 2012-06-07
* 作    者： 联锁组
* 功能描述： 通信状态数据管理文件  
* 函数包括：
* 使用注意：
* 修改记录：   
*
********************************************************/ 

#include "TransmitStateDataManage.h"
#include "../HLHTMsgHead/HLHTHeadContexManage.h"



TransmitRelationDataStruct TransmitRelaDataStru[TRANSMIT_OBJECT_SUM_MAX]; /*定义通信关系数据结构体*/
TransmitStateDataStruct TransmitStateDataStru[TRANSMIT_OBJECT_SUM_MAX * TRANSMIT_OBJECT_DEVICE_SUM_MAX];  /*定义通信状态结构体*/

UINT32 gRedTransmitObjRcvCyc[TRANSMIT_OBJECT_SUM_MAX * TRANSMIT_OBJECT_DEVICE_SUM_MAX] = { 0U };
UINT32 gBlueTransmitObjRcvCyc[TRANSMIT_OBJECT_SUM_MAX * TRANSMIT_OBJECT_DEVICE_SUM_MAX] = { 0U };


UINT16 TransmitRelaDataCurSum = 0u; /*通信关系配置数据当前总数*/
UINT16 TransmitStateDataCurSum = 0u; /*通信关系状态数据当前总数*/

/*联锁到ATS通信周期记录*/
UINT8 ComCycNumForCiToAts = 0u;/*初始化为0，仅该变量为0时发送数据，并置为COM_CYCNUM_CI_TO_ATS，不发送数据的周期内该值递减*/
/*联锁到现地通信周期记录*/
UINT8 ComCycNumForCiToXianDi = 0u;/*初始化为0，仅该变量为0时发送数据，并置为COM_CYCNUM_CI_TO_XIANDI，不发送数据的周期内该值递减*/

/*
* 功能描述： 获取指定CI到指定设备的设备总数
* 参数说明： const UINT8 systemType, 系统类型
* 返回值  ： 0: 获取数据失败
*			 >0: 获取数据成功     
*/
UINT8 GetCiTransmitSpecifySysDevSum(const UINT8 systemType)
{
    UINT8 localCiId = 0u; /*本地联锁ID*/
	UINT8 ii = 0u;
	UINT8 retVal = 0u;
	
	localCiId = GetSystemParaLocalCiId(); /*获取本机联锁ID*/
	
	if ((0u == localCiId) || (CI_SUM_MAX <=localCiId))
	{ /*本机联锁ID错误*/
	
	    retVal = 0u;
	}
	else
	{
		if ((CI_SYSTEM_TYPE_ATS != systemType) &&
			(CI_SYSTEM_TYPE_CI != systemType) &&
			(CI_SYSTEM_TYPE_RC != systemType) &&
			(CI_SYSTEM_TYPE_ZC != systemType) &&
			(CI_SYSTEM_TYPE_LEU != systemType) &&
			(CI_SYSTEM_TYPE_XIANDI != systemType) &&
			(CI_SYSTEM_TYPE_PLATFORM !=systemType) &&
			(CI_SYSTEM_TYPE_PSC != systemType) &&
			(CI_SYSTEM_TYPE_OC != systemType) &&
			(CI_SYSTEM_TYPE_AXIS != systemType)&&
			(CI_SYSTEM_TYPE_TMC != systemType))
		{ /*系统类型错误*/

			retVal = 0u;
		}
		else
		{
			for (ii = 0u ; ii < TransmitRelaDataCurSum; ii++)
			{
				if ((TransmitRelaDataStru[ii].CiId == localCiId) &&
					(TransmitRelaDataStru[ii].SystemType == systemType))
				{
					retVal = TransmitRelaDataStru[ii].DeviceSum;
					break;
				}
				else
				{
					;
				}
			}
		}
	}
	return retVal;
}


/*
* 功能描述： 获取指定CI到指定设备的设备ID组
* 参数说明： const UINT8 systemType, 系统类型
* 返回值  ： NULL: 获取数据失败
*			 !NULL: 获取数据成功     
*/
UINT8* GetCiTransmitSpecifySysDevBuf(const UINT8 systemType)
{
    UINT8 localCiId = 0u; /*本地联锁ID*/
	UINT8 ii = 0u;
	UINT8 * retVal = NULL;
	
	localCiId = GetSystemParaLocalCiId(); /*获取本机联锁ID*/
	
	if ((0u == localCiId) || (localCiId >= CI_SUM_MAX))
	{ /*本机联锁ID错误*/
	
	    retVal = NULL;
	}
	else
	{
		if ((CI_SYSTEM_TYPE_ATS != systemType) &&
			(CI_SYSTEM_TYPE_CI != systemType) &&
			(CI_SYSTEM_TYPE_RC != systemType) &&
			(CI_SYSTEM_TYPE_ZC != systemType) &&
			(CI_SYSTEM_TYPE_LEU != systemType) &&
			(CI_SYSTEM_TYPE_XIANDI != systemType) &&
			(CI_SYSTEM_TYPE_PLATFORM != systemType) &&
			(CI_SYSTEM_TYPE_PSC != systemType) &&
			(CI_SYSTEM_TYPE_OC != systemType) &&
			(CI_SYSTEM_TYPE_TMC != systemType) &&
			(CI_SYSTEM_TYPE_AXIS != systemType))
		{ /*系统类型错误*/

			retVal = NULL;
		}
		else
		{
			for (ii = 0u ; ii < TransmitRelaDataCurSum; ii++)
			{
				if ((TransmitRelaDataStru[ii].CiId == localCiId) &&
					(TransmitRelaDataStru[ii].SystemType == systemType))
				{
					retVal = TransmitRelaDataStru[ii].DeviceIdBuf;
					break;
				}
				else
				{
					;
				}
			}
		}
	}
	return retVal;
}


/*
* 功能描述： 获取指定CI到指定设备ID的接收数据周期号
* 参数说明： const UINT8 localCiId, 本机联锁站ID
*			 const UINT8 DeviceType, 设备类型
*			 const UINT8 DeviceId, 设备ID
* 返回值  ： 0: 获取数据失败
*			 >0: 接收数据周期号         
*/
UINT32 GetCiTransmitRecvDataCycNum(const UINT8 localCiId, const UINT8 DeviceType, const UINT8 DeviceId)
{
	UINT8 ii = 0u;
	UINT32 retVal = CI_ERROR;
	
	if ((localCiId <= 0u) || (localCiId > CI_SUM_MAX))
	{
		retVal = CI_ERROR;
	}
	else
	{
		if ((CI_SYSTEM_TYPE_ATS == DeviceType) ||
			(CI_SYSTEM_TYPE_ZC == DeviceType) ||
			(CI_SYSTEM_TYPE_XIANDI == DeviceType) ||
			(CI_SYSTEM_TYPE_TMC == DeviceType))
		{
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType))
				{
					retVal = TransmitStateDataStru[ii].RecvDataCycNum;
					break;
				}
			}
		}
		else if ((CI_SYSTEM_TYPE_CI == DeviceType) || (CI_SYSTEM_TYPE_RC == DeviceType) || (CI_SYSTEM_TYPE_PLATFORM == DeviceType)
			|| (CI_SYSTEM_TYPE_PSC == DeviceType) || (CI_SYSTEM_TYPE_LEU == DeviceType)
			 || (CI_SYSTEM_TYPE_AXIS == DeviceType) || (CI_SYSTEM_TYPE_OC == DeviceType))
		{
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
					retVal = TransmitStateDataStru[ii].RecvDataCycNum;
					break;
				}
			}
		}
		else
		{
			retVal = CI_ERROR;
		}
	}
	


	return retVal;

}

/*
* 功能描述： 设置指定CI到指定设备ID的接收数据周期号
* 参数说明： const UINT8 localCiId, 本机联锁站ID
*			 const UINT8 DeviceType, 设备类型
*			 const UINT8 DeviceId, 设备ID
*            const UINT32 recvDataCycNum, 接受数据周期
* 返回值  ： 0: 设置数据失败
*			 1: 设置数据成功        
*/
UINT8 SetCiTransmitRecvDataCycNum(const UINT8 localCiId, const UINT8 DeviceType, const UINT8 DeviceId, const UINT32 recvDataCycNum)
{
    UINT8 ii = 0u;
	UINT8 retVal = CI_ERROR;
    
    if ((localCiId <= 0u) || (localCiId > CI_SUM_MAX))
    {
        retVal = CI_ERROR;
    }
	else
	{
		if ((CI_SYSTEM_TYPE_ATS == DeviceType)
			|| (CI_SYSTEM_TYPE_ZC == DeviceType)		
			|| (CI_SYSTEM_TYPE_XIANDI == DeviceType)
			|| (CI_SYSTEM_TYPE_TMC == DeviceType)
			)
		{
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType))
				{
					TransmitStateDataStru[ii].RecvDataCycNum = recvDataCycNum;
					retVal = CI_SUCCESS;
					break;

				}

			}
		}
		else if ((CI_SYSTEM_TYPE_CI == DeviceType) || (CI_SYSTEM_TYPE_RC == DeviceType) || (CI_SYSTEM_TYPE_PLATFORM == DeviceType)||
			     (CI_SYSTEM_TYPE_PSC == DeviceType) || (CI_SYSTEM_TYPE_LEU == DeviceType) || 
				 (CI_SYSTEM_TYPE_OC == DeviceType) || (CI_SYSTEM_TYPE_AXIS == DeviceType)) 
		{
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
					TransmitStateDataStru[ii].RecvDataCycNum = recvDataCycNum;
					retVal = CI_SUCCESS;
					break;
				}
			}
		}
		else
		{
			retVal = CI_ERROR;
		}
	}
    

    
	return retVal;
}

/*
* 功能描述： 获取指定CI到指定设备ID的通信状态
* 参数说明： const UINT8 DeviceType, 设备类型
*			 const UINT8 DeviceId, 设备ID
* 返回值  ： 0: 获取数据失败
*			 >0: 通信状态         
*/
UINT8 GetCiTransmitState(const UINT8 DeviceType, const UINT8 DeviceId)
{
	UINT16 ii = 0u;
	UINT8 retVal = CI_ERROR;
	
	if ((CI_SYSTEM_TYPE_CI == DeviceType) || (CI_SYSTEM_TYPE_RC == DeviceType) || (CI_SYSTEM_TYPE_PLATFORM == DeviceType) || (CI_SYSTEM_TYPE_PSC == DeviceType)
		|| (CI_SYSTEM_TYPE_AXIS == DeviceType) || (CI_SYSTEM_TYPE_LEU == DeviceType) || (CI_SYSTEM_TYPE_OC == DeviceType)
		|| (CI_SYSTEM_TYPE_ATS == DeviceType) || (CI_SYSTEM_TYPE_XIANDI == DeviceType) || (CI_SYSTEM_TYPE_ZC == DeviceType) 
	    || (CI_SYSTEM_TYPE_IVOC == DeviceType) || (CI_SYSTEM_TYPE_TMC == DeviceType))
    {
        for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
        {
            if ((TransmitStateDataStru[ii].DeviceType == DeviceType)
                && (TransmitStateDataStru[ii].DeviceId == DeviceId))
			{
                retVal = TransmitStateDataStru[ii].transmitState;
				break;       
			}
			else
			{
				; /*继续查找*/
			}
        
		}
    }
	else
	{
		retVal = CI_ERROR;
	}

	return retVal;


}

/*
* 功能描述： 更新指定CI到指定设备ID的通信状态
* 参数说明： const UINT8 localCiId, 本机联锁站ID
*			 const UINT8 DeviceType, 设备类型
*			 const UINT8 DeviceId, 设备ID
* 返回值  ： 0: 更新失败
*			 1: 更新成功  
* 修改：     增加红蓝网状态更新
*/
UINT8 UpdateCiTransmitState(const UINT8 localCiId, const UINT8 DeviceType, const UINT8 DeviceId)
{
    UINT8 ii = 0u;
	UINT8 retVal = CI_ERROR;
	UINT8 getResult = CI_ERROR;
	UINT32 badComCycNum = 0u;
	UINT32 dwRedBadComCycNum = 0U;
	UINT32 dwBlueBadComCycNum = 0U;
	UINT32 badEnsureCount = 0u;
	UINT32 transmitBadEnsureTime = 0U;/*通信中断时间*/
	UINT32 dwCurCycNum = 0U;/*当前周期*/
	UINT32 srcRecvCiMsgSN = 0U;
	UINT32 srcCrntOwnMsgSN = 0U;
	UINT32 srcPreOwnMsgSN = 0U;
	UINT32 srcPeriod = 0U;
	UINT8 chMainWorckMode = GetLocalCiBackUpPattern();/*主备模式*/
	HLHTHeadContextStru* pstrHlhtHeadCtx = NULL;  /*互联互通报文头上下文数据*/
	INT32 InfoRemainValidiy = 0;/*通信包头可靠信校验*/
	INT32 CiRecordInfoRemainValidiy = 0;/*CI同步通信包头可靠信校验*/
	UINT32 dwUpToMainCyc = 0U;/*本机升主机周期号*/
	UINT8 chSystemType = GetSystemWorkType();
	dwCurCycNum = Get2oo2CurCycleNum();
	dwUpToMainCyc = GetSystemUpdateMainUseCycNum();
	if ((CI_SYSTEM_TYPE_ZC == DeviceType)
		|| (CI_SYSTEM_TYPE_ATS == DeviceType) || (CI_SYSTEM_TYPE_XIANDI == DeviceType)
		||(CI_SYSTEM_TYPE_CI == DeviceType) || (CI_SYSTEM_TYPE_RC == DeviceType)
		|| (CI_SYSTEM_TYPE_OC == DeviceType) 
		|| (CI_SYSTEM_TYPE_TMC == DeviceType)
		|| (CI_SYSTEM_TYPE_AXIS == DeviceType))
	{
		srcRecvCiMsgSN = GetHLHTHeadCtxSrcOppSN(DeviceType, DeviceId);
		srcCrntOwnMsgSN = GetHLHTHeadCtxSrcCrntSN(DeviceType, DeviceId);
		srcPreOwnMsgSN = GetHLHTHeadCtxSrcOwnSN(DeviceType, DeviceId);
		srcPeriod = GetHLHTHeadCtxComPeriod(DeviceType, DeviceId);
	}
	else
	{
		; /*暂不使用互联互通头进行通信校验*/
	}
    if ((0u == localCiId) || (CI_SUM_MAX < localCiId))
    {
        retVal = CI_ERROR;
    }
	else
	{
		if (CI_SYSTEM_TYPE_ZC == DeviceType)
		{
			transmitBadEnsureTime = GetCiAndZcTransmitBadEnsureTime();
			badEnsureCount = GetCiAndZcTransBadEnsureCnt();
			for(ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
					/*使用互联互通报文头判断有效性*/
					getResult = GetHLHTHeadCtxValid(CI_SYSTEM_TYPE_ZC, DeviceId);
					InfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
						GetSystemParaAppCycleTime(), srcRecvCiMsgSN, srcCrntOwnMsgSN, srcPreOwnMsgSN, srcPeriod);
					if ((CI_SUCCESS == getResult)&&(0 < InfoRemainValidiy))
					{
						/*通信良好*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
					}
					else
					{
						/*通信超时*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
					}

					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount)&&(dwBlueBadComCycNum <= badEnsureCount))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if ((dwRedBadComCycNum <= badEnsureCount) || (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;      
					break;
				}
			}
		}
		else if ((CI_SYSTEM_TYPE_ATS == DeviceType) || (CI_SYSTEM_TYPE_XIANDI == DeviceType))
		{
			transmitBadEnsureTime = GetCiAndAtsTransBadEnsureTime();
			badEnsureCount = GetCiAndAtsTransBadEnsureCnt();
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
				
					/*CI与ATS通信中断起始周期由于510公共数据结构中无此配置，先写死为40个周期-12000ms*/
					getResult = GetHLHTHeadCtxValid(DeviceType, DeviceId);
					InfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
						GetSystemParaAppCycleTime(), srcRecvCiMsgSN, srcCrntOwnMsgSN, srcPreOwnMsgSN, srcPeriod);
					if ((CI_SUCCESS == getResult) && (0 < InfoRemainValidiy))
					{
						/*通信良好*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
					}
					else
					{
						/*通信超时*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
					}

					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount) && (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if ((dwRedBadComCycNum <= badEnsureCount) || (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;
					break;
				}
			}
		}
		else if ((CI_SYSTEM_TYPE_CI == DeviceType) || (CI_SYSTEM_TYPE_RC == DeviceType))
		{
			transmitBadEnsureTime = GetCiAndCiTransmitBadEnsureTime();
			badEnsureCount = GetCiAndCiTransBadEnsureCnt();
			for (ii = 0U; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
					getResult = GetHLHTHeadCtxValid(DeviceType, DeviceId);
					InfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
						GetSystemParaAppCycleTime(), srcRecvCiMsgSN, srcCrntOwnMsgSN, srcPreOwnMsgSN, srcPeriod);
					if ((SYSTEM_WORK_MODE_ALL == chSystemType) && (RC_WORK_MODE_TIOC == CI_SYSTEM_TYPE_RCI) && (SYSTEM_WORK_MODE_MAIN == chMainWorckMode))
					{
						/*RC模式且双系统中主用模式*/
						if (CI_SYSTEM_TYPE_CI == DeviceType)
						{
							pstrHlhtHeadCtx = GetCiToRcWithCiHeadCtx(DeviceId);
						}
						else
						{
							pstrHlhtHeadCtx = GetCiToRcWithRcHeadCtx(DeviceId);
						}
						if (NULL != pstrHlhtHeadCtx)
						{
							CiRecordInfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
								GetSystemParaAppCycleTime(), pstrHlhtHeadCtx->sourceRecvOppMsgSN, pstrHlhtHeadCtx->sourceCrntOwnMsgSN, pstrHlhtHeadCtx->sourcePreOwnMsgSN, pstrHlhtHeadCtx->sourceComPeriod);
							if (((0 < InfoRemainValidiy) && ((srcRecvCiMsgSN >= dwUpToMainCyc) || (pstrHlhtHeadCtx->sourceCrntOwnMsgSN < srcCrntOwnMsgSN)))
								|| (0 < CiRecordInfoRemainValidiy))
							{
								/*与对应联锁报文有效  且在以下两个条件中满足一个
								1.RC接收相邻RC/CI发送的“对方消息序列号”大于等于当前RC升主周期号
								2.RC接收相邻RC/CI发送的“本方消息序列号”大于CI同步的CI与RC/CI GAL包中的“本方消息序列号*/
								TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
							}
							else
							{
								if (TRANSMIT_STATE_GOOD == TransmitStateDataStru[ii].transmitState)
								{
									TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
									debug_infor_printf((const)"RC%x%d Transmist Check RecvCiMsgSN:%d_CIRecordSN:%d,LocalSN:%d\n",
										DeviceType, DeviceId, srcRecvCiMsgSN, pstrHlhtHeadCtx->sourceCrntOwnMsgSN, srcCrntOwnMsgSN);
								}
							}
						}
						else
						{
							TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
						}
					}
					else				
					{
						/*本联锁为CI/备用RC/单系统运行模式*/
						if ((CI_SUCCESS == getResult) && (0 < InfoRemainValidiy))
						{
							/*通信良好*/
							TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
						}
						else
						{
							/*通信超时*/
							TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
						}
					}
					
					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount) && (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if ((dwRedBadComCycNum <= badEnsureCount) || (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;  
					break;
				}
			}
		}
		else if(CI_SYSTEM_TYPE_PLATFORM == DeviceType)
		{
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
                    /*通信良好*/
                    badComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataCycNum;
					badEnsureCount = GetCiAndPsdTransBadEnsureCnt(); 
					if (badComCycNum > badEnsureCount)
					{
                     	/*通信超时*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
					}
					else
					{
                       	/*通信良好*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
					}	

					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount) && (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if ((dwRedBadComCycNum <= badEnsureCount) || (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;  
					break;
				}
			}
		}
		else if(CI_SYSTEM_TYPE_PSC == DeviceType)
		{
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
                	/*通信良好*/
					badComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataCycNum; 
					badEnsureCount = GetCiAndPscTransBadEnsureCnt();
					if (badComCycNum > badEnsureCount)
					{
                     	/*通信超时*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
					}
					else
					{
	                   /*通信良好*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
					}

					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount) && (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if ((dwRedBadComCycNum <= badEnsureCount) || (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;  
					break;
				}
			}
		}
		else if (CI_SYSTEM_TYPE_AXIS == DeviceType)
		{
			badEnsureCount = GetCiAndAxleTransBadEnsureCnt();
			transmitBadEnsureTime = GetCiAndAxleTransBadEnsureTime();
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
                   	/*通信良好*/
				
					/*使用互联互通报文头判断有效性*/
					getResult = GetHLHTHeadCtxValid(CI_SYSTEM_TYPE_AXIS, DeviceId);
					InfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
						GetSystemParaAppCycleTime(), srcRecvCiMsgSN, srcCrntOwnMsgSN, srcPreOwnMsgSN, srcPeriod);
					if ((CI_SUCCESS == getResult) && (0 < InfoRemainValidiy))
					{
						/*通信良好*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
					}
					else
					{
						/*通信超时*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
					}

					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount) && (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if ((dwRedBadComCycNum <= badEnsureCount) || (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;
					break;
				}
			}
		}
		else if (CI_SYSTEM_TYPE_OC == DeviceType)
		{
			badEnsureCount = GetCiAndOcTransmitBadEnsureCnt();
			transmitBadEnsureTime = GetCiAndOcTransmitBadEnsureTime();
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
					/*使用互联互通报文头判断有效性*/
					getResult = GetHLHTHeadCtxValid(CI_SYSTEM_TYPE_OC, DeviceId);
					InfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
						GetSystemParaAppCycleTime(), srcRecvCiMsgSN, srcCrntOwnMsgSN, srcPreOwnMsgSN, srcPeriod);
					if (RC_WORK_MODE_CI == CI_SYSTEM_TYPE_RCI)
					{
						if ((CI_SUCCESS == getResult) &&( 0< InfoRemainValidiy))
						{
							/*通信良好*/
							TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
						}
						else
						{
							TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
						}
					}
					else
					{
						/*RC模式*/
						pstrHlhtHeadCtx = GetCiToRcHLHTOcHeadCtx(DeviceId);
						if(NULL != pstrHlhtHeadCtx)
						{ 
							CiRecordInfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
								GetSystemParaAppCycleTime(), pstrHlhtHeadCtx->sourceRecvOppMsgSN, pstrHlhtHeadCtx->sourceCrntOwnMsgSN, pstrHlhtHeadCtx->sourcePreOwnMsgSN, pstrHlhtHeadCtx->sourceComPeriod);
						}
						else
						{
							CiRecordInfoRemainValidiy = 0;
						}
						if (SYSTEM_WORK_MODE_MAIN == chMainWorckMode)
						{
							/*RC主用，通信良好条件为:*/
							/*1.RC直接接收OC数据报文有效，且（(大于等于升主周期号）或（比同步到的CI数据更新)）*/
							/*2.或  同步的主机CI数据仍然有效*/
							if (((0 < InfoRemainValidiy) && ((srcRecvCiMsgSN >= dwUpToMainCyc) || (pstrHlhtHeadCtx->sourceCrntOwnMsgSN < srcCrntOwnMsgSN)))/*RC-OC链路有效且最新*/
								|| (0 < CiRecordInfoRemainValidiy))/*CI同步依旧有效*/
							{
								TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
							}
							else
							{
								debug_infor_printf((const)"CiRecordInfoRemainValidiy: %d srcRecvCiMsgSN =%d sourceCrntOwnMsgSN=%d \n", CiRecordInfoRemainValidiy, srcRecvCiMsgSN, srcCrntOwnMsgSN);/*联锁数据有效性*/
								TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
							}						
						}
						else
						{
							/*RC备用 完全依赖CI接收数据的有效性*/							
							if (0 < CiRecordInfoRemainValidiy)
							{
								/*通信良好*/
								TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
							}
							else
							{
								TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
							}
						}
					}

					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount) && (dwBlueBadComCycNum <= badEnsureCount)&&
						(0U != TransmitStateDataStru[ii].RecvDataRedCycNum) && (0U !=TransmitStateDataStru[ii].RecvDataBlueCycNum))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if (((dwRedBadComCycNum <= badEnsureCount)&&(0U != TransmitStateDataStru[ii].RecvDataRedCycNum))
						|| (dwBlueBadComCycNum <= badEnsureCount)&& (0U != TransmitStateDataStru[ii].RecvDataBlueCycNum))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;
					break;
				}
			}
		}
		else if(CI_SYSTEM_TYPE_LEU == DeviceType)
		{
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
                   	/*通信良好，配置文件中无LEU通信中断时间配置，暂时使用与ZC通信中断配置*/
					badComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataCycNum;
					badEnsureCount = COM_ERROR_LEU_MAXCYCLENUM;
					if (badComCycNum > badEnsureCount)
					{
						/*通信超时*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
					}
					else
					{
						/*通信良好*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
					}	

					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount) && (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if ((dwRedBadComCycNum <= badEnsureCount) || (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;  
					break;
				}
			}

		}	
		else if (CI_SYSTEM_TYPE_TMC == DeviceType)
		{
			transmitBadEnsureTime = GetTiocAndTmcTransBadEnsureTime();
			badEnsureCount = GetTiocAndTmcTransBadEnsureCnt();
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
					/*使用互联互通报文头判断有效性*/
					getResult = GetHLHTHeadCtxValid(CI_SYSTEM_TYPE_TMC, DeviceId);
					InfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
						GetSystemParaAppCycleTime(), srcRecvCiMsgSN, srcCrntOwnMsgSN, srcPreOwnMsgSN, srcPeriod);
					if ((CI_SUCCESS == getResult) && (0 < InfoRemainValidiy))
					{
						/*通信良好*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_GOOD;
					}
					else
					{
						/*通信超时*/
						TransmitStateDataStru[ii].transmitState = TRANSMIT_STATE_BAD;
					}

					TransmitStateDataStru[ii].RecvDataRedCycNum = gRedTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataRedCycNum : gRedTransmitObjRcvCyc[ii];
					TransmitStateDataStru[ii].RecvDataBlueCycNum = gBlueTransmitObjRcvCyc[ii] == 0U ? TransmitStateDataStru[ii].RecvDataBlueCycNum : gBlueTransmitObjRcvCyc[ii];
					dwRedBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataRedCycNum;
					dwBlueBadComCycNum = Get2oo2CurCycleNum() - TransmitStateDataStru[ii].RecvDataBlueCycNum;
					if ((dwRedBadComCycNum <= badEnsureCount) && (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*冗余双网正常*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_GOOD;
					}
					else if ((dwRedBadComCycNum <= badEnsureCount) || (dwBlueBadComCycNum <= badEnsureCount))
					{
						/*单网通信良好*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_LINK;
					}
					else
					{
						/*通信故障*/
						TransmitStateDataStru[ii].redundantTransmitState = TRANSMIT_2OO2_STATE_BAD;
					}
					retVal = CI_SUCCESS;
					break;
				}
			}

		}
		else
		{
			retVal = CI_ERROR;
		}
	}
    

    
	return retVal;

}

/*
* 功能描述： 获取联锁到其他通信周期记录
* 参数说明： const UINT8 DeviceType, 设备类型
* 返回值  ： 通信记录值 
*/
UINT8 GetComCycNumForCiToOtherSys(const UINT8 DeviceType)
{
    UINT8 comCycNum = 0u;
    
    if (CI_SYSTEM_TYPE_ATS == DeviceType)
    {
        comCycNum = ComCycNumForCiToAts;
    }
    else if (CI_SYSTEM_TYPE_XIANDI == DeviceType)
    {
        comCycNum = ComCycNumForCiToXianDi;
    }
    else
    {
        /*不做处理*/
    }

    return comCycNum;
}

/*
* 功能描述： 设置联锁到其他通信周期记录
* 参数说明： const UINT8 DeviceType, 设备类型
*            const UINT8 ComCycNumForCiToOtherSys, 通信周期记录
* 返回值  ： 通信记录值 
*/
void SetComCycNumForCiToOtherSys(const UINT8 DeviceType, const UINT8 ComCycNum)
{
    
    if (CI_SYSTEM_TYPE_ATS == DeviceType)
    {
        ComCycNumForCiToAts = ComCycNum;
    }
    else if (CI_SYSTEM_TYPE_XIANDI == DeviceType)
    {
        ComCycNumForCiToXianDi = ComCycNum;
    }
    else
    {
        /*不做处理*/
    }
}

/*
* 功能描述： 获取本联锁与其它联锁通信状态
* 参数说明： const UINT8 otherCiId, 其它联锁ID
* 返回值  ：TRANSMIT_STATE_GOOD通信正常
			TRANSMIT_STATE_BAD通信故障
			NOT_IN_TRAINSMIT_LIST（不在通信列表中） 
*/
UINT8 GetLocalCiAndOtherCiTransSta(const UINT8 otherCiId)
{
	UINT8 ii = 0u;
	UINT8 retVal = CI_ERROR;
	UINT8 chTransState = 0U;
	UINT8 chSystemType = GetSystemWorkType();
	UINT8 chMainMode = 0U;/*主用模式*/
	if (SYSTEM_WORK_MODE_ALL == chSystemType)
	{
		/*获取主用模式*/
		chMainMode = GetNearRCISendMainUseMode(otherCiId);
		chTransState = GetCiWithOtherCiTransmitState(chMainMode, otherCiId);
	}
	else
	{
		chTransState = GetCiTransmitState(CI_SYSTEM_TYPE_RCI, otherCiId);
	}

	if ((TRANSMIT_STATE_GOOD == chTransState)||(TRANSMIT_STATE_BAD == chTransState))
	{
		retVal = chTransState;
	}
	else
	{
		retVal = NOT_IN_TRAINSMIT_LIST;
	}
	return retVal;
}


/*
* @func： 获取指定Tioc通信ATS网关设备类型ID
* @param： void
* @return  ： 0: 获取数据失败
*			 >0: 设备ID
*/
UINT16 GetTiocTrnsmtGtwyDevTypeId(void)
{
	UINT8 ii = 0u;
	UINT16 retVal = CI_ERROR;
	const UINT8 chLocalTiocId = GetSystemParaLocalCiId();
	
	for (ii = 0u; ii < TransmitRelaDataCurSum; ii++)
	{
		if ((TransmitRelaDataStru[ii].CiId == chLocalTiocId)
			&&(CI_SYSTEM_TYPE_ATS==TransmitRelaDataStru[ii].SystemType))
		{
			retVal = TransmitRelaDataStru[ii].wGatewayComputerId;
			break;
		}
	}
	
	return retVal;
}

/*
* 功能描述： 设置指定CI到指定设备ID的接收数据周期号
* 参数说明： const UINT8 DeviceType, 设备类型
*			 const UINT8 DeviceId, 设备ID
*			 const UINT8 chNetType ,   网络类型
* 返回值  ： 0: 设置数据失败
*			 1: 设置数据成功
*/
UINT8 SetCiTrnsmitCycNumByNetType(const UINT8 DeviceType, const UINT8 DeviceId, const UINT8 chNetType)
{
	UINT8 ii = 0u;
	UINT8 retVal = CI_ERROR;
	UINT8 localCiId = 0U;
	UINT32 recvDataCycNum = 0U;
	recvDataCycNum = Get2oo2CurCycleNum();
	localCiId = GetSystemParaLocalCiId();
	if ((localCiId <= 0u) || (localCiId > CI_SUM_MAX))
	{
		retVal = CI_ERROR;
	}else if((TRANSMIT_NET_ATC_RED !=chNetType)&&(TRANSMIT_NET_ATC_BLUE!= chNetType)) 
	{
		retVal = CI_ERROR;
	}
	else
	{
		if ((CI_SYSTEM_TYPE_CI == DeviceType) || (CI_SYSTEM_TYPE_RC == DeviceType) || (CI_SYSTEM_TYPE_PLATFORM == DeviceType) ||
			(CI_SYSTEM_TYPE_PSC == DeviceType) || (CI_SYSTEM_TYPE_LEU == DeviceType) ||
			(CI_SYSTEM_TYPE_OC == DeviceType) || (CI_SYSTEM_TYPE_AXIS == DeviceType) ||
			(CI_SYSTEM_TYPE_ATS == DeviceType) || (CI_SYSTEM_TYPE_ZC == DeviceType) || 
			(CI_SYSTEM_TYPE_XIANDI == DeviceType) || (CI_SYSTEM_TYPE_TMC == DeviceType))
		{
			for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
			{
				if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
					&& (TransmitStateDataStru[ii].DeviceType == DeviceType)
					&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
				{
					if (TRANSMIT_NET_ATC_RED == chNetType)
					{
						gRedTransmitObjRcvCyc[ii] = recvDataCycNum;
					}
					else
					{
						gBlueTransmitObjRcvCyc[ii] = recvDataCycNum;
					}
					retVal = CI_SUCCESS;
					break;
				}
			}
		}
		else
		{
			retVal = CI_ERROR;
		}
	}



	return retVal;
}

/*
* 功能描述： 获取CI与其他设备通信状态（从2002组件挪过来）
* 参数说明： const UINT8 systemType, 系统类型
*            const UINT8 index, 状态数组下标
* 返回值  ： 通信状态
*/
UINT8 Get2oo2CiAndOtherSysCommState(const UINT8 DeviceType,const UINT8 chIndex)
{
	UINT16 ii = 0U;
	UINT16 jj = 0U;
	UINT8 retVal = CI_ERROR;
	UINT8 DeviceId = 0U;

	for (ii = 0u; ii < TRANSMIT_OBJECT_SUM_MAX; ii++)
	{
		if (TransmitRelaDataStru[ii].SystemType == DeviceType)
		{
			if (TRANSMIT_OBJECT_DEVICE_SUM_MAX > chIndex)
			{
				DeviceId = TransmitRelaDataStru[ii].DeviceIdBuf[chIndex];
				if ((CI_SYSTEM_TYPE_CI == DeviceType) || (CI_SYSTEM_TYPE_RC == DeviceType) || (CI_SYSTEM_TYPE_PLATFORM == DeviceType) || (CI_SYSTEM_TYPE_PSC == DeviceType)
					|| (CI_SYSTEM_TYPE_AXIS == DeviceType) || (CI_SYSTEM_TYPE_LEU == DeviceType) || (CI_SYSTEM_TYPE_OC == DeviceType)
					|| (CI_SYSTEM_TYPE_ATS == DeviceType) || (CI_SYSTEM_TYPE_XIANDI == DeviceType) || (CI_SYSTEM_TYPE_ZC == DeviceType) 
					|| (CI_SYSTEM_TYPE_VOBC == DeviceType) || (CI_SYSTEM_TYPE_IVOC == DeviceType) || (CI_SYSTEM_TYPE_TMC == DeviceType))
				{
					for (jj = 0u; jj < TransmitStateDataCurSum; jj++)
					{
						if ((TransmitStateDataStru[jj].DeviceType == DeviceType)
							&& (TransmitStateDataStru[jj].DeviceId == DeviceId))
						{
							retVal = TransmitStateDataStru[jj].redundantTransmitState;
							break;

						}
						else
						{
							; /*继续执行*/
						}

					}
				}
				else
				{
					retVal = CI_ERROR;
				}
			}
			else
			{
				; /*不处理*/
			}
			break;
		}
		else
		{
			; /*继续执行*/
		}
	}
#ifdef WIN32
	return (TRANSMIT_STATE_GOOD ==GetCiTransmitState(DeviceType, DeviceId)?TRANSMIT_2OO2_STATE_GOOD: TRANSMIT_2OO2_STATE_BAD);
#else
	return retVal;
#endif
}


/*
* @func： 获取指定Tioc通信ATS接口机设备ID
* @param： void
* @return  ： 0: 获取数据失败
*			 >0: 设备ID
*/
UINT16 GetATSInterfaceTypeId(void)
{
	UINT8 ii = 0u;
	UINT16 retVal = CI_ERROR;
	const UINT8 chLocalTiocId = GetSystemParaLocalCiId();

	for (ii = 0u; ii < TransmitRelaDataCurSum; ii++)
	{
		if ((TransmitRelaDataStru[ii].CiId == chLocalTiocId)
			&& (CI_SYSTEM_TYPE_ATS_INTERFACE == TransmitRelaDataStru[ii].SystemType))
		{
			retVal = (UINT16)((UINT16)CI_SYSTEM_TYPE_ATS_INTERFACE<<8)+ TransmitRelaDataStru[ii].DeviceIdBuf[0];
			break;
		}
	}

	return retVal;
}

/*
* 功能描述： 获取指定到指定Rc ID的通信状态 仅按照报文头判断 不考虑同步
* 参数说明： const UINT8 DeviceId, 设备ID
* 返回值  ： 0: 获取数据失败
*			 >0: 通信状态
*/
UINT8 GetRcPbTacsTransmitState(const UINT8 DeviceId)
{
	UINT32 transmitBadEnsureTime = 0U;
	UINT32 badEnsureCount = 0U;
	UINT16 ii = 0U;
	UINT8 localCiId = 0u;/*本地联锁ID*/
	UINT8 getResult = CI_ERROR;
	INT32 InfoRemainValidiy = 0;/*通信包头可靠信校验*/
	UINT32 srcRecvCiMsgSN = 0U;
	UINT32 srcCrntOwnMsgSN = 0U;
	UINT32 srcPreOwnMsgSN = 0U;
	UINT32 srcPeriod = 0U;
	UINT32 dwCurCycNum = 0U;
	UINT8 chRetval = TRANSMIT_STATE_BAD;
	localCiId = GetSystemParaLocalCiId();
	transmitBadEnsureTime = GetCiAndCiTransmitBadEnsureTime();
	badEnsureCount = GetCiAndCiTransBadEnsureCnt();
	dwCurCycNum = Get2oo2CurCycleNum();
	if (CI_SYSTEM_TYPE_RC == CI_SYSTEM_TYPE_RCI)
	{
		for (ii = 0U; ii < TransmitStateDataCurSum; ii++)
		{
			if ((TransmitStateDataStru[ii].chLocalCiId == localCiId)
				&& (TransmitStateDataStru[ii].DeviceType == CI_SYSTEM_TYPE_RC)
				&& (TransmitStateDataStru[ii].DeviceId == DeviceId))
			{
				srcRecvCiMsgSN = GetHLHTHeadCtxSrcOppSN(CI_SYSTEM_TYPE_RC, DeviceId);
				srcCrntOwnMsgSN = GetHLHTHeadCtxSrcCrntSN(CI_SYSTEM_TYPE_RC, DeviceId);
				srcPreOwnMsgSN = GetHLHTHeadCtxSrcOwnSN(CI_SYSTEM_TYPE_RC, DeviceId);
				srcPeriod = GetHLHTHeadCtxComPeriod(CI_SYSTEM_TYPE_RC, DeviceId);
				getResult = GetHLHTHeadCtxValid(CI_SYSTEM_TYPE_RC, DeviceId);
				InfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
					GetSystemParaAppCycleTime(), srcRecvCiMsgSN, srcCrntOwnMsgSN, srcPreOwnMsgSN, srcPeriod);
				if ((CI_SUCCESS == getResult) && (0 < InfoRemainValidiy))
				{
					/*通信良好*/
					chRetval = TRANSMIT_STATE_GOOD;
				}
				else
				{
					/*通信超时*/
					chRetval = TRANSMIT_STATE_BAD;
				}
			}
		}
	}
	else
	{
		chRetval = TRANSMIT_STATE_BAD;
	}
	return chRetval;
}

/*
* 功能描述： 获取本控区的主用模式与相邻CI的报文有效性
* 参数说明： const UINT8 DeviceType, 设备类型
*			 const UINT8 DeviceId, 设备ID
* 返回值  ： 0: 获取数据失败
*			 >0: 通信状态    
*/
UINT8 GetCiWithOtherCiTransmitState(const UINT8 DeviceType, const UINT8 DeviceId)
{
	UINT8 chRetval = CI_ERROR;
	HLHTHeadContextStru* pstrHlhtHeadCtx = NULL;  /*CI同步的互联互通报文头上下文数据*/
	INT32 InfoRemainValidiy = 0;/*通信报文校验*/
	UINT32 transmitBadEnsureTime = 0U;/*中断时间*/
	UINT32 dwCurCycNum = 0U;/*当前周期*/
	UINT8 cControlState = GetLocalCiBackUpPattern();
	transmitBadEnsureTime = GetCiAndCiTransmitBadEnsureTime();
	dwCurCycNum = Get2oo2CurCycleNum();
	if (SYSTEM_WORK_MODE_MAIN == cControlState)
	{
		chRetval = GetCiTransmitState(DeviceType, DeviceId);
	}
	else
	{
		/*备用模式获取本控区CI同步数据*/
		if (CI_SYSTEM_TYPE_CI == DeviceType)
		{
			pstrHlhtHeadCtx = GetCiToRcWithCiHeadCtx(DeviceId);
		}
		else if (CI_SYSTEM_TYPE_RC == DeviceType)
		{
			pstrHlhtHeadCtx = GetCiToRcWithRcHeadCtx(DeviceId);
		}
		else
		{
			pstrHlhtHeadCtx = NULL;
		}
		if (NULL != pstrHlhtHeadCtx)
		{
			InfoRemainValidiy = HLHTCommonMsgRemainingValidity(transmitBadEnsureTime, dwCurCycNum,
				GetSystemParaAppCycleTime(), pstrHlhtHeadCtx->sourceRecvOppMsgSN, pstrHlhtHeadCtx->sourceCrntOwnMsgSN, pstrHlhtHeadCtx->sourcePreOwnMsgSN, pstrHlhtHeadCtx->sourceComPeriod);
			if (0 < InfoRemainValidiy)
			{
				chRetval = TRANSMIT_STATE_GOOD;
			}
			else
			{
				chRetval = TRANSMIT_STATE_BAD;
			}
		}
	}
	return chRetval;
}