OLD | NEW |
(Empty) | |
| 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 struct
ure */ |
| 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 */ |
| 62 wchar_t *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 implementatio
n |
| 74 only if the device contains more than one interface.
*/ |
| 75 int interface_number; |
| 76 |
| 77 /** Pointer to the next device */ |
| 78 struct hid_device_info *next; |
| 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 b
y |
| 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 handle
s |
| 89 being opened by different threads simultaneously. |
| 90 |
| 91 @ingroup API |
| 92 |
| 93 @returns |
| 94 This function returns 0 on success and -1 on err
or. |
| 95 */ |
| 96 int HID_API_EXPORT HID_API_CALL hid_init(void); |
| 97 |
| 98 /** @brief Finalize the HIDAPI library. |
| 99 |
| 100 This function frees all of the static data associated wi
th |
| 101 HIDAPI. It should be called at the end of execution to a
void |
| 102 memory leaks. |
| 103 |
| 104 @ingroup API |
| 105 |
| 106 @returns |
| 107 This function returns 0 on success and -1 on err
or. |
| 108 */ |
| 109 int HID_API_EXPORT HID_API_CALL hid_exit(void); |
| 110 |
| 111 /** @brief Enumerate the HID Devices. |
| 112 |
| 113 This function returns a linked list of all the HID devic
es |
| 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, the
n |
| 118 all HID devices will be returned. |
| 119 |
| 120 @ingroup API |
| 121 @param vendor_id The Vendor ID (VID) of the types of dev
ice |
| 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_enumera
te(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 */ |
| 142 void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hi
d_device_info *devs); |
| 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 op
en. |
| 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/hidraw
0 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 */ |
| 175 HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const cha
r *path); |
| 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. Sin
ce |
| 182 the Report ID is mandatory, calls to hid_write() will al
ways |
| 183 contain one more byte than the report contains. For exam
ple, |
| 184 if a hid report is 16 bytes long, 17 bytes must be passe
d 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 throug
h |
| 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 numbe
r 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, c
onst 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 re
ports. |
| 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 w
ith |
| 215 multiple reports, make sure to read an extra byt
e for |
| 216 the report number. |
| 217 @param milliseconds timeout in milliseconds or -1 for bl
ocking wait. |
| 218 |
| 219 @returns |
| 220 This function returns the actual number of bytes
read and |
| 221 -1 on error. |
| 222 */ |
| 223 int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev
, unsigned char *data, size_t length, int milliseconds); |
| 224 |
| 225 /** @brief Read an Input report from a HID device. |
| 226 |
| 227 Input reports are returned |
| 228 to the host through the INTERRUPT IN endpoint. The first byt
e will |
| 229 contain the Report number if the device uses numbered re
ports. |
| 230 |
| 231 @ingroup API |
| 232 @param device A device handle returned from hid_open(). |
| 233 @param data A buffer to put the read data into. |
| 234 @param length The number of bytes to read. For devices w
ith |
| 235 multiple reports, make sure to read an extra byt
e for |
| 236 the report number. |
| 237 |
| 238 @returns |
| 239 This function returns the actual number of bytes
read and |
| 240 -1 on error. |
| 241 */ |
| 242 int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, un
signed char *data, size_t length); |
| 243 |
| 244 /** @brief Set the device handle to be non-blocking. |
| 245 |
| 246 In non-blocking mode calls to hid_read() will return |
| 247 immediately with a value of 0 if there is no data to be |
| 248 read. In blocking mode, hid_read() will wait (block) unt
il |
| 249 there is data to read before returning. |
| 250 |
| 251 Nonblocking can be turned on and off at any time. |
| 252 |
| 253 @ingroup API |
| 254 @param device A device handle returned from hid_open(). |
| 255 @param nonblock enable or not the nonblocking reads |
| 256 - 1 to enable nonblocking |
| 257 - 0 to disable nonblocking. |
| 258 |
| 259 @returns |
| 260 This function returns 0 on success and -1 on err
or. |
| 261 */ |
| 262 int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device
*device, int nonblock); |
| 263 |
| 264 /** @brief Send a Feature report to the device. |
| 265 |
| 266 Feature reports are sent over the Control endpoint as a |
| 267 Set_Report transfer. The first byte of @p data[] must |
| 268 contain the Report ID. For devices which only support a |
| 269 single report, this must be set to 0x0. The remaining by
tes |
| 270 contain the report data. Since the Report ID is mandator
y, |
| 271 calls to hid_send_feature_report() will always contain o
ne |
| 272 more byte than the report contains. For example, if a hi
d |
| 273 report is 16 bytes long, 17 bytes must be passed to |
| 274 hid_send_feature_report(): the Report ID (or 0x0, for |
| 275 devices which do not use numbered reports), followed by
the |
| 276 report data (16 bytes). In this example, the length pass
ed |
| 277 in would be 17. |
| 278 |
| 279 @ingroup API |
| 280 @param device A device handle returned from hid_open(). |
| 281 @param data The data to send, including the report numbe
r as |
| 282 the first byte. |
| 283 @param length The length in bytes of the data to send, i
ncluding |
| 284 the report number. |
| 285 |
| 286 @returns |
| 287 This function returns the actual number of bytes
written and |
| 288 -1 on error. |
| 289 */ |
| 290 int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_devi
ce *device, const unsigned char *data, size_t length); |
| 291 |
| 292 /** @brief Get a feature report from a HID device. |
| 293 |
| 294 Make sure to set the first byte of @p data[] to the Repo
rt |
| 295 ID of the report to be read. Make sure to allow space f
or |
| 296 this extra byte in @p data[]. |
| 297 |
| 298 @ingroup API |
| 299 @param device A device handle returned from hid_open(). |
| 300 @param data A buffer to put the read data into, includin
g |
| 301 the Report ID. Set the first byte of @p data[] t
o the |
| 302 Report ID of the report to be read. |
| 303 @param length The number of bytes to read, including an |
| 304 extra byte for the report ID. The buffer can be
longer |
| 305 than the actual report. |
| 306 |
| 307 @returns |
| 308 This function returns the number of bytes read a
nd |
| 309 -1 on error. |
| 310 */ |
| 311 int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_devic
e *device, unsigned char *data, size_t length); |
| 312 |
| 313 /** @brief Close a HID device. |
| 314 |
| 315 @ingroup API |
| 316 @param device A device handle returned from hid_open(). |
| 317 */ |
| 318 void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device); |
| 319 |
| 320 /** @brief Get The Manufacturer String from a HID device. |
| 321 |
| 322 @ingroup API |
| 323 @param device A device handle returned from hid_open(). |
| 324 @param string A wide string buffer to put the data into. |
| 325 @param maxlen The length of the buffer in multiples of w
char_t. |
| 326 |
| 327 @returns |
| 328 This function returns 0 on success and -1 on err
or. |
| 329 */ |
| 330 int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *
device, wchar_t *string, size_t maxlen); |
| 331 |
| 332 /** @brief Get The Product String from a HID device. |
| 333 |
| 334 @ingroup API |
| 335 @param device A device handle returned from hid_open(). |
| 336 @param string A wide string buffer to put the data into. |
| 337 @param maxlen The length of the buffer in multiples of w
char_t. |
| 338 |
| 339 @returns |
| 340 This function returns 0 on success and -1 on err
or. |
| 341 */ |
| 342 int HID_API_EXPORT_CALL hid_get_product_string(hid_device *devic
e, wchar_t *string, size_t maxlen); |
| 343 |
| 344 /** @brief Get The Serial Number String from a HID device. |
| 345 |
| 346 @ingroup API |
| 347 @param device A device handle returned from hid_open(). |
| 348 @param string A wide string buffer to put the data into. |
| 349 @param maxlen The length of the buffer in multiples of w
char_t. |
| 350 |
| 351 @returns |
| 352 This function returns 0 on success and -1 on err
or. |
| 353 */ |
| 354 int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device
*device, wchar_t *string, size_t maxlen); |
| 355 |
| 356 /** @brief Get a string from a HID device, based on its string i
ndex. |
| 357 |
| 358 @ingroup API |
| 359 @param device A device handle returned from hid_open(). |
| 360 @param string_index The index of the string to get. |
| 361 @param string A wide string buffer to put the data into. |
| 362 @param maxlen The length of the buffer in multiples of w
char_t. |
| 363 |
| 364 @returns |
| 365 This function returns 0 on success and -1 on err
or. |
| 366 */ |
| 367 int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *devic
e, int string_index, wchar_t *string, size_t maxlen); |
| 368 |
| 369 /** @brief Get a string describing the last error which occurred
. |
| 370 |
| 371 @ingroup API |
| 372 @param device A device handle returned from hid_open(). |
| 373 |
| 374 @returns |
| 375 This function returns a string containing the la
st error |
| 376 which occurred or NULL if none has occurred. |
| 377 */ |
| 378 HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device
*device); |
| 379 |
| 380 #ifdef __cplusplus |
| 381 } |
| 382 #endif |
| 383 |
| 384 #endif |
| 385 |
OLD | NEW |