/*!@brief Initialize libhid: scan for USB busses and devices using libusb. * * Call this routine before making any other libhid calls. * * @return HID_RET_SUCCESS if everything was properly initialized. */ hid_return hid_init() { if (hid_is_initialised()) { ERROR("cannot initialised already initialised HID library"); return HID_RET_ALREADY_INITIALISED; } /* Include version to make deciphering logfiles easier: */ NOTICE(PACKAGE_STRING " is being initialized."); TRACE("initialising USB subsystem..."); usb_init(); TRACE("scanning for USB busses..."); if (usb_find_busses() < 0) { ERROR("failed to scan for USB busses"); return HID_RET_FAIL_FIND_BUSSES; } TRACE("scanning for USB devices..."); if (usb_find_devices() < 0) { ERROR("failed to scan for USB devices"); return HID_RET_FAIL_FIND_DEVICES; } initialised = true; NOTICE("successfully initialised HID library."); return HID_RET_SUCCESS; }
/*!@brief Complement to hid_init(): cleans up after libhid. */ hid_return hid_cleanup() { if (!hid_is_initialised()) { ERROR("cannot cleanup uninitialised HID library."); return HID_RET_NOT_INITIALISED; } initialised = false; NOTICE("successfully deinitialised HID library."); return HID_RET_SUCCESS; }
static t_int init_libhid(t_usbhid *x) { if (! hid_is_initialised() ) { x->x_hid_return = hid_init(); if(x->x_hid_return != HID_RET_SUCCESS) { error("[usbhid] hid_init failed with return code %d\n", x->x_hid_return); } } return(x->x_hid_return); }
/*!@brief Retrieve a numeric input item * * @param[in] hidif Which interface to send to * @param[in] path Path to input item * @param[in] depth See hid_find_object() * @param[out] value Result from hid_extract_value() * * @todo Handle exponent and unit conversion (separate library?) */ hid_return hid_get_item_value(HIDInterface* const hidif, int const path[], unsigned int const depth, double *const value) { ASSERT(hid_is_initialised()); ASSERT(hid_is_opened(hidif)); unsigned int size; unsigned char buffer[32]; /*! @todo Dynamically allocate the item buffer */ if (!hid_is_opened(hidif)) { WARNING("the device has not been opened."); return HID_RET_DEVICE_NOT_OPENED; } TRACE("retrieving report from USB device %s...", hidif->id); hidif->hid_data->Type = ITEM_FEATURE; hidif->hid_data->ReportID = 0; /* TODO: i think this and the buffer stuff should be passed in */ hid_find_object(hidif, path, depth); hid_get_report_size(hidif, hidif->hid_data->ReportID, hidif->hid_data->Type, &size); ASSERT(size <= 32); /* remove when buffer situation is fixed. */ int len = usb_control_msg(hidif->dev_handle, USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE, HID_REPORT_GET, hidif->hid_data->ReportID + (HID_RT_FEATURE << 8), hidif->interface, (char*)buffer, size, USB_TIMEOUT); if (len < 0) { WARNING("failed to retrieve report from USB device %s:%s.", hidif->id,usb_strerror()); return HID_RET_FAIL_GET_REPORT; } if ((unsigned)len != size) { WARNING("failed to retrieve complete report from USB device %s; " "requested: %d bytes, got: %d bytes.", hidif->id, size, len); return HID_RET_FAIL_GET_REPORT; } if (hid_extract_value(hidif, buffer, value) != HID_RET_SUCCESS) { return HID_RET_FAIL_GET_REPORT; } NOTICE("successfully retrieved report from USB device %s.", hidif->id); return HID_RET_SUCCESS; }
/*!@brief Send an entire output report to the device * * This routine uses a control message to send the report. To use an interrupt * endpoint, use hid_interrupt_write(). * * @param[in] hidif Which interface to send to * @param[in] path Path to an output item (to find Report ID) * @param[in] depth See hid_find_object() * @param[in] buffer Output Report * @param[in] size How many bytes to send */ hid_return hid_set_output_report(HIDInterface* const hidif, int const path[], unsigned int const depth, char const* const buffer, unsigned int const size) { ASSERT(hid_is_initialised()); ASSERT(hid_is_opened(hidif)); ASSERT(buffer); if (!buffer) return HID_RET_INVALID_PARAMETER; if (!hid_is_opened(hidif)) { WARNING("the device has not been opened."); return HID_RET_DEVICE_NOT_OPENED; } TRACE("looking up report ID..."); hidif->hid_data->Type = ITEM_OUTPUT; hidif->hid_data->ReportID = 0; hid_find_object(hidif, path, depth); TRACE("sending report ID 0x%02x (length: %d) to USB device %s...", hidif->hid_data->ReportID, size, hidif->id); int len = usb_control_msg(hidif->dev_handle, USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE, HID_REPORT_SET, hidif->hid_data->ReportID + (HID_RT_OUTPUT << 8), hidif->interface, (char*)buffer, size, USB_TIMEOUT); if (len < 0) { WARNING("failed to send report to USB device %s:%s.", hidif->id,usb_strerror()); return HID_RET_FAIL_SET_REPORT; } if (len != (signed)size) { WARNING("failed to send complete report to USB device %s; " "requested: %d bytes, sent: %d bytes.", hidif->id, size, len); return HID_RET_FAIL_SET_REPORT; } NOTICE("successfully sent report to USB device %s.", hidif->id); return HID_RET_SUCCESS; }