GNU Radio's HOWTO Package
hidapi.h
Go to the documentation of this file.
1 /*******************************************************
2  HIDAPI - Multi-Platform library for
3  communication with HID devices.
4 
5  Alan Ott
6  Signal 11 Software
7 
8  8/22/2009
9 
10  Copyright 2009, All Rights Reserved.
11 
12  At the discretion of the user of this library,
13  this software may be licensed under the terms of the
14  GNU General Public License v3, a BSD-Style license, or the
15  original HIDAPI license as outlined in the LICENSE.txt,
16  LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
17  files located at the root of the source distribution.
18  These files may also be found in the public source
19  code repository located at:
20  http://github.com/signal11/hidapi .
21 ********************************************************/
22 
23 /** @file
24  * @defgroup API hidapi API
25  */
26 
27 #ifndef HIDAPI_H__
28 #define HIDAPI_H__
29 
30 #include <wchar.h>
31 
32 #ifdef _WIN32
33  #define HID_API_EXPORT __declspec(dllexport)
34  #define HID_API_CALL
35 #else
36  #define HID_API_EXPORT /**< API export macro */
37  #define HID_API_CALL /**< API call macro */
38 #endif
39 
40 #define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45  struct hid_device_;
46  typedef struct hid_device_ hid_device; /**< opaque hidapi structure */
47 
48  /** hidapi info structure */
49  struct hid_device_info {
50  /** Platform-specific device path */
51  char *path;
52  /** Device Vendor ID */
53  unsigned short vendor_id;
54  /** Device Product ID */
55  unsigned short product_id;
56  /** Serial Number */
57  wchar_t *serial_number;
58  /** Device Release Number in binary-coded decimal,
59  also known as Device Version Number */
60  unsigned short release_number;
61  /** Manufacturer String */
63  /** Product string */
64  wchar_t *product_string;
65  /** Usage Page for this Device/Interface
66  (Windows/Mac only). */
67  unsigned short usage_page;
68  /** Usage for this Device/Interface
69  (Windows/Mac only).*/
70  unsigned short usage;
71  /** The USB interface which this logical device
72  represents. Valid on both Linux implementations
73  in all cases, and valid on the Windows implementation
74  only if the device contains more than one interface. */
76 
77  /** Pointer to the next device */
79  };
80 
81 
82  /** @brief Initialize the HIDAPI library.
83 
84  This function initializes the HIDAPI library. Calling it is not
85  strictly necessary, as it will be called automatically by
86  hid_enumerate() and any of the hid_open_*() functions if it is
87  needed. This function should be called at the beginning of
88  execution however, if there is a chance of HIDAPI handles
89  being opened by different threads simultaneously.
90 
91  @ingroup API
92 
93  @returns
94  This function returns 0 on success and -1 on error.
95  */
97 
98  /** @brief Finalize the HIDAPI library.
99 
100  This function frees all of the static data associated with
101  HIDAPI. It should be called at the end of execution to avoid
102  memory leaks.
103 
104  @ingroup API
105 
106  @returns
107  This function returns 0 on success and -1 on error.
108  */
110 
111  /** @brief Enumerate the HID Devices.
112 
113  This function returns a linked list of all the HID devices
114  attached to the system which match vendor_id and product_id.
115  If @p vendor_id is set to 0 then any vendor matches.
116  If @p product_id is set to 0 then any product matches.
117  If @p vendor_id and @p product_id are both set to 0, then
118  all HID devices will be returned.
119 
120  @ingroup API
121  @param vendor_id The Vendor ID (VID) of the types of device
122  to open.
123  @param product_id The Product ID (PID) of the types of
124  device to open.
125 
126  @returns
127  This function returns a pointer to a linked list of type
128  struct #hid_device, containing information about the HID devices
129  attached to the system, or NULL in the case of failure. Free
130  this linked list by calling hid_free_enumeration().
131  */
132  struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id);
133 
134  /** @brief Free an enumeration Linked List
135 
136  This function frees a linked list created by hid_enumerate().
137 
138  @ingroup API
139  @param devs Pointer to a list of struct_device returned from
140  hid_enumerate().
141  */
143 
144  /** @brief Open a HID device using a Vendor ID (VID), Product ID
145  (PID) and optionally a serial number.
146 
147  If @p serial_number is NULL, the first device with the
148  specified VID and PID is opened.
149 
150  @ingroup API
151  @param vendor_id The Vendor ID (VID) of the device to open.
152  @param product_id The Product ID (PID) of the device to open.
153  @param serial_number The Serial Number of the device to open
154  (Optionally NULL).
155 
156  @returns
157  This function returns a pointer to a #hid_device object on
158  success or NULL on failure.
159  */
160  HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);
161 
162  /** @brief Open a HID device by its path name.
163 
164  The path name be determined by calling hid_enumerate(), or a
165  platform-specific path name can be used (eg: /dev/hidraw0 on
166  Linux).
167 
168  @ingroup API
169  @param path The path name of the device to open
170 
171  @returns
172  This function returns a pointer to a #hid_device object on
173  success or NULL on failure.
174  */
176 
177  /** @brief Write an Output report to a HID device.
178 
179  The first byte of @p data[] must contain the Report ID. For
180  devices which only support a single report, this must be set
181  to 0x0. The remaining bytes contain the report data. Since
182  the Report ID is mandatory, calls to hid_write() will always
183  contain one more byte than the report contains. For example,
184  if a hid report is 16 bytes long, 17 bytes must be passed to
185  hid_write(), the Report ID (or 0x0, for devices with a
186  single report), followed by the report data (16 bytes). In
187  this example, the length passed in would be 17.
188 
189  hid_write() will send the data on the first OUT endpoint, if
190  one exists. If it does not, it will send the data through
191  the Control Endpoint (Endpoint 0).
192 
193  @ingroup API
194  @param device A device handle returned from hid_open().
195  @param data The data to send, including the report number as
196  the first byte.
197  @param length The length in bytes of the data to send.
198 
199  @returns
200  This function returns the actual number of bytes written and
201  -1 on error.
202  */
203  int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length);
204 
205  /** @brief Read an Input report from a HID device with timeout.
206 
207  Input reports are returned
208  to the host through the INTERRUPT IN endpoint. The first byte will
209  contain the Report number if the device uses numbered reports.
210 
211  @ingroup API
212  @param device A device handle returned from hid_open().
213  @param data A buffer to put the read data into.
214  @param length The number of bytes to read. For devices with
215  multiple reports, make sure to read an extra byte for
216  the report number.
217  @param milliseconds timeout in milliseconds or -1 for blocking wait.
218 
219  @returns
220  This function returns the actual number of bytes read and
221  -1 on error. If no packet was available to be read within
222  the timeout period, this function returns 0.
223  */
224  int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds);
225 
226  /** @brief Read an Input report from a HID device.
227 
228  Input reports are returned
229  to the host through the INTERRUPT IN endpoint. The first byte will
230  contain the Report number if the device uses numbered reports.
231 
232  @ingroup API
233  @param device A device handle returned from hid_open().
234  @param data A buffer to put the read data into.
235  @param length The number of bytes to read. For devices with
236  multiple reports, make sure to read an extra byte for
237  the report number.
238 
239  @returns
240  This function returns the actual number of bytes read and
241  -1 on error. If no packet was available to be read and
242  the handle is in non-blocking mode, this function returns 0.
243  */
244  int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length);
245 
246  /** @brief Set the device handle to be non-blocking.
247 
248  In non-blocking mode calls to hid_read() will return
249  immediately with a value of 0 if there is no data to be
250  read. In blocking mode, hid_read() will wait (block) until
251  there is data to read before returning.
252 
253  Nonblocking can be turned on and off at any time.
254 
255  @ingroup API
256  @param device A device handle returned from hid_open().
257  @param nonblock enable or not the nonblocking reads
258  - 1 to enable nonblocking
259  - 0 to disable nonblocking.
260 
261  @returns
262  This function returns 0 on success and -1 on error.
263  */
264  int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock);
265 
266  /** @brief Send a Feature report to the device.
267 
268  Feature reports are sent over the Control endpoint as a
269  Set_Report transfer. The first byte of @p data[] must
270  contain the Report ID. For devices which only support a
271  single report, this must be set to 0x0. The remaining bytes
272  contain the report data. Since the Report ID is mandatory,
273  calls to hid_send_feature_report() will always contain one
274  more byte than the report contains. For example, if a hid
275  report is 16 bytes long, 17 bytes must be passed to
276  hid_send_feature_report(): the Report ID (or 0x0, for
277  devices which do not use numbered reports), followed by the
278  report data (16 bytes). In this example, the length passed
279  in would be 17.
280 
281  @ingroup API
282  @param device A device handle returned from hid_open().
283  @param data The data to send, including the report number as
284  the first byte.
285  @param length The length in bytes of the data to send, including
286  the report number.
287 
288  @returns
289  This function returns the actual number of bytes written and
290  -1 on error.
291  */
292  int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length);
293 
294  /** @brief Get a feature report from a HID device.
295 
296  Make sure to set the first byte of @p data[] to the Report
297  ID of the report to be read. Make sure to allow space for
298  this extra byte in @p data[].
299 
300  @ingroup API
301  @param device A device handle returned from hid_open().
302  @param data A buffer to put the read data into, including
303  the Report ID. Set the first byte of @p data[] to the
304  Report ID of the report to be read.
305  @param length The number of bytes to read, including an
306  extra byte for the report ID. The buffer can be longer
307  than the actual report.
308 
309  @returns
310  This function returns the number of bytes read and
311  -1 on error.
312  */
313  int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length);
314 
315  /** @brief Close a HID device.
316 
317  @ingroup API
318  @param device A device handle returned from hid_open().
319  */
321 
322  /** @brief Get The Manufacturer String from a HID device.
323 
324  @ingroup API
325  @param device A device handle returned from hid_open().
326  @param string A wide string buffer to put the data into.
327  @param maxlen The length of the buffer in multiples of wchar_t.
328 
329  @returns
330  This function returns 0 on success and -1 on error.
331  */
332  int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen);
333 
334  /** @brief Get The Product String from a HID device.
335 
336  @ingroup API
337  @param device A device handle returned from hid_open().
338  @param string A wide string buffer to put the data into.
339  @param maxlen The length of the buffer in multiples of wchar_t.
340 
341  @returns
342  This function returns 0 on success and -1 on error.
343  */
344  int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen);
345 
346  /** @brief Get The Serial Number String from a HID device.
347 
348  @ingroup API
349  @param device A device handle returned from hid_open().
350  @param string A wide string buffer to put the data into.
351  @param maxlen The length of the buffer in multiples of wchar_t.
352 
353  @returns
354  This function returns 0 on success and -1 on error.
355  */
356  int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen);
357 
358  /** @brief Get a string from a HID device, based on its string index.
359 
360  @ingroup API
361  @param device A device handle returned from hid_open().
362  @param string_index The index of the string to get.
363  @param string A wide string buffer to put the data into.
364  @param maxlen The length of the buffer in multiples of wchar_t.
365 
366  @returns
367  This function returns 0 on success and -1 on error.
368  */
369  int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen);
370 
371  /** @brief Get a string describing the last error which occurred.
372 
373  @ingroup API
374  @param device A device handle returned from hid_open().
375 
376  @returns
377  This function returns a string containing the last error
378  which occurred or NULL if none has occurred.
379  */
380  HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device);
381 
382 #ifdef __cplusplus
383 }
384 #endif
385 
386 #endif
387 
hid_error
const HID_API_EXPORT wchar_t *HID_API_CALL hid_error(hid_device *device)
Get a string describing the last error which occurred.
hid_device_info::interface_number
int interface_number
Definition: hidapi.h:75
hid_write
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
Write an Output report to a HID device.
hid_send_feature_report
int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
Send a Feature report to the device.
hid_exit
int HID_API_EXPORT HID_API_CALL hid_exit(void)
Finalize the HIDAPI library.
hid_close
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
Close a HID device.
hid_read
int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length)
Read an Input report from a HID device.
hid_free_enumeration
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
hid_init
int HID_API_EXPORT HID_API_CALL hid_init(void)
Initialize the HIDAPI library.
HID_API_EXPORT_CALL
#define HID_API_EXPORT_CALL
Definition: hidapi.h:40
hid_get_indexed_string
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
hid_device_info::manufacturer_string
wchar_t * manufacturer_string
Definition: hidapi.h:62
hid_device_info::next
struct hid_device_info * next
Definition: hidapi.h:78
hid_device_info
Definition: hidapi.h:49
hid_enumerate
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
hid_device_info::product_id
unsigned short product_id
Definition: hidapi.h:55
hid_device_info::serial_number
wchar_t * serial_number
Definition: hidapi.h:57
hid_device_info::release_number
unsigned short release_number
Definition: hidapi.h:60
hid_device_info::usage
unsigned short usage
Definition: hidapi.h:70
hid_get_serial_number_string
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
HID_API_CALL
#define HID_API_CALL
Definition: hidapi.h:37
hid_device_info::usage_page
unsigned short usage_page
Definition: hidapi.h:67
hid_get_feature_report
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
Get a feature report from a HID device.
hid_set_nonblocking
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
Set the device handle to be non-blocking.
hid_get_product_string
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
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)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
hid_device
struct hid_device_ hid_device
Definition: hidapi.h:46
hid_device_info::path
char * path
Definition: hidapi.h:51
hid_get_manufacturer_string
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
hid_read_timeout
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.
hid_device_info::vendor_id
unsigned short vendor_id
Definition: hidapi.h:53
hid_device_info::product_string
wchar_t * product_string
Definition: hidapi.h:64
hid_open_path
HID_API_EXPORT hid_device *HID_API_CALL hid_open_path(const char *path)
Open a HID device by its path name.
HID_API_EXPORT
#define HID_API_EXPORT
Definition: hidapi.h:36