/********************************************************
*                                                                                                            
* �� �� ���� 2oo2InterfaceDataManage.c   
* ��Ȩ˵���� �������ؿƼ����޹�˾ 
* ����ʱ�䣺 2012-03-26
* ��    �ߣ� ������
* ���������� ��ȫ������ӿ����ݹ���
* �޸ļ�¼��   
*
********************************************************/ 

#include "../2oo2/2oo2InterfaceDataManage.h"
#include "MSCP_APP_API.h"
#include "stdarg.h"
#include "protclStruct.h"
#include "ParsePackCommon.h"
#include "dquCIInterFace.h"
#define PACK_LOG_ALARM_LENGTH ((UINT16)94)   /*ÿ���ڷְ����ѵ���־��ӡ����*/

/*2OO2*/ 
#ifdef PLATFORM_2OO2
extern FSFB_INFO_STRU  Fsfb_Info;                                         /*FSFBͨ��Э��ṹ��*/
extern ProtclConfigInfoStru gCbtcData;                                    /*ͨ��Э��ṹ��*/
extern QueueStruct canQueueRecv;                                          /*IO��CI���ݶ��� */
extern QueueStruct canQueueSend;                                          /*CI��IO���ݶ��� */
extern UINT32 CycleNum;                                                   /*ƽ̨���ں�*/
extern UINT8 Computer_Id_Local_File;			                          /*ƽ̨��������ID*/
extern UINT8 processor_mode;					                          /*ƽ̨����ģʽ*/
extern UINT8 FSIOLinkStatusInfo;                                          /*��������FSIO֮������״̬*/
extern UINT8 LEULinkStatusInfo;                                           /*������LEUͨ��״̬*/
extern UINT8 ZCLinkStatusInfo;                                            /*������ZCͨ��״̬*/
extern UINT8  gSheBeiType;                                                /*������豸����*/
extern UINT8  gSheBeiID;                                                  /*������豸ID*/
extern UINT8 LocalCiId;                                                   /*ϵͳ�����ļ��ж��������ID*/
extern QueueStruct logPrintfQue;                                          /*��־�洢���ݶ���*/
UINT8  gSheBeiType; 
#endif

PlatComStatusStruct PlatComStatusStru[TRANSMIT_OBJECT_SUM_MAX] = {NULL}; /*����ƽ̨ͨ�Ź�ϵ���ݽṹ��*/

QueueStruct* FsfbToCiDataQueue = NULL;		                             /*Fsfb��CI���ݶ���*/
QueueStruct* CiToFsfbDataQueue = NULL;		                             /*CI��Fsfb���ݶ���*/

QueueStruct* ProtocolToCiDataQueue = NULL;                               /*Э�鵽Ӧ�õ����ݶ���*/
QueueStruct* CiToProtocolDataQueue = NULL;                               /*CI��Э�����ݶ���*/

QueueStruct* AtsToCiDataQueue = NULL;		                             /*ATS��CI���ݶ���*/
QueueStruct* CiToAtsDataQueue = NULL;		                             /*CI��ATS���ݶ���*/
QueueStruct* XianDiToCiDataQueue = NULL;		                         /*�ֵص�CI���ݶ���*/
QueueStruct* CiToXianDiDataQueue = NULL;		                         /*CI���ֵ����ݶ���*/
QueueStruct* IoToCiDataQueue = NULL;		                             /*IO��CI���ݶ���*/
QueueStruct* CiToIoDataQueue = NULL;		                             /*CI��IO���ݶ���*/
QueueStruct* ZcToCiDataQueue = NULL;		                             /*ZC��CI���ݶ���*/
QueueStruct* CiToZcDataQueue = NULL;		                             /*CI��Zc���ݶ���*/
QueueStruct* LEUToCiDataQueue = NULL;		                             /*LEU��CI���ݶ���*/
QueueStruct* CiToLEUDataQueue = NULL;		                             /*CI��LEU���ݶ���*/
QueueStruct* VobcToCiDataQueue = NULL;		                             /*Vobc��CI���ݶ���*/
QueueStruct* CiToVobcDataQueue = NULL;		                             /*CI��Vobc���ݶ���*/
QueueStruct* PlatformToCiDataQueue = NULL;		                         /*Platform��CI���ݶ���*/
QueueStruct* CiToPlatformDataQueue = NULL;		                         /*CI��platform���ݶ���*/
QueueStruct* PscToCiDataQueue = NULL;		                             /*Psc��CI���ݶ���*/
QueueStruct* CiToPscDataQueue = NULL;		                             /*CI��Psc���ݶ���*/
QueueStruct* CiRecvFromCiDataQueue = NULL;	                             /*CI��������CI���ݶ���*/
QueueStruct* CiSendToCiDataQueue = NULL;	                             /*CI���͵�����CI���ݶ���*/

QueueStruct* AxisToCiDataQueue = NULL;		                             /*Axis��CI���ݶ���*/
QueueStruct* CiToAxisDataQueue = NULL;		                             /*CI��Axis���ݶ���*/
QueueStruct* OcToCiDataQueue = NULL;									 /*Oc��Ci�����ݶ���*/
QueueStruct* CiToOcDataQueue = NULL;									 /*Ci��Oc�����ݶ���*/

