Giter Site home page Giter Site logo

libusb.dart's Introduction

libusb

Dart wrapper via dart:ffi for https://github.com/libusb/libusb

Environment

  • Windows(10)
  • macOS
  • Linux(Ubuntu 18.04 LTS)

Usage

Checkout example

Features and bugs

Please file feature requests and bugs at the issue tracker.

Build

Prepare llvm(9+)

  • Windows: winget install -e --id LLVM.LLVM
  • macOS: brew install llvm
  • Linux: sudo apt install libclang-10-dev

Build libusb_xxx.dart

  • Windows/Linux:
pub run ffigen
move lib/libusb.dart lib/libusb64.dart

Refactor timeval to timeval64

  • macOS:
pub run ffigen
mv lib/libusb.dart lib/libusb32.dart

Refactor timeval to timeval32

Contribute

Prepare libusb.h

Download xxx verion from https://github.com/libusb/libusb/releases and extract libusb.h

Prepare libusb-1.0 dynamic library

  • Windows:

Download xxx version from https://github.com/libusb/libusb/releases and extract

copy libusb-1.0.23\MS64\dll\libusb-1.0.dll libusb-1.0\
  • macOS:

Download xxx version from https://homebrew.bintray.com/bottles/libusb-1.0.23.catalina.bottle.tar.gz and extract

cp libusb/1.0.23/lib/libusb-1.0.dylib libusb-1.0/
  • Linux:

Download xxx version from http://old.kali.org/kali/pool/main/libu/libusb-1.0/ and install

cp /lib/x86_64-linux-gnu/libusb-1.0.so.0.xxx libusb-1.0/libusb-1.0.so

libusb.dart's People

Contributors

sunbreak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

libusb.dart's Issues

Update ffi package dependency

Currently, using this package as dependency causes compile error

Launching lib/main.dart on macOS in debug mode...
lib/main.dart:1
../lib/src/quick_usb_desktop.dart:110:38: Error: Expected type 'Utf8' to be a valid and instantiated subtype of 'NativeType'.
 - 'Utf8' is from 'package:ffi/src/utf8.dart' ('../../../../.pub-cache/hosted/pub.dartlang.org/ffi-1.1.2/lib/src/utf8.dart').

Offending line is

Pointer<ffi.Utf8> string = ffi.malloc(256);

Seems this is known issue
flutter/flutter#76705

To resolve it, ffi package dependency needs to be updated to latest version (currently 1.1.2)

open error: LIBUSB_ERROR_ACCESS

hi:
I tried to run listdeviceproducts Dart file, but always in libusb_ When you open, you get this error: open error: libusb_ ERROR_ ACCESS
image
Could you help me?

Windows support

Since flutter desktop supports windows it would be very useful to also include Windows support.

[generation] unavailable inline C function

For example:

/**
* \ingroup libusb_misc
* Convert a 16-bit value from host-endian to little-endian format. On
* little endian systems, this function does nothing. On big endian systems,
* the bytes are swapped.
* \param x the host-endian value to convert
* \returns the value in little-endian byte order
*/
static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
{
union {
uint8_t b8[2];
uint16_t b16;
} _tmp;
_tmp.b8[1] = (uint8_t) (x >> 8);
_tmp.b8[0] = (uint8_t) (x & 0xff);
return _tmp.b16;
}

results

/// \ingroup libusb_misc
/// Convert a 16-bit value from host-endian to little-endian format. On
/// little endian systems, this function does nothing. On big endian systems,
/// the bytes are swapped.
/// \param x the host-endian value to convert
/// \returns the value in little-endian byte order
int libusb_cpu_to_le16(
int x,
) {
_libusb_cpu_to_le16 ??=
_dylib.lookupFunction<_c_libusb_cpu_to_le16, _dart_libusb_cpu_to_le16>(
'libusb_cpu_to_le16');
return _libusb_cpu_to_le16(
x,
);
}

which doen't exist in dynamic library


Calling it would throw ArgumentError

test('libusb_cpu_to_le16', () {
expect(() => libusb.libusb_cpu_to_le16(0), throwsArgumentError);
});

Marked those inline functions @Deprecated('inline') as a workaround

Linux ffigen documentation

