📄 hiddevice.cpp
字号:
HidD_SetNumInputBuffers(hHidDeviceHandle, numInputBuffers);
}
else
{
// Pull the current settings if default is selected
HidD_GetNumInputBuffers(hHidDeviceHandle, (PULONG)(&m_MaxReportRequest));
}
}
// Cleanup the preparesed data
HidD_FreePreparsedData(preparsedData);
}
}
// Increment the device number
deviceNum++;
// If the handle hasn't been found close it and continue the search
if (!found)
{
CloseHandle(hHidDeviceHandle);
}
}
// Increment the number of devices found
i++;
}
// If the device wasnt found, return an error code
if (!found)
{
status = HID_DEVICE_NOT_FOUND;
}
}
return status;
}
BOOL CHIDDevice::IsOpened()
{
BOOL status = FALSE;
// Check if a device is opened, and the handle is valid
if (m_DeviceOpened && (m_Handle != INVALID_HANDLE_VALUE) && (m_Handle != NULL))
{
status = TRUE;
}
else
{
// If the device is opened, and the handle is invalid or NULL
// reset the device opened flag, and the handle
if (m_DeviceOpened)
{
ResetDeviceData();
}
status = FALSE;
}
return status;
}
BYTE CHIDDevice::SetFeatureReport(BYTE* buffer, DWORD bufferSize)
{
BYTE status = HID_DEVICE_SUCCESS;
// Check to see that the device is opened
if (m_DeviceOpened)
{
if (!HidD_SetFeature(m_Handle, buffer, bufferSize))
{
status = HID_DEVICE_TRANSFER_FAILED;
}
}
else
{
status = HID_DEVICE_NOT_OPENED;
}
return status;
}
BYTE CHIDDevice::GetFeatureReport(BYTE* buffer, DWORD bufferSize)
{
BYTE reportID = buffer[0];
BYTE status = HID_DEVICE_SUCCESS;
// Check to see that the device is opened
if (m_DeviceOpened)
{
// Clear out the report buffer, and set the head to the report ID
memset(buffer, 0, bufferSize);
buffer[0] = reportID;
if (!HidD_GetFeature(m_Handle, buffer, bufferSize))
{
status = HID_DEVICE_TRANSFER_FAILED;
}
}
else
{
status = HID_DEVICE_NOT_OPENED;
}
return status;
}
BYTE CHIDDevice::SetReport_Interrupt(BYTE* buffer, DWORD bufferSize)
{
BYTE status = HID_DEVICE_SUCCESS;
if (bufferSize <= (DWORD)(m_OutputReportBufferLength))
{
// Check to see that the device is opened
if (IsOpened())
{
DWORD bytesWritten = 0;
OVERLAPPED o = {0};
o.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// Try to write the file
if (!WriteFile(m_Handle, buffer, bufferSize, &bytesWritten, &o))
{
// If the write fails, see if it is because IO is pending
if (GetLastError() == ERROR_IO_PENDING)
{
//If there is still data to be written, wait on the event for 3 seconds
DWORD waitStatus = WaitForSingleObject(o.hEvent, m_SetReportTimeout);
// If the object is signaled, then get the overlapped result, the write succeeded
// Otherwise determine if the error was a timeout, or another error
if (waitStatus == WAIT_OBJECT_0)
{
GetOverlappedResult(m_Handle, &o, &bytesWritten, FALSE);
}
else if (waitStatus == WAIT_TIMEOUT)
{
status = HID_DEVICE_TRANSFER_TIMEOUT;
CancelIo(m_Handle);
}
else
{
status = HID_DEVICE_TRANSFER_FAILED;
CancelIo(m_Handle);
}
}
else
{
status = HID_DEVICE_TRANSFER_FAILED;
}
}
CloseHandle(o.hEvent);
}
else
{
status = HID_DEVICE_NOT_OPENED;
}
}
else
{
status = HID_DEVICE_INVALID_BUFFER_SIZE;
}
return status;
}
BYTE CHIDDevice::GetReport_Interrupt(BYTE* buffer, DWORD bufferSize, WORD numReports, DWORD* bytesReturned)
{
BYTE status = HID_DEVICE_SUCCESS;
DWORD bytesRead = 0;
if ((bufferSize >= (DWORD)(m_InputReportBufferLength * numReports)) && (numReports <= m_MaxReportRequest))
{
// Clear out the report buffer, and set the head to the report ID
memset(buffer, 0, bufferSize);
// Check to see that the device is opened
if (IsOpened())
{
OVERLAPPED o = {0};
o.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// Try to read input data
if (!ReadFile(m_Handle, buffer, m_InputReportBufferLength * numReports, &bytesRead, &o))
{
// If the read fails, see if it is because IO is pending
if (GetLastError() == ERROR_IO_PENDING)
{
// If there is still data to read, wait on the event object for 3 seconds
DWORD waitStatus = WaitForSingleObject(o.hEvent, m_GetReportTimeout);
// If the object is signaled, then get the overlapped result, the read succeeded
// Otherwise determine if the error was a timeout, or another error
if (waitStatus == WAIT_OBJECT_0)
{
GetOverlappedResult(m_Handle, &o, &bytesRead, FALSE);
}
else if (waitStatus == WAIT_TIMEOUT)
{
status = HID_DEVICE_TRANSFER_TIMEOUT;
CancelIo(m_Handle);
}
else
{
status = HID_DEVICE_TRANSFER_FAILED;
CancelIo(m_Handle);
}
}
else
{
status = HID_DEVICE_TRANSFER_FAILED;
}
}
CloseHandle(o.hEvent);
}
else
{
status = HID_DEVICE_NOT_OPENED;
}
}
else
{
status = HID_DEVICE_INVALID_BUFFER_SIZE;
}
// If the read succeeded, then send the number of bytes read back
if (status == HID_DEVICE_SUCCESS)
{
*bytesReturned = bytesRead;
}
return status;
}
BYTE CHIDDevice::SetReport_Control(BYTE* buffer, DWORD bufferSize)
{
BYTE status = HID_DEVICE_SUCCESS;
DWORD lasterror;
//if (bufferSize >= m_OutputReportBufferLength)
//if (1)
{
// Clear out the report buffer, and set the head to the report ID
// memset(buffer, 0, bufferSize);
// Check to see that the device is opened
if (IsOpened())
{
// Call SetOutputReport to send this report buffer over the control pipe
if (!HidD_SetOutputReport(m_Handle, buffer, bufferSize))
{
status = HID_DEVICE_TRANSFER_FAILED;
lasterror = GetLastError();
}
}
else
{
status = HID_DEVICE_NOT_OPENED;
}
}
/*
else
{
status = HID_DEVICE_INVALID_BUFFER_SIZE;
}
*/
return status;
}
BYTE CHIDDevice::GetReport_Control(BYTE* buffer, DWORD bufferSize)
{
BYTE status = HID_DEVICE_SUCCESS;
unsigned char reportID = buffer[0];
if (bufferSize >= m_OutputReportBufferLength)
{
// Clear out the report buffer, and set the head to the report ID
memset(buffer, 0, bufferSize);
buffer[0] = reportID;
// Check to see that the device is opened
if (IsOpened())
{
// Call GetInputReport to get the requested report buffer over the control pipe
if (!HidD_GetInputReport(m_Handle, buffer, m_OutputReportBufferLength))
{
status = HID_DEVICE_TRANSFER_FAILED;
}
}
else
{
status = HID_DEVICE_NOT_OPENED;
}
}
else
{
status = HID_DEVICE_INVALID_BUFFER_SIZE;
}
return status;
}
BOOL CHIDDevice::FlushBuffers()
{
return HidD_FlushQueue(m_Handle);
}
WORD CHIDDevice::GetInputReportBufferLength()
{
// Returns specified buffer length
return m_InputReportBufferLength;
}
WORD CHIDDevice::GetOutputReportBufferLength()
{
// Returns specified buffer length
return m_OutputReportBufferLength;
}
WORD CHIDDevice::GetFeatureReportBufferLength()
{
// Returns specified buffer length
return m_FeatureReportBufferLength;
}
WORD CHIDDevice::GetMaxReportRequest()
{
WORD maxReportRequest;
// Returns the maximum allowable reports to request for read at a time
if (m_MaxReportRequest > 0)
maxReportRequest = m_MaxReportRequest;
else
maxReportRequest = 32;
return maxReportRequest;
}
void CHIDDevice::GetTimeouts(UINT* getReportTimeout, UINT* setReportTimeout)
{
// Will get the timeouts for Get/SetReport
*getReportTimeout = m_GetReportTimeout;
*setReportTimeout = m_SetReportTimeout;
}
void CHIDDevice::SetTimeouts(UINT getReportTimeout, UINT setReportTimeout)
{
// Will set the timeouts for Get/SetReport
m_GetReportTimeout = getReportTimeout;
m_SetReportTimeout = setReportTimeout;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -