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

DVD

开发平台:

C/C++

  1. #include <string.h>
  2. #include "stevt.h"
  3. #include "stsys.h"
  4. #include "stcommon.h"
  5. #include "stpti.h"
  6. #include "errors.h"
  7. #include "osp.h"
  8. #include "dmx.h"
  9. #include "stpti_hal.h"
  10. #include "appltype.h"
  11. #include "sti5105.h"
  12. #include "swconfig.h"
  13. #if defined(DUAL_DVR)
  14.     #define KB_PTI_MAX_NUM 3
  15. #elif defined(PIP_SUPPORT)
  16.     #define KB_PTI_MAX_NUM 2
  17. #else
  18.     #define KB_PTI_MAX_NUM 1
  19. #endif
  20. /* STPTI */
  21. #define PTI_DEVICE               STPTI_DEVICE_PTI_4
  22. #define PTI_TCDEVICEADDRESS      (STPTI_DevicePtr_t)PTI_BASE_ADDRESS
  23. #define PTI_INTERRUPTNUMBER      PTI_INTERRUPT
  24. #define PTI_SECTIONFILTERMODE    STPTI_FILTER_OPERATING_MODE_32x8
  25. #define PTI_DVBTCLOADER          STPTI_DVBTCLoaderUL
  26. #define PTIB_TCDEVICEADDRESS     0
  27. #define PTIB_INTERRUPTNUMBER     0
  28. #define DMX_SECTION_SIZE (4*1024)
  29. #if (KB_PTI_MAX_NUM==3)
  30.     ST_DeviceName_t g_KB_PTIDeviceNameMultiple[KB_PTI_MAX_NUM] = {"PTI0","PTI1","PTI2"};
  31. #elif (KB_PTI_MAX_NUM==2)
  32.     ST_DeviceName_t g_KB_PTIDeviceNameMultiple[KB_PTI_MAX_NUM] = {"PTI0","PTI1"};
  33. #else
  34.     ST_DeviceName_t g_KB_PTIDeviceNameMultiple[KB_PTI_MAX_NUM] = {"PTI0"};
  35. #endif
  36. STPTI_Handle_t g_KB_PTIHandleMultiple[KB_PTI_MAX_NUM];
  37. #define DEFAULT_PTI (0)
  38. #define DMX_TOTAL_KEY_PAIRS          (12) /* total number of key index */
  39. extern ST_Partition_t *NcachePartition; /* non-cached partition */
  40. extern ST_Partition_t *SystemPartition; /* general partition    */
  41. typedef struct
  42. {
  43. BOOL   Allocated; /* indicates whether channel is allocated */
  44. BOOL   Started;              /* indicates whether channel is started */
  45. BOOL   InternalUnlock; /* indicates whether channel data is released internal */
  46. KB_DMXChannelType Type;      /* channel type enumeration */
  47. STPTI_Slot_t    SlotHandle;     /* Slot handle */
  48.     STPTI_Pid_t     Pid;            /* PID */
  49.     STPTI_Buffer_t  BufferHandle;   /* PTI Destination Buffer handle */
  50. UINT32          BufferSize;     /* PTI Destination Buffer size   */
  51. UINT8           *UserBuffer_p;  /* User Buffer */
  52.     UINT32          UserBufferSize; /* User Buffer */
  53. STPTI_Signal_t  SignalHandle;   /* PTI Destination Signal handle */
  54. STPTI_Descrambler_t DescramblerHandle; /* The ID of the descrambler currently used */
  55. KB_DMX_NotifyFunc NotifyFunctionPtr;
  56. }DMX_Channel;
  57. /*********************** Use generic slots *********************/
  58. const STPTI_SlotType_t STPTI_SlotTypes[] =
  59. {
  60. STPTI_SLOT_TYPE_NULL, /* Invalid */
  61.     STPTI_SLOT_TYPE_PES,    /* GENERIC_PES */
  62.     STPTI_SLOT_TYPE_SECTION,/* SECTION */
  63.     STPTI_SLOT_TYPE_EMM, /* EMM */
  64.     STPTI_SLOT_TYPE_ECM, /* ECM */
  65.     STPTI_SLOT_TYPE_RAW, /* RAW */
  66. };
  67. typedef struct _backdata{
  68. UINT32 channelID;
  69. UINT32 filterID;
  70. }BackData;
  71. #define BACKDATA_N 500
  72. static BackData g_backdata[BACKDATA_N];
  73. #define BACKID_N 100
  74. //static UINT32 g_backid[BACKID_N];
  75.  #define EXM_BUFF_LEN 1024
  76.  static STPTI_Signal_t  g_SignalHandle_for_TF;
  77.  static UINT8    Exm_Buf[EXM_BUFF_LEN];
  78. static DMX_Channel gChnnlInfo[KB_DMX_TOTAL_GENERIC_CHANNELS];
  79. static STPTI_Signal_t  gSinHandle;
  80. extern ST_Partition_t  *DemuxPartition;
  81. static UINT8 *Dmx_Buf;
  82. static void Dmx_Task(void *params);
  83. static INT32 KD_DmxAllocateBuf(UINT32 channelID, UINT32 bufferSize);
  84. static void* KD_DMXAllocate(UINT32 size);
  85. static void  KD_DMXFree(void *ptr);
  86. /* Semaphore for protect channel operation */
  87. static UINT32 Dmx_SemLock;
  88. /* get the first empty channel */
  89. static INT32 GetFirstEmptyChannelID(void)
  90. {
  91. INT32 i;
  92. for(i = 0; i < KB_DMX_TOTAL_GENERIC_CHANNELS; i++)
  93. {
  94. if(gChnnlInfo[i].Allocated == FALSE)
  95. {
  96. break;
  97. }
  98. }
  99. return i;
  100. }
  101. static void Dmx_Task(void *params)
  102. {
  103. ST_ErrorCode_t  ST_ErrorCode;
  104. STPTI_Buffer_t  WhichBufferGotSignal;
  105. DMX_Channel  *pChInfo;
  106. UINT32 DataSize;
  107. U32   NumFilterMatches = 0;
  108. BOOL  CRCValid;
  109. INT32  chId;
  110. while(1)
  111. {
  112. ST_ErrorCode = STPTI_SignalWaitBuffer(gSinHandle, &WhichBufferGotSignal, STPTI_TIMEOUT_INFINITY);
  113. /* STPTI_SignalWaitBuffer() generated an error */
  114.     if (ST_ErrorCode == ST_ERROR_TIMEOUT)
  115.     {
  116.         continue;
  117.     }
  118.     /* if not a harmless timeout then get rid of anything in the STPTI internal buffer */
  119.     else if (ST_ErrorCode != ST_NO_ERROR)
  120.     {
  121.         STPTI_BufferFlush(WhichBufferGotSignal);
  122. continue;
  123.     }
  124. /* Find the channel, that buffer handle equal WhichBufferGotSignal */
  125. for(chId = 0; chId < KB_DMX_TOTAL_GENERIC_CHANNELS; chId++)
  126. {
  127. if( (gChnnlInfo[chId].Allocated == TRUE)
  128. && (gChnnlInfo[chId].BufferHandle == WhichBufferGotSignal) )
  129. {
  130. /* got it */
  131. break;
  132. }
  133. }
  134. if(chId >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  135. {
  136. STPTI_BufferFlush(WhichBufferGotSignal);
  137. continue;
  138. }
  139. KB_OSPSemGet(Dmx_SemLock, KB_Wait, 0);
  140. pChInfo = &gChnnlInfo[chId];
  141. DataSize = 0;
  142. Dmx_Buf = KD_DMXAllocate(DMX_SECTION_SIZE);
  143. if(Dmx_Buf == NULL)
  144. {
  145.             DBGPrint("Dmx_Buf == NULLn");
  146. KB_OSPSemSet(Dmx_SemLock);
  147. continue;
  148. }
  149. switch(STPTI_SlotTypes[pChInfo->Type])
  150. {
  151. case STPTI_SLOT_TYPE_PES:
  152. #ifdef STPTI_DVB_SUPPORT
  153. ST_ErrorCode = STPTI_BufferReadPes(pChInfo->BufferHandle, (U8*)Dmx_Buf, (U32)DMX_SECTION_SIZE,
  154.    NULL, 0, (U32*)&DataSize,
  155.    STPTI_COPY_TRANSFER_BY_MEMCPY);
  156. #else
  157. STPTI_BufferFlush(WhichBufferGotSignal);
  158. KD_DMXFree(Dmx_Buf);
  159. KB_OSPSemSet(Dmx_SemLock);
  160. continue;
  161. #endif
  162. break;
  163.     case STPTI_SLOT_TYPE_SECTION: /* three type are same: section */
  164.     case STPTI_SLOT_TYPE_EMM:
  165.     case STPTI_SLOT_TYPE_ECM:
  166. ST_ErrorCode = STPTI_BufferReadSection(pChInfo->BufferHandle, NULL, 0, &NumFilterMatches, &CRCValid,
  167.                                                (U8*)Dmx_Buf, (U32)DMX_SECTION_SIZE, NULL, 0,
  168.                                                (U32*)&DataSize, STPTI_COPY_TRANSFER_BY_MEMCPY);
  169. break;
  170.     case STPTI_SLOT_TYPE_RAW:
  171. ST_ErrorCode = STPTI_BufferReadTSPacket(pChInfo->BufferHandle, (U8*)Dmx_Buf, (U32)DMX_SECTION_SIZE,
  172.    NULL, 0, (U32*)&DataSize,
  173.    STPTI_COPY_TRANSFER_BY_MEMCPY);
  174. break;
  175. default:
  176. /* do no support type */
  177. STPTI_BufferFlush(WhichBufferGotSignal);
  178. KD_DMXFree(Dmx_Buf);
  179. KB_OSPSemSet(Dmx_SemLock);
  180. continue;
  181. break;
  182. }
  183.         if (ST_ErrorCode != ST_NO_ERROR)    /* unexpected fault reading data */
  184.         {
  185.                      STPTI_BufferFlush(WhichBufferGotSignal);
  186. KD_DMXFree(Dmx_Buf);
  187. KB_OSPSemSet(Dmx_SemLock);
  188. continue;
  189.         }
  190. if(DataSize == 0)
  191. {
  192. KD_DMXFree(Dmx_Buf);
  193. KB_OSPSemSet(Dmx_SemLock);
  194. continue;
  195. }
  196. if(pChInfo->NotifyFunctionPtr != NULL)
  197. {
  198. pChInfo->NotifyFunctionPtr(chId, Dmx_Buf, DataSize);
  199. if(pChInfo->InternalUnlock == TRUE)
  200. {
  201. KD_DMXFree(Dmx_Buf);
  202. }
  203. }
  204. KB_OSPSemSet(Dmx_SemLock);
  205. }
  206. }
  207. static INT32 Associate(UINT32 channelID, UINT32 filterID)
  208. {
  209. int i;
  210. for(i=0;i<BACKDATA_N;i++){
  211. if(g_backdata[i].channelID==channelID&&g_backdata[i].filterID==filterID)
  212. break;
  213. }
  214. if(i==BACKDATA_N){
  215. for(i=0;i<BACKDATA_N;i++){
  216. if(g_backdata[i].channelID==0&&g_backdata[i].filterID==0){
  217. g_backdata[i].channelID=channelID;
  218. g_backdata[i].filterID=filterID;
  219. break;
  220. }
  221. }
  222. if(i==BACKDATA_N)
  223. return RETFIAL1;
  224. }
  225. else
  226. return RETFIAL1;
  227. return RETOK;
  228. }
  229. static  INT32 Disassociate(UINT32 channelID, UINT32 filterID)
  230. {
  231. int i;
  232. for(i=0;i<BACKDATA_N;i++){
  233. if(g_backdata[i].channelID==channelID&&g_backdata[i].filterID==filterID){
  234. g_backdata[i].channelID=0;
  235. g_backdata[i].filterID=0;
  236. break;
  237. }
  238. }
  239. if(i==BACKDATA_N)
  240. return RETFIAL1;
  241. return RETOK;
  242. }
  243. #if 0
  244. static void AddDescramblerId(UINT32 descramblerID)
  245. {
  246. int i;
  247. for(i=0;i<BACKID_N;i++){
  248. if(g_backid[i]==0){
  249. g_backid[i]=descramblerID;
  250. break;
  251. }
  252. }
  253. }
  254. static BOOL RemoveDescramblerId(UINT32 descramblerID)
  255. {
  256. int i;
  257. for(i=0;i<BACKID_N;i++){
  258. if(g_backid[i]==descramblerID){
  259. g_backid[i]=0;
  260. return TRUE;
  261. }
  262. }
  263. return FALSE;
  264. }
  265. static BOOL IsDescramblerId(UINT32 descramblerID)
  266. {
  267. int i;
  268. for(i=0;i<BACKID_N;i++){
  269. if(g_backid[i]==descramblerID)
  270. return TRUE;
  271. }
  272. return FALSE;
  273. }
  274. #endif
  275. INT32 KB_DmxInit(void)
  276. {
  277. INT32 i, Index;
  278. ST_ErrorCode_t         ST_ErrorCode;
  279.     STPTI_InitParams_t     STPTI_InitParams;
  280.     STPTI_OpenParams_t     STPTI_OpenParams;
  281. UINT32 taskId;
  282. KB_OSPSemInit("DMXS", 1, J_OSP_WAIT_FIFO, &Dmx_SemLock);
  283. /* Init the data struct info */
  284. for(i = 0; i < KB_DMX_TOTAL_GENERIC_CHANNELS; i++)
  285. {
  286. gChnnlInfo[i].Allocated  = FALSE;
  287. gChnnlInfo[i].Started = FALSE;
  288. gChnnlInfo[i].InternalUnlock = TRUE;
  289. gChnnlInfo[i].Type = DMX_CHANNEL_INVALID;
  290. gChnnlInfo[i].SlotHandle = STPTI_NullHandle();
  291. gChnnlInfo[i].Pid = STPTI_InvalidPid();
  292. gChnnlInfo[i].BufferHandle = STPTI_NullHandle();
  293. gChnnlInfo[i].BufferSize = 0;
  294. gChnnlInfo[i].UserBuffer_p = NULL;
  295. gChnnlInfo[i].UserBufferSize  = 0;
  296. gChnnlInfo[i].SignalHandle = STPTI_NullHandle();
  297. gChnnlInfo[i].DescramblerHandle = STPTI_NullHandle();
  298. gChnnlInfo[i].NotifyFunctionPtr = NULL;
  299. }
  300. /* Init stpti */
  301. STPTI_InitParams.Device                     = PTI_DEVICE;
  302.     STPTI_InitParams.TCDeviceAddress_p          = (STPTI_DevicePtr_t)PTI_TCDEVICEADDRESS;
  303.     STPTI_InitParams.InterruptNumber            = PTI_INTERRUPTNUMBER;
  304.     STPTI_InitParams.DescramblerAssociation     = STPTI_DESCRAMBLER_ASSOCIATION_ASSOCIATE_WITH_PIDS;
  305.     STPTI_InitParams.Partition_p                = SystemPartition;
  306.     STPTI_InitParams.NCachePartition_p          = NcachePartition;
  307.     strcpy( STPTI_InitParams.EventHandlerName, "EVT" );
  308.     STPTI_InitParams.EventProcessPriority       = STPTI_EVENT_PROCESS_PRIORITY;
  309.     STPTI_InitParams.InterruptProcessPriority   = STPTI_INTERRUPT_PROCESS_PRIORITY;
  310.     #if defined(STPTI_CAROUSEL_SUPPORT)
  311.         STPTI_InitParams.CarouselProcessPriority    = 0;
  312.         STPTI_InitParams.CarouselEntryType          = STPTI_CAROUSEL_ENTRY_TYPE_SIMPLE;
  313.     #endif
  314.     STPTI_InitParams.InterruptLevel             = PTI_INTERRUPT_LEVEL;
  315.     STPTI_InitParams.SectionFilterOperatingMode = PTI_SECTIONFILTERMODE;
  316.     #if defined(STPTI_INDEX_SUPPORT)
  317.         STPTI_InitParams.IndexAssociation           = STPTI_INDEX_ASSOCIATION_ASSOCIATE_WITH_SLOTS;
  318.         STPTI_InitParams.IndexProcessPriority       = 0;
  319.     #endif
  320.     STPTI_InitParams.StreamID = STPTI_STREAM_ID_NOTAGS;
  321.     STPTI_InitParams.PacketArrivalTimeSource = STPTI_ARRIVAL_TIME_SOURCE_PTI;
  322. for(Index = 0; Index < KB_PTI_MAX_NUM ; Index++)
  323. {
  324. STPTI_InitParams.TCLoader_p = PTI_DVBTCLOADER; /* for PTI4 */
  325.         STPTI_InitParams.TCCodes    = STPTI_SUPPORTED_TCCODES_SUPPORTS_DVB;
  326. STPTI_InitParams.NumberOfSlots      = KB_DMX_TOTAL_GENERIC_CHANNELS;
  327.     STPTI_InitParams.DiscardOnCrcError  = FALSE;
  328. /* initialises PTI device */
  329.     ST_ErrorCode = STPTI_Init(g_KB_PTIDeviceNameMultiple[Index], &STPTI_InitParams);
  330.     if (ST_ErrorCode != ST_NO_ERROR)
  331.     {
  332.       STTBX_Print(("%sn", KB_ErrorGetText(ST_ErrorCode) ));
  333.       return( ST_ErrorCode );
  334.     }
  335.     STTBX_Print(("%sn", STPTI_GetRevision() ));
  336. STPTI_OpenParams.DriverPartition_p    = SystemPartition;
  337.         STPTI_OpenParams.NonCachedPartition_p = NcachePartition;
  338.         /* open PTI device */
  339.         STTBX_Print(("PTI_Open = "));
  340.         ST_ErrorCode = STPTI_Open(g_KB_PTIDeviceNameMultiple[Index], &STPTI_OpenParams, &g_KB_PTIHandleMultiple[Index]);
  341.         STTBX_Print(("%sn", KB_ErrorGetText(ST_ErrorCode) ));
  342.         if (ST_ErrorCode != ST_NO_ERROR) return RETFIAL1;
  343. ST_ErrorCode = STPTI_EnableErrorEvent(g_KB_PTIDeviceNameMultiple[Index], STPTI_EVENT_SECTIONS_DISCARDED_ON_CRC_CHECK_EVT);
  344.         if (ST_ErrorCode != ST_NO_ERROR)
  345.             return( ST_ErrorCode );
  346.         ST_ErrorCode = STPTI_EnableErrorEvent(g_KB_PTIDeviceNameMultiple[Index], STPTI_EVENT_TC_CODE_FAULT_EVT);
  347.         if (ST_ErrorCode != ST_NO_ERROR)
  348.             return( ST_ErrorCode );
  349.         ST_ErrorCode = STPTI_EnableErrorEvent(g_KB_PTIDeviceNameMultiple[Index], STPTI_EVENT_INVALID_PARAMETER_EVT);
  350.         if (ST_ErrorCode != ST_NO_ERROR)
  351.             return( ST_ErrorCode );
  352.         ST_ErrorCode = STPTI_EnableErrorEvent(g_KB_PTIDeviceNameMultiple[Index], STPTI_EVENT_BUFFER_OVERFLOW_EVT);
  353.         if (ST_ErrorCode != ST_NO_ERROR)
  354.             return( ST_ErrorCode );
  355.         /* Enable receipt of a number of STPTI events */
  356.         ST_ErrorCode = STPTI_EnableErrorEvent(g_KB_PTIDeviceNameMultiple[Index], STPTI_EVENT_CC_ERROR_EVT);
  357.         if (ST_ErrorCode != ST_NO_ERROR)
  358.             return( ST_ErrorCode );
  359.         ST_ErrorCode = STPTI_EnableErrorEvent(g_KB_PTIDeviceNameMultiple[Index], STPTI_EVENT_INTERRUPT_FAIL_EVT);
  360.         if (ST_ErrorCode != ST_NO_ERROR)
  361.             return( ST_ErrorCode );
  362.         ST_ErrorCode = STPTI_EnableErrorEvent(g_KB_PTIDeviceNameMultiple[Index], STPTI_EVENT_INVALID_LINK_EVT);
  363.         if (ST_ErrorCode != ST_NO_ERROR)
  364.             return( ST_ErrorCode );
  365.         ST_ErrorCode = STPTI_EnableErrorEvent(g_KB_PTIDeviceNameMultiple[Index], STPTI_EVENT_PACKET_ERROR_EVT);
  366.         if (ST_ErrorCode != ST_NO_ERROR)
  367.             return( ST_ErrorCode );
  368. }
  369. ST_ErrorCode = STPTI_SignalAllocate(g_KB_PTIHandleMultiple[DEFAULT_PTI], &gSinHandle);
  370. if (ST_ErrorCode != ST_NO_ERROR) return( ST_ErrorCode );
  371. KB_OSPTaskInit("DMXT", 10240, Dmx_Task, DMX_PRIORITY, NULL, &taskId);
  372. return( ST_ErrorCode );
  373. }
  374. INT32 KB_DmxAllocateChnnl(KB_DMXChannelType channelType, UINT32 bufferSize, BOOL isInternal, UINT32 *channelID)
  375. {
  376. INT32 rc = RETFIAL1;
  377. ST_ErrorCode_t   ST_ErrorCode;
  378. STPTI_SlotData_t STPTI_SlotData;
  379. INT32 index;
  380. if( (channelType == DMX_CHANNEL_INVALID) || (channelID == NULL) )
  381. {
  382. return RETFAIL3;
  383. }
  384. *channelID = KB_DMX_TOTAL_GENERIC_CHANNELS;
  385. STPTI_SlotData.SlotType = STPTI_SlotTypes[channelType];
  386.     STPTI_SlotData.SlotFlags.SignalOnEveryTransportPacket   = FALSE;
  387.     STPTI_SlotData.SlotFlags.CollectForAlternateOutputOnly  = FALSE;
  388.     STPTI_SlotData.SlotFlags.AlternateOutputInjectCarouselPacket = FALSE;
  389.     STPTI_SlotData.SlotFlags.StoreLastTSHeader  = FALSE;
  390.     STPTI_SlotData.SlotFlags.InsertSequenceError    = FALSE;
  391.     STPTI_SlotData.SlotFlags.OutPesWithoutMetadata  = FALSE;
  392.     STPTI_SlotData.SlotFlags.ForcePesLengthToZero   = FALSE;
  393.     STPTI_SlotData.SlotFlags.AppendSyncBytePrefixToRawData = FALSE;
  394.     STPTI_SlotData.SlotFlags.SoftwareCDFifo = FALSE;
  395. index = GetFirstEmptyChannelID();
  396. if(index == KB_DMX_TOTAL_GENERIC_CHANNELS)
  397. {
  398. return rc;
  399. }
  400. else
  401. {
  402. ST_ErrorCode = STPTI_SlotAllocate( g_KB_PTIHandleMultiple[DEFAULT_PTI],
  403.                                            &gChnnlInfo[index].SlotHandle,
  404.                                            &STPTI_SlotData);
  405.         if (ST_ErrorCode == ST_NO_ERROR)
  406.         {
  407. gChnnlInfo[index].Allocated = TRUE;
  408. gChnnlInfo[index].Type = channelType;
  409. if(RETOK == KD_DmxAllocateBuf(index, bufferSize))
  410. {
  411. gChnnlInfo[index].InternalUnlock = isInternal;
  412. *channelID = index;
  413. rc = RETOK;
  414. }
  415. else
  416. {
  417. /* can't allocate buffer, so deallocate the slot */
  418. STPTI_SlotDeallocate(gChnnlInfo[index].SlotHandle);
  419. gChnnlInfo[index].SlotHandle = STPTI_NullHandle();
  420. gChnnlInfo[index].Allocated = FALSE;
  421. gChnnlInfo[index].Type = DMX_CHANNEL_INVALID;
  422. }
  423.         }
  424. }
  425. return rc;
  426. }
  427. INT32 KB_DmxFreeChnnl(UINT32 channelID)
  428. {
  429. DMX_Channel *pChInfo;
  430. if(channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS) return RETFAIL3;
  431. if(gChnnlInfo[channelID].Allocated == FALSE)
  432. {
  433. /* This channel has not been allocated. return success immediately */
  434. return RETOK;
  435. }
  436. pChInfo = &gChnnlInfo[channelID];
  437. /* Stop the channel first */
  438. if(pChInfo->Started == TRUE)
  439. {
  440. pChInfo->Started = FALSE;
  441. }
  442. if(pChInfo->BufferHandle != STPTI_NullHandle())
  443. {
  444.     STPTI_SlotUnLink(pChInfo->SlotHandle);
  445. if(pChInfo->SignalHandle != STPTI_NullHandle())
  446. {
  447.      STPTI_SignalDisassociateBuffer(pChInfo->SignalHandle, pChInfo->BufferHandle);
  448. pChInfo->SignalHandle = STPTI_NullHandle();
  449.     }
  450.     STPTI_BufferDeallocate(pChInfo->BufferHandle);
  451. pChInfo->BufferHandle = STPTI_NullHandle();
  452. }
  453.     STPTI_SlotDeallocate(pChInfo->SlotHandle);
  454. pChInfo->SlotHandle = STPTI_NullHandle();
  455. pChInfo->Allocated = FALSE;
  456. pChInfo->Type = DMX_CHANNEL_INVALID;
  457. pChInfo->InternalUnlock = TRUE;
  458. return RETOK;
  459. }
  460. INT32 KB_DmxSetChnnlPID(UINT32 channelID, UINT16 pid)
  461. {
  462. ST_ErrorCode_t   ST_ErrorCode;
  463. INT32 rc;
  464. if( (channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  465.    || (gChnnlInfo[channelID].Allocated == FALSE) )
  466. {
  467. return RETFAIL3;
  468. }
  469.    ST_ErrorCode = STPTI_SlotClearPid(gChnnlInfo[channelID].SlotHandle);
  470. if(ST_NO_ERROR != ST_ErrorCode)
  471. {
  472. rc = RETFIAL1;
  473. }
  474. ST_ErrorCode = STPTI_SlotSetPid(gChnnlInfo[channelID].SlotHandle, (STPTI_Pid_t)pid);
  475. if(ST_NO_ERROR != ST_ErrorCode)
  476. {
  477. rc = RETFIAL1;
  478. }
  479. else
  480. {
  481. gChnnlInfo[channelID].Pid = pid;
  482. if(gChnnlInfo[channelID].BufferHandle != STPTI_NullHandle())
  483. {
  484. STPTI_BufferFlush(gChnnlInfo[channelID].BufferHandle);
  485. }
  486. rc = RETOK;
  487. }
  488. return rc;
  489. }
  490. INT32 KB_DmxControlChnnl(UINT32 channelID, UINT32 channelCmd)
  491. {
  492. ST_ErrorCode_t   ST_ErrorCode = ST_NO_ERROR;
  493. if( (channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  494.    || (gChnnlInfo[channelID].Allocated == FALSE) )
  495. {
  496. return RETFAIL3;
  497. }
  498. switch(channelCmd)
  499. {
  500. case KB_DMX_START:
  501. ST_ErrorCode = STPTI_SlotSetPid(gChnnlInfo[channelID].SlotHandle, (STPTI_Pid_t)gChnnlInfo[channelID].Pid);
  502. break;
  503. case KB_DMX_STOP:
  504. ST_ErrorCode = STPTI_SlotClearPid(gChnnlInfo[channelID].SlotHandle);
  505. break;
  506. case KB_DMX_RESET:
  507. break;
  508. }
  509. if(ST_NO_ERROR != ST_ErrorCode)
  510. {
  511. return RETFIAL1;
  512. }
  513. return RETOK;
  514. }
  515. INT32 KB_DmxRegNotice(UINT32 channelID, KB_DMX_NotifyFunc funcPtr)
  516. {
  517. if( (channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  518.    || (gChnnlInfo[channelID].Allocated == FALSE) )
  519. {
  520. return RETFAIL3;
  521. }
  522. gChnnlInfo[channelID].NotifyFunctionPtr = funcPtr;
  523. return RETOK;
  524. }
  525. /* filter functions */
  526. INT32 KB_DmxAllocateFilter(KB_DMXFilterType filterType, UINT32 filterDepth, UINT32 *filterID)
  527. {
  528. ST_ErrorCode_t  ST_ErrorCode;
  529. STPTI_FilterType_t stpti_FilterType;
  530. if(filterID == NULL)
  531. {
  532. return RETFAIL3;
  533. }
  534. switch(filterType)
  535. {
  536. case DMX_SECTION_FILTER:
  537. stpti_FilterType = STPTI_FILTER_TYPE_SECTION_FILTER_SHORT_MODE;
  538. break;
  539.     case DMX_SECTION_FILTER_NEG_MATCH_MODE:
  540. stpti_FilterType = STPTI_FILTER_TYPE_SECTION_FILTER_NEG_MATCH_MODE;
  541. break;
  542.     case DMX_PES_FILTER:
  543. stpti_FilterType = STPTI_FILTER_TYPE_PES_FILTER;
  544. break;
  545.     case DMX_TSHEADER_FILTER:
  546. stpti_FilterType = STPTI_FILTER_TYPE_TSHEADER_FILTER;
  547. break;
  548. default:
  549. return RETFIAL1;
  550. break;
  551. }
  552. filterDepth = filterDepth; /* avoid compiler warning */
  553. *filterID = STPTI_NullHandle();
  554. ST_ErrorCode = STPTI_FilterAllocate(g_KB_PTIHandleMultiple[DEFAULT_PTI],
  555.   stpti_FilterType,
  556.   (STPTI_Filter_t*)filterID);
  557. if(ST_NO_ERROR != ST_ErrorCode)
  558. {
  559. return RETFIAL1;
  560. }
  561. return RETOK;
  562. }
  563. INT32 KB_DmxFreeFilter(UINT32 filterID)
  564. {
  565. ST_ErrorCode_t  ST_ErrorCode;
  566. if(filterID == STPTI_NullHandle()) return RETFAIL3;
  567. ST_ErrorCode = STPTI_FilterDeallocate((STPTI_Filter_t)filterID);
  568. if(ST_NO_ERROR != ST_ErrorCode)
  569. {
  570. return RETFIAL1;
  571. }
  572. return RETOK;
  573. }
  574. INT32 KB_DmxSetFilter(UINT32 filterID, UINT8* filterMatch, UINT8* filterMask, UINT32 filterNegate)
  575. {
  576. ST_ErrorCode_t  ST_ErrorCode;
  577. STPTI_FilterData_t FilterData;
  578. WORD2BYTE *DataMask;
  579. UINT8 stFilterMatch[16];
  580. UINT8 stFilterMask[16];
  581. if( (filterID == STPTI_NullHandle())
  582. || (filterMatch == NULL) || (filterMask == NULL) )
  583. {
  584. return RETFAIL3;
  585. }
  586. filterNegate = filterNegate; /* No effect. Only avoid compiler warning */
  587. /* incoming data is in the following format:
  588.     * BYTE DESCRIPTION BYTE_POSITION_IN_SF EXAMPLE
  589.     * =============================================================
  590.     * 0 TABLE_ID (8) MS_WORD.BYTE0 PAT_TABLE_ID
  591.     * 1,2 SECTION_LENGTH (12) - -
  592.     * 3,4 TP_STREAM_ID (16) MS_WORD.BYTE1,2 0x0000
  593.     * 5.6-7 RESERVED (2) MS_WORD.BYTE3.6-7 0x3
  594.     * 5.5-1 VERSION_NUMBER (5) MS_WORD.BYTE3.5-1 0
  595.     * 5.0 CUR_NXT_INDICATOR (1) MS_WORD.BYTE3.0 0
  596.     * 6 SECTION_NUMBER (8) LS_WORD.BYTE0 0
  597.     * 7 LAST_SECTION_NUMBER (8) LS_WORD.BYTE1 0
  598.     * 8 - LS_WORD.BYTE2 0
  599.     * 9 - LS_WORD.BYTE3 0
  600.     */
  601. memset(stFilterMatch, 0, sizeof(stFilterMatch));
  602. memset(stFilterMask, 0, sizeof(stFilterMask));
  603. DataMask = (WORD2BYTE *)&stFilterMatch[0];
  604. DataMask->byte.ucByte0 = filterMatch[0];
  605. DataMask->byte.ucByte1 = filterMatch[3];
  606. DataMask->byte.ucByte2 = filterMatch[4];
  607. DataMask->byte.ucByte3 = filterMatch[5];
  608. DataMask = (WORD2BYTE *)&stFilterMatch[4];
  609. DataMask->byte.ucByte0 = filterMatch[6];
  610. DataMask->byte.ucByte1 = filterMatch[7];
  611. DataMask = (WORD2BYTE *)&stFilterMask[0];
  612. DataMask->byte.ucByte0 = filterMask[0];
  613. DataMask->byte.ucByte1 = filterMask[3];
  614. DataMask->byte.ucByte2 = filterMask[4];
  615. DataMask->byte.ucByte3 = filterMask[5];
  616. DataMask = (WORD2BYTE *)&stFilterMask[4];
  617. DataMask->byte.ucByte0 = filterMask[6];
  618. DataMask->byte.ucByte1 = filterMask[7];
  619. FilterData.FilterBytes_p = (U8*)stFilterMatch;
  620. FilterData.FilterMasks_p = (U8*)stFilterMask;
  621. FilterData.FilterType = STPTI_FILTER_TYPE_SECTION_FILTER_SHORT_MODE;
  622. FilterData.FilterRepeatMode = STPTI_FILTER_REPEAT_MODE_STPTI_FILTER_REPEATED;
  623.     FilterData.InitialStateEnabled = TRUE; 
  624. FilterData.u.SectionFilter.DiscardOnCrcError  = TRUE;
  625.     FilterData.u.SectionFilter.ModePattern_p      = NULL;
  626.     FilterData.u.SectionFilter.NotMatchMode       = FALSE;
  627. ST_ErrorCode = STPTI_FilterSet((STPTI_Filter_t)filterID, &FilterData);
  628. if(ST_NO_ERROR != ST_ErrorCode)
  629. {
  630. return RETFIAL1;
  631. }
  632. return RETOK;
  633. }
  634. //-add by shriek-
  635. INT32 KB_DmxSetFilterByType(UINT32 filterID, UINT8* filterMatch, UINT8* filterMask, UINT32 filterNegate, STPTI_FilterType_t filterType)
  636. {
  637. ST_ErrorCode_t  ST_ErrorCode;
  638. STPTI_FilterData_t FilterData;
  639. WORD2BYTE *DataMask;
  640. UINT8 stFilterMatch[16];
  641. UINT8 stFilterMask[16];
  642. if( (filterID == STPTI_NullHandle())
  643. || (filterMatch == NULL) || (filterMask == NULL) )
  644. {
  645. return RETFAIL3;
  646. }
  647. filterNegate = filterNegate; /* No effect. Only avoid compiler warning */
  648. /* incoming data is in the following format:
  649.     * BYTE DESCRIPTION BYTE_POSITION_IN_SF EXAMPLE
  650.     * =============================================================
  651.     * 0 TABLE_ID (8) MS_WORD.BYTE0 PAT_TABLE_ID
  652.     * 1,2 SECTION_LENGTH (12) - -
  653.     * 3,4 TP_STREAM_ID (16) MS_WORD.BYTE1,2 0x0000
  654.     * 5.6-7 RESERVED (2) MS_WORD.BYTE3.6-7 0x3
  655.     * 5.5-1 VERSION_NUMBER (5) MS_WORD.BYTE3.5-1 0
  656.     * 5.0 CUR_NXT_INDICATOR (1) MS_WORD.BYTE3.0 0
  657.     * 6 SECTION_NUMBER (8) LS_WORD.BYTE0 0
  658.     * 7 LAST_SECTION_NUMBER (8) LS_WORD.BYTE1 0
  659.     * 8 - LS_WORD.BYTE2 0
  660.     * 9 - LS_WORD.BYTE3 0
  661.     */
  662. memset(stFilterMatch, 0, sizeof(stFilterMatch));
  663. memset(stFilterMask, 0, sizeof(stFilterMask));
  664. DataMask = (WORD2BYTE *)&stFilterMatch[0];
  665. DataMask->byte.ucByte0 = filterMatch[0];
  666. DataMask->byte.ucByte1 = filterMatch[3];
  667. DataMask->byte.ucByte2 = filterMatch[4];
  668. DataMask->byte.ucByte3 = filterMatch[5];
  669. DataMask = (WORD2BYTE *)&stFilterMatch[4];
  670. DataMask->byte.ucByte0 = filterMatch[6];
  671. DataMask->byte.ucByte1 = filterMatch[7];
  672. DataMask = (WORD2BYTE *)&stFilterMask[0];
  673. DataMask->byte.ucByte0 = filterMask[0];
  674. DataMask->byte.ucByte1 = filterMask[3];
  675. DataMask->byte.ucByte2 = filterMask[4];
  676. DataMask->byte.ucByte3 = filterMask[5];
  677. DataMask = (WORD2BYTE *)&stFilterMask[4];
  678. DataMask->byte.ucByte0 = filterMask[6];
  679. DataMask->byte.ucByte1 = filterMask[7];
  680. FilterData.FilterBytes_p = (U8*)stFilterMatch;
  681. FilterData.FilterMasks_p = (U8*)stFilterMask;
  682. FilterData.FilterType = STPTI_FILTER_TYPE_SECTION_FILTER_SHORT_MODE;
  683. FilterData.FilterRepeatMode = filterType;
  684.        FilterData.InitialStateEnabled = TRUE; 
  685. FilterData.u.SectionFilter.DiscardOnCrcError  = TRUE;
  686.        FilterData.u.SectionFilter.ModePattern_p      = NULL;
  687.        FilterData.u.SectionFilter.NotMatchMode       = FALSE;
  688. ST_ErrorCode = STPTI_FilterSet((STPTI_Filter_t)filterID, &FilterData);
  689. if(ST_NO_ERROR != ST_ErrorCode)
  690. {
  691. printf("n [KB_DmxSetFilterByType]: ST_ErrorCode=%d",ST_ErrorCode);
  692. return RETFIAL1;
  693. }
  694. return RETOK;
  695. }
  696. INT32 KB_DmxAssociateFilter(UINT32 channelID, UINT32 filterID)
  697. {
  698. ST_ErrorCode_t  ST_ErrorCode;
  699. if( (channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  700.    || (gChnnlInfo[channelID].Allocated == FALSE)
  701.    || (filterID == STPTI_NullHandle()) )
  702. {
  703. return RETFAIL3;
  704. }
  705. ST_ErrorCode = STPTI_FilterAssociate((STPTI_Filter_t)filterID, gChnnlInfo[channelID].SlotHandle);
  706. if(ST_NO_ERROR != ST_ErrorCode)
  707. {
  708. return RETFIAL1;
  709. }
  710. Associate(channelID, filterID);
  711. return RETOK;
  712. }
  713. INT32 KB_DmxDisassociateFilter(UINT32 channelID, UINT32 filterID)
  714. {
  715. ST_ErrorCode_t  ST_ErrorCode;
  716. if( (channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  717.    || (gChnnlInfo[channelID].Allocated == FALSE)
  718.    || (filterID == STPTI_NullHandle()) )
  719. {
  720. return RETFAIL3;
  721. }
  722. if(Disassociate(channelID, filterID)==RETFIAL1)
  723. return RETFIAL1;
  724. ST_ErrorCode = STPTI_FilterDisassociate((STPTI_Filter_t)filterID, gChnnlInfo[channelID].SlotHandle);
  725. if(ST_NO_ERROR != ST_ErrorCode)
  726. {
  727. return RETFIAL1;
  728. }
  729. return RETOK;
  730. }
  731. /* buffer functions */
  732. static INT32 KD_DmxAllocateBuf(UINT32 channelID, UINT32 bufferSize)
  733. {
  734. ST_ErrorCode_t   ST_ErrorCode;
  735. DMX_Channel *pChInfo;
  736. if( (channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  737.    || (gChnnlInfo[channelID].Allocated == FALSE) )
  738. {
  739. return RETFAIL3;
  740. }
  741. if(0 == bufferSize)
  742. {
  743. return RETFAIL3;
  744. }
  745. pChInfo = &gChnnlInfo[channelID];
  746. if(pChInfo->BufferHandle != STPTI_NullHandle()) /* Already allocated buffer for this slot */
  747. {
  748. return RETFIAL1;
  749. }
  750. ST_ErrorCode = STPTI_BufferAllocate(g_KB_PTIHandleMultiple[DEFAULT_PTI],
  751. (U32)bufferSize, 1, &(pChInfo->BufferHandle));
  752. if(ST_NO_ERROR != ST_ErrorCode) goto Err_End;
  753. pChInfo->BufferSize = bufferSize;
  754. ST_ErrorCode = STPTI_SlotLinkToBuffer(pChInfo->SlotHandle, pChInfo->BufferHandle);
  755. if(ST_NO_ERROR != ST_ErrorCode) goto Err_Buf;
  756. STPTI_SlotClearPid(pChInfo->SlotHandle);
  757. STPTI_BufferFlush(pChInfo->BufferHandle);
  758. pChInfo->SignalHandle = gSinHandle;// 所有buffer均用同一个signal
  759. ST_ErrorCode = STPTI_SignalAssociateBuffer(pChInfo->SignalHandle, pChInfo->BufferHandle);
  760. if(ST_NO_ERROR != ST_ErrorCode)
  761. {
  762. pChInfo->SignalHandle = STPTI_NullHandle();
  763. goto Err_Buf;
  764. }
  765. return RETOK;
  766. Err_Buf:
  767. STPTI_SlotUnLink(pChInfo->SlotHandle);
  768. STPTI_BufferDeallocate(pChInfo->BufferHandle);
  769. pChInfo->BufferHandle = STPTI_NullHandle();
  770. pChInfo->BufferSize = 0;
  771. Err_End:
  772. return RETFIAL1;
  773. }
  774. INT32 KB_DmxUnlock(UINT32 channelID, UINT8* bufferPtr)
  775. {
  776. if( (channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  777.    || (gChnnlInfo[channelID].Allocated == FALSE) )
  778. {
  779. return RETFAIL3;
  780. }
  781. if(gChnnlInfo[channelID].InternalUnlock == TRUE)
  782. {
  783. return RETFIAL1;
  784. }
  785. KD_DMXFree(bufferPtr);
  786. return RETOK;
  787. }
  788. static void* KD_DMXAllocate(UINT32 size)
  789. {
  790. return memory_allocate(DemuxPartition, (size_t)size);
  791. }
  792. static void  KD_DMXFree(void *ptr)
  793. {
  794. if(ptr == NULL) return;
  795. memory_deallocate(DemuxPartition, ptr);
  796. }
  797. /* descrambler functions */
  798. INT32 KB_DmxOpenDescrambler(void)
  799. {
  800. ST_ErrorCode_t Error;
  801.     STPTI_Descrambler_t DescramblerHandle;
  802.     if (ST_NO_ERROR != (Error = STPTI_DescramblerAllocate(g_KB_PTIHandleMultiple[DEFAULT_PTI], &DescramblerHandle, STPTI_DESCRAMBLER_TYPE_DVB_DESCRAMBLER )))
  803.     {
  804.         return RETFIAL1;
  805.     }
  806. return (INT32)DescramblerHandle;
  807. }
  808. INT32 KB_DmxCloseDescrambler(UINT32 descramblerID)
  809. {
  810. ST_ErrorCode_t Error;
  811.     if (ST_NO_ERROR != (Error = STPTI_DescramblerDeallocate((STPTI_Descrambler_t)descramblerID)))
  812.     {
  813.         return RETFIAL1;
  814.     }
  815. return RETOK;
  816. }
  817. INT32 KB_DmxSetEventKey(UINT32 descramblerID, const UINT8* evenKey)
  818. {
  819. ST_ErrorCode_t Error;
  820. if(evenKey == NULL)
  821. {
  822. return RETFAIL3;
  823. }
  824. if (ST_NO_ERROR != (Error = STPTI_DescramblerSet((STPTI_Descrambler_t)descramblerID, STPTI_KEY_PARITY_EVEN_PARITY, STPTI_KEY_USAGE_VALID_FOR_TRANSPORT, (U8*)evenKey)))
  825. {
  826.         return RETFIAL1;
  827. }
  828. return RETOK;
  829. }
  830. INT32 KB_DmxSetOddKey(UINT32 descramblerID, const UINT8* oddKey)
  831. {
  832. ST_ErrorCode_t Error;
  833. if(oddKey == NULL)
  834. {
  835. return RETFAIL3;
  836. }
  837. if (ST_NO_ERROR != (Error = STPTI_DescramblerSet((STPTI_Descrambler_t)descramblerID, STPTI_KEY_PARITY_ODD_PARITY, STPTI_KEY_USAGE_VALID_FOR_TRANSPORT, (U8*)oddKey)))
  838. {
  839.         return RETFIAL1;
  840. }
  841. return RETOK;
  842. }
  843. INT32 KB_DmxSetDescPID(UINT32 descramblerID, UINT16 pid)
  844. {
  845. ST_ErrorCode_t Error;
  846. STPTI_SlotOrPid_t   SlotOrPid;
  847. SlotOrPid.Pid = (STPTI_Pid_t)pid;            
  848.     if (ST_NO_ERROR != (Error = STPTI_DescramblerAssociate((STPTI_Descrambler_t)descramblerID, SlotOrPid)))
  849.     {
  850.         return RETFIAL1;
  851.     }
  852. return RETOK;
  853. }
  854. INT32 KD_DmxCloseDescPID(UINT32 descramblerID, UINT16 pid)
  855. {
  856. ST_ErrorCode_t Error;
  857. STPTI_SlotOrPid_t   SlotOrPid;
  858. SlotOrPid.Pid = (STPTI_Pid_t)pid;  
  859.     if (ST_NO_ERROR != (Error = STPTI_DescramblerDisassociate((STPTI_Descrambler_t)descramblerID, SlotOrPid)))
  860.     {
  861.         return RETFIAL1;
  862.     }
  863. return RETOK;
  864. }
  865. static INT32 KD_DmxAllocateBuffCA(UINT32 channelID, UINT32 bufferSize)
  866. {
  867. ST_ErrorCode_t   ST_ErrorCode;
  868. DMX_Channel *pChInfo;
  869. if( (channelID >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  870.    || (gChnnlInfo[channelID].Allocated == FALSE) )
  871. {
  872. return RETFAIL3;
  873. }
  874. if(0 == bufferSize)
  875. {
  876. return RETFAIL3;
  877. }
  878. pChInfo = &gChnnlInfo[channelID];
  879. if(pChInfo->BufferHandle != STPTI_NullHandle()) /* Already allocated buffer for this slot */
  880. {
  881. return RETFIAL1;
  882. }
  883. ST_ErrorCode = STPTI_BufferAllocate(g_KB_PTIHandleMultiple[DEFAULT_PTI],
  884. (U32)bufferSize, 1, &(pChInfo->BufferHandle));
  885. if(ST_NO_ERROR != ST_ErrorCode) goto Err_End;
  886. pChInfo->BufferSize = bufferSize;
  887. ST_ErrorCode = STPTI_SlotLinkToBuffer(pChInfo->SlotHandle, pChInfo->BufferHandle);
  888. if(ST_NO_ERROR != ST_ErrorCode) goto Err_Buf;
  889. STPTI_SlotClearPid(pChInfo->SlotHandle);
  890. STPTI_BufferFlush(pChInfo->BufferHandle);
  891. pChInfo->SignalHandle = g_SignalHandle_for_TF;// 所有Ecm Emm均用同一个signal
  892. ST_ErrorCode = STPTI_SignalAssociateBuffer(pChInfo->SignalHandle, pChInfo->BufferHandle);
  893. if(ST_NO_ERROR != ST_ErrorCode)
  894. {
  895. pChInfo->SignalHandle = STPTI_NullHandle();
  896. goto Err_Buf;
  897. }
  898. return RETOK;
  899. Err_Buf:
  900. STPTI_SlotUnLink(pChInfo->SlotHandle);
  901. STPTI_BufferDeallocate(pChInfo->BufferHandle);
  902. pChInfo->BufferHandle = STPTI_NullHandle();
  903. pChInfo->BufferSize = 0;
  904. Err_End:
  905. return RETFIAL1;
  906. }
  907. INT32 KD_DmxAllocateChnnlCA(KB_DMXChannelType channelType, UINT32 bufferSize, BOOL isInternal, UINT32 *channelID)
  908. {
  909. INT32 rc = RETFIAL1;
  910. ST_ErrorCode_t   ST_ErrorCode;
  911. STPTI_SlotData_t STPTI_SlotData;
  912. INT32 index;
  913. if( (channelType == DMX_CHANNEL_INVALID) || (channelID == NULL) )
  914. {
  915. return RETFAIL3;
  916. }
  917. *channelID = KB_DMX_TOTAL_GENERIC_CHANNELS;
  918. STPTI_SlotData.SlotType = STPTI_SlotTypes[channelType];
  919.     STPTI_SlotData.SlotFlags.SignalOnEveryTransportPacket   = FALSE;
  920.     STPTI_SlotData.SlotFlags.CollectForAlternateOutputOnly  = FALSE;
  921.     STPTI_SlotData.SlotFlags.AlternateOutputInjectCarouselPacket = FALSE;
  922.     STPTI_SlotData.SlotFlags.StoreLastTSHeader  = FALSE;
  923.     STPTI_SlotData.SlotFlags.InsertSequenceError    = FALSE;
  924.     STPTI_SlotData.SlotFlags.OutPesWithoutMetadata  = FALSE;
  925.     STPTI_SlotData.SlotFlags.ForcePesLengthToZero   = FALSE;
  926.     STPTI_SlotData.SlotFlags.AppendSyncBytePrefixToRawData = FALSE;
  927.     STPTI_SlotData.SlotFlags.SoftwareCDFifo = FALSE;
  928. index = GetFirstEmptyChannelID();
  929. if(index == KB_DMX_TOTAL_GENERIC_CHANNELS)
  930. {
  931. }
  932. else
  933. {
  934. ST_ErrorCode = STPTI_SlotAllocate( g_KB_PTIHandleMultiple[DEFAULT_PTI],
  935.                                            &gChnnlInfo[index].SlotHandle,
  936.                                            &STPTI_SlotData);
  937.         if (ST_ErrorCode == ST_NO_ERROR)
  938.         {
  939. gChnnlInfo[index].Allocated = TRUE;
  940. gChnnlInfo[index].Type = channelType;
  941. if(RETOK == KD_DmxAllocateBuffCA(index, bufferSize))
  942. {
  943. gChnnlInfo[index].InternalUnlock = isInternal;
  944. *channelID = index;
  945. rc = RETOK;
  946. }
  947. else
  948. {
  949. /* can't allocate buffer, so deallocate the slot */
  950. STPTI_SlotDeallocate(gChnnlInfo[index].SlotHandle);
  951. gChnnlInfo[index].SlotHandle = STPTI_NullHandle();
  952. gChnnlInfo[index].Allocated = FALSE;
  953. gChnnlInfo[index].Type = DMX_CHANNEL_INVALID;
  954. }
  955.         }
  956.         
  957. }
  958. return rc;
  959. }
  960. static void Exm_Task(void *params)
  961. {
  962. ST_ErrorCode_t  ST_ErrorCode;
  963. STPTI_Buffer_t  WhichBufferGotSignal;
  964. DMX_Channel  *pChInfo;
  965. UINT32 DataSize;
  966. U32   NumFilterMatches = 0;
  967. BOOL  CRCValid;
  968. INT32  chId;
  969. while(1)
  970. {
  971. ST_ErrorCode = STPTI_SignalWaitBuffer(g_SignalHandle_for_TF, &WhichBufferGotSignal, STPTI_TIMEOUT_INFINITY);
  972. /* STPTI_SignalWaitBuffer() generated an error */
  973.     if (ST_ErrorCode == ST_ERROR_TIMEOUT)
  974.     {
  975.         continue;
  976.     }
  977.     /* if not a harmless timeout then get rid of anything in the STPTI internal buffer */
  978.     else if (ST_ErrorCode != ST_NO_ERROR)
  979.     {
  980.         STPTI_BufferFlush(WhichBufferGotSignal);
  981. continue;
  982.     }
  983. /* Find the channel, that buffer handle equal WhichBufferGotSignal */
  984. for(chId = 0; chId < KB_DMX_TOTAL_GENERIC_CHANNELS; chId++)
  985. {
  986. if( (gChnnlInfo[chId].Allocated == TRUE)
  987. && (gChnnlInfo[chId].BufferHandle == WhichBufferGotSignal) )
  988. {
  989. /* got it */
  990. break;
  991. }
  992. }
  993. if(chId >= KB_DMX_TOTAL_GENERIC_CHANNELS)
  994. {
  995. STPTI_BufferFlush(WhichBufferGotSignal);
  996. continue;
  997. }
  998. pChInfo = &gChnnlInfo[chId];
  999. DataSize = 0;
  1000. memset(Exm_Buf,0,EXM_BUFF_LEN);
  1001. switch(STPTI_SlotTypes[pChInfo->Type])
  1002. {
  1003. case STPTI_SLOT_TYPE_PES:
  1004. #ifdef STPTI_DVB_SUPPORT
  1005. ST_ErrorCode = STPTI_BufferReadPes(pChInfo->BufferHandle, (U8*)Exm_Buf, (U32)EXM_BUFF_LEN,
  1006.    NULL, 0, (U32*)&DataSize,
  1007.    STPTI_COPY_TRANSFER_BY_MEMCPY);
  1008. #else
  1009. STPTI_BufferFlush(WhichBufferGotSignal);
  1010. continue;
  1011. #endif
  1012. break;
  1013.     case STPTI_SLOT_TYPE_SECTION: /* three type are same: section */
  1014.     case STPTI_SLOT_TYPE_EMM:
  1015.     case STPTI_SLOT_TYPE_ECM:
  1016. ST_ErrorCode = STPTI_BufferReadSection(pChInfo->BufferHandle, NULL, 0, &NumFilterMatches, &CRCValid,
  1017.                                                (U8*)Exm_Buf, (U32)EXM_BUFF_LEN, NULL, 0,
  1018.                                                (U32*)&DataSize, STPTI_COPY_TRANSFER_BY_MEMCPY);
  1019. break;
  1020.     case STPTI_SLOT_TYPE_RAW:
  1021. ST_ErrorCode = STPTI_BufferReadTSPacket(pChInfo->BufferHandle, (U8*)Exm_Buf, (U32)EXM_BUFF_LEN,
  1022.    NULL, 0, (U32*)&DataSize,
  1023.    STPTI_COPY_TRANSFER_BY_MEMCPY);
  1024. break;
  1025. default:
  1026. /* do no support type */
  1027. STPTI_BufferFlush(WhichBufferGotSignal);
  1028. continue;
  1029. break;
  1030. }
  1031. if (ST_ErrorCode != ST_NO_ERROR)    /* unexpected fault reading data */
  1032. {
  1033. STPTI_BufferFlush(WhichBufferGotSignal);
  1034. continue;
  1035. }
  1036. if(DataSize == 0)
  1037. {
  1038. continue;
  1039. }
  1040. if(pChInfo->NotifyFunctionPtr != NULL)
  1041. {
  1042. pChInfo->NotifyFunctionPtr(chId, Exm_Buf, DataSize);
  1043. }
  1044. }
  1045. }
  1046. INT32 KD_DmxInitCA(void)
  1047. {
  1048. ST_ErrorCode_t         ST_ErrorCode;
  1049. UINT32 taskId;
  1050. ST_ErrorCode = STPTI_SignalAllocate(g_KB_PTIHandleMultiple[DEFAULT_PTI], &g_SignalHandle_for_TF);
  1051. if (ST_ErrorCode != ST_NO_ERROR) return( ST_ErrorCode );
  1052. KB_OSPTaskInit("EXMT", 10240, Exm_Task, DMX_PRIORITY, NULL, &taskId);
  1053. return( ST_ErrorCode );
  1054. }
  1055. /* EOF --------------------------------------------------------------------- */