UINT32* CurCycleNum = NULL;					                             /*��ǰ���ں�*/
UINT32 StartCycleNum = 0u;                                                /*�ϵ����ں�*/
UINT8* PlatComputerId = NULL;                                            /*ƽ̨��������ID*/
UINT8* PlatRunMode = NULL;	                                             /*ƽ̨����ģʽ*/
UINT8* CiAndIoComState = NULL;                                           /*������IOͨ��״̬*/
UINT8* CiAndLeuComState = NULL;                                          /*������LEUͨ��״̬*/
UINT8* CiAndZcComState = NULL;                                           /*������ZCͨ��״̬*/
UINT8* CiAndCiComDeviceNum = NULL;                                       /*������������*/
UINT8* CiAndCiComIdBuf = NULL;                                           /*����������ͨ���豸ID*/
UINT8* CiAndCiComState = NULL;                                           /*����������ͨ��״̬*/
UINT8* PlatSheBeiType = NULL;                                            /*ƽ̨�����豸����*/
extern TransmitRelationDataStruct TransmitRelaDataStru[TRANSMIT_OBJECT_SUM_MAX];
extern UINT16 TransmitRelaDataCurSum;

#if (kPLATFORM == kVXWORKS)
ProtclConfigInfoStru gCbtcData;
QueueStruct canQueueRecv;            /*IO��CI���ݶ��� */
QueueStruct canQueueSend;            /*CI��IO���ݶ��� */
UINT32 CycleNum;                     /*ƽ̨���ں�*/
UINT8 Computer_Id_Local_File;			/*ƽ̨��������ID*/
FSFB_INFO_STRU  Fsfb_Info;           /*FSFBͨ��Э��ṹ��*/
UINT8 processor_mode;					/*ƽ̨����ģʽ*/
UINT8 FSIOLinkStatusInfo;            /*��������FSIO֮������״̬*/
QueueStruct logPrintfQue;            /*��־���ݶ��� */
#endif

/*
* ���������� ƽ̨��ʼ��
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨ATS��CI�ӿڶ���
*/
UINT8 Initial2oo2Interface(void)
{
	UINT8 rtn = 0u;
	UINT16 locTypeId = 0;
	UINT8 result = 0u;
	
    /*����ƽ̨���ݶ���ָ��*/
	
#ifdef PLATFORM_2OO2
    
	
	ProtocolToCiDataQueue = &gCbtcData.DataToApp;       /*��ʼ��Э�鵽CI���ݶ���*/
	CiToProtocolDataQueue = &gCbtcData.OutputDataQueue; /*��ʼ��CI��Э�����ݶ���*/
	
    FsfbToCiDataQueue = &Fsfb_Info.DataToApp;           /*��ʼ��Fsfb��CI���ݶ���*/
    CiToFsfbDataQueue = &Fsfb_Info.OutputDataQueue;     /*��ʼ��CI��Fsfb���ݶ���*/
	
	CurCycleNum = &CycleNum;                            /*��ʼ��ƽ̨����*/
	StartCycleNum = MSCP_GetSysCount(); /*�ϵ����ں�*/

    PlatComputerId = &Computer_Id_Local_File;
    PlatRunMode = &processor_mode;
	
	IoToCiDataQueue = &canQueueRecv;                   /*��ʼ��IO��CI���ݶ���*/
	CiToIoDataQueue = &canQueueSend;                   /*��ʼ��CI��IO���ݶ���*/
	
	AtsToCiDataQueue = NULL;					       /*��ʼ��ATS��CI���ݶ���*/
    CiToAtsDataQueue = CiToProtocolDataQueue;          /*��ʼ��CI��ATS���ݶ���*/
	
    XianDiToCiDataQueue = NULL;					       /*��ʼ��ATS��CI���ݶ���*/
	CiToXianDiDataQueue = CiToProtocolDataQueue;       /*��ʼ��CI��ATS���ݶ���*/

	
	ZcToCiDataQueue = NULL;							   /*��ʼ��ZC��CI���ݶ���*/
    CiToZcDataQueue = CiToProtocolDataQueue;           /*��ʼ��CI��ZC���ݶ���*/
	
	AxisToCiDataQueue = NULL;							 /*��ʼ��Axis��CI���ݶ���*/
	CiToAxisDataQueue = CiToProtocolDataQueue;           /*��ʼ��CI��Axis���ݶ���*/

	CiRecvFromCiDataQueue = NULL;					   /*��ʼ��CI��������CI���ݶ���*/
	CiSendToCiDataQueue = CiToProtocolDataQueue;       /*��ʼ��CI���͵�����CI���ݶ���*/
	
    VobcToCiDataQueue = NULL;						   /*��ʼ��Vobc��CI���ݶ���*/
	CiToVobcDataQueue = CiToProtocolDataQueue;         /*��ʼ��CI��Vobc���ݶ���*/

	PlatformToCiDataQueue = NULL;                           /*��ʼ��Psd��CI���ݶ���*/
	CiToPlatformDataQueue = CiToProtocolDataQueue;          /*��ʼ��CI��Psd���ݶ���*/

	PscToCiDataQueue = NULL;							/*��ʼ��PSC��CI���ݶ���*/
	CiToPscDataQueue = CiToProtocolDataQueue;			/*��ʼ��CI��PSC���ݶ���*/
	
    LEUToCiDataQueue = NULL;						   /*��ʼ��Leu��CI���ݶ���*/
	CiToLEUDataQueue = CiToFsfbDataQueue;			   /*��ʼ��CI��Leu���ݶ���*/
	
    CiAndIoComState = &FSIOLinkStatusInfo;             /*������IOͨ��״̬*/
	locTypeId = MSCP_GetOutnetID();
    /*LocalCiId = gSheBeiID;*/                             /*�����豸ID*/
    PlatSheBeiType = &gSheBeiType;                     /*�����豸����*/
	LocalCiId = (UINT8)(locTypeId & 0xFFu);                             /*�����豸ID*/
	gSheBeiType = (UINT8)((locTypeId >> 8u) & 0xFFu);
	
	OcToCiDataQueue = NULL;								/*��ʼ��Oc��Ci�����ݶ���*/
	CiToOcDataQueue = CiToProtocolDataQueue;			/*��ʼ��Ci��Oc�����ݶ���*/

	if (1U != QueueInitial(&logPrintfQue, 500000U))       /*��ʼ����־�������*/
	{
		debug_infor_printf((const)"canQueueRecv init error \n");
	}

#if 0
    /*������LEU��ZC��CI�ȵ�ͨ��״̬����*/
    if (TransmitRelaDataCurSum < TRANSMIT_OBJECT_SUM_MAX)
    {
        for (ii = 0u; ii < TRANSMIT_OBJECT_SUM_MAX; ii++)
        {
            PlatComStatusStru[ii].SystemType = &(ComDevStatusInfo[ii].DeviceTypeOther);
            PlatComStatusStru[ii].SystemNum = &(ComDevStatusInfo[ii].ComDevSum);
            for (jj = 0u; jj < TRANSMIT_OBJECT_DEVICE_SUM_MAX; jj++)
            {
                PlatComStatusStru[ii].SystemIdBuf[jj] = &(ComDevStatusInfo[ii].ComDevIdBuf[jj]);
                PlatComStatusStru[ii].CommunicationStatusBuf[jj] = &(ComDevStatusInfo[ii].ComDevIdComStatusBuf[jj]);
            }
        }
        
    }
    else
    {
        
    }
#endif

#endif
	result = HLHTCiIdMapInitial();
	if (CI_SUCCESS == result)
	{
		rtn = 1;
	}

	return rtn;

}

