⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pindevice.cpp

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 CPP
📖 第 1 页 / 共 5 页
字号:

    switch(ulReqFlags)
    {
        case CSPROPERTY_TYPE_GET:
            if( OutBufLen < sizeof ( CSSTATE ) )
            {
                dwError = ERROR_MORE_DATA;
                break;
            }

            __try
            {
                memcpy( pOutBuf, &m_CsState, sizeof ( CSSTATE ) );
                *pdwBytesTransferred = sizeof(CSSTATE);
            }
            __except( EXCEPTION_EXECUTE_HANDLER )        
            {
                dwError = ERROR_MORE_DATA;
                break;
            }

            dwError = ERROR_SUCCESS;
            break;

        case CSPROPERTY_TYPE_SET:

            CSSTATE csState;
            if( OutBufLen < sizeof( CSSTATE ))
            {
                dwError = ERROR_MORE_DATA;
                break;
            }

            if( !BSPCameraMemCpy( &csState, pOutBuf, sizeof( CSSTATE )))
            {
                dwError = ERROR_MORE_DATA;
                break;
            }

            // Call SetState to change state.
            dwError = SetState(csState, NULL);

            break;

        default:
            DEBUGMSG(ZONE_IOCTL|ZONE_ERROR, (_T("PIN_IOControl(%08x): Invalid Request\r\n"), this));

            break;
    }

    return dwError;
}


