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

#include "TransmitStateDataManage.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_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_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_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_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)
{
	UINT8 ii = 0u;
	UINT8 retVal = CI_ERROR;
	
	if ((CI_SYSTEM_TYPE_CI == 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_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;
	UINT32 badComCycNum = 0u;
	UINT32 dwRedBadComCycNum = 0U;
	UINT32 dwBlueBadComCycNum = 0U;
    UINT32 badEnsureCount = 0u;

    if ((0u == localCiId) || (CI_SUM_MAX < localCiId))
    {
        retVal = CI_ERROR;
    }
	else
	{
		if (CI_SYSTEM_TYPE_ZC == 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 = GetCiAndZcTransBadEnsureCnt();
					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_ATS == DeviceType) || (CI_SYSTEM_TYPE_XIANDI == 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;
					/*CI与ATS通信中断起始周期由于510公共数据结构中无此配置，先写死为20个周期*/
					badEnsureCount = 20U;
					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_CI == 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 = GetCiAndCiTransBadEnsureCnt();
					if ((badComCycNum > badEnsureCount) || (0u == TransmitStateDataStru[ii].RecvDataCycNum))
					{
                     	/*通信超时*/
						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_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)
		{
			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 =  GetCiAndAxleTransBadEnsureCnt();
					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_OC == 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 = GetCiAndOcTransmitBadEnsureCnt();
					if ((badComCycNum > badEnsureCount)||(0U == TransmitStateDataStru[ii].RecvDataCycNum))
					{
                    	/*通信超时*/
						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)&&
						(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)
		{
			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 = GetTiocAndTmcTransBadEnsureCnt();
					if ((badComCycNum > badEnsureCount) || (0u == TransmitStateDataStru[ii].RecvDataCycNum))
					{
						/*通信超时*/
						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
		{
			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 localCi = GetSystemParaLocalCiId();
	UINT32 curCycle = Get2oo2CurCycleNum();
	for (ii = 0u; ii < TransmitStateDataCurSum; ii++)
	{
		if ((TransmitStateDataStru[ii].chLocalCiId == localCi)
			&& (TransmitStateDataStru[ii].DeviceType == CI_SYSTEM_TYPE_CI)
			&& (TransmitStateDataStru[ii].DeviceId == otherCiId))
		{
			/*通信良好*/
			if (curCycle - TransmitStateDataStru[ii].RecvDataCycNum > GetCiAndCiTransBadEnsureCnt())
			{
				/*通信超时*/
				retVal = TRANSMIT_STATE_BAD;
			}
			else
			{
				/*通信良好*/
				retVal = TRANSMIT_STATE_GOOD;
			}	
			break;
		}
	}
	if (CI_ERROR == retVal)
	{/*不在通信列表*/
		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_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_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_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;
}