m3m0r7 / php-libusb
0.0.1
2024-03-15 15:36 UTC
Requires
- symfony/process: ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.51
- phpunit/phpunit: ^11.0
- rector/rector: ^1.0
- squizlabs/php_codesniffer: ^3.9
README
What is this?
This project is wrapping the libusb with PHP. The libusb is written in C and bridge to it via PHP FFI. This project is experimental project.
Requirements
- PHP 8.3+
- libusb
Prepare
macOS
- Install libusb via brew if you use macOS
$ brew install libusb
- You will get dynamic library is here:
/opt/homebrew/Cellar/libusb/<your libusb version>/lib/libusb-<your libusb version>.dylib
- You will get header file is here:
/opt/homebrew/Cellar/libusb/<your libusb version>/include/libusb-<your libusb version>/libusb.h
Windows
TBD
Ubuntu
TBD
Get started
Quick start
- Install this library
$ composer require m3m0r7/php-libusb
- Write below code quick start code and save as
test.php
<?php require __DIR__ . '/vendor/autoload.php'; $libusb = new \Libusb\Libusb( new \Libusb\LibusbHandle( new \Libusb\Connector\Libusb1_0( new \Libusb\Loader\FileLoader( // Set libusb header file here: '/path/to/libusb.h', // Set libusb library // Replace "so" to "dylib" if you use macOS: '/path/to/libusb-1.0.so', ), ) ), ); /** * @var \Libusb\Device\DeviceInterface $device */ foreach ($libusb->devices() as $device) { printf( "%s (serial: %s)\n", $device->descriptor()->product(), $device->descriptor()->serialNumber(), ); }
- Run
php test.php
to be got devices list
$ php test.php
USB Single Serial (serial: XXX)
YubiKey OTP+FIDO+CCID (serial: XXX)
USB2.0 Hub (serial: XXX)
USB3.1 Hub (serial: XXX)
4-Port USB 2.0 Hub (serial: XXX)
Logitech StreamCam (serial: XXX)
Send/Receive packet from serial port
- Write below code and save as
test2.php
<?php require __DIR__ . '/vendor/autoload.php'; $libusb = new \Libusb\Libusb( new \Libusb\LibusbHandle( new \Libusb\Connector\Libusb1_0( new \Libusb\Loader\FileLoader( // Set libusb header file here: '/path/to/libusb.h', // Set libusb library // Replace "so" to "dylib" if you use macOS: '/path/to/libusb-1.0.so', ), ) ), ); /** * @var \Libusb\Device\DeviceInterface $device */ [$device] = $libusb->devices( // Specify vendor id is here 0x0101, // Specify product id is here 0xabab, ); $device // Set configuration ->setConfiguration(1) // Set claim interface ->setClaimInterface(0); // Create bulk transfer endpoint $bulkTransfer = $device->bulkTransferEndpoints(); // Send packet $bulkTransfer ->send( (string) new \Libusb\Stream\Packet([0x01, 0x02, 0x03, 0x04]), ); // Received packet $received = $bulkTransfer ->receive();
- Run
php test2.php
then sent packets to the device and received from it.
How to test
./vendor/bin/phpunit
LICENSE
MIT