$ dart pub run ffigen
Deprecated. Use `dart run` instead.
Running in Directory: '/mnt/raid10/workspace-automation/app/libusb.dart'
Input Headers: [libusb-1.0/libusb.h]
[SEVERE] : Header libusb-1.0/libusb.h: Total errors/warnings: 1.
[SEVERE] :     /usr/include/sys/types.h:144:10: fatal error: 'stddef.h' file not found [Lexical or Preprocessor Issue]
[WARNING]: Skipped Function 'libusb_cpu_to_le16', inline functions are not supported.
[WARNING]: Removed All Struct Members from libusb_bos_dev_capability_descriptor(libusb_bos_dev_capability_descriptor), Flexible array members not supported.
[WARNING]: Removed All Struct Members from libusb_bos_descriptor(libusb_bos_descriptor), Flexible array members not supported.
[WARNING]: Removed All Struct Members from libusb_transfer(libusb_transfer), Flexible array members not supported.
[WARNING]: Skipped Function 'libusb_control_transfer_get_data', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_control_transfer_get_setup', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_control_setup', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_control_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_bulk_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_bulk_stream_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_interrupt_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_iso_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_set_iso_packet_lengths', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_get_iso_packet_buffer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_get_iso_packet_buffer_simple', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_get_descriptor', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_get_string_descriptor', inline functions are not supported.
Finished, Bindings generated in /mnt/raid10/workspace-automation/app/libusb.dart/lib/src/libusb.ffigen.dart

This was the resolution:

$ clang --version
clang version 16.0.6 (Fedora 16.0.6-3.fc38)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ find /usr -iname stddef.h
/usr/include/linux/stddef.h
/usr/include/c++/v1/stddef.h
/usr/lib/gcc/x86_64-redhat-linux/13/include/stddef.h
/usr/lib/gcc/aarch64-linux-gnu/13/include/stddef.h
/usr/lib/gcc/arm-linux-gnueabi/13/include/stddef.h
/usr/lib/gcc/arm-none-eabi/13.2.0/include/stddef.h
/usr/lib64/clang/16/include/stddef.h
/usr/lib64/llvm15/lib/clang/15.0.7/include/stddef.h
find: ‘/usr/libexec/initscripts/legacy-actions/auditd’: Permission denied
find: ‘/usr/share/empty.sshd’: Permission denied
find: ‘/usr/share/polkit-1/rules.d’: Permission denied
/usr/src/kernels/6.5.7-200.fc38.x86_64/include/linux/stddef.h
/usr/src/kernels/6.5.7-200.fc38.x86_64/include/uapi/linux/stddef.h
/usr/src/kernels/6.5.7-200.fc38.x86_64/tools/include/uapi/linux/stddef.h
/usr/src/kernels/6.5.8-200.fc38.x86_64/include/linux/stddef.h
/usr/src/kernels/6.5.8-200.fc38.x86_64/include/uapi/linux/stddef.h
/usr/src/kernels/6.5.8-200.fc38.x86_64/tools/include/uapi/linux/stddef.h
/usr/src/kernels/6.5.9-200.fc38.x86_64/include/linux/stddef.h
/usr/src/kernels/6.5.9-200.fc38.x86_64/include/uapi/linux/stddef.h
/usr/src/kernels/6.5.9-200.fc38.x86_64/tools/include/uapi/linux/stddef.h
$ dart pub run ffigen --compiler-opts "-I/usr/lib64/clang/16/include/"
Deprecated. Use `dart run` instead.
Running in Directory: '/mnt/raid10/workspace-automation/app/libusb.dart'
Input Headers: [libusb-1.0/libusb.h]
[WARNING]: Skipped Function 'libusb_cpu_to_le16', inline functions are not supported.
[WARNING]: Removed All Struct Members from libusb_bos_dev_capability_descriptor(libusb_bos_dev_capability_descriptor), Flexible array members not supported.
[WARNING]: Removed All Struct Members from libusb_bos_descriptor(libusb_bos_descriptor), Flexible array members not supported.
[WARNING]: Removed All Struct Members from libusb_transfer(libusb_transfer), Flexible array members not supported.
[WARNING]: Skipped Function 'libusb_control_transfer_get_data', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_control_transfer_get_setup', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_control_setup', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_control_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_bulk_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_bulk_stream_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_interrupt_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_fill_iso_transfer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_set_iso_packet_lengths', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_get_iso_packet_buffer', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_get_iso_packet_buffer_simple', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_get_descriptor', inline functions are not supported.
[WARNING]: Skipped Function 'libusb_get_string_descriptor', inline functions are not supported.
Finished, Bindings generated in /mnt/raid10/workspace-automation/app/libusb.dart/lib/src/libusb.ffigen.dart

[generation] libusb_windows.dart syntax error

flutter pub run ffigen on Windows results with syntax error. Removing consts solves the error

ffi.Pointer<ffi.Pointer<ffi.NativeFunction<const SERVER_ROUTINE>>> DispatchTable; // 101960
ffi.Pointer<ffi.Pointer<ffi.NativeFunction<const STUB_THUNK>>> ThunkTable; // 101966 
// ...
typedef const SERVER_ROUTINE = ... // 241521
typedef const STUB_THUNK = ... // 241524

Related: dart-lang/native#382

Example missing on how to use the library?

Thank you for the package.

Sorry but I cannot see a straightforward way to using it.

  1. I cannot see an example section in the packages https://pub.dev/packages/libusb
  2. I can see an example folder in the github page but the use of it causes errors from listDeviceProducts.dart

