PrePAN

Sign in to PrePAN

USB::LibUSB Perl interface to the libusb-1.0 API

Good

Synopsis

 use USB::LibUSB;

 #
 # simple program to list all devices on the USB
 #
 
 my $ctx = USB::LibUSB->init();
 my @devices = $ctx->get_device_list();
 
 for my $dev (@devices) {
     my $bus_number = $dev->get_bus_number();
     my $device_address = $dev->get_device_address();
     my $desc = $dev->get_device_descriptor();
     my $idVendor = $desc->{idVendor};
     my $idProduct = $desc->{idProduct};
     
     printf("Bus %03d Device %03d: ID %04x:%04x\n", $bus_number,
            $device_address, $idVendor, $idProduct);
 }
    
 #
 # Synchronous bulk transfers
 #

 my $ctx = USB::LibUSB->init();
 my $handle = $ctx->open_device_with_vid_pid(0x1111, 0x2222);
 $handle->set_auto_detach_kernel_driver(1);

 # We want to use interface 0
 $handle->claim_interface(0);

 $handle->bulk_transfer_write($endpoint, $data, $timeout);
 my $data = $handle->bulk_transfer_read($endpoint, $length, $timeout);

Description

This module provides a Perl interface to the libusb-1.0 API. It provides access to most basic libusb functionality including read-out of device descriptors and synchronous device I/O. The objective is to provide the full portability of libusb-1.0 (Linux, Windows, BSDs, OSX,...).

The module has a two-tier design:

  • USB::LibUSB::XS

Raw XS interface, stay as close at possible to the libusb API. Not intended to be used directly.

  • USB::LibUSB

Based on USB::LibUSB::XS, adds convenient error handling and additional high-level functionality (e.g. device discovery with vid, pid and serial number). Easy to build more functionality without knowing about XS.

Update: Now on CPAN

Comments

Is there a reason you don't simply take over Device::USB? It's an interface to libusb also, and it's description states "I have not had the time or need to update the module, and no one has been willing to take it over."
Otherwise, there's already a USB:: namespace; it makes sense to use it: USB::LibUSB.
New top-level namespaces are not recommended: https://pause.perl.org/pause/query?ACTION=pause_namingmodules#Top_level_namespaces
Thanks for the comment,

Regarding Device::USB, it is written for the llibusb-0.1 API, which is incompatible with libusb-1.0 API. Including the LibUSB functionality in Device::USB would require drastic rewrite and API changes in it (but several other modules depend on it's current API). I shell ask the Device::USB maintainer for his opinion. In the end it depends on how much in demand Device::USB still is.

Regarding namespaces, by making extensive use of the T_PTROBJ XS typemap, LibUSB creates quite a lot of package clutter (LibUSB::Device, LibUSB::XS::Device::Handle, ... ). Moreover I'm currently writing more high-level driver code (https://github.com/amba/LibUSB-USBTMC) which is intended to live in the LibUSB:: namespace (and I'm hoping that more people will contribute high-level code). Wouldn't USB::LibUSB sound a bit redundant?
Maybe USB::LibUSB is not too bad. I could then use USB::TMC for the USBTMC driver.
BFOZ just gave me the OK to use the "USB::" namespace, so will probably go for USB::LibUSB.

Please sign up to post a review.