GNU Radio 3.6.5 C++ API
|
00001 /******************************************************* 00002 HIDAPI - Multi-Platform library for 00003 communication with HID devices. 00004 00005 Alan Ott 00006 Signal 11 Software 00007 00008 8/22/2009 00009 00010 Copyright 2009, All Rights Reserved. 00011 00012 At the discretion of the user of this library, 00013 this software may be licensed under the terms of the 00014 GNU Public License v3, a BSD-Style license, or the 00015 original HIDAPI license as outlined in the LICENSE.txt, 00016 LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt 00017 files located at the root of the source distribution. 00018 These files may also be found in the public source 00019 code repository located at: 00020 http://github.com/signal11/hidapi . 00021 ********************************************************/ 00022 00023 /** @file 00024 * @defgroup API hidapi API 00025 */ 00026 00027 #ifndef HIDAPI_H__ 00028 #define HIDAPI_H__ 00029 00030 #include <wchar.h> 00031 00032 #ifdef _WIN32 00033 #define HID_API_EXPORT __declspec(dllexport) 00034 #define HID_API_CALL 00035 #else 00036 #define HID_API_EXPORT /**< API export macro */ 00037 #define HID_API_CALL /**< API call macro */ 00038 #endif 00039 00040 #define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/ 00041 00042 #ifdef __cplusplus 00043 extern "C" { 00044 #endif 00045 struct hid_device_; 00046 typedef struct hid_device_ hid_device; /**< opaque hidapi structure */ 00047 00048 /** hidapi info structure */ 00049 struct hid_device_info { 00050 /** Platform-specific device path */ 00051 char *path; 00052 /** Device Vendor ID */ 00053 unsigned short vendor_id; 00054 /** Device Product ID */ 00055 unsigned short product_id; 00056 /** Serial Number */ 00057 wchar_t *serial_number; 00058 /** Device Release Number in binary-coded decimal, 00059 also known as Device Version Number */ 00060 unsigned short release_number; 00061 /** Manufacturer String */ 00062 wchar_t *manufacturer_string; 00063 /** Product string */ 00064 wchar_t *product_string; 00065 /** Usage Page for this Device/Interface 00066 (Windows/Mac only). */ 00067 unsigned short usage_page; 00068 /** Usage for this Device/Interface 00069 (Windows/Mac only).*/ 00070 unsigned short usage; 00071 /** The USB interface which this logical device 00072 represents. Valid on both Linux implementations 00073 in all cases, and valid on the Windows implementation 00074 only if the device contains more than one interface. */ 00075 int interface_number; 00076 00077 /** Pointer to the next device */ 00078 struct hid_device_info *next; 00079 }; 00080 00081 00082 /** @brief Initialize the HIDAPI library. 00083 00084 This function initializes the HIDAPI library. Calling it is not 00085 strictly necessary, as it will be called automatically by 00086 hid_enumerate() and any of the hid_open_*() functions if it is 00087 needed. This function should be called at the beginning of 00088 execution however, if there is a chance of HIDAPI handles 00089 being opened by different threads simultaneously. 00090 00091 @ingroup API 00092 00093 @returns 00094 This function returns 0 on success and -1 on error. 00095 */ 00096 int HID_API_EXPORT HID_API_CALL hid_init(void); 00097 00098 /** @brief Finalize the HIDAPI library. 00099 00100 This function frees all of the static data associated with 00101 HIDAPI. It should be called at the end of execution to avoid 00102 memory leaks. 00103 00104 @ingroup API 00105 00106 @returns 00107 This function returns 0 on success and -1 on error. 00108 */ 00109 int HID_API_EXPORT HID_API_CALL hid_exit(void); 00110 00111 /** @brief Enumerate the HID Devices. 00112 00113 This function returns a linked list of all the HID devices 00114 attached to the system which match vendor_id and product_id. 00115 If @p vendor_id and @p product_id are both set to 0, then 00116 all HID devices will be returned. 00117 00118 @ingroup API 00119 @param vendor_id The Vendor ID (VID) of the types of device 00120 to open. 00121 @param product_id The Product ID (PID) of the types of 00122 device to open. 00123 00124 @returns 00125 This function returns a pointer to a linked list of type 00126 struct #hid_device, containing information about the HID devices 00127 attached to the system, or NULL in the case of failure. Free 00128 this linked list by calling hid_free_enumeration(). 00129 */ 00130 struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id); 00131 00132 /** @brief Free an enumeration Linked List 00133 00134 This function frees a linked list created by hid_enumerate(). 00135 00136 @ingroup API 00137 @param devs Pointer to a list of struct_device returned from 00138 hid_enumerate(). 00139 */ 00140 void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs); 00141 00142 /** @brief Open a HID device using a Vendor ID (VID), Product ID 00143 (PID) and optionally a serial number. 00144 00145 If @p serial_number is NULL, the first device with the 00146 specified VID and PID is opened. 00147 00148 @ingroup API 00149 @param vendor_id The Vendor ID (VID) of the device to open. 00150 @param product_id The Product ID (PID) of the device to open. 00151 @param serial_number The Serial Number of the device to open 00152 (Optionally NULL). 00153 00154 @returns 00155 This function returns a pointer to a #hid_device object on 00156 success or NULL on failure. 00157 */ 00158 HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number); 00159 00160 /** @brief Open a HID device by its path name. 00161 00162 The path name be determined by calling hid_enumerate(), or a 00163 platform-specific path name can be used (eg: /dev/hidraw0 on 00164 Linux). 00165 00166 @ingroup API 00167 @param path The path name of the device to open 00168 00169 @returns 00170 This function returns a pointer to a #hid_device object on 00171 success or NULL on failure. 00172 */ 00173 HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path); 00174 00175 /** @brief Write an Output report to a HID device. 00176 00177 The first byte of @p data[] must contain the Report ID. For 00178 devices which only support a single report, this must be set 00179 to 0x0. The remaining bytes contain the report data. Since 00180 the Report ID is mandatory, calls to hid_write() will always 00181 contain one more byte than the report contains. For example, 00182 if a hid report is 16 bytes long, 17 bytes must be passed to 00183 hid_write(), the Report ID (or 0x0, for devices with a 00184 single report), followed by the report data (16 bytes). In 00185 this example, the length passed in would be 17. 00186 00187 hid_write() will send the data on the first OUT endpoint, if 00188 one exists. If it does not, it will send the data through 00189 the Control Endpoint (Endpoint 0). 00190 00191 @ingroup API 00192 @param device A device handle returned from hid_open(). 00193 @param data The data to send, including the report number as 00194 the first byte. 00195 @param length The length in bytes of the data to send. 00196 00197 @returns 00198 This function returns the actual number of bytes written and 00199 -1 on error. 00200 */ 00201 int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length); 00202 00203 /** @brief Read an Input report from a HID device with timeout. 00204 00205 Input reports are returned 00206 to the host through the INTERRUPT IN endpoint. The first byte will 00207 contain the Report number if the device uses numbered reports. 00208 00209 @ingroup API 00210 @param dev A device handle returned from hid_open(). 00211 @param data A buffer to put the read data into. 00212 @param length The number of bytes to read. For devices with 00213 multiple reports, make sure to read an extra byte for 00214 the report number. 00215 @param milliseconds timeout in milliseconds or -1 for blocking wait. 00216 00217 @returns 00218 This function returns the actual number of bytes read and 00219 -1 on error. 00220 */ 00221 int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds); 00222 00223 /** @brief Read an Input report from a HID device. 00224 00225 Input reports are returned 00226 to the host through the INTERRUPT IN endpoint. The first byte will 00227 contain the Report number if the device uses numbered reports. 00228 00229 @ingroup API 00230 @param device A device handle returned from hid_open(). 00231 @param data A buffer to put the read data into. 00232 @param length The number of bytes to read. For devices with 00233 multiple reports, make sure to read an extra byte for 00234 the report number. 00235 00236 @returns 00237 This function returns the actual number of bytes read and 00238 -1 on error. 00239 */ 00240 int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length); 00241 00242 /** @brief Set the device handle to be non-blocking. 00243 00244 In non-blocking mode calls to hid_read() will return 00245 immediately with a value of 0 if there is no data to be 00246 read. In blocking mode, hid_read() will wait (block) until 00247 there is data to read before returning. 00248 00249 Nonblocking can be turned on and off at any time. 00250 00251 @ingroup API 00252 @param device A device handle returned from hid_open(). 00253 @param nonblock enable or not the nonblocking reads 00254 - 1 to enable nonblocking 00255 - 0 to disable nonblocking. 00256 00257 @returns 00258 This function returns 0 on success and -1 on error. 00259 */ 00260 int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock); 00261 00262 /** @brief Send a Feature report to the device. 00263 00264 Feature reports are sent over the Control endpoint as a 00265 Set_Report transfer. The first byte of @p data[] must 00266 contain the Report ID. For devices which only support a 00267 single report, this must be set to 0x0. The remaining bytes 00268 contain the report data. Since the Report ID is mandatory, 00269 calls to hid_send_feature_report() will always contain one 00270 more byte than the report contains. For example, if a hid 00271 report is 16 bytes long, 17 bytes must be passed to 00272 hid_send_feature_report(): the Report ID (or 0x0, for 00273 devices which do not use numbered reports), followed by the 00274 report data (16 bytes). In this example, the length passed 00275 in would be 17. 00276 00277 @ingroup API 00278 @param device A device handle returned from hid_open(). 00279 @param data The data to send, including the report number as 00280 the first byte. 00281 @param length The length in bytes of the data to send, including 00282 the report number. 00283 00284 @returns 00285 This function returns the actual number of bytes written and 00286 -1 on error. 00287 */ 00288 int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length); 00289 00290 /** @brief Get a feature report from a HID device. 00291 00292 Make sure to set the first byte of @p data[] to the Report 00293 ID of the report to be read. Make sure to allow space for 00294 this extra byte in @p data[]. 00295 00296 @ingroup API 00297 @param device A device handle returned from hid_open(). 00298 @param data A buffer to put the read data into, including 00299 the Report ID. Set the first byte of @p data[] to the 00300 Report ID of the report to be read. 00301 @param length The number of bytes to read, including an 00302 extra byte for the report ID. The buffer can be longer 00303 than the actual report. 00304 00305 @returns 00306 This function returns the number of bytes read and 00307 -1 on error. 00308 */ 00309 int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length); 00310 00311 /** @brief Close a HID device. 00312 00313 @ingroup API 00314 @param device A device handle returned from hid_open(). 00315 */ 00316 void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device); 00317 00318 /** @brief Get The Manufacturer String from a HID device. 00319 00320 @ingroup API 00321 @param device A device handle returned from hid_open(). 00322 @param string A wide string buffer to put the data into. 00323 @param maxlen The length of the buffer in multiples of wchar_t. 00324 00325 @returns 00326 This function returns 0 on success and -1 on error. 00327 */ 00328 int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen); 00329 00330 /** @brief Get The Product String from a HID device. 00331 00332 @ingroup API 00333 @param device A device handle returned from hid_open(). 00334 @param string A wide string buffer to put the data into. 00335 @param maxlen The length of the buffer in multiples of wchar_t. 00336 00337 @returns 00338 This function returns 0 on success and -1 on error. 00339 */ 00340 int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen); 00341 00342 /** @brief Get The Serial Number String from a HID device. 00343 00344 @ingroup API 00345 @param device A device handle returned from hid_open(). 00346 @param string A wide string buffer to put the data into. 00347 @param maxlen The length of the buffer in multiples of wchar_t. 00348 00349 @returns 00350 This function returns 0 on success and -1 on error. 00351 */ 00352 int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen); 00353 00354 /** @brief Get a string from a HID device, based on its string index. 00355 00356 @ingroup API 00357 @param device A device handle returned from hid_open(). 00358 @param string_index The index of the string to get. 00359 @param string A wide string buffer to put the data into. 00360 @param maxlen The length of the buffer in multiples of wchar_t. 00361 00362 @returns 00363 This function returns 0 on success and -1 on error. 00364 */ 00365 int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen); 00366 00367 /** @brief Get a string describing the last error which occurred. 00368 00369 @ingroup API 00370 @param device A device handle returned from hid_open(). 00371 00372 @returns 00373 This function returns a string containing the last error 00374 which occurred or NULL if none has occurred. 00375 */ 00376 HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device); 00377 00378 #ifdef __cplusplus 00379 } 00380 #endif 00381 00382 #endif 00383