#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include "readxmltodata.h"
#include ".\\tinyxml\\tinyxml.h"/*读取XML的第三方库*/
#include "Convert.h"
#include "dsuRssp2Func.h"
#include "dquIdmapQuery.h"
#include "SystemParameterManage.h"


#define   PATH_IPCONFIG                "ini\\ZC.xml"                /*存放单机IP配置表的路径*/

static CiAtsCfg ciAtsCfgInfo;
static CiOcCfg ciOcCfgInfo;
DeviceIpInfoStru gDeviceIpInfoArray[1000];
DeviceIpInfoStru gDeviceIpInfoArrayATS[200];
DeviceIpInfoStru gDeviceIpInfoArrayLeu[100];


UINT16 gDeviceNum = 0;//设备条款数目
UINT16 gDeviceNumLeu = 0;//LEUCI配置文件中设备条款数目
UINT16 gDeviceNumAts = 0;//ATSCI配置文件中设备条款数目
UINT16 gDeviceNumOc = 0;//OC配置文件中设备条款数目
static IpInfo pCiIpInfo[4];
static IpInfo pAtsIpInfo[4];

UINT16 gPortDataLen = 0u;/*端口表有效行数*/
PORT_DATA_STRUCT *gpPortData = NULL;/*端口表数据指针，全局变量*/

UINT16 StrToIntData(char chTempData[20],UINT16 wDataLen)
{
	UINT16  wTempID = DEFAULT_ZERO;
	UINT16  wCycle = DEFAULT_ZERO;
	UINT8  wOneData = DEFAULT_ZERO;
	UINT16  wDataMultiple = 1;

	for (wCycle = wDataLen - 1;wCycle >= (UINT8)DEFAULT_ZERO;wCycle--)
	{
		switch (chTempData[wCycle])
		{
		case '0':
			wOneData = 0;
			break;
		case '1':
			wOneData = 1;
			break;
		case '2':
			wOneData = 2;
			break;
		case '3':
			wOneData = 3;
			break;
		case '4':
			wOneData = 4;
			break;
		case '5':
			wOneData = 5;
			break;
		case '6':
			wOneData = 6;
			break;
		case '7':
			wOneData =7;
			break;
		case '8':
			wOneData = 8;
			break;
		case '9':
			wOneData = 9;
			break;
		case 'a':
			wOneData = 10;
			break;
		case 'b':
			wOneData = 11;
			break;
		case 'c':
			wOneData = 12;
			break;
		case 'd':
			wOneData = 13;
			break;
		case 'e':
			wOneData = 14;
			break;
		case 'f':
			wOneData = 15;
			break;
		}

		if (wCycle == (wDataLen - 1))
		{
			wDataMultiple = 1;
		} 
		else
		{
			wDataMultiple = wDataMultiple * 16;
		}

		wTempID = wOneData * wDataMultiple + wTempID;

		if (wCycle == 0)
		{
			break;
		} 
		else
		{
		}
	}

	return wTempID;
}

