Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP - do not merge] Libusb android #2309

Closed
wants to merge 30 commits into from
Closed

[WIP - do not merge] Libusb android #2309

wants to merge 30 commits into from

Conversation

dirkhh
Copy link
Collaborator

@dirkhh dirkhh commented Oct 1, 2019

Describe the pull request:

  • Bug fix
  • Functional change
  • New feature
  • Code cleanup
  • Build system change
  • Documentation change
  • Language translation

Pull request long description:

This looks like a huge PR, but that's because it includes #2305
Only the last three commits are interesting

Changes made:

  1. update libusb to v1.0.23 which includes the Android changes
  2. take @glance- 's Take 1 to scan for devices
  3. add more code to it to actually create the callback typedef and correctly call the callback and also to add the devices to the libusb devices array
  4. add the JNI code to android.cpp to get the necessary information from Android

Related issues:

Many, many, many - all the ones stating that you can't download from FTDI dive computers on Android.
Of course, you STILL can't download from them, not even on the Android phones were previously this worked. So it its current state this PR makes it worse. But it feels like I'm close.

Additional information:

Release note:

Yet to come

Documentation change:

Mentions:

@glance- please take a look and help me figure out what's wrong. I'll add a adb logcat that shows how far we get as a comment in a moment.

bstoeger and others added 30 commits September 25, 2019 22:12
To allow efficient moving of downloaded dives from the download
thread to the model, implement a general move function that
moves table data. Instantiate that function for the dive and
dive_site tables.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Currently, desktop and mobile are accessing the DownloadThread
and the DiveImportedModel concurrently. This makes a big data
flow mess. To achieve a more hierarchical data flow, start
by making the DownloadThread a subobject of DiveImportedModel.

Start the download by calling a function in DiveImportedModel.

Route the finished signal through DiveImportedModel. Thus,
the model can reload itself with the new data.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
In DiveImportedModel::deleteDeselected(), unselected dives were
deleted from the dive-table. But this left the model in an
inconsistent state and the frontend was not informed of the
missing dives.

Fix this by invoking the appropriate beginRemoveRows()/
endRemoveRows() pairs. Move the functionality into its
own function so that it can be reused by the desktop version.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
In DownloadFromDCWidget::on_ok_clicked() deselected dives were
directly deleted from the dive table, leaving DiveImportedModel
in an inconsistent state. Use the function in DiveImportedModel
instead. This also removes code duplication.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Add a comment that the passed-in dives are consumed.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
When importing dives, consume the tables from DiveImportedModel
and not the DownloadThread. This appears more logical and avoids
an inconsistent state of the DiveImportedModel: On import the
tables would be reset, but the DiveImportedModel wasn't
informed of that.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Clearing the table in the thread leaves the model in an inconsistent
state. Don't do that.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
The plan is to make the model the authoritative source of
the imported dives. Therefore, access the number of
downloaded dives from there.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
QML doesn't access these directly anymore but goes via the model.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
These are not passed through QML anymore.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Even though the returned dive is not const, the table is not
changed, as it only contains pointers.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
The DiveImportedModel and DownloadThread used the same table
of dives and dive sites. This made it very hard to keep the
model consistent: Every modification of the download thread
would make the model inconsistent and could lead to memory
corruption owing to dangling pointers.

Therefore, keep a copy in the model. When updating the model,
use move-semantics, i.e. move the data and reset the tables
of the thread to zero elements.

Since the DiveImportedModel and the DownloadThread are very
tightly integrated, remove the accessor-functions of the
dive and dive-site tables. They fulfilled no purpose
whatsoever as they gave the same access-rights as a public
field.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
DiveImportedModel::recordDives() called add_imported_dives(). But that
actually consumes the dive and dive-site tables. Which in turn will
lead to an inconsistent model.

