#include "RRMDataPrepare.h"
#include "LinkConfigDataManage.h"
#include "SystemParameterManage.h"
#include "AxisSectionConfigDataManage.h"
#include "WLBrakeDataManage.h"
#include "EnvelopeDefine.h"
#include "ELMDataPrepare.h"
#include "EnvelopeManageAPI.h"
#include "SignalDataManage.h"
#include "dsuStruct.h"
#include "ESBDataManage.h"
#include "PSDDataManage.h"
#include "EvacuationAreaManage.h"
#include <stdarg.h>
#include "DeviceConfigDataManage.h"

/**
 * @brief 打印Debug信息
 * @param[in] UINT8 level 日志级别
 * @param[in] const char *fmt 打印信息
 * @return 无
 * @note 通过调用外部接口，实现本模块的打印
 */
void rrm_PrintDebugInfo(UINT8 level,const char *fmt, ...)
{
	va_list para;
	CHAR tmpPrintBuf[RRM_PRINT_LEN] = {'\0'};

	if (RRM_LOG_LEVEL >= level)
	{
		va_start(para,fmt);
		(void)vsnprintf(tmpPrintBuf,RRM_PRINT_LEN,fmt,para);
		va_end(para);

		debug_infor_printf((const)"%s",tmpPrintBuf);
	}
	else
	{
		/** 日志级别不够*/
	}	
}


/**
* @brief 获取RRM功能开关
* @param[in] 无
* @param[out] 无
* @return CI_SUCCESS 功能打开
          CI_ERROR 功能关闭
* @note 无
*/
UINT8 rrm_PrepareRRMSwitch(void)
{
	UINT8 ret = CI_ERROR;
	UINT8 funRtn = GetCiFunctionSwitchConfig(FUNC_RRM_SWITCH);

	ASSERT_SETRET(FUNC_SWITCH_ON == funRtn,ret,CI_SUCCESS,CI_ERROR);
	return ret;
}

/**
* @brief 获取出站过程中支持远程RM功能开关
* @param[in] 无
* @param[out] 无
* @return CI_SUCCESS 功能打开
          CI_ERROR 功能关闭
* @note 无
*/
UINT8 rrm_PrepareRRMStaSwitch(void)
{
	UINT8 ret = CI_ERROR;
	UINT8 funRtn = GetCiFunctionSwitchConfig(FUNC_DEPART_RRM_SWITCH);

	ASSERT_SETRET(FUNC_SWITCH_ON == funRtn,ret,CI_SUCCESS,CI_ERROR);
	return ret;
}


/**
 * @brief 获取列车RRM申请状态
 * @param[in] UINT16 trainID 列车ID
 * @param[out] 无
 * @return 无
 * @note 无
 */
UINT8 rrm_PrepareRRMRequest(UINT16 trainID)
{
	UINT8 ret = CI_ERROR;
	UINT8 funRtn = TRAIN_RRM_RQST_NO;
	funRtn = GetTrainRrmRqst(trainID);

	if (TRAIN_RRM_RQST_YES == funRtn)
	{
		ret = CI_SUCCESS;
	}
	else
	{
		/* 列车未申请RRM */
	}
	return ret;
}

/**
 * @brief 获取列车下一站台
 * @param[in] UINT16 trainID 列车ID
 * @param[out] 无
 * @return UINT16 
 * @note 无
 */
UINT16 rrm_PrepareNextStaOfTrain(UINT16 trainID)
{
	return GetTrainAtsLgcStation(trainID);
}

/**
 * @brief 获取列车关联的包络 
 * @param[in] 列车ID
 * @param[out] 无
 * @return NULL 存在UCT包络或者查询失败；非NULL，查询正确
 * @note by cgh 20240403
 */
const EnvelopeStruct* rrm_PrepareTrainRelELM(UINT16 trainID)
{
	const EnvelopeStruct* pEnvelop = NULL;
	UINT8 ElmCnt = 0u;
	UINT16 envelopeId[TRAIN_REL_ENVELOPE_SUM_MAX] = { 0u };

	ElmCnt = elm_GetTrainRelELM(trainID, envelopeId);
	pEnvelop = elm_GetEnvelopeInfoByID(envelopeId[0u]);

	if ((1u == ElmCnt) && (NULL != pEnvelop) &&
		((TYPE_UCT_ENVELOPE == pEnvelop->envelopeType) || (TYPE_CT_ENVELOPE == pEnvelop->envelopeType)))
	{
		/** 查询正确*/
	}
	else
	{
		pEnvelop = NULL;
	}
	return pEnvelop;
}


/**
 * @brief 获取列车所在区域是否为正线
 * @param[in] const EnvelopeStruct* pTrainELM
 * @param[out] 无
 * @return UINT8
           CI_SUCCESS 属于正线
           CI_ERROR 不属于正线
 * @note 无
 */
UINT8 rrm_PrepareTrainAreaIsZX(IN const EnvelopeStruct* pTrainELM)
{
	UINT8 ret = CI_ERROR;
	UINT8 chTiocType = GetLocalCiType();
	UINT8 chLocalId = GetSystemParaLocalCiId();
	UINT16 headOCId = 0u;
	UINT16 tailOCId = 0u;

	/** 入口参数校验 */
	ASSERT_RET_VALUE((NULL == pTrainELM) || (0u == pTrainELM->ACCnt),ret,0x80000000u);

	headOCId = (UINT8)GetAxisBelongTioc(pTrainELM->ACID[pTrainELM->ACCnt - 1u]);
	tailOCId = (UINT8)GetAxisBelongTioc(pTrainELM->ACID[0u]);
	
	if ((headOCId == tailOCId) && (headOCId == chLocalId) && ((CI_TYPE_ZX == chTiocType) || (CI_TYPE_SCX == chTiocType)))
	{
		ret = CI_SUCCESS;
	}
	else
	{
		ret = CI_ERROR;
	}

	return ret;
}


/**
 * @brief 获取计轴区段关联的ATS逻辑站台ID
 * @param[in] UINT16 ACID
 * @param[out] 无
 * @return UINT16
           0:计轴区段不为站台
		   0xffffu:查询失败
		   其他：查询正确
 * @note 无
 */
UINT16 rrm_PrepareAtsLgcStaOfAC(UINT16 ACID)
{
	UINT16 ret = 0xffffu;
	UINT8 tmpLinkcnt = 0u;
	const UINT16 *pLinkIdBuf = NULL;
	UINT16 startLinkofSta = 0u;
	UINT16 endLinkofSta = 0u;


	/*- 获取计轴包含的link信息 */
	tmpLinkcnt = GetAxisLinkSum(ACID);
	pLinkIdBuf = GetAxisLinkIdBuf(ACID);

	if ((0u == tmpLinkcnt) || (NULL == pLinkIdBuf))
	{
		/** 查询失败 */
	}
	else
	{
		startLinkofSta = GetLinkPlatfrm(pLinkIdBuf[0u]);
		endLinkofSta = GetLinkPlatfrm(pLinkIdBuf[tmpLinkcnt - 1u]);

		if (startLinkofSta == endLinkofSta)
		{
			ret = startLinkofSta;
		}
		else
		{
			/** 查询失败 */
		}
	}

	return ret;
}


