kb_dc.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:16k
- //*****************************************************************************
- //文件名:kb_dsmcc_carosel.c
- //
- //描 述 : Dsmcc 处理主文件.
- //
- //作 者:shriek
- //
- //日 期: 2007.3.01
- //
- //版 本: v1.0
- //*****************************************************************************
- //-----------------------------------------------------------------------------
- // Project include files:
- //-----------------------------------------------------------------------------
- #include "kb_dc_pub.h"
- #include "kb_dc.h"
- #include "stbmoviesinfo.h"
- extern unsigned char byFilterBuf[];
- extern void kb_dc_LoadCurTs(void);
- unsigned char * kb_dc_getMainFreqCfgBuf(int *xmlSize);
- /**
- * init dsmcc module
- * @return none.
- */
- void kb_dc_init(void)
- {
- int err;
- DCDebug2("nn-------------------kb_dsmcc_init new version----------------------n");
- err = KB_DC_DmxInit();
- if(err!=0)
- {
- DCDebug1("n kb_dsmcc_init erro:%d",err);
- return;
- }
- Section_Pool_Init();
- }
- /** kb_dc_getMainFreqCfg
- * get main frequency cfg file
- *
- *
- * @return.
- */
- int kb_dc_getMainFreqCfg(void)
- {
- unsigned char *xmlBuf=NULL;
- int xmlLen;
-
- xmlBuf = kb_dc_getMainFreqCfgBuf(&xmlLen);
- if(xmlBuf==NULL)
- {
- DCDebug1("n kb_dc_getMainFreqCfgBuf NULL!");
- return 0;
- }
- KB_XmlParse((char*)xmlBuf, xmlLen);
- KBDcFree(xmlBuf);
- xmlBuf=NULL;
- //装载数据
- KB_DC_ReloadXmlServCfgMap();
-
- return 1;
- }
- /**
- * get main frequency cfg file
- *
- *
- * @return.
- */
- unsigned char * kb_dc_getMainFreqCfgBuf(int *xmlSize)
- {
- unsigned char *buf=NULL, *sec=NULL;
- unsigned char *cfgFile=NULL;
- int dLen=0, secLen=0;
- char deffName[KB_DCFILE_NAME_MAX_LEN];
- unsigned short es_pid;
- unsigned short exId;
- KBServiceCfgDes *servDes=NULL;
- unsigned long freq, sym;
- unsigned char mod;
- int res;
- KB_DC_ServCfg_Map_Array *ServMapArry=NULL;
- static int cnt=0;
-
- #ifndef KB_DC_SERVICECFG_IN_WHOLENET_MODE
- #ifdef KB_DC_SERVICECFG_IN_MAINFREQ_MODE
- freq = 339000;
- sym = 6875;
- mod = 3;
- if(KB_DC_SetQam(freq, sym, mod)!=MB_SUCCESS)
- {
- DCDebug1("n main frequency %ld lock failure!", freq);
- return NULL;
- }
- else
- DCDebug1("n main frequency %ld lock success!", freq);
- #endif
- #endif
- ServMapArry = KB_GetXmlServCfgMapHead();
- if(ServMapArry==NULL) /*第一次搜索业务配置文件*/
- {
- if(cnt==0)
- {
- DCDebug2("n 第一次搜索业务配置文件");
- res =KBDsmccSipsiProc(0x00);
- if(res<=0)
- {
- DCDebug1("n KBDsmccSipsiProc failed");
- return NULL;
- }
- cnt++;
- }
-
- }
- else
- {
- ServMapArry->count = 0; /*清空数据*/
- }
- servDes = (KBServiceCfgDes *)KBGetSerCfgDesPtr("service_cfg_xml");
- if(servDes==NULL)
- {
- DCDebug1("n kb_dc_getMainFreqCfgBuf: servDes is NULL");
- return NULL;
- }
-
- //取transactionId 低16位
- exId = (unsigned short)((servDes->transaction_id)&0xffff);
- es_pid = servDes->es_pid;
- sprintf(deffName, "%s", servDes->CfgName);
- DCDebug3("n kb_dc_getMovieCfg: exid=%x", exId);
- dLen=KBDsmccGetDIITable(es_pid, exId, &buf);
- if(dLen<0)
- {
- buf =NULL;
- KBFreeSectionBuffer(buf);
- return NULL;
- }
-
- sec=buf;
- DCDebug2("Got DII...n");
- while(dLen>0)
- {
- secLen=(((unsigned short)(sec[1]<<8)|sec[2])&0x0fff)+3;
- if(ParseDIISection(deffName, sec, secLen)!=1)
- {
- DCDebug1("n ParseDIISection error");
- KBFreeSectionBuffer(buf);
- FreeDCFILE();
- FreeDCDIR();
- return NULL;
- }
- sec+=secLen;
- dLen-=secLen;
- }
- DCDebug2("parsed DII...n");
- //保存业务配置文件版本号
- kb_dc_SaveServCfgVersion(deffName);
- dLen = KBDsmccGetDDBTable(es_pid, (unsigned short)Get_firstModID_ByName(deffName), &buf);
- if(dLen<0)
- {
- DCDebug1("n KBDsmccGetDDBTable error");
- KBFreeSectionBuffer(buf);
- FreeDCFILE();
- FreeDCDIR();
- return NULL;
- }
- DCDebug2("Got DDB...n");
-
- sec=buf;
- while(TRUE)
- {
- secLen=(((unsigned short)(sec[1]<<8)|sec[2])&0x0fff)+3;
- res = ParseALLDDBSection(sec, (unsigned int)Get_firstModID_ByName(deffName));
- sec+=secLen;
- dLen-=secLen;
- if(res==1||res<0)
- break;
- }
- DCDebug2("Parsed DDB...n");
- KBFreeSectionBuffer(buf);
- if(cfgFile==NULL)
- {
- cfgFile=(unsigned char*)KBDcMalloc(GetDCFILE()->fileLength*sizeof(unsigned char));
- if(cfgFile==NULL)
- {
- DCDebug1("malloc error!n");
- FreeDCFILE();
- FreeDCDIR();
- return NULL;
- }
-
- KBDcMemset(cfgFile, 0x00, GetDCFILE()->fileLength*sizeof(unsigned char));
- KBDcMemcpy((void*)cfgFile, (const void*)GetDCFILE()->section, (size_t)GetDCFILE()->fileLength);
- *xmlSize=GetDCFILE()->fileLength;
-
- FreeDCFILE();
- FreeDCDIR();
- }
-
- //test only
- kb_dc_moduleInfo_Test();
-
- if(*xmlSize==0)
- return NULL;
- else
- return cfgFile;
- }
- /**
- * kb_dc_CfgDiiCheck
- * DII 监测,主要监测版本号
- *
- *
- * @return.
- */
- int kb_dc_CfgDiiCheckByType(unsigned char type)
- {
- unsigned char *buf=NULL, *sec=NULL;
- int dLen=0, secLen=0;
- char deffName[KB_DCFILE_NAME_MAX_LEN];
- unsigned short es_pid;
- unsigned short exId;
- KB_DC_ServCfg_Map *servMap=NULL;
- KBServiceCfgDes *servDes=NULL;
- DCDIR *dir;
- int res;
- if(type==SERV_CFG_MAIN)
- {
- servDes = (KBServiceCfgDes *)KBGetSerCfgDesPtr("service_cfg_xml");
- if(servDes==NULL)
- {
- DCDebug1("n kb_dc_CfgDiiCheckByType: servDes is NULL");
- return 0;
- }
- //取transactionId 低16位
- exId = (unsigned short)((servDes->transaction_id)&0xffff);
- es_pid = servDes->es_pid;
- sprintf(deffName, "%s", servDes->CfgName);
- }
- else
- {
- servMap = KB_GetCurServMapItem(type);
- if(servMap==NULL)
- {
- DCDebug1("n KB_GetCurServMapItem NULL! type=%d",type);
- return 0;
- }
-
- //取transactionId 低16位
- exId = (unsigned short)(servMap->m_transactionsId&0xffff);
- es_pid= servMap->m_nPid;
- sprintf(deffName, "Group_@_PID%d_StbCfg.xml", es_pid);
- }
-
- dLen=KBDsmccGetDIITable(es_pid, exId, &buf);
- if(dLen<0)
- {
- KBFreeSectionBuffer(buf);
- return 0;
- }
- DCDebug2("Got DII...n");
-
- sec=buf;
- while(dLen>0)
- {
- secLen=(((unsigned short)(sec[1]<<8)|sec[2])&0x0fff)+3;
- if(ParseDIISection(deffName, sec, secLen)!=1)
- {
- DCDebug1("n ParseDIISection error");
- KBFreeSectionBuffer(buf);
- FreeDCFILE();
- FreeDCDIR();
- return 0;
- }
- sec+=secLen;
- dLen-=secLen;
- }
- DCDebug2("parsed DII...n");
- dir = GetDCDIR();
- if(dir!=NULL) dir->pid = es_pid;
-
- //module version check
- if(kb_dc_CfgVerCheck((unsigned char *)deffName, type)) //||kb_dc_FreqIsChanged())
- {
- DCDebug1("n -[kb_dc_CfgDiiCheck]-module version be changed! return===");
- if(type==SERV_CFG_MAIN)
- {
- FreeDCFILE();
- FreeDCDIR();
- }
- res = 1;
- }
- else
- {
- if(type==SERV_CFG_MAIN)
- DCDebug2("n -[kb_dc_CfgDiiCheck]-service configure file version not updated! return===");
- else
- DCDebug2("n -[kb_dc_CfgDiiCheck]-movie configure file version not updated! return===");
-
- FreeDCFILE();
- FreeDCDIR();
- res = 0;
- }
- kb_dc_LoadCurTs();
- //free memory before exit
- KBFreeSectionBuffer(buf);
- sec=NULL;
-
- return res;
- }
-
- /**
- * kb_dc_CfgDiiCheck
- * DII 监测,主要监测版本号
- *
- *
- * @return.
- */
- int kb_dc_CfgDiiCheck(void)
- {
- KB_DC_ServCfg_Map *servMap=NULL;
- int flag=1;
- DCDebug2("n -----kb_dc_getCfgVersion-------");
-
- servMap = KB_GetCurServMapItem(SERV_CFG_ADVERTISE);
- if(servMap==NULL)
- {
- DCDebug2("n KB_GetCurServMapItem NULL! To recieve service configure files");
- //第一次搜索业务配置文件
- if(kb_dc_getMainFreqCfg()==0)
- {
- DCDebug1("n kb_dc_getMainFreqCfg failure!");
- return 0;
- }
- flag =1;
- }
- //如果刚搜索过业务配置文件,则不在同一接口再次检查版本号了
- if(flag==0)
- {
- if(kb_dc_CfgDiiCheckByType(SERV_CFG_MAIN)==1)
- {
- DCDebug2("n service configure version change! To update service configure files");
- if(kb_dc_getMainFreqCfg()==0)
- {
- DCDebug1("n kb_dc_getMainFreqCfg failure!");
- return 0;
- }
- }
- }
-
- return (kb_dc_CfgDiiCheckByType(SERV_CFG_ADVERTISE));
- }
- /**
- * get xml config file
- * xmlSize < pointer to storage size of xml file >
-
- * @return pointer of xml file
- */
- unsigned char *kb_dc_getCfgXml(unsigned long *xmlSize)
- {
- int res;
- unsigned char *cfgFile=NULL;
- char deffName[KB_DCFILE_NAME_MAX_LEN];
- unsigned char *buf=NULL, *sec=NULL;
- int dLen=0, secLen=0;
- pDCDIR dir;
- DCDebug2("n ----------kb_dsmcc_getCfgXml-------------n");
- dir = GetDCDIR();
- if(dir==NULL)
- {
- DCDebug1("kb_dc_getCfgXml: dir is NULLn");
- FreeDCFILE();
- return 0;
- }
- sprintf(deffName, "Group_@_PID%d_StbCfg.xml", dir->pid);
- dLen = KBDsmccGetDDBTable(dir->pid, (unsigned short)Get_firstModID_ByName(deffName), &buf);
- if(dLen<0)
- {
- KBFreeSectionBuffer(buf);
- FreeDCFILE();
- FreeDCDIR();
- return 0;
- }
- DCDebug2("Got DDB...n");
-
- sec=buf;
- while(TRUE)
- {
- secLen=(((unsigned short)(sec[1]<<8)|sec[2])&0x0fff)+3;
- res = ParseALLDDBSection(sec, (unsigned int)Get_firstModID_ByName(deffName));
- sec+=secLen;
- dLen-=secLen;
- if(res==1||res<0)
- break;
- }
- DCDebug2("Parsed DDB...n");
- KBFreeSectionBuffer(buf);
-
- if(cfgFile==NULL)
- {
- cfgFile=(unsigned char*)KBDcMalloc(GetDCFILE()->fileLength*sizeof(unsigned char));
- if(cfgFile==NULL)
- {
- DCDebug1("malloc error!n");
- FreeDCFILE();
- FreeDCDIR();
- return NULL;
- }
- KBDcMemset(cfgFile, 0x00, GetDCFILE()->fileLength*sizeof(unsigned char));
-
- KBDcMemcpy((void*)cfgFile, (const void*)GetDCFILE()->section, (size_t)GetDCFILE()->fileLength);
- *xmlSize=GetDCFILE()->fileLength;
- FreeDCFILE();
- FreeDCDIR();
- }
- //test only
- kb_dc_moduleInfo_Test();
-
- if(*xmlSize==0)
- return NULL;
- else
- return cfgFile;
- }
- void kb_dc_Test0(unsigned short index)
- {
- int i=0, k=0;
- DCFILE *file=NULL;
- DCDebug3("n --------------kb_dc_Test0----------------");
- file=GetDCFILE();
- DCDebug3("n module %d have total %d blocks ",index, file->BufStatus.modStat[index].blockCount);
- for(i=0;i<file->BufStatus.modStat[index].blockCount;i++)
- {
- if(file->BufStatus.modStat[index].blockStat[i]==FILLED)
- {
- k++;
- DCDebug3("n block %d been received!!!",i);
- }
- }
- DCDebug3("n total %d block been received!!!",k);
- k=0;
- for(i=0;i<file->BufStatus.modStat[index].blockCount;i++)
- {
- if(file->BufStatus.modStat[index].blockStat[i]==EMPTY)
- {
- k++;
- DCDebug3("n block %d not received!!!",i);
- }
- }
- DCDebug3("n total %d block not received!!!",k);
- }
- int kb_dc_OneByOneGet(unsigned short esPid, unsigned short firstModuleId, unsigned short index)
- {
- unsigned char *sec=NULL;
- int i=0, k=0;
- unsigned short secLen = 0;
- DCFILE *file=NULL;
- unsigned long ErrCode;
- int res = 0;
- DCDebug2("n --------------kb_dc_OneByOneGet----------------");
- kb_dc_Test0(index);
-
- file=GetDCFILE();
- for(i=0;i<file->BufStatus.modStat[index].blockCount;i++)
- {
- if(file->BufStatus.modStat[index].blockStat[i]==EMPTY)
- {
- k = 0;
- while(k<=5)
- {
- DCDebug3("n to re-receive block %d ",i);
- ErrCode = KB_DC_FilterReSet( esPid, 0x3c, firstModuleId+index, UNSET_PARAM, i);
- if ( ErrCode != Err_OK )return -1;
-
- KBDcMemset(byFilterBuf, EMPTY, PSI_SECTION_SIZE);
- ErrCode = KB_DC_GetSection(byFilterBuf, &secLen);
- if(Err_OK != ErrCode)
- {
- DCDebug1("nr Get block %d error[%d].rn",i, ErrCode );
- return -2;
- }
- if(0 != crc32(byFilterBuf, secLen))
- {
- k++;
- DCDebug1("[kb_dc_getSingleMovie]:Get block %d Crc error in filter process.rn" , i);
- if(k>5)
- return -3;
- else
- continue;
- }
- sec = byFilterBuf;
- res = ParseALLDDBSection(sec, firstModuleId);
-
- //数据全部收取完毕,跳出循环
- if(res==1)
- return 1;
- else if(res==0)
- break;
- else
- return -4;
-
- }
- }
- }
- return 0;
- }
- /**
- * get single movie data
- * @return
- */
- int kb_dc_getSingleMovie(unsigned short esPid, unsigned short firstModuleId,
- const unsigned char *fName, long fSize, unsigned char *dMovie)
- {
- unsigned char *sec=NULL;
- int i=0, modulesCnt=0, blocksCnt=0;
- unsigned short secLen = 0;
- unsigned short moduleId=firstModuleId;
- DCFILE *file=NULL, *pfile=NULL;
- int lastModuleSize=0;
- unsigned long ErrCode, Err_Counter = 0;
- int res;
- blocksCnt=(int)fSize/4066+(((int)fSize%4066==0)?0:1);
- modulesCnt=blocksCnt/256+((blocksCnt%256==0)?0:1);
- lastModuleSize=(int)fSize-(modulesCnt-1)*256*4066;
-
- pfile = (DCFILE*)KBDcMalloc(sizeof(DCFILE));
- if(pfile==NULL)
- {
- DCDebug1("nfile malloc error");
- return 0;
- }
- InitFileStruct(pfile);
-
- pfile->section = (unsigned char *)KBDcMalloc(fSize);
- if(pfile->section==NULL)
- {
- DCDebug1("n filesection malloc error");
- FreeOneDCFILE(pfile);
- return 0;
- }
- KBDcMemset(pfile->section, 0, fSize);
- //初始化buffer status
- if(kb_dc_bufferStatusBuild(pfile, blocksCnt) == 0)
- {
- DCDebug1("n kb_dc_bufferStatusBuild failed");
- FreeOneDCFILE(pfile);
- return 0;
- }
- //save pfile to gDCFile
- SetDCFILE(pfile);
- pfile = NULL;
- file=GetDCFILE();
- if(file==NULL)
- {
- DCDebug1("n GetDCFILE NULL");
- FreeOneDCFILE(file);
- return 0;
- }
-
- file->fileLength=fSize;
- strcpy((char*)file->fileName, (const char*)fName);
- file->position=0;
- DCDebug3("nn ----modulesCnt =%d blocksCnt=%d",modulesCnt, blocksCnt);
- for(i=0; i<modulesCnt; i++)
- {
- moduleId = firstModuleId + i;
- ErrCode = KB_DC_FilterReSet( esPid, 0x3c, moduleId, UNSET_PARAM, UNSET_PARAM);
- if ( ErrCode != Err_OK)
- {
- FreeDCFILE();
- return 0;
- }
- ResetTimeOut();
- while(kb_dc_moduleIsEnd(file,i)!=1)
- {
- ErrCode = KB_DC_GetSection(byFilterBuf, &secLen);
- if(Err_OK != ErrCode)
- {
- DCDebug1("nr Get section error[%d].rn",ErrCode );
- FreeDCFILE();
- return 0;
- }
- #if 1
- if(0 != crc32(byFilterBuf, secLen))
- {
- DCDebug1("[kb_dc_getSingleMovie]:block %d Crc error in filter process.rn", byFilterBuf[6]);
- Err_Counter++;
- if(Err_Counter>=30)
- {
- DCDebug1("[kb_dc_getSingleMovie]:Receive packet too error!rn");
- KB_DC_FilterStop();
- FreeDCFILE();
- return 0;
- }
- continue;
- }
- #endif
- sec = byFilterBuf;
- res = ParseALLDDBSection(sec, firstModuleId);
- //数据全部收取完毕,跳出循环
- if(res==1) break;
- //冗余处理
- if(res<0)
- {
- if(res == -1 )
- {
- DCDebug1("n parameter error, please check the cofigure file!");
- KB_DC_FilterStop();
- FreeDCFILE();
- return 0;
- }
- //循环次数过多,采用one-by-one filter mode收取剩余的block
- else if(res==-2)
- {
- KB_DC_FilterStop();
- KB_OSPTaskDelay(500);
- res = kb_dc_OneByOneGet(esPid, firstModuleId, i);
- if(res<0) //error in filtering
- {
- DCDebug1("n The oneByone get mode also failed[%d", res);
- FreeDCFILE();
- return 0;
- }
- else if(res==1) //all file data over
- {
- break;
- }
- else //one module data over, only break while
- {
- DCDebug4("n -just do nothing");
- }
-
- }
-
- }
-
- }
-
- }
- if(dMovie==NULL)
- {
- FreeDCFILE();
- return 0;
- }
- else
- {
- KBDcMemcpy((void*)dMovie, (const void*)GetDCFILE()->section, (size_t)GetDCFILE()->fileLength);
- FreeDCFILE();
- return 1;
- }
- }
-
- /**
- * get movie data by information
- * pInfo < information to get data and buf to storage data >
-
- * @return true, false otherwise.
- */
- bool kb_dc_getMovie(KB_MovieInfoNode *pInfo)
- {
- if(pInfo==NULL)
- return FALSE;
- DCDebug2("n KBDsmccGetMovie():pid=[%d], fMid=[%d], name=[%s], size=[%d]...n", pInfo->st_MovieInfo.m_nPid,
- pInfo->st_MovieInfo.m_nFirstModuleId, pInfo->st_MovieInfo.m_strName, pInfo->st_MovieInfo.m_nSize);
- if(!kb_dc_getSingleMovie(pInfo->st_MovieInfo.m_nPid, pInfo->st_MovieInfo.m_nFirstModuleId,
- pInfo->st_MovieInfo.m_strName, pInfo->st_MovieInfo.m_nSize,
- pInfo->st_MovieInfo.m_pData))
- {
- DCDebug2("n Got Movie error!!! n");
- return FALSE;
- }
- else
- DCDebug2("nGot Movie Sucess...n");
-
- return TRUE;
- }
- void PrintfData(unsigned char *buf, unsigned int len)
- {
- int i;
- unsigned int line = 0x00000000;
- DCDebug4("buf_len=[%d]n", len);
- for(i=0;i<len;i++)
- {
- if(i==0)
- {
- DCDebug4("n");
- DCDebug4("%04x: ",line);
- }
-
- DCDebug4("%4.2x",buf[i]);
- if((i+1)%16==0)
- {
- line = line + 16;
- DCDebug4("n");
- DCDebug4("%04x: ",line);
- }
-
- }
- DCDebug4("n print endn");
- return;
- }