/********************************************************
*
* 文 件 名： HLHTHeadContexManage.c
* 版权说明： 北京交控科技有限公司
* 创建时间： 2016-12-17
* 作    者： 联锁产品组
* 功能描述： 互联互通报文头上下文数据管理
* 函数包括：
* 使用注意：
* 修改记录：
*
********************************************************/
#include "HLHTHeadContexManage.h"
#include "CiSystemDefine.h"
#include "../2oo2/2oo2InterfaceDataManage.h"
#include "CommonMemory.h"

HLHTHeadContextStru ciHLHTHeadCtxs[CI_SUM_MAX]={0};/*存放与本CI通信的相邻CI互联互通报文头上下文数据*/
HLHTHeadContextStru vobcHLHTHeadCtxs[VOBC_SUM_MAX]={0};/*存放与本CI通信的VOBC互联互通报文头上下文数据*/
HLHTHeadContextStru vobcBackupHLHTHeadCtxs[VOBC_BACKUP_SUM_MAX] = { 0 };/*存放与本CI通信的后备模式VOBC互联互通报文头上下文数据*/
HLHTHeadContextStru zcHLHTHeadCtxs[ZC_SUM_MAX]={0};/*存放与本CI通信的ZC互联互通报文头上下文数据*/
HLHTHeadContextStru ocHLHTHeadCtxs[OC_SUM_MAX]={0};/*存放与本CI通信的OC互联互通报文头上下文数据*/
HLHTHeadContextStru axleHLHTHeadCtxs[AXLE_SUM_MAX] = { 0 };/*存放与本CI通信的计轴系统互联互通报文头上下文数据*/
HLHTHeadContextStru tmcHLHTHeadCtxs[TMC_SUM_MAX] = { 0 };/*存放与本TIOC通信的TMC互联互通报文头上下文数据*/
HLHTHeadContextStru IteHLHTHeadCtxs[ITE_SUM_MAX][VOBC_INC_ITE_SUM_MAX] = { 0U };/*存放与本TIOC通信的ITE互联互通报文头上下文数据*/

/*
* 功能描述:本地函数，获得某种设备类型的用于后续运算的互联互通头上下文数据
* 参数说明:
	const UINT8 devType:设备类型
	UINT16 *devMaxSum:出口参数,某种设备类型的允许数量
	HLHTHeadContextStru **hlhtHeadCtxPtr:出口参数,某种设备类型的互联互通头上下文数据指针
* 返回值:
	CI_ERROR:获取失败
*	CI_SUCCESS:获取成功
*/
static UINT8 GetHLHTHeadCtxTmepVars(const UINT8 devType, UINT16 *devMaxSum, HLHTHeadContextStru **hlhtHeadCtxPtr);
/*
* 功能描述:不同系统的互联互通通用头时效性处理
* 参数说明:
	const UINT32 timeoutSnCount,保持有效的最大周期数
	const UINT32 timeoutSpan,保持有效的最大时间
	HLHTHeadContextStru headCtxs[],待处理的通用头数组
	const UINT8 headCount,待处理的通用头的数量
* 返回值:
	无
*/
static void ProcHlhtHeadCtxTimeOutDiffSys(const UINT32 timeoutSnCount, const UINT32 timeoutSpan, HLHTHeadContextStru headCtxs[], const UINT16 headCount);

/*
* 功能描述:获取某个设备最近一次给CI发送数据时其当前的序号
* 参数说明:
	const UINT8 devType:设备类型
	const UINT8 devId:设备ID
* 返回值:
	某个设备最近一次给CI发送数据时其当前的序号,0u为无效值
*/
UINT32 GetHLHTHeadCtxSrcCrntSN(const UINT8 devType, const UINT8 devId)
{
	UINT32 retVal = 0u;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);
	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum))
		{
			retVal = tempHeadCtxPtr[devId].sourceCrntOwnMsgSN;
		}
		else
		{
			retVal = 0u;
		}
	}
    else
	{
	    /*无操作*/
	}
	return retVal;
}
/*
* 功能描述:获取某个设备最近一次给CI发送数据时里面填写的上次收到CI数据其当时的序号
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
* 返回值:
某个设备最近一次给CI发送数据时里面填写的上次收到CI数据其当时的序号,0u为无效值
*/
UINT32 GetHLHTHeadCtxSrcOwnSN(const UINT8 devType, const UINT8 devId)
{
	UINT32 retVal = 0u;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);
	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum))
		{
			if (tempHeadCtxPtr[devId].sourceId == devId)
			{
				retVal = tempHeadCtxPtr[devId].sourcePreOwnMsgSN;
			}
			else
			{
				retVal = 0u;
			}
		}
		else
		{
			retVal = 0u;
		}
	}
	else
	{
	    /*无操作*/
	}

	return retVal;
}
/*
* 功能描述:获取CI最近收到某个设备的数据时CI自身当时的序号
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
* 返回值:
CI最近收到某个设备的数据时自身当时的序号,0u为无效值
*/
UINT32 GetHLHTHeadCtxCiRcvSN(const UINT8 devType, const UINT8 devId)
{
	UINT32 retVal = 0u;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;


	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);
	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			retVal = tempHeadCtxPtr[devId].ciPreRecvOwnSn;
		}
		else
		{
			retVal = 0u;
		}
	}
    else
	{
	    /*无操作*/
	}
	return retVal;
}
/*
* 功能描述:设置某个与CI通信的设备的通信周期
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
const UINT16 period:通信周期
* 返回值:
无
*/
void SetHLHTHeadCtxComPeriod(const UINT8 devType, const UINT8 devId, const UINT16 period)
{
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			tempHeadCtxPtr[devId].sourceId = devId;
			tempHeadCtxPtr[devId].sourceComPeriod = period;

		}
		else
	    {
	         /*无操作*/
	    }
	}
    else
	{
	    /*无操作*/
	}
}
/*
* 功能描述:设置CI最近收到某个设备数据时通用头里面包含的对方当前的周期号
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
const UINT16 srcCrntSN:对方当前的周期号
* 返回值:
无
*/
void SetHLHTHeadCtxSrcCrntSN(const UINT8 devType, const UINT8 devId, const UINT32 srcCrntSN)
{
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			if (tempHeadCtxPtr[devId].sourceId == devId)
			{
				tempHeadCtxPtr[devId].sourceId = devId;
				tempHeadCtxPtr[devId].sourceCrntOwnMsgSN = srcCrntSN;
			}
			else
	        {
	             /*无操作*/
	        }
		}
		else
	    {
	         /*无操作*/
	    }
	}
    else
	{
	    /*无操作*/
	}
}
/*
* 功能描述:设置某个设备上一次收到CI数据时里面包含的CI当前的序号
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
const UINT16 srcOppSN:对方上一次收到CI数据时里面包含的CI当前的序号
* 返回值:
CI_SUCCESS:成功
CI_ERROR:失败
*/
void SetHLHTHeadCtxSrcOppSN(const UINT8 devType, const UINT8 devId, const UINT32 srcOppSN)
{
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			tempHeadCtxPtr[devId].sourceId = devId;
			tempHeadCtxPtr[devId].sourceRecvOppMsgSN = srcOppSN;
		}
		else
	    {
	         /*无操作*/
	    }
	}
    else
	{
	    /*无操作*/
	}
}
/*
* 功能描述:设置某个设备上一次收到CI数据时其自身的当前序号
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
const UINT16 srcOwnSN:某个设备上一次收到CI数据时其自身的当前序号
* 返回值:
CI_SUCCESS:成功
CI_ERROR:失败
*/
void SetHLHTHeadCtxSrcOwnSN(const UINT8 devType, const UINT8 devId, const UINT32 srcOwnSN)
{
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum))
		{
			tempHeadCtxPtr[devId].sourceId = devId;
			tempHeadCtxPtr[devId].sourcePreOwnMsgSN = srcOwnSN;

		}
		else
	    {
	         /*无操作*/
	    }
	}
	else
	{
	    /*无操作*/
	}
}
/*
* 功能描述:设置CI收到数据时CI自身的序号
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
const UINT16 ciRcvSn:CI收到数据时CI自身的序号
* 返回值:
CI_SUCCESS:成功
CI_ERROR:失败
*/
void SetHLHTHeadCtxCiRcvSN(const UINT8 devType, const UINT8 devId, const UINT32 ciRcvSn)
{
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			tempHeadCtxPtr[devId].sourceId = devId;
			tempHeadCtxPtr[devId].ciPreRecvOwnSn = ciRcvSn;
		}
		else
	    {
	         /*无操作*/
	    }
	}
	else
	{
	    /*无操作*/
	}
}

/*
* 功能描述:删除某个设备的互联互通头
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
* 返回值:
无
*/
void ClearHlhtHeadCtx(const UINT8 devType, const UINT8 devId)
{
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			(void)CommonMemSet(&tempHeadCtxPtr[devId], (UINT32)sizeof(HLHTHeadContextStru), 0u , (UINT32)sizeof(HLHTHeadContextStru));

			tempHeadCtxPtr[devId].sourceCrntOwnMsgSN = UINT32_MAX;
			tempHeadCtxPtr[devId].ciPreRecvOwnSn = UINT32_MAX;
		}
		else
	    {
	         /*无操作*/
	    }
	}
	else
	{
	    /*无操作*/
	}

}