//------------------------------------------------------------------------------
//
//  FUNCTION:        PinHandleConnDataFormatSubReqs
//
//  DESCRIPTION:     get or set the current data format for the pin
//
//  PARAMETERS:      ULONG                          ulReqFlags,
//                   PCS_DATAFORMAT_VIDEOINFOHEADER pCsDataFormatVidInfoHdr,
//                   PDWORD                         pdwBytesTransferred
//
//  RETURNS:         Returns ERROR_SUCCESS if the operation completes successfully;
//                   otherwise, returns a Microsoft Win32 error code.
//
//------------------------------------------------------------------------------
DWORD CPinDevice::PinHandleConnDataFormatSubReqs(
    ULONG                          ulReqFlags,
    PCS_DATAFORMAT_VIDEOINFOHEADER pCsDataFormatVidInfoHdr,  // Warning: this buffer is unsafe, use with caution
    PDWORD                         pdwBytesTransferred
    )
{
    DWORD dwError = ERROR_INVALID_PARAMETER ;
    PCS_DATARANGE_VIDEO pCsDataRangeVideoMatched = NULL;
//    ULONG ulNewFrameSize;

    // We must have called IOCTL_STREAM_INSTANTIATE before setting format
    if ( -1 == m_ulPinId )
    {
        return dwError;
    }
    
    // The incoming video info header is unsafe. The data might change on a separate thread
    // while it's being accessed. For security purposes, let's make a copy of the data
    // before any attempt to access them is done, and then work off the copy

    switch( ulReqFlags )
    {
        case CSPROPERTY_TYPE_SET:
            if (m_CsState != CSSTATE_STOP)
            {
                DEBUGMSG(ZONE_IOCTL|ZONE_ERROR, (_T("PIN_IOControl(%08x): Can not set data format when running. Stop pin first.\r\n"), this));
                return dwError;
            }

            if (m_ulPinId == STILL) //It need not configure STILL pin's data format
            {
                DEBUGMSG(ZONE_IOCTL|ZONE_ERROR, (_T("PIN_IOControl(%08x): Can not change STILL Pin's dataformat.\r\n"), this));
                return dwError;
            }
            
            if ( TRUE == m_pCamAdapter->AdapterCompareFormat(m_ulPinId, pCsDataFormatVidInfoHdr, &pCsDataRangeVideoMatched, TRUE))
            {
                // We found our format
                memcpy(&m_CsDataRangeVideo, pCsDataRangeVideoMatched, sizeof(CS_DATARANGE_VIDEO));
                memcpy(&m_CsDataRangeVideo, &pCsDataFormatVidInfoHdr->DataFormat, sizeof(CSDATARANGE));
                memcpy(&m_CsDataRangeVideo.VideoInfoHeader, &pCsDataFormatVidInfoHdr->VideoInfoHeader, sizeof(CS_VIDEOINFOHEADER));

                m_RtAveTimePerFrame = m_CsDataRangeVideo.VideoInfoHeader.AvgTimePerFrame;
                *pdwBytesTransferred = sizeof ( CS_DATARANGE_VIDEO );

                // Assure that we will reconfigure IPU before starting.
                m_pCamAdapter->CameraMarkAsModified(m_ulPinId);

                DEBUGMSG(ZONE_IOCTL|ZONE_ERROR, (_T("PIN_IOControl(%08x): Modified CSDataRangeVideo for Pin %x.  New width: %x, New height: %x\r\n"), 
                    this, m_ulPinId, m_CsDataRangeVideo.VideoInfoHeader.bmiHeader.biWidth, m_CsDataRangeVideo.VideoInfoHeader.bmiHeader.biHeight));
                if (m_pCamAdapter->m_StillPinInherited == m_ulPinId)
                {
                    memcpy(&m_pCamAdapter->m_StrmInstances[STILL].pPinDev->m_CsDataRangeVideo, &m_CsDataRangeVideo, sizeof(CS_DATARANGE_VIDEO));
                    memcpy(&m_pCamAdapter->m_StrmInstances[STILL].pPinDev->m_CsDataRangeVideo.DataRange, &m_CsDataRangeVideo.DataRange, sizeof(CSDATARANGE));
                    memcpy(&m_pCamAdapter->m_StrmInstances[STILL].pPinDev->m_CsDataRangeVideo.VideoInfoHeader, &m_CsDataRangeVideo.VideoInfoHeader, sizeof(CS_VIDEOINFOHEADER));

                    m_pCamAdapter->m_StrmInstances[STILL].pPinDev->m_RtAveTimePerFrame = m_RtAveTimePerFrame;
                }

                dwError = ERROR_SUCCESS;
            }
            else
            {
                DEBUGMSG(ZONE_IOCTL|ZONE_ERROR, (_T("PIN_IOControl(%08x): Failed Adapter format compare.  Did not change settings.\r\n"), this));
            }

            break;
            
        case CSPROPERTY_TYPE_GET:
            memcpy(&pCsDataFormatVidInfoHdr->DataFormat, &m_CsDataRangeVideo.DataRange, sizeof(CSDATARANGE));
            memcpy(&pCsDataFormatVidInfoHdr->VideoInfoHeader, &m_CsDataRangeVideo.VideoInfoHeader, sizeof(CS_VIDEOINFOHEADER));
            *pdwBytesTransferred = sizeof (CS_DATARANGE_VIDEO);
            dwError = ERROR_SUCCESS;
            
            break;
            
        default:
            DEBUGMSG(ZONE_IOCTL|ZONE_ERROR, (_T("PIN_IOControl(%08x): Invalid Request\r\n"), this));
    }

    return dwError;
}


