Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
hidapi API

Functions

int HID_API_EXPORT HID_API_CALL hid_init (void)
 Initialize the HIDAPI library.
 
int HID_API_EXPORT HID_API_CALL hid_exit (void)
 Finalize the HIDAPI library.
 
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate (unsigned short vendor_id, unsigned short product_id)
 Enumerate the HID Devices.
 
void HID_API_EXPORT HID_API_CALL hid_free_enumeration (struct hid_device_info *devs)
 Free an enumeration Linked List.
 
HID_API_EXPORT hid_device *HID_API_CALL hid_open (unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
 Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
 
HID_API_EXPORT hid_device *HID_API_CALL hid_open_path (const char *path)
 Open a HID device by its path name.
 
int HID_API_EXPORT HID_API_CALL hid_write (hid_device *dev, const unsigned char *data, size_t length)
 Write an Output report to a HID device.
 
int HID_API_EXPORT HID_API_CALL hid_read_timeout (hid_device *dev, unsigned char *data, size_t length, int milliseconds)
 Read an Input report from a HID device with timeout.
 
int HID_API_EXPORT HID_API_CALL hid_read (hid_device *dev, unsigned char *data, size_t length)
 Read an Input report from a HID device.
 
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking (hid_device *dev, int nonblock)
 Set the device handle to be non-blocking.
 
int HID_API_EXPORT HID_API_CALL hid_send_feature_report (hid_device *dev, const unsigned char *data, size_t length)
 Send a Feature report to the device.
 
int HID_API_EXPORT HID_API_CALL hid_get_feature_report (hid_device *dev, unsigned char *data, size_t length)
 Get a feature report from a HID device.
 
void HID_API_EXPORT HID_API_CALL hid_close (hid_device *dev)
 Close a HID device.
 
int HID_API_EXPORT_CALL hid_get_manufacturer_string (hid_device *dev, wchar_t *string, size_t maxlen)
 Get The Manufacturer String from a HID device.
 
int HID_API_EXPORT_CALL hid_get_product_string (hid_device *dev, wchar_t *string, size_t maxlen)
 Get The Product String from a HID device.
 
int HID_API_EXPORT_CALL hid_get_serial_number_string (hid_device *dev, wchar_t *string, size_t maxlen)
 Get The Serial Number String from a HID device.
 
int HID_API_EXPORT_CALL hid_get_indexed_string (hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
 Get a string from a HID device, based on its string index.
 
HID_API_EXPORT const wchar_t *HID_API_CALL hid_error (hid_device *dev)
 Get a string describing the last error which occurred.
 

Detailed Description

Function Documentation

◆ hid_close()

#include <src/hidapi/include/hidapi.h>

Close a HID device.

Parameters
devA device handle returned from hid_open().
892{
893 if (!dev)
894 return;
895 close(dev->device_handle);
896 free(dev);
897}
void free(void *)
int device_handle
Definition hid.c:202

References hid_device_::device_handle, hid_device_::disconnected, free(), free_hid_device(), hid_device_::input_report_buf, hid_device_::input_reports, hid_device_::max_input_report_len, hid_device_::mutex, pthread_barrier_wait(), return_data(), hid_device_::run_loop, hid_device_::run_loop_mode, hid_device_::shutdown_barrier, hid_device_::shutdown_thread, hid_device_::source, and hid_device_::thread.

Referenced by Slic3r::GUI::Mouse3DController::connect_device(), and Slic3r::GUI::Mouse3DController::disconnect_device().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_enumerate()

struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate ( unsigned short  vendor_id,
unsigned short  product_id 
)

#include <src/hidapi/include/hidapi.h>

Enumerate the HID Devices.

This function returns a linked list of all the HID devices attached to the system which match vendor_id and product_id. If vendor_id is set to 0 then any vendor matches. If product_id is set to 0 then any product matches. If vendor_id and product_id are both set to 0, then all HID devices will be returned.

Parameters
vendor_idThe Vendor ID (VID) of the types of device to open.
product_idThe Product ID (PID) of the types of device to open.
Returns
This function returns a pointer to a linked list of type struct hid_device_info, containing information about the HID devices attached to the system, or NULL in the case of failure. Free this linked list by calling hid_free_enumeration().
511{
512 struct udev *udev;
513 struct udev_enumerate *enumerate;
514 struct udev_list_entry *devices, *dev_list_entry;
515
516 struct hid_device_info *root = NULL; /* return object */
517 struct hid_device_info *cur_dev = NULL;
518 struct hid_device_info *prev_dev = NULL; /* previous device */
519
520 hid_init();
521
522 /* Create the udev object */
523 udev = hid_wrapper_udev_new();
524 if (!udev) {
525 printf("Can't create udev\n");
526 return NULL;
527 }
528
529 /* Create a list of the devices in the 'hidraw' subsystem. */
534 /* For each item, see if it matches the vid/pid, and if so
535 create a udev_device record for it */
536 for (dev_list_entry = devices; dev_list_entry; dev_list_entry = hid_wrapper_udev_list_entry_get_next(dev_list_entry)) {
537 const char *sysfs_path;
538 const char *dev_path;
539 const char *str;
540 struct udev_device *raw_dev; /* The device's hidraw udev node. */
541 struct udev_device *hid_dev; /* The device's HID udev node. */
542 struct udev_device *usb_dev; /* The device's USB udev node. */
543 struct udev_device *intf_dev; /* The device's interface (in the USB sense). */
544 unsigned short dev_vid;
545 unsigned short dev_pid;
546 char *serial_number_utf8 = NULL;
547 char *product_name_utf8 = NULL;
548 int bus_type;
549 int result;
550
551 /* Get the filename of the /sys entry for the device
552 and create a udev_device object (dev) representing it */
553 sysfs_path = hid_wrapper_udev_list_entry_get_name(dev_list_entry);
554 raw_dev = hid_wrapper_udev_device_new_from_syspath(udev, sysfs_path);
555 dev_path = hid_wrapper_udev_device_get_devnode(raw_dev);
556
558 raw_dev,
559 "hid",
560 NULL);
561
562 if (!hid_dev) {
563 /* Unable to find parent hid device. */
564 goto next;
565 }
566
567 result = parse_uevent_info(
569 &bus_type,
570 &dev_vid,
571 &dev_pid,
572 &serial_number_utf8,
573 &product_name_utf8);
574
575 if (!result) {
576 /* parse_uevent_info() failed for at least one field. */
577 goto next;
578 }
579
580 if (bus_type != BUS_USB && bus_type != BUS_BLUETOOTH) {
581 /* We only know how to handle USB and BT devices. */
582 goto next;
583 }
584
585 /* Check the VID/PID against the arguments */
586 if ((vendor_id == 0x0 || vendor_id == dev_vid) &&
587 (product_id == 0x0 || product_id == dev_pid)) {
588 struct hid_device_info *tmp;
589
590 /* VID/PID match. Create the record. */
591 tmp = malloc(sizeof(struct hid_device_info));
592 if (cur_dev) {
593 cur_dev->next = tmp;
594 }
595 else {
596 root = tmp;
597 }
598 prev_dev = cur_dev;
599 cur_dev = tmp;
600
601 /* Fill out the record */
602 cur_dev->next = NULL;
603 cur_dev->path = dev_path? strdup(dev_path): NULL;
604
605 /* VID/PID */
606 cur_dev->vendor_id = dev_vid;
607 cur_dev->product_id = dev_pid;
608
609 /* Serial Number */
610 cur_dev->serial_number = utf8_to_wchar_t(serial_number_utf8);
611
612 /* Release Number */
613 cur_dev->release_number = 0x0;
614
615 /* Interface Number */
616 cur_dev->interface_number = -1;
617
618 switch (bus_type) {
619 case BUS_USB:
620 /* The device pointed to by raw_dev contains information about
621 the hidraw device. In order to get information about the
622 USB device, get the parent device with the
623 subsystem/devtype pair of "usb"/"usb_device". This will
624 be several levels up the tree, but the function will find
625 it. */
627 raw_dev,
628 "usb",
629 "usb_device");
630
631 if (!usb_dev) {
632 /* Free this device */
633 free(cur_dev->serial_number);
634 free(cur_dev->path);
635 free(cur_dev);
636
637 /* Take it off the device list. */
638 if (prev_dev) {
639 prev_dev->next = NULL;
640 cur_dev = prev_dev;
641 }
642 else {
643 cur_dev = root = NULL;
644 }
645
646 goto next;
647 }
648
649 /* Manufacturer and Product strings */
652
653 /* Release Number */
654 str = hid_wrapper_udev_device_get_sysattr_value(usb_dev, "bcdDevice");
655 cur_dev->release_number = (str)? strtol(str, NULL, 16): 0x0;
656
657 /* Get a handle to the interface's udev node. */
659 raw_dev,
660 "usb",
661 "usb_interface");
662 if (intf_dev) {
663 str = hid_wrapper_udev_device_get_sysattr_value(intf_dev, "bInterfaceNumber");
664 cur_dev->interface_number = (str)? strtol(str, NULL, 16): -1;
665 }
666
667 break;
668
669 case BUS_BLUETOOTH:
670 /* Manufacturer and Product strings */
671 cur_dev->manufacturer_string = wcsdup(L"");
672 cur_dev->product_string = utf8_to_wchar_t(product_name_utf8);
673
674 break;
675
676 default:
677 /* Unknown device type - this should never happen, as we
678 * check for USB and Bluetooth devices above */
679 break;
680 }
681 }
682
683 next:
684 free(serial_number_utf8);
685 free(product_name_utf8);
687 /* hid_dev, usb_dev and intf_dev don't need to be (and can't be)
688 unref()d. It will cause a double-free() error. I'm not
689 sure why. */
690 }
691 /* Free the enumerator and udev objects. */
694
695 return root;
696}
void * malloc(YYSIZE_T)
int HID_API_EXPORT hid_init(void)
Initialize the HIDAPI library.
Definition hid.c:246
unsigned short product_id
Definition hidapi.h:55
struct hid_device_info * next
Definition hidapi.h:82
wchar_t * manufacturer_string
Definition hidapi.h:62
unsigned short vendor_id
Definition hidapi.h:53
char * path
Definition hidapi.h:51
unsigned short release_number
Definition hidapi.h:60
wchar_t * serial_number
Definition hidapi.h:57
int interface_number
Definition hidapi.h:79
wchar_t * product_string
Definition hidapi.h:64
Definition hidapi.h:49
const char * device_string_names[]
Definition hid.c:186
static hid_wrapper_udev_enumerate_add_match_subsystem_type hid_wrapper_udev_enumerate_add_match_subsystem
Definition hid.c:78
static hid_wrapper_udev_unref_type hid_wrapper_udev_unref
Definition hid.c:86
static hid_wrapper_udev_enumerate_unref_type hid_wrapper_udev_enumerate_unref
Definition hid.c:82
static wchar_t * utf8_to_wchar_t(const char *utf8)
Definition hid.c:243
static hid_wrapper_udev_device_new_from_syspath_type hid_wrapper_udev_device_new_from_syspath
Definition hid.c:76
static hid_wrapper_udev_new_type hid_wrapper_udev_new
Definition hid.c:85
static hid_wrapper_udev_enumerate_get_list_entry_type hid_wrapper_udev_enumerate_get_list_entry
Definition hid.c:79
static hid_wrapper_udev_list_entry_get_name_type hid_wrapper_udev_list_entry_get_name
Definition hid.c:83
static hid_wrapper_udev_enumerate_scan_devices_type hid_wrapper_udev_enumerate_scan_devices
Definition hid.c:81
static hid_wrapper_udev_list_entry_get_next_type hid_wrapper_udev_list_entry_get_next
Definition hid.c:84
static int parse_uevent_info(const char *uevent, int *bus_type, unsigned short *vendor_id, unsigned short *product_id, char **serial_number_utf8, char **product_name_utf8)
Definition hid.c:334
static hid_wrapper_udev_device_get_sysattr_value_type hid_wrapper_udev_device_get_sysattr_value
Definition hid.c:74
static wchar_t * copy_udev_string(struct udev_device *dev, const char *udev_name)
Definition hid.c:262
static hid_wrapper_udev_enumerate_new_type hid_wrapper_udev_enumerate_new
Definition hid.c:80
static hid_wrapper_udev_device_get_parent_with_subsystem_devtype_type hid_wrapper_udev_device_get_parent_with_subsystem_devtype
Definition hid.c:73
@ DEVICE_STRING_MANUFACTURER
Definition hid.c:194
@ DEVICE_STRING_PRODUCT
Definition hid.c:195
static hid_wrapper_udev_device_get_devnode_type hid_wrapper_udev_device_get_devnode
Definition hid.c:72
static hid_wrapper_udev_device_unref_type hid_wrapper_udev_device_unref
Definition hid.c:77
void enumerate(Iterator from, Iterator to, std::function< void(TIteratorValue< Iterator >, size_t)> fn, std::launch policy=std::launch::deferred|std::launch::async)
Definition parallel.hpp:22
#define L(s)
Definition I18N.hpp:18

