NightFeather / hidapi_dart

access hidapi from dart space
MIT License
4 stars 0 forks source link

Compatibility with flutter 2 #1

Closed redDwarf03 closed 2 years ago

redDwarf03 commented 2 years ago

Hello I’m searching a way to interact with device supports usb HID protocol with APDU commands Perhaps your lib will be a good way but could you migrate to flutter 2 please if possible? Thank you

NightFeather commented 2 years ago

Sure, but it'll take a couple days before I have time to look into this.

NightFeather commented 2 years ago

Upgraded.

redDwarf03 commented 2 years ago

thank for your work small question. i have a pid 0411 and vid 0bda in example, we have: int? vid = int.tryParse(parsable.removeAt(0)); so, I can't pass hex value ?

NightFeather commented 2 years ago

https://api.dart.dev/stable/2.15.1/dart-core/int/parse.html you can prepend 0x before the number marking it as a hex number. int.tryParse will detect this.

redDwarf03 commented 2 years ago

ok thank you. i met another issue... sorry for that. I'm working with macOS M1 I have install hidapi with brew install hidapi But when I execute the example, I have this message:

ArgumentError (Invalid argument(s): Failed to load dynamic library 'libhidapi-hidraw.so': dlopen(libhidapi-hidraw.so, 0x0001):
NightFeather commented 2 years ago

oh, 'cause currently I only wrote lib loading for Windows/Linux, didn't handle Mac OS X. Can you paste the filename of file installed by brew here (specifically the *.dylib)

redDwarf03 commented 2 years ago

here the folder with infos

hidapi_macos_brew.zip

NightFeather commented 2 years ago

Okay, I added the lib loading in branch macosx. Can you clone the branch and try running the examples see if it reports any errors?

redDwarf03 commented 2 years ago

thx. I test but I think I have an error with path...

Invalid argument(s): Failed to load dynamic library 'libhidapi.dylib': dlopen(libhidapi.dylib, 0x0001): tried: '/Users/reddwarf/SSe/flutter/bin/cache/dart-sdk/bin/./libhidapi.dylib' (no such file), '/Users/reddwarf/SSe/flutter/bin/cache/dart-sdk/bin/../../../libhidapi.dylib' (no such file), '/Users/reddwarf/SSe/flutter/bin/cache/dart-sdk/bin/Frameworks/libhidapi.dylib' (no such file), '/Users/reddwarf/SSe/flutter/bin/cache/dart-sdk/bin/./libhidapi.dylib' (no such file), '/Users/reddwarf/SSe/flutter/bin/cache/dart-sdk/bin/../../../libhidapi.dylib' (no such file), '/Users/reddwarf/SSe/flutter/bin/cache/dart-sdk/bin/Frameworks/libhidapi.dylib' (no such file), 'libhidapi.dylib' (no such file), '/usr/lib/libhidapi.dylib' (no such file), '/Users/reddwarf/SSe/app/SANDBOX/hidapi_dart-2/example/libhidapi.dylib' (no such file), '/usr/lib/libhidapi.dylib' (no such file)

do you know where the program search lib because, it doesn't seem to search with $PATH value

NightFeather commented 2 years ago

Uh, sorry for that, I'm not very familiar with Mac OS.

redDwarf03 commented 2 years ago

i met some issue with mac because of difference of architecture between intel and apple chipset.. so, i tried to test iwth windows and i have this error when i launch the example

reading...
Unhandled exception:
Invalid argument(s): Failed to lookup symbol 'wcslen': error code 749726248
#0      DynamicLibrary.lookup (dart:ffi-patch/ffi_dynamic_library_patch.dart:34:70)
#1      _wcslen (package:hidapi_dart/hidapi_ffi.dart)
#2      _wcslen (package:hidapi_dart/hidapi_ffi.dart)
#3      wstringLen
#4      fromWString
#5      HID.getProductString.<anonymous closure>
#6      using
#7      HID.getProductString
#8      info
#9      main
#10     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:32)
#11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

Exited (255)

any idea?

redDwarf03 commented 2 years ago

NB: i downloaded the dll from https://github.com/libusb/hidapi/releases and copied into system32 folders dll

NightFeather commented 2 years ago

oh, this is the native function I loaded for implementing some buffer handling in ffi interface. Looks like it's not located in the dll I selected. Let me fix it

NightFeather commented 2 years ago

Fixed, should be fine now.

redDwarf03 commented 2 years ago

i will check this. Thank you !!!

redDwarf03 commented 2 years ago

that's ok. thank you! i will continue to explore your lib. i close this issue

redDwarf03 commented 2 years ago

when i used, that's ok, the lib get my usb device (product and serial) i use the read function, nothing is return var ret = await hid.read(timeout: 1); return '' if i precise a timeout and if i precise nothing, the method never returns

redDwarf03 commented 2 years ago

is it possible to discuss with usb device. i would like to send APDU et get the response cf: https://speculos.ledger.com/user/clients.html

NightFeather commented 2 years ago

this should be the intended behavior. if you didn't supply the timeout, it will just wait until the device is readable. if you supplied a timeout but the timeout expires, it will return an empty string instead (since hidapi will report it read 0 bytes in this case.)

redDwarf03 commented 2 years ago

sorry but I need a small help in your example, what is the command please to send command 'e0c4000000' to a device with pid = '0x1011' and vid='0x2C97' please And how to get the result. thx

NightFeather commented 2 years ago

You can invoke the example program like dart run main.dart 0x2c97 0x1011 -- e0c4000000. this should send and read response from the device. and don't forget to pull the changes before executing the command above, I've changed the argument parsing.

redDwarf03 commented 2 years ago

thx i appreciate a lot your help!

. i tried yesterday like you said The raw for 0x2c97 is 'àA' and the response is [196, 0, 0 ,0, 0, 0...] i was surprise by the value of raw in input. And how to transform the output in String ?

NightFeather commented 2 years ago

Oh, because the old example will just dump the raw value, updated example program should output hex string instead.

NightFeather commented 2 years ago

And, don't forget to pull the newer version because I've pushed a fix for a crash would occur under some circumstances.

redDwarf03 commented 2 years ago

ok. great. i will test that in few hours. don't you think, it could be interesting to integrate directly in your project hidapi libs for windows, linux and macos ? is it possible ?

NightFeather commented 2 years ago

uh, you're saying bundle the hidapi libs in this project? maybe doable, but gotta find out how to do this properly.

redDwarf03 commented 2 years ago

yeah, something like that: https://github.com/woodemi/libusb.dart/blob/4372dfcf9d5e74b7ae75f6199427059aba1ed522/example/listdevs.dart#L8

redDwarf03 commented 2 years ago

for info, i test with your last dev My APDU in input 'e0c4000000' is changed by raw = 'c3a0c384000000'. Why don't you send directly the APDU ? and i receive '' when i read. So for my context, it doesn't work sadly

other thing: the timeout paramter is missing here: https://github.com/NightFeather/hidapi_dart/blob/cad10a47a74d4ced470f3ee10ece1c8eea117bcc/example/main.dart#L128

when i execute the example with my paramter, i changed the timeout with 5 but i don't wait 5 seconds for the result. it responded quickly

NightFeather commented 2 years ago

Uh, looks like something have gone wrong during the payload parsing. and looks like need documentation for the timeout parameter, actually it's in milliseconds not seconds.

NightFeather commented 2 years ago

Okay, the real payload sent should be fine. The changed payload are the artifact from I'm using wrong way to convert it to hex string. Looks like using utf8 codec for those stuff isn't a good idea.

redDwarf03 commented 2 years ago

ok. have you any idea ?

NightFeather commented 2 years ago

I'm gonna rework some parts so it may take a bit of time.

redDwarf03 commented 2 years ago

Ok thank I will test for you if you need

NightFeather commented 2 years ago

Fix pushed.

redDwarf03 commented 2 years ago

thank you for your speed :)

new test and result for: [write, 0x2c97, 0x1011, --, e0c4000000]

'> e0c4000000 < c4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

I'm waiting the value '1b300200010100039000'

for info, i tested a web version https://pub.dev/packages/web_hid and it works in web mode. perhaps it could be helpful for you and your lib

redDwarf03 commented 2 years ago

the project i refer integrate this: https://wicg.github.io/webhid/

redDwarf03 commented 2 years ago

i test with another apdu '> b0010000 < 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

it seems the response = substring(APDU, 2)

redDwarf03 commented 2 years ago

Hello. Have you any time to work on this? Thx!

redDwarf03 commented 2 years ago

Hello. do you think you will work on the project or do you want i close this issue ? Thx