/*
* 功能描述:初始化
* 参数说明:
*const UINT8 devType:设备类型
*/
void InitHlhtHeadCtx(const UINT8 devType)
{
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;
	UINT16 i = 0u;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
			for (i = 0u; i < maxDevSum; i++)
			{
				(void)CommonMemSet(&tempHeadCtxPtr[i], (UINT32)sizeof(HLHTHeadContextStru), 0u, (UINT32)sizeof(HLHTHeadContextStru));
				/* 初始化GAL头为建链状态 */
				tempHeadCtxPtr[i].sourceCrntOwnMsgSN = UINT32_MAX;
				tempHeadCtxPtr[i].ciPreRecvOwnSn = UINT32_MAX;
			}
	}
	else
	{
	     /*无操作*/
	}
}

/*
* 功能描述： 处理互联互联互通通用报文头时效性
* 参数说明：
	无
* 返回值  ：
	无
*/
void ProcHlhtHeadCtxTimeOut(void)
{
	UINT32 timeoutSnCount = 0U;
	UINT32 timeoutSpan = 0U;

	/*处理VOBC通用报文头的时效性*/
	timeoutSnCount = GetCiAndVobcTransBadEnsureCnt();
	timeoutSpan = GetCiAndVobcTransBadEnsureTime();
	ProcHlhtHeadCtxTimeOutDiffSys(timeoutSnCount, timeoutSpan, vobcHLHTHeadCtxs, VOBC_SUM_MAX);

	/*使用VOBC通信中断时间处理ITE通用报文头的时效性*/
	ProcIteHlhtHeadCtxTimeOut(timeoutSnCount, timeoutSpan);

	/*处理后备模式VOBC通用报文头的时效性*/
	timeoutSnCount = GetVobcCiBackUpCommErrorCount();
	timeoutSpan = GetVobcCiBackUpCommErrorTime();
	ProcHlhtHeadCtxTimeOutDiffSys(timeoutSnCount, timeoutSpan, vobcBackupHLHTHeadCtxs, VOBC_BACKUP_SUM_MAX);

	/*处理CI通用报文头的时效性*/
	timeoutSnCount = GetCiAndCiTransBadEnsureCnt();
	timeoutSpan = GetCiAndCiTransmitBadEnsureTime();
	ProcHlhtHeadCtxTimeOutDiffSys(timeoutSnCount, timeoutSpan, ciHLHTHeadCtxs, CI_SUM_MAX);

	/*处理与CI通信的ZC通用报文头的时效性*/
	timeoutSnCount = GetCiAndZcTransBadEnsureCnt();
	timeoutSpan = GetCiAndZcTransmitBadEnsureTime();
	ProcHlhtHeadCtxTimeOutDiffSys(timeoutSnCount, timeoutSpan, zcHLHTHeadCtxs, ZC_SUM_MAX);

	/*处理与TIOC通信的TMC通用报文头的时效性*/
	timeoutSnCount = GetTiocAndTmcTransBadEnsureCnt();
	timeoutSpan = GetTiocAndTmcTransBadEnsureTime();
	ProcHlhtHeadCtxTimeOutDiffSys(timeoutSnCount, timeoutSpan, tmcHLHTHeadCtxs, TMC_SUM_MAX);

	/*处理与OC通信报文头的时效性*/
	timeoutSnCount = GetCiAndOcTransmitBadEnsureCnt();
	timeoutSpan = GetCiAndOcTransmitBadEnsureTime();
	ProcHlhtHeadCtxTimeOutDiffSys(timeoutSnCount, timeoutSpan, ocHLHTHeadCtxs, OC_SUM_MAX);

	/*处理与AXLE通信报文头的时效性*/
	timeoutSnCount = GetCiAndAxleTransBadEnsureCnt();
	timeoutSpan = GetCiAndAxleTransBadEnsureTime();
	ProcHlhtHeadCtxTimeOutDiffSys(timeoutSnCount, timeoutSpan, axleHLHTHeadCtxs, AXLE_SUM_MAX);
}
/*
* 功能描述： 根据设备类型，获得具备初始通信互联互通头的所有设备ID
* 参数说明：
const UINT8 devType, 设备类型
UINT8* devIdBuf, 存储满足条件的设备编号的数组
const UINT8 devIdBufMaxLen, devIdBuf的容量，防止函数内访问越界
UINT8* devSum,表明满足条件的设备编号的数量
UINT16* devLogIdBuf 存储满足条件的设备逻辑ID的数组
* 返回值  ：
CI_SUCCESS:获得成功
CI_ERROR:获得失败
*/
UINT8 GetHlhtInitHeadDevsByDevType(const UINT8 devType, UINT8* devIdBuf, const UINT8 devIdBufMaxLen, UINT8* devSum, UINT16* devLogIdBuf)
{
	UINT8 retVal = CI_ERROR;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT16 i = 0U;
	UINT8 tempDevSum = 0u;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if ((devIdBuf != NULL) && (CI_SUCCESS == HLHTHeadVal))
	{
		if (devIdBufMaxLen >= maxDevSum)
		{
			for (i = 0u;i < maxDevSum;i++)
			{
				if ((UINT32_MAX == tempHeadCtxPtr[i].sourceRecvOppMsgSN) && (UINT32_MAX == tempHeadCtxPtr[i].sourcePreOwnMsgSN))
				{/*说明该通用头是对方还没有收到本ci的任何数据时，向ci发送的数据帧的报文头，即初始通信报文头*/
					devIdBuf[tempDevSum] = tempHeadCtxPtr[i].sourceId;
					devLogIdBuf[tempDevSum] = tempHeadCtxPtr[i].logId;
					tempDevSum++;
				}
				else
	            {
	                 /*无操作*/
	            }
			}

			retVal = CI_SUCCESS;
		}
		else
	    {
	        /*无操作*/
	    }
	}
	else
	{
	    /*无操作*/
	}

	/*为出口参数赋值*/
	if (CI_SUCCESS == retVal)
	{
		*devSum = tempDevSum;
	}
	else
	{
		*devSum = 0u;
	}

	return retVal;
}

/*
* 功能描述:本地函数，获得某种设备类型的用于后续运算的互联互通头上下文数据
* 参数说明:
const UINT8 devType:设备类型
UINT16 *devMaxSum:出口参数,某种设备类型的允许数量
HLHTHeadContextStru **hlhtHeadCtxPtr:出口参数,某种设备类型的互联互通头上下文数据指针
* 返回值:
CI_ERROR:获取失败
*	CI_SUCCESS:获取成功
*/
static UINT8 GetHLHTHeadCtxTmepVars(const UINT8 devType, UINT16 *devMaxSum, HLHTHeadContextStru **hlhtHeadCtxPtr)
{
	UINT8 retVal = CI_ERROR;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;

	if ((CI_SYSTEM_TYPE_CI == devType))
	{
		maxDevSum = CI_SUM_MAX;
		tempHeadCtxPtr = ciHLHTHeadCtxs;

		retVal = CI_SUCCESS;
	}
	else if (CI_SYSTEM_TYPE_VOBC == devType)
	{
		maxDevSum = VOBC_SUM_MAX;
		tempHeadCtxPtr = vobcHLHTHeadCtxs;

		retVal = CI_SUCCESS;
	}
	else if (CI_SYSTEM_TYPE_VOBC_BACKUP == devType)
	{
		maxDevSum = VOBC_BACKUP_SUM_MAX;
		tempHeadCtxPtr = vobcBackupHLHTHeadCtxs;

		retVal = CI_SUCCESS;
	}
	else if (CI_SYSTEM_TYPE_ZC == devType)
	{
		maxDevSum = ZC_SUM_MAX;
		tempHeadCtxPtr = zcHLHTHeadCtxs;

		retVal = CI_SUCCESS;
	}
	else if (CI_SYSTEM_TYPE_OC == devType)
	{
		maxDevSum = OC_SUM_MAX;
		tempHeadCtxPtr = ocHLHTHeadCtxs;

		retVal = CI_SUCCESS;
	}
	else if (CI_SYSTEM_TYPE_AXIS == devType)
	{
		maxDevSum = AXLE_SUM_MAX;
		tempHeadCtxPtr = axleHLHTHeadCtxs;

		retVal = CI_SUCCESS;
	}
	else if (CI_SYSTEM_TYPE_TMC == devType)
	{
		maxDevSum = TMC_SUM_MAX;
		tempHeadCtxPtr = tmcHLHTHeadCtxs;

		retVal = CI_SUCCESS;
	}
	else
	{
		retVal = CI_ERROR;
	}

	*devMaxSum = maxDevSum;
	*hlhtHeadCtxPtr = tempHeadCtxPtr;

	return retVal;
}
/*
* 功能描述:不同系统的互联互通通用头时效性处理
* 参数说明:
const UINT32 timeoutSnCount,保持有效的最大周期数
const UINT32 timeoutSpan,保持有效的最大时间
HLHTHeadContextStru headCtxs[],待处理的通用头数组
const UINT8 headCount,待处理的通用头的数量
* 返回值:
无
*/
static void ProcHlhtHeadCtxTimeOutDiffSys(const UINT32 timeoutSnCount, const UINT32 timeoutSpan, HLHTHeadContextStru headCtxs[], const UINT16 headCount)
{
	UINT32 curSn = Get2oo2CurCycleNum();
	UINT16 i = 0U;
	HLHTHeadContextStru tempHeadCtx = { 0u };
	UINT8 headValid = 0u;
	INT32 remainValidiy;/*剩余的时间*/

	if (NULL != headCtxs)
	{
		for (i = 0u;i < headCount;i++)
		{
			headValid = 1u; /*置为有效*/
			tempHeadCtx = headCtxs[i];

			if (0u != tempHeadCtx.sourceId)
			{/*说明是个有效的报文头*/
				if ((UINT32_MAX == tempHeadCtx.sourceRecvOppMsgSN) && (UINT32_MAX == tempHeadCtx.sourcePreOwnMsgSN))
				{/*说明对方还没有收到本ci的任何数据时，向ci发送的数据帧*/
					if ((curSn - tempHeadCtx.ciPreRecvOwnSn) > timeoutSnCount)
					{/*超过通信超时时间，也需要清除*/
						headValid = 0u;
					}
					else
					{
						;  /*不处理*/
					}
				}
				else
				{
					/*正常计算报文头的时效性*/
					remainValidiy = HLHTCommonMsgRemainingValidity(timeoutSpan, curSn, GetSystemParaAppCycleTime(), tempHeadCtx.sourceRecvOppMsgSN, tempHeadCtx.sourceCrntOwnMsgSN, tempHeadCtx.sourcePreOwnMsgSN, tempHeadCtx.sourceComPeriod);

					if (remainValidiy <= 0)
					{
						headValid = 0u;
					}
					else
					{
						;  /*不处理*/
					}
				}

				if (0u == headValid)
				{
					if (headCtxs == axleHLHTHeadCtxs) /*计轴系统中断时,GAL层对方序号,收到对方数据时本方序号,设置0xFFFFFFFF*/
					{
						headCtxs[i].logId = 0u;
						headCtxs[i].sourceRecvOppMsgSN = 0u;
						headCtxs[i].sourcePreOwnMsgSN = 0u;
						headCtxs[i].sourceComPeriod = 0u;
						headCtxs[i].sourceCrntOwnMsgSN = 0xFFFFFFFFu;
						headCtxs[i].ciPreRecvOwnSn = 0xFFFFFFFFu;
					}
					else
					{
						(void)CommonMemSet(&(headCtxs[i]), (UINT32)sizeof(HLHTHeadContextStru), 0u, (UINT32)sizeof(HLHTHeadContextStru));
						headCtxs[i].sourceCrntOwnMsgSN = 0xFFFFFFFFu;
						headCtxs[i].ciPreRecvOwnSn = 0xFFFFFFFFu;
					}
				}
				else
				{
					;  /*不处理*/
				}
			}
			else
			{
				;  /*不处理*/
			}
		}
	}
	else
	{
		; /*不处理*/
	}
}

/*
* 功能描述:获取某个与CI通信的设备的互联互通报文头上下文信息是否存在并且有效
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
* 返回值:
CI_ERROR:不存在
*	CI_SUCCESS:存在
*/
UINT8 GetHLHTHeadCtxValid(const UINT8 devType, const UINT8 devId)
{
	UINT8 retVal = CI_ERROR;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum))
		{
			if ((tempHeadCtxPtr[devId].sourceId == devId) && 
				(UINT32_MAX != tempHeadCtxPtr[devId].sourceRecvOppMsgSN) && 
				(UINT32_MAX !=tempHeadCtxPtr[devId].sourcePreOwnMsgSN))
			{
				retVal = CI_SUCCESS;
			}
			else
			{
				retVal = CI_ERROR;
			}
		}
		else
		{
			retVal = CI_ERROR;
		}
	}
	else
	{
		retVal = CI_ERROR;		
	}
	return retVal;
}

/*
* 功能描述:判断后备模式初始帧
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
* 返回值:
CI_ERROR:不是
*	CI_SUCCESS:是
*/
UINT8 BackupHeadCheckValid(const UINT8 devType, const UINT8 devId)
{
	UINT8 retVal = CI_ERROR;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;
	UINT32 curSn = Get2oo2CurCycleNum();
	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if ((CI_SUCCESS == HLHTHeadVal) && (CI_SYSTEM_TYPE_VOBC_BACKUP ==devType))
	{
		if ((0u < devId) && (devId < maxDevSum))
		{
			if ((tempHeadCtxPtr[devId].sourceId == devId) &&
				(UINT32_MAX == tempHeadCtxPtr[devId].sourceRecvOppMsgSN) &&
				(UINT32_MAX == tempHeadCtxPtr[devId].sourcePreOwnMsgSN))
			{
				retVal = CI_SUCCESS;
			}
			else
			{
				retVal = CI_ERROR;
			}
		}
		else
		{
			retVal = CI_ERROR;
		}
	}
	else
	{
		retVal = CI_ERROR;
	}
	if (CI_ERROR == retVal)
	{
		debug_infor_printf((const)"\n CurCycle:%d devID:%d devId:%d sourceRecvOppMsgSN:%d sourcePreOwnMsgSN:%d\n", curSn, devType, devId, tempHeadCtxPtr[devId].sourceRecvOppMsgSN, tempHeadCtxPtr[devId].sourcePreOwnMsgSN);
	}
	else
	{
		; /*不处理*/
	}
	return retVal;
}

/*
* 功能描述:获取某个与CI通信的设备的互联互通报文头上下文信息是否存在
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
* 返回值:
CI_ERROR:不存在
*	CI_SUCCESS:存在
*/
UINT8 GetHLHTHeadCtxExistance(const UINT8 devType,const UINT8 devId)
{
	UINT8 retVal = CI_ERROR;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			if ((tempHeadCtxPtr[devId].sourceId == devId))
			{
				retVal = CI_SUCCESS;
			}
			else
			{
				retVal = CI_ERROR;
			}
		}
		else
		{
			retVal = CI_ERROR;
		}
	}
	else
	{
		retVal = CI_ERROR;
	}

	return retVal;
}

/*
* 功能描述:设置CI收到数据时对方的LogId
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
const UINT16 ciRcvLogId:CI收到数据时对方的LogId
* 返回值:
CI_SUCCESS:成功
CI_ERROR:失败
*/
void SetHLHTHeadCtxCiRcvLogId(const UINT8 devType, const UINT8 devId, const UINT16 ciRcvLogId)
{
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);

	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			tempHeadCtxPtr[devId].sourceId = devId;
			tempHeadCtxPtr[devId].logId = ciRcvLogId;
		}
		else
	    {
	        /*无操作*/
	    }
	}
    else
	{
	    /*无操作*/
	}
}

/*
* 功能描述:获取某个设备上次收到CI数据时CI的序号
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
* 返回值:
某个设备上次收到CI数据时CI的序号,0u为无效值
*/
UINT32 GetHLHTHeadCtxSrcOppSN(const UINT8 devType, const UINT8 devId)
{
	UINT32 retVal = 0u;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);
	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			if (tempHeadCtxPtr[devId].sourceId == devId)
			{
				retVal = tempHeadCtxPtr[devId].sourceRecvOppMsgSN;
			}
			else
			{
				retVal = 0u;
			}
		}
		else
		{
			retVal = 0u;
		}
	}
    else
	{
	    /*无操作*/
	}
	return retVal;
}

/*
* 功能描述:获取某个设备的通信周期
* 参数说明:
const UINT8 devType:设备类型
const UINT8 devId:设备ID
* 返回值:
某个的通信周期
*/
UINT32 GetHLHTHeadCtxComPeriod(const UINT8 devType, const UINT8 devId)
{
	UINT32 retVal = 0u;
	UINT16 maxDevSum = 0u;
	HLHTHeadContextStru *tempHeadCtxPtr = NULL;
	UINT8 HLHTHeadVal = CI_ERROR;

	HLHTHeadVal = GetHLHTHeadCtxTmepVars(devType, &maxDevSum, &tempHeadCtxPtr);
	if (CI_SUCCESS == HLHTHeadVal)
	{
		if ((0u < devId) && (devId < maxDevSum) )
		{
			if (tempHeadCtxPtr[devId].sourceId == devId)
			{
				retVal = tempHeadCtxPtr[devId].sourceComPeriod;
			}
			else
			{
				retVal = 0u;
			}
		}
		else
		{
			retVal = 0u;
		}
	}
    else
	{
	    /*无操作*/
	}
	return retVal;
}

/*
* 功能描述：设置指定ITE的互联互通报文头上下文数据
* 参数说明：const UINT8 iteId ITE ID
*           const UINT16 iteLogId ITE逻辑ID
*           const HLHTGALInfoStruct* strHlhtGalHead 待设置的ITE互联互通报文头
* 返 回 值：无
* Created By LQ 2024.06.05
*/
void SetIteHLHTHeadCtx(const UINT8 iteId, const UINT16 iteLogId, const HLHTGALInfoStruct* pstrHlhtGalHead)
{
	UINT16 wIndex = 0U;
	UINT32 dwCurCycNum = 0U;  /*当前周期*/

	if ((0U < iteId) && (ITE_SUM_MAX > iteId) && (0U < iteLogId) && (VOBC_INC_ITE_SUM_MAX >= iteLogId) && (NULL != pstrHlhtGalHead))
	{
		dwCurCycNum = Get2oo2CurCycleNum();
		wIndex = iteLogId - 1U;
		IteHLHTHeadCtxs[iteId][wIndex].sourceId = iteId;
		IteHLHTHeadCtxs[iteId][wIndex].logId = iteLogId;
		IteHLHTHeadCtxs[iteId][wIndex].sourceComPeriod = pstrHlhtGalHead->comPeriod;
		IteHLHTHeadCtxs[iteId][wIndex].sourceCrntOwnMsgSN = pstrHlhtGalHead->crntOwnMsgSN;
		IteHLHTHeadCtxs[iteId][wIndex].sourceRecvOppMsgSN = pstrHlhtGalHead->recvOppMsgSN;
		IteHLHTHeadCtxs[iteId][wIndex].sourcePreOwnMsgSN = pstrHlhtGalHead->preOwnMsgSN;
		IteHLHTHeadCtxs[iteId][wIndex].ciPreRecvOwnSn = dwCurCycNum;
	}
	else
	{
		/*不处理*/
	}
}

/*
* 功能描述：获取指定ITE的互联互通报文头上下文数据
* 参数说明：const UINT8 iteId ITE ID
*           const UINT16 iteLogId ITE逻辑ID
* 返 回 值：NULL 获取数据失败
*           非NULL 获取数据成功
* Created By LQ 2024.06.05
*/
HLHTHeadContextStru* GetIteHLHTHeadCtx(const UINT8 iteId, const UINT16 iteLogId)
{
	HLHTHeadContextStru* pstrRetVal = NULL;
	UINT16 wIndex = 0U;

	if ((0U < iteId) && (ITE_SUM_MAX > iteId) && (0U < iteLogId) && (VOBC_INC_ITE_SUM_MAX >= iteLogId))
	{
		wIndex = iteLogId - 1U;
		pstrRetVal = &IteHLHTHeadCtxs[iteId][wIndex];
	}
	else
	{
		/*不处理*/
	}

	return pstrRetVal;
}

/*
* 功能描述：删除指定ITE的互联互通报文头上下文
* 参数说明：const UINT8 devId ITE ID
*           const UINT16 logId ITE逻辑ID
* 返 回 值：无
* Created By LQ 2024.06.05
*/
void ClearIteHlhtHeadCtx(const UINT8 devId, const UINT16 logId)
{
	UINT16 wIndex = 0U;

	if ((0U < devId) && (ITE_SUM_MAX > devId) && (0U < logId) && (VOBC_INC_ITE_SUM_MAX >= logId))
	{
		wIndex = logId - 1U;
		(void)CommonMemSet(&IteHLHTHeadCtxs[devId][wIndex], (UINT32)sizeof(HLHTHeadContextStru), 0U, (UINT32)sizeof(HLHTHeadContextStru));

		IteHLHTHeadCtxs[devId][wIndex].sourceCrntOwnMsgSN = UINT32_MAX;
		IteHLHTHeadCtxs[devId][wIndex].ciPreRecvOwnSn = UINT32_MAX;
	}
	else
	{
		/*不处理*/
	}
}

/*
* 功能描述：校验ITE互联互通报文头上下文信息是否有效
* 参数说明：const UINT8 cIteId ITE ID
*           const UINT16 wIteLogId ITE逻辑ID
* 返 回 值：CI_ERROR 0 无效
*	        CI_SUCCESS 1 有效
* Created By LQ 2024.06.06
*/
UINT8 CheckIteHLHTHeadCtxValid(const UINT8 cIteId, const UINT16 wIteLogId)
{
	UINT8 retVal = CI_ERROR;
	UINT16 wIndex = 0U;

	if ((0U < cIteId) && (ITE_SUM_MAX > cIteId) && (0U < wIteLogId) && (VOBC_INC_ITE_SUM_MAX >= wIteLogId))
	{
		wIndex = wIteLogId - 1U;
		if ((IteHLHTHeadCtxs[cIteId][wIndex].sourceId == cIteId) &&
			(UINT32_MAX != IteHLHTHeadCtxs[cIteId][wIndex].sourceRecvOppMsgSN) &&
			(UINT32_MAX != IteHLHTHeadCtxs[cIteId][wIndex].sourcePreOwnMsgSN))
		{
			retVal = CI_SUCCESS;
		}
		else
		{
			retVal = CI_ERROR;
		}
	}
	else
	{
		retVal = CI_ERROR;  /*获取ITE互联互通报文头上下文信息，返回0失败*/
	}

	return retVal;
}