References BUF_LEN, copy_udev_string(), DEVICE_STRING_MANUFACTURER, device_string_names, DEVICE_STRING_PRODUCT, dup_wcs(), FALSE, free(), get_int_property(), get_manufacturer_string(), get_product_id(), get_product_string(), get_serial_number(), get_vendor_id(), hid_init(), hid_mgr, hid_wrapper_udev_device_get_devnode, hid_wrapper_udev_device_get_parent_with_subsystem_devtype, hid_wrapper_udev_device_get_sysattr_value, hid_wrapper_udev_device_new_from_syspath, hid_wrapper_udev_device_unref, hid_wrapper_udev_enumerate_add_match_subsystem, hid_wrapper_udev_enumerate_get_list_entry, hid_wrapper_udev_enumerate_new, hid_wrapper_udev_enumerate_scan_devices, hid_wrapper_udev_enumerate_unref, hid_wrapper_udev_list_entry_get_name, hid_wrapper_udev_list_entry_get_next, hid_wrapper_udev_new, hid_wrapper_udev_unref, hidapi_IOHIDDeviceGetService(), HidD_FreePreparsedData, HidD_GetAttributes, HidD_GetManufacturerString, HidD_GetPreparsedData, HidD_GetProductString, HidD_GetSerialNumberString, HidP_GetCaps, HIDP_STATUS_SUCCESS, hid_device_info::interface_number, L, malloc(), hid_device_info::manufacturer_string, hid_device_info::next, open_device(), parse_uevent_info(), hid_device_info::path, process_pending_events(), hid_device_info::product_id, hid_device_info::product_string, HIDD_ATTRIBUTES::ProductID, hid_device_info::release_number, hid_device_info::serial_number, HIDD_ATTRIBUTES::Size, hid_device_info::usage, _HIDP_CAPS::Usage, hid_device_info::usage_page, _HIDP_CAPS::UsagePage, utf8_to_wchar_t(), hid_device_info::vendor_id, HIDD_ATTRIBUTES::VendorID, HIDD_ATTRIBUTES::VersionNumber, and WSTR_LEN.

