├── 3rdparty └── libusb-1.0 │ ├── include │ └── libusb.h │ └── lib │ ├── libusb-1.0.so │ ├── libusb-1.0.so.0 │ └── libusb-1.0.so.0.1.0 ├── UsbComm.pro ├── main.cpp ├── readme.md ├── usbcomm.cpp ├── usbcomm.h ├── widget.cpp ├── widget.h └── widget.ui /3rdparty/libusb-1.0/include/libusb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Public libusb header file 3 | * Copyright © 2001 Johannes Erdfelt 4 | * Copyright © 2007-2008 Daniel Drake 5 | * Copyright © 2012 Pete Batard 6 | * Copyright © 2012 Nathan Hjelm 7 | * For more information, please visit: http://libusb.info 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 | */ 23 | 24 | #ifndef LIBUSB_H 25 | #define LIBUSB_H 26 | 27 | #ifdef _MSC_VER 28 | /* on MS environments, the inline keyword is available in C++ only */ 29 | #if !defined(__cplusplus) 30 | #define inline __inline 31 | #endif 32 | /* ssize_t is also not available (copy/paste from MinGW) */ 33 | #ifndef _SSIZE_T_DEFINED 34 | #define _SSIZE_T_DEFINED 35 | #undef ssize_t 36 | #ifdef _WIN64 37 | typedef __int64 ssize_t; 38 | #else 39 | typedef int ssize_t; 40 | #endif /* _WIN64 */ 41 | #endif /* _SSIZE_T_DEFINED */ 42 | #endif /* _MSC_VER */ 43 | 44 | /* stdint.h is not available on older MSVC */ 45 | #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H)) 46 | typedef unsigned __int8 uint8_t; 47 | typedef unsigned __int16 uint16_t; 48 | typedef unsigned __int32 uint32_t; 49 | #else 50 | #include 51 | #endif 52 | 53 | #if !defined(_WIN32_WCE) 54 | #include 55 | #endif 56 | 57 | #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__) || defined(__HAIKU__) 58 | #include 59 | #endif 60 | 61 | #include 62 | #include 63 | 64 | /* 'interface' might be defined as a macro on Windows, so we need to 65 | * undefine it so as not to break the current libusb API, because 66 | * libusb_config_descriptor has an 'interface' member 67 | * As this can be problematic if you include windows.h after libusb.h 68 | * in your sources, we force windows.h to be included first. */ 69 | #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE) 70 | #include 71 | #if defined(interface) 72 | #undef interface 73 | #endif 74 | #if !defined(__CYGWIN__) 75 | #include 76 | #endif 77 | #endif 78 | 79 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) 80 | #define LIBUSB_DEPRECATED_FOR(f) \ 81 | __attribute__((deprecated("Use " #f " instead"))) 82 | #else 83 | #define LIBUSB_DEPRECATED_FOR(f) 84 | #endif /* __GNUC__ */ 85 | 86 | /** \def LIBUSB_CALL 87 | * \ingroup misc 88 | * libusb's Windows calling convention. 89 | * 90 | * Under Windows, the selection of available compilers and configurations 91 | * means that, unlike other platforms, there is not one true calling 92 | * convention (calling convention: the manner in which parameters are 93 | * passed to functions in the generated assembly code). 94 | * 95 | * Matching the Windows API itself, libusb uses the WINAPI convention (which 96 | * translates to the stdcall convention) and guarantees that the 97 | * library is compiled in this way. The public header file also includes 98 | * appropriate annotations so that your own software will use the right 99 | * convention, even if another convention is being used by default within 100 | * your codebase. 101 | * 102 | * The one consideration that you must apply in your software is to mark 103 | * all functions which you use as libusb callbacks with this LIBUSB_CALL 104 | * annotation, so that they too get compiled for the correct calling 105 | * convention. 106 | * 107 | * On non-Windows operating systems, this macro is defined as nothing. This 108 | * means that you can apply it to your code without worrying about 109 | * cross-platform compatibility. 110 | */ 111 | /* LIBUSB_CALL must be defined on both definition and declaration of libusb 112 | * functions. You'd think that declaration would be enough, but cygwin will 113 | * complain about conflicting types unless both are marked this way. 114 | * The placement of this macro is important too; it must appear after the 115 | * return type, before the function name. See internal documentation for 116 | * API_EXPORTED. 117 | */ 118 | #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE) 119 | #define LIBUSB_CALL WINAPI 120 | #else 121 | #define LIBUSB_CALL 122 | #endif 123 | 124 | /** \def LIBUSB_API_VERSION 125 | * \ingroup misc 126 | * libusb's API version. 127 | * 128 | * Since version 1.0.13, to help with feature detection, libusb defines 129 | * a LIBUSB_API_VERSION macro that gets increased every time there is a 130 | * significant change to the API, such as the introduction of a new call, 131 | * the definition of a new macro/enum member, or any other element that 132 | * libusb applications may want to detect at compilation time. 133 | * 134 | * The macro is typically used in an application as follows: 135 | * \code 136 | * #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01001234) 137 | * // Use one of the newer features from the libusb API 138 | * #endif 139 | * \endcode 140 | * 141 | * Internally, LIBUSB_API_VERSION is defined as follows: 142 | * (libusb major << 24) | (libusb minor << 16) | (16 bit incremental) 143 | */ 144 | #define LIBUSB_API_VERSION 0x01000104 145 | 146 | /* The following is kept for compatibility, but will be deprecated in the future */ 147 | #define LIBUSBX_API_VERSION LIBUSB_API_VERSION 148 | 149 | #ifdef __cplusplus 150 | extern "C" { 151 | #endif 152 | 153 | /** 154 | * \ingroup misc 155 | * Convert a 16-bit value from host-endian to little-endian format. On 156 | * little endian systems, this function does nothing. On big endian systems, 157 | * the bytes are swapped. 158 | * \param x the host-endian value to convert 159 | * \returns the value in little-endian byte order 160 | */ 161 | static inline uint16_t libusb_cpu_to_le16(const uint16_t x) 162 | { 163 | union { 164 | uint8_t b8[2]; 165 | uint16_t b16; 166 | } _tmp; 167 | _tmp.b8[1] = (uint8_t) (x >> 8); 168 | _tmp.b8[0] = (uint8_t) (x & 0xff); 169 | return _tmp.b16; 170 | } 171 | 172 | /** \def libusb_le16_to_cpu 173 | * \ingroup misc 174 | * Convert a 16-bit value from little-endian to host-endian format. On 175 | * little endian systems, this function does nothing. On big endian systems, 176 | * the bytes are swapped. 177 | * \param x the little-endian value to convert 178 | * \returns the value in host-endian byte order 179 | */ 180 | #define libusb_le16_to_cpu libusb_cpu_to_le16 181 | 182 | /* standard USB stuff */ 183 | 184 | /** \ingroup desc 185 | * Device and/or Interface Class codes */ 186 | enum libusb_class_code { 187 | /** In the context of a \ref libusb_device_descriptor "device descriptor", 188 | * this bDeviceClass value indicates that each interface specifies its 189 | * own class information and all interfaces operate independently. 190 | */ 191 | LIBUSB_CLASS_PER_INTERFACE = 0, 192 | 193 | /** Audio class */ 194 | LIBUSB_CLASS_AUDIO = 1, 195 | 196 | /** Communications class */ 197 | LIBUSB_CLASS_COMM = 2, 198 | 199 | /** Human Interface Device class */ 200 | LIBUSB_CLASS_HID = 3, 201 | 202 | /** Physical */ 203 | LIBUSB_CLASS_PHYSICAL = 5, 204 | 205 | /** Printer class */ 206 | LIBUSB_CLASS_PRINTER = 7, 207 | 208 | /** Image class */ 209 | LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */ 210 | LIBUSB_CLASS_IMAGE = 6, 211 | 212 | /** Mass storage class */ 213 | LIBUSB_CLASS_MASS_STORAGE = 8, 214 | 215 | /** Hub class */ 216 | LIBUSB_CLASS_HUB = 9, 217 | 218 | /** Data class */ 219 | LIBUSB_CLASS_DATA = 10, 220 | 221 | /** Smart Card */ 222 | LIBUSB_CLASS_SMART_CARD = 0x0b, 223 | 224 | /** Content Security */ 225 | LIBUSB_CLASS_CONTENT_SECURITY = 0x0d, 226 | 227 | /** Video */ 228 | LIBUSB_CLASS_VIDEO = 0x0e, 229 | 230 | /** Personal Healthcare */ 231 | LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f, 232 | 233 | /** Diagnostic Device */ 234 | LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc, 235 | 236 | /** Wireless class */ 237 | LIBUSB_CLASS_WIRELESS = 0xe0, 238 | 239 | /** Application class */ 240 | LIBUSB_CLASS_APPLICATION = 0xfe, 241 | 242 | /** Class is vendor-specific */ 243 | LIBUSB_CLASS_VENDOR_SPEC = 0xff 244 | }; 245 | 246 | /** \ingroup desc 247 | * Descriptor types as defined by the USB specification. */ 248 | enum libusb_descriptor_type { 249 | /** Device descriptor. See libusb_device_descriptor. */ 250 | LIBUSB_DT_DEVICE = 0x01, 251 | 252 | /** Configuration descriptor. See libusb_config_descriptor. */ 253 | LIBUSB_DT_CONFIG = 0x02, 254 | 255 | /** String descriptor */ 256 | LIBUSB_DT_STRING = 0x03, 257 | 258 | /** Interface descriptor. See libusb_interface_descriptor. */ 259 | LIBUSB_DT_INTERFACE = 0x04, 260 | 261 | /** Endpoint descriptor. See libusb_endpoint_descriptor. */ 262 | LIBUSB_DT_ENDPOINT = 0x05, 263 | 264 | /** BOS descriptor */ 265 | LIBUSB_DT_BOS = 0x0f, 266 | 267 | /** Device Capability descriptor */ 268 | LIBUSB_DT_DEVICE_CAPABILITY = 0x10, 269 | 270 | /** HID descriptor */ 271 | LIBUSB_DT_HID = 0x21, 272 | 273 | /** HID report descriptor */ 274 | LIBUSB_DT_REPORT = 0x22, 275 | 276 | /** Physical descriptor */ 277 | LIBUSB_DT_PHYSICAL = 0x23, 278 | 279 | /** Hub descriptor */ 280 | LIBUSB_DT_HUB = 0x29, 281 | 282 | /** SuperSpeed Hub descriptor */ 283 | LIBUSB_DT_SUPERSPEED_HUB = 0x2a, 284 | 285 | /** SuperSpeed Endpoint Companion descriptor */ 286 | LIBUSB_DT_SS_ENDPOINT_COMPANION = 0x30 287 | }; 288 | 289 | /* Descriptor sizes per descriptor type */ 290 | #define LIBUSB_DT_DEVICE_SIZE 18 291 | #define LIBUSB_DT_CONFIG_SIZE 9 292 | #define LIBUSB_DT_INTERFACE_SIZE 9 293 | #define LIBUSB_DT_ENDPOINT_SIZE 7 294 | #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 295 | #define LIBUSB_DT_HUB_NONVAR_SIZE 7 296 | #define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE 6 297 | #define LIBUSB_DT_BOS_SIZE 5 298 | #define LIBUSB_DT_DEVICE_CAPABILITY_SIZE 3 299 | 300 | /* BOS descriptor sizes */ 301 | #define LIBUSB_BT_USB_2_0_EXTENSION_SIZE 7 302 | #define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE 10 303 | #define LIBUSB_BT_CONTAINER_ID_SIZE 20 304 | 305 | /* We unwrap the BOS => define its max size */ 306 | #define LIBUSB_DT_BOS_MAX_SIZE ((LIBUSB_DT_BOS_SIZE) +\ 307 | (LIBUSB_BT_USB_2_0_EXTENSION_SIZE) +\ 308 | (LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) +\ 309 | (LIBUSB_BT_CONTAINER_ID_SIZE)) 310 | 311 | #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ 312 | #define LIBUSB_ENDPOINT_DIR_MASK 0x80 313 | 314 | /** \ingroup desc 315 | * Endpoint direction. Values for bit 7 of the 316 | * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme. 317 | */ 318 | enum libusb_endpoint_direction { 319 | /** In: device-to-host */ 320 | LIBUSB_ENDPOINT_IN = 0x80, 321 | 322 | /** Out: host-to-device */ 323 | LIBUSB_ENDPOINT_OUT = 0x00 324 | }; 325 | 326 | #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */ 327 | 328 | /** \ingroup desc 329 | * Endpoint transfer type. Values for bits 0:1 of the 330 | * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field. 331 | */ 332 | enum libusb_transfer_type { 333 | /** Control endpoint */ 334 | LIBUSB_TRANSFER_TYPE_CONTROL = 0, 335 | 336 | /** Isochronous endpoint */ 337 | LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, 338 | 339 | /** Bulk endpoint */ 340 | LIBUSB_TRANSFER_TYPE_BULK = 2, 341 | 342 | /** Interrupt endpoint */ 343 | LIBUSB_TRANSFER_TYPE_INTERRUPT = 3, 344 | 345 | /** Stream endpoint */ 346 | LIBUSB_TRANSFER_TYPE_BULK_STREAM = 4, 347 | }; 348 | 349 | /** \ingroup misc 350 | * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */ 351 | enum libusb_standard_request { 352 | /** Request status of the specific recipient */ 353 | LIBUSB_REQUEST_GET_STATUS = 0x00, 354 | 355 | /** Clear or disable a specific feature */ 356 | LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, 357 | 358 | /* 0x02 is reserved */ 359 | 360 | /** Set or enable a specific feature */ 361 | LIBUSB_REQUEST_SET_FEATURE = 0x03, 362 | 363 | /* 0x04 is reserved */ 364 | 365 | /** Set device address for all future accesses */ 366 | LIBUSB_REQUEST_SET_ADDRESS = 0x05, 367 | 368 | /** Get the specified descriptor */ 369 | LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, 370 | 371 | /** Used to update existing descriptors or add new descriptors */ 372 | LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, 373 | 374 | /** Get the current device configuration value */ 375 | LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, 376 | 377 | /** Set device configuration */ 378 | LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, 379 | 380 | /** Return the selected alternate setting for the specified interface */ 381 | LIBUSB_REQUEST_GET_INTERFACE = 0x0A, 382 | 383 | /** Select an alternate interface for the specified interface */ 384 | LIBUSB_REQUEST_SET_INTERFACE = 0x0B, 385 | 386 | /** Set then report an endpoint's synchronization frame */ 387 | LIBUSB_REQUEST_SYNCH_FRAME = 0x0C, 388 | 389 | /** Sets both the U1 and U2 Exit Latency */ 390 | LIBUSB_REQUEST_SET_SEL = 0x30, 391 | 392 | /** Delay from the time a host transmits a packet to the time it is 393 | * received by the device. */ 394 | LIBUSB_SET_ISOCH_DELAY = 0x31, 395 | }; 396 | 397 | /** \ingroup misc 398 | * Request type bits of the 399 | * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control 400 | * transfers. */ 401 | enum libusb_request_type { 402 | /** Standard */ 403 | LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), 404 | 405 | /** Class */ 406 | LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), 407 | 408 | /** Vendor */ 409 | LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), 410 | 411 | /** Reserved */ 412 | LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5) 413 | }; 414 | 415 | /** \ingroup misc 416 | * Recipient bits of the 417 | * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control 418 | * transfers. Values 4 through 31 are reserved. */ 419 | enum libusb_request_recipient { 420 | /** Device */ 421 | LIBUSB_RECIPIENT_DEVICE = 0x00, 422 | 423 | /** Interface */ 424 | LIBUSB_RECIPIENT_INTERFACE = 0x01, 425 | 426 | /** Endpoint */ 427 | LIBUSB_RECIPIENT_ENDPOINT = 0x02, 428 | 429 | /** Other */ 430 | LIBUSB_RECIPIENT_OTHER = 0x03, 431 | }; 432 | 433 | #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C 434 | 435 | /** \ingroup desc 436 | * Synchronization type for isochronous endpoints. Values for bits 2:3 of the 437 | * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in 438 | * libusb_endpoint_descriptor. 439 | */ 440 | enum libusb_iso_sync_type { 441 | /** No synchronization */ 442 | LIBUSB_ISO_SYNC_TYPE_NONE = 0, 443 | 444 | /** Asynchronous */ 445 | LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, 446 | 447 | /** Adaptive */ 448 | LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, 449 | 450 | /** Synchronous */ 451 | LIBUSB_ISO_SYNC_TYPE_SYNC = 3 452 | }; 453 | 454 | #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 455 | 456 | /** \ingroup desc 457 | * Usage type for isochronous endpoints. Values for bits 4:5 of the 458 | * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in 459 | * libusb_endpoint_descriptor. 460 | */ 461 | enum libusb_iso_usage_type { 462 | /** Data endpoint */ 463 | LIBUSB_ISO_USAGE_TYPE_DATA = 0, 464 | 465 | /** Feedback endpoint */ 466 | LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, 467 | 468 | /** Implicit feedback Data endpoint */ 469 | LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2, 470 | }; 471 | 472 | /** \ingroup desc 473 | * A structure representing the standard USB device descriptor. This 474 | * descriptor is documented in section 9.6.1 of the USB 3.0 specification. 475 | * All multiple-byte fields are represented in host-endian format. 476 | */ 477 | struct libusb_device_descriptor { 478 | /** Size of this descriptor (in bytes) */ 479 | uint8_t bLength; 480 | 481 | /** Descriptor type. Will have value 482 | * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this 483 | * context. */ 484 | uint8_t bDescriptorType; 485 | 486 | /** USB specification release number in binary-coded decimal. A value of 487 | * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */ 488 | uint16_t bcdUSB; 489 | 490 | /** USB-IF class code for the device. See \ref libusb_class_code. */ 491 | uint8_t bDeviceClass; 492 | 493 | /** USB-IF subclass code for the device, qualified by the bDeviceClass 494 | * value */ 495 | uint8_t bDeviceSubClass; 496 | 497 | /** USB-IF protocol code for the device, qualified by the bDeviceClass and 498 | * bDeviceSubClass values */ 499 | uint8_t bDeviceProtocol; 500 | 501 | /** Maximum packet size for endpoint 0 */ 502 | uint8_t bMaxPacketSize0; 503 | 504 | /** USB-IF vendor ID */ 505 | uint16_t idVendor; 506 | 507 | /** USB-IF product ID */ 508 | uint16_t idProduct; 509 | 510 | /** Device release number in binary-coded decimal */ 511 | uint16_t bcdDevice; 512 | 513 | /** Index of string descriptor describing manufacturer */ 514 | uint8_t iManufacturer; 515 | 516 | /** Index of string descriptor describing product */ 517 | uint8_t iProduct; 518 | 519 | /** Index of string descriptor containing device serial number */ 520 | uint8_t iSerialNumber; 521 | 522 | /** Number of possible configurations */ 523 | uint8_t bNumConfigurations; 524 | }; 525 | 526 | /** \ingroup desc 527 | * A structure representing the standard USB endpoint descriptor. This 528 | * descriptor is documented in section 9.6.6 of the USB 3.0 specification. 529 | * All multiple-byte fields are represented in host-endian format. 530 | */ 531 | struct libusb_endpoint_descriptor { 532 | /** Size of this descriptor (in bytes) */ 533 | uint8_t bLength; 534 | 535 | /** Descriptor type. Will have value 536 | * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in 537 | * this context. */ 538 | uint8_t bDescriptorType; 539 | 540 | /** The address of the endpoint described by this descriptor. Bits 0:3 are 541 | * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction, 542 | * see \ref libusb_endpoint_direction. 543 | */ 544 | uint8_t bEndpointAddress; 545 | 546 | /** Attributes which apply to the endpoint when it is configured using 547 | * the bConfigurationValue. Bits 0:1 determine the transfer type and 548 | * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for 549 | * isochronous endpoints and correspond to \ref libusb_iso_sync_type. 550 | * Bits 4:5 are also only used for isochronous endpoints and correspond to 551 | * \ref libusb_iso_usage_type. Bits 6:7 are reserved. 552 | */ 553 | uint8_t bmAttributes; 554 | 555 | /** Maximum packet size this endpoint is capable of sending/receiving. */ 556 | uint16_t wMaxPacketSize; 557 | 558 | /** Interval for polling endpoint for data transfers. */ 559 | uint8_t bInterval; 560 | 561 | /** For audio devices only: the rate at which synchronization feedback 562 | * is provided. */ 563 | uint8_t bRefresh; 564 | 565 | /** For audio devices only: the address if the synch endpoint */ 566 | uint8_t bSynchAddress; 567 | 568 | /** Extra descriptors. If libusb encounters unknown endpoint descriptors, 569 | * it will store them here, should you wish to parse them. */ 570 | const unsigned char *extra; 571 | 572 | /** Length of the extra descriptors, in bytes. */ 573 | int extra_length; 574 | }; 575 | 576 | /** \ingroup desc 577 | * A structure representing the standard USB interface descriptor. This 578 | * descriptor is documented in section 9.6.5 of the USB 3.0 specification. 579 | * All multiple-byte fields are represented in host-endian format. 580 | */ 581 | struct libusb_interface_descriptor { 582 | /** Size of this descriptor (in bytes) */ 583 | uint8_t bLength; 584 | 585 | /** Descriptor type. Will have value 586 | * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE 587 | * in this context. */ 588 | uint8_t bDescriptorType; 589 | 590 | /** Number of this interface */ 591 | uint8_t bInterfaceNumber; 592 | 593 | /** Value used to select this alternate setting for this interface */ 594 | uint8_t bAlternateSetting; 595 | 596 | /** Number of endpoints used by this interface (excluding the control 597 | * endpoint). */ 598 | uint8_t bNumEndpoints; 599 | 600 | /** USB-IF class code for this interface. See \ref libusb_class_code. */ 601 | uint8_t bInterfaceClass; 602 | 603 | /** USB-IF subclass code for this interface, qualified by the 604 | * bInterfaceClass value */ 605 | uint8_t bInterfaceSubClass; 606 | 607 | /** USB-IF protocol code for this interface, qualified by the 608 | * bInterfaceClass and bInterfaceSubClass values */ 609 | uint8_t bInterfaceProtocol; 610 | 611 | /** Index of string descriptor describing this interface */ 612 | uint8_t iInterface; 613 | 614 | /** Array of endpoint descriptors. This length of this array is determined 615 | * by the bNumEndpoints field. */ 616 | const struct libusb_endpoint_descriptor *endpoint; 617 | 618 | /** Extra descriptors. If libusb encounters unknown interface descriptors, 619 | * it will store them here, should you wish to parse them. */ 620 | const unsigned char *extra; 621 | 622 | /** Length of the extra descriptors, in bytes. */ 623 | int extra_length; 624 | }; 625 | 626 | /** \ingroup desc 627 | * A collection of alternate settings for a particular USB interface. 628 | */ 629 | struct libusb_interface { 630 | /** Array of interface descriptors. The length of this array is determined 631 | * by the num_altsetting field. */ 632 | const struct libusb_interface_descriptor *altsetting; 633 | 634 | /** The number of alternate settings that belong to this interface */ 635 | int num_altsetting; 636 | }; 637 | 638 | /** \ingroup desc 639 | * A structure representing the standard USB configuration descriptor. This 640 | * descriptor is documented in section 9.6.3 of the USB 3.0 specification. 641 | * All multiple-byte fields are represented in host-endian format. 642 | */ 643 | struct libusb_config_descriptor { 644 | /** Size of this descriptor (in bytes) */ 645 | uint8_t bLength; 646 | 647 | /** Descriptor type. Will have value 648 | * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG 649 | * in this context. */ 650 | uint8_t bDescriptorType; 651 | 652 | /** Total length of data returned for this configuration */ 653 | uint16_t wTotalLength; 654 | 655 | /** Number of interfaces supported by this configuration */ 656 | uint8_t bNumInterfaces; 657 | 658 | /** Identifier value for this configuration */ 659 | uint8_t bConfigurationValue; 660 | 661 | /** Index of string descriptor describing this configuration */ 662 | uint8_t iConfiguration; 663 | 664 | /** Configuration characteristics */ 665 | uint8_t bmAttributes; 666 | 667 | /** Maximum power consumption of the USB device from this bus in this 668 | * configuration when the device is fully operation. Expressed in units 669 | * of 2 mA when the device is operating in high-speed mode and in units 670 | * of 8 mA when the device is operating in super-speed mode. */ 671 | uint8_t MaxPower; 672 | 673 | /** Array of interfaces supported by this configuration. The length of 674 | * this array is determined by the bNumInterfaces field. */ 675 | const struct libusb_interface *interface; 676 | 677 | /** Extra descriptors. If libusb encounters unknown configuration 678 | * descriptors, it will store them here, should you wish to parse them. */ 679 | const unsigned char *extra; 680 | 681 | /** Length of the extra descriptors, in bytes. */ 682 | int extra_length; 683 | }; 684 | 685 | /** \ingroup desc 686 | * A structure representing the superspeed endpoint companion 687 | * descriptor. This descriptor is documented in section 9.6.7 of 688 | * the USB 3.0 specification. All multiple-byte fields are represented in 689 | * host-endian format. 690 | */ 691 | struct libusb_ss_endpoint_companion_descriptor { 692 | 693 | /** Size of this descriptor (in bytes) */ 694 | uint8_t bLength; 695 | 696 | /** Descriptor type. Will have value 697 | * \ref libusb_descriptor_type::LIBUSB_DT_SS_ENDPOINT_COMPANION in 698 | * this context. */ 699 | uint8_t bDescriptorType; 700 | 701 | 702 | /** The maximum number of packets the endpoint can send or 703 | * recieve as part of a burst. */ 704 | uint8_t bMaxBurst; 705 | 706 | /** In bulk EP: bits 4:0 represents the maximum number of 707 | * streams the EP supports. In isochronous EP: bits 1:0 708 | * represents the Mult - a zero based value that determines 709 | * the maximum number of packets within a service interval */ 710 | uint8_t bmAttributes; 711 | 712 | /** The total number of bytes this EP will transfer every 713 | * service interval. valid only for periodic EPs. */ 714 | uint16_t wBytesPerInterval; 715 | }; 716 | 717 | /** \ingroup desc 718 | * A generic representation of a BOS Device Capability descriptor. It is 719 | * advised to check bDevCapabilityType and call the matching 720 | * libusb_get_*_descriptor function to get a structure fully matching the type. 721 | */ 722 | struct libusb_bos_dev_capability_descriptor { 723 | /** Size of this descriptor (in bytes) */ 724 | uint8_t bLength; 725 | /** Descriptor type. Will have value 726 | * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY 727 | * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ 728 | uint8_t bDescriptorType; 729 | /** Device Capability type */ 730 | uint8_t bDevCapabilityType; 731 | /** Device Capability data (bLength - 3 bytes) */ 732 | uint8_t dev_capability_data 733 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) 734 | [] /* valid C99 code */ 735 | #else 736 | [0] /* non-standard, but usually working code */ 737 | #endif 738 | ; 739 | }; 740 | 741 | /** \ingroup desc 742 | * A structure representing the Binary Device Object Store (BOS) descriptor. 743 | * This descriptor is documented in section 9.6.2 of the USB 3.0 specification. 744 | * All multiple-byte fields are represented in host-endian format. 745 | */ 746 | struct libusb_bos_descriptor { 747 | /** Size of this descriptor (in bytes) */ 748 | uint8_t bLength; 749 | 750 | /** Descriptor type. Will have value 751 | * \ref libusb_descriptor_type::LIBUSB_DT_BOS LIBUSB_DT_BOS 752 | * in this context. */ 753 | uint8_t bDescriptorType; 754 | 755 | /** Length of this descriptor and all of its sub descriptors */ 756 | uint16_t wTotalLength; 757 | 758 | /** The number of separate device capability descriptors in 759 | * the BOS */ 760 | uint8_t bNumDeviceCaps; 761 | 762 | /** bNumDeviceCap Device Capability Descriptors */ 763 | struct libusb_bos_dev_capability_descriptor *dev_capability 764 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) 765 | [] /* valid C99 code */ 766 | #else 767 | [0] /* non-standard, but usually working code */ 768 | #endif 769 | ; 770 | }; 771 | 772 | /** \ingroup desc 773 | * A structure representing the USB 2.0 Extension descriptor 774 | * This descriptor is documented in section 9.6.2.1 of the USB 3.0 specification. 775 | * All multiple-byte fields are represented in host-endian format. 776 | */ 777 | struct libusb_usb_2_0_extension_descriptor { 778 | /** Size of this descriptor (in bytes) */ 779 | uint8_t bLength; 780 | 781 | /** Descriptor type. Will have value 782 | * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY 783 | * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ 784 | uint8_t bDescriptorType; 785 | 786 | /** Capability type. Will have value 787 | * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION 788 | * LIBUSB_BT_USB_2_0_EXTENSION in this context. */ 789 | uint8_t bDevCapabilityType; 790 | 791 | /** Bitmap encoding of supported device level features. 792 | * A value of one in a bit location indicates a feature is 793 | * supported; a value of zero indicates it is not supported. 794 | * See \ref libusb_usb_2_0_extension_attributes. */ 795 | uint32_t bmAttributes; 796 | }; 797 | 798 | /** \ingroup desc 799 | * A structure representing the SuperSpeed USB Device Capability descriptor 800 | * This descriptor is documented in section 9.6.2.2 of the USB 3.0 specification. 801 | * All multiple-byte fields are represented in host-endian format. 802 | */ 803 | struct libusb_ss_usb_device_capability_descriptor { 804 | /** Size of this descriptor (in bytes) */ 805 | uint8_t bLength; 806 | 807 | /** Descriptor type. Will have value 808 | * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY 809 | * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ 810 | uint8_t bDescriptorType; 811 | 812 | /** Capability type. Will have value 813 | * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY 814 | * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY in this context. */ 815 | uint8_t bDevCapabilityType; 816 | 817 | /** Bitmap encoding of supported device level features. 818 | * A value of one in a bit location indicates a feature is 819 | * supported; a value of zero indicates it is not supported. 820 | * See \ref libusb_ss_usb_device_capability_attributes. */ 821 | uint8_t bmAttributes; 822 | 823 | /** Bitmap encoding of the speed supported by this device when 824 | * operating in SuperSpeed mode. See \ref libusb_supported_speed. */ 825 | uint16_t wSpeedSupported; 826 | 827 | /** The lowest speed at which all the functionality supported 828 | * by the device is available to the user. For example if the 829 | * device supports all its functionality when connected at 830 | * full speed and above then it sets this value to 1. */ 831 | uint8_t bFunctionalitySupport; 832 | 833 | /** U1 Device Exit Latency. */ 834 | uint8_t bU1DevExitLat; 835 | 836 | /** U2 Device Exit Latency. */ 837 | uint16_t bU2DevExitLat; 838 | }; 839 | 840 | /** \ingroup desc 841 | * A structure representing the Container ID descriptor. 842 | * This descriptor is documented in section 9.6.2.3 of the USB 3.0 specification. 843 | * All multiple-byte fields, except UUIDs, are represented in host-endian format. 844 | */ 845 | struct libusb_container_id_descriptor { 846 | /** Size of this descriptor (in bytes) */ 847 | uint8_t bLength; 848 | 849 | /** Descriptor type. Will have value 850 | * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY 851 | * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ 852 | uint8_t bDescriptorType; 853 | 854 | /** Capability type. Will have value 855 | * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID 856 | * LIBUSB_BT_CONTAINER_ID in this context. */ 857 | uint8_t bDevCapabilityType; 858 | 859 | /** Reserved field */ 860 | uint8_t bReserved; 861 | 862 | /** 128 bit UUID */ 863 | uint8_t ContainerID[16]; 864 | }; 865 | 866 | /** \ingroup asyncio 867 | * Setup packet for control transfers. */ 868 | struct libusb_control_setup { 869 | /** Request type. Bits 0:4 determine recipient, see 870 | * \ref libusb_request_recipient. Bits 5:6 determine type, see 871 | * \ref libusb_request_type. Bit 7 determines data transfer direction, see 872 | * \ref libusb_endpoint_direction. 873 | */ 874 | uint8_t bmRequestType; 875 | 876 | /** Request. If the type bits of bmRequestType are equal to 877 | * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD 878 | * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to 879 | * \ref libusb_standard_request. For other cases, use of this field is 880 | * application-specific. */ 881 | uint8_t bRequest; 882 | 883 | /** Value. Varies according to request */ 884 | uint16_t wValue; 885 | 886 | /** Index. Varies according to request, typically used to pass an index 887 | * or offset */ 888 | uint16_t wIndex; 889 | 890 | /** Number of bytes to transfer */ 891 | uint16_t wLength; 892 | }; 893 | 894 | #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup)) 895 | 896 | /* libusb */ 897 | 898 | struct libusb_context; 899 | struct libusb_device; 900 | struct libusb_device_handle; 901 | 902 | /** \ingroup lib 903 | * Structure providing the version of the libusb runtime 904 | */ 905 | struct libusb_version { 906 | /** Library major version. */ 907 | const uint16_t major; 908 | 909 | /** Library minor version. */ 910 | const uint16_t minor; 911 | 912 | /** Library micro version. */ 913 | const uint16_t micro; 914 | 915 | /** Library nano version. */ 916 | const uint16_t nano; 917 | 918 | /** Library release candidate suffix string, e.g. "-rc4". */ 919 | const char *rc; 920 | 921 | /** For ABI compatibility only. */ 922 | const char* describe; 923 | }; 924 | 925 | /** \ingroup lib 926 | * Structure representing a libusb session. The concept of individual libusb 927 | * sessions allows for your program to use two libraries (or dynamically 928 | * load two modules) which both independently use libusb. This will prevent 929 | * interference between the individual libusb users - for example 930 | * libusb_set_debug() will not affect the other user of the library, and 931 | * libusb_exit() will not destroy resources that the other user is still 932 | * using. 933 | * 934 | * Sessions are created by libusb_init() and destroyed through libusb_exit(). 935 | * If your application is guaranteed to only ever include a single libusb 936 | * user (i.e. you), you do not have to worry about contexts: pass NULL in 937 | * every function call where a context is required. The default context 938 | * will be used. 939 | * 940 | * For more information, see \ref contexts. 941 | */ 942 | typedef struct libusb_context libusb_context; 943 | 944 | /** \ingroup dev 945 | * Structure representing a USB device detected on the system. This is an 946 | * opaque type for which you are only ever provided with a pointer, usually 947 | * originating from libusb_get_device_list(). 948 | * 949 | * Certain operations can be performed on a device, but in order to do any 950 | * I/O you will have to first obtain a device handle using libusb_open(). 951 | * 952 | * Devices are reference counted with libusb_ref_device() and 953 | * libusb_unref_device(), and are freed when the reference count reaches 0. 954 | * New devices presented by libusb_get_device_list() have a reference count of 955 | * 1, and libusb_free_device_list() can optionally decrease the reference count 956 | * on all devices in the list. libusb_open() adds another reference which is 957 | * later destroyed by libusb_close(). 958 | */ 959 | typedef struct libusb_device libusb_device; 960 | 961 | 962 | /** \ingroup dev 963 | * Structure representing a handle on a USB device. This is an opaque type for 964 | * which you are only ever provided with a pointer, usually originating from 965 | * libusb_open(). 966 | * 967 | * A device handle is used to perform I/O and other operations. When finished 968 | * with a device handle, you should call libusb_close(). 969 | */ 970 | typedef struct libusb_device_handle libusb_device_handle; 971 | 972 | /** \ingroup dev 973 | * Speed codes. Indicates the speed at which the device is operating. 974 | */ 975 | enum libusb_speed { 976 | /** The OS doesn't report or know the device speed. */ 977 | LIBUSB_SPEED_UNKNOWN = 0, 978 | 979 | /** The device is operating at low speed (1.5MBit/s). */ 980 | LIBUSB_SPEED_LOW = 1, 981 | 982 | /** The device is operating at full speed (12MBit/s). */ 983 | LIBUSB_SPEED_FULL = 2, 984 | 985 | /** The device is operating at high speed (480MBit/s). */ 986 | LIBUSB_SPEED_HIGH = 3, 987 | 988 | /** The device is operating at super speed (5000MBit/s). */ 989 | LIBUSB_SPEED_SUPER = 4, 990 | }; 991 | 992 | /** \ingroup dev 993 | * Supported speeds (wSpeedSupported) bitfield. Indicates what 994 | * speeds the device supports. 995 | */ 996 | enum libusb_supported_speed { 997 | /** Low speed operation supported (1.5MBit/s). */ 998 | LIBUSB_LOW_SPEED_OPERATION = 1, 999 | 1000 | /** Full speed operation supported (12MBit/s). */ 1001 | LIBUSB_FULL_SPEED_OPERATION = 2, 1002 | 1003 | /** High speed operation supported (480MBit/s). */ 1004 | LIBUSB_HIGH_SPEED_OPERATION = 4, 1005 | 1006 | /** Superspeed operation supported (5000MBit/s). */ 1007 | LIBUSB_SUPER_SPEED_OPERATION = 8, 1008 | }; 1009 | 1010 | /** \ingroup dev 1011 | * Masks for the bits of the 1012 | * \ref libusb_usb_2_0_extension_descriptor::bmAttributes "bmAttributes" field 1013 | * of the USB 2.0 Extension descriptor. 1014 | */ 1015 | enum libusb_usb_2_0_extension_attributes { 1016 | /** Supports Link Power Management (LPM) */ 1017 | LIBUSB_BM_LPM_SUPPORT = 2, 1018 | }; 1019 | 1020 | /** \ingroup dev 1021 | * Masks for the bits of the 1022 | * \ref libusb_ss_usb_device_capability_descriptor::bmAttributes "bmAttributes" field 1023 | * field of the SuperSpeed USB Device Capability descriptor. 1024 | */ 1025 | enum libusb_ss_usb_device_capability_attributes { 1026 | /** Supports Latency Tolerance Messages (LTM) */ 1027 | LIBUSB_BM_LTM_SUPPORT = 2, 1028 | }; 1029 | 1030 | /** \ingroup dev 1031 | * USB capability types 1032 | */ 1033 | enum libusb_bos_type { 1034 | /** Wireless USB device capability */ 1035 | LIBUSB_BT_WIRELESS_USB_DEVICE_CAPABILITY = 1, 1036 | 1037 | /** USB 2.0 extensions */ 1038 | LIBUSB_BT_USB_2_0_EXTENSION = 2, 1039 | 1040 | /** SuperSpeed USB device capability */ 1041 | LIBUSB_BT_SS_USB_DEVICE_CAPABILITY = 3, 1042 | 1043 | /** Container ID type */ 1044 | LIBUSB_BT_CONTAINER_ID = 4, 1045 | }; 1046 | 1047 | /** \ingroup misc 1048 | * Error codes. Most libusb functions return 0 on success or one of these 1049 | * codes on failure. 1050 | * You can call libusb_error_name() to retrieve a string representation of an 1051 | * error code or libusb_strerror() to get an end-user suitable description of 1052 | * an error code. 1053 | */ 1054 | enum libusb_error { 1055 | /** Success (no error) */ 1056 | LIBUSB_SUCCESS = 0, 1057 | 1058 | /** Input/output error */ 1059 | LIBUSB_ERROR_IO = -1, 1060 | 1061 | /** Invalid parameter */ 1062 | LIBUSB_ERROR_INVALID_PARAM = -2, 1063 | 1064 | /** Access denied (insufficient permissions) */ 1065 | LIBUSB_ERROR_ACCESS = -3, 1066 | 1067 | /** No such device (it may have been disconnected) */ 1068 | LIBUSB_ERROR_NO_DEVICE = -4, 1069 | 1070 | /** Entity not found */ 1071 | LIBUSB_ERROR_NOT_FOUND = -5, 1072 | 1073 | /** Resource busy */ 1074 | LIBUSB_ERROR_BUSY = -6, 1075 | 1076 | /** Operation timed out */ 1077 | LIBUSB_ERROR_TIMEOUT = -7, 1078 | 1079 | /** Overflow */ 1080 | LIBUSB_ERROR_OVERFLOW = -8, 1081 | 1082 | /** Pipe error */ 1083 | LIBUSB_ERROR_PIPE = -9, 1084 | 1085 | /** System call interrupted (perhaps due to signal) */ 1086 | LIBUSB_ERROR_INTERRUPTED = -10, 1087 | 1088 | /** Insufficient memory */ 1089 | LIBUSB_ERROR_NO_MEM = -11, 1090 | 1091 | /** Operation not supported or unimplemented on this platform */ 1092 | LIBUSB_ERROR_NOT_SUPPORTED = -12, 1093 | 1094 | /* NB: Remember to update LIBUSB_ERROR_COUNT below as well as the 1095 | message strings in strerror.c when adding new error codes here. */ 1096 | 1097 | /** Other error */ 1098 | LIBUSB_ERROR_OTHER = -99, 1099 | }; 1100 | 1101 | /* Total number of error codes in enum libusb_error */ 1102 | #define LIBUSB_ERROR_COUNT 14 1103 | 1104 | /** \ingroup asyncio 1105 | * Transfer status codes */ 1106 | enum libusb_transfer_status { 1107 | /** Transfer completed without error. Note that this does not indicate 1108 | * that the entire amount of requested data was transferred. */ 1109 | LIBUSB_TRANSFER_COMPLETED, 1110 | 1111 | /** Transfer failed */ 1112 | LIBUSB_TRANSFER_ERROR, 1113 | 1114 | /** Transfer timed out */ 1115 | LIBUSB_TRANSFER_TIMED_OUT, 1116 | 1117 | /** Transfer was cancelled */ 1118 | LIBUSB_TRANSFER_CANCELLED, 1119 | 1120 | /** For bulk/interrupt endpoints: halt condition detected (endpoint 1121 | * stalled). For control endpoints: control request not supported. */ 1122 | LIBUSB_TRANSFER_STALL, 1123 | 1124 | /** Device was disconnected */ 1125 | LIBUSB_TRANSFER_NO_DEVICE, 1126 | 1127 | /** Device sent more data than requested */ 1128 | LIBUSB_TRANSFER_OVERFLOW, 1129 | 1130 | /* NB! Remember to update libusb_error_name() 1131 | when adding new status codes here. */ 1132 | }; 1133 | 1134 | /** \ingroup asyncio 1135 | * libusb_transfer.flags values */ 1136 | enum libusb_transfer_flags { 1137 | /** Report short frames as errors */ 1138 | LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0, 1139 | 1140 | /** Automatically free() transfer buffer during libusb_free_transfer() */ 1141 | LIBUSB_TRANSFER_FREE_BUFFER = 1<<1, 1142 | 1143 | /** Automatically call libusb_free_transfer() after callback returns. 1144 | * If this flag is set, it is illegal to call libusb_free_transfer() 1145 | * from your transfer callback, as this will result in a double-free 1146 | * when this flag is acted upon. */ 1147 | LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2, 1148 | 1149 | /** Terminate transfers that are a multiple of the endpoint's 1150 | * wMaxPacketSize with an extra zero length packet. This is useful 1151 | * when a device protocol mandates that each logical request is 1152 | * terminated by an incomplete packet (i.e. the logical requests are 1153 | * not separated by other means). 1154 | * 1155 | * This flag only affects host-to-device transfers to bulk and interrupt 1156 | * endpoints. In other situations, it is ignored. 1157 | * 1158 | * This flag only affects transfers with a length that is a multiple of 1159 | * the endpoint's wMaxPacketSize. On transfers of other lengths, this 1160 | * flag has no effect. Therefore, if you are working with a device that 1161 | * needs a ZLP whenever the end of the logical request falls on a packet 1162 | * boundary, then it is sensible to set this flag on every 1163 | * transfer (you do not have to worry about only setting it on transfers 1164 | * that end on the boundary). 1165 | * 1166 | * This flag is currently only supported on Linux. 1167 | * On other systems, libusb_submit_transfer() will return 1168 | * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set. 1169 | * 1170 | * Available since libusb-1.0.9. 1171 | */ 1172 | LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3, 1173 | }; 1174 | 1175 | /** \ingroup asyncio 1176 | * Isochronous packet descriptor. */ 1177 | struct libusb_iso_packet_descriptor { 1178 | /** Length of data to request in this packet */ 1179 | unsigned int length; 1180 | 1181 | /** Amount of data that was actually transferred */ 1182 | unsigned int actual_length; 1183 | 1184 | /** Status code for this packet */ 1185 | enum libusb_transfer_status status; 1186 | }; 1187 | 1188 | struct libusb_transfer; 1189 | 1190 | /** \ingroup asyncio 1191 | * Asynchronous transfer callback function type. When submitting asynchronous 1192 | * transfers, you pass a pointer to a callback function of this type via the 1193 | * \ref libusb_transfer::callback "callback" member of the libusb_transfer 1194 | * structure. libusb will call this function later, when the transfer has 1195 | * completed or failed. See \ref asyncio for more information. 1196 | * \param transfer The libusb_transfer struct the callback function is being 1197 | * notified about. 1198 | */ 1199 | typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer); 1200 | 1201 | /** \ingroup asyncio 1202 | * The generic USB transfer structure. The user populates this structure and 1203 | * then submits it in order to request a transfer. After the transfer has 1204 | * completed, the library populates the transfer with the results and passes 1205 | * it back to the user. 1206 | */ 1207 | struct libusb_transfer { 1208 | /** Handle of the device that this transfer will be submitted to */ 1209 | libusb_device_handle *dev_handle; 1210 | 1211 | /** A bitwise OR combination of \ref libusb_transfer_flags. */ 1212 | uint8_t flags; 1213 | 1214 | /** Address of the endpoint where this transfer will be sent. */ 1215 | unsigned char endpoint; 1216 | 1217 | /** Type of the endpoint from \ref libusb_transfer_type */ 1218 | unsigned char type; 1219 | 1220 | /** Timeout for this transfer in millseconds. A value of 0 indicates no 1221 | * timeout. */ 1222 | unsigned int timeout; 1223 | 1224 | /** The status of the transfer. Read-only, and only for use within 1225 | * transfer callback function. 1226 | * 1227 | * If this is an isochronous transfer, this field may read COMPLETED even 1228 | * if there were errors in the frames. Use the 1229 | * \ref libusb_iso_packet_descriptor::status "status" field in each packet 1230 | * to determine if errors occurred. */ 1231 | enum libusb_transfer_status status; 1232 | 1233 | /** Length of the data buffer */ 1234 | int length; 1235 | 1236 | /** Actual length of data that was transferred. Read-only, and only for 1237 | * use within transfer callback function. Not valid for isochronous 1238 | * endpoint transfers. */ 1239 | int actual_length; 1240 | 1241 | /** Callback function. This will be invoked when the transfer completes, 1242 | * fails, or is cancelled. */ 1243 | libusb_transfer_cb_fn callback; 1244 | 1245 | /** User context data to pass to the callback function. */ 1246 | void *user_data; 1247 | 1248 | /** Data buffer */ 1249 | unsigned char *buffer; 1250 | 1251 | /** Number of isochronous packets. Only used for I/O with isochronous 1252 | * endpoints. */ 1253 | int num_iso_packets; 1254 | 1255 | /** Isochronous packet descriptors, for isochronous transfers only. */ 1256 | struct libusb_iso_packet_descriptor iso_packet_desc 1257 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) 1258 | [] /* valid C99 code */ 1259 | #else 1260 | [0] /* non-standard, but usually working code */ 1261 | #endif 1262 | ; 1263 | }; 1264 | 1265 | /** \ingroup misc 1266 | * Capabilities supported by an instance of libusb on the current running 1267 | * platform. Test if the loaded library supports a given capability by calling 1268 | * \ref libusb_has_capability(). 1269 | */ 1270 | enum libusb_capability { 1271 | /** The libusb_has_capability() API is available. */ 1272 | LIBUSB_CAP_HAS_CAPABILITY = 0x0000, 1273 | /** Hotplug support is available on this platform. */ 1274 | LIBUSB_CAP_HAS_HOTPLUG = 0x0001, 1275 | /** The library can access HID devices without requiring user intervention. 1276 | * Note that before being able to actually access an HID device, you may 1277 | * still have to call additional libusb functions such as 1278 | * \ref libusb_detach_kernel_driver(). */ 1279 | LIBUSB_CAP_HAS_HID_ACCESS = 0x0100, 1280 | /** The library supports detaching of the default USB driver, using 1281 | * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */ 1282 | LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101 1283 | }; 1284 | 1285 | /** \ingroup lib 1286 | * Log message levels. 1287 | * - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default) 1288 | * - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr 1289 | * - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr 1290 | * - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stdout, warning 1291 | * and error messages are printed to stderr 1292 | * - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stdout, 1293 | * warnings and errors to stderr 1294 | */ 1295 | enum libusb_log_level { 1296 | LIBUSB_LOG_LEVEL_NONE = 0, 1297 | LIBUSB_LOG_LEVEL_ERROR, 1298 | LIBUSB_LOG_LEVEL_WARNING, 1299 | LIBUSB_LOG_LEVEL_INFO, 1300 | LIBUSB_LOG_LEVEL_DEBUG, 1301 | }; 1302 | 1303 | int LIBUSB_CALL libusb_init(libusb_context **ctx); 1304 | void LIBUSB_CALL libusb_exit(libusb_context *ctx); 1305 | void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level); 1306 | const struct libusb_version * LIBUSB_CALL libusb_get_version(void); 1307 | int LIBUSB_CALL libusb_has_capability(uint32_t capability); 1308 | const char * LIBUSB_CALL libusb_error_name(int errcode); 1309 | int LIBUSB_CALL libusb_setlocale(const char *locale); 1310 | const char * LIBUSB_CALL libusb_strerror(enum libusb_error errcode); 1311 | 1312 | ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx, 1313 | libusb_device ***list); 1314 | void LIBUSB_CALL libusb_free_device_list(libusb_device **list, 1315 | int unref_devices); 1316 | libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev); 1317 | void LIBUSB_CALL libusb_unref_device(libusb_device *dev); 1318 | 1319 | int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev, 1320 | int *config); 1321 | int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev, 1322 | struct libusb_device_descriptor *desc); 1323 | int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev, 1324 | struct libusb_config_descriptor **config); 1325 | int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev, 1326 | uint8_t config_index, struct libusb_config_descriptor **config); 1327 | int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev, 1328 | uint8_t bConfigurationValue, struct libusb_config_descriptor **config); 1329 | void LIBUSB_CALL libusb_free_config_descriptor( 1330 | struct libusb_config_descriptor *config); 1331 | int LIBUSB_CALL libusb_get_ss_endpoint_companion_descriptor( 1332 | struct libusb_context *ctx, 1333 | const struct libusb_endpoint_descriptor *endpoint, 1334 | struct libusb_ss_endpoint_companion_descriptor **ep_comp); 1335 | void LIBUSB_CALL libusb_free_ss_endpoint_companion_descriptor( 1336 | struct libusb_ss_endpoint_companion_descriptor *ep_comp); 1337 | int LIBUSB_CALL libusb_get_bos_descriptor(libusb_device_handle *handle, 1338 | struct libusb_bos_descriptor **bos); 1339 | void LIBUSB_CALL libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos); 1340 | int LIBUSB_CALL libusb_get_usb_2_0_extension_descriptor( 1341 | struct libusb_context *ctx, 1342 | struct libusb_bos_dev_capability_descriptor *dev_cap, 1343 | struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension); 1344 | void LIBUSB_CALL libusb_free_usb_2_0_extension_descriptor( 1345 | struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension); 1346 | int LIBUSB_CALL libusb_get_ss_usb_device_capability_descriptor( 1347 | struct libusb_context *ctx, 1348 | struct libusb_bos_dev_capability_descriptor *dev_cap, 1349 | struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap); 1350 | void LIBUSB_CALL libusb_free_ss_usb_device_capability_descriptor( 1351 | struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap); 1352 | int LIBUSB_CALL libusb_get_container_id_descriptor(struct libusb_context *ctx, 1353 | struct libusb_bos_dev_capability_descriptor *dev_cap, 1354 | struct libusb_container_id_descriptor **container_id); 1355 | void LIBUSB_CALL libusb_free_container_id_descriptor( 1356 | struct libusb_container_id_descriptor *container_id); 1357 | uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev); 1358 | uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev); 1359 | int LIBUSB_CALL libusb_get_port_numbers(libusb_device *dev, uint8_t* port_numbers, int port_numbers_len); 1360 | LIBUSB_DEPRECATED_FOR(libusb_get_port_numbers) 1361 | int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length); 1362 | libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev); 1363 | uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev); 1364 | int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev); 1365 | int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev, 1366 | unsigned char endpoint); 1367 | int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev, 1368 | unsigned char endpoint); 1369 | 1370 | int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle); 1371 | void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle); 1372 | libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle); 1373 | 1374 | int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev, 1375 | int configuration); 1376 | int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev, 1377 | int interface_number); 1378 | int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev, 1379 | int interface_number); 1380 | 1381 | libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid( 1382 | libusb_context *ctx, uint16_t vendor_id, uint16_t product_id); 1383 | 1384 | int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev, 1385 | int interface_number, int alternate_setting); 1386 | int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev, 1387 | unsigned char endpoint); 1388 | int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev); 1389 | 1390 | int LIBUSB_CALL libusb_alloc_streams(libusb_device_handle *dev, 1391 | uint32_t num_streams, unsigned char *endpoints, int num_endpoints); 1392 | int LIBUSB_CALL libusb_free_streams(libusb_device_handle *dev, 1393 | unsigned char *endpoints, int num_endpoints); 1394 | 1395 | int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev, 1396 | int interface_number); 1397 | int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev, 1398 | int interface_number); 1399 | int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev, 1400 | int interface_number); 1401 | int LIBUSB_CALL libusb_set_auto_detach_kernel_driver( 1402 | libusb_device_handle *dev, int enable); 1403 | 1404 | /* async I/O */ 1405 | 1406 | /** \ingroup asyncio 1407 | * Get the data section of a control transfer. This convenience function is here 1408 | * to remind you that the data does not start until 8 bytes into the actual 1409 | * buffer, as the setup packet comes first. 1410 | * 1411 | * Calling this function only makes sense from a transfer callback function, 1412 | * or situations where you have already allocated a suitably sized buffer at 1413 | * transfer->buffer. 1414 | * 1415 | * \param transfer a transfer 1416 | * \returns pointer to the first byte of the data section 1417 | */ 1418 | static inline unsigned char *libusb_control_transfer_get_data( 1419 | struct libusb_transfer *transfer) 1420 | { 1421 | return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE; 1422 | } 1423 | 1424 | /** \ingroup asyncio 1425 | * Get the control setup packet of a control transfer. This convenience 1426 | * function is here to remind you that the control setup occupies the first 1427 | * 8 bytes of the transfer data buffer. 1428 | * 1429 | * Calling this function only makes sense from a transfer callback function, 1430 | * or situations where you have already allocated a suitably sized buffer at 1431 | * transfer->buffer. 1432 | * 1433 | * \param transfer a transfer 1434 | * \returns a casted pointer to the start of the transfer data buffer 1435 | */ 1436 | static inline struct libusb_control_setup *libusb_control_transfer_get_setup( 1437 | struct libusb_transfer *transfer) 1438 | { 1439 | return (struct libusb_control_setup *)(void *) transfer->buffer; 1440 | } 1441 | 1442 | /** \ingroup asyncio 1443 | * Helper function to populate the setup packet (first 8 bytes of the data 1444 | * buffer) for a control transfer. The wIndex, wValue and wLength values should 1445 | * be given in host-endian byte order. 1446 | * 1447 | * \param buffer buffer to output the setup packet into 1448 | * This pointer must be aligned to at least 2 bytes boundary. 1449 | * \param bmRequestType see the 1450 | * \ref libusb_control_setup::bmRequestType "bmRequestType" field of 1451 | * \ref libusb_control_setup 1452 | * \param bRequest see the 1453 | * \ref libusb_control_setup::bRequest "bRequest" field of 1454 | * \ref libusb_control_setup 1455 | * \param wValue see the 1456 | * \ref libusb_control_setup::wValue "wValue" field of 1457 | * \ref libusb_control_setup 1458 | * \param wIndex see the 1459 | * \ref libusb_control_setup::wIndex "wIndex" field of 1460 | * \ref libusb_control_setup 1461 | * \param wLength see the 1462 | * \ref libusb_control_setup::wLength "wLength" field of 1463 | * \ref libusb_control_setup 1464 | */ 1465 | static inline void libusb_fill_control_setup(unsigned char *buffer, 1466 | uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 1467 | uint16_t wLength) 1468 | { 1469 | struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer; 1470 | setup->bmRequestType = bmRequestType; 1471 | setup->bRequest = bRequest; 1472 | setup->wValue = libusb_cpu_to_le16(wValue); 1473 | setup->wIndex = libusb_cpu_to_le16(wIndex); 1474 | setup->wLength = libusb_cpu_to_le16(wLength); 1475 | } 1476 | 1477 | struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets); 1478 | int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer); 1479 | int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer); 1480 | void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer); 1481 | void LIBUSB_CALL libusb_transfer_set_stream_id( 1482 | struct libusb_transfer *transfer, uint32_t stream_id); 1483 | uint32_t LIBUSB_CALL libusb_transfer_get_stream_id( 1484 | struct libusb_transfer *transfer); 1485 | 1486 | /** \ingroup asyncio 1487 | * Helper function to populate the required \ref libusb_transfer fields 1488 | * for a control transfer. 1489 | * 1490 | * If you pass a transfer buffer to this function, the first 8 bytes will 1491 | * be interpreted as a control setup packet, and the wLength field will be 1492 | * used to automatically populate the \ref libusb_transfer::length "length" 1493 | * field of the transfer. Therefore the recommended approach is: 1494 | * -# Allocate a suitably sized data buffer (including space for control setup) 1495 | * -# Call libusb_fill_control_setup() 1496 | * -# If this is a host-to-device transfer with a data stage, put the data 1497 | * in place after the setup packet 1498 | * -# Call this function 1499 | * -# Call libusb_submit_transfer() 1500 | * 1501 | * It is also legal to pass a NULL buffer to this function, in which case this 1502 | * function will not attempt to populate the length field. Remember that you 1503 | * must then populate the buffer and length fields later. 1504 | * 1505 | * \param transfer the transfer to populate 1506 | * \param dev_handle handle of the device that will handle the transfer 1507 | * \param buffer data buffer. If provided, this function will interpret the 1508 | * first 8 bytes as a setup packet and infer the transfer length from that. 1509 | * This pointer must be aligned to at least 2 bytes boundary. 1510 | * \param callback callback function to be invoked on transfer completion 1511 | * \param user_data user data to pass to callback function 1512 | * \param timeout timeout for the transfer in milliseconds 1513 | */ 1514 | static inline void libusb_fill_control_transfer( 1515 | struct libusb_transfer *transfer, libusb_device_handle *dev_handle, 1516 | unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data, 1517 | unsigned int timeout) 1518 | { 1519 | struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer; 1520 | transfer->dev_handle = dev_handle; 1521 | transfer->endpoint = 0; 1522 | transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; 1523 | transfer->timeout = timeout; 1524 | transfer->buffer = buffer; 1525 | if (setup) 1526 | transfer->length = (int) (LIBUSB_CONTROL_SETUP_SIZE 1527 | + libusb_le16_to_cpu(setup->wLength)); 1528 | transfer->user_data = user_data; 1529 | transfer->callback = callback; 1530 | } 1531 | 1532 | /** \ingroup asyncio 1533 | * Helper function to populate the required \ref libusb_transfer fields 1534 | * for a bulk transfer. 1535 | * 1536 | * \param transfer the transfer to populate 1537 | * \param dev_handle handle of the device that will handle the transfer 1538 | * \param endpoint address of the endpoint where this transfer will be sent 1539 | * \param buffer data buffer 1540 | * \param length length of data buffer 1541 | * \param callback callback function to be invoked on transfer completion 1542 | * \param user_data user data to pass to callback function 1543 | * \param timeout timeout for the transfer in milliseconds 1544 | */ 1545 | static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer, 1546 | libusb_device_handle *dev_handle, unsigned char endpoint, 1547 | unsigned char *buffer, int length, libusb_transfer_cb_fn callback, 1548 | void *user_data, unsigned int timeout) 1549 | { 1550 | transfer->dev_handle = dev_handle; 1551 | transfer->endpoint = endpoint; 1552 | transfer->type = LIBUSB_TRANSFER_TYPE_BULK; 1553 | transfer->timeout = timeout; 1554 | transfer->buffer = buffer; 1555 | transfer->length = length; 1556 | transfer->user_data = user_data; 1557 | transfer->callback = callback; 1558 | } 1559 | 1560 | /** \ingroup asyncio 1561 | * Helper function to populate the required \ref libusb_transfer fields 1562 | * for a bulk transfer using bulk streams. 1563 | * 1564 | * Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103 1565 | * 1566 | * \param transfer the transfer to populate 1567 | * \param dev_handle handle of the device that will handle the transfer 1568 | * \param endpoint address of the endpoint where this transfer will be sent 1569 | * \param stream_id bulk stream id for this transfer 1570 | * \param buffer data buffer 1571 | * \param length length of data buffer 1572 | * \param callback callback function to be invoked on transfer completion 1573 | * \param user_data user data to pass to callback function 1574 | * \param timeout timeout for the transfer in milliseconds 1575 | */ 1576 | static inline void libusb_fill_bulk_stream_transfer( 1577 | struct libusb_transfer *transfer, libusb_device_handle *dev_handle, 1578 | unsigned char endpoint, uint32_t stream_id, 1579 | unsigned char *buffer, int length, libusb_transfer_cb_fn callback, 1580 | void *user_data, unsigned int timeout) 1581 | { 1582 | libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, 1583 | length, callback, user_data, timeout); 1584 | transfer->type = LIBUSB_TRANSFER_TYPE_BULK_STREAM; 1585 | libusb_transfer_set_stream_id(transfer, stream_id); 1586 | } 1587 | 1588 | /** \ingroup asyncio 1589 | * Helper function to populate the required \ref libusb_transfer fields 1590 | * for an interrupt transfer. 1591 | * 1592 | * \param transfer the transfer to populate 1593 | * \param dev_handle handle of the device that will handle the transfer 1594 | * \param endpoint address of the endpoint where this transfer will be sent 1595 | * \param buffer data buffer 1596 | * \param length length of data buffer 1597 | * \param callback callback function to be invoked on transfer completion 1598 | * \param user_data user data to pass to callback function 1599 | * \param timeout timeout for the transfer in milliseconds 1600 | */ 1601 | static inline void libusb_fill_interrupt_transfer( 1602 | struct libusb_transfer *transfer, libusb_device_handle *dev_handle, 1603 | unsigned char endpoint, unsigned char *buffer, int length, 1604 | libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) 1605 | { 1606 | transfer->dev_handle = dev_handle; 1607 | transfer->endpoint = endpoint; 1608 | transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; 1609 | transfer->timeout = timeout; 1610 | transfer->buffer = buffer; 1611 | transfer->length = length; 1612 | transfer->user_data = user_data; 1613 | transfer->callback = callback; 1614 | } 1615 | 1616 | /** \ingroup asyncio 1617 | * Helper function to populate the required \ref libusb_transfer fields 1618 | * for an isochronous transfer. 1619 | * 1620 | * \param transfer the transfer to populate 1621 | * \param dev_handle handle of the device that will handle the transfer 1622 | * \param endpoint address of the endpoint where this transfer will be sent 1623 | * \param buffer data buffer 1624 | * \param length length of data buffer 1625 | * \param num_iso_packets the number of isochronous packets 1626 | * \param callback callback function to be invoked on transfer completion 1627 | * \param user_data user data to pass to callback function 1628 | * \param timeout timeout for the transfer in milliseconds 1629 | */ 1630 | static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer, 1631 | libusb_device_handle *dev_handle, unsigned char endpoint, 1632 | unsigned char *buffer, int length, int num_iso_packets, 1633 | libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) 1634 | { 1635 | transfer->dev_handle = dev_handle; 1636 | transfer->endpoint = endpoint; 1637 | transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; 1638 | transfer->timeout = timeout; 1639 | transfer->buffer = buffer; 1640 | transfer->length = length; 1641 | transfer->num_iso_packets = num_iso_packets; 1642 | transfer->user_data = user_data; 1643 | transfer->callback = callback; 1644 | } 1645 | 1646 | /** \ingroup asyncio 1647 | * Convenience function to set the length of all packets in an isochronous 1648 | * transfer, based on the num_iso_packets field in the transfer structure. 1649 | * 1650 | * \param transfer a transfer 1651 | * \param length the length to set in each isochronous packet descriptor 1652 | * \see libusb_get_max_packet_size() 1653 | */ 1654 | static inline void libusb_set_iso_packet_lengths( 1655 | struct libusb_transfer *transfer, unsigned int length) 1656 | { 1657 | int i; 1658 | for (i = 0; i < transfer->num_iso_packets; i++) 1659 | transfer->iso_packet_desc[i].length = length; 1660 | } 1661 | 1662 | /** \ingroup asyncio 1663 | * Convenience function to locate the position of an isochronous packet 1664 | * within the buffer of an isochronous transfer. 1665 | * 1666 | * This is a thorough function which loops through all preceding packets, 1667 | * accumulating their lengths to find the position of the specified packet. 1668 | * Typically you will assign equal lengths to each packet in the transfer, 1669 | * and hence the above method is sub-optimal. You may wish to use 1670 | * libusb_get_iso_packet_buffer_simple() instead. 1671 | * 1672 | * \param transfer a transfer 1673 | * \param packet the packet to return the address of 1674 | * \returns the base address of the packet buffer inside the transfer buffer, 1675 | * or NULL if the packet does not exist. 1676 | * \see libusb_get_iso_packet_buffer_simple() 1677 | */ 1678 | static inline unsigned char *libusb_get_iso_packet_buffer( 1679 | struct libusb_transfer *transfer, unsigned int packet) 1680 | { 1681 | int i; 1682 | size_t offset = 0; 1683 | int _packet; 1684 | 1685 | /* oops..slight bug in the API. packet is an unsigned int, but we use 1686 | * signed integers almost everywhere else. range-check and convert to 1687 | * signed to avoid compiler warnings. FIXME for libusb-2. */ 1688 | if (packet > INT_MAX) 1689 | return NULL; 1690 | _packet = (int) packet; 1691 | 1692 | if (_packet >= transfer->num_iso_packets) 1693 | return NULL; 1694 | 1695 | for (i = 0; i < _packet; i++) 1696 | offset += transfer->iso_packet_desc[i].length; 1697 | 1698 | return transfer->buffer + offset; 1699 | } 1700 | 1701 | /** \ingroup asyncio 1702 | * Convenience function to locate the position of an isochronous packet 1703 | * within the buffer of an isochronous transfer, for transfers where each 1704 | * packet is of identical size. 1705 | * 1706 | * This function relies on the assumption that every packet within the transfer 1707 | * is of identical size to the first packet. Calculating the location of 1708 | * the packet buffer is then just a simple calculation: 1709 | * buffer + (packet_size * packet) 1710 | * 1711 | * Do not use this function on transfers other than those that have identical 1712 | * packet lengths for each packet. 1713 | * 1714 | * \param transfer a transfer 1715 | * \param packet the packet to return the address of 1716 | * \returns the base address of the packet buffer inside the transfer buffer, 1717 | * or NULL if the packet does not exist. 1718 | * \see libusb_get_iso_packet_buffer() 1719 | */ 1720 | static inline unsigned char *libusb_get_iso_packet_buffer_simple( 1721 | struct libusb_transfer *transfer, unsigned int packet) 1722 | { 1723 | int _packet; 1724 | 1725 | /* oops..slight bug in the API. packet is an unsigned int, but we use 1726 | * signed integers almost everywhere else. range-check and convert to 1727 | * signed to avoid compiler warnings. FIXME for libusb-2. */ 1728 | if (packet > INT_MAX) 1729 | return NULL; 1730 | _packet = (int) packet; 1731 | 1732 | if (_packet >= transfer->num_iso_packets) 1733 | return NULL; 1734 | 1735 | return transfer->buffer + ((int) transfer->iso_packet_desc[0].length * _packet); 1736 | } 1737 | 1738 | /* sync I/O */ 1739 | 1740 | int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle, 1741 | uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 1742 | unsigned char *data, uint16_t wLength, unsigned int timeout); 1743 | 1744 | int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle, 1745 | unsigned char endpoint, unsigned char *data, int length, 1746 | int *actual_length, unsigned int timeout); 1747 | 1748 | int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle, 1749 | unsigned char endpoint, unsigned char *data, int length, 1750 | int *actual_length, unsigned int timeout); 1751 | 1752 | /** \ingroup desc 1753 | * Retrieve a descriptor from the default control pipe. 1754 | * This is a convenience function which formulates the appropriate control 1755 | * message to retrieve the descriptor. 1756 | * 1757 | * \param dev a device handle 1758 | * \param desc_type the descriptor type, see \ref libusb_descriptor_type 1759 | * \param desc_index the index of the descriptor to retrieve 1760 | * \param data output buffer for descriptor 1761 | * \param length size of data buffer 1762 | * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure 1763 | */ 1764 | static inline int libusb_get_descriptor(libusb_device_handle *dev, 1765 | uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length) 1766 | { 1767 | return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, 1768 | LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t) ((desc_type << 8) | desc_index), 1769 | 0, data, (uint16_t) length, 1000); 1770 | } 1771 | 1772 | /** \ingroup desc 1773 | * Retrieve a descriptor from a device. 1774 | * This is a convenience function which formulates the appropriate control 1775 | * message to retrieve the descriptor. The string returned is Unicode, as 1776 | * detailed in the USB specifications. 1777 | * 1778 | * \param dev a device handle 1779 | * \param desc_index the index of the descriptor to retrieve 1780 | * \param langid the language ID for the string descriptor 1781 | * \param data output buffer for descriptor 1782 | * \param length size of data buffer 1783 | * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure 1784 | * \see libusb_get_string_descriptor_ascii() 1785 | */ 1786 | static inline int libusb_get_string_descriptor(libusb_device_handle *dev, 1787 | uint8_t desc_index, uint16_t langid, unsigned char *data, int length) 1788 | { 1789 | return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, 1790 | LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index), 1791 | langid, data, (uint16_t) length, 1000); 1792 | } 1793 | 1794 | int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev, 1795 | uint8_t desc_index, unsigned char *data, int length); 1796 | 1797 | /* polling and timeouts */ 1798 | 1799 | int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx); 1800 | void LIBUSB_CALL libusb_lock_events(libusb_context *ctx); 1801 | void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx); 1802 | int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx); 1803 | int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx); 1804 | void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx); 1805 | void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx); 1806 | int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv); 1807 | 1808 | int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx, 1809 | struct timeval *tv); 1810 | int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx, 1811 | struct timeval *tv, int *completed); 1812 | int LIBUSB_CALL libusb_handle_events(libusb_context *ctx); 1813 | int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed); 1814 | int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx, 1815 | struct timeval *tv); 1816 | int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx); 1817 | int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx, 1818 | struct timeval *tv); 1819 | 1820 | /** \ingroup poll 1821 | * File descriptor for polling 1822 | */ 1823 | struct libusb_pollfd { 1824 | /** Numeric file descriptor */ 1825 | int fd; 1826 | 1827 | /** Event flags to poll for from . POLLIN indicates that you 1828 | * should monitor this file descriptor for becoming ready to read from, 1829 | * and POLLOUT indicates that you should monitor this file descriptor for 1830 | * nonblocking write readiness. */ 1831 | short events; 1832 | }; 1833 | 1834 | /** \ingroup poll 1835 | * Callback function, invoked when a new file descriptor should be added 1836 | * to the set of file descriptors monitored for events. 1837 | * \param fd the new file descriptor 1838 | * \param events events to monitor for, see \ref libusb_pollfd for a 1839 | * description 1840 | * \param user_data User data pointer specified in 1841 | * libusb_set_pollfd_notifiers() call 1842 | * \see libusb_set_pollfd_notifiers() 1843 | */ 1844 | typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events, 1845 | void *user_data); 1846 | 1847 | /** \ingroup poll 1848 | * Callback function, invoked when a file descriptor should be removed from 1849 | * the set of file descriptors being monitored for events. After returning 1850 | * from this callback, do not use that file descriptor again. 1851 | * \param fd the file descriptor to stop monitoring 1852 | * \param user_data User data pointer specified in 1853 | * libusb_set_pollfd_notifiers() call 1854 | * \see libusb_set_pollfd_notifiers() 1855 | */ 1856 | typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data); 1857 | 1858 | const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds( 1859 | libusb_context *ctx); 1860 | void LIBUSB_CALL libusb_free_pollfds(const struct libusb_pollfd **pollfds); 1861 | void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx, 1862 | libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, 1863 | void *user_data); 1864 | 1865 | /** \ingroup hotplug 1866 | * Callback handle. 1867 | * 1868 | * Callbacks handles are generated by libusb_hotplug_register_callback() 1869 | * and can be used to deregister callbacks. Callback handles are unique 1870 | * per libusb_context and it is safe to call libusb_hotplug_deregister_callback() 1871 | * on an already deregisted callback. 1872 | * 1873 | * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 1874 | * 1875 | * For more information, see \ref hotplug. 1876 | */ 1877 | typedef int libusb_hotplug_callback_handle; 1878 | 1879 | /** \ingroup hotplug 1880 | * 1881 | * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 1882 | * 1883 | * Flags for hotplug events */ 1884 | typedef enum { 1885 | /** Default value when not using any flags. */ 1886 | LIBUSB_HOTPLUG_NO_FLAGS = 0, 1887 | 1888 | /** Arm the callback and fire it for all matching currently attached devices. */ 1889 | LIBUSB_HOTPLUG_ENUMERATE = 1<<0, 1890 | } libusb_hotplug_flag; 1891 | 1892 | /** \ingroup hotplug 1893 | * 1894 | * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 1895 | * 1896 | * Hotplug events */ 1897 | typedef enum { 1898 | /** A device has been plugged in and is ready to use */ 1899 | LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 0x01, 1900 | 1901 | /** A device has left and is no longer available. 1902 | * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device. 1903 | * It is safe to call libusb_get_device_descriptor on a device that has left */ 1904 | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 0x02, 1905 | } libusb_hotplug_event; 1906 | 1907 | /** \ingroup hotplug 1908 | * Wildcard matching for hotplug events */ 1909 | #define LIBUSB_HOTPLUG_MATCH_ANY -1 1910 | 1911 | /** \ingroup hotplug 1912 | * Hotplug callback function type. When requesting hotplug event notifications, 1913 | * you pass a pointer to a callback function of this type. 1914 | * 1915 | * This callback may be called by an internal event thread and as such it is 1916 | * recommended the callback do minimal processing before returning. 1917 | * 1918 | * libusb will call this function later, when a matching event had happened on 1919 | * a matching device. See \ref hotplug for more information. 1920 | * 1921 | * It is safe to call either libusb_hotplug_register_callback() or 1922 | * libusb_hotplug_deregister_callback() from within a callback function. 1923 | * 1924 | * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 1925 | * 1926 | * \param ctx context of this notification 1927 | * \param device libusb_device this event occurred on 1928 | * \param event event that occurred 1929 | * \param user_data user data provided when this callback was registered 1930 | * \returns bool whether this callback is finished processing events. 1931 | * returning 1 will cause this callback to be deregistered 1932 | */ 1933 | typedef int (LIBUSB_CALL *libusb_hotplug_callback_fn)(libusb_context *ctx, 1934 | libusb_device *device, 1935 | libusb_hotplug_event event, 1936 | void *user_data); 1937 | 1938 | /** \ingroup hotplug 1939 | * Register a hotplug callback function 1940 | * 1941 | * Register a callback with the libusb_context. The callback will fire 1942 | * when a matching event occurs on a matching device. The callback is 1943 | * armed until either it is deregistered with libusb_hotplug_deregister_callback() 1944 | * or the supplied callback returns 1 to indicate it is finished processing events. 1945 | * 1946 | * If the \ref LIBUSB_HOTPLUG_ENUMERATE is passed the callback will be 1947 | * called with a \ref LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED for all devices 1948 | * already plugged into the machine. Note that libusb modifies its internal 1949 | * device list from a separate thread, while calling hotplug callbacks from 1950 | * libusb_handle_events(), so it is possible for a device to already be present 1951 | * on, or removed from, its internal device list, while the hotplug callbacks 1952 | * still need to be dispatched. This means that when using \ref 1953 | * LIBUSB_HOTPLUG_ENUMERATE, your callback may be called twice for the arrival 1954 | * of the same device, once from libusb_hotplug_register_callback() and once 1955 | * from libusb_handle_events(); and/or your callback may be called for the 1956 | * removal of a device for which an arrived call was never made. 1957 | * 1958 | * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 1959 | * 1960 | * \param[in] ctx context to register this callback with 1961 | * \param[in] events bitwise or of events that will trigger this callback. See \ref 1962 | * libusb_hotplug_event 1963 | * \param[in] flags hotplug callback flags. See \ref libusb_hotplug_flag 1964 | * \param[in] vendor_id the vendor id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY 1965 | * \param[in] product_id the product id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY 1966 | * \param[in] dev_class the device class to match or \ref LIBUSB_HOTPLUG_MATCH_ANY 1967 | * \param[in] cb_fn the function to be invoked on a matching event/device 1968 | * \param[in] user_data user data to pass to the callback function 1969 | * \param[out] handle pointer to store the handle of the allocated callback (can be NULL) 1970 | * \returns LIBUSB_SUCCESS on success LIBUSB_ERROR code on failure 1971 | */ 1972 | int LIBUSB_CALL libusb_hotplug_register_callback(libusb_context *ctx, 1973 | libusb_hotplug_event events, 1974 | libusb_hotplug_flag flags, 1975 | int vendor_id, int product_id, 1976 | int dev_class, 1977 | libusb_hotplug_callback_fn cb_fn, 1978 | void *user_data, 1979 | libusb_hotplug_callback_handle *handle); 1980 | 1981 | /** \ingroup hotplug 1982 | * Deregisters a hotplug callback. 1983 | * 1984 | * Deregister a callback from a libusb_context. This function is safe to call from within 1985 | * a hotplug callback. 1986 | * 1987 | * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 1988 | * 1989 | * \param[in] ctx context this callback is registered with 1990 | * \param[in] handle the handle of the callback to deregister 1991 | */ 1992 | void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context *ctx, 1993 | libusb_hotplug_callback_handle handle); 1994 | 1995 | #ifdef __cplusplus 1996 | } 1997 | #endif 1998 | 1999 | #endif 2000 | -------------------------------------------------------------------------------- /3rdparty/libusb-1.0/lib/libusb-1.0.so: -------------------------------------------------------------------------------- 1 | libusb-1.0.so.0.1.0 -------------------------------------------------------------------------------- /3rdparty/libusb-1.0/lib/libusb-1.0.so.0: -------------------------------------------------------------------------------- 1 | libusb-1.0.so.0.1.0 -------------------------------------------------------------------------------- /3rdparty/libusb-1.0/lib/libusb-1.0.so.0.1.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/justdoit-mqr/UsbComm/991c1d9315cb2f93202ca1e654b7b881f45acfab/3rdparty/libusb-1.0/lib/libusb-1.0.so.0.1.0 -------------------------------------------------------------------------------- /UsbComm.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2021-03-15T13:30:45 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = UsbComm 12 | TEMPLATE = app 13 | 14 | #头文件包含路径 方便代码中直接包含子目录下的头文件 15 | INCLUDEPATH += 3rdparty/ 16 | 17 | SOURCES += main.cpp\ 18 | widget.cpp \ 19 | usbcomm.cpp 20 | 21 | HEADERS += widget.h \ 22 | usbcomm.h 23 | 24 | FORMS += widget.ui 25 | 26 | LIBS += -L./3rdparty/libusb-1.0/lib -lusb-1.0 27 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | Widget w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # UsbComm 2 | 这是一个使用Qt框架开发的USB应用层通信组件,内部对libusb的常用api接口进行了二次封装,方便开发使用。主要实现了两大功能:++与usb设备端进行通信数据传输(UsbComm)++ 和 ++负责对usb设备热插拔监测(UsbMonitor)++。 3 | 注:在项目的3rdparty目录下提供了libusb-1.0的头文件和库,这里是我用的Ubuntu16.04平台通过"apt install libusb-1.0-0-dev"命令安装,版本是1.0.20,对于不同的平台和环境只需要替换头文件和库即可。 4 | 5 | ## 功能概述 6 | UsbComm组件目前由三个类组成:`UsbComm`、`UsbMonitor`和`UsbEventHandler`,为了方便移植,三个类统一声明/定义在usbcomm.h和usbcomm.cpp中。 7 | ### 1.UsbComm 8 | USB通信主类,该类主要实现与usb设备端的通信数据传输。内部按需封装libusb的方法接口,并维护着当前打开的设备句柄列表和声明的接口列表,所以对于设备句柄和接口的相关操作尽量都使用该类的方法处理,不要在外边单独使用原生libusb接口,避免造成内部维护的列表失效而产生异常。 9 | ``` 10 | void findUsbDevices();//探测系统当前接入的usb设备,打印设备详细信息(调试用) 11 | 12 | /*设备初始化*/ 13 | bool openUsbDevice(QMultiMap &vpidMap);//打开指定设备(可能有多个) 14 | void closeUsbDevice(libusb_device_handle *deviceHandle);//关闭指定设备 15 | void closeAllUsbDevice();//关闭所有设备 16 | bool setUsbConfig(libusb_device_handle *deviceHandle,int bConfigurationValue=1);//激活usb设备当前配置 17 | bool claimUsbInterface(libusb_device_handle *deviceHandle,int interfaceNumber);//声明usb设备的接口 18 | void releaseUsbInterface(libusb_device_handle *deviceHandle,int interfaceNumber);//释放usb设备声明的接口 19 | bool setUsbInterfaceAltSetting(libusb_device_handle *deviceHandle,int interfaceNumber,int bAlternateSetting);//激活usb设备接口备用设置 20 | bool resetUsbDevice(libusb_device_handle *deviceHandle);//重置usb设备 21 | /*数据传输*/ 22 | int bulkTransfer(libusb_device_handle *deviceHandle,quint8 endpoint, quint8 *data, 23 | int length, quint32 timeout);//(批量(块)传输) 24 | 25 | /*设备查询*/ 26 | int getOpenedDeviceCount(){return deviceHandleList.size();}//获取当前打开的设备数量 27 | /*该类中所有方法的函参(libusb_device_handle *deviceHandle)必须通过以下getDeviceHandleFrom*方法获取*/ 28 | libusb_device_handle *getDeviceHandleFromIndex(int index);//通过索引获取打开的设备句柄 29 | libusb_device_handle *getDeviceHandleFromVpidAndPort(quint16 vid,quint16 pid,qint16 port);//通过vpid和端口号获取打开的设备句柄 30 | ``` 31 | ### 2.UsbMonitor 32 | USB热插拔监测类,该类可以用来定义成"全局"(有较长的生命周期)对象,实现对指定的usb设备进行热插拔监测。 33 | ``` 34 | //注册热插拔监测服务 35 | bool registerHotplugMonitorService(int deviceClass=LIBUSB_HOTPLUG_MATCH_ANY, 36 | int vendorId=LIBUSB_HOTPLUG_MATCH_ANY, 37 | int productId=LIBUSB_HOTPLUG_MATCH_ANY); 38 | //注销热插拔监测服务 39 | void deregisterHotplugMonitorService(); 40 | 41 | signals: 42 | void deviceHotplugSig(bool isAttached);//设备插拔信号 43 | ``` 44 | ### 3.UsbEventHandler 45 | USB事件处理类,该类继承自QThread,重写run()方法,在子线程中轮询处理挂起的事件(主要是USB设备的热插拔事件),进而触发热插拔的回调函数。目前该类单纯是配合UsbMonitor的热插拔监测接口使用,相关处理已经封装在接口内,其他地方无需使用。 46 | 47 | ## 小结 48 | 该组件的设计初衷是为了实现在嵌入式Linux平台连接USB热敏打印机打印小票的需求。因为使用的打印机不提供Linux系统的驱动,而Linux系统通用usblp驱动跟设备不匹配,所以最终只能使用libusb这种'免驱'设计,在应用层直接与usb设备建立通信,使用ESC/POS指令控制打印机。为了日后能够应对其他USB设备的通信,故将usb通信部分单独提取出来封装成该组件,方便使用。 49 | 而之后又遇到一个与USB接口相机通信取图的需求,所以在原来组件的基础上进行了一些修改,将热插拔监测功能从UsbComm中分离出去,单独成类。而UsbComm只负责通信数据传输,内部维护设备句柄列表,实现对多个设备(包括相同vpid的设备)的访问。 50 | ## 参考资料 51 | 1. [libusb官网](https://libusb.info/) 52 | 2. [libusb源代码(Github)](https://github.com/libusb/libusb) 53 | 3. [libusb源代码(Sourceforge)](https://sourceforge.net/projects/libusb/) 54 | 4. [libusb-1.0-api接口文档](http://libusb.sourceforge.net/api-1.0/) 55 | 5. [巨人的肩膀之QtUsb](https://github.com/fpoussin/QtUsb) 56 | 6. [巨人的肩膀之QUSB](https://github.com/bimetek/QUSB) 57 | 58 | ## 作者联系方式 59 | **邮箱:justdoit_mqr@163.com** 60 | **新浪微博:@为-何-而来** 61 | -------------------------------------------------------------------------------- /usbcomm.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * Copyright (C) 2021-2022 MiaoQingrui. All rights reserved. 4 | * Author: 缪庆瑞 5 | * 6 | ****************************************************************************/ 7 | /* 8 | *@author: 缪庆瑞 9 | *@date: 2021.03.15 10 | *@brief: USB应用层通信组件(对libusb的api接口进行二次封装,方便使用) 11 | */ 12 | #include "usbcomm.h" 13 | #include 14 | #include 15 | 16 | /* 17 | *@brief: 构造函数,负责对libusb进行初始化 18 | *@author: 缪庆瑞 19 | *@date: 2021.03.15 20 | */ 21 | UsbComm::UsbComm(QObject *parent) 22 | : QObject(parent) 23 | { 24 | //成员变量初始化 25 | context = NULL; 26 | //libusb初始化 27 | int err = libusb_init(&context); 28 | if(err != LIBUSB_SUCCESS) 29 | { 30 | qDebug()<<"libusb_init error:"<表 74 | *@return: bool:true=成功 false=失败 75 | */ 76 | bool UsbComm::openUsbDevice(QMultiMap &vpidMap) 77 | { 78 | if(vpidMap.isEmpty()) 79 | { 80 | qDebug()<<"vpidMap is empty"; 81 | return false; 82 | } 83 | 84 | closeAllUsbDevice();//先关闭所有已打开的设备 85 | 86 | libusb_device **devs; 87 | ssize_t count = libusb_get_device_list(context,&devs);//获取设备列表 88 | if(count < 0) 89 | { 90 | qDebug()<<"libusb_get_device_list is error"; 91 | return false; 92 | } 93 | for(int i=0;i claimedInterfaceList = handleClaimedInterfacesMap.value(deviceHandle); 217 | if(!claimedInterfaceList.contains(interfaceNumber)) 218 | { 219 | claimedInterfaceList.append(interfaceNumber); 220 | handleClaimedInterfacesMap.insert(deviceHandle,claimedInterfaceList); 221 | } 222 | } 223 | else 224 | { 225 | QList claimedInterfaceList; 226 | claimedInterfaceList.append(interfaceNumber); 227 | handleClaimedInterfacesMap.insert(deviceHandle,claimedInterfaceList); 228 | } 229 | 230 | return true; 231 | } 232 | /* 233 | *@brief: 释放usb设备声明的接口 234 | *@author: 缪庆瑞 235 | *@date: 2022.02.22 236 | *@param: deviceHandle:设备句柄 237 | *@param: interfaceNumber:要释放的接口号,-1表示释放当前所有生命过的接口 238 | */ 239 | void UsbComm::releaseUsbInterface(libusb_device_handle *deviceHandle,int interfaceNumber) 240 | { 241 | //设备句柄不存在 242 | if(!deviceHandleList.contains(deviceHandle)) 243 | { 244 | return; 245 | } 246 | //设备当前未声明任何接口 247 | if(!handleClaimedInterfacesMap.contains(deviceHandle)) 248 | { 249 | return; 250 | } 251 | 252 | QList claimedInterfaceList = handleClaimedInterfacesMap.value(deviceHandle); 253 | if(interfaceNumber != -1) 254 | { 255 | if(claimedInterfaceList.contains(interfaceNumber)) 256 | { 257 | libusb_release_interface(deviceHandle,interfaceNumber); 258 | claimedInterfaceList.removeAll(interfaceNumber); 259 | handleClaimedInterfacesMap.insert(deviceHandle,claimedInterfaceList); 260 | } 261 | } 262 | else 263 | { 264 | for(int i=0;i=0 && index < deviceHandleList.size()) 382 | { 383 | return deviceHandleList.at(index); 384 | } 385 | return NULL; 386 | } 387 | /* 388 | *@brief: 通过vpid和端口号获取打开的设备句柄 389 | *@author: 缪庆瑞 390 | *@date: 2022.02.22 391 | *@param: vid:厂商id 392 | *@param: pid:产品id 393 | *@param: port:端口号(通常是与硬件接口绑定的,可通过dmesg查看),-1表示不匹配端口 394 | *@return: libusb_device_handle:设备句柄 395 | */ 396 | libusb_device_handle *UsbComm::getDeviceHandleFromVpidAndPort(quint16 vid, quint16 pid, qint16 port) 397 | { 398 | for(int i=0;i配置(configuration)->接口(interface)->备用设置(altsetting)->端点(endpoint)*/ 436 | 437 | /*设备(device)*/ 438 | libusb_device_descriptor deviceDesc; 439 | int err = libusb_get_device_descriptor(usbDevice, &deviceDesc); 440 | if(err != LIBUSB_SUCCESS) 441 | { 442 | qDebug()<<"libusb_get_device_descriptor error:"<bConfigurationValue; 465 | qDebug()<<"Number of interfaces: "<<(int)configDesc->bNumInterfaces; 466 | /*接口(interface)*/ 467 | for(int j=0; j<(int)configDesc->bNumInterfaces;j++) 468 | { 469 | qDebug()<<"\tInterface index:"<interface[j]; 472 | qDebug()<<"\tNumber of alternate settings: "<num_altsetting; 473 | /*备用设置(altsetting)*/ 474 | for(int k=0; knum_altsetting; k++) 475 | { 476 | qDebug()<<"\t\tAltsetting index:"<altsetting[k]; 479 | qDebug()<<"\t\tInterface Class: "<< 480 | QString("0x%1").arg((int)interfaceDesc->bInterfaceClass,2,16,QChar('0'));//接口类 481 | qDebug()<<"\t\tInterface Number: "<<(int)interfaceDesc->bInterfaceNumber; 482 | qDebug()<<"\t\tAlternate settings: "<<(int)interfaceDesc->bAlternateSetting; 483 | qDebug()<<"\t\tNumber of endpoints: "<<(int)interfaceDesc->bNumEndpoints; 484 | /*端点(endpoint)*/ 485 | for(int m=0; m<(int)interfaceDesc->bNumEndpoints; m++) 486 | { 487 | qDebug()<<"\t\t\tEndpoint index:"<endpoint[m]; 490 | qDebug()<<"\t\t\tEP address: "<< 491 | QString("0x%1").arg((int)endpointDesc->bEndpointAddress,2,16,QChar('0')); 492 | qDebug()<<"\t\t\tEP transfer type:"<< 493 | (endpointDesc->bmAttributes&0x03);//端点传输类型,详见enum libusb_transfer_type{} 494 | } 495 | } 496 | } 497 | libusb_free_config_descriptor(configDesc);//释放配置描述符空间 498 | } 499 | qDebug()<<"***************************************"<setStopped(false); 563 | hotplugEventHandler->start(); 564 | 565 | return true; 566 | } 567 | /* 568 | *@brief: 注销热插拔监测服务 569 | *@author: 缪庆瑞 570 | *@date: 2022.02.22 571 | */ 572 | void UsbMonitor::deregisterHotplugMonitorService() 573 | { 574 | if(hotplugHandle != -1)//注销回调 575 | { 576 | libusb_hotplug_deregister_callback(context,hotplugHandle); 577 | hotplugHandle = -1; 578 | } 579 | if(hotplugEventHandler != NULL)//停止热插拔事件处理线程 580 | { 581 | hotplugEventHandler->setStopped(true); 582 | hotplugEventHandler->wait();//等待线程结束 583 | } 584 | } 585 | 586 | /* 587 | *@brief: 热插拔回调函数(在UsbEventHandler子线程中执行) 588 | * 注1:必须是静态成员函数或全局函数,否则会因为隐含的this指针导致注册回调语句编译不通过, 589 | * 也因此回调函数无法直接使用实例对象,但可以通过函参user_data访问实例对象的方法与数据。 590 | * 注2:该函数内使用user_data发射实例对象的信号,因为信号依附于子线程发射,而槽一般在主线 591 | * 程,connect默认采用队列连接,确保了该函数只做最小处理,绝不拖泥带水。 592 | *@author: 缪庆瑞 593 | *@date: 2022.02.22 594 | *@param: ctx:表示libusb的一个会话 595 | *@param: device:热插拔的设备 596 | *@param: event:热插拔的事件 597 | *@param: user_data:用户数据指针,在调用libusb_hotplug_register_callback()传递, 598 | * 我们这里传的是this指针,实现在静态成员函数访问实例对象的方法与数据。 599 | *@return: int:当返回值为1时,则会撤销注册(deregistered) 600 | */ 601 | int UsbMonitor::hotplugCallback(libusb_context *ctx, libusb_device *device, 602 | libusb_hotplug_event event, void *user_data) 603 | { 604 | Q_UNUSED(ctx) 605 | Q_UNUSED(device) 606 | //强制转换成注册热插拔监测的实例对象指针 607 | UsbMonitor *tmpUsbMonitor = (UsbMonitor*)user_data; 608 | //设备插入 609 | if(event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) 610 | { 611 | emit tmpUsbMonitor->deviceHotplugSig(true); 612 | } 613 | //设备拔出 614 | else 615 | { 616 | emit tmpUsbMonitor->deviceHotplugSig(false); 617 | } 618 | return 0; 619 | } 620 | 621 | /* 622 | *@brief: 构造函数 623 | *@author: 缪庆瑞 624 | *@date: 2021.03.18 625 | *@param: context:表示libusb的一个会话 626 | *@parent: parent:父对象 627 | */ 628 | UsbEventHandler::UsbEventHandler(libusb_context *context, QObject *parent) 629 | :QThread(parent) 630 | { 631 | this->context = context; 632 | this->stopped = false; 633 | } 634 | /* 635 | *@brief: 子线程运行 636 | *@author: 缪庆瑞 637 | *@date: 2021.03.18 638 | */ 639 | void UsbEventHandler::run() 640 | { 641 | //超时时间 100ms 642 | struct timeval tv; 643 | tv.tv_sec = 0; 644 | tv.tv_usec = 100000; 645 | 646 | while(!this->stopped && context != NULL) 647 | { 648 | //qDebug()<<"libusb_handle_events()......."; 649 | /* 处理挂起的事件,非阻塞,超时即返回 650 | * 最开始使用的是libusb_handle_events()阻塞操作,但该阻塞会导致线程无法正常结束, 651 | * 调用terminate()强制结束后执行wait操作会卡死,怀疑是该阻塞操作会陷入内核态,导 652 | * 致在用户态下强制终止线程失败。 653 | * 注:如果有挂起的热插拔事件,注册的回调函数会在该线程内被调用。 654 | */ 655 | libusb_handle_events_timeout_completed(context,&tv,NULL); 656 | } 657 | } 658 | -------------------------------------------------------------------------------- /usbcomm.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * 3 | * Copyright (C) 2021-2022 MiaoQingrui. All rights reserved. 4 | * Author: 缪庆瑞 5 | * 6 | ****************************************************************************/ 7 | /* 8 | *@author: 缪庆瑞 9 | *@date: 2021.03.15 10 | *@brief: USB应用层通信组件(对libusb的api接口进行二次封装,方便使用) 11 | * 12 | * 1.与usb设备端进行通信数据传输(UsbComm) 13 | * 2.负责对usb设备热插拔监测(UsbMonitor) 14 | * 15 | */ 16 | #ifndef USBCOMM_H 17 | #define USBCOMM_H 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "libusb-1.0/include/libusb.h" 24 | 25 | /*************************************************USB数据通信*************************************************************/ 26 | 27 | /* USB通信主类 28 | * 该类主要实现与usb设备端的通信数据传输。 29 | * 内部按需封装libusb的方法接口,并维护着当前打开的设备句柄列表和声明的接口列表,所以对于设备句柄和接口的相关操作尽量都使用该类的方法处理, 30 | * 不要在外边单独使用原生libusb接口,避免造成内部维护的列表失效而产生异常。 31 | */ 32 | class UsbComm : public QObject 33 | { 34 | Q_OBJECT 35 | public: 36 | explicit UsbComm(QObject *parent = 0); 37 | ~UsbComm(); 38 | 39 | void findUsbDevices();//探测系统当前接入的usb设备,打印设备详细信息(调试用) 40 | 41 | /*设备初始化*/ 42 | bool openUsbDevice(QMultiMap &vpidMap);//打开指定设备(可能有多个) 43 | void closeUsbDevice(libusb_device_handle *deviceHandle);//关闭指定设备 44 | void closeAllUsbDevice();//关闭所有设备 45 | bool setUsbConfig(libusb_device_handle *deviceHandle,int bConfigurationValue=1);//激活usb设备当前配置 46 | bool claimUsbInterface(libusb_device_handle *deviceHandle,int interfaceNumber);//声明usb设备的接口 47 | void releaseUsbInterface(libusb_device_handle *deviceHandle,int interfaceNumber);//释放usb设备声明的接口 48 | bool setUsbInterfaceAltSetting(libusb_device_handle *deviceHandle,int interfaceNumber,int bAlternateSetting);//激活usb设备接口备用设置 49 | bool resetUsbDevice(libusb_device_handle *deviceHandle);//重置usb设备 50 | /*数据传输*/ 51 | int bulkTransfer(libusb_device_handle *deviceHandle,quint8 endpoint, quint8 *data, 52 | int length, quint32 timeout);//(批量(块)传输) 53 | 54 | /*设备查询*/ 55 | int getOpenedDeviceCount(){return deviceHandleList.size();}//获取当前打开的设备数量 56 | /*该类中所有方法的函参(libusb_device_handle *deviceHandle)必须通过以下getDeviceHandleFrom*方法获取*/ 57 | libusb_device_handle *getDeviceHandleFromIndex(int index);//通过索引获取打开的设备句柄 58 | libusb_device_handle *getDeviceHandleFromVpidAndPort(quint16 vid,quint16 pid,qint16 port);//通过vpid和端口号获取打开的设备句柄 59 | 60 | private: 61 | void printDevInfo(libusb_device *usbDevice);//打印USB设备详细信息 62 | 63 | libusb_context *context;//表示libusb的一个会话,由libusb_init创建 64 | QList deviceHandleList;//打开的usb设备句柄列表 65 | QMap > handleClaimedInterfacesMap;//句柄对应声明的接口列表的map 66 | 67 | }; 68 | 69 | /*************************************************USB热插拔监测*************************************************************/ 70 | 71 | /* USB热插拔监测类 72 | * 该类可以用来定义成"全局"(有较长的生命周期)对象,实现对指定的usb设备进行热插拔监测。 73 | */ 74 | class UsbEventHandler; 75 | 76 | class UsbMonitor : public QObject 77 | { 78 | Q_OBJECT 79 | public: 80 | UsbMonitor(QObject *parent = 0); 81 | ~UsbMonitor(); 82 | 83 | //注册热插拔监测服务 84 | bool registerHotplugMonitorService(int deviceClass=LIBUSB_HOTPLUG_MATCH_ANY, 85 | int vendorId=LIBUSB_HOTPLUG_MATCH_ANY, 86 | int productId=LIBUSB_HOTPLUG_MATCH_ANY); 87 | //注销热插拔监测服务 88 | void deregisterHotplugMonitorService(); 89 | 90 | signals: 91 | void deviceHotplugSig(bool isAttached);//设备插拔信号 92 | 93 | private: 94 | //热插拔回调函数 95 | static int LIBUSB_CALL hotplugCallback(libusb_context *ctx,libusb_device *device, 96 | libusb_hotplug_event event,void *user_data); 97 | 98 | libusb_context *context;//表示libusb的一个会话,由libusb_init创建 99 | libusb_hotplug_callback_handle hotplugHandle;//热插拔回调句柄 100 | UsbEventHandler *hotplugEventHandler;//热插拔事件处理对象 101 | 102 | }; 103 | 104 | /* USB事件处理类 105 | * 该类继承自QThread,重写run()方法,在子线程中轮询处理挂起的事件(主要是USB设备的 106 | * 热插拔事件),进而触发热插拔的回调函数。目前该类单纯是配合UsbMonitor的热插拔监测 107 | * 接口使用,相关处理已经封装在接口内,其他地方无需使用。 108 | */ 109 | class UsbEventHandler : public QThread 110 | { 111 | Q_OBJECT 112 | public: 113 | UsbEventHandler(libusb_context *context, QObject *parent = 0); 114 | //设置控制线程结束的标记变量状态 115 | void setStopped(bool stopped){this->stopped = stopped;} 116 | 117 | protected: 118 | virtual void run(); 119 | 120 | private: 121 | libusb_context *context;//表示libusb的一个会话,由构造函参传递 122 | volatile bool stopped;//标记变量,控制线程结束 123 | }; 124 | 125 | #endif // USBCOMM_H 126 | -------------------------------------------------------------------------------- /widget.cpp: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | #include "ui_widget.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Widget::Widget(QWidget *parent) : 9 | QWidget(parent), 10 | ui(new Ui::Widget),hotplugMonitor(NULL),usbReceive(NULL),recvBuffer(NULL) 11 | { 12 | ui->setupUi(this); 13 | } 14 | 15 | Widget::~Widget() 16 | { 17 | delete ui; 18 | if(recvBuffer) 19 | { 20 | free(recvBuffer); 21 | } 22 | } 23 | //列出当前接入到系统的所有usb设备 24 | void Widget::on_pushButton_clicked() 25 | { 26 | UsbComm usbComm; 27 | usbComm.findUsbDevices(); 28 | } 29 | //打开指定的usb设备,并发送通信数据 30 | void Widget::on_pushButton_2_clicked() 31 | { 32 | UsbComm usbComm; 33 | //这里对应的是我们用的USB接口的热敏打印机设备 34 | QMultiMap vpidMap; 35 | vpidMap.insert(0x0483,0x5748); 36 | if(usbComm.openUsbDevice(vpidMap)) 37 | { 38 | if(usbComm.claimUsbInterface(usbComm.getDeviceHandleFromIndex(0),0)) 39 | { 40 | QByteArray array0("hello printers\n"); 41 | qDebug()<fromUnicode(QString::fromUtf8("我看看能不能打印中文!\n")); 46 | qDebug()<deregisterHotplugMonitorService(); 67 | hotplugMonitor->registerHotplugMonitorService(); 68 | } 69 | //设备插拔信号响应槽 70 | void Widget::deviceHotplugSlot(bool isAttached) 71 | { 72 | if(isAttached)//插入 73 | { 74 | qDebug()<<"usb device attached"; 75 | } 76 | else//拔出 77 | { 78 | qDebug()<<"usb device detached"; 79 | } 80 | } 81 | //打开指定的usb设备,并接收通信数据 82 | void Widget::on_pushButton_4_clicked() 83 | { 84 | static int flag = 0; 85 | const int package_len = 8192; 86 | if(usbReceive == NULL) 87 | { 88 | usbReceive = new UsbComm(this); 89 | //这里对应的是我们用的USB接口的4k相机 90 | QMultiMap vpidMap; 91 | vpidMap.insert(0x04b4,0x00f1); 92 | if(usbReceive->openUsbDevice(vpidMap)) 93 | { 94 | if(usbReceive->claimUsbInterface(usbReceive->getDeviceHandleFromIndex(0),0)) 95 | { 96 | flag = 1; 97 | recvBuffer = (unsigned char *)malloc(package_len); 98 | } 99 | } 100 | if(flag != 1) 101 | { 102 | usbReceive->deleteLater(); 103 | usbReceive = NULL; 104 | } 105 | } 106 | if(flag == 1) 107 | { 108 | memset(recvBuffer,0,package_len); 109 | int len = usbReceive->bulkTransfer(usbReceive->getDeviceHandleFromIndex(0),0x81,recvBuffer,package_len,1); 110 | if(len < 0) 111 | { 112 | qDebug()<<"bulkTransfer error"<resetUsbDevice(usbReceive->getDeviceHandleFromIndex(0)); 128 | qDebug()<<"resetFlag============"< 5 | #include "usbcomm.h" 6 | 7 | namespace Ui { 8 | class Widget; 9 | } 10 | 11 | class Widget : public QWidget 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit Widget(QWidget *parent = 0); 17 | ~Widget(); 18 | 19 | private slots: 20 | void on_pushButton_clicked(); 21 | void on_pushButton_2_clicked(); 22 | void on_pushButton_3_clicked(); 23 | void deviceHotplugSlot(bool isAttached);//设备插拔响应槽 24 | 25 | void on_pushButton_4_clicked(); 26 | void on_pushButton_5_clicked(); 27 | 28 | private: 29 | Ui::Widget *ui; 30 | 31 | UsbMonitor *hotplugMonitor; 32 | UsbComm *usbReceive; 33 | unsigned char *recvBuffer; 34 | 35 | }; 36 | 37 | #endif // WIDGET_H 38 | -------------------------------------------------------------------------------- /widget.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Widget 4 | 5 | 6 | 7 | 0 8 | 0 9 | 640 10 | 463 11 | 12 | 13 | 14 | Widget 15 | 16 | 17 | 18 | 19 | 20 | list devices 21 | 22 | 23 | 24 | 25 | 26 | 27 | write device(0x0483,0x5748) 28 | 29 | 30 | 31 | 32 | 33 | 34 | hotplug monitor 35 | 36 | 37 | 38 | 39 | 40 | 41 | read device(0x04b4,0x00f1) 42 | 43 | 44 | 45 | 46 | 47 | 48 | reset device(0x04b4,0x00f1) 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | --------------------------------------------------------------------------------