/*
* 功能描述：获取具备初始通信互联互通头的ITEID
* 参数说明：UINT8* cIteNum 初始通信的ITE数量（出参）
*           UINT8 pIteIdBuf[ITE_TO_CI_SUM_MAX] 初始通信的ITE ID（出参）
*           UINT16 pIteLogIdBuf[ITE_TO_CI_SUM_MAX] 初始通信的ITE逻辑ID（出参）
* 返 回 值：CI_ERROR 0 获得失败
*           CI_SUCCESS 1 获得成功
* Created By LQ 2024.06.08
*/
UINT8 GetIteHlhtInitHeadDevs(UINT8* cIteNum, UINT8 pIteIdBuf[ITE_TO_CI_SUM_MAX], UINT16 pIteLogIdBuf[ITE_TO_CI_SUM_MAX])
{
	UINT8 cRetVal = CI_ERROR;
	UINT8 ii = 0U;
	UINT8 jj = 0U;
	UINT8 cDevSum = 0U;

	if ((NULL != cIteNum) && (NULL != pIteIdBuf) && (NULL != pIteLogIdBuf))
	{
		for (ii = 0U; ii < ITE_SUM_MAX; ii++)
		{
			for (jj = 0U; jj < VOBC_INC_ITE_SUM_MAX; jj++)
			{
				if ((UINT32_MAX == IteHLHTHeadCtxs[ii][jj].sourceRecvOppMsgSN) && (UINT32_MAX == IteHLHTHeadCtxs[ii][jj].sourcePreOwnMsgSN))
				{
					/*添加初始通信的ITE互联互通报文头*/
					pIteIdBuf[cDevSum] = IteHLHTHeadCtxs[ii][jj].sourceId;
					pIteLogIdBuf[cDevSum] = IteHLHTHeadCtxs[ii][jj].logId;
					cDevSum++;
				}
				else
				{
					/*无操作*/
				}
			}
		}

		*cIteNum = cDevSum;  /*传出初始通信的ITE数量*/

		cRetVal = CI_SUCCESS;
	}
	else
	{
		/*入参非法，不处理*/
	}

	return cRetVal;
}

/*
* 功能描述：ITE互联互通报文头时效性处理
* 参数说明：const UINT32 dwTimeoutSnCount 保持有效的最大周期数
*           const UINT32 dwTimeoutSpan 保持有效的最大时间
* 返 回 值：无
* Created By LQ 2024.06.11
*/
void ProcIteHlhtHeadCtxTimeOut(const UINT32 dwTimeoutSnCount, const UINT32 dwTimeoutSpan)
{
	UINT32 dwCurCycNum = 0U;
	UINT8 ii = 0U;
	UINT16 jj = 0U;
	UINT8 cHeadValidFlag = CI_ERROR;  /*报文头有效标志*/
	INT32 dwRemainValidiy = 0;  /*剩余的时间*/
	UINT32 dwAppCycle = 0U;  /*应用周期*/

	dwCurCycNum = Get2oo2CurCycleNum();
	dwAppCycle = GetSystemParaAppCycleTime();
	for (ii = 0U; ii < ITE_SUM_MAX; ii++)
	{
		for (jj = 0U; jj < VOBC_INC_ITE_SUM_MAX; jj++)
		{
			cHeadValidFlag = CI_SUCCESS;  /*每循环一次，初始化为有效*/

			if (0U != IteHLHTHeadCtxs[ii][jj].sourceId)
			{
				/*有效报文头，继续判断时效性*/
				if ((UINT32_MAX == IteHLHTHeadCtxs[ii][jj].sourceRecvOppMsgSN) && (UINT32_MAX == IteHLHTHeadCtxs[ii][jj].sourcePreOwnMsgSN))
				{
					/*说明对方还没有收到本ci的任何数据时，向ci发送的数据帧*/
					if ((dwCurCycNum - IteHLHTHeadCtxs[ii][jj].ciPreRecvOwnSn) > dwTimeoutSnCount)
					{						
						cHeadValidFlag = CI_ERROR;  /*超过通信超时时间，需要清除报文头*/
					}
					else
					{
						/*未超时，保留报文头*/
					}
				}
				else
				{
					/*正常计算报文头的时效性*/
					dwRemainValidiy = HLHTCommonMsgRemainingValidity(dwTimeoutSpan, dwCurCycNum, dwAppCycle, IteHLHTHeadCtxs[ii][jj].sourceRecvOppMsgSN, IteHLHTHeadCtxs[ii][jj].sourceCrntOwnMsgSN, IteHLHTHeadCtxs[ii][jj].sourcePreOwnMsgSN, IteHLHTHeadCtxs[ii][jj].sourceComPeriod);

					if (dwRemainValidiy <= 0)
					{
						cHeadValidFlag = CI_ERROR;  /*超过通信超时时间，需要清除报文头*/
					}
					else
					{
						/*未超时，保留报文头*/
					}
				}

				if (CI_SUCCESS == cHeadValidFlag)
				{
					/*未超时，保留报文头*/
				}
				else
				{
					/*超时，清除报文头*/
					(void)CommonMemSet(&(IteHLHTHeadCtxs[ii][jj]), (UINT32)sizeof(HLHTHeadContextStru), 0U, (UINT32)sizeof(HLHTHeadContextStru));
					IteHLHTHeadCtxs[ii][jj].sourceCrntOwnMsgSN = UINT32_MAX;
					IteHLHTHeadCtxs[ii][jj].ciPreRecvOwnSn = UINT32_MAX;
				}
			}
			else
			{
				/*无效报文头，不处理*/
			}
		}
	}
}