Properly reset the model by using the consumeTables() function.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This index was never set to anything else than 0. Might as
well remove it.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This is redundant, as the actual size is stored in the dive table.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
The download thread is not accessed directly by QML anymore.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
On many devices the Screen property doesn't give us a manufacturer,
model, or name.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Simply nice to have - even though it didn't help me track down the issue
this time around.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
So even setups relying on the wrapper script can do faster rebuilds.
This also cleans up a bug that made passing through the release
parameter fail in the past, and removes overly verbose debugging output.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Different Android devices seem to have different patterns of throwing
incorrect width information at us. This seems like a really bad hack,
but for the ones I've seen so far this should give us the right width
information.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
We want the new default font in place before the QML engine is
initialized.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Somehow we need to be able to figure out which width information is
correct.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
We get incorrect changes to a new screen width that causes us to try
draw to a much larger screen than we actually have. Ignore those
changes.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
There was a sad typo in commit eecca6a ("Mobile: replace model-reset
by row-addition in DiveListModel::reload()").

Additionally, we need to also refresh the dive list so that the new
dives are shown.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This only applies to those platforms where we build libusb from source,
but critically that includes Android and v1.0.23 contains several
Android updates that might get us closer to broader Android support for
FTDI downloads.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This is based on patches from Anton Ludin and adds the new device
scanning callback.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This adds all devices that Android reports back to us to the libusb
device list.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
@derek
Copy link

derek bot commented Oct 1, 2019

Thank you for your contribution. I've just checked and your commit doesn't appear to be signed-off. That's something we need before your Pull Request can be merged. Please see our contributing guide.
Tip: if you only have one commit so far then run: git commit --amend --signoff and then git push --force.

@derek derek bot added the no-dco label Oct 1, 2019
@dirkhh
Copy link
Collaborator Author

dirkhh commented Oct 1, 2019

09-30 17:32:28.005  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "0.006: Successfully opened logfile /storage/emulated/0/Android/data/org.subsurfacedivelog.mobile/files/Documents/subsurface.log at Mon Sep 30 17:32:28 2019"
09-30 17:32:28.006  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "0.007: Starting Subsurface-mobile:2.2.2(4.9.3.111):Android  (9.0):arm64:en-US"
09-30 17:32:28.006  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "0.007: built with libdivecomputer v0.7.0-devel-Subsurface-NG (ce6d9896a79afaa82641132e338f8744714c8593)"
09-30 17:32:28.006  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "0.007: built with Qt Version 5.12.4, runtime from Qt Version 5.12.4"
09-30 17:32:28.006  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "0.007: built with libgit2 0.26.0"
09-30 17:32:28.006  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "0.007: Running on Android  (9.0)"
09-30 17:32:28.006  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "0.007: Pixel 3 XL/google/crosshatch"
[...]
09-30 17:32:39.243  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.244: DCDownloadThread started for Heinrichs Weikamp OSTC 3 on FTDI downloading only new dives"
09-30 17:32:39.244  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: Starting download from  ftdi
09-30 17:32:39.244  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: downloading only new dives
09-30 17:32:39.248  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.249: Opening serial device ftdi"
09-30 17:32:39.248  9080  9428 D /android/subsurface/core/serial_ftdi.c: INFO: in ftdi_open
09-30 17:32:39.248  9080  9428 D /android/subsurface/core/serial_ftdi.c: INFO: serial_ftdi_open called
09-30 17:32:39.248  9080  9428 D /android/subsurface/core/serial_ftdi.c: INFO: setting up ftdi_ctx
09-30 17:32:39.252  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.253: scan_devices called"
09-30 17:32:39.256  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.257: nr of devices = 1"
09-30 17:32:39.256  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.257: name /dev/bus/usb/001/002 vid 1027 pid 24577 configCount 1"
09-30 17:32:39.257  9080  9428 D libusb  : [timestamp] [threadID] facility level [function call] <message>
09-30 17:32:39.257  9080  9428 D libusb  : --------------------------------------------------------------------------------
09-30 17:32:39.257  9080  9428 E libusb  : [ 0.006384] [000024d4] libusb: error [android_enumerate_device] libusb adding device 1/2
09-30 17:32:39.257  9080  9428 I libusb  : [ 0.006433] [000024d4] libusb: info [android_enumerate_device] libusb adding device 1/2
09-30 17:32:39.257  9080  9428 E libusb  : [ 0.006451] [000024d4] libusb: error [linux_scan_devices] back from scanning for devices on Android
09-30 17:32:39.257  9080  9428 D libusb  : [ 0.006476] [000024d4] libusb: debug [usbi_add_pollfd] add fd 52 events 1
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006615] [000024d4] libusb: debug [usbi_io_init] using timerfd for timeouts
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006623] [000024d4] libusb: debug [usbi_add_pollfd] add fd 59 events 1
09-30 17:32:39.258  9080  9428 D /android/subsurface/core/serial_ftdi.c: INFO: initialize ftdi_ctx
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006645] [000024d4] libusb: debug [libusb_init] libusb v1.0.23.11402
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006660] [000024d4] libusb: debug [get_kernel_version] reported kernel version is 4.9.148-g695fa5606dab-ab5636409
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006669] [000024d4] libusb: debug [op_init] bulk continuation flag supported
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006674] [000024d4] libusb: debug [op_init] zero length packet flag supported
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006679] [000024d4] libusb: debug [op_init] max iso packet length is (likely) 49152 bytes
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006710] [000024d4] libusb: debug [op_init] sysfs can relate devices
09-30 17:32:39.258  9080  9428 D libusb  : [ 0.006716] [000024d4] libusb: debug [op_init] sysfs has complete descriptors
09-30 17:32:39.258  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.259: scan_devices called"
09-30 17:32:39.258  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.259: nr of devices = 1"
09-30 17:32:39.258  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.259: name /dev/bus/usb/001/002 vid 1027 pid 24577 configCount 1"
09-30 17:32:39.259  9080  9428 E libusb  : [ 0.007562] [000024d4] libusb: error [android_enumerate_device] libusb adding device 1/2
09-30 17:32:39.259  9080  9428 I libusb  : [ 0.007578] [000024d4] libusb: info [android_enumerate_device] libusb adding device 1/2
09-30 17:32:39.259  9080  9428 E libusb  : [ 0.007591] [000024d4] libusb: error [linux_scan_devices] back from scanning for devices on Android
09-30 17:32:39.259  9080  9428 D libusb  : [ 0.007606] [000024d4] libusb: debug [usbi_add_pollfd] add fd 62 events 1
09-30 17:32:39.259  9080  9428 D libusb  : [ 0.007619] [000024d4] libusb: debug [usbi_io_init] using timerfd for timeouts
09-30 17:32:39.259  9080  9428 D libusb  : [ 0.007622] [000024d4] libusb: debug [usbi_add_pollfd] add fd 70 events 1
09-30 17:32:39.259  9080  9428 D /android/subsurface/core/serial_ftdi.c: INFO: call serial_ftdi_open_device
09-30 17:32:39.259  9080  9428 D /android/subsurface/core/serial_ftdi.c: INFO: serial_ftdi_open_device called
09-30 17:32:39.259  9080  9428 D libusb  : [ 0.007639] [000024d4] libusb: debug [libusb_get_device_list] 
09-30 17:32:39.259  9080  9428 D libusb  : [ 0.007754] [000024d4] libusb: debug [libusb_get_device_descriptor] 
09-30 17:32:39.259  9080  9428 D libusb  : [ 0.007764] [000024d4] libusb: debug [libusb_open] open 1.0
09-30 17:32:39.259  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.260: get_usb_fd(1027,24577) called"
09-30 17:32:39.259  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.260: Looking at device with VID/PID 1027/24577"
09-30 17:32:39.263  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.264: successfully opened the device"
09-30 17:32:39.263  9080  9428 E libusb  : [ 0.012344] [000024d4] libusb: error [_get_usbfs_fd] got fd 56 when opening usb device
09-30 17:32:39.263  9080  9428 D libusb  : [ 0.012366] [000024d4] libusb: debug [usbi_add_pollfd] add fd 56 events 4
09-30 17:32:39.263  9080  9428 D libusb  : [ 0.012384] [000024d4] libusb: debug [libusb_close] 
09-30 17:32:39.263  9080  9428 D libusb  : [ 0.012391] [000024d4] libusb: debug [usbi_remove_pollfd] remove fd 56
09-30 17:32:39.263  9080  9428 D libusb  : [ 0.012405] [000024d4] libusb: debug [libusb_open] open 1.0
09-30 17:32:39.263  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.264: get_usb_fd(1027,24577) called"
09-30 17:32:39.264  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.265: Looking at device with VID/PID 1027/24577"
09-30 17:32:39.265  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.266: successfully opened the device"
09-30 17:32:39.265  9080  9428 E libusb  : [ 0.013858] [000024d4] libusb: error [_get_usbfs_fd] got fd 56 when opening usb device
09-30 17:32:39.265  9080  9428 D libusb  : [ 0.013873] [000024d4] libusb: debug [usbi_add_pollfd] add fd 56 events 4
09-30 17:32:39.265  9080  9428 D libusb  : [ 0.013880] [000024d4] libusb: debug [libusb_get_device_descriptor] 
09-30 17:32:39.265  9080  9428 D libusb  : [ 0.013893] [000024d4] libusb: debug [libusb_get_config_descriptor] index 0
09-30 17:32:39.265  9080  9428 E libusb  : [ 0.013898] [000024d4] libusb: error [seek_to_next_config] short descriptor read -18/9
09-30 17:32:39.265  9080  9428 D /android/subsurface/core/serial_ftdi.c: INFO: FTDI tried VID 0403 pid 6001 ret -10
09-30 17:32:39.265  9080  9428 D /android/subsurface/core/serial_ftdi.c: 
09-30 17:32:39.265  9080  9428 D /android/subsurface/core/serial_ftdi.c: ERROR: libusb_get_config_descriptor() failed
09-30 17:32:39.265  9080  9428 D /android/subsurface/core/serial_ftdi.c: INFO: serial_ftdi_open() failed
09-30 17:32:39.265  9080  9428 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: Finishing download thread: "Unable to open ftdi Heinrichs Weikamp (OSTC 3)"
09-30 17:32:39.265  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.266: Input/output error"
09-30 17:32:39.266  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: no new dives downloaded
09-30 17:32:39.266  9080  9162 D /android/subsurface/qt-models/messagehandlermodel.cpp: INFO: "11.267: DCDownloadThread finished"

