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

📄 emm.c

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

#define MAX_EMMG_BUFFER_LENGTH  (1024*4)

#define MAX_EMMG_CONNECTED  3


/*char   emmgdatagrambuf[MAX_EMMG_BUFFER_LENGTH];*/
int    g_emm_datagram_length;

extern CA_CONFIG g_ca_config;

CA_EMMG_CHANNEL_INFO		g_emmg_channel_info[3];

CA_EMMG_STREAM_INFO			g_emmg_stream_info[3];
/*CA_EMMG_STREAM_BW_REQUEST	g_emmg_stram_bw_request[3];*/
/*CA_EMMG_DATA_PROVISION		g_emmg_data_provision;*/

/*对于EMM发起的连接列表*/
CA_EMM_TCPMEMBER    g_Emm_TcpMember[MAX_EMMG_CONNECTED];

int                   g_Emmg_connected[3];
unsigned long    ulTidEmmTcp[3];

int		g_socket_emmg=0;
int		g_socket_emmg1=0; /*added by xu*/
int		g_socket_emmg2=0; /*added by xu*/

/*-------------------------------------------------------*/
/* added by johnnyling 2004-08-24                        */
/*-------------------------------------------------------*/
#define EMM_MAX_PAYLOD (188 - 1 - sizeof(TS_TAG))
char     g_emm_ts_buf[3][188];
int     g_emm_buf_off[3] = {0,0,0};

/*fd_set   g_read_mask;*/
/*-------------------------------------------------------*/
/* added by johnnyling 2004-08-24                        */
/*-------------------------------------------------------*/

