⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 caproc.c

📁 有线电视系统前端设备复用器原代码。 用语接受卫星信号
💻 C
📖 第 1 页 / 共 4 页
字号:
/*-------------------------------------------------------------------------------------*/
/*caproc.c                                                                             */
/*-------------------------------------------------------------------------------------*/
#include "psos.h"
#include <probe.h>
#include <prepc.h>

#include "pna.h"
#include "sys_conf.h"
#include "caproc.h"
#include "repid.h"
#include "structure.h"
extern void my_Delay(unsigned long int dly_ms);
CA_CONFIG g_ca_config;
int       g_has_valid_ca_config;
int       g_has_valid_ca1_config;
int       g_has_valid_ca2_config;

unsigned long g_qid_ca;

unsigned char careportlogbuffer[256];

unsigned long g_tid_ecmg = 0;
unsigned long g_tid_ecmg1 = 0;
unsigned long g_tid_ecmg2 = 0;


unsigned long g_tid_emmg = 0;
unsigned long g_tid_emmg1 = 0; /*Add by xu*/
unsigned long g_tid_emmg2 = 0; /*Add by xu*/


unsigned long g_qid_ecmg = 0;
unsigned long g_qid_ecmg1 = 0;
unsigned long g_qid_ecmg2 = 0;

unsigned long g_CW_Qint;

unsigned long g_CW_TkInt = 0;

int g_emmg_suspend = 0;
int g_emmg1_suspend = 0;
int g_emmg2_suspend = 0;

extern unsigned long    ulTidEmmTcp[3];

extern	CA_ECM_DATA	g_ecm_data[3];

extern  CA_ECMG_STREAM_STATUS_LIST g_ecmg_stream_status_list[3];

extern  System_Data SystemData;

extern unsigned char g_current_cp_number;
extern unsigned short g_ecmg_waitcommand[3];
extern int                   g_Emmg_connected[3];

extern int g_first_CW;
int g_IsResetStatue = 0;

unsigned long ulMultEMM;

#define MAX_EMMG_CONNECTED  3
extern CA_EMM_TCPMEMBER    g_Emm_TcpMember[MAX_EMMG_CONNECTED];
extern unsigned long    ulTidEmmTcp[3];

int      bJudgeConfig = 0;

extern int g_ecmg_connected[3];
extern int g_Emmg_connected[3];

/*将此数据存到加扰设置中做为一个版本标识*/
/*由此标识决定是否取相应加扰设置的内容*/
/*当加扰结构改变时,可相应更改此标识*/
unsigned long ulDateVersion = 200508022;

#define EMMG_STACK_LEN  8192
#define ECMG_STACK_LEN  4096