Referenced by Slic3r::GUI::Mouse3DController::connect_device(), and hid_open().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_error()

HID_API_EXPORT const wchar_t *HID_API_CALL hid_error ( hid_device dev)

#include <src/hidapi/include/hidapi.h>

Get a string describing the last error which occurred.

Parameters
devA device handle returned from hid_open().
Returns
This function returns a string containing the last error which occurred or NULL if none has occurred.
922{
923 return NULL;
924}

References hid_device_::last_error_str.

◆ hid_exit()

int HID_API_EXPORT HID_API_CALL hid_exit ( void  )

#include <src/hidapi/include/hidapi.h>

Finalize the HIDAPI library.

This function frees all of the static data associated with HIDAPI. It should be called at the end of execution to avoid memory leaks.

Returns
This function returns 0 on success and -1 on error.
504{
505 /* Nothing to do for this in the Linux/hidraw implementation. */
507 return 0;
508}
static void hid_wrapper_udev_close()
Definition hid.c:88

References FALSE, hid_mgr, hid_wrapper_udev_close(), initialized, and lib_handle.

Referenced by hid_init(), and Slic3r::GUI::Mouse3DController::run().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_free_enumeration()

void HID_API_EXPORT HID_API_CALL hid_free_enumeration ( struct hid_device_info devs)

#include <src/hidapi/include/hidapi.h>

Free an enumeration Linked List.

This function frees a linked list created by hid_enumerate().

Parameters
devsPointer to a list of struct_device returned from hid_enumerate().
699{
700 struct hid_device_info *d = devs;
701 while (d) {
702 struct hid_device_info *next = d->next;
703 free(d->path);
704 free(d->serial_number);
705 free(d->manufacturer_string);
706 free(d->product_string);
707 free(d);
708 d = next;
709 }
710}

References free(), and hid_device_info::next.

Referenced by Slic3r::GUI::Mouse3DController::connect_device(), and hid_open().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_get_feature_report()

int HID_API_EXPORT HID_API_CALL hid_get_feature_report ( hid_device dev,
unsigned char *  data,
size_t  length 
)

#include <src/hidapi/include/hidapi.h>

Get a feature report from a HID device.

Set the first byte of data[] to the Report ID of the report to be read. Make sure to allow space for this extra byte in data[]. Upon return, the first byte will still contain the Report ID, and the report data will start in data[1].

Parameters
devA device handle returned from hid_open().
dataA buffer to put the read data into, including the Report ID. Set the first byte of data[] to the Report ID of the report to be read, or set it to zero if your device does not use numbered reports.
lengthThe number of bytes to read, including an extra byte for the report ID. The buffer can be longer than the actual report.
Returns
This function returns the number of bytes read plus one for the report ID (which is still in the first byte), or -1 on error.
879{
880 int res;
881
882 res = ioctl(dev->device_handle, HIDIOCGFEATURE(length), data);
883 if (res < 0)
884 perror("ioctl (GFEATURE)");
885
886
887 return res;
888}
#define HIDIOCGFEATURE(len)
Definition hid.c:181