/*将EMM数据送到FIFO中*/
void send_emm(char *buf,int buflen,unsigned short pid,char datatype,unsigned char ucCACount)
{
	int             i;
	int             tsnum;
	TS_TAG          *pTsHead;
	char            *pSection;
	short           offset;
	char            *pts;
	char            tspkt[188];
	short           rst;
	int             first;

	static int      continue_counter[3]={0,0,0};
	
	if(datatype)
	{/* ts packet */
		tsnum= buflen / 188;
		for(i=0;i<tsnum;i++)
		{
			pts= buf + i*188;
			/*获取PTS信息及PID*/
			pTsHead=(TS_TAG *)pts;
			pTsHead->PID=pid;
			pTsHead->Continuity_counter=continue_counter[ucCACount];
			ts_send(pts);

			/*-------------------------------------------------*/
			/* added by johnnyling 2004-02-16                                            */
			/*---------------------------------------------------*/
			if(continue_counter[ucCACount]==15)
				continue_counter[ucCACount]=0;
			else
				continue_counter[ucCACount]++;
		}
	}
	else
	{/*将段打包成TS包写到FIFO中*/
		pSection = buf;
		offset = 0;

		rst=buflen;
		first=1;
		while(rst>0)
		{
			pTsHead=(TS_TAG *)tspkt;
			MemSet(tspkt,188,0xFF);

			pTsHead->Sync_byte             = 0x47;
			pTsHead->Transport_err_indicator     = 0;

			if(first)			
				pTsHead->Payload_unit_start_indicator=1;
			else
				pTsHead->Payload_unit_start_indicator=0;

			pTsHead->Transport_priority          =0;
			pTsHead->PID                         =pid;
			pTsHead->Transport_scambling_control =0;
			pTsHead->Adaption_field_control      =1;
			pTsHead->Continuity_counter          =continue_counter[ucCACount];

			if(continue_counter[ucCACount]==15)
				continue_counter[ucCACount]=0;
			else
				continue_counter[ucCACount]++;

			if(first)
			{/*第一个包*/
				*(char *)((char *)pTsHead + sizeof(TS_TAG))=0; /* pointer field */
				if(rst<=(188 - 1 - sizeof(TS_TAG)))
				{/*长度小于183字节*/
					MemCopy((char *)pTsHead + sizeof(TS_TAG) + 1,buf + offset,rst);
					offset+=rst;
					ts_send(tspkt);
					break;
				}
				else
				{/*大于183字节,要多次发送*/
					MemCopy((char *)pTsHead + sizeof(TS_TAG) + 1,buf + offset,188-1-sizeof(TS_TAG));
					offset+=188-1-sizeof(TS_TAG);
					ts_send(tspkt);
					rst-=188 - 1 - sizeof(TS_TAG);
				}
				first=0;
			}
			else
			{
				if(rst<=(188 - sizeof(TS_TAG)))
				{
					MemCopy((char *)pTsHead + sizeof(TS_TAG),buf + offset,rst);
					offset+=rst;
					ts_send(tspkt);
					break;
				}
				else
				{
					MemCopy((char *)pTsHead + sizeof(TS_TAG),buf + offset,188 - sizeof(TS_TAG));
					offset+=188 - sizeof(TS_TAG);
					ts_send(tspkt);
					rst-=188 - sizeof(TS_TAG);
				}
			}
		}
	}
}
/*
void send_emm_datagram(char *buf,int buflen,unsigned short pid,char datatype)
{
	SECTION_TAG *pSection;
	char        *pbuf;
	int         rst=buflen;

	static      cnt=0;
		
	pSection=(SECTION_TAG *)buf;
	while(rst>0)
	{
		while((pSection->TableID<0x80)||(pSection->TableID>0x89))
		{
			pSection=(SECTION_TAG *)((char *)pSection + 1);
			rst--;

			if(rst<=0)
				return;
		}

		send_emm((char *)pSection,pSection->SectionLength + 3,pid,datatype);
		rst-=pSection->SectionLength + 3;
		pSection=(SECTION_TAG *)((char *)pSection + pSection->SectionLength + 3);
	}
}
*/
/*---------------------------------------------------------------------------*/
/* updated by johnnyling 2004-02-16                                          */
/* support ts packet emm                                                     */
/*---------------------------------------------------------------------------*/
#if 0
void send_emm_datagram(char *buf,int buflen,unsigned short pid,char datatype)
{
	SECTION_TAG *pSection;
	char        *pbuf;
	int         rst=buflen;

	static      cnt=0;
		
	if(datatype)
	{
		send_emm(buf,buflen,pid,datatype);
	}
	else
	{
		pSection=(SECTION_TAG *)buf;
		/*-----------------------------------------------------------------------------*/
		/*去掉中视联section前面的垃圾   updated by johnnyling 2004-02-17               */
		/*-----------------------------------------------------------------------------*/
		if((g_ca_config.Para_Config.CA_system_id>=0x4900)&&( g_ca_config.Para_Config.CA_system_id<=0x49ff))
		{
			while(rst>0)
			{
				while((pSection->TableID<0x80)||(pSection->TableID>0x89))
				{
					pSection=(SECTION_TAG *)((char *)pSection + 1);
					rst--;

					if(rst<=0)
						return;
				}

				send_emm((char *)pSection,pSection->SectionLength + 3,pid,datatype);
				rst-=pSection->SectionLength + 3;
				pSection=(SECTION_TAG *)((char *)pSection + pSection->SectionLength + 3);
			}
		}
		else
			send_emm((char *)pSection,pSection->SectionLength + 3,pid,datatype);
	}
}
#else
/*发送EMM数据*/
void send_emm_datagram(char *buf,int buflen,unsigned short pid,unsigned char ucCACount)
{
	SECTION_TAG *pSection;
	char        *pbuf;
	int         rst=buflen;
	char   *pTemp;

	static      cnt=0;

	if(g_emmg_channel_info[ucCACount].section_TSpket_flag)
	{/*表示为TS流,直接发送*/
		send_emm(buf,buflen,pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
	}
	else
	{/*接收的为段,需打包成TS包*/
		pSection=(SECTION_TAG *)buf;
		/*-----------------------------------------------------------------------------*/
		/*去掉中视联section前面的垃圾   updated by johnnyling 2004-02-17               */
		/*-----------------------------------------------------------------------------*/
		if((g_ca_config.Para_Config[ucCACount].CA_system_id>=0x4900)&&( g_ca_config.Para_Config[ucCACount].CA_system_id<=0x49ff))
		{
			while(rst>0)
			{
				while((pSection->TableID<0x80)||(pSection->TableID>0x89))
				{/*如PID不正确,逐个往后查找*/
					pSection=(SECTION_TAG *)((char *)pSection + 1);
					rst--;

					if(rst<=0)
						return;
				}

				if(pSection->SectionLength + 3>=EMM_MAX_PAYLOD)
				{
					/*------------------------------------------------------------------*/
					/* 先发送缓冲的SECTION                                              */
					/*------------------------------------------------------------------*/
					if(g_emm_buf_off[ucCACount])
						send_emm((char *)g_emm_ts_buf[ucCACount],g_emm_buf_off[ucCACount],pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
					g_emm_buf_off[ucCACount]=0;
					/*------------------------------------------------------------------*/
					/* 先发送缓冲的SECTION                                              */
					/*------------------------------------------------------------------*/
					send_emm((char *)pSection,pSection->SectionLength + 3,pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
				}
				else if(pSection->SectionLength + 3 + g_emm_buf_off[ucCACount] >= EMM_MAX_PAYLOD)
				{
					/*------------------------------------------------------------------*/
					/* 先发送缓冲的SECTION                                              */
					/*------------------------------------------------------------------*/
					if(g_emm_buf_off[ucCACount])
						send_emm((char *)g_emm_ts_buf[ucCACount],g_emm_buf_off[ucCACount],pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
					g_emm_buf_off[ucCACount]=0;
					/*------------------------------------------------------------------*/
					/*缓冲本次的SECTION                                                 */
					/*------------------------------------------------------------------*/
					memcpy(g_emm_ts_buf[ucCACount],pSection,pSection->SectionLength + 3);
					g_emm_buf_off[ucCACount]=pSection->SectionLength + 3;
				}
				else
				{
					/*------------------------------------------------------------------*/
					/*缓冲本次的SECTION                                                 */
					/*------------------------------------------------------------------*/
					memcpy(g_emm_ts_buf[ucCACount] + g_emm_buf_off[ucCACount],pSection,pSection->SectionLength + 3);
					g_emm_buf_off[ucCACount]+=buflen;
				}

				rst-=pSection->SectionLength + 3;
				pSection=(SECTION_TAG *)((char *)pSection + pSection->SectionLength + 3);
			}
		}
		else
		{/*当数据小于183字节时,缓存下来,做为下次发送*/
			if(buflen>=EMM_MAX_PAYLOD)
			{
				/*------------------------------------------------------------------*/
				/* 先发送缓冲的SECTION                                              */
				/*------------------------------------------------------------------*/
				if(g_emm_buf_off[ucCACount])
				{
					send_emm((char *)g_emm_ts_buf[ucCACount],g_emm_buf_off[ucCACount],pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
	 			}
				g_emm_buf_off[ucCACount]=0;	
				/*------------------------------------------------------------------*/
				/* 先发送缓冲的SECTION                                              */
				/*------------------------------------------------------------------*/
				send_emm((char *)pSection,pSection->SectionLength + 3,pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
	 		}
			else if(buflen+g_emm_buf_off[ucCACount]>=EMM_MAX_PAYLOD)
			{
				/*------------------------------------------------------------------*/
				/* 先发送缓冲的SECTION                                              */
				/*------------------------------------------------------------------*/
				if(g_emm_buf_off[ucCACount])
				{
					send_emm((char *)g_emm_ts_buf[ucCACount],g_emm_buf_off[ucCACount],pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
	 			}
				g_emm_buf_off[ucCACount]=0;
				/*------------------------------------------------------------------*/
				/*缓冲本次的SECTION                                                 */
				/*------------------------------------------------------------------*/
				memcpy(g_emm_ts_buf[ucCACount],buf,buflen);
				g_emm_buf_off[ucCACount]=buflen;

	 		}
			else
			{
	 				send_emm(buf,buflen,pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
					g_emm_buf_off[ucCACount] = 0;
				
			}			
		}
	}
}

#endif

/*发送通道测试信息*/
int  emmgsendchannelstatus(int socket,unsigned char ucCACount)
{
	volatile CA_MESSAGE_HEAD      *pHead;
	volatile CA_MESSAGE_ITEM_HEAD *pItemHead;
	int                  MsgLen=0;
	int i;
	int                  rc;

	char   EmmgSendBuf[2048];

	pHead=(CA_MESSAGE_HEAD *)EmmgSendBuf;
	pHead->protocol_version=0x02;
	pHead->message_type    =EMMG2MUX_CHANNEL_STATUS;
	pHead->message_length  =0;

	pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pHead + sizeof(CA_MESSAGE_HEAD));

	pItemHead->parameter_type=EMMG2MUX_client_ID;
	pItemHead->parameter_length=4;
	*(unsigned long *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD))=g_emmg_channel_info[ucCACount].client_ID;
	pHead->message_length  +=sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length;
	pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length);

	pItemHead->parameter_type=EMMG2MUX_data_channel_ID;
	pItemHead->parameter_length=2;
	*(unsigned short *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD))=g_emmg_channel_info[ucCACount].data_channel_ID;
	pHead->message_length  +=sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length;
	pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length);

	pItemHead->parameter_type=EMMG2MUX_section_TSpkt_flag;
	pItemHead->parameter_length=1;
	*(unsigned char *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD))=g_emmg_channel_info[ucCACount].section_TSpket_flag;
	pHead->message_length  +=sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length;
	pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length);

	MsgLen=pHead->message_length + sizeof(CA_MESSAGE_HEAD);
	/*将数据发送到EMMG*/
/*	rc=ca_send(socket,EmmgSendBuf,MsgLen,g_ca_config.Para_Config[ucCACount].Network_Delay);*/
       rc=send(socket,EmmgSendBuf,MsgLen,0);
	if(rc!=MsgLen)
		return -1;
	else 
	{
		caSendLog(LOGTYPE_NORMAL,"emmg: Send channel status message,channelid=%d.",g_emmg_channel_info[ucCACount].data_channel_ID);
		Display("emmg: Send channel status message,clientId=%d.",g_emmg_channel_info[ucCACount].client_ID);
		return rc;		
	}
}

/*发送流状态信息*/
int emmgsendstreamstatus(int  socket,unsigned char ucCACount)
{
	CA_MESSAGE_HEAD      *pHead;
	CA_MESSAGE_ITEM_HEAD *pItemHead;
	int                  MsgLen=0;
	int i;
	int                  rc;

	char          EmmgSendBuf[2048];

	pHead=(CA_MESSAGE_HEAD *)EmmgSendBuf;
	pHead->protocol_version=0x02;
	pHead->message_type    =EMMG2MUX_STREAM_STATUS;
	pHead->message_length  =0;

	pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pHead + sizeof(CA_MESSAGE_HEAD));

⌨️ 快捷键说明

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