So we find the device that is connected (yay), we are able to open it (extra yay), but then we fail in libusb_get_config_descriptor when trying to read config information from the device. I'm guessing that I need to populate more information about the UsbEndpoints. But I'm running out of time here and I'm traveling for the next two days so I'm not sure when I can continue, so I wanted to make sure that others who are intersted can take a look.

@glance-
Copy link
Collaborator

glance- commented Oct 1, 2019

Good work.

error [seek_to_next_config] short descriptor read -18/9

We obviously have opened the device and is talking to it, but we can't really talk to it.

This is probably due to the os_priv bits in the struct libusb_device being random data. I guess we need to try to call initialize_device in android_enumerate_device.

@dirkhh
Copy link
Collaborator Author

dirkhh commented Oct 1, 2019

That call to initialize_device() wants a sysfs_dir which we don't have, but it deals gracefully with that, a wrapped_fd which I think it may figure out itself by calling _get_usbfs_fd() if I pass in a negative value, and the devaddr which I hardcode to 0 right now, as I don't know where to get that from. Reading a bit more of that code that devaddr may be the problem as it uses that value to create a path to the device...
I'll play with it when/if I have the time. I certainly appreciate the suggestion as I'm feeling like I'm poking at this with very limited understanding of how this should work.

@dirkhh
Copy link
Collaborator Author

