libvcomm  1.0
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
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 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 and @p product_id are both set to 0, then
116  all HID devices will be returned.
117 
118  @ingroup API
119  @param vendor_id The Vendor ID (VID) of the types of device
120  to open.
121  @param product_id The Product ID (PID) of the types of
122  device to open.
123 
124  @returns
125  This function returns a pointer to a linked list of type
126  struct #hid_device, containing information about the HID devices
127  attached to the system, or NULL in the case of failure. Free
128  this linked list by calling hid_free_enumeration().
129  */
130  struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id);
131 
132  /** @brief Free an enumeration Linked List
133 
134  This function frees a linked list created by hid_enumerate().
135 
136  @ingroup API
137  @param devs Pointer to a list of struct_device returned from
138  hid_enumerate().
139  */
141 
142  /** @brief Open a HID device using a Vendor ID (VID), Product ID
143  (PID) and optionally a serial number.
144 
145  If @p serial_number is NULL, the first device with the
146  specified VID and PID is opened.
147 
148  @ingroup API
149  @param vendor_id The Vendor ID (VID) of the device to open.
150  @param product_id The Product ID (PID) of the device to open.
151  @param serial_number The Serial Number of the device to open
152  (Optionally NULL).
153 
154  @returns
155  This function returns a pointer to a #hid_device object on
156  success or NULL on failure.
157  */
158  HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);
159 
160  /** @brief Open a HID device by its path name.
161 
162  The path name be determined by calling hid_enumerate(), or a
163  platform-specific path name can be used (eg: /dev/hidraw0 on
164  Linux).
165 
166  @ingroup API
167  @param path The path name of the device to open
168 
169  @returns
170  This function returns a pointer to a #hid_device object on
171  success or NULL on failure.
172  */
174 
175  /** @brief Write an Output report to a HID device.
176 
177  The first byte of @p data[] must contain the Report ID. For
178  devices which only support a single report, this must be set
179  to 0x0. The remaining bytes contain the report data. Since
180  the Report ID is mandatory, calls to hid_write() will always
181  contain one more byte than the report contains. For example,
182  if a hid report is 16 bytes long, 17 bytes must be passed to
183  hid_write(), the Report ID (or 0x0, for devices with a
184  single report), followed by the report data (16 bytes). In
185  this example, the length passed in would be 17.
186 
187  hid_write() will send the data on the first OUT endpoint, if
188  one exists. If it does not, it will send the data through
189  the Control Endpoint (Endpoint 0).
190 
191  @ingroup API
192  @param device A device handle returned from hid_open().
193  @param data The data to send, including the report number as
194  the first byte.
195  @param length The length in bytes of the data to send.
196 
197  @returns
198  This function returns the actual number of bytes written and
199  -1 on error.
200  */
201  int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length);
202 
203  /** @brief Read an Input report from a HID device with timeout.
204 
205  Input reports are returned
206  to the host through the INTERRUPT IN endpoint. The first byte will
207  contain the Report number if the device uses numbered reports.
208 
209  @ingroup API
210  @param device A device handle returned from hid_open().
211  @param data A buffer to put the read data into.
212  @param length The number of bytes to read. For devices with
213  multiple reports, make sure to read an extra byte for
214  the report number.
215  @param milliseconds timeout in milliseconds or -1 for blocking wait.
216 
217  @returns
218  This function returns the actual number of bytes read and
219  -1 on error.
220  */
221  int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds);
222 
223  /** @brief Read an Input report from a HID device.
224 
225  Input reports are returned
226  to the host through the INTERRUPT IN endpoint. The first byte will
227  contain the Report number if the device uses numbered reports.
228 
229  @ingroup API
230  @param device A device handle returned from hid_open().
231  @param data A buffer to put the read data into.
232  @param length The number of bytes to read. For devices with
233  multiple reports, make sure to read an extra byte for
234  the report number.
235 
236  @returns
237  This function returns the actual number of bytes read and
238  -1 on error.
239  */
240  int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length);
241 
242  /** @brief Set the device handle to be non-blocking.
243 
244  In non-blocking mode calls to hid_read() will return
245  immediately with a value of 0 if there is no data to be
246  read. In blocking mode, hid_read() will wait (block) until
247  there is data to read before returning.
248 
249  Nonblocking can be turned on and off at any time.
250 
251  @ingroup API
252  @param device A device handle returned from hid_open().
253  @param nonblock enable or not the nonblocking reads
254  - 1 to enable nonblocking
255  - 0 to disable nonblocking.
256 
257  @returns
258  This function returns 0 on success and -1 on error.
259  */
260  int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock);
261 
262  /** @brief Send a Feature report to the device.
263 
264  Feature reports are sent over the Control endpoint as a
265  Set_Report transfer. The first byte of @p data[] must
266  contain the Report ID. For devices which only support a
267  single report, this must be set to 0x0. The remaining bytes
268  contain the report data. Since the Report ID is mandatory,
269  calls to hid_send_feature_report() will always contain one
270  more byte than the report contains. For example, if a hid
271  report is 16 bytes long, 17 bytes must be passed to
272  hid_send_feature_report(): the Report ID (or 0x0, for
273  devices which do not use numbered reports), followed by the
274  report data (16 bytes). In this example, the length passed
275  in would be 17.
276 
277  @ingroup API
278  @param device A device handle returned from hid_open().
279  @param data The data to send, including the report number as
280  the first byte.
281  @param length The length in bytes of the data to send, including
282  the report number.
283 
284  @returns
285  This function returns the actual number of bytes written and
286  -1 on error.
287  */
288  int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length);
289 
290  /** @brief Get a feature report from a HID device.
291 
292  Make sure to set the first byte of @p data[] to the Report
293  ID of the report to be read. Make sure to allow space for
294  this extra byte in @p data[].
295 
296  @ingroup API
297  @param device A device handle returned from hid_open().
298  @param data A buffer to put the read data into, including
299  the Report ID. Set the first byte of @p data[] to the
300  Report ID of the report to be read.
301  @param length The number of bytes to read, including an
302  extra byte for the report ID. The buffer can be longer
303  than the actual report.
304 
305  @returns
306  This function returns the number of bytes read and
307  -1 on error.
308  */
309  int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length);
310 
311  /** @brief Close a HID device.
312 
313  @ingroup API
314  @param device A device handle returned from hid_open().
315  */
317 
318  /** @brief Get The Manufacturer String from a HID device.
319 
320  @ingroup API
321  @param device A device handle returned from hid_open().
322  @param string A wide string buffer to put the data into.
323  @param maxlen The length of the buffer in multiples of wchar_t.
324 
325  @returns
326  This function returns 0 on success and -1 on error.
327  */
328  int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen);
329 
330  /** @brief Get The Product String from a HID device.
331 
332  @ingroup API
333  @param device A device handle returned from hid_open().
334  @param string A wide string buffer to put the data into.
335  @param maxlen The length of the buffer in multiples of wchar_t.
336 
337  @returns
338  This function returns 0 on success and -1 on error.
339  */
340  int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen);
341 
342  /** @brief Get The Serial Number String from a HID device.
343 
344  @ingroup API
345  @param device A device handle returned from hid_open().
346  @param string A wide string buffer to put the data into.
347  @param maxlen The length of the buffer in multiples of wchar_t.
348 
349  @returns
350  This function returns 0 on success and -1 on error.
351  */
352  int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen);
353 
354  /** @brief Get a string from a HID device, based on its string index.
355 
356  @ingroup API
357  @param device A device handle returned from hid_open().
358  @param string_index The index of the string to get.
359  @param string A wide string buffer to put the data into.
360  @param maxlen The length of the buffer in multiples of wchar_t.
361 
362  @returns
363  This function returns 0 on success and -1 on error.
364  */
365  int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen);
366 
367  /** @brief Get a string describing the last error which occurred.
368 
369  @ingroup API
370  @param device A device handle returned from hid_open().
371 
372  @returns
373  This function returns a string containing the last error
374  which occurred or NULL if none has occurred.
375  */
376  HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device);
377 
378 #ifdef __cplusplus
379 }
380 #endif
381 
382 #endif
383