libusb: библиотека для работы с USB-устройствами
libusb (Universal Serial Bus)
libusb is a software library that provides tools for interacting with devices connected through USB. It allows developers to create applications that work with various types of USB devices, including peripheral devices such as printers, scanners, flash drives, as well as USB hubs and other gadgets.
One of the advantages of libusb is its free and open license, which allows for unrestricted use, study, and modification of the code. The library is supported on various operating systems, including Windows, macOS, and Linux, providing cross-platform compatibility for applications.
To work with libusb, you need to import the corresponding header files and link the static or dynamic library. Let's consider some code examples for basic operations using libusb.
1. Initializing and opening a USB device:
#include <libusb.h>
int main() {
libusb_device_handle *dev_handle;
libusb_init(NULL);
dev_handle = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID);
if (dev_handle == NULL) {
printf("Failed to open device\n");
return 1;
}
printf("Device opened successfully\n");
// Device interaction
libusb_close(dev_handle);
libusb_exit(NULL);
return 0;
}
In this example, we initialize the library, open the USB device with the specified vendor ID (VENDOR_ID) and product ID (PRODUCT_ID). If the device is successfully opened, we can perform operations with it.
2. Getting device information:
struct libusb_device_descriptor desc;
libusb_get_device_descriptor(libusb_get_device(dev_handle), &desc);
printf("Vendor ID: 0x%04x\n", desc.idVendor);
printf("Product ID: 0x%04x\n", desc.idProduct);
printf("Device Class: 0x%02x\n", desc.bDeviceClass);
// Other device characteristics
This code allows you to retrieve various information about the opened USB device, such as vendor and product identifiers, device class, and other characteristics.
3. Reading and writing data using control transfers:
unsigned char buf[64];
int transferred;
libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
READ_REQUEST, 0, 0, buf, sizeof(buf), TIMEOUT);
// Process the read data
libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR,
WRITE_REQUEST, 0, 0, data, sizeof(data), TIMEOUT);
// Process the written data
In this example, we use control transfers to read and write data with the device. We specify the transfer direction (IN for reading, OUT for writing), request type (vendor request), request code, indices, and other parameters. The transfer is done through the buffer specified in the parameters.
4. Asynchronous operations:
libusb_transfer *transfer = libusb_alloc_transfer(0);
unsigned char buf[64];
libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buf, sizeof(buf),
callback, NULL, TIMEOUT);
libusb_submit_transfer(transfer);
All these operations can be performed synchronously or asynchronously. For asynchronous transfers, the `libusb_transfer` structure and the `libusb_submit_transfer()` and `libusb_cancel_transfer()` functions are used. The `callback` function is called after the completion of the operation.
Thus, libusb provides developers with a powerful tool for interacting with USB devices. It allows for seamless switching between different operating systems, handling asynchronous tasks, and obtaining various information about connected devices. Thanks to its open license and active developer community, libusb continues to evolve and improve.