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

📄 bcamadapter.cpp

📁 BCAM 1394 Driver
💻 CPP
📖 第 1 页 / 共 4 页
字号:
  static HANDLE OpenDevice( LPCTSTR szDeviceName, BOOL bOverlapped );
private:
  static DWORD AddVirtualDevice( PVIRT_DEVICE pVirtualDevice, ULONG BusNumber );
  static void CreateVirtualDevices();
  static DWORD RemoveVirtualDevice( PVIRT_DEVICE    pVirtualDevice, ULONG BusNumber );
  static void DeleteVirtualDevices();
public:
  /// Default constructor, creates the virtual devices
  CBcamDevCreator() { 
    CreateVirtualDevices(); 
  }
/*
  /// Destructor, deletes the virtual devices.
  ~CBcamDevCreator() { 
    DeleteVirtualDevices(); 
  }
*/
};
/// The one and only creator.
static CBcamDevCreator _Creator;


#define BUS_NAME  "\\\\.\\1394BUS"
#define DEVICE_ID "1394_BASLER_VIRTUAL_DEVICE"


//------------------------------------------------------------------------------
// HANDLE CBcamDevCreator::OpenDevice( PCSTR szDeviceName, BOOL bOverLapped )
// Author: HNebelun
//------------------------------------------------------------------------------
/**
 * \brief Open a named device.
 *
 * If the handle is opened in overlapped mode (asynchronous operation) device I/O 
 * control calls must provide an initialized OVERLAPPED structure as parameter.
 *
 * \param     szDeviceName  Pointer to the device name
 * \param     bOverLapped   Flag indicating synchronous or asynchronous operation.
 * \return    
 *
 * The handle of the opened file is returned. In case of failure the return value
 * is INVALID_HANDLE_VALUE.
 * 
 */
//------------------------------------------------------------------------------
HANDLE CBcamDevCreator::OpenDevice(
    LPCTSTR   szDeviceName,
    BOOL    bOverLapped
    )
{
    HANDLE  hDevice;

    if (bOverLapped) 
    {
        hDevice = CreateFile( szDeviceName,
                              GENERIC_WRITE | GENERIC_READ,
                              FILE_SHARE_WRITE | FILE_SHARE_READ,
                              NULL,
                              OPEN_EXISTING,
                              FILE_FLAG_OVERLAPPED,
                              NULL
                              );
    }
    else 
    {
        hDevice = CreateFile( szDeviceName,
                              GENERIC_WRITE | GENERIC_READ,
                              FILE_SHARE_WRITE | FILE_SHARE_READ,
                              NULL,
                              OPEN_EXISTING,
                              0,
                              NULL
                              );
    }


    return hDevice;

}

//------------------------------------------------------------------------------
// DWORD CBcamDevCreator::AddVirtualDevice( PVIRT_DEVICE pVirtualDevice, ULONG BusNumber )
// Author: HNebelun
//------------------------------------------------------------------------------
/**
 * \brief Attach a virtual device to a bus.
 *
 * A virtual device is attached using a IEEE1394 API request.
 *
 * \param     pVirtualDevice Pointer to the virtual device information
 * \param     BusNumber Id of the 1394 bus object
 * \return    
 *
 * Returns an success or error code.
 * \retval ERROR_SUCCESS on success
 * \retval ERROR_INVALID_HANDLE  in case of an invalid handle value
 * 
 */
//------------------------------------------------------------------------------
DWORD CBcamDevCreator::AddVirtualDevice( PVIRT_DEVICE pVirtualDevice, ULONG BusNumber )
{
    HANDLE                      hDevice;
    ULONG                       ulStrLen;
    TCHAR                       BusName[16] = _T( BUS_NAME );
    PIEEE1394_API_REQUEST       p1394ApiReq;
    PIEEE1394_VDEV_PNP_REQUEST  pDevPnpReq;
    DWORD                       dwBytesRet;
    DWORD                       dwRet;


    _itoa(BusNumber, (PCHAR)BusName+11, 10);
    hDevice = OpenDevice(BusName, FALSE);

    if (hDevice != INVALID_HANDLE_VALUE) 
    {
    
        ulStrLen = strlen(pVirtualDevice->DeviceID);
        
        p1394ApiReq = (PIEEE1394_API_REQUEST) LocalAlloc(LPTR, sizeof(IEEE1394_API_REQUEST)+ulStrLen);

        p1394ApiReq->RequestNumber  = IEEE1394_API_ADD_VIRTUAL_DEVICE;
        p1394ApiReq->Flags          = pVirtualDevice->fulFlags;

        pDevPnpReq = &p1394ApiReq->u.AddVirtualDevice;

        pDevPnpReq->fulFlags    = pVirtualDevice->fulFlags;
        pDevPnpReq->Reserved    = 0;
        pDevPnpReq->InstanceId  = pVirtualDevice->InstanceID;
        strncpy( (PCHAR)&pDevPnpReq->DeviceId, pVirtualDevice->DeviceID, ulStrLen);


        dwRet = DeviceIoControl( hDevice,
                                 IOCTL_IEEE1394_API_REQUEST,
                                 p1394ApiReq,
                                 sizeof(IEEE1394_API_REQUEST)+ulStrLen,
                                 NULL,
                                 0,
                                 &dwBytesRet,
                                 NULL
                                 );

        if (!dwRet) 
        {

            dwRet = GetLastError();
        }
        else 
        {

            dwRet = ERROR_SUCCESS;
        }

        // free resources

        CloseHandle (hDevice);
        if (p1394ApiReq)
            LocalFree(p1394ApiReq);
    }
    else 
    {
        dwRet = ERROR_INVALID_HANDLE;
    }   
    
    return dwRet;
}

//------------------------------------------------------------------------------
// void CBcamDevCreator::CreateVirtualDevices()
// Author: HNebelun
//------------------------------------------------------------------------------
/**
 * \brief Create a virtual device for each 1394 bus object.
 */
//------------------------------------------------------------------------------
void CBcamDevCreator::CreateVirtualDevices()
{
  VIRT_DEVICE     virtDevice;
  CHAR            DeviceID[32] = DEVICE_ID;
  ULONG           busNumber = 0;
  DWORD           dwRet;
  static const int MAX_RETRIES = 10;
  static const int DELAY = 500;
  
  TRACE( _T( "CreateVirtualDevices()\n" ) );
  virtDevice.fulFlags = 0;
  virtDevice.InstanceID.HighPart = 0;
  virtDevice.InstanceID.LowPart = 0;
  virtDevice.DeviceID = DeviceID;
  
  unsigned int numberOfDevices = 0;
  do 
  {
    dwRet = AddVirtualDevice ( &virtDevice, busNumber);
    
    if (dwRet == ERROR_SUCCESS) 
    {
      TRACE( _T( "Virtual Driver successfully added on 1394Bus%i\n" ), busNumber );
      numberOfDevices ++;
    }
    
    virtDevice.InstanceID.LowPart++;
    busNumber++;
    
  } while (dwRet == ERROR_SUCCESS);

  if ( numberOfDevices > 0 )
  {
    int tries = 0;
    while ( tries++ < MAX_RETRIES )
    {
      if ( CBcamAdapter::DeviceNames().size() >= numberOfDevices )
        break;
      Sleep(DELAY);
    }
  }
  
  return;
  
}

//------------------------------------------------------------------------------
// DWORDCBcamDevCreator::RemoveVirtualDevice( PVIRT_DEVICE pVirtualDevice, ULONG BusNumber )
// Author: HNebelun
//------------------------------------------------------------------------------
/**
 * \brief Remove a virtual device
 *
 * A virtual device is removed using a IEEE1394 API request.
 *
 * \param     pVirtualDevice  Pointer to the virtual device information
 * \param     BusNumber Id of the 1394 bus object
 * \return    
 *
 * Returns an success or error code.
 * \retval ERROR_SUCCESS on success
 * \retval ERROR_INVALID_HANDLE  in case of an invalid handle value
 */
//------------------------------------------------------------------------------
DWORD
CBcamDevCreator::RemoveVirtualDevice(
    PVIRT_DEVICE    pVirtualDevice,
    ULONG           BusNumber
    )
{
    HANDLE                      hDevice;
    ULONG                       ulStrLen;
    TCHAR                       BusName[16] = _T( BUS_NAME );
    PIEEE1394_API_REQUEST       p1394ApiReq;
    PIEEE1394_VDEV_PNP_REQUEST  pDevPnpReq;
    DWORD                       dwBytesRet;
    DWORD                       dwRet;

    if (!pVirtualDevice->DeviceID)
    {
        dwRet = ERROR_INVALID_PARAMETER;
        goto Exit_RemoveVirtualDriver;
    }

    _itoa(BusNumber, (PCHAR)BusName+11, 10);
    hDevice = OpenDevice(BusName, FALSE);

    if (hDevice != INVALID_HANDLE_VALUE) 
    {
        ulStrLen = strlen(pVirtualDevice->DeviceID);
        
        p1394ApiReq = (PIEEE1394_API_REQUEST) LocalAlloc(LPTR, sizeof(IEEE1394_API_REQUEST)+ulStrLen);

        p1394ApiReq->RequestNumber = IEEE1394_API_REMOVE_VIRTUAL_DEVICE;
        p1394ApiReq->Flags = pVirtualDevice->fulFlags;

        pDevPnpReq = &p1394ApiReq->u.RemoveVirtualDevice;

        pDevPnpReq->fulFlags = 0;
        pDevPnpReq->Reserved = 0;
        pDevPnpReq->InstanceId = pVirtualDevice->InstanceID;
        strncpy( (PCHAR)&pDevPnpReq->DeviceId, pVirtualDevice->DeviceID, ulStrLen);


        dwRet = DeviceIoControl( hDevice,
                                 IOCTL_IEEE1394_API_REQUEST,
                                 p1394ApiReq,
                                 sizeof(IEEE1394_API_REQUEST)+ulStrLen,
                                 NULL,
                                 0,
                                 &dwBytesRet,
                                 NULL
                                 );

        if (!dwRet) 
        {
            dwRet = GetLastError();
        }
        else 
        {
            dwRet = ERROR_SUCCESS;
        }

        // free resources

        CloseHandle (hDevice);
        if (p1394ApiReq)
            LocalFree(p1394ApiReq);
    }
    else 
    {
        dwRet = ERROR_INVALID_HANDLE;
    }   
    
Exit_RemoveVirtualDriver:

    return dwRet;
}

//------------------------------------------------------------------------------
// void CBcamDevCreator::DeleteVirtualDevices()
// Author: HNebelun
//------------------------------------------------------------------------------
/**
 * \brief Delete all virtual devices created previously
 *
 */
//------------------------------------------------------------------------------
void CBcamDevCreator::DeleteVirtualDevices()
{
  VIRT_DEVICE     virtDevice;
  CHAR            DeviceID[32] = DEVICE_ID;
  ULONG           busNumber = 0;
  DWORD           dwRet;
  
  TRACE( _T( "DeleteVirtualDevices()\n" ) );
  
  virtDevice.fulFlags = 0;
  virtDevice.InstanceID.HighPart = 0;
  virtDevice.InstanceID.LowPart = 0;
  virtDevice.DeviceID = DeviceID;
  
  do {
    dwRet = RemoveVirtualDevice ( &virtDevice, busNumber );
    
    if (dwRet == ERROR_SUCCESS) 
    {
      TRACE( _T( "Virtual Driver successfully removed on 1394Bus%i\n" ), busNumber );
    }
    
    virtDevice.InstanceID.LowPart++;
    busNumber++;
    
  } while (dwRet == ERROR_SUCCESS);
  
  return;
  
}

// !! already defined in bcam.cpp

//------------------------------------------------------------------------------
// class CBcamDevInfo 
// Author: 
//------------------------------------------------------------------------------
/**
* \brief Helps with the list of devices.
*
*/
//------------------------------------------------------------------------------
class CBcamDevInfo 
{
  HDEVINFO m_hDevInfo; ///< Handle to the device information
public:
  
  //------------------------------------------------------------------------------
  // CBcamDevInfo( GUID& classguid )
  // Author: 
  //------------------------------------------------------------------------------
  /**
  * \brief Create a device list.
  *
  * \param     classguid Identifies the device interface
  * \throw BcamException The value of \c ::GetLastError() is thrown.
  */
  //------------------------------------------------------------------------------
  CBcamDevInfo( const GUID& classguid )
    : m_hDevInfo( ::SetupDiGetClassDevs( 
#if _SETUPAPI_VER >= 0x0500
   &classguid, 
#else
    const_cast<GUID*>( &classguid ),
#endif
    NULL, 
    NULL, 
    DIGCF_DEVICEINTERFACE | DIGCF_PRESENT) )
    
  {
    if (m_hDevInfo == INVALID_HANDLE_VALUE)
      throw BcamException( ::GetLastError(), _T( "CBcamDevInfo" ) );
  }
  //------------------------------------------------------------------------------
  // ~CBcamDevInfo()
  // Author: 
  //------------------------------------------------------------------------------
  /**
  * \brief Destroy the device list
  */
  //------------------------------------------------------------------------------
  ~CBcamDevInfo()
  {
    if (m_hDevInfo != INVALID_HANDLE_VALUE) 
    {
      SetupDiDestroyDeviceInfoList( m_hDevInfo );
      m_hDevInfo = INVALID_HANDLE_VALUE;
    }
  }
  /// Return the handle to the device list.
  operator HDEVINFO() { return m_hDevInfo; }
};



HWND CBcamAdapter::s_hWndInterfaceNotify; 
HDEVNOTIFY CBcamAdapter::s_hDevInterfaceNotify; 


//------------------------------------------------------------------------------
// CBcamAdapter::CBcamAdapter(): m_hDevice( INVALID_HANDLE_VALUE ), m_pResetContext( NULL )
// Author: HNebelun
//------------------------------------------------------------------------------
/**
 * \brief Default constructor
 */
//------------------------------------------------------------------------------
CBcamAdapter::CBcamAdapter()
: m_hDevice( INVALID_HANDLE_VALUE ), m_pResetContext( NULL )
{

}

//------------------------------------------------------------------------------
// CBcamAdapter::~CBcamAdapter()
// Author: HNebelun
//------------------------------------------------------------------------------
/**
 * \brief Destructor
 *
 */
//------------------------------------------------------------------------------
CBcamAdapter::~CBcamAdapter()
{
  ClearBusResetContext();
 // UnregisterClient();

  if (INVALID_HANDLE_VALUE != m_hDevice)
  {
    CloseHandle( m_hDevice );
    m_hDevice = INVALID_HANDLE_VALUE;
  }

}

//------------------------------------------------------------------------------
/**
* \brief Registers a client window to recieve notifications
*
* \param     hwnd Handle to the client window
* 
* \throw BcamException The value of \c ::GetLastError() is thrown.
*/
//------------------------------------------------------------------------------
/*
void CBcamAdapter::RegisterClient(HWND hwnd)
{
  if (s_hDevInterfaceNotify != INVALID_HANDLE_VALUE)
    UnregisterClient();

  DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
  ZeroMemory( &NotificationFilter, sizeof NotificationFilter );
  NotificationFilter.dbcc_size        = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  NotificationFilter.dbcc_devicetype  = DBT_DEVTYP_DEVICEINTERFACE;
  NotificationFilter.dbcc_classguid   = GUID_BACC_DEVICE;

  s_hWndInterfaceNotify = hwnd;
  s_hDevInterfaceNotify = RegisterDeviceNotification( hwnd,
    &NotificationFilter,
    DEVICE_NOTIFY_WINDOW_HANDLE

⌨️ 快捷键说明

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