kb_dc_sipsi.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:14k
- //-----------------------------------------------------------------------------
- // Project include files:
- //-----------------------------------------------------------------------------
- #include "kb_dc_pub.h"
- static KBDsmccPat KBDsmccPAT;
- static KBDsmccPmts KBDsmccPMTs;
- static KBDsmccEsArray KBDsmccEsAry;
- static KBServiceCfgDesArry KBSerCfgDesAry;
- extern unsigned char byFilterBuf[];
- //用于存储DII version 结构
- static KBDSMCCDIIVERSIONARRAY curDiiVer;
- int KBDsmccGetPAT(unsigned int tsId, unsigned char *buf)
- {
- int sErr = Err_OK;
- unsigned short iDataSize = 0;
- tsId = tsId;
- if(KB_DC_FilterReSet(0x00, 0x00, UNSET_PARAM, UNSET_PARAM, 0)!=0)
- {
- DCDebug1("n KBDsmccGetPAT: KB_DC_FilterSet error!");
- return -1;
- }
- sErr = KB_DC_GetSection(buf, &iDataSize);
- if(Err_OK != sErr)
- {
- DCDebug1("nrGet section error[%d].rn",sErr );
- return sErr;
- }
-
- return sErr;
- }
- int KBDsmccGetPMT(unsigned short pid, unsigned int proNo, unsigned char *buf)
- {
- //return KBDsmccGetTable(pid, 0x02, proNo, buf, 0);
- int sErr = Err_OK;
- unsigned short iDataSize = 0;
- if(KB_DC_FilterReSet(pid, 0x02, proNo, UNSET_PARAM, 0)!=0)
- {
- DCDebug1("n KBDsmccGetPAT: KB_DC_FilterSet error!");
- return -3;
- }
- sErr = KB_DC_GetSection(buf, &iDataSize);
- if(Err_OK != sErr)
- {
- DCDebug1("nrGet section error[%d].rn",sErr );
- return sErr;
- }
-
- return sErr;
- }
- int KBDsmccGetDIITable(unsigned short pid, unsigned short extId, unsigned char **buf)
- {
- return KBDcGetTable(pid, 0x3b, extId, buf, 0);
- //return KBDcGetTable(pid, 0x3b, PROGRAM_DONT_CARE, buf, 0);
- }
- int KBDsmccGetDDBTable(unsigned short pid, unsigned short moduleId, unsigned char **buf)
- {
- //return KBDsmccGetTable(pid, 0x3c, moduleId, buf, 0);
- return KBDcGetTable(pid, 0x3c, moduleId, buf, 0);
- }
- void KBFreeSectionBuffer(unsigned char * buf)
- {
- if(buf!=NULL)
- {
- KBDcFree(buf);
- buf=NULL;
- }
- }
- int KBDsmccSipsiProc(unsigned int tsId)
- {
- unsigned char *buf=NULL;
- int programCnt=0, esCnt=0, dsmccEsCnt=0;
- int i=0;
- UINT32 sErr = Err_OK;
- int res =0;
- //先初始化变量
- KB_DC_SipsiDataInit();
-
- buf = (unsigned char *)KBDcMalloc(PSI_SECTION_SIZE);
- if(buf==NULL)
- {
- return 0;
- }
- KBDcMemset(buf, 0x00, PSI_SECTION_SIZE);
- DCDebug3("n get PAT");
- sErr=KBDsmccGetPAT(tsId, buf);
- if(Err_OK != sErr)
- {
- DCDebug1("nr KBDsmccGetPAT error[%d].rn",sErr );
- KBFreeSectionBuffer(buf);
- return 0;
- }
- programCnt=KBDsmccParsePAT(buf);
- DCDebug3("n got PAT");
- DCDebug2("n programCnt=%d",programCnt);
- /*以防多次调用进行不必要的累加 shriek*/
- KBDcMemset(&KBDsmccPMTs, 0x00, sizeof(KBDsmccPmts));
- for(i=0; i<programCnt; i++)
- {
- KBDsmccPmt *pmt=NULL;
- KBDsmccPatInfo *patInfo=&KBDsmccPAT.pat_info[i];
- DCDebug3("nn---- patInfo->pmt_pid=%x, patInfo->program_number=%x",patInfo->pmt_pid,patInfo->program_number);
- if(patInfo==NULL)
- {
- KBFreeSectionBuffer(buf);
- return 0;
- }
- DCDebug3("n get PMT");
-
- sErr = KBDsmccGetPMT(patInfo->pmt_pid, patInfo->program_number, buf);
- if(Err_OK != sErr)
- {
- DCDebug1("nr KBDsmccGetPMT error[%d].rn",sErr );
- KBFreeSectionBuffer(buf);
- return 0;
- }
- DCDebug3("n got PMT");
- pmt = &KBDsmccPMTs.pmts[KBDsmccPMTs.count];
- esCnt=KBDsmccParsePMT(buf, pmt);
- if(esCnt<0)
- {
- KBFreeSectionBuffer(buf);
- KBDsmccReleaseAllPMTs();
- return 0;
- }
-
- KBDsmccPMTs.count++;
- }
-
- dsmccEsCnt=KBBuildDsmccEsArray();
- if(dsmccEsCnt==0)
- res = 0;
- else
- res =1;
- KBFreeSectionBuffer(buf);
- KBDsmccReleaseAllPMTs();
-
- return res;
- }
- int KBDsmccParsePAT(const unsigned char *pat_section)
- {
- const unsigned char *p=pat_section;
- KBDsmccPat *pat=&KBDsmccPAT;
- int cnt=0;
- int section_len=0, loop_len=0;
- unsigned short pid=0, number=0;
- p++;
- section_len=((unsigned short)(p[0]<<8)|p[1])&0x0fff;
- p+=2;
- pat->transport_stream_id=(unsigned short)(p[0]<<8)|p[1];
- p+=2;
- pat->version=(p[0]>>1)&0x1f;
- p++;
- p+=2;
- //info loop , no include nit
- loop_len=section_len+3-8-4;
- while(loop_len>0)
- {
- number=(unsigned short)(p[0]<<8)|p[1];
- p+=2;
- pid=((unsigned short)(p[0]<<8)|p[1])&0x1fff;
- p+=2;
- if(number==0)
- {
- loop_len-=2+2;
- continue;//skip nit
- }
- if((cnt+1)>KB_DSMCC_PAT_INFOS_MAX) break;
- pat->pat_info[cnt].pmt_pid=pid;
- pat->pat_info[cnt].program_number=number;
- cnt++;
- loop_len-=2+2;
- }
- //if(loop_len==0) DCDebug4("PAT loop length check OK=%d...n", loop_len);
- pat->pat_info_count=cnt;
- return cnt;
- }
- int KBDsmccParsePMT(const unsigned char *pmt_section, KBDsmccPmt *pPmt)
- {
- const unsigned char *p=pmt_section;
- KBDsmccPmt *pmt=pPmt;
- int section_len=0, es_loop_len=0;
- int cnt=0;
- DCDebug4("rn------KBDsmccParsePMT:%x %x %x %x %x %x %x %x %x ", p[0], p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8]);
- p++;
- section_len=((unsigned short)(p[0]<<8)|p[1])&0x0fff;
- p+=2;
- pmt->program_number=(unsigned short)(p[0]<<8)|p[1];
- p+=2;
- pmt->version=(p[0]>>1)&0x1f;
- p++;
- p+=2;
- pmt->pcr_pid=((unsigned short)(p[0]<<8)|p[1])&0x1fff;
- p+=2;
-
- pmt->info_length=((unsigned short)(p[0]<<8)|p[1])&0x0fff;
- p+=2;
- if(pmt->info_bytes!=NULL)
- {
- KBDcFree(pmt->info_bytes);
- pmt->info_bytes=NULL;
- }
- if(pmt->info_length!=0)
- {
- pmt->info_bytes=(unsigned char*)KBDcMalloc((UINT32)sizeof(unsigned char)*(UINT32)(pmt->info_length));
- if(pmt->info_bytes==NULL)
- {
- return -1;
- }
- KBDcMemset(pmt->info_bytes, 0x00, (UINT32)sizeof(unsigned char)*(UINT32)(pmt->info_length));
- KBDcMemcpy(pmt->info_bytes, p, pmt->info_length);
- }
-
- p+=pmt->info_length;
-
- es_loop_len=section_len+3-12-pmt->info_length-4;
- while(es_loop_len>0)
- {
- unsigned char stream_type=0;
- unsigned short es_pid=0;
- int len=0;
- unsigned char *temp=NULL;
- stream_type=*p++;
- es_pid=((unsigned short)(p[0]<<8)|p[1])&0x1fff;
- p+=2;
- if((cnt+1)>KB_DSMCC_PAT_INFOS_MAX) break;
- pmt->pmt_es[cnt].es_pid=es_pid;
- pmt->pmt_es[cnt].stream_type=stream_type;
- len=((unsigned short)(p[0]<<8)|p[1])&0x0fff;
- pmt->pmt_es[cnt].es_info_length=len;
- p+=2;
- if(temp!=NULL)
- {
- KBDcFree(temp);
- temp=NULL;
- }
- if(len!=0)
- {
- temp=(unsigned char*)KBDcMalloc((UINT32)sizeof(unsigned char)*(UINT32)len);
- if(temp==NULL)
- {
- return -1;
- }
- KBDcMemcpy(temp, p, len);
- }
- pmt->pmt_es[cnt].es_info_bytes=temp;
- p+=len;
- cnt++;
- es_loop_len-=1+2+2+len;
- }
-
- pmt->pmt_es_count=cnt;
- return cnt;
- }
- const unsigned char* KBGetDescriptor(unsigned char tag, const unsigned char *desBytes, int dlen)
- {
- const unsigned char *p=desBytes;
-
- while(dlen>0)
- {
- if(tag==p[0]) return p;
- dlen-=1+1+p[1];
- p+=1+1+p[1];
- }
- return NULL;
- }
- unsigned char KBParseDataBroadcastIdDescriptor(const unsigned char *dDes, int dLen, KBDsmccEs *es)
- {
- const unsigned char *p=dDes;
- if(p==NULL) return 0;
- es->data_broadcast_id=((unsigned short)(p[2]<<8))|p[3];
- p+=4;
- es->carousel_type_id=p[0]>>6;
- p++;
- es->transaction_id=((unsigned int)(p[0]<<24))|((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3];
- p+=4;
- es->DSI_timeout=((unsigned int)(p[0]<<24))|((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3];
- p+=4;
- es->DII_timeout=((unsigned int)(p[0]<<24))|((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3];
- p+=4;
- es->leak_rate=(((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3])&0x3fffff;
- p+=3;
- es->dii_version = *p;
-
- if(dLen==1+1+2+1+4*3+3+1) return 1;
- else return 0;
- }
- //parse private service configure descriptor
- unsigned char KBParseServCfgDescriptor(const unsigned char *dDes, int dLen, KBServiceCfgDes *servDes)
- {
- const unsigned char *p=dDes;
- unsigned int ctypelen =0, cnamelen=0;
- DCDebug3("n -[KBParseServCfgDescriptor]-: %x %x %x",dDes[0], dDes[1], dDes[2]);
- if(p==NULL) return 0;
- p+=2;
- ctypelen = p[0];
- if(ctypelen>KB_SERVICE_PRIVATEDES_NAME_MAX)
- {
- DCDebug1("n -[KBParseServCfgDescriptor]-ctypelen too overlong!");
- }
- KBDcMemcpy(servDes->CfgType, ++p, ctypelen);
- servDes->CfgType[ctypelen] = 0;
- p+=ctypelen;
- cnamelen = p[0];
- if(cnamelen>KB_SERVICE_PRIVATEDES_NAME_MAX)
- {
- DCDebug1("n -[KBParseServCfgDescriptor]-cnamelen too overlong!");
- }
- KBDcMemcpy(servDes->CfgName, ++p, cnamelen);
- servDes->CfgName[cnamelen] = 0;
- if(dLen == ctypelen+cnamelen+4)
- return 1;
- else
- {
- DCDebug1("n -[KBParseServCfgDescriptor]-dLen is error!");
- return 0;
- }
- }
- int KBBuildDsmccEsArray(void)
- {
- KBDsmccEsArray *ess=&KBDsmccEsAry;
- int i=0, j=0, k=-1,l =-1;
- for(i=0; i<KBDsmccPMTs.count; i++)
- {
- const KBDsmccPmt *pmt=&KBDsmccPMTs.pmts[i];
- for(j=0; j<pmt->pmt_es_count; j++)
- {
- const KBDsmccPmtEs *es=NULL;
- const unsigned char *dDes=NULL;
- es=&pmt->pmt_es[j];
- DCDebug3("n===es->stream_type=0x%02x", es->stream_type);
- if((es->stream_type==0x0C)||(es->stream_type==0x0B))
- {
- k++;
- ess->es[k].stream_type=es->stream_type;
- ess->es[k].es_pid=es->es_pid;
- ess->es[k].ts_id=KBDsmccPAT.transport_stream_id;
- //DataBroadcastIdDescriptor
- dDes=KBGetDescriptor(0x66, es->es_info_bytes, (int)es->es_info_length);
- if(dDes!=NULL)
- {
- KBParseDataBroadcastIdDescriptor(dDes, dDes[1]+2, &ess->es[k]);
- }
-
- //service cfg descriptor in main frequency
- dDes=KBGetDescriptor(0x90, es->es_info_bytes, (int)es->es_info_length);
- if(dDes!=NULL)
- {
- l++;
- KBSerCfgDesAry.servDes[l].es_pid = ess->es[k].es_pid;
- KBSerCfgDesAry.servDes[l].transaction_id = ess->es[k].transaction_id;
- KBParseServCfgDescriptor(dDes, dDes[1]+2, &KBSerCfgDesAry.servDes[l]);
- KBSerCfgDesAry.count = l+1;
- DCDebug2("n --sercfgDes foud--!");
- DCDebug3("n es_pid=%d", KBSerCfgDesAry.servDes[l].es_pid);
- DCDebug3("n transaction_id=%d", KBSerCfgDesAry.servDes[l].transaction_id);
- DCDebug3("n CfgName=%s", KBSerCfgDesAry.servDes[l].CfgName);
- DCDebug3("n CfgType=%s", KBSerCfgDesAry.servDes[l].CfgType);
- }
- }
- }
- }
- ess->count=k+1;
- #if 1
- KBSerCfgDesAry.servDes[0].es_pid = 6200;
- strcpy((char *)KBSerCfgDesAry.servDes[0].CfgName, (const char *)"ServiceCfg.xml");
- strcpy((char *)KBSerCfgDesAry.servDes[0].CfgType, (const char *)"service_cfg_xml");
- KBSerCfgDesAry.count = 1;
- #endif
- DCDebug2("n Get dc data.dsmccEsCnt=%d", ess->count);
- DCDebug2("n KBSerCfgDesAry.count=%d", KBSerCfgDesAry.count);
- return (k+1);
- }
- #if 1
- /*-----------将ES DII version 信息存储--------*/
- int KB_DC_LoadCurDiiVerion(void)
- {
- int i,k=0;
- KBDSMCCDIIVERSIONARRAY KBDsmccDiiVersion;
- DCDebug3("n ----KB_DC_LoadCurDiiVerion-----");
- KBDcMemset(&curDiiVer, 0x00, sizeof(KBDSMCCDIIVERSIONARRAY));
-
- //load
- for(i=0; i<KBDsmccEsAry.count; i++)
- {
- KBDsmccDiiVersion.esDiiVer[i].es_pid = KBDsmccEsAry.es[i].es_pid;
- KBDsmccDiiVersion.esDiiVer[i].dii_version = KBDsmccEsAry.es[i].dii_version;
- KBDsmccDiiVersion.esDiiVer[i].ts_id = KBDsmccEsAry.es[i].ts_id;
- k++;
- }
- KBDsmccDiiVersion.count = KBDsmccEsAry.count;
- //必须赋值为True,以标识不是开机第一次load
- KBDsmccDiiVersion.useFlag = True;
- //store
- KBDcMemcpy(&curDiiVer, &KBDsmccDiiVersion , sizeof(KBDSMCCDIIVERSIONARRAY));
-
- return 1;
- }
- KBDSMCCDIIVERSIONARRAY *KB_DC_GetCurDiiVersionPtr(void)
- {
- return &curDiiVer;
- }
- // 功能: 监测DII 版本号是否更新,
- // 返回值: 是则返回1,
- // 否返回0
- // 错误返回-1
- int KB_DC_DiiVerCheck(unsigned int tsId)
- {
- int i=0;
- DCDebug3("n ---KB_DC_DiiVerCheck------");
- //res = KBDsmccSipsiProc(0x00);
- //if(res<=0)
- //{
- // DCDebug4("n KB_DC_DiiVerCheck return error[%d]",res);
- // return -1;
- //}
- if(KBDsmccEsAry.count>=KB_DSMCC_ES_MAX)
- return -1;
- #if 1
- //开机第一次监测,应该返回1,强制去搜索配置文件
- if(curDiiVer.useFlag == False)
- {
- DCDebug3("n 开机第一次监测,所以返回1");
- curDiiVer.useFlag = True;
- return 1;
- }
- #endif
- DCDebug4("n ====KBDsmccEsAry.es[i].dii_version=%d", KBDsmccEsAry.es[0].dii_version);
- //判断DII version是否过期
- for(i=0; i<KBDsmccEsAry.count; i++)
- {
- DCDebug4("n KBDsmccEsAry: es_pid=%x, ts_id=%x", KBDsmccEsAry.es[i].es_pid, KBDsmccEsAry.es[i].ts_id);
- DCDebug4("n curDiiVer: es_pid=%x, ts_id=%x", curDiiVer.esDiiVer[i].es_pid, curDiiVer.esDiiVer[i].ts_id);
- if((curDiiVer.esDiiVer[i].es_pid==KBDsmccEsAry.es[i].es_pid)
- &&(curDiiVer.esDiiVer[i].ts_id==KBDsmccEsAry.es[i].ts_id))
- {
- DCDebug3("n cur_dii[%d]vs[%d]stream_dii", curDiiVer.esDiiVer[i].dii_version, KBDsmccEsAry.es[i].dii_version);
- if(curDiiVer.esDiiVer[i].dii_version!=KBDsmccEsAry.es[i].dii_version)
- {
- DCDebug3("n DII version is updated!!!!!!!!!!");
- //KB_DC_LoadCurDiiVerion();
- return 1;
- }
- else
- {
- DCDebug3("n DII version not updated!");
- return 0;
- }
- }
- }
-
- return 1;
- }
- //--------------------------------end---------------------------------
- #endif
- void KB_DC_SipsiDataInit(void)
- {
- KBDcMemset(&KBDsmccEsAry, 0x00, sizeof(KBDsmccEsArray));
- KBDcMemset(&KBDsmccPMTs, 0x00, sizeof(KBDsmccPmts));
- KBDcMemset(&KBDsmccPAT, 0x00, sizeof(KBDsmccPat));
- KBDcMemset(&KBSerCfgDesAry, 0x00, sizeof(KBServiceCfgDesArry));
- }
- void KBDsmccReleasePMT(KBDsmccPmt *pmt)
- {
- int i=0;
-
- if(pmt==NULL) return;
- if(pmt->info_bytes!=NULL)
- {
- KBDcFree(pmt->info_bytes);
- pmt->info_bytes=NULL;
- }
- for(i=0; i<pmt->pmt_es_count; i++)
- {
- KBDsmccPmtEs *es=&pmt->pmt_es[i];
- if(es==NULL) break;
- if(es->es_info_bytes!=NULL)
- {
- KBDcFree(es->es_info_bytes);
- es->es_info_bytes=NULL;
- }
- }
- }
- void KBDsmccReleaseAllPMTs(void)
- {
- int i=0;
- for(i=0; i<KBDsmccPMTs.count; i++)
- KBDsmccReleasePMT(&KBDsmccPMTs.pmts[i]);
- }
- KBDsmccEsArray *KBGetDsmccEsArrayPtr(void)
- {
- KBDsmccEsArray *ess=&KBDsmccEsAry;
- return ess;
- }
- KBServiceCfgDes *KBGetSerCfgDesPtr( char *typeString)
- {
- int i;
- if(KBSerCfgDesAry.count==0)
- return NULL;
-
- for(i=0; i<KBSerCfgDesAry.count; i++)
- {
- if(0==strcmp((const char*)KBSerCfgDesAry.servDes[i].CfgType, (const char*)typeString))
- {
- return &KBSerCfgDesAry.servDes[i];
- }
- }
-
- return NULL;
- }
- //printf the content of EsArray
- int KBPrintDsmccEsArray(void)
- {
- KBDsmccEsArray *ess=&KBDsmccEsAry;
- int i;
- DCDebug4("nn--------------------------------------------");
- for(i=0; i<ess->count; i++)
- {
- DCDebug4("n es_pid=%d",ess->es[i].es_pid);
- }
- DCDebug4("n--------------------------------------------nn");
- return (i+1);
- }