dirkhh commented Oct 1, 2019

Placed a call to initialize_device() before calling usbi_connect_device().
This once again fails with a short descriptor read. It successfully gets the fd from Android, then issues a read against that fd and get 0 bytes back.
Unsure what to try next.

@dirkhh
Copy link
Collaborator Author

dirkhh commented Oct 2, 2019

Several more hours of trying to find a way to get libusb to be able to access config and descriptors. I must be missing something very fundamental. We get an fd that seems valid, but then all the reads go nowhere. No errors, just zero bytes back.
I saw that there's a way to get the raw descriptor from Java and considered transplanting that, but that seems... crazy. If that's the way we need to do things, then we might as well try the other path that you suggested and write an "interface layer" that translates our file IO to JNI calls...
I know that you are extremely limited in time @glance- ... but I could really use some help here.

@StephenGoodall
Copy link
Contributor

sorry to be a pain @dirkhh but i'm trying to build from this branch, to hopefully get as far as you have done above but I don't think it's seeing the new "libusb_set_android_scan_devices_callback" function that was added. I'm guessing the subsequent errors would be because of the first

/home/stephen/subsurface/core/android.cpp:46:2: error: use of undeclared identifier 'libusb_set_android_scan_devices_callback'; did you mean 'libusb_set_android_open_callback'?
        libusb_set_android_scan_devices_callback(scan_devices);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        libusb_set_android_open_callback
/home/stephen/ndk-arm/sysroot/usr/include/libusb-1.0/libusb.h:2009:6: note: 'libusb_set_android_open_callback' declared here
void libusb_set_android_open_callback(libusb_android_open_callback_func aocf);
     ^
/home/stephen/subsurface/core/android.cpp:46:43: error: cannot initialize a parameter of type 'libusb_android_open_callback_func' (aka 'int (*)(unsigned short, unsigned short)') with
      an lvalue of type 'int (struct libusb_context *)': different number of parameters (2 vs 1)
        libusb_set_android_scan_devices_callback(scan_devices);
                                                 ^~~~~~~~~~~~
/home/stephen/ndk-arm/sysroot/usr/include/libusb-1.0/libusb.h:2009:73: note: passing argument to parameter 'aocf' here
void libusb_set_android_open_callback(libusb_android_open_callback_func aocf);
                                                                        ^
/home/stephen/subsurface/core/android.cpp:195:25: error: use of undeclared identifier 'LIBUSB_OPTION_LOG_LEVEL'
        libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
                               ^
/home/stephen/subsurface/core/android.cpp:238:4: error: use of undeclared identifier 'android_enumerate_device'
                        android_enumerate_device(ctx, busnum, portnum, 0, configurationCount, dd);
                        ^
4 errors generated.
make[2]: *** [core/CMakeFiles/subsurface_corelib.dir/android.cpp.o] Error 1
make[1]: *** [core/CMakeFiles/subsurface_corelib.dir/all] Error 2
make: *** [all] Error 2

Are there any other changes that would be needed to get it to run as far as your example logs show?

it's very likely that i won't be able to help but i'd love to try - with that in mind, if you don't have time for this I understand!

@dirkhh
Copy link
Collaborator Author

dirkhh commented Nov 9, 2019

That function is added here 3d52b5a specifically https://github.com/Subsurface-divelog/subsurface/blob/abda7296c9102a65d00761c777539e11717515a7/packaging/android/patches/libusb-android.patch#L42

So my guess would be that the patch failed earlier, or something went wrong with making sure that you actually use the updated libusb

@StephenGoodall
Copy link
Contributor

thanks @dirkhh , it was the patch - i think a previous build run from master branch had already patched in libusb_set_android_open_callback so the check in the build script thought it didn't need to patch.
When i wiped out the directories and ran the build script again, it did the patch and completed this time 👍

@StephenGoodall
Copy link
Contributor

I tried adding a few bits to see how far i could get (spoiler - not very)
I found this short example program - not sure if it might be useful or not as an example (I think you already have this bit working anyway) https://wiki.termux.com/wiki/Termux-usb

I thought I'd try and put in some of the same calls that you use when you run the get_usb_fd function, in the hope that i could get the libusb device "object", then use that to populate the values for the enumerate call - as you mentioned thinking that you need to get the device address.
It seems to hang on the init call though.

After:

dd.idProduct = productId;

I put in:

QAndroidJniObject usbDeviceConnection = usbManager.callObjectMethod("openDevice", "(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;", device.object());

jint fileDes = usbDeviceConnection.callMethod<jint>("getFileDescriptor","()I");
LOG(QString("File Descriptor - %1").arg(fileDes));

libusb_context *context;
libusb_device_handle *handle;
libusb_device *libusbdevice;
struct libusb_device_descriptor desc;

LOG("starting init");
int initResult = libusb_init(&context);
LOG("init done");
if (initResult > 0) {
  // Some error occurred while initialising the device. Exit.
  LOG("failed init");
  LOG(QString("Error Code: %1").arg(initResult));
  return -1;
}
libusb_wrap_sys_device(context, (intptr_t) fileDes, &handle);
LOG("wrap done");
libusbdevice = libusb_get_device(handle);
LOG("device got");
libusb_get_device_descriptor(libusbdevice, &desc);
LOG("descriptor got");
LOG(QString().sprintf("Vendor ID: %04x\n",desc.idVendor));

I don't think is particularly helpful as it's not working, but thought it might be a different way of getting the required info.........even if getting the device just so we can get the details needed to get the device later seems odd

@dirkhh
Copy link
Collaborator Author

dirkhh commented Dec 5, 2019

@StephenGoodall - I am so hoping that you are still looking at this. Or someone else (nudge @glance- hint). I SO desperately want someone to figure this out and fix it...

@StephenGoodall
Copy link
Contributor

@dirkhh I've been on holiday and catching up on work but it's still in my thoughts! When I get more time I'll have another try at poking it to see if I can get any further..... I don't understand c++ though so I'm not too hopeful, I can be quite persistent though so I'll keep plugging away!

@dirkhh
Copy link
Collaborator Author

dirkhh commented Dec 5, 2019

I would be surprised if understanding of C++ was critical here. It's the understanding of the interaction of two C libraries with the serial device that we get back from Java.
The current code fails because the things that it gets back from the Java call doesn't behave as we expect it to behave... no C++ magic involved :-)

@dirkhh
Copy link
Collaborator Author

dirkhh commented Mar 8, 2020

this is no longer useful. closing

@dirkhh dirkhh closed this Mar 8, 2020
@dirkhh dirkhh deleted the libusbAndroid branch April 13, 2020 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants