#include "AxisAssertManage.h"

static AxisAsserStruct gs_AxisAssert[AXIS_SECTION_SUM_MAX] = {0u};     /** 计轴故障诊断数据缓存 */
static UINT8 gs_InitEndlineFlag = AAM_END_LINE_UNINIT;      /* 尽头线ARB特殊处理标志 */


/**
 * @brief 初始化计轴诊断信息
 * @param[in] 无
 * @param[out] 无
 * @return  CI_ERROR, 处理错误
 *          CI_SUCCESS, 处理成功
 * @note 
 */
UINT8 aam_InitAxisAssert(void)
{
	return CommonMemSet(&gs_AxisAssert[0u],(UINT32)sizeof(AxisAsserStruct) * AXIS_SECTION_SUM_MAX,0u, (UINT32)sizeof(AxisAsserStruct)*AXIS_SECTION_SUM_MAX);
}

/**
 * @brief 通过数组索引获取计轴诊断信息
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
const AxisAsserStruct* aam_GetAxisAssertInfo(UINT16 acIndex)
{
	AxisAsserStruct* pRtn = NULL;
	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		pRtn = &gs_AxisAssert[acIndex];
	}
	return pRtn;
}

/**
 * @brief 设置包络进入的计轴区段类型
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
void aam_SetEnvelopeEnterType(UINT16 acIndex,UINT8 type,UINT16 trainID)
{
	UINT8 cycle = 0u;

	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		if (AAM_ENVELOPE_HEAD_ENTER == type)
		{
			for (cycle = 0u;cycle < gs_AxisAssert[acIndex].headEnterTrainCnt; cycle++)
			{
				if (trainID == gs_AxisAssert[acIndex].headEnterTrainId[cycle])
				{
					break;
				}
			}
			
			if ((cycle == gs_AxisAssert[acIndex].headEnterTrainCnt) && 
				(gs_AxisAssert[acIndex].headEnterTrainCnt < SECTION_REL_ENVELOPE_SUM_MAX))
			{
				 gs_AxisAssert[acIndex].headEnterTrainId[cycle] = trainID;
				 gs_AxisAssert[acIndex].headEnterTrainCnt++;
			}
			else
			{
				/** 不处理*/
			}
		}
		else if(AAM_ENVELOPE_TAIL_ENTER == type)
		{
			for (cycle = 0u;cycle < gs_AxisAssert[acIndex].tailEnterTrainCnt; cycle++)
			{
				if (trainID == gs_AxisAssert[acIndex].tailEnterTrainId[cycle])
				{
					break;
				}
			}

			if ((cycle == gs_AxisAssert[acIndex].tailEnterTrainCnt) && 
				(gs_AxisAssert[acIndex].tailEnterTrainCnt < SECTION_REL_ENVELOPE_SUM_MAX))
			{
				gs_AxisAssert[acIndex].tailEnterTrainId[cycle] = trainID;
				gs_AxisAssert[acIndex].tailEnterTrainCnt++;
			}
			else
			{
				/** 不处理*/
			}
		}
		else
		{
			/** 不处理 */
		}
	}
}


/**
 * @brief 设置好车通过标志位
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
void aam_SetPassTrainFlag(UINT16 acIndex,UINT16 trainID)
{
	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		gs_AxisAssert[acIndex].passTrainFlag = AAM_CT_PASS;
		gs_AxisAssert[acIndex].passTrainID = trainID;
	}
}

/**
 * @brief 冗余计时位增加
 * @param[in] UINT16 acIndex 计轴索引
 * @param[in] UINT8 maxTimes 占用到空闲的冗余时间
 * @param[out] 无
 * @return 无
 * @note 
 */
void aam_AddRedunTime(UINT16 acIndex,UINT8 maxTimes)
{
	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		if (gs_AxisAssert[acIndex].redunTime >= maxTimes)
		{
			/** 计时器满，不继续累加 */
		}
		else
		{
			gs_AxisAssert[acIndex].redunTime++;
		}
	}
}

/**
 * @brief 设置计轴状态
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
void aam_SetAxisStatus(UINT16 acIndex,UINT8 status)
{
	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX) &&
		((AAM_STATUS_FREE_OR_REDUN == status) ||(AAM_STATUS_ARB == status) || (AAM_STATUS_OCC == status)))
	{
		gs_AxisAssert[acIndex].acAssertStatus = status;
	}
}

/**
 * @brief 设置指定诊断信息
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_SetAxisAssert(UINT16 acIndex,const AxisAsserStruct* pBuff)
{
	UINT8 ret = CI_ERROR;
	UINT8 funRtn = 0u;

	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX) && (NULL != pBuff))
	{
		funRtn = CommonMemCpy((void*)&gs_AxisAssert[acIndex], (UINT32)sizeof(AxisAsserStruct), (const void*)pBuff, (UINT32)sizeof(AxisAsserStruct));
		ASSERT_SETRET(1u == funRtn,ret,CI_SUCCESS,CI_ERROR);
	}
	return ret;
}


/**
 * @brief 重置指定计轴诊断信息存储空间
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
void aam_ResetAxisAssertByACIndex(UINT16 acIndex)
{
	UINT8 chCanArbTag = 0U;/*保留Arb判断*/
	if ((0U != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		chCanArbTag = gs_AxisAssert[acIndex].redunOverCanArb;
		(void)CommonMemSet(&gs_AxisAssert[acIndex], (UINT32)sizeof(AxisAsserStruct),0, (UINT32)sizeof(AxisAsserStruct));
		gs_AxisAssert[acIndex].redunOverCanArb = chCanArbTag;
	}
}

/**
 * @brief 清除好车通过标志位
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_ClearPassTrainFlag(UINT16 acIndex)
{
	UINT8 ret = CI_ERROR;

	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		gs_AxisAssert[acIndex].passTrainFlag = AAM_UT_PASS;
		gs_AxisAssert[acIndex].passTrainID = 0u;
		ret = CI_SUCCESS;
	}

	return ret;
}

/**
 * @brief 清除冗余计时位
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_ClearRedunTime(UINT16 acIndex)
{
	UINT8 ret = CI_ERROR;

	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		gs_AxisAssert[acIndex].redunTime = 0u;
		ret = CI_SUCCESS;
	}

	return ret;
}

/**
 * @brief 清除计轴上车头进入的信息
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
void aam_ClearHeadEnterTrainId(UINT16 acIndex)
{
	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		(void)CommonMemSet(&gs_AxisAssert[acIndex].headEnterTrainId[0], (UINT32)sizeof(UINT16)*SECTION_REL_ENVELOPE_SUM_MAX,0, (UINT32)sizeof(UINT16)*SECTION_REL_ENVELOPE_SUM_MAX);
		gs_AxisAssert[acIndex].headEnterTrainCnt = 0u;
	}
}

/**
 * @brief 清除计轴上车头进入的信息的指定列车信息
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
void aam_ClearHeadEnterIdForTrain(UINT16 acIndex,UINT16 trainId)
{
	UINT8 cycle = 0u;
	UINT8 moveCycle = 0u;

	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX) && (0u != gs_AxisAssert[acIndex].headEnterTrainCnt))
	{
		for (cycle = 0u;cycle < gs_AxisAssert[acIndex].headEnterTrainCnt; cycle++)
		{
			if (trainId == gs_AxisAssert[acIndex].headEnterTrainId[cycle])
			{
				break;
			}
		}
		if (cycle == gs_AxisAssert[acIndex].headEnterTrainCnt)
		{
			/** 数据上没有，不处理 */
		}
		else 
		{
			/** 找到，直接清除*/
			for (moveCycle = cycle; moveCycle < (gs_AxisAssert[acIndex].headEnterTrainCnt - 1u);moveCycle++)
			{
				gs_AxisAssert[acIndex].headEnterTrainId[cycle] = gs_AxisAssert[acIndex].headEnterTrainId[cycle+1u];
			}
			gs_AxisAssert[acIndex].headEnterTrainId[moveCycle] = 0u;
			gs_AxisAssert[acIndex].headEnterTrainCnt--;
		}
	}
}