/*
* ���������� ��ȡAxis��CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: Axis��CI�ӿڶ���
*/
QueueStruct* Get2oo2AxisToCiDataQueue(void)
{
	return AxisToCiDataQueue;
}

/*
* ���������� ��ȡƽ̨ͨ��Э�鵽CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨ͨ��Э�鵽CI�ӿڶ���
*/
QueueStruct* Get2oo2ProtocolToCiDataQueue(void)
{

    return ProtocolToCiDataQueue;
}

/*
* ���������� ��ȡƽ̨CI��ͨ��Э��ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨CI��ͨ��Э��ӿڶ���
*/
QueueStruct* Get2oo2CiToProtocolDataQueue(void)
{

    return CiToProtocolDataQueue;
}

/*
* ���������� ��ȡƽ̨Fsfb��CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨CI��Fsfb�ӿڶ���
*/
QueueStruct* Get2oo2FsfbToCiDataQueue(void)
{

    return FsfbToCiDataQueue;
}

/*
* ���������� ��ȡƽ̨CI��FsfbP�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨CI��Fsfb�ӿڶ���
*/
QueueStruct* Get2oo2CiToFsfbDataQueue(void)
{

    return CiToFsfbDataQueue;
}



/*
* ���������� ��ȡƽ̨ATS��CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨ATS��CI�ӿڶ���
*/
QueueStruct* Get2oo2AtsToCiDataQueue(void)
{

    return AtsToCiDataQueue;
}


/*
* ���������� ��ȡƽ̨CI���ֵؽӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨CI���ֵؽӿڶ���
*/
QueueStruct* Get2oo2CiToXianDiDataQueue(void)
{
    return CiToXianDiDataQueue;
}

/*
* ���������� ��ȡƽ̨�ֵص�CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨�ֵص�CI�ӿڶ���
*/
QueueStruct* Get2oo2XianDiToCiDataQueue(void)
{

    return XianDiToCiDataQueue;
}


/*
* ���������� ��ȡƽ̨CI��ATS�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨CI��ATS�ӿڶ���
*/
QueueStruct* Get2oo2CiToAtsDataQueue(void)
{
    return CiToAtsDataQueue;
}


/*
* ���������� ��ȡZC��CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ZC��CI�ӿڶ���
*/
QueueStruct* Get2oo2ZcToCiDataQueue(void)
{
	return ZcToCiDataQueue;
}


/*
* ���������� ��ȡCI��ZC�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI��ZC�ӿڶ���
*/
QueueStruct* Get2oo2CiToZcDataQueue(void)
{
	return CiToZcDataQueue;
}



/*
* ���������� ��ȡƽ̨CI������CI���սӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨CI����CI�ӿڶ���
*/
QueueStruct* Get2oo2CiRecvCiDataQueue(void)
{
	return CiRecvFromCiDataQueue;
}


/*
* ���������� ��ȡCI���͵�����CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI����CI�ӿڶ���
*/
QueueStruct* Get2oo2CiSendCiDataQueue(void)
{
	return CiSendToCiDataQueue;
}

/*
* ���������� ��ȡƽ̨IO��CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨IO��CI�ӿڶ���
*/
QueueStruct* Get2oo2IoToCiDataQueue(void)
{
	return IoToCiDataQueue;
}


/*
* ���������� ��ȡƽ̨CI��IO�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: ƽ̨CI��IO�ӿڶ���
*/
QueueStruct* Get2oo2CiToIoDataQueue(void)
{
    return CiToIoDataQueue;
}

/*
* ���������� ��ȡLeu��CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: Leu��CI�ӿڶ���
*/
QueueStruct* Get2oo2LEUToCiDataQueue(void)
{
	return LEUToCiDataQueue;
}

/*
* ���������� ��ȡCI��Leu�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI��Leu�ӿڶ���
*/
QueueStruct* Get2oo2CiToLEUDataQueue(void)
{
	return CiToLEUDataQueue;
}

/*
* ���������� ��ȡCI��Vobc�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI��Vobc�ӿڶ���
*/
QueueStruct* Get2oo2VobcToCiDataQueue(void)
{
    return VobcToCiDataQueue;
}

/*
* ���������� ��ȡCI��Vobc�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI��Vobc�ӿڶ���
*/
QueueStruct* Get2oo2CiToVobcDataQueue(void)
{
    return CiToVobcDataQueue;
}


/*
* ���������� ��ȡCI��Axis�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI��Axis�ӿڶ���
*/
QueueStruct* Get2oo2CiToAxisDataQueue(void)
{
	return CiToAxisDataQueue;
}

/*
* ���������� ��ȡCI��Oc�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI��Oc�ӿڶ���
*/
QueueStruct* Get2oo2CiToOcDataQueue(void)
{
	return CiToOcDataQueue;
}


/*
* ���������� ��ȡPSD��CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: PSD��CI�ӿڶ���
*/
QueueStruct* Get2oo2PlatformToCiDataQueue(void)
{
    return PlatformToCiDataQueue;
}

/*
* ���������� ��ȡCI��Psd�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI��Psd�ӿڶ���
*/
QueueStruct* Get2oo2CiToPlatformDataQueue(void)
{
    return CiToPlatformDataQueue;
}

/*
* ���������� ��ȡPsc��CI�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: Psc��CI�ӿڶ���
*/
QueueStruct* Get2oo2PscToCiDataQueue(void)
{
	return PscToCiDataQueue;
}

/*
* ���������� ��ȡCI��Psc�ӿڶ���
* ����˵���� void
* ����ֵ  �� NULL: ��ȡ����ʧ��
*           !NULL: CI��Psc�ӿڶ���
*/
QueueStruct* Get2oo2CiToPscDataQueue(void)
{
	return CiToPscDataQueue;
}

/*
* ���������� ��ȡƽ̨���ں�
* ����˵���� void
* ����ֵ  �� 0: ��ȡ����ʧ��
*           >0: ƽ̨���ں�
*/
UINT32 Get2oo2CurCycleNum(void)
{
#if (kPLATFORM==kVXWORKS)
	return MSCP_GetSysCount();
#else
	return *CurCycleNum;
#endif
	
}


/*
* ���������� ��ȡƽ̨����ID
* ����˵���� void
* ����ֵ  �� 0: ��ȡ����ʧ��
*           >0: ƽ̨����ID
*/
UINT8 Get2oo2ComputerId(void)
{
#if (kPLATFORM==kVXWORKS)
	return MSCP_GetBoardID();
#else
	return *PlatComputerId;
#endif

	
}


/*
* ���������� ��ȡƽ̨��������ģʽ
* ����˵���� void
* ����ֵ  �� 0: ��ȡ����ʧ��
*           >0: ƽ̨��������ģʽ
*/
UINT8 Get2oo2ProcessMode(void)
{
#if (kPLATFORM==kVXWORKS)
	UINT8 rtn = 0u;
	UINT8 cutSysState = 0U;
	ENUM_4x2_STATE_T state = ENUM_4x2_SHUTDOWN;
	state = MSCP_GetLastLocal2x2State();
	cutSysState = MSCP_2x2_GetSwitchFlag();
	if (ENUM_4x2_MASTER == state)
	{
		rtn = 0xA0;
	}
	else if ((ENUM_4x2_SLAVE == state)||(1==cutSysState))
	{
		rtn = 0x50;
	}
	else
	{
		rtn = 0U;
	}

	debug_infor_printf((const)"MSCP_GetLastLocal2x2State %x", state);
	return rtn;
#else

	return *PlatRunMode;
#endif
}

/*
* ���������� ��ȡƽ̨������������ģʽ
* ����˵���� void
* ����ֵ  �� 0: ��ȡ����ʧ��
*           >0: ƽ̨��������ģʽ
*/
UINT8 Get2oo2OhterProcessMode(void)
{
	UINT8 rtn = 0u;
#if (kPLATFORM==kVXWORKS)
	
	UINT8 cutSysState = 0U;
	ENUM_4x2_STATE_T state = ENUM_4x2_SHUTDOWN;
	state = MSCP_GetLastPartner2x2State();
	cutSysState = MSCP_2x2_GetSwitchFlag();
	if (ENUM_4x2_MASTER == state)
	{
		rtn = 0x0A;
	}
	else if ((ENUM_4x2_SLAVE == state) || (1 == cutSysState))
	{
		rtn = 0x05;
	}
	else
	{
		rtn = 0U;
	}
	debug_infor_printf((const)"MSCP_GetLastPartner2x2State %x", state);
	return rtn;
#else

	return rtn;
#endif
}
/*
* ���������� ��ȡCI�������豸ͨ�ŵĸ��豸����
* ����˵���� const UINT8 systemType, ϵͳ����
* ����ֵ  �� ͨ���豸��Ŀ
*/
UINT8 Get2oo2CiAndOtherSysCommNum(const UINT8 systemType)
{
    UINT8 ii = 0u;
    UINT8 returnValue = 0u;

    for (ii = 0u; ii < TRANSMIT_OBJECT_SUM_MAX; ii++)
    {
        if (TransmitRelaDataStru[ii].SystemType == systemType)
        {
            returnValue = TransmitRelaDataStru[ii].DeviceSum;
            
            break;
        }
    }

	return returnValue;
}



/*
* ���������� ��ȡCI��IOͨ��״̬
* ����˵���� void
* ����ֵ  �� 
*/
UINT8 Get2oo2CiAndIoCommState(void)
{
	return *CiAndIoComState;
}

/*
* ���������� ��ȡ��ʼ���ں�
* ����˵���� void
* ����ֵ  �� 
*/
UINT32 Get2oo2StartCycleNum(void)
{
	return StartCycleNum;
}

/*
* ���������� ��ȡƽ̨�豸����
* ����˵���� void
* ����ֵ  �� 
*/
UINT8 Get2oo2SheBeiType(void)
{
#ifdef PLATFORM_2OO2

    return gSheBeiType;

#endif

	
}


#if (kPLATFORM==kVXWORKS)
/*��ά����������־��ӡ-�ַ�������*/
void debug_infor_printf(char* debug_string, ...)
{
	UINT32 wdataLength = 0U;
	va_list argList;
	CM_CHAR msgBuf[1024 + 256];
	va_start(argList, debug_string);
	(void)vsnprintf(msgBuf, sizeof(msgBuf), debug_string, argList);
	va_end(argList);

	wdataLength = (UINT32)strlen(msgBuf);
	msgBuf[wdataLength] = '\n';
	/*��־���䵽������*/
	(void)QueueWrite((wdataLength+ (UINT32)1u), (UINT8*)msgBuf, &logPrintfQue);
}

/*��ά����������־��ӡ-�ֽ���������*/
void debug_out_array(UINT8 mode, UINT8* debug_array, UINT16 len)
{
	UINT16 i = 0U;
	UINT16 indexChar = 0U;
	CM_CHAR msgBuf[UDPSEND_BUF_MAX * 3U] = { '\0' };
	CM_CHAR byteTochar[6] = { '\0' };
	UINT16 wdataLength = 0U;
	/*һ��8�����ֽ�ת��Ϊ2���ַ��������ֽ����鲻�ܳ����������ֵ*/
	if ((NULL != debug_array) && (UDPSEND_BUF_MAX > len))
	{
		if (0xAA == mode)
		{
#if 0
			for (i = 0U; i < len; i++)
			{
				/*���ֽ�����(16����)ת��Ϊ�ַ����飬�ٴ������*/
				/*���ֽڲ�����λ����'0'*/
				if (i == len - 1)
				{
					indexChar += sprintf(msgBuf + indexChar, "%02x", debug_array[i]);
				}
				else
				{
					indexChar += sprintf(msgBuf + indexChar, "%02x-", debug_array[i]);
				}
			}
			wdataLength = (UINT16)strlen(msgBuf);

#endif 
			/*����####+2�ֽڳ��ȣ���������ʶ��####Ȼ�����*/
			byteTochar[0] = '#';
			byteTochar[1] = '#';
			byteTochar[2] = '#';
			byteTochar[3] = '#';
			ShortToChar(len, &byteTochar[4]);
			(void)QueueWrite(6, (UINT8*)byteTochar, &logPrintfQue);
			(void)QueueWrite(len, (UINT8*)debug_array, &logPrintfQue);
		}
		else
		{
			; /*������*/
		}

	}
	else
	{
		; /*������*/
	}
}

void DarkInfoPack(UINT16 mode, UINT8* debug_array, UINT16 len)
{
	if (NULL != debug_array)
	{
		MSCP_PFM_LOG_DARK(0x55, mode, len, debug_array);
	}
	else
	{
		; /*������*/
	}
}
#endif

/*������������*/
UINT8 OutnetSafeRecv(UINT16* srcTypeId, UINT16* srcLogicId, UINT8* data, UINT32* dataLen, UINT32 dataMaxLen)
{
	UINT8 rtn = 0u; /*����ֵ*/
	
	UINT8 lifeTime = 0u; /*����ʱ��*/

	rtn = MSCP_AppRecvMsg(ENUM_MSG_OUTNET_SAFE, srcTypeId, srcLogicId, &lifeTime, data, dataLen, dataMaxLen);

	return rtn;
}

/*������ȫ����*/
UINT8 InnetSafeRecv(UINT16* srcTypeId, UINT16* srcLogicId, UINT8* data, UINT32* dataLen, UINT32 dataMaxLen)
{
	UINT8 rtn = 0u; /*����ֵ*/

	UINT8 lifeTime = 0u; /*����ʱ��*/

	rtn = MSCP_AppRecvMsg(ENUM_MSG_INNET_MAINTENANCE, srcTypeId, srcLogicId, &lifeTime, data, dataLen, dataMaxLen);

	return rtn;
}


/*������������*/
UINT8 OutnetSafeSend(UINT16 dstTypeId, UINT16 dstLogicId, UINT8* data, UINT32 dataLen)
{
	UINT8 rtn = 0u; /*����ֵ*/

	/*��ʵ����*/
	rtn = MSCP_AppSendMsg(ENUM_MSG_OUTNET_SAFE, dstTypeId, dstLogicId, data, dataLen);

	return rtn;
}

/*����ά�������*/
UINT8 InnetSafeSend(UINT16 dstTypeId, UINT16 dstLogicId, UINT8* data, UINT32 dataLen)
{
	UINT8 rtn = 0u; /*����ֵ*/

	/*��ʵ����*/
	rtn = MSCP_AppSendMsg(ENUM_MSG_INNET_MAINTENANCE, dstTypeId, dstLogicId, data, dataLen);

	return rtn;
}