void SetDataOfConfig(char chTempData[20],char chTempName[200],UINT16 wDataLen)
{
	static int wConfigCycle = DEFAULT_ZERO;
	char chChangeData[20];
	int wTempID = DEFAULT_ZERO;

	memset(chChangeData,DEFAULT_ZERO,sizeof(char) * 20);

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_DeviceIdAndTpye>"))
	{
		if (('0' == chTempData[0]) && ('x' == chTempData[1]))
		{
			memset(chChangeData,DEFAULT_ZERO,sizeof(char) * 20);
			memcpy(chChangeData,&chTempData[2],4);	

			gDeviceIpInfoArray[wConfigCycle].deviceName = StrToIntData(chChangeData,4);
		}
		else
		{
			printf("Id and Tpye error!\n");
		}
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_LogicId>"))
	{
		gDeviceIpInfoArray[wConfigCycle].logicId = atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_Red_IP>"))
	{
		sscanf(chTempData,"%d.%d.%d.%d",&gDeviceIpInfoArray[wConfigCycle].redIp[0],&gDeviceIpInfoArray[wConfigCycle].redIp[1],&gDeviceIpInfoArray[wConfigCycle].redIp[2],&gDeviceIpInfoArray[wConfigCycle].redIp[3]);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_Blue_IP>"))
	{
		sscanf(chTempData,"%d.%d.%d.%d",&gDeviceIpInfoArray[wConfigCycle].blueIp[0],&gDeviceIpInfoArray[wConfigCycle].blueIp[1],&gDeviceIpInfoArray[wConfigCycle].blueIp[2],&gDeviceIpInfoArray[wConfigCycle].blueIp[3]);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_SFP_BluePort>"))
	{
		gDeviceIpInfoArray[wConfigCycle].bluePort[SFP_IDX] = (UINT16)atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_SFP_RedPort>"))
	{
		gDeviceIpInfoArray[wConfigCycle].redPort[SFP_IDX] = atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_RP_BluePort>"))
	{
		gDeviceIpInfoArray[wConfigCycle].bluePort[RP_IDX] = atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_RP_RedPort>"))
	{
		gDeviceIpInfoArray[wConfigCycle].redPort[RP_IDX] = atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_RSSP_BluePort>"))
	{
		gDeviceIpInfoArray[wConfigCycle].bluePort[RSSP1_IDX] = atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_RSSP_RedPort>"))
	{
		gDeviceIpInfoArray[wConfigCycle].redPort[RSSP1_IDX] = atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_RSSP2_BluePort>"))
	{
		gDeviceIpInfoArray[wConfigCycle].bluePort[RSSP2_IDX] = atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<str_RSSP2_RedPort>"))
	{
		gDeviceIpInfoArray[wConfigCycle].redPort[RSSP2_IDX] = atoi(chTempData);

		wConfigCycle++;
		gDeviceNum = wConfigCycle;
	} 
	else
	{
	}
}

void GetOneData(char chDataBuff[200],const UINT16 strLen)
{
	int wCycle = DEFAULT_ZERO;

	UINT8  chStartFlag = DEFAULT_ZERO;
	UINT8  chEndFlag = DEFAULT_ZERO;
	char  chTempData[20];
	char  chTempName[200];
	UINT8  chDataIndex = DEFAULT_ZERO;
	UINT8  chNameIndex = DEFAULT_ZERO;
	UINT8  chNameFlag = DEFAULT_ZERO;

	memset(chTempData,DEFAULT_ZERO,sizeof(char) * 20);
	memset(chTempName,DEFAULT_ZERO,sizeof(char) * 200);

	for (wCycle = (UINT16)DEFAULT_ZERO;wCycle < strLen;wCycle++)
	{
		if ((chDataBuff[wCycle] == '<') && ((UINT8)FLAG_SET != chStartFlag))
		{
			chNameFlag = (UINT8)FLAG_SET;
		} 
		else
		{
			if ((UINT8)FLAG_SET == chStartFlag)
			{
				chNameFlag = (UINT8)FLAG_UNSET;
			} 
			else
			{
			}
		}

		if (FLAG_SET == chNameFlag)
		{
			chTempName[chNameIndex] = chDataBuff[wCycle];
			chNameIndex++;
		} 
		else
		{
		}

		if (chDataBuff[wCycle] == '>')
		{
			if (wCycle == strLen - 1)
			{
				break;
			} 
			else
			{
				chStartFlag = FLAG_SET;
				continue;
			}
		} 
		else
		{
		}

		if ((UINT8)FLAG_SET == chStartFlag)
		{
			if ((chDataBuff[wCycle] == '<') && (chDataBuff[wCycle + 1] == '/'))
			{
				break;
			} 
			else
			{
				chTempData[chDataIndex] = chDataBuff[wCycle];
				chDataIndex++;
			}
		} 
		else
		{
		}
	}

	if ((chDataIndex > 0) && (chNameIndex > 0))
	{
		SetDataOfConfig(chTempData,chTempName,chDataIndex);
	} 
	else
	{
	}
}

void ReadXml(const char * pPath)
{
	FILE *pFile = NULL;
	char ch;
	char chDataBuff[200];
	UINT16 wIndex = DEFAULT_ZERO;

	memset(gDeviceIpInfoArray, DEFAULT_ZERO, sizeof(gDeviceIpInfoArray));
	memset(chDataBuff, DEFAULT_ZERO, sizeof(char) * 200);

	if(gDeviceNum == 0 )
	{
		if (NULL == (pFile = fopen(pPath,"rt")))
		{
			printf("Open File error!\n");

			return;
		} 
		else
		{
		}

		while (EOF != (ch=fgetc(pFile)))
		{
			if (ch != '\n')
			{
				if (ch != ' ')
				{
					chDataBuff[wIndex] = ch;
					wIndex++;
				} 
				else
				{
				}
			} 
			else
			{
				GetOneData(chDataBuff,wIndex);
				memset(chDataBuff,0,sizeof(char) * 200);
				wIndex = 0;
			}
		}


		fclose(pFile);

	}
}

/*********************************************
*函数功能：根据设备ID（类型+ID）获得指定协议编号的网络EndPoint信息
*参数说明：无
*入口参数：
		devId：设备ID
		prtlIdx：协议编号
*出口参数：
		pIpInfos：找到的网络EndPoint信息
		ipCnt：网络EndPoint数量
*返回值：失败或成功
*********************************************/

UINT8 GetNetEndPoint(UINT16 devId, UINT16 logicId, UINT8 prtlIdx, IpInfo pIpInfos[], UINT8 *ipCnt)
{
	UINT16 idx = 0;
	UINT8 retVal = ERR_DEF;
	UINT8 tempIpCnt = 0;
	*ipCnt=0;
	int flag=0;
	int i=0;
	UINT16 index = 0;
	UINT32 hlhtId = 0u;

	UINT8 getHlhtRet = 0u;
	UINT8 isFind = 0u;
	UINT8 devType = 0u;
	UINT8 id = 0u;
	UINT32 vobcId[2] = { 0,0 }; 
	UINT32 iteId[2] = { 0,0 };
	UINT8 tempDevType = 0;
	UINT16 tempDevId = 0;
	UINT8 chFuncRtn = 0U;
	UINT8 chDsnSwitch = GetCiFunctionSwitchConfig(FUNC_DSN_LINE_SWITCH);

	if(prtlIdx<PRTL_CNT)
	{
		devType = (UINT8)(devId / 256);
		id = (UINT8)(devId & 0x00FF);
		switch (devType)
		{
		case DEVTYPE_ZC:
			getHlhtRet = dquQueryOutDevId(QFUN_IDMAP_ZC_ID, id, &hlhtId);
			break;
		case DEVTYPE_ATS:
			getHlhtRet = dquQueryOutDevId(QFUN_IDMAP_ATS_ID, id, &hlhtId);
			break;
		case DEVTYPE_CI:
			getHlhtRet = dquQueryOutDevId(QFUN_IDMAP_CI_ID, id, &hlhtId);
			break;
		case DEVTYPE_VOBC:
			getHlhtRet = dquQueryOutVobcDevId(id, vobcId);
			if ((0u == logicId) || (1u == logicId) || (2u == logicId))
			{
				if ((0u == logicId) || (1u == logicId))
				{
					hlhtId = vobcId[0];
				}
				else
				{
					hlhtId = vobcId[1];
				}
			}
			break;
		case DEVTYPE_ITE:
			getHlhtRet = dquQueryOutIteDevId(id, iteId);
			if ((0u == logicId) || (1u == logicId) || (2u == logicId))
			{
				if ((0u == logicId) || (1u == logicId))
				{
					hlhtId = iteId[0];
				}
				else
				{
					hlhtId = iteId[1];
				}
			}
			break;
		case DEVTYPE_NDSU:
			getHlhtRet = dquQueryOutDevId(QFUN_IDMAP_DSU_ID, id, &hlhtId);
			break;
		case DEVTYPE_AOM:
			getHlhtRet = dquQueryOutAOMDevId(id, vobcId);
			if ((0u == logicId) || (1u == logicId) || (2u == logicId))
			{
				if ((0u == logicId) || (1u == logicId))
				{
					hlhtId = vobcId[0];
				}
				else
				{
					hlhtId = vobcId[1];
				}
			}
			break;
		default:
			getHlhtRet = 0;
			break;
		}

		if (0xaau == chDsnSwitch)
		{
			for (idx = 0; idx < gDeviceNum; idx++)
			{
				tempDevType = gDeviceIpInfoArray[idx].deviceName >> 8;
				if (0x18 == tempDevType)
				{
					chFuncRtn = 1;
					break;
				}
				else
				{
					chFuncRtn = 0;
				}
			}
		}
		else
		{
			; /* 不走云接口机 */
		}

		tempDevType = devId >> 8;
		if ((1 == chFuncRtn) && ((0x14 == tempDevType) || (0x18 == tempDevType)))/* 有云的类型配置 并且 当前的类型是车 需要将IP改为云的IP */
		{
			tempDevId = 0x3c00 | GetSystemParaLocalCiId();
			for (idx = 0; idx < gDeviceNum; idx++)
			{
				if (0x1801 == gDeviceIpInfoArray[idx].deviceName)
				{
					/* 存云的IP */
					pIpInfos[0].blueIp = LongFromChar(gDeviceIpInfoArray[idx].blueIp);
					pIpInfos[0].redIp = LongFromChar(gDeviceIpInfoArray[idx].redIp);
					pIpInfos[0].bluePort = gDeviceIpInfoArray[idx].bluePort[prtlIdx];
					pIpInfos[0].redPort = gDeviceIpInfoArray[idx].redPort[prtlIdx];
				}
				if (tempDevId == gDeviceIpInfoArray[idx].deviceName)
				{
					/* 存OC的IP */
					pIpInfos[1].blueIp = LongFromChar(gDeviceIpInfoArray[idx].blueIp);
					pIpInfos[1].redIp = LongFromChar(gDeviceIpInfoArray[idx].redIp);
					pIpInfos[1].bluePort = gDeviceIpInfoArray[idx].bluePort[prtlIdx];
					pIpInfos[1].redPort = gDeviceIpInfoArray[idx].redPort[prtlIdx];
				}
				if (devId == gDeviceIpInfoArray[idx].deviceName && logicId == gDeviceIpInfoArray[idx].logicId)
				{
					/* 存车的IP */
					pIpInfos[2].blueIp = LongFromChar(gDeviceIpInfoArray[idx].blueIp);
					pIpInfos[2].redIp = LongFromChar(gDeviceIpInfoArray[idx].redIp);
					pIpInfos[2].bluePort = gDeviceIpInfoArray[idx].bluePort[prtlIdx];
					pIpInfos[2].redPort = gDeviceIpInfoArray[idx].redPort[prtlIdx];
				}
				retVal = 2;
			}
		}
		else
		{
			/*idx设备ip配置表索引*/
			for (idx = 0; idx < gDeviceNum; idx++)
			{
				isFind = 0u;
				if ((1u == getHlhtRet) && (0u != hlhtId)) /*有互联互通编号*/
				{
					if (hlhtId == gDeviceIpInfoArray[idx].estId)
					{
						isFind = 1u;
					}
				}
				else
				{
					if (devId == gDeviceIpInfoArray[idx].deviceName)
					{
						isFind = 1u;
					}
				}

				if (1u == isFind)
				{
					pIpInfos[tempIpCnt].blueIp = LongFromChar(gDeviceIpInfoArray[idx].blueIp);
					pIpInfos[tempIpCnt].redIp = LongFromChar(gDeviceIpInfoArray[idx].redIp);
					pIpInfos[tempIpCnt].bluePort = gDeviceIpInfoArray[idx].bluePort[prtlIdx];/*篮网端口，需要修改*/
					pIpInfos[tempIpCnt].redPort = gDeviceIpInfoArray[idx].redPort[prtlIdx];/*红网网端口，需要修改*/
					if (0x14 == (devId >> 8) && ((0xFFFF == pIpInfos[tempIpCnt].bluePort) || (0xFFFF == pIpInfos[tempIpCnt].redPort)))
					{
						flag = 1;
					}
					index = tempIpCnt;
					tempIpCnt++;
				}

				if (1 == flag)
				{
					for (i = 0; i < gPortDataLen; i++)
					{
						if ((SYSTEM_TYPE_CI == (gpPortData[i].SrcDeviceType & 0xFF))
							&& (SYSTEM_TYPE_VOBC == (gpPortData[i].DstDeviceType & 0xFF))
							&& (FACTORY_NO_TCT == gpPortData[i].SrcFactoryNo) /*交控厂商号2*/
							&& (gpPortData[i].DstFactoryNo == ((gDeviceIpInfoArray[idx].estId >> 29) & 0x07))
							)/*源设备类型是车,hlhtId高3bit源厂商号*/
						{
							if (0x1 == logicId)
							{
								pIpInfos[index].bluePort = gpPortData[i].Tc1BlueRecvPort;/*篮网端口，需要修改*/
								pIpInfos[index].redPort = gpPortData[i].Tc1RedRecvPort;/*红网网端口，需要修改*/
							}
							else if (0x2 == logicId)
							{
								pIpInfos[index].bluePort = gpPortData[i].Tc2BlueRecvPort;/*篮网端口，需要修改*/
								pIpInfos[index].redPort = gpPortData[i].Tc2RedRecvPort;/*红网网端口，需要修改*/
							}
							break;
						}
					}
					flag = 0;
				}

			}
		}


	}
	if(tempIpCnt>0)
	{
		retVal = SUC_DEF;
	}

	*ipCnt=tempIpCnt;

	return retVal;
}

/*********************************************
*函数功能：根据设备ID（类型+ID）获得指定协议编号的ATS网络EndPoint信息
	*参数说明：无
	*入口参数：
	devId：设备ID
	prtlIdx：协议编号
	*出口参数：
	pIpInfos：找到的网络EndPoint信息
	ipCnt：网络EndPoint数量
	*返回值：失败或成功
	*********************************************/
UINT8 GetNetATSEndPoint(UINT16 devId, UINT16 logicId, UINT8 prtlIdx, IpInfo pIpInfos[], UINT8 *ipCnt)
{
	UINT16 idx = 0;
	UINT8 retVal = ERR_DEF;
	UINT8 tempIpCnt = 0;
	*ipCnt=0;

	if(prtlIdx<PRTL_CNT)
	{
		for(idx = 0; idx < gDeviceNumAts; idx++)
		{
			if(devId == gDeviceIpInfoArrayATS[idx].deviceName && logicId == gDeviceIpInfoArrayATS[idx].logicId)
			{
				pIpInfos[tempIpCnt].blueIp = LongFromChar(gDeviceIpInfoArrayATS[idx].blueIp);
				pIpInfos[tempIpCnt].redIp = LongFromChar(gDeviceIpInfoArrayATS[idx].redIp);
				pIpInfos[tempIpCnt].bluePort = gDeviceIpInfoArrayATS[idx].bluePort[prtlIdx];
				pIpInfos[tempIpCnt].redPort = gDeviceIpInfoArrayATS[idx].redPort[prtlIdx];

				tempIpCnt++;
			}
		}
	}
	if(tempIpCnt>0)
	{
		retVal = SUC_DEF;
	}

	*ipCnt=tempIpCnt;

	return retVal;
}


/* 根据设备名称和协议类型查找IP和端口，
*查找成功返回0，查找失败返回 其他值 
*一个设备有多个IP，num为查找到的IP个数
*/
UINT8 GetIpAndPortByDeviceName(UINT16 deviceName,UINT8 protocolType,IpInfo pIpInfo[],UINT8 *num)
{
	UINT16 index = 0;/*查找范围条目索引*/
	UINT8 idxPrtcl = 0;/*协议对应的端口*/
	UINT8 retVal = 1;/*返回值，查找成功返回零，初始化为1*/
	UINT8 ipIndex = 0;/*查找到的IP数目*/

	UINT8 flag = 0;/*标记要查找的协议类型是否存在*/
	switch (protocolType)
	{
	case 1:		/* sfp 协议 */
		idxPrtcl = 0;
		flag = 1;
		break;
	case 2:	/* rp */
		idxPrtcl = 1;
		flag = 1;
		break;
	case 3: /* rsr */
		idxPrtcl = 2;
		flag = 1;
		break;
	case 4: /* rssp */
		idxPrtcl = 2;
		flag = 1;
		break;
	case 5: /* rssp */
		idxPrtcl = 3;
		flag = 1;
		break;
	default:
		flag = 0;/*没有要查找的协议类型*/
		break; /* 默认 sfp */
	}
	
	if (0 == gDeviceNum)
	{
		/*printf("读取XML配置文件失败！\n");*/
		retVal = 2;/*读取XML配置文件失败*/
	}
	for (index = 0; index < gDeviceNum; index++)
	{
		/* 待查找设备名称和协议是否存在 */		
		if ((flag == 1)&&(deviceName == gDeviceIpInfoArray[index].deviceName))
		{
			/*pIpInfo[ipIndex].blueIp 赋值 */
			pIpInfo[ipIndex].blueIp = LongFromChar(gDeviceIpInfoArray[index].blueIp);
			pIpInfo[ipIndex].redIp = LongFromChar(gDeviceIpInfoArray[index].redIp);
			pIpInfo[ipIndex].bluePort = gDeviceIpInfoArray[index].bluePort[idxPrtcl];
			pIpInfo[ipIndex].redPort = gDeviceIpInfoArray[index].redPort[idxPrtcl];
			ipIndex++;
		}

	}
	*num = ipIndex;
	if (ipIndex == 0)
	{
		retVal = 1;/*没有查找到*/
	}
	else
	{
		retVal = 0;
	}
	return retVal;
}

/*根据IP和端口查找设备名称，
查找成功返回零，查找失败返回其他值*/
UINT8 GetDeviceNameByIpAndPort1(UINT16 port, UINT32 ip, UINT16* deviceName, UINT16* pLogId, UINT32* pEtcid)
{
	UINT8 retVal = 0;/*查找成功返回0，初始化为1*/
	UINT16 index = 0;
	UINT8 flag = 0;
	UINT16 i = 1;

	if (0 == gDeviceNum)
	{
		retVal = 0;/*读取XML文件失败*/
	}
	for (index = 0; index < gDeviceNum; index++)
	{
		if ((ip == LongFromChar(gDeviceIpInfoArray[index].blueIp)) || (ip == LongFromChar(gDeviceIpInfoArray[index].redIp)))
		{
			flag = 1;
			*deviceName = gDeviceIpInfoArray[index].deviceName;
			/*VOBC*/
			if (DEVTYPE_VOBC == (UINT8)(gDeviceIpInfoArray[index].deviceName >> 8))
			{
				*pLogId = dquGetVobcNumForEstId(gDeviceIpInfoArray[index].estId);
				*pEtcid = gDeviceIpInfoArray[index].estId;
			}
			retVal = 1;
			break;
		}
		else
		{
			flag = 0;
		}
	}
	if ((flag == 1) && ((port == gDeviceIpInfoArray[index].bluePort[0]) || (port == gDeviceIpInfoArray[index].bluePort[1])
		|| (port == gDeviceIpInfoArray[index].bluePort[2]) || (port == gDeviceIpInfoArray[index].bluePort[3]) || (port == gDeviceIpInfoArray[index].redPort[0]) || (port == gDeviceIpInfoArray[index].redPort[1])
		|| (port == gDeviceIpInfoArray[index].redPort[2]) || (port == gDeviceIpInfoArray[index].redPort[3])))
	{
		retVal = 1;
	}
	else if ((flag == 1) && ((65535 == gDeviceIpInfoArray[index].bluePort[2])
		&& (65535 == gDeviceIpInfoArray[index].redPort[2]))
		&& (DEVTYPE_VOBC == (UINT8)(gDeviceIpInfoArray[index].deviceName >> 8)))
	{
		retVal = 0u;

		for (i = 0; i < gPortDataLen; i++)
		{
			if ((SYSTEM_TYPE_VOBC == (gpPortData[i].SrcDeviceType & 0xFF))
				&& (SYSTEM_TYPE_CI == (gpPortData[i].DstDeviceType & 0xFF))
				&& (gpPortData[i].SrcFactoryNo == ((gDeviceIpInfoArray[index].estId >> 29) & 0x07))
				&& (FACTORY_NO_TCT == (gpPortData[i].DstFactoryNo))
				)/*源设备类型是车,hlhtId高3bit源厂商号*/
			{
				if (0x1 == ((*pLogId) & 0x3) && ((port == gpPortData[i].Tc1BlueSendPort) || (port == gpPortData[i].Tc1RedSendPort)))
				{
					*pLogId = 0x1;
					retVal = 1;
					break;
				}
				else if (0x2 == ((*pLogId) & 0x3) && ((port == gpPortData[i].Tc2BlueSendPort) || (port == gpPortData[i].Tc2RedSendPort)))
				{
					*pLogId = 0x2;
					retVal = 1;
					break;
				}
				else
				{
					retVal = 0;
				}

			}
		}

	}
	else
	{
		retVal = 0;
	}
	return retVal;
}

/*根据IP和端口查找设备名称，
查找成功返回零，查找失败返回其他值*/
UINT8 GetDeviceNameByIpAndPort(UINT16 port,UINT32 ip,UINT16 *deviceName,UINT16 *pLogId)
{
	UINT8 retVal = 1;/*查找成功返回0，初始化为1*/
	UINT16 index = 0;
	UINT8 flag = 0;

	if (0 == gDeviceNum)
	{
		retVal = 2;/*读取XML文件失败*/
	}
	for (index = 0;index < gDeviceNum;index++)
	{
		if ((ip == LongFromChar(gDeviceIpInfoArray[index].blueIp))||(ip == LongFromChar(gDeviceIpInfoArray[index].redIp)))
		{
			flag = 1;
		} 
		else
		{
			flag = 0;
		}
		if ((flag == 1)&&((port == gDeviceIpInfoArray[index].bluePort[0])||(port == gDeviceIpInfoArray[index].bluePort[1])
			||(port == gDeviceIpInfoArray[index].bluePort[2])||(port == gDeviceIpInfoArray[index].bluePort[3])||(port == gDeviceIpInfoArray[index].redPort[0])||(port == gDeviceIpInfoArray[index].redPort[1])
			||(port == gDeviceIpInfoArray[index].redPort[2])||(port == gDeviceIpInfoArray[index].redPort[3])))
		{
			*deviceName = gDeviceIpInfoArray[index].deviceName;
			/*VOBC*/
			if((DEVTYPE_VOBC == (UINT8)(gDeviceIpInfoArray[index].deviceName >> 8))|| (DEVTYPE_ITE == (UINT8)(gDeviceIpInfoArray[index].deviceName >> 8)))
			{
				*pLogId = gDeviceIpInfoArray[index].logicId;
			}
			retVal = 0;
			break;
		} 
		else
		{
			retVal = 1;
		}
	}
	return retVal;
}

/*根据IP和端口查找设备名称，
查找成功返回零，查找失败返回其他值*/
UINT8 GetEtcsIdByIp(UINT8 ip[4], UINT32 *etcsId,UINT16 *pDevName)
{
	UINT8 retVal = 1;/*查找成功返回0，初始化为1*/
	UINT16 index = 0;
	UINT8 flag = 0;
	UINT16 logicId = 0;


	if (0 == gDeviceNum)
	{
		retVal = 2;/*读取XML文件失败*/
	}
	for (index = 0;index < gDeviceNum;index++)
	{
		if ((LongFromChar(ip) == LongFromChar(gDeviceIpInfoArray[index].blueIp))||(LongFromChar(ip) == LongFromChar(gDeviceIpInfoArray[index].redIp)))
		{
			flag = 1;
		} 
		else
		{
			flag = 0;
		}
		if (1 == flag)
		{
			*pDevName = gDeviceIpInfoArray[index].deviceName;
			logicId = gDeviceIpInfoArray[index].logicId;
			*etcsId = gDeviceIpInfoArray[index].estId;
			retVal = 1;
		}
		/*if ((flag == 1)&&((port == gDeviceIpInfoArray[index].bluePort[0])||(port == gDeviceIpInfoArray[index].bluePort[1])
			||(port == gDeviceIpInfoArray[index].bluePort[2])||(port == gDeviceIpInfoArray[index].bluePort[3])||(port == gDeviceIpInfoArray[index].redPort[0])||(port == gDeviceIpInfoArray[index].redPort[1])
			||(port == gDeviceIpInfoArray[index].redPort[2])||(port == gDeviceIpInfoArray[index].redPort[3])))
		{
			devName = gDeviceIpInfoArray[index].deviceName;
			logicId = gDeviceIpInfoArray[index].logicId;

			dquDevName2EtcsId(devName, logicId, etcsId);

			retVal = 0;

			break;
		} 
		else
		{
			retVal = 1;
		}*/
	}
	return retVal;
}

/*根据IP和端口查找设备名称，
查找成功返回零，查找失败返回其他值*/
UINT8 GetEtcsIdByIpAndPort(UINT16 port, UINT32 ip, UINT32 *etcsId)
{
	UINT8 retVal = 1;/*查找成功返回0，初始化为1*/
	UINT16 index = 0;
	UINT8 flag = 0;
	UINT16 devName = 0;
	UINT16 logicId = 0;
	

	if (0 == gDeviceNum)
	{
		retVal = 2;/*读取XML文件失败*/
	}
	for (index = 0;index < gDeviceNum;index++)
	{
		if ((ip == LongFromChar(gDeviceIpInfoArray[index].blueIp))||(ip == LongFromChar(gDeviceIpInfoArray[index].redIp)))
		{
			flag = 1;
		} 
		else
		{
			flag = 0;
		}
		if ((flag == 1)&&((port == gDeviceIpInfoArray[index].bluePort[0])||(port == gDeviceIpInfoArray[index].bluePort[1])
			||(port == gDeviceIpInfoArray[index].bluePort[2])||(port == gDeviceIpInfoArray[index].bluePort[3])||(port == gDeviceIpInfoArray[index].redPort[0])||(port == gDeviceIpInfoArray[index].redPort[1])
			||(port == gDeviceIpInfoArray[index].redPort[2])||(port == gDeviceIpInfoArray[index].redPort[3])))
		{
			devName = gDeviceIpInfoArray[index].deviceName;
			logicId = gDeviceIpInfoArray[index].logicId;

			dquDevName2EtcsId(devName, logicId, etcsId);

			retVal = 0;

			break;
		} 
		else
		{
			retVal = 1;
		}
	}
	return retVal;
}

void SetDataOfConfigLeu(char chTempData[20],char chTempName[200],UINT16 wDataLen)
{
	static int wConfigCycle = DEFAULT_ZERO;
	char chChangeData[20];
	int wTempID = DEFAULT_ZERO;

	memset(chChangeData,DEFAULT_ZERO,sizeof(char) * 20);

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<DeviceTypeAndId>"))
	{
		if (('0' == chTempData[0]) && ('x' == chTempData[1]))
		{
			memset(chChangeData,DEFAULT_ZERO,sizeof(char) * 20);
			memcpy(chChangeData,&chTempData[2],4);	

			//str_NativeIpConfig.strNativeIPConfig[wConfigCycle].wDevicdTpye = StrToIntData(chChangeData,2);

			gDeviceIpInfoArrayLeu[wConfigCycle].deviceName = StrToIntData(chChangeData,4);
		}

		/*if(('0' == chTempData[0]) && ('x' == chTempData[1]))
		{
			memset(chChangeData,DEFAULT_ZERO,sizeof(char) * 20);
			memcpy(chChangeData,&chTempData[2],4);
			gDeviceIpInfoArray[wConfigCycle].deviceName = StrToIntData(chChangeData,4);
		}*/
		else
		{
			printf("Id and Tpye error!\n");
		}
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<RedIP>"))
	{
		//memcpy(str_NativeIpConfig.strNativeIPConfig[wConfigCycle].chRedIP,chTempData,wDataLen);
		sscanf(chTempData,"%d.%d.%d.%d",&gDeviceIpInfoArrayLeu[wConfigCycle].redIp[0],&gDeviceIpInfoArrayLeu[wConfigCycle].redIp[1],&gDeviceIpInfoArrayLeu[wConfigCycle].redIp[2],&gDeviceIpInfoArrayLeu[wConfigCycle].redIp[3]);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<BlueIP>"))
	{
		//memcpy(str_NativeIpConfig.strNativeIPConfig[wConfigCycle].chBlueIP,chTempData,wDataLen);
		sscanf(chTempData,"%d.%d.%d.%d",&gDeviceIpInfoArrayLeu[wConfigCycle].blueIp[0],&gDeviceIpInfoArrayLeu[wConfigCycle].blueIp[1],&gDeviceIpInfoArrayLeu[wConfigCycle].blueIp[2],&gDeviceIpInfoArrayLeu[wConfigCycle].blueIp[3]);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<BluePort>"))
	{
		//str_NativeIpConfig.strNativeIPConfig[wConfigCycle].wSFPRedPort = atoi(chTempData);
		gDeviceIpInfoArrayLeu[wConfigCycle].bluePort[0] = atoi(chTempData);
	} 
	else
	{
	}

	if ((UINT8)DEFAULT_ZERO == strcmp(chTempName,"<RedPort>"))
	{
		//		str_NativeIpConfig.strNativeIPConfig[wConfigCycle].wSFPBluePort = atoi(chTempData);
		gDeviceIpInfoArrayLeu[wConfigCycle].redPort[0] = (UINT16)atoi(chTempData);
		wConfigCycle++;
		gDeviceNumLeu = wConfigCycle;
	} 
	else
	{
	}
}

void GetOneDataLeu(char chDataBuff[200],const UINT16 strLen)
{
	int wCycle = DEFAULT_ZERO;

	UINT8  chStartFlag = DEFAULT_ZERO;
	UINT8  chEndFlag = DEFAULT_ZERO;
	char  chTempData[20];
	char  chTempName[200];
	UINT8  chDataIndex = DEFAULT_ZERO;
	UINT8  chNameIndex = DEFAULT_ZERO;
	UINT8  chNameFlag = DEFAULT_ZERO;

	memset(chTempData,DEFAULT_ZERO,sizeof(char) * 20);
	memset(chTempName,DEFAULT_ZERO,sizeof(char) * 200);

	for (wCycle = (UINT16)DEFAULT_ZERO;wCycle < strLen;wCycle++)
	{
		if ((chDataBuff[wCycle] == '<') && ((UINT8)FLAG_SET != chStartFlag))
		{
			chNameFlag = (UINT8)FLAG_SET;
		} 
		else
		{
			if ((UINT8)FLAG_SET == chStartFlag)
			{
				chNameFlag = (UINT8)FLAG_UNSET;
			} 
			else
			{
			}
		}

		if (FLAG_SET == chNameFlag)
		{
			chTempName[chNameIndex] = chDataBuff[wCycle];
			chNameIndex++;
		} 
		else
		{
		}

		if (chDataBuff[wCycle] == '>')
		{
			if (wCycle == strLen - 1)
			{
				break;
			} 
			else
			{
				chStartFlag = FLAG_SET;
				continue;
			}
		} 
		else
		{
		}

		if ((UINT8)FLAG_SET == chStartFlag)
		{
			if ((chDataBuff[wCycle] == '<') && (chDataBuff[wCycle + 1] == '/'))
			{
				break;
			} 
			else
			{
				chTempData[chDataIndex] = chDataBuff[wCycle];
				chDataIndex++;
			}
		} 
		else
		{
		}
	}

	if ((chDataIndex > 0) && (chNameIndex > 0))
	{
		SetDataOfConfigLeu(chTempData,chTempName,chDataIndex);
	} 
	else
	{
	}
}
void ReadXmlLeuCICfg(const char * pPath)
{
	FILE *pFile = NULL;
	char ch;
	char chDataBuff[200];
	UINT16 wIndex = DEFAULT_ZERO;

	memset(gDeviceIpInfoArrayLeu,DEFAULT_ZERO,sizeof(gDeviceIpInfoArrayLeu));
	memset(chDataBuff,DEFAULT_ZERO,sizeof(char) * 200);

	if(gDeviceNumLeu == 0 )
	{
		if (NULL == (pFile = fopen(pPath,"rt")))
		{
			printf("Open File error!\n");

			return;
		} 
		else
		{
		}

		while (EOF != (ch=fgetc(pFile)))
		{
			if (ch != '\n')
			{
				if (ch != ' ')
				{
					chDataBuff[wIndex] = ch;
					wIndex++;
				} 
				else
				{
				}
			} 
			else
			{
				GetOneDataLeu(chDataBuff,wIndex);
				memset(chDataBuff,0,sizeof(char) * 200);
				wIndex = 0;
			}
		}


		fclose(pFile);

	}
}

/* 根据设备名称查找IP和端口，
*查找成功返回0，查找失败返回 其他值 
*一个设备有多个IP，num为查找到的IP个数
*/
UINT8 GetIpAndPortByDeviceNameLEUCI(UINT16 deviceName,IpInfo pIpInfo[],UINT8 *num)
{
	UINT16 index = 0;/*查找范围条目索引*/
	UINT8 retVal = 1;/*返回值，查找成功返回零，初始化为1*/
	UINT8 ipIndex = 0;/*查找到的IP数目*/

	if (0 == gDeviceNumLeu)
	{
		/*printf("读取XML配置文件失败！\n");*/
		retVal = 2;/*读取XML配置文件失败*/
	}
	for (index = 0; index < gDeviceNumLeu; index++)
	{
		/* 待查找设备名称和协议是否存在 */		
		if (deviceName == gDeviceIpInfoArrayLeu[index].deviceName)
		{
			/*pIpInfo[ipIndex].blueIp 赋值 */
			pIpInfo[ipIndex].blueIp = LongFromChar(gDeviceIpInfoArrayLeu[index].blueIp);
			pIpInfo[ipIndex].redIp = LongFromChar(gDeviceIpInfoArrayLeu[index].redIp);
			pIpInfo[ipIndex].bluePort = gDeviceIpInfoArrayLeu[index].bluePort[0];
			pIpInfo[ipIndex].redPort = gDeviceIpInfoArrayLeu[index].redPort[0];
			ipIndex++;
		}

	}
	*num = ipIndex;
	if (ipIndex == 0)
	{
		retVal = 1;/*没有查找到*/
	}
	else
	{
		retVal = 0;
	}
	return retVal;
}

/*根据IP和端口查找设备名称，
查找成功返回零，查找失败返回其他值*/
UINT8 GetDeviceNameByIpAndPortLEU(UINT16 port,UINT32 ip,UINT16 *deviceName)
{
	UINT8 retVal = 1;/*查找成功返回0，初始化为1*/
	UINT16 index = 0;
	UINT8 flag = 0;

	if (0 == gDeviceNumLeu)
	{
		retVal = 2;/*读取XML文件失败*/
	}
	for (index = 0;index < gDeviceNumLeu;index++)
	{
		if ((ip == LongFromChar(gDeviceIpInfoArrayLeu[index].blueIp))||(ip == LongFromChar(gDeviceIpInfoArrayLeu[index].redIp)))
		{
			flag = 1;
		} 
		else
		{
			flag = 0;
		}
		if ((flag == 1)&&((port == gDeviceIpInfoArrayLeu[index].bluePort[0])||(port == gDeviceIpInfoArrayLeu[index].redPort[0])))
		{
			*deviceName = gDeviceIpInfoArrayLeu[index].deviceName;
			retVal = 0;
			break;
		} 
		else
		{
			retVal = 1;
		}
	}
	return retVal;
}

/*********************************************
*函数功能：读取CI在入ATS网的配置
*参数说明：
*入口参数：
		pPath：配置文件路径
*出口参数：
		无
*返回值：失败或成功 0:成功 1：失败
*********************************************/
UINT8 ReadCiAtsCfg(const char *pPath)
{
	UINT8 retResult=ERR_DEF;

	TiXmlNode* rootNode;
	TiXmlNode* tNode;
	TiXmlNode* tNode1;
	const char* pChar;
	char charArr[100];
	int nodeType;
	UINT8 ciCnt=0;

	TiXmlDocument doc(pPath);
	if(doc.LoadFile())
	{
		rootNode=doc.RootElement();

		tNode=rootNode->FirstChild("InAts");
		pChar=tNode->FirstChild()->Value();
		ciAtsCfgInfo.InAts=(UINT8)atoi(pChar);

		tNode=rootNode->FirstChild("XdInAts");
		pChar=tNode->FirstChild()->Value();
		ciAtsCfgInfo.XdInAts=(UINT8)atoi(pChar);

		tNode = rootNode->FirstChild("IpS");
		for( tNode = tNode->FirstChild();tNode;tNode = tNode->NextSibling() )
		{
			nodeType = tNode->Type();/*获得节点类型*/
			switch(nodeType)
			{
			case TiXmlNode::TINYXML_ELEMENT:
				tNode1=tNode->FirstChild("DevId");
				pChar=tNode1->FirstChild()->Value();
				memcpy(charArr,&(pChar[2]),4);	
				ciAtsCfgInfo.IpS[ciCnt].DevId=(UINT16)StrToIntData(charArr,4);

				tNode1=tNode->FirstChild("RedIp");
				pChar=tNode1->FirstChild()->Value();
				sscanf(pChar,"%d.%d.%d.%d",&(charArr[0]),&(charArr[1]),&(charArr[2]),&(charArr[3]));
				ciAtsCfgInfo.IpS[ciCnt].RedIp=LongFromChar((UINT8 *)charArr);

				tNode1=tNode->FirstChild("BlueIp");
				pChar=tNode1->FirstChild()->Value();
				sscanf(pChar,"%d.%d.%d.%d",&(charArr[0]),&(charArr[1]),&(charArr[2]),&(charArr[3]));
				ciAtsCfgInfo.IpS[ciCnt].BlueIp=LongFromChar((UINT8 *)charArr);

				tNode1=tNode->FirstChild("RedPort");
				pChar=tNode1->FirstChild()->Value();
				ciAtsCfgInfo.IpS[ciCnt].RedPort=(UINT16)atoi(pChar);

				tNode1=tNode->FirstChild("BluePort");
				pChar=tNode1->FirstChild()->Value();
				ciAtsCfgInfo.IpS[ciCnt].BluePort=(UINT16)atoi(pChar);

				ciCnt++;/*ci数量增加*/
				break;
			default:
				break;
			}
		}
		retResult=SUC_DEF;/*不进行异常判断，都认为成功，可以优化*/
	}

	ciAtsCfgInfo.CiCount=ciCnt;

	return retResult;
}

/*********************************************
*函数功能：读取CI在入OC网的配置
*参数说明：
*入口参数：
pPath：配置文件路径
*出口参数：
无
*返回值：失败或成功 0:成功 1：失败
*********************************************/
UINT8 ReadCiOcCfg(const char *pPath,UINT16 wDevName, CiOcIpInfo* pIpInfo)
{
	UINT8 retResult = 0u;

	TiXmlNode* rootNode = NULL;
	TiXmlNode* tNode = NULL;
	TiXmlNode* tNode1 = NULL;
	const char* pChar;
	char charArr[100];
	int nodeType = 0;
	UINT8 ciCnt = 0u;
	UINT16 wTmpDevName = 0u;
	UINT16 wTmpDevSum = gDeviceNum;

	TiXmlDocument doc(pPath);
	if ((doc.LoadFile()) && (NULL != pIpInfo))
	{
		rootNode = doc.RootElement();

		tNode = rootNode->FirstChild("InCI");
		pChar = tNode->FirstChild()->Value();
		ciOcCfgInfo.InCi = (UINT8)atoi(pChar);

		tNode = rootNode->FirstChild("IpS");
		for (tNode = tNode->FirstChild(); tNode; tNode = tNode->NextSibling())
		{
			nodeType = tNode->Type();/*获得节点类型*/
			switch (nodeType)
			{
			case TiXmlNode::TINYXML_ELEMENT:
				tNode1 = tNode->FirstChild("DevId");
				pChar = tNode1->FirstChild()->Value();
				memcpy(charArr, &(pChar[2]), 4);
				wTmpDevName = (UINT16)StrToIntData(charArr, 4);

				/* 获取设备类型与编号 */
				ciOcCfgInfo.IpS[ciCnt].DevId = (UINT8)(wTmpDevName & 0x00FF);
				ciOcCfgInfo.IpS[ciCnt].DevType = (UINT8)((wTmpDevName & 0xFF00) >> 8);

				tNode1 = tNode->FirstChild("RedIp");
				pChar = tNode1->FirstChild()->Value();
				sscanf(pChar, "%d.%d.%d.%d", &(charArr[0]), &(charArr[1]), &(charArr[2]), &(charArr[3]));
				ciOcCfgInfo.IpS[ciCnt].RedIp = LongFromChar((UINT8 *)charArr);

				tNode1 = tNode->FirstChild("BlueIp");
				pChar = tNode1->FirstChild()->Value();
				sscanf(pChar, "%d.%d.%d.%d", &(charArr[0]), &(charArr[1]), &(charArr[2]), &(charArr[3]));
				ciOcCfgInfo.IpS[ciCnt].BlueIp = LongFromChar((UINT8 *)charArr);

				tNode1 = tNode->FirstChild("RedPort");
				pChar = tNode1->FirstChild()->Value();
				ciOcCfgInfo.IpS[ciCnt].RedPort = (UINT16)atoi(pChar);

				tNode1 = tNode->FirstChild("BluePort");
				pChar = tNode1->FirstChild()->Value();
				ciOcCfgInfo.IpS[ciCnt].BluePort = (UINT16)atoi(pChar);

				/* 将读取到的ip与端口都写入查询数据中 */
				gDeviceIpInfoArray[wTmpDevSum].deviceName = wTmpDevName;
				gDeviceIpInfoArray[wTmpDevSum].estId = 0u;

				LongToChar(ciOcCfgInfo.IpS[ciCnt].BlueIp, gDeviceIpInfoArray[wTmpDevSum].blueIp);
				LongToChar(ciOcCfgInfo.IpS[ciCnt].RedIp, gDeviceIpInfoArray[wTmpDevSum].redIp);

				gDeviceIpInfoArray[wTmpDevSum].bluePort[SFP_IDX] = UINT16_MAX;
				gDeviceIpInfoArray[wTmpDevSum].redPort[SFP_IDX] = UINT16_MAX;

				gDeviceIpInfoArray[wTmpDevSum].bluePort[RP_IDX] = UINT16_MAX;
				gDeviceIpInfoArray[wTmpDevSum].redPort[RP_IDX] = UINT16_MAX;

				gDeviceIpInfoArray[wTmpDevSum].bluePort[RSSP1_IDX] = ciOcCfgInfo.IpS[ciCnt].BluePort;
				gDeviceIpInfoArray[wTmpDevSum].redPort[RSSP1_IDX] = ciOcCfgInfo.IpS[ciCnt].RedPort;

				gDeviceIpInfoArray[wTmpDevSum].bluePort[RSSP2_IDX] = UINT16_MAX;
				gDeviceIpInfoArray[wTmpDevSum].redPort[RSSP2_IDX] = UINT16_MAX;

				/* 将CI接收的IP与端口传出去 */
				if (wTmpDevName == wDevName)
				{
					pIpInfo->DevId = ciOcCfgInfo.IpS[ciCnt].DevId;
					pIpInfo->DevType = ciOcCfgInfo.IpS[ciCnt].DevType;
					pIpInfo->BlueIp = ciOcCfgInfo.IpS[ciCnt].BlueIp;
					pIpInfo->BluePort = ciOcCfgInfo.IpS[ciCnt].BluePort;
					pIpInfo->RedIp = ciOcCfgInfo.IpS[ciCnt].RedIp;
					pIpInfo->RedPort = ciOcCfgInfo.IpS[ciCnt].RedPort;
				}

				wTmpDevSum++;
				ciCnt++;/*ci数量增加*/
				break;
			default:
				break;
			}
		}
		retResult = ciOcCfgInfo.InCi;
		ciOcCfgInfo.CiCount = ciCnt;
	}
	else
	{
		;
	}

	if ((1u == retResult) && (wTmpDevSum > gDeviceNum))
	{
		gDeviceNum = wTmpDevSum;
	}

	return retResult;
}

/*********************************************
*函数功能：读取指定CI在ATS网络中的网络节点信息
*参数说明：
*入口参数：
		devId：CI设备ID
		UINT8 ccovIdx:协议编号
*出口参数：
		pIpInfo：CI在ATS网络中的网络节点信息
*返回值：失败或成功 0:成功 1：失败
*********************************************/
UINT8 ReadCiAtsIpInfo(UINT16 devId,UINT8 ccovIdx,CiAtsIpInfo *pIpInfo)
{
	UINT8 retResult=ERR_DEF;
	UINT8 ciCnt=0;
	UINT8 numAts = 0;
	UINT8 ipAddrAts[4];

	UINT8 i;
	ciAtsCfgInfo.InAts = 1u;
	ciAtsCfgInfo.XdInAts = 1u;
	ciAtsCfgInfo.IpS[ciCnt].DevId = devId;

	GetNetATSEndPoint(devId, 0, ccovIdx, pAtsIpInfo, &numAts);
	if (numAts > 0)
	{
		LongToChar(pAtsIpInfo[numAts-1].redIp,ipAddrAts);

		ciAtsCfgInfo.IpS[ciCnt].RedIp=LongFromChar((UINT8 *)ipAddrAts);

		LongToChar(pAtsIpInfo[numAts-1].blueIp,ipAddrAts);

		ciAtsCfgInfo.IpS[ciCnt].BlueIp=LongFromChar((UINT8 *)ipAddrAts);

		ciAtsCfgInfo.IpS[ciCnt].RedPort=pAtsIpInfo[numAts-1].redPort;

		ciAtsCfgInfo.IpS[ciCnt].BluePort=pAtsIpInfo[numAts-1].bluePort;

		ciCnt++;/*ci数量增加*/
		retResult=SUC_DEF;/*不进行异常判断，都认为成功，可以优化*/
		ciAtsCfgInfo.CiCount = ciCnt;

		for(i=0;i<ciAtsCfgInfo.CiCount;i++)
		{
			if(devId==ciAtsCfgInfo.IpS[i].DevId)
			{/*设备编号匹配*/
				pIpInfo->DevId=devId;
				pIpInfo->RedIp=ciAtsCfgInfo.IpS[i].RedIp;
				pIpInfo->BlueIp=ciAtsCfgInfo.IpS[i].BlueIp;
				pIpInfo->RedPort=ciAtsCfgInfo.IpS[i].RedPort;
				pIpInfo->BluePort=ciAtsCfgInfo.IpS[i].BluePort;

				retResult=SUC_DEF;
				break;
			}
		}

	}
	else
	{
		printf("\n Read ci.fs CI_ATS fail.\n");
	}

	return retResult;
}

/*********************************************
*函数功能：读取CI是否入ATS网标志
*参数说明：
*入口参数：
		无
*出口参数：
		无
*返回值：CI是否入ATS网标志
*********************************************/
UINT8 ReadCiInAtsFlag(void)
{
	return ciAtsCfgInfo.InAts;
}

/*********************************************
*函数功能：读取CI与现地通信方式标志
*参数说明：
*入口参数：
		无
*出口参数：
		无
*返回值：CI与现地通信方式标志
*********************************************/
UINT8 ReadXdInAtsFlag(void)
{
	return ciAtsCfgInfo.XdInAts;
}

/*********************************************
*函数功能：通过互联互通ID获取VOBC编号
*参数说明：
*入口参数：
		EtcsId VOBC互联互通ID
*出口参数：
		无
*返回值：VOBC编号
*********************************************/
UINT16 dquGetVobcNumForEstId(UINT32 EtcsId)
{
	/*变量初始化*/
	UINT8 endId = 0u;
	UINT16 ret = 0u;

	endId = GET_VOBC_END_ID(EtcsId);
	switch (endId)
	{
	case 1:
		ret = VOBC_ENDID_TC1;
		break;
	case 2:
		ret = VOBC_ENDID_TC2;
		break;
	case 3:
		ret = VOBC_ENDID_REDUN;
		break;
	default:
		ret = 0;
		break;
	}
	return(ret);
}


/*
* 功能描述：初始化端口表数据。
* 输入参数：UINT8* FSName， 文件名或者指针地址
* 返 回 值：1，成功
*           0，失败，数据有错误
*/
UINT8 InitPortTableData(UINT8* FSName)
{
	UINT8 rtVal = 0u;
	UINT8* pData=NULL;       /*用于读取数据的指针*/
	UINT32 dataLen=0;        /*数据长度*/
	UINT32 dataOff=0;        /*数据偏移量*/
	UINT8 tempRet = 0u;
	UINT32 i = 0u;
	UINT32 j = 0u;
	UINT16 rowCount = 0u;
	UINT32 dataSize = 0u;          /*数据大小（字节数）*/
	UINT16 oneRowLen = 0u;

	/*初始化端口表结构体表*/
	/*读取数据*/
	tempRet=dquGetConfigDataNew((CHAR*)FSName, DSU_DEVICE_IP_CONFIG_TYPE, DUS_PORT_TABLE_ID, &pData ,&dataSize, &rowCount);
	if (tempRet!=1u)
	{
		/*数据读取失败*/
		rtVal = 0u;		
	}
	else
	{
		rtVal = 1u;
		/*防护数据为0*/
		if((0u != dataSize) && (0u != rowCount))
		{
			/*继续执行*/
			if (0u == (dataSize%rowCount))
			{
				oneRowLen = dataSize/rowCount;/*求一行的长度*/
				gPortDataLen = rowCount;  /*计算表格中数据的个数*/
				/*为结构体申请空间*/
				gpPortData = (PORT_DATA_STRUCT*)malloc(sizeof(PORT_DATA_STRUCT)*(gPortDataLen));

				if (NULL == gpPortData)
				{
					/*空间分配失败*/
					rtVal=0;
				}
				else
				{
					rtVal = 1;
					/*继续执行*/
					CommonMemSet(gpPortData,sizeof(PORT_DATA_STRUCT)*(gPortDataLen),0,sizeof(PORT_DATA_STRUCT)*(gPortDataLen));
				}
				if (1u == rtVal)
				{		
					/*通过循环将数据解析到结构体变量中，顺序与结构体定义相对应*/
					for (i=0;i<gPortDataLen;i++)
					{
						dataOff = 0u;

						gpPortData[i].IndexId = ShortFromChar(pData + dataOff);
						dataOff +=2;

						gpPortData[i].SrcDeviceType = LongFromChar(pData+dataOff);/*源设备类型*/
						dataOff +=4;
						gpPortData[i].DstDeviceType = LongFromChar(pData+dataOff);/*目的设备类型*/
						dataOff +=4;

						gpPortData[i].SrcFactoryNo = pData[dataOff]; /*源厂商编号*/
						dataOff++;

						gpPortData[i].DstFactoryNo = pData[dataOff]; /*目的厂商编号*/
						dataOff++;

						gpPortData[i].Tc1RedSendPort = ShortFromChar(pData+dataOff);/*Tc1红网发送端口*/
						dataOff += 2;

						gpPortData[i].Tc1RedRecvPort = ShortFromChar(pData+dataOff);/*Tc1红网接收端口*/
						dataOff += 2;

						gpPortData[i].Tc1BlueSendPort = ShortFromChar(pData+dataOff);/*Tc1蓝网发送端口*/
						dataOff += 2;

						gpPortData[i].Tc1BlueRecvPort = ShortFromChar(pData+dataOff);/*Tc1蓝网接收端口*/
						dataOff += 2;


						gpPortData[i].Tc2RedSendPort = ShortFromChar(pData+dataOff);/*Tc2红网发送端口*/
						dataOff += 2;

						gpPortData[i].Tc2RedRecvPort = ShortFromChar(pData+dataOff);/*Tc2红网接收端口*/
						dataOff += 2;

						gpPortData[i].Tc2BlueSendPort = ShortFromChar(pData+dataOff);/*Tc2蓝网发送端口*/
						dataOff += 2;

						gpPortData[i].Tc2BlueRecvPort = ShortFromChar(pData+dataOff);/*Tc2蓝网接收端口*/
						dataOff += 2;

						pData += oneRowLen;/*跳过一行长度*/
					}
				}
			}
			else
			{
				rtVal = 0u;
			}
		}
	}
	return rtVal;
}

/*
* 功能描述：根据设备名称获取红蓝网的IP地址和端口号
* 输入参数：UINT32 hlhtId 互联互通ID
*			UINT8 tc1tc2	头尾端
*			UINT8 redOrBlue	红蓝网
* 输出参数：UINT16* pPort	端口
* 返 回 值：1:正常返回 0：错误返回
*/
UINT8 GetPortFromPortData(UINT32 hlhtId, UINT8 tc1Ortc2,UINT8 redOrBlue,UINT16* pPort)
{
	UINT16 rtVal = 0u;
	UINT8 dstFactoryNo = 0u;/*目的厂商*/
	UINT16 iPortDataIndex = 0u;

	dstFactoryNo = (hlhtId >>29) & MASK_HLHLNO_FACTORY;/*根据互联互通ID获取厂商ID,高三位*/

	if (tc1Ortc2 > 0)
	{/*头尾端有效*/
		for (iPortDataIndex = 0u;iPortDataIndex<gPortDataLen;iPortDataIndex++)
		{
			if ((VOBC_ENDID_TC1 == tc1Ortc2) && (RED_NET == redOrBlue))
			{/*红网，头端*/
				if ((gpPortData[iPortDataIndex].SrcFactoryNo == FACTORY_NO_TCT) &&
					((gpPortData[iPortDataIndex].DstFactoryNo == dstFactoryNo)) &&
					((gpPortData[iPortDataIndex].SrcDeviceType == SYSTEM_TYPE_CI)) )
				{/*源厂商交控，目的厂商与互联互通编号中厂商编号一致，源设备类型为CI*/
					rtVal = 1u;
					*pPort = gpPortData[iPortDataIndex].Tc1RedRecvPort;
					break;
				}

			}
			else if ((VOBC_ENDID_TC2 == tc1Ortc2) && (RED_NET == redOrBlue))
			{/*红网，尾端*/
				if ((gpPortData[iPortDataIndex].SrcFactoryNo == FACTORY_NO_TCT) &&
					((gpPortData[iPortDataIndex].DstFactoryNo == dstFactoryNo)) &&
					((gpPortData[iPortDataIndex].SrcDeviceType == SYSTEM_TYPE_CI)) )
				{/*源厂商交控，目的厂商与互联互通编号中厂商编号一致，源设备类型为CI*/
					rtVal = 1u;
					*pPort = gpPortData[iPortDataIndex].Tc2RedRecvPort;
					break;
				}
			}
			else if ((VOBC_ENDID_TC1 == tc1Ortc2) && (BLUE_NET == redOrBlue))
			{/*篮网，头端*/
				if ((gpPortData[iPortDataIndex].SrcFactoryNo == FACTORY_NO_TCT) &&
					((gpPortData[iPortDataIndex].DstFactoryNo == dstFactoryNo)) &&
					((gpPortData[iPortDataIndex].SrcDeviceType == SYSTEM_TYPE_CI)) )
				{/*源厂商交控，目的厂商与互联互通编号中厂商编号一致，源设备类型为CI*/
					rtVal = 1u;
					*pPort = gpPortData[iPortDataIndex].Tc1BlueRecvPort;
					break;
				}
			}
			else if ((VOBC_ENDID_TC2 == tc1Ortc2) && (BLUE_NET == redOrBlue))
			{/*篮网，尾端*/
				if ((gpPortData[iPortDataIndex].SrcFactoryNo == FACTORY_NO_TCT) &&
					((gpPortData[iPortDataIndex].DstFactoryNo == dstFactoryNo)) &&
					((gpPortData[iPortDataIndex].SrcDeviceType == SYSTEM_TYPE_CI)) )
				{/*源厂商交控，目的厂商与互联互通编号中厂商编号一致，源设备类型为CI*/
					rtVal = 1u;
					*pPort = gpPortData[iPortDataIndex].Tc2BlueRecvPort;
					break;
				}
			}
			else
			{
				/*配置出问题了*/
			}
		}
	}


	return rtVal;
}