Scroll to navigation

Device::USB::Device(3pm) User Contributed Perl Documentation Device::USB::Device(3pm)

Device::USB::Device

This class encapsulates the USB device structure and the methods that may be applied to it.

NAME

Device::USB::Device - Use libusb to access USB devices.

VERSION

Version 0.38

SYNOPSIS

Device:USB::Device provides a Perl object for accessing a USB device using the libusb library.

    use Device::USB;
    my $usb = Device::USB->new();
    my $dev = $usb->find_device( $VENDOR, $PRODUCT );
    printf "Device: %04X:%04X\n", $dev->idVendor(), $dev->idProduct();
    print "Manufactured by ", $dev->manufacturer(), "\n",
          " Product: ", $dev->product(), "\n";
    $dev->set_configuration( $CFG );
    $dev->control_msg( @params );
    ...

See the libusb manual for more information about most of the methods. The functionality is generally the same as the libusb function whose name is the method name prepended with "usb_".

DESCRIPTION

This module defines a Perl object that represents the data and functionality associated with a USB device. The object interface provides read-only access to the important data associated with a device. It also provides methods for almost all of the functions supplied by libusb. Where necessary, the interfaces to these methods were changed to better match Perl usage. However, most of the methods are straight-forward wrappers around their libusb counterparts.

METHODS

Close the device connected to the object.
Retrieve the filename associated with the device.
In list context, return a list of the configuration structures for this device. In scalar context, return a reference to that list. This method is deprecated in favor of the two new methods: configurations and get_configuration.
In list context, return a list of the configuration structures for this device. In scalar context, return a reference to that list.
Retrieve the configuration requested by index. The legal values are from 0 to bNumConfigurations() - 1. Negative values access from the back of the list of configurations.
index numeric index of the index to return. If not supplied, use 0.

Returns an object encapsulating the configuration on success, or "undef" on failure.

There a several accessor methods that return data from the device and device descriptor. Each is named after the field that they return. All of the BCD fields have been changed to floating point numbers, so that you don't have to decode them yourself.

The methods include:

Retrieve the manufacture name from the device as a string. Return undef if the device read fails.
Retrieve the product name from the device as a string. Return undef if the device read fails.
Retrieve the serial number from the device as a string. Return undef if the device read fails.
Open the device. If the device is already open, close it and reopen it.

If the device fails to open, the reason will be available in $!.

Sets the active configuration of the device.
the integer specified in the descriptor field bConfigurationValue.

returns 0 on success or <0 on error

When using libusb-win32 under Windows, it is important to call "set_configuration()" after the "open()" but before any other method calls. Without this call, other methods may not work. This call is not required under Linux.

Sets the active alternative setting of the current interface for the device.
the integer specified in the descriptor field bAlternateSetting.

returns 0 on success or <0 on error

Clears any halt status on the supplied endpoint.
the integer specified bEndpointAddress descriptor field.

returns 0 on success or <0 on error

Resets the device. This also closes the handle and invalidates this device. This device will be unusable.
Claims the specified interface with the operating system.
The interface value listed in the descriptor field bInterfaceNumber.

Returns 0 on success, <0 on failure.

Releases the specified interface back to the operating system.
The interface value listed in the descriptor field bInterfaceNumber.

Returns 0 on success, <0 on failure.

Performs a control request to the default control pipe on a device.
Any returned data is placed here. If you don't want any returned data, pass undef.
Size of supplied buffer.
Milliseconds to wait for response.

Returns number of bytes read or written on success, <0 on failure.

Retrieve a string descriptor from the device.
The index of the string in the string list.
The language id used to specify which of the supported languages the string should be encoded in.

Returns a Unicode string. The function returns undef on error.

Retrieve a string descriptor from the device.
The index of the string in the string list.

Returns a C-style string if successful, or undef on error.

Retrieve a descriptor from the device
The type of descriptor to retrieve.
The index of that descriptor in the list of descriptors of that type.

TODO: This method needs major rewrite to be Perl-ish. I need to provide a better way to specify the type (or at least document which are available), and I need to return a Perl data structure, not a buffer of binary data.

Retrieve an endpoint-specific descriptor from the device
Endpoint to query.
The type of descriptor to retrieve.
The index of that descriptor in the list of descriptors.
Buffer into which to write the requested descriptor
Max size to read into the buffer.

TODO: This method needs major rewrite to be Perl-ish. I need to provide a better way to specify the type (or at least document which are available), and I need to return a Perl data structure, not a buffer of binary data.

Perform a bulk read request from the specified endpoint.
The number of the endpoint to read
Buffer into which to write the requested data.
Max size to read into the buffer.
Maximum time to wait (in milliseconds)

The function returns the number of bytes returned or <0 on error.

USB is packet based, not stream based. So using "bulk_read()" to read part of the packet acts like a peek. The next time you read, all of the packet is still there.

The data is only removed when you read the entire packet. For this reason, you should always call "bulk_read()" with the total packet size.

Perform a interrupt read request from the specified endpoint.
The number of the endpoint to read
Buffer into which to write the requested data.
Max size to read into the buffer.
Maximum time to wait (in milliseconds)

The function returns the number of bytes returned or <0 on error.

Perform a bulk write request to the specified endpoint.
The number of the endpoint to write
Buffer from which to write the requested data.
Maximum time to wait (in milliseconds)

The function returns the number of bytes written or <0 on error.

Perform a interrupt write request to the specified endpoint.
The number of the endpoint to write
Buffer from which to write the requested data.
Maximum time to wait (in milliseconds)

The function returns the number of bytes written or <0 on error.

This function returns the name of the driver bound to the interface specified by the parameter interface.
$interface
The interface number of interest.

Returns "undef" on error.

This function will detach a kernel driver from the interface specified by parameter interface. Applications using libusb can then try claiming the interface. Returns 0 on success or < 0 on error.

DIAGNOSTICS

This is an explanation of the diagnostic and error messages this module can generate.

Unable to open the USB device for the reason given.

DEPENDENCIES

This module depends on the Carp, Inline and Inline::C modules, as well as the strict and warnings pragmas. Obviously, libusb must be available since that is the entire reason for the module's existence.

AUTHOR

G. Wade Johnson (gwadej at cpan dot org) Paul Archer (paul at paularcher dot org)

Houston Perl Mongers Group

BUGS

Please report any bugs or feature requests to "bug-device-usb@rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Device::USB>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

Thanks go to various members of the Houston Perl Mongers group for input on the module. But thanks mostly go to Paul Archer who proposed the project and helped with the development.

COPYRIGHT & LICENSE

Copyright 2006-2013 Houston Perl Mongers

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

2022-10-19 perl v5.36.0