UINT32 InnetRawIoRecv(UINT8* data, UINT32 dataMaxLen)
{
	UINT32 dataLen = 0u; /*����ֵ*/

#if (kPLATFORM==kVXWORKS)
	
	UINT16 srcTypeId = 0u; /*Դ����ID*/

	dataLen = MSCP_InnetRaw_AppRecv(1, &srcTypeId, data, dataMaxLen);
#else
	QueueStruct* rawDataQueue = Get2oo2IoToCiDataQueue();

	UINT32 dataQueueTotalLen = 0;											/*���������ܳ���*/
	
	if (rawDataQueue != NULL)
	{
		/*��ȡ���������ܳ���*/
		dataQueueTotalLen = QueueStatus(rawDataQueue);
		
        if (dataQueueTotalLen >= dataMaxLen)
        {/*IO�������� >= IO���������Ч����ʱ:��ȡIO���������Ч����*/
            
			/*�Ӷ����ж�������*/
			dataQueueTotalLen = dataMaxLen;
			(void)QueueRead(dataQueueTotalLen, data, rawDataQueue);

		}
        else if(0U < dataQueueTotalLen)
        {/*0 < dataQueueTotalLen < maxIoDataLen*/
            
			/*�Ӷ����ж�������*/
			(void)QueueRead((UINT16)dataQueueTotalLen, data, rawDataQueue);
        }
		else
		{/*dataQueueTotalLen == 0*/
		}
#if 0
		/*����IO��CI����֡*/
        ParseIoToCiDataFrame(tempDataBuf, (UINT16)dataQueueTotalLen);
#endif
        /*��ȡ���������ܳ���*/
        dataQueueTotalLen = QueueStatus(rawDataQueue);

		if (0U < dataQueueTotalLen)
		{
			(void)QueueClear(rawDataQueue);	/*�����*/

			dataLen = CI_ERROR;
		}
		else
		{
			dataLen = CI_SUCCESS;
		}
	}
#endif
	return dataLen;
}

UINT8 InnetRawIoSend(UINT8* data, UINT32 dataLen)
{
	UINT8 rtn = 0u; /*����ֵ*/
#if (kPLATFORM==kVXWORKS)
	rtn = MSCP_InnetRaw_AppSend(1, 0x0901, data, dataLen);
#else
	QueueStruct* rawDataQueueToSend = Get2oo2CiToIoDataQueue();/*�������ݵĶ���*/
	if (rawDataQueueToSend != NULL)
	{
		(void)QueueClear(rawDataQueueToSend);					/*�����*/

		if (0U == QueueWrite(dataLen, data, rawDataQueueToSend))
		{ /*д����ʧ��*/

			(void)QueueClear(rawDataQueueToSend);   /*�����*/

			debug_infor_printf((const)"Error: write rawDataQueueToSend fail, QueueWrite = 0");

			rtn = CI_ERROR;	                    
		}
		else
		{
			;
		}
	}
	else
	{
		; /*������*/
	}
#endif
	return rtn;
}

UINT32 InnetRawLeuRecv(UINT16* srcTypeId, UINT8* data, UINT32 dataMaxLen)
{
	UINT32 dataLen = 0u; /*����ֵ*/
#if (kPLATFORM==kVXWORKS)
	dataLen = MSCP_InnetRaw_AppRecv(2, srcTypeId, data, dataMaxLen);
#else

#endif
	return dataLen;
}

UINT8 InnetRawLeuSend(UINT16 dstTypeId, UINT8* data, UINT32 dataLen)
{
	UINT8 rtn = 0u; /*����ֵ*/
#if (kPLATFORM==kVXWORKS)
	rtn = MSCP_InnetRaw_AppSend(2, dstTypeId, data, dataLen);
#else

#endif
	return rtn;
}


/*
* ���������� ����ƽ̨����֡����
* ����˵���� OUT UINT8* CC1_data  CC1��������
*          OUT UINT8* CC2_data  CC2��������
*          OUT UINT8* SEB1_data  SEB1��������
*          OUT UINT8* SEB2_data  SEB2��������
*           OUT UINT8* CC1_commFFlag  CC1�������ݱ�־
*           OUT UINT8* CC2_commFFlag  CC2�������ݱ�־
*           OUT UINT8* SEB1_commFFlag  SEB1�������ݱ�־
*           OUT UINT8* SEB2_commFFlag  SEB2�������ݱ�־
* ����ֵ  ����
* BY LC
*/
void InnetHeartFrameRecv(OUT UINT8* CC1_data, OUT UINT8* CC2_data, OUT UINT8* SEB1_data, OUT UINT8* SEB2_data, OUT UINT8* CC1_commFFlag, OUT UINT8* CC2_commFFlag, OUT UINT8* SEB1_commFFlag, OUT UINT8* SEB2_commFFlag)
{
	UINT32  remainCount = UINT32_MAX;
	UINT8 count = 0U;
	UINT8 tmpDataBuff[HEART_FRAME_MAX_LENGTH] = { 0U };
	UINT8 rtnDataBuff_CCA[HEART_FRAME_MAX_LENGTH] = { 0U };
	UINT8 rtnDataBuff_CCB[HEART_FRAME_MAX_LENGTH] = { 0U };
	UINT8 rtnDataBuff_SEBA[HEART_FRAME_MAX_LENGTH] = { 0U };
	UINT8 rtnDataBuff_SEBB[HEART_FRAME_MAX_LENGTH] = { 0U };
	UINT32 cycNew_CCA = 0U;
	UINT32 cycNew_CCB = 0U;
	UINT32 cycNew_SEBA = 0U;
	UINT32 cycNew_SEBB = 0U;
	UINT32 tmpCyc = 0U;

	while (0U != remainCount)
	{
		/*�������*/
		(void)CommonMemSet(&tmpDataBuff, HEART_FRAME_MAX_LENGTH, 0U, HEART_FRAME_MAX_LENGTH);
		
#if (kPLATFORM == kVXWORKS)

		remainCount = MSCP_OutNetHeart_AppRecv(OUT &tmpDataBuff, HEART_FRAME_MAX_LENGTH);
		debug_infor_printf((const)"\nBoard ID: %d, Printf Heart DataBuff from [MSCP_OutNetHeart_AppRecv], remainCount = %d", tmpDataBuff[2], remainCount);
		debug_out_array(0xAA, tmpDataBuff, HEART_FRAME_REAL_LENGTH);
#endif
		
		
		if (0U != tmpDataBuff[2])
		{
			count++;

			/*����CC1����*/
			if (BOARD_TYPE_CCUA == tmpDataBuff[2])
			{
				tmpCyc = LongFromChar(&tmpDataBuff[3]);	/*��ȡ��֡���ں�*/
				if (tmpCyc > cycNew_CCA)
				{
					cycNew_CCA = tmpCyc;
					(void)CommonMemCpy((void*)&rtnDataBuff_CCA, HEART_FRAME_MAX_LENGTH, (const void*)&tmpDataBuff, HEART_FRAME_MAX_LENGTH);

				}
				*CC1_commFFlag = CI_SUCCESS;
			}
			/*����CC2����*/
			else if (BOARD_TYPE_CCUB == tmpDataBuff[2])
			{
				tmpCyc = LongFromChar(&tmpDataBuff[3]);	/*��ȡ��֡���ں�*/
				if (tmpCyc > cycNew_CCB)
				{
					cycNew_CCB = tmpCyc;
					(void)CommonMemCpy((void*)&rtnDataBuff_CCB, HEART_FRAME_MAX_LENGTH, (const void*)&tmpDataBuff, HEART_FRAME_MAX_LENGTH);
				}
				*CC2_commFFlag = CI_SUCCESS;
			}
			/*����SEB1����*/
			else if (BOARD_TYPE_SEBA == tmpDataBuff[2])
			{
				tmpCyc = LongFromChar(&tmpDataBuff[3]);	/*��ȡ��֡���ں�*/
				if (tmpCyc > cycNew_SEBA)
				{
					cycNew_SEBA = tmpCyc;
					(void)CommonMemCpy((void*)&rtnDataBuff_SEBA, HEART_FRAME_MAX_LENGTH, (const void*)&tmpDataBuff, HEART_FRAME_MAX_LENGTH);
				}
				*SEB1_commFFlag = CI_SUCCESS;
			}
			/*����SEB2����*/
			else if (BOARD_TYPE_SEBB == tmpDataBuff[2])
			{
				tmpCyc = LongFromChar(&tmpDataBuff[3]);	/*��ȡ��֡���ں�*/
				if (tmpCyc > cycNew_SEBB)
				{
					cycNew_SEBB = tmpCyc;
					(void)CommonMemCpy((void*)&rtnDataBuff_SEBB, HEART_FRAME_MAX_LENGTH, (const void*)&tmpDataBuff, HEART_FRAME_MAX_LENGTH);
				}
				*SEB2_commFFlag = CI_SUCCESS;
			}
			else
			{
				/*������Դ�忨�Ƿ���������*/
			}
			
			/*�ѽ��ܵ�������ֵ�� �˳�ѭ��*/
			if (HEART_FRAME_MAX_SUM == count)
			{
				remainCount = 0U;
			}
		}
		else
		{
			remainCount = 0U;
		}
	}

	(void)CommonMemCpy((void*)CC1_data, HEART_FRAME_MAX_LENGTH, (const void*)&rtnDataBuff_CCA, HEART_FRAME_MAX_LENGTH);
	(void)CommonMemCpy((void*)CC2_data, HEART_FRAME_MAX_LENGTH, (const void*)&rtnDataBuff_CCB, HEART_FRAME_MAX_LENGTH);
	(void)CommonMemCpy((void*)SEB1_data, HEART_FRAME_MAX_LENGTH, (const void*)&rtnDataBuff_SEBA, HEART_FRAME_MAX_LENGTH);
	(void)CommonMemCpy((void*)SEB2_data, HEART_FRAME_MAX_LENGTH, (const void*)&rtnDataBuff_SEBB, HEART_FRAME_MAX_LENGTH);

}


/*
* ���������� ��ȡ��ϵ�յ�����ϵIO����״̬
* ����˵����UINT8* ioData ��ȡ��IO����
* ����ֵ  �� CI_SUCCESS ��ȡ�ɹ�
*           CI_ERROR  ��ȡʧ��
* BY LC
*/
UINT8 InnetChannelIODataRecv(UINT8* ioData)
{
	UINT8 retVal = CI_ERROR;
	UINT8 ioRecvFlag = 0U;
	UINT8 recvIOData[IO_DATA_LENGTH] = { 0U };/*�յ���40�ֽ�GPIO���ݣ���50�ֽڴ���*/
	UINT8 paraseIOData[IO_DATA_DATA_LENGTH] = { 0U };/*ʵ������Ϊ4��PU4��״̬ 16�ֽ�*/

#if (kPLATFORM == kVXWORKS)
	if (NULL != ioData)
	{
		/*��ȡ��ϵPU�յ���IO����*/
		ioRecvFlag = MSCP_2x2_GetLastIoInput(recvIOData, (UINT16)IO_DATA_LENGTH);
		if (0x55U == ioRecvFlag)
		{
			debug_infor_printf((const)"\nRECV IOData:");
			debug_out_array(0xAA, recvIOData, (UINT16)IO_DATA_LENGTH);
			(void)CommonMemCpy(&paraseIOData[0], 4U, &recvIOData[0], 4U);/*PU1����*/
			(void)CommonMemCpy(&paraseIOData[4], 4U, &recvIOData[10], 4U);/*PU2����*/
			(void)CommonMemCpy(&paraseIOData[8], 4U, &recvIOData[20], 4U);/*PU3����*/
			(void)CommonMemCpy(&paraseIOData[12], 4U, &recvIOData[30], 4U);/*PU4����*/
			debug_infor_printf((const)"\nPUA IOData:%d-%d-%d-%d  %d-%d-%d-%d", paraseIOData[0], paraseIOData[1], paraseIOData[2], paraseIOData[3], paraseIOData[4], paraseIOData[5], paraseIOData[6], paraseIOData[7]);
			debug_infor_printf((const)"\nPUB IOData:%d-%d-%d-%d  %d-%d-%d-%d", paraseIOData[8], paraseIOData[9], paraseIOData[10], paraseIOData[11], paraseIOData[12], paraseIOData[13], paraseIOData[14], paraseIOData[15]);
			retVal = CI_SUCCESS;
		}
		else
		{
			/*����ʧ��*/
		}
	}

	if (CI_SUCCESS == retVal)
	{
		(void)CommonMemCpy(ioData, IO_DATA_DATA_LENGTH, paraseIOData, IO_DATA_DATA_LENGTH);
	}
	else
	{
		/*����ʧ��*/
	}
#endif

	return retVal;
}





/*
* ���������� �������е����ݴ����ȥ
* ����˵���� UINT8* thisString, ����������
* ����ֵ  �� 0: ��ӡʧ��
*			 1: ��ӡ�ɹ�
*/
void SendOut_LogBuff()
{
#if (kPLATFORM==kVXWORKS)

	UINT16 ii = 0U;
	UINT16 subPackLength = 0U;  /*ÿ���ְ������ݳ���*/
	UINT8 pData[UDPSEND_BUF_MAX + 1] = { 0U };
	UINT16 wCount = 0U;   /*��������*/
	UINT16 wTotalNum = 0U;  /*�ܰ���*/
	UINT16 wLastLength = 0U;
	UINT16 wNowNum = 1U;   /*��ǰ���������*/
	UINT32 dataLength = 0U;  /*��������־�ܳ���*/
	UINT8 retval = CI_ERROR;
	UINT32 wCycleNum = 0U;
	UINT32  wCRCCheck = 0U;
	UINT32 curCycNum = 0U;

	curCycNum = Get2oo2CurCycleNum();
	subPackLength = GetLogSubPackDataLength();

	/****���������贫�����־��С******/
	wCycleNum = Get2oo2CurCycleNum();
	dataLength = QueueStatus(&logPrintfQue) + PACK_LOG_ALARM_LENGTH; /*��־���г���+���ӡ�ķְ���¼����*/
	wCount = (UINT16)(dataLength / (subPackLength - 13U));  /* ÿ������13�ֽ��� ��ͷ��CRC  9+4*/
	wLastLength = (UINT16)(dataLength % (subPackLength - 13U));  /* ��������*/
	if (0U == wLastLength)
	{
		wTotalNum = wCount;
	}
	else
	{
		wTotalNum = wCount + 1U;
	}
	
	debug_infor_printf((const)"\nSystemCycleNum:%10d,Pack %2d Frame Log,Full Pack Length Is %4d,Last Pack Length Is %4d", wCycleNum, wTotalNum, subPackLength, wLastLength);

	/*��־�ֳɹ涨����һ������*/
	for (ii = 0U; ii < wCount; ii++)
	{
		(void)CommonMemSet(pData, (UINT32)sizeof(pData), 0u, (UINT32)sizeof(pData));  /*���pData*/

		LongToChar(wCycleNum, &pData[0]);  /*���ں� 4�ֽ�*/

		pData[4] = 0x10;  /*������*/

		pData[5] = (UINT8)wTotalNum;  /*�ܰ���*/

		pData[6] = (UINT8)wNowNum++;  /*��������*/

		ShortToChar(subPackLength - 13U, &pData[7]);  /*�������ݳ��� 2�ֽ�*/

		retval = QueueRead(subPackLength - 13U, &pData[9], &logPrintfQue); /*����������*/
		
		wCRCCheck = Tioc_CommonCrc32(&pData[0], (UINT32)((UINT32)subPackLength - 4U));

		LongToChar(wCRCCheck, &pData[subPackLength - 4U]);  /*CRC 4�ֽ�*/

		if (CI_SUCCESS == retval)
		{
			/*���ͷְ�*/
			MSCP_LogBin(pData, subPackLength);
		}
	}
	/*������������*/
	wLastLength = (UINT16)QueueStatus(&logPrintfQue);
	(void)CommonMemSet(pData, (UINT32)sizeof(pData), 0u, (UINT32)sizeof(pData));  /*���pData*/

	LongToChar(wCycleNum, &pData[0]);  /*���ں� 4�ֽ�*/

	pData[4] = 0x10;  /*������*/

	pData[5] = (UINT8)wTotalNum;  /*�ܰ���*/

	pData[6] = (UINT8)wNowNum++;  /*��������*/

	ShortToChar((UINT16)dataLength, &pData[7]);  /*�������ݳ��� 2�ֽ�*/

	retval = QueueRead(wLastLength, &pData[9], &logPrintfQue); /*����������*/

	wCRCCheck = Tioc_CommonCrc32(&pData[0], (UINT32)((UINT32)wLastLength + 9U));/*��CRCУ����*/

	LongToChar(wCRCCheck, &pData[wLastLength +9U]);  /*CRC 4�ֽ�*/

	if (CI_SUCCESS == retval)
	{
		/*���ͷְ�*/
		MSCP_LogBin(pData, wLastLength+13U);

	}

#endif
	/*������ɺ���ն���*/
	(void)QueueClear(&logPrintfQue);
}


