Skip to content

Commit

Permalink
Release 0.1.23+2
Browse files Browse the repository at this point in the history
Signed-off-by: wangkun <[email protected]>
  • Loading branch information
wangkun committed Feb 9, 2021
1 parent 18c9dda commit b67b2f7
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ build/

# Directory created by dartdoc
doc/api/

.vscode/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.23+2

- Add `@Deprecated('inline')` for unavailable inline C functions

## 0.1.23+1

- Add support for Windows
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ copy libusb-1.0.23\MS64\dll\libusb-1.0.dll libusb-1.0\
Download `xxx` version from https://homebrew.bintray.com/bottles/libusb-1.0.23.catalina.bottle.tar.gz and extract

```
cp libusb/1.0.21/lib/libusb-1.0.dylib libusb-1.0/
cp libusb/1.0.23/lib/libusb-1.0.dylib libusb-1.0/
```

- Linux:
Expand Down
116 changes: 116 additions & 0 deletions example/showdevs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart' as ffi;
import 'package:libusb/libusb_macos.dart';

final _libusb = Libusb(DynamicLibrary.open('${Directory.current.path}/libusb-1.0/libusb-1.0.dylib'));

void main() {
var deviceNameList = QuickUsbMacos().getDeviceNameList();
print('deviceNameList ${deviceNameList.join(', ')}');
}

class QuickUsbMacos {
List<String> getDeviceNameList() {
var init = _libusb.libusb_init(nullptr);
if (init != libusb_error.LIBUSB_SUCCESS) {
throw StateError('init error: ${_libusb.describeError(init)}');
}

var deviceListPtr = ffi.allocate<Pointer<Pointer<libusb_device>>>();
try {
var count = _libusb.libusb_get_device_list(nullptr, deviceListPtr);
if (count < 0) {
return [];
}
try {
return _iterateDeviceName(deviceListPtr.value).toList();
} finally {
_libusb.libusb_free_device_list(deviceListPtr.value, 1);
}
} finally {
ffi.free(deviceListPtr);
_libusb.libusb_exit(nullptr);
}
}

Iterable<String> _iterateDeviceName(
Pointer<Pointer<libusb_device>> deviceList) sync* {
var descPtr = ffi.allocate<libusb_device_descriptor>();
var devHandlePtr = ffi.allocate<Pointer<libusb_device_handle>>();
final strDescLength = 42;
var strDescPtr = ffi.allocate<Uint8>(count: strDescLength);

for (var i = 0; deviceList[i] != nullptr; i++) {
yield _getDeviceName(
deviceList[i], descPtr, devHandlePtr, strDescPtr, strDescLength);
}

ffi.free(descPtr);
ffi.free(devHandlePtr);
ffi.free(strDescPtr);
}

String _getDeviceName(
Pointer<libusb_device> dev,
Pointer<libusb_device_descriptor> descPtr,
Pointer<Pointer<libusb_device_handle>> devHandlePtr,
Pointer<Uint8> strDescPtr,
int strDescLength) {
var devDesc = _libusb.libusb_get_device_descriptor(dev, descPtr);
if (devDesc != libusb_error.LIBUSB_SUCCESS) {
print('devDesc error: ${_libusb.describeError(devDesc)}');
return '';
}
var idVendor = descPtr.ref.idVendor.toRadixString(16).padLeft(4, '0');
var idProduct = descPtr.ref.idProduct.toRadixString(16).padLeft(4, '0');
var idDevice = '$idVendor:$idProduct';

if (descPtr.ref.iProduct == 0) {
print('$idDevice iProduct empty');
return '';
}

var open = _libusb.libusb_open(dev, devHandlePtr);
if (open != libusb_error.LIBUSB_SUCCESS) {
print('$idDevice open error: ${_libusb.describeError(open)}');
return '';
}
var devHandle = devHandlePtr.value;

try {
var langDesc = _libusb.libusb_get_string_descriptor(
devHandle, 0, 0, strDescPtr, strDescLength);
if (langDesc < 0) {
print('$idDevice langDesc error: ${_libusb.describeError(langDesc)}');
return '';
}
var langId = strDescPtr[2] << 8 | strDescPtr[3];

var prodDesc = _libusb.libusb_get_string_descriptor(
devHandle, descPtr.ref.iProduct, langId, strDescPtr, strDescLength);
if (prodDesc < 0) {
print('$idDevice prodDesc error: ${_libusb.describeError(prodDesc)}');
return '';
}
return utf8.decode(strDescPtr.asTypedList(prodDesc));
} finally {
_libusb.libusb_close(devHandle);
}
}
}

const int _kMaxSmi64 = (1 << 62) - 1;
const int _kMaxSmi32 = (1 << 30) - 1;
final int _maxSize = sizeOf<IntPtr>() == 8 ? _kMaxSmi64 : _kMaxSmi32;

extension LibusbExtension on Libusb {
String describeError(int error) {
var array = _libusb.libusb_error_name(error);
var nativeString = array.asTypedList(_maxSize);
var strlen = nativeString.indexWhere((char) => char == 0);
return utf8.decode(array.asTypedList(strlen));
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: libusb
description: Dart wrapper via `dart:ffi` for https://github.com/libusb/libusb
version: 0.1.23+1
version: 0.1.23+2
homepage: https://github.com/woodemi/libusb.dart

environment:
Expand Down

0 comments on commit b67b2f7

Please sign in to comment.