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

📄 usbio.cpp

📁 ti-Chipcon CC251x 2.4G Soc应用开发源码实例。包括rf,powermodes,clockmodes,flashRW,interrupts,timer,pwm,uart...所有
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/************************************************************************
 *
 *  Module:       UsbIo.cpp
 *  Long name:    CUsbIo class
 *  Description:  CUsbIo base device class implementation
 *
 *  Runtime Env.: Win32, Part of UsbioLib
 *  Author(s):    Guenter Hildebrandt, Udo Eberhardt
 *  Company:      Thesycon GmbH, Ilmenau
 ************************************************************************/

// for shorter and faster windows.h
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
// unicode is not supported by USBIOLIB
#ifdef UNICODE
#undef UNICODE
#endif

#include <windows.h>
#include <stdio.h>
#include "usbio.h"


// static members
CSetupApiDll CUsbIo::smSetupApi;



// standard constructor
CUsbIo::CUsbIo()
{
  FileHandle = NULL;
  ZeroMemory(&Overlapped,sizeof(Overlapped));
  InitializeCriticalSection(&CritSect);
  CheckedBuildDetected = FALSE;
  DemoVersionDetected = FALSE;
  LightVersionDetected = FALSE;
  mDevDetail = NULL;
}


// destructor
CUsbIo::~CUsbIo()
{
  // close file handle
  Close();
  // free resources
  DeleteCriticalSection(&CritSect);

}


HDEVINFO 
CUsbIo::CreateDeviceList(const GUID *InterfaceGuid)
{
  HDEVINFO h;

  // make sure the setupapi dll is loaded
  if ( !smSetupApi.Load() ) {
    return NULL;
  }

  h = (smSetupApi.SetupDiGetClassDevs)(
        (GUID*)InterfaceGuid,                 // LPGUID ClassGuid, 
        NULL,                                 // PCTSTR Enumerator, 
        NULL,                                 // HWND hwndParent, 
        DIGCF_DEVICEINTERFACE | DIGCF_PRESENT // DWORD Flags
        );
  return ( (h==INVALID_HANDLE_VALUE) ? NULL : h );
}


void 
CUsbIo::DestroyDeviceList(HDEVINFO DeviceList)
{
  // make sure the setupapi dll is loaded
  if ( !smSetupApi.Load() ) {
    return;
  }

  if ( DeviceList!=NULL ) {
    (smSetupApi.SetupDiDestroyDeviceInfoList)(DeviceList);
  }
}



DWORD CUsbIo::Open(int DeviceNumber, HDEVINFO DeviceList, const GUID* InterfaceGuid)
{
  DWORD Status;
  HANDLE h;
  char NameBuffer[80];
  const char* Name;

  if ( FileHandle != NULL ) {
    // already open
    return USBIO_ERR_SUCCESS;
  } 
  
  if ( DeviceList == NULL ) {
    // use the old way, using a well-known device name
    // build device name
    sprintf(NameBuffer,"\\\\.\\" USBIO_DEVICE_NAME "%d",DeviceNumber);
    Name = NameBuffer;
  } else {
    // use the device interface identified by InterfaceGuid
    // a GUID must be provided in this case
		Status = GetDeviceInstanceDetails(DeviceNumber, DeviceList, InterfaceGuid);
    if ( Status != USBIO_ERR_SUCCESS ) {
			return Status;
		}
    // get name
    Name = GetDevicePathName();
  }

  // try to open the device driver
  h = ::CreateFile(
            Name,
            GENERIC_READ | GENERIC_WRITE,       // access mode
            FILE_SHARE_WRITE | FILE_SHARE_READ, // share mode
            NULL,                               // security desc.
            OPEN_EXISTING,                      // how to create
            FILE_FLAG_OVERLAPPED,               // file attributes
            NULL                                // template file
            );
  if ( h == INVALID_HANDLE_VALUE ) {
    Status = USBIO_ERR_DEVICE_NOT_FOUND;
  } else {
    // save handle
    FileHandle = h;
    // init the event with auto reset, not signaled
    Overlapped.hEvent = CreateEvent(NULL ,FALSE ,FALSE ,NULL); 
    if ( Overlapped.hEvent == NULL ) {
      Status = USBIO_ERR_NO_MEMORY;
      Close();
    } else {
      
      // now get version info
      USBIO_DRIVER_INFO info;
      Status = GetDriverInfo(&info);
      if ( Status != USBIO_ERR_SUCCESS ) {
        // failed
        Close();
      } else {

        CheckedBuildDetected = (info.Flags&USBIO_INFOFLAG_CHECKED_BUILD) ? TRUE : FALSE;
        DemoVersionDetected = (info.Flags&USBIO_INFOFLAG_DEMO_VERSION) ? TRUE : FALSE;
        LightVersionDetected = (info.Flags&USBIO_INFOFLAG_LIGHT_VERSION) ? TRUE : FALSE;

        // now check the API version
        // currently used version must match to the driver installed
        if ( info.APIVersion != USBIO_API_VERSION ) {
          // wrong version
          Status = USBIO_ERR_VERSION_MISMATCH;
          Close();
        } else {
          
          // success
          Status = USBIO_ERR_SUCCESS;
        }
      }
    }
  }
  return Status;
}



void CUsbIo::Close()
{
  if ( FileHandle != NULL ) {
    ::CloseHandle(FileHandle);
    FileHandle = NULL;
  }
  if ( Overlapped.hEvent != NULL ) {
    ::CloseHandle(Overlapped.hEvent);
    Overlapped.hEvent = NULL;
  }
  if ( mDevDetail != NULL ) {
    delete [] (char*)mDevDetail;
    mDevDetail = NULL;
  }
}


DWORD
CUsbIo::GetDeviceInstanceDetails(int DeviceNumber, HDEVINFO DeviceList, const GUID* InterfaceGuid)
{
	DWORD Status;
  BOOL succ;

  // check parameters
	if ( DeviceList==NULL || InterfaceGuid==NULL ) {
    return USBIO_ERR_INVALID_FUNCTION_PARAM;
	}

  // make sure the setupapi dll is loaded
  if ( !smSetupApi.Load() ) {
    return USBIO_ERR_LOAD_SETUP_API_FAILED;
  }

  // delete old detail data if any
  if ( mDevDetail!=NULL ) {
    delete [] (char*)mDevDetail;
    mDevDetail = NULL;
  }

  // enumerate the interface
  // get the device information for the given device number
  SP_DEVICE_INTERFACE_DATA DevData;
  ZeroMemory(&DevData,sizeof(DevData));
  DevData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  succ = (smSetupApi.SetupDiEnumDeviceInterfaces)(DeviceList, NULL, (GUID*)InterfaceGuid, DeviceNumber, &DevData );
  if ( !succ ) {
    Status = GetLastError();
    if ( Status==ERROR_NO_MORE_ITEMS ) {
			Status = USBIO_ERR_NO_SUCH_DEVICE_INSTANCE;
		}
    return Status;
  }

  // get length of the detailed information, allocate buffer
  DWORD ReqLen = 0;
  (smSetupApi.SetupDiGetDeviceInterfaceDetail)(DeviceList, &DevData, NULL, 0, &ReqLen, NULL);
	if ( ReqLen==0 ) {
    return USBIO_ERR_FAILED;
	}
	mDevDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA*) new char[ReqLen];
  if ( mDevDetail==NULL ) {
    return USBIO_ERR_NO_MEMORY;
  }

  // now get the  detailed device information
  ZeroMemory(mDevDetail,ReqLen);
  mDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  succ = (smSetupApi.SetupDiGetDeviceInterfaceDetail)(DeviceList, &DevData, mDevDetail, ReqLen, &ReqLen, NULL);
  if ( !succ ) {
    Status = GetLastError();
    return Status;
  }

	// success, mDevDetail contains the device instance details now
	return USBIO_ERR_SUCCESS;
}


const char*
CUsbIo::GetDevicePathName()
{
  if ( mDevDetail!=NULL ) {
    return mDevDetail->DevicePath;
  } else {
    return NULL;
  }
}


DWORD CUsbIo::GetDriverInfo(USBIO_DRIVER_INFO *DriverInfo)
{
  DWORD Status;

  Status = IoctlSync(
              IOCTL_USBIO_GET_DRIVER_INFO,
              NULL,
              0,
              DriverInfo,
              sizeof(USBIO_DRIVER_INFO),
              NULL
              );

  return Status;
}



BOOL
CUsbIo::IsOperatingAtHighSpeed()
{
	USBIO_DEVICE_INFO info;
	ZeroMemory(&info,sizeof(info));

  DWORD Status = GetDeviceInfo(&info);
	if ( Status==USBIO_ERR_SUCCESS ) {
		return (info.Flags&USBIO_DEVICE_INFOFLAG_HIGH_SPEED) ? TRUE : FALSE;
	} else {
		// query failed
		return FALSE;
	}
}



DWORD CUsbIo::GetDeviceInfo(USBIO_DEVICE_INFO* DeviceInfo)
{
  return IoctlSync(
              IOCTL_USBIO_GET_DEVICE_INFO,
              NULL,
              0,
              DeviceInfo,
              sizeof(USBIO_DEVICE_INFO),
              NULL
              );
}


DWORD CUsbIo::GetBandwidthInfo(USBIO_BANDWIDTH_INFO* BandwidthInfo)
{
  return IoctlSync(
              IOCTL_USBIO_GET_BANDWIDTH_INFO,
              NULL,
              0,
              BandwidthInfo,
              sizeof(USBIO_BANDWIDTH_INFO),
              NULL
              );
}


DWORD CUsbIo::GetDescriptor(
        void* Buffer,
        DWORD& ByteCount,
        USBIO_REQUEST_RECIPIENT Recipient,
        UCHAR DescriptorType,
        UCHAR DescriptorIndex/*=0*/,
        USHORT LanguageId/*=0*/ 
        )
{
  DWORD Status;
  USBIO_DESCRIPTOR_REQUEST req;

  // zero the struct if any fields are added...
  ZeroMemory(&req,sizeof(req));
  req.Recipient = Recipient;
  req.DescriptorType = DescriptorType;
  req.DescriptorIndex = DescriptorIndex;
  req.LanguageId = LanguageId;
  
  Status = IoctlSync(
              IOCTL_USBIO_GET_DESCRIPTOR,
              &req,
              sizeof(req),
              Buffer,
              ByteCount,
              &ByteCount
              );

  return Status;
}



DWORD CUsbIo::SetDescriptor(
        const void* Buffer,
        DWORD& ByteCount,
        USBIO_REQUEST_RECIPIENT Recipient,
        UCHAR DescriptorType,
        UCHAR DescriptorIndex/*=0*/,
        USHORT LanguageId/*=0*/ 
        )
{
  DWORD Status;
  USBIO_DESCRIPTOR_REQUEST req;

  // zero the struct if any fields are added...
  ZeroMemory(&req,sizeof(req));
  req.Recipient = Recipient;
  req.DescriptorType = DescriptorType;
  req.DescriptorIndex = DescriptorIndex;
  req.LanguageId = LanguageId;
  
  Status = IoctlSync(
              IOCTL_USBIO_SET_DESCRIPTOR,
              &req,
              sizeof(req),
              (void*)Buffer,
              ByteCount,
              &ByteCount
              );

  return Status;
}



DWORD CUsbIo::SetFeature(
        USBIO_REQUEST_RECIPIENT Recipient,
        USHORT FeatureSelector,
        USHORT Index/*=0*/ 
        )
{
  DWORD Status;
  USBIO_FEATURE_REQUEST req;

  // zero the struct if any fields are added...
  ZeroMemory(&req,sizeof(req));
  req.Recipient = Recipient;
  req.FeatureSelector = FeatureSelector;
  req.Index = Index;
  
  Status = IoctlSync(
              IOCTL_USBIO_SET_FEATURE,
              &req,
              sizeof(req),
              NULL,
              0,
              NULL
              );

  return Status;
}



DWORD CUsbIo::ClearFeature(
        USBIO_REQUEST_RECIPIENT Recipient,
        USHORT FeatureSelector,
        USHORT Index/*=0*/ 
        )
{
  DWORD Status;
  USBIO_FEATURE_REQUEST req;

  // zero the struct if any fields are added...
  ZeroMemory(&req,sizeof(req));
  req.Recipient = Recipient;
  req.FeatureSelector = FeatureSelector;
  req.Index = Index;
  
  Status = IoctlSync(
              IOCTL_USBIO_CLEAR_FEATURE,
              &req,
              sizeof(req),
              NULL,
              0,
              NULL
              );

  return Status;
}



DWORD CUsbIo::GetStatus(
        USHORT& StatusValue,
        USBIO_REQUEST_RECIPIENT Recipient,
        USHORT Index/*=0*/ )
{
  DWORD Status;
  USBIO_STATUS_REQUEST req;
  USBIO_STATUS_REQUEST_DATA data;

  // zero the structs if any fields are added...
  ZeroMemory(&req,sizeof(req));
  ZeroMemory(&data,sizeof(data));
  req.Recipient = Recipient;
  req.Index = Index;
  
  Status = IoctlSync(
              IOCTL_USBIO_GET_STATUS,
              &req,
              sizeof(req),
              &data,
              sizeof(data),
              NULL
              );

  StatusValue = data.Status;

  return Status;
}



DWORD CUsbIo::ClassOrVendorInRequest(
        void* Buffer,
        DWORD& ByteCount,
        const USBIO_CLASS_OR_VENDOR_REQUEST* Request 
        )
{
  DWORD Status;

  Status = IoctlSync(
              IOCTL_USBIO_CLASS_OR_VENDOR_IN_REQUEST,
              Request,
              sizeof(USBIO_CLASS_OR_VENDOR_REQUEST),
              Buffer,
              ByteCount,
              &ByteCount
              );

  return Status;
}



DWORD CUsbIo::ClassOrVendorOutRequest(
        const void* Buffer,
        DWORD& ByteCount,
        const USBIO_CLASS_OR_VENDOR_REQUEST* Request
        )
{
  DWORD Status;

  Status = IoctlSync(
              IOCTL_USBIO_CLASS_OR_VENDOR_OUT_REQUEST,
              Request,
              sizeof(USBIO_CLASS_OR_VENDOR_REQUEST),
              (void*)Buffer,
              ByteCount,
              &ByteCount
              );

  return Status;
}



DWORD CUsbIo::SetConfiguration(const USBIO_SET_CONFIGURATION* Conf)
{
  DWORD Status;

  Status = IoctlSync(
              IOCTL_USBIO_SET_CONFIGURATION,
              Conf,
              sizeof(USBIO_SET_CONFIGURATION),
              NULL,
              0,
              NULL
              );

  return Status;
}


DWORD CUsbIo::UnconfigureDevice()
{
  DWORD Status;

  Status = IoctlSync(
              IOCTL_USBIO_UNCONFIGURE_DEVICE,
              NULL,
              0,
              NULL,
              0,
              NULL
              );

  return Status;

⌨️ 快捷键说明

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