References hid_device_::device_handle, hid_device_::disconnected, HidD_GetFeature, HIDIOCGFEATURE, IOCTL_HID_GET_FEATURE, register_error(), and TRUE.

+ Here is the call graph for this function:

◆ hid_get_indexed_string()

int HID_API_EXPORT_CALL hid_get_indexed_string ( hid_device dev,
int  string_index,
wchar_t *  string,
size_t  maxlen 
)

#include <src/hidapi/include/hidapi.h>

Get a string from a HID device, based on its string index.

Parameters
devA device handle returned from hid_open().
string_indexThe index of the string to get.
stringA wide string buffer to put the data into.
maxlenThe length of the buffer in multiples of wchar_t.
Returns
This function returns 0 on success and -1 on error.
916{
917 return -1;
918}

References hid_device_::device_handle, HidD_GetIndexedString, MAX_STRING_WCHARS, MIN, and register_error().

+ Here is the call graph for this function:

◆ hid_get_manufacturer_string()

int HID_API_EXPORT_CALL hid_get_manufacturer_string ( hid_device dev,
wchar_t *  string,
size_t  maxlen 
)

#include <src/hidapi/include/hidapi.h>

Get The Manufacturer String from a HID device.

Parameters
devA device handle returned from hid_open().
stringA wide string buffer to put the data into.
maxlenThe length of the buffer in multiples of wchar_t.
Returns
This function returns 0 on success and -1 on error.
901{
902 return get_device_string(dev, DEVICE_STRING_MANUFACTURER, string, maxlen);
903}
static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t *string, size_t maxlen)
Definition hid.c:387

References hid_device_::device_handle, DEVICE_STRING_MANUFACTURER, get_device_string(), get_manufacturer_string(), HidD_GetManufacturerString, MAX_STRING_WCHARS, MIN, and register_error().

Referenced by Slic3r::GUI::Mouse3DController::connect_device().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_get_product_string()

int HID_API_EXPORT_CALL hid_get_product_string ( hid_device dev,
wchar_t *  string,
size_t  maxlen 
)

#include <src/hidapi/include/hidapi.h>

Get The Product String from a HID device.

Parameters
devA device handle returned from hid_open().
stringA wide string buffer to put the data into.
maxlenThe length of the buffer in multiples of wchar_t.
Returns
This function returns 0 on success and -1 on error.
906{
907 return get_device_string(dev, DEVICE_STRING_PRODUCT, string, maxlen);
908}

References hid_device_::device_handle, DEVICE_STRING_PRODUCT, get_device_string(), get_product_string(), HidD_GetProductString, MAX_STRING_WCHARS, MIN, and register_error().

Referenced by Slic3r::GUI::Mouse3DController::connect_device().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_get_serial_number_string()

int HID_API_EXPORT_CALL hid_get_serial_number_string ( hid_device dev,
wchar_t *  string,
size_t  maxlen 
)

#include <src/hidapi/include/hidapi.h>

Get The Serial Number String from a HID device.

Parameters
devA device handle returned from hid_open().
stringA wide string buffer to put the data into.
maxlenThe length of the buffer in multiples of wchar_t.
Returns
This function returns 0 on success and -1 on error.
911{
912 return get_device_string(dev, DEVICE_STRING_SERIAL, string, maxlen);
913}
@ DEVICE_STRING_SERIAL
Definition hid.c:196

References hid_device_::device_handle, DEVICE_STRING_SERIAL, get_device_string(), get_serial_number(), HidD_GetSerialNumberString, MAX_STRING_WCHARS, MIN, and register_error().

+ Here is the call graph for this function:

◆ hid_init()

int HID_API_EXPORT HID_API_CALL hid_init ( void  )

#include <src/hidapi/include/hidapi.h>

Initialize the HIDAPI library.

This function initializes the HIDAPI library. Calling it is not strictly necessary, as it will be called automatically by hid_enumerate() and any of the hid_open_*() functions if it is needed. This function should be called at the beginning of execution however, if there is a chance of HIDAPI handles being opened by different threads simultaneously.

Returns
This function returns 0 on success and -1 on error.
490{
491 const char *locale;
492
493 /* Set the locale if it's not set. */
494 locale = setlocale(LC_CTYPE, NULL);
495 if (!locale)
496 setlocale(LC_CTYPE, "");
497
499
500 return hid_wrapper_udev_init();
501}
static __u32 detect_kernel_version(void)
Definition hid.c:210
static int hid_wrapper_udev_init()
Definition hid.c:114
static __u32 kernel_version
Definition hid.c:208

References detect_kernel_version(), hid_exit(), hid_mgr, hid_wrapper_udev_init(), init_hid_manager(), initialized, kernel_version, lookup_functions(), and TRUE.

Referenced by hid_enumerate(), hid_open_path(), and Slic3r::GUI::Mouse3DController::run().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_open()

HID_API_EXPORT hid_device *HID_API_CALL hid_open ( unsigned short  vendor_id,
unsigned short  product_id,
const wchar_t *  serial_number 
)

#include <src/hidapi/include/hidapi.h>

Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.

If serial_number is NULL, the first device with the specified VID and PID is opened.

Parameters
vendor_idThe Vendor ID (VID) of the device to open.
product_idThe Product ID (PID) of the device to open.
serial_numberThe Serial Number of the device to open (Optionally NULL).
Returns
This function returns a pointer to a hid_device object on success or NULL on failure.
713{
714 struct hid_device_info *devs, *cur_dev;
715 const char *path_to_open = NULL;
716 hid_device *handle = NULL;
717
719 cur_dev = devs;
720 while (cur_dev) {
721 if (cur_dev->vendor_id == vendor_id &&
722 cur_dev->product_id == product_id) {
723 if (serial_number) {
724 if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
725 path_to_open = cur_dev->path;
726 break;
727 }
728 }
729 else {
730 path_to_open = cur_dev->path;
731 break;
732 }
733 }
734 cur_dev = cur_dev->next;
735 }
736
737 if (path_to_open) {
738 /* Open the device */
739 handle = hid_open_path(path_to_open);
740 }
741
743
744 return handle;
745}
HID_API_EXPORT hid_device *HID_API_CALL hid_open_path(const char *path)
Open a HID device by its path name.
Definition hid.c:557
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition hid.c:271
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
Definition hid.c:505
Definition hid.c:201

References hid_enumerate(), hid_free_enumeration(), hid_open_path(), hid_device_info::next, hid_device_info::path, hid_device_info::product_id, hid_device_info::serial_number, and hid_device_info::vendor_id.

Referenced by Slic3r::GUI::Mouse3DController::connect_device().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_open_path()

HID_API_EXPORT hid_device *HID_API_CALL hid_open_path ( const char *  path)

#include <src/hidapi/include/hidapi.h>

Open a HID device by its path name.

The path name be determined by calling hid_enumerate(), or a platform-specific path name can be used (eg: /dev/hidraw0 on Linux).

Parameters
pathThe path name of the device to open
Returns
This function returns a pointer to a hid_device object on success or NULL on failure.
748{
749 hid_device *dev = NULL;
750
751 hid_init();
752
753 dev = new_hid_device();
754
755 /* OPEN HERE */
756 dev->device_handle = open(path, O_RDWR);
757
758 /* If we have a good handle, return it. */
759 if (dev->device_handle > 0) {
760
761 /* Get the report descriptor */
762 int res, desc_size = 0;
763 struct hidraw_report_descriptor rpt_desc;
764
765 memset(&rpt_desc, 0x0, sizeof(rpt_desc));
766
767 /* Get Report Descriptor Size */
768 res = ioctl(dev->device_handle, HIDIOCGRDESCSIZE, &desc_size);
769 if (res < 0)
770 perror("HIDIOCGRDESCSIZE");
771
772
773 /* Get Report Descriptor */
774 rpt_desc.size = desc_size;
775 res = ioctl(dev->device_handle, HIDIOCGRDESC, &rpt_desc);
776 if (res < 0) {
777 perror("HIDIOCGRDESC");
778 } else {
779 /* Determine if this device uses numbered reports. */
781 uses_numbered_reports(rpt_desc.value,
782 rpt_desc.size);
783 }
784
785 return dev;
786 }
787 else {
788 /* Unable to open any devices. */
789 free(dev);
790 return NULL;
791 }
792}
static int uses_numbered_reports(__u8 *report_descriptor, __u32 size)
Definition hid.c:269
int uses_numbered_reports
Definition hid.c:204
static hid_device * new_hid_device()
Definition hid.c:146

References hid_device_::barrier, hid_device_::device_handle, FALSE, free(), free_hid_device(), get_max_report_length(), hid_device_removal_callback(), hid_init(), hid_report_callback(), HidD_FreePreparsedData, HidD_GetPreparsedData, HidD_SetNumInputBuffers, HidP_GetCaps, HIDP_STATUS_SUCCESS, hid_device_::input_report_buf, hid_device_::input_report_length, _HIDP_CAPS::InputReportByteLength, malloc(), hid_device_::max_input_report_len, new_hid_device(), open_device(), hid_device_::output_report_length, _HIDP_CAPS::OutputReportByteLength, hid_device_info::path, pthread_barrier_wait(), hid_device_::read_buf, read_thread(), register_error(), hid_device_::run_loop_mode, hid_device_::thread, TRUE, hid_device_::uses_numbered_reports, and uses_numbered_reports().

Referenced by Slic3r::GUI::Mouse3DController::connect_device(), and hid_open().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_read()

int HID_API_EXPORT HID_API_CALL hid_read ( hid_device dev,
unsigned char *  data,
size_t  length 
)

#include <src/hidapi/include/hidapi.h>

Read an Input report from a HID device.

Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports.

Parameters
devA device handle returned from hid_open().
dataA buffer to put the read data into.
lengthThe number of bytes to read. For devices with multiple reports, make sure to read an extra byte for the report number.
Returns
This function returns the actual number of bytes read and -1 on error. If no packet was available to be read and the handle is in non-blocking mode, this function returns 0.
852{
853 return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
854}
int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
Read an Input report from a HID device with timeout.
Definition hid.c:680
int blocking
Definition hid.c:203

References hid_device_::blocking, and hid_read_timeout().

+ Here is the call graph for this function:

◆ hid_read_timeout()

int HID_API_EXPORT HID_API_CALL hid_read_timeout ( hid_device dev,
unsigned char *  data,
size_t  length,
int  milliseconds 
)

#include <src/hidapi/include/hidapi.h>

Read an Input report from a HID device with timeout.

Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports.

Parameters
devA device handle returned from hid_open().
dataA buffer to put the read data into.
lengthThe number of bytes to read. For devices with multiple reports, make sure to read an extra byte for the report number.
millisecondstimeout in milliseconds or -1 for blocking wait.
Returns
This function returns the actual number of bytes read and -1 on error. If no packet was available to be read within the timeout period, this function returns 0.
806{
807 int bytes_read;
808
809 if (milliseconds >= 0) {
810 /* Milliseconds is either 0 (non-blocking) or > 0 (contains
811 a valid timeout). In both cases we want to call poll()
812 and wait for data to arrive. Don't rely on non-blocking
813 operation (O_NONBLOCK) since some kernels don't seem to
814 properly report device disconnection through read() when
815 in non-blocking mode. */
816 int ret;
817 struct pollfd fds;
818
819 fds.fd = dev->device_handle;
820 fds.events = POLLIN;
821 fds.revents = 0;
822 ret = poll(&fds, 1, milliseconds);
823 if (ret == -1 || ret == 0) {
824 /* Error or timeout */
825 return ret;
826 }
827 else {
828 /* Check for errors on the file descriptor. This will
829 indicate a device disconnection. */
830 if (fds.revents & (POLLERR | POLLHUP | POLLNVAL))
831 return -1;
832 }
833 }
834
835 bytes_read = read(dev->device_handle, data, length);
836 if (bytes_read < 0 && (errno == EAGAIN || errno == EINPROGRESS))
837 bytes_read = 0;
838
839 if (bytes_read >= 0 &&
840 kernel_version != 0 &&
841 kernel_version < KERNEL_VERSION(2,6,34) &&
843 /* Work around a kernel bug. Chop off the first byte. */
844 memmove(data, data+1, bytes_read);
845 bytes_read--;
846 }
847
848 return bytes_read;
849}

References cond_timedwait(), cond_wait(), hid_device_::condition, input_report::data, hid_device_::device_handle, hid_device_::disconnected, FALSE, gettimeofday(), hid_device_::input_report_length, hid_device_::input_reports, kernel_version, hid_device_::mutex, hid_device_::ol, hid_device_::read_buf, hid_device_::read_pending, register_error(), return_data(), hid_device_::shutdown_thread, TRUE, and hid_device_::uses_numbered_reports.

Referenced by Slic3r::GUI::Mouse3DController::collect_input(), and hid_read().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hid_send_feature_report()

int HID_API_EXPORT HID_API_CALL hid_send_feature_report ( hid_device dev,
const unsigned char *  data,
size_t  length 
)

#include <src/hidapi/include/hidapi.h>

Send a Feature report to the device.

Feature reports are sent over the Control endpoint as a Set_Report transfer. The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data. Since the Report ID is mandatory, calls to hid_send_feature_report() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_send_feature_report(): the Report ID (or 0x0, for devices which do not use numbered reports), followed by the report data (16 bytes). In this example, the length passed in would be 17.

Parameters
devA device handle returned from hid_open().
dataThe data to send, including the report number as the first byte.
lengthThe length in bytes of the data to send, including the report number.
Returns
This function returns the actual number of bytes written and -1 on error.
868{
869 int res;
870
871 res = ioctl(dev->device_handle, HIDIOCSFEATURE(length), data);
872 if (res < 0)
873 perror("ioctl (SFEATURE)");
874
875 return res;
876}
#define HIDIOCSFEATURE(len)
Definition hid.c:178

References hid_device_::device_handle, HidD_SetFeature, HIDIOCSFEATURE, register_error(), and set_report().

+ Here is the call graph for this function:

◆ hid_set_nonblocking()

int HID_API_EXPORT HID_API_CALL hid_set_nonblocking ( hid_device dev,
int  nonblock 
)

#include <src/hidapi/include/hidapi.h>

Set the device handle to be non-blocking.

In non-blocking mode calls to hid_read() will return immediately with a value of 0 if there is no data to be read. In blocking mode, hid_read() will wait (block) until there is data to read before returning.

Nonblocking can be turned on and off at any time.

Parameters
devA device handle returned from hid_open().
nonblockenable or not the nonblocking reads
  • 1 to enable nonblocking
  • 0 to disable nonblocking.
Returns
This function returns 0 on success and -1 on error.
857{
858 /* Do all non-blocking in userspace using poll(), since it looks
859 like there's a bug in the kernel in some versions where
860 read() will not return -1 on disconnection of the USB device */
861
862 dev->blocking = !nonblock;
863 return 0; /* Success */
864}

References hid_device_::blocking.

◆ hid_write()

int HID_API_EXPORT HID_API_CALL hid_write ( hid_device dev,
const unsigned char *  data,
size_t  length 
)

#include <src/hidapi/include/hidapi.h>

Write an Output report to a HID device.

The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data. Since the Report ID is mandatory, calls to hid_write() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_write(), the Report ID (or 0x0, for devices with a single report), followed by the report data (16 bytes). In this example, the length passed in would be 17.

hid_write() will send the data on the first OUT endpoint, if one exists. If it does not, it will send the data through the Control Endpoint (Endpoint 0).

Parameters
devA device handle returned from hid_open().
dataThe data to send, including the report number as the first byte.
lengthThe length in bytes of the data to send.
Returns
This function returns the actual number of bytes written and -1 on error.
796{
797 int bytes_written;
798
799 bytes_written = write(dev->device_handle, data, length);
800
801 return bytes_written;
802}

References input_report::data, hid_device_::device_handle, free(), malloc(), hid_device_::output_report_length, register_error(), set_report(), and TRUE.

+ Here is the call graph for this function: