Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
hid.c File Reference
#include <windows.h>
#include <setupapi.h>
#include <winioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include "hidapi.h"
+ Include dependency graph for hid.c:

Go to the source code of this file.

Classes

struct  _HIDD_ATTRIBUTES
 
struct  _HIDP_CAPS
 
struct  hid_device_
 

Macros

#define MAX_STRING_WCHARS   0xFFF
 
#define HID_OUT_CTL_CODE(id)    CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
 
#define IOCTL_HID_GET_FEATURE   HID_OUT_CTL_CODE(100)
 
#define MIN(x, y)   ((x) < (y)? (x): (y))
 
#define HIDP_STATUS_SUCCESS   0x110000
 
#define RESOLVE(x)   x = (x##_)GetProcAddress(lib_handle, #x); if (!x) return -1;
 
#define WSTR_LEN   512
 
#define P32
 

Typedefs

typedef LONG NTSTATUS
 
typedef struct _HIDD_ATTRIBUTES HIDD_ATTRIBUTES
 
typedef struct _HIDD_ATTRIBUTESPHIDD_ATTRIBUTES
 
typedef USHORT USAGE
 
typedef struct _HIDP_CAPS HIDP_CAPS
 
typedef struct _HIDP_CAPSPHIDP_CAPS
 
typedef voidPHIDP_PREPARSED_DATA
 
typedef BOOLEAN(__stdcall * HidD_GetAttributes_) (HANDLE device, PHIDD_ATTRIBUTES attrib)
 
typedef BOOLEAN(__stdcall * HidD_GetSerialNumberString_) (HANDLE device, PVOID buffer, ULONG buffer_len)
 
typedef BOOLEAN(__stdcall * HidD_GetManufacturerString_) (HANDLE handle, PVOID buffer, ULONG buffer_len)
 
typedef BOOLEAN(__stdcall * HidD_GetProductString_) (HANDLE handle, PVOID buffer, ULONG buffer_len)
 
typedef BOOLEAN(__stdcall * HidD_SetFeature_) (HANDLE handle, PVOID data, ULONG length)
 
typedef BOOLEAN(__stdcall * HidD_GetFeature_) (HANDLE handle, PVOID data, ULONG length)
 
typedef BOOLEAN(__stdcall * HidD_GetIndexedString_) (HANDLE handle, ULONG string_index, PVOID buffer, ULONG buffer_len)
 
typedef BOOLEAN(__stdcall * HidD_GetPreparsedData_) (HANDLE handle, PHIDP_PREPARSED_DATA *preparsed_data)
 
typedef BOOLEAN(__stdcall * HidD_FreePreparsedData_) (PHIDP_PREPARSED_DATA preparsed_data)
 
typedef NTSTATUS(__stdcall * HidP_GetCaps_) (PHIDP_PREPARSED_DATA preparsed_data, HIDP_CAPS *caps)
 
typedef BOOLEAN(__stdcall * HidD_SetNumInputBuffers_) (HANDLE handle, ULONG number_buffers)
 

Functions

static hid_devicenew_hid_device ()
 
static void free_hid_device (hid_device *dev)
 
static void register_error (hid_device *dev, const char *op)
 
static int lookup_functions ()
 
static HANDLE open_device (const char *path, BOOL open_rw)
 
int HID_API_EXPORT hid_init (void)
 Initialize the HIDAPI library.
 
int HID_API_EXPORT 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_API_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_API_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_API_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_API_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.
 

Variables

static HidD_GetAttributes_ HidD_GetAttributes
 
static HidD_GetSerialNumberString_ HidD_GetSerialNumberString
 
static HidD_GetManufacturerString_ HidD_GetManufacturerString
 
static HidD_GetProductString_ HidD_GetProductString
 
static HidD_SetFeature_ HidD_SetFeature
 
static HidD_GetFeature_ HidD_GetFeature
 
static HidD_GetIndexedString_ HidD_GetIndexedString
 
static HidD_GetPreparsedData_ HidD_GetPreparsedData
 
static HidD_FreePreparsedData_ HidD_FreePreparsedData
 
static HidP_GetCaps_ HidP_GetCaps
 
static HidD_SetNumInputBuffers_ HidD_SetNumInputBuffers
 
static HMODULE lib_handle = NULL
 
static BOOLEAN initialized = FALSE
 
unsigned short VendorID = 0x04d8
 
unsigned short ProductID = 0x3f
 

Class Documentation

◆ _HIDD_ATTRIBUTES

struct _HIDD_ATTRIBUTES
Class Members
USHORT ProductID
ULONG Size
USHORT VendorID
USHORT VersionNumber

◆ _HIDP_CAPS

struct _HIDP_CAPS
Class Members
USHORT FeatureReportByteLength
USHORT fields_not_used_by_hidapi[10]
USHORT InputReportByteLength
USHORT OutputReportByteLength
USHORT Reserved[17]
USAGE Usage
USAGE UsagePage

◆ hid_device_

struct hid_device_
+ Collaboration diagram for hid_device_:
Class Members
pthread_barrier_t barrier
int blocking
BOOL blocking
pthread_cond_t condition
int device_handle
IOHIDDeviceRef device_handle
HANDLE device_handle
int disconnected
uint8_t * input_report_buf
size_t input_report_length
struct input_report * input_reports
DWORD last_error_num
void * last_error_str
CFIndex max_input_report_len
pthread_mutex_t mutex
OVERLAPPED ol
USHORT output_report_length
char * read_buf
BOOL read_pending
CFRunLoopRef run_loop
CFStringRef run_loop_mode
pthread_barrier_t shutdown_barrier
int shutdown_thread
CFRunLoopSourceRef source
pthread_t thread
int uses_numbered_reports

Macro Definition Documentation

◆ HID_OUT_CTL_CODE

#define HID_OUT_CTL_CODE (   id)     CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)

◆ HIDP_STATUS_SUCCESS

#define HIDP_STATUS_SUCCESS   0x110000

◆ IOCTL_HID_GET_FEATURE

#define IOCTL_HID_GET_FEATURE   HID_OUT_CTL_CODE(100)

◆ MAX_STRING_WCHARS

#define MAX_STRING_WCHARS   0xFFF

◆ MIN

#define MIN (   x,
 
)    ((x) < (y)? (x): (y))

◆ P32

#define P32

◆ RESOLVE

#define RESOLVE (   x)    x = (x##_)GetProcAddress(lib_handle, #x); if (!x) return -1;

◆ WSTR_LEN

#define WSTR_LEN   512

Typedef Documentation

◆ HIDD_ATTRIBUTES

◆ HidD_FreePreparsedData_

typedef BOOLEAN(__stdcall * HidD_FreePreparsedData_) (PHIDP_PREPARSED_DATA preparsed_data)

◆ HidD_GetAttributes_

typedef BOOLEAN(__stdcall * HidD_GetAttributes_) (HANDLE device, PHIDD_ATTRIBUTES attrib)

◆ HidD_GetFeature_

typedef BOOLEAN(__stdcall * HidD_GetFeature_) (HANDLE handle, PVOID data, ULONG length)

◆ HidD_GetIndexedString_

typedef BOOLEAN(__stdcall * HidD_GetIndexedString_) (HANDLE handle, ULONG string_index, PVOID buffer, ULONG buffer_len)

◆ HidD_GetManufacturerString_

typedef BOOLEAN(__stdcall * HidD_GetManufacturerString_) (HANDLE handle, PVOID buffer, ULONG buffer_len)

◆ HidD_GetPreparsedData_

typedef BOOLEAN(__stdcall * HidD_GetPreparsedData_) (HANDLE handle, PHIDP_PREPARSED_DATA *preparsed_data)

◆ HidD_GetProductString_

typedef BOOLEAN(__stdcall * HidD_GetProductString_) (HANDLE handle, PVOID buffer, ULONG buffer_len)

◆ HidD_GetSerialNumberString_

typedef BOOLEAN(__stdcall * HidD_GetSerialNumberString_) (HANDLE device, PVOID buffer, ULONG buffer_len)

◆ HidD_SetFeature_

typedef BOOLEAN(__stdcall * HidD_SetFeature_) (HANDLE handle, PVOID data, ULONG length)

◆ HidD_SetNumInputBuffers_

typedef BOOLEAN(__stdcall * HidD_SetNumInputBuffers_) (HANDLE handle, ULONG number_buffers)

◆ HIDP_CAPS

typedef struct _HIDP_CAPS HIDP_CAPS

◆ HidP_GetCaps_

typedef NTSTATUS(__stdcall * HidP_GetCaps_) (PHIDP_PREPARSED_DATA preparsed_data, HIDP_CAPS *caps)

◆ NTSTATUS

typedef LONG NTSTATUS

◆ PHIDD_ATTRIBUTES

◆ PHIDP_CAPS

typedef struct _HIDP_CAPS * PHIDP_CAPS

◆ PHIDP_PREPARSED_DATA

◆ USAGE

typedef USHORT USAGE

Function Documentation

◆ free_hid_device()

static void free_hid_device ( hid_device dev)
static
164{
165 CloseHandle(dev->ol.hEvent);
166 CloseHandle(dev->device_handle);
167 LocalFree(dev->last_error_str);
168 free(dev->read_buf);
169 free(dev);
170}
void free(void *)
char * read_buf
Definition hid.c:142
int device_handle
Definition hid.c:202
void * last_error_str
Definition hid.c:139
OVERLAPPED ol
Definition hid.c:143

References hid_device_::device_handle, free(), hid_device_::last_error_str, hid_device_::ol, and hid_device_::read_buf.

Referenced by hid_close(), and hid_open_path().

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

◆ lookup_functions()

static int lookup_functions ( )
static
205{
206 lib_handle = LoadLibraryA("hid.dll");
207 if (lib_handle) {
208#define RESOLVE(x) x = (x##_)GetProcAddress(lib_handle, #x); if (!x) return -1;
220#undef RESOLVE
221 }
222 else
223 return -1;
224
225 return 0;
226}
static HidD_GetManufacturerString_ HidD_GetManufacturerString
Definition hid.c:120
#define RESOLVE(x)
static HidD_FreePreparsedData_ HidD_FreePreparsedData
Definition hid.c:126
static HidD_GetPreparsedData_ HidD_GetPreparsedData
Definition hid.c:125
static HidD_SetNumInputBuffers_ HidD_SetNumInputBuffers
Definition hid.c:128
static HMODULE lib_handle
Definition hid.c:130
static HidD_GetFeature_ HidD_GetFeature
Definition hid.c:123
static HidD_GetSerialNumberString_ HidD_GetSerialNumberString
Definition hid.c:119
static HidP_GetCaps_ HidP_GetCaps
Definition hid.c:127
static HidD_GetIndexedString_ HidD_GetIndexedString
Definition hid.c:124
static HidD_SetFeature_ HidD_SetFeature
Definition hid.c:122
static HidD_GetAttributes_ HidD_GetAttributes
Definition hid.c:118
static HidD_GetProductString_ HidD_GetProductString
Definition hid.c:121

References HidD_FreePreparsedData, HidD_GetAttributes, HidD_GetFeature, HidD_GetIndexedString, HidD_GetManufacturerString, HidD_GetPreparsedData, HidD_GetProductString, HidD_GetSerialNumberString, HidD_SetFeature, HidD_SetNumInputBuffers, HidP_GetCaps, lib_handle, and RESOLVE.

Referenced by hid_init().

+ Here is the caller graph for this function:

◆ new_hid_device()

static hid_device * new_hid_device ( void  )
static
147{
148 hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device));
149 dev->device_handle = INVALID_HANDLE_VALUE;
150 dev->blocking = TRUE;
151 dev->output_report_length = 0;
152 dev->input_report_length = 0;
153 dev->last_error_str = NULL;
154 dev->last_error_num = 0;
155 dev->read_pending = FALSE;
156 dev->read_buf = NULL;
157 memset(&dev->ol, 0, sizeof(dev->ol));
158 dev->ol.hEvent = CreateEvent(NULL, FALSE, FALSE /*initial state f=nonsignaled*/, NULL);
159
160 return dev;
161}
#define TRUE
Definition mesh.c:42
#define FALSE
Definition mesh.c:45
size_t input_report_length
Definition hid.c:138
USHORT output_report_length
Definition hid.c:137
DWORD last_error_num
Definition hid.c:140
BOOL read_pending
Definition hid.c:141
int blocking
Definition hid.c:203
Definition hid.c:201

References hid_device_::blocking, hid_device_::device_handle, FALSE, hid_device_::input_report_length, hid_device_::last_error_num, hid_device_::last_error_str, hid_device_::ol, hid_device_::output_report_length, hid_device_::read_buf, hid_device_::read_pending, and TRUE.

Referenced by hid_open_path().

+ Here is the caller graph for this function:

◆ open_device()

static HANDLE open_device ( const char *  path,
BOOL  open_rw 
)
static
230{
231 HANDLE handle;
232 DWORD desired_access = (open_rw)? (GENERIC_WRITE | GENERIC_READ): 0;
233 DWORD share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
234
235 handle = CreateFileA(path,
236 desired_access,
237 share_mode,
238 NULL,
239 OPEN_EXISTING,
240 FILE_FLAG_OVERLAPPED,/*FILE_ATTRIBUTE_NORMAL,*/
241 0);
242
243 return handle;
244}

Referenced by hid_enumerate(), and hid_open_path().

+ Here is the caller graph for this function:

◆ register_error()

static void register_error ( hid_device dev,
const char *  op 
)
static
173{
174 WCHAR *ptr, *msg;
175
176 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
177 FORMAT_MESSAGE_FROM_SYSTEM |
178 FORMAT_MESSAGE_IGNORE_INSERTS,
179 NULL,
180 GetLastError(),
181 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
182 (LPVOID)&msg, 0/*sz*/,
183 NULL);
184
185 /* Get rid of the CR and LF that FormatMessage() sticks at the
186 end of the message. Thanks Microsoft! */
187 ptr = msg;
188 while (*ptr) {
189 if (*ptr == '\r') {
190 *ptr = 0x0000;
191 break;
192 }
193 ptr++;
194 }
195
196 /* Store the message off in the Device entry so that
197 the hid_error() function can pick it up. */
198 LocalFree(dev->last_error_str);
199 dev->last_error_str = msg;
200}

References hid_device_::last_error_str.

Referenced by hid_get_feature_report(), hid_get_indexed_string(), hid_get_manufacturer_string(), hid_get_product_string(), hid_get_serial_number_string(), hid_open_path(), hid_read_timeout(), hid_send_feature_report(), and hid_write().

+ Here is the caller graph for this function:

Variable Documentation

◆ HidD_FreePreparsedData

HidD_FreePreparsedData_ HidD_FreePreparsedData
static

◆ HidD_GetAttributes

HidD_GetAttributes_ HidD_GetAttributes
static

Referenced by hid_enumerate(), and lookup_functions().

◆ HidD_GetFeature

HidD_GetFeature_ HidD_GetFeature
static

◆ HidD_GetIndexedString

HidD_GetIndexedString_ HidD_GetIndexedString
static

◆ HidD_GetManufacturerString

HidD_GetManufacturerString_ HidD_GetManufacturerString
static

◆ HidD_GetPreparsedData

HidD_GetPreparsedData_ HidD_GetPreparsedData
static

◆ HidD_GetProductString

HidD_GetProductString_ HidD_GetProductString
static

◆ HidD_GetSerialNumberString

HidD_GetSerialNumberString_ HidD_GetSerialNumberString
static

◆ HidD_SetFeature

HidD_SetFeature_ HidD_SetFeature
static

◆ HidD_SetNumInputBuffers

HidD_SetNumInputBuffers_ HidD_SetNumInputBuffers
static

Referenced by hid_open_path(), and lookup_functions().

◆ HidP_GetCaps

HidP_GetCaps_ HidP_GetCaps
static

◆ initialized

◆ lib_handle

HMODULE lib_handle = NULL
static

Referenced by hid_exit(), and lookup_functions().

◆ ProductID

unsigned short ProductID = 0x3f

◆ VendorID

unsigned short VendorID = 0x04d8