/**
 * @brief 清除计轴上车尾进入的信息
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
void aam_ClearTailEnterTrainId(UINT16 acIndex)
{
	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		(void)CommonMemSet(&gs_AxisAssert[acIndex].tailEnterTrainId[0u], (UINT32)sizeof(UINT16)*SECTION_REL_ENVELOPE_SUM_MAX,0, (UINT32)sizeof(UINT16)*SECTION_REL_ENVELOPE_SUM_MAX);
		gs_AxisAssert[acIndex].tailEnterTrainCnt = 0u;
	}
}


/**
 * @brief 清除计轴上车尾进入的信息的指定列车信息
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
void aam_ClearTailEnterIdForTrain(UINT16 acIndex,UINT16 trainId)
{
	UINT8 cycle = 0u;
	UINT8 moveCycle = 0u;

	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX) && (0u != gs_AxisAssert[acIndex].tailEnterTrainCnt))
	{
		for (cycle = 0u;cycle < gs_AxisAssert[acIndex].tailEnterTrainCnt; cycle++)
		{
			if (trainId == gs_AxisAssert[acIndex].tailEnterTrainId[cycle])
			{
				break;
			}
		}
		if (cycle == gs_AxisAssert[acIndex].tailEnterTrainCnt)
		{
			/** 数据上没有，不处理 */
		}
		else 
		{
			/** 找到，直接清除*/
			for (moveCycle = cycle; moveCycle < (gs_AxisAssert[acIndex].tailEnterTrainCnt - 1u);moveCycle++)
			{
				gs_AxisAssert[acIndex].tailEnterTrainId[cycle] = gs_AxisAssert[acIndex].tailEnterTrainId[cycle+1u];
			}
			gs_AxisAssert[acIndex].tailEnterTrainId[moveCycle] = 0u;
			gs_AxisAssert[acIndex].tailEnterTrainCnt--;
		}
	}
}


/**
 * @brief 设置尽头线ARB特殊处理标志 
 * @param[in] UINT8 flag 处理标志
 * @param[out]
 * @return 
 * @note 
 */
void aam_SetInitEndlineFlag(UINT8 flag)
{
	if ((AAM_END_LINE_UNINIT == flag) || (AAM_END_LINE_INIT == flag))
	{
		gs_InitEndlineFlag = flag;
	}
	else
	{
		gs_InitEndlineFlag = AAM_END_LINE_INIT;
	}
}


/**
 * @brief 获取尽头线ARB特殊处理标志 
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_GetInitEndlineFlag(void)
{
	return gs_InitEndlineFlag;
}




/**
 * @brief 获取好车通过标志位
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_GetPassTrainFlag(UINT16 acIndex)
{
	UINT8 ret = AAM_UT_PASS;

	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		ret = gs_AxisAssert[acIndex].passTrainFlag;
	}

	return ret;
}

/**
 * @brief 获取计轴状态
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_GetAxisStatus(UINT16 acIndex)
{
	UINT8 ret = AAM_STATUS_OCC;
	if ((0U != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		ret = gs_AxisAssert[acIndex].acAssertStatus;
	}
	return ret;
}


/**
 * @brief 获取计轴冗余时间
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_GetRedunTime(UINT16 acIndex)
{
	UINT8 ret = 0u;
	if ((0u != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		ret = gs_AxisAssert[acIndex].redunTime;
	}
	return ret;
}


/**
 * @brief 获取列车车头进入标志
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_GetHeadEnterFlag(UINT16 acIndex,UINT16 trainID)
{
	UINT8 ret = CI_ERROR;
	UINT8 cycle = 0u;

	if ((0u != acIndex) && (AXIS_SECTION_SUM_MAX > acIndex) && (0u != gs_AxisAssert[acIndex].headEnterTrainCnt))
	{
		for (cycle = 0u;cycle < gs_AxisAssert[acIndex].headEnterTrainCnt; cycle++)
		{
			if (trainID == gs_AxisAssert[acIndex].headEnterTrainId[cycle])
			{
				ret = CI_SUCCESS;
				break;
			}
			else
			{
				;
			}
		}
	}
	return ret;
}


/**
 * @brief 获取列车车尾进入标志
 * @param[in] 
 * @param[out]
 * @return 
 * @note 
 */
UINT8 aam_GetTailEnterFlag(UINT16 acIndex,UINT16 trainID)
{
	UINT8 ret = CI_ERROR;
	UINT8 cycle = 0u;

	if ((0u != acIndex) && (AXIS_SECTION_SUM_MAX > acIndex) && (0u != gs_AxisAssert[acIndex].tailEnterTrainCnt))
	{
		for (cycle = 0u;cycle < gs_AxisAssert[acIndex].tailEnterTrainCnt; cycle++)
		{
			if (trainID == gs_AxisAssert[acIndex].tailEnterTrainId[cycle])
			{
				ret = CI_SUCCESS;
				break;
			}
			else
			{
				;
			}
		}
	}
	else
	{
		; /*参数取值非法*/
	}
	return ret;
}

/*
* 功能描述： 获取指定ID计轴区段冗余结束可否ARB标志
* 参数说明： const UINT16 acIndex, 计轴索引
* 返回值  ： 0x55 可ARB 0xaa 不可ARB
* Create:  fan 2024/10/14
*/
UINT8 aam_GetAxisRedunCanArbFlag(const UINT16 acIndex)
{
	UINT8 ret = REDUN_OVER_ARB_NO;
	if ((0U != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		ret = gs_AxisAssert[acIndex].redunOverCanArb;
	}
	else
	{
		; /*返回REDUN_OVER_ARB_NO*/
	}
	return ret;
}


/*
* 功能描述： 设置指定ID计轴区段冗余结束可否ARB标志
* 参数说明： const UINT16 acIndex, 计轴索引
*			 const UINT8 redunOverArbFlag, 冗余结束可否ARB标志
* 返回值  ： 无
* Create:  fan 2024/10/14
*/
void aam_SetAxisRedunCanArbFlag(const UINT16 acIndex, const UINT8 redunOverArbFlag)
{

	if ((0u != acIndex) && (AXIS_SECTION_SUM_MAX > acIndex))
	{
		if ((REDUN_OVER_ARB_YES == redunOverArbFlag) || (REDUN_OVER_ARB_NO == redunOverArbFlag))
		{
			gs_AxisAssert[acIndex].redunOverCanArb = redunOverArbFlag;  /*计轴区段冗余结束可否ARB标志*/
		}
		else
		{
			; /*不做处理*/
		}
	}
	else
	{
		; /*不做处理*/
	}
}

/*
* 功能描述：设置指定ID计轴区段计轴通信中断可判ARB标志
* 参数说明：const UINT16 acIndex 计轴索引
*			const UINT8 cCommBadArbFlag 计轴通信中断可判ARB标志
* 返 回 值：无
* Created By LQ 2025.02.24
*/
void aam_SetAxisCommBadArbFlag(const UINT16 acIndex, const UINT8 cCommBadArbFlag)
{

	if ((0U != acIndex) && (AXIS_SECTION_SUM_MAX > acIndex))
	{
		if ((AAM_COMM_BAD_ARB_YES == cCommBadArbFlag) || (AAM_COMM_BAD_ARB_NO == cCommBadArbFlag))
		{
			gs_AxisAssert[acIndex].AxisCommBadArbFlag = cCommBadArbFlag;
		}
		else
		{
			; /*不做处理*/
		}
	}
	else
	{
		; /*不做处理*/
	}
}

/*
* 功能描述：获取指定ID计轴区段计轴通信中断可判ARB标志
* 参数说明：const UINT16 acIndex 计轴索引
* 返 回 值：0x55 有效
*           0xAA 无效
* Created By LQ 2025.02.24
*/
UINT8 aam_GetAxisCommBadArbFlag(const UINT16 acIndex)
{
	UINT8 ret = AAM_COMM_BAD_ARB_NO;

	if ((0U != acIndex) && (acIndex < AXIS_SECTION_SUM_MAX))
	{
		ret = gs_AxisAssert[acIndex].AxisCommBadArbFlag;
	}
	else
	{
		;
	}

	return ret;
}