#define EMMG_PRI               233
#define ECMG_PRI               234
void caSendLog(unsigned char LogType, const char *fmt,...)
{
#if 0

	int		ret;
	char    buf[256];
	va_list	ap;                                             

	MESSAGE Msg;
	Packet  *pkt;

	va_start(ap, fmt);                              
	ret = vsprintf(buf, fmt, ap);
	va_end(ap);

	pkt=(Packet *)careportlogbuffer;
	pkt->Command=COMMAND_REPORT_CA_LOG;

	pkt->DeviceType			=DEVICE_TYPE_MULTIPLEXER;
	pkt->DeviceIdt			=SystemData.EthernetPara.IP;
	pkt->PacketIdt			=0;
	pkt->Result				=0;
	pkt->PacketHeadLength	=sizeof(Packet)- 4;
	pkt->PacketLength		=sizeof(Packet)- 4 + 2 + ret;

	*(unsigned char *)(&(pkt->Data))=LogType;
	*(unsigned char *)((unsigned char *)&(pkt->Data) + 1)=ret;
	MemCopy((unsigned char *)&(pkt->Data) + 2,buf,ret);

	Msg.Flag   =0;
	Msg.Length =pkt->PacketLength;
	Msg.MsgType=MSG_MONI;
	Msg.Pdata  =(unsigned char *)pkt;

	SendQueue(QN_TCPIPSEND,&Msg);

#endif

	if(LOGTYPE_ERROR==LogType)
		Display(fmt);
}
/*
void SaveCAConfig(void)
{
	int           i;
	unsigned long VerifyFlag=0;
	unsigned char *ptmp;

	ptmp=(unsigned char *)&g_ca_config;
	for(i=0;i<sizeof(CA_CONFIG) - 4;i++)
		VerifyFlag+=*(ptmp + i);

	g_ca_config.VerifyFlag=VerifyFlag;

	SST39VF512_Sector_Erase(60*1024);
	SST39VF512_Write(60*1024,(unsigned char *)&g_ca_config,sizeof(CA_CONFIG));
}
int LoadCaConfig(void)
{
	int           i;
	unsigned long VerifyFlag=0;
	unsigned char *ptmp;

	MemCopy((unsigned char *)&g_ca_config,(unsigned char *)(BD_FLASH_ADDR + 60*1024),sizeof(CA_CONFIG));

	ptmp=(unsigned char *)&g_ca_config;
	for(i=0;i<sizeof(CA_CONFIG) - 4;i++)
		VerifyFlag+=*(ptmp + i);

	if(VerifyFlag!=g_ca_config.VerifyFlag)
	{
		MemSet((unsigned char *)&g_ca_config,sizeof(CA_CONFIG),0x0);
		g_has_valid_ca_config=0;
		g_ca_config.Stream_Count=0;
		return -1;
	}
	else
	{
		g_has_valid_ca_config=1;
		return 0;
	}
}
*/

/*保存加扰配置到FLASH*/
void SaveCAConfig(void)
{
	int           i,j;
	unsigned long VerifyFlag=0;
	unsigned char *ptmp;
	unsigned char *writebuf;
	int           offset;
	unsigned char  ucCACount=0;


	writebuf=(unsigned char *)MemAlloc(RNG2ID,"SaveCAConfig",8*1024);
	if(writebuf == NULL)
		return;

	/*增加版本号标识,跟着为数据长度,再后是相应设置,
	最后是校验,校验从数据长度开始*/
	*(unsigned long *)(writebuf) = ulDateVersion;
	offset = 8;
	
	/*当前CA个数*/
	if(g_has_valid_ca_config)
		ucCACount |= 0x01;
	if(g_has_valid_ca1_config)
		ucCACount |= 0x02;
	if(g_has_valid_ca2_config)
		ucCACount |= 0x04;
/*	if(ucCACount == 0)
		return;
*/
	
	*(writebuf+offset) = ucCACount;
	offset++;


	ptmp=(unsigned char *)&g_ca_config.Para_Config[0];
	MemCopy(writebuf + offset,ptmp,sizeof(CA_PARA_CONFIG) - 256);
	offset+=sizeof(CA_PARA_CONFIG) - 256;

	MemCopy(writebuf + offset,g_ca_config.Para_Config[0].EMM_Private_Data,g_ca_config.Para_Config[0].EMM_Private_Data_Length);
	offset+=g_ca_config.Para_Config[0].EMM_Private_Data_Length;

/*	if(g_has_valid_ca1_config)*/
	{
		ptmp=(unsigned char *)&g_ca_config.Para_Config[1];
		MemCopy(writebuf + offset,ptmp,sizeof(CA_PARA_CONFIG) - 256);
		offset+=sizeof(CA_PARA_CONFIG) - 256;

		MemCopy(writebuf + offset,g_ca_config.Para_Config[1].EMM_Private_Data,g_ca_config.Para_Config[1].EMM_Private_Data_Length);
		offset+=g_ca_config.Para_Config[1].EMM_Private_Data_Length;
	}

/*	if(g_has_valid_ca2_config)*/
	{
		ptmp=(unsigned char *)&g_ca_config.Para_Config[2];
		MemCopy(writebuf + offset,ptmp,sizeof(CA_PARA_CONFIG) - 256);
		offset+=sizeof(CA_PARA_CONFIG) - 256;

		MemCopy(writebuf + offset,g_ca_config.Para_Config[2].EMM_Private_Data,g_ca_config.Para_Config[2].EMM_Private_Data_Length);
		offset+=g_ca_config.Para_Config[2].EMM_Private_Data_Length;
	}

	*(unsigned char *)(writebuf + offset) = g_ca_config.AC_Data_Count;
	offset++;

	for(i=0;i<g_ca_config.AC_Data_Count;i++)
	{
		*(unsigned char *)(writebuf + offset)=g_ca_config.AC_Data_List[i].AC_Index;
		offset++;

		*(unsigned char *)(writebuf + offset)=g_ca_config.AC_Data_List[i].AC_Data_Name_Length;
		offset++;
		for(j=0;j<g_ca_config.AC_Data_List[i].AC_Data_Name_Length;j++)
			*(unsigned char *)(writebuf + offset + j)=g_ca_config.AC_Data_List[i].AC_Data_Name[j];
		offset+=g_ca_config.AC_Data_List[i].AC_Data_Name_Length;

/*		if(g_has_valid_ca_config) */
		{
			*(unsigned short *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_PID;
			offset+=2;
		
			*(unsigned char *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[0].AC_Data_Length;
			offset++;
			for(j=0;j<g_ca_config.AC_Data_List[i].SubDataConfig[0].AC_Data_Length;j++)
				*(unsigned char *)(writebuf + offset + j)=g_ca_config.AC_Data_List[i].SubDataConfig[0].AC_Data[j];
			offset+=g_ca_config.AC_Data_List[i].SubDataConfig[0].AC_Data_Length;

			*(unsigned char *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_Private_Data_Length;
			offset++;
			for(j=0;j<g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_Private_Data_Length;j++)
				*(unsigned char *)(writebuf + offset + j)=g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_Private_Data[j];
			offset+=g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_Private_Data_Length;
		}
		
/*		if(g_has_valid_ca1_config)*/
		{
			*(unsigned short *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[1].ECM_PID;
			offset+=2;
		
			*(unsigned char *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[1].AC_Data_Length;
			offset++;
			for(j=0;j<g_ca_config.AC_Data_List[i].SubDataConfig[1].AC_Data_Length;j++)
				*(unsigned char *)(writebuf + offset + j)=g_ca_config.AC_Data_List[i].SubDataConfig[1].AC_Data[j];
			offset+=g_ca_config.AC_Data_List[i].SubDataConfig[1].AC_Data_Length;

			*(unsigned char *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[1].ECM_Private_Data_Length;
			offset++;
			for(j=0;j<g_ca_config.AC_Data_List[i].SubDataConfig[1].ECM_Private_Data_Length;j++)
				*(unsigned char *)(writebuf + offset + j)=g_ca_config.AC_Data_List[i].SubDataConfig[1].ECM_Private_Data[j];
			offset+=g_ca_config.AC_Data_List[i].SubDataConfig[1].ECM_Private_Data_Length;		
		}

/*		if(g_has_valid_ca2_config)*/
		{
		*(unsigned short *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[2].ECM_PID;
		offset+=2;
		
		*(unsigned char *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[2].AC_Data_Length;
		offset++;
		for(j=0;j<g_ca_config.AC_Data_List[i].SubDataConfig[2].AC_Data_Length;j++)
			*(unsigned char *)(writebuf + offset + j)=g_ca_config.AC_Data_List[i].SubDataConfig[2].AC_Data[j];
		offset+=g_ca_config.AC_Data_List[i].SubDataConfig[2].AC_Data_Length;

		*(unsigned char *)(writebuf + offset)=g_ca_config.AC_Data_List[i].SubDataConfig[2].ECM_Private_Data_Length;
		offset++;
		for(j=0;j<g_ca_config.AC_Data_List[i].SubDataConfig[2].ECM_Private_Data_Length;j++)
			*(unsigned char *)(writebuf + offset + j)=g_ca_config.AC_Data_List[i].SubDataConfig[2].ECM_Private_Data[j];
		offset+=g_ca_config.AC_Data_List[i].SubDataConfig[2].ECM_Private_Data_Length;		
		}
	}

	*(unsigned char *)(writebuf + offset)=g_ca_config.Stream_Count;
	offset++;

	for(i=0;i<g_ca_config.Stream_Count;i++)
	{
		*(unsigned short *)(writebuf + offset)=g_ca_config.Stream_Config[i].Stream_PID;
		offset+=2;
		*(unsigned char *)(writebuf + offset)=g_ca_config.Stream_Config[i].CW_Index;
		offset++;
	}

	/*-------------------------------------------------------------------------------*/
	/* firset 4 bytes save the config length                                         */
	/*-------------------------------------------------------------------------------*/
	*(unsigned long *)(writebuf+4)=offset - 8;

	for(i=0;i<offset;i++)
		VerifyFlag+=*(writebuf + i);

	*(unsigned long *)(writebuf + offset)=VerifyFlag;

	if(offset>4096)
	{
		MemFree(writebuf);
		Display("ca config is bigger than 4k, can not be saved!\n");
		return;
	}
	else
	{
		SST39VF512_Sector_Erase(60*1024);
		SST39VF512_Write(60*1024,writebuf,offset + 4);
		MemFree(writebuf);
	}
}

/*从FLASH读出CA配置信息*/
int LoadCaConfig(void)
{
	int           i,j;
	unsigned long VerifyFlag=0;
	unsigned long configlen;
	unsigned char *readbuf;
	int           offset;
	int      compulen;
	unsigned long  ulVersion;

	ulVersion = *(unsigned long*)(BD_FLASH_ADDR + 60*1024);
	if(ulVersion != ulDateVersion)
	{/*版本号不对,返回失败,表示无相应版本的加扰设置信息*/
		g_has_valid_ca_config=0;
		g_has_valid_ca1_config=0;
		g_has_valid_ca2_config=0;
		MemSet((unsigned char *)&g_ca_config,sizeof(CA_CONFIG),0x0);
		return -1;
	}

	configlen=*(unsigned long *)(BD_FLASH_ADDR + 60*1024+4);
	/*configlen = 5*1024;*/
/*	if((configlen>4*1024)||(configlen<sizeof(CA_PARA_CONFIG) - 256))*/

	/*为了升级和以前版本的兼容*/
	compulen = 3*(sizeof(CA_PARA_CONFIG) - 256);
	if((configlen>4*1024)||(configlen < compulen))
	{
		g_has_valid_ca_config=0;
		g_has_valid_ca1_config=0;
		g_has_valid_ca2_config=0;

		MemSet((unsigned char *)&g_ca_config,sizeof(CA_CONFIG),0x0);

		return -1;
	}
	else
	{
		/*读取配置*/
		readbuf=(unsigned char *)MemAlloc(RNG2ID,"LoadCaConfig",configlen + 4 + 4 + 4);
		if(readbuf == NULL)
			return -1;

		MemCopy(readbuf,(unsigned char *)(BD_FLASH_ADDR + 60*1024),configlen + 4 + 4 + 4);
		offset=8;
		/*-------------------------------------------------------------------------------*/
		/*verify this config                                                            */
		/*-------------------------------------------------------------------------------*/
		for(i=0;i<configlen + 8;i++)
			VerifyFlag+=*(readbuf + i);

		if(VerifyFlag!=*(unsigned long *)(readbuf + 8 + configlen))
		{/*校验不对,返回失败*/
			MemFree(readbuf);

			g_has_valid_ca_config=0;
			g_has_valid_ca1_config=0;
			g_has_valid_ca2_config=0;

			MemSet((unsigned char *)&g_ca_config,sizeof(CA_CONFIG),0x0);

			return -1;
		}

		bJudgeConfig = 1;
		/*获得CA个数*/
		g_ca_config.ucCACount = *(readbuf + offset);
		g_has_valid_ca_config=0;
		g_has_valid_ca1_config=0;
		g_has_valid_ca2_config=0;			
		
		offset += 1;
/*
		if(!(g_ca_config.ucCACount & 0x07))
		{
			g_has_valid_ca_config=0;
			g_has_valid_ca1_config = 0;
			g_has_valid_ca2_config = 0;
			MemSet((unsigned char *)&g_ca_config,sizeof(CA_CONFIG),0x0);

			return -1;
		}
*/		
		if(g_ca_config.ucCACount & 0x01)
			g_has_valid_ca_config = 1;
		if(g_ca_config.ucCACount & 0x02)
			g_has_valid_ca1_config = 1;
		if(g_ca_config.ucCACount & 0x04)
			g_has_valid_ca2_config = 1;
			
		MemCopy((unsigned char *)&(g_ca_config.Para_Config[0]),readbuf + offset,sizeof(CA_PARA_CONFIG) - 256);
		offset+=sizeof(CA_PARA_CONFIG) - 256;

		MemCopy(g_ca_config.Para_Config[0].EMM_Private_Data,readbuf + offset,g_ca_config.Para_Config[0].EMM_Private_Data_Length);
		offset+=g_ca_config.Para_Config[0].EMM_Private_Data_Length;

/*		if(g_has_valid_ca1_config)*/
		{/*获得第二个CA的设置*/
			MemCopy((unsigned char *)&(g_ca_config.Para_Config[1]),readbuf + offset,sizeof(CA_PARA_CONFIG) - 256);
			offset+=sizeof(CA_PARA_CONFIG) - 256;

			MemCopy(g_ca_config.Para_Config[1].EMM_Private_Data,readbuf + offset,g_ca_config.Para_Config[1].EMM_Private_Data_Length);
			offset+=g_ca_config.Para_Config[1].EMM_Private_Data_Length;			
		}

/*		if(g_has_valid_ca2_config) */
		{
			MemCopy((unsigned char *)&(g_ca_config.Para_Config[2]),readbuf + offset,sizeof(CA_PARA_CONFIG) - 256);
			offset+=sizeof(CA_PARA_CONFIG) - 256;

			MemCopy(g_ca_config.Para_Config[2].EMM_Private_Data,readbuf + offset,g_ca_config.Para_Config[2].EMM_Private_Data_Length);
			offset+=g_ca_config.Para_Config[2].EMM_Private_Data_Length;			
			
		}

		g_ca_config.AC_Data_Count=*(unsigned char *)(readbuf + offset);
		offset++;

		for(i=0;i<g_ca_config.AC_Data_Count;i++)
		{/*读取ACDATA列表数据*/
			g_ca_config.AC_Data_List[i].AC_Index=*(unsigned char *)(readbuf + offset);
			offset++;

			g_ca_config.AC_Data_List[i].AC_Data_Name_Length=*(unsigned char *)(readbuf + offset);
			offset++;
			for(j=0;j<g_ca_config.AC_Data_List[i].AC_Data_Name_Length;j++)
				g_ca_config.AC_Data_List[i].AC_Data_Name[j]=*(unsigned char *)(readbuf + offset + j);
			offset+=g_ca_config.AC_Data_List[i].AC_Data_Name_Length;

			{
				g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_PID=*(unsigned short *)(readbuf + offset);
				offset+=2;

				g_ca_config.AC_Data_List[i].SubDataConfig[0].AC_Data_Length=*(unsigned char *)(readbuf + offset);
				offset++;
				for(j=0;j<g_ca_config.AC_Data_List[i].SubDataConfig[0].AC_Data_Length;j++)
					g_ca_config.AC_Data_List[i].SubDataConfig[0].AC_Data[j]=*(unsigned char *)(readbuf + offset + j);
				offset+=g_ca_config.AC_Data_List[i].SubDataConfig[0].AC_Data_Length;

				g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_Private_Data_Length=*(unsigned char *)(readbuf + offset);
				offset++;
				for(j=0;j<g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_Private_Data_Length;j++)
					g_ca_config.AC_Data_List[i].SubDataConfig[0].ECM_Private_Data[j]=*(unsigned char *)(readbuf + offset + j);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -