246 lines
6.8 KiB
C
246 lines
6.8 KiB
C
|
/***************************************************************************
|
|||
|
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> USBHID<EFBFBD>豸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* <EFBFBD><EFBFBD> <EFBFBD>ߣ<EFBFBD> ղ<EFBFBD><EFBFBD>
|
|||
|
* ʱ <EFBFBD>䣺 2016/10/29
|
|||
|
***************************************************************************/
|
|||
|
#include <Windows.h>
|
|||
|
#include <Dbt.h>
|
|||
|
#include <hidsdi.h>
|
|||
|
#include <SetupAPI.h>
|
|||
|
#include <stdio.h>
|
|||
|
#include <usbioctl.h>
|
|||
|
|
|||
|
#include "USB_Win32_HIDDevice.h"
|
|||
|
#include "MagCalibrate.h"
|
|||
|
#include "SensorImpl.h"
|
|||
|
|
|||
|
#pragma comment(lib,"setupapi.lib")
|
|||
|
#pragma comment(lib, "hid.lib")
|
|||
|
|
|||
|
unsigned char usb_output_buffer[65];
|
|||
|
unsigned char usb_input_buffer[256];
|
|||
|
unsigned char usb_feature_report_buffer[256];
|
|||
|
|
|||
|
unsigned char buffer[255];
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
|
|||
|
HANDLE dematrix_usb_handle = INVALID_HANDLE_VALUE;
|
|||
|
HDEVINFO device_info_set;
|
|||
|
GUID HidGuid;
|
|||
|
SP_DEVICE_INTERFACE_DATA device_interface_data;
|
|||
|
HIDP_CAPS dematrix_device_capabilities;
|
|||
|
|
|||
|
HANDLE DM_HID_Read_HANDLE;
|
|||
|
HANDLE DM_HID_Write_HANDLE;
|
|||
|
|
|||
|
int i=5;
|
|||
|
BOOL device_is_found;
|
|||
|
OVERLAPPED usb_read_over_lapped;
|
|||
|
PSP_DEVICE_INTERFACE_DETAIL_DATA pData;
|
|||
|
|
|||
|
|
|||
|
BOOL HIDFindDeviceFromVendor(USHORT PID,USHORT VID,CHAR *Path)
|
|||
|
{
|
|||
|
HANDLE DM_USB_HANDLE;
|
|||
|
HIDD_ATTRIBUTES Dev_Attr;
|
|||
|
|
|||
|
Dev_Attr.Size = sizeof(Dev_Attr);
|
|||
|
|
|||
|
DM_USB_HANDLE = CreateFileA(Path, (DWORD)NULL, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
|||
|
OPEN_EXISTING, (DWORD)NULL, NULL);
|
|||
|
if(DM_USB_HANDLE != INVALID_HANDLE_VALUE)
|
|||
|
{
|
|||
|
if (HidD_GetAttributes(DM_USB_HANDLE, &Dev_Attr) == FALSE) return FALSE;
|
|||
|
|
|||
|
HidD_GetNumInputBuffers(DM_USB_HANDLE, &i);
|
|||
|
//printf("BufferSize[%d]\n",i);
|
|||
|
dematrix_usb_get_capabilities(DM_USB_HANDLE);
|
|||
|
//printf("Cap.out.length[%d]\n", dematrix_device_capabilities.OutputReportByteLength);
|
|||
|
//printf("(PID=%x,VID=%x,VersionTest=%x)\n", Dev_Attr.ProductID, Dev_Attr.VendorID, Dev_Attr.VersionNumber);
|
|||
|
if (Dev_Attr.ProductID == PID && Dev_Attr.VendorID == VID)
|
|||
|
{
|
|||
|
//printf("(PID=%x,VID=%x,VersionTest=%x)\n", Dev_Attr.ProductID, Dev_Attr.VendorID, Dev_Attr.VersionNumber);
|
|||
|
return TRUE;
|
|||
|
}
|
|||
|
}
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
BOOL InitPathFromInterfaceData(HDEVINFO hdevInfoSet, SP_DEVICE_INTERFACE_DATA* pidata)
|
|||
|
{
|
|||
|
DWORD detailSize = 0;
|
|||
|
BOOL Result;
|
|||
|
|
|||
|
Result = SetupDiGetDeviceInterfaceDetail(hdevInfoSet,pidata, NULL, (DWORD)NULL, &detailSize, NULL);
|
|||
|
|
|||
|
pData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(detailSize);
|
|||
|
if (pData == NULL){
|
|||
|
printf("<EFBFBD>ڴ治<EFBFBD>㣡\n");
|
|||
|
SetupDiDestroyDeviceInfoList(hdevInfoSet);
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
pData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
|||
|
|
|||
|
Result = SetupDiGetDeviceInterfaceDetail(hdevInfoSet, pidata, pData, detailSize, NULL, NULL);
|
|||
|
if(Result==TRUE)
|
|||
|
{
|
|||
|
//printf("Path = %s\n",(char *)(pData->DevicePath));
|
|||
|
return TRUE;
|
|||
|
}
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
|
|||
|
/*<2A><><EFBFBD><EFBFBD>USB<53>豸,<2C><>USB<53>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>ö<EFBFBD>٣<EFBFBD><D9A3><EFBFBD><EFBFBD><EFBFBD>PID<49><44>VID<49><44><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3>ʹ<EFBFBD><CDB4><EFBFBD>USB<53>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
|||
|
BOOL DM_HIDDeviceEnumerate(USHORT PID,USHORT VID)
|
|||
|
{
|
|||
|
HDEVINFO hdevInfoSet;
|
|||
|
SP_DEVICE_INTERFACE_DATA interfaceData;
|
|||
|
|
|||
|
|
|||
|
int deviceIndex = 0;
|
|||
|
interfaceData.cbSize = sizeof(interfaceData);
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD><C8A1>GUID */
|
|||
|
HidD_GetHidGuid(&HidGuid);
|
|||
|
|
|||
|
hdevInfoSet = SetupDiGetClassDevs(&HidGuid, NULL, NULL, DIGCF_INTERFACEDEVICE | DIGCF_PRESENT);
|
|||
|
|
|||
|
if (hdevInfoSet == INVALID_HANDLE_VALUE) return FALSE;
|
|||
|
|
|||
|
for(deviceIndex = 0;
|
|||
|
SetupDiEnumDeviceInterfaces(hdevInfoSet, NULL, &HidGuid, deviceIndex, &interfaceData);
|
|||
|
deviceIndex++)
|
|||
|
{
|
|||
|
//printf("index = %d\n",deviceIndex);
|
|||
|
|
|||
|
if (!InitPathFromInterfaceData(hdevInfoSet, &interfaceData))
|
|||
|
continue;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD>Ҫ·<D2AA><C2B7>
|
|||
|
if(HIDFindDeviceFromVendor(PID,VID,(CHAR*)(pData->DevicePath)))
|
|||
|
{
|
|||
|
//printf("Path = %s\n",(char *)(pData->DevicePath));
|
|||
|
|
|||
|
DM_HID_Read_HANDLE = CreateFileA((char *)(pData->DevicePath), (DWORD)GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |
|
|||
|
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
|
|||
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
|
|||
|
|
|||
|
DM_HID_Write_HANDLE = CreateFileA((char *)(pData->DevicePath), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|||
|
(LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, 0, NULL);
|
|||
|
|
|||
|
|
|||
|
device_is_found = TRUE;
|
|||
|
SetEvent(usb_read_over_lapped.hEvent);
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>\n");
|
|||
|
break;
|
|||
|
}else
|
|||
|
continue;
|
|||
|
}
|
|||
|
SetupDiDestroyDeviceInfoList(hdevInfoSet);
|
|||
|
|
|||
|
if(device_is_found==TRUE)return TRUE;
|
|||
|
else
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
|
|||
|
void USB_HID_Init()
|
|||
|
{
|
|||
|
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>õ<EFBFBD>Overlapped<65>ṹ<EFBFBD><E1B9B9>
|
|||
|
//ƫ<><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0
|
|||
|
usb_read_over_lapped.Offset = 0;
|
|||
|
usb_read_over_lapped.OffsetHigh = 0;
|
|||
|
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>ṩ<EFBFBD><E1B9A9>ReadFileʹ<65>ã<EFBFBD><C3A3><EFBFBD>ReadFile<6C><65><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><C3B8>¼<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
usb_read_over_lapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|||
|
|
|||
|
start_thread(dematrix_usb_private_read_thread, NULL);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
int globalpacklen = 0;
|
|||
|
DWORD dematrix_usb_private_read_thread(LPVOID param)
|
|||
|
{
|
|||
|
DWORD length;
|
|||
|
|
|||
|
//<2F><><EFBFBD>߳<EFBFBD><DFB3>Ǹ<EFBFBD><C7B8><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
|
|||
|
while (1)
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>Ϊ<EFBFBD><CEAA>Ч״̬
|
|||
|
ResetEvent(usb_read_over_lapped.hEvent);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD>Ѿ<EFBFBD><D1BE>ҵ<EFBFBD>
|
|||
|
if (device_is_found == TRUE)
|
|||
|
{
|
|||
|
|
|||
|
if (DM_HID_Read_HANDLE == INVALID_HANDLE_VALUE) {
|
|||
|
printf("<EFBFBD><EFBFBD>Ч<EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>豸ʱʧ<EFBFBD><EFBFBD>");
|
|||
|
}
|
|||
|
else {
|
|||
|
|
|||
|
CancelIo(DM_HID_Read_HANDLE);
|
|||
|
// DKHD2<44><32><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>62<36><32>DK2һ<32><D2BB>Ҫʹ<D2AA><CAB9>64<36><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB7><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|||
|
ReadFile(DM_HID_Read_HANDLE, usb_input_buffer,globalpacklen, NULL, &usb_read_over_lapped);
|
|||
|
}
|
|||
|
|
|||
|
//<2F>ȴ<EFBFBD><C8B4>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
WaitForSingleObject(usb_read_over_lapped.hEvent, INFINITE);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD>γ<EFBFBD><CEB3><EFBFBD>Ҳ<EFBFBD>ᵼ<EFBFBD><E1B5BC><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱMyDevFound
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>٣<EFBFBD><D9A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>MyDevFoundΪ<64>ٵĻ<D9B5><C4BB>ͽ<EFBFBD><CDBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>
|
|||
|
if (device_is_found == FALSE) continue;
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>豸û<E8B1B8>б<EFBFBD><D0B1><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD>ReadFile<6C><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>
|
|||
|
//ͨ<><CDA8>GetOverlappedResult<6C><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡʵ<C8A1>ʶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
GetOverlappedResult(DM_HID_Read_HANDLE, &usb_read_over_lapped, &length, TRUE);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>Ϊ0<CEAA><30><EFBFBD><EFBFBD><F2BDABB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
|||
|
if (length != 0)
|
|||
|
{
|
|||
|
int useful_length = usb_input_buffer[1];
|
|||
|
|
|||
|
GetControllerSensor(usb_input_buffer);
|
|||
|
}
|
|||
|
}
|
|||
|
else {
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3>ֱ<EFBFBD><D6B1><EFBFBD>´<EFBFBD><C2B4>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
WaitForSingleObject(usb_read_over_lapped.hEvent, INFINITE);
|
|||
|
}
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
void dematrix_usb_get_capabilities(HANDLE hid_usb_handle)
|
|||
|
{
|
|||
|
PHIDP_PREPARSED_DATA PreparsedData;
|
|||
|
|
|||
|
if (hid_usb_handle == NULL) return;
|
|||
|
|
|||
|
HidD_GetPreparsedData(hid_usb_handle, &PreparsedData);
|
|||
|
|
|||
|
HidP_GetCaps(PreparsedData, &dematrix_device_capabilities);
|
|||
|
|
|||
|
HidD_FreePreparsedData(PreparsedData);
|
|||
|
}
|
|||
|
|
|||
|
DWORD start_thread(void *function_address, void *param)
|
|||
|
{
|
|||
|
DWORD temp;
|
|||
|
#ifdef _WIN32
|
|||
|
HANDLE hThread;
|
|||
|
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)function_address, param, 0, &temp);
|
|||
|
if (hThread != NULL) return (DWORD)hThread;
|
|||
|
else return 0x00;
|
|||
|
#else
|
|||
|
#ifdef linux
|
|||
|
pthread_t hThread;
|
|||
|
int Result;
|
|||
|
Result = pthread_create(&hThread, NULL, (void *)function_address, param);
|
|||
|
if (Result != NULL) return Result;
|
|||
|
else return -NULL;
|
|||
|
#endif
|
|||
|
#endif
|
|||
|
}
|