/**
 * @brief 获取计轴区段是否为折返区域
 * @param[in] UINT16 ACID
 * @param[out]无
 * @return UINT8
           CI_SUCCESS 是折返区域
           CI_ERROR 不为折返区域
 * @note 
 */
 UINT8 rrm_PrepareACisRevertArea(UINT16 ACID)
 {
	UINT8 ret = CI_ERROR;
	UINT8 tmpLinkcnt = 0u;
	const UINT16 *pLinkIdBuf = NULL;
	UINT8 cycle = 0u;
	UINT8 isRevertArea = LINK_REVERT_AREA_NO;

	/*- 获取计轴包含的link信息 */
	tmpLinkcnt = GetAxisLinkSum(ACID);
	pLinkIdBuf = GetAxisLinkIdBuf(ACID);

	if ((0u == tmpLinkcnt) || (NULL == pLinkIdBuf))
	{
		/** 查询失败 */
	}
	else
	{
		for (cycle = 0u; cycle < tmpLinkcnt;cycle++)
		{
			isRevertArea = IsLinkRevertArea(pLinkIdBuf[cycle]);

			if (CI_SUCCESS != isRevertArea)
			{
				break;
			}
		}

		if (cycle == tmpLinkcnt)
		{/** 是折返轨 */
			ret = CI_SUCCESS;
		}
		else
		{
			ret = CI_ERROR;
		}
	}
	return ret;
 }


 /**
 * @brief 计轴区段防护锁闭条件检查
 * @param[in] const UINT16 wCurACID 当前计轴区段ID
 * @param[in] const UINT16 wNextACID 下一个计轴区段ID
 * @param[out]无
 * @return UINT8
		   CI_SUCCESS 检查通过
		   CI_ERROR 检查不通过
		   CI_TRUE 无保护区段
 * @note by cgh 20240425
 */
 UINT8 rrm_PrepareACisOverlapLock(const UINT16 wCurACID, const UINT16 wNextACID)
 {
	 UINT8 chReVal = CI_ERROR;
	 UINT16 wCurPhySecId = 0U;/*当前物理区段ID*/
	 UINT16 wNextPhySecId = 0U;/*下一个物理区段ID*/
	 UINT16 wCurPhySecLockRouteId = 0U;/*当前物理区段锁闭所属进路*/
	 UINT16 wNextPhySecLockRouteId = 0U;/*下一个物理区段锁闭所属进路*/
	 UINT8 chOverlopExistCheck = CI_ERROR;/*保护区段存在检查*/
	 UINT16 wPhySecPLockRouteId = 0U;/*物理区段防护锁闭所属进路*/
	 UINT16 chOverlopId = 0U;/*保护区段ID*/

	 if ((0U < wCurACID) && (AXIS_SECTION_SUM_MAX > wCurACID) && (0U < wNextACID) && (AXIS_SECTION_SUM_MAX > wNextACID))
	 {
		 wCurPhySecId = GetAxisSecRelativePhySecId(wCurACID);
		 wNextPhySecId = GetAxisSecRelativePhySecId(wNextACID);

		 if ((0U < wCurPhySecId) && (PHYSICAL_SECTION_SUM_MAX > wCurPhySecId) && (0U < wNextPhySecId) && (PHYSICAL_SECTION_SUM_MAX > wNextPhySecId))
		 {
			 wCurPhySecLockRouteId = GetPhySecRLockBelongRouteId(wCurPhySecId);
			 wNextPhySecLockRouteId = GetPhySecRLockBelongRouteId(wNextPhySecId);

			 if ((wCurPhySecLockRouteId == wNextPhySecLockRouteId) && (0U != wCurPhySecLockRouteId))
			 {
				 /*站台区段和站台区段锁闭所属进路一致（反向进路无出站信号机场景，此时锁闭即可）*/
				 chReVal = CI_SUCCESS;
			 }
			 else
			 {
				 chOverlopExistCheck = rrm_OverlapExistCheck(wCurACID);

				 if (CI_SUCCESS == chOverlopExistCheck)
				 {
					 chReVal = CI_TRUE; /*没有保护区段，此步成功*/
				 }
				 else
				 {
					 wPhySecPLockRouteId = GetPhySecPLockBelongRouteId(wNextPhySecId);
					 if(wCurPhySecLockRouteId == wPhySecPLockRouteId)
					 {
						 chOverlopId = GetRouteOverlapLockedId(wPhySecPLockRouteId);
						 chReVal = OverlapUsableCheck(chOverlopId);
					 }
					 else
					 {
						 chReVal = CI_ERROR; /*站台锁闭所属进路和站外区段防护锁闭不一致，返回失败*/
					 }	 
				 }
			 }
		 }
		 else
		 {
			 ; /*非法*/
		 }
	 }
	 else
	 {
		 ; /*非法*/
	 }

	 return chReVal;
 }

/**
 * @brief 获取列车运行方向及激活端
 * @param[in] UINT16 trainID 列车ID
 * @param[out] UINT8 *pDir 列车方向
 * @param[out] UINT8 *pACT 列车激活端
 * @return 无
 * @note 无
 */
void rrm_PrepareTrainDirAndAct(UINT16 trainID,OUT UINT8 *pDir,OUT UINT8 *pACT)
{
	/** 入口参数校验 */
	ASSERT_RET_NO_VALUE(NULL == pDir,0x80000000u);
	ASSERT_RET_NO_VALUE(NULL == pACT,0x40000000u);

	*pDir = GetTrainDir(trainID);
	*pACT = GetTrainActive(trainID);
}


/**
 * @brief 获取全线紧急状态
 * @param[in] 无
 * @param[out] 无
 * @return UINT8
           CI_SUCCESS 已设置全线紧急状态
           CI_ERROR 未设置全线紧急状态
 * @note 
 */
UINT8 rrm_PrepareWLBrake(void)
{
	UINT8 ret = CI_ERROR;
	UINT8 funRtn = WlbGetState();

	/** 设置返回值 */
	ASSERT_SETRET(WLB_SET == funRtn,ret,CI_SUCCESS,CI_ERROR);
	return ret;
}

/**
 * @brief 获取本车车头占压的区段前方是否存在其他车
 * @param[in] const EnvelopeStruct* pTemp 包络信息
 * @param[out] 无
 * @return UINT8
           CI_SUCCESS 前方不存在其他车
           CI_ERROR 前方存在其他车
 * @note 无
 */
UINT8 rrm_PrepareForwardTrain(IN const EnvelopeStruct* pTemp)
{
	UINT8 ret = CI_ERROR;
	UINT8 cycle = 0u;
	UINT8 funRtn = 0u;
	UINT8 elmCnt = 0u;
	UINT16 envelopeIDs[SECTION_REL_ENVELOPE_SUM_MAX] = {0u};
	const EnvelopeStruct* pTempInfo = NULL;

	/** 入口参数校验 */
	ASSERT_RET_VALUE((NULL == pTemp) || (0u == pTemp->ACCnt),ret,0x80000000u);

	/** 获取计轴上的包络信息 */
	elmCnt = elm_GetEnvelopeIDsInAC(pTemp->ACID[pTemp->ACCnt - 1u],envelopeIDs);

	if (SECTION_REL_ENVELOPE_SUM_MAX <= elmCnt)
	{
		/** 查询错误*/
	}
	else
	{
		for (cycle = 0u; cycle < elmCnt; cycle++)
		{
			pTempInfo = elm_GetEnvelopeInfoByID(envelopeIDs[cycle]);

			if (NULL == pTempInfo)
			{
				/** 查询错误*/
				break;
			}
			else
			{
				if (pTempInfo->id == pTemp->id)
				{
					/** 本包络，跳过 */
				}
				else
				{
					/** 比较包络的前后关系 */
					funRtn = elm_PrepareRelationBtwEnvelope(pTemp,pTemp->startPos,pTempInfo);

					if (0x01U == funRtn)
					{
						/** 包络A在包络B前,继续遍历 */
					}
					else
					{
						/** 前方存在包络，跳出循环 */
						break;
					}
				}
			}
		}

		if (cycle == elmCnt)
		{
			/** 前方不存在其他车 */
			ret = CI_SUCCESS;
		}
		else
		{
			ret = CI_ERROR;
		}
	}
	return ret;
}


/**
 * @brief 获取本车车尾后方相邻计轴区段是否存在本车
 * @param[in] UINT16 trainID 列车ID
 * @param[in] const EnvelopeStruct* pTemp 列车包络信息
 * @param[out] 无
 * @return UINT8
           CI_SUCCESS 后方不存在本车
           CI_ERROR 后方存在本车
 * @note 
 */
UINT8 rrm_PrepareBackHaveSameTrain(UINT16 trainID,IN const EnvelopeStruct* pTemp)
{
	UINT8 ret = CI_ERROR;
	UINT32 errorCode = 0u;
	UINT8 localOCId = GetSystemParaLocalCiId();
	UINT8 cycle = 0u;
	UINT8 cycleElm = 0u;
	UINT8 cycleTrain = 0u;
	UINT8 acCnt = 0u;
	UINT16 arACID[AC_MAXCNT_IN_SWITCH] = {0u};
	UINT8 nextOCId = 0u;
	UINT8 ACIsOwn = 0u;
	UINT8 elmCnt = 0u;
	UINT8 trainIsOwn = 0u;
	UINT16 tailLinkID = 0u;
	UINT16 tailBelongOCId = 0u;
	UINT16 envelopeIDs[SECTION_REL_ENVELOPE_SUM_MAX] = {0u};
	const EnvelopeStruct* pTempInfo = NULL;
	UINT8 cDirectionPointFlag = 0U;			/*是否具有方向变化点标志*/

	/** 入口参数校验 */
	ASSERT_RET_VALUE((NULL == pTemp) || (0u == pTemp->ACCnt),ret,0x80000000u);

	/** 查询车尾后方计轴 */
	acCnt = elm_PrepareAdjacentAC(pTemp->ACID[0u], ((UINT8)~(pTemp->startPos.Dir)), arACID, &cDirectionPointFlag);

	if (UINT8_MAX == acCnt)
	{
		/** 查询失败，不会报 */
		errorCode += 0x40000000u;
	}
	else
	{	
		/** 查看通信车安全车尾是否属于本OC */
		if (TYPE_CT_ENVELOPE == pTemp->envelopeType)
		{
			/** 获取列车安全车尾 */
			tailLinkID = GetTrainSafeTailLink(trainID);

			/** 获取车尾所属OC */
			tailBelongOCId = (UINT8)GetLinkManageTiocById(tailLinkID);

			if (tailBelongOCId == localOCId)
			{
				trainIsOwn = ELM_BELONG_OWNER;
			}
			else
			{
				trainIsOwn = ELM_BELONG_OTHER;
			}
		}
		else
		{
			/** 非CT包络，不处理 */
		}


		for (cycle = 0u; cycle < acCnt;cycle++)
		{
			/** 获取计轴是否属于本集中区计轴 */
			ACIsOwn = rrm_PrepareACIsSelf(arACID[cycle],&nextOCId);

			/** 获取计轴上的包络信息 */
			elmCnt = elm_GetEnvelopeIDsInAC(arACID[cycle],envelopeIDs);

			/** 判断是否满足退出条件 */
			ASSERT_BREAK(SECTION_REL_ENVELOPE_SUM_MAX <= elmCnt,errorCode,0x20000000u);

			if ((0u == elmCnt) || (ELM_BELONG_OWNER == ACIsOwn))
			{
				/** 后方计轴不存在包络或者属于本OC，继续遍历 */
			}
			else
			{
				for (cycleElm = 0u; cycleElm < elmCnt; cycleElm++)
				{
					pTempInfo = elm_GetEnvelopeInfoByID(envelopeIDs[cycleElm]);

					/** 判断是否满足退出条件 */
					ASSERT_BREAK((NULL == pTempInfo) || (0u == pTempInfo->trianCnt),errorCode,0x10000000u);

					/** 后方包络存在本车，本TIOC不汇报 */
					for (cycleTrain = 0u; cycleTrain < pTempInfo->trianCnt;cycleTrain++)
					{
						if (((ELM_BELONG_OWNER != ACIsOwn) && (TYPE_UKT_ENVELOPE == pTempInfo->envelopeType) && (TYPE_UCT_ENVELOPE == pTemp->envelopeType))
							|| ((ELM_BELONG_OTHER == trainIsOwn) && (TYPE_UKT_ENVELOPE == pTempInfo->envelopeType) && (TYPE_CT_ENVELOPE == pTemp->envelopeType))
							 ||(pTempInfo->trainID[cycleTrain] == trainID))
						{
							/** 如果UCT后方计轴为非本集中区且为UT占压，或者跨分界点后方计轴为非本集中区且为UT占压，或者后方包络存在本车 */
							errorCode += 0x08000000u;
							break;
						}
						else
						{
							/** 继续遍历 */
						}
					}

					/** 后方包络存在本车，跳出循环 */
					ASSERT_BREAK(cycleTrain != pTempInfo->trianCnt,errorCode,0x04000000u);
				}

				/** 查询失败或者后方包络存在本车，本TIOC不汇报 */
				ASSERT_BREAK(cycleElm != elmCnt,errorCode,0x02000000u);
			}
		}

		if (cycle == acCnt)
		{
			/** 后方不存在本车 */
			ret = CI_SUCCESS;
		}
		else
		{
			/** 查询失败或者后方存在本车 */
			ret = CI_ERROR;
			ASSER_DEBUG(0u != errorCode,RRM_LOG(RRM_LOG_ERROR,"errno:0x%08x info:Train%d back has sameTrain.",errorCode,trainID));
		}
	}

	return ret;
}

/**
 * @brief 获取车尾包含的逻辑区段
 * @param[in] UINT16 trainID 列车ID
 * @param[in] const EnvelopeStruct* pTemp 包络信息
 * @param[out] 无
 * @return UINT16
           查询失败：0
		   查询成功：车尾逻辑区段
 * @note 
 */
UINT16 rrm_PrepareSecIDofTail(UINT16 trainID,IN const EnvelopeStruct* pTemp)
{
	UINT16 ret = 0u;
	UINT32 errorCode = 0u;
	UINT8 funRtn = CI_ERROR;
	UINT8 logSectionSum = 0;
	UINT16 *logIdBuf = NULL;

	/** 入口参数校验 */
	ASSERT_RET_VALUE((NULL == pTemp) || (0u == pTemp->ACCnt),ret,0x80000000u);

	/** 查询车尾后方计轴是否存在本车 */
	funRtn = rrm_PrepareBackHaveSameTrain(trainID,pTemp);

	if (CI_SUCCESS == funRtn)
	{
		/*获取逻辑区段信息*/
		logSectionSum = GetAxisSecLogSectionSum(pTemp->ACID[0u]);
		logIdBuf = GetAxisSecLogSectionIdBuf(pTemp->ACID[0u]);

		if ((0u == logSectionSum) || (NULL == logIdBuf))
		{
			/** 查询错误 */
			errorCode += 0x40000000u;
		}
		else
		{
			if (ELM_DIR_SAME == pTemp->startPos.Dir)
			{
				ret = logIdBuf[0u];
			}
			else
			{
				ret = logIdBuf[logSectionSum - 1u];
			}
		}
	}
	else
	{
		/** 打印维护信息 */
		ASSER_DEBUG(0u != errorCode,RRM_LOG(RRM_LOG_ERROR,"errno:0x%08x info:Train%d not report to ATS.",errorCode,trainID));
	}

	return ret;
}

 /**
 * @brief 查询计轴前/后方计轴
 * @param[in] UINT16 ACID 基准计轴ID
 * @param[in] UINT8 dir 查询方向
 * @param[out] UINT16 arACID[AC_MAXCNT_IN_SWITCH] 相邻计轴ID
 * @return UINT8_MAX 查询失败
           0~AC_MAXCNT_IN_SWITCH  查询成功
 * @note 无
 */
UINT8 rrm_PrepareAdjacentAC(UINT16 ACID, UINT8 dir, INOUT UINT16 arACID[AC_MAXCNT_IN_SWITCH], OUT UINT8* directionPointFlag)
{
	UINT8 cDirectionPointFlag = 0U;			/*是否具有方向变化点标志*/
	UINT8 retVal = 0U;

	retVal = elm_PrepareAdjacentAC(ACID, dir, arACID, &cDirectionPointFlag);

	*directionPointFlag = cDirectionPointFlag;
	return  retVal;
}

/**
  * @brief 获取计轴区段是否属于本OC 
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] UINT8 *pOCId 所属OCid
  * @return ELM_BELONG_OTHER 属于相邻OC
            ELM_BELONG_OWNER 属于本OC
			其他，非法
  * @note 无
  */
UINT8 rrm_PrepareACIsSelf(UINT16 acID,UINT8 *pOCId)
{
	return elm_PrepareACIsSelf(acID,pOCId);
}


/**
  * @brief 获取计轴关联的已办进路
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] 无
  * @return UINT16
			0，计轴未关联已办理进路
			其他，计轴关联的已办理进路ID
  * @note 无
  */ 
UINT16 rrm_PrepareAcofRoute(UINT16 acID)
{
	return GetAxisSecLockBelongRoute(acID);
}


  /**
  * @brief 获取进路的始端信号机开放状态
  * @param[in] UINT16 routeID 进路ID
  * @param[out] 无
  * @return UINT8
           CI_SUCCESS 信号机开放
           CI_ERROR 信号机关闭
  * @note 
  */
UINT8 rrm_PrepareOriginSigStatOfRoute(UINT16 routeID)
{
	UINT8 ret = CI_ERROR;
	UINT16 sigId = 0u;
	UINT8 sigColor = 0u;
	UINT8 sigStatus = SIGNAL_CLOSE;

	sigId = GetRouteStartSignalId(routeID);

	if (0u == sigId)
	{
		/** 查询失败*/
	}
	else
	{
		/** 获取信号状态 */
		sigColor = GetSignalCurColor(sigId);
		sigStatus = GetSignalOpenCloseState(sigColor);

		/** 设置函数返回值 */
		ASSERT_SETRET(SIGNAL_OPEN == sigStatus,ret,CI_SUCCESS,CI_ERROR);
	}
	return ret;
}

/**
 * @brief 通过计轴ID获取计轴状态
 * @param[in] UINT16 ACID 计轴区段ID
 * @return ELM_AC_OCC 计轴占用
		   ELM_AC_FREE 计轴空闲
 * @note 
 */
UINT8 rrm_PrepareACStatus(UINT16 ACID)
{
	return elm_PrepareACStatus(ACID);
}


/**
 * @brief 获取计轴区段的锁闭状态和锁闭方向
 * @param[in] UINT16 ACID 计轴区段ID
 * @param[in] UINT16 flag 区段类型
 * @param[out] UINT8* pLockStat 计轴锁闭状态（）
 * @param[out] UINT8* pLockDir 计轴锁闭方向
 * @return 无
 * @note 无
 */
void rrm_PrepareACLockStatAndDir(UINT16 ACID,UINT8 flag,OUT UINT8* pLockStat,OUT UINT8* pLockDir)
{
	UINT16 phyID = 0U;
	UINT16 RLockBelongRouteId = 0U;
	UINT16 PLockBelongRouteId = 0U;
	UINT8 LockStatus = PHYSICAL_SECTION_LOCK_NO;
	UINT8 tmpDir = 0U;
	UINT8 LockDir = 0U;
	UINT8 chPhySecLockShouldOppsed = ROUTE_PHYSEC_SHOULD_NOT_OPPOSE_LOCK;/*物理区段锁闭方向是否应该取反*/

	/** 入口参数校验 */
	ASSERT_RET_NO_VALUE(NULL == pLockStat,0x80000000u);
	ASSERT_RET_NO_VALUE(NULL == pLockDir,0x40000000u);

	/** 通过计轴区段获取获取区段ID */
	phyID = GetAxisSecRelativePhySecId(ACID);

	if (TYPE_OF_PATH_C == flag)
	{
		RLockBelongRouteId = GetPhySecRLockBelongRouteId(phyID);
		PLockBelongRouteId = GetPhySecPLockBelongRouteId(phyID);
	}
	else
	{
		RLockBelongRouteId = GetPhySecRLockBelongRouteId(phyID);
	}

	//获取锁闭状态
	LockStatus = GetPhySecLockState(phyID);
	/** 获取锁闭方向 */
	tmpDir = GetPhySecLockDirection(phyID);

	if (0U < RLockBelongRouteId)
	{
		chPhySecLockShouldOppsed = GetRoutePhySecShouldLockOppose(RLockBelongRouteId, phyID);
		if (ROUTE_PHYSEC_SHOULD_OPPOSE_LOCK == chPhySecLockShouldOppsed)
		{
			tmpDir = ~tmpDir;
		}
	}
	LockDir = ConvertDirectionToLogic(tmpDir);

	if (((0u == RLockBelongRouteId) && (0u == PLockBelongRouteId)) || (DIR_INVALID == LockDir) 
		|| ((PHYSICAL_SECTION_LOCK_NORMAL != LockStatus) && (PHYSICAL_SECTION_LOCK_PROTECT != LockStatus)
			&& (PHYSICAL_SECTION_LOCK_DELAY != LockStatus)))
	{
		(*pLockStat) = SEC_LOCK_ERROR;
		(*pLockDir) = 0u;
	}
	else
	{
		(*pLockStat) = SEC_LOCK_OK;
		(*pLockDir) = LockDir;
	}
}

/**
  * @brief 获取计轴区段封锁状态和封锁原因
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] UINT8 *pFlockStatus 封锁状态
  * @param[out] UINT8 *pFlockType 封锁原因
  * @return 无
  * @note 
  */ 
void rrm_PrepareACFLockStatusAndType(UINT16 acID,OUT UINT8 *pFlockStatus,OUT UINT8 *pFlockType)
{
	UINT16 phyID = 0u;

	/** 入口参数校验 */
	ASSERT_RET_NO_VALUE(NULL == pFlockStatus,0x80000000u);
	ASSERT_RET_NO_VALUE(NULL == pFlockType,0x40000000u);

	/** 通过计轴区段获取获取区段ID */
	phyID = GetAxisSecRelativePhySecId(acID);

	/** 获取封锁状态和封锁类型 */
	(*pFlockStatus) = GetPhySecFLockState(phyID);
	(*pFlockType) = GetPhySecFLockType(phyID);
}


 /**
  * @brief 获取计轴关联的道岔位置是否正确
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] 无
  * @return UINT8
           CI_SUCCESS 道岔位置正确
           CI_ERROR 道岔位置不正确
  * @note 无
  */
UINT8 rrm_PrepareSwitchStatOfAC(UINT16 acID)
{
	UINT8 ret = CI_ERROR;
	UINT8 cycle = 0u;
	UINT8 tmpSwPos = SWITCH_POSITION_INVALID;
	UINT8 tmpSwCntInAC = 0u;
	const AxisSwitchStruct * pSwitchInfoBuf = NULL;

	/** 获取计轴上的道岔信息*/
	tmpSwCntInAC = GetAxisSwitchSum(acID);
	pSwitchInfoBuf = GetAxisSwitchIdStru(acID);

	if (NULL == pSwitchInfoBuf)
	{
		/** 查询错误 */
	}
	else
	{
		for (cycle = 0u; cycle < tmpSwCntInAC; cycle++)
		{
			tmpSwPos = GetSwitchCurPosition(pSwitchInfoBuf[cycle].SwitchId);

			if (tmpSwPos == pSwitchInfoBuf[cycle].SwitchPosition)
			{
				/** 道岔位置正确*/
			}
			else
			{
				/** 道岔位置不正确 */
				break;
			}
		}
		/** 设置函数返回值 */
		ASSERT_SETRET(cycle == tmpSwCntInAC,ret,CI_SUCCESS,CI_ERROR);
	}
	return ret;
}

/**
  * @brief 获取计轴上关联的ESB是否按下
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] 无
  * @return UINT8
           CI_SUCCESS ESB未按下
           CI_ERROR ESB按下
  * @note 无
  */
 UINT8 rrm_PrepareESBStatOfAC(UINT16 acID)
 {
	 UINT8 ret = CI_ERROR;
	 UINT8 cycle = 0u;
	 UINT8 cycleESB = 0u;
	 UINT8 chLinkNum  = 0u;
	 UINT8 ESBStatus = ESB_STATE_VALID_YES;
	 const UINT16 *pLinkIdBuf = NULL;
	 const DSU_DEVICE_IN_LINK_STRU* pTempDeviceStru = NULL;

	 chLinkNum = GetAxisLinkSum(acID);
	 pLinkIdBuf = GetAxisLinkIdBuf(acID);

	 if ((0u == chLinkNum) || (NULL == pLinkIdBuf))
	 {
		 /** 查询失败*/
	 }
	 else
	 {
		 for (cycle = 0u; cycle < chLinkNum;cycle++)
		 {
			 /** 入口数据是否正确*/
			 if ((NULL == g_dsuEmapExtendStru) || (NULL == g_dsuEmapExtendStru->pDsuDeviceInLinkIdx) || (NULL == g_dsuEmapStru)
				 || (pLinkIdBuf[cycle] > g_dsuEmapStru->dsuEmapIndexStru->LINKINDEXNUM) || (0xFFFFU == g_dsuEmapStru->dsuEmapIndexStru->dsuLinkIndex[pLinkIdBuf[cycle]]))
			 {
				 /** 函数入口参数异常，查询失败*/
				 break;
			 }
			 else
			 {
				 /** 找到Link和其他设备间的映射关系信息缓存地址*/
				 pTempDeviceStru = g_dsuEmapExtendStru->pDsuDeviceInLinkIdx + pLinkIdBuf[cycle];

				 if (NULL != pTempDeviceStru)
				 {
					 /*- 遍历Link上所有ESB */
					 for (cycleESB = 0u;cycleESB < pTempDeviceStru->struESBDev.wDevCount;cycleESB++)
					 {
						 ESBStatus = GetEsbState(pTempDeviceStru->struESBDev.wDevSetId[cycleESB]);

						 if (ESB_STATE_VALID_NO == ESBStatus)
						 {
							 /** ESB未按下，继续遍历*/
						 }
						 else
						 {
							 break;
						 }
					 }

					 if (cycleESB != pTempDeviceStru->struESBDev.wDevCount)
					 {
						 break;
					 }
					 else
					 {
						 /** 继续遍历 */
					 }
				 }
				 else
				 {
					 /** 查询失败*/
					 break;
				 }
			 }
		 }

		 /** 设置函数返回值 */
		 ASSERT_SETRET(cycle == chLinkNum,ret,CI_SUCCESS,CI_ERROR);
	 }

	 return ret;
 }


  /**
  * @brief 获取计轴上关联的PSD是否按下
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] 无
  * @return UINT8
           CI_SUCCESS PSD未打开
           CI_ERROR PSD打开
  * @note 无
  */
 UINT8 rrm_PreparePSDStatOfAC(UINT16 acID)
 {
	 UINT8 ret = CI_ERROR;
	 UINT8 cycle = 0u;
	 UINT8 cyclePSD = 0u;
	 UINT8 chLinkNum  = 0u;
	 UINT8 PSDStatus = PSD_STATE_OPEN;
	 const UINT16 *pLinkIdBuf = NULL;
	 const DSU_DEVICE_IN_LINK_STRU* pTempDeviceStru = NULL;

	 chLinkNum = GetAxisLinkSum(acID);
	 pLinkIdBuf = GetAxisLinkIdBuf(acID);

	 if ((0u == chLinkNum) || (NULL == pLinkIdBuf))
	 {
		 /** 查询失败*/
	 }
	 else
	 {
		 for (cycle = 0u; cycle < chLinkNum;cycle++)
		 {
			 /** 入口数据是否正确*/
			 if ((NULL == g_dsuEmapExtendStru) || (NULL == g_dsuEmapExtendStru->pDsuDeviceInLinkIdx) || (NULL == g_dsuEmapStru)
				 || (pLinkIdBuf[cycle] > g_dsuEmapStru->dsuEmapIndexStru->LINKINDEXNUM) || (0xFFFFU == g_dsuEmapStru->dsuEmapIndexStru->dsuLinkIndex[pLinkIdBuf[cycle]]))
			 {
				 /** 函数入口参数异常，查询失败*/
				 break;
			 }
			 else
			 {
				 /** 找到Link和其他设备间的映射关系信息缓存地址*/
				 pTempDeviceStru = g_dsuEmapExtendStru->pDsuDeviceInLinkIdx + pLinkIdBuf[cycle];

				 if (NULL != pTempDeviceStru)
				 {
					 /*- 遍历Link上所有PSD */
					 for (cyclePSD = 0u;cyclePSD < pTempDeviceStru->struScreenDev.wDevCount;cyclePSD++)
					 {
						 PSDStatus = GetPsdState(pTempDeviceStru->struScreenDev.wDevSetId[cyclePSD]);

						 if (PSD_STATE_CLOSE == PSDStatus)
						 {
							 /** 屏蔽门关闭，继续遍历*/
						 }
						 else
						 {
							 break;
						 }
					 }

					 if (cyclePSD != pTempDeviceStru->struScreenDev.wDevCount)
					 {
						 break;
					 }
					 else
					 {
						 /** 继续遍历 */
					 }
				 }
				 else
				 {
					 /** 查询失败*/
					 break;
				 }
			 }
		 }

		 /** 设置函数返回值 */
		 ASSERT_SETRET(cycle == chLinkNum,ret,CI_SUCCESS,CI_ERROR);
	 }

	 return ret;
 }


 /**
  * @brief 获取区段是否进行了安全防护（是否存在安全防护区域）
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] 无
  * @return UINT8
           CI_SUCCESS 区段未进行安全防护
           CI_ERROR 区段进行了安全防护
  * @note 无
  */
 UINT8 rrm_PrepareSafeProtectOfAC(UINT16 acID)
 {
	 UINT8 ret = CI_ERROR;
	 UINT8 funRtn = CI_ERROR;
	 UINT8 cycle = 0u;
	 UINT8 chLgcNum = 0u;
	 const UINT16 *pLgcIdBuf = NULL;

	 chLgcNum = GetAxisSecLogSectionSum(acID);
	 pLgcIdBuf = GetAxisSecLogSectionIdBuf(acID);

	 if ((0u == chLgcNum) || (NULL == pLgcIdBuf))
	 {
		 /** 查询失败 */
	 }
	 else
	 {
		 for (cycle = 0u;cycle < chLgcNum; cycle++)
		 {
			 funRtn = IsLgcInEa(pLgcIdBuf[cycle]);
			 if (CI_SUCCESS == funRtn)
			 {
				 /** 不存在防护区域*/
			 }
			 else
			 {
				 /** 存在防护区域 */
				 break;
			 }
		 }

		 /** 设置函数返回值 */
		 ASSERT_SETRET(cycle == chLgcNum,ret,CI_SUCCESS,CI_ERROR);
	 }

	 return ret;
 }


  /**
  * @brief 获取区段是否存在侵限或者侧防风险
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out]无
  * @return UINT8
           CI_SUCCESS 区段未进行安全防护
           CI_ERROR 区段进行了安全防护
  * @note 无
  */
 UINT8 rrm_PrepareVainAndLtrlDfncCheck(UINT16 acID)
 {
	 UINT8 ret = CI_ERROR;
	 UINT8 cycle = 0u;
	 UINT8 VainCheckFlag = CI_ERROR;
	 UINT8 LtrlDfncCheckFlag = CI_ERROR;
	 UINT8 chLogSecCnt = 0u;
	 const UINT16* pLogSecCntBuff = NULL;
	 UINT16 DevId = 0u;
	 UINT8 DevState = 0u;

	 /** 获取计轴关联的逻辑区段ID */
	 chLogSecCnt = GetAxisSecLogSectionSum(acID);
	 pLogSecCntBuff = GetAxisSecLogSectionIdBuf(acID);

	 if ((0u == chLogSecCnt) || (NULL == pLogSecCntBuff))
	 {
		 /** 查询错误 */
	 }
	 else
	 {
		 for (cycle = 0u; cycle < chLogSecCnt;cycle++)
		 {
			 VainCheckFlag = LogicIntruSecVainCheck(pLogSecCntBuff[cycle],&DevId,&DevState);
			 LtrlDfncCheckFlag = LogicSecSwitchLtrlDfncCheck(pLogSecCntBuff[cycle],&DevId,&DevState);

			 if ((CI_ERROR == VainCheckFlag) || (CI_ERROR == LtrlDfncCheckFlag))
			 {
				 break;
			 }
			 else
			 {
				 /** 继续遍历*/
			 }
		 }


		 /** 设置函数返回值 */
		 ASSERT_SETRET(cycle == chLogSecCnt,ret,CI_SUCCESS,CI_ERROR);
	 }

	 return ret;
 }

  /** 
  * @brief 获取当前计轴沿查询方向的端点是否为方向变化点（预留函数：适配灯泡线功能）
  * @param[in] UINT16 acID 计轴区段ID
  * @param[in] UINT8 Dir 查询方向
  * @param[out] 无
  * @return UINT8
           DIR_CHANGE: 存在方向变化点
           DIR_UNCHANGE： 不存在方向变化点
		   UINT8_MAX 查询失败		   
  * @note 
  */
 UINT8 rrm_PrepareDirChangeOfAC(UINT16 acID,UINT8 Dir)
 {
	 UINT8 ret = DIR_UNCHANGE;
	 return ret;
 }

 /**
 * @brief 设置列车RRM授权状态、授权不允许理由及下一TIOC ID 
 * @param[in] UINT16 trainID 列车ID
 * @param[in] UINT8 rrmReply rrm授权状态
 * @param[in] UINT8 rrmReason rrm授权理由
 * @param[in] UINT8 nextOCId 下一个TIOC ID
 * @param[out] 无
 * @return  无
 * @note  无
 */
void rrm_SetRRMResult(UINT16 trainID,UINT8 rrmReply,UINT8 rrmReason,UINT8 nextOCId)
{
	UINT16 wRcIdWithType = 0x3c00U;

	wRcIdWithType |= (UINT16)nextOCId;

	SetTrainRrmState(trainID,rrmReply);
	SetTrainRrmReson(trainID,rrmReason);
	SetTrainNextTioc(trainID, wRcIdWithType);  /*设置下一控区ID需带0x3c，否则给IVOC组包时无法获取互联互通ID*/
}

/**
 * @brief 获取计轴区段锁闭所属进路是否存在保护区段
 * @param[in] UINT16 CurACID 当前计轴区段
 * @param[out]无
 * @return UINT8
		   CI_SUCCESS 不具备保护区段
		   CI_ERROR 具备保护区段
 * @note by cgh 20231102 支持下一站台内置保护区段远程RM
 */
UINT8 rrm_OverlapExistCheck(UINT16 CurACID)
{
	UINT8 chReval = CI_ERROR;
	UINT16 wCurAcRelatePhyId = 0U;/*当前计轴区段关联物理区段ID*/
	UINT16 wRLockBelongRouteId = 0U;/*物理区段锁闭所属进路*/
	UINT16 wRouteIdOverlopSum = 0U;/*物理区段锁闭所属进路保护区段数目*/

	if ((0U < CurACID) && (AXIS_SECTION_SUM_MAX > CurACID))
	{
		wCurAcRelatePhyId = GetAxisSecRelativePhySecId(CurACID);
		if ((0U < wCurAcRelatePhyId) && (PHYSICAL_SECTION_SUM_MAX > wCurAcRelatePhyId))
		{
			wRLockBelongRouteId = GetPhySecRLockBelongRouteId(wCurAcRelatePhyId);
			if ((0U < wRLockBelongRouteId) && (ROUTE_SUM_MAX > wRLockBelongRouteId))
			{
				wRouteIdOverlopSum = GetRouteOverlapSum(wRLockBelongRouteId);
				if (0U == wRouteIdOverlopSum)
				{
					chReval = CI_SUCCESS;
				}
				else
				{
					; /*返回失败*/
				}
			}
			else
			{
				; /*返回失败*/
			}
		}
		else
		{
			; /*返回失败*/
		}
	}
	else
	{
		; /*返回失败*/
	}

	return chReval;
}

/**
  * @brief 获取计轴上关联的防淹门状态
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] 无
  * @return UINT8
		   CI_SUCCESS 防淹门状态检查通过
		   CI_ERROR 防淹门状态检查不通过
  * @note by cgh 20240222
  */
UINT8 rrm_PrepareFGStatOfAC(UINT16 acID)
{
	UINT8 ret = CI_ERROR;
	UINT16 ii = 0U;
	UINT16 wCurAcRelatePhyId = 0U;/*当前计轴区段关联物理区段ID*/
	UINT16 wPhySecRelFloodGateNum = 0U;  /*物理区段关联的防淹门数量*/
	UINT16* pPhyLocatedFloodId = NULL;/*物理区段所在防淹门ID*/
	UINT8 chFloodState = 0u;/*防淹门状态*/

	if ((0U < acID) && (AXIS_SECTION_SUM_MAX > acID))
	{
		wCurAcRelatePhyId = GetAxisSecRelativePhySecId(acID);
		if ((0U < wCurAcRelatePhyId) && (PHYSICAL_SECTION_SUM_MAX > wCurAcRelatePhyId))
		{
			
			wPhySecRelFloodGateNum = GetPhySecLocatedFloodSum(wCurAcRelatePhyId);
			pPhyLocatedFloodId = GetPhySecLocatedFloodId(wCurAcRelatePhyId);

			if ((0U == wPhySecRelFloodGateNum) && (NULL != pPhyLocatedFloodId))
			{
				/*物理区段ID合法且不关联防淹门，检查通过*/
				ret = CI_SUCCESS;
			}
			else if ((FLOOD_GATE_SUM_MAX > wPhySecRelFloodGateNum) && (NULL != pPhyLocatedFloodId))
			{
				/*检查1：检查防淹门开启锁闭状态为开启锁闭*/
				for (ii = 0U; ii < wPhySecRelFloodGateNum; ii++)
				{
					chFloodState = GetFloodGateCurOpenLockState(pPhyLocatedFloodId[ii]);
					if (FLOOD_GATE_OPENLOCK == chFloodState)
					{
						ret = CI_SUCCESS;
					}
					else
					{
						ret = CI_ERROR;
					}

					/*检查2：检查防淹门关门请求状态为未关门请求*/
					if (CI_SUCCESS == ret)
					{
						chFloodState = GetFloodGateCurCloseRequestSta(pPhyLocatedFloodId[ii]);
						if (FLOOD_GATE_NOT_CLOSEREQ == chFloodState)
						{
							ret = CI_SUCCESS;
						}
						else
						{
							ret = CI_ERROR;
						}
					}

					/*检查3：检查防淹门当前同意关门输入状态为不同意关门*/
					if (CI_SUCCESS == ret)
					{
						chFloodState = GetFloodGateCurAgreeCloseSta(pPhyLocatedFloodId[ii]);
						if (FLOOD_GATE_NOT_AGREE_CLOSE == chFloodState)
						{
							ret = CI_SUCCESS;
						}
						else
						{
							ret = CI_ERROR;
						}
					}

					if (CI_SUCCESS != ret)
					{
						ret = CI_ERROR;
						break;  /*任意一个防淹门检查不通过，则停止检查，返回失败*/
					}
					else
					{
						/*继续检查*/
					}
				}
			}
			else
			{
				;  /*防淹门数据不合法，返回0失败*/
			}
		}
		else
		{
			; /*ID有误，返回检查不通过*/
		}
	}
	else
	{
		; /*计轴区段ID有误，返回检查不通过*/
	}

	return ret;
}

/**
  * @brief 获取计轴上关联的物理区段的区域紧急制动状态
  * @param[in] UINT16 acID 计轴区段ID
  * @param[out] 无
  * @return UINT8
		   CI_SUCCESS 区域紧急制动状态检查通过
		   CI_ERROR 区域紧急制动状态检查不通过
  * @note by cgh 20240222
  */
UINT8 rrm_PrepareAreaUrgentStatOfAC(UINT16 acID)
{
	UINT8 ret = CI_ERROR;
	UINT16 wCurAcRelatePhyId = 0U;/*当前计轴区段关联物理区段ID*/
	UINT8 chPhySecAreaUrgentState = 0U;/*物理区段区域紧急制动状态*/

	if ((0U < acID) && (AXIS_SECTION_SUM_MAX > acID))
	{
		wCurAcRelatePhyId = GetAxisSecRelativePhySecId(acID);
		if ((0U < wCurAcRelatePhyId) && (PHYSICAL_SECTION_SUM_MAX > wCurAcRelatePhyId))
		{
			chPhySecAreaUrgentState = GetPhySecAreaUrgentState(wCurAcRelatePhyId);

			if (PHYSEC_AREA_URGENT_NO == chPhySecAreaUrgentState)
			{
				/*区域紧急制动状态无效，检查通过*/
				ret = CI_SUCCESS;
			}
			else
			{
				/*有区域紧急制动或者非法值，检查不通过*/
				ret = CI_ERROR;
			}	
		}
		else
		{
			; /*ID有误，返回检查不通过*/
		}
	}
	else
	{
		; /*计轴区段ID有误，返回检查不通过*/
	}
	return ret;
}

/**
* @brief 判断RC是否为正线或试车线
* @param[in] const UINT8 cRcId RC ID
* @param[out] 无
* @return CI_SUCCESS 1 正线或试车线
		  CI_ERROR 0 非正线且非试车线
* @note Created By LQ 2024.02.28
*/
UINT8 rrm_PrepareRcIsZXorSCX(const UINT8 cRcId)
{
	UINT8 retVal = CI_ERROR;
	UINT8 cCiType = 0U;

	if ((0U < cRcId) && (CI_SUM_MAX > cRcId))
	{
		cCiType = GetCiType(cRcId);
		ASSERT_SETRET(((CI_TYPE_ZX == cCiType) || (CI_TYPE_SCX == cCiType)), retVal, CI_SUCCESS, CI_ERROR);
	}
	else
	{
		/*入参非法，返回0失败*/
	}

	return retVal;
}

/**
 * @brief 检查列车是否处于分界点且未向移交RC申请RRM
 * @param[in] const UINT16 wTrainID 列车ID
 * @param[in] const EnvelopeStruct* pTemp 列车包络信息
 * @param[out] 无
 * @return CI_SUCCESS 1 成功
		   CI_ERROR 0 失败
 * @note Created By LQ 2024.03.01
 */
UINT8 rrm_CfpTrainApplyCheck(const UINT16 wTrainID, IN const EnvelopeStruct* pTemp)
{
	UINT8 retVal = CI_ERROR;
	UINT32 errorCode = 0u;
	UINT8 ii = 0U;
	UINT8 jj = 0U;
	UINT8 kk = 0U;
	UINT8 cAxisSecNum = 0U;  /*包络后方计轴区段数量*/
	UINT16 szAxisSecId[AC_MAXCNT_IN_SWITCH] = { 0U };  /*包络后方计轴区段ID数组*/
	UINT8 cNextRcId = 0U;
	UINT8 cAxisSecIsOwnFlag = 0U;  /*计轴区段是否为本控区标志*/
	UINT8 cElmNum = 0U;
	UINT16 szEnvelopeID[SECTION_REL_ENVELOPE_SUM_MAX] = { 0U };
	const EnvelopeStruct* pTempInfo = NULL;
	UINT8 cDirChangePointFlag = 0U;  /*是否具有方向变化点标志*/
	UINT8 cTrainInCfpFlag = FLAG_UNSET;  /*列车是否跨压分界点标志*/\
	UINT16 wCfpId = 0U;  /*分界点ID*/
	UINT16 wCfpTrainId = 0U;  /*分界点列车ID*/
	UINT8 cRrmSrcRcApplyState = TRAIN_RRM_LOCALCI_NOT_APPLY;  /*源RC申请状态*/

	/** 入口参数校验 */
	ASSERT_RET_VALUE((NULL == pTemp) || (0U == pTemp->ACCnt), retVal, 0x80000000U);
	ASSERT_RET_VALUE((0U == (wTrainID & 0x00FFU)) || (0x00FFU == (wTrainID & 0x00FFU)), retVal, 0x40000000U);
	ASSERT_RET_VALUE((TYPE_CT_ENVELOPE != pTemp->envelopeType) && (TYPE_UCT_ENVELOPE != pTemp->envelopeType), retVal, 0x20000000U);
	
	cAxisSecNum = elm_PrepareAdjacentAC(pTemp->ACID[0U], ((UINT8)~(pTemp->startPos.Dir)), szAxisSecId, &cDirChangePointFlag);  /** 查询包络尾后方计轴 */

	if (UINT8_MAX == cAxisSecNum)
	{
		errorCode += 0x10000000U;  /*后方计轴区段查询失败，记录故障码*/
	}
	else
	{
		for (ii = 0U; ii < cAxisSecNum; ii++)
		{			
			cAxisSecIsOwnFlag = rrm_PrepareACIsSelf(szAxisSecId[ii], &cNextRcId);  /*判断计轴区段所属控区*/
			cElmNum = elm_GetEnvelopeIDsInAC(szAxisSecId[ii], szEnvelopeID);  /*获取计轴上的包络信息*/
			ASSERT_BREAK(SECTION_REL_ENVELOPE_SUM_MAX <= cElmNum, errorCode, 0x08000000U);

			if ((0U != cElmNum) && (ELM_BELONG_OWNER != cAxisSecIsOwnFlag))
			{
				/*后方计轴存在包络且不属于本RC，继续检查*/
				for (jj = 0U; jj < cElmNum; jj++)
				{
					pTempInfo = elm_GetEnvelopeInfoByID(szEnvelopeID[jj]);
					ASSERT_BREAK(NULL == pTempInfo, errorCode, 0x04000000U);

					/*包络非空，检查包络是否包含本车*/
					for (kk = 0U; kk < pTempInfo->trianCnt; kk++)
					{
						if ((pTempInfo->trainID[kk] & 0x00FFU) == (wTrainID & 0x00FFU))
						{
							/*后方计轴区段不属于本控区且包含本车包络，判断列车为跨分界点，停止遍历*/
							cTrainInCfpFlag = FLAG_SET;
							break;
						}
						else
						{
							/*继续遍历*/
						}
					}

					if (kk != pTempInfo->trianCnt)
					{
						break;
					}
					else
					{
						/*继续检查*/
					}
				}

				if (jj != cElmNum)
				{
					break;
				}
				else
				{
					/*继续检查*/
				}
			}
			else
			{
				/*继续遍历*/
			}
		}

		if (FLAG_UNSET != cTrainInCfpFlag)
		{
			/*列车跨压分界点，继续判断本RC是否收到相邻RC的RRM申请*/
			wCfpId = GetAxisRelateCfpId(pTemp->ACID[0U]);
			wCfpTrainId = GetCfpRrmTrainId(wCfpId);
			cRrmSrcRcApplyState = GetCfpRrmSrcApplyState(wCfpId);

			if (((wCfpTrainId & 0x00FFU) == (wTrainID & 0x00FFU)) && (TRAIN_RRM_LOCALCI_APPLY == cRrmSrcRcApplyState))
			{
				retVal = CI_SUCCESS;  /*车跨分界点且收到源RC申请RRM，返回1成功*/
			}
			else
			{
				errorCode += 0x02000000U;
				retVal = CI_ERROR;
			}			
		}
		else if(0U == errorCode)
		{
			retVal = CI_SUCCESS;  /*车未跨分界点，返回1成功*/
		}
		else
		{
			retVal = CI_ERROR;  /*返回0失败*/
		}

		ASSER_DEBUG(0U != errorCode, RRM_LOG(RRM_LOG_ERROR, "errno:0x%08x info:Train%d back has sameTrain.", errorCode, wTrainID));
	}

	return retVal;
}

/**
* @brief 判断停车区域是否属于本控区
* @param[in] const UINT16 wAtsLgcStaId 停车区域ID
* @param[out] 无
* @return CI_SUCCESS 不属于本控区
		  CI_ERROR   属于本控区
* @note bv cgh 20240918
*/
UINT8 rrm_PrepareAtsLgcStaIsOwn(const UINT16 wAtsLgcStaId)
{
	UINT8 retVal = CI_ERROR;
	UINT16 wAtsLgcStaBelCiId = 0U;
	UINT8 chLocalCiId = 0U;/*本地联锁ID*/

	if ((0U != wAtsLgcStaId) && (UINT16_MAX != wAtsLgcStaId))
	{
		wAtsLgcStaBelCiId = GetAtsLgcStationBelongTioc(wAtsLgcStaId);
		chLocalCiId = GetSystemParaLocalCiId();

		if ((wAtsLgcStaBelCiId == (UINT16)chLocalCiId) && (0U != wAtsLgcStaBelCiId))
		{
			/*属于本控区*/
			;
		}
		else
		{
			retVal = CI_SUCCESS;
		}		
	}
	else
	{
		/*入参非法，返回属于本控区，在外面检查失败*/
	}

	return retVal;
}