Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * tdal_dmx_filter.c
- *
- * Created on: Apr 5, 2012
- * Author: mudri
- */
- /***********************************************************************/
- /* Includes */
- /***********************************************************************/
- #include "tdal_dmx_priv.h"
- /***********************************************************************/
- /* Defines */
- /***********************************************************************/
- /***********************************************************************/
- /* Macros */
- /***********************************************************************/
- #define CRC32_MAX_COEFFICIENTS 256
- #define CRC32_POLYNOMIAL 0x04C11DB7
- #ifdef PATCH_SECTION_PE
- #define kTDAL_DMX_PAT_TABLE_ID 0x00
- #define kTDAL_DMX_SDT_TABLE_ID 0x42
- #endif //PATCH_SECTION_PE
- /***********************************************************************/
- /* Typedefs */
- /***********************************************************************/
- /***********************************************************************/
- /* Global Variables (GLOBAL/IMPORT) */
- /***********************************************************************/
- /***********************************************************************/
- /* Local Module Variables (MODULE) */
- /***********************************************************************/
- /***********************************************************************/
- /* Local File Variables (LOCAL) */
- /***********************************************************************/
- #ifdef PATCH_SECTION_PE
- #define PAT_SECTIONS_TO_SKIP 2
- #define SDT_SECTIONS_TO_SKIP 2
- GLOBAL uint16_t patTsId = 0xFFFF;
- GLOBAL uint16_t sdtTsId = 0xFFFF;
- LOCAL uint8_t patCount = PAT_SECTIONS_TO_SKIP;
- LOCAL uint8_t sdtCount = SDT_SECTIONS_TO_SKIP;
- #endif //PATCH_SECTION_PE
- /***********************************************************************/
- /* Local Functions Declarations (LOCAL) */
- /***********************************************************************/
- LOCAL tTDAL_DMX_FilterId p_LookForFreeFilter(void);
- LOCAL void p_writeChecksum(uint8_t * const checksum, uint8_t const * block, size_t const length);
- /***********************************************************************/
- /* Functions Definitions (GLOBAL) */
- /***********************************************************************/
- /************************************************************************
- * Function Name : TDAL_DMX_Allocate_Filter
- *
- * Description : This function allocates a filter on a channel. The output
- * is the filter Id if succedded. Else the output is
- * kTDAL_DMX_ILLEGAL_FILTER.
- *
- * Side effects :
- *
- * Return : kTDAL_DMX_NO_ERROR if all is correct
- * kTDAL_DMX_ERROR_INVALID_CHANNEL_ID if bad Id
- * kTDAL_DMX_ERROR_TOO_MANY_REQ if there is no more filter available
- * kTDAL_DMX_ERROR_MUTEX for mutex issue
- * kTDAL_DMX_ERROR_UNABLE_TO_GRANT
- *
- ***********************************************************************/
- tTDAL_DMX_Error TDAL_DMX_Allocate_Filter(tTDAL_DMX_ChannelId ChannelId, tTDAL_DMX_FilterId *pFilterId)
- {
- tTDAL_DMX_Error Error = kTDAL_DMX_NO_ERROR;
- HRESULT hr = S_OK;
- tTDAL_DMX_FilterId FilterId;
- UINT32 BufferSize = 0, hBuffer = 0;
- uint8_t *pFilter = NULL;
- uint8_t *pMask = NULL;
- uint8_t *pMode = NULL;
- uint8_t i;
- mTBOX_FCT_ENTER("TDAL_DMX_Allocate_Filter");
- TDAL_LockMutex(TDAL_DMX_SectionMutex);
- if (!TDAL_DMX_Initialized)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL, "TDAL_DMX_Allocate_Filter: TDAL_DMX module has not been initialized\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_NOT_INITIALIZED;
- mTBOX_RETURN(Error);
- }
- if ((ChannelId < 0 || ChannelId >= kTDAL_DMX_MAX_NB_OF_CHANNELS) ||
- (TDAL_DMX_Channel[ChannelId].used == FALSE))
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"TDAL_DMX_Allocate_Filter: ChannelId not available or not allocated\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_INVALID_CHANNEL_ID;
- mTBOX_RETURN(Error);
- }
- if(TDAL_DMX_Channel[ChannelId].ChannelStream != eTDAL_DMX_SECTION_STREAM)
- {
- for(i = 0; i < kTDAL_DMX_MAX_NB_OF_FILTERS; i++)
- {
- if((TDAL_DMX_Filter[i].AssociatedChannelId == ChannelId) &&
- (TDAL_DMX_Filter[i].used == TRUE))
- {
- #if defined(TTXT_OSD_ON) && defined(TTXT_VBI_ON)
- if((TDAL_DMX_Filter[ChannelId].used) &&
- (TDAL_DMX_Filter[ChannelId].ChannelStream == eTDAL_DMX_TELETEXT_STREAM))
- {
- *pFilterId = TDAL_DMX_Filter[i].FilterId;
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(kTDAL_DMX_NO_ERROR);
- }
- #endif
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Allocate_Filter: filter already exist on this PES channel\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_TOO_MANY_REQ;
- mTBOX_RETURN(Error);
- }
- }
- }
- FilterId = p_LookForFreeFilter();
- if (FilterId == kTDAL_DMX_ILLEGAL_FILTER)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL, "Allocate_Filter: Unable to found a free filter\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_TOO_MANY_REQ;
- mTBOX_RETURN(Error);
- }
- if (TDAL_DMX_Channel[ChannelId].ChannelStream == eTDAL_DMX_SECTION_STREAM)
- {
- pFilter = TDAL_Malloc(kTDAL_DMX_DEPTH_SIZE);
- if (pFilter == NULL)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Allocate_Filter: Unable to allocate size for FilterBytes\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_MUTEX;
- mTBOX_RETURN(Error);
- }
- pMask = TDAL_Malloc(kTDAL_DMX_DEPTH_SIZE);
- if (pMask == NULL)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Allocate_Filter: Unable to allocate size for FilterMasks\n"));
- TDAL_Free(pFilter);
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_MUTEX;
- mTBOX_RETURN(Error);
- }
- pMode = TDAL_Malloc(kTDAL_DMX_DEPTH_SIZE);
- if (pMode == NULL)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Allocate_Filter: Unable to allocate size for ModePattern\n"));
- TDAL_Free(pFilter);
- TDAL_Free(pMask);
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_MUTEX;
- mTBOX_RETURN(Error);
- }
- memset(pFilter, 0x00, kTDAL_DMX_DEPTH_SIZE);
- memset(pMask, 0x00, kTDAL_DMX_DEPTH_SIZE);
- memset(pMode, 0x00, kTDAL_DMX_DEPTH_SIZE);
- BufferSize = 8 * kTDAL_DMX_MAX_SECTION_SIZE;
- hr = MV_PE_StreamBufAllocate(TDAL_DMX_hPE, BufferSize, &hBuffer);
- if(hr != S_OK)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Control_Filter: Error in MV_PE_StreamBufAllocate\n"));
- TDAL_Free(pFilter);
- TDAL_Free(pMask);
- TDAL_Free(pMode);
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- mTBOX_RETURN(Error);
- }
- }
- TDAL_DMX_Filter[FilterId].FilterId = FilterId;
- TDAL_DMX_Filter[FilterId].AssociatedChannelId = ChannelId;
- TDAL_DMX_Filter[FilterId].used = TRUE;
- TDAL_DMX_Filter[FilterId].enable = FALSE;
- TDAL_DMX_Filter[FilterId].waitingEnable = FALSE;
- TDAL_DMX_Filter[FilterId].set = FALSE;
- TDAL_DMX_Filter[FilterId].tag = 0;
- TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer = NULL;
- TDAL_DMX_Filter[FilterId].sectionBuffer.bufferSize = BufferSize;
- TDAL_DMX_Filter[FilterId].sectionBuffer.sectionSize = 0;
- TDAL_DMX_Filter[FilterId].hSource = kTDAL_DMX_INVALID_PE_SOURCE;
- TDAL_DMX_Filter[FilterId].hBuffer = hBuffer;
- TDAL_DMX_Filter[FilterId].BufferSize = BufferSize;
- TDAL_DMX_Filter[FilterId].hFilter = 0;
- TDAL_DMX_Filter[FilterId].Filter = pFilter;
- TDAL_DMX_Filter[FilterId].Mask = pMask;
- TDAL_DMX_Filter[FilterId].Mode = pMode;
- TDAL_DMX_Filter[FilterId].Size = 0;
- TDAL_DMX_Filter[FilterId].efilterCRCMode = eTDAL_DMX_CRC_CHECK;
- *pFilterId = FilterId;
- mTBOX_TRACE((kTBOX_NIV_3, "Allocate_Filter: PID -->> 0x%x, FilterId -->> %d\n", TDAL_DMX_Channel[TDAL_DMX_Filter[FilterId].AssociatedChannelId].ChannelPid, FilterId));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(Error);
- }
- /***********************************************************************
- * Function Name : TDAL_DMX_Set_Filter
- *
- * Description : This function sets a hardware section filter on channel with
- * specific condition ( table_id, table_id extension,
- * version number, section number .....)
- *
- * Side effects :
- *
- * Return : kTDAL_DMX_NO_ERROR if all is correct
- * kTDAL_DMX_ERROR_INVALID_CHANNEL_ID if channel is invalid
- * or channel not used or bad PID on the channel
- * kTDAL_DMX_ERROR_INVALID_FILTER_ID if filter is invalid
- * kTDAL_DMX_ERROR_BAD_PARAMETERS if size is bad
- * kTDAL_DMX_ERROR_MUTEX for mutex error
- * kTDAL_DMX_ERROR_UNABLE_TO_GRANT
- *
- **********************************************************************/
- tTDAL_DMX_Error TDAL_DMX_Set_Filter(tTDAL_DMX_ChannelId ChannelId,
- tTDAL_DMX_FilterId FilterId,
- uint32_t Filter_track,
- uint8_t Size,
- uint8_t *pFilter,
- uint8_t *pMask,
- uint8_t *pMode,
- tTDAL_DMX_FilterCRCMode eCRCMode)
- {
- tTDAL_DMX_Error Error = kTDAL_DMX_NO_ERROR;
- uint8_t i;
- mTBOX_FCT_ENTER("TDAL_DMX_Set_Filter");
- TDAL_LockMutex(TDAL_DMX_SectionMutex);
- mTBOX_TRACE((kTBOX_NIV_1, "TDAL_DMX_Set_Filter: FilterId -->> %d\n", FilterId));
- if (!TDAL_DMX_Initialized)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL, "TDAL_DMX_Set_Filter: TDAL_DMX module has not been initialized\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_NOT_INITIALIZED;
- mTBOX_RETURN(Error);
- }
- if ((ChannelId < 0 || ChannelId >= kTDAL_DMX_MAX_NB_OF_CHANNELS) ||
- (TDAL_DMX_Channel[ChannelId].used == FALSE ) ||
- (TDAL_DMX_Channel[ChannelId].ChannelPid == kTDAL_DMX_ILLEGAL_PID))
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"TDAL_DMX_Allocate_Filter: ChannelId not available or not allocated\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_INVALID_CHANNEL_ID;
- mTBOX_RETURN(Error);
- }
- if (FilterId < 0 || FilterId >= kTDAL_DMX_MAX_NB_OF_FILTERS ||TDAL_DMX_Filter[FilterId].used == FALSE)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Set_Filter: Argument FilterId not available\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_INVALID_FILTER_ID;
- mTBOX_RETURN(Error);
- }
- if (Size > 8 || pFilter == (uint8_t*) NULL || pMask == (uint8_t*) NULL || pMode == (uint8_t*) NULL)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Set_Filter: bad argument size\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_BAD_PARAMETERS;
- mTBOX_RETURN(Error);
- }
- if(TDAL_DMX_Channel[TDAL_DMX_Filter[FilterId].AssociatedChannelId].ChannelStream == eTDAL_DMX_SECTION_STREAM)
- {
- for(i = 0; i < Size; i++)
- {
- TDAL_DMX_Filter[FilterId].Filter[i]= pFilter[i];
- TDAL_DMX_Filter[FilterId].Mask[i] = pMask[i];
- TDAL_DMX_Filter[FilterId].Mode[i] = pMode[i];
- }
- TDAL_DMX_Filter[FilterId].Size = Size;
- }
- TDAL_DMX_Filter[FilterId].efilterCRCMode = eCRCMode;
- TDAL_DMX_Filter[FilterId].tag = Filter_track;
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(Error);
- }
- /***********************************************************************
- * Function Name : TDAL_DMX_Control_Filter
- *
- * Description : This function is used to control a filter
- * (enabling, stopping, resetting)
- *
- * Side effects :
- *
- * Return : kTDAL_DMX_NO_ERROR if all is correct
- * kTDAL_DMX_ERROR_INVALID_FILTER_ID if no filter is invalid
- * kTDAL_DMX_ERROR_BAD_PARAMETERS if bad argument
- * kTDAL_DMX_ERROR_MUTEX for mutex issue
- * kTDAL_DMX_ERROR_CANT_CONTROL_FREE_FILTER if filter is not used
- * kTDAL_DMX_ERROR_UNABLE_TO_GRANT if not able to start or
- * to stop the filter
- *
- **********************************************************************/
- tTDAL_DMX_Error TDAL_DMX_Control_Filter(tTDAL_DMX_FilterId FilterId, tTDAL_DMX_Ctrl Ctrl)
- {
- tTDAL_DMX_Error Error = kTDAL_DMX_NO_ERROR;
- HRESULT hr = S_OK;
- MV_PE_SECTION_FILTER_PARAM Param;
- uint8_t i;
- tTDAL_DMX_ChannelId ChannelId;
- mTBOX_FCT_ENTER("TDAL_DMX_Control_Filter");
- TDAL_LockMutex(TDAL_DMX_SectionMutex);
- /* TO_DO */
- /* Perform error checking, (initialized?, FilterId out of bounds?, Filter is used?, Channel has illegal PID?) */
- /* moramo da koristimo funkcije
- nanastavnim materijalom,
- */
- if (!TDAL_DMX_Initialized)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL, "TDAL_DMX_Set_Filter: TDAL_DMX module has not been initialized\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_NOT_INITIALIZED;
- mTBOX_RETURN(Error);
- }
- if (FilterId < 0 || FilterId >= kTDAL_DMX_MAX_NB_OF_FILTERS ||TDAL_DMX_Filter[FilterId].used == FALSE)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Set_Filter: Argument FilterId not available\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_INVALID_FILTER_ID;
- mTBOX_RETURN(Error);
- }
- ChannelId = TDAL_DMX_Filter[FilterId].AssociatedChannelId;
- if ((ChannelId < 0 || ChannelId >= kTDAL_DMX_MAX_NB_OF_CHANNELS) ||
- (TDAL_DMX_Channel[ChannelId].used == FALSE ) ||
- (TDAL_DMX_Channel[ChannelId].ChannelPid == kTDAL_DMX_ILLEGAL_PID))
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"TDAL_DMX_Allocate_Filter: ChannelId not available or not allocated\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_INVALID_CHANNEL_ID;
- mTBOX_RETURN(Error);
- }
- /* TO_DO */
- /* Depending on the Ctrl parameter perform enabling or disabling of the filter given by FilterId */
- memset(&Param, 0, sizeof(Param));
- Param.Pid = TDAL_DMX_Channel[ChannelId].ChannelPid;
- Param.Mask[0] = TDAL_DMX_Filter[FilterId].Mask[0];
- Param.NotMask[0] = ~TDAL_DMX_Filter[FilterId].Mode[0];
- Param.Match[0] = TDAL_DMX_Filter[FilterId].Filter[0];
- // NENAD BI OVO URADIO PREKO MECPY BREEE
- // "Ja bih to uradio preko memcpy." - Nenad Jovanovic - 2014
- for( i = 1; i<TDAL_DMX_Filter[FilterId].Size; i++)
- {
- Param.Mask[i+2] = TDAL_DMX_Filter[FilterId].Mask[i];
- Param.Match[i+2] = TDAL_DMX_Filter[FilterId].Filter[i];
- Param.NotMask[i+2] = ~TDAL_DMX_Filter[FilterId].Mode[i];
- }
- // === //
- // \\
- // \\
- // // \\ //
- tTDAL_DMX_DemuxId DemuxId = TDAL_DMX_Channel[ChannelId].DemuxId ;
- UINT32 hSource;
- hSource = TDAL_DMX_Demux[DemuxId].hSource;
- switch(Ctrl)
- {
- case eTDAL_DMX_CTRL_ENABLE:
- hr = MV_PE_SourceAddSectionFilter(TDAL_DMX_hPE, hSource, &Param,
- TDAL_DMX_Filter[FilterId].hBuffer, 0, &TDAL_DMX_Filter[FilterId].hFilter);
- if (hr != S_OK)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Control_Filter: MV_PE_SourceAddSectionFilter FAILED [%d]\n", Ctrl));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- mTBOX_RETURN(Error);
- }
- TDAL_DMX_Filter[FilterId].enable = true;
- break;
- case eTDAL_DMX_CTRL_DISABLE:
- hr = MV_PE_SourceRemoveSectionFilter(TDAL_DMX_hPE, hSource, TDAL_DMX_Filter[FilterId].hFilter);
- if(hr != S_OK)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"MV_PE_SourceRemoveSectionFilter\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- mTBOX_RETURN(Error);
- }
- break;
- case eTDAL_DMX_CTRL_RESET:
- break;
- default:
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Control_Filter: Argument Ctrl is an unknown value [%d]\n", Ctrl));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_BAD_PARAMETERS;
- mTBOX_RETURN(Error);
- break;
- }
- /* Note */
- /* Carefully fill in Param structure needed for adding section filter */
- /* bytes with index 0 are copied to index 0 but bytes with nonzero index i are copied to index i+2 */
- // enbablovan filter
- // treba da iskoristimo da bi to staviti u filter hadnele
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(Error);
- }
- /***********************************************************************
- * Function Name : TDAL_DMX_Free_Filter
- *
- * Description : This function is used to free a filter from a channel
- *
- * Side effects :
- *
- * Return : kTDAL_DMX_NO_ERROR if all is correct
- * kTDAL_DMX_ERROR_INVALID_FILTER_ID if filter Id is invalid
- * kTDAL_DMX_ERROR_MUTEX for mutex issue
- * kTDAL_DMX_ERROR_CANT_CONTROL_FREE_FILTER if filter is not used
- * kTDAL_DMX_ERROR_UNABLE_TO_GRANT if not able to stop or to
- * deallocate the filter
- *
- **********************************************************************/
- tTDAL_DMX_Error TDAL_DMX_Free_Filter(tTDAL_DMX_FilterId FilterId)
- {
- tTDAL_DMX_Error Error = kTDAL_DMX_NO_ERROR;
- bool result = TRUE;
- mTBOX_FCT_ENTER("TDAL_DMX_Free_Filter");
- TDAL_LockMutex(TDAL_DMX_SectionMutex);
- mTBOX_TRACE((kTBOX_NIV_3, "TDAL_DMX_Free_Filter: FilterId -->> %d\n",FilterId));
- if (!TDAL_DMX_Initialized)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL, "TDAL_DMX_Free_Filter: TDAL_DMX module has not been initialized\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_NOT_INITIALIZED;
- mTBOX_RETURN(Error);
- }
- if (FilterId < 0 || FilterId >= kTDAL_DMX_MAX_NB_OF_FILTERS)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"TDAL_DMX_Free_Filter: FilterId not valid or filter not used\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_INVALID_FILTER_ID;
- mTBOX_RETURN(Error);
- }
- if (TDAL_DMX_Filter[FilterId].used == FALSE)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"TDAL_DMX_Free_Filter: Filter not used\t FilterId -> %d\n", FilterId));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- Error = kTDAL_DMX_ERROR_CANT_CONTROL_FREE_FILTER;
- mTBOX_RETURN(Error);
- }
- #if defined(TTXT_OSD_ON) && defined(TTXT_VBI_ON)
- if((TDAL_DMX_Channel[TDAL_DMX_Filter[FilterId].AssociatedChannelId].ChannelStream == eTDAL_DMX_TELETEXT_STREAM) &&
- (TDAL_DMX_Filter[FilterId].used))
- {
- mTBOX_RETURN(kTDAL_DMX_NO_ERROR);
- }
- #endif
- result = m_FreeFilterAndAssociatedRessources(FilterId);
- if(!result)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"TDAL_DMX_Free_Filter: Error in m_FreeFilterAndAssociatedRessources\n"));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(Error);
- }
- TDAL_DMX_Filter[FilterId].used = FALSE;
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(Error);
- }
- /***********************************************************************
- * Function Name : TDAL_DMX_Free_Buffer
- *
- * Description : This function is used to free a buffer
- *
- * Side effects :
- *
- * Return : kTDAL_DMX_NO_ERROR if all is correct
- * kTDAL_DMX_ERROR_BAD_PARAMETERS if buffer is null
- *
- **********************************************************************/
- tTDAL_DMX_Error TDAL_DMX_Free_Buffer(void * buffer)
- {
- tTDAL_DMX_Error Error = kTDAL_DMX_NO_ERROR;
- mTBOX_FCT_ENTER("TDAL_DMX_Free_Buffer");
- TDAL_LockMutex(TDAL_DMX_SectionMutex);
- if (!TDAL_DMX_Initialized)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL, "Free_Buffer: TDAL_DMX module has not been initialized\n"));
- Error = kTDAL_DMX_ERROR_NOT_INITIALIZED;
- }
- else
- {
- if (buffer == NULL)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"Free_Buffer: buffer null \n"));
- Error = kTDAL_DMX_ERROR_BAD_PARAMETERS;
- }
- else
- {
- TDAL_Free(buffer);
- }
- }
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(Error);
- }
- /***********************************************************************/
- /* Functions Definitions (MODULE) */
- /***********************************************************************/
- /***********************************************************************
- * Function Name : m_FreeFilterAndAssociatedRessources
- *
- * Description :
- *
- * Side effects :
- *
- * Comment :
- *
- **********************************************************************/
- bool m_FreeFilterAndAssociatedRessources(tTDAL_DMX_FilterId FilterId)
- {
- tTDAL_DMX_Error Error = kTDAL_DMX_NO_ERROR;
- HRESULT hr = S_OK;
- mTBOX_FCT_ENTER("m_FreeFilterAndAssociatedRessources");
- if(TDAL_DMX_Channel[TDAL_DMX_Filter[FilterId].AssociatedChannelId].ChannelStream == eTDAL_DMX_SECTION_STREAM)
- {
- TDAL_Free(TDAL_DMX_Filter[FilterId].Filter);
- TDAL_Free(TDAL_DMX_Filter[FilterId].Mask);
- TDAL_Free(TDAL_DMX_Filter[FilterId].Mode);
- hr = MV_PE_StreamBufRelease(TDAL_DMX_hPE, TDAL_DMX_Filter[FilterId].hBuffer);
- if(hr != S_OK)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"m_FreeFilterAndAssociatedRessources: Error in MV_PE_StreamBufRelease\n"));
- mTBOX_RETURN(FALSE);
- }
- }
- mTBOX_RETURN(TRUE);
- }
- /***********************************************************************
- * Function Name : m_sectionReceivedCallback
- *
- * Description :
- *
- * Side effects :
- *
- * Comment :
- *
- **********************************************************************/
- HRESULT m_sectionReceivedCallback(UINT32 EventCode, void *EventInfo, void *Context)
- {
- tTDAL_DMX_Error Error = kTDAL_DMX_NO_ERROR;
- HRESULT hr = S_OK;
- tTDAL_DMX_FilterId FilterId;
- UINT32 Fullness = 0, hBufOffset = 0;
- uint32_t checksum;
- MV_PE_SECTION_FILTER_PARAM Param;
- MV_PE_SECTION_INFO sectionInfo;
- uint8_t i;
- mTBOX_FCT_ENTER("m_sectionReceivedCallback");
- TDAL_LockMutex(TDAL_DMX_SectionMutex);
- sectionInfo = *(MV_PE_SECTION_INFO*) EventInfo;
- for(i = 0; i < kTDAL_DMX_MAX_NB_OF_FILTERS; i++)
- {
- if((sectionInfo.hFilter == (HANDLE)TDAL_DMX_Filter[i].hFilter) &&
- (TDAL_DMX_Filter[i].used == TRUE) &&
- (TDAL_DMX_Filter[i].enable == TRUE) &&
- (TDAL_DMX_Filter[i].waitingEnable == FALSE))
- {
- FilterId = i;
- break;
- }
- }
- if(i == kTDAL_DMX_MAX_NB_OF_FILTERS)
- {
- mTBOX_TRACE((kTBOX_NIV_3,"m_sectionReceivedCallback: Error in FilterID\n"));
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(E_FAIL);
- }
- if(Error == kTDAL_DMX_NO_ERROR)
- {
- hr = MV_PE_StreamBufGetFullness(TDAL_DMX_hPE, TDAL_DMX_Filter[FilterId].hBuffer, &Fullness);
- if(hr != S_OK)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"m_sectionReceivedCallback: Error in MV_PE_StreamBufGetFullness\n"));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- else
- {
- if(!Fullness)
- {
- mTBOX_TRACE((kTBOX_NIV_WARNING,"m_sectionReceivedCallback: Fullness is 0\n"));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- }
- }
- if(Error == kTDAL_DMX_NO_ERROR)
- {
- TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer = TDAL_Malloc(TDAL_DMX_Filter[FilterId].sectionBuffer.bufferSize);
- if(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer == NULL)
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"m_sectionReceivedCallback: Error in TDAL_Malloc\n"));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- else
- {
- memset(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer, 0, TDAL_DMX_Filter[FilterId].sectionBuffer.bufferSize);
- TDAL_DMX_Filter[FilterId].sectionBuffer.sectionSize = 0;
- }
- }
- if(Error == kTDAL_DMX_NO_ERROR)
- {
- hr = MV_PE_StreamBufRead(TDAL_DMX_hPE, TDAL_DMX_Filter[FilterId].hBuffer, TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer, kTDAL_DMX_SECTION_SIZE_INFO);
- if(hr != S_OK)
- {
- mTBOX_TRACE((kTBOX_NIV_WARNING,"m_sectionReceivedCallback: Error in MV_PE_StreamBufRead1\n"));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- else
- {
- hBufOffset += kTDAL_DMX_SECTION_SIZE_INFO;
- }
- }
- if(Error == kTDAL_DMX_NO_ERROR)
- {
- TDAL_DMX_Filter[FilterId].sectionBuffer.sectionSize = (uint16_t) (TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[2] |
- ((TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[1] & 0x0F) << 8));
- hr = MV_PE_StreamBufRead(TDAL_DMX_hPE,
- TDAL_DMX_Filter[FilterId].hBuffer,
- &TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[3],
- TDAL_DMX_Filter[FilterId].sectionBuffer.sectionSize);
- if(hr != S_OK)
- {
- mTBOX_TRACE((kTBOX_NIV_WARNING,"m_sectionReceivedCallback: Error in MV_PE_StreamBufRead2\n"));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- else
- {
- hBufOffset += TDAL_DMX_Filter[FilterId].sectionBuffer.sectionSize;
- }
- }
- if(Error == kTDAL_DMX_NO_ERROR)
- {
- if(TDAL_DMX_Filter[FilterId].efilterCRCMode == eTDAL_DMX_CRC_CHECK)
- {
- p_writeChecksum((uint8_t*) &checksum,
- TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer,
- TDAL_DMX_Filter[FilterId].sectionBuffer.sectionSize + kTDAL_DMX_SECTION_SIZE_INFO);
- if(checksum)
- {
- mTBOX_TRACE((kTBOX_NIV_3,"m_sectionReceivedCallback: CRC Error for table id -> 0x%x\n", TDAL_DMX_Filter[FilterId].Filter[0]));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- }
- }
- if(Error == kTDAL_DMX_NO_ERROR)
- {
- #ifdef PATCH_SECTION_PE
- if(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[0] == kTDAL_DMX_PAT_TABLE_ID)
- {
- if((patTsId != ((uint16_t)(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[3] << 8 | TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[4]))) ||
- (patCount == 0))
- {
- patTsId = (uint16_t)(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[3] << 8 | TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[4]);
- patCount = PAT_SECTIONS_TO_SKIP;
- }
- else
- {
- patCount --;
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- }
- else if(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[0] == kTDAL_DMX_SDT_TABLE_ID)
- {
- if((sdtTsId != ((uint16_t)(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[3] << 8 | TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[4]))) ||
- (sdtCount == 0))
- {
- sdtTsId = (uint16_t)(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[3] << 8 | TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer[4]);
- sdtCount = SDT_SECTIONS_TO_SKIP;
- }
- else
- {
- sdtCount --;
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- }
- #endif
- }
- if(Error == kTDAL_DMX_NO_ERROR)
- {
- if (TDAL_DMX_Channel[TDAL_DMX_Filter[FilterId].AssociatedChannelId].Callback != NULL)
- {
- TDAL_DMX_Channel[TDAL_DMX_Filter[FilterId].AssociatedChannelId].Callback(TDAL_DMX_Filter[FilterId].AssociatedChannelId,
- FilterId,
- TDAL_DMX_Filter[FilterId].tag,
- (TDAL_DMX_Filter[FilterId].sectionBuffer.sectionSize +
- kTDAL_DMX_SECTION_SIZE_INFO),
- TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer);
- TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer = NULL;
- }
- else
- {
- mTBOX_TRACE((kTBOX_NIV_CRITICAL,"m_sectionReceivedCallback: Channel callback is NULL\n"));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- }
- if(Error != kTDAL_DMX_NO_ERROR)
- {
- if(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer != NULL)
- {
- TDAL_Free(TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer);
- TDAL_DMX_Filter[FilterId].sectionBuffer.pBuffer = NULL;
- }
- if((Fullness - hBufOffset) > 0)
- {
- hr = MV_PE_StreamBufRead(TDAL_DMX_hPE,
- TDAL_DMX_Filter[FilterId].hBuffer,
- NULL,
- (Fullness - hBufOffset));
- if(hr != S_OK)
- {
- mTBOX_TRACE((kTBOX_NIV_WARNING,"m_sectionReceivedCallback: Error in MV_PE_StreamBufRead3\n"));
- Error = kTDAL_DMX_ERROR_UNABLE_TO_GRANT;
- }
- }
- }
- TDAL_UnlockMutex(TDAL_DMX_SectionMutex);
- mTBOX_RETURN(((Error == kTDAL_DMX_NO_ERROR) ? S_OK : E_FAIL));
- }
- /***********************************************************************/
- /* Functions Definitions (LOCAL) */
- /***********************************************************************/
- /***********************************************************************
- * Function Name : p_LookForFreeFilter
- *
- * Description : This function returns a free filter
- *
- * Side effects :
- *
- * Return : FilterId if found a free channel
- * kTDAL_DMX_ILLEGAL_FILTER otherwise
- *
- **********************************************************************/
- LOCAL tTDAL_DMX_FilterId p_LookForFreeFilter(void)
- {
- tTDAL_DMX_FilterId FilterId = kTDAL_DMX_ILLEGAL_FILTER;
- uint8_t i;
- mTBOX_FCT_ENTER("p_LookForFreeFilter");
- for (i = 0; i < kTDAL_DMX_MAX_NB_OF_FILTERS; i++)
- {
- if (TDAL_DMX_Filter[i].used == FALSE)
- {
- FilterId = i;
- break;
- }
- }
- mTBOX_RETURN(FilterId);
- }
- /***********************************************************************
- * Function Name : p_writeChecksum
- *
- * Description :
- *
- * Side effects :
- *
- * Comment :
- *
- **********************************************************************/
- LOCAL void p_writeChecksum(uint8_t * const checksum, uint8_t const * block, size_t const length)
- {
- static int32_t crc32_table[ CRC32_MAX_COEFFICIENTS ] = {1};
- mTBOX_FCT_ENTER("p_writeChecksum");
- if (crc32_table[0])
- {
- int loopCntCoef, loopCntBit;
- for (loopCntCoef = 0; loopCntCoef < CRC32_MAX_COEFFICIENTS; loopCntCoef++)
- {
- uint32_t coef32 = (uint32_t) loopCntCoef << 24;
- for (loopCntBit = 0; loopCntBit < 8; loopCntBit++)
- if (coef32 & 0x80000000)
- coef32 = ((coef32 << 1) ^ CRC32_POLYNOMIAL);
- else
- coef32 <<= 1;
- crc32_table[loopCntCoef] = coef32;
- }
- }
- uint8_t const * const end = block + length;
- uint32_t crc32 = 0xFFFFFFFF;
- for (; block < end; block++)
- crc32 = (crc32 << 8) ^ crc32_table[ ((crc32 >> 24) ^ *block) & 0x000000FF ];
- checksum[0] = (uint8_t) (crc32 >> 24);
- checksum[1] = (uint8_t) (crc32 >> 16);
- checksum[2] = (uint8_t) (crc32 >> 8);
- checksum[3] = (uint8_t) (crc32);
- mTBOX_RETURN;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement