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

DVD

开发平台:

C/C++

  1. /******************************************************************************
  2. *   
  3. *    FileName   :kb_ota.c
  4. *    FileDesc   : This is the dtvia implementation file for mojo                                
  5. *
  6. ******************************************************************************/
  7. //-----------------------------------------------------------------------------
  8. // Standard include files:
  9. //-----------------------------------------------------------------------------
  10. //
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include <string.h>
  14. //-----------------------------------------------------------------------------
  15. // Project include files:
  16. //-----------------------------------------------------------------------------
  17. //
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include "Gendef.h"
  21. #include "appltype.h"
  22. #include "osp.h"
  23. #include "sti5105.h"
  24. #include "Dmd.h"
  25. #include "Dmx.h"
  26. #include "Timer.h"
  27. #include "db.h"
  28. #include "Eprom.h"
  29. #include "Timer.h"
  30. #include "kb_ota.h"
  31. #include "osd.h"
  32. #include "kb_porting.h"
  33. #include "kb_machblue_client_lc.h"
  34. #include "kb_machblue_client.h"
  35. //-----------------------------------------------------------------------------
  36. // Types and defines:
  37. //-----------------------------------------------------------------------------
  38. //
  39. static UInt32 _unused;
  40. #define UNUSED_PARAMETER2(x) _unused = (UInt32)(x);
  41. //priority and stack size of CA Client task
  42. #define OTA_CLIENT_PRIORITY (1*16) 
  43. #define OTA_CLIENT_STACK_SIZE (2 * 1024)
  44. //queue size and packet size of CA Client Queue
  45. #define OTA_CLIENT_QUEUE_SIZE 64
  46. #define OTA_CLIENT_PACKET_SIZE (sizeof(stCAClientMsg))
  47. //-----------------------------------------------------------------------------
  48. // Global data:
  49. //-----------------------------------------------------------------------------
  50. //
  51. typedef enum
  52. {
  53.     L_SCT_PAT = 0,
  54.     L_SCT_CAT,
  55.     L_SCT_PMT,
  56.     L_SCT_LPMT,
  57.     L_SCT_SDT,
  58.     L_SCT_BAT,
  59.     L_SCT_NIT,
  60.     L_SCT_DII,
  61.     L_SCT_DSI,
  62.     L_SCT_CTRL,
  63.     L_SCT_TDT,
  64.     L_SCT_DUMMY
  65. }CaClientFilterEnum;
  66. static TaskHandle_t  _hTask;
  67. static QueueHandle_t   _hQueue;
  68. QueueHandle_t   _OtaQueue;
  69. static Timer_Handle_t _lDownloadTimer = 0xffffffff;
  70. static Int32 _TimerCount;
  71. static Bool _TimerFlag;
  72. static UCHAR _uSectionHardMask[8];
  73. static UCHAR _uSectionHardValue[8];
  74. static UInt8 _bakBatSection[4 * 1024]; //For Bug101
  75. static UInt8 _bakPatSection[4*1024], _bakdlPmtSection[4*1024];
  76. static UInt8 _shareNitSection[4 * 1024], _bakNitSection[4 * 1024];
  77. static UInt8 _bakDSISection[4 * 1024];
  78. static TF_Linkage_Descriptor _astTFLinkage[MAX_IRDETO_LINKAGES];
  79. static UInt32 _lTFLinkageNum;
  80. static TF_Linkage_Descriptor * _pManuDlLinkage = NULL;
  81. static Bool _bCurrentNitValid, _bCurrentBatValid = False; //For Bug101
  82. static BOOL _flag_Download_Allowed;
  83. static UInt32  _dlFreq, _dlSymb, _dlQam;
  84. static UInt32 _dlPid;
  85. static Bool _bDownloadChecking = False;
  86. static Bool _bDownloadComparing = False;
  87. static Bool        _bPmtFirst = True;
  88. static Bool _bOnlyStopTimer = False;
  89. unsigned short _Error_Code = 255;
  90. //暂时使用
  91. UInt32   DmuxDrvDebugMsgLvl = 0x000; 
  92. //loader参数
  93.  TFLoaderPara  gKBloaderPara;
  94. Bool _bDownloadTrigger = False;
  95. static BOOL updateLock=True;
  96.  UInt8  OTAParabuf[68];
  97.  Int8 SectionCounter = 0;
  98. static BOOL updatePrevent=FALSE;
  99. static BOOL updateMainPrevent=FALSE;
  100. static BOOL OTAOsdRun =FALSE;
  101. static BOOL OTAManuEnable =FALSE;
  102. int g_menu_ota = FALSE;
  103. static BOOL gUpdateSearch=FALSE;
  104. int playlock=0;
  105. Ota_Hint_Message ota_message;
  106. //-----------------------------------------------------------------------------
  107. // Internal Function Prototypes:
  108. //-----------------------------------------------------------------------------
  109. //
  110. static StbErr_t PsiFilterSetup( DMX_FilterID lFilterId, UInt16 lPid, 
  111. UInt16 lTableId, UInt16 lTableIdExt, 
  112. UInt16 lVersion, UInt16 lSectionNumber, CaClientFilterEnum Num);
  113. StbErr_t KB_PsiFilter_Setup(DMX_FilterID lFilterId, UInt16 lPid, 
  114. UInt16 lTableId, UInt16 lTableIdExt, 
  115. UInt16 lVersion, UInt16 lSectionNumber, CaClientFilterEnum Num);
  116. void CommonfilterCallback(UINT32 nChannelID, UINT8 *pData, UINT32 nDataLength);
  117. static void OTAClientTask(void * dummy);
  118. TFLoaderPara   * ReadHLLoaderParams(void);
  119. Bool OTA_CLIENT_GetDLTuneParams(UInt32 * olFreq, UInt32 * olSymb, UInt32 * olModulate);
  120. static int GetDLTuneParams(void);
  121. int KB_BeginDownloadSearching(void);
  122. static void DownloadTimerCallback (void);
  123. void KB_ErrorMsg(UInt16 code);
  124. void WriteHLLoaderParams(void);
  125. static UInt16 ExtractDLPidFromPMT(void);
  126. static UInt16 ExtractDlPmtPidFromPat(void);
  127. static void FePara(UInt32 SaveLoad);
  128. static int StbStreamPara_Compare(unsigned short pid);
  129. void KB_UpdateCheckClear(void);
  130. static void ExtraceTFLinkage(void);
  131. static void ExtractManuDlLinkageEx(void);
  132. int GetUpgradeType(TF_Linkage_Descriptor  * pManuDlLinkage);
  133. unsigned char OTA_CLIENT_GetDownloadState(void);
  134. INT32 KB_DCFilter_Service(unsigned short pid);
  135. void MSG_RemainTimeDisplay(Int32 second, Bool flag);
  136. void KB_Flash_lc_MSGBox_Close(void);
  137. //-----------------------------------------------------------------------------
  138. // 函数实体
  139. //-----------------------------------------------------------------------------
  140. //
  141. //手动设置开关
  142. void KB_OTAManuEnableSet(BOOL flag)
  143. {       
  144. OTAManuEnable = flag;
  145. return;
  146. }
  147. BOOL KB_OTAManuEnableGet()
  148. {
  149.        return OTAManuEnable;
  150. }
  151. //空中升级开关
  152. void KB_UpdatePreventSet(BOOL flag)
  153. {       
  154. updatePrevent = flag;
  155. return;
  156. }
  157. BOOL KB_UpdatePreventGet()
  158. {
  159. printf("n KB_UpdatePreventGet=%d",updatePrevent );
  160.        return updatePrevent;
  161. }
  162. void KB_UpdateMainPreventSet(BOOL flag)
  163. {       
  164. updateMainPrevent = flag;
  165. return;
  166. }
  167. BOOL KB_UpdateMainPreventGet()
  168. {
  169.        return updateMainPrevent;
  170. }
  171. void KB_OTARunSet(BOOL flag)
  172. {       
  173. OTAOsdRun = flag;
  174. return;
  175. }
  176. BOOL KB_OTARunGet()
  177. {
  178.        return OTAOsdRun;
  179. }
  180. /*************************************************************************************
  181. GetUpdateSearchStatus
  182. Description: get Updating status
  183. Input: dummy - unused parameter
  184. Output: None
  185. Return: None
  186. *************************************************************************************/
  187. BOOL GetUpdateSearchStatus()
  188. {
  189.         return gUpdateSearch;
  190. }
  191. /*************************************************************************************
  192. setUpdateSearchStatus
  193. Description: set Updating status
  194. Input: dummy - unused parameter
  195. Output: None
  196. Return: None
  197. *************************************************************************************/
  198. void SetUpdateSearchStatus(BOOL flag)
  199. {
  200.         gUpdateSearch = flag;
  201. }
  202. void Set_DownloadChecking(BOOL flag)
  203. {
  204.         _bDownloadChecking = flag;
  205. }
  206. BOOL Get_DownloadChecking(void)
  207. {
  208.         return _bDownloadChecking;
  209. }
  210. void LockPlayPgm()
  211. {
  212.         playlock = 1;
  213. }
  214. /*************************************************************************************
  215. UnLockPlayPgm
  216. Description:打开视频播放锁
  217. Input: dummy - 
  218. Output: None
  219. Return: None
  220. *************************************************************************************/
  221. void UnLockPlayPgm(void)
  222. {
  223.         playlock = 0;
  224. }
  225. /*************************************************************************************
  226. GetLockPlayPgm
  227. Description: 关闭视频播放锁
  228. Input: dummy - 
  229. Output: None
  230. Return: None
  231. *************************************************************************************/
  232. int GetLockPlayPgm(void)
  233. {
  234.     return playlock;
  235. }
  236. /*************************************************************************************
  237. Ota_test2
  238. Description: test only 
  239. Input: dummy - 
  240. Output: None
  241. Return: None
  242. *************************************************************************************/
  243. void Ota_test2(void)
  244. {
  245.        KB_OTAManuEnableSet(False);
  246. }
  247.  int Kb_dmux_init(void)
  248. {
  249. INT32  nReturn;
  250. #define FILTER_DEPTH      (8)
  251. nReturn = KB_DmxAllocateChnnl(DMX_CHANNEL_SECTION, 5 * 1024, TRUE, &channelID);
  252. if(nReturn != 0)
  253. {
  254. printf("KD_DmxAllocateChnnl fail = %ldn",nReturn);
  255. return -1;
  256. }
  257. KB_DmxRegNotice(channelID, CommonfilterCallback);
  258. nReturn = KB_DmxAllocateFilter(DMX_SECTION_FILTER, FILTER_DEPTH, &filterID);
  259. if(nReturn != 0)
  260. {
  261. printf("KD_DmxAllocateFilter fail = %ldn",nReturn);
  262. return -1;
  263. }
  264. printf("n channelID=%ld",channelID);
  265. printf("n filterID=%ld",filterID);
  266. nReturn = KB_DmxAssociateFilter(channelID, filterID);  
  267. printf("n nReturn=%ld",nReturn);
  268. if(nReturn != 0)
  269. {
  270. printf("KD_DmxAssociateFilter fail = %ldn",nReturn);
  271. return -1;
  272. }
  273. return 0;
  274. }
  275. /*************************************************************************************
  276.   OTA_CLIENT_Service
  277. Description: Send message to CA Client task for service
  278. **************************************************************************************/
  279. void OTA_CLIENT_Service (KB_OSPMsgNode *pMsg)
  280. {
  281.  //printf("n OTA_CLIENT_Service:Word1=%ld   Word2=%ld", pMsg->Word1, pMsg->Word2);
  282.  KB_OSPMsgSend(_hQueue, pMsg);
  283. }
  284. /*************************************************************************************
  285.   OTA_lc_Service
  286. Description: Send message to CA Client task for service
  287. **************************************************************************************/
  288. void OTA_lc_Service (KB_OSPMsgNode *pMsg)
  289. {
  290.  printf("n OTA_lc_Service:Word1=%ld", pMsg->Word1);
  291.  KB_OSPMsgSend(_OtaQueue, pMsg);
  292. }
  293. Int8 KB_PsiFilter_Stop(void)
  294. {
  295.  KB_DmxControlChnnl(channelID, KB_DMX_STOP);   
  296.  SectionCounter--;  
  297.  //printf("n KB_PsiFilter_Stop return: %d", SectionCounter);
  298.  return  SectionCounter;
  299. }
  300. StbErr_t KB_PsiFilter_Setup(DMX_FilterID lFilterId, UInt16 lPid, 
  301. UInt16 lTableId, UInt16 lTableIdExt, 
  302. UInt16 lVersion, UInt16 lSectionNumber, CaClientFilterEnum Num)
  303. {
  304. StbErr_t Ret;
  305. Ret = PsiFilterSetup( lFilterId, lPid, lTableId, lTableIdExt, lVersion, lSectionNumber, Num);
  306. if(Ret != TM_OK) 
  307. {
  308.      Print("n[KB_PsiFilter_Setup]Error: set filter failed.n");
  309.      SectionCounter=0;
  310.      return Ret;
  311. SectionCounter=1;
  312. return Ret;
  313. }
  314. /*************************************************************************************
  315. CommonFilterCallback
  316. Description: When cat, sdt or pmt reached, this function would be called.
  317. Input: tFilteriD -  filter ID
  318. pstPacket - section data
  319. dummy * - dummy parameter
  320. Output: None
  321. Return: Always 0
  322. *************************************************************************************/
  323. void CommonfilterCallback(UINT32 nChannelID, UINT8 *pData, UINT32 nDataLength)
  324. {
  325.        KB_OSPMsgNode   MsgCaClient;        
  326.  Int8 res;
  327. if(pData == NULL)
  328. return;
  329. if(nDataLength > 1024)
  330. {
  331. Print("rn[CA_CLIENT]Section is too largern");
  332. while(1);
  333. }
  334. switch (pData[0]) //tableid
  335. {
  336.     case 0x00:  // PAT:
  337.         Print("nCommonfilterCallback:pat recieved");
  338. res = KB_PsiFilter_Stop();
  339. if(res<0) break;
  340.  memcpy(_bakPatSection, pData, nDataLength);
  341.         MsgCaClient.Word1 = E_PAT_REACH;
  342.         MsgCaClient.Word2  = nDataLength;
  343.         OTA_CLIENT_Service (&MsgCaClient);
  344.         break;
  345.         
  346.     case 0x02:  // PMT      
  347.       Print("nCommonfilterCallback:pmt recieved");
  348.               res = KB_PsiFilter_Stop();
  349. if(res<0) break;
  350. memcpy(_bakdlPmtSection, pData, nDataLength);
  351. MsgCaClient.Word1 = E_PMT_REACH;
  352. MsgCaClient.Word2  = nDataLength;
  353. OTA_CLIENT_Service (&MsgCaClient);
  354. break;
  355.     case 0x4A:  // BAT
  356. Print("nCommonfilterCallback:BAT recieved");
  357. res = KB_PsiFilter_Stop();
  358. if(res<0) break;
  359. memcpy(_bakBatSection, pData, nDataLength);
  360. MsgCaClient.Word1 = E_BAT_REACH;
  361. MsgCaClient.Word2  = nDataLength;
  362. OTA_CLIENT_Service (&MsgCaClient);
  363. break;
  364.     case 0x40:  // NIT
  365. Print("nCommonfilterCallback:NIT recieved");
  366.  res = KB_PsiFilter_Stop();
  367. if(res<0) break;
  368. memcpy(_bakNitSection, pData, nDataLength);        
  369. MsgCaClient.Word1 = E_NIT_REACH;
  370. MsgCaClient.Word2  = nDataLength;         
  371. OTA_CLIENT_Service (&MsgCaClient);
  372.         break;
  373.      case 0x3b:  // DII DSI
  374. Print("nCommonfilterCallback:DSI recieved");
  375. res = KB_PsiFilter_Stop();
  376. if(res<0) break;
  377. memcpy(_bakDSISection, pData, nDataLength);        
  378. MsgCaClient.Word1 = E_DSI_REACH;
  379. MsgCaClient.Word2  = nDataLength;         
  380. OTA_CLIENT_Service (&MsgCaClient);
  381. break;
  382. }
  383. return;
  384. }
  385. /**************************************************************************************
  386. PsiFilterSetup
  387. Description: Set up psi filter for section receiving
  388. Input: lFilterId - filter ID
  389. lPid, lTableId, lTableIdExt, lVersion, lSectionNumber:    when set as UNSET_PARAM, 
  390. then this parameter unused. If lVersion setted, version check function
  391. would be launched.
  392. Output:
  393. None
  394. Return:
  395. None
  396. **************************************************************************************/
  397. static StbErr_t PsiFilterSetup(DMX_FilterID lFilterId, UInt16 lPid, 
  398. UInt16 lTableId, UInt16 lTableIdExt, 
  399. UInt16 lVersion, UInt16 lSectionNumber, CaClientFilterEnum Num)
  400. {
  401. unsigned char  byte1, byte2;
  402. StbErr_t sErr = Err_OK;
  403.     KB_DmxControlChnnl(channelID, KB_DMX_START);     
  404. memset(_uSectionHardMask, 0, 8);
  405. if(lTableId != UNSET_PARAM)
  406. {
  407. _uSectionHardMask[0]  = 0xff;
  408. _uSectionHardValue[0] = (UCHAR)lTableId;
  409. }
  410. if(lTableIdExt != UNSET_PARAM)
  411. {
  412. byte1 = (UCHAR)((lTableIdExt >> 8) & 0xff);
  413. byte2 = (UCHAR)(lTableIdExt & 0xff);
  414. _uSectionHardMask[3]  = 0xff;
  415. _uSectionHardMask[4]  = 0xff;
  416. _uSectionHardValue[3] = byte1;
  417. _uSectionHardValue[4] = byte2;
  418. if(lTableId == 0xfe && byte2 == 0xff)
  419. {
  420. _uSectionHardMask[2]  = 0x00;
  421. }
  422. }
  423. if(lVersion != UNSET_PARAM)
  424. {
  425. _uSectionHardMask[5]  = 0x02;
  426. _uSectionHardValue[5] = (UInt8)(~lVersion);
  427. }
  428. if(lSectionNumber != UNSET_PARAM)
  429. {
  430. _uSectionHardMask[6]  = 0xff;
  431. _uSectionHardValue[6] = (UInt8)lSectionNumber;
  432. }
  433. #if 0
  434.        printf("nvalue[8]={%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x}", _uSectionHardValue[0], _uSectionHardValue[1],_uSectionHardValue[2], _uSectionHardValue[3],
  435. _uSectionHardValue[4], _uSectionHardValue[5],_uSectionHardValue[6], _uSectionHardValue[1]);
  436. printf("nmask[8]= {%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x}", _uSectionHardMask[0], _uSectionHardMask[1],_uSectionHardMask[2], _uSectionHardMask[3],
  437. _uSectionHardMask[4], _uSectionHardMask[5],_uSectionHardMask[6], _uSectionHardMask[1]);
  438. #endif
  439. KB_DmxSetChnnlPID(channelID, lPid);
  440. sErr = KB_DmxSetFilterByType(lFilterId, _uSectionHardValue, _uSectionHardMask, 0, STPTI_FILTER_REPEAT_MODE_STPTI_FILTER_REPEATED/*STPTI_FILTER_REPEAT_MODE_STPTI_FILTER_ONE_SHOT*/);
  441. if(Err_OK != sErr)
  442. {
  443. return sErr;
  444. }
  445.    
  446.        return (sErr);
  447. }
  448. void test3()
  449. {
  450. char *pszInfo;
  451. char buf[40];
  452. if(KB_Flash_lc_MSGBox(NULL)==IDOK)
  453. {
  454. printf("n test3: seclect ok");
  455. pszInfo="升级过程大致需要%d分钟,5秒后开始升级。";
  456. if(pszInfo != NULL)
  457. sprintf(buf, pszInfo, 9); 
  458. KB_Flash_lc_MSGBox(buf);
  459. }
  460. else
  461. printf("n test3:seclect exit");
  462. }
  463. /**************************************************************************************
  464.  CA_CLIENT_Init
  465. Description: CA Client Initialization, 
  466. Input: None
  467. Output: None
  468. Return: None
  469. Error Debug: Enabled.
  470. **************************************************************************************/
  471. void OTA_CLIENT_Init(void)
  472. {
  473.        StbErr_t  Ret;
  474. KB_OSPRet res;
  475.        printf("nn---------[huqs]:OTA_CLIENT_Init()-----------");
  476. #ifdef OTA_TEST1
  477. {
  478. FePara(0);
  479. }
  480. #endif
  481. //read loader infomation from STB
  482. ReadHLLoaderParams();
  483. // Message queue create. Task create
  484. res = KB_OSPQueInit("OTA", OTA_CLIENT_QUEUE_SIZE, &_hQueue);
  485. res = KB_OSPQueInit("OTA2", OTA_CLIENT_QUEUE_SIZE, &_OtaQueue);
  486. res|= KB_OSPTaskInit("OTA", OTA_CLIENT_STACK_SIZE, OTAClientTask, OTA_CLIENT_PRIORITY, NULL, &_hTask);
  487. if(res!= Ret_OK)
  488. {
  489.      Print("[OTA Client]Error: create OTA client task failed.n");
  490. }
  491. //timer
  492.  _lDownloadTimer = KB_TimerCreate(KB_TIMER_REPEAT, (KB_TIMER_FUNC_POINTER)DownloadTimerCallback, NULL);
  493. //setup filter for BAT processing
  494. Kb_dmux_init();
  495. //Ret = KB_PsiFilter_Setup(_lOTAFilter, 0x11, 0x4A, 0xff00, UNSET_PARAM, 0, L_SCT_BAT);
  496. //if(Ret != TM_OK) 
  497. //{
  498. //     Print("n[OTA Client]Error: set filter for BAT failed.n");
  499. //} 
  500. }
  501. /*************************************************************************************
  502. KB_DCFilter_Service
  503. Description: 
  504. Input: dummy - unused parameter
  505. Output: None
  506. Return: None
  507. *************************************************************************************/
  508. INT32 KB_DCFilter_Service(unsigned short pid)
  509. {
  510. INT32 HdmxRet;
  511. UInt32 Freq, Symb, Qam;
  512. printf("n -KB_DCFilter_Service-");
  513. OTA_CLIENT_GetDLTuneParams(&Freq, &Symb, &Qam);
  514. #ifdef OTA_TEST1
  515. if(KB_CHN_SetQam(299000000, 6875000, 3)==0)
  516. #else
  517. if(KB_CHN_SetQam(Freq, Symb, Qam)==0)
  518. #endif
  519. printf("n KB_DCFilter_Service: setqam(%ld)  ok!", Freq);
  520. HdmxRet = KB_PsiFilter_Setup(_lOTAFilter, pid, 0x3b, UNSET_PARAM, UNSET_PARAM,0, L_SCT_DSI);
  521. if(HdmxRet != TM_OK) 
  522. {
  523. Print("n[OTA Client]Error: set filter for DII failed.");
  524. }
  525. }
  526. else
  527. {
  528. printf("nn-------- KB_DCFilter_Service: can't setqam(%ld)", Freq);
  529. KB_Timer_Stop(_lDownloadTimer);
  530. OSA_wait(1000);
  531. KB_ErrorMsg(IRD_DL_CONN_FAIL);
  532. KB_UpdateCheckClear();
  533. }
  534. return HdmxRet;
  535. }
  536. /*************************************************************************************
  537. CAClientTask
  538. Description: CA Client task processing function
  539. Input: dummy - unused parameter
  540. Output: None
  541. Return: None
  542. *************************************************************************************/
  543. static void OTAClientTask(void * dummy)
  544. {
  545. KB_OSPMsgNode Msg;
  546. UInt16      pmtPid, ctrlPid;
  547. KB_OSPMsgNode   MsgCaClient;
  548. INT32 HdmxRet;
  549. static UInt8 current_section=0;
  550. char   *info;
  551. int otatype;
  552. int ota_count = 0;
  553. INT32 status;
  554. int nres=-1;
  555. printf("rnn------OTAClientTask------");
  556. UNUSED_PARAMETER2(dummy);
  557. _flag_Download_Allowed = 1;
  558.           
  559. while(1)
  560. {
  561. status = KB_OSPMsgGet(_hQueue, KB_Wait, 0, &Msg);
  562. if(status != Ret_OK)
  563. {
  564. continue;
  565. }
  566. if(_bDownloadChecking == True)
  567. {
  568.            switch(Msg.Word1)
  569.            {
  570.               //*****************************
  571.          case E_DOWNLOAD_SEARCHING:
  572.               //*****************************
  573.                      Print ("nnE_DOWNLOAD_SEARCHING");                 
  574.                  
  575. //reset timer
  576. _TimerCount = 27;  _TimerFlag = True;
  577. KB_Timer_Start(_lDownloadTimer, 1000);
  578.       
  579. OSA_wait(20);
  580.               HdmxRet = KB_PsiFilter_Setup(_lOTAFilter, 0, 0, UNSET_PARAM, UNSET_PARAM, 0, L_SCT_PAT);
  581.               if(HdmxRet != TM_OK) 
  582.               {
  583.             Print("n[OTA Client]Error: set filter for pat failed.n");
  584.               }
  585. OSA_wait(100);
  586.          break;
  587.         
  588.               //*****************************
  589.          case E_PAT_REACH:
  590.               //*****************************
  591.                      Print ("n---E_PAT_REACH---");
  592.          pmtPid = ExtractDlPmtPidFromPat();
  593. printf("n-pmtPid=%04x",pmtPid);
  594.          if (pmtPid != 0x1FFF)
  595.          {
  596.                     KB_PsiFilter_Setup(_lOTAFilter, pmtPid, 0x02, UNSET_PARAM, UNSET_PARAM, 0, L_SCT_LPMT);
  597.       
  598.                 }
  599. else
  600. {
  601.                            KB_Timer_Stop(_lDownloadTimer);
  602. OSA_wait(200);
  603.                             KB_ErrorMsg(IRD_DL_NOT_PRESENT);
  604. }
  605.          break;
  606.               //*****************************
  607.          case E_DSI_REACH:
  608.               //*****************************
  609. Print ("n---E_DSI_REACH---");
  610.  if(_pManuDlLinkage!=NULL&&_bDownloadComparing)
  611.  {
  612. _bOnlyStopTimer = True;
  613. nres = StbStreamPara_Compare(ctrlPid);
  614. if(nres>0)
  615. {
  616. printf("n StbStreamPara_Compare>0, ok");
  617. FePara(0);
  618. ota_count = 0;
  619. KB_ErrorMsg(IRD_DL_PRESENT);
  620. _bDownloadChecking = False;
  621. }
  622. else
  623. {
  624. printf("n StbStreamPara_Compare<0, error,   res=%d", nres);
  625. KB_Timer_Stop(_lDownloadTimer);
  626. OSA_wait(200);
  627. if(nres==-1||nres==-2||nres==-3)
  628. KB_ErrorMsg(IRD_DL_PARA1_NOT_EQUAL);
  629. else if(nres==-4)
  630. KB_ErrorMsg(IRD_DL_PARA2_NOT_EQUAL);
  631. OSA_wait(200);
  632. KB_OSD_ClearAll(0xf000);
  633. _bDownloadChecking = False;
  634. }
  635.  }
  636.  
  637.                       break;
  638.   
  639.               //*****************************
  640.          case E_PMT_REACH:
  641.               //*****************************
  642.                      Print ("n---E_PMT_REACH---");
  643.                     KB_Timer_Stop(_lDownloadTimer);
  644.       _TimerCount = 26; _TimerFlag = False; //for control section
  645.          MSG_RemainTimeDisplay(_TimerCount, _TimerFlag);
  646.          KB_Timer_Start(_lDownloadTimer, 1000);
  647.   
  648.          ctrlPid = ExtractDLPidFromPMT();
  649. _dlPid = ctrlPid;
  650. printf("n--ctrlpid=%x",ctrlPid);
  651.          if(ctrlPid == 0x1FFF)
  652.          {
  653.          KB_Timer_Stop(_lDownloadTimer);
  654. OSA_wait(200);
  655.          KB_ErrorMsg(IRD_DL_NOT_PRESENT);
  656.          _bDownloadChecking = False;
  657.          }
  658.          else
  659.          {
  660. #ifdef OTA_TEST1
  661. KB_DCFilter_Service(0x80);
  662. #else
  663. KB_DCFilter_Service(ctrlPid);
  664. #endif
  665. _bDownloadComparing = True;
  666.          }
  667.          break;       
  668.             
  669.           }
  670.  }
  671.         else
  672.         {
  673.             switch (Msg.Word1)
  674.             {
  675.                 //*****************************
  676.                 case E_DOWNLOAD_CHECKING:
  677.                 //*****************************
  678.                             Print ("nn[CaClientTask]:begin to E_DOWNLOAD_CHECKING");
  679.          if(GetDLTuneParams()==0)
  680.          {
  681.          if(KB_BeginDownloadSearching()<0)
  682.                    _bDownloadChecking = False;
  683. else
  684.           _bDownloadChecking = True;
  685.          }
  686.          else if(GetDLTuneParams()<0)
  687.          {    
  688.           if(Msg.Word2== True)
  689.           {
  690.                  _bDownloadChecking = False;
  691.                         KB_ErrorMsg(IRD_DL_NIT_FAIL);
  692.           }
  693.          }
  694.                            break;                      
  695.                     
  696.                 //*****************************
  697.                 case E_STOP_CURRENT_SERVICE:
  698.                 //*****************************
  699.                     Print ("n[CaClientTask] E_STOP_CURRENT_SERVICEn");
  700.                     if(_bPmtFirst == False)
  701.                     {
  702.                         if (DmuxDrvDebugMsgLvl & 0x10)
  703.                             Print ("{Send to CA_TASK} CA_CLIENT__STOP_SERVICEn");
  704.                     }
  705.                     break;
  706.                     
  707.                 //*****************************
  708.                 case E_NEW_TS_ASSOCIATED:
  709.                 //*****************************
  710.                     printf ("n--[OTAClientTask]   E_NEW_TS_ASSOCIATEDn");
  711. HdmxRet = KB_PsiFilter_Setup(_lOTAFilter, 0x11, 0x4A, 0xff00, UNSET_PARAM, 0, L_SCT_BAT);
  712. if(HdmxRet != TM_OK) 
  713. {
  714. printf("n[OTA Client]Error: set filter for BAT failed.n");
  715.                     
  716.                      _bCurrentBatValid = False;
  717.            _pManuDlLinkage = NULL;
  718.                      break;
  719.                     
  720.                 //*****************************
  721.                 case E_CURRENT_TS_DEASSOCIATED:
  722.                 //*****************************
  723. printf ("n[CaClientTask]  E_CURRENT_TS_DEASSOCIATEDn");    
  724. HPIv2_HDMX_FilterStop();      
  725. break;                    
  726.                                        
  727.                                 
  728.                 //*****************************
  729.                 case E_BAT_REACH:
  730.                 //*****************************
  731.                      Print ("n[OTAClientTask] E_BAT_REACH: updatePrevent=%d", updatePrevent); 
  732. if(KB_OTA_IsPermit()==FALSE) break;
  733.   
  734. _bCurrentBatValid = True;            
  735.              ExtraceTFLinkage();
  736.              ExtractManuDlLinkageEx();
  737. if(_pManuDlLinkage==NULL)
  738. {
  739. printf("n _pManuDlLinkage=NULL");
  740. //KB_UpdatePreventSet(True);
  741. KB_OTAManuEnableSet(False);
  742. KB_UpdateCheckClear();
  743. break;
  744. }
  745.             
  746.                      if(!OTA_CLIENT_GetDownloadState())
  747.                      {
  748. SetUpdateSearchStatus(FALSE);
  749. break;
  750.                      }
  751.  
  752.                      //启动nit 搜索  
  753.                      MsgCaClient.Word1 = E_FILTER_SETPU;
  754.                      MsgCaClient.Word2  = E_NIT_FILTER_SETUP;     
  755. MsgCaClient.Word3  = current_section;    
  756.                      OTA_CLIENT_Service (&MsgCaClient);
  757.                      break;
  758.     //*****************************
  759.     case E_FILTER_SETPU:
  760.     //*****************************
  761.         Print ("nn-[OTAClientTask]- E_FILTER_SETPU ----current_section= %ld",Msg.Word3);          
  762.                       if(Msg.Word2==E_NIT_FILTER_SETUP)
  763.                       {
  764.                             //HPIv2_HDMX_FilterStop();
  765.                      HdmxRet = KB_PsiFilter_Setup(_lOTAFilter, 0x10, 0x40, UNSET_PARAM, UNSET_PARAM, Msg.Word3, L_SCT_NIT);
  766.                      if(HdmxRet != TM_OK) 
  767.                      {
  768.                    Print("n[OTA Client]Error: set filter for NIT failed.");
  769.                      }
  770.                       }
  771.                       else if(Msg.Word2 == E_PAT_FILTER_SETUP)
  772.                       {
  773.        HPIv2_HDMX_FilterStop();
  774.                             HdmxRet = KB_PsiFilter_Setup(_lOTAFilter, 0, 0, UNSET_PARAM, UNSET_PARAM, 0, L_SCT_PAT);
  775.  if(HdmxRet != TM_OK) 
  776.                      {
  777.                    Print("n[OTA Client]Error: set filter for PAT failed.");
  778.                      }
  779.                       }
  780.  
  781.  OSA_wait(100);
  782.  
  783.         break;
  784.                 //*****************************
  785.                 case E_NIT_REACH:
  786.                 //*****************************
  787.                    Print ("nn-[OTAClientTask] E_NIT_REACH "); 
  788.      if(KB_OTA_IsPermit()==FALSE) break;
  789.                    memcpy(_shareNitSection, _bakNitSection, Msg.Word2);   
  790.                     _bCurrentNitValid = True;
  791.                     //搜到nit后启动检查
  792.       if(_bCurrentNitValid == True)
  793.              {
  794.              if(_pManuDlLinkage != NULL)
  795.              {
  796.                     SetUpdateSearchStatus(TRUE);
  797.              if(_pManuDlLinkage->control_byte_2 == 1 || _pManuDlLinkage->control_byte_2 == 2)
  798.              {
  799. otatype =GetUpgradeType(_pManuDlLinkage);
  800. printf("n otatype=%d", otatype);
  801. if(otatype==OTA_INVALID)
  802. {
  803. KB_UpdatePreventSet(True);
  804. KB_OTAManuEnableSet(False);
  805. //stop the filter
  806. KB_UpdateCheckClear();
  807. break;
  808. }
  809. else
  810. {
  811. KB_UpdatePreventSet(False);
  812. KB_OTAManuEnableSet(True);
  813. }
  814. if(g_menu_ota)
  815. otatype = OTA_USER_CONFORMED;
  816.        //关闭当前后续和频道列表
  817.        KB_CurMovie_DispClear();  
  818. //强制升级
  819. if(otatype==OTA_FORCED)
  820. {
  821.        
  822. printf("nE_NIT_REACH reach, and start download checking.");
  823. MsgCaClient.Word1 = E_DOWNLOAD_CHECKING;
  824. MsgCaClient.Word2  = TRUE;          //show the error message
  825. OTA_CLIENT_Service (&MsgCaClient);
  826. KB_OTARunSet(True);
  827. g_menu_ota =FALSE;
  828. }
  829. //用户许可升级
  830. else if(otatype==OTA_USER_CONFORMED)  
  831. {
  832. KB_OTARunSet(True);
  833. g_menu_ota =FALSE;
  834. if(ota_count>0)
  835. break;
  836. ota_count ++;
  837. if(KB_Flash_lc_MSGBox(NULL) == IDOK)
  838. {
  839. printf("n user seclect to update! go E_DOWNLOAD_CHECKING");
  840. OSA_wait(200);
  841. MsgCaClient.Word1 = E_DOWNLOAD_CHECKING;
  842. MsgCaClient.Word2  = TRUE;          //show the error message
  843. OTA_CLIENT_Service (&MsgCaClient);
  844. else
  845. {
  846. KB_Flash_lc_MSGBox_Close();
  847. KB_OSD_ClearAll(0xF000);
  848. OSA_wait(200);
  849. otatype = OTA_MANUL;
  850. KB_UpdatePreventSet(True);
  851. DTV_EPG_SetToLastProg();
  852. }
  853. }
  854. ota_count =0;
  855. //手动升级
  856. if(otatype==OTA_MANUL)
  857. {
  858. printf("n 手动升级");
  859. KB_UpdateCheckClear();
  860. break;
  861. }
  862.              }
  863.             
  864.              }
  865. else
  866. {
  867. printf("n error:  _pManuDlLinkage=NULL!");
  868. SetUpdateSearchStatus(FALSE);
  869. KB_OTAManuEnableSet(False);
  870. }
  871.              }
  872.                      break;
  873.                 
  874.                 //*****************************
  875.                 case E_TDT_REACH:
  876.                 //*****************************
  877.                  break;
  878.             }
  879.         }
  880. }
  881. }
  882. static void DownloadTimerCallback (void)
  883. {
  884. _TimerCount --;
  885. printf("n%ld   _bOnlyStopTimer=%d",_TimerCount, _bOnlyStopTimer);
  886. if(_TimerCount<6) //end
  887. {
  888.        //SetLockOTAStartFlag(False);
  889.        updateLock = False;
  890. //stop pat, pmt filter, control section filter                            
  891. if(_TimerFlag == True) //pmt pat parse
  892.        printf("n_TimerFlag=True");
  893. KB_ErrorMsg(IRD_DL_SVC_FAIL);
  894. }
  895. else
  896. {
  897.        printf("n_TimerFlag=false  _bOnlyStopTimer=%d", _bOnlyStopTimer);
  898. HPIv2_HDMX_FilterStop();
  899. if(_bOnlyStopTimer != True)
  900. {
  901. KB_ErrorMsg(IRD_DL_NOT_PRESENT);
  902. KB_Timer_Stop(_lDownloadTimer);
  903. }
  904. else
  905. {
  906. KB_Timer_Stop(_lDownloadTimer);
  907. _bOnlyStopTimer = False;
  908. }
  909. }
  910. printf("n--a men");
  911. _bDownloadChecking = False;
  912. }
  913. else
  914. {
  915. MSG_RemainTimeDisplay(_TimerCount, _TimerFlag);
  916. KB_Timer_Start(_lDownloadTimer, 1000);
  917. }
  918. }
  919. // read ota parameter from 0xB8040000 start
  920. TFLoaderPara   * ReadHLLoaderParams(void)
  921. {
  922.        INT32    Status;
  923. UInt8  *buf;
  924.        TFLoaderPara   *kbpara;
  925.  int i;
  926.  buf = OTAParabuf;
  927.  Status = KB_E2PRead((UINT32)KB_NVM_UPDATE_SERIAL_OFFSET, (UINT8 *)buf, 68, NULL);
  928. #if 0
  929. printf("n----ReadHLLoaderParams start------");
  930. for(i=0;i<48;i++)
  931. {
  932.     printf("%02x ",buf[i]);
  933.     if((i+1)%15==0)
  934.     printf("n");      
  935. }
  936. printf("n----ReadHLLoaderParams end------");
  937. #endif
  938. kbpara = &gKBloaderPara;
  939. //if((buf[9]==0x69))
  940. {
  941. kbpara->KB_MANUFACTURER_ID = buf[9];
  942. kbpara->STB_SERIAL_NO = buf[10]*256*256*256  + buf[11]*256*256 +buf[12]*256 +buf[13];
  943. kbpara->KB_HARDWARE_VERSION_NUMBER = buf[14];
  944. kbpara->KB_HARDWARE_MAJOR_VERSION_NUMBER = buf[15]*256 + buf[16];
  945. kbpara->KB_HARDWARE_MINOR_VERSION_NUMBER = buf[17]*256 + buf[18];
  946. kbpara->KB_HARDWARE_BATCH_NO = buf[19]*256 + buf[20];
  947. kbpara->LOAD_SEQUENCE_NUMBER   = buf[21]*256 + buf[22];
  948. kbpara->KB_SOFTWARE_MAJOR_VERSION_NUMBER =  buf[23]*256 + buf[24];
  949. kbpara->KB_SOFTWARE_MINOR_VERSION_NUMBER =  buf[25]*256 + buf[26];
  950. kbpara->kB_REGION_CODE  =  buf[27]*256 + buf[28];
  951. kbpara->kB_KeY_TYPE  =  buf[29];
  952.        kbpara->IMAGE_SIZE  =  buf[1]*256*256*256 + buf[2]*256*256 + buf[3]*256  +buf[4];
  953.    
  954.   kbpara->SIGN_DATE.year  = BCDtoINT(buf[30])*100 + BCDtoINT(buf[31]);
  955. kbpara->SIGN_DATE.month = BCDtoINT(buf[32]);
  956. kbpara->SIGN_DATE.day= BCDtoINT(buf[33]);
  957. kbpara->SIGN_TIME.hour = BCDtoINT(buf[34]);
  958. kbpara->SIGN_TIME.minute= BCDtoINT(buf[35]);
  959. #if 0
  960.   kbpara->DLOAD_DATE.year  = BCDtoINT(buf[36])*100 + BCDtoINT(buf[37]);
  961. kbpara->DLOAD_DATE.month = BCDtoINT(buf[38]);
  962. kbpara->DLOAD_DATE.day= BCDtoINT(buf[39]);
  963. kbpara->DLOAD_TIME.hour = BCDtoINT(buf[40]);
  964. kbpara->DLOAD_TIME.minute= BCDtoINT(buf[41]);
  965. kbpara->DLOAD_TIME.second = BCDtoINT(buf[42]);
  966. kbpara->DLOAD_DUR = buf[43]*256 + buf[44];
  967. #endif
  968. }
  969. #if 1   /*test*/
  970.        printf("n--------------------------n");
  971. printf(" IMAGE_SIZE = %x.n", kbpara->IMAGE_SIZE);
  972. printf(" LOAD_SEQUENCE_NUMBER = %x.n", kbpara->LOAD_SEQUENCE_NUMBER);
  973. printf(" KB_MANUFACTURER_ID = %x.n", kbpara->KB_MANUFACTURER_ID);
  974. printf(" STB_SERIAL_NO = %x.n", kbpara->STB_SERIAL_NO);
  975. printf(" KB_HARDWARE_BATCH_NO = %x.n", kbpara->KB_HARDWARE_BATCH_NO);
  976. printf(" KB_HARDWARE_VERSION_NUMBER = %x.n", kbpara->KB_HARDWARE_VERSION_NUMBER);
  977. printf(" KB_HARDWARE_MAJOR_VERSION_NUMBER = %x.n", kbpara->KB_HARDWARE_MAJOR_VERSION_NUMBER);
  978. printf(" KB_HARDWARE_MINOR_VERSION_NUMBER = %x.n", kbpara->KB_HARDWARE_MINOR_VERSION_NUMBER);
  979. printf(" KB_SOFTWARE_MAJOR_VERSION_NUMBER = %x.n", kbpara->KB_SOFTWARE_MAJOR_VERSION_NUMBER);
  980. printf(" KB_SOFTWARE_MINOR_VERSION_NUMBER = %x.n", kbpara->KB_SOFTWARE_MINOR_VERSION_NUMBER);
  981. printf(" kB_REGION_CODE = %x.n", kbpara->kB_REGION_CODE);
  982. printf(" kB_KeY_TYPE = %x.n", kbpara->kB_KeY_TYPE);
  983. printf(" sign:   %d-%d-%d: %d-%dn", kbpara->SIGN_DATE.year,kbpara->SIGN_DATE.month, kbpara->SIGN_DATE.day, kbpara->SIGN_TIME.hour, kbpara->SIGN_TIME.minute);
  984. printf(" dload: %d-%d-%d: %d-%d-%dn",  kbpara->DLOAD_DATE.year,kbpara->DLOAD_DATE.month, kbpara->DLOAD_DATE.day, kbpara->DLOAD_TIME.hour, kbpara->DLOAD_TIME.minute, kbpara->DLOAD_TIME.second);
  985. printf(" dload_dur:  %dn",kbpara->DLOAD_DUR);
  986. printf("n--------------------------n");
  987.        #endif
  988.         return kbpara; 
  989. }
  990. Bool OTA_CLIENT_GetDLTuneParams(UInt32 * olFreq, UInt32 * olSymb, UInt32 * olModulate)
  991. {
  992. *olFreq = _dlFreq;
  993. *olSymb = _dlSymb;
  994. *olModulate = _dlQam;
  995. #ifdef OTA_TEST2
  996. *olFreq = 299000000;
  997. *olSymb = 6875000;
  998. *olModulate = 3;
  999. #endif
  1000. return True;
  1001. }
  1002. static int GetDLTuneParams(void)
  1003. {
  1004. UInt8 * readPtr;
  1005. Int32  dataLength;
  1006. Int32  network_descriptors_length, transport_stream_loop_length,transport_descriptors_length;
  1007. UInt16 transport_stream_id, original_network_id;
  1008. Int32 tempLength;
  1009. UInt8 * tempP;
  1010. UInt8 last_section, current_section;
  1011. Int32  sectionLength;
  1012. UInt8 tableid;
  1013. KB_OSPMsgNode  MsgCaClient;
  1014. if(_bCurrentNitValid == False)
  1015. return False;
  1016. if(_bCurrentBatValid == False)
  1017. return False;
  1018. if(_pManuDlLinkage == NULL)
  1019. return False;
  1020. //Parse nit for freq, symb and modulate. _shareNitSection
  1021. readPtr = _shareNitSection;
  1022. current_section = readPtr[6] ;
  1023. last_section = readPtr[7];                
  1024.        dataLength = (readPtr[1] & 0x0f)*256 + readPtr[2] + 3 - 4; /*4 - crc32 */
  1025. sectionLength = dataLength;
  1026. tableid = readPtr[0];
  1027. readPtr += 8;
  1028. dataLength -= 8;
  1029. //to network_descriptors_length
  1030. network_descriptors_length = (readPtr[0] & 0x0f) * 256 + readPtr[1];
  1031. readPtr += (network_descriptors_length + 2);
  1032. dataLength -= (network_descriptors_length + 2);
  1033. if(dataLength <= 0) return False;
  1034. //to transport_stream_loop_length
  1035. transport_stream_loop_length = (readPtr[0] & 0x0f) * 256 + readPtr[1];
  1036. readPtr += 2;
  1037. dataLength -= 2;
  1038. while(transport_stream_loop_length > 0)
  1039. {
  1040. transport_stream_id = readPtr[0] * 256 + readPtr[1];
  1041. original_network_id = readPtr[2] * 256 + readPtr[3];
  1042. transport_descriptors_length = (readPtr[4] & 0x0f) * 256 + readPtr[5];
  1043. printf("nn---[GetDLTuneParams1]:transport_stream_id=%x, original_network_id=%x, len = %d.", transport_stream_id, original_network_id, transport_descriptors_length);
  1044. printf("n---[GetDLTuneParams2]:transport_stream_id=%x, original_network_id=%x", _pManuDlLinkage->transport_stream_id, _pManuDlLinkage->original_network_id);
  1045. if(transport_stream_id == _pManuDlLinkage->transport_stream_id
  1046. && original_network_id == _pManuDlLinkage->original_network_id)
  1047. {
  1048. UInt8 desTag, desLen;
  1049. tempLength = transport_descriptors_length;
  1050. tempP = (readPtr + 6);
  1051. while(tempLength > 0)
  1052. {
  1053. desTag = tempP[0];
  1054. desLen = tempP[1];
  1055. if(desTag == 0x44)
  1056. {
  1057. _dlFreq = (((tempP[2]/16)*10 + (tempP[2]&0x0f)) * 1000000 +
  1058. ((tempP[3]/16)*10 + (tempP[3]&0x0f))*10000 +
  1059. ((tempP[4]/16)*10 + (tempP[4]&0x0f))*100 +
  1060. ((tempP[5]/16)*10 + (tempP[5]&0x0f)))*100;
  1061. _dlSymb = (((tempP[9]/16)*10 + (tempP[9]&0x0f)) * 100000 +
  1062. ((tempP[10]/16)*10 + (tempP[10]&0x0f))*1000 +
  1063. ((tempP[11]/16)*10 + (tempP[11]&0x0f))*10 +
  1064. (tempP[12]/16))*100;
  1065. //_dlQam = (tempP[8] + HPIv2_eDemodConstellationQam16 - 1);
  1066. _dlQam = (tempP[8]);
  1067. printf("n --GetDLTuneParams:(%d, %d, %d).", _dlFreq, _dlSymb, _dlQam);
  1068. return 0;
  1069. }
  1070. tempP += (2 + desLen);
  1071. tempLength -= (2 + desLen);
  1072. }
  1073. }
  1074. readPtr += (6 + transport_descriptors_length);
  1075. dataLength -= (6 + transport_descriptors_length);
  1076. transport_stream_loop_length -= (6 + transport_descriptors_length);
  1077. //printf("n---[GetDLTuneParams1]:transport_stream_loop_length= %d.", transport_stream_loop_length);
  1078. }
  1079.         if ( current_section<last_section)
  1080.         {            
  1081.                current_section++;
  1082.  //启动nit 搜索  
  1083.                MsgCaClient.Word1 = E_FILTER_SETPU;
  1084.                MsgCaClient.Word2  = E_NIT_FILTER_SETUP;     
  1085.         MsgCaClient.Word3  = current_section;    
  1086.                OTA_CLIENT_Service (&MsgCaClient);
  1087.         return 1;
  1088.         }
  1089.  
  1090. return -1;
  1091. }
  1092. void SetLockOTAStartFlag(BOOL flag)
  1093. {
  1094.       updateLock = flag;
  1095. }
  1096.  
  1097. BOOL GetLockOTAFlag(void)
  1098. {
  1099.      return updateLock;
  1100. }
  1101. // 1: push 0 : pop
  1102. void BQ_TF_ShowConnectInfo(char *DisplayStr,int ShowFlag)
  1103. {
  1104. static int flag=0;
  1105. static char buf[200];
  1106. if(ShowFlag==1)
  1107. {
  1108. if(!_bDownloadTrigger)
  1109. {
  1110. //BQ_MSG_Show(DisplayStr, BT_NONE, MSGB_WAIT_FOREVER,0);
  1111. KB_Flash_lc_MSGBox(DisplayStr);
  1112. else
  1113. {
  1114. flag = 1;
  1115. strcpy(buf,DisplayStr);
  1116. }
  1117. else
  1118. {
  1119. if(flag == 1)
  1120. {
  1121. //BQ_MSG_Box(buf, BT_NONE, MSGB_WAIT_FOREVER,0);
  1122. KB_Flash_lc_MSGBox(DisplayStr);
  1123. flag = 0;
  1124. }
  1125. }
  1126. }
  1127. void MSG_RemainTimeDisplay(Int32 second, Bool flag)
  1128. {
  1129. char buf[100];
  1130. char *pszInfo;
  1131. //nLangType=BQ_Browser_Language();
  1132. if(flag == TRUE)
  1133. {
  1134. //pszInfo=OSD_GetLanguageInfoAddr(nLangType,BQ_PROMPTMSSG,BQ_PROMPTMSSG_19);
  1135. pszInfo = "正在连接下载流... (剩余%02d秒)";
  1136. if(pszInfo != NULL)
  1137. sprintf(buf, pszInfo, second);
  1138. BQ_TF_ShowConnectInfo(buf,1);
  1139. }
  1140. else
  1141. {
  1142. //pszInfo=OSD_GetLanguageInfoAddr(nLangType,BQ_PROMPTMSSG,BQ_PROMPTMSSG_20);
  1143. pszInfo= "正在分析下载流... (剩余%02d秒)";
  1144. if(pszInfo != NULL)
  1145. sprintf(buf, pszInfo, second);
  1146. BQ_TF_ShowConnectInfo(buf,1);
  1147. }
  1148. return;
  1149. }
  1150. //
  1151. // KB_ErrorMsg:显示错误信息
  1152. //
  1153. void KB_ErrorMsg(UInt16 code)
  1154. {
  1155. char *pszInfo;
  1156. //nLangType=BQ_Browser_Language();
  1157. _Error_Code = code;
  1158. printf("n ------KB_ErrorMsg-: %d",code);
  1159. UnLockPlayPgm();   
  1160. if(_Error_Code == IRD_DL_NIT_FAIL)
  1161. {
  1162. if(!_bDownloadTrigger)
  1163. {
  1164. //pszInfo=OSD_GetLanguageInfoAddr(nLangType,BQ_PROMPTMSSG,BQ_PROMPTMSSG_11);
  1165. pszInfo = "查找下载服务的调谐参数失败!";
  1166. KB_Flash_lc_MSGBox(pszInfo);
  1167. _bDownloadTrigger = False;
  1168. }
  1169. SetUpdateSearchStatus(FALSE);
  1170. OSA_wait(5000);
  1171. KB_KEY_SetReceive(1);
  1172. KB_AV_PlayStart();
  1173. KB_OSD_ClearAll(0xF000);
  1174. UnLockPlayPgm();
  1175. DTV_EPG_SetToLastProg();
  1176. printf("Please resume play here...n");
  1177. }
  1178. else if(_Error_Code == IRD_DL_SVC_FAIL)
  1179. {
  1180. if(!_bDownloadTrigger)
  1181. {
  1182. OSA_wait(500);
  1183. //pszInfo=OSD_GetLanguageInfoAddr(nLangType, BQ_PROMPTMSSG, BQ_PROMPTMSSG_12);
  1184. pszInfo = "连接下载流失败! 5秒后消失。";
  1185. KB_Flash_lc_MSGBox(pszInfo);
  1186. }
  1187.        SetUpdateSearchStatus(FALSE);
  1188. OSA_wait(5000);
  1189. KB_KEY_SetReceive(1);
  1190. KB_AV_PlayStart();
  1191. KB_OSD_ClearAll(0xF000);
  1192. UnLockPlayPgm();
  1193. DTV_EPG_SetToLastProg();
  1194. printf("Please resume play here...n");
  1195. }
  1196. else if(_Error_Code == IRD_DL_CONN_FAIL)
  1197. {
  1198. if(!_bDownloadTrigger)
  1199. {
  1200. OSA_wait(500);
  1201. //pszInfo=OSD_GetLanguageInfoAddr(nLangType, BQ_PROMPTMSSG, BQ_PROMPTMSSG_12);
  1202. pszInfo = "不能锁定升级流所在频点,放弃升级。";
  1203. KB_Flash_lc_MSGBox(pszInfo);
  1204. }
  1205.        SetUpdateSearchStatus(FALSE);
  1206. OSA_wait(5000);
  1207. KB_KEY_SetReceive(1);
  1208. KB_AV_PlayStart();
  1209. KB_OSD_ClearAll(0xF000);
  1210. UnLockPlayPgm();
  1211. DTV_EPG_SetToLastProg();
  1212. printf("Please resume play here...n");
  1213. }
  1214. else if(_Error_Code == IRD_DL_NOT_PRESENT)
  1215. {
  1216. if(!_bDownloadTrigger)
  1217. {
  1218. OSA_wait(500);
  1219. //pszInfo=OSD_GetLanguageInfoAddr(nLangType,BQ_PROMPTMSSG,BQ_PROMPTMSSG_13);
  1220. pszInfo = "下载流无效! 5秒后消失.";
  1221. KB_Flash_lc_MSGBox(pszInfo);
  1222. }
  1223.               SetUpdateSearchStatus(FALSE);
  1224. OSA_wait(5000);
  1225. KB_KEY_SetReceive(1);
  1226.        KB_AV_PlayStart();
  1227. KB_OSD_ClearAll(0xF000);
  1228. DTV_EPG_SetToLastProg();
  1229. printf("Please resume play here ...n");
  1230. }
  1231. //add by shriek
  1232. else if(_Error_Code == IRD_DL_PARA1_NOT_EQUAL)
  1233. {
  1234. printf("n IRD_DL_PARA1_NOT_EQUAL");
  1235. if(!_bDownloadTrigger)
  1236. {
  1237. OSA_wait(500);
  1238. //pszInfo=OSD_GetLanguageInfoAddr(nLangType,BQ_PROMPTMSSG,BQ_PROMPTMSSG_36);
  1239. pszInfo = "参数错误(0001), 放弃此次升级!";
  1240. KB_Flash_lc_MSGBox(pszInfo);
  1241. }
  1242.               SetUpdateSearchStatus(FALSE);
  1243. OSA_wait(5000);
  1244. KB_KEY_SetReceive(1);
  1245.        KB_AV_PlayStart();
  1246. KB_OSD_ClearAll(0xF000);
  1247. DTV_EPG_SetToLastProg();
  1248. printf("Please resume play here ...n");
  1249. }
  1250. else if(_Error_Code == IRD_DL_PARA2_NOT_EQUAL)
  1251. {
  1252. printf("n IRD_DL_PARA2_NOT_EQUAL");
  1253. if(!_bDownloadTrigger)
  1254. {
  1255. OSA_wait(500);
  1256. //pszInfo=OSD_GetLanguageInfoAddr(nLangType,BQ_PROMPTMSSG,BQ_PROMPTMSSG_37);
  1257. pszInfo = "参数错误(0002), 放弃此次升级!";
  1258. KB_Flash_lc_MSGBox(pszInfo);
  1259. }
  1260.               SetUpdateSearchStatus(FALSE);
  1261. OSA_wait(5000);
  1262. KB_KEY_SetReceive(1);
  1263.        KB_AV_PlayStart();
  1264. KB_OSD_ClearAll(0xF000);
  1265. DTV_EPG_SetToLastProg();
  1266. printf("Please resume play here ...n");
  1267. }
  1268. //shriek end
  1269. else if(_Error_Code == IRD_DL_PRESENT)
  1270. {
  1271. char buf[40];
  1272. if(!_bDownloadTrigger)
  1273. {       
  1274.       printf("n---begin while");
  1275.        while(1)
  1276.        {       
  1277.            printf("nupdateLock=%d    GetLockOTAFlag()=%d",updateLock,GetLockOTAFlag());
  1278.            if(!GetLockOTAFlag()||!updateLock)
  1279.             {
  1280.                 printf("n--------break ");
  1281.     break;
  1282.             }
  1283.        }
  1284. printf("n---out of while");
  1285. //pszInfo=OSD_GetLanguageInfoAddr(nLangType,BQ_PROMPTMSSG,BQ_PROMPTMSSG_14);
  1286. pszInfo="升级过程大致需要%d分钟,5 秒后开始升级";
  1287. if(pszInfo != NULL)
  1288. sprintf(buf, pszInfo, _pManuDlLinkage->time); 
  1289. KB_Flash_lc_MSGBox(buf);
  1290. OSA_wait(5000);
  1291. KB_KEY_SetReceive(1);
  1292. //将部分更新的参数备份
  1293. WriteHLLoaderParams();
  1294. printf("n -----begin to start KB_Restart()-----");
  1295. KB_Restart();
  1296. }
  1297. }
  1298. KB_OTARunSet(False);
  1299. KB_UpdatePreventSet(True);
  1300. KB_Flash_lc_MSGBox_Close();
  1301. return;
  1302. }
  1303. void WriteHLLoaderParams(void)
  1304. {
  1305. UInt8  buf[4], buf2[4];
  1306. printf("n -----WriteHLLoaderParams-----");
  1307. buf[0] = (_pManuDlLinkage->major_sw_version>>8)&0x00ff;
  1308. buf[1] =  ( _pManuDlLinkage->major_sw_version)&0x00ff;
  1309. buf[2] = (_pManuDlLinkage->minor_sw_version>>8)&0x00ff;
  1310. buf[3] =  ( _pManuDlLinkage->minor_sw_version)&0x00ff;
  1311. printf("rn[_LOADER_111]: major: %x %x     minor: %x %x", buf[0],buf[1],buf[2],buf[3]);
  1312. if(KB_E2PWrite(KB_NVM_UPDATE_SW_MAJOR_VERSION_BACKUP_OFFSET, buf, 4, NULL)!=Ret_OK)
  1313. {
  1314.       printf("n Save OTA parameter error!");
  1315.        }
  1316. #if 1
  1317. KB_E2PRead((UINT32)KB_NVM_UPDATE_SW_MAJOR_VERSION_BACKUP_OFFSET, (UINT8 *)buf2, (UINT32)4, NULL);
  1318. printf("rn[_LOADER_222]: major: %x %x     minor: %x %x", buf2[0],buf2[1],buf2[2],buf2[3]);
  1319. #endif
  1320. }
  1321. static UInt16 ExtractDLPidFromPMT(void)
  1322. {
  1323. UInt8 * readPtr;
  1324. Int32 dataLength;
  1325. UInt16 ctrlPid;
  1326. Int32 program_info_length;
  1327. Int32 ES_info_length;
  1328. readPtr = _bakdlPmtSection;
  1329. dataLength = (readPtr[1] & 0x0f)*256 + readPtr[2] + 3 - 4; /* 4 - crc32 */
  1330. readPtr += 8;
  1331. dataLength -= 8;
  1332. program_info_length = (readPtr[2] & 0x0f) * 256 + readPtr[3];
  1333. readPtr += 4;
  1334. dataLength -= 4;
  1335. ctrlPid = 0x1FFF;
  1336. readPtr += program_info_length;
  1337. dataLength -= program_info_length;
  1338. while(dataLength > 0)
  1339. {
  1340. printf("nstream type = 0x%x.", readPtr[0]);
  1341. ctrlPid = (UInt16)(((UInt16)readPtr[1] & 0x1f) * 256 + (UInt16)readPtr[2]);
  1342. printf("npid = %x.n", ctrlPid);
  1343. if(readPtr[0] == 0x80 || readPtr[0] == 0x82 || readPtr[0] == 0x84)
  1344. {
  1345. return ctrlPid;
  1346. }
  1347. ES_info_length = (readPtr[3] & 0x0f) * 256 + readPtr[4];
  1348. // printf("es length = %d.n", ES_info_length);
  1349. readPtr += (5 + ES_info_length);
  1350. dataLength -= (5 + ES_info_length);
  1351. }
  1352. return ctrlPid;
  1353. }
  1354. static UInt16 ExtractDlPmtPidFromPat(void)
  1355. {
  1356. UInt8 * readPtr;
  1357. Int32 dataLength;
  1358. UInt16 pmtPid;
  1359. readPtr = _bakPatSection;
  1360. dataLength = (readPtr[1] & 0x0f)*256 + readPtr[2] + 3 - 4; /* 4 - crc32 */
  1361. readPtr += 8;
  1362. dataLength -= 8;
  1363. pmtPid = 0x1FFF;
  1364. printf("n ExtractDlPmtPidFromPat: %x ", readPtr[0] * 256 + readPtr[1]);
  1365. while(dataLength > 0)
  1366. {
  1367. #ifdef OTA_TEST1
  1368. if((readPtr[0] * 256 + readPtr[1]) == 0x0064)  
  1369. #else
  1370. if((readPtr[0] * 256 + readPtr[1]) == _pManuDlLinkage->service_id)
  1371. #endif
  1372. {
  1373. pmtPid = (UInt16)(((UInt16)readPtr[2] & 0x1f) * 256 + (UInt16)readPtr[3]);
  1374. return pmtPid;
  1375. }
  1376. readPtr += 4;
  1377. dataLength -= 4;
  1378. }
  1379. return pmtPid;
  1380. }
  1381. static void FePara(UInt32 SaveLoad)
  1382. {
  1383. //0:Save,   1: Load
  1384. UInt8 writeData[14];
  1385. UInt32  Freq, Symb, Qam;
  1386. UInt32 dlpid;
  1387. UInt32 i;
  1388. for(i=0;i<14;i++)   writeData[i]=0xFF;
  1389. #ifdef OTA_TEST1
  1390. _dlFreq = 315000000;
  1391.   _dlSymb = 6875000;
  1392. _dlQam = 3; 
  1393. _dlPid = 128;
  1394. #endif
  1395. Freq = _dlFreq/1000;
  1396. Symb = _dlSymb/1000;
  1397. Qam = _dlQam;
  1398. dlpid = _dlPid;
  1399. if(SaveLoad==0)
  1400. {
  1401. printf("n[Upgrade_NVM_WriteData]:nOTAFre=%ld   _dlSymb=%ld",_dlFreq,_dlSymb);
  1402. //频率的转换    
  1403. writeData[0]=(Freq>>24)&0xff;
  1404. writeData[1]=(Freq>>16)&0xff;
  1405. writeData[2]=(Freq>>8)&0xff;
  1406. writeData[3]=Freq&0xff;
  1407. printf("nn--writeData[0]=%x",writeData[0]);
  1408. printf("n--writeData[1]=%x",writeData[1]);
  1409. printf("n--writeData[2]=%x",writeData[2]);
  1410. printf("n--writeData[3]=%x",writeData[3]);
  1411. //end
  1412. //符号率的转换    
  1413. writeData[4]=(Symb>>24)&0xff;
  1414. writeData[5]=(Symb>>16)&0xff;
  1415. writeData[6]=(Symb>>8)&0xff;
  1416. writeData[7]=Symb&0xff;
  1417. printf("nn--writeData[4]=%x",writeData[4]);
  1418. printf("n--writeData[5]=%x",writeData[5]);
  1419. printf("n--writeData[6]=%x",writeData[6]);
  1420. printf("n--writeData[7]=%x",writeData[7]);
  1421. //end
  1422. writeData[8]=Qam+3;
  1423. writeData[9]=(dlpid>>8)&0xff;
  1424. writeData[10]=dlpid&0xff;
  1425. writeData[12]=0x01;
  1426. writeData[13]=0x00;  
  1427. #if 1
  1428. printf("n --[loader params]: %d, %d, %d, %x    addr=0x%xn", Freq, Symb, Qam, dlpid, KB_NVM_UPDATE_BASE_ADDR);
  1429. #endif
  1430.  if(KB_E2PWrite(KB_NVM_UPDATE_BASE_ADDR, writeData, 14, NULL)!=Ret_OK)
  1431. {
  1432. printf("n Save OTA parameter error!");
  1433. }  
  1434. #if 1
  1435. {
  1436. unsigned char flag[14];
  1437. KB_E2PRead((UINT32)KB_NVM_UPDATE_BASE_ADDR, (UINT8 *)flag, (UINT32)14, NULL);
  1438. printf("n 0x%x 0x%x 0x%x 0x%x  0x%x 0x%x 0x%x 0x%x",flag[0],flag[1],flag[2],flag[3],flag[4],flag[5],flag[6],flag[7]);
  1439. printf("n 0x%x  0x%x 0x%x  0x%x  0x%x 0x%x",flag[8],flag[9],flag[10],flag[11],flag[12],flag[13]);
  1440. }
  1441. #endif
  1442.  
  1443. KB_OSPTaskDelay(500);
  1444. }
  1445. }
  1446. static int KB_Parse_OTAStream(unsigned short ppid, TF_STREAM_PARA *DC_para)
  1447. {
  1448. unsigned char *allsectionbuf;
  1449. int    result;
  1450. unsigned short DSI_Enable;
  1451. result = 1;
  1452. allsectionbuf=_bakDSISection;
  1453. //result=DTV_GetTable(ppid,0x3b,0xff,(unsigned char **)&allsectionbuf,2);
  1454.         /* If table detected then process data */
  1455. if (result == 1)
  1456. {
  1457. DSI_Enable= (UInt16)(allsectionbuf[10] << 8) & 0xff00;
  1458. DSI_Enable|= (UInt16)allsectionbuf[11] & 0xff;
  1459. if(DSI_Enable==0x1006)
  1460. {
  1461. //制造商ID
  1462. DC_para->manufacturer_code= (UInt32)(allsectionbuf[61] << 16) & 0xff0000;
  1463. DC_para->manufacturer_code|= (UInt32)(allsectionbuf[62] <<8) & 0xff00;
  1464. DC_para->manufacturer_code|= (UInt32)(allsectionbuf[63] & 0xff);
  1465. //硬件模式
  1466. DC_para->hw_batch_no= (UInt16)(allsectionbuf[64]<<8) & 0xff00;
  1467. DC_para->hw_batch_no|=(UInt16)allsectionbuf[65] & 0xff;
  1468. //硬件版本
  1469. DC_para->major_hw_version= (UInt8)allsectionbuf[66];
  1470. DC_para->minor_hw_version=(UInt8)allsectionbuf[67] ;
  1471. //软件版本 
  1472. DC_para->major_sw_version= (UInt8)allsectionbuf[77];
  1473. DC_para->minor_sw_version|=(UInt8)allsectionbuf[78] ;
  1474. }    
  1475. }
  1476. else
  1477. printf("rnNo OTA  found.rn");
  1478.    return result;
  1479. }
  1480. static int StbStreamPara_Compare(unsigned short pid)
  1481. {
  1482. TF_STREAM_PARA stream_para;
  1483. printf("n --------[StbStreamPara_Compare]--------------");
  1484. memset(&stream_para,0x00,sizeof(TF_STREAM_PARA));
  1485. KB_Parse_OTAStream(pid, &stream_para);
  1486. #if 1
  1487. printf("n----------StbStreamPara_Compare start--------------");
  1488. printf("n manufacturer_code=%x", stream_para.manufacturer_code);
  1489. printf("n major_hw_version=%x",  stream_para.major_hw_version);
  1490. printf("n minor_hw_version=%x",  stream_para.minor_hw_version);
  1491. printf("n hw_batch_no=%x",  stream_para.hw_batch_no);
  1492. printf("n major_sw_version=%x",  stream_para.major_sw_version);
  1493.        printf("n major_sw_version=%x",  stream_para.minor_sw_version);
  1494. printf("n----------StbStreamPara_Compare end--------------");
  1495. #endif
  1496. if(stream_para.manufacturer_code!=gKBloaderPara.KB_MANUFACTURER_ID)
  1497. {
  1498. return -1;
  1499. }
  1500. if(stream_para.major_hw_version!=gKBloaderPara.KB_HARDWARE_MAJOR_VERSION_NUMBER
  1501.    ||stream_para.minor_hw_version!=gKBloaderPara.KB_HARDWARE_MINOR_VERSION_NUMBER)
  1502. {
  1503. return -2;
  1504. }
  1505. if(stream_para.hw_batch_no==0)
  1506. {
  1507. printf("n do not concern batch_no");
  1508. }
  1509. else 
  1510. {
  1511. if(stream_para.hw_batch_no!=gKBloaderPara.KB_HARDWARE_BATCH_NO)
  1512. return -3;
  1513. }
  1514. if(stream_para.major_sw_version!=_pManuDlLinkage->major_sw_version||stream_para.minor_sw_version!=_pManuDlLinkage->minor_sw_version)
  1515. {
  1516. return -4;
  1517. }
  1518. return 1;
  1519. }
  1520. void setDownloadTrigerFlag(BOOL flag)
  1521. {
  1522. _bDownloadTrigger = flag;
  1523. }
  1524. //
  1525. // MSG_BeginDownloadSearching
  1526. //
  1527. int KB_BeginDownloadSearching(void)
  1528. {
  1529. UInt32 Freq, Symb, Qam;
  1530. KB_OSPMsgNode   MsgCaClient;
  1531. unsigned long preFreq;
  1532. printf("rn -[MSG_BeginDownloadSearching]-: _bDownloadTrigger=%d",_bDownloadTrigger);
  1533. if(!_bDownloadTrigger)
  1534.     KB_KEY_SetReceive(0);
  1535.        //预先保存频点 shriek
  1536. preFreq = KB_GetDTVCHN_Freq(KB_GetCurCHNTsID());
  1537. OSA_wait(2000);
  1538. SetLockOTAStartFlag(True);
  1539. MSG_RemainTimeDisplay(30, True);
  1540.        //关闭播放
  1541. LockPlayPgm();
  1542. KB_AV_PlayStop();
  1543. DTV_EPG_StopLastProg();
  1544.    KB_QAM_Stop(); 
  1545. //BQ_EPG_DispNowNextInfoClear();
  1546. //close filters of OTA
  1547.     KB_UpdateCheckClear();
  1548. OSA_wait(1000);
  1549.    MSG_RemainTimeDisplay(29, True);
  1550. OTA_CLIENT_GetDLTuneParams(&Freq, &Symb, &Qam);
  1551. if(KB_CHN_SetQam(Freq, Symb, Qam)==0)
  1552.      printf("rn setQam :(%ld , %ld , %ld)  ok !", Freq, Symb, Qam);    
  1553.             OSA_wait(1000);
  1554.         MSG_RemainTimeDisplay(28, True);
  1555.      OSA_wait(1000);
  1556.         MSG_RemainTimeDisplay(27, True);
  1557.   
  1558.          MsgCaClient.Word1 = E_DOWNLOAD_SEARCHING;
  1559.      MsgCaClient.Word2 = FALSE;
  1560.          OTA_CLIENT_Service (&MsgCaClient);    
  1561.      return 0;
  1562. }
  1563. else
  1564. //KB_restoreCurfre(preFreq);
  1565. printf("rn can't setqam  :(%ld , %ld , %ld) ",Freq, Symb, Qam);  
  1566. KB_Timer_Stop(_lDownloadTimer);
  1567. OSA_wait(200);
  1568. KB_ErrorMsg(IRD_DL_SVC_FAIL);
  1569.      return -1;
  1570. }
  1571.    
  1572. return 0;
  1573. }
  1574. void KB_UpdateCheckClear(void)
  1575. {
  1576.        KB_OSPMsgNode   MsgCaClient;
  1577. printf("n KB_UpdateCheckClear");
  1578.     MsgCaClient.Word1 = E_CURRENT_TS_DEASSOCIATED;
  1579. MsgCaClient.Word2 = FALSE; 
  1580.     OTA_CLIENT_Service (&MsgCaClient);
  1581. return;
  1582. }
  1583. //input  _bakBatSection
  1584. //output _lIrdetoLinkageNum & _astIrdetoLinkage
  1585. static void ExtraceTFLinkage(void)
  1586. {
  1587. UInt8 * readPtr;
  1588. Int32  descriptor_length, bouquet_descriptors_length;
  1589. Bool bIrdeto;
  1590. TF_Linkage_Descriptor * pTF;
  1591. UInt8 * tempPtr;
  1592. Int32 tempLength;
  1593. UInt16 ts_id, net_id, svc_id;
  1594. UInt8 linkage_type;
  1595. UInt8 desTag,desLen;
  1596. //UInt8 download_time = 0xffff;
  1597. readPtr = _bakBatSection;
  1598. readPtr += 8;
  1599. //bouquet_descriptors_length
  1600. bouquet_descriptors_length = (readPtr[0] & 0x0f)*256 + readPtr[1];
  1601. readPtr += 2;
  1602. _lTFLinkageNum = 0;
  1603. bIrdeto = False;
  1604.        //同方私有数据
  1605. while(bouquet_descriptors_length > 0) 
  1606.         desTag=readPtr[0]; 
  1607.         desLen=readPtr[1]; 
  1608.         if(desTag==0x5F) 
  1609.         { 
  1610.             if(readPtr[2]==0x00&&readPtr[3]==0x00&& 
  1611.                 readPtr[4]==0x0f&&readPtr[5]==0xf8) 
  1612.             { 
  1613.                 bIrdeto = True; 
  1614.                 break; 
  1615.             } 
  1616.         } 
  1617.         readPtr+=2; 
  1618.         bouquet_descriptors_length-=2; 
  1619.         readPtr+=desLen; 
  1620.         bouquet_descriptors_length-=desLen; 
  1621. //modified by xia 06/11/21 end
  1622. if(bIrdeto == False) return;
  1623.        #if 1    //for debug
  1624.        readPtr = _bakBatSection; 
  1625. readPtr += 3;
  1626. //printf("nbouquet_id=0x%04x",readPtr[0]*256+readPtr[1]);
  1627. #endif  //debug only 
  1628. readPtr = _bakBatSection; 
  1629. readPtr += 8; 
  1630. bouquet_descriptors_length = (readPtr[0] & 0x0f)*256 + readPtr[1]; 
  1631. readPtr += 2; 
  1632. while(bouquet_descriptors_length > 0)
  1633. {
  1634. //printf("nndescriptor tag = %x.", readPtr[0]);
  1635. descriptor_length = readPtr[1];
  1636. //printf("------descriptor descriptor_length = %d字节.", descriptor_length);
  1637. if(readPtr[0] == 0x4A) //linkage descriptor
  1638. {
  1639. tempPtr = readPtr;
  1640. tempLength = descriptor_length;
  1641. ts_id = readPtr[2] * 256 + readPtr[3];
  1642. net_id = readPtr[4] * 256 + readPtr[5];
  1643. svc_id = readPtr[6] * 256 + readPtr[7];
  1644. linkage_type = readPtr[8];
  1645. tempPtr += 9;
  1646. tempLength -= 7;
  1647.                     #if 0 //debug by shriek
  1648.       printf("n[huqs]: transport_stream_id = %x.", ts_id);
  1649.       printf("n[huqs]: net_id = %x.", net_id);
  1650.       printf("n[huqs]: service_id = %x.", svc_id);
  1651.       printf("n[huqs]: linkage_type = %x.", linkage_type);
  1652.       printf("n[huqs]: tempLength = %x.", tempLength);
  1653.       #endif
  1654. if(linkage_type == 0x82 && _lTFLinkageNum < MAX_IRDETO_LINKAGES)
  1655. {
  1656. while(tempLength > 0)
  1657. {
  1658. pTF = &(_astTFLinkage[_lTFLinkageNum]);
  1659. pTF->transport_stream_id = ts_id;
  1660. pTF->original_network_id = net_id;
  1661. pTF->service_id = svc_id;
  1662. pTF->linkage_type = 0x82;
  1663. pTF->manufacturer_code = tempPtr[1]*256 + tempPtr[2];
  1664. pTF->control_byte_1 = tempPtr[4];
  1665. pTF->major_hw_version = tempPtr[6]*256 + tempPtr[7];
  1666. pTF->minor_hw_version = tempPtr[9]*256 + tempPtr[10];
  1667. pTF->hw_batch_no =  tempPtr[12]*256 + tempPtr[13];
  1668. pTF->major_sw_version = tempPtr[17]*256 + tempPtr[18];
  1669. pTF->minor_sw_version = tempPtr[20]*256 + tempPtr[21];
  1670. pTF->stb_type = tempPtr[23];
  1671. pTF->region_code_start = tempPtr[25]*256 + tempPtr[26];
  1672. pTF->region_code_end = tempPtr[28]*256 + tempPtr[29];
  1673. pTF->upgrage_type = tempPtr[33];
  1674. pTF->stb_sequence_start= tempPtr[35]*256*256*256 + tempPtr[36]*256*256 + tempPtr[37]*256 + tempPtr[38];
  1675. pTF->stb_sequence_end = tempPtr[40]*256*256*256 + tempPtr[41]*256*256 + tempPtr[42]*256 + tempPtr[43];
  1676. pTF->time = tempPtr[45];
  1677. pTF->control_byte_2 = tempPtr[47];
  1678. //printf("n---linkage_type:0x82 manufacturer_code=%x, hardware_code=%x, --", pTF->manufacturer_code,pTF->hardware_code);
  1679. tempPtr += 48;
  1680. tempLength -= 48;
  1681.                                            
  1682. //record only linkage concern with this box
  1683. #ifdef OTA_TEST
  1684. if(pTF->manufacturer_code == 0x45)
  1685. _lTFLinkageNum ++;
  1686. #else
  1687. if(pTF->manufacturer_code == gKBloaderPara.KB_MANUFACTURER_ID)
  1688. _lTFLinkageNum ++;
  1689. #endif
  1690. }
  1691. }
  1692. // printf("linkage type is 0x80n");
  1693. }
  1694. readPtr += 2;
  1695. bouquet_descriptors_length -= 2;
  1696. readPtr += descriptor_length;
  1697. bouquet_descriptors_length -= descriptor_length;
  1698. }
  1699. // dataLength -= bouquet_descriptors_length;
  1700. #if 1 //debug
  1701. printf("nn_lIrdetoLinkageNum = %d.n", _lTFLinkageNum);
  1702. for(tempLength = 0; tempLength < _lTFLinkageNum; tempLength ++)
  1703. {
  1704. printf("--------------------------n");
  1705. pTF = &(_astTFLinkage[tempLength]);
  1706. printf(" transport_stream_id = %x.n", pTF->transport_stream_id);
  1707. printf(" original_network_id = %x.n", pTF->original_network_id);
  1708. printf(" service_id = %x.n", pTF->service_id);
  1709. printf(" linkage_type = %x.n", pTF->linkage_type);
  1710. printf(" manufacturer_code = %x.n", pTF->manufacturer_code);
  1711. printf(" control_type_1 = %x.n", pTF->control_byte_1);
  1712. printf(" major_hw_version = %x.n", pTF->major_hw_version);
  1713. printf(" minor_hw_version = %x.n", pTF->minor_hw_version);
  1714. printf(" hw_batch_no = %x.n", pTF->hw_batch_no);
  1715. printf(" major_sw_version = %x.n", pTF->major_sw_version);
  1716. printf(" minor_sw_version = %x.n", pTF->minor_sw_version);
  1717. printf(" stb_type = %x.n", pTF->stb_type);
  1718. printf(" region_code_start = %x.n", pTF->region_code_start);
  1719. printf(" region_code_end = %x.n", pTF->region_code_end);
  1720. printf(" upgrage_type = %x.n", pTF->upgrage_type);
  1721. printf(" stb_sequence_start = %x.n", pTF->stb_sequence_start);
  1722. printf(" stb_sequence_end = %x.n", pTF->stb_sequence_end);
  1723. printf(" time = %x.n", pTF->time);
  1724. printf(" control_byte_2 = %x.n", pTF->control_byte_2);
  1725. }
  1726. #endif
  1727. }
  1728. static void ExtractManuDlLinkage(void)
  1729. {
  1730. UInt32 i, j;
  1731. TF_Linkage_Descriptor * pTF;
  1732. UInt16 twobyte;
  1733. Bool valid_load_sequence;
  1734. #if 0
  1735. _pManuDlLinkage = NULL;
  1736. for(i = 0; i < _lTFLinkageNum; i ++)
  1737. {
  1738. pTF = &(_astTFLinkage[i]);
  1739. if( pTF->major_hw_version== gKBloaderPara.KB_HARDWARE_MAJOR_VERSION_NUMBER
  1740. &&pTF->linkage_type == 0x82)
  1741.      break;
  1742. }
  1743.        printf("n[ExtractManuDlLinkage1]:hardware_code=%d,  IRD_HARDWARE_VERSION_NUMBER=%d",pTF->minor_hw_version, gKBloaderPara.KB_HARDWARE_MAJOR_VERSION_NUMBER);
  1744. for(j = 0; j < _lTFLinkageNum; j ++)
  1745. {
  1746. pTF = &(_astTFLinkage[j]);
  1747. if(pTF->load_sequence_number!=0)
  1748. {
  1749. #ifdef OTA_TEST
  1750. if(pTF->load_sequence_number>=gKBloaderPara.LOAD_SEQUENCE_NUMBER)
  1751. {
  1752.  valid_load_sequence = True;
  1753.  gKBloaderPara.LOAD_SEQUENCE_NUMBER = pTF->load_sequence_number;
  1754. }
  1755. else
  1756. valid_load_sequence = False;
  1757. #else
  1758. if(pTF->load_sequence_number>gKBloaderPara.LOAD_SEQUENCE_NUMBER)
  1759. {
  1760.  valid_load_sequence = True;
  1761.  gKBloaderPara.LOAD_SEQUENCE_NUMBER = pTF->load_sequence_number;
  1762. }
  1763. else
  1764. valid_load_sequence = False;
  1765. #endif
  1766. }
  1767. else if(pTF->load_sequence_number==0)
  1768. {
  1769. if(gKBloaderPara.LOAD_SEQUENCE_NUMBER!=1)
  1770. {
  1771.  valid_load_sequence = True;
  1772.  gKBloaderPara.LOAD_SEQUENCE_NUMBER = 1;
  1773. }
  1774. else
  1775.  valid_load_sequence = False;
  1776. }        
  1777.        twobyte = pTF->major_hw_version* 256 + pTF->minor_hw_version;
  1778. printf("n valid_load_sequence=%d", valid_load_sequence);
  1779. #ifdef OTA_TEST
  1780. if( pTF->hardware_code == 0
  1781.  && pTF->linkage_type == 0x82
  1782.  && valid_load_sequence )
  1783. {
  1784.         _pManuDlLinkage = pTF;
  1785.  break;
  1786. }
  1787. #else
  1788. if( pTF->hardware_code == gKBloaderPara.KB_HARDWARE_VERSION_NUMBER
  1789.      && pTF->linkage_type == 0x82
  1790.      && valid_load_sequence )
  1791. {
  1792.         _pManuDlLinkage = pTF;
  1793.  break;
  1794. }
  1795. #endif
  1796.   }
  1797.   if(j >= _lTFLinkageNum)
  1798. return;
  1799. #endif
  1800. }
  1801. static void ExtractManuDlLinkageEx(void)
  1802. {
  1803. UInt32 j;
  1804. TF_Linkage_Descriptor * pTF;
  1805. UInt32 fourbyte_bat,  fourbyte_stb;
  1806. Bool valid_load_sequence;
  1807. unsigned long region_code;
  1808. _pManuDlLinkage = NULL;
  1809. for(j = 0; j < _lTFLinkageNum; j ++)
  1810. {
  1811. pTF = &(_astTFLinkage[j]);
  1812. #ifdef OTA_TEST
  1813. if(pTF->manufacturer_code==0x45
  1814. &&pTF->major_hw_version== 0x0001
  1815. &&pTF->minor_hw_version== 0x0001
  1816. &&pTF->hw_batch_no==0x0001
  1817. &&pTF->stb_type==0x01)
  1818. {
  1819. valid_load_sequence=True;
  1820. }
  1821. else
  1822. valid_load_sequence=False;
  1823. #else
  1824. if( pTF->manufacturer_code==gKBloaderPara.KB_MANUFACTURER_ID
  1825. &&pTF->major_hw_version== gKBloaderPara.KB_HARDWARE_MAJOR_VERSION_NUMBER
  1826. &&pTF->minor_hw_version== gKBloaderPara.KB_HARDWARE_MINOR_VERSION_NUMBER
  1827. &&pTF->hw_batch_no==gKBloaderPara.KB_HARDWARE_BATCH_NO
  1828. &&pTF->stb_type==gKBloaderPara.kB_KeY_TYPE)
  1829. {
  1830. #ifdef OTA_SWITCH_OPEN
  1831. if(pTF->region_code_start==0||pTF->region_code_end==0)
  1832. {
  1833. valid_load_sequence=True;
  1834. }
  1835. else
  1836. {
  1837. region_code = KB_GetRegion_Code();
  1838. printf("n ------region_code = %ld", region_code);
  1839. if(region_code>=pTF->region_code_start&&region_code<=pTF->region_code_end)
  1840. valid_load_sequence=True;
  1841. }
  1842. else
  1843. {
  1844. valid_load_sequence=False;
  1845. }
  1846. }
  1847. #else
  1848. valid_load_sequence = True;
  1849. #endif
  1850. }
  1851. else
  1852. valid_load_sequence = False;
  1853. #endif
  1854.        fourbyte_bat  =(UInt32)(pTF->major_sw_version*256*256 + pTF->minor_sw_version);
  1855. fourbyte_stb= (UInt32)(gKBloaderPara.KB_SOFTWARE_MAJOR_VERSION_NUMBER*256*256 + gKBloaderPara.KB_SOFTWARE_MINOR_VERSION_NUMBER);
  1856.    
  1857. printf("n valid_load_sequence=%dn fourbyte_bat=%ld   fourbyte_stb=%ld", valid_load_sequence, fourbyte_bat, fourbyte_stb);
  1858. #ifdef OTA_TEST2
  1859. if(valid_load_sequence)
  1860. {
  1861. _pManuDlLinkage = pTF;
  1862. break;
  1863. }
  1864. #else
  1865. if(valid_load_sequence)
  1866. {
  1867. if(pTF->control_byte_1== 0xA2)
  1868. {
  1869. if(fourbyte_stb<fourbyte_bat)  
  1870. {
  1871. _pManuDlLinkage = pTF;
  1872. break;
  1873. }
  1874. }
  1875. else if(pTF->control_byte_1== 0xA4)
  1876. {
  1877. if(fourbyte_stb>fourbyte_bat)
  1878. {
  1879. _pManuDlLinkage = pTF;
  1880. break;
  1881. }
  1882. }
  1883. else if(pTF->control_byte_1== 0xA8)
  1884. {
  1885. if(fourbyte_stb!=fourbyte_bat)
  1886. {
  1887. _pManuDlLinkage = pTF;
  1888. break;
  1889. }
  1890. }
  1891. }
  1892. #endif
  1893.   }
  1894.   if(j >= _lTFLinkageNum)
  1895. return;
  1896. }
  1897. /*************************************************************************************
  1898. Function name: GetUpgradeType
  1899. Description: 获取升级类型
  1900. Input:  TF_Linkage_Descriptor  * pManuDlLinkage,      
  1901. Output: None
  1902. Return: upgrade type
  1903. *************************************************************************************/
  1904. int GetUpgradeType(TF_Linkage_Descriptor  * pManuDlLinkage)
  1905. {
  1906.        int OTA_Type = OTA_INVALID;
  1907. if(pManuDlLinkage->upgrage_type==0x22)   /* 1 */
  1908. {
  1909. OTA_Type = OTA_MANUL;
  1910. }
  1911. else if(pManuDlLinkage->upgrage_type==0x26)   /* 2 */
  1912. {
  1913. OTA_Type = OTA_USER_CONFORMED;
  1914. }
  1915. else if(pManuDlLinkage->upgrage_type==0x2e)   /* 3 */
  1916. {
  1917. OTA_Type = OTA_FORCED;
  1918. }
  1919. else if(pManuDlLinkage->upgrage_type==0x42)   /* 4 */
  1920. {
  1921. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1922.    OTA_Type = OTA_MANUL;
  1923. }
  1924. else if(pManuDlLinkage->upgrage_type==0x46)   /* 5 */
  1925. {
  1926. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1927.    OTA_Type = OTA_USER_CONFORMED;
  1928. }
  1929. else if(pManuDlLinkage->upgrage_type==0x4E)   /* 6 */
  1930. {
  1931. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1932.    OTA_Type = OTA_FORCED;
  1933. }
  1934. else if(pManuDlLinkage->upgrage_type==0x82)   /* 7 */
  1935. {
  1936. if((gKBloaderPara.STB_SERIAL_NO<pManuDlLinkage->stb_sequence_start)||(gKBloaderPara.STB_SERIAL_NO>pManuDlLinkage->stb_sequence_end))
  1937.    OTA_Type = OTA_MANUL;
  1938. }
  1939. else if(pManuDlLinkage->upgrage_type==0x86)   /* 8 */
  1940. {
  1941. if((gKBloaderPara.STB_SERIAL_NO<pManuDlLinkage->stb_sequence_start)||(gKBloaderPara.STB_SERIAL_NO>pManuDlLinkage->stb_sequence_end))
  1942.    OTA_Type = OTA_USER_CONFORMED;
  1943. }
  1944. else if(pManuDlLinkage->upgrage_type==0x8e)   /* 9 */
  1945. {
  1946. if((gKBloaderPara.STB_SERIAL_NO<pManuDlLinkage->stb_sequence_start)||(gKBloaderPara.STB_SERIAL_NO>pManuDlLinkage->stb_sequence_end))
  1947.    OTA_Type = OTA_FORCED;
  1948. }
  1949. else if(pManuDlLinkage->upgrage_type==0xa6)   /* 10 */
  1950. {
  1951. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1952.    OTA_Type = OTA_USER_CONFORMED;
  1953. else
  1954.   OTA_Type = OTA_MANUL;
  1955. }
  1956. else if(pManuDlLinkage->upgrage_type==0xae)   /* 11 */
  1957. {
  1958. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1959.    OTA_Type = OTA_FORCED;
  1960. else
  1961.   OTA_Type = OTA_MANUL;
  1962. }
  1963. else if(pManuDlLinkage->upgrage_type==0xc2)   /* 12 */
  1964. {
  1965. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1966.    OTA_Type = OTA_MANUL;
  1967. else
  1968.   OTA_Type = OTA_USER_CONFORMED;
  1969. }
  1970. else if(pManuDlLinkage->upgrage_type==0xce)   /* 13 */
  1971. {
  1972. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1973.    OTA_Type = OTA_FORCED;
  1974. else
  1975.   OTA_Type = OTA_USER_CONFORMED;
  1976. }
  1977. else if(pManuDlLinkage->upgrage_type==0xe2)   /* 14 */
  1978. {
  1979. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1980.    OTA_Type = OTA_MANUL;
  1981. else
  1982.   OTA_Type = OTA_FORCED;
  1983. }
  1984. else if(pManuDlLinkage->upgrage_type==0xee)   /* 15 */
  1985. {
  1986. if((gKBloaderPara.STB_SERIAL_NO>=pManuDlLinkage->stb_sequence_start)&&(gKBloaderPara.STB_SERIAL_NO<=pManuDlLinkage->stb_sequence_end))
  1987.    OTA_Type = OTA_USER_CONFORMED;
  1988. else
  1989.   OTA_Type = OTA_FORCED;
  1990. }
  1991. return OTA_Type;
  1992. }
  1993. void OTA_test1(void)
  1994. {
  1995.       int nres;
  1996. nres = StbStreamPara_Compare(0x80);
  1997. printf("n  res=%d", nres);
  1998. if(nres>0)
  1999. {
  2000. printf("n StbStreamPara_Compare>0, ok");
  2001. }
  2002. else
  2003. {
  2004. printf("n StbStreamPara_Compare<0, error,   res=%d", nres);
  2005. if(nres==-1||nres==-2||nres==-3)
  2006. KB_ErrorMsg(IRD_DL_PARA1_NOT_EQUAL);
  2007. else if(nres==-4)
  2008. KB_ErrorMsg(IRD_DL_PARA2_NOT_EQUAL);
  2009. OSA_wait(200);
  2010. }
  2011. }
  2012. //True - Enable dl button, False - Disable dl button
  2013. unsigned char OTA_CLIENT_GetDownloadState(void)
  2014. {
  2015. if(_bCurrentBatValid == False) //bat valid is false
  2016. {
  2017. printf("n--==_bCurrentBatValid == False==--n");
  2018. return 0;
  2019. }
  2020. if(_pManuDlLinkage == NULL)
  2021. {
  2022. Print("n--==_pManuDlLinkage == NULL==--n");
  2023. return 0;
  2024. }
  2025. else if(_pManuDlLinkage->control_byte_2 == 0)
  2026. return 1;
  2027. else if(_pManuDlLinkage->control_byte_2 == 2 
  2028. && _flag_Download_Allowed == TRUE)
  2029. return 2;
  2030. else
  2031. {
  2032. printf("n--==_pManuDlLinkage->control_byte == %d, _flag_Download_Allowed=%d==--n", _pManuDlLinkage->control_byte_2, _flag_Download_Allowed);
  2033. return 0;
  2034. }
  2035. }
  2036. /******************************************************************************
  2037. Function name: KB_Flash_lc_MSGBox
  2038. Description: local connection of flash message box
  2039. Input:  None
  2040. Output: None
  2041. Return: None
  2042. ******************************************************************************/
  2043. int KB_Flash_lc_MSGBox(char *str) 
  2044. {
  2045. KB_OSPMsgNode Msg;
  2046. int status;
  2047. int ret;
  2048. if(str==NULL)
  2049. {
  2050. ota_message.flag = 0;
  2051. kb_machblue_movie_play(kb_movie_menu_otahint);
  2052. while(1)
  2053. {
  2054. status = KB_OSPMsgGet(_OtaQueue, KB_Wait, 0, &Msg);
  2055. if(status != Ret_OK)
  2056. {
  2057. continue;
  2058. }
  2059. switch (Msg.Word1)
  2060. {
  2061. case 1:
  2062. printf("n 1 received!");
  2063. ret = IDOK;
  2064. return ret;
  2065. case 0:
  2066.        printf("n 0 received!");
  2067. ret = IDNO;
  2068. return ret;
  2069. default:
  2070. printf("n else received!");
  2071. break;
  2072. }
  2073. }
  2074. }
  2075. else
  2076. {
  2077. ota_message.flag = 1;
  2078. strcpy(ota_message.ota_hint_message, str);
  2079.        KB_MB_lc_OTA_hint_2_movie(ota_message.ota_hint_message);        
  2080. }
  2081. return ret;
  2082. }
  2083. /******************************************************************************
  2084. Function name: KB_Flash_lc_MSGBox_Close
  2085. Description: close local connection of flash message box
  2086. Input:  None
  2087. Output: None
  2088. Return: None
  2089. ******************************************************************************/
  2090. void KB_Flash_lc_MSGBox_Close(void)
  2091. {
  2092. printf("n ---------KB_Flash_lc_MSGBox_Close---------------- ");
  2093. kb_machblue_movie_close(TRUE);
  2094. }
  2095. //-----------------------------------
  2096. //----------