BOOL vmultib_connect(pvmultib_client vmultib) { // // Find the HID devices // vmultib->hControl = SearchMatchingHwID(0xff00, 0x0001); if (vmultib->hControl == INVALID_HANDLE_VALUE || vmultib->hControl == NULL) return FALSE; vmultib->hMessage = SearchMatchingHwID(0xff00, 0x0002); if (vmultib->hMessage == INVALID_HANDLE_VALUE || vmultib->hMessage == NULL) { vmultib_disconnect(vmultib); return FALSE; } // // Set the buffer count to 10 on the message HID // if (!HidD_SetNumInputBuffers(vmultib->hMessage, 10)) { printf("failed HidD_SetNumInputBuffers %d\n", GetLastError()); vmultib_disconnect(vmultib); return FALSE; } return TRUE; }
HID_API_EXPORT hid_device* HID_API_CALL hid_open_path(const char *path) { hid_device *dev; HIDP_CAPS caps; PHIDP_PREPARSED_DATA pp_data = NULL; BOOLEAN res; NTSTATUS nt_res; if (hid_init() < 0) { return NULL; } dev = new_hid_device(); /* Open a handle to the device */ dev->device_handle = open_device(path, FALSE); /* Check validity of write_handle. */ if (dev->device_handle == INVALID_HANDLE_VALUE) { /* Unable to open the device. */ register_error(dev, "CreateFile"); goto err; } /* Set the Input Report buffer size to 64 reports. */ res = HidD_SetNumInputBuffers(dev->device_handle, 64); if (!res) { register_error(dev, "HidD_SetNumInputBuffers"); goto err; } /* Get the Input Report length for the device. */ res = HidD_GetPreparsedData(dev->device_handle, &pp_data); if (!res) { register_error(dev, "HidD_GetPreparsedData"); goto err; } nt_res = HidP_GetCaps(pp_data, &caps); if (nt_res != HIDP_STATUS_SUCCESS) { register_error(dev, "HidP_GetCaps"); goto err_pp_data; } dev->output_report_length = caps.OutputReportByteLength; dev->input_report_length = caps.InputReportByteLength; HidD_FreePreparsedData(pp_data); dev->read_buf = (char*) malloc(dev->input_report_length); return dev; err_pp_data: HidD_FreePreparsedData(pp_data); err: free_hid_device(dev); return NULL; }
LIBFREESPACE_API int freespace_openDevice(FreespaceDeviceId id) { int idx; struct FreespaceDeviceStruct* device = freespace_private_getDeviceById(id); if (device == NULL) { return FREESPACE_ERROR_NO_DEVICE; } if (device->isOpened_) { // Each device can only be opened once. return FREESPACE_ERROR_BUSY; } for (idx = 0; idx < device->handleCount_; idx++) { struct FreespaceSubStruct* s = &device->handle_[idx]; if (s->handle_ != NULL) { // Device was partially (incorrectly) opened. freespace_private_forceCloseDevice(device); return FREESPACE_ERROR_BUSY; } if (s->devicePath == NULL) { // Device was not fully enumerated. freespace_private_forceCloseDevice(device); return FREESPACE_ERROR_NO_DEVICE; } DEBUG_WPRINTF(L"Open %s\n", s->devicePath); s->handle_ = CreateFile(s->devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); { DWORD d; if (!GetHandleInformation(s->handle_, &d)) { // We do not have the correct handle. DEBUG_PRINTF("freespace_openDevice failed with code %d\n", GetLastError()); } } if (s->handle_ == INVALID_HANDLE_VALUE) { freespace_private_forceCloseDevice(device); return FREESPACE_ERROR_NO_DEVICE; } if (!BindIoCompletionCallback(s->handle_, freespace_private_overlappedCallback, 0)) { freespace_private_forceCloseDevice(device); return FREESPACE_ERROR_UNEXPECTED; } if (!HidD_SetNumInputBuffers(s->handle_, HID_NUM_INPUT_BUFFERS)) { freespace_private_forceCloseDevice(device); return FREESPACE_ERROR_NO_DEVICE; } // Create the read event. s->readOverlapped_.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (s->readOverlapped_.hEvent == NULL) { freespace_private_forceCloseDevice(device); return FREESPACE_ERROR_UNEXPECTED; } s->readOverlapped_.Offset = 0; s->readOverlapped_.OffsetHigh = 0; s->readStatus_ = FALSE; } device->isOpened_ = TRUE; // Enable send by initializing all send events. for (idx = 0; idx < FREESPACE_MAXIMUM_SEND_MESSAGE_COUNT; idx++) { device->send_[idx].overlapped_.hEvent = NULL; if (initializeSendStruct(&device->send_[idx]) != FREESPACE_SUCCESS) { freespace_private_forceCloseDevice(device); return FREESPACE_ERROR_UNEXPECTED; } } // If async mode has been enabled already, then start the receive // process going. if (freespace_instance_->fdAddedCallback_) { int rc; rc = initiateAsyncReceives(device); if (rc != FREESPACE_SUCCESS) { freespace_private_forceCloseDevice(device); return rc; } } return FREESPACE_SUCCESS; }