image

I just want to import the library on windows platform and use its provided functions to access usb device and send data to it. Will there be a step by step guide in getting this done?

Again thank you for your work and apologies if the solution was obvious but i could not pick up on it.

Examples fail to run

Very excited to have found this library, unfortunately when I try to run the examples I receive the following FFI errors:

$ dart --version
Dart SDK version: 2.18.6 (stable) (Tue Dec 13 21:15:14 2022 +0000) on "macos_arm64
$ dart run example/main.dart
lib/src/libusb.ffigen.dart:112:15: Error: Type 'pkg_ffi.Char' not found.
  ffi.Pointer<pkg_ffi.Char> libusb_error_name(
              ^^^^^^^^^^^^
lib/src/libusb.ffigen.dart:127:17: Error: Type 'pkg_ffi.Char' not found.
    ffi.Pointer<pkg_ffi.Char> locale,
                ^^^^^^^^^^^^
lib/src/libusb.ffigen.dart:140:15: Error: Type 'pkg_ffi.Char' not found.
  ffi.Pointer<pkg_ffi.Char> libusb_strerror(
              ^^^^^^^^^^^^
lib/src/libusb.ffigen.dart:221:17: Error: Type 'pkg_ffi.Int' not found.
    ffi.Pointer<pkg_ffi.Int> config,
                ^^^^^^^^^^^
lib/src/libusb.ffigen.dart:901:17: Error: Type 'pkg_ffi.UnsignedChar' not found.
    ffi.Pointer<pkg_ffi.UnsignedChar> endpoints,
                ^^^^^^^^^^^^^^^^^^^^
lib/src/libusb.ffigen.dart:925:17: Error: Type 'pkg_ffi.UnsignedChar' not found.
    ffi.Pointer<pkg_ffi.UnsignedChar> endpoints,
                ^^^^^^^^^^^^^^^^^^^^
lib/src/libusb.ffigen.dart:945:15: Error: Type 'pkg_ffi.UnsignedChar' not found.
  ffi.Pointer<pkg_ffi.UnsignedChar> libusb_dev_mem_alloc(
              ^^^^^^^^^^^^^^^^^^^^
lib/src/libusb.ffigen.dart:966:17: Error: Type 'pkg_ffi.UnsignedChar' not found.
    ffi.Pointer<pkg_ffi.UnsignedChar> buffer,
                ^^^^^^^^^^^^^^^^^^^^
lib/src/libusb.ffigen.dart:1152:17: Error: Type 'pkg_ffi.UnsignedChar' not found.
    ffi.Pointer<pkg_ffi.UnsignedChar> data,
                ^^^^^^^^^^^^^^^^^^^^
lib/src/libusb.ffigen.dart:1186:17: Error: Type 'pkg_ffi.UnsignedChar' not found.
    ffi.Pointer<pkg_ffi.UnsignedChar> data,

`pkg_ffi.Size` doesn't map to int

Since 0763bbd upgraded to ffigen 5.0.0-dev.0

flutter pub run ffigen results in pkg_ffi.Size in Dart funtion decleration

  ffi.Pointer<pkg_ffi.UnsignedChar> libusb_dev_mem_alloc(
    ffi.Pointer<libusb_device_handle> dev_handle,
    pkg_ffi.Size length,
  ) {
    return _libusb_dev_mem_alloc(
      dev_handle,
      length,
    );
  }

  late final _libusb_dev_mem_allocPtr = _lookup<
      ffi.NativeFunction<
          ffi.Pointer<pkg_ffi.UnsignedChar> Function(
              ffi.Pointer<libusb_device_handle>,
              pkg_ffi.Size)>>('libusb_dev_mem_alloc');
  late final _libusb_dev_mem_alloc = _libusb_dev_mem_allocPtr.asFunction<
      ffi.Pointer<pkg_ffi.UnsignedChar> Function(
          ffi.Pointer<libusb_device_handle>, pkg_ffi.Size)>();
  int libusb_dev_mem_free(
    ffi.Pointer<libusb_device_handle> dev_handle,
    ffi.Pointer<pkg_ffi.UnsignedChar> buffer,
    pkg_ffi.Size length,
  ) {
    return _libusb_dev_mem_free(
      dev_handle,
      buffer,
      length,
    );
  }

  late final _libusb_dev_mem_freePtr = _lookup<
      ffi.NativeFunction<
          pkg_ffi.Int Function(
              ffi.Pointer<libusb_device_handle>,
              ffi.Pointer<pkg_ffi.UnsignedChar>,
              pkg_ffi.Size)>>('libusb_dev_mem_free');
  late final _libusb_dev_mem_free = _libusb_dev_mem_freePtr.asFunction<
      int Function(ffi.Pointer<libusb_device_handle>,
          ffi.Pointer<pkg_ffi.UnsignedChar>, pkg_ffi.Size)>();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.