bool SendReceiveData::init() { //========================== PHID_DEVICE devices; ULONG numberOfDevices; BOOL deviceFound; TCHAR deviceName[1024]; //========================== deviceFound = 0; toggleBit = FALSE; irCode = 0; lastValue = 0; repeats = 0; if(FindKnownHidDevices(&devices,&numberOfDevices)) { for(UINT i=0; i<numberOfDevices; i++) { if(devices[i].Attributes.ProductID==0x37) { if(devices[i].Attributes.VendorID==0x18B1) { if(devices[i].Caps.UsagePage==0xFF80) { if(devices[i].Caps.Usage==0) { _tcscpy_s(deviceName,_countof(deviceName),devices[i].DevicePath); deviceFound = TRUE; } } } } } CloseHidDevices(devices,numberOfDevices); free(devices); } if(deviceFound) { if(OpenHidDevice(deviceName,TRUE,TRUE,TRUE,FALSE,&device)) { threadHandle = CreateThread(NULL,0,MaxterPlus,(void *)this,0,NULL); if(threadHandle) { return true; } } } return false; }
BOOLEAN FindKnownHidDevices ( OUT PHID_DEVICE * HidDevices, // A array of struct _HID_DEVICE OUT PULONG NumberDevices // the length of this array. ) /*++ Routine Description: Do the required PnP things in order to find all the HID devices in the system at this time. --*/ { HDEVINFO hardwareDeviceInfo; SP_DEVICE_INTERFACE_DATA deviceInfoData; ULONG i; BOOLEAN done; PHID_DEVICE hidDeviceInst; GUID hidGuid; PSP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData = NULL; ULONG predictedLength = 0; ULONG requiredLength = 0; PHID_DEVICE newHidDevices; HidD_GetHidGuid (&hidGuid); *HidDevices = NULL; *NumberDevices = 0; // // Open a handle to the plug and play dev node. // hardwareDeviceInfo = SetupDiGetClassDevs ( &hidGuid, NULL, // Define no enumerator (global) NULL, // Define no (DIGCF_PRESENT | // Only Devices present DIGCF_DEVICEINTERFACE)); // Function class devices. // // Take a wild guess to start // *NumberDevices = 4; done = FALSE; deviceInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA); i=0; while (!done) { *NumberDevices *= 2; if (*HidDevices) { newHidDevices = realloc (*HidDevices, (*NumberDevices * sizeof (HID_DEVICE))); if (NULL == newHidDevices) { free(*HidDevices); } *HidDevices = newHidDevices; } else { *HidDevices = calloc (*NumberDevices, sizeof (HID_DEVICE)); } if (NULL == *HidDevices) { SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); return FALSE; } hidDeviceInst = *HidDevices + i; for (; i < *NumberDevices; i++, hidDeviceInst++) { if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo, 0, // No care about specific PDOs &hidGuid, i, &deviceInfoData)) { // // allocate a function class device data structure to receive the // goods about this particular device. // SetupDiGetDeviceInterfaceDetail ( hardwareDeviceInfo, &deviceInfoData, NULL, // probing so no output buffer yet 0, // probing so output buffer length of zero &requiredLength, NULL); // not interested in the specific dev-node predictedLength = requiredLength; functionClassDeviceData = malloc (predictedLength); if (functionClassDeviceData) { functionClassDeviceData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA); } else { SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); return FALSE; } // // Retrieve the information from Plug and Play. // if (! SetupDiGetDeviceInterfaceDetail ( hardwareDeviceInfo, &deviceInfoData, functionClassDeviceData, predictedLength, &requiredLength, NULL)) { SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); free(functionClassDeviceData); return FALSE; } // // Open device with just generic query abilities to begin with // if (! OpenHidDevice (functionClassDeviceData -> DevicePath, FALSE, // ReadAccess - none FALSE, // WriteAccess - none FALSE, // Overlapped - no FALSE, // Exclusive - no hidDeviceInst)) { SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); free(functionClassDeviceData); return FALSE; } } else { if (ERROR_NO_MORE_ITEMS == GetLastError()) { done = TRUE; break; } } } } *NumberDevices = i; SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); free(functionClassDeviceData); return TRUE; }
void WM_COMMAND_hook ( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { PHID_DEVICE pDevice; BOOL status; switch (LOWORD(wParam)) { case IDC_FLD_CTRL_DEV: { if (BT_writeDevice.HidDevice != 0) { HWND hUART_DEV = GetDlgItem(hDlg_main, IDC_UART_DEVICE); SetWindowText(hUART_DEV, ""); HWND hBUTT = GetDlgItem(hDlg_main, IDC_FLD_CTRL_DEV); SetWindowText(hBUTT, "DEV OPEN"); CloseHidDevice(&BT_writeDevice); EnableWindow(GetDlgItem(hDlg, IDC_DEVICES), 1); // Set device change message to enable other controls PostMessage(hDlg, WM_COMMAND, IDC_DEVICES + (CBN_SELCHANGE<<16), (LPARAM) GetDlgItem(hDlg,IDC_DEVICES)); ZeroMemory(&BT_writeDevice, sizeof(BT_writeDevice)); } else { INT iIndex; pDevice = NULL; iIndex = (INT) SendDlgItemMessage(hDlg, IDC_DEVICES, CB_GETCURSEL, 0, 0); if (CB_ERR != iIndex) { pDevice = (PHID_DEVICE) SendDlgItemMessage(hDlg, IDC_DEVICES, CB_GETITEMDATA, iIndex, 0); } if (NULL == pDevice) break; status = OpenHidDevice(pDevice -> DevicePath, FALSE, TRUE, FALSE, FALSE, &BT_writeDevice); if (!status) { MessageBox(hDlg, "Couldn't open device for write access", HCLIENT_ERROR, MB_ICONEXCLAMATION); ZeroMemory(&BT_writeDevice, sizeof(BT_writeDevice)); } // Disable all other controls EnableWindow(GetDlgItem(hDlg, IDC_EXTCALLS), 0); EnableWindow(GetDlgItem(hDlg, IDC_READ), 0); EnableWindow(GetDlgItem(hDlg, IDC_WRITE), 0); EnableWindow(GetDlgItem(hDlg, IDC_FEATURES), 0); EnableWindow(GetDlgItem(hDlg, IDC_DEVICES), 0); StringCbPrintf(szTempBuff, SMALL_BUFF, "VID: 0x%04X PID: 0x%04X UsagePage: 0x%X Usage: 0x%X", BT_writeDevice.Attributes.VendorID, BT_writeDevice.Attributes.ProductID, BT_writeDevice.Caps.UsagePage, BT_writeDevice.Caps.Usage); HWND hUART_DEV = GetDlgItem(hDlg_main, IDC_UART_DEVICE); SetWindowText(hUART_DEV, szTempBuff); HWND hBUTT = GetDlgItem(hDlg_main, IDC_FLD_CTRL_DEV); SetWindowText(hBUTT, "DEV CLOSE"); } // End of Device open break; } // End of FLD_CTRL_DEV case } // End of wParam switch return; }