kb_dc_sipsi.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:14k
源码类别:

DVD

开发平台:

C/C++

  1. //-----------------------------------------------------------------------------
  2. // Project include files:
  3. //-----------------------------------------------------------------------------
  4. #include "kb_dc_pub.h"
  5. static KBDsmccPat KBDsmccPAT;
  6. static KBDsmccPmts KBDsmccPMTs;
  7. static KBDsmccEsArray KBDsmccEsAry;
  8. static KBServiceCfgDesArry KBSerCfgDesAry;
  9. extern unsigned char byFilterBuf[];
  10. //用于存储DII version 结构
  11. static KBDSMCCDIIVERSIONARRAY curDiiVer;
  12. int KBDsmccGetPAT(unsigned int tsId, unsigned char *buf)
  13. {    
  14. int  sErr = Err_OK;
  15. unsigned short iDataSize = 0;
  16. tsId = tsId;
  17. if(KB_DC_FilterReSet(0x00, 0x00, UNSET_PARAM, UNSET_PARAM, 0)!=0)
  18. {
  19. DCDebug1("n KBDsmccGetPAT: KB_DC_FilterSet error!");
  20. return -1;
  21. }
  22. sErr  = KB_DC_GetSection(buf, &iDataSize);
  23. if(Err_OK != sErr)
  24. {
  25. DCDebug1("nrGet section error[%d].rn",sErr );
  26. return sErr;
  27. }
  28. return sErr;
  29. }
  30. int KBDsmccGetPMT(unsigned short pid, unsigned int proNo, unsigned char *buf)
  31. {
  32. //return KBDsmccGetTable(pid, 0x02, proNo, buf, 0);
  33. int  sErr = Err_OK;
  34. unsigned short iDataSize = 0;
  35. if(KB_DC_FilterReSet(pid, 0x02, proNo, UNSET_PARAM, 0)!=0)
  36. {
  37. DCDebug1("n KBDsmccGetPAT: KB_DC_FilterSet error!");
  38. return -3;
  39. }
  40. sErr  = KB_DC_GetSection(buf, &iDataSize);
  41. if(Err_OK != sErr)
  42. {
  43. DCDebug1("nrGet section error[%d].rn",sErr );
  44. return sErr;
  45. }
  46. return sErr;
  47. }
  48. int KBDsmccGetDIITable(unsigned short pid, unsigned short extId, unsigned char **buf)
  49. {
  50. return KBDcGetTable(pid, 0x3b, extId, buf, 0);
  51. //return KBDcGetTable(pid, 0x3b, PROGRAM_DONT_CARE, buf, 0);
  52. }
  53. int KBDsmccGetDDBTable(unsigned short pid, unsigned short moduleId, unsigned char **buf)
  54. {
  55. //return KBDsmccGetTable(pid, 0x3c, moduleId, buf, 0);
  56. return KBDcGetTable(pid, 0x3c, moduleId, buf, 0);
  57. }
  58. void KBFreeSectionBuffer(unsigned char * buf)
  59. {
  60. if(buf!=NULL)
  61. {
  62. KBDcFree(buf);
  63. buf=NULL;   
  64. }
  65. int KBDsmccSipsiProc(unsigned int tsId)
  66. {
  67. unsigned char *buf=NULL;
  68. int programCnt=0, esCnt=0, dsmccEsCnt=0;
  69. int i=0;
  70. UINT32  sErr = Err_OK;
  71. int res =0;
  72. //先初始化变量
  73. KB_DC_SipsiDataInit();
  74. buf = (unsigned char *)KBDcMalloc(PSI_SECTION_SIZE);
  75. if(buf==NULL) 
  76. {
  77.        return 0;
  78. }
  79. KBDcMemset(buf, 0x00, PSI_SECTION_SIZE);
  80. DCDebug3("n get PAT");
  81. sErr=KBDsmccGetPAT(tsId, buf);  
  82. if(Err_OK != sErr)
  83. {
  84. DCDebug1("nr KBDsmccGetPAT error[%d].rn",sErr );
  85. KBFreeSectionBuffer(buf);
  86. return 0;
  87. }
  88. programCnt=KBDsmccParsePAT(buf);
  89. DCDebug3("n got PAT");
  90. DCDebug2("n programCnt=%d",programCnt);
  91.        /*以防多次调用进行不必要的累加  shriek*/
  92. KBDcMemset(&KBDsmccPMTs, 0x00, sizeof(KBDsmccPmts));
  93. for(i=0; i<programCnt; i++)
  94. {
  95. KBDsmccPmt *pmt=NULL;
  96. KBDsmccPatInfo *patInfo=&KBDsmccPAT.pat_info[i];
  97. DCDebug3("nn---- patInfo->pmt_pid=%x,     patInfo->program_number=%x",patInfo->pmt_pid,patInfo->program_number);
  98. if(patInfo==NULL) 
  99. {
  100. KBFreeSectionBuffer(buf);
  101. return 0;
  102. }
  103. DCDebug3("n get PMT");
  104. sErr = KBDsmccGetPMT(patInfo->pmt_pid, patInfo->program_number, buf);
  105. if(Err_OK != sErr)
  106. {
  107. DCDebug1("nr KBDsmccGetPMT error[%d].rn",sErr );
  108. KBFreeSectionBuffer(buf);
  109. return 0;
  110. }
  111. DCDebug3("n got PMT");
  112. pmt = &KBDsmccPMTs.pmts[KBDsmccPMTs.count];
  113. esCnt=KBDsmccParsePMT(buf, pmt);
  114. if(esCnt<0)
  115. {
  116. KBFreeSectionBuffer(buf);
  117. KBDsmccReleaseAllPMTs();
  118. return 0;
  119. }
  120. KBDsmccPMTs.count++;
  121. }
  122. dsmccEsCnt=KBBuildDsmccEsArray();
  123. if(dsmccEsCnt==0) 
  124.     res = 0;
  125. else 
  126.     res =1;
  127. KBFreeSectionBuffer(buf);
  128. KBDsmccReleaseAllPMTs();
  129. return res;
  130. }
  131. int KBDsmccParsePAT(const unsigned char *pat_section)
  132. {
  133. const unsigned char *p=pat_section;
  134. KBDsmccPat *pat=&KBDsmccPAT;
  135. int cnt=0;
  136. int section_len=0, loop_len=0;
  137. unsigned short pid=0, number=0;
  138. p++;
  139. section_len=((unsigned short)(p[0]<<8)|p[1])&0x0fff;
  140. p+=2;
  141. pat->transport_stream_id=(unsigned short)(p[0]<<8)|p[1];
  142. p+=2;
  143. pat->version=(p[0]>>1)&0x1f;
  144. p++;
  145. p+=2;
  146. //info loop , no include nit 
  147. loop_len=section_len+3-8-4;
  148. while(loop_len>0)
  149. {
  150. number=(unsigned short)(p[0]<<8)|p[1];
  151. p+=2;
  152. pid=((unsigned short)(p[0]<<8)|p[1])&0x1fff;
  153. p+=2;
  154. if(number==0) 
  155. {
  156. loop_len-=2+2;
  157. continue;//skip nit
  158. }
  159. if((cnt+1)>KB_DSMCC_PAT_INFOS_MAX) break;
  160. pat->pat_info[cnt].pmt_pid=pid;
  161. pat->pat_info[cnt].program_number=number;
  162. cnt++;
  163. loop_len-=2+2;
  164. }
  165. //if(loop_len==0) DCDebug4("PAT loop length check OK=%d...n", loop_len);
  166. pat->pat_info_count=cnt;
  167. return cnt;
  168. }
  169. int KBDsmccParsePMT(const unsigned char *pmt_section, KBDsmccPmt *pPmt)
  170. {
  171. const unsigned char *p=pmt_section;
  172. KBDsmccPmt *pmt=pPmt;
  173. int section_len=0,  es_loop_len=0;
  174. int cnt=0;
  175.        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]);
  176. p++;
  177. section_len=((unsigned short)(p[0]<<8)|p[1])&0x0fff;
  178. p+=2;
  179. pmt->program_number=(unsigned short)(p[0]<<8)|p[1];
  180. p+=2;
  181. pmt->version=(p[0]>>1)&0x1f;
  182. p++;
  183. p+=2;
  184. pmt->pcr_pid=((unsigned short)(p[0]<<8)|p[1])&0x1fff;
  185. p+=2;
  186. pmt->info_length=((unsigned short)(p[0]<<8)|p[1])&0x0fff;
  187. p+=2;
  188. if(pmt->info_bytes!=NULL) 
  189. {
  190. KBDcFree(pmt->info_bytes); 
  191. pmt->info_bytes=NULL;
  192. }
  193. if(pmt->info_length!=0)
  194. {
  195. pmt->info_bytes=(unsigned char*)KBDcMalloc((UINT32)sizeof(unsigned char)*(UINT32)(pmt->info_length));
  196. if(pmt->info_bytes==NULL)
  197. {
  198. return -1;
  199. }
  200. KBDcMemset(pmt->info_bytes, 0x00, (UINT32)sizeof(unsigned char)*(UINT32)(pmt->info_length));
  201. KBDcMemcpy(pmt->info_bytes, p, pmt->info_length);
  202. }
  203. p+=pmt->info_length;
  204. es_loop_len=section_len+3-12-pmt->info_length-4;
  205. while(es_loop_len>0)
  206. {
  207. unsigned char stream_type=0;
  208. unsigned short es_pid=0;
  209. int len=0;
  210. unsigned char *temp=NULL;
  211. stream_type=*p++;
  212. es_pid=((unsigned short)(p[0]<<8)|p[1])&0x1fff;
  213. p+=2;
  214. if((cnt+1)>KB_DSMCC_PAT_INFOS_MAX) break;
  215. pmt->pmt_es[cnt].es_pid=es_pid;
  216. pmt->pmt_es[cnt].stream_type=stream_type;
  217. len=((unsigned short)(p[0]<<8)|p[1])&0x0fff;
  218. pmt->pmt_es[cnt].es_info_length=len;
  219. p+=2;
  220. if(temp!=NULL) 
  221. {
  222. KBDcFree(temp);
  223. temp=NULL;
  224. }
  225. if(len!=0)
  226. {
  227. temp=(unsigned char*)KBDcMalloc((UINT32)sizeof(unsigned char)*(UINT32)len);
  228. if(temp==NULL)
  229. {
  230. return -1;
  231. }
  232. KBDcMemcpy(temp, p, len);
  233. }
  234. pmt->pmt_es[cnt].es_info_bytes=temp;
  235. p+=len;
  236. cnt++;
  237. es_loop_len-=1+2+2+len;
  238. }
  239. pmt->pmt_es_count=cnt;
  240. return cnt;
  241. }
  242. const unsigned char*  KBGetDescriptor(unsigned char tag, const unsigned char *desBytes, int dlen)
  243. {
  244. const unsigned char *p=desBytes;
  245. while(dlen>0)
  246. {
  247. if(tag==p[0]) return p;
  248. dlen-=1+1+p[1];
  249. p+=1+1+p[1];
  250. }
  251. return NULL;
  252. }
  253. unsigned char KBParseDataBroadcastIdDescriptor(const unsigned char *dDes, int dLen, KBDsmccEs *es)
  254. {
  255. const unsigned char *p=dDes;
  256. if(p==NULL) return 0;
  257. es->data_broadcast_id=((unsigned short)(p[2]<<8))|p[3];
  258. p+=4;
  259. es->carousel_type_id=p[0]>>6;
  260. p++;
  261. es->transaction_id=((unsigned int)(p[0]<<24))|((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3];
  262. p+=4;
  263. es->DSI_timeout=((unsigned int)(p[0]<<24))|((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3];
  264. p+=4;
  265. es->DII_timeout=((unsigned int)(p[0]<<24))|((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3];
  266. p+=4;
  267. es->leak_rate=(((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3])&0x3fffff;
  268. p+=3;
  269. es->dii_version = *p;
  270. if(dLen==1+1+2+1+4*3+3+1) return 1;
  271. else return 0;
  272. }
  273. //parse private service configure descriptor
  274. unsigned char KBParseServCfgDescriptor(const unsigned char *dDes, int dLen, KBServiceCfgDes *servDes)
  275. {
  276. const unsigned char *p=dDes;
  277. unsigned int ctypelen =0, cnamelen=0;
  278. DCDebug3("n -[KBParseServCfgDescriptor]-: %x %x %x",dDes[0], dDes[1], dDes[2]);
  279. if(p==NULL) return 0;
  280. p+=2;
  281. ctypelen = p[0];
  282. if(ctypelen>KB_SERVICE_PRIVATEDES_NAME_MAX)
  283. {
  284. DCDebug1("n -[KBParseServCfgDescriptor]-ctypelen too overlong!");
  285. }
  286. KBDcMemcpy(servDes->CfgType, ++p, ctypelen);
  287. servDes->CfgType[ctypelen] = 0;
  288. p+=ctypelen;
  289. cnamelen = p[0];
  290. if(cnamelen>KB_SERVICE_PRIVATEDES_NAME_MAX)
  291. {
  292. DCDebug1("n -[KBParseServCfgDescriptor]-cnamelen too overlong!");
  293. }
  294. KBDcMemcpy(servDes->CfgName, ++p, cnamelen);
  295. servDes->CfgName[cnamelen] = 0;
  296. if(dLen == ctypelen+cnamelen+4)
  297. return 1;
  298. else
  299. {
  300. DCDebug1("n -[KBParseServCfgDescriptor]-dLen is error!");
  301. return 0;
  302. }
  303. }
  304. int KBBuildDsmccEsArray(void)
  305. {
  306. KBDsmccEsArray *ess=&KBDsmccEsAry;
  307. int i=0, j=0, k=-1,l =-1;
  308. for(i=0; i<KBDsmccPMTs.count; i++)
  309. {
  310. const KBDsmccPmt *pmt=&KBDsmccPMTs.pmts[i];
  311. for(j=0; j<pmt->pmt_es_count; j++)
  312. {
  313. const KBDsmccPmtEs *es=NULL;
  314. const unsigned char *dDes=NULL;
  315. es=&pmt->pmt_es[j];
  316. DCDebug3("n===es->stream_type=0x%02x", es->stream_type);
  317. if((es->stream_type==0x0C)||(es->stream_type==0x0B))
  318. {
  319. k++;
  320. ess->es[k].stream_type=es->stream_type;
  321. ess->es[k].es_pid=es->es_pid;
  322. ess->es[k].ts_id=KBDsmccPAT.transport_stream_id;
  323. //DataBroadcastIdDescriptor
  324. dDes=KBGetDescriptor(0x66, es->es_info_bytes, (int)es->es_info_length);
  325. if(dDes!=NULL)
  326. {
  327. KBParseDataBroadcastIdDescriptor(dDes, dDes[1]+2, &ess->es[k]);
  328. }
  329.     
  330. //service cfg descriptor in main frequency
  331. dDes=KBGetDescriptor(0x90, es->es_info_bytes, (int)es->es_info_length);
  332. if(dDes!=NULL)
  333. {
  334. l++;
  335. KBSerCfgDesAry.servDes[l].es_pid = ess->es[k].es_pid;
  336. KBSerCfgDesAry.servDes[l].transaction_id = ess->es[k].transaction_id;
  337. KBParseServCfgDescriptor(dDes, dDes[1]+2, &KBSerCfgDesAry.servDes[l]);
  338. KBSerCfgDesAry.count = l+1;
  339. DCDebug2("n --sercfgDes foud--!"); 
  340. DCDebug3("n es_pid=%d", KBSerCfgDesAry.servDes[l].es_pid);
  341. DCDebug3("n transaction_id=%d", KBSerCfgDesAry.servDes[l].transaction_id);
  342. DCDebug3("n CfgName=%s", KBSerCfgDesAry.servDes[l].CfgName);
  343. DCDebug3("n CfgType=%s", KBSerCfgDesAry.servDes[l].CfgType);
  344. }
  345. }
  346. }
  347. }
  348. ess->count=k+1;
  349. #if 1
  350. KBSerCfgDesAry.servDes[0].es_pid = 6200;
  351. strcpy((char *)KBSerCfgDesAry.servDes[0].CfgName, (const char *)"ServiceCfg.xml");
  352. strcpy((char *)KBSerCfgDesAry.servDes[0].CfgType, (const char *)"service_cfg_xml");
  353. KBSerCfgDesAry.count = 1;
  354. #endif
  355. DCDebug2("n Get dc data.dsmccEsCnt=%d", ess->count); 
  356. DCDebug2("n KBSerCfgDesAry.count=%d", KBSerCfgDesAry.count); 
  357. return (k+1);
  358. }
  359. #if 1
  360. /*-----------将ES DII version 信息存储--------*/
  361. int KB_DC_LoadCurDiiVerion(void)
  362. {
  363. int i,k=0;
  364. KBDSMCCDIIVERSIONARRAY KBDsmccDiiVersion; 
  365. DCDebug3("n ----KB_DC_LoadCurDiiVerion-----");
  366. KBDcMemset(&curDiiVer, 0x00, sizeof(KBDSMCCDIIVERSIONARRAY));
  367. //load
  368. for(i=0; i<KBDsmccEsAry.count; i++)
  369. {
  370. KBDsmccDiiVersion.esDiiVer[i].es_pid = KBDsmccEsAry.es[i].es_pid;
  371. KBDsmccDiiVersion.esDiiVer[i].dii_version =  KBDsmccEsAry.es[i].dii_version;
  372. KBDsmccDiiVersion.esDiiVer[i].ts_id =  KBDsmccEsAry.es[i].ts_id;
  373. k++;
  374. }
  375. KBDsmccDiiVersion.count = KBDsmccEsAry.count;
  376. //必须赋值为True,以标识不是开机第一次load
  377. KBDsmccDiiVersion.useFlag = True; 
  378. //store
  379. KBDcMemcpy(&curDiiVer, &KBDsmccDiiVersion , sizeof(KBDSMCCDIIVERSIONARRAY));
  380. return 1;
  381. }
  382. KBDSMCCDIIVERSIONARRAY *KB_DC_GetCurDiiVersionPtr(void)
  383. {
  384. return &curDiiVer;
  385. }
  386. // 功能:       监测DII 版本号是否更新,
  387. // 返回值:   是则返回1,
  388. //         否返回0
  389. //         错误返回-1
  390. int KB_DC_DiiVerCheck(unsigned int tsId)
  391. {
  392. int i=0;
  393. DCDebug3("n ---KB_DC_DiiVerCheck------");
  394.        //res = KBDsmccSipsiProc(0x00);
  395. //if(res<=0)
  396. //{
  397. // DCDebug4("n KB_DC_DiiVerCheck return error[%d]",res);
  398. // return -1;
  399. //}
  400. if(KBDsmccEsAry.count>=KB_DSMCC_ES_MAX)
  401. return -1;
  402. #if 1
  403. //开机第一次监测,应该返回1,强制去搜索配置文件
  404. if(curDiiVer.useFlag == False)
  405. {
  406. DCDebug3("n 开机第一次监测,所以返回1");
  407. curDiiVer.useFlag = True;
  408. return 1;
  409. }
  410. #endif
  411. DCDebug4("n ====KBDsmccEsAry.es[i].dii_version=%d", KBDsmccEsAry.es[0].dii_version);
  412. //判断DII version是否过期
  413. for(i=0; i<KBDsmccEsAry.count; i++)
  414. {
  415. DCDebug4("n KBDsmccEsAry: es_pid=%x, ts_id=%x", KBDsmccEsAry.es[i].es_pid, KBDsmccEsAry.es[i].ts_id);
  416. DCDebug4("n curDiiVer: es_pid=%x, ts_id=%x", curDiiVer.esDiiVer[i].es_pid, curDiiVer.esDiiVer[i].ts_id);
  417. if((curDiiVer.esDiiVer[i].es_pid==KBDsmccEsAry.es[i].es_pid)
  418. &&(curDiiVer.esDiiVer[i].ts_id==KBDsmccEsAry.es[i].ts_id))
  419. {
  420. DCDebug3("n cur_dii[%d]vs[%d]stream_dii", curDiiVer.esDiiVer[i].dii_version, KBDsmccEsAry.es[i].dii_version);
  421. if(curDiiVer.esDiiVer[i].dii_version!=KBDsmccEsAry.es[i].dii_version)
  422. {
  423. DCDebug3("n DII version is updated!!!!!!!!!!");
  424. //KB_DC_LoadCurDiiVerion();
  425. return 1;
  426. }
  427. else 
  428. {
  429. DCDebug3("n DII version not updated!");
  430. return 0;
  431. }
  432. }
  433. }
  434. return 1;
  435. }
  436. //--------------------------------end---------------------------------
  437. #endif
  438. void KB_DC_SipsiDataInit(void)
  439. {
  440. KBDcMemset(&KBDsmccEsAry, 0x00, sizeof(KBDsmccEsArray));
  441. KBDcMemset(&KBDsmccPMTs, 0x00, sizeof(KBDsmccPmts));
  442. KBDcMemset(&KBDsmccPAT, 0x00, sizeof(KBDsmccPat));
  443. KBDcMemset(&KBSerCfgDesAry, 0x00, sizeof(KBServiceCfgDesArry));
  444. }
  445. void KBDsmccReleasePMT(KBDsmccPmt *pmt)
  446. {
  447. int i=0;
  448. if(pmt==NULL) return;
  449. if(pmt->info_bytes!=NULL) 
  450. {
  451. KBDcFree(pmt->info_bytes);
  452. pmt->info_bytes=NULL;
  453. }
  454. for(i=0; i<pmt->pmt_es_count; i++)
  455. {
  456. KBDsmccPmtEs *es=&pmt->pmt_es[i];
  457. if(es==NULL) break;
  458. if(es->es_info_bytes!=NULL) 
  459. {
  460. KBDcFree(es->es_info_bytes);
  461. es->es_info_bytes=NULL;
  462. }
  463. }
  464. }
  465. void KBDsmccReleaseAllPMTs(void)
  466. {
  467. int i=0;
  468. for(i=0; i<KBDsmccPMTs.count; i++)
  469. KBDsmccReleasePMT(&KBDsmccPMTs.pmts[i]);
  470. }
  471. KBDsmccEsArray *KBGetDsmccEsArrayPtr(void)
  472. {
  473. KBDsmccEsArray *ess=&KBDsmccEsAry;
  474. return ess;
  475. }
  476. KBServiceCfgDes *KBGetSerCfgDesPtr( char *typeString)
  477. {
  478. int i;
  479. if(KBSerCfgDesAry.count==0)
  480. return NULL;
  481. for(i=0; i<KBSerCfgDesAry.count; i++)
  482. {
  483. if(0==strcmp((const char*)KBSerCfgDesAry.servDes[i].CfgType, (const char*)typeString))
  484. {
  485. return &KBSerCfgDesAry.servDes[i];
  486. }
  487. }
  488. return NULL;
  489. }
  490. //printf the content of EsArray
  491. int KBPrintDsmccEsArray(void)
  492. {
  493. KBDsmccEsArray *ess=&KBDsmccEsAry;
  494. int i;
  495.        DCDebug4("nn--------------------------------------------");
  496. for(i=0; i<ess->count; i++)
  497. {
  498.     DCDebug4("n   es_pid=%d",ess->es[i].es_pid);
  499. }
  500. DCDebug4("n--------------------------------------------nn");
  501. return (i+1);
  502. }