//------------------------------------------------------------------------------
//
//  FUNCTION:        AllocateBuffer
//
//  DESCRIPTION:     Allocate stream buffers. 
//
//  PARAMETERS:      LPVOID pOutBuf
//
//  RETURNS:         Returns ERROR_SUCCESS if the operation completes successfully;
//                   otherwise, returns a Microsoft Win32 error code.
//
//------------------------------------------------------------------------------
DWORD 
CPinDevice::AllocateBuffer( PCS_STREAM_DESCRIPTOR pCsDescriptor, LPVOID pOutBuf, DWORD  OutBufLen, DWORD *pdwBytesTransferred )
{
    DWORD                   dwError = ERROR_INVALID_PARAMETER;
    DWORD                   dwResult = -1;
    PCS_STREAM_DESCRIPTOR   pCsDescriptorOut = (PCS_STREAM_DESCRIPTOR) pOutBuf;
    MarshalledBuffer_t  MarshalledStreamDesc_pOutBuf(pCsDescriptorOut, OutBufLen, ARG_O_PTR, FALSE, TRUE);
    pCsDescriptorOut = reinterpret_cast<PCS_STREAM_DESCRIPTOR>( MarshalledStreamDesc_pOutBuf.ptr() );

    if( (OutBufLen > 0) && (NULL == pCsDescriptorOut) )
    {
        return dwError;
    }

    MarshalledBuffer_t MarshalledStreamDesc(pCsDescriptor, sizeof(CS_STREAM_DESCRIPTOR), ARG_O_PTR, FALSE, TRUE);
    pCsDescriptor = reinterpret_cast<PCS_STREAM_DESCRIPTOR>( MarshalledStreamDesc.ptr() );

    if( NULL == pCsDescriptor )
    {
        return dwError;
    }

    // There are 2 cases here: the buffer comes from the hardware or from the software. 
    // If the buffer comes from the software, we generate a new entry in the table up to the maximum allowed.
    // If the buffer comes from the hardware, we setup the application stream descriptor

    EnterCriticalSection( &m_csStreamBuffer );

    //Check the BufferCount after the critical section is entered.
    //This prevents synch issues with validating the BufferCount
    if( m_dwBufferCount >= m_ulMaxNumOfBuffers )
    {
        dwError = ERROR_INVALID_PARAMETER;
        goto Cleanup;
    }

    if( GetCurrentMemoryModel() == CSPROPERTY_BUFFER_DRIVER )
    {
        if( NULL == pOutBuf || OutBufLen < sizeof(CS_STREAM_DESCRIPTOR) )
        {
            goto Cleanup;
        }
        // pOutBuf has already been validated through MapCallerPtr in the IOCTL function
        if( pCsDescriptorOut == NULL )
        {
            goto Cleanup;
        }

        // Get one of the hardware buffers, and setup the descriptor
        ASSERT( m_pStreamDescriptorList[ m_dwBufferCount ].m_fBusy == FALSE );

        dwError = HwSetupStreamDescriptor( m_dwBufferCount );
        if( dwError != ERROR_SUCCESS )
        {
            goto Cleanup;
        }

        if( !BSPCameraMemCpy( pCsDescriptorOut, &(m_pStreamDescriptorList[ m_dwBufferCount ].csStreamDescriptorShadow), sizeof( CS_STREAM_DESCRIPTOR )))
        {
            dwError = ERROR_INVALID_PARAMETER;
            goto Cleanup;
        }

        m_pStreamDescriptorList[ m_dwBufferCount ].pCsStreamDescriptorExternal = NULL;
        m_pStreamDescriptorList[ m_dwBufferCount ].m_fBusy = TRUE;
        m_dwBufferCount++;

        if( pdwBytesTransferred )
        {
            *pdwBytesTransferred = sizeof(CS_STREAM_DESCRIPTOR);
        }
    }
    else if( GetCurrentMemoryModel() == CSPROPERTY_BUFFER_CLIENT_LIMITED )
    {
        // The software is allocated by the software, let's copy the descriptor and generate a handle
        ASSERT( m_pStreamDescriptorList[ m_dwBufferCount ].m_fBusy == FALSE );

        if( !BSPCameraMemCpy( &( m_pStreamDescriptorList[ m_dwBufferCount ].csStreamDescriptorShadow ), pCsDescriptor, sizeof( CS_STREAM_DESCRIPTOR )))
        {
            dwError = ERROR_INVALID_PARAMETER;
            goto Cleanup;
        }

        // Let's populate the handle and the buffer field.
        dwError = SwSetupStreamDescriptor( m_dwBufferCount, &( m_pStreamDescriptorList[ m_dwBufferCount ].csStreamDescriptorShadow ), pCsDescriptor );
        if( dwError != ERROR_SUCCESS )
        {
            goto Cleanup;
        }

        m_pStreamDescriptorList[ m_dwBufferCount ].pCsStreamDescriptorExternal = NULL;
        m_pStreamDescriptorList[ m_dwBufferCount ].m_fBusy = TRUE;
        m_dwBufferCount++;
    }
    else if( GetCurrentMemoryModel() == CSPROPERTY_BUFFER_CLIENT_UNLIMITED )
    {
        // let's find a slot available
        DWORD dwAvailableRow = -1;
        for( DWORD i = 0; ( i < m_ulMaxNumOfBuffers ) && ( dwAvailableRow == -1 ); i++ )
        {
            if( m_pStreamDescriptorList[ i ].m_fBusy == FALSE )
            {
                dwAvailableRow = i;
            }
        }
        if( dwAvailableRow == -1 )
        {
            goto Cleanup;
        }

        if( !BSPCameraMemCpy( &( m_pStreamDescriptorList[ dwAvailableRow ].csStreamDescriptorShadow ), pCsDescriptor, sizeof( CS_STREAM_DESCRIPTOR )))
        {
            dwError = ERROR_INVALID_PARAMETER;
            goto Cleanup;
        }

        dwError = SwSetupStreamDescriptor( dwAvailableRow, &( m_pStreamDescriptorList[ dwAvailableRow ].csStreamDescriptorShadow ), pCsDescriptor );
        if( dwError != ERROR_SUCCESS )
        {
            goto Cleanup;
        }

        m_pStreamDescriptorList[ dwAvailableRow ].pCsStreamDescriptorExternal = NULL;
        m_pStreamDescriptorList[ dwAvailableRow ].m_fBusy = TRUE;
        m_dwBufferCount++;
    }

Cleanup:
    LeaveCriticalSection( &m_csStreamBuffer );
    return dwError;
}


//------------------------------------------------------------------------------
//
//  FUNCTION:        DeallocateBuffer
//
//  DESCRIPTION:     Free the memory for the buffer
//
//  PARAMETERS:      LPVOID pOutBuf
//
//  RETURNS:         Returns ERROR_SUCCESS if the operation completes successfully;
//                   otherwise, returns a Microsoft Win32 error code.
//
//------------------------------------------------------------------------------
DWORD
CPinDevice::DeallocateBuffer( PCS_STREAM_DESCRIPTOR pCsDescriptor )
{
    LPVOID  lpBuffer;
    DWORD   dwHandle;
    LONG    lIndex;
    DWORD   dwError = ERROR_SUCCESS;    

    MarshalledBuffer_t MarshalledStreamDesc(pCsDescriptor, sizeof(CS_STREAM_DESCRIPTOR), ARG_O_PTR, FALSE, TRUE);
    pCsDescriptor = reinterpret_cast<PCS_STREAM_DESCRIPTOR>( MarshalledStreamDesc.ptr() );

    if( NULL == pCsDescriptor )
    {
        return dwError;
    }

    lpBuffer = pCsDescriptor->CsStreamHeader.Data;
    dwHandle = pCsDescriptor->CsStreamHeader.Handle;

    // Get the entry for this buffer in the internal list
    EnterCriticalSection( &m_csStreamBuffer );

    //Check if there are any buffers to deallocate    
    if(0 == m_dwBufferCount)
    {
        dwError = ERROR_INVALID_PARAMETER;
        goto Cleanup;
    }

    lIndex = GetIndexFromHandle( dwHandle, lpBuffer );
    if( lIndex == -1 )
    {
        dwError = ERROR_INVALID_PARAMETER;
        goto Cleanup;
    }

    // If the row is not in use, let's make it available
    if( m_pStreamDescriptorList[ lIndex ].pCsStreamDescriptorExternal != NULL )
    {
        dwError = ERROR_INVALID_PARAMETER;
        goto Cleanup;
    }

    ASSERT( m_pStreamDescriptorList[ lIndex ].m_fBusy == TRUE );
    m_pStreamDescriptorList[ lIndex ].m_fBusy = FALSE;
    m_dwBufferCount--;

    if( GetCurrentMemoryModel() == CSPROPERTY_BUFFER_DRIVER )
    {
        // We release the buffer. 
        //m_pCamAdapter->CameraRemoteLocalFree( m_pStreamDescriptorList[ lInd

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -